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.15 by root, Sat Apr 20 15:23:26 2019 UTC vs.
Revision 1.27 by root, Mon Apr 22 11:01:49 2019 UTC

58 ASN_CLASS_SHIFT = 6, 58 ASN_CLASS_SHIFT = 6,
59 59
60 // ASN_APPLICATION SNMP 60 // ASN_APPLICATION SNMP
61 SNMP_IPADDRESS = 0x00, 61 SNMP_IPADDRESS = 0x00,
62 SNMP_COUNTER32 = 0x01, 62 SNMP_COUNTER32 = 0x01,
63 SNMP_GAUGE32 = 0x02,
63 SNMP_UNSIGNED32 = 0x02, 64 SNMP_UNSIGNED32 = 0x02,
64 SNMP_TIMETICKS = 0x03, 65 SNMP_TIMETICKS = 0x03,
65 SNMP_OPAQUE = 0x04, 66 SNMP_OPAQUE = 0x04,
66 SNMP_COUNTER64 = 0x06, 67 SNMP_COUNTER64 = 0x06,
67}; 68};
80 BER_TYPE_IPADDRESS, 81 BER_TYPE_IPADDRESS,
81 BER_TYPE_CROAK, 82 BER_TYPE_CROAK,
82}; 83};
83 84
84enum { 85enum {
85 BER_CLASS = 0, 86 BER_CLASS = 0,
86 BER_TAG = 1, 87 BER_TAG = 1,
87 BER_CONSTRUCTED = 2, 88 BER_FLAGS = 2,
88 BER_DATA = 3, 89 BER_DATA = 3,
89 BER_ARRAYSIZE 90 BER_ARRAYSIZE
90}; 91};
91 92
92#define MAX_OID_STRLEN 4096 93#define MAX_OID_STRLEN 4096
93 94
140{ 141{
141 if (!SvOK (profile)) 142 if (!SvOK (profile))
142 return default_profile; 143 return default_profile;
143 144
144 if (!SvROK (profile)) 145 if (!SvROK (profile))
145 croak ("invalid profile"); 146 croak ("Convert::BER::XS::Profile expected");
146 147
147 profile = SvRV (profile); 148 profile = SvRV (profile);
148 149
149 if (SvSTASH (profile) != profile_stash) 150 if (SvSTASH (profile) != profile_stash)
150 croak ("invalid profile object"); 151 croak ("Convert::BER::XS::Profile expected");
151 152
152 return (void *)profile; 153 return (void *)profile;
153} 154}
154 155
155static int 156static int
227} 228}
228 229
229// get_* functions fetch something from the buffer 230// get_* functions fetch something from the buffer
230// decode_* functions use get_* fun ctions to decode ber values 231// decode_* functions use get_* fun ctions to decode ber values
231 232
233// get single octet
234static U8
235get_u8 (void)
236{
237 if (cur == end)
238 error ("unexpected end of message buffer");
239
240 return *cur++;
241}
242
232// get n octets 243// get n octets
233static U8 * 244static U8 *
234get_n (UV count) 245get_n (UV count)
235{ 246{
236 want (count); 247 want (count);
237 U8 *res = cur; 248 U8 *res = cur;
238 cur += count; 249 cur += count;
239 return res; 250 return res;
240} 251}
241 252
242// get single octet
243static U8
244get_u8 (void)
245{
246 if (cur == end)
247 error ("unexpected end of message buffer");
248
249 return *cur++;
250}
251
252// get ber-encoded integer (i.e. pack "w") 253// get ber-encoded integer (i.e. pack "w")
253static UV 254static UV
254get_w (void) 255get_w (void)
255{ 256{
256 UV res = 0; 257 UV res = 0;
258 U8 c = get_u8 ();
259
260 if (expect_false (c == 0x80))
261 error ("illegal BER padding (X.690 8.1.2.4.2, 8.19.2)");
257 262
258 for (;;) 263 for (;;)
259 { 264 {
260 U8 c = get_u8 (); 265 if (expect_false (res >> UVSIZE * 8 - 7))
266 error ("BER variable length integer overflow");
267
261 res = (res << 7) | (c & 0x7f); 268 res = (res << 7) | (c & 0x7f);
262 269
263 if (!(c & 0x80)) 270 if (!(c & 0x80))
264 return res; 271 return res;
272
273 c = get_u8 ();
265 } 274 }
266} 275}
267 276
268static UV 277static UV
269get_length (void) 278get_length (void)
276 res = 0; 285 res = 0;
277 286
278 switch (cnt) 287 switch (cnt)
279 { 288 {
280 case 0: 289 case 0:
281 error ("indefinite ASN.1 lengths not supported"); 290 error ("indefinite BER value lengths not supported");
282 return 0; 291
292 case 0x7f:
293 error ("BER reserved value in length (X.690 8.1.3.5)");
283 294
284 default: 295 default:
285 error ("ASN.1 length too long"); 296 error ("BER value length too long (must fit into UV)");
286 return 0;
287 297
298#if UVSIZE > 4
288 case 8: res = (res << 8) | get_u8 (); 299 case 8: res = (res << 8) | get_u8 ();
289 case 7: res = (res << 8) | get_u8 (); 300 case 7: res = (res << 8) | get_u8 ();
290 case 6: res = (res << 8) | get_u8 (); 301 case 6: res = (res << 8) | get_u8 ();
291 case 5: res = (res << 8) | get_u8 (); 302 case 5: res = (res << 8) | get_u8 ();
303#endif
292 case 4: res = (res << 8) | get_u8 (); 304 case 4: res = (res << 8) | get_u8 ();
293 case 3: res = (res << 8) | get_u8 (); 305 case 3: res = (res << 8) | get_u8 ();
294 case 2: res = (res << 8) | get_u8 (); 306 case 2: res = (res << 8) | get_u8 ();
295 case 1: res = (res << 8) | get_u8 (); 307 case 1: res = (res << 8) | get_u8 ();
296 } 308 }
303decode_int (void) 315decode_int (void)
304{ 316{
305 UV len = get_length (); 317 UV len = get_length ();
306 318
307 if (!len) 319 if (!len)
308 { 320 error ("invalid BER_TYPE_INT length zero (X.690 8.3.1)");
309 error ("invalid integer length equal to zero");
310 return 0;
311 }
312 321
313 U8 *data = get_n (len); 322 U8 *data = get_n (len);
314 323
324 if (expect_false (len > 1))
325 {
326 U16 mask = (data [0] << 8) | data [1] & 0xff80;
327
328 if (expect_false (mask == 0xff80 || mask == 0x0000))
329 error ("illegal padding in BER_TYPE_INT (X.690 8.3.2)");
330 }
331
315 int negative = data [0] & 0x80; 332 int negative = data [0] & 0x80;
316 333
317 UV val = negative ? -1 : 0; // copy signbit to all bits 334 UV val = negative ? -1 : 0; // copy signbit to all bits
335
336 if (len > UVSIZE + (!negative && !*data))
337 error ("BER_TYPE_INT overflow");
318 338
319 do 339 do
320 val = (val << 8) | *data++; 340 val = (val << 8) | *data++;
321 while (--len); 341 while (--len);
322 342
370{ 390{
371 UV len = get_length (); 391 UV len = get_length ();
372 392
373 if (len <= 0) 393 if (len <= 0)
374 { 394 {
375 error ("OBJECT IDENTIFIER length equal to zero"); 395 error ("BER_TYPE_OID length must not be zero");
376 return &PL_sv_undef; 396 return &PL_sv_undef;
377 } 397 }
378 398
379 U8 *end = cur + len; 399 U8 *end = cur + len;
380 UV w = get_w (); 400 UV w = get_w ();
381 401
382 static char oid[MAX_OID_STRLEN]; // static, becaueds too large for stack 402 static char oid[MAX_OID_STRLEN]; // static, because too large for stack
383 char *app = oid; 403 char *app = oid;
384 404
385 if (relative) 405 if (relative)
386 app = write_uv (app, w); 406 app = write_uv (app, w);
387 else 407 else
388 { 408 {
409 UV w1, w2;
410
411 if (w < 2 * 40)
412 (w1 = w / 40), (w2 = w % 40);
413 else
414 (w1 = 2), (w2 = w - 2 * 40);
415
389 app = write_uv (app, (U8)w / 40); 416 app = write_uv (app, w1);
390 *app++ = '.'; 417 *app++ = '.';
391 app = write_uv (app, (U8)w % 40); 418 app = write_uv (app, w2);
392 } 419 }
393 420
394 while (cur < end) 421 while (cur < end)
395 { 422 {
396 // we assume an oid component is never > 64 digits 423 // we assume an oid component is never > 64 digits
453 int tag = identifier & ASN_TAG_MASK; 480 int tag = identifier & ASN_TAG_MASK;
454 481
455 if (tag == ASN_TAG_BER) 482 if (tag == ASN_TAG_BER)
456 tag = get_w (); 483 tag = get_w ();
457 484
458 if (tag == ASN_TAG_BER)
459 tag = get_w ();
460
461 if (constructed) 485 if (constructed)
462 { 486 {
463 UV len = get_length (); 487 UV len = get_length ();
464 UV seqend = (cur - buf) + len; 488 UV seqend = (cur - buf) + len;
465 AV *av = (AV *)sv_2mortal ((SV *)newAV ()); 489 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
466 490
467 while (cur < buf + seqend) 491 while (cur < buf + seqend)
468 av_push (av, decode_ber ()); 492 av_push (av, decode_ber ());
469 493
470 if (cur > buf + seqend) 494 if (cur > buf + seqend)
471 croak ("constructed type %02x overflow (%x %x)\n", identifier, cur - buf, seqend); 495 croak ("CONSTRUCTED type %02x length overflow (0x%x 0x%x)\n", identifier, (int)(cur - buf), (int)seqend);
472 496
473 res = newRV_inc ((SV *)av); 497 res = newRV_inc ((SV *)av);
474 } 498 }
475 else 499 else
476 switch (profile_lookup (cur_profile, klass, tag)) 500 switch (profile_lookup (cur_profile, klass, tag))
478 case BER_TYPE_NULL: 502 case BER_TYPE_NULL:
479 { 503 {
480 UV len = get_length (); 504 UV len = get_length ();
481 505
482 if (len) 506 if (len)
483 croak ("BER_TYPE_NULL value with non-zero length %d encountered", len); 507 croak ("BER_TYPE_NULL value with non-zero length %d encountered (X.690 8.8.2)", len);
484 508
485 res = &PL_sv_undef; 509 res = &PL_sv_undef;
486 } 510 }
487 break; 511 break;
488 512
489 case BER_TYPE_BOOL: 513 case BER_TYPE_BOOL:
490 { 514 {
491 UV len = get_length (); 515 UV len = get_length ();
492 516
493 if (len != 1) 517 if (len != 1)
494 croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered", len); 518 croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered (X.690 8.2.1)", len);
495 519
496 res = newSVcacheint (!!get_u8 ()); 520 res = newSVcacheint (!!get_u8 ());
497 } 521 }
498 break; 522 break;
499 523
521 case BER_TYPE_IPADDRESS: 545 case BER_TYPE_IPADDRESS:
522 { 546 {
523 UV len = get_length (); 547 UV len = get_length ();
524 548
525 if (len != 4) 549 if (len != 4)
526 croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered", len); 550 croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered (RFC 2578 7.1.5)", len);
527 551
528 U8 c1 = get_u8 (); 552 U8 c1 = get_u8 ();
529 U8 c2 = get_u8 (); 553 U8 c2 = get_u8 ();
530 U8 c3 = get_u8 (); 554 U8 c3 = get_u8 ();
531 U8 c4 = get_u8 (); 555 U8 c4 = get_u8 ();
541 case BER_TYPE_UCS4: 565 case BER_TYPE_UCS4:
542 res = decode_ucs (4); 566 res = decode_ucs (4);
543 break; 567 break;
544 568
545 case BER_TYPE_REAL: 569 case BER_TYPE_REAL:
570 error ("BER_TYPE_REAL not implemented");
571
546 case BER_TYPE_CROAK: 572 case BER_TYPE_CROAK:
573 croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag);
574
547 default: 575 default:
548 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 576 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
549 } 577 }
550 578
551 AV *av = newAV (); 579 AV *av = newAV ();
552 av_fill (av, BER_ARRAYSIZE - 1); 580 av_fill (av, BER_ARRAYSIZE - 1);
553 AvARRAY (av)[BER_CLASS ] = newSVcacheint (klass); 581 AvARRAY (av)[BER_CLASS] = newSVcacheint (klass);
554 AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); 582 AvARRAY (av)[BER_TAG ] = newSVcacheint (tag);
555 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (constructed ? 1 : 0); 583 AvARRAY (av)[BER_FLAGS] = newSVcacheint (constructed ? 1 : 0);
556 AvARRAY (av)[BER_DATA ] = res; 584 AvARRAY (av)[BER_DATA ] = res;
557 585
558 return newRV_noinc ((SV *)av); 586 return newRV_noinc ((SV *)av);
559} 587}
560 588
561///////////////////////////////////////////////////////////////////////////// 589/////////////////////////////////////////////////////////////////////////////
566strlen_sum (STRLEN l1, STRLEN l2) 594strlen_sum (STRLEN l1, STRLEN l2)
567{ 595{
568 size_t sum = l1 + l2; 596 size_t sum = l1 + l2;
569 597
570 if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) 598 if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum)
571 croak ("JSON::XS: string size overflow"); 599 croak ("Convert::BER::XS: string size overflow");
572 600
573 return sum; 601 return sum;
574} 602}
575 603
576static void 604static void
881{ 909{
882 AV *av = ber_tuple (tuple); 910 AV *av = ber_tuple (tuple);
883 911
884 int klass = SvIV (AvARRAY (av)[BER_CLASS]); 912 int klass = SvIV (AvARRAY (av)[BER_CLASS]);
885 int tag = SvIV (AvARRAY (av)[BER_TAG]); 913 int tag = SvIV (AvARRAY (av)[BER_TAG]);
886 int constructed = SvIV (AvARRAY (av)[BER_CONSTRUCTED]) ? ASN_CONSTRUCTED : 0; 914 int constructed = SvIV (AvARRAY (av)[BER_FLAGS]) & 1 ? ASN_CONSTRUCTED : 0;
887 SV *data = AvARRAY (av)[BER_DATA]; 915 SV *data = AvARRAY (av)[BER_DATA];
888 916
889 int identifier = (klass << ASN_CLASS_SHIFT) | constructed; 917 int identifier = (klass << ASN_CLASS_SHIFT) | constructed;
890 918
891 if (expect_false (tag >= ASN_TAG_BER)) 919 if (expect_false (tag >= ASN_TAG_BER))
902 // and adjust later 930 // and adjust later
903 need (1); 931 need (1);
904 STRLEN mark = len_fixup_mark (); 932 STRLEN mark = len_fixup_mark ();
905 933
906 if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV)) 934 if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV))
907 croak ("BER constructed data must be array-reference"); 935 croak ("BER CONSTRUCTED data must be array-reference");
908 936
909 AV *av = (AV *)SvRV (data); 937 AV *av = (AV *)SvRV (data);
910 int fill = AvFILL (av); 938 int fill = AvFILL (av);
911 939
912 if (expect_false (SvRMAGICAL (av))) 940 if (expect_false (SvRMAGICAL (av)))
913 croak ("BER constructed data must not be tied"); 941 croak ("BER CONSTRUCTED data must not be tied");
914 942
915 int i; 943 int i;
916 for (i = 0; i <= fill; ++i) 944 for (i = 0; i <= fill; ++i)
917 encode_ber (AvARRAY (av)[i]); 945 encode_ber (AvARRAY (av)[i]);
918 946
925 put_length (0); 953 put_length (0);
926 break; 954 break;
927 955
928 case BER_TYPE_BOOL: 956 case BER_TYPE_BOOL:
929 put_length (1); 957 put_length (1);
930 *cur++ = SvTRUE (data) ? 0xff : 0x00; 958 *cur++ = SvTRUE (data) ? 0xff : 0x00; // 0xff = DER/CER
931 break; 959 break;
932 960
933 case BER_TYPE_OID: 961 case BER_TYPE_OID:
934 encode_oid (data, 0); 962 encode_oid (data, 0);
935 break; 963 break;
973 case BER_TYPE_UCS4: 1001 case BER_TYPE_UCS4:
974 encode_ucs (data, 4); 1002 encode_ucs (data, 4);
975 break; 1003 break;
976 1004
977 case BER_TYPE_REAL: 1005 case BER_TYPE_REAL:
1006 croak ("BER_TYPE_REAL not implemented");
1007
978 case BER_TYPE_CROAK: 1008 case BER_TYPE_CROAK:
1009 croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag);
1010
979 default: 1011 default:
980 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 1012 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
981 } 1013 }
982 1014
983} 1015}
1037 const_iv (ASN_CONTEXT) 1069 const_iv (ASN_CONTEXT)
1038 const_iv (ASN_PRIVATE) 1070 const_iv (ASN_PRIVATE)
1039 1071
1040 const_iv (BER_CLASS) 1072 const_iv (BER_CLASS)
1041 const_iv (BER_TAG) 1073 const_iv (BER_TAG)
1042 const_iv (BER_CONSTRUCTED) 1074 const_iv (BER_FLAGS)
1043 const_iv (BER_DATA) 1075 const_iv (BER_DATA)
1044 1076
1045 const_iv (BER_TYPE_BYTES) 1077 const_iv (BER_TYPE_BYTES)
1046 const_iv (BER_TYPE_UTF8) 1078 const_iv (BER_TYPE_UTF8)
1047 const_iv (BER_TYPE_UCS2) 1079 const_iv (BER_TYPE_UCS2)
1055 const_iv (BER_TYPE_IPADDRESS) 1087 const_iv (BER_TYPE_IPADDRESS)
1056 const_iv (BER_TYPE_CROAK) 1088 const_iv (BER_TYPE_CROAK)
1057 1089
1058 const_iv (SNMP_IPADDRESS) 1090 const_iv (SNMP_IPADDRESS)
1059 const_iv (SNMP_COUNTER32) 1091 const_iv (SNMP_COUNTER32)
1092 const_iv (SNMP_GAUGE32)
1060 const_iv (SNMP_UNSIGNED32) 1093 const_iv (SNMP_UNSIGNED32)
1061 const_iv (SNMP_TIMETICKS) 1094 const_iv (SNMP_TIMETICKS)
1062 const_iv (SNMP_OPAQUE) 1095 const_iv (SNMP_OPAQUE)
1063 const_iv (SNMP_COUNTER64) 1096 const_iv (SNMP_COUNTER64)
1064 }; 1097 };
1065 1098
1066 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) 1099 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--)
1067 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); 1100 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv));
1068} 1101}
1069 1102
1070SV * 1103void
1071ber_decode (SV *ber, SV *profile = &PL_sv_undef) 1104ber_decode (SV *ber, SV *profile = &PL_sv_undef)
1105 ALIAS:
1106 ber_decode_prefix = 1
1072 CODE: 1107 PPCODE:
1073{ 1108{
1074 cur_profile = SvPROFILE (profile); 1109 cur_profile = SvPROFILE (profile);
1075 STRLEN len; 1110 STRLEN len;
1076 buf = (U8 *)SvPVbyte (ber, len); 1111 buf = (U8 *)SvPVbyte (ber, len);
1077 cur = buf; 1112 cur = buf;
1078 end = buf + len; 1113 end = buf + len;
1079 1114
1080 RETVAL = decode_ber (); 1115 SV *tuple = decode_ber ();
1116
1117 EXTEND (SP, 2);
1118 PUSHs (sv_2mortal (tuple));
1119
1120 if (ix)
1121 PUSHs (sv_2mortal (newSViv (cur - buf)));
1122 else if (cur != end)
1123 error ("trailing garbage after BER value");
1081} 1124}
1082 OUTPUT: RETVAL
1083 1125
1084void 1126void
1085ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *constructed = &PL_sv_undef, SV *data = &PL_sv_undef) 1127ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *flags = &PL_sv_undef, SV *data = &PL_sv_undef)
1086 PPCODE: 1128 PPCODE:
1087{ 1129{
1088 if (!SvOK (tuple)) 1130 if (!SvOK (tuple))
1089 XSRETURN_NO; 1131 XSRETURN_NO;
1090 1132
1092 croak ("ber_is: tuple must be BER tuple (array-ref)"); 1134 croak ("ber_is: tuple must be BER tuple (array-ref)");
1093 1135
1094 AV *av = (AV *)SvRV (tuple); 1136 AV *av = (AV *)SvRV (tuple);
1095 1137
1096 XPUSHs ( 1138 XPUSHs (
1097 (!SvOK (klass) || SvIV (AvARRAY (av)[BER_CLASS ]) == SvIV (klass)) 1139 (!SvOK (klass) || SvIV (AvARRAY (av)[BER_CLASS]) == SvIV (klass))
1098 && (!SvOK (tag) || SvIV (AvARRAY (av)[BER_TAG ]) == SvIV (tag)) 1140 && (!SvOK (tag) || SvIV (AvARRAY (av)[BER_TAG ]) == SvIV (tag))
1099 && (!SvOK (constructed) || !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) == !SvIV (constructed)) 1141 && (!SvOK (flags) || !SvIV (AvARRAY (av)[BER_FLAGS]) == !SvIV (flags))
1100 && (!SvOK (data) || sv_eq (AvARRAY (av)[BER_DATA ], data)) 1142 && (!SvOK (data) || sv_eq (AvARRAY (av)[BER_DATA ], data))
1101 ? &PL_sv_yes : &PL_sv_undef); 1143 ? &PL_sv_yes : &PL_sv_undef);
1102} 1144}
1103 1145
1104void 1146void
1105ber_is_seq (SV *tuple) 1147ber_is_seq (SV *tuple)
1109 XSRETURN_UNDEF; 1151 XSRETURN_UNDEF;
1110 1152
1111 AV *av = ber_tuple (tuple); 1153 AV *av = ber_tuple (tuple);
1112 1154
1113 XPUSHs ( 1155 XPUSHs (
1114 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL 1156 SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL
1115 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_SEQUENCE 1157 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_SEQUENCE
1116 && SvIV (AvARRAY (av)[BER_CONSTRUCTED]) 1158 && SvIV (AvARRAY (av)[BER_FLAGS])
1117 ? AvARRAY (av)[BER_DATA] : &PL_sv_undef); 1159 ? AvARRAY (av)[BER_DATA] : &PL_sv_undef);
1118} 1160}
1119 1161
1120void 1162void
1121ber_is_int (SV *tuple, SV *value = &PL_sv_undef) 1163ber_is_int (SV *tuple, SV *value = &PL_sv_undef)
1127 AV *av = ber_tuple (tuple); 1169 AV *av = ber_tuple (tuple);
1128 1170
1129 UV data = SvUV (AvARRAY (av)[BER_DATA]); 1171 UV data = SvUV (AvARRAY (av)[BER_DATA]);
1130 1172
1131 XPUSHs ( 1173 XPUSHs (
1132 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL 1174 SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL
1133 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_INTEGER 1175 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_INTEGER
1134 && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) 1176 && !SvIV (AvARRAY (av)[BER_FLAGS])
1135 && (!SvOK (value) || data == SvUV (value)) 1177 && (!SvOK (value) || data == SvUV (value))
1136 ? sv_2mortal (data ? newSVsv (AvARRAY (av)[BER_DATA]) : newSVpv ("0 but true", 0)) 1178 ? sv_2mortal (data ? newSVsv (AvARRAY (av)[BER_DATA]) : newSVpv ("0 but true", 0))
1137 : &PL_sv_undef); 1179 : &PL_sv_undef);
1138} 1180}
1139 1181
1145 XSRETURN_NO; 1187 XSRETURN_NO;
1146 1188
1147 AV *av = ber_tuple (tuple); 1189 AV *av = ber_tuple (tuple);
1148 1190
1149 XPUSHs ( 1191 XPUSHs (
1150 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL 1192 SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL
1151 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_OBJECT_IDENTIFIER 1193 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_OBJECT_IDENTIFIER
1152 && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) 1194 && !SvIV (AvARRAY (av)[BER_FLAGS])
1153 && (!SvOK (oid) || sv_eq (AvARRAY (av)[BER_DATA], oid)) 1195 && (!SvOK (oid) || sv_eq (AvARRAY (av)[BER_DATA], oid))
1154 ? newSVsv (AvARRAY (av)[BER_DATA]) : &PL_sv_undef); 1196 ? newSVsv (AvARRAY (av)[BER_DATA]) : &PL_sv_undef);
1155} 1197}
1156 1198
1157############################################################################# 1199#############################################################################
1175ber_int (SV *sv) 1217ber_int (SV *sv)
1176 CODE: 1218 CODE:
1177{ 1219{
1178 AV *av = newAV (); 1220 AV *av = newAV ();
1179 av_fill (av, BER_ARRAYSIZE - 1); 1221 av_fill (av, BER_ARRAYSIZE - 1);
1180 AvARRAY (av)[BER_CLASS ] = newSVcacheint (ASN_UNIVERSAL); 1222 AvARRAY (av)[BER_CLASS] = newSVcacheint (ASN_UNIVERSAL);
1181 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_INTEGER); 1223 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_INTEGER);
1182 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (0); 1224 AvARRAY (av)[BER_FLAGS] = newSVcacheint (0);
1183 AvARRAY (av)[BER_DATA ] = newSVsv (sv); 1225 AvARRAY (av)[BER_DATA ] = newSVsv (sv);
1184 RETVAL = newRV_noinc ((SV *)av); 1226 RETVAL = newRV_noinc ((SV *)av);
1185} 1227}
1186 OUTPUT: RETVAL 1228 OUTPUT: RETVAL
1187 1229
1188# TODO: not arrayref, but elements? 1230# TODO: not arrayref, but elements?
1190ber_seq (SV *arrayref) 1232ber_seq (SV *arrayref)
1191 CODE: 1233 CODE:
1192{ 1234{
1193 AV *av = newAV (); 1235 AV *av = newAV ();
1194 av_fill (av, BER_ARRAYSIZE - 1); 1236 av_fill (av, BER_ARRAYSIZE - 1);
1195 AvARRAY (av)[BER_CLASS ] = newSVcacheint (ASN_UNIVERSAL); 1237 AvARRAY (av)[BER_CLASS] = newSVcacheint (ASN_UNIVERSAL);
1196 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_SEQUENCE); 1238 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_SEQUENCE);
1197 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (1); 1239 AvARRAY (av)[BER_FLAGS] = newSVcacheint (1);
1198 AvARRAY (av)[BER_DATA ] = newSVsv (arrayref); 1240 AvARRAY (av)[BER_DATA ] = newSVsv (arrayref);
1199 RETVAL = newRV_noinc ((SV *)av); 1241 RETVAL = newRV_noinc ((SV *)av);
1200} 1242}
1201 OUTPUT: RETVAL 1243 OUTPUT: RETVAL
1202 1244
1203MODULE = Convert::BER::XS PACKAGE = Convert::BER::XS::Profile 1245MODULE = Convert::BER::XS PACKAGE = Convert::BER::XS::Profile

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines