253{
254 char strbuf[40];
255
256 switch (v)
257 {
258 case 0:
260 break;
261
262 case 1:
263 {
264#ifdef HAVE_UUID_E2FS
265 uuid_t uu;
266
267 uuid_generate_time(uu);
268 uuid_unparse(uu, strbuf);
269
270
271
272
273
274
275 if (ptr &&
len <= 36)
276 strcpy(strbuf + (36 -
len), ptr);
277#else
278 uuid_t uu;
279 uint32_t status = uuid_s_ok;
281
282 uuid_create(&uu, &status);
283
284 if (status == uuid_s_ok)
285 {
286 uuid_to_string(&uu, &
str, &status);
287 if (status == uuid_s_ok)
288 {
290
291
292
293
294
295
296 if (strbuf[14] != '1')
298 (
errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
299
300 errmsg(
"uuid_create() produced a version %c UUID instead of the expected version 1",
301 strbuf[14])));
302
303
304
305
306
307
308 if (ptr &&
len <= 36)
309 strcpy(strbuf + (36 -
len), ptr);
310 }
312 }
313
314 if (status != uuid_s_ok)
316 (
errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
317 errmsg(
"uuid library failure: %d",
318 (int) status)));
319#endif
320 break;
321 }
322
323 case 3:
324 case 5:
325 {
327#ifdef HAVE_UUID_BSD
328 uint32_t status = uuid_s_ok;
330#endif
331
332 if (v == 3)
333 {
335
337 elog(
ERROR,
"could not initialize %s context: %s",
"MD5",
341 elog(
ERROR,
"could not update %s context: %s",
"MD5",
343
345 sizeof(uu)) < 0)
346 elog(
ERROR,
"could not finalize %s context: %s",
"MD5",
349 }
350 else
351 {
354
356 elog(
ERROR,
"could not initialize %s context: %s",
"SHA1",
360 elog(
ERROR,
"could not update %s context: %s",
"SHA1",
363 elog(
ERROR,
"could not finalize %s context: %s",
"SHA1",
366
367 memcpy(&uu, sha1result, sizeof(uu));
368 }
369
370
373
374#ifdef HAVE_UUID_E2FS
375
377 uuid_unparse((unsigned char *) &uu, strbuf);
378#else
379 uuid_to_string(&uu, &
str, &status);
380
381 if (status == uuid_s_ok)
383
385
386 if (status != uuid_s_ok)
388 (
errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
389 errmsg(
"uuid library failure: %d",
390 (int) status)));
391#endif
392 break;
393 }
394
395 case 4:
396 default:
397 {
398#ifdef HAVE_UUID_E2FS
399 uuid_t uu;
400
401 uuid_generate_random(uu);
402 uuid_unparse(uu, strbuf);
403#else
405 "%08lx-%04x-%04x-%04x-%04x%08lx",
406 (unsigned long) arc4random(),
407 (unsigned) (arc4random() & 0xffff),
408 (unsigned) ((arc4random() & 0xfff) | 0x4000),
409 (unsigned) ((arc4random() & 0x3fff) | 0x8000),
410 (unsigned) (arc4random() & 0xffff),
411 (unsigned long) arc4random());
412#endif
413 break;
414 }
415 }
416
418}
const char * pg_cryptohash_error(pg_cryptohash_ctx *ctx)
int pg_cryptohash_update(pg_cryptohash_ctx *ctx, const uint8 *data, size_t len)
pg_cryptohash_ctx * pg_cryptohash_create(pg_cryptohash_type type)
int pg_cryptohash_init(pg_cryptohash_ctx *ctx)
void pg_cryptohash_free(pg_cryptohash_ctx *ctx)
int pg_cryptohash_final(pg_cryptohash_ctx *ctx, uint8 *dest, size_t len)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
#define DirectFunctionCall1(func, arg1)
size_t strlcpy(char *dst, const char *src, size_t siz)
static Datum CStringGetDatum(const char *X)
#define SHA1_DIGEST_LENGTH
#define UUID_V3_OR_V5(uu, v)
#define UUID_TO_LOCAL(uu)
#define UUID_TO_NETWORK(uu)
Datum uuid_in(PG_FUNCTION_ARGS)