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.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};
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;
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);
407 else
408 {
409 UV w1, w2;
410
404 else if (w < 2 * 40) 411 if (w < 2 * 40)
405 { 412 (w1 = w / 40), (w2 = w % 40);
413 else
414 (w1 = 2), (w2 = w - 2 * 40);
415
406 app = write_uv (app, (U8)w / 40); 416 app = write_uv (app, w1);
407 *app++ = '.'; 417 *app++ = '.';
408 app = write_uv (app, (U8)w % 40);
409 }
410 else
411 {
412 app = write_uv (app, 2); 418 app = write_uv (app, w2);
413 *app++ = '.';
414 app = write_uv (app, w - 2 * 40);
415 } 419 }
416 420
417 while (cur < end) 421 while (cur < end)
418 { 422 {
419 // we assume an oid component is never > 64 digits 423 // we assume an oid component is never > 64 digits
420 if (oid + sizeof (oid) - app < 64) 424 if (oid + sizeof (oid) - app < 64)
421 croak ("BER_TYPE_OID to long to decode"); 425 croak ("BER_TYPE_OID to long to decode");
422 426
423 w = get_w_nopad (); 427 w = get_w ();
424 *app++ = '.'; 428 *app++ = '.';
425 app = write_uv (app, w); 429 app = write_uv (app, w);
426 } 430 }
427 431
428 return newSVpvn (oid, app - oid); 432 return newSVpvn (oid, app - oid);
476 int tag = identifier & ASN_TAG_MASK; 480 int tag = identifier & ASN_TAG_MASK;
477 481
478 if (tag == ASN_TAG_BER) 482 if (tag == ASN_TAG_BER)
479 tag = get_w (); 483 tag = get_w ();
480 484
481 if (tag == ASN_TAG_BER)
482 tag = get_w ();
483
484 if (constructed) 485 if (constructed)
485 { 486 {
486 UV len = get_length (); 487 UV len = get_length ();
487 UV seqend = (cur - buf) + len; 488 UV seqend = (cur - buf) + len;
488 AV *av = (AV *)sv_2mortal ((SV *)newAV ()); 489 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
489 490
490 while (cur < buf + seqend) 491 while (cur < buf + seqend)
491 av_push (av, decode_ber ()); 492 av_push (av, decode_ber ());
492 493
493 if (cur > buf + seqend) 494 if (cur > buf + seqend)
494 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);
495 496
496 res = newRV_inc ((SV *)av); 497 res = newRV_inc ((SV *)av);
497 } 498 }
498 else 499 else
499 switch (profile_lookup (cur_profile, klass, tag)) 500 switch (profile_lookup (cur_profile, klass, tag))
501 case BER_TYPE_NULL: 502 case BER_TYPE_NULL:
502 { 503 {
503 UV len = get_length (); 504 UV len = get_length ();
504 505
505 if (len) 506 if (len)
506 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);
507 508
508 res = &PL_sv_undef; 509 res = &PL_sv_undef;
509 } 510 }
510 break; 511 break;
511 512
512 case BER_TYPE_BOOL: 513 case BER_TYPE_BOOL:
513 { 514 {
514 UV len = get_length (); 515 UV len = get_length ();
515 516
516 if (len != 1) 517 if (len != 1)
517 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);
518 519
519 res = newSVcacheint (!!get_u8 ()); 520 res = newSVcacheint (!!get_u8 ());
520 } 521 }
521 break; 522 break;
522 523
544 case BER_TYPE_IPADDRESS: 545 case BER_TYPE_IPADDRESS:
545 { 546 {
546 UV len = get_length (); 547 UV len = get_length ();
547 548
548 if (len != 4) 549 if (len != 4)
549 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);
550 551
551 U8 c1 = get_u8 (); 552 U8 c1 = get_u8 ();
552 U8 c2 = get_u8 (); 553 U8 c2 = get_u8 ();
553 U8 c3 = get_u8 (); 554 U8 c3 = get_u8 ();
554 U8 c4 = get_u8 (); 555 U8 c4 = get_u8 ();
564 case BER_TYPE_UCS4: 565 case BER_TYPE_UCS4:
565 res = decode_ucs (4); 566 res = decode_ucs (4);
566 break; 567 break;
567 568
568 case BER_TYPE_REAL: 569 case BER_TYPE_REAL:
570 error ("BER_TYPE_REAL not implemented");
571
569 case BER_TYPE_CROAK: 572 case BER_TYPE_CROAK:
573 croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag);
574
570 default: 575 default:
571 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 576 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
572 } 577 }
573 578
574 AV *av = newAV (); 579 AV *av = newAV ();
589strlen_sum (STRLEN l1, STRLEN l2) 594strlen_sum (STRLEN l1, STRLEN l2)
590{ 595{
591 size_t sum = l1 + l2; 596 size_t sum = l1 + l2;
592 597
593 if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) 598 if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum)
594 croak ("JSON::XS: string size overflow"); 599 croak ("Convert::BER::XS: string size overflow");
595 600
596 return sum; 601 return sum;
597} 602}
598 603
599static void 604static void
925 // and adjust later 930 // and adjust later
926 need (1); 931 need (1);
927 STRLEN mark = len_fixup_mark (); 932 STRLEN mark = len_fixup_mark ();
928 933
929 if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV)) 934 if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV))
930 croak ("BER constructed data must be array-reference"); 935 croak ("BER CONSTRUCTED data must be array-reference");
931 936
932 AV *av = (AV *)SvRV (data); 937 AV *av = (AV *)SvRV (data);
933 int fill = AvFILL (av); 938 int fill = AvFILL (av);
934 939
935 if (expect_false (SvRMAGICAL (av))) 940 if (expect_false (SvRMAGICAL (av)))
936 croak ("BER constructed data must not be tied"); 941 croak ("BER CONSTRUCTED data must not be tied");
937 942
938 int i; 943 int i;
939 for (i = 0; i <= fill; ++i) 944 for (i = 0; i <= fill; ++i)
940 encode_ber (AvARRAY (av)[i]); 945 encode_ber (AvARRAY (av)[i]);
941 946
996 case BER_TYPE_UCS4: 1001 case BER_TYPE_UCS4:
997 encode_ucs (data, 4); 1002 encode_ucs (data, 4);
998 break; 1003 break;
999 1004
1000 case BER_TYPE_REAL: 1005 case BER_TYPE_REAL:
1006 croak ("BER_TYPE_REAL not implemented");
1007
1001 case BER_TYPE_CROAK: 1008 case BER_TYPE_CROAK:
1009 croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag);
1010
1002 default: 1011 default:
1003 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 1012 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
1004 } 1013 }
1005 1014
1006} 1015}
1078 const_iv (BER_TYPE_IPADDRESS) 1087 const_iv (BER_TYPE_IPADDRESS)
1079 const_iv (BER_TYPE_CROAK) 1088 const_iv (BER_TYPE_CROAK)
1080 1089
1081 const_iv (SNMP_IPADDRESS) 1090 const_iv (SNMP_IPADDRESS)
1082 const_iv (SNMP_COUNTER32) 1091 const_iv (SNMP_COUNTER32)
1092 const_iv (SNMP_GAUGE32)
1083 const_iv (SNMP_UNSIGNED32) 1093 const_iv (SNMP_UNSIGNED32)
1084 const_iv (SNMP_TIMETICKS) 1094 const_iv (SNMP_TIMETICKS)
1085 const_iv (SNMP_OPAQUE) 1095 const_iv (SNMP_OPAQUE)
1086 const_iv (SNMP_COUNTER64) 1096 const_iv (SNMP_COUNTER64)
1087 }; 1097 };
1088 1098
1089 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--)
1090 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); 1100 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv));
1091} 1101}
1092 1102
1093SV * 1103void
1094ber_decode (SV *ber, SV *profile = &PL_sv_undef) 1104ber_decode (SV *ber, SV *profile = &PL_sv_undef)
1105 ALIAS:
1106 ber_decode_prefix = 1
1095 CODE: 1107 PPCODE:
1096{ 1108{
1097 cur_profile = SvPROFILE (profile); 1109 cur_profile = SvPROFILE (profile);
1098 STRLEN len; 1110 STRLEN len;
1099 buf = (U8 *)SvPVbyte (ber, len); 1111 buf = (U8 *)SvPVbyte (ber, len);
1100 cur = buf; 1112 cur = buf;
1101 end = buf + len; 1113 end = buf + len;
1102 1114
1103 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");
1104} 1124}
1105 OUTPUT: RETVAL
1106 1125
1107void 1126void
1108ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *flags = &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)
1109 PPCODE: 1128 PPCODE:
1110{ 1129{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines