|
6 | 6 | * Copyright (c) 2007-2008, PostgreSQL Global Development Group
|
7 | 7 | *
|
8 | 8 | * IDENTIFICATION
|
9 |
| - * $PostgreSQL: pgsql/src/backend/utils/adt/uuid.c,v 1.7 2008/01/01 20:31:21 tgl Exp $ |
| 9 | + * $PostgreSQL: pgsql/src/backend/utils/adt/uuid.c,v 1.8 2008/11/03 22:14:40 petere Exp $ |
10 | 10 | *
|
11 | 11 | *-------------------------------------------------------------------------
|
12 | 12 | */
|
@@ -74,60 +74,51 @@ uuid_out(PG_FUNCTION_ARGS)
|
74 | 74 | }
|
75 | 75 |
|
76 | 76 | /*
|
77 |
| - * We allow UUIDs in three input formats: 8x-4x-4x-4x-12x, |
78 |
| - * {8x-4x-4x-4x-12x}, and 32x, where "nx" means n hexadecimal digits |
79 |
| - * (only the first format is used for output). We convert the first |
80 |
| - * two formats into the latter format before further processing. |
| 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 | 81 | */
|
82 | 82 | static void
|
83 | 83 | string_to_uuid(const char *source, pg_uuid_t *uuid)
|
84 | 84 | {
|
85 |
| - char hex_buf[32]; /* not NUL terminated */ |
86 |
| - int i; |
87 |
| - int src_len; |
88 |
| - |
89 |
| - src_len = strlen(source); |
90 |
| - if (src_len != 32 && src_len != 36 && src_len != 38) |
91 |
| - goto syntax_error; |
| 85 | + const char *src = source; |
| 86 | + int i, braces = 0; |
92 | 87 |
|
93 |
| - if (src_len == 32) |
94 |
| - memcpy(hex_buf, source, src_len); |
95 |
| - else |
| 88 | + if (src[0] == '{') |
96 | 89 | {
|
97 |
| - const char *str = source; |
98 |
| - |
99 |
| - if (src_len == 38) |
100 |
| - { |
101 |
| - if (str[0] != '{' || str[37] != '}') |
102 |
| - goto syntax_error; |
103 |
| - |
104 |
| - str++; /* skip the first character */ |
105 |
| - } |
106 |
| - |
107 |
| - if (str[8] != '-' || str[13] != '-' || |
108 |
| - str[18] != '-' || str[23] != '-') |
109 |
| - goto syntax_error; |
110 |
| - |
111 |
| - memcpy(hex_buf, str, 8); |
112 |
| - memcpy(hex_buf + 8, str + 9, 4); |
113 |
| - memcpy(hex_buf + 12, str + 14, 4); |
114 |
| - memcpy(hex_buf + 16, str + 19, 4); |
115 |
| - memcpy(hex_buf + 20, str + 24, 12); |
| 90 | + ++src; |
| 91 | + braces = 1; |
116 | 92 | }
|
117 | 93 |
|
118 | 94 | for (i = 0; i < UUID_LEN; i++)
|
119 | 95 | {
|
120 | 96 | char str_buf[3];
|
121 | 97 |
|
122 |
| - memcpy(str_buf, &hex_buf[i * 2], 2); |
| 98 | + if (src[0] == '\0' || src[1] == '\0') |
| 99 | + goto syntax_error; |
| 100 | + memcpy(str_buf, src, 2); |
123 | 101 | if (!isxdigit((unsigned char) str_buf[0]) ||
|
124 | 102 | !isxdigit((unsigned char) str_buf[1]))
|
125 | 103 | goto syntax_error;
|
126 | 104 |
|
127 | 105 | str_buf[2] = '\0';
|
128 | 106 | uuid->data[i] = (unsigned char) strtoul(str_buf, NULL, 16);
|
| 107 | + src += 2; |
| 108 | + if (src[0] == '-' && (i % 2) == 1 && i < UUID_LEN - 1) |
| 109 | + src++; |
129 | 110 | }
|
130 | 111 |
|
| 112 | + if (braces) |
| 113 | + { |
| 114 | + if (*src!= '}') |
| 115 | + goto syntax_error; |
| 116 | + ++src; |
| 117 | + } |
| 118 | + |
| 119 | + if (*src != '\0') |
| 120 | + goto syntax_error; |
| 121 | + |
131 | 122 | return;
|
132 | 123 |
|
133 | 124 | syntax_error:
|
|
0 commit comments