1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
--- contrib/uuid-ossp/Makefile.orig 2014-03-17 20:36:46.000000000 +0100
+++ contrib/uuid-ossp/Makefile 2014-03-19 21:19:28.000000000 +0100
@@ -1,12 +1,14 @@
# contrib/uuid-ossp/Makefile
+# modified using http://pgfoundry.org/projects/uuid-freebsd/
+# to actually not use ossp, since uuid methods are all
+# built in into libc in FreeBSD /girgen@
MODULE_big = uuid-ossp
OBJS = uuid-ossp.o
EXTENSION = uuid-ossp
DATA = uuid-ossp--1.0.sql uuid-ossp--unpackaged--1.0.sql
-
-SHLIB_LINK += $(OSSP_UUID_LIBS)
+SHLIB_LINK = -lmd
ifdef USE_PGXS
PG_CONFIG = pg_config
--- contrib/uuid-ossp/uuid-ossp--1.0.sql.orig 2014-03-17 20:36:46.000000000 +0100
+++ contrib/uuid-ossp/uuid-ossp--1.0.sql 2014-03-19 21:19:28.000000000 +0100
@@ -1,5 +1,12 @@
/* contrib/uuid-ossp/uuid-ossp--1.0.sql */
+-- this module is modified by girgen@ for FreeBSD using
+-- http://pgfoundry.org/projects/uuid-freebsd/
+-- based on the work of Andrew Gierth.
+-- The name is still uuid-ossp to maintain verbatim compatibility
+-- with the original ossp based module, although the built-in libc
+-- routines are actually used instead, not ossp.
+
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use '''CREATE EXTENSION "uuid-ossp"''' to load this file. \quit
--- contrib/uuid-ossp/uuid-ossp.c.orig 2014-03-17 20:36:46.000000000 +0100
+++ contrib/uuid-ossp/uuid-ossp.c 2014-03-19 21:20:56.000000000 +0100
@@ -6,6 +6,10 @@
*
* contrib/uuid-ossp/uuid-ossp.c
*
+ * Modified to use FreeBSD's built in uuid instead of ossp:
+ * Copyright (c) 2009 Andrew Gierth
+ *
+ * URL: http://pgfoundry.org/projects/uuid-freebsd
*-------------------------------------------------------------------------
*/
@@ -14,27 +18,14 @@
#include "utils/builtins.h"
#include "utils/uuid.h"
-/*
- * There's some confusion over the location of the uuid.h header file.
- * On Debian, it's installed as ossp/uuid.h, while on Fedora, or if you
- * install ossp-uuid from a tarball, it's installed as uuid.h. Don't know
- * what other systems do.
- */
-#ifdef HAVE_OSSP_UUID_H
-#include <ossp/uuid.h>
-#else
-#ifdef HAVE_UUID_H
-#include <uuid.h>
-#else
-#error OSSP uuid.h not found
-#endif
-#endif
-
-/* better both be 16 */
-#if (UUID_LEN != UUID_LEN_BIN)
-#error UUID length mismatch
-#endif
+/* OS has a uuid_hash that conflicts with ours; kill it*/
+/* explicit path since we do _not_ want to get any other version */
+#define uuid_hash freebsd_uuid_hash
+#include "/usr/include/uuid.h"
+#undef uuid_hash
+#include <md5.h>
+#include <sha.h>
PG_MODULE_MAGIC;
@@ -64,163 +55,175 @@
PG_FUNCTION_INFO_V1(uuid_generate_v4);
PG_FUNCTION_INFO_V1(uuid_generate_v5);
-static void
-pguuid_complain(uuid_rc_t rc)
-{
- char *err = uuid_error(rc);
-
- if (err != NULL)
- ereport(ERROR,
- (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
- errmsg("OSSP uuid library failure: %s", err)));
- else
- ereport(ERROR,
- (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
- errmsg("OSSP uuid library failure: error code %d", rc)));
-}
+/* we assume that the string representation is portable and that the
+ * native binary representation might not be. But for *ns, we assume
+ * that pg's internal storage of uuids is the simple byte-oriented
+ * binary format. */
-static char *
-uuid_to_string(const uuid_t *uuid)
-{
- char *buf = palloc(UUID_LEN_STR + 1);
- void *ptr = buf;
- size_t len = UUID_LEN_STR + 1;
- uuid_rc_t rc;
-
- rc = uuid_export(uuid, UUID_FMT_STR, &ptr, &len);
- if (rc != UUID_RC_OK)
- pguuid_complain(rc);
-
- return buf;
-}
-
-
-static void
-string_to_uuid(const char *str, uuid_t *uuid)
+static Datum
+internal_uuid_create(int v, unsigned char *ns, char *ptr, int len)
{
- uuid_rc_t rc;
-
- rc = uuid_import(uuid, UUID_FMT_STR, str, UUID_LEN_STR + 1);
- if (rc != UUID_RC_OK)
- pguuid_complain(rc);
-}
+ char strbuf[40];
+ switch (v)
+ {
+ case 0: /* constant-value uuids: nil, or namespace uuids */
+ strlcpy(strbuf, ptr, 37);
+ break;
+
+ case 4: default: /* random uuid */
+ {
+ sprintf(strbuf, "%08lx-%04x-%04x-%04x-%04x%08lx",
+ (unsigned long) arc4random(),
+ (unsigned) (arc4random() & 0xffff),
+ (unsigned) ((arc4random() & 0xfff) | 0x4000),
+ (unsigned) ((arc4random() & 0x3fff) | 0x8000),
+ (unsigned) (arc4random() & 0xffff),
+ (unsigned long) arc4random());
+ break;
+ }
+
+ case 1: /* time/node-based uuids */
+ {
+ uuid_t uu;
+ uint32_t status = uuid_s_ok;
+ char *str = NULL;
+
+ uuid_create(&uu, &status);
+
+ if (status == uuid_s_ok)
+ {
+ uuid_to_string(&uu, &str, &status);
+ if (status == uuid_s_ok)
+ {
+ strlcpy(strbuf, str, 37);
+
+ /* PTR, if set, replaces the trailing characters of the uuid;
+ * this is to support v1mc, where a random multicast MAC is
+ * used instead of the physical one
+ */
+
+ if (ptr && len <= 36)
+ strcpy(strbuf + (36 - len), ptr);
+ }
+ if (str)
+ free(str);
+ }
-static Datum
-special_uuid_value(const char *name)
-{
- uuid_t *uuid;
- char *str;
- uuid_rc_t rc;
-
- rc = uuid_create(&uuid);
- if (rc != UUID_RC_OK)
- pguuid_complain(rc);
- rc = uuid_load(uuid, name);
- if (rc != UUID_RC_OK)
- pguuid_complain(rc);
- str = uuid_to_string(uuid);
- rc = uuid_destroy(uuid);
- if (rc != UUID_RC_OK)
- pguuid_complain(rc);
+ if (status != uuid_s_ok)
+ {
+ ereport(ERROR,
+ (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
+ errmsg("FreeBSD uuid library failure: %d", (int) status)));
+ }
+
+ break;
+ }
+
+ case 3: /* namespace-based MD5 uuids */
+ {
+ /* we could use pg's md5(), but we're already pulling in libmd */
+ MD5_CTX ctx;
+ unsigned char buf[16];
+
+ MD5Init(&ctx);
+ MD5Update(&ctx, ns, 16);
+ MD5Update(&ctx, (unsigned char *)ptr, len);
+ MD5Final(buf, &ctx);
+
+ sprintf(strbuf,
+ "%02x%02x%02x%02x-"
+ "%02x%02x-%02x%02x-%02x%02x-"
+ "%02x%02x%02x%02x%02x%02x",
+ buf[0], buf[1], buf[2], buf[3],
+ buf[4], buf[5], ((buf[6] & 0xf) | 0x30), buf[7],
+ ((buf[8] & 0x3F) | 0x80), buf[9], buf[10], buf[11],
+ buf[12], buf[13], buf[14], buf[15]);
+
+ break;
+ }
+
+ case 5: /* namespace-based SHA1 uuids */
+ {
+ SHA_CTX ctx;
+ unsigned char buf[20];
+
+ SHA1_Init(&ctx);
+ SHA1_Update(&ctx, ns, 16);
+ SHA1_Update(&ctx, (unsigned char *)ptr, len);
+ SHA1_Final(buf, &ctx);
+
+ sprintf(strbuf,
+ "%02x%02x%02x%02x-"
+ "%02x%02x-%02x%02x-%02x%02x-"
+ "%02x%02x%02x%02x%02x%02x",
+ buf[0], buf[1], buf[2], buf[3],
+ buf[4], buf[5], ((buf[6] & 0xf) | 0x30), buf[7],
+ ((buf[8] & 0x3F) | 0x80), buf[9], buf[10], buf[11],
+ buf[12], buf[13], buf[14], buf[15]);
+
+ break;
+ }
+ }
- return DirectFunctionCall1(uuid_in, CStringGetDatum(str));
+ return DirectFunctionCall1(uuid_in, CStringGetDatum(strbuf));
}
Datum
uuid_nil(PG_FUNCTION_ARGS)
{
- return special_uuid_value("nil");
+ return internal_uuid_create(0, NULL, "00000000-0000-0000-0000-000000000000", 36);
}
Datum
uuid_ns_dns(PG_FUNCTION_ARGS)
{
- return special_uuid_value("ns:DNS");
+ return internal_uuid_create(0, NULL, "6ba7b810-9dad-11d1-80b4-00c04fd430c8", 36);
}
Datum
uuid_ns_url(PG_FUNCTION_ARGS)
{
- return special_uuid_value("ns:URL");
+ return internal_uuid_create(0, NULL, "6ba7b811-9dad-11d1-80b4-00c04fd430c8", 36);
}
Datum
uuid_ns_oid(PG_FUNCTION_ARGS)
{
- return special_uuid_value("ns:OID");
+ return internal_uuid_create(0, NULL, "6ba7b812-9dad-11d1-80b4-00c04fd430c8", 36);
}
Datum
uuid_ns_x500(PG_FUNCTION_ARGS)
{
- return special_uuid_value("ns:X500");
-}
-
-
-static Datum
-uuid_generate_internal(int mode, const uuid_t *ns, const char *name)
-{
- uuid_t *uuid;
- char *str;
- uuid_rc_t rc;
-
- rc = uuid_create(&uuid);
- if (rc != UUID_RC_OK)
- pguuid_complain(rc);
- rc = uuid_make(uuid, mode, ns, name);
- if (rc != UUID_RC_OK)
- pguuid_complain(rc);
- str = uuid_to_string(uuid);
- rc = uuid_destroy(uuid);
- if (rc != UUID_RC_OK)
- pguuid_complain(rc);
-
- return DirectFunctionCall1(uuid_in, CStringGetDatum(str));
+ return internal_uuid_create(0, NULL, "6ba7b814-9dad-11d1-80b4-00c04fd430c8", 36);
}
Datum
uuid_generate_v1(PG_FUNCTION_ARGS)
{
- return uuid_generate_internal(UUID_MAKE_V1, NULL, NULL);
+ return internal_uuid_create(1, NULL, NULL, 0);
}
Datum
uuid_generate_v1mc(PG_FUNCTION_ARGS)
{
- return uuid_generate_internal(UUID_MAKE_V1 | UUID_MAKE_MC, NULL, NULL);
-}
-
-
-static Datum
-uuid_generate_v35_internal(int mode, pg_uuid_t *ns, text *name)
-{
- uuid_t *ns_uuid;
- Datum result;
- uuid_rc_t rc;
-
- rc = uuid_create(&ns_uuid);
- if (rc != UUID_RC_OK)
- pguuid_complain(rc);
- string_to_uuid(DatumGetCString(DirectFunctionCall1(uuid_out, UUIDPGetDatum(ns))),
- ns_uuid);
-
- result = uuid_generate_internal(mode,
- ns_uuid,
- text_to_cstring(name));
-
- rc = uuid_destroy(ns_uuid);
- if (rc != UUID_RC_OK)
- pguuid_complain(rc);
+ char buf[20];
- return result;
+ sprintf(buf, "-%04x-%04x%08lx",
+ (unsigned)((arc4random() & 0x3FFF) | 0x8000),
+ /* set IEEE802 multicast and local-admin bits */
+ (unsigned)((arc4random() & 0xffff) | 0x0300),
+ (unsigned long) arc4random());
+
+ return internal_uuid_create(1, NULL, buf, 18);
}
@@ -230,14 +233,15 @@
pg_uuid_t *ns = PG_GETARG_UUID_P(0);
text *name = PG_GETARG_TEXT_P(1);
- return uuid_generate_v35_internal(UUID_MAKE_V3, ns, name);
+ return internal_uuid_create(3, (unsigned char *)ns,
+ VARDATA(name), VARSIZE(name) - VARHDRSZ);
}
Datum
uuid_generate_v4(PG_FUNCTION_ARGS)
{
- return uuid_generate_internal(UUID_MAKE_V4, NULL, NULL);
+ return internal_uuid_create(4, NULL, NULL, 0);
}
@@ -247,5 +251,6 @@
pg_uuid_t *ns = PG_GETARG_UUID_P(0);
text *name = PG_GETARG_TEXT_P(1);
- return uuid_generate_v35_internal(UUID_MAKE_V5, ns, name);
+ return internal_uuid_create(5, (unsigned char *)ns,
+ VARDATA(name), VARSIZE(name) - VARHDRSZ);
}
--- contrib/Makefile.orig 2014-03-21 09:05:11.000000000 +0100
+++ contrib/Makefile 2014-03-21 09:05:29.000000000 +0100
@@ -48,6 +48,7 @@
test_parser \
tsearch2 \
unaccent \
+ uuid-ossp \
vacuumlo
ifeq ($(with_openssl),yes)
|