1 | #include "EXTERN.h" |
1 | #include "EXTERN.h" |
2 | #include "perl.h" |
2 | #include "perl.h" |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
4 | |
4 | |
5 | // C99 required |
5 | // C99 required! |
|
|
6 | // this is not just for comments, but also for |
|
|
7 | // integer constant semantics, |
|
|
8 | // sscanf format modifiers and more. |
6 | |
9 | |
7 | enum { |
10 | enum { |
8 | // ASN_TAG |
11 | // ASN_TAG |
9 | ASN_BOOLEAN = 0x01, |
12 | ASN_BOOLEAN = 0x01, |
10 | ASN_INTEGER32 = 0x02, |
13 | ASN_INTEGER = 0x02, |
11 | ASN_BIT_STRING = 0x03, |
14 | ASN_BIT_STRING = 0x03, |
12 | ASN_OCTET_STRING = 0x04, |
15 | ASN_OCTET_STRING = 0x04, |
13 | ASN_NULL = 0x05, |
16 | ASN_NULL = 0x05, |
14 | ASN_OBJECT_IDENTIFIER = 0x06, |
17 | ASN_OBJECT_IDENTIFIER = 0x06, |
15 | ASN_OID = 0x06, //X |
18 | ASN_OID = 0x06, |
16 | ASN_OBJECT_DESCRIPTOR = 0x07, //X |
19 | ASN_OBJECT_DESCRIPTOR = 0x07, |
17 | ASN_EXTERNAL = 0x08, //X |
20 | ASN_EXTERNAL = 0x08, |
18 | ASN_REAL = 0x09, //X |
21 | ASN_REAL = 0x09, |
19 | ASN_ENUMERATED = 0x0a, //X |
22 | ASN_ENUMERATED = 0x0a, |
20 | ASN_EMBEDDED_PDV = 0x0b, //X |
23 | ASN_EMBEDDED_PDV = 0x0b, |
21 | ASN_UTF8_STRING = 0x0c, //X |
24 | ASN_UTF8_STRING = 0x0c, |
22 | ASN_RELATIVE_OID = 0x0d, //X |
25 | ASN_RELATIVE_OID = 0x0d, |
23 | ASN_SEQUENCE = 0x10, |
26 | ASN_SEQUENCE = 0x10, |
24 | ASN_SET = 0x11, //X |
27 | ASN_SET = 0x11, |
25 | ASN_NUMERIC_STRING = 0x12, //X |
28 | ASN_NUMERIC_STRING = 0x12, |
26 | ASN_PRINTABLE_STRING = 0x13, //X |
29 | ASN_PRINTABLE_STRING = 0x13, |
27 | ASN_TELETEX_STRING = 0x14, //X |
30 | ASN_TELETEX_STRING = 0x14, |
28 | ASN_T61_STRING = 0x14, //X |
31 | ASN_T61_STRING = 0x14, |
29 | ASN_VIDEOTEX_STRING = 0x15, //X |
32 | ASN_VIDEOTEX_STRING = 0x15, |
30 | ASN_IA5_STRING = 0x16, //X |
33 | ASN_IA5_STRING = 0x16, |
31 | ASN_ASCII_STRING = 0x16, //X |
34 | ASN_ASCII_STRING = 0x16, |
32 | ASN_UTC_TIME = 0x17, //X |
35 | ASN_UTC_TIME = 0x17, |
33 | ASN_GENERALIZED_TIME = 0x18, //X |
36 | ASN_GENERALIZED_TIME = 0x18, |
34 | ASN_GRAPHIC_STRING = 0x19, //X |
37 | ASN_GRAPHIC_STRING = 0x19, |
35 | ASN_VISIBLE_STRING = 0x1a, //X |
38 | ASN_VISIBLE_STRING = 0x1a, |
36 | ASN_ISO646_STRING = 0x1a, //X |
39 | ASN_ISO646_STRING = 0x1a, |
37 | ASN_GENERAL_STRING = 0x1b, //X |
40 | ASN_GENERAL_STRING = 0x1b, |
38 | ASN_UNIVERSAL_STRING = 0x1c, //X |
41 | ASN_UNIVERSAL_STRING = 0x1c, |
39 | ASN_CHARACTER_STRING = 0x1d, //X |
42 | ASN_CHARACTER_STRING = 0x1d, |
40 | ASN_BMP_STRING = 0x1e, //X |
43 | ASN_BMP_STRING = 0x1e, |
41 | |
44 | |
42 | ASN_TAG_BER = 0x1f, |
45 | ASN_TAG_BER = 0x1f, |
43 | ASN_TAG_MASK = 0x1f, |
46 | ASN_TAG_MASK = 0x1f, |
44 | |
47 | |
45 | // primitive/constructed |
48 | // primitive/constructed |
… | |
… | |
92 | |
95 | |
93 | static profile_type *cur_profile, *default_profile; |
96 | static profile_type *cur_profile, *default_profile; |
94 | static SV *buf_sv; // encoding buffer |
97 | static SV *buf_sv; // encoding buffer |
95 | static U8 *buf, *cur, *end; // buffer start, current, end |
98 | static U8 *buf, *cur, *end; // buffer start, current, end |
96 | |
99 | |
|
|
100 | #if PERL_VERSION < 18 |
|
|
101 | # define utf8_to_uvchr_buf(s,e,l) utf8_to_uvchr (s, l) |
|
|
102 | #endif |
|
|
103 | |
97 | #if __GNUC__ >= 3 |
104 | #if __GNUC__ >= 3 |
98 | # define expect(expr,value) __builtin_expect ((expr), (value)) |
105 | # define expect(expr,value) __builtin_expect ((expr), (value)) |
99 | # define INLINE static inline |
106 | # define INLINE static inline |
100 | #else |
107 | #else |
101 | # define expect(expr,value) (expr) |
108 | # define expect(expr,value) (expr) |
… | |
… | |
155 | return BER_TYPE_BYTES; |
162 | return BER_TYPE_BYTES; |
156 | |
163 | |
157 | return SvPVX (sv)[idx]; |
164 | return SvPVX (sv)[idx]; |
158 | } |
165 | } |
159 | |
166 | |
160 | static int |
167 | static void |
161 | profile_set (profile_type *profile, int klass, int tag, int type) |
168 | profile_set (profile_type *profile, int klass, int tag, int type) |
162 | { |
169 | { |
163 | SV *sv = (SV *)profile; |
170 | SV *sv = (SV *)profile; |
164 | U32 idx = (tag << 2) + klass; |
171 | U32 idx = (tag << 2) + klass; |
165 | STRLEN oldlen = SvCUR (sv); |
172 | STRLEN oldlen = SvCUR (sv); |
… | |
… | |
174 | |
181 | |
175 | SvPVX (sv)[idx] = type; |
182 | SvPVX (sv)[idx] = type; |
176 | } |
183 | } |
177 | |
184 | |
178 | static SV * |
185 | static SV * |
179 | profile_new () |
186 | profile_new (void) |
180 | { |
187 | { |
181 | SV *sv = newSVpvn ("", 0); |
188 | SV *sv = newSVpvn ("", 0); |
182 | |
189 | |
183 | static const struct { |
190 | static const struct { |
184 | int klass; |
191 | int klass; |
185 | int tag; |
192 | int tag; |
186 | int type; |
193 | int type; |
187 | } *celem, default_map[] = { |
194 | } *celem, default_map[] = { |
188 | { ASN_UNIVERSAL, ASN_BOOLEAN , BER_TYPE_BOOL }, |
195 | { ASN_UNIVERSAL, ASN_BOOLEAN , BER_TYPE_BOOL }, |
189 | { ASN_UNIVERSAL, ASN_INTEGER32 , BER_TYPE_INT }, |
196 | { ASN_UNIVERSAL, ASN_INTEGER , BER_TYPE_INT }, |
190 | { ASN_UNIVERSAL, ASN_NULL , BER_TYPE_NULL }, |
197 | { ASN_UNIVERSAL, ASN_NULL , BER_TYPE_NULL }, |
191 | { ASN_UNIVERSAL, ASN_OBJECT_IDENTIFIER, BER_TYPE_OID }, |
198 | { ASN_UNIVERSAL, ASN_OBJECT_IDENTIFIER, BER_TYPE_OID }, |
192 | { ASN_UNIVERSAL, ASN_OBJECT_DESCRIPTOR, BER_TYPE_OID }, |
|
|
193 | { ASN_UNIVERSAL, ASN_RELATIVE_OID , BER_TYPE_RELOID }, |
199 | { ASN_UNIVERSAL, ASN_RELATIVE_OID , BER_TYPE_RELOID }, |
194 | { ASN_UNIVERSAL, ASN_REAL , BER_TYPE_REAL }, |
200 | { ASN_UNIVERSAL, ASN_REAL , BER_TYPE_REAL }, |
|
|
201 | { ASN_UNIVERSAL, ASN_ENUMERATED , BER_TYPE_INT }, |
195 | { ASN_UNIVERSAL, ASN_UTF8_STRING , BER_TYPE_UTF8 }, |
202 | { ASN_UNIVERSAL, ASN_UTF8_STRING , BER_TYPE_UTF8 }, |
196 | { ASN_UNIVERSAL, ASN_BMP_STRING , BER_TYPE_UCS2 }, |
203 | { ASN_UNIVERSAL, ASN_BMP_STRING , BER_TYPE_UCS2 }, |
197 | { ASN_UNIVERSAL, ASN_UNIVERSAL_STRING , BER_TYPE_UCS4 }, |
204 | { ASN_UNIVERSAL, ASN_UNIVERSAL_STRING , BER_TYPE_UCS4 }, |
198 | }; |
205 | }; |
199 | |
206 | |
200 | for (celem = default_map + sizeof (default_map) / sizeof (default_map [0]); celem > default_map; celem--) |
207 | for (celem = default_map + sizeof (default_map) / sizeof (default_map [0]); celem-- > default_map; ) |
201 | profile_set ((void *)sv, celem->klass, celem->tag, celem->type); |
208 | profile_set ((profile_type *)sv, celem->klass, celem->tag, celem->type); |
202 | |
209 | |
203 | return sv_bless (newRV_noinc (sv), profile_stash); |
210 | return sv_bless (newRV_noinc (sv), profile_stash); |
204 | } |
211 | } |
205 | |
212 | |
206 | ///////////////////////////////////////////////////////////////////////////// |
213 | ///////////////////////////////////////////////////////////////////////////// |
… | |
… | |
241 | |
248 | |
242 | return *cur++; |
249 | return *cur++; |
243 | } |
250 | } |
244 | |
251 | |
245 | // get ber-encoded integer (i.e. pack "w") |
252 | // get ber-encoded integer (i.e. pack "w") |
246 | static U32 |
253 | static UV |
247 | get_w (void) |
254 | get_w (void) |
248 | { |
255 | { |
249 | U32 res = 0; |
256 | UV res = 0; |
250 | |
257 | |
251 | for (;;) |
258 | for (;;) |
252 | { |
259 | { |
253 | U8 c = get_u8 (); |
260 | U8 c = get_u8 (); |
254 | res = (res << 7) | (c & 0x7f); |
261 | res = (res << 7) | (c & 0x7f); |
… | |
… | |
256 | if (!(c & 0x80)) |
263 | if (!(c & 0x80)) |
257 | return res; |
264 | return res; |
258 | } |
265 | } |
259 | } |
266 | } |
260 | |
267 | |
261 | static U32 |
268 | static UV |
262 | get_length (void) |
269 | get_length (void) |
263 | { |
270 | { |
264 | U32 res = get_u8 (); |
271 | UV res = get_u8 (); |
265 | |
272 | |
266 | if (res & 0x80) |
273 | if (res & 0x80) |
267 | { |
274 | { |
268 | int cnt = res & 0x7f; |
275 | int cnt = res & 0x7f; |
269 | res = 0; |
276 | res = 0; |
… | |
… | |
276 | |
283 | |
277 | default: |
284 | default: |
278 | error ("ASN.1 length too long"); |
285 | error ("ASN.1 length too long"); |
279 | return 0; |
286 | return 0; |
280 | |
287 | |
|
|
288 | case 8: res = (res << 8) | get_u8 (); |
|
|
289 | case 7: res = (res << 8) | get_u8 (); |
|
|
290 | case 6: res = (res << 8) | get_u8 (); |
|
|
291 | case 5: res = (res << 8) | get_u8 (); |
281 | case 4: res = (res << 8) | get_u8 (); |
292 | case 4: res = (res << 8) | get_u8 (); |
282 | case 3: res = (res << 8) | get_u8 (); |
293 | case 3: res = (res << 8) | get_u8 (); |
283 | case 2: res = (res << 8) | get_u8 (); |
294 | case 2: res = (res << 8) | get_u8 (); |
284 | case 1: res = (res << 8) | get_u8 (); |
295 | case 1: res = (res << 8) | get_u8 (); |
285 | } |
296 | } |
… | |
… | |
287 | |
298 | |
288 | return res; |
299 | return res; |
289 | } |
300 | } |
290 | |
301 | |
291 | static SV * |
302 | static SV * |
292 | decode_int () |
303 | decode_int (void) |
293 | { |
304 | { |
294 | int len = get_length (); |
305 | UV len = get_length (); |
295 | |
306 | |
296 | if (len <= 0) |
307 | if (!len) |
297 | { |
308 | { |
298 | error ("integer length equal to zero"); |
309 | error ("invalid integer length equal to zero"); |
299 | return 0; |
310 | return 0; |
300 | } |
311 | } |
301 | |
312 | |
302 | U8 *data = get_n (len); |
313 | U8 *data = get_n (len); |
303 | |
314 | |
… | |
… | |
315 | } |
326 | } |
316 | |
327 | |
317 | static SV * |
328 | static SV * |
318 | decode_data (void) |
329 | decode_data (void) |
319 | { |
330 | { |
320 | U32 len = get_length (); |
331 | UV len = get_length (); |
321 | U8 *data = get_n (len); |
|
|
322 | return newSVpvn ((char *)data, len); |
332 | return newSVpvn ((char *)get_n (len), len); |
323 | } |
333 | } |
324 | |
334 | |
325 | // gelper for decode_object_identifier |
335 | // helper for decode_object_identifier |
326 | static char * |
336 | static char * |
327 | write_uv (char *buf, U32 u) |
337 | write_uv (char *buf, UV u) |
328 | { |
338 | { |
329 | // the one-digit case is absolutely predominant, so this pays off (hopefully) |
339 | // the one-digit case is absolutely predominant, so this pays off (hopefully) |
330 | if (expect_true (u < 10)) |
340 | if (expect_true (u < 10)) |
331 | *buf++ = u + '0'; |
341 | *buf++ = u + '0'; |
332 | else |
342 | else |
333 | { |
343 | { |
|
|
344 | // this *could* be done much faster using branchless fixed-point arithmetics |
334 | char *beg = buf; |
345 | char *beg = buf; |
335 | |
346 | |
336 | do |
347 | do |
337 | { |
348 | { |
338 | *buf++ = u % 10 + '0'; |
349 | *buf++ = u % 10 + '0'; |
339 | u /= 10; |
350 | u /= 10; |
340 | } |
351 | } |
341 | while (u); |
352 | while (u); |
342 | |
353 | |
343 | // reverse digits |
354 | // reverse digits |
344 | for (char *ptr = buf; --ptr != beg; ++beg) |
355 | char *ptr = buf; |
|
|
356 | while (--ptr > beg) |
345 | { |
357 | { |
346 | char c = *ptr; |
358 | char c = *ptr; |
347 | *ptr = *beg; |
359 | *ptr = *beg; |
348 | *beg = c; |
360 | *beg = c; |
|
|
361 | ++beg; |
349 | } |
362 | } |
350 | } |
363 | } |
351 | |
364 | |
352 | return buf; |
365 | return buf; |
353 | } |
366 | } |
354 | |
367 | |
355 | static SV * |
368 | static SV * |
356 | decode_oid (int relative) |
369 | decode_oid (int relative) |
357 | { |
370 | { |
358 | U32 len = get_length (); |
371 | UV len = get_length (); |
359 | |
372 | |
360 | if (len <= 0) |
373 | if (len <= 0) |
361 | { |
374 | { |
362 | error ("OBJECT IDENTIFIER length equal to zero"); |
375 | error ("OBJECT IDENTIFIER length equal to zero"); |
363 | return &PL_sv_undef; |
376 | return &PL_sv_undef; |
364 | } |
377 | } |
365 | |
378 | |
366 | U8 *end = cur + len; |
379 | U8 *end = cur + len; |
367 | U32 w = get_w (); |
380 | UV w = get_w (); |
368 | |
381 | |
369 | static char oid[MAX_OID_STRLEN]; // must be static |
382 | static char oid[MAX_OID_STRLEN]; // static, becaueds too large for stack |
370 | char *app = oid; |
383 | char *app = oid; |
371 | |
384 | |
372 | if (relative) |
385 | if (relative) |
373 | app = write_uv (app, w); |
386 | app = write_uv (app, w); |
374 | else |
387 | else |
… | |
… | |
376 | app = write_uv (app, (U8)w / 40); |
389 | app = write_uv (app, (U8)w / 40); |
377 | *app++ = '.'; |
390 | *app++ = '.'; |
378 | app = write_uv (app, (U8)w % 40); |
391 | app = write_uv (app, (U8)w % 40); |
379 | } |
392 | } |
380 | |
393 | |
|
|
394 | while (cur < end) |
|
|
395 | { |
381 | // we assume an oid component is never > 64 bytes |
396 | // we assume an oid component is never > 64 digits |
382 | while (cur < end && oid + sizeof (oid) - app > 64) |
397 | if (oid + sizeof (oid) - app < 64) |
383 | { |
398 | croak ("BER_TYPE_OID to long to decode"); |
|
|
399 | |
384 | w = get_w (); |
400 | w = get_w (); |
385 | *app++ = '.'; |
401 | *app++ = '.'; |
386 | app = write_uv (app, w); |
402 | app = write_uv (app, w); |
387 | } |
403 | } |
388 | |
404 | |
… | |
… | |
393 | static SV * |
409 | static SV * |
394 | decode_ucs (int chrsize) |
410 | decode_ucs (int chrsize) |
395 | { |
411 | { |
396 | SV *res = NEWSV (0, 0); |
412 | SV *res = NEWSV (0, 0); |
397 | |
413 | |
398 | U32 len = get_length (); |
414 | UV len = get_length (); |
399 | |
415 | |
400 | if (len & (chrsize - 1)) |
416 | if (len & (chrsize - 1)) |
401 | croak ("BER_TYPE_UCS has an invalid number of octets (%d)", len); |
417 | croak ("BER_TYPE_UCS has an invalid number of octets (%d)", len); |
402 | |
418 | |
403 | while (len) |
419 | while (len) |
… | |
… | |
424 | |
440 | |
425 | return res; |
441 | return res; |
426 | } |
442 | } |
427 | |
443 | |
428 | static SV * |
444 | static SV * |
429 | decode_ber () |
445 | decode_ber (void) |
430 | { |
446 | { |
431 | int identifier = get_u8 (); |
447 | int identifier = get_u8 (); |
432 | |
448 | |
433 | SV *res; |
449 | SV *res; |
434 | |
450 | |
… | |
… | |
442 | if (tag == ASN_TAG_BER) |
458 | if (tag == ASN_TAG_BER) |
443 | tag = get_w (); |
459 | tag = get_w (); |
444 | |
460 | |
445 | if (constructed) |
461 | if (constructed) |
446 | { |
462 | { |
447 | U32 len = get_length (); |
463 | UV len = get_length (); |
448 | U32 seqend = (cur - buf) + len; |
464 | UV seqend = (cur - buf) + len; |
449 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
465 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
450 | |
466 | |
451 | while (cur < buf + seqend) |
467 | while (cur < buf + seqend) |
452 | av_push (av, decode_ber ()); |
468 | av_push (av, decode_ber ()); |
453 | |
469 | |
… | |
… | |
458 | } |
474 | } |
459 | else |
475 | else |
460 | switch (profile_lookup (cur_profile, klass, tag)) |
476 | switch (profile_lookup (cur_profile, klass, tag)) |
461 | { |
477 | { |
462 | case BER_TYPE_NULL: |
478 | case BER_TYPE_NULL: |
|
|
479 | { |
|
|
480 | UV len = get_length (); |
|
|
481 | |
|
|
482 | if (len) |
|
|
483 | croak ("BER_TYPE_NULL value with non-zero length %d encountered", len); |
|
|
484 | |
463 | res = &PL_sv_undef; |
485 | res = &PL_sv_undef; |
|
|
486 | } |
464 | break; |
487 | break; |
465 | |
488 | |
466 | case BER_TYPE_BOOL: |
489 | case BER_TYPE_BOOL: |
467 | { |
490 | { |
468 | U32 len = get_length (); |
491 | UV len = get_length (); |
469 | |
492 | |
470 | if (len != 1) |
493 | if (len != 1) |
471 | croak ("BER_TYPE_BOOLEAN type with invalid length %d encountered", len); |
494 | croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered", len); |
472 | |
495 | |
473 | res = newSVcacheint (get_u8 () ? 0 : 1); |
496 | res = newSVcacheint (!!get_u8 ()); |
474 | } |
497 | } |
475 | break; |
498 | break; |
476 | |
499 | |
477 | case BER_TYPE_OID: |
500 | case BER_TYPE_OID: |
478 | res = decode_oid (0); |
501 | res = decode_oid (0); |
… | |
… | |
495 | res = decode_data (); |
518 | res = decode_data (); |
496 | break; |
519 | break; |
497 | |
520 | |
498 | case BER_TYPE_IPADDRESS: |
521 | case BER_TYPE_IPADDRESS: |
499 | { |
522 | { |
500 | U32 len = get_length (); |
523 | UV len = get_length (); |
501 | |
524 | |
502 | if (len != 4) |
525 | if (len != 4) |
503 | croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered", len); |
526 | croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered", len); |
504 | |
527 | |
505 | U8 c1 = get_u8 (); |
528 | U8 c1 = get_u8 (); |
… | |
… | |
553 | static void |
576 | static void |
554 | set_buf (SV *sv) |
577 | set_buf (SV *sv) |
555 | { |
578 | { |
556 | STRLEN len; |
579 | STRLEN len; |
557 | buf_sv = sv; |
580 | buf_sv = sv; |
558 | buf = SvPVbyte (buf_sv, len); |
581 | buf = (U8 *)SvPVbyte (buf_sv, len); |
559 | cur = buf; |
582 | cur = buf; |
560 | end = buf + len; |
583 | end = buf + len; |
561 | } |
584 | } |
562 | |
585 | |
563 | /* similar to SvGROW, but somewhat safer and guarantees exponential realloc strategy */ |
586 | /* similar to SvGROW, but somewhat safer and guarantees exponential realloc strategy */ |
… | |
… | |
577 | need (STRLEN len) |
600 | need (STRLEN len) |
578 | { |
601 | { |
579 | if (expect_false ((uintptr_t)(end - cur) < len)) |
602 | if (expect_false ((uintptr_t)(end - cur) < len)) |
580 | { |
603 | { |
581 | STRLEN pos = cur - buf; |
604 | STRLEN pos = cur - buf; |
582 | buf = my_sv_grow (buf_sv, pos, len); |
605 | buf = (U8 *)my_sv_grow (buf_sv, pos, len); |
583 | cur = buf + pos; |
606 | cur = buf + pos; |
584 | end = buf + SvLEN (buf_sv) - 1; |
607 | end = buf + SvLEN (buf_sv) - 1; |
585 | } |
608 | } |
586 | } |
609 | } |
587 | |
610 | |
… | |
… | |
591 | need (1); |
614 | need (1); |
592 | *cur++ = val; |
615 | *cur++ = val; |
593 | } |
616 | } |
594 | |
617 | |
595 | static void |
618 | static void |
596 | put_w_nocheck (U32 val) |
619 | put_w_nocheck (UV val) |
597 | { |
620 | { |
|
|
621 | #if UVSIZE > 4 |
|
|
622 | *cur = (val >> 7 * 9) | 0x80; cur += val >= ((UV)1 << (7 * 9)); |
|
|
623 | *cur = (val >> 7 * 8) | 0x80; cur += val >= ((UV)1 << (7 * 8)); |
|
|
624 | *cur = (val >> 7 * 7) | 0x80; cur += val >= ((UV)1 << (7 * 7)); |
|
|
625 | *cur = (val >> 7 * 6) | 0x80; cur += val >= ((UV)1 << (7 * 6)); |
|
|
626 | *cur = (val >> 7 * 5) | 0x80; cur += val >= ((UV)1 << (7 * 5)); |
|
|
627 | #endif |
598 | *cur = (val >> 7 * 4) | 0x80; cur += val >= (1 << (7 * 4)); |
628 | *cur = (val >> 7 * 4) | 0x80; cur += val >= ((UV)1 << (7 * 4)); |
599 | *cur = (val >> 7 * 3) | 0x80; cur += val >= (1 << (7 * 3)); |
629 | *cur = (val >> 7 * 3) | 0x80; cur += val >= ((UV)1 << (7 * 3)); |
600 | *cur = (val >> 7 * 2) | 0x80; cur += val >= (1 << (7 * 2)); |
630 | *cur = (val >> 7 * 2) | 0x80; cur += val >= ((UV)1 << (7 * 2)); |
601 | *cur = (val >> 7 * 1) | 0x80; cur += val >= (1 << (7 * 1)); |
631 | *cur = (val >> 7 * 1) | 0x80; cur += val >= ((UV)1 << (7 * 1)); |
602 | *cur = val & 0x7f; cur += 1; |
632 | *cur = val & 0x7f; cur += 1; |
603 | } |
633 | } |
604 | |
634 | |
605 | static void |
635 | static void |
606 | put_w (U32 val) |
636 | put_w (UV val) |
607 | { |
637 | { |
608 | need (5); // we only handle up to 5 bytes |
638 | need (5); // we only handle up to 5 bytes |
609 | |
639 | |
610 | put_w_nocheck (val); |
640 | put_w_nocheck (val); |
611 | } |
641 | } |
612 | |
642 | |
613 | static U8 * |
643 | static U8 * |
614 | put_length_at (U32 val, U8 *cur) |
644 | put_length_at (UV val, U8 *cur) |
615 | { |
645 | { |
616 | if (val < 0x7fU) |
646 | if (val < 0x7fU) |
617 | *cur++ = val; |
647 | *cur++ = val; |
618 | else |
648 | else |
619 | { |
649 | { |
620 | U8 *lenb = cur++; |
650 | U8 *lenb = cur++; |
621 | |
651 | |
|
|
652 | #if UVSIZE > 4 |
|
|
653 | *cur = val >> 56; cur += *cur > 0; |
|
|
654 | *cur = val >> 48; cur += *cur > 0; |
|
|
655 | *cur = val >> 40; cur += *cur > 0; |
|
|
656 | *cur = val >> 32; cur += *cur > 0; |
|
|
657 | #endif |
622 | *cur = val >> 24; cur += *cur > 0; |
658 | *cur = val >> 24; cur += *cur > 0; |
623 | *cur = val >> 16; cur += *cur > 0; |
659 | *cur = val >> 16; cur += *cur > 0; |
624 | *cur = val >> 8; cur += *cur > 0; |
660 | *cur = val >> 8; cur += *cur > 0; |
625 | *cur = val ; cur += 1; |
661 | *cur = val ; cur += 1; |
626 | |
662 | |
… | |
… | |
629 | |
665 | |
630 | return cur; |
666 | return cur; |
631 | } |
667 | } |
632 | |
668 | |
633 | static void |
669 | static void |
634 | put_length (U32 val) |
670 | put_length (UV val) |
635 | { |
671 | { |
636 | need (5 + val); |
672 | need (5 + val); |
637 | cur = put_length_at (val, cur); |
673 | cur = put_length_at (val, cur); |
638 | } |
674 | } |
639 | |
675 | |
640 | // return how many bytes the encoded length requires |
676 | // return how many bytes the encoded length requires |
641 | static int length_length (U32 val) |
677 | static int length_length (UV val) |
642 | { |
678 | { |
643 | return val < 0x7fU |
679 | return val < 0x7fU |
644 | ? 1 |
680 | ? 1 |
645 | : 2 + (val > 0xffU) + (val > 0xffffU) + (val > 0xffffffU); |
681 | : 2 |
|
|
682 | + (val > 0xffU) |
|
|
683 | + (val > 0xffffU) |
|
|
684 | + (val > 0xffffffU) |
|
|
685 | #if UVSIZE > 4 |
|
|
686 | + (val > 0xffffffffU) |
|
|
687 | + (val > 0xffffffffffU) |
|
|
688 | + (val > 0xffffffffffffU) |
|
|
689 | + (val > 0xffffffffffffffU) |
|
|
690 | #endif |
|
|
691 | ; |
646 | } |
692 | } |
647 | |
693 | |
648 | static void |
694 | static void |
649 | encode_data (const char *ptr, STRLEN len) |
695 | encode_data (const char *ptr, STRLEN len) |
650 | { |
696 | { |
… | |
… | |
712 | |
758 | |
713 | *lenb = cur - lenb - 1; |
759 | *lenb = cur - lenb - 1; |
714 | } |
760 | } |
715 | |
761 | |
716 | // we don't know the length yet, so we optimistically |
762 | // we don't know the length yet, so we optimistically |
717 | // assume the length will need one octet later. if that |
763 | // assume the length will need one octet later. If that |
718 | // turns out to be wrong, we memove as needed. |
764 | // turns out to be wrong, we memmove as needed. |
719 | // mark the beginning |
765 | // mark the beginning |
720 | static STRLEN |
766 | static STRLEN |
721 | len_fixup_mark () |
767 | len_fixup_mark (void) |
722 | { |
768 | { |
723 | return cur++ - buf; |
769 | return cur++ - buf; |
724 | } |
770 | } |
725 | |
771 | |
726 | // patch up the length |
772 | // patch up the length |
… | |
… | |
813 | put_length (uchars * chrsize); |
859 | put_length (uchars * chrsize); |
814 | |
860 | |
815 | while (uchars--) |
861 | while (uchars--) |
816 | { |
862 | { |
817 | STRLEN uclen; |
863 | STRLEN uclen; |
818 | UV uchr = utf8_to_uvchr_buf (ptr, ptr + len, &uclen); |
864 | UV uchr = utf8_to_uvchr_buf ((U8 *)ptr, (U8 *)ptr + len, &uclen); |
819 | |
865 | |
820 | ptr += uclen; |
866 | ptr += uclen; |
821 | len -= uclen; |
867 | len -= uclen; |
822 | |
868 | |
823 | if (chrsize == 4) |
869 | if (chrsize == 4) |
… | |
… | |
864 | int fill = AvFILL (av); |
910 | int fill = AvFILL (av); |
865 | |
911 | |
866 | if (expect_false (SvRMAGICAL (av))) |
912 | if (expect_false (SvRMAGICAL (av))) |
867 | croak ("BER constructed data must not be tied"); |
913 | croak ("BER constructed data must not be tied"); |
868 | |
914 | |
|
|
915 | int i; |
869 | for (int i = 0; i <= fill; ++i) |
916 | for (i = 0; i <= fill; ++i) |
870 | encode_ber (AvARRAY (av)[i]); |
917 | encode_ber (AvARRAY (av)[i]); |
871 | |
918 | |
872 | len_fixup (mark); |
919 | len_fixup (mark); |
873 | } |
920 | } |
874 | else |
921 | else |
… | |
… | |
951 | const char *name; |
998 | const char *name; |
952 | IV iv; |
999 | IV iv; |
953 | } *civ, const_iv[] = { |
1000 | } *civ, const_iv[] = { |
954 | #define const_iv(name) { # name, name }, |
1001 | #define const_iv(name) { # name, name }, |
955 | const_iv (ASN_BOOLEAN) |
1002 | const_iv (ASN_BOOLEAN) |
956 | const_iv (ASN_INTEGER32) |
1003 | const_iv (ASN_INTEGER) |
957 | const_iv (ASN_BIT_STRING) |
1004 | const_iv (ASN_BIT_STRING) |
958 | const_iv (ASN_OCTET_STRING) |
1005 | const_iv (ASN_OCTET_STRING) |
959 | const_iv (ASN_NULL) |
1006 | const_iv (ASN_NULL) |
960 | const_iv (ASN_OBJECT_IDENTIFIER) |
1007 | const_iv (ASN_OBJECT_IDENTIFIER) |
961 | const_iv (ASN_OBJECT_DESCRIPTOR) |
1008 | const_iv (ASN_OBJECT_DESCRIPTOR) |
… | |
… | |
1024 | ber_decode (SV *ber, SV *profile = &PL_sv_undef) |
1071 | ber_decode (SV *ber, SV *profile = &PL_sv_undef) |
1025 | CODE: |
1072 | CODE: |
1026 | { |
1073 | { |
1027 | cur_profile = SvPROFILE (profile); |
1074 | cur_profile = SvPROFILE (profile); |
1028 | STRLEN len; |
1075 | STRLEN len; |
1029 | buf = SvPVbyte (ber, len); |
1076 | buf = (U8 *)SvPVbyte (ber, len); |
1030 | cur = buf; |
1077 | cur = buf; |
1031 | end = buf + len; |
1078 | end = buf + len; |
1032 | |
1079 | |
1033 | RETVAL = decode_ber (); |
1080 | RETVAL = decode_ber (); |
1034 | } |
1081 | } |
… | |
… | |
1069 | && SvIV (AvARRAY (av)[BER_CONSTRUCTED]) |
1116 | && SvIV (AvARRAY (av)[BER_CONSTRUCTED]) |
1070 | ? AvARRAY (av)[BER_DATA] : &PL_sv_undef); |
1117 | ? AvARRAY (av)[BER_DATA] : &PL_sv_undef); |
1071 | } |
1118 | } |
1072 | |
1119 | |
1073 | void |
1120 | void |
1074 | ber_is_i32 (SV *tuple, SV *value = &PL_sv_undef) |
1121 | ber_is_int (SV *tuple, SV *value = &PL_sv_undef) |
1075 | PPCODE: |
1122 | PPCODE: |
1076 | { |
1123 | { |
1077 | if (!SvOK (tuple)) |
1124 | if (!SvOK (tuple)) |
1078 | XSRETURN_NO; |
1125 | XSRETURN_NO; |
1079 | |
1126 | |
1080 | AV *av = ber_tuple (tuple); |
1127 | AV *av = ber_tuple (tuple); |
1081 | |
1128 | |
1082 | IV data = SvIV (AvARRAY (av)[BER_DATA]); |
1129 | UV data = SvUV (AvARRAY (av)[BER_DATA]); |
1083 | |
1130 | |
1084 | XPUSHs ( |
1131 | XPUSHs ( |
1085 | SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL |
1132 | SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL |
1086 | && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_INTEGER32 |
1133 | && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_INTEGER |
1087 | && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) |
1134 | && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) |
1088 | && (!SvOK (value) || data == SvIV (value)) |
1135 | && (!SvOK (value) || data == SvUV (value)) |
1089 | ? sv_2mortal (data ? newSViv (data) : newSVpv ("0 but true", 0)) |
1136 | ? sv_2mortal (data ? newSVsv (AvARRAY (av)[BER_DATA]) : newSVpv ("0 but true", 0)) |
1090 | : &PL_sv_undef); |
1137 | : &PL_sv_undef); |
1091 | } |
1138 | } |
1092 | |
1139 | |
1093 | void |
1140 | void |
1094 | ber_is_oid (SV *tuple, SV *oid = &PL_sv_undef) |
1141 | ber_is_oid (SV *tuple, SV *oid = &PL_sv_undef) |
… | |
… | |
1123 | SvCUR_set (buf_sv, cur - buf); |
1170 | SvCUR_set (buf_sv, cur - buf); |
1124 | XPUSHs (buf_sv); |
1171 | XPUSHs (buf_sv); |
1125 | } |
1172 | } |
1126 | |
1173 | |
1127 | SV * |
1174 | SV * |
1128 | ber_i32 (IV iv) |
1175 | ber_int (SV *sv) |
1129 | CODE: |
1176 | CODE: |
1130 | { |
1177 | { |
1131 | AV *av = newAV (); |
1178 | AV *av = newAV (); |
1132 | av_fill (av, BER_ARRAYSIZE - 1); |
1179 | av_fill (av, BER_ARRAYSIZE - 1); |
1133 | AvARRAY (av)[BER_CLASS ] = newSVcacheint (ASN_UNIVERSAL); |
1180 | AvARRAY (av)[BER_CLASS ] = newSVcacheint (ASN_UNIVERSAL); |
1134 | AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_INTEGER32); |
1181 | AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_INTEGER); |
1135 | AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (0); |
1182 | AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (0); |
1136 | AvARRAY (av)[BER_DATA ] = newSViv (iv); |
1183 | AvARRAY (av)[BER_DATA ] = newSVsv (sv); |
1137 | RETVAL = newRV_noinc ((SV *)av); |
1184 | RETVAL = newRV_noinc ((SV *)av); |
1138 | } |
1185 | } |
1139 | OUTPUT: RETVAL |
1186 | OUTPUT: RETVAL |
1140 | |
1187 | |
1141 | # TODO: not arrayref, but elements? |
1188 | # TODO: not arrayref, but elements? |