… | |
… | |
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 | }; |
68 | |
69 | |
|
|
70 | // tlow-level types this module can ecode the above (and more) into |
69 | enum { |
71 | enum { |
70 | BER_TYPE_BYTES, |
72 | BER_TYPE_BYTES, |
71 | BER_TYPE_UTF8, |
73 | BER_TYPE_UTF8, |
72 | BER_TYPE_UCS2, |
74 | BER_TYPE_UCS2, |
73 | BER_TYPE_UCS4, |
75 | BER_TYPE_UCS4, |
… | |
… | |
79 | BER_TYPE_REAL, |
81 | BER_TYPE_REAL, |
80 | BER_TYPE_IPADDRESS, |
82 | BER_TYPE_IPADDRESS, |
81 | BER_TYPE_CROAK, |
83 | BER_TYPE_CROAK, |
82 | }; |
84 | }; |
83 | |
85 | |
|
|
86 | // tuple array indices |
84 | enum { |
87 | enum { |
85 | BER_CLASS = 0, |
88 | BER_CLASS = 0, |
86 | BER_TAG = 1, |
89 | BER_TAG = 1, |
87 | BER_FLAGS = 2, |
90 | BER_FLAGS = 2, |
88 | BER_DATA = 3, |
91 | BER_DATA = 3, |
… | |
… | |
140 | { |
143 | { |
141 | if (!SvOK (profile)) |
144 | if (!SvOK (profile)) |
142 | return default_profile; |
145 | return default_profile; |
143 | |
146 | |
144 | if (!SvROK (profile)) |
147 | if (!SvROK (profile)) |
145 | croak ("invalid profile"); |
148 | croak ("Convert::BER::XS::Profile expected"); |
146 | |
149 | |
147 | profile = SvRV (profile); |
150 | profile = SvRV (profile); |
148 | |
151 | |
149 | if (SvSTASH (profile) != profile_stash) |
152 | if (SvSTASH (profile) != profile_stash) |
150 | croak ("invalid profile object"); |
153 | croak ("Convert::BER::XS::Profile expected"); |
151 | |
154 | |
152 | return (void *)profile; |
155 | return (void *)profile; |
153 | } |
156 | } |
154 | |
157 | |
155 | static int |
158 | static int |
… | |
… | |
227 | } |
230 | } |
228 | |
231 | |
229 | // get_* functions fetch something from the buffer |
232 | // get_* functions fetch something from the buffer |
230 | // decode_* functions use get_* fun ctions to decode ber values |
233 | // decode_* functions use get_* fun ctions to decode ber values |
231 | |
234 | |
|
|
235 | // get single octet |
|
|
236 | static U8 |
|
|
237 | get_u8 (void) |
|
|
238 | { |
|
|
239 | if (cur == end) |
|
|
240 | error ("unexpected end of message buffer"); |
|
|
241 | |
|
|
242 | return *cur++; |
|
|
243 | } |
|
|
244 | |
232 | // get n octets |
245 | // get n octets |
233 | static U8 * |
246 | static U8 * |
234 | get_n (UV count) |
247 | get_n (UV count) |
235 | { |
248 | { |
236 | want (count); |
249 | want (count); |
237 | U8 *res = cur; |
250 | U8 *res = cur; |
238 | cur += count; |
251 | cur += count; |
239 | return res; |
252 | return res; |
240 | } |
253 | } |
241 | |
254 | |
242 | // get single octet |
|
|
243 | static U8 |
|
|
244 | get_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") |
255 | // get ber-encoded integer (i.e. pack "w") |
253 | static UV |
256 | static UV |
254 | get_w (void) |
257 | get_w (void) |
255 | { |
258 | { |
256 | UV res = 0; |
259 | UV res = 0; |
… | |
… | |
259 | if (expect_false (c == 0x80)) |
262 | if (expect_false (c == 0x80)) |
260 | error ("illegal BER padding (X.690 8.1.2.4.2, 8.19.2)"); |
263 | error ("illegal BER padding (X.690 8.1.2.4.2, 8.19.2)"); |
261 | |
264 | |
262 | for (;;) |
265 | for (;;) |
263 | { |
266 | { |
|
|
267 | if (expect_false (res >> UVSIZE * 8 - 7)) |
|
|
268 | error ("BER variable length integer overflow"); |
|
|
269 | |
264 | res = (res << 7) | (c & 0x7f); |
270 | res = (res << 7) | (c & 0x7f); |
265 | |
271 | |
266 | if (!(c & 0x80)) |
272 | if (expect_true (!(c & 0x80))) |
267 | return res; |
273 | return res; |
268 | |
274 | |
269 | c = get_u8 (); |
275 | c = get_u8 (); |
270 | } |
276 | } |
271 | } |
277 | } |
… | |
… | |
273 | static UV |
279 | static UV |
274 | get_length (void) |
280 | get_length (void) |
275 | { |
281 | { |
276 | UV res = get_u8 (); |
282 | UV res = get_u8 (); |
277 | |
283 | |
278 | if (res & 0x80) |
284 | if (expect_false (res & 0x80)) |
279 | { |
285 | { |
280 | 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 | |
281 | res = 0; |
300 | res = 0; |
282 | |
301 | do |
283 | switch (cnt) |
302 | res = (res << 8) | *cur++; |
284 | { |
303 | while (--cnt); |
285 | case 0: |
|
|
286 | error ("indefinite ASN.1 lengths not supported"); |
|
|
287 | |
|
|
288 | case 0x7f: |
|
|
289 | error ("ASN.1 reserved value in length (X.690 8.1.3.5)"); |
|
|
290 | |
|
|
291 | default: |
|
|
292 | error ("ASN.1 length too long (only up to 2**64 octets supported)"); |
|
|
293 | |
|
|
294 | case 8: res = (res << 8) | get_u8 (); |
|
|
295 | case 7: res = (res << 8) | get_u8 (); |
|
|
296 | case 6: res = (res << 8) | get_u8 (); |
|
|
297 | case 5: res = (res << 8) | get_u8 (); |
|
|
298 | case 4: res = (res << 8) | get_u8 (); |
|
|
299 | case 3: res = (res << 8) | get_u8 (); |
|
|
300 | case 2: res = (res << 8) | get_u8 (); |
|
|
301 | case 1: res = (res << 8) | get_u8 (); |
|
|
302 | } |
|
|
303 | } |
304 | } |
304 | |
305 | |
305 | return res; |
306 | return res; |
306 | } |
307 | } |
307 | |
308 | |
308 | static SV * |
309 | static SV * |
309 | decode_int (void) |
310 | decode_int (UV len) |
310 | { |
311 | { |
311 | UV len = get_length (); |
|
|
312 | |
|
|
313 | if (!len) |
312 | if (!len) |
314 | error ("invalid integer length equal to zero (X.690 8.3.1)"); |
313 | error ("invalid BER_TYPE_INT length zero (X.690 8.3.1)"); |
315 | |
314 | |
316 | U8 *data = get_n (len); |
315 | U8 *data = get_n (len); |
317 | |
316 | |
318 | if (expect_false (len > 1)) |
317 | if (expect_false (len > 1)) |
319 | { |
318 | { |
320 | U16 mask = (data [0] << 8) | data [1] & 0xff80; |
319 | U16 mask = (data [0] << 8) | data [1] & 0xff80; |
321 | |
320 | |
322 | if (expect_false (mask == 0xff80 || mask == 0x0000)) |
321 | if (expect_false (mask == 0xff80 || mask == 0x0000)) |
323 | error ("illegal padding in integer (X.690 8.3.2)"); |
322 | error ("illegal padding in BER_TYPE_INT (X.690 8.3.2)"); |
324 | } |
323 | } |
325 | |
324 | |
326 | int negative = data [0] & 0x80; |
325 | int negative = data [0] & 0x80; |
327 | |
326 | |
328 | UV val = negative ? -1 : 0; // copy signbit to all bits |
327 | UV val = negative ? -1 : 0; // copy signbit to all bits |
|
|
328 | |
|
|
329 | if (len > UVSIZE + (!negative && !*data)) |
|
|
330 | error ("BER_TYPE_INT overflow"); |
329 | |
331 | |
330 | do |
332 | do |
331 | val = (val << 8) | *data++; |
333 | val = (val << 8) | *data++; |
332 | while (--len); |
334 | while (--len); |
333 | |
335 | |
… | |
… | |
335 | // but that's ok, as perl relies on it as well. |
337 | // but that's ok, as perl relies on it as well. |
336 | return negative ? newSViv ((IV)val) : newSVuv (val); |
338 | return negative ? newSViv ((IV)val) : newSVuv (val); |
337 | } |
339 | } |
338 | |
340 | |
339 | static SV * |
341 | static SV * |
340 | decode_data (void) |
342 | decode_data (UV len) |
341 | { |
343 | { |
342 | UV len = get_length (); |
|
|
343 | return newSVpvn ((char *)get_n (len), len); |
344 | return newSVpvn ((char *)get_n (len), len); |
344 | } |
345 | } |
345 | |
346 | |
346 | // helper for decode_object_identifier |
347 | // helper for decode_object_identifier |
347 | static char * |
348 | static char * |
… | |
… | |
375 | |
376 | |
376 | return buf; |
377 | return buf; |
377 | } |
378 | } |
378 | |
379 | |
379 | static SV * |
380 | static SV * |
380 | decode_oid (int relative) |
381 | decode_oid (UV len, int relative) |
381 | { |
382 | { |
382 | UV len = get_length (); |
|
|
383 | |
|
|
384 | if (len <= 0) |
383 | if (len <= 0) |
385 | { |
384 | { |
386 | error ("OBJECT IDENTIFIER length equal to zero"); |
385 | error ("BER_TYPE_OID length must not be zero"); |
387 | return &PL_sv_undef; |
386 | return &PL_sv_undef; |
388 | } |
387 | } |
389 | |
388 | |
390 | U8 *end = cur + len; |
389 | U8 *end = cur + len; |
391 | UV w = get_w (); |
390 | UV w = get_w (); |
392 | |
391 | |
393 | static char oid[MAX_OID_STRLEN]; // static, becaueds too large for stack |
392 | static char oid[MAX_OID_STRLEN]; // static, because too large for stack |
394 | char *app = oid; |
393 | char *app = oid; |
395 | |
394 | |
396 | if (relative) |
395 | if (relative) |
397 | app = write_uv (app, w); |
396 | app = write_uv (app, w); |
|
|
397 | else |
|
|
398 | { |
|
|
399 | UV w1, w2; |
|
|
400 | |
398 | else if (w < 2 * 40) |
401 | if (w < 2 * 40) |
399 | { |
402 | (w1 = w / 40), (w2 = w % 40); |
|
|
403 | else |
|
|
404 | (w1 = 2), (w2 = w - 2 * 40); |
|
|
405 | |
400 | app = write_uv (app, (U8)w / 40); |
406 | app = write_uv (app, w1); |
401 | *app++ = '.'; |
407 | *app++ = '.'; |
402 | app = write_uv (app, (U8)w % 40); |
|
|
403 | } |
|
|
404 | else |
|
|
405 | { |
|
|
406 | app = write_uv (app, 2); |
408 | app = write_uv (app, w2); |
407 | *app++ = '.'; |
|
|
408 | app = write_uv (app, w - 2 * 40); |
|
|
409 | } |
409 | } |
410 | |
410 | |
411 | while (cur < end) |
411 | while (cur < end) |
412 | { |
412 | { |
413 | // we assume an oid component is never > 64 digits |
413 | // we assume an oid component is never > 64 digits |
… | |
… | |
422 | return newSVpvn (oid, app - oid); |
422 | return newSVpvn (oid, app - oid); |
423 | } |
423 | } |
424 | |
424 | |
425 | // TODO: this is unacceptably slow |
425 | // TODO: this is unacceptably slow |
426 | static SV * |
426 | static SV * |
427 | decode_ucs (int chrsize) |
427 | decode_ucs (UV len, int chrsize) |
428 | { |
428 | { |
429 | SV *res = NEWSV (0, 0); |
|
|
430 | |
|
|
431 | UV len = get_length (); |
|
|
432 | |
|
|
433 | if (len & (chrsize - 1)) |
429 | if (len & (chrsize - 1)) |
434 | 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); |
435 | |
433 | |
436 | while (len) |
434 | while (len) |
437 | { |
435 | { |
438 | U8 b1 = get_u8 (); |
436 | U8 b1 = get_u8 (); |
439 | U8 b2 = get_u8 (); |
437 | U8 b2 = get_u8 (); |
… | |
… | |
479 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
477 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
480 | |
478 | |
481 | while (cur < buf + seqend) |
479 | while (cur < buf + seqend) |
482 | av_push (av, decode_ber ()); |
480 | av_push (av, decode_ber ()); |
483 | |
481 | |
484 | if (cur > buf + seqend) |
482 | if (expect_false (cur > buf + seqend)) |
485 | 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); |
486 | |
484 | |
487 | res = newRV_inc ((SV *)av); |
485 | res = newRV_inc ((SV *)av); |
488 | } |
486 | } |
489 | else |
487 | else |
|
|
488 | { |
|
|
489 | UV len = get_length (); |
|
|
490 | |
490 | switch (profile_lookup (cur_profile, klass, tag)) |
491 | switch (profile_lookup (cur_profile, klass, tag)) |
491 | { |
492 | { |
492 | case BER_TYPE_NULL: |
493 | case BER_TYPE_NULL: |
493 | { |
494 | if (expect_false (len)) |
494 | UV len = get_length (); |
|
|
495 | |
|
|
496 | if (len) |
|
|
497 | 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); |
498 | |
496 | |
499 | res = &PL_sv_undef; |
497 | res = &PL_sv_undef; |
500 | } |
|
|
501 | break; |
498 | break; |
502 | |
499 | |
503 | case BER_TYPE_BOOL: |
500 | case BER_TYPE_BOOL: |
504 | { |
|
|
505 | UV len = get_length (); |
|
|
506 | |
|
|
507 | if (len != 1) |
501 | if (expect_false (len != 1)) |
508 | 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); |
509 | |
503 | |
510 | res = newSVcacheint (!!get_u8 ()); |
504 | res = newSVcacheint (!!get_u8 ()); |
511 | } |
|
|
512 | break; |
505 | break; |
513 | |
506 | |
514 | case BER_TYPE_OID: |
507 | case BER_TYPE_OID: |
515 | res = decode_oid (0); |
508 | res = decode_oid (len, 0); |
516 | break; |
509 | break; |
517 | |
510 | |
518 | case BER_TYPE_RELOID: |
511 | case BER_TYPE_RELOID: |
519 | res = decode_oid (1); |
512 | res = decode_oid (len, 1); |
520 | break; |
513 | break; |
521 | |
514 | |
522 | case BER_TYPE_INT: |
515 | case BER_TYPE_INT: |
523 | res = decode_int (); |
516 | res = decode_int (len); |
524 | break; |
517 | break; |
525 | |
518 | |
526 | case BER_TYPE_UTF8: |
519 | case BER_TYPE_UTF8: |
527 | res = decode_data (); |
520 | res = decode_data (len); |
528 | SvUTF8_on (res); |
521 | SvUTF8_on (res); |
529 | break; |
522 | break; |
530 | |
523 | |
531 | case BER_TYPE_BYTES: |
524 | case BER_TYPE_BYTES: |
532 | res = decode_data (); |
525 | res = decode_data (len); |
533 | break; |
526 | break; |
534 | |
527 | |
535 | case BER_TYPE_IPADDRESS: |
528 | case BER_TYPE_IPADDRESS: |
536 | { |
529 | { |
537 | UV len = get_length (); |
|
|
538 | |
|
|
539 | if (len != 4) |
530 | if (len != 4) |
540 | 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); |
541 | |
532 | |
542 | U8 c1 = get_u8 (); |
533 | U8 *data = get_n (4); |
543 | U8 c2 = get_u8 (); |
534 | res = newSVpvf ("%d.%d.%d.%d", data [0], data [1], data [2], data [3]); |
544 | U8 c3 = get_u8 (); |
|
|
545 | U8 c4 = get_u8 (); |
|
|
546 | |
|
|
547 | res = newSVpvf ("%d.%d.%d.%d", c1, c2, c3, c4); |
|
|
548 | } |
535 | } |
549 | break; |
536 | break; |
550 | |
537 | |
551 | case BER_TYPE_UCS2: |
538 | case BER_TYPE_UCS2: |
552 | res = decode_ucs (2); |
539 | res = decode_ucs (len, 2); |
553 | break; |
540 | break; |
554 | |
541 | |
555 | case BER_TYPE_UCS4: |
542 | case BER_TYPE_UCS4: |
556 | res = decode_ucs (4); |
543 | res = decode_ucs (len, 4); |
557 | break; |
544 | break; |
558 | |
545 | |
559 | case BER_TYPE_REAL: |
546 | case BER_TYPE_REAL: |
|
|
547 | error ("BER_TYPE_REAL not implemented"); |
|
|
548 | |
560 | case BER_TYPE_CROAK: |
549 | case BER_TYPE_CROAK: |
|
|
550 | croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag); |
|
|
551 | |
561 | default: |
552 | default: |
562 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
553 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
563 | } |
554 | } |
|
|
555 | } |
564 | |
556 | |
565 | AV *av = newAV (); |
557 | AV *av = newAV (); |
566 | av_fill (av, BER_ARRAYSIZE - 1); |
558 | av_fill (av, BER_ARRAYSIZE - 1); |
567 | AvARRAY (av)[BER_CLASS] = newSVcacheint (klass); |
559 | AvARRAY (av)[BER_CLASS] = newSVcacheint (klass); |
568 | AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); |
560 | AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); |
… | |
… | |
655 | } |
647 | } |
656 | |
648 | |
657 | static U8 * |
649 | static U8 * |
658 | put_length_at (UV val, U8 *cur) |
650 | put_length_at (UV val, U8 *cur) |
659 | { |
651 | { |
660 | if (val < 0x7fU) |
652 | if (val <= 0x7fU) |
661 | *cur++ = val; |
653 | *cur++ = val; |
662 | else |
654 | else |
663 | { |
655 | { |
664 | U8 *lenb = cur++; |
656 | U8 *lenb = cur++; |
665 | |
657 | |
666 | #if UVSIZE > 4 |
658 | #if UVSIZE > 4 |
667 | *cur = val >> 56; cur += *cur > 0; |
659 | *cur = val >> 56; cur += val >= ((UV)1 << (8 * 7)); |
668 | *cur = val >> 48; cur += *cur > 0; |
660 | *cur = val >> 48; cur += val >= ((UV)1 << (8 * 6)); |
669 | *cur = val >> 40; cur += *cur > 0; |
661 | *cur = val >> 40; cur += val >= ((UV)1 << (8 * 5)); |
670 | *cur = val >> 32; cur += *cur > 0; |
662 | *cur = val >> 32; cur += val >= ((UV)1 << (8 * 4)); |
671 | #endif |
663 | #endif |
672 | *cur = val >> 24; cur += *cur > 0; |
664 | *cur = val >> 24; cur += val >= ((UV)1 << (8 * 3)); |
673 | *cur = val >> 16; cur += *cur > 0; |
665 | *cur = val >> 16; cur += val >= ((UV)1 << (8 * 2)); |
674 | *cur = val >> 8; cur += *cur > 0; |
666 | *cur = val >> 8; cur += val >= ((UV)1 << (8 * 1)); |
675 | *cur = val ; cur += 1; |
667 | *cur = val ; cur += 1; |
676 | |
668 | |
677 | *lenb = 0x80 + cur - lenb - 1; |
669 | *lenb = 0x80 + cur - lenb - 1; |
678 | } |
670 | } |
679 | |
671 | |
… | |
… | |
681 | } |
673 | } |
682 | |
674 | |
683 | static void |
675 | static void |
684 | put_length (UV val) |
676 | put_length (UV val) |
685 | { |
677 | { |
686 | need (5 + val); |
678 | need (9 + val); |
687 | cur = put_length_at (val, cur); |
679 | cur = put_length_at (val, cur); |
688 | } |
680 | } |
689 | |
681 | |
690 | // return how many bytes the encoded length requires |
682 | // return how many bytes the encoded length requires |
691 | static int length_length (UV val) |
683 | static int length_length (UV val) |
692 | { |
684 | { |
693 | return val < 0x7fU |
685 | // use hashing with a DeBruin sequence, anyone? |
|
|
686 | return expect_true (val <= 0x7fU) |
694 | ? 1 |
687 | ? 1 |
695 | : 2 |
688 | : 2 |
696 | + (val > 0xffU) |
689 | + (val > 0x000000000000ffU) |
697 | + (val > 0xffffU) |
690 | + (val > 0x0000000000ffffU) |
698 | + (val > 0xffffffU) |
691 | + (val > 0x00000000ffffffU) |
699 | #if UVSIZE > 4 |
692 | #if UVSIZE > 4 |
700 | + (val > 0xffffffffU) |
693 | + (val > 0x000000ffffffffU) |
701 | + (val > 0xffffffffffU) |
694 | + (val > 0x0000ffffffffffU) |
702 | + (val > 0xffffffffffffU) |
695 | + (val > 0x00ffffffffffffU) |
703 | + (val > 0xffffffffffffffU) |
696 | + (val > 0xffffffffffffffU) |
704 | #endif |
697 | #endif |
705 | ; |
698 | ; |
706 | } |
699 | } |
707 | |
700 | |
… | |
… | |
916 | // and adjust later |
909 | // and adjust later |
917 | need (1); |
910 | need (1); |
918 | STRLEN mark = len_fixup_mark (); |
911 | STRLEN mark = len_fixup_mark (); |
919 | |
912 | |
920 | if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV)) |
913 | if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV)) |
921 | croak ("BER constructed data must be array-reference"); |
914 | croak ("BER CONSTRUCTED data must be array-reference"); |
922 | |
915 | |
923 | AV *av = (AV *)SvRV (data); |
916 | AV *av = (AV *)SvRV (data); |
924 | int fill = AvFILL (av); |
917 | int fill = AvFILL (av); |
925 | |
918 | |
926 | if (expect_false (SvRMAGICAL (av))) |
919 | if (expect_false (SvRMAGICAL (av))) |
927 | croak ("BER constructed data must not be tied"); |
920 | croak ("BER CONSTRUCTED data must not be tied"); |
928 | |
921 | |
929 | int i; |
922 | int i; |
930 | for (i = 0; i <= fill; ++i) |
923 | for (i = 0; i <= fill; ++i) |
931 | encode_ber (AvARRAY (av)[i]); |
924 | encode_ber (AvARRAY (av)[i]); |
932 | |
925 | |
… | |
… | |
987 | case BER_TYPE_UCS4: |
980 | case BER_TYPE_UCS4: |
988 | encode_ucs (data, 4); |
981 | encode_ucs (data, 4); |
989 | break; |
982 | break; |
990 | |
983 | |
991 | case BER_TYPE_REAL: |
984 | case BER_TYPE_REAL: |
|
|
985 | croak ("BER_TYPE_REAL not implemented"); |
|
|
986 | |
992 | case BER_TYPE_CROAK: |
987 | case BER_TYPE_CROAK: |
|
|
988 | croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag); |
|
|
989 | |
993 | default: |
990 | default: |
994 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
991 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
995 | } |
992 | } |
996 | |
993 | |
997 | } |
994 | } |
… | |
… | |
1069 | const_iv (BER_TYPE_IPADDRESS) |
1066 | const_iv (BER_TYPE_IPADDRESS) |
1070 | const_iv (BER_TYPE_CROAK) |
1067 | const_iv (BER_TYPE_CROAK) |
1071 | |
1068 | |
1072 | const_iv (SNMP_IPADDRESS) |
1069 | const_iv (SNMP_IPADDRESS) |
1073 | const_iv (SNMP_COUNTER32) |
1070 | const_iv (SNMP_COUNTER32) |
|
|
1071 | const_iv (SNMP_GAUGE32) |
1074 | const_iv (SNMP_UNSIGNED32) |
1072 | const_iv (SNMP_UNSIGNED32) |
1075 | const_iv (SNMP_TIMETICKS) |
1073 | const_iv (SNMP_TIMETICKS) |
1076 | const_iv (SNMP_OPAQUE) |
1074 | const_iv (SNMP_OPAQUE) |
1077 | const_iv (SNMP_COUNTER64) |
1075 | const_iv (SNMP_COUNTER64) |
1078 | }; |
1076 | }; |
1079 | |
1077 | |
1080 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) |
1078 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) |
1081 | newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); |
1079 | newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); |
1082 | } |
1080 | } |
1083 | |
1081 | |
1084 | SV * |
1082 | void |
1085 | ber_decode (SV *ber, SV *profile = &PL_sv_undef) |
1083 | ber_decode (SV *ber, SV *profile = &PL_sv_undef) |
|
|
1084 | ALIAS: |
|
|
1085 | ber_decode_prefix = 1 |
1086 | CODE: |
1086 | PPCODE: |
1087 | { |
1087 | { |
1088 | cur_profile = SvPROFILE (profile); |
1088 | cur_profile = SvPROFILE (profile); |
1089 | STRLEN len; |
1089 | STRLEN len; |
1090 | buf = (U8 *)SvPVbyte (ber, len); |
1090 | buf = (U8 *)SvPVbyte (ber, len); |
1091 | cur = buf; |
1091 | cur = buf; |
1092 | end = buf + len; |
1092 | end = buf + len; |
1093 | |
1093 | |
1094 | RETVAL = decode_ber (); |
1094 | SV *tuple = decode_ber (); |
|
|
1095 | |
|
|
1096 | EXTEND (SP, 2); |
|
|
1097 | PUSHs (sv_2mortal (tuple)); |
|
|
1098 | |
|
|
1099 | if (ix) |
|
|
1100 | PUSHs (sv_2mortal (newSViv (cur - buf))); |
|
|
1101 | else if (cur != end) |
|
|
1102 | error ("trailing garbage after BER value"); |
1095 | } |
1103 | } |
1096 | OUTPUT: RETVAL |
|
|
1097 | |
1104 | |
1098 | void |
1105 | void |
1099 | ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *flags = &PL_sv_undef, SV *data = &PL_sv_undef) |
1106 | ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *flags = &PL_sv_undef, SV *data = &PL_sv_undef) |
1100 | PPCODE: |
1107 | PPCODE: |
1101 | { |
1108 | { |