1 | #include "EXTERN.h" |
1 | #include "EXTERN.h" |
2 | #include "perl.h" |
2 | #include "perl.h" |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
|
|
4 | |
|
|
5 | #include <math.h> |
4 | |
6 | |
5 | // C99 required! |
7 | // C99 required! |
6 | // this is not just for comments, but also for |
8 | // this is not just for comments, but also for |
7 | // integer constant semantics, |
9 | // integer constant semantics, |
8 | // sscanf format modifiers and more. |
10 | // sscanf format modifiers and more. |
… | |
… | |
65 | SNMP_TIMETICKS = 0x03, |
67 | SNMP_TIMETICKS = 0x03, |
66 | SNMP_OPAQUE = 0x04, |
68 | SNMP_OPAQUE = 0x04, |
67 | SNMP_COUNTER64 = 0x06, |
69 | SNMP_COUNTER64 = 0x06, |
68 | }; |
70 | }; |
69 | |
71 | |
|
|
72 | // tlow-level types this module can ecode the above (and more) into |
70 | enum { |
73 | enum { |
71 | BER_TYPE_BYTES, |
74 | BER_TYPE_BYTES, |
72 | BER_TYPE_UTF8, |
75 | BER_TYPE_UTF8, |
73 | BER_TYPE_UCS2, |
76 | BER_TYPE_UCS2, |
74 | BER_TYPE_UCS4, |
77 | BER_TYPE_UCS4, |
… | |
… | |
80 | BER_TYPE_REAL, |
83 | BER_TYPE_REAL, |
81 | BER_TYPE_IPADDRESS, |
84 | BER_TYPE_IPADDRESS, |
82 | BER_TYPE_CROAK, |
85 | BER_TYPE_CROAK, |
83 | }; |
86 | }; |
84 | |
87 | |
|
|
88 | // tuple array indices |
85 | enum { |
89 | enum { |
86 | BER_CLASS = 0, |
90 | BER_CLASS = 0, |
87 | BER_TAG = 1, |
91 | BER_TAG = 1, |
88 | BER_FLAGS = 2, |
92 | BER_FLAGS = 2, |
89 | BER_DATA = 3, |
93 | BER_DATA = 3, |
… | |
… | |
100 | |
104 | |
101 | #if PERL_VERSION < 18 |
105 | #if PERL_VERSION < 18 |
102 | # define utf8_to_uvchr_buf(s,e,l) utf8_to_uvchr (s, l) |
106 | # define utf8_to_uvchr_buf(s,e,l) utf8_to_uvchr (s, l) |
103 | #endif |
107 | #endif |
104 | |
108 | |
|
|
109 | #ifndef SvREFCNT_inc_NN |
|
|
110 | #define SvREFCNT_inc_NN(x) SvREFCNT_inc (x) |
|
|
111 | #endif |
|
|
112 | #ifndef SvREFCNT_dec_NN |
|
|
113 | #define SvREFCNT_dec_NN(x) SvREFCNT_dec (x) |
|
|
114 | #endif |
|
|
115 | |
105 | #if __GNUC__ >= 3 |
116 | #if __GNUC__ >= 3 |
106 | # define expect(expr,value) __builtin_expect ((expr), (value)) |
117 | # define expect(expr,value) __builtin_expect ((expr), (value)) |
107 | # define INLINE static inline |
118 | # define INLINE static inline |
108 | #else |
119 | #else |
109 | # define expect(expr,value) (expr) |
120 | # define expect(expr,value) (expr) |
… | |
… | |
141 | { |
152 | { |
142 | if (!SvOK (profile)) |
153 | if (!SvOK (profile)) |
143 | return default_profile; |
154 | return default_profile; |
144 | |
155 | |
145 | if (!SvROK (profile)) |
156 | if (!SvROK (profile)) |
146 | croak ("invalid profile"); |
157 | croak ("Convert::BER::XS::Profile expected"); |
147 | |
158 | |
148 | profile = SvRV (profile); |
159 | profile = SvRV (profile); |
149 | |
160 | |
150 | if (SvSTASH (profile) != profile_stash) |
161 | if (SvSTASH (profile) != profile_stash) |
151 | croak ("invalid profile object"); |
162 | croak ("Convert::BER::XS::Profile expected"); |
152 | |
163 | |
153 | return (void *)profile; |
164 | return (void *)profile; |
154 | } |
165 | } |
155 | |
166 | |
156 | static int |
167 | static int |
… | |
… | |
228 | } |
239 | } |
229 | |
240 | |
230 | // get_* functions fetch something from the buffer |
241 | // get_* functions fetch something from the buffer |
231 | // decode_* functions use get_* fun ctions to decode ber values |
242 | // decode_* functions use get_* fun ctions to decode ber values |
232 | |
243 | |
|
|
244 | // get single octet |
|
|
245 | static U8 |
|
|
246 | get_u8 (void) |
|
|
247 | { |
|
|
248 | if (cur == end) |
|
|
249 | error ("unexpected end of message buffer"); |
|
|
250 | |
|
|
251 | return *cur++; |
|
|
252 | } |
|
|
253 | |
233 | // get n octets |
254 | // get n octets |
234 | static U8 * |
255 | static U8 * |
235 | get_n (UV count) |
256 | get_n (UV count) |
236 | { |
257 | { |
237 | want (count); |
258 | want (count); |
238 | U8 *res = cur; |
259 | U8 *res = cur; |
239 | cur += count; |
260 | cur += count; |
240 | return res; |
261 | return res; |
241 | } |
262 | } |
242 | |
263 | |
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") |
264 | // get ber-encoded integer (i.e. pack "w") |
254 | static UV |
265 | static UV |
255 | get_w (void) |
266 | get_w (void) |
256 | { |
267 | { |
257 | UV res = 0; |
268 | UV res = 0; |
258 | U8 c = get_u8 (); |
269 | U8 c = get_u8 (); |
259 | |
270 | |
260 | if (expect_false (c == 0x80)) |
271 | if (expect_false (c == 0x80)) |
261 | error ("illegal BER padding (X.690 8.1.2.4.2, 8.19.2)"); |
272 | error ("invalid BER padding (X.690 8.1.2.4.2, 8.19.2)"); |
262 | |
273 | |
263 | for (;;) |
274 | for (;;) |
264 | { |
275 | { |
265 | if (expect_false (res >> UVSIZE * 8 - 7)) |
276 | if (expect_false (res >> UVSIZE * 8 - 7)) |
266 | error ("BER variable integer overflow"); |
277 | error ("BER variable length integer overflow"); |
267 | |
278 | |
268 | res = (res << 7) | (c & 0x7f); |
279 | res = (res << 7) | (c & 0x7f); |
269 | |
280 | |
270 | if (!(c & 0x80)) |
281 | if (expect_true (!(c & 0x80))) |
271 | return res; |
282 | return res; |
272 | |
283 | |
273 | c = get_u8 (); |
284 | c = get_u8 (); |
274 | } |
285 | } |
275 | } |
286 | } |
… | |
… | |
277 | static UV |
288 | static UV |
278 | get_length (void) |
289 | get_length (void) |
279 | { |
290 | { |
280 | UV res = get_u8 (); |
291 | UV res = get_u8 (); |
281 | |
292 | |
282 | if (res & 0x80) |
293 | if (expect_false (res & 0x80)) |
283 | { |
294 | { |
284 | int cnt = res & 0x7f; |
295 | U8 cnt = res & 0x7f; |
|
|
296 | |
|
|
297 | // this genewrates quite ugly code, but the overhead |
|
|
298 | // of copying the bytes for these lengths is probably so high |
|
|
299 | // that a slightly inefficient get_length won't matter. |
|
|
300 | |
|
|
301 | if (expect_false (cnt == 0)) |
|
|
302 | error ("invalid use of indefinite BER length form in primitive encoding (X.690 8.1.3.2)"); |
|
|
303 | |
|
|
304 | if (expect_false (cnt > UVSIZE)) |
|
|
305 | error ("BER value length too long (must fit into UV) or BER reserved value in length (X.690 8.1.3.5)"); |
|
|
306 | |
|
|
307 | want (cnt); |
|
|
308 | |
285 | res = 0; |
309 | res = 0; |
286 | |
310 | do |
287 | switch (cnt) |
311 | res = (res << 8) | *cur++; |
288 | { |
312 | 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 | } |
313 | } |
308 | |
314 | |
309 | return res; |
315 | return res; |
310 | } |
316 | } |
311 | |
317 | |
312 | static SV * |
318 | static SV * |
313 | decode_int (void) |
319 | decode_int (UV len) |
314 | { |
320 | { |
315 | UV len = get_length (); |
|
|
316 | |
|
|
317 | if (!len) |
321 | if (!len) |
318 | error ("invalid integer length equal to zero (X.690 8.3.1)"); |
322 | error ("invalid BER_TYPE_INT length zero (X.690 8.3.1)"); |
319 | |
323 | |
320 | U8 *data = get_n (len); |
324 | U8 *data = get_n (len); |
321 | |
325 | |
322 | if (expect_false (len > 1)) |
326 | if (expect_false (len > 1)) |
323 | { |
327 | { |
324 | U16 mask = (data [0] << 8) | data [1] & 0xff80; |
328 | U16 mask = (data [0] << 8) | data [1] & 0xff80; |
325 | |
329 | |
326 | if (expect_false (mask == 0xff80 || mask == 0x0000)) |
330 | if (expect_false (mask == 0xff80 || mask == 0x0000)) |
327 | error ("illegal padding in integer (X.690 8.3.2)"); |
331 | error ("invalid padding in BER_TYPE_INT (X.690 8.3.2)"); |
328 | } |
332 | } |
329 | |
333 | |
330 | int negative = data [0] & 0x80; |
334 | int negative = data [0] & 0x80; |
331 | |
335 | |
332 | UV val = negative ? -1 : 0; // copy signbit to all bits |
336 | UV val = negative ? -1 : 0; // copy signbit to all bits |
333 | |
337 | |
334 | if (len > UVSIZE + (!negative && !*data)) |
338 | 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"); |
339 | error ("BER_TYPE_INT overflow"); |
337 | |
340 | |
338 | do |
341 | do |
339 | val = (val << 8) | *data++; |
342 | val = (val << 8) | *data++; |
340 | while (--len); |
343 | while (--len); |
341 | |
344 | |
… | |
… | |
343 | // but that's ok, as perl relies on it as well. |
346 | // but that's ok, as perl relies on it as well. |
344 | return negative ? newSViv ((IV)val) : newSVuv (val); |
347 | return negative ? newSViv ((IV)val) : newSVuv (val); |
345 | } |
348 | } |
346 | |
349 | |
347 | static SV * |
350 | static SV * |
348 | decode_data (void) |
351 | decode_data (UV len) |
349 | { |
352 | { |
350 | UV len = get_length (); |
|
|
351 | return newSVpvn ((char *)get_n (len), len); |
353 | return newSVpvn ((char *)get_n (len), len); |
352 | } |
354 | } |
353 | |
355 | |
354 | // helper for decode_object_identifier |
356 | // helper for decode_object_identifier |
355 | static char * |
357 | static char * |
… | |
… | |
383 | |
385 | |
384 | return buf; |
386 | return buf; |
385 | } |
387 | } |
386 | |
388 | |
387 | static SV * |
389 | static SV * |
388 | decode_oid (int relative) |
390 | decode_oid (UV len, int relative) |
389 | { |
391 | { |
390 | UV len = get_length (); |
|
|
391 | |
|
|
392 | if (len <= 0) |
392 | if (len <= 0) |
393 | { |
393 | { |
394 | error ("OBJECT IDENTIFIER length equal to zero"); |
394 | error ("BER_TYPE_OID length must not be zero"); |
395 | return &PL_sv_undef; |
395 | return &PL_sv_undef; |
396 | } |
396 | } |
397 | |
397 | |
398 | U8 *end = cur + len; |
398 | U8 *end = cur + len; |
399 | UV w = get_w (); |
399 | UV w = get_w (); |
… | |
… | |
401 | static char oid[MAX_OID_STRLEN]; // static, because too large for stack |
401 | static char oid[MAX_OID_STRLEN]; // static, because too large for stack |
402 | char *app = oid; |
402 | char *app = oid; |
403 | |
403 | |
404 | if (relative) |
404 | if (relative) |
405 | app = write_uv (app, w); |
405 | app = write_uv (app, w); |
|
|
406 | else |
|
|
407 | { |
|
|
408 | UV w1, w2; |
|
|
409 | |
406 | else if (w < 2 * 40) |
410 | if (w < 2 * 40) |
407 | { |
411 | (w1 = w / 40), (w2 = w % 40); |
|
|
412 | else |
|
|
413 | (w1 = 2), (w2 = w - 2 * 40); |
|
|
414 | |
408 | app = write_uv (app, (U8)w / 40); |
415 | app = write_uv (app, w1); |
409 | *app++ = '.'; |
416 | *app++ = '.'; |
410 | app = write_uv (app, (U8)w % 40); |
|
|
411 | } |
|
|
412 | else |
|
|
413 | { |
|
|
414 | app = write_uv (app, 2); |
417 | app = write_uv (app, w2); |
415 | *app++ = '.'; |
|
|
416 | app = write_uv (app, w - 2 * 40); |
|
|
417 | } |
418 | } |
418 | |
419 | |
419 | while (cur < end) |
420 | while (cur < end) |
420 | { |
421 | { |
421 | // we assume an oid component is never > 64 digits |
422 | // we assume an oid component is never > 64 digits |
… | |
… | |
428 | } |
429 | } |
429 | |
430 | |
430 | return newSVpvn (oid, app - oid); |
431 | return newSVpvn (oid, app - oid); |
431 | } |
432 | } |
432 | |
433 | |
|
|
434 | // oh my, this is a total mess |
|
|
435 | static SV * |
|
|
436 | decode_real (UV len) |
|
|
437 | { |
|
|
438 | SV *res; |
|
|
439 | U8 *beg = cur; |
|
|
440 | |
|
|
441 | if (len == 0) |
|
|
442 | res = newSVnv (0.); |
|
|
443 | else |
|
|
444 | { |
|
|
445 | U8 info = get_u8 (); |
|
|
446 | |
|
|
447 | if (info & 0x80) |
|
|
448 | { |
|
|
449 | // binary |
|
|
450 | static const U8 base[] = { 2, 8, 16, 0 }; |
|
|
451 | NV S = info & 0x40 ? -1 : 1; // sign |
|
|
452 | NV B = base [(info >> 4) & 3]; // base |
|
|
453 | NV F = 1 << ((info >> 2) & 3); // scale factor ("shift") |
|
|
454 | int L = info & 3; // exponent length |
|
|
455 | |
|
|
456 | if (!B) |
|
|
457 | croak ("BER_TYPE_REAL binary encoding uses invalid base (0x%02x)", info); |
|
|
458 | |
|
|
459 | SAVETMPS; |
|
|
460 | |
|
|
461 | SV *E = sv_2mortal (decode_int (L == 3 ? get_u8 () : L + 1)); |
|
|
462 | SV *M = sv_2mortal (decode_int (len - (cur - beg))); |
|
|
463 | |
|
|
464 | res = newSVnv (S * SvNV (M) * F * Perl_pow (B, SvNV (E))); |
|
|
465 | |
|
|
466 | FREETMPS; |
|
|
467 | } |
|
|
468 | else if (info & 0x40) |
|
|
469 | { |
|
|
470 | // SpecialRealValue |
|
|
471 | U8 special = get_u8 (); |
|
|
472 | NV val; |
|
|
473 | |
|
|
474 | switch (special) |
|
|
475 | { |
|
|
476 | case 0x40: val = NV_INF; break; |
|
|
477 | case 0x41: val = -NV_INF; break; |
|
|
478 | case 0x42: val = NV_NAN; break; |
|
|
479 | case 0x43: val = -(NV)0.; break; |
|
|
480 | |
|
|
481 | default: |
|
|
482 | croak ("BER_TYPE_REAL SpecialRealValues invalid encoding 0x%02x (X.690 8.5.9)", special); |
|
|
483 | } |
|
|
484 | |
|
|
485 | res = newSVnv (val); |
|
|
486 | } |
|
|
487 | else |
|
|
488 | { |
|
|
489 | // decimal |
|
|
490 | dSP; |
|
|
491 | SAVETMPS; |
|
|
492 | PUSHMARK (SP); |
|
|
493 | EXTEND (SP, 2); |
|
|
494 | PUSHs (sv_2mortal (newSVcacheint (info & 0x3f))); |
|
|
495 | PUSHs (sv_2mortal (newSVpvn (get_n (len - 1), len - 1))); |
|
|
496 | PUTBACK; |
|
|
497 | call_pv ("Convert::BER::XS::_decode_real_decimal", G_SCALAR); |
|
|
498 | SPAGAIN; |
|
|
499 | res = SvREFCNT_inc_NN (POPs); |
|
|
500 | PUTBACK; |
|
|
501 | FREETMPS; |
|
|
502 | } |
|
|
503 | } |
|
|
504 | |
|
|
505 | if (cur - beg != len) |
|
|
506 | { |
|
|
507 | SvREFCNT_dec_NN (res); |
|
|
508 | croak ("BER_TYPE_REAL invalid content length (X.690 8,5)"); |
|
|
509 | } |
|
|
510 | |
|
|
511 | return res; |
|
|
512 | } |
|
|
513 | |
433 | // TODO: this is unacceptably slow |
514 | // TODO: this is unacceptably slow |
434 | static SV * |
515 | static SV * |
435 | decode_ucs (int chrsize) |
516 | decode_ucs (UV len, int chrsize) |
436 | { |
517 | { |
437 | SV *res = NEWSV (0, 0); |
|
|
438 | |
|
|
439 | UV len = get_length (); |
|
|
440 | |
|
|
441 | if (len & (chrsize - 1)) |
518 | if (len & (chrsize - 1)) |
442 | croak ("BER_TYPE_UCS has an invalid number of octets (%d)", len); |
519 | croak ("BER_TYPE_UCS has an invalid number of octets (%d)", len); |
|
|
520 | |
|
|
521 | SV *res = NEWSV (0, 0); |
443 | |
522 | |
444 | while (len) |
523 | while (len) |
445 | { |
524 | { |
446 | U8 b1 = get_u8 (); |
525 | U8 b1 = get_u8 (); |
447 | U8 b2 = get_u8 (); |
526 | U8 b2 = get_u8 (); |
… | |
… | |
480 | if (tag == ASN_TAG_BER) |
559 | if (tag == ASN_TAG_BER) |
481 | tag = get_w (); |
560 | tag = get_w (); |
482 | |
561 | |
483 | if (constructed) |
562 | if (constructed) |
484 | { |
563 | { |
|
|
564 | want (1); |
|
|
565 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
|
|
566 | |
|
|
567 | if (expect_false (*cur == 0x80)) |
|
|
568 | { |
|
|
569 | // indefinite length |
|
|
570 | ++cur; |
|
|
571 | |
|
|
572 | for (;;) |
|
|
573 | { |
|
|
574 | want (2); |
|
|
575 | if (!cur [0] && !cur [1]) |
|
|
576 | { |
|
|
577 | cur += 2; |
|
|
578 | break; |
|
|
579 | } |
|
|
580 | |
|
|
581 | av_push (av, decode_ber ()); |
|
|
582 | } |
|
|
583 | } |
|
|
584 | else |
|
|
585 | { |
|
|
586 | UV len = get_length (); |
|
|
587 | UV seqend = (cur - buf) + len; |
|
|
588 | |
|
|
589 | while (cur < buf + seqend) |
|
|
590 | av_push (av, decode_ber ()); |
|
|
591 | |
|
|
592 | if (expect_false (cur > buf + seqend)) |
|
|
593 | croak ("CONSTRUCTED type %02x length overflow (0x%x 0x%x)\n", identifier, (int)(cur - buf), (int)seqend); |
|
|
594 | } |
|
|
595 | |
|
|
596 | res = newRV_inc ((SV *)av); |
|
|
597 | } |
|
|
598 | else |
|
|
599 | { |
485 | UV len = get_length (); |
600 | UV len = get_length (); |
486 | UV seqend = (cur - buf) + len; |
|
|
487 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
|
|
488 | |
601 | |
489 | while (cur < buf + seqend) |
|
|
490 | av_push (av, decode_ber ()); |
|
|
491 | |
|
|
492 | if (cur > buf + seqend) |
|
|
493 | croak ("constructed type %02x length overflow (0x%x 0x%x)\n", identifier, (int)(cur - buf), (int)seqend); |
|
|
494 | |
|
|
495 | res = newRV_inc ((SV *)av); |
|
|
496 | } |
|
|
497 | else |
|
|
498 | switch (profile_lookup (cur_profile, klass, tag)) |
602 | switch (profile_lookup (cur_profile, klass, tag)) |
499 | { |
603 | { |
500 | case BER_TYPE_NULL: |
604 | case BER_TYPE_NULL: |
501 | { |
605 | if (expect_false (len)) |
502 | UV len = get_length (); |
|
|
503 | |
|
|
504 | if (len) |
|
|
505 | croak ("BER_TYPE_NULL value with non-zero length %d encountered (X.690 8.8.2)", len); |
606 | croak ("BER_TYPE_NULL value with non-zero length %d encountered (X.690 8.8.2)", len); |
506 | |
607 | |
507 | res = &PL_sv_undef; |
608 | res = &PL_sv_undef; |
508 | } |
|
|
509 | break; |
609 | break; |
510 | |
610 | |
511 | case BER_TYPE_BOOL: |
611 | case BER_TYPE_BOOL: |
512 | { |
|
|
513 | UV len = get_length (); |
|
|
514 | |
|
|
515 | if (len != 1) |
612 | if (expect_false (len != 1)) |
516 | croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered (X.690 8.2.1)", len); |
613 | croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered (X.690 8.2.1)", len); |
517 | |
614 | |
518 | res = newSVcacheint (!!get_u8 ()); |
615 | res = newSVcacheint (!!get_u8 ()); |
519 | } |
|
|
520 | break; |
616 | break; |
521 | |
617 | |
522 | case BER_TYPE_OID: |
618 | case BER_TYPE_OID: |
523 | res = decode_oid (0); |
619 | res = decode_oid (len, 0); |
524 | break; |
620 | break; |
525 | |
621 | |
526 | case BER_TYPE_RELOID: |
622 | case BER_TYPE_RELOID: |
527 | res = decode_oid (1); |
623 | res = decode_oid (len, 1); |
528 | break; |
624 | break; |
529 | |
625 | |
530 | case BER_TYPE_INT: |
626 | case BER_TYPE_INT: |
531 | res = decode_int (); |
627 | res = decode_int (len); |
532 | break; |
628 | break; |
533 | |
629 | |
534 | case BER_TYPE_UTF8: |
630 | case BER_TYPE_UTF8: |
535 | res = decode_data (); |
631 | res = decode_data (len); |
536 | SvUTF8_on (res); |
632 | SvUTF8_on (res); |
537 | break; |
633 | break; |
538 | |
634 | |
539 | case BER_TYPE_BYTES: |
635 | case BER_TYPE_BYTES: |
540 | res = decode_data (); |
636 | res = decode_data (len); |
541 | break; |
637 | break; |
542 | |
638 | |
543 | case BER_TYPE_IPADDRESS: |
639 | case BER_TYPE_IPADDRESS: |
544 | { |
640 | { |
545 | UV len = get_length (); |
|
|
546 | |
|
|
547 | if (len != 4) |
641 | if (len != 4) |
548 | croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered (RFC 2578 7.1.5)", len); |
642 | croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered (RFC 2578 7.1.5)", len); |
549 | |
643 | |
550 | U8 c1 = get_u8 (); |
644 | U8 *data = get_n (4); |
551 | U8 c2 = get_u8 (); |
645 | 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 | } |
646 | } |
557 | break; |
647 | break; |
558 | |
648 | |
559 | case BER_TYPE_UCS2: |
649 | case BER_TYPE_UCS2: |
560 | res = decode_ucs (2); |
650 | res = decode_ucs (len, 2); |
561 | break; |
651 | break; |
562 | |
652 | |
563 | case BER_TYPE_UCS4: |
653 | case BER_TYPE_UCS4: |
564 | res = decode_ucs (4); |
654 | res = decode_ucs (len, 4); |
565 | break; |
655 | break; |
566 | |
656 | |
567 | case BER_TYPE_REAL: |
657 | case BER_TYPE_REAL: |
|
|
658 | res = decode_real (len); |
|
|
659 | break; |
|
|
660 | |
568 | case BER_TYPE_CROAK: |
661 | case BER_TYPE_CROAK: |
|
|
662 | croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag); |
|
|
663 | |
569 | default: |
664 | default: |
570 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
665 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
571 | } |
666 | } |
|
|
667 | } |
572 | |
668 | |
573 | AV *av = newAV (); |
669 | AV *av = newAV (); |
574 | av_fill (av, BER_ARRAYSIZE - 1); |
670 | av_fill (av, BER_ARRAYSIZE - 1); |
575 | AvARRAY (av)[BER_CLASS] = newSVcacheint (klass); |
671 | AvARRAY (av)[BER_CLASS] = newSVcacheint (klass); |
576 | AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); |
672 | AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); |
… | |
… | |
663 | } |
759 | } |
664 | |
760 | |
665 | static U8 * |
761 | static U8 * |
666 | put_length_at (UV val, U8 *cur) |
762 | put_length_at (UV val, U8 *cur) |
667 | { |
763 | { |
668 | if (val < 0x7fU) |
764 | if (val <= 0x7fU) |
669 | *cur++ = val; |
765 | *cur++ = val; |
670 | else |
766 | else |
671 | { |
767 | { |
672 | U8 *lenb = cur++; |
768 | U8 *lenb = cur++; |
673 | |
769 | |
674 | #if UVSIZE > 4 |
770 | #if UVSIZE > 4 |
675 | *cur = val >> 56; cur += *cur > 0; |
771 | *cur = val >> 56; cur += val >= ((UV)1 << (8 * 7)); |
676 | *cur = val >> 48; cur += *cur > 0; |
772 | *cur = val >> 48; cur += val >= ((UV)1 << (8 * 6)); |
677 | *cur = val >> 40; cur += *cur > 0; |
773 | *cur = val >> 40; cur += val >= ((UV)1 << (8 * 5)); |
678 | *cur = val >> 32; cur += *cur > 0; |
774 | *cur = val >> 32; cur += val >= ((UV)1 << (8 * 4)); |
679 | #endif |
775 | #endif |
680 | *cur = val >> 24; cur += *cur > 0; |
776 | *cur = val >> 24; cur += val >= ((UV)1 << (8 * 3)); |
681 | *cur = val >> 16; cur += *cur > 0; |
777 | *cur = val >> 16; cur += val >= ((UV)1 << (8 * 2)); |
682 | *cur = val >> 8; cur += *cur > 0; |
778 | *cur = val >> 8; cur += val >= ((UV)1 << (8 * 1)); |
683 | *cur = val ; cur += 1; |
779 | *cur = val ; cur += 1; |
684 | |
780 | |
685 | *lenb = 0x80 + cur - lenb - 1; |
781 | *lenb = 0x80 + cur - lenb - 1; |
686 | } |
782 | } |
687 | |
783 | |
… | |
… | |
689 | } |
785 | } |
690 | |
786 | |
691 | static void |
787 | static void |
692 | put_length (UV val) |
788 | put_length (UV val) |
693 | { |
789 | { |
694 | need (5 + val); |
790 | need (9 + val); |
695 | cur = put_length_at (val, cur); |
791 | cur = put_length_at (val, cur); |
696 | } |
792 | } |
697 | |
793 | |
698 | // return how many bytes the encoded length requires |
794 | // return how many bytes the encoded length requires |
699 | static int length_length (UV val) |
795 | static int length_length (UV val) |
700 | { |
796 | { |
701 | return val < 0x7fU |
797 | // use hashing with a DeBruin sequence, anyone? |
|
|
798 | return expect_true (val <= 0x7fU) |
702 | ? 1 |
799 | ? 1 |
703 | : 2 |
800 | : 2 |
704 | + (val > 0xffU) |
801 | + (val > 0x000000000000ffU) |
705 | + (val > 0xffffU) |
802 | + (val > 0x0000000000ffffU) |
706 | + (val > 0xffffffU) |
803 | + (val > 0x00000000ffffffU) |
707 | #if UVSIZE > 4 |
804 | #if UVSIZE > 4 |
708 | + (val > 0xffffffffU) |
805 | + (val > 0x000000ffffffffU) |
709 | + (val > 0xffffffffffU) |
806 | + (val > 0x0000ffffffffffU) |
710 | + (val > 0xffffffffffffU) |
807 | + (val > 0x00ffffffffffffU) |
711 | + (val > 0xffffffffffffffU) |
808 | + (val > 0xffffffffffffffU) |
712 | #endif |
809 | #endif |
713 | ; |
810 | ; |
714 | } |
811 | } |
715 | |
812 | |
… | |
… | |
851 | } |
948 | } |
852 | |
949 | |
853 | len_fixup (mark); |
950 | len_fixup (mark); |
854 | } |
951 | } |
855 | |
952 | |
856 | // check whether an SV is a BER tuple and returns its AV * |
953 | static void |
857 | static AV * |
954 | encode_real (SV *data) |
858 | ber_tuple (SV *tuple) |
|
|
859 | { |
955 | { |
860 | SV *rv; |
956 | NV nv = SvNV (data); |
861 | |
957 | |
862 | if (expect_false (!SvROK (tuple) || SvTYPE ((rv = SvRV (tuple))) != SVt_PVAV)) |
958 | if (expect_false (nv == (NV)0.)) |
863 | croak ("BER tuple must be array-reference"); |
959 | { |
|
|
960 | if (signbit (nv)) |
|
|
961 | { |
|
|
962 | // negative zero |
|
|
963 | need (3); |
|
|
964 | *cur++ = 2; |
|
|
965 | *cur++ = 0x40; |
|
|
966 | *cur++ = 0x43; |
|
|
967 | } |
|
|
968 | else |
|
|
969 | { |
|
|
970 | // positive zero |
|
|
971 | need (1); |
|
|
972 | *cur++ = 0; |
|
|
973 | } |
|
|
974 | } |
|
|
975 | else if (expect_false (Perl_isinf (nv))) |
|
|
976 | { |
|
|
977 | need (3); |
|
|
978 | *cur++ = 2; |
|
|
979 | *cur++ = 0x40; |
|
|
980 | *cur++ = nv < (NV)0. ? 0x41 : 0x40; |
|
|
981 | } |
|
|
982 | else if (expect_false (Perl_isnan (nv))) |
|
|
983 | { |
|
|
984 | need (3); |
|
|
985 | *cur++ = 2; |
|
|
986 | *cur++ = 0x40; |
|
|
987 | *cur++ = 0x42; |
|
|
988 | } |
|
|
989 | else |
|
|
990 | { |
|
|
991 | // use decimal encoding |
|
|
992 | dSP; |
|
|
993 | SAVETMPS; |
|
|
994 | PUSHMARK (SP); |
|
|
995 | EXTEND (SP, 2); |
|
|
996 | PUSHs (data); |
|
|
997 | PUSHs (sv_2mortal (newSVcacheint (NV_DIG))); |
|
|
998 | PUTBACK; |
|
|
999 | call_pv ("Convert::BER::XS::_encode_real_decimal", G_SCALAR); |
|
|
1000 | SPAGAIN; |
864 | |
1001 | |
865 | if (expect_false (SvRMAGICAL (rv))) |
1002 | SV *sv = POPs; |
866 | croak ("BER tuple must not be tied"); |
1003 | STRLEN l; |
|
|
1004 | char *f = SvPV (sv, l); |
867 | |
1005 | |
868 | if (expect_false (AvFILL ((AV *)rv) != BER_ARRAYSIZE - 1)) |
1006 | put_length (l); |
869 | croak ("BER tuple must contain exactly %d elements, not %d", BER_ARRAYSIZE, AvFILL ((AV *)rv) + 1); |
1007 | memcpy (cur, f, l); |
|
|
1008 | cur += l; |
870 | |
1009 | |
871 | return (AV *)rv; |
1010 | PUTBACK; |
|
|
1011 | FREETMPS; |
|
|
1012 | } |
872 | } |
1013 | } |
873 | |
1014 | |
874 | static void |
1015 | static void |
875 | encode_ucs (SV *data, int chrsize) |
1016 | encode_ucs (SV *data, int chrsize) |
876 | { |
1017 | { |
… | |
… | |
896 | |
1037 | |
897 | *cur++ = uchr >> 8; |
1038 | *cur++ = uchr >> 8; |
898 | *cur++ = uchr; |
1039 | *cur++ = uchr; |
899 | } |
1040 | } |
900 | } |
1041 | } |
|
|
1042 | |
|
|
1043 | // check whether an SV is a BER tuple and returns its AV * |
|
|
1044 | static AV * |
|
|
1045 | ber_tuple (SV *tuple) |
|
|
1046 | { |
|
|
1047 | SV *rv; |
|
|
1048 | |
|
|
1049 | if (expect_false (!SvROK (tuple) || SvTYPE ((rv = SvRV (tuple))) != SVt_PVAV)) |
|
|
1050 | croak ("BER tuple must be array-reference"); |
|
|
1051 | |
|
|
1052 | if (expect_false (SvRMAGICAL (rv))) |
|
|
1053 | croak ("BER tuple must not be tied"); |
|
|
1054 | |
|
|
1055 | if (expect_false (AvFILL ((AV *)rv) != BER_ARRAYSIZE - 1)) |
|
|
1056 | croak ("BER tuple must contain exactly %d elements, not %d", BER_ARRAYSIZE, AvFILL ((AV *)rv) + 1); |
|
|
1057 | |
|
|
1058 | return (AV *)rv; |
|
|
1059 | } |
|
|
1060 | |
901 | static void |
1061 | static void |
902 | encode_ber (SV *tuple) |
1062 | encode_ber (SV *tuple) |
903 | { |
1063 | { |
904 | AV *av = ber_tuple (tuple); |
1064 | AV *av = ber_tuple (tuple); |
905 | |
1065 | |
… | |
… | |
924 | // and adjust later |
1084 | // and adjust later |
925 | need (1); |
1085 | need (1); |
926 | STRLEN mark = len_fixup_mark (); |
1086 | STRLEN mark = len_fixup_mark (); |
927 | |
1087 | |
928 | if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV)) |
1088 | if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV)) |
929 | croak ("BER constructed data must be array-reference"); |
1089 | croak ("BER CONSTRUCTED data must be array-reference"); |
930 | |
1090 | |
931 | AV *av = (AV *)SvRV (data); |
1091 | AV *av = (AV *)SvRV (data); |
932 | int fill = AvFILL (av); |
1092 | int fill = AvFILL (av); |
933 | |
1093 | |
934 | if (expect_false (SvRMAGICAL (av))) |
1094 | if (expect_false (SvRMAGICAL (av))) |
935 | croak ("BER constructed data must not be tied"); |
1095 | croak ("BER CONSTRUCTED data must not be tied"); |
936 | |
1096 | |
937 | int i; |
1097 | int i; |
938 | for (i = 0; i <= fill; ++i) |
1098 | for (i = 0; i <= fill; ++i) |
939 | encode_ber (AvARRAY (av)[i]); |
1099 | encode_ber (AvARRAY (av)[i]); |
940 | |
1100 | |
… | |
… | |
995 | case BER_TYPE_UCS4: |
1155 | case BER_TYPE_UCS4: |
996 | encode_ucs (data, 4); |
1156 | encode_ucs (data, 4); |
997 | break; |
1157 | break; |
998 | |
1158 | |
999 | case BER_TYPE_REAL: |
1159 | case BER_TYPE_REAL: |
|
|
1160 | encode_real (data); |
|
|
1161 | break; |
|
|
1162 | |
1000 | case BER_TYPE_CROAK: |
1163 | case BER_TYPE_CROAK: |
|
|
1164 | croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag); |
|
|
1165 | |
1001 | default: |
1166 | default: |
1002 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
1167 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
1003 | } |
1168 | } |
1004 | |
1169 | |
1005 | } |
1170 | } |
… | |
… | |
1100 | STRLEN len; |
1265 | STRLEN len; |
1101 | buf = (U8 *)SvPVbyte (ber, len); |
1266 | buf = (U8 *)SvPVbyte (ber, len); |
1102 | cur = buf; |
1267 | cur = buf; |
1103 | end = buf + len; |
1268 | end = buf + len; |
1104 | |
1269 | |
|
|
1270 | PUTBACK; |
1105 | SV *tuple = decode_ber (); |
1271 | SV *tuple = decode_ber (); |
|
|
1272 | SPAGAIN; |
1106 | |
1273 | |
1107 | EXTEND (SP, 2); |
1274 | EXTEND (SP, 2); |
1108 | PUSHs (sv_2mortal (tuple)); |
1275 | PUSHs (sv_2mortal (tuple)); |
1109 | |
1276 | |
1110 | if (ix) |
1277 | if (ix) |
1111 | PUSHs (sv_2mortal (newSViv (cur - buf))); |
1278 | PUSHs (sv_2mortal (newSViv (cur - buf))); |
1112 | else if (cur != end) |
1279 | else if (cur != end) |
1113 | error ("trailing garbage after BER data"); |
1280 | error ("trailing garbage after BER value"); |
1114 | } |
1281 | } |
1115 | |
1282 | |
1116 | void |
1283 | 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) |
1284 | 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: |
1285 | PPCODE: |
… | |
… | |
1195 | cur_profile = SvPROFILE (profile); |
1362 | cur_profile = SvPROFILE (profile); |
1196 | buf_sv = sv_2mortal (NEWSV (0, 256)); |
1363 | buf_sv = sv_2mortal (NEWSV (0, 256)); |
1197 | SvPOK_only (buf_sv); |
1364 | SvPOK_only (buf_sv); |
1198 | set_buf (buf_sv); |
1365 | set_buf (buf_sv); |
1199 | |
1366 | |
|
|
1367 | PUTBACK; |
1200 | encode_ber (tuple); |
1368 | encode_ber (tuple); |
|
|
1369 | SPAGAIN; |
1201 | |
1370 | |
1202 | SvCUR_set (buf_sv, cur - buf); |
1371 | SvCUR_set (buf_sv, cur - buf); |
1203 | XPUSHs (buf_sv); |
1372 | XPUSHs (buf_sv); |
1204 | } |
1373 | } |
1205 | |
1374 | |