… | |
… | |
58 | ASN_CLASS_SHIFT = 6, |
58 | ASN_CLASS_SHIFT = 6, |
59 | |
59 | |
60 | // ASN_APPLICATION SNMP |
60 | // ASN_APPLICATION SNMP |
61 | SNMP_IPADDRESS = 0x00, |
61 | SNMP_IPADDRESS = 0x00, |
62 | SNMP_COUNTER32 = 0x01, |
62 | SNMP_COUNTER32 = 0x01, |
|
|
63 | SNMP_GAUGE32 = 0x02, |
63 | SNMP_UNSIGNED32 = 0x02, |
64 | SNMP_UNSIGNED32 = 0x02, |
64 | SNMP_TIMETICKS = 0x03, |
65 | SNMP_TIMETICKS = 0x03, |
65 | SNMP_OPAQUE = 0x04, |
66 | SNMP_OPAQUE = 0x04, |
66 | SNMP_COUNTER64 = 0x06, |
67 | SNMP_COUNTER64 = 0x06, |
67 | }; |
68 | }; |
… | |
… | |
252 | // get ber-encoded integer (i.e. pack "w") |
253 | // get ber-encoded integer (i.e. pack "w") |
253 | static UV |
254 | static UV |
254 | get_w (void) |
255 | get_w (void) |
255 | { |
256 | { |
256 | UV res = 0; |
257 | UV res = 0; |
|
|
258 | U8 c = get_u8 (); |
|
|
259 | |
|
|
260 | if (expect_false (c == 0x80)) |
|
|
261 | error ("illegal BER padding (X.690 8.1.2.4.2, 8.19.2)"); |
257 | |
262 | |
258 | for (;;) |
263 | for (;;) |
259 | { |
264 | { |
260 | U8 c = get_u8 (); |
|
|
261 | res = (res << 7) | (c & 0x7f); |
265 | res = (res << 7) | (c & 0x7f); |
262 | |
266 | |
263 | if (!(c & 0x80)) |
267 | if (!(c & 0x80)) |
264 | return res; |
268 | return res; |
|
|
269 | |
|
|
270 | c = get_u8 (); |
265 | } |
271 | } |
266 | } |
272 | } |
267 | |
273 | |
268 | static UV |
274 | static UV |
269 | get_length (void) |
275 | get_length (void) |
… | |
… | |
277 | |
283 | |
278 | switch (cnt) |
284 | switch (cnt) |
279 | { |
285 | { |
280 | case 0: |
286 | case 0: |
281 | error ("indefinite ASN.1 lengths not supported"); |
287 | error ("indefinite ASN.1 lengths not supported"); |
282 | return 0; |
|
|
283 | |
288 | |
284 | //case 0x80: // indefinite length |
289 | case 0x7f: |
|
|
290 | error ("ASN.1 reserved value in length (X.690 8.1.3.5)"); |
285 | |
291 | |
286 | //case 0xff: reserved |
|
|
287 | default: |
292 | default: |
288 | error ("ASN.1 length too long"); |
293 | error ("ASN.1 length too long (only up to 2**64 octets supported)"); |
289 | return 0; |
|
|
290 | |
294 | |
291 | case 8: res = (res << 8) | get_u8 (); |
295 | case 8: res = (res << 8) | get_u8 (); |
292 | case 7: res = (res << 8) | get_u8 (); |
296 | case 7: res = (res << 8) | get_u8 (); |
293 | case 6: res = (res << 8) | get_u8 (); |
297 | case 6: res = (res << 8) | get_u8 (); |
294 | case 5: res = (res << 8) | get_u8 (); |
298 | case 5: res = (res << 8) | get_u8 (); |
… | |
… | |
306 | decode_int (void) |
310 | decode_int (void) |
307 | { |
311 | { |
308 | UV len = get_length (); |
312 | UV len = get_length (); |
309 | |
313 | |
310 | if (!len) |
314 | if (!len) |
311 | { |
|
|
312 | error ("invalid integer length equal to zero"); |
315 | error ("invalid integer length equal to zero (X.690 8.3.1)"); |
313 | return 0; |
|
|
314 | } |
|
|
315 | |
316 | |
316 | U8 *data = get_n (len); |
317 | U8 *data = get_n (len); |
|
|
318 | |
|
|
319 | if (expect_false (len > 1)) |
|
|
320 | { |
|
|
321 | U16 mask = (data [0] << 8) | data [1] & 0xff80; |
|
|
322 | |
|
|
323 | if (expect_false (mask == 0xff80 || mask == 0x0000)) |
|
|
324 | error ("illegal padding in integer (X.690 8.3.2)"); |
|
|
325 | } |
317 | |
326 | |
318 | int negative = data [0] & 0x80; |
327 | int negative = data [0] & 0x80; |
319 | |
328 | |
320 | UV val = negative ? -1 : 0; // copy signbit to all bits |
329 | UV val = negative ? -1 : 0; // copy signbit to all bits |
321 | |
330 | |
… | |
… | |
462 | int tag = identifier & ASN_TAG_MASK; |
471 | int tag = identifier & ASN_TAG_MASK; |
463 | |
472 | |
464 | if (tag == ASN_TAG_BER) |
473 | if (tag == ASN_TAG_BER) |
465 | tag = get_w (); |
474 | tag = get_w (); |
466 | |
475 | |
467 | if (tag == ASN_TAG_BER) |
|
|
468 | tag = get_w (); |
|
|
469 | |
|
|
470 | if (constructed) |
476 | if (constructed) |
471 | { |
477 | { |
472 | UV len = get_length (); |
478 | UV len = get_length (); |
473 | UV seqend = (cur - buf) + len; |
479 | UV seqend = (cur - buf) + len; |
474 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
480 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
475 | |
481 | |
476 | while (cur < buf + seqend) |
482 | while (cur < buf + seqend) |
477 | av_push (av, decode_ber ()); |
483 | av_push (av, decode_ber ()); |
478 | |
484 | |
479 | if (cur > buf + seqend) |
485 | if (cur > buf + seqend) |
480 | croak ("constructed type %02x overflow (%x %x)\n", identifier, cur - buf, seqend); |
486 | croak ("constructed type %02x length overflow (0x%x 0x%x)\n", identifier, (int)(cur - buf), (int)seqend); |
481 | |
487 | |
482 | res = newRV_inc ((SV *)av); |
488 | res = newRV_inc ((SV *)av); |
483 | } |
489 | } |
484 | else |
490 | else |
485 | switch (profile_lookup (cur_profile, klass, tag)) |
491 | switch (profile_lookup (cur_profile, klass, tag)) |
… | |
… | |
487 | case BER_TYPE_NULL: |
493 | case BER_TYPE_NULL: |
488 | { |
494 | { |
489 | UV len = get_length (); |
495 | UV len = get_length (); |
490 | |
496 | |
491 | if (len) |
497 | if (len) |
492 | croak ("BER_TYPE_NULL value with non-zero length %d encountered", len); |
498 | croak ("BER_TYPE_NULL value with non-zero length %d encountered (X.690 8.8.2)", len); |
493 | |
499 | |
494 | res = &PL_sv_undef; |
500 | res = &PL_sv_undef; |
495 | } |
501 | } |
496 | break; |
502 | break; |
497 | |
503 | |
498 | case BER_TYPE_BOOL: |
504 | case BER_TYPE_BOOL: |
499 | { |
505 | { |
500 | UV len = get_length (); |
506 | UV len = get_length (); |
501 | |
507 | |
502 | if (len != 1) |
508 | if (len != 1) |
503 | croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered", len); |
509 | croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered (X.690 8.2.1)", len); |
504 | |
510 | |
505 | res = newSVcacheint (!!get_u8 ()); |
511 | res = newSVcacheint (!!get_u8 ()); |
506 | } |
512 | } |
507 | break; |
513 | break; |
508 | |
514 | |
… | |
… | |
530 | case BER_TYPE_IPADDRESS: |
536 | case BER_TYPE_IPADDRESS: |
531 | { |
537 | { |
532 | UV len = get_length (); |
538 | UV len = get_length (); |
533 | |
539 | |
534 | if (len != 4) |
540 | if (len != 4) |
535 | croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered", len); |
541 | croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered (RFC 2578 7.1.5)", len); |
536 | |
542 | |
537 | U8 c1 = get_u8 (); |
543 | U8 c1 = get_u8 (); |
538 | U8 c2 = get_u8 (); |
544 | U8 c2 = get_u8 (); |
539 | U8 c3 = get_u8 (); |
545 | U8 c3 = get_u8 (); |
540 | U8 c4 = get_u8 (); |
546 | U8 c4 = get_u8 (); |
… | |
… | |
575 | strlen_sum (STRLEN l1, STRLEN l2) |
581 | strlen_sum (STRLEN l1, STRLEN l2) |
576 | { |
582 | { |
577 | size_t sum = l1 + l2; |
583 | size_t sum = l1 + l2; |
578 | |
584 | |
579 | if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) |
585 | if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) |
580 | croak ("JSON::XS: string size overflow"); |
586 | croak ("Convert::BER::XS: string size overflow"); |
581 | |
587 | |
582 | return sum; |
588 | return sum; |
583 | } |
589 | } |
584 | |
590 | |
585 | static void |
591 | static void |
… | |
… | |
1064 | const_iv (BER_TYPE_IPADDRESS) |
1070 | const_iv (BER_TYPE_IPADDRESS) |
1065 | const_iv (BER_TYPE_CROAK) |
1071 | const_iv (BER_TYPE_CROAK) |
1066 | |
1072 | |
1067 | const_iv (SNMP_IPADDRESS) |
1073 | const_iv (SNMP_IPADDRESS) |
1068 | const_iv (SNMP_COUNTER32) |
1074 | const_iv (SNMP_COUNTER32) |
|
|
1075 | const_iv (SNMP_GAUGE32) |
1069 | const_iv (SNMP_UNSIGNED32) |
1076 | const_iv (SNMP_UNSIGNED32) |
1070 | const_iv (SNMP_TIMETICKS) |
1077 | const_iv (SNMP_TIMETICKS) |
1071 | const_iv (SNMP_OPAQUE) |
1078 | const_iv (SNMP_OPAQUE) |
1072 | const_iv (SNMP_COUNTER64) |
1079 | const_iv (SNMP_COUNTER64) |
1073 | }; |
1080 | }; |
1074 | |
1081 | |
1075 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) |
1082 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) |
1076 | newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); |
1083 | newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); |
1077 | } |
1084 | } |
1078 | |
1085 | |
1079 | SV * |
1086 | void |
1080 | ber_decode (SV *ber, SV *profile = &PL_sv_undef) |
1087 | ber_decode (SV *ber, SV *profile = &PL_sv_undef) |
|
|
1088 | ALIAS: |
|
|
1089 | ber_decode_prefix = 1 |
1081 | CODE: |
1090 | PPCODE: |
1082 | { |
1091 | { |
1083 | cur_profile = SvPROFILE (profile); |
1092 | cur_profile = SvPROFILE (profile); |
1084 | STRLEN len; |
1093 | STRLEN len; |
1085 | buf = (U8 *)SvPVbyte (ber, len); |
1094 | buf = (U8 *)SvPVbyte (ber, len); |
1086 | cur = buf; |
1095 | cur = buf; |
1087 | end = buf + len; |
1096 | end = buf + len; |
1088 | |
1097 | |
1089 | RETVAL = decode_ber (); |
1098 | SV *tuple = decode_ber (); |
|
|
1099 | |
|
|
1100 | EXTEND (SP, 2); |
|
|
1101 | PUSHs (sv_2mortal (tuple)); |
|
|
1102 | |
|
|
1103 | if (ix) |
|
|
1104 | PUSHs (sv_2mortal (newSViv (cur - buf))); |
|
|
1105 | else if (cur != end) |
|
|
1106 | error ("trailing garbage after BER data"); |
1090 | } |
1107 | } |
1091 | OUTPUT: RETVAL |
|
|
1092 | |
1108 | |
1093 | void |
1109 | void |
1094 | ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *flags = &PL_sv_undef, SV *data = &PL_sv_undef) |
1110 | ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *flags = &PL_sv_undef, SV *data = &PL_sv_undef) |
1095 | PPCODE: |
1111 | PPCODE: |
1096 | { |
1112 | { |