11/*
22 *PostgreSQL type definitions for MAC addresses.
33 *
4- *$Header: /cvsroot/pgsql/src/backend/utils/adt/mac.c,v 1.20 2001/03/22 03:59:51 momjian Exp $
4+ *$Header: /cvsroot/pgsql/src/backend/utils/adt/mac.c,v 1.21 2001/08/21 21:23:21 tgl Exp $
55 */
66
77#include "postgres.h"
@@ -28,40 +28,32 @@ Datum
2828macaddr_in (PG_FUNCTION_ARGS )
2929{
3030char * str = PG_GETARG_CSTRING (0 );
31+ macaddr * result ;
3132int a ,
3233b ,
3334c ,
3435d ,
3536e ,
3637f ;
37- macaddr * result ;
3838int count ;
3939
40- if (strlen (str )> 0 )
41- {
42- count = sscanf (str ,"%x:%x:%x:%x:%x:%x" ,& a ,& b ,& c ,& d ,& e ,& f );
43- if (count != 6 )
44- count = sscanf (str ,"%x-%x-%x-%x-%x-%x" ,& a ,& b ,& c ,& d ,& e ,& f );
45- if (count != 6 )
46- count = sscanf (str ,"%2x%2x%2x:%2x%2x%2x" ,& a ,& b ,& c ,& d ,& e ,& f );
47- if (count != 6 )
48- count = sscanf (str ,"%2x%2x%2x-%2x%2x%2x" ,& a ,& b ,& c ,& d ,& e ,& f );
49- if (count != 6 )
50- count = sscanf (str ,"%2x%2x.%2x%2x.%2x%2x" ,& a ,& b ,& c ,& d ,& e ,& f );
51-
52- if (count != 6 )
53- elog (ERROR ,"macaddr_in: error in parsing \"%s\"" ,str );
54-
55- if ((a < 0 )|| (a > 255 )|| (b < 0 )|| (b > 255 )||
56- (c < 0 )|| (c > 255 )|| (d < 0 )|| (d > 255 )||
57- (e < 0 )|| (e > 255 )|| (f < 0 )|| (f > 255 ))
58- elog (ERROR ,"macaddr_in: illegal address \"%s\"" ,str );
59- }
60- else
61- {
62- a = b = c = d = e = f = 0 ;/* special case for missing
63- * address */
64- }
40+ count = sscanf (str ,"%x:%x:%x:%x:%x:%x" ,& a ,& b ,& c ,& d ,& e ,& f );
41+ if (count != 6 )
42+ count = sscanf (str ,"%x-%x-%x-%x-%x-%x" ,& a ,& b ,& c ,& d ,& e ,& f );
43+ if (count != 6 )
44+ count = sscanf (str ,"%2x%2x%2x:%2x%2x%2x" ,& a ,& b ,& c ,& d ,& e ,& f );
45+ if (count != 6 )
46+ count = sscanf (str ,"%2x%2x%2x-%2x%2x%2x" ,& a ,& b ,& c ,& d ,& e ,& f );
47+ if (count != 6 )
48+ count = sscanf (str ,"%2x%2x.%2x%2x.%2x%2x" ,& a ,& b ,& c ,& d ,& e ,& f );
49+
50+ if (count != 6 )
51+ elog (ERROR ,"macaddr_in: error in parsing \"%s\"" ,str );
52+
53+ if ((a < 0 )|| (a > 255 )|| (b < 0 )|| (b > 255 )||
54+ (c < 0 )|| (c > 255 )|| (d < 0 )|| (d > 255 )||
55+ (e < 0 )|| (e > 255 )|| (f < 0 )|| (f > 255 ))
56+ elog (ERROR ,"macaddr_in: illegal address \"%s\"" ,str );
6557
6658result = (macaddr * )palloc (sizeof (macaddr ));
6759
@@ -87,20 +79,13 @@ macaddr_out(PG_FUNCTION_ARGS)
8779
8880result = (char * )palloc (32 );
8981
90- if ((hibits (addr )> 0 )|| (lobits (addr )> 0 ))
91- {
92- sprintf (result ,"%02x:%02x:%02x:%02x:%02x:%02x" ,
93- addr -> a ,addr -> b ,addr -> c ,addr -> d ,addr -> e ,addr -> f );
94- }
95- else
96- {
97- result [0 ]= '\0' ;/* special case for missing address */
98- }
82+ sprintf (result ,"%02x:%02x:%02x:%02x:%02x:%02x" ,
83+ addr -> a ,addr -> b ,addr -> c ,addr -> d ,addr -> e ,addr -> f );
9984
10085PG_RETURN_CSTRING (result );
10186}
10287
103- /* macaddr_text()
88+ /*
10489 * Convert macaddr to text data type.
10590 */
10691
@@ -127,23 +112,23 @@ macaddr_text(PG_FUNCTION_ARGS)
127112PG_RETURN_TEXT_P (result );
128113}
129114
130- /* text_macaddr()
115+ /*
131116 * Convert text to macaddr data type.
132117 */
133118
134119Datum
135120text_macaddr (PG_FUNCTION_ARGS )
136121{
137- Datum result ;
138122text * addr = PG_GETARG_TEXT_P (0 );
123+ Datum result ;
139124char str [18 ];
140125int len ;
141126
142127len = (VARSIZE (addr )- VARHDRSZ );
143128if (len >=18 )
144129elog (ERROR ,"Text is too long to convert to MAC address" );
145130
146- memmove (str ,VARDATA (addr ),len );
131+ memcpy (str ,VARDATA (addr ),len );
147132* (str + len )= '\0' ;
148133
149134result = DirectFunctionCall1 (macaddr_in ,CStringGetDatum (str ));
@@ -255,8 +240,8 @@ hashmacaddr(PG_FUNCTION_ARGS)
255240Datum
256241macaddr_trunc (PG_FUNCTION_ARGS )
257242{
258- macaddr * result ;
259243macaddr * addr = PG_GETARG_MACADDR_P (0 );
244+ macaddr * result ;
260245
261246result = (macaddr * )palloc (sizeof (macaddr ));
262247