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.19 by root, Sat Apr 20 16:34:34 2019 UTC vs.
Revision 1.25 by root, Sun Apr 21 10:40:30 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};
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;
265 }
266}
267 272
268// get_w, but disallow padding 273 c = get_u8 ();
269static UV 274 }
270get_w_nopad (void)
271{
272 U8 first = get_u8 ();
273
274 if (first == 0x80)
275 error ("illegal BER padding");
276
277 --cur;
278
279 return get_w ();
280} 275}
281 276
282static UV 277static UV
283get_length (void) 278get_length (void)
284{ 279{
290 res = 0; 285 res = 0;
291 286
292 switch (cnt) 287 switch (cnt)
293 { 288 {
294 case 0: 289 case 0:
295 error ("indefinite ASN.1 lengths not supported"); 290 error ("indefinite BER value lengths not supported");
296 return 0;
297 291
298 //case 0x80: // indefinite length 292 case 0x7f:
293 error ("BER reserved value in length (X.690 8.1.3.5)");
299 294
300 //case 0xff: reserved
301 default: 295 default:
302 error ("ASN.1 length too long"); 296 error ("BER value length too long (must fit into UV)");
303 return 0;
304 297
298#if UVSIZE > 4
305 case 8: res = (res << 8) | get_u8 (); 299 case 8: res = (res << 8) | get_u8 ();
306 case 7: res = (res << 8) | get_u8 (); 300 case 7: res = (res << 8) | get_u8 ();
307 case 6: res = (res << 8) | get_u8 (); 301 case 6: res = (res << 8) | get_u8 ();
308 case 5: res = (res << 8) | get_u8 (); 302 case 5: res = (res << 8) | get_u8 ();
303#endif
309 case 4: res = (res << 8) | get_u8 (); 304 case 4: res = (res << 8) | get_u8 ();
310 case 3: res = (res << 8) | get_u8 (); 305 case 3: res = (res << 8) | get_u8 ();
311 case 2: res = (res << 8) | get_u8 (); 306 case 2: res = (res << 8) | get_u8 ();
312 case 1: res = (res << 8) | get_u8 (); 307 case 1: res = (res << 8) | get_u8 ();
313 } 308 }
320decode_int (void) 315decode_int (void)
321{ 316{
322 UV len = get_length (); 317 UV len = get_length ();
323 318
324 if (!len) 319 if (!len)
325 { 320 error ("invalid BER_TYPE_INT length zero (X.690 8.3.1)");
326 error ("invalid integer length equal to zero");
327 return 0;
328 }
329 321
330 U8 *data = get_n (len); 322 U8 *data = get_n (len);
331 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
332 int negative = data [0] & 0x80; 332 int negative = data [0] & 0x80;
333 333
334 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");
335 338
336 do 339 do
337 val = (val << 8) | *data++; 340 val = (val << 8) | *data++;
338 while (--len); 341 while (--len);
339 342
387{ 390{
388 UV len = get_length (); 391 UV len = get_length ();
389 392
390 if (len <= 0) 393 if (len <= 0)
391 { 394 {
392 error ("OBJECT IDENTIFIER length equal to zero"); 395 error ("BER_TYPE_OID length must not be zero");
393 return &PL_sv_undef; 396 return &PL_sv_undef;
394 } 397 }
395 398
396 U8 *end = cur + len; 399 U8 *end = cur + len;
397 UV w = get_w_nopad (); 400 UV w = get_w ();
398 401
399 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
400 char *app = oid; 403 char *app = oid;
401 404
402 if (relative) 405 if (relative)
403 app = write_uv (app, w); 406 app = write_uv (app, w);
404 else if (w < 2 * 40) 407 else if (w < 2 * 40)
418 { 421 {
419 // we assume an oid component is never > 64 digits 422 // we assume an oid component is never > 64 digits
420 if (oid + sizeof (oid) - app < 64) 423 if (oid + sizeof (oid) - app < 64)
421 croak ("BER_TYPE_OID to long to decode"); 424 croak ("BER_TYPE_OID to long to decode");
422 425
423 w = get_w_nopad (); 426 w = get_w ();
424 *app++ = '.'; 427 *app++ = '.';
425 app = write_uv (app, w); 428 app = write_uv (app, w);
426 } 429 }
427 430
428 return newSVpvn (oid, app - oid); 431 return newSVpvn (oid, app - oid);
476 int tag = identifier & ASN_TAG_MASK; 479 int tag = identifier & ASN_TAG_MASK;
477 480
478 if (tag == ASN_TAG_BER) 481 if (tag == ASN_TAG_BER)
479 tag = get_w (); 482 tag = get_w ();
480 483
481 if (tag == ASN_TAG_BER)
482 tag = get_w ();
483
484 if (constructed) 484 if (constructed)
485 { 485 {
486 UV len = get_length (); 486 UV len = get_length ();
487 UV seqend = (cur - buf) + len; 487 UV seqend = (cur - buf) + len;
488 AV *av = (AV *)sv_2mortal ((SV *)newAV ()); 488 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
489 489
490 while (cur < buf + seqend) 490 while (cur < buf + seqend)
491 av_push (av, decode_ber ()); 491 av_push (av, decode_ber ());
492 492
493 if (cur > buf + seqend) 493 if (cur > buf + seqend)
494 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);
495 495
496 res = newRV_inc ((SV *)av); 496 res = newRV_inc ((SV *)av);
497 } 497 }
498 else 498 else
499 switch (profile_lookup (cur_profile, klass, tag)) 499 switch (profile_lookup (cur_profile, klass, tag))
501 case BER_TYPE_NULL: 501 case BER_TYPE_NULL:
502 { 502 {
503 UV len = get_length (); 503 UV len = get_length ();
504 504
505 if (len) 505 if (len)
506 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);
507 507
508 res = &PL_sv_undef; 508 res = &PL_sv_undef;
509 } 509 }
510 break; 510 break;
511 511
512 case BER_TYPE_BOOL: 512 case BER_TYPE_BOOL:
513 { 513 {
514 UV len = get_length (); 514 UV len = get_length ();
515 515
516 if (len != 1) 516 if (len != 1)
517 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);
518 518
519 res = newSVcacheint (!!get_u8 ()); 519 res = newSVcacheint (!!get_u8 ());
520 } 520 }
521 break; 521 break;
522 522
544 case BER_TYPE_IPADDRESS: 544 case BER_TYPE_IPADDRESS:
545 { 545 {
546 UV len = get_length (); 546 UV len = get_length ();
547 547
548 if (len != 4) 548 if (len != 4)
549 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);
550 550
551 U8 c1 = get_u8 (); 551 U8 c1 = get_u8 ();
552 U8 c2 = get_u8 (); 552 U8 c2 = get_u8 ();
553 U8 c3 = get_u8 (); 553 U8 c3 = get_u8 ();
554 U8 c4 = get_u8 (); 554 U8 c4 = get_u8 ();
564 case BER_TYPE_UCS4: 564 case BER_TYPE_UCS4:
565 res = decode_ucs (4); 565 res = decode_ucs (4);
566 break; 566 break;
567 567
568 case BER_TYPE_REAL: 568 case BER_TYPE_REAL:
569 error ("BER_TYPE_REAL not implemented");
570
569 case BER_TYPE_CROAK: 571 case BER_TYPE_CROAK:
572 croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag);
573
570 default: 574 default:
571 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 575 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
572 } 576 }
573 577
574 AV *av = newAV (); 578 AV *av = newAV ();
589strlen_sum (STRLEN l1, STRLEN l2) 593strlen_sum (STRLEN l1, STRLEN l2)
590{ 594{
591 size_t sum = l1 + l2; 595 size_t sum = l1 + l2;
592 596
593 if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) 597 if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum)
594 croak ("JSON::XS: string size overflow"); 598 croak ("Convert::BER::XS: string size overflow");
595 599
596 return sum; 600 return sum;
597} 601}
598 602
599static void 603static void
925 // and adjust later 929 // and adjust later
926 need (1); 930 need (1);
927 STRLEN mark = len_fixup_mark (); 931 STRLEN mark = len_fixup_mark ();
928 932
929 if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV)) 933 if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV))
930 croak ("BER constructed data must be array-reference"); 934 croak ("BER CONSTRUCTED data must be array-reference");
931 935
932 AV *av = (AV *)SvRV (data); 936 AV *av = (AV *)SvRV (data);
933 int fill = AvFILL (av); 937 int fill = AvFILL (av);
934 938
935 if (expect_false (SvRMAGICAL (av))) 939 if (expect_false (SvRMAGICAL (av)))
936 croak ("BER constructed data must not be tied"); 940 croak ("BER CONSTRUCTED data must not be tied");
937 941
938 int i; 942 int i;
939 for (i = 0; i <= fill; ++i) 943 for (i = 0; i <= fill; ++i)
940 encode_ber (AvARRAY (av)[i]); 944 encode_ber (AvARRAY (av)[i]);
941 945
996 case BER_TYPE_UCS4: 1000 case BER_TYPE_UCS4:
997 encode_ucs (data, 4); 1001 encode_ucs (data, 4);
998 break; 1002 break;
999 1003
1000 case BER_TYPE_REAL: 1004 case BER_TYPE_REAL:
1005 croak ("BER_TYPE_REAL not implemented");
1006
1001 case BER_TYPE_CROAK: 1007 case BER_TYPE_CROAK:
1008 croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag);
1009
1002 default: 1010 default:
1003 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 1011 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
1004 } 1012 }
1005 1013
1006} 1014}
1078 const_iv (BER_TYPE_IPADDRESS) 1086 const_iv (BER_TYPE_IPADDRESS)
1079 const_iv (BER_TYPE_CROAK) 1087 const_iv (BER_TYPE_CROAK)
1080 1088
1081 const_iv (SNMP_IPADDRESS) 1089 const_iv (SNMP_IPADDRESS)
1082 const_iv (SNMP_COUNTER32) 1090 const_iv (SNMP_COUNTER32)
1091 const_iv (SNMP_GAUGE32)
1083 const_iv (SNMP_UNSIGNED32) 1092 const_iv (SNMP_UNSIGNED32)
1084 const_iv (SNMP_TIMETICKS) 1093 const_iv (SNMP_TIMETICKS)
1085 const_iv (SNMP_OPAQUE) 1094 const_iv (SNMP_OPAQUE)
1086 const_iv (SNMP_COUNTER64) 1095 const_iv (SNMP_COUNTER64)
1087 }; 1096 };
1088 1097
1089 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--)
1090 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); 1099 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv));
1091} 1100}
1092 1101
1093SV * 1102void
1094ber_decode (SV *ber, SV *profile = &PL_sv_undef) 1103ber_decode (SV *ber, SV *profile = &PL_sv_undef)
1104 ALIAS:
1105 ber_decode_prefix = 1
1095 CODE: 1106 PPCODE:
1096{ 1107{
1097 cur_profile = SvPROFILE (profile); 1108 cur_profile = SvPROFILE (profile);
1098 STRLEN len; 1109 STRLEN len;
1099 buf = (U8 *)SvPVbyte (ber, len); 1110 buf = (U8 *)SvPVbyte (ber, len);
1100 cur = buf; 1111 cur = buf;
1101 end = buf + len; 1112 end = buf + len;
1102 1113
1103 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");
1104} 1123}
1105 OUTPUT: RETVAL
1106 1124
1107void 1125void
1108ber_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)
1109 PPCODE: 1127 PPCODE:
1110{ 1128{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines