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.18 by root, Sat Apr 20 16:12:53 2019 UTC vs.
Revision 1.26 by root, Sun Apr 21 10:42:22 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};
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;
283 291
284 //case 0x80: // indefinite length 292 case 0x7f:
293 error ("BER reserved value in length (X.690 8.1.3.5)");
285 294
286 //case 0xff: reserved
287 default: 295 default:
288 error ("ASN.1 length too long"); 296 error ("BER value length too long (must fit into UV)");
289 return 0;
290 297
298#if UVSIZE > 4
291 case 8: res = (res << 8) | get_u8 (); 299 case 8: res = (res << 8) | get_u8 ();
292 case 7: res = (res << 8) | get_u8 (); 300 case 7: res = (res << 8) | get_u8 ();
293 case 6: res = (res << 8) | get_u8 (); 301 case 6: res = (res << 8) | get_u8 ();
294 case 5: res = (res << 8) | get_u8 (); 302 case 5: res = (res << 8) | get_u8 ();
303#endif
295 case 4: res = (res << 8) | get_u8 (); 304 case 4: res = (res << 8) | get_u8 ();
296 case 3: res = (res << 8) | get_u8 (); 305 case 3: res = (res << 8) | get_u8 ();
297 case 2: res = (res << 8) | get_u8 (); 306 case 2: res = (res << 8) | get_u8 ();
298 case 1: res = (res << 8) | get_u8 (); 307 case 1: res = (res << 8) | get_u8 ();
299 } 308 }
306decode_int (void) 315decode_int (void)
307{ 316{
308 UV len = get_length (); 317 UV len = get_length ();
309 318
310 if (!len) 319 if (!len)
311 { 320 error ("invalid BER_TYPE_INT length zero (X.690 8.3.1)");
312 error ("invalid integer length equal to zero");
313 return 0;
314 }
315 321
316 U8 *data = get_n (len); 322 U8 *data = get_n (len);
317 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
318 int negative = data [0] & 0x80; 332 int negative = data [0] & 0x80;
319 333
320 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");
321 338
322 do 339 do
323 val = (val << 8) | *data++; 340 val = (val << 8) | *data++;
324 while (--len); 341 while (--len);
325 342
373{ 390{
374 UV len = get_length (); 391 UV len = get_length ();
375 392
376 if (len <= 0) 393 if (len <= 0)
377 { 394 {
378 error ("OBJECT IDENTIFIER length equal to zero"); 395 error ("BER_TYPE_OID length must not be zero");
379 return &PL_sv_undef; 396 return &PL_sv_undef;
380 } 397 }
381 398
382 U8 *end = cur + len; 399 U8 *end = cur + len;
383 UV w = get_w (); 400 UV w = get_w ();
384 401
385 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
386 char *app = oid; 403 char *app = oid;
387 404
388 if (relative) 405 if (relative)
389 app = write_uv (app, w); 406 app = write_uv (app, w);
390 else if (w < 2 * 40) 407 else if (w < 2 * 40)
462 int tag = identifier & ASN_TAG_MASK; 479 int tag = identifier & ASN_TAG_MASK;
463 480
464 if (tag == ASN_TAG_BER) 481 if (tag == ASN_TAG_BER)
465 tag = get_w (); 482 tag = get_w ();
466 483
467 if (tag == ASN_TAG_BER)
468 tag = get_w ();
469
470 if (constructed) 484 if (constructed)
471 { 485 {
472 UV len = get_length (); 486 UV len = get_length ();
473 UV seqend = (cur - buf) + len; 487 UV seqend = (cur - buf) + len;
474 AV *av = (AV *)sv_2mortal ((SV *)newAV ()); 488 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
475 489
476 while (cur < buf + seqend) 490 while (cur < buf + seqend)
477 av_push (av, decode_ber ()); 491 av_push (av, decode_ber ());
478 492
479 if (cur > buf + seqend) 493 if (cur > buf + seqend)
480 croak ("constructed type %02x overflow (%x %x)\n", identifier, cur - buf, seqend); 494 croak ("CONSTRUCTED type %02x length overflow (0x%x 0x%x)\n", identifier, (int)(cur - buf), (int)seqend);
481 495
482 res = newRV_inc ((SV *)av); 496 res = newRV_inc ((SV *)av);
483 } 497 }
484 else 498 else
485 switch (profile_lookup (cur_profile, klass, tag)) 499 switch (profile_lookup (cur_profile, klass, tag))
487 case BER_TYPE_NULL: 501 case BER_TYPE_NULL:
488 { 502 {
489 UV len = get_length (); 503 UV len = get_length ();
490 504
491 if (len) 505 if (len)
492 croak ("BER_TYPE_NULL value with non-zero length %d encountered", len); 506 croak ("BER_TYPE_NULL value with non-zero length %d encountered (X.690 8.8.2)", len);
493 507
494 res = &PL_sv_undef; 508 res = &PL_sv_undef;
495 } 509 }
496 break; 510 break;
497 511
498 case BER_TYPE_BOOL: 512 case BER_TYPE_BOOL:
499 { 513 {
500 UV len = get_length (); 514 UV len = get_length ();
501 515
502 if (len != 1) 516 if (len != 1)
503 croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered", len); 517 croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered (X.690 8.2.1)", len);
504 518
505 res = newSVcacheint (!!get_u8 ()); 519 res = newSVcacheint (!!get_u8 ());
506 } 520 }
507 break; 521 break;
508 522
530 case BER_TYPE_IPADDRESS: 544 case BER_TYPE_IPADDRESS:
531 { 545 {
532 UV len = get_length (); 546 UV len = get_length ();
533 547
534 if (len != 4) 548 if (len != 4)
535 croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered", len); 549 croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered (RFC 2578 7.1.5)", len);
536 550
537 U8 c1 = get_u8 (); 551 U8 c1 = get_u8 ();
538 U8 c2 = get_u8 (); 552 U8 c2 = get_u8 ();
539 U8 c3 = get_u8 (); 553 U8 c3 = get_u8 ();
540 U8 c4 = get_u8 (); 554 U8 c4 = get_u8 ();
550 case BER_TYPE_UCS4: 564 case BER_TYPE_UCS4:
551 res = decode_ucs (4); 565 res = decode_ucs (4);
552 break; 566 break;
553 567
554 case BER_TYPE_REAL: 568 case BER_TYPE_REAL:
569 error ("BER_TYPE_REAL not implemented");
570
555 case BER_TYPE_CROAK: 571 case BER_TYPE_CROAK:
572 croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag);
573
556 default: 574 default:
557 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 575 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
558 } 576 }
559 577
560 AV *av = newAV (); 578 AV *av = newAV ();
575strlen_sum (STRLEN l1, STRLEN l2) 593strlen_sum (STRLEN l1, STRLEN l2)
576{ 594{
577 size_t sum = l1 + l2; 595 size_t sum = l1 + l2;
578 596
579 if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) 597 if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum)
580 croak ("JSON::XS: string size overflow"); 598 croak ("Convert::BER::XS: string size overflow");
581 599
582 return sum; 600 return sum;
583} 601}
584 602
585static void 603static void
911 // and adjust later 929 // and adjust later
912 need (1); 930 need (1);
913 STRLEN mark = len_fixup_mark (); 931 STRLEN mark = len_fixup_mark ();
914 932
915 if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV)) 933 if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV))
916 croak ("BER constructed data must be array-reference"); 934 croak ("BER CONSTRUCTED data must be array-reference");
917 935
918 AV *av = (AV *)SvRV (data); 936 AV *av = (AV *)SvRV (data);
919 int fill = AvFILL (av); 937 int fill = AvFILL (av);
920 938
921 if (expect_false (SvRMAGICAL (av))) 939 if (expect_false (SvRMAGICAL (av)))
922 croak ("BER constructed data must not be tied"); 940 croak ("BER CONSTRUCTED data must not be tied");
923 941
924 int i; 942 int i;
925 for (i = 0; i <= fill; ++i) 943 for (i = 0; i <= fill; ++i)
926 encode_ber (AvARRAY (av)[i]); 944 encode_ber (AvARRAY (av)[i]);
927 945
982 case BER_TYPE_UCS4: 1000 case BER_TYPE_UCS4:
983 encode_ucs (data, 4); 1001 encode_ucs (data, 4);
984 break; 1002 break;
985 1003
986 case BER_TYPE_REAL: 1004 case BER_TYPE_REAL:
1005 croak ("BER_TYPE_REAL not implemented");
1006
987 case BER_TYPE_CROAK: 1007 case BER_TYPE_CROAK:
1008 croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag);
1009
988 default: 1010 default:
989 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 1011 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
990 } 1012 }
991 1013
992} 1014}
1064 const_iv (BER_TYPE_IPADDRESS) 1086 const_iv (BER_TYPE_IPADDRESS)
1065 const_iv (BER_TYPE_CROAK) 1087 const_iv (BER_TYPE_CROAK)
1066 1088
1067 const_iv (SNMP_IPADDRESS) 1089 const_iv (SNMP_IPADDRESS)
1068 const_iv (SNMP_COUNTER32) 1090 const_iv (SNMP_COUNTER32)
1091 const_iv (SNMP_GAUGE32)
1069 const_iv (SNMP_UNSIGNED32) 1092 const_iv (SNMP_UNSIGNED32)
1070 const_iv (SNMP_TIMETICKS) 1093 const_iv (SNMP_TIMETICKS)
1071 const_iv (SNMP_OPAQUE) 1094 const_iv (SNMP_OPAQUE)
1072 const_iv (SNMP_COUNTER64) 1095 const_iv (SNMP_COUNTER64)
1073 }; 1096 };
1074 1097
1075 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) 1098 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--)
1076 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); 1099 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv));
1077} 1100}
1078 1101
1079SV * 1102void
1080ber_decode (SV *ber, SV *profile = &PL_sv_undef) 1103ber_decode (SV *ber, SV *profile = &PL_sv_undef)
1104 ALIAS:
1105 ber_decode_prefix = 1
1081 CODE: 1106 PPCODE:
1082{ 1107{
1083 cur_profile = SvPROFILE (profile); 1108 cur_profile = SvPROFILE (profile);
1084 STRLEN len; 1109 STRLEN len;
1085 buf = (U8 *)SvPVbyte (ber, len); 1110 buf = (U8 *)SvPVbyte (ber, len);
1086 cur = buf; 1111 cur = buf;
1087 end = buf + len; 1112 end = buf + len;
1088 1113
1089 RETVAL = decode_ber (); 1114 SV *tuple = decode_ber ();
1115
1116 EXTEND (SP, 2);
1117 PUSHs (sv_2mortal (tuple));
1118
1119 if (ix)
1120 PUSHs (sv_2mortal (newSViv (cur - buf)));
1121 else if (cur != end)
1122 error ("trailing garbage after BER value");
1090} 1123}
1091 OUTPUT: RETVAL
1092 1124
1093void 1125void
1094ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *flags = &PL_sv_undef, SV *data = &PL_sv_undef) 1126ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *flags = &PL_sv_undef, SV *data = &PL_sv_undef)
1095 PPCODE: 1127 PPCODE:
1096{ 1128{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines