]> granicus.if.org Git - postgresql/blob - src/backend/utils/adt/uuid.c
Update copyright for 2014
[postgresql] / src / backend / utils / adt / uuid.c
1 /*-------------------------------------------------------------------------
2  *
3  * uuid.c
4  *        Functions for the built-in type "uuid".
5  *
6  * Copyright (c) 2007-2014, PostgreSQL Global Development Group
7  *
8  * IDENTIFICATION
9  *        src/backend/utils/adt/uuid.c
10  *
11  *-------------------------------------------------------------------------
12  */
13
14 #include "postgres.h"
15
16 #include "access/hash.h"
17 #include "libpq/pqformat.h"
18 #include "utils/builtins.h"
19 #include "utils/uuid.h"
20
21 /* uuid size in bytes */
22 #define UUID_LEN 16
23
24 /* pg_uuid_t is declared to be struct pg_uuid_t in uuid.h */
25 struct pg_uuid_t
26 {
27         unsigned char data[UUID_LEN];
28 };
29
30 static void string_to_uuid(const char *source, pg_uuid_t *uuid);
31 static int      uuid_internal_cmp(const pg_uuid_t *arg1, const pg_uuid_t *arg2);
32
33 Datum
34 uuid_in(PG_FUNCTION_ARGS)
35 {
36         char       *uuid_str = PG_GETARG_CSTRING(0);
37         pg_uuid_t  *uuid;
38
39         uuid = (pg_uuid_t *) palloc(sizeof(*uuid));
40         string_to_uuid(uuid_str, uuid);
41         PG_RETURN_UUID_P(uuid);
42 }
43
44 Datum
45 uuid_out(PG_FUNCTION_ARGS)
46 {
47         pg_uuid_t  *uuid = PG_GETARG_UUID_P(0);
48         static const char hex_chars[] = "0123456789abcdef";
49         StringInfoData buf;
50         int                     i;
51
52         initStringInfo(&buf);
53         for (i = 0; i < UUID_LEN; i++)
54         {
55                 int                     hi;
56                 int                     lo;
57
58                 /*
59                  * We print uuid values as a string of 8, 4, 4, 4, and then 12
60                  * hexadecimal characters, with each group is separated by a hyphen
61                  * ("-"). Therefore, add the hyphens at the appropriate places here.
62                  */
63                 if (i == 4 || i == 6 || i == 8 || i == 10)
64                         appendStringInfoChar(&buf, '-');
65
66                 hi = uuid->data[i] >> 4;
67                 lo = uuid->data[i] & 0x0F;
68
69                 appendStringInfoChar(&buf, hex_chars[hi]);
70                 appendStringInfoChar(&buf, hex_chars[lo]);
71         }
72
73         PG_RETURN_CSTRING(buf.data);
74 }
75
76 /*
77  * We allow UUIDs as a series of 32 hexadecimal digits with an optional dash
78  * after each group of 4 hexadecimal digits, and optionally surrounded by {}.
79  * (The canonical format 8x-4x-4x-4x-12x, where "nx" means n hexadecimal
80  * digits, is the only one used for output.)
81  */
82 static void
83 string_to_uuid(const char *source, pg_uuid_t *uuid)
84 {
85         const char *src = source;
86         bool            braces = false;
87         int                     i;
88
89         if (src[0] == '{')
90         {
91                 src++;
92                 braces = true;
93         }
94
95         for (i = 0; i < UUID_LEN; i++)
96         {
97                 char            str_buf[3];
98
99                 if (src[0] == '\0' || src[1] == '\0')
100                         goto syntax_error;
101                 memcpy(str_buf, src, 2);
102                 if (!isxdigit((unsigned char) str_buf[0]) ||
103                         !isxdigit((unsigned char) str_buf[1]))
104                         goto syntax_error;
105
106                 str_buf[2] = '\0';
107                 uuid->data[i] = (unsigned char) strtoul(str_buf, NULL, 16);
108                 src += 2;
109                 if (src[0] == '-' && (i % 2) == 1 && i < UUID_LEN - 1)
110                         src++;
111         }
112
113         if (braces)
114         {
115                 if (*src != '}')
116                         goto syntax_error;
117                 src++;
118         }
119
120         if (*src != '\0')
121                 goto syntax_error;
122
123         return;
124
125 syntax_error:
126         ereport(ERROR,
127                         (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
128                          errmsg("invalid input syntax for uuid: \"%s\"",
129                                         source)));
130 }
131
132 Datum
133 uuid_recv(PG_FUNCTION_ARGS)
134 {
135         StringInfo      buffer = (StringInfo) PG_GETARG_POINTER(0);
136         pg_uuid_t  *uuid;
137
138         uuid = (pg_uuid_t *) palloc(UUID_LEN);
139         memcpy(uuid->data, pq_getmsgbytes(buffer, UUID_LEN), UUID_LEN);
140         PG_RETURN_POINTER(uuid);
141 }
142
143 Datum
144 uuid_send(PG_FUNCTION_ARGS)
145 {
146         pg_uuid_t  *uuid = PG_GETARG_UUID_P(0);
147         StringInfoData buffer;
148
149         pq_begintypsend(&buffer);
150         pq_sendbytes(&buffer, (char *) uuid->data, UUID_LEN);
151         PG_RETURN_BYTEA_P(pq_endtypsend(&buffer));
152 }
153
154 /* internal uuid compare function */
155 static int
156 uuid_internal_cmp(const pg_uuid_t *arg1, const pg_uuid_t *arg2)
157 {
158         return memcmp(arg1->data, arg2->data, UUID_LEN);
159 }
160
161 Datum
162 uuid_lt(PG_FUNCTION_ARGS)
163 {
164         pg_uuid_t  *arg1 = PG_GETARG_UUID_P(0);
165         pg_uuid_t  *arg2 = PG_GETARG_UUID_P(1);
166
167         PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) < 0);
168 }
169
170 Datum
171 uuid_le(PG_FUNCTION_ARGS)
172 {
173         pg_uuid_t  *arg1 = PG_GETARG_UUID_P(0);
174         pg_uuid_t  *arg2 = PG_GETARG_UUID_P(1);
175
176         PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) <= 0);
177 }
178
179 Datum
180 uuid_eq(PG_FUNCTION_ARGS)
181 {
182         pg_uuid_t  *arg1 = PG_GETARG_UUID_P(0);
183         pg_uuid_t  *arg2 = PG_GETARG_UUID_P(1);
184
185         PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) == 0);
186 }
187
188 Datum
189 uuid_ge(PG_FUNCTION_ARGS)
190 {
191         pg_uuid_t  *arg1 = PG_GETARG_UUID_P(0);
192         pg_uuid_t  *arg2 = PG_GETARG_UUID_P(1);
193
194         PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) >= 0);
195 }
196
197 Datum
198 uuid_gt(PG_FUNCTION_ARGS)
199 {
200         pg_uuid_t  *arg1 = PG_GETARG_UUID_P(0);
201         pg_uuid_t  *arg2 = PG_GETARG_UUID_P(1);
202
203         PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) > 0);
204 }
205
206 Datum
207 uuid_ne(PG_FUNCTION_ARGS)
208 {
209         pg_uuid_t  *arg1 = PG_GETARG_UUID_P(0);
210         pg_uuid_t  *arg2 = PG_GETARG_UUID_P(1);
211
212         PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) != 0);
213 }
214
215 /* handler for btree index operator */
216 Datum
217 uuid_cmp(PG_FUNCTION_ARGS)
218 {
219         pg_uuid_t  *arg1 = PG_GETARG_UUID_P(0);
220         pg_uuid_t  *arg2 = PG_GETARG_UUID_P(1);
221
222         PG_RETURN_INT32(uuid_internal_cmp(arg1, arg2));
223 }
224
225 /* hash index support */
226 Datum
227 uuid_hash(PG_FUNCTION_ARGS)
228 {
229         pg_uuid_t  *key = PG_GETARG_UUID_P(0);
230
231         return hash_any(key->data, UUID_LEN);
232 }