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.24 by root, Sun Apr 21 01:58:15 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;
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)) 265 if (expect_false (res >> UVSIZE * 8 - 7))
266 error ("BER variable integer overflow"); 266 error ("BER variable length integer overflow");
267 267
268 res = (res << 7) | (c & 0x7f); 268 res = (res << 7) | (c & 0x7f);
269 269
270 if (!(c & 0x80)) 270 if (expect_true (!(c & 0x80)))
271 return res; 271 return res;
272 272
273 c = get_u8 (); 273 c = get_u8 ();
274 } 274 }
275} 275}
277static UV 277static UV
278get_length (void) 278get_length (void)
279{ 279{
280 UV res = get_u8 (); 280 UV res = get_u8 ();
281 281
282 if (res & 0x80) 282 if (expect_false (res & 0x80))
283 { 283 {
284 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
285 res = 0; 298 res = 0;
286 299 do
287 switch (cnt) 300 res = (res << 8) | *cur++;
288 { 301 while (--cnt);
289 case 0:
290 error ("indefinite ASN.1 lengths not supported");
291
292 case 0x7f:
293 error ("ASN.1 reserved value in length (X.690 8.1.3.5)");
294
295 default:
296 error ("ASN.1 length too long (only up to 2**64 octets supported)");
297
298 case 8: res = (res << 8) | get_u8 ();
299 case 7: res = (res << 8) | get_u8 ();
300 case 6: res = (res << 8) | get_u8 ();
301 case 5: res = (res << 8) | get_u8 ();
302 case 4: res = (res << 8) | get_u8 ();
303 case 3: res = (res << 8) | get_u8 ();
304 case 2: res = (res << 8) | get_u8 ();
305 case 1: res = (res << 8) | get_u8 ();
306 }
307 } 302 }
308 303
309 return res; 304 return res;
310} 305}
311 306
313decode_int (void) 308decode_int (void)
314{ 309{
315 UV len = get_length (); 310 UV len = get_length ();
316 311
317 if (!len) 312 if (!len)
318 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)");
319 314
320 U8 *data = get_n (len); 315 U8 *data = get_n (len);
321 316
322 if (expect_false (len > 1)) 317 if (expect_false (len > 1))
323 { 318 {
324 U16 mask = (data [0] << 8) | data [1] & 0xff80; 319 U16 mask = (data [0] << 8) | data [1] & 0xff80;
325 320
326 if (expect_false (mask == 0xff80 || mask == 0x0000)) 321 if (expect_false (mask == 0xff80 || mask == 0x0000))
327 error ("illegal padding in integer (X.690 8.3.2)"); 322 error ("illegal padding in BER_TYPE_INT (X.690 8.3.2)");
328 } 323 }
329 324
330 int negative = data [0] & 0x80; 325 int negative = data [0] & 0x80;
331 326
332 UV val = negative ? -1 : 0; // copy signbit to all bits 327 UV val = negative ? -1 : 0; // copy signbit to all bits
333 328
334 if (len > UVSIZE + (!negative && !*data)) 329 if (len > UVSIZE + (!negative && !*data))
335 error ("INTEGER overflow"); 330 error ("BER_TYPE_INT overflow");
336 331
337 do 332 do
338 val = (val << 8) | *data++; 333 val = (val << 8) | *data++;
339 while (--len); 334 while (--len);
340 335
388{ 383{
389 UV len = get_length (); 384 UV len = get_length ();
390 385
391 if (len <= 0) 386 if (len <= 0)
392 { 387 {
393 error ("OBJECT IDENTIFIER length equal to zero"); 388 error ("BER_TYPE_OID length must not be zero");
394 return &PL_sv_undef; 389 return &PL_sv_undef;
395 } 390 }
396 391
397 U8 *end = cur + len; 392 U8 *end = cur + len;
398 UV w = get_w (); 393 UV w = get_w ();
400 static char oid[MAX_OID_STRLEN]; // static, because too large for stack 395 static char oid[MAX_OID_STRLEN]; // static, because too large for stack
401 char *app = oid; 396 char *app = oid;
402 397
403 if (relative) 398 if (relative)
404 app = write_uv (app, w); 399 app = write_uv (app, w);
400 else
401 {
402 UV w1, w2;
403
405 else if (w < 2 * 40) 404 if (w < 2 * 40)
406 { 405 (w1 = w / 40), (w2 = w % 40);
406 else
407 (w1 = 2), (w2 = w - 2 * 40);
408
407 app = write_uv (app, (U8)w / 40); 409 app = write_uv (app, w1);
408 *app++ = '.'; 410 *app++ = '.';
409 app = write_uv (app, (U8)w % 40);
410 }
411 else
412 {
413 app = write_uv (app, 2); 411 app = write_uv (app, w2);
414 *app++ = '.';
415 app = write_uv (app, w - 2 * 40);
416 } 412 }
417 413
418 while (cur < end) 414 while (cur < end)
419 { 415 {
420 // we assume an oid component is never > 64 digits 416 // we assume an oid component is never > 64 digits
487 483
488 while (cur < buf + seqend) 484 while (cur < buf + seqend)
489 av_push (av, decode_ber ()); 485 av_push (av, decode_ber ());
490 486
491 if (cur > buf + seqend) 487 if (cur > buf + seqend)
492 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);
493 489
494 res = newRV_inc ((SV *)av); 490 res = newRV_inc ((SV *)av);
495 } 491 }
496 else 492 else
497 switch (profile_lookup (cur_profile, klass, tag)) 493 switch (profile_lookup (cur_profile, klass, tag))
544 UV len = get_length (); 540 UV len = get_length ();
545 541
546 if (len != 4) 542 if (len != 4)
547 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);
548 544
549 U8 c1 = get_u8 (); 545 U8 *data = get_n (4);
550 U8 c2 = get_u8 (); 546 res = newSVpvf ("%d.%d.%d.%d", data [0], data [1], data [2], data [3]);
551 U8 c3 = get_u8 ();
552 U8 c4 = get_u8 ();
553
554 res = newSVpvf ("%d.%d.%d.%d", c1, c2, c3, c4);
555 } 547 }
556 break; 548 break;
557 549
558 case BER_TYPE_UCS2: 550 case BER_TYPE_UCS2:
559 res = decode_ucs (2); 551 res = decode_ucs (2);
562 case BER_TYPE_UCS4: 554 case BER_TYPE_UCS4:
563 res = decode_ucs (4); 555 res = decode_ucs (4);
564 break; 556 break;
565 557
566 case BER_TYPE_REAL: 558 case BER_TYPE_REAL:
559 error ("BER_TYPE_REAL not implemented");
560
567 case BER_TYPE_CROAK: 561 case BER_TYPE_CROAK:
562 croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag);
563
568 default: 564 default:
569 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 565 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
570 } 566 }
571 567
572 AV *av = newAV (); 568 AV *av = newAV ();
662} 658}
663 659
664static U8 * 660static U8 *
665put_length_at (UV val, U8 *cur) 661put_length_at (UV val, U8 *cur)
666{ 662{
667 if (val < 0x7fU) 663 if (val <= 0x7fU)
668 *cur++ = val; 664 *cur++ = val;
669 else 665 else
670 { 666 {
671 U8 *lenb = cur++; 667 U8 *lenb = cur++;
672 668
673#if UVSIZE > 4 669#if UVSIZE > 4
674 *cur = val >> 56; cur += *cur > 0; 670 *cur = val >> 56; cur += val >= ((UV)1 << (8 * 7));
675 *cur = val >> 48; cur += *cur > 0; 671 *cur = val >> 48; cur += val >= ((UV)1 << (8 * 6));
676 *cur = val >> 40; cur += *cur > 0; 672 *cur = val >> 40; cur += val >= ((UV)1 << (8 * 5));
677 *cur = val >> 32; cur += *cur > 0; 673 *cur = val >> 32; cur += val >= ((UV)1 << (8 * 4));
678#endif 674#endif
679 *cur = val >> 24; cur += *cur > 0; 675 *cur = val >> 24; cur += val >= ((UV)1 << (8 * 3));
680 *cur = val >> 16; cur += *cur > 0; 676 *cur = val >> 16; cur += val >= ((UV)1 << (8 * 2));
681 *cur = val >> 8; cur += *cur > 0; 677 *cur = val >> 8; cur += val >= ((UV)1 << (8 * 1));
682 *cur = val ; cur += 1; 678 *cur = val ; cur += 1;
683 679
684 *lenb = 0x80 + cur - lenb - 1; 680 *lenb = 0x80 + cur - lenb - 1;
685 } 681 }
686 682
688} 684}
689 685
690static void 686static void
691put_length (UV val) 687put_length (UV val)
692{ 688{
693 need (5 + val); 689 need (9 + val);
694 cur = put_length_at (val, cur); 690 cur = put_length_at (val, cur);
695} 691}
696 692
697// return how many bytes the encoded length requires 693// return how many bytes the encoded length requires
698static int length_length (UV val) 694static int length_length (UV val)
699{ 695{
700 return val < 0x7fU 696 // use hashing with a DeBruin sequence, anyone?
697 return expect_true (val <= 0x7fU)
701 ? 1 698 ? 1
702 : 2 699 : 2
703 + (val > 0xffU) 700 + (val > 0x000000000000ffU)
704 + (val > 0xffffU) 701 + (val > 0x0000000000ffffU)
705 + (val > 0xffffffU) 702 + (val > 0x00000000ffffffU)
706#if UVSIZE > 4 703#if UVSIZE > 4
707 + (val > 0xffffffffU) 704 + (val > 0x000000ffffffffU)
708 + (val > 0xffffffffffU) 705 + (val > 0x0000ffffffffffU)
709 + (val > 0xffffffffffffU) 706 + (val > 0x00ffffffffffffU)
710 + (val > 0xffffffffffffffU) 707 + (val > 0xffffffffffffffU)
711#endif 708#endif
712 ; 709 ;
713} 710}
714 711
923 // and adjust later 920 // and adjust later
924 need (1); 921 need (1);
925 STRLEN mark = len_fixup_mark (); 922 STRLEN mark = len_fixup_mark ();
926 923
927 if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV)) 924 if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV))
928 croak ("BER constructed data must be array-reference"); 925 croak ("BER CONSTRUCTED data must be array-reference");
929 926
930 AV *av = (AV *)SvRV (data); 927 AV *av = (AV *)SvRV (data);
931 int fill = AvFILL (av); 928 int fill = AvFILL (av);
932 929
933 if (expect_false (SvRMAGICAL (av))) 930 if (expect_false (SvRMAGICAL (av)))
934 croak ("BER constructed data must not be tied"); 931 croak ("BER CONSTRUCTED data must not be tied");
935 932
936 int i; 933 int i;
937 for (i = 0; i <= fill; ++i) 934 for (i = 0; i <= fill; ++i)
938 encode_ber (AvARRAY (av)[i]); 935 encode_ber (AvARRAY (av)[i]);
939 936
994 case BER_TYPE_UCS4: 991 case BER_TYPE_UCS4:
995 encode_ucs (data, 4); 992 encode_ucs (data, 4);
996 break; 993 break;
997 994
998 case BER_TYPE_REAL: 995 case BER_TYPE_REAL:
996 croak ("BER_TYPE_REAL not implemented");
997
999 case BER_TYPE_CROAK: 998 case BER_TYPE_CROAK:
999 croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag);
1000
1000 default: 1001 default:
1001 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 1002 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
1002 } 1003 }
1003 1004
1004} 1005}
1107 PUSHs (sv_2mortal (tuple)); 1108 PUSHs (sv_2mortal (tuple));
1108 1109
1109 if (ix) 1110 if (ix)
1110 PUSHs (sv_2mortal (newSViv (cur - buf))); 1111 PUSHs (sv_2mortal (newSViv (cur - buf)));
1111 else if (cur != end) 1112 else if (cur != end)
1112 error ("trailing garbage after BER data"); 1113 error ("trailing garbage after BER value");
1113} 1114}
1114 1115
1115void 1116void
1116ber_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)
1117 PPCODE: 1118 PPCODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines