File: UUID.xs

package info (click to toggle)
libuuid-perl 0.28-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 128 kB
  • sloc: perl: 17; makefile: 5
file content (580 lines) | stat: -rw-r--r-- 14,747 bytes parent folder | download
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
#ifdef __cplusplus
extern "C" {
#endif

#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"


#ifdef PERL__UUID__UUID_UUID_H
#include <uuid/uuid.h>
#elif PERL__UUID__UUID_H
#include <uuid.h>
#elif PERL__UUID__RPC_H
#include <rpc.h>
#include <rpcdce.h>
#endif

#ifdef __cplusplus
}
#endif


/*
#ifndef SvPV_nolen
# define SvPV_nolen(sv) SvPV(sv, na)
#endif
*/

/* 2 hex digits per byte + 4 separators + 1 trailing null */
#define UUID_BUF_SZ() (2 * PERL__UUID__STRUCT_SZ + 4 + 1)

#ifdef PERL__UUID__E2FS_INT
#define UUID_T uuid_t
#define UUID2SV(u) ((char*)u)
#define SV2UUID(s) ((unsigned char*)SvGROW(s, sizeof(uuid_t)+1))

#elif PERL__UUID__RPC_INT
#define UUID_T uuid_t
#define UUID2SV(u) ((char*)&u)
#define SV2UUID(s) ((uuid_t*)SvGROW(s, sizeof(uuid_t)+1))

#elif PERL__UUID__WIN_INT
#define UUID_T UUID
#define UUID2SV(u) ((char*)&u)
#define SV2UUID(s) ((UUID*)SvGROW(s, sizeof(UUID)+1))

#elif PERL__UUID__OSSP_INT
#define UUID_T uuid_t*
#define UUID2SV(u) ((char*)&u)
#define SV2UUID(s) ((uuid_t*)SvGROW(s, UUID_LEN_STR+1))

#endif

#define SV2STR(s)  (SvGROW(s, UUID_BUF_SZ()+1))


#ifdef PERL__UUID__OSSP_INT
void croak_ossp(uuid_rc_t st) {
    croak("%s", uuid_error(st));
}
#endif


void do_generate(SV *str) {
    UUID_T uuid;
#ifdef PERL__UUID__E2FS_INT
    uuid_generate( uuid );
#elif PERL__UUID__RPC_INT
    int32_t s;
    uuid_create(&uuid, &s);
#elif PERL__UUID__WIN_INT
    RPC_STATUS st;
    st = UuidCreate(&uuid);
#elif PERL__UUID__OSSP_INT
    char *s = NULL;
    size_t len = 0;
    uuid_rc_t st;
    if ((st = uuid_create(&uuid)) != UUID_RC_OK) croak_ossp(st);
    if ((st = uuid_make(uuid,UUID_MAKE_V1|UUID_MAKE_MC)) != UUID_RC_OK) croak_ossp(st);
    if ((st = uuid_export(uuid, UUID_FMT_BIN, &s, &len)) != UUID_RC_OK) croak_ossp(st);
    if ((st = uuid_destroy(uuid)) != UUID_RC_OK) croak_ossp(st);
    sv_setpvn(str, s, len);
    return;
#endif
    sv_setpvn(str, UUID2SV(uuid), sizeof(UUID_T));
}


void do_generate_random(SV *str) {
    UUID_T uuid;
#ifdef PERL__UUID__E2FS_INT
    uuid_generate_random( uuid );
#elif PERL__UUID__RPC_INT
    int32_t s;
    uuid_create(&uuid, &s);
#elif PERL__UUID__WIN_INT
    UuidCreate(&uuid);
#elif PERL__UUID__OSSP_INT
    char *s = NULL;
    size_t len = 0;
    uuid_rc_t st;
    if ((st = uuid_create(&uuid)) != UUID_RC_OK) croak_ossp(st);
    if ((st = uuid_make(uuid,UUID_MAKE_V4)) != UUID_RC_OK) croak_ossp(st);
    if ((st = uuid_export(uuid, UUID_FMT_BIN, &s, &len)) != UUID_RC_OK) croak_ossp(st);
    if ((st = uuid_destroy(uuid)) != UUID_RC_OK) croak_ossp(st);
    sv_setpvn(str, s, len);
    return;
#endif
    sv_setpvn(str, UUID2SV(uuid), sizeof(UUID_T));
}


void do_generate_time(SV *str) {
    UUID_T uuid;
#ifdef PERL__UUID__E2FS_INT
    uuid_generate_time( uuid );
#elif PERL__UUID__RPC_INT
    int32_t s;
    uuid_create(&uuid, &s);
#elif PERL__UUID__WIN_INT
    UuidCreateSequential(&uuid);
#elif PERL__UUID__OSSP_INT
    char *s = NULL;
    size_t len = 0;
    uuid_rc_t st;
    if ((st = uuid_create(&uuid)) != UUID_RC_OK) croak_ossp(st);
    if ((st = uuid_make(uuid,UUID_MAKE_V1)) != UUID_RC_OK) croak_ossp(st);
    if ((st = uuid_export(uuid, UUID_FMT_BIN, &s, &len)) != UUID_RC_OK) croak_ossp(st);
    if ((st = uuid_destroy(uuid)) != UUID_RC_OK) croak_ossp(st);
    sv_setpvn(str, s, len);
    return;
    (void)st;
#endif
    sv_setpvn(str, UUID2SV(uuid), sizeof(UUID_T));
}


void do_unparse(SV *in, SV * out) {
#ifdef PERL__UUID__E2FS_INT
    char str[UUID_BUF_SZ()];
    uuid_unparse(SV2UUID(in), str);
    sv_setpvn(out, str, UUID_BUF_SZ()-1);
#elif PERL__UUID__RPC_INT
    char *str;
    int32_t s;
    uuid_to_string(SV2UUID(in), &str, &s); /* free str */
    sv_setpvn(out, str, UUID_BUF_SZ()-1);
    free(str);
#elif PERL__UUID__WIN_INT
    RPC_CSTR str;
    RPC_STATUS st;
    st = UuidToString(SV2UUID(in), &str); /* free str */
    if( st != RPC_S_OK )
        croak("UuidToString error: %i", st);
    sv_setpvn(out, str, UUID_BUF_SZ()-1);
    RpcStringFree(&str);
#elif PERL__UUID__OSSP_INT
    UUID_T uuid;
    char *str = NULL;
    size_t len = UUID_LEN_STR;
    uuid_rc_t st;
    if ((st = uuid_create(&uuid)) != UUID_RC_OK) croak_ossp(st);
    if ((st = uuid_import(uuid, UUID_FMT_BIN, SV2UUID(in), UUID_LEN_BIN)) != UUID_RC_OK) croak_ossp(st);
    if ((st = uuid_export(uuid, UUID_FMT_STR, &str, &len)) != UUID_RC_OK) croak_ossp(st);
    if ((st = uuid_destroy(uuid)) != UUID_RC_OK) croak_ossp(st);
    sv_setpvn(out, str, len-1);
    free(str);
    (void)st;
#endif
}


void do_unparse_lower(SV *in, SV * out) {
#ifdef PERL__UUID__E2FS_INT
    char *p, str[UUID_BUF_SZ()];
    /* uuid_unparse_lower(SV2UUID(in), str); */ /* not on SunOS */
    uuid_unparse(SV2UUID(in), str);
    for(p=str; *p; ++p) *p = tolower(*p);
    sv_setpvn(out, str, UUID_BUF_SZ()-1);
#elif PERL__UUID__RPC_INT
    char *p, *str;
    int32_t s;
    uuid_to_string(SV2UUID(in), &str, &s); /* free str */
    for(p=str; *p; ++p) *p = tolower(*p);
    sv_setpvn(out, str, UUID_BUF_SZ()-1);
    free(str);
#elif PERL__UUID__WIN_INT
    char *p;
    RPC_CSTR str;
    RPC_STATUS st;
    st = UuidToString(SV2UUID(in), &str); /* free str */
    if( st != RPC_S_OK )
        croak("UuidToString error: %i", st);
    for(p=str; *p; ++p) *p = tolower(*p);
    sv_setpvn(out, str, UUID_BUF_SZ()-1);
    RpcStringFree(&str);
#elif PERL__UUID__OSSP_INT
    UUID_T uuid;
    char *p;
    char *str = NULL;
    uuid_rc_t st;
    if ((st = uuid_create(&uuid)) != UUID_RC_OK) croak_ossp(st);
    if ((st = uuid_import(uuid, UUID_FMT_BIN, SV2UUID(in), UUID_BUF_SZ()-1)) != UUID_RC_OK) croak_ossp(st);
    if ((st = uuid_export(SV2UUID(in), UUID_FMT_STR, &str, NULL)) != UUID_RC_OK) croak_ossp(st);
    if ((st = uuid_destroy(uuid)) != UUID_RC_OK) croak_ossp(st);
    for(p=str; *p; ++p) *p = tolower(*p);
    sv_setpv(out, str);
    free(str);
    (void)st;
#endif
}


void do_unparse_upper(SV *in, SV * out) {
#ifdef PERL__UUID__E2FS_INT
    char *p, str[UUID_BUF_SZ()];
    /* uuid_unparse_upper(SV2UUID(in), str); */ /* not on SunOS */
    uuid_unparse(SV2UUID(in), str);
    for(p=str; *p; ++p) *p = toupper(*p);
    sv_setpvn(out, str, UUID_BUF_SZ()-1);
#elif PERL__UUID__RPC_INT
    char *p, *str;
    int32_t s;
    uuid_to_string(SV2UUID(in), &str, &s); /* free str */
    for(p=str; *p; ++p) *p = toupper(*p);
    sv_setpvn(out, str, UUID_BUF_SZ()-1);
    free(str);
#elif PERL__UUID__WIN_INT
    char *p;
    RPC_CSTR str;
    RPC_STATUS st;
    st = UuidToString(SV2UUID(in), &str); /* free str */
    if( st != RPC_S_OK )
        croak("UuidToString error: %i", st);
    for(p=str; *p; ++p) *p = toupper(*p);
    sv_setpvn(out, str, UUID_BUF_SZ()-1);
    RpcStringFree(&str);
#elif PERL__UUID__OSSP_INT
    UUID_T uuid;
    char *p;
    char *str = NULL;
    uuid_rc_t st;
    if ((st = uuid_create(&uuid)) != UUID_RC_OK) croak_ossp(st);
    if ((st = uuid_import(uuid, UUID_FMT_BIN, SV2UUID(in), UUID_BUF_SZ()-1)) != UUID_RC_OK) croak_ossp(st);
    if ((st = uuid_export(SV2UUID(in), UUID_FMT_STR, &str, NULL)) != UUID_RC_OK) croak_ossp(st);
    if ((st = uuid_destroy(uuid)) != UUID_RC_OK) croak_ossp(st);
    for(p=str; *p; ++p) *p = toupper(*p);
    sv_setpv(out, str);
    free(str);
    (void)st;
#endif
}


int do_parse(SV *in, SV *out) {
    UUID_T uuid;
#ifdef PERL__UUID__E2FS_INT
    int rc;
    rc = uuid_parse(SV2STR(in), uuid);
    if( !rc )
        sv_setpvn(out, UUID2SV(uuid), sizeof(UUID_T));
    return rc;
#elif PERL__UUID__RPC_INT
    int rc;
    uuid_from_string(SV2STR(in), &uuid, &rc);
    if( !rc )
        sv_setpvn(out, UUID2SV(uuid), sizeof(UUID_T));
    return rc == uuid_s_ok ? 0 : -1;
#elif PERL__UUID__WIN_INT
    RPC_STATUS rc;
    rc = UuidFromString(SV2STR(in), &uuid);
    if( rc == RPC_S_OK )
        sv_setpvn(out, UUID2SV(uuid), sizeof(UUID_T));
    return rc == RPC_S_OK ? 0 : -1;
#elif PERL__UUID__OSSP_INT
    char *str = NULL;
    if (uuid_create(&uuid) != UUID_RC_OK)
        return -1;
    if (uuid_import(uuid, UUID_FMT_STR, SV2STR(in), UUID_LEN_STR) != UUID_RC_OK) {
        uuid_destroy(uuid);
        return -1;
    }
    if (uuid_export(uuid, UUID_FMT_BIN, &str, NULL) != UUID_RC_OK) {
        uuid_destroy(uuid);
        return -1;
    }
    uuid_destroy(uuid);
    sv_setpvn(out, str, UUID_LEN_BIN);
    free(str);
    return 0;
#endif
}


void do_clear(SV *in) {
    UUID_T uuid;
#ifdef PERL__UUID__E2FS_INT
    uuid_clear(uuid);
#elif PERL__UUID__RPC_INT
    int32_t s;
    uuid_create_nil(&uuid,&s);
#elif PERL__UUID__WIN_INT
    UuidCreateNil(&uuid);
#elif PERL__UUID__OSSP_INT
    char *str = NULL;
    size_t len = UUID_LEN_BIN;
    uuid_rc_t st;
    if ((st = uuid_create(&uuid)) != UUID_RC_OK) croak_ossp(st);
    if ((st = uuid_load(uuid, "nil")) != UUID_RC_OK) croak_ossp(st);
    if ((st = uuid_export(uuid, UUID_FMT_BIN, &str, &len)) != UUID_RC_OK) croak_ossp(st);
    if ((st = uuid_destroy(uuid)) != UUID_RC_OK) croak_ossp(st);
    sv_setpvn(in, str, len);
    return;
#endif
    sv_setpvn(in, UUID2SV(uuid), sizeof(UUID_T));
}


int do_is_null(SV *in) {
#ifdef PERL__UUID__E2FS_INT
    if( SvCUR(in) != sizeof(uuid_t) )
        return 0;
    return uuid_is_null(SV2UUID(in));
#elif PERL__UUID__RPC_INT
    int32_t s;
    return uuid_is_nil(SV2UUID(in),&s);
#elif PERL__UUID__WIN_INT
    int rc;
    RPC_STATUS st;
    rc = UuidIsNil(SV2UUID(in), &st);
    return rc == TRUE ? 1 : 0;
#elif PERL__UUID__OSSP_INT
    UUID_T uuid;
    int i;
    uuid_rc_t st;
    if ((st = uuid_create(&uuid)) != UUID_RC_OK) croak_ossp(st);
    if ((st = uuid_import(uuid, UUID_FMT_BIN, SV2STR(in), UUID_BUF_SZ()-1)) != UUID_RC_OK) croak_ossp(st);
    if ((st = uuid_isnil(uuid, &i)) != UUID_RC_OK) croak_ossp(st);
    if ((st = uuid_destroy(uuid)) != UUID_RC_OK) croak_ossp(st);
    return i ? 1 : 0;
#endif
}


int do_compare(SV *uu1, SV *uu2) {
#ifdef PERL__UUID__E2FS_INT
/*
    if( SvCUR(uu1) == sizeof(uuid_t) )
        if( SvCUR(uu2) == sizeof(uuid_t) )
            return uuid_compare(SV2UUID(uu1), SV2UUID(uu2));
*/
#elif PERL__UUID__RPC_INT
/*
    int32_t s;
    if( SvCUR(uu1) == sizeof(uuid_t) )
        if( SvCUR(uu2) == sizeof(uuid_t) )
            return uuid_compare(SV2UUID(uu1), SV2UUID(uu2), &s);
*/
#elif PERL__UUID__WIN_INT
#elif PERL__UUID__OSSP_INT
#endif
    return sv_cmp(uu1, uu2);
}


void do_copy(SV *dst, SV *src) {
    UUID_T uuid;
#ifdef PERL__UUID__E2FS_INT
    if( SvCUR(src) != sizeof(uuid_t) )
        uuid_clear(uuid);
    else
        uuid_copy(uuid, SV2UUID(src));
#elif PERL__UUID__RPC_INT
    int32_t s;
    if( SvCUR(src) != sizeof(uuid_t) )
        uuid_create_nil(&uuid, &s);
    else
        memcpy(&uuid, SV2UUID(src), sizeof(uuid_t));
        /* uuid_copy(uuid, SV2UUID(src), &s);  <-- duh, not on bsd */
#elif PERL__UUID__WIN_INT
    if( SvCUR(src) != sizeof(uuid_t) )
        UuidCreateNil(&uuid);
    else
        memcpy(&uuid, SV2UUID(src), sizeof(UUID));
#elif PERL__UUID__OSSP_INT
    size_t len = SvCUR(src);
    if ( len != UUID_LEN_BIN ) {
        char *str = NULL;
        len = 0;
        uuid_rc_t st;
        if ((st = uuid_create(&uuid)) != UUID_RC_OK) croak_ossp(st);
        if ((st = uuid_export(uuid, UUID_FMT_BIN, &str, &len)) != UUID_RC_OK) croak_ossp(st);
        if ((st = uuid_destroy(uuid)) != UUID_RC_OK) croak_ossp(st);
        sv_setpvn(dst, str, len);
        free(str);
        return;
    }
    sv_copypv(dst, src);
    return;
#endif
    sv_setpvn(dst, UUID2SV(uuid), sizeof(UUID_T));
}


