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.16 by root, Sat Apr 20 15:37:27 2019 UTC vs.
Revision 1.34 by root, Thu Feb 6 11:51:40 2020 UTC

1#include "EXTERN.h" 1#include "EXTERN.h"
2#include "perl.h" 2#include "perl.h"
3#include "XSUB.h" 3#include "XSUB.h"
4
5#include <math.h>
4 6
5// C99 required! 7// C99 required!
6// this is not just for comments, but also for 8// this is not just for comments, but also for
7// integer constant semantics, 9// integer constant semantics,
8// sscanf format modifiers and more. 10// sscanf format modifiers and more.
58 ASN_CLASS_SHIFT = 6, 60 ASN_CLASS_SHIFT = 6,
59 61
60 // ASN_APPLICATION SNMP 62 // ASN_APPLICATION SNMP
61 SNMP_IPADDRESS = 0x00, 63 SNMP_IPADDRESS = 0x00,
62 SNMP_COUNTER32 = 0x01, 64 SNMP_COUNTER32 = 0x01,
65 SNMP_GAUGE32 = 0x02,
63 SNMP_UNSIGNED32 = 0x02, 66 SNMP_UNSIGNED32 = 0x02,
64 SNMP_TIMETICKS = 0x03, 67 SNMP_TIMETICKS = 0x03,
65 SNMP_OPAQUE = 0x04, 68 SNMP_OPAQUE = 0x04,
66 SNMP_COUNTER64 = 0x06, 69 SNMP_COUNTER64 = 0x06,
67}; 70};
68 71
72// low-level types this module can ecode the above (and more) into
69enum { 73enum {
70 BER_TYPE_BYTES, 74 BER_TYPE_BYTES,
71 BER_TYPE_UTF8, 75 BER_TYPE_UTF8,
72 BER_TYPE_UCS2, 76 BER_TYPE_UCS2,
73 BER_TYPE_UCS4, 77 BER_TYPE_UCS4,
79 BER_TYPE_REAL, 83 BER_TYPE_REAL,
80 BER_TYPE_IPADDRESS, 84 BER_TYPE_IPADDRESS,
81 BER_TYPE_CROAK, 85 BER_TYPE_CROAK,
82}; 86};
83 87
88// tuple array indices
84enum { 89enum {
85 BER_CLASS = 0, 90 BER_CLASS = 0,
86 BER_TAG = 1, 91 BER_TAG = 1,
87 BER_FLAGS = 2, 92 BER_FLAGS = 2,
88 BER_DATA = 3, 93 BER_DATA = 3,
99 104
100#if PERL_VERSION < 18 105#if PERL_VERSION < 18
101# define utf8_to_uvchr_buf(s,e,l) utf8_to_uvchr (s, l) 106# define utf8_to_uvchr_buf(s,e,l) utf8_to_uvchr (s, l)
102#endif 107#endif
103 108
109#ifndef SvREFCNT_inc_NN
110#define SvREFCNT_inc_NN(x) SvREFCNT_inc (x)
111#endif
112#ifndef SvREFCNT_dec_NN
113#define SvREFCNT_dec_NN(x) SvREFCNT_dec (x)
114#endif
115
104#if __GNUC__ >= 3 116#if __GNUC__ >= 3
105# define expect(expr,value) __builtin_expect ((expr), (value)) 117# define expect(expr,value) __builtin_expect ((expr), (value))
106# define INLINE static inline 118# define INLINE static inline
107#else 119#else
108# define expect(expr,value) (expr) 120# define expect(expr,value) (expr)
140{ 152{
141 if (!SvOK (profile)) 153 if (!SvOK (profile))
142 return default_profile; 154 return default_profile;
143 155
144 if (!SvROK (profile)) 156 if (!SvROK (profile))
145 croak ("invalid profile"); 157 croak ("Convert::BER::XS::Profile expected");
146 158
147 profile = SvRV (profile); 159 profile = SvRV (profile);
148 160
149 if (SvSTASH (profile) != profile_stash) 161 if (SvSTASH (profile) != profile_stash)
150 croak ("invalid profile object"); 162 croak ("Convert::BER::XS::Profile expected");
151 163
152 return (void *)profile; 164 return (void *)profile;
153} 165}
154 166
155static int 167static int
227} 239}
228 240
229// get_* functions fetch something from the buffer 241// get_* functions fetch something from the buffer
230// decode_* functions use get_* fun ctions to decode ber values 242// decode_* functions use get_* fun ctions to decode ber values
231 243
244// get single octet
245static U8
246get_u8 (void)
247{
248 if (cur == end)
249 error ("unexpected end of message buffer");
250
251 return *cur++;
252}
253
232// get n octets 254// get n octets
233static U8 * 255static U8 *
234get_n (UV count) 256get_n (UV count)
235{ 257{
236 want (count); 258 want (count);
237 U8 *res = cur; 259 U8 *res = cur;
238 cur += count; 260 cur += count;
239 return res; 261 return res;
240} 262}
241 263
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") 264// get ber-encoded integer (i.e. pack "w")
253static UV 265static UV
254get_w (void) 266get_w (void)
255{ 267{
256 UV res = 0; 268 UV res = 0;
269 U8 c = get_u8 ();
270
271 if (expect_false (c == 0x80))
272 error ("invalid BER padding (X.690 8.1.2.4.2, 8.19.2)");
257 273
258 for (;;) 274 for (;;)
259 { 275 {
260 U8 c = get_u8 (); 276 if (expect_false (res >> UVSIZE * 8 - 7))
277 error ("BER variable length integer overflow");
278
261 res = (res << 7) | (c & 0x7f); 279 res = (res << 7) | (c & 0x7f);
262 280
263 if (!(c & 0x80)) 281 if (expect_true (!(c & 0x80)))
264 return res; 282 return res;
283
284 c = get_u8 ();
265 } 285 }
266} 286}
267 287
268static UV 288static UV
269get_length (void) 289get_length (void)
270{ 290{
271 UV res = get_u8 (); 291 UV res = get_u8 ();
272 292
273 if (res & 0x80) 293 if (expect_false (res & 0x80))
274 { 294 {
275 int cnt = res & 0x7f; 295 U8 cnt = res & 0x7f;
296
297 // this genewrates quite ugly code, but the overhead
298 // of copying the bytes for these lengths is probably so high
299 // that a slightly inefficient get_length won't matter.
300
301 if (expect_false (cnt == 0))
302 error ("invalid use of indefinite BER length form in primitive encoding (X.690 8.1.3.2)");
303
304 if (expect_false (cnt > UVSIZE))
305 error ("BER value length too long (must fit into UV) or BER reserved value in length (X.690 8.1.3.5)");
306
307 want (cnt);
308
276 res = 0; 309 res = 0;
277 310 do
278 switch (cnt) 311 res = (res << 8) | *cur++;
279 { 312 while (--cnt);
280 case 0:
281 error ("indefinite ASN.1 lengths not supported");
282 return 0;
283
284 default:
285 error ("ASN.1 length too long");
286 return 0;
287
288 case 8: res = (res << 8) | get_u8 ();
289 case 7: res = (res << 8) | get_u8 ();
290 case 6: res = (res << 8) | get_u8 ();
291 case 5: res = (res << 8) | get_u8 ();
292 case 4: res = (res << 8) | get_u8 ();
293 case 3: res = (res << 8) | get_u8 ();
294 case 2: res = (res << 8) | get_u8 ();
295 case 1: res = (res << 8) | get_u8 ();
296 }
297 } 313 }
298 314
299 return res; 315 return res;
300} 316}
301 317
302static SV * 318static SV *
303decode_int (void) 319decode_int (UV len)
304{ 320{
305 UV len = get_length ();
306
307 if (!len) 321 if (!len)
308 { 322 error ("invalid BER_TYPE_INT length zero (X.690 8.3.1)");
309 error ("invalid integer length equal to zero");
310 return 0;
311 }
312 323
313 U8 *data = get_n (len); 324 U8 *data = get_n (len);
314 325
326 if (expect_false (len > 1))
327 {
328 U16 mask = (data [0] << 8) | data [1] & 0xff80;
329
330 if (expect_false (mask == 0xff80 || mask == 0x0000))
331 error ("invalid padding in BER_TYPE_INT (X.690 8.3.2)");
332 }
333
315 int negative = data [0] & 0x80; 334 int negative = data [0] & 0x80;
316 335
317 UV val = negative ? -1 : 0; // copy signbit to all bits 336 UV val = negative ? -1 : 0; // copy signbit to all bits
337
338 if (len > UVSIZE + (!negative && !*data))
339 error ("BER_TYPE_INT overflow");
318 340
319 do 341 do
320 val = (val << 8) | *data++; 342 val = (val << 8) | *data++;
321 while (--len); 343 while (--len);
322 344
324 // but that's ok, as perl relies on it as well. 346 // but that's ok, as perl relies on it as well.
325 return negative ? newSViv ((IV)val) : newSVuv (val); 347 return negative ? newSViv ((IV)val) : newSVuv (val);
326} 348}
327 349
328static SV * 350static SV *
329decode_data (void) 351decode_data (UV len)
330{ 352{
331 UV len = get_length ();
332 return newSVpvn ((char *)get_n (len), len); 353 return newSVpvn ((char *)get_n (len), len);
333} 354}
334 355
335// helper for decode_object_identifier 356// helper for decode_object_identifier
336static char * 357static char *
364 385
365 return buf; 386 return buf;
366} 387}
367 388
368static SV * 389static SV *
369decode_oid (int relative) 390decode_oid (UV len, int relative)
370{ 391{
371 UV len = get_length ();
372
373 if (len <= 0) 392 if (len <= 0)
374 { 393 {
375 error ("OBJECT IDENTIFIER length equal to zero"); 394 error ("BER_TYPE_OID length must not be zero");
376 return &PL_sv_undef; 395 return &PL_sv_undef;
377 } 396 }
378 397
379 U8 *end = cur + len; 398 U8 *end = cur + len;
380 UV w = get_w (); 399 UV w = get_w ();
381 400
382 static char oid[MAX_OID_STRLEN]; // static, becaueds too large for stack 401 static char oid[MAX_OID_STRLEN]; // static, because too large for stack
383 char *app = oid; 402 char *app = oid;
384 403
385 if (relative) 404 if (relative)
386 app = write_uv (app, w); 405 app = write_uv (app, w);
387 else 406 else
388 { 407 {
408 UV w1, w2;
409
410 if (w < 2 * 40)
411 (w1 = w / 40), (w2 = w % 40);
412 else
413 (w1 = 2), (w2 = w - 2 * 40);
414
389 app = write_uv (app, (U8)w / 40); 415 app = write_uv (app, w1);
390 *app++ = '.'; 416 *app++ = '.';
391 app = write_uv (app, (U8)w % 40); 417 app = write_uv (app, w2);
392 } 418 }
393 419
394 while (cur < end) 420 while (cur < end)
395 { 421 {
396 // we assume an oid component is never > 64 digits 422 // we assume an oid component is never > 64 digits
403 } 429 }
404 430
405 return newSVpvn (oid, app - oid); 431 return newSVpvn (oid, app - oid);
406} 432}
407 433
434// oh my, this is a total mess
435static SV *
436decode_real (UV len)
437{
438 SV *res;
439 U8 *beg = cur;
440
441 if (len == 0)
442 res = newSVnv (0.);
443 else
444 {
445 U8 info = get_u8 ();
446
447 if (info & 0x80)
448 {
449 // binary
450 static const U8 base[] = { 2, 8, 16, 0 };
451 NV S = info & 0x40 ? -1 : 1; // sign
452 NV B = base [(info >> 4) & 3]; // base
453 NV F = 1 << ((info >> 2) & 3); // scale factor ("shift")
454 int L = info & 3; // exponent length
455
456 if (!B)
457 croak ("BER_TYPE_REAL binary encoding uses invalid base (0x%02x)", info);
458
459 SAVETMPS;
460
461 SV *E = sv_2mortal (decode_int (L == 3 ? get_u8 () : L + 1));
462 SV *M = sv_2mortal (decode_int (len - (cur - beg)));
463
464 res = newSVnv (S * SvNV (M) * F * Perl_pow (B, SvNV (E)));
465
466 FREETMPS;
467 }
468 else if (info & 0x40)
469 {
470 // SpecialRealValue
471 U8 special = get_u8 ();
472 NV val;
473
474 switch (special)
475 {
476 case 0x40: val = NV_INF; break;
477 case 0x41: val = -NV_INF; break;
478 case 0x42: val = NV_NAN; break;
479 case 0x43: val = -(NV)0.; break;
480
481 default:
482 croak ("BER_TYPE_REAL SpecialRealValues invalid encoding 0x%02x (X.690 8.5.9)", special);
483 }
484
485 res = newSVnv (val);
486 }
487 else
488 {
489 // decimal
490 dSP;
491 SAVETMPS;
492 PUSHMARK (SP);
493 EXTEND (SP, 2);
494 PUSHs (sv_2mortal (newSVcacheint (info & 0x3f)));
495 PUSHs (sv_2mortal (newSVpvn (get_n (len - 1), len - 1)));
496 PUTBACK;
497 call_pv ("Convert::BER::XS::_decode_real_decimal", G_SCALAR);
498 SPAGAIN;
499 res = SvREFCNT_inc_NN (POPs);
500 PUTBACK;
501 FREETMPS;
502 }
503 }
504
505 if (cur - beg != len)
506 {
507 SvREFCNT_dec_NN (res);
508 croak ("BER_TYPE_REAL invalid content length (X.690 8,5)");
509 }
510
511 return res;
512}
513
408// TODO: this is unacceptably slow 514// TODO: this is unacceptably slow
409static SV * 515static SV *
410decode_ucs (int chrsize) 516decode_ucs (UV len, int chrsize)
411{ 517{
412 SV *res = NEWSV (0, 0);
413
414 UV len = get_length ();
415
416 if (len & (chrsize - 1)) 518 if (len & (chrsize - 1))
417 croak ("BER_TYPE_UCS has an invalid number of octets (%d)", len); 519 croak ("BER_TYPE_UCS has an invalid number of octets (%d)", len);
520
521 SV *res = NEWSV (0, 0);
418 522
419 while (len) 523 while (len)
420 { 524 {
421 U8 b1 = get_u8 (); 525 U8 b1 = get_u8 ();
422 U8 b2 = get_u8 (); 526 U8 b2 = get_u8 ();
453 int tag = identifier & ASN_TAG_MASK; 557 int tag = identifier & ASN_TAG_MASK;
454 558
455 if (tag == ASN_TAG_BER) 559 if (tag == ASN_TAG_BER)
456 tag = get_w (); 560 tag = get_w ();
457 561
458 if (tag == ASN_TAG_BER)
459 tag = get_w ();
460
461 if (constructed) 562 if (constructed)
462 { 563 {
564 want (1);
565 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
566
567 if (expect_false (*cur == 0x80))
568 {
569 // indefinite length
570 ++cur;
571
572 for (;;)
573 {
574 want (2);
575 if (!cur [0] && !cur [1])
576 {
577 cur += 2;
578 break;
579 }
580
581 av_push (av, decode_ber ());
582 }
583 }
584 else
585 {
586 UV len = get_length ();
587 UV seqend = (cur - buf) + len;
588
589 while (cur < buf + seqend)
590 av_push (av, decode_ber ());
591
592 if (expect_false (cur > buf + seqend))
593 croak ("CONSTRUCTED type %02x length overflow (0x%x 0x%x)\n", identifier, (int)(cur - buf), (int)seqend);
594 }
595
596 res = newRV_inc ((SV *)av);
597 }
598 else
599 {
463 UV len = get_length (); 600 UV len = get_length ();
464 UV seqend = (cur - buf) + len;
465 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
466 601
467 while (cur < buf + seqend)
468 av_push (av, decode_ber ());
469
470 if (cur > buf + seqend)
471 croak ("constructed type %02x overflow (%x %x)\n", identifier, cur - buf, seqend);
472
473 res = newRV_inc ((SV *)av);
474 }
475 else
476 switch (profile_lookup (cur_profile, klass, tag)) 602 switch (profile_lookup (cur_profile, klass, tag))
477 { 603 {
478 case BER_TYPE_NULL: 604 case BER_TYPE_NULL:
479 { 605 if (expect_false (len))
480 UV len = get_length ();
481
482 if (len)
483 croak ("BER_TYPE_NULL value with non-zero length %d encountered", len); 606 croak ("BER_TYPE_NULL value with non-zero length %d encountered (X.690 8.8.2)", len);
484 607
485 res = &PL_sv_undef; 608 res = &PL_sv_undef;
486 }
487 break; 609 break;
488 610
489 case BER_TYPE_BOOL: 611 case BER_TYPE_BOOL:
490 {
491 UV len = get_length ();
492
493 if (len != 1) 612 if (expect_false (len != 1))
494 croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered", len); 613 croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered (X.690 8.2.1)", len);
495 614
496 res = newSVcacheint (!!get_u8 ()); 615 res = newSVcacheint (!!get_u8 ());
497 }
498 break; 616 break;
499 617
500 case BER_TYPE_OID: 618 case BER_TYPE_OID:
501 res = decode_oid (0); 619 res = decode_oid (len, 0);
502 break; 620 break;
503 621
504 case BER_TYPE_RELOID: 622 case BER_TYPE_RELOID:
505 res = decode_oid (1); 623 res = decode_oid (len, 1);
506 break; 624 break;
507 625
508 case BER_TYPE_INT: 626 case BER_TYPE_INT:
509 res = decode_int (); 627 res = decode_int (len);
510 break; 628 break;
511 629
512 case BER_TYPE_UTF8: 630 case BER_TYPE_UTF8:
513 res = decode_data (); 631 res = decode_data (len);
514 SvUTF8_on (res); 632 SvUTF8_on (res);
515 break; 633 break;
516 634
517 case BER_TYPE_BYTES: 635 case BER_TYPE_BYTES:
518 res = decode_data (); 636 res = decode_data (len);
519 break; 637 break;
520 638
521 case BER_TYPE_IPADDRESS: 639 case BER_TYPE_IPADDRESS:
522 { 640 {
523 UV len = get_length ();
524
525 if (len != 4) 641 if (len != 4)
526 croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered", len); 642 croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered (RFC 2578 7.1.5)", len);
527 643
528 U8 c1 = get_u8 (); 644 U8 *data = get_n (4);
529 U8 c2 = get_u8 (); 645 res = newSVpvf ("%d.%d.%d.%d", data [0], data [1], data [2], data [3]);
530 U8 c3 = get_u8 ();
531 U8 c4 = get_u8 ();
532
533 res = newSVpvf ("%d.%d.%d.%d", c1, c2, c3, c4);
534 } 646 }
535 break; 647 break;
536 648
537 case BER_TYPE_UCS2: 649 case BER_TYPE_UCS2:
538 res = decode_ucs (2); 650 res = decode_ucs (len, 2);
539 break; 651 break;
540 652
541 case BER_TYPE_UCS4: 653 case BER_TYPE_UCS4:
542 res = decode_ucs (4); 654 res = decode_ucs (len, 4);
543 break; 655 break;
544 656
545 case BER_TYPE_REAL: 657 case BER_TYPE_REAL:
658 res = decode_real (len);
659 break;
660
546 case BER_TYPE_CROAK: 661 case BER_TYPE_CROAK:
662 croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag);
663
547 default: 664 default:
548 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 665 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
549 } 666 }
667 }
550 668
551 AV *av = newAV (); 669 AV *av = newAV ();
552 av_fill (av, BER_ARRAYSIZE - 1); 670 av_fill (av, BER_ARRAYSIZE - 1);
553 AvARRAY (av)[BER_CLASS] = newSVcacheint (klass); 671 AvARRAY (av)[BER_CLASS] = newSVcacheint (klass);
554 AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); 672 AvARRAY (av)[BER_TAG ] = newSVcacheint (tag);
566strlen_sum (STRLEN l1, STRLEN l2) 684strlen_sum (STRLEN l1, STRLEN l2)
567{ 685{
568 size_t sum = l1 + l2; 686 size_t sum = l1 + l2;
569 687
570 if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) 688 if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum)
571 croak ("JSON::XS: string size overflow"); 689 croak ("Convert::BER::XS: string size overflow");
572 690
573 return sum; 691 return sum;
574} 692}
575 693
576static void 694static void
641} 759}
642 760
643static U8 * 761static U8 *
644put_length_at (UV val, U8 *cur) 762put_length_at (UV val, U8 *cur)
645{ 763{
646 if (val < 0x7fU) 764 if (val <= 0x7fU)
647 *cur++ = val; 765 *cur++ = val;
648 else 766 else
649 { 767 {
650 U8 *lenb = cur++; 768 U8 *lenb = cur++;
651 769
652#if UVSIZE > 4 770#if UVSIZE > 4
653 *cur = val >> 56; cur += *cur > 0; 771 *cur = val >> 56; cur += val >= ((UV)1 << (8 * 7));
654 *cur = val >> 48; cur += *cur > 0; 772 *cur = val >> 48; cur += val >= ((UV)1 << (8 * 6));
655 *cur = val >> 40; cur += *cur > 0; 773 *cur = val >> 40; cur += val >= ((UV)1 << (8 * 5));
656 *cur = val >> 32; cur += *cur > 0; 774 *cur = val >> 32; cur += val >= ((UV)1 << (8 * 4));
657#endif 775#endif
658 *cur = val >> 24; cur += *cur > 0; 776 *cur = val >> 24; cur += val >= ((UV)1 << (8 * 3));
659 *cur = val >> 16; cur += *cur > 0; 777 *cur = val >> 16; cur += val >= ((UV)1 << (8 * 2));
660 *cur = val >> 8; cur += *cur > 0; 778 *cur = val >> 8; cur += val >= ((UV)1 << (8 * 1));
661 *cur = val ; cur += 1; 779 *cur = val ; cur += 1;
662 780
663 *lenb = 0x80 + cur - lenb - 1; 781 *lenb = 0x80 + cur - lenb - 1;
664 } 782 }
665 783
667} 785}
668 786
669static void 787static void
670put_length (UV val) 788put_length (UV val)
671{ 789{
672 need (5 + val); 790 need (9 + val);
673 cur = put_length_at (val, cur); 791 cur = put_length_at (val, cur);
674} 792}
675 793
676// return how many bytes the encoded length requires 794// return how many bytes the encoded length requires
677static int length_length (UV val) 795static int length_length (UV val)
678{ 796{
679 return val < 0x7fU 797 // use hashing with a DeBruin sequence, anyone?
798 return expect_true (val <= 0x7fU)
680 ? 1 799 ? 1
681 : 2 800 : 2
682 + (val > 0xffU) 801 + (val > 0x000000000000ffU)
683 + (val > 0xffffU) 802 + (val > 0x0000000000ffffU)
684 + (val > 0xffffffU) 803 + (val > 0x00000000ffffffU)
685#if UVSIZE > 4 804#if UVSIZE > 4
686 + (val > 0xffffffffU) 805 + (val > 0x000000ffffffffU)
687 + (val > 0xffffffffffU) 806 + (val > 0x0000ffffffffffU)
688 + (val > 0xffffffffffffU) 807 + (val > 0x00ffffffffffffU)
689 + (val > 0xffffffffffffffU) 808 + (val > 0xffffffffffffffU)
690#endif 809#endif
691 ; 810 ;
692} 811}
693 812
829 } 948 }
830 949
831 len_fixup (mark); 950 len_fixup (mark);
832} 951}
833 952
834// check whether an SV is a BER tuple and returns its AV * 953static void
835static AV * 954encode_real (SV *data)
836ber_tuple (SV *tuple)
837{ 955{
838 SV *rv; 956 NV nv = SvNV (data);
839 957
840 if (expect_false (!SvROK (tuple) || SvTYPE ((rv = SvRV (tuple))) != SVt_PVAV)) 958 if (expect_false (nv == (NV)0.))
841 croak ("BER tuple must be array-reference"); 959 {
960 if (signbit (nv))
961 {
962 // negative zero
963 need (3);
964 *cur++ = 2;
965 *cur++ = 0x40;
966 *cur++ = 0x43;
967 }
968 else
969 {
970 // positive zero
971 need (1);
972 *cur++ = 0;
973 }
974 }
975 else if (expect_false (Perl_isinf (nv)))
976 {
977 need (3);
978 *cur++ = 2;
979 *cur++ = 0x40;
980 *cur++ = nv < (NV)0. ? 0x41 : 0x40;
981 }
982 else if (expect_false (Perl_isnan (nv)))
983 {
984 need (3);
985 *cur++ = 2;
986 *cur++ = 0x40;
987 *cur++ = 0x42;
988 }
989 else
990 {
991 // use decimal encoding
992 dSP;
993 SAVETMPS;
994 PUSHMARK (SP);
995 EXTEND (SP, 2);
996 PUSHs (data);
997 PUSHs (sv_2mortal (newSVcacheint (NV_DIG)));
998 PUTBACK;
999 call_pv ("Convert::BER::XS::_encode_real_decimal", G_SCALAR);
1000 SPAGAIN;
842 1001
843 if (expect_false (SvRMAGICAL (rv))) 1002 SV *sv = POPs;
844 croak ("BER tuple must not be tied"); 1003 STRLEN l;
1004 char *f = SvPV (sv, l);
845 1005
846 if (expect_false (AvFILL ((AV *)rv) != BER_ARRAYSIZE - 1)) 1006 put_length (l);
847 croak ("BER tuple must contain exactly %d elements, not %d", BER_ARRAYSIZE, AvFILL ((AV *)rv) + 1); 1007 memcpy (cur, f, l);
1008 cur += l;
848 1009
849 return (AV *)rv; 1010 PUTBACK;
1011 FREETMPS;
1012 }
850} 1013}
851 1014
852static void 1015static void
853encode_ucs (SV *data, int chrsize) 1016encode_ucs (SV *data, int chrsize)
854{ 1017{
874 1037
875 *cur++ = uchr >> 8; 1038 *cur++ = uchr >> 8;
876 *cur++ = uchr; 1039 *cur++ = uchr;
877 } 1040 }
878} 1041}
1042
1043// check whether an SV is a BER tuple and returns its AV *
1044static AV *
1045ber_tuple (SV *tuple)
1046{
1047 SV *rv;
1048
1049 if (expect_false (!SvROK (tuple) || SvTYPE ((rv = SvRV (tuple))) != SVt_PVAV))
1050 croak ("BER tuple must be array-reference");
1051
1052 if (expect_false (SvRMAGICAL (rv)))
1053 croak ("BER tuple must not be tied");
1054
1055 if (expect_false (AvFILL ((AV *)rv) != BER_ARRAYSIZE - 1))
1056 croak ("BER tuple must contain exactly %d elements, not %d", BER_ARRAYSIZE, AvFILL ((AV *)rv) + 1);
1057
1058 return (AV *)rv;
1059}
1060
879static void 1061static void
880encode_ber (SV *tuple) 1062encode_ber (SV *tuple)
881{ 1063{
882 AV *av = ber_tuple (tuple); 1064 AV *av = ber_tuple (tuple);
883 1065
902 // and adjust later 1084 // and adjust later
903 need (1); 1085 need (1);
904 STRLEN mark = len_fixup_mark (); 1086 STRLEN mark = len_fixup_mark ();
905 1087
906 if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV)) 1088 if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV))
907 croak ("BER constructed data must be array-reference"); 1089 croak ("BER CONSTRUCTED data must be array-reference");
908 1090
909 AV *av = (AV *)SvRV (data); 1091 AV *av = (AV *)SvRV (data);
910 int fill = AvFILL (av); 1092 int fill = AvFILL (av);
911 1093
912 if (expect_false (SvRMAGICAL (av))) 1094 if (expect_false (SvRMAGICAL (av)))
913 croak ("BER constructed data must not be tied"); 1095 croak ("BER CONSTRUCTED data must not be tied");
914 1096
915 int i; 1097 int i;
916 for (i = 0; i <= fill; ++i) 1098 for (i = 0; i <= fill; ++i)
917 encode_ber (AvARRAY (av)[i]); 1099 encode_ber (AvARRAY (av)[i]);
918 1100
925 put_length (0); 1107 put_length (0);
926 break; 1108 break;
927 1109
928 case BER_TYPE_BOOL: 1110 case BER_TYPE_BOOL:
929 put_length (1); 1111 put_length (1);
930 *cur++ = SvTRUE (data) ? 0xff : 0x00; 1112 *cur++ = SvTRUE (data) ? 0xff : 0x00; // 0xff = DER/CER
931 break; 1113 break;
932 1114
933 case BER_TYPE_OID: 1115 case BER_TYPE_OID:
934 encode_oid (data, 0); 1116 encode_oid (data, 0);
935 break; 1117 break;
973 case BER_TYPE_UCS4: 1155 case BER_TYPE_UCS4:
974 encode_ucs (data, 4); 1156 encode_ucs (data, 4);
975 break; 1157 break;
976 1158
977 case BER_TYPE_REAL: 1159 case BER_TYPE_REAL:
1160 encode_real (data);
1161 break;
1162
978 case BER_TYPE_CROAK: 1163 case BER_TYPE_CROAK:
1164 croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag);
1165
979 default: 1166 default:
980 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 1167 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
981 } 1168 }
982 1169
983} 1170}
1055 const_iv (BER_TYPE_IPADDRESS) 1242 const_iv (BER_TYPE_IPADDRESS)
1056 const_iv (BER_TYPE_CROAK) 1243 const_iv (BER_TYPE_CROAK)
1057 1244
1058 const_iv (SNMP_IPADDRESS) 1245 const_iv (SNMP_IPADDRESS)
1059 const_iv (SNMP_COUNTER32) 1246 const_iv (SNMP_COUNTER32)
1247 const_iv (SNMP_GAUGE32)
1060 const_iv (SNMP_UNSIGNED32) 1248 const_iv (SNMP_UNSIGNED32)
1061 const_iv (SNMP_TIMETICKS) 1249 const_iv (SNMP_TIMETICKS)
1062 const_iv (SNMP_OPAQUE) 1250 const_iv (SNMP_OPAQUE)
1063 const_iv (SNMP_COUNTER64) 1251 const_iv (SNMP_COUNTER64)
1064 }; 1252 };
1065 1253
1066 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) 1254 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--)
1067 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); 1255 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv));
1068} 1256}
1069 1257
1070SV * 1258void
1071ber_decode (SV *ber, SV *profile = &PL_sv_undef) 1259ber_decode (SV *ber, SV *profile = &PL_sv_undef)
1260 ALIAS:
1261 ber_decode_prefix = 1
1072 CODE: 1262 PPCODE:
1073{ 1263{
1074 cur_profile = SvPROFILE (profile); 1264 cur_profile = SvPROFILE (profile);
1075 STRLEN len; 1265 STRLEN len;
1076 buf = (U8 *)SvPVbyte (ber, len); 1266 buf = (U8 *)SvPVbyte (ber, len);
1077 cur = buf; 1267 cur = buf;
1078 end = buf + len; 1268 end = buf + len;
1079 1269
1270 PUTBACK;
1080 RETVAL = decode_ber (); 1271 SV *tuple = decode_ber ();
1272 SPAGAIN;
1273
1274 EXTEND (SP, 2);
1275 PUSHs (sv_2mortal (tuple));
1276
1277 if (ix)
1278 PUSHs (sv_2mortal (newSViv (cur - buf)));
1279 else if (cur != end)
1280 error ("trailing garbage after BER value");
1081} 1281}
1082 OUTPUT: RETVAL
1083 1282
1084void 1283void
1085ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *flags = &PL_sv_undef, SV *data = &PL_sv_undef) 1284ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *flags = &PL_sv_undef, SV *data = &PL_sv_undef)
1086 PPCODE: 1285 PPCODE:
1087{ 1286{
1163 cur_profile = SvPROFILE (profile); 1362 cur_profile = SvPROFILE (profile);
1164 buf_sv = sv_2mortal (NEWSV (0, 256)); 1363 buf_sv = sv_2mortal (NEWSV (0, 256));
1165 SvPOK_only (buf_sv); 1364 SvPOK_only (buf_sv);
1166 set_buf (buf_sv); 1365 set_buf (buf_sv);
1167 1366
1367 PUTBACK;
1168 encode_ber (tuple); 1368 encode_ber (tuple);
1369 SPAGAIN;
1169 1370
1170 SvCUR_set (buf_sv, cur - buf); 1371 SvCUR_set (buf_sv, cur - buf);
1171 XPUSHs (buf_sv); 1372 XPUSHs (buf_sv);
1172} 1373}
1173 1374

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines