ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Convert-BER-XS/XS.xs
(Generate patch)

Comparing Convert-BER-XS/XS.xs (file contents):
Revision 1.6 by root, Sat Apr 20 01:03:59 2019 UTC vs.
Revision 1.12 by root, Sat Apr 20 12:35:03 2019 UTC

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
7enum { 10enum {
8 // ASN_TAG 11 // ASN_TAG
9 ASN_BOOLEAN = 0x01, 12 ASN_BOOLEAN = 0x01,
10 ASN_INTEGER32 = 0x02, 13 ASN_INTEGER32 = 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
93static profile_type *cur_profile, *default_profile; 96static profile_type *cur_profile, *default_profile;
94static SV *buf_sv; // encoding buffer 97static SV *buf_sv; // encoding buffer
95static U8 *buf, *cur, *end; // buffer start, current, end 98static 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
160static int 167static void
161profile_set (profile_type *profile, int klass, int tag, int type) 168profile_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);
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/////////////////////////////////////////////////////////////////////////////
339 u /= 10; 346 u /= 10;
340 } 347 }
341 while (u); 348 while (u);
342 349
343 // reverse digits 350 // reverse digits
344 for (char *ptr = buf; --ptr != beg; ++beg) 351 char *ptr = buf;
352 while (--ptr != beg)
345 { 353 {
346 char c = *ptr; 354 char c = *ptr;
347 *ptr = *beg; 355 *ptr = *beg;
348 *beg = c; 356 *beg = c;
357 ++beg;
349 } 358 }
350 } 359 }
351 360
352 return buf; 361 return buf;
353} 362}
385 *app++ = '.'; 394 *app++ = '.';
386 app = write_uv (app, w); 395 app = write_uv (app, w);
387 } 396 }
388 397
389 return newSVpvn (oid, app - oid); 398 return newSVpvn (oid, app - oid);
399}
400
401// TODO: this is unacceptably slow
402static SV *
403decode_ucs (int chrsize)
404{
405 SV *res = NEWSV (0, 0);
406
407 U32 len = get_length ();
408
409 if (len & (chrsize - 1))
410 croak ("BER_TYPE_UCS has an invalid number of octets (%d)", len);
411
412 while (len)
413 {
414 U8 b1 = get_u8 ();
415 U8 b2 = get_u8 ();
416 U32 chr = (b1 << 8) | b2;
417
418 if (chrsize == 4)
419 {
420 U8 b3 = get_u8 ();
421 U8 b4 = get_u8 ();
422 chr = (chr << 16) | (b3 << 8) | b4;
423 }
424
425 U8 uchr [UTF8_MAXBYTES];
426 int uclen = uvuni_to_utf8 (uchr, chr) - uchr;
427
428 sv_catpvn (res, (const char *)uchr, uclen);
429 len -= chrsize;
430 }
431
432 SvUTF8_on (res);
433
434 return res;
390} 435}
391 436
392static SV * 437static SV *
393decode_ber () 438decode_ber ()
394{ 439{
473 518
474 res = newSVpvf ("%d.%d.%d.%d", c1, c2, c3, c4); 519 res = newSVpvf ("%d.%d.%d.%d", c1, c2, c3, c4);
475 } 520 }
476 break; 521 break;
477 522
523 case BER_TYPE_UCS2:
524 res = decode_ucs (2);
525 break;
526
527 case BER_TYPE_UCS4:
528 res = decode_ucs (4);
529 break;
530
478 case BER_TYPE_REAL: 531 case BER_TYPE_REAL:
479 case BER_TYPE_UCS2:
480 case BER_TYPE_UCS4:
481 case BER_TYPE_CROAK: 532 case BER_TYPE_CROAK:
482 default: 533 default:
483 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 534 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
484 } 535 }
485 536
511static void 562static void
512set_buf (SV *sv) 563set_buf (SV *sv)
513{ 564{
514 STRLEN len; 565 STRLEN len;
515 buf_sv = sv; 566 buf_sv = sv;
516 buf = SvPVbyte (buf_sv, len); 567 buf = (U8 *)SvPVbyte (buf_sv, len);
517 cur = buf; 568 cur = buf;
518 end = buf + len; 569 end = buf + len;
519} 570}
520 571
521/* similar to SvGROW, but somewhat safer and guarantees exponential realloc strategy */ 572/* similar to SvGROW, but somewhat safer and guarantees exponential realloc strategy */
535need (STRLEN len) 586need (STRLEN len)
536{ 587{
537 if (expect_false ((uintptr_t)(end - cur) < len)) 588 if (expect_false ((uintptr_t)(end - cur) < len))
538 { 589 {
539 STRLEN pos = cur - buf; 590 STRLEN pos = cur - buf;
540 buf = my_sv_grow (buf_sv, pos, len); 591 buf = (U8 *)my_sv_grow (buf_sv, pos, len);
541 cur = buf + pos; 592 cur = buf + pos;
542 end = buf + SvLEN (buf_sv) - 1; 593 end = buf + SvLEN (buf_sv) - 1;
543 } 594 }
544} 595}
545 596
589} 640}
590 641
591static void 642static void
592put_length (U32 val) 643put_length (U32 val)
593{ 644{
594 need (5); 645 need (5 + val);
595 cur = put_length_at (val, cur); 646 cur = put_length_at (val, cur);
596} 647}
597 648
598// return how many bytes the encoded length requires 649// return how many bytes the encoded length requires
599static int length_length (U32 val) 650static int length_length (U32 val)
605 656
606static void 657static void
607encode_data (const char *ptr, STRLEN len) 658encode_data (const char *ptr, STRLEN len)
608{ 659{
609 put_length (len); 660 put_length (len);
610 need (len);
611 memcpy (cur, ptr, len); 661 memcpy (cur, ptr, len);
612 cur += len; 662 cur += len;
613} 663}
614 664
615static void 665static void
761 811
762 return (AV *)rv; 812 return (AV *)rv;
763} 813}
764 814
765static void 815static void
816encode_ucs (SV *data, int chrsize)
817{
818 STRLEN uchars = sv_len_utf8 (data);
819 STRLEN len;;
820 char *ptr = SvPVutf8 (data, len);
821
822 put_length (uchars * chrsize);
823
824 while (uchars--)
825 {
826 STRLEN uclen;
827 UV uchr = utf8_to_uvchr_buf ((U8 *)ptr, (U8 *)ptr + len, &uclen);
828
829 ptr += uclen;
830 len -= uclen;
831
832 if (chrsize == 4)
833 {
834 *cur++ = uchr >> 24;
835 *cur++ = uchr >> 16;
836 }
837
838 *cur++ = uchr >> 8;
839 *cur++ = uchr;
840 }
841}
842static void
766encode_ber (SV *tuple) 843encode_ber (SV *tuple)
767{ 844{
768 AV *av = ber_tuple (tuple); 845 AV *av = ber_tuple (tuple);
769 846
770 int klass = SvIV (AvARRAY (av)[BER_CLASS]); 847 int klass = SvIV (AvARRAY (av)[BER_CLASS]);
796 int fill = AvFILL (av); 873 int fill = AvFILL (av);
797 874
798 if (expect_false (SvRMAGICAL (av))) 875 if (expect_false (SvRMAGICAL (av)))
799 croak ("BER constructed data must not be tied"); 876 croak ("BER constructed data must not be tied");
800 877
878 int i;
801 for (int i = 0; i <= fill; ++i) 879 for (i = 0; i <= fill; ++i)
802 encode_ber (AvARRAY (av)[i]); 880 encode_ber (AvARRAY (av)[i]);
803 881
804 len_fixup (mark); 882 len_fixup (mark);
805 } 883 }
806 else 884 else
810 put_length (0); 888 put_length (0);
811 break; 889 break;
812 890
813 case BER_TYPE_BOOL: 891 case BER_TYPE_BOOL:
814 put_length (1); 892 put_length (1);
815 put_u8 (SvTRUE (data) ? 0xff : 0x00); 893 *cur++ = SvTRUE (data) ? 0xff : 0x00;
816 break; 894 break;
817 895
818 case BER_TYPE_OID: 896 case BER_TYPE_OID:
819 encode_oid (data, 0); 897 encode_oid (data, 0);
820 break; 898 break;
849 sscanf (SvPV_nolen (data), "%hhu.%hhu.%hhu.%hhu", ip + 0, ip + 1, ip + 2, ip + 3); 927 sscanf (SvPV_nolen (data), "%hhu.%hhu.%hhu.%hhu", ip + 0, ip + 1, ip + 2, ip + 3);
850 encode_data ((const char *)ip, sizeof (ip)); 928 encode_data ((const char *)ip, sizeof (ip));
851 } 929 }
852 break; 930 break;
853 931
932 case BER_TYPE_UCS2:
933 encode_ucs (data, 2);
934 break;
935
936 case BER_TYPE_UCS4:
937 encode_ucs (data, 4);
938 break;
939
854 case BER_TYPE_REAL: 940 case BER_TYPE_REAL:
855 case BER_TYPE_UCS2:
856 case BER_TYPE_UCS4:
857 case BER_TYPE_CROAK: 941 case BER_TYPE_CROAK:
858 default: 942 default:
859 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 943 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
860 } 944 }
861 945
950ber_decode (SV *ber, SV *profile = &PL_sv_undef) 1034ber_decode (SV *ber, SV *profile = &PL_sv_undef)
951 CODE: 1035 CODE:
952{ 1036{
953 cur_profile = SvPROFILE (profile); 1037 cur_profile = SvPROFILE (profile);
954 STRLEN len; 1038 STRLEN len;
955 buf = SvPVbyte (ber, len); 1039 buf = (U8 *)SvPVbyte (ber, len);
956 cur = buf; 1040 cur = buf;
957 end = buf + len; 1041 end = buf + len;
958 1042
959 RETVAL = decode_ber (); 1043 RETVAL = decode_ber ();
960} 1044}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines