… | |
… | |
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; |
|
|
260 | U8 c = get_u8 (); |
|
|
261 | |
|
|
262 | if (expect_false (c == 0x80)) |
|
|
263 | error ("illegal BER padding (X.690 8.1.2.4.2, 8.19.2)"); |
257 | |
264 | |
258 | for (;;) |
265 | for (;;) |
259 | { |
266 | { |
260 | U8 c = get_u8 (); |
267 | if (expect_false (res >> UVSIZE * 8 - 7)) |
|
|
268 | error ("BER variable length integer overflow"); |
|
|
269 | |
261 | res = (res << 7) | (c & 0x7f); |
270 | res = (res << 7) | (c & 0x7f); |
262 | |
271 | |
263 | if (!(c & 0x80)) |
272 | if (expect_true (!(c & 0x80))) |
264 | return res; |
273 | return res; |
|
|
274 | |
|
|
275 | c = get_u8 (); |
265 | } |
276 | } |
266 | } |
277 | } |
267 | |
278 | |
268 | static UV |
279 | static UV |
269 | get_length (void) |
280 | get_length (void) |
270 | { |
281 | { |
271 | UV res = get_u8 (); |
282 | UV res = get_u8 (); |
272 | |
283 | |
273 | if (res & 0x80) |
284 | if (expect_false (res & 0x80)) |
274 | { |
285 | { |
275 | 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 | |
276 | res = 0; |
300 | res = 0; |
277 | |
301 | do |
278 | switch (cnt) |
302 | res = (res << 8) | *cur++; |
279 | { |
303 | 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 | } |
304 | } |
298 | |
305 | |
299 | return res; |
306 | return res; |
300 | } |
307 | } |
301 | |
308 | |
302 | static SV * |
309 | static SV * |
303 | decode_int (void) |
310 | decode_int (UV len) |
304 | { |
311 | { |
305 | UV len = get_length (); |
|
|
306 | |
|
|
307 | if (!len) |
312 | if (!len) |
308 | { |
313 | 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 | |
314 | |
313 | U8 *data = get_n (len); |
315 | U8 *data = get_n (len); |
314 | |
316 | |
|
|
317 | if (expect_false (len > 1)) |
|
|
318 | { |
|
|
319 | U16 mask = (data [0] << 8) | data [1] & 0xff80; |
|
|
320 | |
|
|
321 | if (expect_false (mask == 0xff80 || mask == 0x0000)) |
|
|
322 | error ("illegal padding in BER_TYPE_INT (X.690 8.3.2)"); |
|
|
323 | } |
|
|
324 | |
315 | int negative = data [0] & 0x80; |
325 | int negative = data [0] & 0x80; |
316 | |
326 | |
317 | 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"); |
318 | |
331 | |
319 | do |
332 | do |
320 | val = (val << 8) | *data++; |
333 | val = (val << 8) | *data++; |
321 | while (--len); |
334 | while (--len); |
322 | |
335 | |
… | |
… | |
324 | // but that's ok, as perl relies on it as well. |
337 | // but that's ok, as perl relies on it as well. |
325 | return negative ? newSViv ((IV)val) : newSVuv (val); |
338 | return negative ? newSViv ((IV)val) : newSVuv (val); |
326 | } |
339 | } |
327 | |
340 | |
328 | static SV * |
341 | static SV * |
329 | decode_data (void) |
342 | decode_data (UV len) |
330 | { |
343 | { |
331 | UV len = get_length (); |
|
|
332 | return newSVpvn ((char *)get_n (len), len); |
344 | return newSVpvn ((char *)get_n (len), len); |
333 | } |
345 | } |
334 | |
346 | |
335 | // helper for decode_object_identifier |
347 | // helper for decode_object_identifier |
336 | static char * |
348 | static char * |
… | |
… | |
364 | |
376 | |
365 | return buf; |
377 | return buf; |
366 | } |
378 | } |
367 | |
379 | |
368 | static SV * |
380 | static SV * |
369 | decode_oid (int relative) |
381 | decode_oid (UV len, int relative) |
370 | { |
382 | { |
371 | UV len = get_length (); |
|
|
372 | |
|
|
373 | if (len <= 0) |
383 | if (len <= 0) |
374 | { |
384 | { |
375 | error ("OBJECT IDENTIFIER length equal to zero"); |
385 | error ("BER_TYPE_OID length must not be zero"); |
376 | return &PL_sv_undef; |
386 | return &PL_sv_undef; |
377 | } |
387 | } |
378 | |
388 | |
379 | U8 *end = cur + len; |
389 | U8 *end = cur + len; |
380 | UV w = get_w (); |
390 | UV w = get_w (); |
381 | |
391 | |
382 | 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 |
383 | char *app = oid; |
393 | char *app = oid; |
384 | |
394 | |
385 | if (relative) |
395 | if (relative) |
386 | app = write_uv (app, w); |
396 | app = write_uv (app, w); |
387 | else |
397 | else |
388 | { |
398 | { |
|
|
399 | UV w1, w2; |
|
|
400 | |
|
|
401 | if (w < 2 * 40) |
|
|
402 | (w1 = w / 40), (w2 = w % 40); |
|
|
403 | else |
|
|
404 | (w1 = 2), (w2 = w - 2 * 40); |
|
|
405 | |
389 | app = write_uv (app, (U8)w / 40); |
406 | app = write_uv (app, w1); |
390 | *app++ = '.'; |
407 | *app++ = '.'; |
391 | app = write_uv (app, (U8)w % 40); |
408 | app = write_uv (app, w2); |
392 | } |
409 | } |
393 | |
410 | |
394 | while (cur < end) |
411 | while (cur < end) |
395 | { |
412 | { |
396 | // we assume an oid component is never > 64 digits |
413 | // we assume an oid component is never > 64 digits |
… | |
… | |
405 | return newSVpvn (oid, app - oid); |
422 | return newSVpvn (oid, app - oid); |
406 | } |
423 | } |
407 | |
424 | |
408 | // TODO: this is unacceptably slow |
425 | // TODO: this is unacceptably slow |
409 | static SV * |
426 | static SV * |
410 | decode_ucs (int chrsize) |
427 | decode_ucs (UV len, int chrsize) |
411 | { |
428 | { |
412 | SV *res = NEWSV (0, 0); |
|
|
413 | |
|
|
414 | UV len = get_length (); |
|
|
415 | |
|
|
416 | if (len & (chrsize - 1)) |
429 | if (len & (chrsize - 1)) |
417 | 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); |
418 | |
433 | |
419 | while (len) |
434 | while (len) |
420 | { |
435 | { |
421 | U8 b1 = get_u8 (); |
436 | U8 b1 = get_u8 (); |
422 | U8 b2 = get_u8 (); |
437 | U8 b2 = get_u8 (); |
… | |
… | |
453 | int tag = identifier & ASN_TAG_MASK; |
468 | int tag = identifier & ASN_TAG_MASK; |
454 | |
469 | |
455 | if (tag == ASN_TAG_BER) |
470 | if (tag == ASN_TAG_BER) |
456 | tag = get_w (); |
471 | tag = get_w (); |
457 | |
472 | |
458 | if (tag == ASN_TAG_BER) |
|
|
459 | tag = get_w (); |
|
|
460 | |
|
|
461 | if (constructed) |
473 | if (constructed) |
462 | { |
474 | { |
|
|
475 | want (1); |
|
|
476 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
|
|
477 | |
|
|
478 | if (expect_false (*cur == 0x80)) |
|
|
479 | { |
|
|
480 | // indefinite length |
|
|
481 | ++cur; |
|
|
482 | |
|
|
483 | for (;;) |
|
|
484 | { |
|
|
485 | want (2); |
|
|
486 | if (!cur [0] && !cur [1]) |
|
|
487 | { |
|
|
488 | cur += 2; |
|
|
489 | break; |
|
|
490 | } |
|
|
491 | |
|
|
492 | av_push (av, decode_ber ()); |
|
|
493 | } |
|
|
494 | } |
|
|
495 | else |
|
|
496 | { |
|
|
497 | UV len = get_length (); |
|
|
498 | UV seqend = (cur - buf) + len; |
|
|
499 | |
|
|
500 | while (cur < buf + seqend) |
|
|
501 | av_push (av, decode_ber ()); |
|
|
502 | |
|
|
503 | if (expect_false (cur > buf + seqend)) |
|
|
504 | croak ("CONSTRUCTED type %02x length overflow (0x%x 0x%x)\n", identifier, (int)(cur - buf), (int)seqend); |
|
|
505 | } |
|
|
506 | |
|
|
507 | res = newRV_inc ((SV *)av); |
|
|
508 | } |
|
|
509 | else |
|
|
510 | { |
463 | UV len = get_length (); |
511 | UV len = get_length (); |
464 | UV seqend = (cur - buf) + len; |
|
|
465 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
|
|
466 | |
512 | |
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)) |
513 | switch (profile_lookup (cur_profile, klass, tag)) |
477 | { |
514 | { |
478 | case BER_TYPE_NULL: |
515 | case BER_TYPE_NULL: |
479 | { |
516 | 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); |
517 | croak ("BER_TYPE_NULL value with non-zero length %d encountered (X.690 8.8.2)", len); |
484 | |
518 | |
485 | res = &PL_sv_undef; |
519 | res = &PL_sv_undef; |
486 | } |
|
|
487 | break; |
520 | break; |
488 | |
521 | |
489 | case BER_TYPE_BOOL: |
522 | case BER_TYPE_BOOL: |
490 | { |
|
|
491 | UV len = get_length (); |
|
|
492 | |
|
|
493 | if (len != 1) |
523 | if (expect_false (len != 1)) |
494 | croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered", len); |
524 | croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered (X.690 8.2.1)", len); |
495 | |
525 | |
496 | res = newSVcacheint (!!get_u8 ()); |
526 | res = newSVcacheint (!!get_u8 ()); |
497 | } |
|
|
498 | break; |
527 | break; |
499 | |
528 | |
500 | case BER_TYPE_OID: |
529 | case BER_TYPE_OID: |
501 | res = decode_oid (0); |
530 | res = decode_oid (len, 0); |
502 | break; |
531 | break; |
503 | |
532 | |
504 | case BER_TYPE_RELOID: |
533 | case BER_TYPE_RELOID: |
505 | res = decode_oid (1); |
534 | res = decode_oid (len, 1); |
506 | break; |
535 | break; |
507 | |
536 | |
508 | case BER_TYPE_INT: |
537 | case BER_TYPE_INT: |
509 | res = decode_int (); |
538 | res = decode_int (len); |
510 | break; |
539 | break; |
511 | |
540 | |
512 | case BER_TYPE_UTF8: |
541 | case BER_TYPE_UTF8: |
513 | res = decode_data (); |
542 | res = decode_data (len); |
514 | SvUTF8_on (res); |
543 | SvUTF8_on (res); |
515 | break; |
544 | break; |
516 | |
545 | |
517 | case BER_TYPE_BYTES: |
546 | case BER_TYPE_BYTES: |
518 | res = decode_data (); |
547 | res = decode_data (len); |
519 | break; |
548 | break; |
520 | |
549 | |
521 | case BER_TYPE_IPADDRESS: |
550 | case BER_TYPE_IPADDRESS: |
522 | { |
551 | { |
523 | UV len = get_length (); |
|
|
524 | |
|
|
525 | if (len != 4) |
552 | if (len != 4) |
526 | croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered", len); |
553 | croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered (RFC 2578 7.1.5)", len); |
527 | |
554 | |
528 | U8 c1 = get_u8 (); |
555 | U8 *data = get_n (4); |
529 | U8 c2 = get_u8 (); |
556 | 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 | } |
557 | } |
535 | break; |
558 | break; |
536 | |
559 | |
537 | case BER_TYPE_UCS2: |
560 | case BER_TYPE_UCS2: |
538 | res = decode_ucs (2); |
561 | res = decode_ucs (len, 2); |
539 | break; |
562 | break; |
540 | |
563 | |
541 | case BER_TYPE_UCS4: |
564 | case BER_TYPE_UCS4: |
542 | res = decode_ucs (4); |
565 | res = decode_ucs (len, 4); |
543 | break; |
566 | break; |
544 | |
567 | |
545 | case BER_TYPE_REAL: |
568 | case BER_TYPE_REAL: |
|
|
569 | error ("BER_TYPE_REAL not implemented"); |
|
|
570 | |
546 | case BER_TYPE_CROAK: |
571 | case BER_TYPE_CROAK: |
|
|
572 | croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag); |
|
|
573 | |
547 | default: |
574 | default: |
548 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
575 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
549 | } |
576 | } |
|
|
577 | } |
550 | |
578 | |
551 | AV *av = newAV (); |
579 | AV *av = newAV (); |
552 | av_fill (av, BER_ARRAYSIZE - 1); |
580 | av_fill (av, BER_ARRAYSIZE - 1); |
553 | AvARRAY (av)[BER_CLASS] = newSVcacheint (klass); |
581 | AvARRAY (av)[BER_CLASS] = newSVcacheint (klass); |
554 | AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); |
582 | AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); |
… | |
… | |
566 | strlen_sum (STRLEN l1, STRLEN l2) |
594 | strlen_sum (STRLEN l1, STRLEN l2) |
567 | { |
595 | { |
568 | size_t sum = l1 + l2; |
596 | size_t sum = l1 + l2; |
569 | |
597 | |
570 | if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) |
598 | if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) |
571 | croak ("JSON::XS: string size overflow"); |
599 | croak ("Convert::BER::XS: string size overflow"); |
572 | |
600 | |
573 | return sum; |
601 | return sum; |
574 | } |
602 | } |
575 | |
603 | |
576 | static void |
604 | static void |
… | |
… | |
641 | } |
669 | } |
642 | |
670 | |
643 | static U8 * |
671 | static U8 * |
644 | put_length_at (UV val, U8 *cur) |
672 | put_length_at (UV val, U8 *cur) |
645 | { |
673 | { |
646 | if (val < 0x7fU) |
674 | if (val <= 0x7fU) |
647 | *cur++ = val; |
675 | *cur++ = val; |
648 | else |
676 | else |
649 | { |
677 | { |
650 | U8 *lenb = cur++; |
678 | U8 *lenb = cur++; |
651 | |
679 | |
652 | #if UVSIZE > 4 |
680 | #if UVSIZE > 4 |
653 | *cur = val >> 56; cur += *cur > 0; |
681 | *cur = val >> 56; cur += val >= ((UV)1 << (8 * 7)); |
654 | *cur = val >> 48; cur += *cur > 0; |
682 | *cur = val >> 48; cur += val >= ((UV)1 << (8 * 6)); |
655 | *cur = val >> 40; cur += *cur > 0; |
683 | *cur = val >> 40; cur += val >= ((UV)1 << (8 * 5)); |
656 | *cur = val >> 32; cur += *cur > 0; |
684 | *cur = val >> 32; cur += val >= ((UV)1 << (8 * 4)); |
657 | #endif |
685 | #endif |
658 | *cur = val >> 24; cur += *cur > 0; |
686 | *cur = val >> 24; cur += val >= ((UV)1 << (8 * 3)); |
659 | *cur = val >> 16; cur += *cur > 0; |
687 | *cur = val >> 16; cur += val >= ((UV)1 << (8 * 2)); |
660 | *cur = val >> 8; cur += *cur > 0; |
688 | *cur = val >> 8; cur += val >= ((UV)1 << (8 * 1)); |
661 | *cur = val ; cur += 1; |
689 | *cur = val ; cur += 1; |
662 | |
690 | |
663 | *lenb = 0x80 + cur - lenb - 1; |
691 | *lenb = 0x80 + cur - lenb - 1; |
664 | } |
692 | } |
665 | |
693 | |
… | |
… | |
667 | } |
695 | } |
668 | |
696 | |
669 | static void |
697 | static void |
670 | put_length (UV val) |
698 | put_length (UV val) |
671 | { |
699 | { |
672 | need (5 + val); |
700 | need (9 + val); |
673 | cur = put_length_at (val, cur); |
701 | cur = put_length_at (val, cur); |
674 | } |
702 | } |
675 | |
703 | |
676 | // return how many bytes the encoded length requires |
704 | // return how many bytes the encoded length requires |
677 | static int length_length (UV val) |
705 | static int length_length (UV val) |
678 | { |
706 | { |
679 | return val < 0x7fU |
707 | // use hashing with a DeBruin sequence, anyone? |
|
|
708 | return expect_true (val <= 0x7fU) |
680 | ? 1 |
709 | ? 1 |
681 | : 2 |
710 | : 2 |
682 | + (val > 0xffU) |
711 | + (val > 0x000000000000ffU) |
683 | + (val > 0xffffU) |
712 | + (val > 0x0000000000ffffU) |
684 | + (val > 0xffffffU) |
713 | + (val > 0x00000000ffffffU) |
685 | #if UVSIZE > 4 |
714 | #if UVSIZE > 4 |
686 | + (val > 0xffffffffU) |
715 | + (val > 0x000000ffffffffU) |
687 | + (val > 0xffffffffffU) |
716 | + (val > 0x0000ffffffffffU) |
688 | + (val > 0xffffffffffffU) |
717 | + (val > 0x00ffffffffffffU) |
689 | + (val > 0xffffffffffffffU) |
718 | + (val > 0xffffffffffffffU) |
690 | #endif |
719 | #endif |
691 | ; |
720 | ; |
692 | } |
721 | } |
693 | |
722 | |
… | |
… | |
902 | // and adjust later |
931 | // and adjust later |
903 | need (1); |
932 | need (1); |
904 | STRLEN mark = len_fixup_mark (); |
933 | STRLEN mark = len_fixup_mark (); |
905 | |
934 | |
906 | if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV)) |
935 | if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV)) |
907 | croak ("BER constructed data must be array-reference"); |
936 | croak ("BER CONSTRUCTED data must be array-reference"); |
908 | |
937 | |
909 | AV *av = (AV *)SvRV (data); |
938 | AV *av = (AV *)SvRV (data); |
910 | int fill = AvFILL (av); |
939 | int fill = AvFILL (av); |
911 | |
940 | |
912 | if (expect_false (SvRMAGICAL (av))) |
941 | if (expect_false (SvRMAGICAL (av))) |
913 | croak ("BER constructed data must not be tied"); |
942 | croak ("BER CONSTRUCTED data must not be tied"); |
914 | |
943 | |
915 | int i; |
944 | int i; |
916 | for (i = 0; i <= fill; ++i) |
945 | for (i = 0; i <= fill; ++i) |
917 | encode_ber (AvARRAY (av)[i]); |
946 | encode_ber (AvARRAY (av)[i]); |
918 | |
947 | |
… | |
… | |
973 | case BER_TYPE_UCS4: |
1002 | case BER_TYPE_UCS4: |
974 | encode_ucs (data, 4); |
1003 | encode_ucs (data, 4); |
975 | break; |
1004 | break; |
976 | |
1005 | |
977 | case BER_TYPE_REAL: |
1006 | case BER_TYPE_REAL: |
|
|
1007 | croak ("BER_TYPE_REAL not implemented"); |
|
|
1008 | |
978 | case BER_TYPE_CROAK: |
1009 | case BER_TYPE_CROAK: |
|
|
1010 | croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag); |
|
|
1011 | |
979 | default: |
1012 | default: |
980 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
1013 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
981 | } |
1014 | } |
982 | |
1015 | |
983 | } |
1016 | } |
… | |
… | |
1055 | const_iv (BER_TYPE_IPADDRESS) |
1088 | const_iv (BER_TYPE_IPADDRESS) |
1056 | const_iv (BER_TYPE_CROAK) |
1089 | const_iv (BER_TYPE_CROAK) |
1057 | |
1090 | |
1058 | const_iv (SNMP_IPADDRESS) |
1091 | const_iv (SNMP_IPADDRESS) |
1059 | const_iv (SNMP_COUNTER32) |
1092 | const_iv (SNMP_COUNTER32) |
|
|
1093 | const_iv (SNMP_GAUGE32) |
1060 | const_iv (SNMP_UNSIGNED32) |
1094 | const_iv (SNMP_UNSIGNED32) |
1061 | const_iv (SNMP_TIMETICKS) |
1095 | const_iv (SNMP_TIMETICKS) |
1062 | const_iv (SNMP_OPAQUE) |
1096 | const_iv (SNMP_OPAQUE) |
1063 | const_iv (SNMP_COUNTER64) |
1097 | const_iv (SNMP_COUNTER64) |
1064 | }; |
1098 | }; |
1065 | |
1099 | |
1066 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) |
1100 | 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)); |
1101 | newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); |
1068 | } |
1102 | } |
1069 | |
1103 | |
1070 | SV * |
1104 | void |
1071 | ber_decode (SV *ber, SV *profile = &PL_sv_undef) |
1105 | ber_decode (SV *ber, SV *profile = &PL_sv_undef) |
|
|
1106 | ALIAS: |
|
|
1107 | ber_decode_prefix = 1 |
1072 | CODE: |
1108 | PPCODE: |
1073 | { |
1109 | { |
1074 | cur_profile = SvPROFILE (profile); |
1110 | cur_profile = SvPROFILE (profile); |
1075 | STRLEN len; |
1111 | STRLEN len; |
1076 | buf = (U8 *)SvPVbyte (ber, len); |
1112 | buf = (U8 *)SvPVbyte (ber, len); |
1077 | cur = buf; |
1113 | cur = buf; |
1078 | end = buf + len; |
1114 | end = buf + len; |
1079 | |
1115 | |
1080 | RETVAL = decode_ber (); |
1116 | SV *tuple = decode_ber (); |
|
|
1117 | |
|
|
1118 | EXTEND (SP, 2); |
|
|
1119 | PUSHs (sv_2mortal (tuple)); |
|
|
1120 | |
|
|
1121 | if (ix) |
|
|
1122 | PUSHs (sv_2mortal (newSViv (cur - buf))); |
|
|
1123 | else if (cur != end) |
|
|
1124 | error ("trailing garbage after BER value"); |
1081 | } |
1125 | } |
1082 | OUTPUT: RETVAL |
|
|
1083 | |
1126 | |
1084 | void |
1127 | void |
1085 | ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *flags = &PL_sv_undef, SV *data = &PL_sv_undef) |
1128 | ber_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: |
1129 | PPCODE: |
1087 | { |
1130 | { |