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.26 by root, Sun Apr 21 10:42:22 2019 UTC vs.
Revision 1.29 by root, Tue Apr 23 19:44:12 2019 UTC

265 if (expect_false (res >> UVSIZE * 8 - 7)) 265 if (expect_false (res >> UVSIZE * 8 - 7))
266 error ("BER variable length 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 BER value lengths not supported");
291
292 case 0x7f:
293 error ("BER reserved value in length (X.690 8.1.3.5)");
294
295 default:
296 error ("BER value length too long (must fit into UV)");
297
298#if UVSIZE > 4
299 case 8: res = (res << 8) | get_u8 ();
300 case 7: res = (res << 8) | get_u8 ();
301 case 6: res = (res << 8) | get_u8 ();
302 case 5: res = (res << 8) | get_u8 ();
303#endif
304 case 4: res = (res << 8) | get_u8 ();
305 case 3: res = (res << 8) | get_u8 ();
306 case 2: res = (res << 8) | get_u8 ();
307 case 1: res = (res << 8) | get_u8 ();
308 }
309 } 302 }
310 303
311 return res; 304 return res;
312} 305}
313 306
402 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
403 char *app = oid; 396 char *app = oid;
404 397
405 if (relative) 398 if (relative)
406 app = write_uv (app, w); 399 app = write_uv (app, w);
400 else
401 {
402 UV w1, w2;
403
407 else if (w < 2 * 40) 404 if (w < 2 * 40)
408 { 405 (w1 = w / 40), (w2 = w % 40);
406 else
407 (w1 = 2), (w2 = w - 2 * 40);
408
409 app = write_uv (app, (U8)w / 40); 409 app = write_uv (app, w1);
410 *app++ = '.'; 410 *app++ = '.';
411 app = write_uv (app, (U8)w % 40);
412 }
413 else
414 {
415 app = write_uv (app, 2); 411 app = write_uv (app, w2);
416 *app++ = '.';
417 app = write_uv (app, w - 2 * 40);
418 } 412 }
419 413
420 while (cur < end) 414 while (cur < end)
421 { 415 {
422 // we assume an oid component is never > 64 digits 416 // we assume an oid component is never > 64 digits
546 UV len = get_length (); 540 UV len = get_length ();
547 541
548 if (len != 4) 542 if (len != 4)
549 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);
550 544
551 U8 c1 = get_u8 (); 545 U8 *data = get_n (4);
552 U8 c2 = get_u8 (); 546 res = newSVpvf ("%d.%d.%d.%d", data [0], data [1], data [2], data [3]);
553 U8 c3 = get_u8 ();
554 U8 c4 = get_u8 ();
555
556 res = newSVpvf ("%d.%d.%d.%d", c1, c2, c3, c4);
557 } 547 }
558 break; 548 break;
559 549
560 case BER_TYPE_UCS2: 550 case BER_TYPE_UCS2:
561 res = decode_ucs (2); 551 res = decode_ucs (2);
668} 658}
669 659
670static U8 * 660static U8 *
671put_length_at (UV val, U8 *cur) 661put_length_at (UV val, U8 *cur)
672{ 662{
673 if (val < 0x7fU) 663 if (val <= 0x7fU)
674 *cur++ = val; 664 *cur++ = val;
675 else 665 else
676 { 666 {
677 U8 *lenb = cur++; 667 U8 *lenb = cur++;
678 668
679#if UVSIZE > 4 669#if UVSIZE > 4
680 *cur = val >> 56; cur += *cur > 0; 670 *cur = val >> 56; cur += val >= ((UV)1 << (8 * 7));
681 *cur = val >> 48; cur += *cur > 0; 671 *cur = val >> 48; cur += val >= ((UV)1 << (8 * 6));
682 *cur = val >> 40; cur += *cur > 0; 672 *cur = val >> 40; cur += val >= ((UV)1 << (8 * 5));
683 *cur = val >> 32; cur += *cur > 0; 673 *cur = val >> 32; cur += val >= ((UV)1 << (8 * 4));
684#endif 674#endif
685 *cur = val >> 24; cur += *cur > 0; 675 *cur = val >> 24; cur += val >= ((UV)1 << (8 * 3));
686 *cur = val >> 16; cur += *cur > 0; 676 *cur = val >> 16; cur += val >= ((UV)1 << (8 * 2));
687 *cur = val >> 8; cur += *cur > 0; 677 *cur = val >> 8; cur += val >= ((UV)1 << (8 * 1));
688 *cur = val ; cur += 1; 678 *cur = val ; cur += 1;
689 679
690 *lenb = 0x80 + cur - lenb - 1; 680 *lenb = 0x80 + cur - lenb - 1;
691 } 681 }
692 682
694} 684}
695 685
696static void 686static void
697put_length (UV val) 687put_length (UV val)
698{ 688{
699 need (5 + val); 689 need (9 + val);
700 cur = put_length_at (val, cur); 690 cur = put_length_at (val, cur);
701} 691}
702 692
703// return how many bytes the encoded length requires 693// return how many bytes the encoded length requires
704static int length_length (UV val) 694static int length_length (UV val)
705{ 695{
706 return val < 0x7fU 696 // use hashing with a DeBruin sequence, anyone?
697 return expect_true (val <= 0x7fU)
707 ? 1 698 ? 1
708 : 2 699 : 2
709 + (val > 0xffU) 700 + (val > 0x000000000000ffU)
710 + (val > 0xffffU) 701 + (val > 0x0000000000ffffU)
711 + (val > 0xffffffU) 702 + (val > 0x00000000ffffffU)
712#if UVSIZE > 4 703#if UVSIZE > 4
713 + (val > 0xffffffffU) 704 + (val > 0x000000ffffffffU)
714 + (val > 0xffffffffffU) 705 + (val > 0x0000ffffffffffU)
715 + (val > 0xffffffffffffU) 706 + (val > 0x00ffffffffffffU)
716 + (val > 0xffffffffffffffU) 707 + (val > 0xffffffffffffffU)
717#endif 708#endif
718 ; 709 ;
719} 710}
720 711

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines