File: LibUUID.xs

package info (click to toggle)
libdata-uuid-libuuid-perl 0.05-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 336 kB
  • sloc: perl: 278; makefile: 3; ansic: 2
file content (295 lines) | stat: -rw-r--r-- 6,522 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
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

#define NEED_load_module
#define NEED_vload_module
#define NEED_sv_2pv_flags
#include "ppport.h"

#include <uuid/uuid.h>

#ifdef PERL_DARWIN
#define PID_CHECK pid_check()
#include <stdlib.h>
#else
#define PID_CHECK
#endif


#define UUID_TYPE_TIME 2
#define UUID_TYPE_RANDOM 4

#define UUID_HEX_SIZE sizeof(uuid_t) * 2
#define UUID_STRING_SIZE 36
#define UUID_BASE64_SIZE 24
#define UUID_BASE64_LF_SIZE 25
#define UUID_BASE64_CRLF_SIZE 26


/* these define RETBUF, which is the PV inside RETVAL preallocated to a certain
 * size. Avoids the copying of XSRETURN_PVN and teaches me to write more
 * obfuscated C ;-) */

/* type bufer[size] with SvCUR set to cur */
#define dRETBUF(type, size, cur) \
    type RETBUF; \
    RETVAL = newSV(size); \
    SvPOK_on(RETVAL); \
    SvCUR_set(RETVAL, cur); \
    RETBUF = (type)SvPVX(RETVAL)

/* sizeof(type) buffer with SvCUR set to size */
#define dRETBUFs(type, size) dRETBUF(type, size, size)

/* null terminated buffer with size + 1 bytes and SvCUR set to size */
#define dRETBUFz(size) \
    dRETBUF(char *, size + 1, size); \
    RETBUF[size + 1] = 0

#define dUUIDRETBUF dRETBUFs(unsigned char *, sizeof(uuid_t))
#define dSTRRETBUF  dRETBUFs(char *, UUID_STRING_SIZE)
#define dHEXRETBUF  dRETBUFz(UUID_HEX_SIZE)

/* FIXME uuid_time, uuid_type, uuid_variant are available in libuuid but not in
 * darwin's uuid.h... consider exposing? */

static pid_t last_pid = 0;

/* PERL_DARWIN only, and fails with
 * -Werror=implicit-function-declaration
 * cf. #1065783

inline STATIC void pid_check () {
    if ( getpid() != last_pid ) {
        last_pid = getpid();
        arc4random_stir();
    }
}
*/

/* generates a new UUID of a given version */
STATIC void new_uuid (IV version, uuid_t uuid) {
    PID_CHECK;

    switch (version) {
        case UUID_TYPE_TIME:
            uuid_generate_time(uuid);
            break;
        case UUID_TYPE_RANDOM:
            uuid_generate_random(uuid);
            break;
        default:
            uuid_generate(uuid);
    }
}

STATIC IV hex_to_uuid (uuid_t uuid, char *pv) {
    int i;

    Zero(uuid, 1, uuid_t);

    /* decode hex */
    for ( i = 0; i < sizeof(uuid_t); i++ ) {
        if ( !isALNUM(*pv) )
            return 0;
    }

    for ( i = 0; i < sizeof(uuid_t); i++ ) {
        /* left nybble */
        if ( isDIGIT(*pv) )
            uuid[i] |= ( *pv++ << 4 ) & 0xf0;
        else
            uuid[i] |= ( (*pv++ + 9) << 4 ) & 0xf0;

        /* right nybble */
        if ( isDIGIT(*pv) )
            uuid[i] |= *pv++ & 0xf;
        else
            uuid[i] |= (*pv++ + 9) & 0xf;

    }

    return 1;
}

/* hex-string, hex, base64 (TODO), or binary sv to uuid_t */
STATIC IV sv_to_uuid (SV *sv, uuid_t uuid) {
    dSP;

    if ( SvPOK(sv) || sv_isobject(sv) ) {
        char *pv;
        STRLEN len;

        if ( SvPOK(sv) ) {
            pv = SvPV_nolen(sv);
            len = SvCUR(sv);
        } else {
            pv = SvPV(sv, len);
        }

        switch ( len ) {
            case UUID_HEX_SIZE:
                return hex_to_uuid(uuid, pv);
            case UUID_BASE64_SIZE:
            case UUID_BASE64_LF_SIZE:
            case UUID_BASE64_CRLF_SIZE:

                load_module(PERL_LOADMOD_NOIMPORT, newSVpvs("MIME::Base64"), NULL);

                PUSHMARK(SP);
                XPUSHs(sv);
                PUTBACK;

                call_pv("MIME::Base64::decode_base64", G_SCALAR);

                SPAGAIN;
                pv = SvPV(TOPs, len);

                /* check that the decoded result looks plausible */
                if (len != sizeof(uuid_t))
                    return 0;

                /* fall through */
            case sizeof(uuid_t):
                uuid_copy(uuid, *(uuid_t *)pv);
                return 1;
            case UUID_STRING_SIZE:
                if ( uuid_parse(pv, uuid) == 0 )
                    return 1;
        }
    }

    return 0;
}


MODULE = Data::UUID::LibUUID            PACKAGE = Data::UUID::LibUUID
PROTOTYPES: ENABLE
BOOT:
    last_pid = getpid();

SV*
uuid_eq(uu1_sv, uu2_sv)
    SV *uu1_sv;
    SV *uu2_sv;
    PROTOTYPE: $$
    PREINIT:
        uuid_t uu1;
        uuid_t uu2;
    PPCODE:
        if ( sv_to_uuid(uu1_sv, uu1) && sv_to_uuid(uu2_sv, uu2) )
            if ( uuid_compare(uu1, uu2) == 0 )
                XSRETURN_YES;
            else
                XSRETURN_NO;
        else
            XSRETURN_UNDEF;

SV*
uuid_compare(uu1_sv, uu2_sv)
    SV *uu1_sv;
    SV *uu2_sv;
    PROTOTYPE: $$
    PREINIT:
        uuid_t uu1;
        uuid_t uu2;
    PPCODE:
        if ( sv_to_uuid(uu1_sv, uu1) && sv_to_uuid(uu2_sv, uu2) )
            XSRETURN_IV(uuid_compare(uu1, uu2));
        else
            XSRETURN_UNDEF;

SV*
new_uuid_binary(...)
    PROTOTYPE: ;$
    PREINIT:
        IV version = UUID_TYPE_TIME;
    CODE:
        dUUIDRETBUF;

        if ( items == 1 ) version = SvIV(ST(0));

        new_uuid(version, RETBUF);
    OUTPUT: RETVAL

SV*
new_uuid_string(...)
    PROTOTYPE: ;$
    PREINIT:
        uuid_t uuid;
        IV version = UUID_TYPE_TIME;
    CODE:
        dSTRRETBUF;

        if ( items == 1 ) version = SvIV(ST(0));

        new_uuid(version, uuid);
        uuid_unparse(uuid, RETBUF);
    OUTPUT: RETVAL

SV*
uuid_to_string(sv)
    SV *sv
    PROTOTYPE: $
    PREINIT:
        uuid_t uuid;
    CODE:
        if ( sv_to_uuid(sv, uuid) ) {
            dSTRRETBUF;
            uuid_unparse(uuid, RETBUF);
        } else
            XSRETURN_UNDEF;
    OUTPUT: RETVAL

SV*
uuid_to_binary(sv)
    SV *sv
    PROTOTYPE: $
    CODE:
        dUUIDRETBUF;
        if ( !sv_to_uuid(sv, RETBUF) )
            XSRETURN_UNDEF;
    OUTPUT: RETVAL

SV*
uuid_to_hex(sv)
    SV *sv
    PROTOTYPE: $
    PREINIT:
        uuid_t uuid;
    CODE:
        if ( sv_to_uuid(sv, uuid) ) {
            int i;
            U8 bits = 0;
            U8 *uuid_ptr = (U8 *)uuid;
            dHEXRETBUF;

            for (i = 0; i < UUID_HEX_SIZE; i++) {
                if (i & 1) bits <<= 4;
                else bits = *uuid_ptr++;
                RETBUF[i] = PL_hexdigit[(bits >> 4) & 15];
            }
        } else XSRETURN_UNDEF;
    OUTPUT: RETVAL

SV*
new_dce_uuid_binary(...)
    CODE:
        dUUIDRETBUF;
        PID_CHECK;
        uuid_generate(RETBUF);
    OUTPUT: RETVAL

SV*
new_dce_uuid_string(...)
    PREINIT:
        uuid_t uuid;
    CODE:
        dSTRRETBUF;
        PID_CHECK;
        uuid_generate(uuid);
        uuid_unparse(uuid, RETBUF);
    OUTPUT: RETVAL