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.21 by root, Sat Apr 20 17:23:21 2019 UTC vs.
Revision 1.29 by root, Tue Apr 23 19:44:12 2019 UTC

141{ 141{
142 if (!SvOK (profile)) 142 if (!SvOK (profile))
143 return default_profile; 143 return default_profile;
144 144
145 if (!SvROK (profile)) 145 if (!SvROK (profile))
146 croak ("invalid profile"); 146 croak ("Convert::BER::XS::Profile expected");
147 147
148 profile = SvRV (profile); 148 profile = SvRV (profile);
149 149
150 if (SvSTASH (profile) != profile_stash) 150 if (SvSTASH (profile) != profile_stash)
151 croak ("invalid profile object"); 151 croak ("Convert::BER::XS::Profile expected");
152 152
153 return (void *)profile; 153 return (void *)profile;
154} 154}
155 155
156static int 156static int
228} 228}
229 229
230// get_* functions fetch something from the buffer 230// get_* functions fetch something from the buffer
231// decode_* functions use get_* fun ctions to decode ber values 231// decode_* functions use get_* fun ctions to decode ber values
232 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
233// get n octets 243// get n octets
234static U8 * 244static U8 *
235get_n (UV count) 245get_n (UV count)
236{ 246{
237 want (count); 247 want (count);
238 U8 *res = cur; 248 U8 *res = cur;
239 cur += count; 249 cur += count;
240 return res; 250 return res;
241} 251}
242 252
243// get single octet
244static U8
245get_u8 (void)
246{
247 if (cur == end)
248 error ("unexpected end of message buffer");
249
250 return *cur++;
251}
252
253// get ber-encoded integer (i.e. pack "w") 253// get ber-encoded integer (i.e. pack "w")
254static UV 254static UV
255get_w (void) 255get_w (void)
256{ 256{
257 UV res = 0; 257 UV res = 0;
260 if (expect_false (c == 0x80)) 260 if (expect_false (c == 0x80))
261 error ("illegal BER padding (X.690 8.1.2.4.2, 8.19.2)"); 261 error ("illegal BER padding (X.690 8.1.2.4.2, 8.19.2)");
262 262
263 for (;;) 263 for (;;)
264 { 264 {
265 if (expect_false (res >> UVSIZE * 8 - 7))
266 error ("BER variable length integer overflow");
267
265 res = (res << 7) | (c & 0x7f); 268 res = (res << 7) | (c & 0x7f);
266 269
267 if (!(c & 0x80)) 270 if (expect_true (!(c & 0x80)))
268 return res; 271 return res;
269 272
270 c = get_u8 (); 273 c = get_u8 ();
271 } 274 }
272} 275}
274static UV 277static UV
275get_length (void) 278get_length (void)
276{ 279{
277 UV res = get_u8 (); 280 UV res = get_u8 ();
278 281
279 if (res & 0x80) 282 if (expect_false (res & 0x80))
280 { 283 {
281 int cnt = res & 0x7f; 284 U8 cnt = res & 0x7f;
285
286 // this genewrates quite ugly code, but the overhead
287 // of copying the bytes for these lengths is probably so high
288 // that a slightly inefficient get_length won't matter.
289
290 if (expect_false (cnt == 0))
291 error ("indefinite BER value lengths not supported");
292
293 if (expect_false (cnt > UVSIZE))
294 error ("BER value length too long (must fit into UV) or BER reserved value in length (X.690 8.1.3.5)");
295
296 want (cnt);
297
282 res = 0; 298 res = 0;
283 299 do
284 switch (cnt) 300 res = (res << 8) | *cur++;
285 { 301 while (--cnt);
286 case 0:
287 error ("indefinite ASN.1 lengths not supported");
288
289 case 0x7f:
290 error ("ASN.1 reserved value in length (X.690 8.1.3.5)");
291
292 default:
293 error ("ASN.1 length too long (only up to 2**64 octets supported)");
294
295 case 8: res = (res << 8) | get_u8 ();
296 case 7: res = (res << 8) | get_u8 ();
297 case 6: res = (res << 8) | get_u8 ();
298 case 5: res = (res << 8) | get_u8 ();
299 case 4: res = (res << 8) | get_u8 ();
300 case 3: res = (res << 8) | get_u8 ();
301 case 2: res = (res << 8) | get_u8 ();
302 case 1: res = (res << 8) | get_u8 ();
303 }
304 } 302 }
305 303
306 return res; 304 return res;
307} 305}
308 306
310decode_int (void) 308decode_int (void)
311{ 309{
312 UV len = get_length (); 310 UV len = get_length ();
313 311
314 if (!len) 312 if (!len)
315 error ("invalid integer length equal to zero (X.690 8.3.1)"); 313 error ("invalid BER_TYPE_INT length zero (X.690 8.3.1)");
316 314
317 U8 *data = get_n (len); 315 U8 *data = get_n (len);
318 316
319 if (expect_false (len > 1)) 317 if (expect_false (len > 1))
320 { 318 {
321 U16 mask = (data [0] << 8) | data [1] & 0xff80; 319 U16 mask = (data [0] << 8) | data [1] & 0xff80;
322 320
323 if (expect_false (mask == 0xff80 || mask == 0x0000)) 321 if (expect_false (mask == 0xff80 || mask == 0x0000))
324 error ("illegal padding in integer (X.690 8.3.2)"); 322 error ("illegal padding in BER_TYPE_INT (X.690 8.3.2)");
325 } 323 }
326 324
327 int negative = data [0] & 0x80; 325 int negative = data [0] & 0x80;
328 326
329 UV val = negative ? -1 : 0; // copy signbit to all bits 327 UV val = negative ? -1 : 0; // copy signbit to all bits
328
329 if (len > UVSIZE + (!negative && !*data))
330 error ("BER_TYPE_INT overflow");
330 331
331 do 332 do
332 val = (val << 8) | *data++; 333 val = (val << 8) | *data++;
333 while (--len); 334 while (--len);
334 335
382{ 383{
383 UV len = get_length (); 384 UV len = get_length ();
384 385
385 if (len <= 0) 386 if (len <= 0)
386 { 387 {
387 error ("OBJECT IDENTIFIER length equal to zero"); 388 error ("BER_TYPE_OID length must not be zero");
388 return &PL_sv_undef; 389 return &PL_sv_undef;
389 } 390 }
390 391
391 U8 *end = cur + len; 392 U8 *end = cur + len;
392 UV w = get_w (); 393 UV w = get_w ();
393 394
394 static char oid[MAX_OID_STRLEN]; // static, becaueds too large for stack 395 static char oid[MAX_OID_STRLEN]; // static, because too large for stack
395 char *app = oid; 396 char *app = oid;
396 397
397 if (relative) 398 if (relative)
398 app = write_uv (app, w); 399 app = write_uv (app, w);
400 else
401 {
402 UV w1, w2;
403
399 else if (w < 2 * 40) 404 if (w < 2 * 40)
400 { 405 (w1 = w / 40), (w2 = w % 40);
406 else
407 (w1 = 2), (w2 = w - 2 * 40);
408
401 app = write_uv (app, (U8)w / 40); 409 app = write_uv (app, w1);
402 *app++ = '.'; 410 *app++ = '.';
403 app = write_uv (app, (U8)w % 40);
404 }
405 else
406 {
407 app = write_uv (app, 2); 411 app = write_uv (app, w2);
408 *app++ = '.';
409 app = write_uv (app, w - 2 * 40);
410 } 412 }
411 413
412 while (cur < end) 414 while (cur < end)
413 { 415 {
414 // we assume an oid component is never > 64 digits 416 // we assume an oid component is never > 64 digits
481 483
482 while (cur < buf + seqend) 484 while (cur < buf + seqend)
483 av_push (av, decode_ber ()); 485 av_push (av, decode_ber ());
484 486
485 if (cur > buf + seqend) 487 if (cur > buf + seqend)
486 croak ("constructed type %02x length overflow (0x%x 0x%x)\n", identifier, (int)(cur - buf), (int)seqend); 488 croak ("CONSTRUCTED type %02x length overflow (0x%x 0x%x)\n", identifier, (int)(cur - buf), (int)seqend);
487 489
488 res = newRV_inc ((SV *)av); 490 res = newRV_inc ((SV *)av);
489 } 491 }
490 else 492 else
491 switch (profile_lookup (cur_profile, klass, tag)) 493 switch (profile_lookup (cur_profile, klass, tag))
538 UV len = get_length (); 540 UV len = get_length ();
539 541
540 if (len != 4) 542 if (len != 4)
541 croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered (RFC 2578 7.1.5)", len); 543 croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered (RFC 2578 7.1.5)", len);
542 544
543 U8 c1 = get_u8 (); 545 U8 *data = get_n (4);
544 U8 c2 = get_u8 (); 546 res = newSVpvf ("%d.%d.%d.%d", data [0], data [1], data [2], data [3]);
545 U8 c3 = get_u8 ();
546 U8 c4 = get_u8 ();
547
548 res = newSVpvf ("%d.%d.%d.%d", c1, c2, c3, c4);
549 } 547 }
550 break; 548 break;
551 549
552 case BER_TYPE_UCS2: 550 case BER_TYPE_UCS2:
553 res = decode_ucs (2); 551 res = decode_ucs (2);
556 case BER_TYPE_UCS4: 554 case BER_TYPE_UCS4:
557 res = decode_ucs (4); 555 res = decode_ucs (4);
558 break; 556 break;
559 557
560 case BER_TYPE_REAL: 558 case BER_TYPE_REAL:
559 error ("BER_TYPE_REAL not implemented");
560
561 case BER_TYPE_CROAK: 561 case BER_TYPE_CROAK:
562 croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag);
563
562 default: 564 default:
563 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 565 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
564 } 566 }
565 567
566 AV *av = newAV (); 568 AV *av = newAV ();
656} 658}
657 659
658static U8 * 660static U8 *
659put_length_at (UV val, U8 *cur) 661put_length_at (UV val, U8 *cur)
660{ 662{
661 if (val < 0x7fU) 663 if (val <= 0x7fU)
662 *cur++ = val; 664 *cur++ = val;
663 else 665 else
664 { 666 {
665 U8 *lenb = cur++; 667 U8 *lenb = cur++;
666 668
667#if UVSIZE > 4 669#if UVSIZE > 4
668 *cur = val >> 56; cur += *cur > 0; 670 *cur = val >> 56; cur += val >= ((UV)1 << (8 * 7));
669 *cur = val >> 48; cur += *cur > 0; 671 *cur = val >> 48; cur += val >= ((UV)1 << (8 * 6));
670 *cur = val >> 40; cur += *cur > 0; 672 *cur = val >> 40; cur += val >= ((UV)1 << (8 * 5));
671 *cur = val >> 32; cur += *cur > 0; 673 *cur = val >> 32; cur += val >= ((UV)1 << (8 * 4));
672#endif 674#endif
673 *cur = val >> 24; cur += *cur > 0; 675 *cur = val >> 24; cur += val >= ((UV)1 << (8 * 3));
674 *cur = val >> 16; cur += *cur > 0; 676 *cur = val >> 16; cur += val >= ((UV)1 << (8 * 2));
675 *cur = val >> 8; cur += *cur > 0; 677 *cur = val >> 8; cur += val >= ((UV)1 << (8 * 1));
676 *cur = val ; cur += 1; 678 *cur = val ; cur += 1;
677 679
678 *lenb = 0x80 + cur - lenb - 1; 680 *lenb = 0x80 + cur - lenb - 1;
679 } 681 }
680 682
682} 684}
683 685
684static void 686static void
685put_length (UV val) 687put_length (UV val)
686{ 688{
687 need (5 + val); 689 need (9 + val);
688 cur = put_length_at (val, cur); 690 cur = put_length_at (val, cur);
689} 691}
690 692
691// return how many bytes the encoded length requires 693// return how many bytes the encoded length requires
692static int length_length (UV val) 694static int length_length (UV val)
693{ 695{
694 return val < 0x7fU 696 // use hashing with a DeBruin sequence, anyone?
697 return expect_true (val <= 0x7fU)
695 ? 1 698 ? 1
696 : 2 699 : 2
697 + (val > 0xffU) 700 + (val > 0x000000000000ffU)
698 + (val > 0xffffU) 701 + (val > 0x0000000000ffffU)
699 + (val > 0xffffffU) 702 + (val > 0x00000000ffffffU)
700#if UVSIZE > 4 703#if UVSIZE > 4
701 + (val > 0xffffffffU) 704 + (val > 0x000000ffffffffU)
702 + (val > 0xffffffffffU) 705 + (val > 0x0000ffffffffffU)
703 + (val > 0xffffffffffffU) 706 + (val > 0x00ffffffffffffU)
704 + (val > 0xffffffffffffffU) 707 + (val > 0xffffffffffffffU)
705#endif 708#endif
706 ; 709 ;
707} 710}
708 711
917 // and adjust later 920 // and adjust later
918 need (1); 921 need (1);
919 STRLEN mark = len_fixup_mark (); 922 STRLEN mark = len_fixup_mark ();
920 923
921 if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV)) 924 if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV))
922 croak ("BER constructed data must be array-reference"); 925 croak ("BER CONSTRUCTED data must be array-reference");
923 926
924 AV *av = (AV *)SvRV (data); 927 AV *av = (AV *)SvRV (data);
925 int fill = AvFILL (av); 928 int fill = AvFILL (av);
926 929
927 if (expect_false (SvRMAGICAL (av))) 930 if (expect_false (SvRMAGICAL (av)))
928 croak ("BER constructed data must not be tied"); 931 croak ("BER CONSTRUCTED data must not be tied");
929 932
930 int i; 933 int i;
931 for (i = 0; i <= fill; ++i) 934 for (i = 0; i <= fill; ++i)
932 encode_ber (AvARRAY (av)[i]); 935 encode_ber (AvARRAY (av)[i]);
933 936
988 case BER_TYPE_UCS4: 991 case BER_TYPE_UCS4:
989 encode_ucs (data, 4); 992 encode_ucs (data, 4);
990 break; 993 break;
991 994
992 case BER_TYPE_REAL: 995 case BER_TYPE_REAL:
996 croak ("BER_TYPE_REAL not implemented");
997
993 case BER_TYPE_CROAK: 998 case BER_TYPE_CROAK:
999 croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag);
1000
994 default: 1001 default:
995 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 1002 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
996 } 1003 }
997 1004
998} 1005}
1081 1088
1082 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) 1089 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--)
1083 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); 1090 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv));
1084} 1091}
1085 1092
1086SV * 1093void
1087ber_decode (SV *ber, SV *profile = &PL_sv_undef) 1094ber_decode (SV *ber, SV *profile = &PL_sv_undef)
1095 ALIAS:
1096 ber_decode_prefix = 1
1088 CODE: 1097 PPCODE:
1089{ 1098{
1090 cur_profile = SvPROFILE (profile); 1099 cur_profile = SvPROFILE (profile);
1091 STRLEN len; 1100 STRLEN len;
1092 buf = (U8 *)SvPVbyte (ber, len); 1101 buf = (U8 *)SvPVbyte (ber, len);
1093 cur = buf; 1102 cur = buf;
1094 end = buf + len; 1103 end = buf + len;
1095 1104
1096 RETVAL = decode_ber (); 1105 SV *tuple = decode_ber ();
1106
1107 EXTEND (SP, 2);
1108 PUSHs (sv_2mortal (tuple));
1109
1110 if (ix)
1111 PUSHs (sv_2mortal (newSViv (cur - buf)));
1112 else if (cur != end)
1113 error ("trailing garbage after BER value");
1097} 1114}
1098 OUTPUT: RETVAL
1099 1115
1100void 1116void
1101ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *flags = &PL_sv_undef, SV *data = &PL_sv_undef) 1117ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *flags = &PL_sv_undef, SV *data = &PL_sv_undef)
1102 PPCODE: 1118 PPCODE:
1103{ 1119{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines