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.13 by root, Sat Apr 20 13:46:14 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);
187 } *celem, default_map[] = { 194 } *celem, default_map[] = {
188 { ASN_UNIVERSAL, ASN_BOOLEAN , BER_TYPE_BOOL }, 195 { ASN_UNIVERSAL, ASN_BOOLEAN , BER_TYPE_BOOL },
189 { ASN_UNIVERSAL, ASN_INTEGER32 , BER_TYPE_INT }, 196 { ASN_UNIVERSAL, ASN_INTEGER32 , BER_TYPE_INT },
190 { ASN_UNIVERSAL, ASN_NULL , BER_TYPE_NULL }, 197 { ASN_UNIVERSAL, ASN_NULL , BER_TYPE_NULL },
191 { ASN_UNIVERSAL, ASN_OBJECT_IDENTIFIER, BER_TYPE_OID }, 198 { ASN_UNIVERSAL, ASN_OBJECT_IDENTIFIER, BER_TYPE_OID },
192 { ASN_UNIVERSAL, ASN_OBJECT_DESCRIPTOR, BER_TYPE_OID },
193 { ASN_UNIVERSAL, ASN_RELATIVE_OID , BER_TYPE_RELOID }, 199 { ASN_UNIVERSAL, ASN_RELATIVE_OID , BER_TYPE_RELOID },
194 { ASN_UNIVERSAL, ASN_REAL , BER_TYPE_REAL }, 200 { ASN_UNIVERSAL, ASN_REAL , BER_TYPE_REAL },
201 { ASN_UNIVERSAL, ASN_ENUMERATED , BER_TYPE_INT },
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/////////////////////////////////////////////////////////////////////////////
329 // the one-digit case is absolutely predominant, so this pays off (hopefully) 336 // the one-digit case is absolutely predominant, so this pays off (hopefully)
330 if (expect_true (u < 10)) 337 if (expect_true (u < 10))
331 *buf++ = u + '0'; 338 *buf++ = u + '0';
332 else 339 else
333 { 340 {
341 // this *could* be done much faster using branchless fixed-ppint arithmetics
334 char *beg = buf; 342 char *beg = buf;
335 343
336 do 344 do
337 { 345 {
338 *buf++ = u % 10 + '0'; 346 *buf++ = u % 10 + '0';
339 u /= 10; 347 u /= 10;
340 } 348 }
341 while (u); 349 while (u);
342 350
343 // reverse digits 351 // reverse digits
344 for (char *ptr = buf; --ptr != beg; ++beg) 352 char *ptr = buf;
353 while (--ptr > beg)
345 { 354 {
346 char c = *ptr; 355 char c = *ptr;
347 *ptr = *beg; 356 *ptr = *beg;
348 *beg = c; 357 *beg = c;
358 ++beg;
349 } 359 }
350 } 360 }
351 361
352 return buf; 362 return buf;
353} 363}
364 } 374 }
365 375
366 U8 *end = cur + len; 376 U8 *end = cur + len;
367 U32 w = get_w (); 377 U32 w = get_w ();
368 378
369 static char oid[MAX_OID_STRLEN]; // must be static 379 static char oid[MAX_OID_STRLEN]; // static, becaueds too large for stack
370 char *app = oid; 380 char *app = oid;
371 381
372 if (relative) 382 if (relative)
373 app = write_uv (app, w); 383 app = write_uv (app, w);
374 else 384 else
376 app = write_uv (app, (U8)w / 40); 386 app = write_uv (app, (U8)w / 40);
377 *app++ = '.'; 387 *app++ = '.';
378 app = write_uv (app, (U8)w % 40); 388 app = write_uv (app, (U8)w % 40);
379 } 389 }
380 390
391 while (cur < end)
392 {
381 // we assume an oid component is never > 64 bytes 393 // we assume an oid component is never > 64 digits
382 while (cur < end && oid + sizeof (oid) - app > 64) 394 if (oid + sizeof (oid) - app < 64)
383 { 395 croak ("BER_TYPE_OID to long to decode");
396
384 w = get_w (); 397 w = get_w ();
385 *app++ = '.'; 398 *app++ = '.';
386 app = write_uv (app, w); 399 app = write_uv (app, w);
387 } 400 }
388 401
389 return newSVpvn (oid, app - oid); 402 return newSVpvn (oid, app - oid);
403}
404
405// TODO: this is unacceptably slow
406static SV *
407decode_ucs (int chrsize)
408{
409 SV *res = NEWSV (0, 0);
410
411 U32 len = get_length ();
412
413 if (len & (chrsize - 1))
414 croak ("BER_TYPE_UCS has an invalid number of octets (%d)", len);
415
416 while (len)
417 {
418 U8 b1 = get_u8 ();
419 U8 b2 = get_u8 ();
420 U32 chr = (b1 << 8) | b2;
421
422 if (chrsize == 4)
423 {
424 U8 b3 = get_u8 ();
425 U8 b4 = get_u8 ();
426 chr = (chr << 16) | (b3 << 8) | b4;
427 }
428
429 U8 uchr [UTF8_MAXBYTES];
430 int uclen = uvuni_to_utf8 (uchr, chr) - uchr;
431
432 sv_catpvn (res, (const char *)uchr, uclen);
433 len -= chrsize;
434 }
435
436 SvUTF8_on (res);
437
438 return res;
390} 439}
391 440
392static SV * 441static SV *
393decode_ber () 442decode_ber ()
394{ 443{
422 } 471 }
423 else 472 else
424 switch (profile_lookup (cur_profile, klass, tag)) 473 switch (profile_lookup (cur_profile, klass, tag))
425 { 474 {
426 case BER_TYPE_NULL: 475 case BER_TYPE_NULL:
476 {
477 U32 len = get_length ();
478
479 if (len)
480 croak ("BER_TYPE_NULL value with non-zero length %d encountered", len);
481
427 res = &PL_sv_undef; 482 res = &PL_sv_undef;
483 }
428 break; 484 break;
429 485
430 case BER_TYPE_BOOL: 486 case BER_TYPE_BOOL:
431 { 487 {
432 U32 len = get_length (); 488 U32 len = get_length ();
433 489
434 if (len != 1) 490 if (len != 1)
435 croak ("BER_TYPE_BOOLEAN type with invalid length %d encountered", len); 491 croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered", len);
436 492
437 res = newSVcacheint (get_u8 () ? 0 : 1); 493 res = newSVcacheint (!!get_u8 ());
438 } 494 }
439 break; 495 break;
440 496
441 case BER_TYPE_OID: 497 case BER_TYPE_OID:
442 res = decode_oid (0); 498 res = decode_oid (0);
473 529
474 res = newSVpvf ("%d.%d.%d.%d", c1, c2, c3, c4); 530 res = newSVpvf ("%d.%d.%d.%d", c1, c2, c3, c4);
475 } 531 }
476 break; 532 break;
477 533
534 case BER_TYPE_UCS2:
535 res = decode_ucs (2);
536 break;
537
538 case BER_TYPE_UCS4:
539 res = decode_ucs (4);
540 break;
541
478 case BER_TYPE_REAL: 542 case BER_TYPE_REAL:
479 case BER_TYPE_UCS2:
480 case BER_TYPE_UCS4:
481 case BER_TYPE_CROAK: 543 case BER_TYPE_CROAK:
482 default: 544 default:
483 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 545 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
484 } 546 }
485 547
511static void 573static void
512set_buf (SV *sv) 574set_buf (SV *sv)
513{ 575{
514 STRLEN len; 576 STRLEN len;
515 buf_sv = sv; 577 buf_sv = sv;
516 buf = SvPVbyte (buf_sv, len); 578 buf = (U8 *)SvPVbyte (buf_sv, len);
517 cur = buf; 579 cur = buf;
518 end = buf + len; 580 end = buf + len;
519} 581}
520 582
521/* similar to SvGROW, but somewhat safer and guarantees exponential realloc strategy */ 583/* similar to SvGROW, but somewhat safer and guarantees exponential realloc strategy */
535need (STRLEN len) 597need (STRLEN len)
536{ 598{
537 if (expect_false ((uintptr_t)(end - cur) < len)) 599 if (expect_false ((uintptr_t)(end - cur) < len))
538 { 600 {
539 STRLEN pos = cur - buf; 601 STRLEN pos = cur - buf;
540 buf = my_sv_grow (buf_sv, pos, len); 602 buf = (U8 *)my_sv_grow (buf_sv, pos, len);
541 cur = buf + pos; 603 cur = buf + pos;
542 end = buf + SvLEN (buf_sv) - 1; 604 end = buf + SvLEN (buf_sv) - 1;
543 } 605 }
544} 606}
545 607
589} 651}
590 652
591static void 653static void
592put_length (U32 val) 654put_length (U32 val)
593{ 655{
594 need (5); 656 need (5 + val);
595 cur = put_length_at (val, cur); 657 cur = put_length_at (val, cur);
596} 658}
597 659
598// return how many bytes the encoded length requires 660// return how many bytes the encoded length requires
599static int length_length (U32 val) 661static int length_length (U32 val)
605 667
606static void 668static void
607encode_data (const char *ptr, STRLEN len) 669encode_data (const char *ptr, STRLEN len)
608{ 670{
609 put_length (len); 671 put_length (len);
610 need (len);
611 memcpy (cur, ptr, len); 672 memcpy (cur, ptr, len);
612 cur += len; 673 cur += len;
613} 674}
614 675
615static void 676static void
761 822
762 return (AV *)rv; 823 return (AV *)rv;
763} 824}
764 825
765static void 826static void
827encode_ucs (SV *data, int chrsize)
828{
829 STRLEN uchars = sv_len_utf8 (data);
830 STRLEN len;;
831 char *ptr = SvPVutf8 (data, len);
832
833 put_length (uchars * chrsize);
834
835 while (uchars--)
836 {
837 STRLEN uclen;
838 UV uchr = utf8_to_uvchr_buf ((U8 *)ptr, (U8 *)ptr + len, &uclen);
839
840 ptr += uclen;
841 len -= uclen;
842
843 if (chrsize == 4)
844 {
845 *cur++ = uchr >> 24;
846 *cur++ = uchr >> 16;
847 }
848
849 *cur++ = uchr >> 8;
850 *cur++ = uchr;
851 }
852}
853static void
766encode_ber (SV *tuple) 854encode_ber (SV *tuple)
767{ 855{
768 AV *av = ber_tuple (tuple); 856 AV *av = ber_tuple (tuple);
769 857
770 int klass = SvIV (AvARRAY (av)[BER_CLASS]); 858 int klass = SvIV (AvARRAY (av)[BER_CLASS]);
796 int fill = AvFILL (av); 884 int fill = AvFILL (av);
797 885
798 if (expect_false (SvRMAGICAL (av))) 886 if (expect_false (SvRMAGICAL (av)))
799 croak ("BER constructed data must not be tied"); 887 croak ("BER constructed data must not be tied");
800 888
889 int i;
801 for (int i = 0; i <= fill; ++i) 890 for (i = 0; i <= fill; ++i)
802 encode_ber (AvARRAY (av)[i]); 891 encode_ber (AvARRAY (av)[i]);
803 892
804 len_fixup (mark); 893 len_fixup (mark);
805 } 894 }
806 else 895 else
810 put_length (0); 899 put_length (0);
811 break; 900 break;
812 901
813 case BER_TYPE_BOOL: 902 case BER_TYPE_BOOL:
814 put_length (1); 903 put_length (1);
815 put_u8 (SvTRUE (data) ? 0xff : 0x00); 904 *cur++ = SvTRUE (data) ? 0xff : 0x00;
816 break; 905 break;
817 906
818 case BER_TYPE_OID: 907 case BER_TYPE_OID:
819 encode_oid (data, 0); 908 encode_oid (data, 0);
820 break; 909 break;
849 sscanf (SvPV_nolen (data), "%hhu.%hhu.%hhu.%hhu", ip + 0, ip + 1, ip + 2, ip + 3); 938 sscanf (SvPV_nolen (data), "%hhu.%hhu.%hhu.%hhu", ip + 0, ip + 1, ip + 2, ip + 3);
850 encode_data ((const char *)ip, sizeof (ip)); 939 encode_data ((const char *)ip, sizeof (ip));
851 } 940 }
852 break; 941 break;
853 942
943 case BER_TYPE_UCS2:
944 encode_ucs (data, 2);
945 break;
946
947 case BER_TYPE_UCS4:
948 encode_ucs (data, 4);
949 break;
950
854 case BER_TYPE_REAL: 951 case BER_TYPE_REAL:
855 case BER_TYPE_UCS2:
856 case BER_TYPE_UCS4:
857 case BER_TYPE_CROAK: 952 case BER_TYPE_CROAK:
858 default: 953 default:
859 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 954 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
860 } 955 }
861 956
950ber_decode (SV *ber, SV *profile = &PL_sv_undef) 1045ber_decode (SV *ber, SV *profile = &PL_sv_undef)
951 CODE: 1046 CODE:
952{ 1047{
953 cur_profile = SvPROFILE (profile); 1048 cur_profile = SvPROFILE (profile);
954 STRLEN len; 1049 STRLEN len;
955 buf = SvPVbyte (ber, len); 1050 buf = (U8 *)SvPVbyte (ber, len);
956 cur = buf; 1051 cur = buf;
957 end = buf + len; 1052 end = buf + len;
958 1053
959 RETVAL = decode_ber (); 1054 RETVAL = decode_ber ();
960} 1055}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines