|
| 1 | +/*------------------------------------------------------------------------- |
| 2 | + * |
| 3 | + * uuid.c |
| 4 | + * Functions for the built-in type "uuid". |
| 5 | + * |
| 6 | + * Copyright (c) 2007, PostgreSQL Global Development Group |
| 7 | + * |
| 8 | + * IDENTIFICATION |
| 9 | + * $PostgreSQL: pgsql/src/backend/utils/adt/uuid.c,v 1.1 2007/01/28 16:16:52 neilc Exp $ |
| 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 | +/* Accepted GUID formats */ |
| 22 | + |
| 23 | +/* UUID_FMT1 is the default output format */ |
| 24 | +#define UUID_FMT1 "%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx" |
| 25 | +#define UUID_FMT2 "{%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx}" |
| 26 | +#define UUID_FMT3 "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx" |
| 27 | + |
| 28 | +/* UUIDs are accepted in any of the following textual input formats. */ |
| 29 | +#define UUID_CHK_FMT1 "00000000-0000-0000-0000-000000000000" |
| 30 | +#define UUID_CHK_FMT2 "{00000000-0000-0000-0000-000000000000}" |
| 31 | +#define UUID_CHK_FMT3 "00000000000000000000000000000000" |
| 32 | + |
| 33 | +#define PRINT_SIZE 40 |
| 34 | + |
| 35 | +/* uuid size in bytes */ |
| 36 | +#define UUID_LEN 16 |
| 37 | + |
| 38 | +/* The uuid_t type is declared as struct uuid_t in uuid.h */ |
| 39 | +struct uuid_t |
| 40 | +{ |
| 41 | + char data[UUID_LEN]; |
| 42 | +}; |
| 43 | + |
| 44 | +static void uuid_data_from_string(const char *source, unsigned char *data); |
| 45 | +static void string_from_uuid_data(const char *fmt, const char *data, char *uuid_str); |
| 46 | +static bool parse_uuid_string(const char *fmt, const char *chk_fmt, |
| 47 | + const char *source, unsigned char *data); |
| 48 | +static bool is_valid_format(const char *source, const char *fmt); |
| 49 | +static int32 uuid_internal_cmp(uuid_t *arg1, uuid_t *arg2); |
| 50 | + |
| 51 | +Datum |
| 52 | +uuid_in(PG_FUNCTION_ARGS) |
| 53 | +{ |
| 54 | + char *uuid_str = PG_GETARG_CSTRING(0); |
| 55 | + uuid_t *uuid; |
| 56 | + uint8 data[UUID_LEN]; |
| 57 | + |
| 58 | + uuid_data_from_string(uuid_str, data); |
| 59 | + uuid = (uuid_t *) palloc(sizeof(uuid_t)); |
| 60 | + memcpy(uuid->data, data, UUID_LEN); |
| 61 | + PG_RETURN_UUID_P(uuid); |
| 62 | +} |
| 63 | + |
| 64 | +Datum |
| 65 | +uuid_out(PG_FUNCTION_ARGS) |
| 66 | +{ |
| 67 | + uuid_t *uuid = (uuid_t *) PG_GETARG_POINTER(0); |
| 68 | + char *uuid_str; |
| 69 | + |
| 70 | + uuid_str = (char *) palloc(PRINT_SIZE); |
| 71 | + string_from_uuid_data(UUID_FMT1, uuid->data, uuid_str); |
| 72 | + PG_RETURN_CSTRING(uuid_str); |
| 73 | +} |
| 74 | + |
| 75 | +/* string to uuid convertor by various format types */ |
| 76 | +static void |
| 77 | +uuid_data_from_string(const char *source, unsigned char *data) |
| 78 | +{ |
| 79 | + if (!parse_uuid_string(UUID_FMT1, UUID_CHK_FMT1, source, data) && |
| 80 | + !parse_uuid_string(UUID_FMT2, UUID_CHK_FMT2, source, data) && |
| 81 | + !parse_uuid_string(UUID_FMT3, UUID_CHK_FMT3, source, data)) |
| 82 | + { |
| 83 | + ereport(ERROR, |
| 84 | + (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), |
| 85 | + errmsg("invalid input syntax for uuid: \"%s\"", |
| 86 | + source))); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +/* check the validity of a uuid string by a given format */ |
| 91 | +static bool |
| 92 | +is_valid_format(const char *source, const char *fmt) |
| 93 | +{ |
| 94 | + int i; |
| 95 | + int fmtlen = strlen(fmt); |
| 96 | + |
| 97 | + /* check length first */ |
| 98 | + if (fmtlen != strlen(source)) |
| 99 | + return false; |
| 100 | + |
| 101 | + for (i = 0; i < fmtlen; i++) |
| 102 | + { |
| 103 | + int fc; |
| 104 | + int sc; |
| 105 | + bool valid_chr; |
| 106 | + |
| 107 | + fc = fmt[i]; |
| 108 | + sc = source[i]; |
| 109 | + |
| 110 | + /* false if format chr is { or - and source is not */ |
| 111 | + if (fc != '0' && fc != sc) |
| 112 | + return false; |
| 113 | + |
| 114 | + /* check for valid char in source */ |
| 115 | + valid_chr = (sc >= '0' && sc <= '9') || |
| 116 | + (sc >= 'a' && sc <= 'f' ) || |
| 117 | + (sc >= 'A' && sc <= 'F' ); |
| 118 | + |
| 119 | + if (fc == '0' && !valid_chr) |
| 120 | + return false; |
| 121 | + } |
| 122 | + |
| 123 | + return true; |
| 124 | +} |
| 125 | + |
| 126 | +/* parse the uuid string to a format and return true if okay */ |
| 127 | +static bool |
| 128 | +parse_uuid_string(const char *fmt, const char *chk_fmt, |
| 129 | + const char *source, unsigned char *data) |
| 130 | +{ |
| 131 | + int result = sscanf(source, fmt, |
| 132 | + &data[0], &data[1], &data[2], &data[3], &data[4], |
| 133 | + &data[5], &data[6], &data[7], &data[8], &data[9], |
| 134 | + &data[10], &data[11], &data[12], &data[13], |
| 135 | + &data[14], &data[15]); |
| 136 | + |
| 137 | + return (result == 16) && is_valid_format(source, chk_fmt); |
| 138 | +} |
| 139 | + |
| 140 | +/* create a string representation of the uuid */ |
| 141 | +static void |
| 142 | +string_from_uuid_data(const char *fmt, const char *data, char *uuid_str) |
| 143 | +{ |
| 144 | + snprintf(uuid_str, PRINT_SIZE, fmt, |
| 145 | + data[0], data[1], data[2], data[3], data[4], |
| 146 | + data[5], data[6], data[7], data[8], data[9], |
| 147 | + data[10], data[11], data[12], data[13], |
| 148 | + data[14], data[15]); |
| 149 | +} |
| 150 | + |
| 151 | +Datum |
| 152 | +uuid_recv(PG_FUNCTION_ARGS) |
| 153 | +{ |
| 154 | + StringInfo buffer = (StringInfo) PG_GETARG_POINTER(0); |
| 155 | + uuid_t *uuid; |
| 156 | + |
| 157 | + uuid = (uuid_t *) palloc(UUID_LEN); |
| 158 | + memcpy(uuid->data, pq_getmsgbytes(buffer, UUID_LEN), UUID_LEN); |
| 159 | + PG_RETURN_POINTER(uuid); |
| 160 | +} |
| 161 | + |
| 162 | +Datum |
| 163 | +uuid_send(PG_FUNCTION_ARGS) |
| 164 | +{ |
| 165 | + uuid_t *uuid = PG_GETARG_UUID_P(0); |
| 166 | + StringInfoData buffer; |
| 167 | + |
| 168 | + pq_begintypsend(&buffer); |
| 169 | + pq_sendbytes(&buffer, uuid->data, UUID_LEN); |
| 170 | + PG_RETURN_BYTEA_P(pq_endtypsend(&buffer)); |
| 171 | +} |
| 172 | + |
| 173 | +/* internal uuid compare function */ |
| 174 | +static int32 |
| 175 | +uuid_internal_cmp(uuid_t *arg1, uuid_t *arg2) |
| 176 | +{ |
| 177 | + return memcmp(arg1->data, arg2->data, UUID_LEN); |
| 178 | +} |
| 179 | + |
| 180 | +Datum |
| 181 | +uuid_lt(PG_FUNCTION_ARGS) |
| 182 | +{ |
| 183 | + uuid_t *arg1 = PG_GETARG_UUID_P(0); |
| 184 | + uuid_t *arg2 = PG_GETARG_UUID_P(1); |
| 185 | + |
| 186 | + PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) < 0); |
| 187 | +} |
| 188 | + |
| 189 | +Datum |
| 190 | +uuid_le(PG_FUNCTION_ARGS) |
| 191 | +{ |
| 192 | + uuid_t *arg1 = PG_GETARG_UUID_P(0); |
| 193 | + uuid_t *arg2 = PG_GETARG_UUID_P(1); |
| 194 | + |
| 195 | + PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) <= 0); |
| 196 | +} |
| 197 | + |
| 198 | +Datum |
| 199 | +uuid_eq(PG_FUNCTION_ARGS) |
| 200 | +{ |
| 201 | + uuid_t *arg1 = PG_GETARG_UUID_P(0); |
| 202 | + uuid_t *arg2 = PG_GETARG_UUID_P(1); |
| 203 | + |
| 204 | + PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) == 0); |
| 205 | +} |
| 206 | + |
| 207 | +Datum |
| 208 | +uuid_ge(PG_FUNCTION_ARGS) |
| 209 | +{ |
| 210 | + uuid_t *arg1 = PG_GETARG_UUID_P(0); |
| 211 | + uuid_t *arg2 = PG_GETARG_UUID_P(1); |
| 212 | + |
| 213 | + PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) >= 0); |
| 214 | +} |
| 215 | + |
| 216 | +Datum |
| 217 | +uuid_gt(PG_FUNCTION_ARGS) |
| 218 | +{ |
| 219 | + uuid_t *arg1 = PG_GETARG_UUID_P(0); |
| 220 | + uuid_t *arg2 = PG_GETARG_UUID_P(1); |
| 221 | + |
| 222 | + PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) > 0); |
| 223 | +} |
| 224 | + |
| 225 | +Datum |
| 226 | +uuid_ne(PG_FUNCTION_ARGS) |
| 227 | +{ |
| 228 | + uuid_t *arg1 = PG_GETARG_UUID_P(0); |
| 229 | + uuid_t *arg2 = PG_GETARG_UUID_P(1); |
| 230 | + |
| 231 | + PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) != 0); |
| 232 | +} |
| 233 | + |
| 234 | +/* handler for btree index operator */ |
| 235 | +Datum |
| 236 | +uuid_cmp(PG_FUNCTION_ARGS) |
| 237 | +{ |
| 238 | + uuid_t *arg1 = PG_GETARG_UUID_P(0); |
| 239 | + uuid_t *arg2 = PG_GETARG_UUID_P(1); |
| 240 | + |
| 241 | + PG_RETURN_INT32(uuid_internal_cmp(arg1, arg2)); |
| 242 | +} |
| 243 | + |
| 244 | +/* hash index support */ |
| 245 | +Datum |
| 246 | +uuid_hash(PG_FUNCTION_ARGS) |
| 247 | +{ |
| 248 | + uuid_t *key = PG_GETARG_UUID_P(0); |
| 249 | + return hash_any((unsigned char *) key, sizeof(uuid_t)); |
| 250 | +} |
| 251 | + |
| 252 | +/* cast text to uuid */ |
| 253 | +Datum |
| 254 | +text_uuid(PG_FUNCTION_ARGS) |
| 255 | +{ |
| 256 | + text *input = PG_GETARG_TEXT_P(0); |
| 257 | + int length; |
| 258 | + char *str; |
| 259 | + Datum result; |
| 260 | + |
| 261 | + length = VARSIZE(input) - VARHDRSZ; |
| 262 | + str = palloc(length + 1); |
| 263 | + memcpy(str, VARDATA(input), length); |
| 264 | + *(str + length) = '\0'; |
| 265 | + |
| 266 | + result = DirectFunctionCall1(uuid_in, CStringGetDatum(str)); |
| 267 | + pfree(str); |
| 268 | + PG_RETURN_DATUM(result); |
| 269 | +} |
| 270 | + |
| 271 | +/* cast uuid to text */ |
| 272 | +Datum |
| 273 | +uuid_text(PG_FUNCTION_ARGS) |
| 274 | +{ |
| 275 | + uuid_t *uuid = PG_GETARG_UUID_P(0); |
| 276 | + Datum uuid_str = DirectFunctionCall1(uuid_out, UUIDPGetDatum(uuid)); |
| 277 | + |
| 278 | + PG_RETURN_DATUM(DirectFunctionCall1(textin, uuid_str)); |
| 279 | +} |
0 commit comments