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.5 by root, Fri Apr 19 23:50:53 2019 UTC vs.
Revision 1.11 by root, Sat Apr 20 12:25:23 2019 UTC

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
72 BER_TYPE_OID, 72 BER_TYPE_OID,
73 BER_TYPE_RELOID, 73 BER_TYPE_RELOID,
74 BER_TYPE_NULL, 74 BER_TYPE_NULL,
75 BER_TYPE_BOOL, 75 BER_TYPE_BOOL,
76 BER_TYPE_REAL, 76 BER_TYPE_REAL,
77 BER_TYPE_IPADDRESS,
77 BER_TYPE_CROAK, 78 BER_TYPE_CROAK,
78}; 79};
79 80
80enum { 81enum {
81 BER_CLASS = 0, 82 BER_CLASS = 0,
91 92
92static profile_type *cur_profile, *default_profile; 93static profile_type *cur_profile, *default_profile;
93static SV *buf_sv; // encoding buffer 94static SV *buf_sv; // encoding buffer
94static U8 *buf, *cur, *end; // buffer start, current, end 95static U8 *buf, *cur, *end; // buffer start, current, end
95 96
97#if PERL_VERSION < 18
98# define utf8_to_uvchr_buf(s,e,l) utf8_to_uvchr (s, l)
99#endif
100
96#if __GNUC__ >= 3 101#if __GNUC__ >= 3
97# define expect(expr,value) __builtin_expect ((expr), (value)) 102# define expect(expr,value) __builtin_expect ((expr), (value))
98# define INLINE static inline 103# define INLINE static inline
99#else 104#else
100# define expect(expr,value) (expr) 105# define expect(expr,value) (expr)
129 134
130static profile_type * 135static profile_type *
131SvPROFILE (SV *profile) 136SvPROFILE (SV *profile)
132{ 137{
133 if (!SvOK (profile)) 138 if (!SvOK (profile))
134 profile = default_profile; 139 return default_profile;
135 140
136 if (!SvROK (profile)) 141 if (!SvROK (profile))
137 croak ("invalid profile"); 142 croak ("invalid profile");
138 143
139 profile = SvRV (profile); 144 profile = SvRV (profile);
154 return BER_TYPE_BYTES; 159 return BER_TYPE_BYTES;
155 160
156 return SvPVX (sv)[idx]; 161 return SvPVX (sv)[idx];
157} 162}
158 163
159static int 164static void
160profile_set (profile_type *profile, int klass, int tag, int type) 165profile_set (profile_type *profile, int klass, int tag, int type)
161{ 166{
162 SV *sv = (SV *)profile; 167 SV *sv = (SV *)profile;
163 U32 idx = (tag << 2) + klass; 168 U32 idx = (tag << 2) + klass;
164 STRLEN oldlen = SvCUR (sv); 169 STRLEN oldlen = SvCUR (sv);
194 { ASN_UNIVERSAL, ASN_UTF8_STRING , BER_TYPE_UTF8 }, 199 { ASN_UNIVERSAL, ASN_UTF8_STRING , BER_TYPE_UTF8 },
195 { ASN_UNIVERSAL, ASN_BMP_STRING , BER_TYPE_UCS2 }, 200 { ASN_UNIVERSAL, ASN_BMP_STRING , BER_TYPE_UCS2 },
196 { ASN_UNIVERSAL, ASN_UNIVERSAL_STRING , BER_TYPE_UCS4 }, 201 { ASN_UNIVERSAL, ASN_UNIVERSAL_STRING , BER_TYPE_UCS4 },
197 }; 202 };
198 203
199 //const_iv (SNMP_IPADDRESS)
200 //const_iv (SNMP_COUNTER32)
201 //const_iv (SNMP_UNSIGNED32)
202 //const_iv (SNMP_TIMETICKS)
203 //const_iv (SNMP_OPAQUE)
204 //const_iv (SNMP_COUNTER64)
205
206 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; )
207 profile_set ((void *)sv, celem->klass, celem->tag, celem->type); 205 profile_set ((profile_type *)sv, celem->klass, celem->tag, celem->type);
208 206
209 return sv_bless (newRV_noinc (sv), profile_stash); 207 return sv_bless (newRV_noinc (sv), profile_stash);
210} 208}
211 209
212///////////////////////////////////////////////////////////////////////////// 210/////////////////////////////////////////////////////////////////////////////
313 311
314 do 312 do
315 val = (val << 8) | *data++; 313 val = (val << 8) | *data++;
316 while (--len); 314 while (--len);
317 315
318 // the cats to IV relies on implementation-defined behaviour (two's complement cast) 316 // the cast to IV relies on implementation-defined behaviour (two's complement cast)
319 // but that's ok, as perl relies on it as well. 317 // but that's ok, as perl relies on it as well.
320 return negative ? newSViv ((IV)val) : newSVuv (val); 318 return negative ? newSViv ((IV)val) : newSVuv (val);
321} 319}
322 320
323static SV * 321static SV *
345 u /= 10; 343 u /= 10;
346 } 344 }
347 while (u); 345 while (u);
348 346
349 // reverse digits 347 // reverse digits
350 for (char *ptr = buf; --ptr != beg; ++beg) 348 char *ptr = buf;
349 while (--ptr != beg)
351 { 350 {
352 char c = *ptr; 351 char c = *ptr;
353 *ptr = *beg; 352 *ptr = *beg;
354 *beg = c; 353 *beg = c;
354 ++beg;
355 } 355 }
356 } 356 }
357 357
358 return buf; 358 return buf;
359} 359}
391 *app++ = '.'; 391 *app++ = '.';
392 app = write_uv (app, w); 392 app = write_uv (app, w);
393 } 393 }
394 394
395 return newSVpvn (oid, app - oid); 395 return newSVpvn (oid, app - oid);
396}
397
398// TODO: this is unacceptably slow
399static SV *
400decode_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;
396} 432}
397 433
398static SV * 434static SV *
399decode_ber () 435decode_ber ()
400{ 436{
436 case BER_TYPE_BOOL: 472 case BER_TYPE_BOOL:
437 { 473 {
438 U32 len = get_length (); 474 U32 len = get_length ();
439 475
440 if (len != 1) 476 if (len != 1)
441 croak ("BOOLEAN type with invalid length %d encountered", len); 477 croak ("BER_TYPE_BOOLEAN type with invalid length %d encountered", len);
442 478
443 res = newSVcacheint (get_u8 () ? 0 : 1); 479 res = newSVcacheint (get_u8 () ? 0 : 1);
444 } 480 }
445 break; 481 break;
446 482
463 499
464 case BER_TYPE_BYTES: 500 case BER_TYPE_BYTES:
465 res = decode_data (); 501 res = decode_data ();
466 break; 502 break;
467 503
504 case BER_TYPE_IPADDRESS:
505 {
506 U32 len = get_length ();
507
508 if (len != 4)
509 croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered", len);
510
511 U8 c1 = get_u8 ();
512 U8 c2 = get_u8 ();
513 U8 c3 = get_u8 ();
514 U8 c4 = get_u8 ();
515
516 res = newSVpvf ("%d.%d.%d.%d", c1, c2, c3, c4);
517 }
518 break;
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
468 case BER_TYPE_REAL: 528 case BER_TYPE_REAL:
469 case BER_TYPE_UCS2:
470 case BER_TYPE_UCS4:
471 case BER_TYPE_CROAK: 529 case BER_TYPE_CROAK:
472 default: 530 default:
473 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 531 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
474 } 532 }
475 533
501static void 559static void
502set_buf (SV *sv) 560set_buf (SV *sv)
503{ 561{
504 STRLEN len; 562 STRLEN len;
505 buf_sv = sv; 563 buf_sv = sv;
506 buf = SvPVbyte (buf_sv, len); 564 buf = (U8 *)SvPVbyte (buf_sv, len);
507 cur = buf; 565 cur = buf;
508 end = buf + len; 566 end = buf + len;
509} 567}
510 568
511/* similar to SvGROW, but somewhat safer and guarantees exponential realloc strategy */ 569/* similar to SvGROW, but somewhat safer and guarantees exponential realloc strategy */
525need (STRLEN len) 583need (STRLEN len)
526{ 584{
527 if (expect_false ((uintptr_t)(end - cur) < len)) 585 if (expect_false ((uintptr_t)(end - cur) < len))
528 { 586 {
529 STRLEN pos = cur - buf; 587 STRLEN pos = cur - buf;
530 buf = my_sv_grow (buf_sv, pos, len); 588 buf = (U8 *)my_sv_grow (buf_sv, pos, len);
531 cur = buf + pos; 589 cur = buf + pos;
532 end = buf + SvLEN (buf_sv) - 1; 590 end = buf + SvLEN (buf_sv) - 1;
533 } 591 }
534} 592}
535 593
579} 637}
580 638
581static void 639static void
582put_length (U32 val) 640put_length (U32 val)
583{ 641{
584 need (5); 642 need (5 + val);
585 cur = put_length_at (val, cur); 643 cur = put_length_at (val, cur);
586} 644}
587 645
588// return how many bytes the encoded length requires 646// return how many bytes the encoded length requires
589static int length_length (U32 val) 647static int length_length (U32 val)
595 653
596static void 654static void
597encode_data (const char *ptr, STRLEN len) 655encode_data (const char *ptr, STRLEN len)
598{ 656{
599 put_length (len); 657 put_length (len);
600 need (len);
601 memcpy (cur, ptr, len); 658 memcpy (cur, ptr, len);
602 cur += len; 659 cur += len;
603} 660}
604 661
605static void 662static void
751 808
752 return (AV *)rv; 809 return (AV *)rv;
753} 810}
754 811
755static void 812static void
813encode_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}
839static void
756encode_ber (SV *tuple) 840encode_ber (SV *tuple)
757{ 841{
758 AV *av = ber_tuple (tuple); 842 AV *av = ber_tuple (tuple);
759 843
760 int klass = SvIV (AvARRAY (av)[BER_CLASS]); 844 int klass = SvIV (AvARRAY (av)[BER_CLASS]);
786 int fill = AvFILL (av); 870 int fill = AvFILL (av);
787 871
788 if (expect_false (SvRMAGICAL (av))) 872 if (expect_false (SvRMAGICAL (av)))
789 croak ("BER constructed data must not be tied"); 873 croak ("BER constructed data must not be tied");
790 874
875 int i;
791 for (int i = 0; i <= fill; ++i) 876 for (i = 0; i <= fill; ++i)
792 encode_ber (AvARRAY (av)[i]); 877 encode_ber (AvARRAY (av)[i]);
793 878
794 len_fixup (mark); 879 len_fixup (mark);
795 } 880 }
796 else 881 else
800 put_length (0); 885 put_length (0);
801 break; 886 break;
802 887
803 case BER_TYPE_BOOL: 888 case BER_TYPE_BOOL:
804 put_length (1); 889 put_length (1);
805 put_u8 (SvTRUE (data) ? 0xff : 0x00); 890 *cur++ = SvTRUE (data) ? 0xff : 0x00;
806 break; 891 break;
807 892
808 case BER_TYPE_OID: 893 case BER_TYPE_OID:
809 encode_oid (data, 0); 894 encode_oid (data, 0);
810 break; 895 break;
831 const char *ptr = SvPVutf8 (data, len); 916 const char *ptr = SvPVutf8 (data, len);
832 encode_data (ptr, len); 917 encode_data (ptr, len);
833 } 918 }
834 break; 919 break;
835 920
921 case BER_TYPE_IPADDRESS:
922 {
923 U8 ip[4];
924 sscanf (SvPV_nolen (data), "%hhu.%hhu.%hhu.%hhu", ip + 0, ip + 1, ip + 2, ip + 3);
925 encode_data ((const char *)ip, sizeof (ip));
926 }
927 break;
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
836 case BER_TYPE_REAL: 937 case BER_TYPE_REAL:
837 case BER_TYPE_UCS2:
838 case BER_TYPE_UCS4:
839 case BER_TYPE_CROAK: 938 case BER_TYPE_CROAK:
840 default: 939 default:
841 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 940 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
842 } 941 }
843 942
911 const_iv (BER_TYPE_OID) 1010 const_iv (BER_TYPE_OID)
912 const_iv (BER_TYPE_RELOID) 1011 const_iv (BER_TYPE_RELOID)
913 const_iv (BER_TYPE_NULL) 1012 const_iv (BER_TYPE_NULL)
914 const_iv (BER_TYPE_BOOL) 1013 const_iv (BER_TYPE_BOOL)
915 const_iv (BER_TYPE_REAL) 1014 const_iv (BER_TYPE_REAL)
1015 const_iv (BER_TYPE_IPADDRESS)
916 const_iv (BER_TYPE_CROAK) 1016 const_iv (BER_TYPE_CROAK)
917 1017
918 const_iv (SNMP_IPADDRESS) 1018 const_iv (SNMP_IPADDRESS)
919 const_iv (SNMP_COUNTER32) 1019 const_iv (SNMP_COUNTER32)
920 const_iv (SNMP_UNSIGNED32) 1020 const_iv (SNMP_UNSIGNED32)
921 const_iv (SNMP_TIMETICKS) 1021 const_iv (SNMP_TIMETICKS)
922 const_iv (SNMP_OPAQUE) 1022 const_iv (SNMP_OPAQUE)
923 const_iv (SNMP_COUNTER64) 1023 const_iv (SNMP_COUNTER64)
924
925 }; 1024 };
926 1025
927 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) 1026 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--)
928 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); 1027 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv));
929
930 default_profile = profile_new ();
931} 1028}
932 1029
933SV * 1030SV *
934ber_decode (SV *ber, SV *profile = &PL_sv_undef) 1031ber_decode (SV *ber, SV *profile = &PL_sv_undef)
935 CODE: 1032 CODE:
936{ 1033{
937 cur_profile = SvPROFILE (profile); 1034 cur_profile = SvPROFILE (profile);
938 STRLEN len; 1035 STRLEN len;
939 buf = SvPVbyte (ber, len); 1036 buf = (U8 *)SvPVbyte (ber, len);
940 cur = buf; 1037 cur = buf;
941 end = buf + len; 1038 end = buf + len;
942 1039
943 RETVAL = decode_ber (); 1040 RETVAL = decode_ber ();
944} 1041}
1069new (SV *klass) 1166new (SV *klass)
1070 CODE: 1167 CODE:
1071 RETVAL = profile_new (); 1168 RETVAL = profile_new ();
1072 OUTPUT: RETVAL 1169 OUTPUT: RETVAL
1073 1170
1171void
1172set (SV *profile, int klass, int tag, int type)
1173 CODE:
1174 profile_set (SvPROFILE (profile), klass, tag, type);
1175
1176IV
1177get (SV *profile, int klass, int tag)
1178 CODE:
1179 RETVAL = profile_lookup (SvPROFILE (profile), klass, tag);
1180 OUTPUT: RETVAL
1181
1182void
1183_set_default (SV *profile)
1184 CODE:
1185 default_profile = SvPROFILE (profile);
1186
1187

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines