1
1
/*
2
2
*PostgreSQL type definitions for MAC addresses.
3
3
*
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 $
5
5
*/
6
6
7
7
#include "postgres.h"
@@ -28,40 +28,32 @@ Datum
28
28
macaddr_in (PG_FUNCTION_ARGS )
29
29
{
30
30
char * str = PG_GETARG_CSTRING (0 );
31
+ macaddr * result ;
31
32
int a ,
32
33
b ,
33
34
c ,
34
35
d ,
35
36
e ,
36
37
f ;
37
- macaddr * result ;
38
38
int count ;
39
39
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 );
65
57
66
58
result = (macaddr * )palloc (sizeof (macaddr ));
67
59
@@ -87,20 +79,13 @@ macaddr_out(PG_FUNCTION_ARGS)
87
79
88
80
result = (char * )palloc (32 );
89
81
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 );
99
84
100
85
PG_RETURN_CSTRING (result );
101
86
}
102
87
103
- /* macaddr_text()
88
+ /*
104
89
* Convert macaddr to text data type.
105
90
*/
106
91
@@ -127,23 +112,23 @@ macaddr_text(PG_FUNCTION_ARGS)
127
112
PG_RETURN_TEXT_P (result );
128
113
}
129
114
130
- /* text_macaddr()
115
+ /*
131
116
* Convert text to macaddr data type.
132
117
*/
133
118
134
119
Datum
135
120
text_macaddr (PG_FUNCTION_ARGS )
136
121
{
137
- Datum result ;
138
122
text * addr = PG_GETARG_TEXT_P (0 );
123
+ Datum result ;
139
124
char str [18 ];
140
125
int len ;
141
126
142
127
len = (VARSIZE (addr )- VARHDRSZ );
143
128
if (len >=18 )
144
129
elog (ERROR ,"Text is too long to convert to MAC address" );
145
130
146
- memmove (str ,VARDATA (addr ),len );
131
+ memcpy (str ,VARDATA (addr ),len );
147
132
* (str + len )= '\0' ;
148
133
149
134
result = DirectFunctionCall1 (macaddr_in ,CStringGetDatum (str ));
@@ -255,8 +240,8 @@ hashmacaddr(PG_FUNCTION_ARGS)
255
240
Datum
256
241
macaddr_trunc (PG_FUNCTION_ARGS )
257
242
{
258
- macaddr * result ;
259
243
macaddr * addr = PG_GETARG_MACADDR_P (0 );
244
+ macaddr * result ;
260
245
261
246
result = (macaddr * )palloc (sizeof (macaddr ));
262
247