… | |
… | |
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 |
… | |
… | |
91 | typedef void profile_type; |
91 | typedef void profile_type; |
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 | |
|
|
97 | #if PERL_VERSION < 18 |
|
|
98 | # define utf8_to_uvchr_buf(s,e,l) utf8_to_uvchr (s, l) |
|
|
99 | #endif |
96 | |
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 |
… | |
… | |
387 | } |
391 | } |
388 | |
392 | |
389 | return newSVpvn (oid, app - oid); |
393 | return newSVpvn (oid, app - oid); |
390 | } |
394 | } |
391 | |
395 | |
|
|
396 | // TODO: this is unacceptably slow |
|
|
397 | static SV * |
|
|
398 | decode_ucs (int chrsize) |
|
|
399 | { |
|
|
400 | SV *res = NEWSV (0, 0); |
|
|
401 | |
|
|
402 | U32 len = get_length (); |
|
|
403 | |
|
|
404 | if (len & (chrsize - 1)) |
|
|
405 | croak ("BER_TYPE_UCS has an invalid number of octets (%d)", len); |
|
|
406 | |
|
|
407 | while (len) |
|
|
408 | { |
|
|
409 | U8 b1 = get_u8 (); |
|
|
410 | U8 b2 = get_u8 (); |
|
|
411 | U32 chr = (b1 << 8) | b2; |
|
|
412 | |
|
|
413 | if (chrsize == 4) |
|
|
414 | { |
|
|
415 | U8 b3 = get_u8 (); |
|
|
416 | U8 b4 = get_u8 (); |
|
|
417 | chr = (chr << 16) | (b3 << 8) | b4; |
|
|
418 | } |
|
|
419 | |
|
|
420 | U8 uchr [UTF8_MAXBYTES]; |
|
|
421 | int uclen = uvuni_to_utf8 (uchr, chr) - uchr; |
|
|
422 | |
|
|
423 | sv_catpvn (res, (const char *)uchr, uclen); |
|
|
424 | len -= chrsize; |
|
|
425 | } |
|
|
426 | |
|
|
427 | SvUTF8_on (res); |
|
|
428 | |
|
|
429 | return res; |
|
|
430 | } |
|
|
431 | |
392 | static SV * |
432 | static SV * |
393 | decode_ber () |
433 | decode_ber () |
394 | { |
434 | { |
395 | int identifier = get_u8 (); |
435 | int identifier = get_u8 (); |
396 | |
436 | |
… | |
… | |
473 | |
513 | |
474 | res = newSVpvf ("%d.%d.%d.%d", c1, c2, c3, c4); |
514 | res = newSVpvf ("%d.%d.%d.%d", c1, c2, c3, c4); |
475 | } |
515 | } |
476 | break; |
516 | break; |
477 | |
517 | |
|
|
518 | case BER_TYPE_UCS2: |
|
|
519 | res = decode_ucs (2); |
|
|
520 | break; |
|
|
521 | |
|
|
522 | case BER_TYPE_UCS4: |
|
|
523 | res = decode_ucs (4); |
|
|
524 | break; |
|
|
525 | |
478 | case BER_TYPE_REAL: |
526 | case BER_TYPE_REAL: |
479 | case BER_TYPE_UCS2: |
|
|
480 | case BER_TYPE_UCS4: |
|
|
481 | case BER_TYPE_CROAK: |
527 | case BER_TYPE_CROAK: |
482 | default: |
528 | default: |
483 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
529 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
484 | } |
530 | } |
485 | |
531 | |
… | |
… | |
589 | } |
635 | } |
590 | |
636 | |
591 | static void |
637 | static void |
592 | put_length (U32 val) |
638 | put_length (U32 val) |
593 | { |
639 | { |
594 | need (5); |
640 | need (5 + val); |
595 | cur = put_length_at (val, cur); |
641 | cur = put_length_at (val, cur); |
596 | } |
642 | } |
597 | |
643 | |
598 | // return how many bytes the encoded length requires |
644 | // return how many bytes the encoded length requires |
599 | static int length_length (U32 val) |
645 | static int length_length (U32 val) |
… | |
… | |
605 | |
651 | |
606 | static void |
652 | static void |
607 | encode_data (const char *ptr, STRLEN len) |
653 | encode_data (const char *ptr, STRLEN len) |
608 | { |
654 | { |
609 | put_length (len); |
655 | put_length (len); |
610 | need (len); |
|
|
611 | memcpy (cur, ptr, len); |
656 | memcpy (cur, ptr, len); |
612 | cur += len; |
657 | cur += len; |
613 | } |
658 | } |
614 | |
659 | |
615 | static void |
660 | static void |
… | |
… | |
761 | |
806 | |
762 | return (AV *)rv; |
807 | return (AV *)rv; |
763 | } |
808 | } |
764 | |
809 | |
765 | static void |
810 | static void |
|
|
811 | encode_ucs (SV *data, int chrsize) |
|
|
812 | { |
|
|
813 | STRLEN uchars = sv_len_utf8 (data); |
|
|
814 | STRLEN len;; |
|
|
815 | char *ptr = SvPVutf8 (data, len); |
|
|
816 | |
|
|
817 | put_length (uchars * chrsize); |
|
|
818 | |
|
|
819 | while (uchars--) |
|
|
820 | { |
|
|
821 | STRLEN uclen; |
|
|
822 | UV uchr = utf8_to_uvchr_buf (ptr, ptr + len, &uclen); |
|
|
823 | |
|
|
824 | ptr += uclen; |
|
|
825 | len -= uclen; |
|
|
826 | |
|
|
827 | if (chrsize == 4) |
|
|
828 | { |
|
|
829 | *cur++ = uchr >> 24; |
|
|
830 | *cur++ = uchr >> 16; |
|
|
831 | } |
|
|
832 | |
|
|
833 | *cur++ = uchr >> 8; |
|
|
834 | *cur++ = uchr; |
|
|
835 | } |
|
|
836 | } |
|
|
837 | static void |
766 | encode_ber (SV *tuple) |
838 | encode_ber (SV *tuple) |
767 | { |
839 | { |
768 | AV *av = ber_tuple (tuple); |
840 | AV *av = ber_tuple (tuple); |
769 | |
841 | |
770 | int klass = SvIV (AvARRAY (av)[BER_CLASS]); |
842 | int klass = SvIV (AvARRAY (av)[BER_CLASS]); |
… | |
… | |
810 | put_length (0); |
882 | put_length (0); |
811 | break; |
883 | break; |
812 | |
884 | |
813 | case BER_TYPE_BOOL: |
885 | case BER_TYPE_BOOL: |
814 | put_length (1); |
886 | put_length (1); |
815 | put_u8 (SvTRUE (data) ? 0xff : 0x00); |
887 | *cur++ = SvTRUE (data) ? 0xff : 0x00; |
816 | break; |
888 | break; |
817 | |
889 | |
818 | case BER_TYPE_OID: |
890 | case BER_TYPE_OID: |
819 | encode_oid (data, 0); |
891 | encode_oid (data, 0); |
820 | break; |
892 | break; |
… | |
… | |
849 | sscanf (SvPV_nolen (data), "%hhu.%hhu.%hhu.%hhu", ip + 0, ip + 1, ip + 2, ip + 3); |
921 | sscanf (SvPV_nolen (data), "%hhu.%hhu.%hhu.%hhu", ip + 0, ip + 1, ip + 2, ip + 3); |
850 | encode_data ((const char *)ip, sizeof (ip)); |
922 | encode_data ((const char *)ip, sizeof (ip)); |
851 | } |
923 | } |
852 | break; |
924 | break; |
853 | |
925 | |
|
|
926 | case BER_TYPE_UCS2: |
|
|
927 | encode_ucs (data, 2); |
|
|
928 | break; |
|
|
929 | |
|
|
930 | case BER_TYPE_UCS4: |
|
|
931 | encode_ucs (data, 4); |
|
|
932 | break; |
|
|
933 | |
854 | case BER_TYPE_REAL: |
934 | case BER_TYPE_REAL: |
855 | case BER_TYPE_UCS2: |
|
|
856 | case BER_TYPE_UCS4: |
|
|
857 | case BER_TYPE_CROAK: |
935 | case BER_TYPE_CROAK: |
858 | default: |
936 | default: |
859 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
937 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
860 | } |
938 | } |
861 | |
939 | |