… | |
… | |
65 | SNMP_TIMETICKS = 0x03, |
65 | SNMP_TIMETICKS = 0x03, |
66 | SNMP_OPAQUE = 0x04, |
66 | SNMP_OPAQUE = 0x04, |
67 | SNMP_COUNTER64 = 0x06, |
67 | SNMP_COUNTER64 = 0x06, |
68 | }; |
68 | }; |
69 | |
69 | |
|
|
70 | // tlow-level types this module can ecode the above (and more) into |
70 | enum { |
71 | enum { |
71 | BER_TYPE_BYTES, |
72 | BER_TYPE_BYTES, |
72 | BER_TYPE_UTF8, |
73 | BER_TYPE_UTF8, |
73 | BER_TYPE_UCS2, |
74 | BER_TYPE_UCS2, |
74 | BER_TYPE_UCS4, |
75 | BER_TYPE_UCS4, |
… | |
… | |
80 | BER_TYPE_REAL, |
81 | BER_TYPE_REAL, |
81 | BER_TYPE_IPADDRESS, |
82 | BER_TYPE_IPADDRESS, |
82 | BER_TYPE_CROAK, |
83 | BER_TYPE_CROAK, |
83 | }; |
84 | }; |
84 | |
85 | |
|
|
86 | // tuple array indices |
85 | enum { |
87 | enum { |
86 | BER_CLASS = 0, |
88 | BER_CLASS = 0, |
87 | BER_TAG = 1, |
89 | BER_TAG = 1, |
88 | BER_FLAGS = 2, |
90 | BER_FLAGS = 2, |
89 | BER_DATA = 3, |
91 | BER_DATA = 3, |
… | |
… | |
265 | if (expect_false (res >> UVSIZE * 8 - 7)) |
267 | if (expect_false (res >> UVSIZE * 8 - 7)) |
266 | error ("BER variable length integer overflow"); |
268 | error ("BER variable length integer overflow"); |
267 | |
269 | |
268 | res = (res << 7) | (c & 0x7f); |
270 | res = (res << 7) | (c & 0x7f); |
269 | |
271 | |
270 | if (!(c & 0x80)) |
272 | if (expect_true (!(c & 0x80))) |
271 | return res; |
273 | return res; |
272 | |
274 | |
273 | c = get_u8 (); |
275 | c = get_u8 (); |
274 | } |
276 | } |
275 | } |
277 | } |
… | |
… | |
277 | static UV |
279 | static UV |
278 | get_length (void) |
280 | get_length (void) |
279 | { |
281 | { |
280 | UV res = get_u8 (); |
282 | UV res = get_u8 (); |
281 | |
283 | |
282 | if (res & 0x80) |
284 | if (expect_false (res & 0x80)) |
283 | { |
285 | { |
284 | int cnt = res & 0x7f; |
286 | U8 cnt = res & 0x7f; |
|
|
287 | |
|
|
288 | // this genewrates quite ugly code, but the overhead |
|
|
289 | // of copying the bytes for these lengths is probably so high |
|
|
290 | // that a slightly inefficient get_length won't matter. |
|
|
291 | |
|
|
292 | if (expect_false (cnt == 0)) |
|
|
293 | error ("indefinite BER value lengths not supported"); |
|
|
294 | |
|
|
295 | if (expect_false (cnt > UVSIZE)) |
|
|
296 | error ("BER value length too long (must fit into UV) or BER reserved value in length (X.690 8.1.3.5)"); |
|
|
297 | |
|
|
298 | want (cnt); |
|
|
299 | |
285 | res = 0; |
300 | res = 0; |
286 | |
301 | do |
287 | switch (cnt) |
302 | res = (res << 8) | *cur++; |
288 | { |
303 | 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 | } |
304 | } |
310 | |
305 | |
311 | return res; |
306 | return res; |
312 | } |
307 | } |
313 | |
308 | |
314 | static SV * |
309 | static SV * |
315 | decode_int (void) |
310 | decode_int (UV len) |
316 | { |
311 | { |
317 | UV len = get_length (); |
|
|
318 | |
|
|
319 | if (!len) |
312 | if (!len) |
320 | error ("invalid BER_TYPE_INT length zero (X.690 8.3.1)"); |
313 | error ("invalid BER_TYPE_INT length zero (X.690 8.3.1)"); |
321 | |
314 | |
322 | U8 *data = get_n (len); |
315 | U8 *data = get_n (len); |
323 | |
316 | |
… | |
… | |
344 | // but that's ok, as perl relies on it as well. |
337 | // but that's ok, as perl relies on it as well. |
345 | return negative ? newSViv ((IV)val) : newSVuv (val); |
338 | return negative ? newSViv ((IV)val) : newSVuv (val); |
346 | } |
339 | } |
347 | |
340 | |
348 | static SV * |
341 | static SV * |
349 | decode_data (void) |
342 | decode_data (UV len) |
350 | { |
343 | { |
351 | UV len = get_length (); |
|
|
352 | return newSVpvn ((char *)get_n (len), len); |
344 | return newSVpvn ((char *)get_n (len), len); |
353 | } |
345 | } |
354 | |
346 | |
355 | // helper for decode_object_identifier |
347 | // helper for decode_object_identifier |
356 | static char * |
348 | static char * |
… | |
… | |
384 | |
376 | |
385 | return buf; |
377 | return buf; |
386 | } |
378 | } |
387 | |
379 | |
388 | static SV * |
380 | static SV * |
389 | decode_oid (int relative) |
381 | decode_oid (UV len, int relative) |
390 | { |
382 | { |
391 | UV len = get_length (); |
|
|
392 | |
|
|
393 | if (len <= 0) |
383 | if (len <= 0) |
394 | { |
384 | { |
395 | error ("BER_TYPE_OID length must not be zero"); |
385 | error ("BER_TYPE_OID length must not be zero"); |
396 | return &PL_sv_undef; |
386 | return &PL_sv_undef; |
397 | } |
387 | } |
… | |
… | |
432 | return newSVpvn (oid, app - oid); |
422 | return newSVpvn (oid, app - oid); |
433 | } |
423 | } |
434 | |
424 | |
435 | // TODO: this is unacceptably slow |
425 | // TODO: this is unacceptably slow |
436 | static SV * |
426 | static SV * |
437 | decode_ucs (int chrsize) |
427 | decode_ucs (UV len, int chrsize) |
438 | { |
428 | { |
439 | SV *res = NEWSV (0, 0); |
|
|
440 | |
|
|
441 | UV len = get_length (); |
|
|
442 | |
|
|
443 | if (len & (chrsize - 1)) |
429 | if (len & (chrsize - 1)) |
444 | croak ("BER_TYPE_UCS has an invalid number of octets (%d)", len); |
430 | croak ("BER_TYPE_UCS has an invalid number of octets (%d)", len); |
|
|
431 | |
|
|
432 | SV *res = NEWSV (0, 0); |
445 | |
433 | |
446 | while (len) |
434 | while (len) |
447 | { |
435 | { |
448 | U8 b1 = get_u8 (); |
436 | U8 b1 = get_u8 (); |
449 | U8 b2 = get_u8 (); |
437 | U8 b2 = get_u8 (); |
… | |
… | |
489 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
477 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
490 | |
478 | |
491 | while (cur < buf + seqend) |
479 | while (cur < buf + seqend) |
492 | av_push (av, decode_ber ()); |
480 | av_push (av, decode_ber ()); |
493 | |
481 | |
494 | if (cur > buf + seqend) |
482 | if (expect_false (cur > buf + seqend)) |
495 | croak ("CONSTRUCTED type %02x length overflow (0x%x 0x%x)\n", identifier, (int)(cur - buf), (int)seqend); |
483 | croak ("CONSTRUCTED type %02x length overflow (0x%x 0x%x)\n", identifier, (int)(cur - buf), (int)seqend); |
496 | |
484 | |
497 | res = newRV_inc ((SV *)av); |
485 | res = newRV_inc ((SV *)av); |
498 | } |
486 | } |
499 | else |
487 | else |
|
|
488 | { |
|
|
489 | UV len = get_length (); |
|
|
490 | |
500 | switch (profile_lookup (cur_profile, klass, tag)) |
491 | switch (profile_lookup (cur_profile, klass, tag)) |
501 | { |
492 | { |
502 | case BER_TYPE_NULL: |
493 | case BER_TYPE_NULL: |
503 | { |
494 | if (expect_false (len)) |
504 | UV len = get_length (); |
|
|
505 | |
|
|
506 | if (len) |
|
|
507 | croak ("BER_TYPE_NULL value with non-zero length %d encountered (X.690 8.8.2)", len); |
495 | croak ("BER_TYPE_NULL value with non-zero length %d encountered (X.690 8.8.2)", len); |
508 | |
496 | |
509 | res = &PL_sv_undef; |
497 | res = &PL_sv_undef; |
510 | } |
|
|
511 | break; |
498 | break; |
512 | |
499 | |
513 | case BER_TYPE_BOOL: |
500 | case BER_TYPE_BOOL: |
514 | { |
|
|
515 | UV len = get_length (); |
|
|
516 | |
|
|
517 | if (len != 1) |
501 | if (expect_false (len != 1)) |
518 | croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered (X.690 8.2.1)", len); |
502 | croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered (X.690 8.2.1)", len); |
519 | |
503 | |
520 | res = newSVcacheint (!!get_u8 ()); |
504 | res = newSVcacheint (!!get_u8 ()); |
521 | } |
|
|
522 | break; |
505 | break; |
523 | |
506 | |
524 | case BER_TYPE_OID: |
507 | case BER_TYPE_OID: |
525 | res = decode_oid (0); |
508 | res = decode_oid (len, 0); |
526 | break; |
509 | break; |
527 | |
510 | |
528 | case BER_TYPE_RELOID: |
511 | case BER_TYPE_RELOID: |
529 | res = decode_oid (1); |
512 | res = decode_oid (len, 1); |
530 | break; |
513 | break; |
531 | |
514 | |
532 | case BER_TYPE_INT: |
515 | case BER_TYPE_INT: |
533 | res = decode_int (); |
516 | res = decode_int (len); |
534 | break; |
517 | break; |
535 | |
518 | |
536 | case BER_TYPE_UTF8: |
519 | case BER_TYPE_UTF8: |
537 | res = decode_data (); |
520 | res = decode_data (len); |
538 | SvUTF8_on (res); |
521 | SvUTF8_on (res); |
539 | break; |
522 | break; |
540 | |
523 | |
541 | case BER_TYPE_BYTES: |
524 | case BER_TYPE_BYTES: |
542 | res = decode_data (); |
525 | res = decode_data (len); |
543 | break; |
526 | break; |
544 | |
527 | |
545 | case BER_TYPE_IPADDRESS: |
528 | case BER_TYPE_IPADDRESS: |
546 | { |
529 | { |
547 | UV len = get_length (); |
|
|
548 | |
|
|
549 | if (len != 4) |
530 | if (len != 4) |
550 | croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered (RFC 2578 7.1.5)", len); |
531 | croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered (RFC 2578 7.1.5)", len); |
551 | |
532 | |
552 | U8 c1 = get_u8 (); |
533 | U8 *data = get_n (4); |
553 | U8 c2 = get_u8 (); |
534 | res = newSVpvf ("%d.%d.%d.%d", data [0], data [1], data [2], data [3]); |
554 | U8 c3 = get_u8 (); |
|
|
555 | U8 c4 = get_u8 (); |
|
|
556 | |
|
|
557 | res = newSVpvf ("%d.%d.%d.%d", c1, c2, c3, c4); |
|
|
558 | } |
535 | } |
559 | break; |
536 | break; |
560 | |
537 | |
561 | case BER_TYPE_UCS2: |
538 | case BER_TYPE_UCS2: |
562 | res = decode_ucs (2); |
539 | res = decode_ucs (len, 2); |
563 | break; |
540 | break; |
564 | |
541 | |
565 | case BER_TYPE_UCS4: |
542 | case BER_TYPE_UCS4: |
566 | res = decode_ucs (4); |
543 | res = decode_ucs (len, 4); |
567 | break; |
544 | break; |
568 | |
545 | |
569 | case BER_TYPE_REAL: |
546 | case BER_TYPE_REAL: |
570 | error ("BER_TYPE_REAL not implemented"); |
547 | error ("BER_TYPE_REAL not implemented"); |
571 | |
548 | |
572 | case BER_TYPE_CROAK: |
549 | case BER_TYPE_CROAK: |
573 | croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag); |
550 | croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag); |
574 | |
551 | |
575 | default: |
552 | default: |
576 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
553 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
577 | } |
554 | } |
|
|
555 | } |
578 | |
556 | |
579 | AV *av = newAV (); |
557 | AV *av = newAV (); |
580 | av_fill (av, BER_ARRAYSIZE - 1); |
558 | av_fill (av, BER_ARRAYSIZE - 1); |
581 | AvARRAY (av)[BER_CLASS] = newSVcacheint (klass); |
559 | AvARRAY (av)[BER_CLASS] = newSVcacheint (klass); |
582 | AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); |
560 | AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); |
… | |
… | |
669 | } |
647 | } |
670 | |
648 | |
671 | static U8 * |
649 | static U8 * |
672 | put_length_at (UV val, U8 *cur) |
650 | put_length_at (UV val, U8 *cur) |
673 | { |
651 | { |
674 | if (val < 0x7fU) |
652 | if (val <= 0x7fU) |
675 | *cur++ = val; |
653 | *cur++ = val; |
676 | else |
654 | else |
677 | { |
655 | { |
678 | U8 *lenb = cur++; |
656 | U8 *lenb = cur++; |
679 | |
657 | |
… | |
… | |
695 | } |
673 | } |
696 | |
674 | |
697 | static void |
675 | static void |
698 | put_length (UV val) |
676 | put_length (UV val) |
699 | { |
677 | { |
700 | need (5 + val); |
678 | need (9 + val); |
701 | cur = put_length_at (val, cur); |
679 | cur = put_length_at (val, cur); |
702 | } |
680 | } |
703 | |
681 | |
704 | // return how many bytes the encoded length requires |
682 | // return how many bytes the encoded length requires |
705 | static int length_length (UV val) |
683 | static int length_length (UV val) |
706 | { |
684 | { |
707 | return val < 0x7fU |
685 | // use hashing with a DeBruin sequence, anyone? |
|
|
686 | return expect_true (val <= 0x7fU) |
708 | ? 1 |
687 | ? 1 |
709 | : 2 |
688 | : 2 |
710 | + (val > 0xffU) |
689 | + (val > 0x000000000000ffU) |
711 | + (val > 0xffffU) |
690 | + (val > 0x0000000000ffffU) |
712 | + (val > 0xffffffU) |
691 | + (val > 0x00000000ffffffU) |
713 | #if UVSIZE > 4 |
692 | #if UVSIZE > 4 |
714 | + (val > 0xffffffffU) |
693 | + (val > 0x000000ffffffffU) |
715 | + (val > 0xffffffffffU) |
694 | + (val > 0x0000ffffffffffU) |
716 | + (val > 0xffffffffffffU) |
695 | + (val > 0x00ffffffffffffU) |
717 | + (val > 0xffffffffffffffU) |
696 | + (val > 0xffffffffffffffU) |
718 | #endif |
697 | #endif |
719 | ; |
698 | ; |
720 | } |
699 | } |
721 | |
700 | |