… | |
… | |
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; |
265 | } |
|
|
266 | } |
|
|
267 | |
274 | |
268 | // get_w, but disallow padding |
275 | c = get_u8 (); |
269 | static UV |
276 | } |
270 | get_w_nopad (void) |
|
|
271 | { |
|
|
272 | U8 first = get_u8 (); |
|
|
273 | |
|
|
274 | if (first == 0x80) |
|
|
275 | error ("illegal BER padding"); |
|
|
276 | |
|
|
277 | --cur; |
|
|
278 | |
|
|
279 | return get_w (); |
|
|
280 | } |
277 | } |
281 | |
278 | |
282 | static UV |
279 | static UV |
283 | get_length (void) |
280 | get_length (void) |
284 | { |
281 | { |
285 | UV res = get_u8 (); |
282 | UV res = get_u8 (); |
286 | |
283 | |
287 | if (res & 0x80) |
284 | if (expect_false (res & 0x80)) |
288 | { |
285 | { |
289 | 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 | |
290 | res = 0; |
300 | res = 0; |
291 | |
301 | do |
292 | switch (cnt) |
302 | res = (res << 8) | *cur++; |
293 | { |
303 | while (--cnt); |
294 | case 0: |
|
|
295 | error ("indefinite ASN.1 lengths not supported"); |
|
|
296 | return 0; |
|
|
297 | |
|
|
298 | //case 0x80: // indefinite length |
|
|
299 | |
|
|
300 | //case 0xff: reserved |
|
|
301 | default: |
|
|
302 | error ("ASN.1 length too long"); |
|
|
303 | return 0; |
|
|
304 | |
|
|
305 | case 8: res = (res << 8) | get_u8 (); |
|
|
306 | case 7: res = (res << 8) | get_u8 (); |
|
|
307 | case 6: res = (res << 8) | get_u8 (); |
|
|
308 | case 5: res = (res << 8) | get_u8 (); |
|
|
309 | case 4: res = (res << 8) | get_u8 (); |
|
|
310 | case 3: res = (res << 8) | get_u8 (); |
|
|
311 | case 2: res = (res << 8) | get_u8 (); |
|
|
312 | case 1: res = (res << 8) | get_u8 (); |
|
|
313 | } |
|
|
314 | } |
304 | } |
315 | |
305 | |
316 | return res; |
306 | return res; |
317 | } |
307 | } |
318 | |
308 | |
319 | static SV * |
309 | static SV * |
320 | decode_int (void) |
310 | decode_int (UV len) |
321 | { |
311 | { |
322 | UV len = get_length (); |
|
|
323 | |
|
|
324 | if (!len) |
312 | if (!len) |
325 | { |
313 | error ("invalid BER_TYPE_INT length zero (X.690 8.3.1)"); |
326 | error ("invalid integer length equal to zero"); |
|
|
327 | return 0; |
|
|
328 | } |
|
|
329 | |
314 | |
330 | U8 *data = get_n (len); |
315 | U8 *data = get_n (len); |
331 | |
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 | |
332 | int negative = data [0] & 0x80; |
325 | int negative = data [0] & 0x80; |
333 | |
326 | |
334 | 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"); |
335 | |
331 | |
336 | do |
332 | do |
337 | val = (val << 8) | *data++; |
333 | val = (val << 8) | *data++; |
338 | while (--len); |
334 | while (--len); |
339 | |
335 | |
… | |
… | |
341 | // but that's ok, as perl relies on it as well. |
337 | // but that's ok, as perl relies on it as well. |
342 | return negative ? newSViv ((IV)val) : newSVuv (val); |
338 | return negative ? newSViv ((IV)val) : newSVuv (val); |
343 | } |
339 | } |
344 | |
340 | |
345 | static SV * |
341 | static SV * |
346 | decode_data (void) |
342 | decode_data (UV len) |
347 | { |
343 | { |
348 | UV len = get_length (); |
|
|
349 | return newSVpvn ((char *)get_n (len), len); |
344 | return newSVpvn ((char *)get_n (len), len); |
350 | } |
345 | } |
351 | |
346 | |
352 | // helper for decode_object_identifier |
347 | // helper for decode_object_identifier |
353 | static char * |
348 | static char * |
… | |
… | |
381 | |
376 | |
382 | return buf; |
377 | return buf; |
383 | } |
378 | } |
384 | |
379 | |
385 | static SV * |
380 | static SV * |
386 | decode_oid (int relative) |
381 | decode_oid (UV len, int relative) |
387 | { |
382 | { |
388 | UV len = get_length (); |
|
|
389 | |
|
|
390 | if (len <= 0) |
383 | if (len <= 0) |
391 | { |
384 | { |
392 | error ("OBJECT IDENTIFIER length equal to zero"); |
385 | error ("BER_TYPE_OID length must not be zero"); |
393 | return &PL_sv_undef; |
386 | return &PL_sv_undef; |
394 | } |
387 | } |
395 | |
388 | |
396 | U8 *end = cur + len; |
389 | U8 *end = cur + len; |
397 | UV w = get_w_nopad (); |
390 | UV w = get_w (); |
398 | |
391 | |
399 | 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 |
400 | char *app = oid; |
393 | char *app = oid; |
401 | |
394 | |
402 | if (relative) |
395 | if (relative) |
403 | app = write_uv (app, w); |
396 | app = write_uv (app, w); |
|
|
397 | else |
|
|
398 | { |
|
|
399 | UV w1, w2; |
|
|
400 | |
404 | else if (w < 2 * 40) |
401 | if (w < 2 * 40) |
405 | { |
402 | (w1 = w / 40), (w2 = w % 40); |
|
|
403 | else |
|
|
404 | (w1 = 2), (w2 = w - 2 * 40); |
|
|
405 | |
406 | app = write_uv (app, (U8)w / 40); |
406 | app = write_uv (app, w1); |
407 | *app++ = '.'; |
407 | *app++ = '.'; |
408 | app = write_uv (app, (U8)w % 40); |
|
|
409 | } |
|
|
410 | else |
|
|
411 | { |
|
|
412 | app = write_uv (app, 2); |
408 | app = write_uv (app, w2); |
413 | *app++ = '.'; |
|
|
414 | app = write_uv (app, w - 2 * 40); |
|
|
415 | } |
409 | } |
416 | |
410 | |
417 | while (cur < end) |
411 | while (cur < end) |
418 | { |
412 | { |
419 | // we assume an oid component is never > 64 digits |
413 | // we assume an oid component is never > 64 digits |
420 | if (oid + sizeof (oid) - app < 64) |
414 | if (oid + sizeof (oid) - app < 64) |
421 | croak ("BER_TYPE_OID to long to decode"); |
415 | croak ("BER_TYPE_OID to long to decode"); |
422 | |
416 | |
423 | w = get_w_nopad (); |
417 | w = get_w (); |
424 | *app++ = '.'; |
418 | *app++ = '.'; |
425 | app = write_uv (app, w); |
419 | app = write_uv (app, w); |
426 | } |
420 | } |
427 | |
421 | |
428 | return newSVpvn (oid, app - oid); |
422 | return newSVpvn (oid, app - oid); |
429 | } |
423 | } |
430 | |
424 | |
431 | // TODO: this is unacceptably slow |
425 | // TODO: this is unacceptably slow |
432 | static SV * |
426 | static SV * |
433 | decode_ucs (int chrsize) |
427 | decode_ucs (UV len, int chrsize) |
434 | { |
428 | { |
435 | SV *res = NEWSV (0, 0); |
|
|
436 | |
|
|
437 | UV len = get_length (); |
|
|
438 | |
|
|
439 | if (len & (chrsize - 1)) |
429 | if (len & (chrsize - 1)) |
440 | 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); |
441 | |
433 | |
442 | while (len) |
434 | while (len) |
443 | { |
435 | { |
444 | U8 b1 = get_u8 (); |
436 | U8 b1 = get_u8 (); |
445 | U8 b2 = get_u8 (); |
437 | U8 b2 = get_u8 (); |
… | |
… | |
476 | int tag = identifier & ASN_TAG_MASK; |
468 | int tag = identifier & ASN_TAG_MASK; |
477 | |
469 | |
478 | if (tag == ASN_TAG_BER) |
470 | if (tag == ASN_TAG_BER) |
479 | tag = get_w (); |
471 | tag = get_w (); |
480 | |
472 | |
481 | if (tag == ASN_TAG_BER) |
|
|
482 | tag = get_w (); |
|
|
483 | |
|
|
484 | if (constructed) |
473 | if (constructed) |
485 | { |
474 | { |
486 | UV len = get_length (); |
475 | UV len = get_length (); |
487 | UV seqend = (cur - buf) + len; |
476 | UV seqend = (cur - buf) + len; |
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 overflow (%x %x)\n", identifier, cur - buf, 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", 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", 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", 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: |
|
|
547 | error ("BER_TYPE_REAL not implemented"); |
|
|
548 | |
569 | case BER_TYPE_CROAK: |
549 | case BER_TYPE_CROAK: |
|
|
550 | croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag); |
|
|
551 | |
570 | default: |
552 | default: |
571 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
553 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
572 | } |
554 | } |
|
|
555 | } |
573 | |
556 | |
574 | AV *av = newAV (); |
557 | AV *av = newAV (); |
575 | av_fill (av, BER_ARRAYSIZE - 1); |
558 | av_fill (av, BER_ARRAYSIZE - 1); |
576 | AvARRAY (av)[BER_CLASS] = newSVcacheint (klass); |
559 | AvARRAY (av)[BER_CLASS] = newSVcacheint (klass); |
577 | AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); |
560 | AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); |
… | |
… | |
589 | strlen_sum (STRLEN l1, STRLEN l2) |
572 | strlen_sum (STRLEN l1, STRLEN l2) |
590 | { |
573 | { |
591 | size_t sum = l1 + l2; |
574 | size_t sum = l1 + l2; |
592 | |
575 | |
593 | if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) |
576 | if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) |
594 | croak ("JSON::XS: string size overflow"); |
577 | croak ("Convert::BER::XS: string size overflow"); |
595 | |
578 | |
596 | return sum; |
579 | return sum; |
597 | } |
580 | } |
598 | |
581 | |
599 | static void |
582 | static void |
… | |
… | |
664 | } |
647 | } |
665 | |
648 | |
666 | static U8 * |
649 | static U8 * |
667 | put_length_at (UV val, U8 *cur) |
650 | put_length_at (UV val, U8 *cur) |
668 | { |
651 | { |
669 | if (val < 0x7fU) |
652 | if (val <= 0x7fU) |
670 | *cur++ = val; |
653 | *cur++ = val; |
671 | else |
654 | else |
672 | { |
655 | { |
673 | U8 *lenb = cur++; |
656 | U8 *lenb = cur++; |
674 | |
657 | |
675 | #if UVSIZE > 4 |
658 | #if UVSIZE > 4 |
676 | *cur = val >> 56; cur += *cur > 0; |
659 | *cur = val >> 56; cur += val >= ((UV)1 << (8 * 7)); |
677 | *cur = val >> 48; cur += *cur > 0; |
660 | *cur = val >> 48; cur += val >= ((UV)1 << (8 * 6)); |
678 | *cur = val >> 40; cur += *cur > 0; |
661 | *cur = val >> 40; cur += val >= ((UV)1 << (8 * 5)); |
679 | *cur = val >> 32; cur += *cur > 0; |
662 | *cur = val >> 32; cur += val >= ((UV)1 << (8 * 4)); |
680 | #endif |
663 | #endif |
681 | *cur = val >> 24; cur += *cur > 0; |
664 | *cur = val >> 24; cur += val >= ((UV)1 << (8 * 3)); |
682 | *cur = val >> 16; cur += *cur > 0; |
665 | *cur = val >> 16; cur += val >= ((UV)1 << (8 * 2)); |
683 | *cur = val >> 8; cur += *cur > 0; |
666 | *cur = val >> 8; cur += val >= ((UV)1 << (8 * 1)); |
684 | *cur = val ; cur += 1; |
667 | *cur = val ; cur += 1; |
685 | |
668 | |
686 | *lenb = 0x80 + cur - lenb - 1; |
669 | *lenb = 0x80 + cur - lenb - 1; |
687 | } |
670 | } |
688 | |
671 | |
… | |
… | |
690 | } |
673 | } |
691 | |
674 | |
692 | static void |
675 | static void |
693 | put_length (UV val) |
676 | put_length (UV val) |
694 | { |
677 | { |
695 | need (5 + val); |
678 | need (9 + val); |
696 | cur = put_length_at (val, cur); |
679 | cur = put_length_at (val, cur); |
697 | } |
680 | } |
698 | |
681 | |
699 | // return how many bytes the encoded length requires |
682 | // return how many bytes the encoded length requires |
700 | static int length_length (UV val) |
683 | static int length_length (UV val) |
701 | { |
684 | { |
702 | return val < 0x7fU |
685 | // use hashing with a DeBruin sequence, anyone? |
|
|
686 | return expect_true (val <= 0x7fU) |
703 | ? 1 |
687 | ? 1 |
704 | : 2 |
688 | : 2 |
705 | + (val > 0xffU) |
689 | + (val > 0x000000000000ffU) |
706 | + (val > 0xffffU) |
690 | + (val > 0x0000000000ffffU) |
707 | + (val > 0xffffffU) |
691 | + (val > 0x00000000ffffffU) |
708 | #if UVSIZE > 4 |
692 | #if UVSIZE > 4 |
709 | + (val > 0xffffffffU) |
693 | + (val > 0x000000ffffffffU) |
710 | + (val > 0xffffffffffU) |
694 | + (val > 0x0000ffffffffffU) |
711 | + (val > 0xffffffffffffU) |
695 | + (val > 0x00ffffffffffffU) |
712 | + (val > 0xffffffffffffffU) |
696 | + (val > 0xffffffffffffffU) |
713 | #endif |
697 | #endif |
714 | ; |
698 | ; |
715 | } |
699 | } |
716 | |
700 | |
… | |
… | |
925 | // and adjust later |
909 | // and adjust later |
926 | need (1); |
910 | need (1); |
927 | STRLEN mark = len_fixup_mark (); |
911 | STRLEN mark = len_fixup_mark (); |
928 | |
912 | |
929 | if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV)) |
913 | if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV)) |
930 | croak ("BER constructed data must be array-reference"); |
914 | croak ("BER CONSTRUCTED data must be array-reference"); |
931 | |
915 | |
932 | AV *av = (AV *)SvRV (data); |
916 | AV *av = (AV *)SvRV (data); |
933 | int fill = AvFILL (av); |
917 | int fill = AvFILL (av); |
934 | |
918 | |
935 | if (expect_false (SvRMAGICAL (av))) |
919 | if (expect_false (SvRMAGICAL (av))) |
936 | croak ("BER constructed data must not be tied"); |
920 | croak ("BER CONSTRUCTED data must not be tied"); |
937 | |
921 | |
938 | int i; |
922 | int i; |
939 | for (i = 0; i <= fill; ++i) |
923 | for (i = 0; i <= fill; ++i) |
940 | encode_ber (AvARRAY (av)[i]); |
924 | encode_ber (AvARRAY (av)[i]); |
941 | |
925 | |
… | |
… | |
996 | case BER_TYPE_UCS4: |
980 | case BER_TYPE_UCS4: |
997 | encode_ucs (data, 4); |
981 | encode_ucs (data, 4); |
998 | break; |
982 | break; |
999 | |
983 | |
1000 | case BER_TYPE_REAL: |
984 | case BER_TYPE_REAL: |
|
|
985 | croak ("BER_TYPE_REAL not implemented"); |
|
|
986 | |
1001 | case BER_TYPE_CROAK: |
987 | case BER_TYPE_CROAK: |
|
|
988 | croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag); |
|
|
989 | |
1002 | default: |
990 | default: |
1003 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
991 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
1004 | } |
992 | } |
1005 | |
993 | |
1006 | } |
994 | } |
… | |
… | |
1078 | const_iv (BER_TYPE_IPADDRESS) |
1066 | const_iv (BER_TYPE_IPADDRESS) |
1079 | const_iv (BER_TYPE_CROAK) |
1067 | const_iv (BER_TYPE_CROAK) |
1080 | |
1068 | |
1081 | const_iv (SNMP_IPADDRESS) |
1069 | const_iv (SNMP_IPADDRESS) |
1082 | const_iv (SNMP_COUNTER32) |
1070 | const_iv (SNMP_COUNTER32) |
|
|
1071 | const_iv (SNMP_GAUGE32) |
1083 | const_iv (SNMP_UNSIGNED32) |
1072 | const_iv (SNMP_UNSIGNED32) |
1084 | const_iv (SNMP_TIMETICKS) |
1073 | const_iv (SNMP_TIMETICKS) |
1085 | const_iv (SNMP_OPAQUE) |
1074 | const_iv (SNMP_OPAQUE) |
1086 | const_iv (SNMP_COUNTER64) |
1075 | const_iv (SNMP_COUNTER64) |
1087 | }; |
1076 | }; |
1088 | |
1077 | |
1089 | 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--) |
1090 | newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); |
1079 | newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); |
1091 | } |
1080 | } |
1092 | |
1081 | |
1093 | SV * |
1082 | void |
1094 | 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 |
1095 | CODE: |
1086 | PPCODE: |
1096 | { |
1087 | { |
1097 | cur_profile = SvPROFILE (profile); |
1088 | cur_profile = SvPROFILE (profile); |
1098 | STRLEN len; |
1089 | STRLEN len; |
1099 | buf = (U8 *)SvPVbyte (ber, len); |
1090 | buf = (U8 *)SvPVbyte (ber, len); |
1100 | cur = buf; |
1091 | cur = buf; |
1101 | end = buf + len; |
1092 | end = buf + len; |
1102 | |
1093 | |
1103 | 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"); |
1104 | } |
1103 | } |
1105 | OUTPUT: RETVAL |
|
|
1106 | |
1104 | |
1107 | void |
1105 | void |
1108 | 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) |
1109 | PPCODE: |
1107 | PPCODE: |
1110 | { |
1108 | { |