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.13 by root, Sat Apr 20 13:46:14 2019 UTC vs.
Revision 1.17 by root, Sat Apr 20 15:45:21 2019 UTC

8// sscanf format modifiers and more. 8// sscanf format modifiers and more.
9 9
10enum { 10enum {
11 // ASN_TAG 11 // ASN_TAG
12 ASN_BOOLEAN = 0x01, 12 ASN_BOOLEAN = 0x01,
13 ASN_INTEGER32 = 0x02, 13 ASN_INTEGER = 0x02,
14 ASN_BIT_STRING = 0x03, 14 ASN_BIT_STRING = 0x03,
15 ASN_OCTET_STRING = 0x04, 15 ASN_OCTET_STRING = 0x04,
16 ASN_NULL = 0x05, 16 ASN_NULL = 0x05,
17 ASN_OBJECT_IDENTIFIER = 0x06, 17 ASN_OBJECT_IDENTIFIER = 0x06,
18 ASN_OID = 0x06, 18 ASN_OID = 0x06,
80 BER_TYPE_IPADDRESS, 80 BER_TYPE_IPADDRESS,
81 BER_TYPE_CROAK, 81 BER_TYPE_CROAK,
82}; 82};
83 83
84enum { 84enum {
85 BER_CLASS = 0, 85 BER_CLASS = 0,
86 BER_TAG = 1, 86 BER_TAG = 1,
87 BER_CONSTRUCTED = 2, 87 BER_FLAGS = 2,
88 BER_DATA = 3, 88 BER_DATA = 3,
89 BER_ARRAYSIZE 89 BER_ARRAYSIZE
90}; 90};
91 91
92#define MAX_OID_STRLEN 4096 92#define MAX_OID_STRLEN 4096
93 93
181 181
182 SvPVX (sv)[idx] = type; 182 SvPVX (sv)[idx] = type;
183} 183}
184 184
185static SV * 185static SV *
186profile_new () 186profile_new (void)
187{ 187{
188 SV *sv = newSVpvn ("", 0); 188 SV *sv = newSVpvn ("", 0);
189 189
190 static const struct { 190 static const struct {
191 int klass; 191 int klass;
192 int tag; 192 int tag;
193 int type; 193 int type;
194 } *celem, default_map[] = { 194 } *celem, default_map[] = {
195 { ASN_UNIVERSAL, ASN_BOOLEAN , BER_TYPE_BOOL }, 195 { ASN_UNIVERSAL, ASN_BOOLEAN , BER_TYPE_BOOL },
196 { ASN_UNIVERSAL, ASN_INTEGER32 , BER_TYPE_INT }, 196 { ASN_UNIVERSAL, ASN_INTEGER , BER_TYPE_INT },
197 { ASN_UNIVERSAL, ASN_NULL , BER_TYPE_NULL }, 197 { ASN_UNIVERSAL, ASN_NULL , BER_TYPE_NULL },
198 { ASN_UNIVERSAL, ASN_OBJECT_IDENTIFIER, BER_TYPE_OID }, 198 { ASN_UNIVERSAL, ASN_OBJECT_IDENTIFIER, BER_TYPE_OID },
199 { ASN_UNIVERSAL, ASN_RELATIVE_OID , BER_TYPE_RELOID }, 199 { ASN_UNIVERSAL, ASN_RELATIVE_OID , BER_TYPE_RELOID },
200 { ASN_UNIVERSAL, ASN_REAL , BER_TYPE_REAL }, 200 { ASN_UNIVERSAL, ASN_REAL , BER_TYPE_REAL },
201 { ASN_UNIVERSAL, ASN_ENUMERATED , BER_TYPE_INT }, 201 { ASN_UNIVERSAL, ASN_ENUMERATED , BER_TYPE_INT },
248 248
249 return *cur++; 249 return *cur++;
250} 250}
251 251
252// get ber-encoded integer (i.e. pack "w") 252// get ber-encoded integer (i.e. pack "w")
253static U32 253static UV
254get_w (void) 254get_w (void)
255{ 255{
256 U32 res = 0; 256 UV res = 0;
257 257
258 for (;;) 258 for (;;)
259 { 259 {
260 U8 c = get_u8 (); 260 U8 c = get_u8 ();
261 res = (res << 7) | (c & 0x7f); 261 res = (res << 7) | (c & 0x7f);
263 if (!(c & 0x80)) 263 if (!(c & 0x80))
264 return res; 264 return res;
265 } 265 }
266} 266}
267 267
268static U32 268static UV
269get_length (void) 269get_length (void)
270{ 270{
271 U32 res = get_u8 (); 271 UV res = get_u8 ();
272 272
273 if (res & 0x80) 273 if (res & 0x80)
274 { 274 {
275 int cnt = res & 0x7f; 275 int cnt = res & 0x7f;
276 res = 0; 276 res = 0;
283 283
284 default: 284 default:
285 error ("ASN.1 length too long"); 285 error ("ASN.1 length too long");
286 return 0; 286 return 0;
287 287
288 case 8: res = (res << 8) | get_u8 ();
289 case 7: res = (res << 8) | get_u8 ();
290 case 6: res = (res << 8) | get_u8 ();
291 case 5: res = (res << 8) | get_u8 ();
288 case 4: res = (res << 8) | get_u8 (); 292 case 4: res = (res << 8) | get_u8 ();
289 case 3: res = (res << 8) | get_u8 (); 293 case 3: res = (res << 8) | get_u8 ();
290 case 2: res = (res << 8) | get_u8 (); 294 case 2: res = (res << 8) | get_u8 ();
291 case 1: res = (res << 8) | get_u8 (); 295 case 1: res = (res << 8) | get_u8 ();
292 } 296 }
294 298
295 return res; 299 return res;
296} 300}
297 301
298static SV * 302static SV *
299decode_int () 303decode_int (void)
300{ 304{
301 int len = get_length (); 305 UV len = get_length ();
302 306
303 if (len <= 0) 307 if (!len)
304 { 308 {
305 error ("integer length equal to zero"); 309 error ("invalid integer length equal to zero");
306 return 0; 310 return 0;
307 } 311 }
308 312
309 U8 *data = get_n (len); 313 U8 *data = get_n (len);
310 314
322} 326}
323 327
324static SV * 328static SV *
325decode_data (void) 329decode_data (void)
326{ 330{
327 U32 len = get_length (); 331 UV len = get_length ();
328 U8 *data = get_n (len);
329 return newSVpvn ((char *)data, len); 332 return newSVpvn ((char *)get_n (len), len);
330} 333}
331 334
332// gelper for decode_object_identifier 335// helper for decode_object_identifier
333static char * 336static char *
334write_uv (char *buf, U32 u) 337write_uv (char *buf, UV u)
335{ 338{
336 // the one-digit case is absolutely predominant, so this pays off (hopefully) 339 // the one-digit case is absolutely predominant, so this pays off (hopefully)
337 if (expect_true (u < 10)) 340 if (expect_true (u < 10))
338 *buf++ = u + '0'; 341 *buf++ = u + '0';
339 else 342 else
340 { 343 {
341 // this *could* be done much faster using branchless fixed-ppint arithmetics 344 // this *could* be done much faster using branchless fixed-point arithmetics
342 char *beg = buf; 345 char *beg = buf;
343 346
344 do 347 do
345 { 348 {
346 *buf++ = u % 10 + '0'; 349 *buf++ = u % 10 + '0';
363} 366}
364 367
365static SV * 368static SV *
366decode_oid (int relative) 369decode_oid (int relative)
367{ 370{
368 U32 len = get_length (); 371 UV len = get_length ();
369 372
370 if (len <= 0) 373 if (len <= 0)
371 { 374 {
372 error ("OBJECT IDENTIFIER length equal to zero"); 375 error ("OBJECT IDENTIFIER length equal to zero");
373 return &PL_sv_undef; 376 return &PL_sv_undef;
374 } 377 }
375 378
376 U8 *end = cur + len; 379 U8 *end = cur + len;
377 U32 w = get_w (); 380 UV w = get_w ();
378 381
379 static char oid[MAX_OID_STRLEN]; // static, becaueds too large for stack 382 static char oid[MAX_OID_STRLEN]; // static, becaueds too large for stack
380 char *app = oid; 383 char *app = oid;
381 384
382 if (relative) 385 if (relative)
406static SV * 409static SV *
407decode_ucs (int chrsize) 410decode_ucs (int chrsize)
408{ 411{
409 SV *res = NEWSV (0, 0); 412 SV *res = NEWSV (0, 0);
410 413
411 U32 len = get_length (); 414 UV len = get_length ();
412 415
413 if (len & (chrsize - 1)) 416 if (len & (chrsize - 1))
414 croak ("BER_TYPE_UCS has an invalid number of octets (%d)", len); 417 croak ("BER_TYPE_UCS has an invalid number of octets (%d)", len);
415 418
416 while (len) 419 while (len)
437 440
438 return res; 441 return res;
439} 442}
440 443
441static SV * 444static SV *
442decode_ber () 445decode_ber (void)
443{ 446{
444 int identifier = get_u8 (); 447 int identifier = get_u8 ();
445 448
446 SV *res; 449 SV *res;
447 450
455 if (tag == ASN_TAG_BER) 458 if (tag == ASN_TAG_BER)
456 tag = get_w (); 459 tag = get_w ();
457 460
458 if (constructed) 461 if (constructed)
459 { 462 {
460 U32 len = get_length (); 463 UV len = get_length ();
461 U32 seqend = (cur - buf) + len; 464 UV seqend = (cur - buf) + len;
462 AV *av = (AV *)sv_2mortal ((SV *)newAV ()); 465 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
463 466
464 while (cur < buf + seqend) 467 while (cur < buf + seqend)
465 av_push (av, decode_ber ()); 468 av_push (av, decode_ber ());
466 469
472 else 475 else
473 switch (profile_lookup (cur_profile, klass, tag)) 476 switch (profile_lookup (cur_profile, klass, tag))
474 { 477 {
475 case BER_TYPE_NULL: 478 case BER_TYPE_NULL:
476 { 479 {
477 U32 len = get_length (); 480 UV len = get_length ();
478 481
479 if (len) 482 if (len)
480 croak ("BER_TYPE_NULL value with non-zero length %d encountered", len); 483 croak ("BER_TYPE_NULL value with non-zero length %d encountered", len);
481 484
482 res = &PL_sv_undef; 485 res = &PL_sv_undef;
483 } 486 }
484 break; 487 break;
485 488
486 case BER_TYPE_BOOL: 489 case BER_TYPE_BOOL:
487 { 490 {
488 U32 len = get_length (); 491 UV len = get_length ();
489 492
490 if (len != 1) 493 if (len != 1)
491 croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered", len); 494 croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered", len);
492 495
493 res = newSVcacheint (!!get_u8 ()); 496 res = newSVcacheint (!!get_u8 ());
515 res = decode_data (); 518 res = decode_data ();
516 break; 519 break;
517 520
518 case BER_TYPE_IPADDRESS: 521 case BER_TYPE_IPADDRESS:
519 { 522 {
520 U32 len = get_length (); 523 UV len = get_length ();
521 524
522 if (len != 4) 525 if (len != 4)
523 croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered", len); 526 croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered", len);
524 527
525 U8 c1 = get_u8 (); 528 U8 c1 = get_u8 ();
545 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 548 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
546 } 549 }
547 550
548 AV *av = newAV (); 551 AV *av = newAV ();
549 av_fill (av, BER_ARRAYSIZE - 1); 552 av_fill (av, BER_ARRAYSIZE - 1);
550 AvARRAY (av)[BER_CLASS ] = newSVcacheint (klass); 553 AvARRAY (av)[BER_CLASS] = newSVcacheint (klass);
551 AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); 554 AvARRAY (av)[BER_TAG ] = newSVcacheint (tag);
552 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (constructed ? 1 : 0); 555 AvARRAY (av)[BER_FLAGS] = newSVcacheint (constructed ? 1 : 0);
553 AvARRAY (av)[BER_DATA ] = res; 556 AvARRAY (av)[BER_DATA ] = res;
554 557
555 return newRV_noinc ((SV *)av); 558 return newRV_noinc ((SV *)av);
556} 559}
557 560
558///////////////////////////////////////////////////////////////////////////// 561/////////////////////////////////////////////////////////////////////////////
611 need (1); 614 need (1);
612 *cur++ = val; 615 *cur++ = val;
613} 616}
614 617
615static void 618static void
616put_w_nocheck (U32 val) 619put_w_nocheck (UV val)
617{ 620{
621#if UVSIZE > 4
622 *cur = (val >> 7 * 9) | 0x80; cur += val >= ((UV)1 << (7 * 9));
623 *cur = (val >> 7 * 8) | 0x80; cur += val >= ((UV)1 << (7 * 8));
624 *cur = (val >> 7 * 7) | 0x80; cur += val >= ((UV)1 << (7 * 7));
625 *cur = (val >> 7 * 6) | 0x80; cur += val >= ((UV)1 << (7 * 6));
626 *cur = (val >> 7 * 5) | 0x80; cur += val >= ((UV)1 << (7 * 5));
627#endif
618 *cur = (val >> 7 * 4) | 0x80; cur += val >= (1 << (7 * 4)); 628 *cur = (val >> 7 * 4) | 0x80; cur += val >= ((UV)1 << (7 * 4));
619 *cur = (val >> 7 * 3) | 0x80; cur += val >= (1 << (7 * 3)); 629 *cur = (val >> 7 * 3) | 0x80; cur += val >= ((UV)1 << (7 * 3));
620 *cur = (val >> 7 * 2) | 0x80; cur += val >= (1 << (7 * 2)); 630 *cur = (val >> 7 * 2) | 0x80; cur += val >= ((UV)1 << (7 * 2));
621 *cur = (val >> 7 * 1) | 0x80; cur += val >= (1 << (7 * 1)); 631 *cur = (val >> 7 * 1) | 0x80; cur += val >= ((UV)1 << (7 * 1));
622 *cur = val & 0x7f; cur += 1; 632 *cur = val & 0x7f; cur += 1;
623} 633}
624 634
625static void 635static void
626put_w (U32 val) 636put_w (UV val)
627{ 637{
628 need (5); // we only handle up to 5 bytes 638 need (5); // we only handle up to 5 bytes
629 639
630 put_w_nocheck (val); 640 put_w_nocheck (val);
631} 641}
632 642
633static U8 * 643static U8 *
634put_length_at (U32 val, U8 *cur) 644put_length_at (UV val, U8 *cur)
635{ 645{
636 if (val < 0x7fU) 646 if (val < 0x7fU)
637 *cur++ = val; 647 *cur++ = val;
638 else 648 else
639 { 649 {
640 U8 *lenb = cur++; 650 U8 *lenb = cur++;
641 651
652#if UVSIZE > 4
653 *cur = val >> 56; cur += *cur > 0;
654 *cur = val >> 48; cur += *cur > 0;
655 *cur = val >> 40; cur += *cur > 0;
656 *cur = val >> 32; cur += *cur > 0;
657#endif
642 *cur = val >> 24; cur += *cur > 0; 658 *cur = val >> 24; cur += *cur > 0;
643 *cur = val >> 16; cur += *cur > 0; 659 *cur = val >> 16; cur += *cur > 0;
644 *cur = val >> 8; cur += *cur > 0; 660 *cur = val >> 8; cur += *cur > 0;
645 *cur = val ; cur += 1; 661 *cur = val ; cur += 1;
646 662
649 665
650 return cur; 666 return cur;
651} 667}
652 668
653static void 669static void
654put_length (U32 val) 670put_length (UV val)
655{ 671{
656 need (5 + val); 672 need (5 + val);
657 cur = put_length_at (val, cur); 673 cur = put_length_at (val, cur);
658} 674}
659 675
660// return how many bytes the encoded length requires 676// return how many bytes the encoded length requires
661static int length_length (U32 val) 677static int length_length (UV val)
662{ 678{
663 return val < 0x7fU 679 return val < 0x7fU
664 ? 1 680 ? 1
665 : 2 + (val > 0xffU) + (val > 0xffffU) + (val > 0xffffffU); 681 : 2
682 + (val > 0xffU)
683 + (val > 0xffffU)
684 + (val > 0xffffffU)
685#if UVSIZE > 4
686 + (val > 0xffffffffU)
687 + (val > 0xffffffffffU)
688 + (val > 0xffffffffffffU)
689 + (val > 0xffffffffffffffU)
690#endif
691 ;
666} 692}
667 693
668static void 694static void
669encode_data (const char *ptr, STRLEN len) 695encode_data (const char *ptr, STRLEN len)
670{ 696{
732 758
733 *lenb = cur - lenb - 1; 759 *lenb = cur - lenb - 1;
734} 760}
735 761
736// we don't know the length yet, so we optimistically 762// we don't know the length yet, so we optimistically
737// assume the length will need one octet later. if that 763// assume the length will need one octet later. If that
738// turns out to be wrong, we memove as needed. 764// turns out to be wrong, we memmove as needed.
739// mark the beginning 765// mark the beginning
740static STRLEN 766static STRLEN
741len_fixup_mark () 767len_fixup_mark (void)
742{ 768{
743 return cur++ - buf; 769 return cur++ - buf;
744} 770}
745 771
746// patch up the length 772// patch up the length
855{ 881{
856 AV *av = ber_tuple (tuple); 882 AV *av = ber_tuple (tuple);
857 883
858 int klass = SvIV (AvARRAY (av)[BER_CLASS]); 884 int klass = SvIV (AvARRAY (av)[BER_CLASS]);
859 int tag = SvIV (AvARRAY (av)[BER_TAG]); 885 int tag = SvIV (AvARRAY (av)[BER_TAG]);
860 int constructed = SvIV (AvARRAY (av)[BER_CONSTRUCTED]) ? ASN_CONSTRUCTED : 0; 886 int constructed = SvIV (AvARRAY (av)[BER_FLAGS]) & 1 ? ASN_CONSTRUCTED : 0;
861 SV *data = AvARRAY (av)[BER_DATA]; 887 SV *data = AvARRAY (av)[BER_DATA];
862 888
863 int identifier = (klass << ASN_CLASS_SHIFT) | constructed; 889 int identifier = (klass << ASN_CLASS_SHIFT) | constructed;
864 890
865 if (expect_false (tag >= ASN_TAG_BER)) 891 if (expect_false (tag >= ASN_TAG_BER))
899 put_length (0); 925 put_length (0);
900 break; 926 break;
901 927
902 case BER_TYPE_BOOL: 928 case BER_TYPE_BOOL:
903 put_length (1); 929 put_length (1);
904 *cur++ = SvTRUE (data) ? 0xff : 0x00; 930 *cur++ = SvTRUE (data) ? 0xff : 0x00; // 0xff = DER/CER
905 break; 931 break;
906 932
907 case BER_TYPE_OID: 933 case BER_TYPE_OID:
908 encode_oid (data, 0); 934 encode_oid (data, 0);
909 break; 935 break;
972 const char *name; 998 const char *name;
973 IV iv; 999 IV iv;
974 } *civ, const_iv[] = { 1000 } *civ, const_iv[] = {
975#define const_iv(name) { # name, name }, 1001#define const_iv(name) { # name, name },
976 const_iv (ASN_BOOLEAN) 1002 const_iv (ASN_BOOLEAN)
977 const_iv (ASN_INTEGER32) 1003 const_iv (ASN_INTEGER)
978 const_iv (ASN_BIT_STRING) 1004 const_iv (ASN_BIT_STRING)
979 const_iv (ASN_OCTET_STRING) 1005 const_iv (ASN_OCTET_STRING)
980 const_iv (ASN_NULL) 1006 const_iv (ASN_NULL)
981 const_iv (ASN_OBJECT_IDENTIFIER) 1007 const_iv (ASN_OBJECT_IDENTIFIER)
982 const_iv (ASN_OBJECT_DESCRIPTOR) 1008 const_iv (ASN_OBJECT_DESCRIPTOR)
1011 const_iv (ASN_CONTEXT) 1037 const_iv (ASN_CONTEXT)
1012 const_iv (ASN_PRIVATE) 1038 const_iv (ASN_PRIVATE)
1013 1039
1014 const_iv (BER_CLASS) 1040 const_iv (BER_CLASS)
1015 const_iv (BER_TAG) 1041 const_iv (BER_TAG)
1016 const_iv (BER_CONSTRUCTED) 1042 const_iv (BER_FLAGS)
1017 const_iv (BER_DATA) 1043 const_iv (BER_DATA)
1018 1044
1019 const_iv (BER_TYPE_BYTES) 1045 const_iv (BER_TYPE_BYTES)
1020 const_iv (BER_TYPE_UTF8) 1046 const_iv (BER_TYPE_UTF8)
1021 const_iv (BER_TYPE_UCS2) 1047 const_iv (BER_TYPE_UCS2)
1054 RETVAL = decode_ber (); 1080 RETVAL = decode_ber ();
1055} 1081}
1056 OUTPUT: RETVAL 1082 OUTPUT: RETVAL
1057 1083
1058void 1084void
1059ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *constructed = &PL_sv_undef, SV *data = &PL_sv_undef) 1085ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *flags = &PL_sv_undef, SV *data = &PL_sv_undef)
1060 PPCODE: 1086 PPCODE:
1061{ 1087{
1062 if (!SvOK (tuple)) 1088 if (!SvOK (tuple))
1063 XSRETURN_NO; 1089 XSRETURN_NO;
1064 1090
1066 croak ("ber_is: tuple must be BER tuple (array-ref)"); 1092 croak ("ber_is: tuple must be BER tuple (array-ref)");
1067 1093
1068 AV *av = (AV *)SvRV (tuple); 1094 AV *av = (AV *)SvRV (tuple);
1069 1095
1070 XPUSHs ( 1096 XPUSHs (
1071 (!SvOK (klass) || SvIV (AvARRAY (av)[BER_CLASS ]) == SvIV (klass)) 1097 (!SvOK (klass) || SvIV (AvARRAY (av)[BER_CLASS]) == SvIV (klass))
1072 && (!SvOK (tag) || SvIV (AvARRAY (av)[BER_TAG ]) == SvIV (tag)) 1098 && (!SvOK (tag) || SvIV (AvARRAY (av)[BER_TAG ]) == SvIV (tag))
1073 && (!SvOK (constructed) || !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) == !SvIV (constructed)) 1099 && (!SvOK (flags) || !SvIV (AvARRAY (av)[BER_FLAGS]) == !SvIV (flags))
1074 && (!SvOK (data) || sv_eq (AvARRAY (av)[BER_DATA ], data)) 1100 && (!SvOK (data) || sv_eq (AvARRAY (av)[BER_DATA ], data))
1075 ? &PL_sv_yes : &PL_sv_undef); 1101 ? &PL_sv_yes : &PL_sv_undef);
1076} 1102}
1077 1103
1078void 1104void
1079ber_is_seq (SV *tuple) 1105ber_is_seq (SV *tuple)
1083 XSRETURN_UNDEF; 1109 XSRETURN_UNDEF;
1084 1110
1085 AV *av = ber_tuple (tuple); 1111 AV *av = ber_tuple (tuple);
1086 1112
1087 XPUSHs ( 1113 XPUSHs (
1088 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL 1114 SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL
1089 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_SEQUENCE 1115 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_SEQUENCE
1090 && SvIV (AvARRAY (av)[BER_CONSTRUCTED]) 1116 && SvIV (AvARRAY (av)[BER_FLAGS])
1091 ? AvARRAY (av)[BER_DATA] : &PL_sv_undef); 1117 ? AvARRAY (av)[BER_DATA] : &PL_sv_undef);
1092} 1118}
1093 1119
1094void 1120void
1095ber_is_i32 (SV *tuple, SV *value = &PL_sv_undef) 1121ber_is_int (SV *tuple, SV *value = &PL_sv_undef)
1096 PPCODE: 1122 PPCODE:
1097{ 1123{
1098 if (!SvOK (tuple)) 1124 if (!SvOK (tuple))
1099 XSRETURN_NO; 1125 XSRETURN_NO;
1100 1126
1101 AV *av = ber_tuple (tuple); 1127 AV *av = ber_tuple (tuple);
1102 1128
1103 IV data = SvIV (AvARRAY (av)[BER_DATA]); 1129 UV data = SvUV (AvARRAY (av)[BER_DATA]);
1104 1130
1105 XPUSHs ( 1131 XPUSHs (
1106 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL 1132 SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL
1107 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_INTEGER32 1133 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_INTEGER
1108 && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) 1134 && !SvIV (AvARRAY (av)[BER_FLAGS])
1109 && (!SvOK (value) || data == SvIV (value)) 1135 && (!SvOK (value) || data == SvUV (value))
1110 ? sv_2mortal (data ? newSViv (data) : newSVpv ("0 but true", 0)) 1136 ? sv_2mortal (data ? newSVsv (AvARRAY (av)[BER_DATA]) : newSVpv ("0 but true", 0))
1111 : &PL_sv_undef); 1137 : &PL_sv_undef);
1112} 1138}
1113 1139
1114void 1140void
1115ber_is_oid (SV *tuple, SV *oid = &PL_sv_undef) 1141ber_is_oid (SV *tuple, SV *oid = &PL_sv_undef)
1119 XSRETURN_NO; 1145 XSRETURN_NO;
1120 1146
1121 AV *av = ber_tuple (tuple); 1147 AV *av = ber_tuple (tuple);
1122 1148
1123 XPUSHs ( 1149 XPUSHs (
1124 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL 1150 SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL
1125 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_OBJECT_IDENTIFIER 1151 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_OBJECT_IDENTIFIER
1126 && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) 1152 && !SvIV (AvARRAY (av)[BER_FLAGS])
1127 && (!SvOK (oid) || sv_eq (AvARRAY (av)[BER_DATA], oid)) 1153 && (!SvOK (oid) || sv_eq (AvARRAY (av)[BER_DATA], oid))
1128 ? newSVsv (AvARRAY (av)[BER_DATA]) : &PL_sv_undef); 1154 ? newSVsv (AvARRAY (av)[BER_DATA]) : &PL_sv_undef);
1129} 1155}
1130 1156
1131############################################################################# 1157#############################################################################
1144 SvCUR_set (buf_sv, cur - buf); 1170 SvCUR_set (buf_sv, cur - buf);
1145 XPUSHs (buf_sv); 1171 XPUSHs (buf_sv);
1146} 1172}
1147 1173
1148SV * 1174SV *
1149ber_i32 (IV iv) 1175ber_int (SV *sv)
1150 CODE: 1176 CODE:
1151{ 1177{
1152 AV *av = newAV (); 1178 AV *av = newAV ();
1153 av_fill (av, BER_ARRAYSIZE - 1); 1179 av_fill (av, BER_ARRAYSIZE - 1);
1154 AvARRAY (av)[BER_CLASS ] = newSVcacheint (ASN_UNIVERSAL); 1180 AvARRAY (av)[BER_CLASS] = newSVcacheint (ASN_UNIVERSAL);
1155 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_INTEGER32); 1181 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_INTEGER);
1156 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (0); 1182 AvARRAY (av)[BER_FLAGS] = newSVcacheint (0);
1157 AvARRAY (av)[BER_DATA ] = newSViv (iv); 1183 AvARRAY (av)[BER_DATA ] = newSVsv (sv);
1158 RETVAL = newRV_noinc ((SV *)av); 1184 RETVAL = newRV_noinc ((SV *)av);
1159} 1185}
1160 OUTPUT: RETVAL 1186 OUTPUT: RETVAL
1161 1187
1162# TODO: not arrayref, but elements? 1188# TODO: not arrayref, but elements?
1164ber_seq (SV *arrayref) 1190ber_seq (SV *arrayref)
1165 CODE: 1191 CODE:
1166{ 1192{
1167 AV *av = newAV (); 1193 AV *av = newAV ();
1168 av_fill (av, BER_ARRAYSIZE - 1); 1194 av_fill (av, BER_ARRAYSIZE - 1);
1169 AvARRAY (av)[BER_CLASS ] = newSVcacheint (ASN_UNIVERSAL); 1195 AvARRAY (av)[BER_CLASS] = newSVcacheint (ASN_UNIVERSAL);
1170 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_SEQUENCE); 1196 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_SEQUENCE);
1171 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (1); 1197 AvARRAY (av)[BER_FLAGS] = newSVcacheint (1);
1172 AvARRAY (av)[BER_DATA ] = newSVsv (arrayref); 1198 AvARRAY (av)[BER_DATA ] = newSVsv (arrayref);
1173 RETVAL = newRV_noinc ((SV *)av); 1199 RETVAL = newRV_noinc ((SV *)av);
1174} 1200}
1175 OUTPUT: RETVAL 1201 OUTPUT: RETVAL
1176 1202
1177MODULE = Convert::BER::XS PACKAGE = Convert::BER::XS::Profile 1203MODULE = Convert::BER::XS PACKAGE = Convert::BER::XS::Profile

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines