… | |
… | |
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 | } |
… | |
… | |
402 | static char oid[MAX_OID_STRLEN]; // static, because too large for stack |
392 | static char oid[MAX_OID_STRLEN]; // static, because too large for stack |
403 | char *app = oid; |
393 | char *app = oid; |
404 | |
394 | |
405 | if (relative) |
395 | if (relative) |
406 | app = write_uv (app, w); |
396 | app = write_uv (app, w); |
|
|
397 | else |
|
|
398 | { |
|
|
399 | UV w1, w2; |
|
|
400 | |
407 | else if (w < 2 * 40) |
401 | if (w < 2 * 40) |
408 | { |
402 | (w1 = w / 40), (w2 = w % 40); |
|
|
403 | else |
|
|
404 | (w1 = 2), (w2 = w - 2 * 40); |
|
|
405 | |
409 | app = write_uv (app, (U8)w / 40); |
406 | app = write_uv (app, w1); |
410 | *app++ = '.'; |
407 | *app++ = '.'; |
411 | app = write_uv (app, (U8)w % 40); |
|
|
412 | } |
|
|
413 | else |
|
|
414 | { |
|
|
415 | app = write_uv (app, 2); |
408 | app = write_uv (app, w2); |
416 | *app++ = '.'; |
|
|
417 | app = write_uv (app, w - 2 * 40); |
|
|
418 | } |
409 | } |
419 | |
410 | |
420 | while (cur < end) |
411 | while (cur < end) |
421 | { |
412 | { |
422 | // we assume an oid component is never > 64 digits |
413 | // we assume an oid component is never > 64 digits |
… | |
… | |
431 | return newSVpvn (oid, app - oid); |
422 | return newSVpvn (oid, app - oid); |
432 | } |
423 | } |
433 | |
424 | |
434 | // TODO: this is unacceptably slow |
425 | // TODO: this is unacceptably slow |
435 | static SV * |
426 | static SV * |
436 | decode_ucs (int chrsize) |
427 | decode_ucs (UV len, int chrsize) |
437 | { |
428 | { |
438 | SV *res = NEWSV (0, 0); |
|
|
439 | |
|
|
440 | UV len = get_length (); |
|
|
441 | |
|
|
442 | if (len & (chrsize - 1)) |
429 | if (len & (chrsize - 1)) |
443 | 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); |
444 | |
433 | |
445 | while (len) |
434 | while (len) |
446 | { |
435 | { |
447 | U8 b1 = get_u8 (); |
436 | U8 b1 = get_u8 (); |
448 | U8 b2 = get_u8 (); |
437 | U8 b2 = get_u8 (); |
… | |
… | |
488 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
477 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
489 | |
478 | |
490 | while (cur < buf + seqend) |
479 | while (cur < buf + seqend) |
491 | av_push (av, decode_ber ()); |
480 | av_push (av, decode_ber ()); |
492 | |
481 | |
493 | if (cur > buf + seqend) |
482 | if (expect_false (cur > buf + seqend)) |
494 | 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); |
495 | |
484 | |
496 | res = newRV_inc ((SV *)av); |
485 | res = newRV_inc ((SV *)av); |
497 | } |
486 | } |
498 | else |
487 | else |
|
|
488 | { |
|
|
489 | UV len = get_length (); |
|
|
490 | |
499 | switch (profile_lookup (cur_profile, klass, tag)) |
491 | switch (profile_lookup (cur_profile, klass, tag)) |
500 | { |
492 | { |
501 | case BER_TYPE_NULL: |
493 | case BER_TYPE_NULL: |
502 | { |
494 | if (expect_false (len)) |
503 | UV len = get_length (); |
|
|
504 | |
|
|
505 | if (len) |
|
|
506 | 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); |
507 | |
496 | |
508 | res = &PL_sv_undef; |
497 | res = &PL_sv_undef; |
509 | } |
|
|
510 | break; |
498 | break; |
511 | |
499 | |
512 | case BER_TYPE_BOOL: |
500 | case BER_TYPE_BOOL: |
513 | { |
|
|
514 | UV len = get_length (); |
|
|
515 | |
|
|
516 | if (len != 1) |
501 | if (expect_false (len != 1)) |
517 | 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); |
518 | |
503 | |
519 | res = newSVcacheint (!!get_u8 ()); |
504 | res = newSVcacheint (!!get_u8 ()); |
520 | } |
|
|
521 | break; |
505 | break; |
522 | |
506 | |
523 | case BER_TYPE_OID: |
507 | case BER_TYPE_OID: |
524 | res = decode_oid (0); |
508 | res = decode_oid (len, 0); |
525 | break; |
509 | break; |
526 | |
510 | |
527 | case BER_TYPE_RELOID: |
511 | case BER_TYPE_RELOID: |
528 | res = decode_oid (1); |
512 | res = decode_oid (len, 1); |
529 | break; |
513 | break; |
530 | |
514 | |
531 | case BER_TYPE_INT: |
515 | case BER_TYPE_INT: |
532 | res = decode_int (); |
516 | res = decode_int (len); |
533 | break; |
517 | break; |
534 | |
518 | |
535 | case BER_TYPE_UTF8: |
519 | case BER_TYPE_UTF8: |
536 | res = decode_data (); |
520 | res = decode_data (len); |
537 | SvUTF8_on (res); |
521 | SvUTF8_on (res); |
538 | break; |
522 | break; |
539 | |
523 | |
540 | case BER_TYPE_BYTES: |
524 | case BER_TYPE_BYTES: |
541 | res = decode_data (); |
525 | res = decode_data (len); |
542 | break; |
526 | break; |
543 | |
527 | |
544 | case BER_TYPE_IPADDRESS: |
528 | case BER_TYPE_IPADDRESS: |
545 | { |
529 | { |
546 | UV len = get_length (); |
|
|
547 | |
|
|
548 | if (len != 4) |
530 | if (len != 4) |
549 | 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); |
550 | |
532 | |
551 | U8 c1 = get_u8 (); |
533 | U8 *data = get_n (4); |
552 | U8 c2 = get_u8 (); |
534 | 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 | } |
535 | } |
558 | break; |
536 | break; |
559 | |
537 | |
560 | case BER_TYPE_UCS2: |
538 | case BER_TYPE_UCS2: |
561 | res = decode_ucs (2); |
539 | res = decode_ucs (len, 2); |
562 | break; |
540 | break; |
563 | |
541 | |
564 | case BER_TYPE_UCS4: |
542 | case BER_TYPE_UCS4: |
565 | res = decode_ucs (4); |
543 | res = decode_ucs (len, 4); |
566 | break; |
544 | break; |
567 | |
545 | |
568 | case BER_TYPE_REAL: |
546 | case BER_TYPE_REAL: |
569 | error ("BER_TYPE_REAL not implemented"); |
547 | error ("BER_TYPE_REAL not implemented"); |
570 | |
548 | |
571 | case BER_TYPE_CROAK: |
549 | case BER_TYPE_CROAK: |
572 | 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); |
573 | |
551 | |
574 | default: |
552 | default: |
575 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
553 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
576 | } |
554 | } |
|
|
555 | } |
577 | |
556 | |
578 | AV *av = newAV (); |
557 | AV *av = newAV (); |
579 | av_fill (av, BER_ARRAYSIZE - 1); |
558 | av_fill (av, BER_ARRAYSIZE - 1); |
580 | AvARRAY (av)[BER_CLASS] = newSVcacheint (klass); |
559 | AvARRAY (av)[BER_CLASS] = newSVcacheint (klass); |
581 | AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); |
560 | AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); |
… | |
… | |
668 | } |
647 | } |
669 | |
648 | |
670 | static U8 * |
649 | static U8 * |
671 | put_length_at (UV val, U8 *cur) |
650 | put_length_at (UV val, U8 *cur) |
672 | { |
651 | { |
673 | if (val < 0x7fU) |
652 | if (val <= 0x7fU) |
674 | *cur++ = val; |
653 | *cur++ = val; |
675 | else |
654 | else |
676 | { |
655 | { |
677 | U8 *lenb = cur++; |
656 | U8 *lenb = cur++; |
678 | |
657 | |
679 | #if UVSIZE > 4 |
658 | #if UVSIZE > 4 |
680 | *cur = val >> 56; cur += *cur > 0; |
659 | *cur = val >> 56; cur += val >= ((UV)1 << (8 * 7)); |
681 | *cur = val >> 48; cur += *cur > 0; |
660 | *cur = val >> 48; cur += val >= ((UV)1 << (8 * 6)); |
682 | *cur = val >> 40; cur += *cur > 0; |
661 | *cur = val >> 40; cur += val >= ((UV)1 << (8 * 5)); |
683 | *cur = val >> 32; cur += *cur > 0; |
662 | *cur = val >> 32; cur += val >= ((UV)1 << (8 * 4)); |
684 | #endif |
663 | #endif |
685 | *cur = val >> 24; cur += *cur > 0; |
664 | *cur = val >> 24; cur += val >= ((UV)1 << (8 * 3)); |
686 | *cur = val >> 16; cur += *cur > 0; |
665 | *cur = val >> 16; cur += val >= ((UV)1 << (8 * 2)); |
687 | *cur = val >> 8; cur += *cur > 0; |
666 | *cur = val >> 8; cur += val >= ((UV)1 << (8 * 1)); |
688 | *cur = val ; cur += 1; |
667 | *cur = val ; cur += 1; |
689 | |
668 | |
690 | *lenb = 0x80 + cur - lenb - 1; |
669 | *lenb = 0x80 + cur - lenb - 1; |
691 | } |
670 | } |
692 | |
671 | |
… | |
… | |
694 | } |
673 | } |
695 | |
674 | |
696 | static void |
675 | static void |
697 | put_length (UV val) |
676 | put_length (UV val) |
698 | { |
677 | { |
699 | need (5 + val); |
678 | need (9 + val); |
700 | cur = put_length_at (val, cur); |
679 | cur = put_length_at (val, cur); |
701 | } |
680 | } |
702 | |
681 | |
703 | // return how many bytes the encoded length requires |
682 | // return how many bytes the encoded length requires |
704 | static int length_length (UV val) |
683 | static int length_length (UV val) |
705 | { |
684 | { |
706 | return val < 0x7fU |
685 | // use hashing with a DeBruin sequence, anyone? |
|
|
686 | return expect_true (val <= 0x7fU) |
707 | ? 1 |
687 | ? 1 |
708 | : 2 |
688 | : 2 |
709 | + (val > 0xffU) |
689 | + (val > 0x000000000000ffU) |
710 | + (val > 0xffffU) |
690 | + (val > 0x0000000000ffffU) |
711 | + (val > 0xffffffU) |
691 | + (val > 0x00000000ffffffU) |
712 | #if UVSIZE > 4 |
692 | #if UVSIZE > 4 |
713 | + (val > 0xffffffffU) |
693 | + (val > 0x000000ffffffffU) |
714 | + (val > 0xffffffffffU) |
694 | + (val > 0x0000ffffffffffU) |
715 | + (val > 0xffffffffffffU) |
695 | + (val > 0x00ffffffffffffU) |
716 | + (val > 0xffffffffffffffU) |
696 | + (val > 0xffffffffffffffU) |
717 | #endif |
697 | #endif |
718 | ; |
698 | ; |
719 | } |
699 | } |
720 | |
700 | |