… | |
… | |
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_CONSTRUCTED = 2, |
90 | BER_FLAGS = 2, |
88 | BER_DATA = 3, |
91 | BER_DATA = 3, |
89 | BER_ARRAYSIZE |
92 | BER_ARRAYSIZE |
90 | }; |
93 | }; |
91 | |
94 | |
92 | #define MAX_OID_STRLEN 4096 |
95 | #define MAX_OID_STRLEN 4096 |
93 | |
96 | |
… | |
… | |
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 | { |
463 | UV len = get_length (); |
475 | UV len = get_length (); |
464 | UV seqend = (cur - buf) + len; |
476 | UV seqend = (cur - buf) + len; |
465 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
477 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
466 | |
478 | |
467 | while (cur < buf + seqend) |
479 | while (cur < buf + seqend) |
468 | av_push (av, decode_ber ()); |
480 | av_push (av, decode_ber ()); |
469 | |
481 | |
470 | if (cur > buf + seqend) |
482 | if (expect_false (cur > buf + seqend)) |
471 | 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); |
472 | |
484 | |
473 | res = newRV_inc ((SV *)av); |
485 | res = newRV_inc ((SV *)av); |
474 | } |
486 | } |
475 | else |
487 | else |
|
|
488 | { |
|
|
489 | UV len = get_length (); |
|
|
490 | |
476 | switch (profile_lookup (cur_profile, klass, tag)) |
491 | switch (profile_lookup (cur_profile, klass, tag)) |
477 | { |
492 | { |
478 | case BER_TYPE_NULL: |
493 | case BER_TYPE_NULL: |
479 | { |
494 | 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); |
495 | croak ("BER_TYPE_NULL value with non-zero length %d encountered (X.690 8.8.2)", len); |
484 | |
496 | |
485 | res = &PL_sv_undef; |
497 | res = &PL_sv_undef; |
486 | } |
|
|
487 | break; |
498 | break; |
488 | |
499 | |
489 | case BER_TYPE_BOOL: |
500 | case BER_TYPE_BOOL: |
490 | { |
|
|
491 | UV len = get_length (); |
|
|
492 | |
|
|
493 | if (len != 1) |
501 | if (expect_false (len != 1)) |
494 | 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); |
495 | |
503 | |
496 | res = newSVcacheint (!!get_u8 ()); |
504 | res = newSVcacheint (!!get_u8 ()); |
497 | } |
|
|
498 | break; |
505 | break; |
499 | |
506 | |
500 | case BER_TYPE_OID: |
507 | case BER_TYPE_OID: |
501 | res = decode_oid (0); |
508 | res = decode_oid (len, 0); |
502 | break; |
509 | break; |
503 | |
510 | |
504 | case BER_TYPE_RELOID: |
511 | case BER_TYPE_RELOID: |
505 | res = decode_oid (1); |
512 | res = decode_oid (len, 1); |
506 | break; |
513 | break; |
507 | |
514 | |
508 | case BER_TYPE_INT: |
515 | case BER_TYPE_INT: |
509 | res = decode_int (); |
516 | res = decode_int (len); |
510 | break; |
517 | break; |
511 | |
518 | |
512 | case BER_TYPE_UTF8: |
519 | case BER_TYPE_UTF8: |
513 | res = decode_data (); |
520 | res = decode_data (len); |
514 | SvUTF8_on (res); |
521 | SvUTF8_on (res); |
515 | break; |
522 | break; |
516 | |
523 | |
517 | case BER_TYPE_BYTES: |
524 | case BER_TYPE_BYTES: |
518 | res = decode_data (); |
525 | res = decode_data (len); |
519 | break; |
526 | break; |
520 | |
527 | |
521 | case BER_TYPE_IPADDRESS: |
528 | case BER_TYPE_IPADDRESS: |
522 | { |
529 | { |
523 | UV len = get_length (); |
|
|
524 | |
|
|
525 | if (len != 4) |
530 | if (len != 4) |
526 | 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); |
527 | |
532 | |
528 | U8 c1 = get_u8 (); |
533 | U8 *data = get_n (4); |
529 | U8 c2 = get_u8 (); |
534 | 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 | } |
535 | } |
535 | break; |
536 | break; |
536 | |
537 | |
537 | case BER_TYPE_UCS2: |
538 | case BER_TYPE_UCS2: |
538 | res = decode_ucs (2); |
539 | res = decode_ucs (len, 2); |
539 | break; |
540 | break; |
540 | |
541 | |
541 | case BER_TYPE_UCS4: |
542 | case BER_TYPE_UCS4: |
542 | res = decode_ucs (4); |
543 | res = decode_ucs (len, 4); |
543 | break; |
544 | break; |
544 | |
545 | |
545 | case BER_TYPE_REAL: |
546 | case BER_TYPE_REAL: |
|
|
547 | error ("BER_TYPE_REAL not implemented"); |
|
|
548 | |
546 | case BER_TYPE_CROAK: |
549 | case BER_TYPE_CROAK: |
|
|
550 | croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag); |
|
|
551 | |
547 | default: |
552 | default: |
548 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
553 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
549 | } |
554 | } |
|
|
555 | } |
550 | |
556 | |
551 | AV *av = newAV (); |
557 | AV *av = newAV (); |
552 | av_fill (av, BER_ARRAYSIZE - 1); |
558 | av_fill (av, BER_ARRAYSIZE - 1); |
553 | AvARRAY (av)[BER_CLASS ] = newSVcacheint (klass); |
559 | AvARRAY (av)[BER_CLASS] = newSVcacheint (klass); |
554 | AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); |
560 | AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); |
555 | AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (constructed ? 1 : 0); |
561 | AvARRAY (av)[BER_FLAGS] = newSVcacheint (constructed ? 1 : 0); |
556 | AvARRAY (av)[BER_DATA ] = res; |
562 | AvARRAY (av)[BER_DATA ] = res; |
557 | |
563 | |
558 | return newRV_noinc ((SV *)av); |
564 | return newRV_noinc ((SV *)av); |
559 | } |
565 | } |
560 | |
566 | |
561 | ///////////////////////////////////////////////////////////////////////////// |
567 | ///////////////////////////////////////////////////////////////////////////// |
… | |
… | |
566 | strlen_sum (STRLEN l1, STRLEN l2) |
572 | strlen_sum (STRLEN l1, STRLEN l2) |
567 | { |
573 | { |
568 | size_t sum = l1 + l2; |
574 | size_t sum = l1 + l2; |
569 | |
575 | |
570 | if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) |
576 | if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) |
571 | croak ("JSON::XS: string size overflow"); |
577 | croak ("Convert::BER::XS: string size overflow"); |
572 | |
578 | |
573 | return sum; |
579 | return sum; |
574 | } |
580 | } |
575 | |
581 | |
576 | static void |
582 | static void |
… | |
… | |
641 | } |
647 | } |
642 | |
648 | |
643 | static U8 * |
649 | static U8 * |
644 | put_length_at (UV val, U8 *cur) |
650 | put_length_at (UV val, U8 *cur) |
645 | { |
651 | { |
646 | if (val < 0x7fU) |
652 | if (val <= 0x7fU) |
647 | *cur++ = val; |
653 | *cur++ = val; |
648 | else |
654 | else |
649 | { |
655 | { |
650 | U8 *lenb = cur++; |
656 | U8 *lenb = cur++; |
651 | |
657 | |
652 | #if UVSIZE > 4 |
658 | #if UVSIZE > 4 |
653 | *cur = val >> 56; cur += *cur > 0; |
659 | *cur = val >> 56; cur += val >= ((UV)1 << (8 * 7)); |
654 | *cur = val >> 48; cur += *cur > 0; |
660 | *cur = val >> 48; cur += val >= ((UV)1 << (8 * 6)); |
655 | *cur = val >> 40; cur += *cur > 0; |
661 | *cur = val >> 40; cur += val >= ((UV)1 << (8 * 5)); |
656 | *cur = val >> 32; cur += *cur > 0; |
662 | *cur = val >> 32; cur += val >= ((UV)1 << (8 * 4)); |
657 | #endif |
663 | #endif |
658 | *cur = val >> 24; cur += *cur > 0; |
664 | *cur = val >> 24; cur += val >= ((UV)1 << (8 * 3)); |
659 | *cur = val >> 16; cur += *cur > 0; |
665 | *cur = val >> 16; cur += val >= ((UV)1 << (8 * 2)); |
660 | *cur = val >> 8; cur += *cur > 0; |
666 | *cur = val >> 8; cur += val >= ((UV)1 << (8 * 1)); |
661 | *cur = val ; cur += 1; |
667 | *cur = val ; cur += 1; |
662 | |
668 | |
663 | *lenb = 0x80 + cur - lenb - 1; |
669 | *lenb = 0x80 + cur - lenb - 1; |
664 | } |
670 | } |
665 | |
671 | |
… | |
… | |
667 | } |
673 | } |
668 | |
674 | |
669 | static void |
675 | static void |
670 | put_length (UV val) |
676 | put_length (UV val) |
671 | { |
677 | { |
672 | need (5 + val); |
678 | need (9 + val); |
673 | cur = put_length_at (val, cur); |
679 | cur = put_length_at (val, cur); |
674 | } |
680 | } |
675 | |
681 | |
676 | // return how many bytes the encoded length requires |
682 | // return how many bytes the encoded length requires |
677 | static int length_length (UV val) |
683 | static int length_length (UV val) |
678 | { |
684 | { |
679 | return val < 0x7fU |
685 | // use hashing with a DeBruin sequence, anyone? |
|
|
686 | return expect_true (val <= 0x7fU) |
680 | ? 1 |
687 | ? 1 |
681 | : 2 |
688 | : 2 |
682 | + (val > 0xffU) |
689 | + (val > 0x000000000000ffU) |
683 | + (val > 0xffffU) |
690 | + (val > 0x0000000000ffffU) |
684 | + (val > 0xffffffU) |
691 | + (val > 0x00000000ffffffU) |
685 | #if UVSIZE > 4 |
692 | #if UVSIZE > 4 |
686 | + (val > 0xffffffffU) |
693 | + (val > 0x000000ffffffffU) |
687 | + (val > 0xffffffffffU) |
694 | + (val > 0x0000ffffffffffU) |
688 | + (val > 0xffffffffffffU) |
695 | + (val > 0x00ffffffffffffU) |
689 | + (val > 0xffffffffffffffU) |
696 | + (val > 0xffffffffffffffU) |
690 | #endif |
697 | #endif |
691 | ; |
698 | ; |
692 | } |
699 | } |
693 | |
700 | |
… | |
… | |
881 | { |
888 | { |
882 | AV *av = ber_tuple (tuple); |
889 | AV *av = ber_tuple (tuple); |
883 | |
890 | |
884 | int klass = SvIV (AvARRAY (av)[BER_CLASS]); |
891 | int klass = SvIV (AvARRAY (av)[BER_CLASS]); |
885 | int tag = SvIV (AvARRAY (av)[BER_TAG]); |
892 | int tag = SvIV (AvARRAY (av)[BER_TAG]); |
886 | int constructed = SvIV (AvARRAY (av)[BER_CONSTRUCTED]) ? ASN_CONSTRUCTED : 0; |
893 | int constructed = SvIV (AvARRAY (av)[BER_FLAGS]) & 1 ? ASN_CONSTRUCTED : 0; |
887 | SV *data = AvARRAY (av)[BER_DATA]; |
894 | SV *data = AvARRAY (av)[BER_DATA]; |
888 | |
895 | |
889 | int identifier = (klass << ASN_CLASS_SHIFT) | constructed; |
896 | int identifier = (klass << ASN_CLASS_SHIFT) | constructed; |
890 | |
897 | |
891 | if (expect_false (tag >= ASN_TAG_BER)) |
898 | if (expect_false (tag >= ASN_TAG_BER)) |
… | |
… | |
902 | // and adjust later |
909 | // and adjust later |
903 | need (1); |
910 | need (1); |
904 | STRLEN mark = len_fixup_mark (); |
911 | STRLEN mark = len_fixup_mark (); |
905 | |
912 | |
906 | if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV)) |
913 | if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV)) |
907 | croak ("BER constructed data must be array-reference"); |
914 | croak ("BER CONSTRUCTED data must be array-reference"); |
908 | |
915 | |
909 | AV *av = (AV *)SvRV (data); |
916 | AV *av = (AV *)SvRV (data); |
910 | int fill = AvFILL (av); |
917 | int fill = AvFILL (av); |
911 | |
918 | |
912 | if (expect_false (SvRMAGICAL (av))) |
919 | if (expect_false (SvRMAGICAL (av))) |
913 | croak ("BER constructed data must not be tied"); |
920 | croak ("BER CONSTRUCTED data must not be tied"); |
914 | |
921 | |
915 | int i; |
922 | int i; |
916 | for (i = 0; i <= fill; ++i) |
923 | for (i = 0; i <= fill; ++i) |
917 | encode_ber (AvARRAY (av)[i]); |
924 | encode_ber (AvARRAY (av)[i]); |
918 | |
925 | |
… | |
… | |
925 | put_length (0); |
932 | put_length (0); |
926 | break; |
933 | break; |
927 | |
934 | |
928 | case BER_TYPE_BOOL: |
935 | case BER_TYPE_BOOL: |
929 | put_length (1); |
936 | put_length (1); |
930 | *cur++ = SvTRUE (data) ? 0xff : 0x00; |
937 | *cur++ = SvTRUE (data) ? 0xff : 0x00; // 0xff = DER/CER |
931 | break; |
938 | break; |
932 | |
939 | |
933 | case BER_TYPE_OID: |
940 | case BER_TYPE_OID: |
934 | encode_oid (data, 0); |
941 | encode_oid (data, 0); |
935 | break; |
942 | break; |
… | |
… | |
973 | case BER_TYPE_UCS4: |
980 | case BER_TYPE_UCS4: |
974 | encode_ucs (data, 4); |
981 | encode_ucs (data, 4); |
975 | break; |
982 | break; |
976 | |
983 | |
977 | case BER_TYPE_REAL: |
984 | case BER_TYPE_REAL: |
|
|
985 | croak ("BER_TYPE_REAL not implemented"); |
|
|
986 | |
978 | case BER_TYPE_CROAK: |
987 | case BER_TYPE_CROAK: |
|
|
988 | croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag); |
|
|
989 | |
979 | default: |
990 | default: |
980 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
991 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
981 | } |
992 | } |
982 | |
993 | |
983 | } |
994 | } |
… | |
… | |
1037 | const_iv (ASN_CONTEXT) |
1048 | const_iv (ASN_CONTEXT) |
1038 | const_iv (ASN_PRIVATE) |
1049 | const_iv (ASN_PRIVATE) |
1039 | |
1050 | |
1040 | const_iv (BER_CLASS) |
1051 | const_iv (BER_CLASS) |
1041 | const_iv (BER_TAG) |
1052 | const_iv (BER_TAG) |
1042 | const_iv (BER_CONSTRUCTED) |
1053 | const_iv (BER_FLAGS) |
1043 | const_iv (BER_DATA) |
1054 | const_iv (BER_DATA) |
1044 | |
1055 | |
1045 | const_iv (BER_TYPE_BYTES) |
1056 | const_iv (BER_TYPE_BYTES) |
1046 | const_iv (BER_TYPE_UTF8) |
1057 | const_iv (BER_TYPE_UTF8) |
1047 | const_iv (BER_TYPE_UCS2) |
1058 | const_iv (BER_TYPE_UCS2) |
… | |
… | |
1055 | const_iv (BER_TYPE_IPADDRESS) |
1066 | const_iv (BER_TYPE_IPADDRESS) |
1056 | const_iv (BER_TYPE_CROAK) |
1067 | const_iv (BER_TYPE_CROAK) |
1057 | |
1068 | |
1058 | const_iv (SNMP_IPADDRESS) |
1069 | const_iv (SNMP_IPADDRESS) |
1059 | const_iv (SNMP_COUNTER32) |
1070 | const_iv (SNMP_COUNTER32) |
|
|
1071 | const_iv (SNMP_GAUGE32) |
1060 | const_iv (SNMP_UNSIGNED32) |
1072 | const_iv (SNMP_UNSIGNED32) |
1061 | const_iv (SNMP_TIMETICKS) |
1073 | const_iv (SNMP_TIMETICKS) |
1062 | const_iv (SNMP_OPAQUE) |
1074 | const_iv (SNMP_OPAQUE) |
1063 | const_iv (SNMP_COUNTER64) |
1075 | const_iv (SNMP_COUNTER64) |
1064 | }; |
1076 | }; |
1065 | |
1077 | |
1066 | 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--) |
1067 | newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); |
1079 | newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); |
1068 | } |
1080 | } |
1069 | |
1081 | |
1070 | SV * |
1082 | void |
1071 | 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 |
1072 | CODE: |
1086 | PPCODE: |
1073 | { |
1087 | { |
1074 | cur_profile = SvPROFILE (profile); |
1088 | cur_profile = SvPROFILE (profile); |
1075 | STRLEN len; |
1089 | STRLEN len; |
1076 | buf = (U8 *)SvPVbyte (ber, len); |
1090 | buf = (U8 *)SvPVbyte (ber, len); |
1077 | cur = buf; |
1091 | cur = buf; |
1078 | end = buf + len; |
1092 | end = buf + len; |
1079 | |
1093 | |
1080 | 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"); |
1081 | } |
1103 | } |
1082 | OUTPUT: RETVAL |
|
|
1083 | |
1104 | |
1084 | void |
1105 | void |
1085 | ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *constructed = &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) |
1086 | PPCODE: |
1107 | PPCODE: |
1087 | { |
1108 | { |
1088 | if (!SvOK (tuple)) |
1109 | if (!SvOK (tuple)) |
1089 | XSRETURN_NO; |
1110 | XSRETURN_NO; |
1090 | |
1111 | |
… | |
… | |
1092 | croak ("ber_is: tuple must be BER tuple (array-ref)"); |
1113 | croak ("ber_is: tuple must be BER tuple (array-ref)"); |
1093 | |
1114 | |
1094 | AV *av = (AV *)SvRV (tuple); |
1115 | AV *av = (AV *)SvRV (tuple); |
1095 | |
1116 | |
1096 | XPUSHs ( |
1117 | XPUSHs ( |
1097 | (!SvOK (klass) || SvIV (AvARRAY (av)[BER_CLASS ]) == SvIV (klass)) |
1118 | (!SvOK (klass) || SvIV (AvARRAY (av)[BER_CLASS]) == SvIV (klass)) |
1098 | && (!SvOK (tag) || SvIV (AvARRAY (av)[BER_TAG ]) == SvIV (tag)) |
1119 | && (!SvOK (tag) || SvIV (AvARRAY (av)[BER_TAG ]) == SvIV (tag)) |
1099 | && (!SvOK (constructed) || !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) == !SvIV (constructed)) |
1120 | && (!SvOK (flags) || !SvIV (AvARRAY (av)[BER_FLAGS]) == !SvIV (flags)) |
1100 | && (!SvOK (data) || sv_eq (AvARRAY (av)[BER_DATA ], data)) |
1121 | && (!SvOK (data) || sv_eq (AvARRAY (av)[BER_DATA ], data)) |
1101 | ? &PL_sv_yes : &PL_sv_undef); |
1122 | ? &PL_sv_yes : &PL_sv_undef); |
1102 | } |
1123 | } |
1103 | |
1124 | |
1104 | void |
1125 | void |
1105 | ber_is_seq (SV *tuple) |
1126 | ber_is_seq (SV *tuple) |
… | |
… | |
1109 | XSRETURN_UNDEF; |
1130 | XSRETURN_UNDEF; |
1110 | |
1131 | |
1111 | AV *av = ber_tuple (tuple); |
1132 | AV *av = ber_tuple (tuple); |
1112 | |
1133 | |
1113 | XPUSHs ( |
1134 | XPUSHs ( |
1114 | SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL |
1135 | SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL |
1115 | && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_SEQUENCE |
1136 | && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_SEQUENCE |
1116 | && SvIV (AvARRAY (av)[BER_CONSTRUCTED]) |
1137 | && SvIV (AvARRAY (av)[BER_FLAGS]) |
1117 | ? AvARRAY (av)[BER_DATA] : &PL_sv_undef); |
1138 | ? AvARRAY (av)[BER_DATA] : &PL_sv_undef); |
1118 | } |
1139 | } |
1119 | |
1140 | |
1120 | void |
1141 | void |
1121 | ber_is_int (SV *tuple, SV *value = &PL_sv_undef) |
1142 | ber_is_int (SV *tuple, SV *value = &PL_sv_undef) |
… | |
… | |
1127 | AV *av = ber_tuple (tuple); |
1148 | AV *av = ber_tuple (tuple); |
1128 | |
1149 | |
1129 | UV data = SvUV (AvARRAY (av)[BER_DATA]); |
1150 | UV data = SvUV (AvARRAY (av)[BER_DATA]); |
1130 | |
1151 | |
1131 | XPUSHs ( |
1152 | XPUSHs ( |
1132 | SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL |
1153 | SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL |
1133 | && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_INTEGER |
1154 | && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_INTEGER |
1134 | && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) |
1155 | && !SvIV (AvARRAY (av)[BER_FLAGS]) |
1135 | && (!SvOK (value) || data == SvUV (value)) |
1156 | && (!SvOK (value) || data == SvUV (value)) |
1136 | ? sv_2mortal (data ? newSVsv (AvARRAY (av)[BER_DATA]) : newSVpv ("0 but true", 0)) |
1157 | ? sv_2mortal (data ? newSVsv (AvARRAY (av)[BER_DATA]) : newSVpv ("0 but true", 0)) |
1137 | : &PL_sv_undef); |
1158 | : &PL_sv_undef); |
1138 | } |
1159 | } |
1139 | |
1160 | |
… | |
… | |
1145 | XSRETURN_NO; |
1166 | XSRETURN_NO; |
1146 | |
1167 | |
1147 | AV *av = ber_tuple (tuple); |
1168 | AV *av = ber_tuple (tuple); |
1148 | |
1169 | |
1149 | XPUSHs ( |
1170 | XPUSHs ( |
1150 | SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL |
1171 | SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL |
1151 | && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_OBJECT_IDENTIFIER |
1172 | && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_OBJECT_IDENTIFIER |
1152 | && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) |
1173 | && !SvIV (AvARRAY (av)[BER_FLAGS]) |
1153 | && (!SvOK (oid) || sv_eq (AvARRAY (av)[BER_DATA], oid)) |
1174 | && (!SvOK (oid) || sv_eq (AvARRAY (av)[BER_DATA], oid)) |
1154 | ? newSVsv (AvARRAY (av)[BER_DATA]) : &PL_sv_undef); |
1175 | ? newSVsv (AvARRAY (av)[BER_DATA]) : &PL_sv_undef); |
1155 | } |
1176 | } |
1156 | |
1177 | |
1157 | ############################################################################# |
1178 | ############################################################################# |
… | |
… | |
1175 | ber_int (SV *sv) |
1196 | ber_int (SV *sv) |
1176 | CODE: |
1197 | CODE: |
1177 | { |
1198 | { |
1178 | AV *av = newAV (); |
1199 | AV *av = newAV (); |
1179 | av_fill (av, BER_ARRAYSIZE - 1); |
1200 | av_fill (av, BER_ARRAYSIZE - 1); |
1180 | AvARRAY (av)[BER_CLASS ] = newSVcacheint (ASN_UNIVERSAL); |
1201 | AvARRAY (av)[BER_CLASS] = newSVcacheint (ASN_UNIVERSAL); |
1181 | AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_INTEGER); |
1202 | AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_INTEGER); |
1182 | AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (0); |
1203 | AvARRAY (av)[BER_FLAGS] = newSVcacheint (0); |
1183 | AvARRAY (av)[BER_DATA ] = newSVsv (sv); |
1204 | AvARRAY (av)[BER_DATA ] = newSVsv (sv); |
1184 | RETVAL = newRV_noinc ((SV *)av); |
1205 | RETVAL = newRV_noinc ((SV *)av); |
1185 | } |
1206 | } |
1186 | OUTPUT: RETVAL |
1207 | OUTPUT: RETVAL |
1187 | |
1208 | |
1188 | # TODO: not arrayref, but elements? |
1209 | # TODO: not arrayref, but elements? |
… | |
… | |
1190 | ber_seq (SV *arrayref) |
1211 | ber_seq (SV *arrayref) |
1191 | CODE: |
1212 | CODE: |
1192 | { |
1213 | { |
1193 | AV *av = newAV (); |
1214 | AV *av = newAV (); |
1194 | av_fill (av, BER_ARRAYSIZE - 1); |
1215 | av_fill (av, BER_ARRAYSIZE - 1); |
1195 | AvARRAY (av)[BER_CLASS ] = newSVcacheint (ASN_UNIVERSAL); |
1216 | AvARRAY (av)[BER_CLASS] = newSVcacheint (ASN_UNIVERSAL); |
1196 | AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_SEQUENCE); |
1217 | AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_SEQUENCE); |
1197 | AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (1); |
1218 | AvARRAY (av)[BER_FLAGS] = newSVcacheint (1); |
1198 | AvARRAY (av)[BER_DATA ] = newSVsv (arrayref); |
1219 | AvARRAY (av)[BER_DATA ] = newSVsv (arrayref); |
1199 | RETVAL = newRV_noinc ((SV *)av); |
1220 | RETVAL = newRV_noinc ((SV *)av); |
1200 | } |
1221 | } |
1201 | OUTPUT: RETVAL |
1222 | OUTPUT: RETVAL |
1202 | |
1223 | |
1203 | MODULE = Convert::BER::XS PACKAGE = Convert::BER::XS::Profile |
1224 | MODULE = Convert::BER::XS PACKAGE = Convert::BER::XS::Profile |