… | |
… | |
141 | { |
141 | { |
142 | if (!SvOK (profile)) |
142 | if (!SvOK (profile)) |
143 | return default_profile; |
143 | return default_profile; |
144 | |
144 | |
145 | if (!SvROK (profile)) |
145 | if (!SvROK (profile)) |
146 | croak ("invalid profile"); |
146 | croak ("Convert::BER::XS::Profile expected"); |
147 | |
147 | |
148 | profile = SvRV (profile); |
148 | profile = SvRV (profile); |
149 | |
149 | |
150 | if (SvSTASH (profile) != profile_stash) |
150 | if (SvSTASH (profile) != profile_stash) |
151 | croak ("invalid profile object"); |
151 | croak ("Convert::BER::XS::Profile expected"); |
152 | |
152 | |
153 | return (void *)profile; |
153 | return (void *)profile; |
154 | } |
154 | } |
155 | |
155 | |
156 | static int |
156 | static int |
… | |
… | |
228 | } |
228 | } |
229 | |
229 | |
230 | // get_* functions fetch something from the buffer |
230 | // get_* functions fetch something from the buffer |
231 | // decode_* functions use get_* fun ctions to decode ber values |
231 | // decode_* functions use get_* fun ctions to decode ber values |
232 | |
232 | |
|
|
233 | // get single octet |
|
|
234 | static U8 |
|
|
235 | get_u8 (void) |
|
|
236 | { |
|
|
237 | if (cur == end) |
|
|
238 | error ("unexpected end of message buffer"); |
|
|
239 | |
|
|
240 | return *cur++; |
|
|
241 | } |
|
|
242 | |
233 | // get n octets |
243 | // get n octets |
234 | static U8 * |
244 | static U8 * |
235 | get_n (UV count) |
245 | get_n (UV count) |
236 | { |
246 | { |
237 | want (count); |
247 | want (count); |
238 | U8 *res = cur; |
248 | U8 *res = cur; |
239 | cur += count; |
249 | cur += count; |
240 | return res; |
250 | return res; |
241 | } |
251 | } |
242 | |
252 | |
243 | // get single octet |
|
|
244 | static U8 |
|
|
245 | get_u8 (void) |
|
|
246 | { |
|
|
247 | if (cur == end) |
|
|
248 | error ("unexpected end of message buffer"); |
|
|
249 | |
|
|
250 | return *cur++; |
|
|
251 | } |
|
|
252 | |
|
|
253 | // get ber-encoded integer (i.e. pack "w") |
253 | // get ber-encoded integer (i.e. pack "w") |
254 | static UV |
254 | static UV |
255 | get_w (void) |
255 | get_w (void) |
256 | { |
256 | { |
257 | UV res = 0; |
257 | UV res = 0; |
… | |
… | |
261 | error ("illegal BER padding (X.690 8.1.2.4.2, 8.19.2)"); |
261 | error ("illegal BER padding (X.690 8.1.2.4.2, 8.19.2)"); |
262 | |
262 | |
263 | for (;;) |
263 | for (;;) |
264 | { |
264 | { |
265 | if (expect_false (res >> UVSIZE * 8 - 7)) |
265 | if (expect_false (res >> UVSIZE * 8 - 7)) |
266 | error ("BER variable integer overflow"); |
266 | error ("BER variable length integer overflow"); |
267 | |
267 | |
268 | res = (res << 7) | (c & 0x7f); |
268 | res = (res << 7) | (c & 0x7f); |
269 | |
269 | |
270 | if (!(c & 0x80)) |
270 | if (expect_true (!(c & 0x80))) |
271 | return res; |
271 | return res; |
272 | |
272 | |
273 | c = get_u8 (); |
273 | c = get_u8 (); |
274 | } |
274 | } |
275 | } |
275 | } |
… | |
… | |
277 | static UV |
277 | static UV |
278 | get_length (void) |
278 | get_length (void) |
279 | { |
279 | { |
280 | UV res = get_u8 (); |
280 | UV res = get_u8 (); |
281 | |
281 | |
282 | if (res & 0x80) |
282 | if (expect_false (res & 0x80)) |
283 | { |
283 | { |
284 | int cnt = res & 0x7f; |
284 | U8 cnt = res & 0x7f; |
|
|
285 | |
|
|
286 | // this genewrates quite ugly code, but the overhead |
|
|
287 | // of copying the bytes for these lengths is probably so high |
|
|
288 | // that a slightly inefficient get_length won't matter. |
|
|
289 | |
|
|
290 | if (expect_false (cnt == 0)) |
|
|
291 | error ("indefinite BER value lengths not supported"); |
|
|
292 | |
|
|
293 | if (expect_false (cnt > UVSIZE)) |
|
|
294 | error ("BER value length too long (must fit into UV) or BER reserved value in length (X.690 8.1.3.5)"); |
|
|
295 | |
|
|
296 | want (cnt); |
|
|
297 | |
285 | res = 0; |
298 | res = 0; |
286 | |
299 | do |
287 | switch (cnt) |
300 | res = (res << 8) | *cur++; |
288 | { |
301 | while (--cnt); |
289 | case 0: |
|
|
290 | error ("indefinite ASN.1 lengths not supported"); |
|
|
291 | |
|
|
292 | case 0x7f: |
|
|
293 | error ("ASN.1 reserved value in length (X.690 8.1.3.5)"); |
|
|
294 | |
|
|
295 | default: |
|
|
296 | error ("ASN.1 length too long (only up to 2**64 octets supported)"); |
|
|
297 | |
|
|
298 | case 8: res = (res << 8) | get_u8 (); |
|
|
299 | case 7: res = (res << 8) | get_u8 (); |
|
|
300 | case 6: res = (res << 8) | get_u8 (); |
|
|
301 | case 5: res = (res << 8) | get_u8 (); |
|
|
302 | case 4: res = (res << 8) | get_u8 (); |
|
|
303 | case 3: res = (res << 8) | get_u8 (); |
|
|
304 | case 2: res = (res << 8) | get_u8 (); |
|
|
305 | case 1: res = (res << 8) | get_u8 (); |
|
|
306 | } |
|
|
307 | } |
302 | } |
308 | |
303 | |
309 | return res; |
304 | return res; |
310 | } |
305 | } |
311 | |
306 | |
… | |
… | |
313 | decode_int (void) |
308 | decode_int (void) |
314 | { |
309 | { |
315 | UV len = get_length (); |
310 | UV len = get_length (); |
316 | |
311 | |
317 | if (!len) |
312 | if (!len) |
318 | 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)"); |
319 | |
314 | |
320 | U8 *data = get_n (len); |
315 | U8 *data = get_n (len); |
321 | |
316 | |
322 | if (expect_false (len > 1)) |
317 | if (expect_false (len > 1)) |
323 | { |
318 | { |
324 | U16 mask = (data [0] << 8) | data [1] & 0xff80; |
319 | U16 mask = (data [0] << 8) | data [1] & 0xff80; |
325 | |
320 | |
326 | if (expect_false (mask == 0xff80 || mask == 0x0000)) |
321 | if (expect_false (mask == 0xff80 || mask == 0x0000)) |
327 | error ("illegal padding in integer (X.690 8.3.2)"); |
322 | error ("illegal padding in BER_TYPE_INT (X.690 8.3.2)"); |
328 | } |
323 | } |
329 | |
324 | |
330 | int negative = data [0] & 0x80; |
325 | int negative = data [0] & 0x80; |
331 | |
326 | |
332 | UV val = negative ? -1 : 0; // copy signbit to all bits |
327 | UV val = negative ? -1 : 0; // copy signbit to all bits |
333 | |
328 | |
334 | if (len > UVSIZE + (!negative && !*data)) |
329 | if (len > UVSIZE + (!negative && !*data)) |
335 | //printf ("len %d > %d + (!%d && !%d) = %d\n", len, UVSIZE, negative, *data, UVSIZE + (!negative && !*data));//D |
|
|
336 | error ("INTEGER overflow"); |
330 | error ("BER_TYPE_INT overflow"); |
337 | |
331 | |
338 | do |
332 | do |
339 | val = (val << 8) | *data++; |
333 | val = (val << 8) | *data++; |
340 | while (--len); |
334 | while (--len); |
341 | |
335 | |
… | |
… | |
389 | { |
383 | { |
390 | UV len = get_length (); |
384 | UV len = get_length (); |
391 | |
385 | |
392 | if (len <= 0) |
386 | if (len <= 0) |
393 | { |
387 | { |
394 | error ("OBJECT IDENTIFIER length equal to zero"); |
388 | error ("BER_TYPE_OID length must not be zero"); |
395 | return &PL_sv_undef; |
389 | return &PL_sv_undef; |
396 | } |
390 | } |
397 | |
391 | |
398 | U8 *end = cur + len; |
392 | U8 *end = cur + len; |
399 | UV w = get_w (); |
393 | UV w = get_w (); |
… | |
… | |
401 | static char oid[MAX_OID_STRLEN]; // static, because too large for stack |
395 | static char oid[MAX_OID_STRLEN]; // static, because too large for stack |
402 | char *app = oid; |
396 | char *app = oid; |
403 | |
397 | |
404 | if (relative) |
398 | if (relative) |
405 | app = write_uv (app, w); |
399 | app = write_uv (app, w); |
|
|
400 | else |
|
|
401 | { |
|
|
402 | UV w1, w2; |
|
|
403 | |
406 | else if (w < 2 * 40) |
404 | if (w < 2 * 40) |
407 | { |
405 | (w1 = w / 40), (w2 = w % 40); |
|
|
406 | else |
|
|
407 | (w1 = 2), (w2 = w - 2 * 40); |
|
|
408 | |
408 | app = write_uv (app, (U8)w / 40); |
409 | app = write_uv (app, w1); |
409 | *app++ = '.'; |
410 | *app++ = '.'; |
410 | app = write_uv (app, (U8)w % 40); |
|
|
411 | } |
|
|
412 | else |
|
|
413 | { |
|
|
414 | app = write_uv (app, 2); |
411 | app = write_uv (app, w2); |
415 | *app++ = '.'; |
|
|
416 | app = write_uv (app, w - 2 * 40); |
|
|
417 | } |
412 | } |
418 | |
413 | |
419 | while (cur < end) |
414 | while (cur < end) |
420 | { |
415 | { |
421 | // we assume an oid component is never > 64 digits |
416 | // we assume an oid component is never > 64 digits |
… | |
… | |
488 | |
483 | |
489 | while (cur < buf + seqend) |
484 | while (cur < buf + seqend) |
490 | av_push (av, decode_ber ()); |
485 | av_push (av, decode_ber ()); |
491 | |
486 | |
492 | if (cur > buf + seqend) |
487 | if (cur > buf + seqend) |
493 | croak ("constructed type %02x length overflow (0x%x 0x%x)\n", identifier, (int)(cur - buf), (int)seqend); |
488 | croak ("CONSTRUCTED type %02x length overflow (0x%x 0x%x)\n", identifier, (int)(cur - buf), (int)seqend); |
494 | |
489 | |
495 | res = newRV_inc ((SV *)av); |
490 | res = newRV_inc ((SV *)av); |
496 | } |
491 | } |
497 | else |
492 | else |
498 | switch (profile_lookup (cur_profile, klass, tag)) |
493 | switch (profile_lookup (cur_profile, klass, tag)) |
… | |
… | |
545 | UV len = get_length (); |
540 | UV len = get_length (); |
546 | |
541 | |
547 | if (len != 4) |
542 | if (len != 4) |
548 | croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered (RFC 2578 7.1.5)", len); |
543 | croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered (RFC 2578 7.1.5)", len); |
549 | |
544 | |
550 | U8 c1 = get_u8 (); |
545 | U8 *data = get_n (4); |
551 | U8 c2 = get_u8 (); |
546 | res = newSVpvf ("%d.%d.%d.%d", data [0], data [1], data [2], data [3]); |
552 | U8 c3 = get_u8 (); |
|
|
553 | U8 c4 = get_u8 (); |
|
|
554 | |
|
|
555 | res = newSVpvf ("%d.%d.%d.%d", c1, c2, c3, c4); |
|
|
556 | } |
547 | } |
557 | break; |
548 | break; |
558 | |
549 | |
559 | case BER_TYPE_UCS2: |
550 | case BER_TYPE_UCS2: |
560 | res = decode_ucs (2); |
551 | res = decode_ucs (2); |
… | |
… | |
563 | case BER_TYPE_UCS4: |
554 | case BER_TYPE_UCS4: |
564 | res = decode_ucs (4); |
555 | res = decode_ucs (4); |
565 | break; |
556 | break; |
566 | |
557 | |
567 | case BER_TYPE_REAL: |
558 | case BER_TYPE_REAL: |
|
|
559 | error ("BER_TYPE_REAL not implemented"); |
|
|
560 | |
568 | case BER_TYPE_CROAK: |
561 | case BER_TYPE_CROAK: |
|
|
562 | croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag); |
|
|
563 | |
569 | default: |
564 | default: |
570 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
565 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
571 | } |
566 | } |
572 | |
567 | |
573 | AV *av = newAV (); |
568 | AV *av = newAV (); |
… | |
… | |
663 | } |
658 | } |
664 | |
659 | |
665 | static U8 * |
660 | static U8 * |
666 | put_length_at (UV val, U8 *cur) |
661 | put_length_at (UV val, U8 *cur) |
667 | { |
662 | { |
668 | if (val < 0x7fU) |
663 | if (val <= 0x7fU) |
669 | *cur++ = val; |
664 | *cur++ = val; |
670 | else |
665 | else |
671 | { |
666 | { |
672 | U8 *lenb = cur++; |
667 | U8 *lenb = cur++; |
673 | |
668 | |
674 | #if UVSIZE > 4 |
669 | #if UVSIZE > 4 |
675 | *cur = val >> 56; cur += *cur > 0; |
670 | *cur = val >> 56; cur += val >= ((UV)1 << (8 * 7)); |
676 | *cur = val >> 48; cur += *cur > 0; |
671 | *cur = val >> 48; cur += val >= ((UV)1 << (8 * 6)); |
677 | *cur = val >> 40; cur += *cur > 0; |
672 | *cur = val >> 40; cur += val >= ((UV)1 << (8 * 5)); |
678 | *cur = val >> 32; cur += *cur > 0; |
673 | *cur = val >> 32; cur += val >= ((UV)1 << (8 * 4)); |
679 | #endif |
674 | #endif |
680 | *cur = val >> 24; cur += *cur > 0; |
675 | *cur = val >> 24; cur += val >= ((UV)1 << (8 * 3)); |
681 | *cur = val >> 16; cur += *cur > 0; |
676 | *cur = val >> 16; cur += val >= ((UV)1 << (8 * 2)); |
682 | *cur = val >> 8; cur += *cur > 0; |
677 | *cur = val >> 8; cur += val >= ((UV)1 << (8 * 1)); |
683 | *cur = val ; cur += 1; |
678 | *cur = val ; cur += 1; |
684 | |
679 | |
685 | *lenb = 0x80 + cur - lenb - 1; |
680 | *lenb = 0x80 + cur - lenb - 1; |
686 | } |
681 | } |
687 | |
682 | |
… | |
… | |
689 | } |
684 | } |
690 | |
685 | |
691 | static void |
686 | static void |
692 | put_length (UV val) |
687 | put_length (UV val) |
693 | { |
688 | { |
694 | need (5 + val); |
689 | need (9 + val); |
695 | cur = put_length_at (val, cur); |
690 | cur = put_length_at (val, cur); |
696 | } |
691 | } |
697 | |
692 | |
698 | // return how many bytes the encoded length requires |
693 | // return how many bytes the encoded length requires |
699 | static int length_length (UV val) |
694 | static int length_length (UV val) |
700 | { |
695 | { |
701 | return val < 0x7fU |
696 | // use hashing with a DeBruin sequence, anyone? |
|
|
697 | return expect_true (val <= 0x7fU) |
702 | ? 1 |
698 | ? 1 |
703 | : 2 |
699 | : 2 |
704 | + (val > 0xffU) |
700 | + (val > 0x000000000000ffU) |
705 | + (val > 0xffffU) |
701 | + (val > 0x0000000000ffffU) |
706 | + (val > 0xffffffU) |
702 | + (val > 0x00000000ffffffU) |
707 | #if UVSIZE > 4 |
703 | #if UVSIZE > 4 |
708 | + (val > 0xffffffffU) |
704 | + (val > 0x000000ffffffffU) |
709 | + (val > 0xffffffffffU) |
705 | + (val > 0x0000ffffffffffU) |
710 | + (val > 0xffffffffffffU) |
706 | + (val > 0x00ffffffffffffU) |
711 | + (val > 0xffffffffffffffU) |
707 | + (val > 0xffffffffffffffU) |
712 | #endif |
708 | #endif |
713 | ; |
709 | ; |
714 | } |
710 | } |
715 | |
711 | |
… | |
… | |
924 | // and adjust later |
920 | // and adjust later |
925 | need (1); |
921 | need (1); |
926 | STRLEN mark = len_fixup_mark (); |
922 | STRLEN mark = len_fixup_mark (); |
927 | |
923 | |
928 | if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV)) |
924 | if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV)) |
929 | croak ("BER constructed data must be array-reference"); |
925 | croak ("BER CONSTRUCTED data must be array-reference"); |
930 | |
926 | |
931 | AV *av = (AV *)SvRV (data); |
927 | AV *av = (AV *)SvRV (data); |
932 | int fill = AvFILL (av); |
928 | int fill = AvFILL (av); |
933 | |
929 | |
934 | if (expect_false (SvRMAGICAL (av))) |
930 | if (expect_false (SvRMAGICAL (av))) |
935 | croak ("BER constructed data must not be tied"); |
931 | croak ("BER CONSTRUCTED data must not be tied"); |
936 | |
932 | |
937 | int i; |
933 | int i; |
938 | for (i = 0; i <= fill; ++i) |
934 | for (i = 0; i <= fill; ++i) |
939 | encode_ber (AvARRAY (av)[i]); |
935 | encode_ber (AvARRAY (av)[i]); |
940 | |
936 | |
… | |
… | |
995 | case BER_TYPE_UCS4: |
991 | case BER_TYPE_UCS4: |
996 | encode_ucs (data, 4); |
992 | encode_ucs (data, 4); |
997 | break; |
993 | break; |
998 | |
994 | |
999 | case BER_TYPE_REAL: |
995 | case BER_TYPE_REAL: |
|
|
996 | croak ("BER_TYPE_REAL not implemented"); |
|
|
997 | |
1000 | case BER_TYPE_CROAK: |
998 | case BER_TYPE_CROAK: |
|
|
999 | croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag); |
|
|
1000 | |
1001 | default: |
1001 | default: |
1002 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
1002 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
1003 | } |
1003 | } |
1004 | |
1004 | |
1005 | } |
1005 | } |
… | |
… | |
1108 | PUSHs (sv_2mortal (tuple)); |
1108 | PUSHs (sv_2mortal (tuple)); |
1109 | |
1109 | |
1110 | if (ix) |
1110 | if (ix) |
1111 | PUSHs (sv_2mortal (newSViv (cur - buf))); |
1111 | PUSHs (sv_2mortal (newSViv (cur - buf))); |
1112 | else if (cur != end) |
1112 | else if (cur != end) |
1113 | error ("trailing garbage after BER data"); |
1113 | error ("trailing garbage after BER value"); |
1114 | } |
1114 | } |
1115 | |
1115 | |
1116 | void |
1116 | void |
1117 | ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *flags = &PL_sv_undef, SV *data = &PL_sv_undef) |
1117 | ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *flags = &PL_sv_undef, SV *data = &PL_sv_undef) |
1118 | PPCODE: |
1118 | PPCODE: |