|
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 UUIDsin three input formats: 8x-4x-4x-4x-12x, |
78 |
| - *{8x-4x-4x-4x-12x}, and 32x, where "nx" means nhexadecimal digits |
79 |
| - * (only the firstformatis used for output). We convert the first |
80 |
| - *two formats intothelatter format before further processing. |
| 77 | + * We allow UUIDsas a series of 32 hexadecimal digits with an optional dash |
| 78 | + *after each group of 4hexadecimal digits, and optionally surrounded by {}. |
| 79 | + * (The canonicalformat8x-4x-4x-4x-12x, where "nx" means n hexadecimal |
| 80 | + *digits, istheonly one used for output.) |
81 | 81 | */
|
82 | 82 | staticvoid
|
83 | 83 | string_to_uuid(constchar*source,pg_uuid_t*uuid)
|
84 | 84 | {
|
85 |
| -charhex_buf[32];/* not NUL terminated */ |
86 |
| -inti; |
87 |
| -intsrc_len; |
88 |
| - |
89 |
| -src_len=strlen(source); |
90 |
| -if (src_len!=32&&src_len!=36&&src_len!=38) |
91 |
| -gotosyntax_error; |
| 85 | +constchar*src=source; |
| 86 | +inti,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 |
| -constchar*str=source; |
98 |
| - |
99 |
| -if (src_len==38) |
100 |
| -{ |
101 |
| -if (str[0]!='{'||str[37]!='}') |
102 |
| -gotosyntax_error; |
103 |
| - |
104 |
| -str++;/* skip the first character */ |
105 |
| -} |
106 |
| - |
107 |
| -if (str[8]!='-'||str[13]!='-'|| |
108 |
| -str[18]!='-'||str[23]!='-') |
109 |
| -gotosyntax_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 | charstr_buf[3];
|
121 | 97 |
|
122 |
| -memcpy(str_buf,&hex_buf[i*2],2); |
| 98 | +if (src[0]=='\0'||src[1]=='\0') |
| 99 | +gotosyntax_error; |
| 100 | +memcpy(str_buf,src,2); |
123 | 101 | if (!isxdigit((unsignedchar)str_buf[0])||
|
124 | 102 | !isxdigit((unsignedchar)str_buf[1]))
|
125 | 103 | gotosyntax_error;
|
126 | 104 |
|
127 | 105 | str_buf[2]='\0';
|
128 | 106 | uuid->data[i]= (unsignedchar)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 | +gotosyntax_error; |
| 116 | +++src; |
| 117 | +} |
| 118 | + |
| 119 | +if (*src!='\0') |
| 120 | +gotosyntax_error; |
| 121 | + |
131 | 122 | return;
|
132 | 123 |
|
133 | 124 | syntax_error:
|
|