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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines