… | |
… | |
10 | ASN_INTEGER32 = 0x02, |
10 | ASN_INTEGER32 = 0x02, |
11 | ASN_BIT_STRING = 0x03, |
11 | ASN_BIT_STRING = 0x03, |
12 | ASN_OCTET_STRING = 0x04, |
12 | ASN_OCTET_STRING = 0x04, |
13 | ASN_NULL = 0x05, |
13 | ASN_NULL = 0x05, |
14 | ASN_OBJECT_IDENTIFIER = 0x06, |
14 | ASN_OBJECT_IDENTIFIER = 0x06, |
15 | ASN_OID = 0x06, //X |
15 | ASN_OID = 0x06, |
16 | ASN_OBJECT_DESCRIPTOR = 0x07, //X |
16 | ASN_OBJECT_DESCRIPTOR = 0x07, |
17 | ASN_EXTERNAL = 0x08, //X |
17 | ASN_EXTERNAL = 0x08, |
18 | ASN_REAL = 0x09, //X |
18 | ASN_REAL = 0x09, |
19 | ASN_ENUMERATED = 0x0a, //X |
19 | ASN_ENUMERATED = 0x0a, |
20 | ASN_EMBEDDED_PDV = 0x0b, //X |
20 | ASN_EMBEDDED_PDV = 0x0b, |
21 | ASN_UTF8_STRING = 0x0c, //X |
21 | ASN_UTF8_STRING = 0x0c, |
22 | ASN_RELATIVE_OID = 0x0d, //X |
22 | ASN_RELATIVE_OID = 0x0d, |
23 | ASN_SEQUENCE = 0x10, |
23 | ASN_SEQUENCE = 0x10, |
24 | ASN_SET = 0x11, //X |
24 | ASN_SET = 0x11, |
25 | ASN_NUMERIC_STRING = 0x12, //X |
25 | ASN_NUMERIC_STRING = 0x12, |
26 | ASN_PRINTABLE_STRING = 0x13, //X |
26 | ASN_PRINTABLE_STRING = 0x13, |
27 | ASN_TELETEX_STRING = 0x14, //X |
27 | ASN_TELETEX_STRING = 0x14, |
28 | ASN_T61_STRING = 0x14, //X |
28 | ASN_T61_STRING = 0x14, |
29 | ASN_VIDEOTEX_STRING = 0x15, //X |
29 | ASN_VIDEOTEX_STRING = 0x15, |
30 | ASN_IA5_STRING = 0x16, //X |
30 | ASN_IA5_STRING = 0x16, |
31 | ASN_ASCII_STRING = 0x16, //X |
31 | ASN_ASCII_STRING = 0x16, |
32 | ASN_UTC_TIME = 0x17, //X |
32 | ASN_UTC_TIME = 0x17, |
33 | ASN_GENERALIZED_TIME = 0x18, //X |
33 | ASN_GENERALIZED_TIME = 0x18, |
34 | ASN_GRAPHIC_STRING = 0x19, //X |
34 | ASN_GRAPHIC_STRING = 0x19, |
35 | ASN_VISIBLE_STRING = 0x1a, //X |
35 | ASN_VISIBLE_STRING = 0x1a, |
36 | ASN_ISO646_STRING = 0x1a, //X |
36 | ASN_ISO646_STRING = 0x1a, |
37 | ASN_GENERAL_STRING = 0x1b, //X |
37 | ASN_GENERAL_STRING = 0x1b, |
38 | ASN_UNIVERSAL_STRING = 0x1c, //X |
38 | ASN_UNIVERSAL_STRING = 0x1c, |
39 | ASN_CHARACTER_STRING = 0x1d, //X |
39 | ASN_CHARACTER_STRING = 0x1d, |
40 | ASN_BMP_STRING = 0x1e, //X |
40 | ASN_BMP_STRING = 0x1e, |
41 | |
41 | |
42 | ASN_TAG_BER = 0x1f, |
42 | ASN_TAG_BER = 0x1f, |
43 | ASN_TAG_MASK = 0x1f, |
43 | ASN_TAG_MASK = 0x1f, |
44 | |
44 | |
45 | // primitive/constructed |
45 | // primitive/constructed |
… | |
… | |
92 | |
92 | |
93 | static profile_type *cur_profile, *default_profile; |
93 | static profile_type *cur_profile, *default_profile; |
94 | static SV *buf_sv; // encoding buffer |
94 | static SV *buf_sv; // encoding buffer |
95 | static U8 *buf, *cur, *end; // buffer start, current, end |
95 | static U8 *buf, *cur, *end; // buffer start, current, end |
96 | |
96 | |
|
|
97 | #if PERL_VERSION < 18 |
|
|
98 | # define utf8_to_uvchr_buf(s,e,l) utf8_to_uvchr (s, l) |
|
|
99 | #endif |
|
|
100 | |
97 | #if __GNUC__ >= 3 |
101 | #if __GNUC__ >= 3 |
98 | # define expect(expr,value) __builtin_expect ((expr), (value)) |
102 | # define expect(expr,value) __builtin_expect ((expr), (value)) |
99 | # define INLINE static inline |
103 | # define INLINE static inline |
100 | #else |
104 | #else |
101 | # define expect(expr,value) (expr) |
105 | # define expect(expr,value) (expr) |
… | |
… | |
155 | return BER_TYPE_BYTES; |
159 | return BER_TYPE_BYTES; |
156 | |
160 | |
157 | return SvPVX (sv)[idx]; |
161 | return SvPVX (sv)[idx]; |
158 | } |
162 | } |
159 | |
163 | |
160 | static int |
164 | static void |
161 | profile_set (profile_type *profile, int klass, int tag, int type) |
165 | profile_set (profile_type *profile, int klass, int tag, int type) |
162 | { |
166 | { |
163 | SV *sv = (SV *)profile; |
167 | SV *sv = (SV *)profile; |
164 | U32 idx = (tag << 2) + klass; |
168 | U32 idx = (tag << 2) + klass; |
165 | STRLEN oldlen = SvCUR (sv); |
169 | STRLEN oldlen = SvCUR (sv); |
… | |
… | |
195 | { ASN_UNIVERSAL, ASN_UTF8_STRING , BER_TYPE_UTF8 }, |
199 | { ASN_UNIVERSAL, ASN_UTF8_STRING , BER_TYPE_UTF8 }, |
196 | { ASN_UNIVERSAL, ASN_BMP_STRING , BER_TYPE_UCS2 }, |
200 | { ASN_UNIVERSAL, ASN_BMP_STRING , BER_TYPE_UCS2 }, |
197 | { ASN_UNIVERSAL, ASN_UNIVERSAL_STRING , BER_TYPE_UCS4 }, |
201 | { ASN_UNIVERSAL, ASN_UNIVERSAL_STRING , BER_TYPE_UCS4 }, |
198 | }; |
202 | }; |
199 | |
203 | |
200 | for (celem = default_map + sizeof (default_map) / sizeof (default_map [0]); celem > default_map; celem--) |
204 | 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); |
205 | profile_set ((profile_type *)sv, celem->klass, celem->tag, celem->type); |
202 | |
206 | |
203 | return sv_bless (newRV_noinc (sv), profile_stash); |
207 | return sv_bless (newRV_noinc (sv), profile_stash); |
204 | } |
208 | } |
205 | |
209 | |
206 | ///////////////////////////////////////////////////////////////////////////// |
210 | ///////////////////////////////////////////////////////////////////////////// |
… | |
… | |
339 | u /= 10; |
343 | u /= 10; |
340 | } |
344 | } |
341 | while (u); |
345 | while (u); |
342 | |
346 | |
343 | // reverse digits |
347 | // reverse digits |
344 | for (char *ptr = buf; --ptr != beg; ++beg) |
348 | char *ptr = buf; |
|
|
349 | while (--ptr != beg) |
345 | { |
350 | { |
346 | char c = *ptr; |
351 | char c = *ptr; |
347 | *ptr = *beg; |
352 | *ptr = *beg; |
348 | *beg = c; |
353 | *beg = c; |
|
|
354 | ++beg; |
349 | } |
355 | } |
350 | } |
356 | } |
351 | |
357 | |
352 | return buf; |
358 | return buf; |
353 | } |
359 | } |
… | |
… | |
385 | *app++ = '.'; |
391 | *app++ = '.'; |
386 | app = write_uv (app, w); |
392 | app = write_uv (app, w); |
387 | } |
393 | } |
388 | |
394 | |
389 | return newSVpvn (oid, app - oid); |
395 | return newSVpvn (oid, app - oid); |
|
|
396 | } |
|
|
397 | |
|
|
398 | // TODO: this is unacceptably slow |
|
|
399 | static SV * |
|
|
400 | decode_ucs (int chrsize) |
|
|
401 | { |
|
|
402 | SV *res = NEWSV (0, 0); |
|
|
403 | |
|
|
404 | U32 len = get_length (); |
|
|
405 | |
|
|
406 | if (len & (chrsize - 1)) |
|
|
407 | croak ("BER_TYPE_UCS has an invalid number of octets (%d)", len); |
|
|
408 | |
|
|
409 | while (len) |
|
|
410 | { |
|
|
411 | U8 b1 = get_u8 (); |
|
|
412 | U8 b2 = get_u8 (); |
|
|
413 | U32 chr = (b1 << 8) | b2; |
|
|
414 | |
|
|
415 | if (chrsize == 4) |
|
|
416 | { |
|
|
417 | U8 b3 = get_u8 (); |
|
|
418 | U8 b4 = get_u8 (); |
|
|
419 | chr = (chr << 16) | (b3 << 8) | b4; |
|
|
420 | } |
|
|
421 | |
|
|
422 | U8 uchr [UTF8_MAXBYTES]; |
|
|
423 | int uclen = uvuni_to_utf8 (uchr, chr) - uchr; |
|
|
424 | |
|
|
425 | sv_catpvn (res, (const char *)uchr, uclen); |
|
|
426 | len -= chrsize; |
|
|
427 | } |
|
|
428 | |
|
|
429 | SvUTF8_on (res); |
|
|
430 | |
|
|
431 | return res; |
390 | } |
432 | } |
391 | |
433 | |
392 | static SV * |
434 | static SV * |
393 | decode_ber () |
435 | decode_ber () |
394 | { |
436 | { |
… | |
… | |
473 | |
515 | |
474 | res = newSVpvf ("%d.%d.%d.%d", c1, c2, c3, c4); |
516 | res = newSVpvf ("%d.%d.%d.%d", c1, c2, c3, c4); |
475 | } |
517 | } |
476 | break; |
518 | break; |
477 | |
519 | |
|
|
520 | case BER_TYPE_UCS2: |
|
|
521 | res = decode_ucs (2); |
|
|
522 | break; |
|
|
523 | |
|
|
524 | case BER_TYPE_UCS4: |
|
|
525 | res = decode_ucs (4); |
|
|
526 | break; |
|
|
527 | |
478 | case BER_TYPE_REAL: |
528 | case BER_TYPE_REAL: |
479 | case BER_TYPE_UCS2: |
|
|
480 | case BER_TYPE_UCS4: |
|
|
481 | case BER_TYPE_CROAK: |
529 | case BER_TYPE_CROAK: |
482 | default: |
530 | default: |
483 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
531 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
484 | } |
532 | } |
485 | |
533 | |
… | |
… | |
511 | static void |
559 | static void |
512 | set_buf (SV *sv) |
560 | set_buf (SV *sv) |
513 | { |
561 | { |
514 | STRLEN len; |
562 | STRLEN len; |
515 | buf_sv = sv; |
563 | buf_sv = sv; |
516 | buf = SvPVbyte (buf_sv, len); |
564 | buf = (U8 *)SvPVbyte (buf_sv, len); |
517 | cur = buf; |
565 | cur = buf; |
518 | end = buf + len; |
566 | end = buf + len; |
519 | } |
567 | } |
520 | |
568 | |
521 | /* similar to SvGROW, but somewhat safer and guarantees exponential realloc strategy */ |
569 | /* similar to SvGROW, but somewhat safer and guarantees exponential realloc strategy */ |
… | |
… | |
535 | need (STRLEN len) |
583 | need (STRLEN len) |
536 | { |
584 | { |
537 | if (expect_false ((uintptr_t)(end - cur) < len)) |
585 | if (expect_false ((uintptr_t)(end - cur) < len)) |
538 | { |
586 | { |
539 | STRLEN pos = cur - buf; |
587 | STRLEN pos = cur - buf; |
540 | buf = my_sv_grow (buf_sv, pos, len); |
588 | buf = (U8 *)my_sv_grow (buf_sv, pos, len); |
541 | cur = buf + pos; |
589 | cur = buf + pos; |
542 | end = buf + SvLEN (buf_sv) - 1; |
590 | end = buf + SvLEN (buf_sv) - 1; |
543 | } |
591 | } |
544 | } |
592 | } |
545 | |
593 | |
… | |
… | |
589 | } |
637 | } |
590 | |
638 | |
591 | static void |
639 | static void |
592 | put_length (U32 val) |
640 | put_length (U32 val) |
593 | { |
641 | { |
594 | need (5); |
642 | need (5 + val); |
595 | cur = put_length_at (val, cur); |
643 | cur = put_length_at (val, cur); |
596 | } |
644 | } |
597 | |
645 | |
598 | // return how many bytes the encoded length requires |
646 | // return how many bytes the encoded length requires |
599 | static int length_length (U32 val) |
647 | static int length_length (U32 val) |
… | |
… | |
605 | |
653 | |
606 | static void |
654 | static void |
607 | encode_data (const char *ptr, STRLEN len) |
655 | encode_data (const char *ptr, STRLEN len) |
608 | { |
656 | { |
609 | put_length (len); |
657 | put_length (len); |
610 | need (len); |
|
|
611 | memcpy (cur, ptr, len); |
658 | memcpy (cur, ptr, len); |
612 | cur += len; |
659 | cur += len; |
613 | } |
660 | } |
614 | |
661 | |
615 | static void |
662 | static void |
… | |
… | |
761 | |
808 | |
762 | return (AV *)rv; |
809 | return (AV *)rv; |
763 | } |
810 | } |
764 | |
811 | |
765 | static void |
812 | static void |
|
|
813 | encode_ucs (SV *data, int chrsize) |
|
|
814 | { |
|
|
815 | STRLEN uchars = sv_len_utf8 (data); |
|
|
816 | STRLEN len;; |
|
|
817 | char *ptr = SvPVutf8 (data, len); |
|
|
818 | |
|
|
819 | put_length (uchars * chrsize); |
|
|
820 | |
|
|
821 | while (uchars--) |
|
|
822 | { |
|
|
823 | STRLEN uclen; |
|
|
824 | UV uchr = utf8_to_uvchr_buf ((U8 *)ptr, (U8 *)ptr + len, &uclen); |
|
|
825 | |
|
|
826 | ptr += uclen; |
|
|
827 | len -= uclen; |
|
|
828 | |
|
|
829 | if (chrsize == 4) |
|
|
830 | { |
|
|
831 | *cur++ = uchr >> 24; |
|
|
832 | *cur++ = uchr >> 16; |
|
|
833 | } |
|
|
834 | |
|
|
835 | *cur++ = uchr >> 8; |
|
|
836 | *cur++ = uchr; |
|
|
837 | } |
|
|
838 | } |
|
|
839 | static void |
766 | encode_ber (SV *tuple) |
840 | encode_ber (SV *tuple) |
767 | { |
841 | { |
768 | AV *av = ber_tuple (tuple); |
842 | AV *av = ber_tuple (tuple); |
769 | |
843 | |
770 | int klass = SvIV (AvARRAY (av)[BER_CLASS]); |
844 | int klass = SvIV (AvARRAY (av)[BER_CLASS]); |
… | |
… | |
796 | int fill = AvFILL (av); |
870 | int fill = AvFILL (av); |
797 | |
871 | |
798 | if (expect_false (SvRMAGICAL (av))) |
872 | if (expect_false (SvRMAGICAL (av))) |
799 | croak ("BER constructed data must not be tied"); |
873 | croak ("BER constructed data must not be tied"); |
800 | |
874 | |
|
|
875 | int i; |
801 | for (int i = 0; i <= fill; ++i) |
876 | for (i = 0; i <= fill; ++i) |
802 | encode_ber (AvARRAY (av)[i]); |
877 | encode_ber (AvARRAY (av)[i]); |
803 | |
878 | |
804 | len_fixup (mark); |
879 | len_fixup (mark); |
805 | } |
880 | } |
806 | else |
881 | else |
… | |
… | |
810 | put_length (0); |
885 | put_length (0); |
811 | break; |
886 | break; |
812 | |
887 | |
813 | case BER_TYPE_BOOL: |
888 | case BER_TYPE_BOOL: |
814 | put_length (1); |
889 | put_length (1); |
815 | put_u8 (SvTRUE (data) ? 0xff : 0x00); |
890 | *cur++ = SvTRUE (data) ? 0xff : 0x00; |
816 | break; |
891 | break; |
817 | |
892 | |
818 | case BER_TYPE_OID: |
893 | case BER_TYPE_OID: |
819 | encode_oid (data, 0); |
894 | encode_oid (data, 0); |
820 | break; |
895 | break; |
… | |
… | |
849 | sscanf (SvPV_nolen (data), "%hhu.%hhu.%hhu.%hhu", ip + 0, ip + 1, ip + 2, ip + 3); |
924 | sscanf (SvPV_nolen (data), "%hhu.%hhu.%hhu.%hhu", ip + 0, ip + 1, ip + 2, ip + 3); |
850 | encode_data ((const char *)ip, sizeof (ip)); |
925 | encode_data ((const char *)ip, sizeof (ip)); |
851 | } |
926 | } |
852 | break; |
927 | break; |
853 | |
928 | |
|
|
929 | case BER_TYPE_UCS2: |
|
|
930 | encode_ucs (data, 2); |
|
|
931 | break; |
|
|
932 | |
|
|
933 | case BER_TYPE_UCS4: |
|
|
934 | encode_ucs (data, 4); |
|
|
935 | break; |
|
|
936 | |
854 | case BER_TYPE_REAL: |
937 | case BER_TYPE_REAL: |
855 | case BER_TYPE_UCS2: |
|
|
856 | case BER_TYPE_UCS4: |
|
|
857 | case BER_TYPE_CROAK: |
938 | case BER_TYPE_CROAK: |
858 | default: |
939 | default: |
859 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
940 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
860 | } |
941 | } |
861 | |
942 | |
… | |
… | |
950 | ber_decode (SV *ber, SV *profile = &PL_sv_undef) |
1031 | ber_decode (SV *ber, SV *profile = &PL_sv_undef) |
951 | CODE: |
1032 | CODE: |
952 | { |
1033 | { |
953 | cur_profile = SvPROFILE (profile); |
1034 | cur_profile = SvPROFILE (profile); |
954 | STRLEN len; |
1035 | STRLEN len; |
955 | buf = SvPVbyte (ber, len); |
1036 | buf = (U8 *)SvPVbyte (ber, len); |
956 | cur = buf; |
1037 | cur = buf; |
957 | end = buf + len; |
1038 | end = buf + len; |
958 | |
1039 | |
959 | RETVAL = decode_ber (); |
1040 | RETVAL = decode_ber (); |
960 | } |
1041 | } |