-
Notifications
You must be signed in to change notification settings - Fork 15
/
uuid.c
453 lines (343 loc) · 10.1 KB
/
uuid.c
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*
+----------------------------------------------------------------------+
| This library is free software; you can redistribute it and/or |
| modify it under the terms of the GNU Lesser General Public |
| License as published by the Free Software Foundation; either |
| version 2.1 of the License, or (at your option) any later version. |
| |
| This library is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| Lesser General Public License for more details. |
| |
| You should have received a copy of the GNU Lesser General Public |
| License in the file LICENSE along with this library; |
| if not, write to the |
| |
| Free Software Foundation, Inc., |
| 59 Temple Place, Suite 330, |
| Boston, MA 02111-1307 USA |
+----------------------------------------------------------------------+
| Authors: Hartmut Holzgraefe <hartmut@php.net> |
| Remi Collet <remi@php.net> |
+----------------------------------------------------------------------+
*/
#include "php_uuid.h"
#ifdef HAVE_UUID
/* workround with define uuid_time uuid_time64 */
#ifdef uuid_time
#undef uuid_time
#define HAVE_TIME64
#endif
#if PHP_VERSION_ID < 80000
#define VALUE_ERROR(n,name,msg) php_error_docref(NULL, E_WARNING, "Argument #%d (%s) %s", n, name, msg); RETURN_FALSE
#define RETURN_THROWS() return
#include "uuid_legacy_arginfo.h"
#else
#define VALUE_ERROR(n,name,msg) zend_argument_value_error(n, msg); RETURN_THROWS()
#include "uuid_arginfo.h"
#endif
/* {{{ uuid_module_entry
*/
zend_module_entry uuid_module_entry = {
STANDARD_MODULE_HEADER,
"uuid",
ext_functions,
PHP_MINIT(uuid), /* Replace with NULL if there is nothing to do at php startup */
NULL, /* Replace with NULL if there is nothing to do at php shutdown */
NULL, /* Replace with NULL if there is nothing to do at request start */
NULL, /* Replace with NULL if there is nothing to do at request end */
PHP_MINFO(uuid),
PHP_UUID_VERSION,
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_UUID
ZEND_GET_MODULE(uuid)
#endif
/* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(uuid)
{
register_uuid_symbols(module_number);
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(uuid)
{
php_info_print_table_start();
php_info_print_table_header(2, "UUID extension", "enabled");
php_info_print_table_row(2, "Version", PHP_UUID_VERSION " (" PHP_UUID_STATE ")");
php_info_print_table_row(2, "Released", PHP_UUID_RELEASED);
php_info_print_table_row(2, "Authors", PHP_UUID_AUTHORS);
php_info_print_table_end();
/* add your stuff here */
}
/* }}} */
/* {{{ proto string uuid_create([int uuid_type])
Generate a new UUID */
PHP_FUNCTION(uuid_create)
{
zend_long uuid_type = UUID_TYPE_DEFAULT;
uuid_t uuid;
char uuid_str[37];
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &uuid_type) == FAILURE) {
RETURN_THROWS();
}
switch(uuid_type) {
case UUID_TYPE_DCE_TIME:
uuid_generate_time(uuid);
break;
case UUID_TYPE_DCE_RANDOM:
uuid_generate_random(uuid);
break;
case UUID_TYPE_DEFAULT:
uuid_generate(uuid);
break;
default:
#if PHP_VERSION_ID < 80000
php_error_docref(NULL,
E_WARNING,
"Unknown/invalid UUID type '%ld' requested, using default type instead",
uuid_type);
uuid_generate(uuid);
#else
zend_argument_value_error(1, "Unknown/invalid UUID type '" ZEND_LONG_FMT "'", uuid_type);
RETURN_THROWS();
#endif
break;
}
uuid_unparse(uuid, uuid_str);
RETURN_STRING(uuid_str);
}
/* }}} uuid_create */
/* {{{ proto bool uuid_is_valid(string uuid)
Check whether a given UUID string is a valid UUID */
PHP_FUNCTION(uuid_is_valid)
{
const char * uuid = NULL;
size_t uuid_len = 0;
uuid_t u;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &uuid, &uuid_len) == FAILURE) {
RETURN_THROWS();
}
RETURN_BOOL(0 == uuid_parse(uuid, u));
}
/* }}} uuid_is_valid */
/* {{{ proto int uuid_compare(string uuid1, string uuid2)
Compare two UUIDs */
PHP_FUNCTION(uuid_compare)
{
const char * uuid1 = NULL;
size_t uuid1_len = 0;
const char * uuid2 = NULL;
size_t uuid2_len = 0;
uuid_t u1, u2;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &uuid1, &uuid1_len, &uuid2, &uuid2_len) == FAILURE) {
RETURN_THROWS();
}
if (uuid_parse(uuid1, u1)) {
VALUE_ERROR(1, "$uuid1", "UUID expected");
}
if (uuid_parse(uuid2, u2)) {
VALUE_ERROR(2, "$uuid2", "UUID expected");
}
RETURN_LONG(uuid_compare(u1, u2));
}
/* }}} uuid_compare */
/* {{{ proto bool uuid_is_null(string uuid)
Check wheter an UUID is the NULL UUID 00000000-0000-0000-0000-000000000000 */
PHP_FUNCTION(uuid_is_null)
{
const char * uuid = NULL;
size_t uuid_len = 0;
uuid_t u;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &uuid, &uuid_len) == FAILURE) {
RETURN_THROWS();
}
if (uuid_parse(uuid, u)) {
VALUE_ERROR(1, "$uuid", "UUID expected");
}
RETURN_BOOL(uuid_is_null(u));
}
/* }}} uuid_is_null */
#ifdef HAVE_UUID_GENERATE_MD5
/* {{{ proto int uuid_generate_md5(string uuid_ns, string name)
Generate a MD5 hashed (predictable) UUID based on a well-known UUID */
PHP_FUNCTION(uuid_generate_md5)
{
const char * uuid, * name;
size_t uuid_len, name_len;
uuid_t ns, out;
char uuid_str[37];
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &uuid, &uuid_len, &name, &name_len) == FAILURE) {
RETURN_THROWS();
}
if (uuid_parse(uuid, ns)) {
VALUE_ERROR(1, "$uuid_ns", "UUID expected");
}
uuid_generate_md5(out, ns, name, name_len);
uuid_unparse(out, uuid_str);
RETURN_STRING(uuid_str);
}
/* }}} uuid_generate_md5 */
#endif /* HAVE_UUID_GENERATE_MD5 */
#ifdef HAVE_UUID_GENERATE_SHA1
/* {{{ proto int uuid_generate_sha1(string uuid_ns, string name)
Generate a SHA1 hashed (predictable) UUID based on a well-known UUID */
PHP_FUNCTION(uuid_generate_sha1)
{
const char * uuid, * name;
size_t uuid_len, name_len;
uuid_t ns, out;
char uuid_str[37];
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &uuid, &uuid_len, &name, &name_len) == FAILURE) {
RETURN_THROWS();
}
if (uuid_parse(uuid, ns)) {
VALUE_ERROR(1, "$uuid_ns", "UUID expected");
}
uuid_generate_sha1(out, ns, name, name_len);
uuid_unparse(out, uuid_str);
RETURN_STRING(uuid_str);
}
/* }}} uuid_generate_sha1 */
#endif /* HAVE_UUID_GENERATE_SHA1 */
#ifdef HAVE_UUID_TYPE
/* {{{ proto int uuid_type(string uuid)
Return the UUIDs type */
PHP_FUNCTION(uuid_type)
{
const char * uuid = NULL;
size_t uuid_len = 0;
uuid_t u;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &uuid, &uuid_len) == FAILURE) {
RETURN_THROWS();
}
if (uuid_parse(uuid, u)) {
VALUE_ERROR(1, "$uuid", "UUID expected");
}
if (uuid_is_null(u)) {
RETURN_LONG(UUID_TYPE_NULL);
}
RETURN_LONG(uuid_type(u));
}
/* }}} uuid_type */
#endif /* HAVE_UUID_TYPE */
#ifdef HAVE_UUID_VARIANT
/* {{{ proto int uuid_variant(string uuid)
Return the UUIDs variant */
PHP_FUNCTION(uuid_variant)
{
const char * uuid = NULL;
size_t uuid_len = 0;
uuid_t u;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &uuid, &uuid_len) == FAILURE) {
RETURN_THROWS();
}
if (uuid_parse(uuid, u)) {
VALUE_ERROR(1, "$uuid", "UUID expected");
}
if (uuid_is_null(u)) {
RETURN_LONG(UUID_TYPE_NULL);
}
RETURN_LONG(uuid_variant(u));
}
/* }}} uuid_variant */
#endif /* HAVE_UUID_VARIANT */
#ifdef HAVE_UUID_TIME
/* {{{ proto int uuid_time(string uuid)
Extract creation time from a time based UUID as UNIX timestamp */
PHP_FUNCTION(uuid_time)
{
const char * uuid = NULL;
size_t uuid_len = 0;
uuid_t u;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &uuid, &uuid_len) == FAILURE) {
RETURN_THROWS();
}
if (uuid_parse(uuid, u)
#ifdef HAVE_UUID_VARIANT
|| (uuid_variant(u) != 1)
#endif
#ifdef HAVE_UUID_TYPE
|| (uuid_type(u) != 1)
#endif
) {
VALUE_ERROR(1, "$uuid", "UUID DCE TIME expected");
}
#ifdef HAVE_TIME64
RETURN_LONG(uuid_time64(u, NULL));
#else
RETURN_LONG(uuid_time(u, NULL));
#endif
}
/* }}} uuid_time */
#endif /* HAVE_UUID_TIME */
/* {{{ proto string uuid_mac(string uuid)
Get UUID creator network MAC address */
PHP_FUNCTION(uuid_mac)
{
const char * uuid = NULL;
size_t uuid_len = 0;
uuid_t u;
char uuid_str[37];
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &uuid, &uuid_len) == FAILURE) {
RETURN_THROWS();
}
if (uuid_parse(uuid, u)
#ifdef HAVE_UUID_VARIANT
|| (uuid_variant(u) != 1)
#endif
#ifdef HAVE_UUID_TYPE
|| (uuid_type(u) != 1)
#endif
) {
VALUE_ERROR(1, "$uuid", "UUID DCE TIME expected");
}
uuid_unparse(u, uuid_str);
RETURN_STRING((char *)(uuid_str + 24));
}
/* }}} uuid_mac */
/* {{{ proto string uuid_parse(string uuid)
*/
PHP_FUNCTION(uuid_parse)
{
const char * uuid = NULL;
size_t uuid_len = 0;
uuid_t uuid_bin;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &uuid, &uuid_len) == FAILURE) {
RETURN_THROWS();
}
if (uuid_parse(uuid, uuid_bin)) {
VALUE_ERROR(1, "$uuid", "UUID expected");
}
RETURN_STRINGL((char *)uuid_bin, sizeof(uuid_t));
}
/* }}} uuid_parse */
/* {{{ proto string uuid_unparse(string uuid)
*/
PHP_FUNCTION(uuid_unparse)
{
const char * uuid = NULL;
size_t uuid_len = 0;
char uuid_txt[37];
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &uuid, &uuid_len) == FAILURE) {
RETURN_THROWS();
}
if (uuid_len != sizeof(uuid_t)) {
VALUE_ERROR(1, "$uuid", "UUID expected");
}
uuid_unparse((unsigned char *)uuid, uuid_txt);
RETURN_STRINGL(uuid_txt, 36);
}
/* }}} uuid_unparse */
#endif /* HAVE_UUID */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/