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_INTEGER32 = 0x02, |
… | |
… | |
159 | return BER_TYPE_BYTES; |
162 | return BER_TYPE_BYTES; |
160 | |
163 | |
161 | return SvPVX (sv)[idx]; |
164 | return SvPVX (sv)[idx]; |
162 | } |
165 | } |
163 | |
166 | |
164 | static int |
167 | static void |
165 | profile_set (profile_type *profile, int klass, int tag, int type) |
168 | profile_set (profile_type *profile, int klass, int tag, int type) |
166 | { |
169 | { |
167 | SV *sv = (SV *)profile; |
170 | SV *sv = (SV *)profile; |
168 | U32 idx = (tag << 2) + klass; |
171 | U32 idx = (tag << 2) + klass; |
169 | STRLEN oldlen = SvCUR (sv); |
172 | STRLEN oldlen = SvCUR (sv); |
… | |
… | |
191 | } *celem, default_map[] = { |
194 | } *celem, default_map[] = { |
192 | { ASN_UNIVERSAL, ASN_BOOLEAN , BER_TYPE_BOOL }, |
195 | { ASN_UNIVERSAL, ASN_BOOLEAN , BER_TYPE_BOOL }, |
193 | { ASN_UNIVERSAL, ASN_INTEGER32 , BER_TYPE_INT }, |
196 | { ASN_UNIVERSAL, ASN_INTEGER32 , BER_TYPE_INT }, |
194 | { ASN_UNIVERSAL, ASN_NULL , BER_TYPE_NULL }, |
197 | { ASN_UNIVERSAL, ASN_NULL , BER_TYPE_NULL }, |
195 | { ASN_UNIVERSAL, ASN_OBJECT_IDENTIFIER, BER_TYPE_OID }, |
198 | { ASN_UNIVERSAL, ASN_OBJECT_IDENTIFIER, BER_TYPE_OID }, |
196 | { ASN_UNIVERSAL, ASN_OBJECT_DESCRIPTOR, BER_TYPE_OID }, |
|
|
197 | { ASN_UNIVERSAL, ASN_RELATIVE_OID , BER_TYPE_RELOID }, |
199 | { ASN_UNIVERSAL, ASN_RELATIVE_OID , BER_TYPE_RELOID }, |
198 | { ASN_UNIVERSAL, ASN_REAL , BER_TYPE_REAL }, |
200 | { ASN_UNIVERSAL, ASN_REAL , BER_TYPE_REAL }, |
|
|
201 | { ASN_UNIVERSAL, ASN_ENUMERATED , BER_TYPE_INT }, |
199 | { ASN_UNIVERSAL, ASN_UTF8_STRING , BER_TYPE_UTF8 }, |
202 | { ASN_UNIVERSAL, ASN_UTF8_STRING , BER_TYPE_UTF8 }, |
200 | { ASN_UNIVERSAL, ASN_BMP_STRING , BER_TYPE_UCS2 }, |
203 | { ASN_UNIVERSAL, ASN_BMP_STRING , BER_TYPE_UCS2 }, |
201 | { ASN_UNIVERSAL, ASN_UNIVERSAL_STRING , BER_TYPE_UCS4 }, |
204 | { ASN_UNIVERSAL, ASN_UNIVERSAL_STRING , BER_TYPE_UCS4 }, |
202 | }; |
205 | }; |
203 | |
206 | |
204 | 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; ) |
205 | profile_set ((void *)sv, celem->klass, celem->tag, celem->type); |
208 | profile_set ((profile_type *)sv, celem->klass, celem->tag, celem->type); |
206 | |
209 | |
207 | return sv_bless (newRV_noinc (sv), profile_stash); |
210 | return sv_bless (newRV_noinc (sv), profile_stash); |
208 | } |
211 | } |
209 | |
212 | |
210 | ///////////////////////////////////////////////////////////////////////////// |
213 | ///////////////////////////////////////////////////////////////////////////// |
… | |
… | |
333 | // the one-digit case is absolutely predominant, so this pays off (hopefully) |
336 | // the one-digit case is absolutely predominant, so this pays off (hopefully) |
334 | if (expect_true (u < 10)) |
337 | if (expect_true (u < 10)) |
335 | *buf++ = u + '0'; |
338 | *buf++ = u + '0'; |
336 | else |
339 | else |
337 | { |
340 | { |
|
|
341 | // this *could* be done much faster using branchless fixed-ppint arithmetics |
338 | char *beg = buf; |
342 | char *beg = buf; |
339 | |
343 | |
340 | do |
344 | do |
341 | { |
345 | { |
342 | *buf++ = u % 10 + '0'; |
346 | *buf++ = u % 10 + '0'; |
343 | u /= 10; |
347 | u /= 10; |
344 | } |
348 | } |
345 | while (u); |
349 | while (u); |
346 | |
350 | |
347 | // reverse digits |
351 | // reverse digits |
348 | for (char *ptr = buf; --ptr != beg; ++beg) |
352 | char *ptr = buf; |
|
|
353 | while (--ptr > beg) |
349 | { |
354 | { |
350 | char c = *ptr; |
355 | char c = *ptr; |
351 | *ptr = *beg; |
356 | *ptr = *beg; |
352 | *beg = c; |
357 | *beg = c; |
|
|
358 | ++beg; |
353 | } |
359 | } |
354 | } |
360 | } |
355 | |
361 | |
356 | return buf; |
362 | return buf; |
357 | } |
363 | } |
… | |
… | |
368 | } |
374 | } |
369 | |
375 | |
370 | U8 *end = cur + len; |
376 | U8 *end = cur + len; |
371 | U32 w = get_w (); |
377 | U32 w = get_w (); |
372 | |
378 | |
373 | static char oid[MAX_OID_STRLEN]; // must be static |
379 | static char oid[MAX_OID_STRLEN]; // static, becaueds too large for stack |
374 | char *app = oid; |
380 | char *app = oid; |
375 | |
381 | |
376 | if (relative) |
382 | if (relative) |
377 | app = write_uv (app, w); |
383 | app = write_uv (app, w); |
378 | else |
384 | else |
… | |
… | |
380 | app = write_uv (app, (U8)w / 40); |
386 | app = write_uv (app, (U8)w / 40); |
381 | *app++ = '.'; |
387 | *app++ = '.'; |
382 | app = write_uv (app, (U8)w % 40); |
388 | app = write_uv (app, (U8)w % 40); |
383 | } |
389 | } |
384 | |
390 | |
|
|
391 | while (cur < end) |
|
|
392 | { |
385 | // we assume an oid component is never > 64 bytes |
393 | // we assume an oid component is never > 64 digits |
386 | while (cur < end && oid + sizeof (oid) - app > 64) |
394 | if (oid + sizeof (oid) - app < 64) |
387 | { |
395 | croak ("BER_TYPE_OID to long to decode"); |
|
|
396 | |
388 | w = get_w (); |
397 | w = get_w (); |
389 | *app++ = '.'; |
398 | *app++ = '.'; |
390 | app = write_uv (app, w); |
399 | app = write_uv (app, w); |
391 | } |
400 | } |
392 | |
401 | |
… | |
… | |
462 | } |
471 | } |
463 | else |
472 | else |
464 | switch (profile_lookup (cur_profile, klass, tag)) |
473 | switch (profile_lookup (cur_profile, klass, tag)) |
465 | { |
474 | { |
466 | case BER_TYPE_NULL: |
475 | case BER_TYPE_NULL: |
|
|
476 | { |
|
|
477 | U32 len = get_length (); |
|
|
478 | |
|
|
479 | if (len) |
|
|
480 | croak ("BER_TYPE_NULL value with non-zero length %d encountered", len); |
|
|
481 | |
467 | res = &PL_sv_undef; |
482 | res = &PL_sv_undef; |
|
|
483 | } |
468 | break; |
484 | break; |
469 | |
485 | |
470 | case BER_TYPE_BOOL: |
486 | case BER_TYPE_BOOL: |
471 | { |
487 | { |
472 | U32 len = get_length (); |
488 | U32 len = get_length (); |
473 | |
489 | |
474 | if (len != 1) |
490 | if (len != 1) |
475 | croak ("BER_TYPE_BOOLEAN type with invalid length %d encountered", len); |
491 | croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered", len); |
476 | |
492 | |
477 | res = newSVcacheint (get_u8 () ? 0 : 1); |
493 | res = newSVcacheint (!!get_u8 ()); |
478 | } |
494 | } |
479 | break; |
495 | break; |
480 | |
496 | |
481 | case BER_TYPE_OID: |
497 | case BER_TYPE_OID: |
482 | res = decode_oid (0); |
498 | res = decode_oid (0); |
… | |
… | |
557 | static void |
573 | static void |
558 | set_buf (SV *sv) |
574 | set_buf (SV *sv) |
559 | { |
575 | { |
560 | STRLEN len; |
576 | STRLEN len; |
561 | buf_sv = sv; |
577 | buf_sv = sv; |
562 | buf = SvPVbyte (buf_sv, len); |
578 | buf = (U8 *)SvPVbyte (buf_sv, len); |
563 | cur = buf; |
579 | cur = buf; |
564 | end = buf + len; |
580 | end = buf + len; |
565 | } |
581 | } |
566 | |
582 | |
567 | /* similar to SvGROW, but somewhat safer and guarantees exponential realloc strategy */ |
583 | /* similar to SvGROW, but somewhat safer and guarantees exponential realloc strategy */ |
… | |
… | |
581 | need (STRLEN len) |
597 | need (STRLEN len) |
582 | { |
598 | { |
583 | if (expect_false ((uintptr_t)(end - cur) < len)) |
599 | if (expect_false ((uintptr_t)(end - cur) < len)) |
584 | { |
600 | { |
585 | STRLEN pos = cur - buf; |
601 | STRLEN pos = cur - buf; |
586 | buf = my_sv_grow (buf_sv, pos, len); |
602 | buf = (U8 *)my_sv_grow (buf_sv, pos, len); |
587 | cur = buf + pos; |
603 | cur = buf + pos; |
588 | end = buf + SvLEN (buf_sv) - 1; |
604 | end = buf + SvLEN (buf_sv) - 1; |
589 | } |
605 | } |
590 | } |
606 | } |
591 | |
607 | |
… | |
… | |
817 | put_length (uchars * chrsize); |
833 | put_length (uchars * chrsize); |
818 | |
834 | |
819 | while (uchars--) |
835 | while (uchars--) |
820 | { |
836 | { |
821 | STRLEN uclen; |
837 | STRLEN uclen; |
822 | UV uchr = utf8_to_uvchr_buf (ptr, ptr + len, &uclen); |
838 | UV uchr = utf8_to_uvchr_buf ((U8 *)ptr, (U8 *)ptr + len, &uclen); |
823 | |
839 | |
824 | ptr += uclen; |
840 | ptr += uclen; |
825 | len -= uclen; |
841 | len -= uclen; |
826 | |
842 | |
827 | if (chrsize == 4) |
843 | if (chrsize == 4) |
… | |
… | |
868 | int fill = AvFILL (av); |
884 | int fill = AvFILL (av); |
869 | |
885 | |
870 | if (expect_false (SvRMAGICAL (av))) |
886 | if (expect_false (SvRMAGICAL (av))) |
871 | croak ("BER constructed data must not be tied"); |
887 | croak ("BER constructed data must not be tied"); |
872 | |
888 | |
|
|
889 | int i; |
873 | for (int i = 0; i <= fill; ++i) |
890 | for (i = 0; i <= fill; ++i) |
874 | encode_ber (AvARRAY (av)[i]); |
891 | encode_ber (AvARRAY (av)[i]); |
875 | |
892 | |
876 | len_fixup (mark); |
893 | len_fixup (mark); |
877 | } |
894 | } |
878 | else |
895 | else |
… | |
… | |
1028 | ber_decode (SV *ber, SV *profile = &PL_sv_undef) |
1045 | ber_decode (SV *ber, SV *profile = &PL_sv_undef) |
1029 | CODE: |
1046 | CODE: |
1030 | { |
1047 | { |
1031 | cur_profile = SvPROFILE (profile); |
1048 | cur_profile = SvPROFILE (profile); |
1032 | STRLEN len; |
1049 | STRLEN len; |
1033 | buf = SvPVbyte (ber, len); |
1050 | buf = (U8 *)SvPVbyte (ber, len); |
1034 | cur = buf; |
1051 | cur = buf; |
1035 | end = buf + len; |
1052 | end = buf + len; |
1036 | |
1053 | |
1037 | RETVAL = decode_ber (); |
1054 | RETVAL = decode_ber (); |
1038 | } |
1055 | } |