SV* do_uuid() {
    UUID_T uuid;
#ifdef PERL__UUID__E2FS_INT
    char str[UUID_BUF_SZ()];
    uuid_generate(uuid);
    uuid_unparse(uuid, str);
    return newSVpvn(str, UUID_BUF_SZ()-1);
#elif PERL__UUID__RPC_INT
    SV *sv;
    char *str;
    int32_t s;
    uuid_create(&uuid, &s);
    uuid_to_string(&uuid, &str, &s); /* free str */
    sv = newSVpvn(str, UUID_BUF_SZ()-1);
    free(str);
    return sv;
#elif PERL__UUID__WIN_INT
    SV *sv;
    RPC_STATUS st;
    RPC_CSTR str;
    UuidCreateSequential(&uuid);
    st = UuidToString(&uuid, &str); /* free str */
    if( st != RPC_S_OK )
        croak("UuidToString error: %i", st);
    sv = newSVpvn(str, UUID_BUF_SZ()-1);
    RpcStringFree(&str);
    return sv;
#elif PERL__UUID__OSSP_INT
    SV *rv;
    char *str = NULL;
    size_t len = UUID_LEN_STR;
    uuid_rc_t st;
    if ((st = uuid_create(&uuid)) != UUID_RC_OK) croak_ossp(st);
    if ((st = uuid_make(uuid, UUID_MAKE_V1|UUID_MAKE_MC)) != UUID_RC_OK) croak_ossp(st);
    if ((st = uuid_export(uuid, UUID_FMT_STR, &str, &len)) != UUID_RC_OK) croak_ossp(st);
    if ((st = uuid_destroy(uuid)) != UUID_RC_OK) croak_ossp(st);
    rv = newSVpvn(str, len-1);
    free(str);
    return rv;
#endif
}


void do_debug() {
    SV *bmsg, *smsg;
#ifdef PERL__UUID__UUID_UUID_H
    PerlIO_puts(PerlIO_stderr(), "# Header: uuid/uuid.h\n");
#elif PERL__UUID__UUID_H
    PerlIO_puts(PerlIO_stderr(), "# Header: uuid.h\n");
#elif PERL__UUID__RPC_H
    PerlIO_puts(PerlIO_stderr(), "# Header: rpc.h\n");
#endif

#ifdef PERL__UUID__E2FS_INT
    PerlIO_puts(PerlIO_stderr(), "# Interface: e2fs\n");
#elif PERL__UUID__RPC_INT
    PerlIO_puts(PerlIO_stderr(), "# Interface: rpc\n");
#elif PERL__UUID__WIN_INT
    PerlIO_puts(PerlIO_stderr(), "# Interface: win\n");
#elif PERL__UUID__OSSP_INT
    PerlIO_puts(PerlIO_stderr(), "# Interface: ossp\n");
#endif

    bmsg = mess("# Buffer size: %i\n", UUID_BUF_SZ());
    PerlIO_puts(PerlIO_stderr(), SvPVX(bmsg));
    smsg = mess("# Struct size: %i\n", PERL__UUID__STRUCT_SZ);
    PerlIO_puts(PerlIO_stderr(), SvPVX(smsg));
}



MODULE = UUID		PACKAGE = UUID		


void
generate(str)
    SV * str
    PROTOTYPE: $
    CODE:
    do_generate(str); 

void
generate_random(str)
    SV * str
    PROTOTYPE: $
    CODE:
    do_generate_random(str); 

void
generate_time(str)
    SV * str
    PROTOTYPE: $
    CODE:
    do_generate_time(str); 

void
unparse(in, out)
    SV * in
    SV * out
    PROTOTYPE: $$
    CODE:
    do_unparse(in, out);

void
unparse_lower(in, out)
    SV * in
    SV * out
    PROTOTYPE: $$
    CODE:
    do_unparse_lower(in, out);

void
unparse_upper(in, out)
    SV * in
    SV * out
    PROTOTYPE: $$
    CODE:
    do_unparse_upper(in, out);

int
parse(in, out)
    SV * in
    SV * out
    PROTOTYPE: $$
    CODE: 
    RETVAL = do_parse(in, out);
    OUTPUT:
    RETVAL

void
clear(in)
    SV * in
    PROTOTYPE: $
    CODE:
    do_clear(in);

int
is_null(in)
    SV * in
    PROTOTYPE: $
    CODE:
    RETVAL = do_is_null(in);
    OUTPUT:
    RETVAL

void
copy(dst, src)
    SV * dst
    SV * src
    CODE:
    do_copy(dst, src);

int
compare(uu1, uu2)
    SV * uu1
    SV * uu2
    CODE:
    RETVAL = do_compare(uu1, uu2);
    OUTPUT:
    RETVAL

SV*
uuid()
    PROTOTYPE:
    CODE:
    RETVAL = do_uuid();
    OUTPUT:
    RETVAL

void
debug()
    PROTOTYPE:
    CODE:
    do_debug();