… | |
… | |
8 | // sscanf format modifiers and more. |
8 | // sscanf format modifiers and more. |
9 | |
9 | |
10 | enum { |
10 | enum { |
11 | // ASN_TAG |
11 | // ASN_TAG |
12 | ASN_BOOLEAN = 0x01, |
12 | ASN_BOOLEAN = 0x01, |
13 | ASN_INTEGER32 = 0x02, |
13 | ASN_INTEGER = 0x02, |
14 | ASN_BIT_STRING = 0x03, |
14 | ASN_BIT_STRING = 0x03, |
15 | ASN_OCTET_STRING = 0x04, |
15 | ASN_OCTET_STRING = 0x04, |
16 | ASN_NULL = 0x05, |
16 | ASN_NULL = 0x05, |
17 | ASN_OBJECT_IDENTIFIER = 0x06, |
17 | ASN_OBJECT_IDENTIFIER = 0x06, |
18 | ASN_OID = 0x06, |
18 | ASN_OID = 0x06, |
… | |
… | |
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 | }; |
… | |
… | |
80 | BER_TYPE_IPADDRESS, |
81 | BER_TYPE_IPADDRESS, |
81 | BER_TYPE_CROAK, |
82 | BER_TYPE_CROAK, |
82 | }; |
83 | }; |
83 | |
84 | |
84 | enum { |
85 | enum { |
85 | BER_CLASS = 0, |
86 | BER_CLASS = 0, |
86 | BER_TAG = 1, |
87 | BER_TAG = 1, |
87 | BER_CONSTRUCTED = 2, |
88 | BER_FLAGS = 2, |
88 | BER_DATA = 3, |
89 | BER_DATA = 3, |
89 | BER_ARRAYSIZE |
90 | BER_ARRAYSIZE |
90 | }; |
91 | }; |
91 | |
92 | |
92 | #define MAX_OID_STRLEN 4096 |
93 | #define MAX_OID_STRLEN 4096 |
93 | |
94 | |
… | |
… | |
140 | { |
141 | { |
141 | if (!SvOK (profile)) |
142 | if (!SvOK (profile)) |
142 | return default_profile; |
143 | return default_profile; |
143 | |
144 | |
144 | if (!SvROK (profile)) |
145 | if (!SvROK (profile)) |
145 | croak ("invalid profile"); |
146 | croak ("Convert::BER::XS::Profile expected"); |
146 | |
147 | |
147 | profile = SvRV (profile); |
148 | profile = SvRV (profile); |
148 | |
149 | |
149 | if (SvSTASH (profile) != profile_stash) |
150 | if (SvSTASH (profile) != profile_stash) |
150 | croak ("invalid profile object"); |
151 | croak ("Convert::BER::XS::Profile expected"); |
151 | |
152 | |
152 | return (void *)profile; |
153 | return (void *)profile; |
153 | } |
154 | } |
154 | |
155 | |
155 | static int |
156 | static int |
… | |
… | |
181 | |
182 | |
182 | SvPVX (sv)[idx] = type; |
183 | SvPVX (sv)[idx] = type; |
183 | } |
184 | } |
184 | |
185 | |
185 | static SV * |
186 | static SV * |
186 | profile_new () |
187 | profile_new (void) |
187 | { |
188 | { |
188 | SV *sv = newSVpvn ("", 0); |
189 | SV *sv = newSVpvn ("", 0); |
189 | |
190 | |
190 | static const struct { |
191 | static const struct { |
191 | int klass; |
192 | int klass; |
192 | int tag; |
193 | int tag; |
193 | int type; |
194 | int type; |
194 | } *celem, default_map[] = { |
195 | } *celem, default_map[] = { |
195 | { ASN_UNIVERSAL, ASN_BOOLEAN , BER_TYPE_BOOL }, |
196 | { ASN_UNIVERSAL, ASN_BOOLEAN , BER_TYPE_BOOL }, |
196 | { ASN_UNIVERSAL, ASN_INTEGER32 , BER_TYPE_INT }, |
197 | { ASN_UNIVERSAL, ASN_INTEGER , BER_TYPE_INT }, |
197 | { ASN_UNIVERSAL, ASN_NULL , BER_TYPE_NULL }, |
198 | { ASN_UNIVERSAL, ASN_NULL , BER_TYPE_NULL }, |
198 | { ASN_UNIVERSAL, ASN_OBJECT_IDENTIFIER, BER_TYPE_OID }, |
199 | { ASN_UNIVERSAL, ASN_OBJECT_IDENTIFIER, BER_TYPE_OID }, |
199 | { ASN_UNIVERSAL, ASN_OBJECT_DESCRIPTOR, BER_TYPE_OID }, |
|
|
200 | { ASN_UNIVERSAL, ASN_RELATIVE_OID , BER_TYPE_RELOID }, |
200 | { ASN_UNIVERSAL, ASN_RELATIVE_OID , BER_TYPE_RELOID }, |
201 | { ASN_UNIVERSAL, ASN_REAL , BER_TYPE_REAL }, |
201 | { ASN_UNIVERSAL, ASN_REAL , BER_TYPE_REAL }, |
|
|
202 | { ASN_UNIVERSAL, ASN_ENUMERATED , BER_TYPE_INT }, |
202 | { ASN_UNIVERSAL, ASN_UTF8_STRING , BER_TYPE_UTF8 }, |
203 | { ASN_UNIVERSAL, ASN_UTF8_STRING , BER_TYPE_UTF8 }, |
203 | { ASN_UNIVERSAL, ASN_BMP_STRING , BER_TYPE_UCS2 }, |
204 | { ASN_UNIVERSAL, ASN_BMP_STRING , BER_TYPE_UCS2 }, |
204 | { ASN_UNIVERSAL, ASN_UNIVERSAL_STRING , BER_TYPE_UCS4 }, |
205 | { ASN_UNIVERSAL, ASN_UNIVERSAL_STRING , BER_TYPE_UCS4 }, |
205 | }; |
206 | }; |
206 | |
207 | |
… | |
… | |
227 | } |
228 | } |
228 | |
229 | |
229 | // get_* functions fetch something from the buffer |
230 | // get_* functions fetch something from the buffer |
230 | // decode_* functions use get_* fun ctions to decode ber values |
231 | // decode_* functions use get_* fun ctions to decode ber values |
231 | |
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 | |
232 | // get n octets |
243 | // get n octets |
233 | static U8 * |
244 | static U8 * |
234 | get_n (UV count) |
245 | get_n (UV count) |
235 | { |
246 | { |
236 | want (count); |
247 | want (count); |
237 | U8 *res = cur; |
248 | U8 *res = cur; |
238 | cur += count; |
249 | cur += count; |
239 | return res; |
250 | return res; |
240 | } |
251 | } |
241 | |
252 | |
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") |
253 | // get ber-encoded integer (i.e. pack "w") |
253 | static U32 |
254 | static UV |
254 | get_w (void) |
255 | get_w (void) |
255 | { |
256 | { |
256 | U32 res = 0; |
257 | UV res = 0; |
|
|
258 | U8 c = get_u8 (); |
|
|
259 | |
|
|
260 | if (expect_false (c == 0x80)) |
|
|
261 | error ("illegal BER padding (X.690 8.1.2.4.2, 8.19.2)"); |
257 | |
262 | |
258 | for (;;) |
263 | for (;;) |
259 | { |
264 | { |
260 | U8 c = get_u8 (); |
265 | if (expect_false (res >> UVSIZE * 8 - 7)) |
|
|
266 | error ("BER variable length integer overflow"); |
|
|
267 | |
261 | res = (res << 7) | (c & 0x7f); |
268 | res = (res << 7) | (c & 0x7f); |
262 | |
269 | |
263 | if (!(c & 0x80)) |
270 | if (expect_true (!(c & 0x80))) |
264 | return res; |
271 | return res; |
265 | } |
|
|
266 | } |
|
|
267 | |
272 | |
|
|
273 | c = get_u8 (); |
|
|
274 | } |
|
|
275 | } |
|
|
276 | |
268 | static U32 |
277 | static UV |
269 | get_length (void) |
278 | get_length (void) |
270 | { |
279 | { |
271 | U32 res = get_u8 (); |
280 | UV res = get_u8 (); |
272 | |
281 | |
273 | if (res & 0x80) |
282 | if (expect_false (res & 0x80)) |
274 | { |
283 | { |
275 | 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 | |
276 | res = 0; |
298 | res = 0; |
277 | |
299 | do |
278 | switch (cnt) |
300 | res = (res << 8) | *cur++; |
279 | { |
301 | 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 4: res = (res << 8) | get_u8 (); |
|
|
289 | case 3: res = (res << 8) | get_u8 (); |
|
|
290 | case 2: res = (res << 8) | get_u8 (); |
|
|
291 | case 1: res = (res << 8) | get_u8 (); |
|
|
292 | } |
|
|
293 | } |
302 | } |
294 | |
303 | |
295 | return res; |
304 | return res; |
296 | } |
305 | } |
297 | |
306 | |
298 | static SV * |
307 | static SV * |
299 | decode_int () |
308 | decode_int (void) |
300 | { |
309 | { |
301 | int len = get_length (); |
310 | UV len = get_length (); |
302 | |
311 | |
303 | if (len <= 0) |
312 | if (!len) |
304 | { |
313 | error ("invalid BER_TYPE_INT length zero (X.690 8.3.1)"); |
305 | error ("integer length equal to zero"); |
|
|
306 | return 0; |
|
|
307 | } |
|
|
308 | |
314 | |
309 | U8 *data = get_n (len); |
315 | U8 *data = get_n (len); |
310 | |
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 | |
311 | int negative = data [0] & 0x80; |
325 | int negative = data [0] & 0x80; |
312 | |
326 | |
313 | 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"); |
314 | |
331 | |
315 | do |
332 | do |
316 | val = (val << 8) | *data++; |
333 | val = (val << 8) | *data++; |
317 | while (--len); |
334 | while (--len); |
318 | |
335 | |
… | |
… | |
322 | } |
339 | } |
323 | |
340 | |
324 | static SV * |
341 | static SV * |
325 | decode_data (void) |
342 | decode_data (void) |
326 | { |
343 | { |
327 | U32 len = get_length (); |
344 | UV len = get_length (); |
328 | U8 *data = get_n (len); |
|
|
329 | return newSVpvn ((char *)data, len); |
345 | return newSVpvn ((char *)get_n (len), len); |
330 | } |
346 | } |
331 | |
347 | |
332 | // gelper for decode_object_identifier |
348 | // helper for decode_object_identifier |
333 | static char * |
349 | static char * |
334 | write_uv (char *buf, U32 u) |
350 | write_uv (char *buf, UV u) |
335 | { |
351 | { |
336 | // the one-digit case is absolutely predominant, so this pays off (hopefully) |
352 | // the one-digit case is absolutely predominant, so this pays off (hopefully) |
337 | if (expect_true (u < 10)) |
353 | if (expect_true (u < 10)) |
338 | *buf++ = u + '0'; |
354 | *buf++ = u + '0'; |
339 | else |
355 | else |
340 | { |
356 | { |
|
|
357 | // this *could* be done much faster using branchless fixed-point arithmetics |
341 | char *beg = buf; |
358 | char *beg = buf; |
342 | |
359 | |
343 | do |
360 | do |
344 | { |
361 | { |
345 | *buf++ = u % 10 + '0'; |
362 | *buf++ = u % 10 + '0'; |
… | |
… | |
347 | } |
364 | } |
348 | while (u); |
365 | while (u); |
349 | |
366 | |
350 | // reverse digits |
367 | // reverse digits |
351 | char *ptr = buf; |
368 | char *ptr = buf; |
352 | while (--ptr != beg) |
369 | while (--ptr > beg) |
353 | { |
370 | { |
354 | char c = *ptr; |
371 | char c = *ptr; |
355 | *ptr = *beg; |
372 | *ptr = *beg; |
356 | *beg = c; |
373 | *beg = c; |
357 | ++beg; |
374 | ++beg; |
… | |
… | |
362 | } |
379 | } |
363 | |
380 | |
364 | static SV * |
381 | static SV * |
365 | decode_oid (int relative) |
382 | decode_oid (int relative) |
366 | { |
383 | { |
367 | U32 len = get_length (); |
384 | UV len = get_length (); |
368 | |
385 | |
369 | if (len <= 0) |
386 | if (len <= 0) |
370 | { |
387 | { |
371 | error ("OBJECT IDENTIFIER length equal to zero"); |
388 | error ("BER_TYPE_OID length must not be zero"); |
372 | return &PL_sv_undef; |
389 | return &PL_sv_undef; |
373 | } |
390 | } |
374 | |
391 | |
375 | U8 *end = cur + len; |
392 | U8 *end = cur + len; |
376 | U32 w = get_w (); |
393 | UV w = get_w (); |
377 | |
394 | |
378 | static char oid[MAX_OID_STRLEN]; // must be static |
395 | static char oid[MAX_OID_STRLEN]; // static, because too large for stack |
379 | char *app = oid; |
396 | char *app = oid; |
380 | |
397 | |
381 | if (relative) |
398 | if (relative) |
382 | app = write_uv (app, w); |
399 | app = write_uv (app, w); |
383 | else |
400 | else |
384 | { |
401 | { |
|
|
402 | UV w1, w2; |
|
|
403 | |
|
|
404 | if (w < 2 * 40) |
|
|
405 | (w1 = w / 40), (w2 = w % 40); |
|
|
406 | else |
|
|
407 | (w1 = 2), (w2 = w - 2 * 40); |
|
|
408 | |
385 | app = write_uv (app, (U8)w / 40); |
409 | app = write_uv (app, w1); |
386 | *app++ = '.'; |
410 | *app++ = '.'; |
387 | app = write_uv (app, (U8)w % 40); |
411 | app = write_uv (app, w2); |
|
|
412 | } |
|
|
413 | |
|
|
414 | while (cur < end) |
388 | } |
415 | { |
389 | |
|
|
390 | // we assume an oid component is never > 64 bytes |
416 | // we assume an oid component is never > 64 digits |
391 | while (cur < end && oid + sizeof (oid) - app > 64) |
417 | if (oid + sizeof (oid) - app < 64) |
392 | { |
418 | croak ("BER_TYPE_OID to long to decode"); |
|
|
419 | |
393 | w = get_w (); |
420 | w = get_w (); |
394 | *app++ = '.'; |
421 | *app++ = '.'; |
395 | app = write_uv (app, w); |
422 | app = write_uv (app, w); |
396 | } |
423 | } |
397 | |
424 | |
… | |
… | |
402 | static SV * |
429 | static SV * |
403 | decode_ucs (int chrsize) |
430 | decode_ucs (int chrsize) |
404 | { |
431 | { |
405 | SV *res = NEWSV (0, 0); |
432 | SV *res = NEWSV (0, 0); |
406 | |
433 | |
407 | U32 len = get_length (); |
434 | UV len = get_length (); |
408 | |
435 | |
409 | if (len & (chrsize - 1)) |
436 | if (len & (chrsize - 1)) |
410 | croak ("BER_TYPE_UCS has an invalid number of octets (%d)", len); |
437 | croak ("BER_TYPE_UCS has an invalid number of octets (%d)", len); |
411 | |
438 | |
412 | while (len) |
439 | while (len) |
… | |
… | |
433 | |
460 | |
434 | return res; |
461 | return res; |
435 | } |
462 | } |
436 | |
463 | |
437 | static SV * |
464 | static SV * |
438 | decode_ber () |
465 | decode_ber (void) |
439 | { |
466 | { |
440 | int identifier = get_u8 (); |
467 | int identifier = get_u8 (); |
441 | |
468 | |
442 | SV *res; |
469 | SV *res; |
443 | |
470 | |
… | |
… | |
446 | int tag = identifier & ASN_TAG_MASK; |
473 | int tag = identifier & ASN_TAG_MASK; |
447 | |
474 | |
448 | if (tag == ASN_TAG_BER) |
475 | if (tag == ASN_TAG_BER) |
449 | tag = get_w (); |
476 | tag = get_w (); |
450 | |
477 | |
451 | if (tag == ASN_TAG_BER) |
|
|
452 | tag = get_w (); |
|
|
453 | |
|
|
454 | if (constructed) |
478 | if (constructed) |
455 | { |
479 | { |
456 | U32 len = get_length (); |
480 | UV len = get_length (); |
457 | U32 seqend = (cur - buf) + len; |
481 | UV seqend = (cur - buf) + len; |
458 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
482 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
459 | |
483 | |
460 | while (cur < buf + seqend) |
484 | while (cur < buf + seqend) |
461 | av_push (av, decode_ber ()); |
485 | av_push (av, decode_ber ()); |
462 | |
486 | |
463 | if (cur > buf + seqend) |
487 | if (cur > buf + seqend) |
464 | croak ("constructed type %02x overflow (%x %x)\n", identifier, cur - buf, seqend); |
488 | croak ("CONSTRUCTED type %02x length overflow (0x%x 0x%x)\n", identifier, (int)(cur - buf), (int)seqend); |
465 | |
489 | |
466 | res = newRV_inc ((SV *)av); |
490 | res = newRV_inc ((SV *)av); |
467 | } |
491 | } |
468 | else |
492 | else |
469 | switch (profile_lookup (cur_profile, klass, tag)) |
493 | switch (profile_lookup (cur_profile, klass, tag)) |
470 | { |
494 | { |
471 | case BER_TYPE_NULL: |
495 | case BER_TYPE_NULL: |
|
|
496 | { |
|
|
497 | UV len = get_length (); |
|
|
498 | |
|
|
499 | if (len) |
|
|
500 | croak ("BER_TYPE_NULL value with non-zero length %d encountered (X.690 8.8.2)", len); |
|
|
501 | |
472 | res = &PL_sv_undef; |
502 | res = &PL_sv_undef; |
|
|
503 | } |
473 | break; |
504 | break; |
474 | |
505 | |
475 | case BER_TYPE_BOOL: |
506 | case BER_TYPE_BOOL: |
476 | { |
507 | { |
477 | U32 len = get_length (); |
508 | UV len = get_length (); |
478 | |
509 | |
479 | if (len != 1) |
510 | if (len != 1) |
480 | croak ("BER_TYPE_BOOLEAN type with invalid length %d encountered", len); |
511 | croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered (X.690 8.2.1)", len); |
481 | |
512 | |
482 | res = newSVcacheint (get_u8 () ? 0 : 1); |
513 | res = newSVcacheint (!!get_u8 ()); |
483 | } |
514 | } |
484 | break; |
515 | break; |
485 | |
516 | |
486 | case BER_TYPE_OID: |
517 | case BER_TYPE_OID: |
487 | res = decode_oid (0); |
518 | res = decode_oid (0); |
… | |
… | |
504 | res = decode_data (); |
535 | res = decode_data (); |
505 | break; |
536 | break; |
506 | |
537 | |
507 | case BER_TYPE_IPADDRESS: |
538 | case BER_TYPE_IPADDRESS: |
508 | { |
539 | { |
509 | U32 len = get_length (); |
540 | UV len = get_length (); |
510 | |
541 | |
511 | if (len != 4) |
542 | if (len != 4) |
512 | croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered", len); |
543 | croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered (RFC 2578 7.1.5)", len); |
513 | |
544 | |
514 | U8 c1 = get_u8 (); |
545 | U8 *data = get_n (4); |
515 | U8 c2 = get_u8 (); |
546 | res = newSVpvf ("%d.%d.%d.%d", data [0], data [1], data [2], data [3]); |
516 | U8 c3 = get_u8 (); |
|
|
517 | U8 c4 = get_u8 (); |
|
|
518 | |
|
|
519 | res = newSVpvf ("%d.%d.%d.%d", c1, c2, c3, c4); |
|
|
520 | } |
547 | } |
521 | break; |
548 | break; |
522 | |
549 | |
523 | case BER_TYPE_UCS2: |
550 | case BER_TYPE_UCS2: |
524 | res = decode_ucs (2); |
551 | res = decode_ucs (2); |
… | |
… | |
527 | case BER_TYPE_UCS4: |
554 | case BER_TYPE_UCS4: |
528 | res = decode_ucs (4); |
555 | res = decode_ucs (4); |
529 | break; |
556 | break; |
530 | |
557 | |
531 | case BER_TYPE_REAL: |
558 | case BER_TYPE_REAL: |
|
|
559 | error ("BER_TYPE_REAL not implemented"); |
|
|
560 | |
532 | case BER_TYPE_CROAK: |
561 | case BER_TYPE_CROAK: |
|
|
562 | croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag); |
|
|
563 | |
533 | default: |
564 | default: |
534 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
565 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
535 | } |
566 | } |
536 | |
567 | |
537 | AV *av = newAV (); |
568 | AV *av = newAV (); |
538 | av_fill (av, BER_ARRAYSIZE - 1); |
569 | av_fill (av, BER_ARRAYSIZE - 1); |
539 | AvARRAY (av)[BER_CLASS ] = newSVcacheint (klass); |
570 | AvARRAY (av)[BER_CLASS] = newSVcacheint (klass); |
540 | AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); |
571 | AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); |
541 | AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (constructed ? 1 : 0); |
572 | AvARRAY (av)[BER_FLAGS] = newSVcacheint (constructed ? 1 : 0); |
542 | AvARRAY (av)[BER_DATA ] = res; |
573 | AvARRAY (av)[BER_DATA ] = res; |
543 | |
574 | |
544 | return newRV_noinc ((SV *)av); |
575 | return newRV_noinc ((SV *)av); |
545 | } |
576 | } |
546 | |
577 | |
547 | ///////////////////////////////////////////////////////////////////////////// |
578 | ///////////////////////////////////////////////////////////////////////////// |
… | |
… | |
552 | strlen_sum (STRLEN l1, STRLEN l2) |
583 | strlen_sum (STRLEN l1, STRLEN l2) |
553 | { |
584 | { |
554 | size_t sum = l1 + l2; |
585 | size_t sum = l1 + l2; |
555 | |
586 | |
556 | if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) |
587 | if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) |
557 | croak ("JSON::XS: string size overflow"); |
588 | croak ("Convert::BER::XS: string size overflow"); |
558 | |
589 | |
559 | return sum; |
590 | return sum; |
560 | } |
591 | } |
561 | |
592 | |
562 | static void |
593 | static void |
… | |
… | |
600 | need (1); |
631 | need (1); |
601 | *cur++ = val; |
632 | *cur++ = val; |
602 | } |
633 | } |
603 | |
634 | |
604 | static void |
635 | static void |
605 | put_w_nocheck (U32 val) |
636 | put_w_nocheck (UV val) |
606 | { |
637 | { |
|
|
638 | #if UVSIZE > 4 |
|
|
639 | *cur = (val >> 7 * 9) | 0x80; cur += val >= ((UV)1 << (7 * 9)); |
|
|
640 | *cur = (val >> 7 * 8) | 0x80; cur += val >= ((UV)1 << (7 * 8)); |
|
|
641 | *cur = (val >> 7 * 7) | 0x80; cur += val >= ((UV)1 << (7 * 7)); |
|
|
642 | *cur = (val >> 7 * 6) | 0x80; cur += val >= ((UV)1 << (7 * 6)); |
|
|
643 | *cur = (val >> 7 * 5) | 0x80; cur += val >= ((UV)1 << (7 * 5)); |
|
|
644 | #endif |
607 | *cur = (val >> 7 * 4) | 0x80; cur += val >= (1 << (7 * 4)); |
645 | *cur = (val >> 7 * 4) | 0x80; cur += val >= ((UV)1 << (7 * 4)); |
608 | *cur = (val >> 7 * 3) | 0x80; cur += val >= (1 << (7 * 3)); |
646 | *cur = (val >> 7 * 3) | 0x80; cur += val >= ((UV)1 << (7 * 3)); |
609 | *cur = (val >> 7 * 2) | 0x80; cur += val >= (1 << (7 * 2)); |
647 | *cur = (val >> 7 * 2) | 0x80; cur += val >= ((UV)1 << (7 * 2)); |
610 | *cur = (val >> 7 * 1) | 0x80; cur += val >= (1 << (7 * 1)); |
648 | *cur = (val >> 7 * 1) | 0x80; cur += val >= ((UV)1 << (7 * 1)); |
611 | *cur = val & 0x7f; cur += 1; |
649 | *cur = val & 0x7f; cur += 1; |
612 | } |
650 | } |
613 | |
651 | |
614 | static void |
652 | static void |
615 | put_w (U32 val) |
653 | put_w (UV val) |
616 | { |
654 | { |
617 | need (5); // we only handle up to 5 bytes |
655 | need (5); // we only handle up to 5 bytes |
618 | |
656 | |
619 | put_w_nocheck (val); |
657 | put_w_nocheck (val); |
620 | } |
658 | } |
621 | |
659 | |
622 | static U8 * |
660 | static U8 * |
623 | put_length_at (U32 val, U8 *cur) |
661 | put_length_at (UV val, U8 *cur) |
624 | { |
662 | { |
625 | if (val < 0x7fU) |
663 | if (val <= 0x7fU) |
626 | *cur++ = val; |
664 | *cur++ = val; |
627 | else |
665 | else |
628 | { |
666 | { |
629 | U8 *lenb = cur++; |
667 | U8 *lenb = cur++; |
630 | |
668 | |
631 | *cur = val >> 24; cur += *cur > 0; |
669 | #if UVSIZE > 4 |
632 | *cur = val >> 16; cur += *cur > 0; |
670 | *cur = val >> 56; cur += val >= ((UV)1 << (8 * 7)); |
633 | *cur = val >> 8; cur += *cur > 0; |
671 | *cur = val >> 48; cur += val >= ((UV)1 << (8 * 6)); |
|
|
672 | *cur = val >> 40; cur += val >= ((UV)1 << (8 * 5)); |
|
|
673 | *cur = val >> 32; cur += val >= ((UV)1 << (8 * 4)); |
|
|
674 | #endif |
|
|
675 | *cur = val >> 24; cur += val >= ((UV)1 << (8 * 3)); |
|
|
676 | *cur = val >> 16; cur += val >= ((UV)1 << (8 * 2)); |
|
|
677 | *cur = val >> 8; cur += val >= ((UV)1 << (8 * 1)); |
634 | *cur = val ; cur += 1; |
678 | *cur = val ; cur += 1; |
635 | |
679 | |
636 | *lenb = 0x80 + cur - lenb - 1; |
680 | *lenb = 0x80 + cur - lenb - 1; |
637 | } |
681 | } |
638 | |
682 | |
639 | return cur; |
683 | return cur; |
640 | } |
684 | } |
641 | |
685 | |
642 | static void |
686 | static void |
643 | put_length (U32 val) |
687 | put_length (UV val) |
644 | { |
688 | { |
645 | need (5 + val); |
689 | need (9 + val); |
646 | cur = put_length_at (val, cur); |
690 | cur = put_length_at (val, cur); |
647 | } |
691 | } |
648 | |
692 | |
649 | // return how many bytes the encoded length requires |
693 | // return how many bytes the encoded length requires |
650 | static int length_length (U32 val) |
694 | static int length_length (UV val) |
651 | { |
695 | { |
652 | return val < 0x7fU |
696 | // use hashing with a DeBruin sequence, anyone? |
|
|
697 | return expect_true (val <= 0x7fU) |
653 | ? 1 |
698 | ? 1 |
654 | : 2 + (val > 0xffU) + (val > 0xffffU) + (val > 0xffffffU); |
699 | : 2 |
|
|
700 | + (val > 0x000000000000ffU) |
|
|
701 | + (val > 0x0000000000ffffU) |
|
|
702 | + (val > 0x00000000ffffffU) |
|
|
703 | #if UVSIZE > 4 |
|
|
704 | + (val > 0x000000ffffffffU) |
|
|
705 | + (val > 0x0000ffffffffffU) |
|
|
706 | + (val > 0x00ffffffffffffU) |
|
|
707 | + (val > 0xffffffffffffffU) |
|
|
708 | #endif |
|
|
709 | ; |
655 | } |
710 | } |
656 | |
711 | |
657 | static void |
712 | static void |
658 | encode_data (const char *ptr, STRLEN len) |
713 | encode_data (const char *ptr, STRLEN len) |
659 | { |
714 | { |
… | |
… | |
721 | |
776 | |
722 | *lenb = cur - lenb - 1; |
777 | *lenb = cur - lenb - 1; |
723 | } |
778 | } |
724 | |
779 | |
725 | // we don't know the length yet, so we optimistically |
780 | // we don't know the length yet, so we optimistically |
726 | // assume the length will need one octet later. if that |
781 | // assume the length will need one octet later. If that |
727 | // turns out to be wrong, we memove as needed. |
782 | // turns out to be wrong, we memmove as needed. |
728 | // mark the beginning |
783 | // mark the beginning |
729 | static STRLEN |
784 | static STRLEN |
730 | len_fixup_mark () |
785 | len_fixup_mark (void) |
731 | { |
786 | { |
732 | return cur++ - buf; |
787 | return cur++ - buf; |
733 | } |
788 | } |
734 | |
789 | |
735 | // patch up the length |
790 | // patch up the length |
… | |
… | |
844 | { |
899 | { |
845 | AV *av = ber_tuple (tuple); |
900 | AV *av = ber_tuple (tuple); |
846 | |
901 | |
847 | int klass = SvIV (AvARRAY (av)[BER_CLASS]); |
902 | int klass = SvIV (AvARRAY (av)[BER_CLASS]); |
848 | int tag = SvIV (AvARRAY (av)[BER_TAG]); |
903 | int tag = SvIV (AvARRAY (av)[BER_TAG]); |
849 | int constructed = SvIV (AvARRAY (av)[BER_CONSTRUCTED]) ? ASN_CONSTRUCTED : 0; |
904 | int constructed = SvIV (AvARRAY (av)[BER_FLAGS]) & 1 ? ASN_CONSTRUCTED : 0; |
850 | SV *data = AvARRAY (av)[BER_DATA]; |
905 | SV *data = AvARRAY (av)[BER_DATA]; |
851 | |
906 | |
852 | int identifier = (klass << ASN_CLASS_SHIFT) | constructed; |
907 | int identifier = (klass << ASN_CLASS_SHIFT) | constructed; |
853 | |
908 | |
854 | if (expect_false (tag >= ASN_TAG_BER)) |
909 | if (expect_false (tag >= ASN_TAG_BER)) |
… | |
… | |
865 | // and adjust later |
920 | // and adjust later |
866 | need (1); |
921 | need (1); |
867 | STRLEN mark = len_fixup_mark (); |
922 | STRLEN mark = len_fixup_mark (); |
868 | |
923 | |
869 | if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV)) |
924 | if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV)) |
870 | croak ("BER constructed data must be array-reference"); |
925 | croak ("BER CONSTRUCTED data must be array-reference"); |
871 | |
926 | |
872 | AV *av = (AV *)SvRV (data); |
927 | AV *av = (AV *)SvRV (data); |
873 | int fill = AvFILL (av); |
928 | int fill = AvFILL (av); |
874 | |
929 | |
875 | if (expect_false (SvRMAGICAL (av))) |
930 | if (expect_false (SvRMAGICAL (av))) |
876 | croak ("BER constructed data must not be tied"); |
931 | croak ("BER CONSTRUCTED data must not be tied"); |
877 | |
932 | |
878 | int i; |
933 | int i; |
879 | for (i = 0; i <= fill; ++i) |
934 | for (i = 0; i <= fill; ++i) |
880 | encode_ber (AvARRAY (av)[i]); |
935 | encode_ber (AvARRAY (av)[i]); |
881 | |
936 | |
… | |
… | |
888 | put_length (0); |
943 | put_length (0); |
889 | break; |
944 | break; |
890 | |
945 | |
891 | case BER_TYPE_BOOL: |
946 | case BER_TYPE_BOOL: |
892 | put_length (1); |
947 | put_length (1); |
893 | *cur++ = SvTRUE (data) ? 0xff : 0x00; |
948 | *cur++ = SvTRUE (data) ? 0xff : 0x00; // 0xff = DER/CER |
894 | break; |
949 | break; |
895 | |
950 | |
896 | case BER_TYPE_OID: |
951 | case BER_TYPE_OID: |
897 | encode_oid (data, 0); |
952 | encode_oid (data, 0); |
898 | break; |
953 | break; |
… | |
… | |
936 | case BER_TYPE_UCS4: |
991 | case BER_TYPE_UCS4: |
937 | encode_ucs (data, 4); |
992 | encode_ucs (data, 4); |
938 | break; |
993 | break; |
939 | |
994 | |
940 | case BER_TYPE_REAL: |
995 | case BER_TYPE_REAL: |
|
|
996 | croak ("BER_TYPE_REAL not implemented"); |
|
|
997 | |
941 | case BER_TYPE_CROAK: |
998 | case BER_TYPE_CROAK: |
|
|
999 | croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag); |
|
|
1000 | |
942 | default: |
1001 | default: |
943 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
1002 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
944 | } |
1003 | } |
945 | |
1004 | |
946 | } |
1005 | } |
… | |
… | |
961 | const char *name; |
1020 | const char *name; |
962 | IV iv; |
1021 | IV iv; |
963 | } *civ, const_iv[] = { |
1022 | } *civ, const_iv[] = { |
964 | #define const_iv(name) { # name, name }, |
1023 | #define const_iv(name) { # name, name }, |
965 | const_iv (ASN_BOOLEAN) |
1024 | const_iv (ASN_BOOLEAN) |
966 | const_iv (ASN_INTEGER32) |
1025 | const_iv (ASN_INTEGER) |
967 | const_iv (ASN_BIT_STRING) |
1026 | const_iv (ASN_BIT_STRING) |
968 | const_iv (ASN_OCTET_STRING) |
1027 | const_iv (ASN_OCTET_STRING) |
969 | const_iv (ASN_NULL) |
1028 | const_iv (ASN_NULL) |
970 | const_iv (ASN_OBJECT_IDENTIFIER) |
1029 | const_iv (ASN_OBJECT_IDENTIFIER) |
971 | const_iv (ASN_OBJECT_DESCRIPTOR) |
1030 | const_iv (ASN_OBJECT_DESCRIPTOR) |
… | |
… | |
1000 | const_iv (ASN_CONTEXT) |
1059 | const_iv (ASN_CONTEXT) |
1001 | const_iv (ASN_PRIVATE) |
1060 | const_iv (ASN_PRIVATE) |
1002 | |
1061 | |
1003 | const_iv (BER_CLASS) |
1062 | const_iv (BER_CLASS) |
1004 | const_iv (BER_TAG) |
1063 | const_iv (BER_TAG) |
1005 | const_iv (BER_CONSTRUCTED) |
1064 | const_iv (BER_FLAGS) |
1006 | const_iv (BER_DATA) |
1065 | const_iv (BER_DATA) |
1007 | |
1066 | |
1008 | const_iv (BER_TYPE_BYTES) |
1067 | const_iv (BER_TYPE_BYTES) |
1009 | const_iv (BER_TYPE_UTF8) |
1068 | const_iv (BER_TYPE_UTF8) |
1010 | const_iv (BER_TYPE_UCS2) |
1069 | const_iv (BER_TYPE_UCS2) |
… | |
… | |
1018 | const_iv (BER_TYPE_IPADDRESS) |
1077 | const_iv (BER_TYPE_IPADDRESS) |
1019 | const_iv (BER_TYPE_CROAK) |
1078 | const_iv (BER_TYPE_CROAK) |
1020 | |
1079 | |
1021 | const_iv (SNMP_IPADDRESS) |
1080 | const_iv (SNMP_IPADDRESS) |
1022 | const_iv (SNMP_COUNTER32) |
1081 | const_iv (SNMP_COUNTER32) |
|
|
1082 | const_iv (SNMP_GAUGE32) |
1023 | const_iv (SNMP_UNSIGNED32) |
1083 | const_iv (SNMP_UNSIGNED32) |
1024 | const_iv (SNMP_TIMETICKS) |
1084 | const_iv (SNMP_TIMETICKS) |
1025 | const_iv (SNMP_OPAQUE) |
1085 | const_iv (SNMP_OPAQUE) |
1026 | const_iv (SNMP_COUNTER64) |
1086 | const_iv (SNMP_COUNTER64) |
1027 | }; |
1087 | }; |
1028 | |
1088 | |
1029 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) |
1089 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) |
1030 | newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); |
1090 | newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); |
1031 | } |
1091 | } |
1032 | |
1092 | |
1033 | SV * |
1093 | void |
1034 | ber_decode (SV *ber, SV *profile = &PL_sv_undef) |
1094 | ber_decode (SV *ber, SV *profile = &PL_sv_undef) |
|
|
1095 | ALIAS: |
|
|
1096 | ber_decode_prefix = 1 |
1035 | CODE: |
1097 | PPCODE: |
1036 | { |
1098 | { |
1037 | cur_profile = SvPROFILE (profile); |
1099 | cur_profile = SvPROFILE (profile); |
1038 | STRLEN len; |
1100 | STRLEN len; |
1039 | buf = (U8 *)SvPVbyte (ber, len); |
1101 | buf = (U8 *)SvPVbyte (ber, len); |
1040 | cur = buf; |
1102 | cur = buf; |
1041 | end = buf + len; |
1103 | end = buf + len; |
1042 | |
1104 | |
1043 | RETVAL = decode_ber (); |
1105 | SV *tuple = decode_ber (); |
|
|
1106 | |
|
|
1107 | EXTEND (SP, 2); |
|
|
1108 | PUSHs (sv_2mortal (tuple)); |
|
|
1109 | |
|
|
1110 | if (ix) |
|
|
1111 | PUSHs (sv_2mortal (newSViv (cur - buf))); |
|
|
1112 | else if (cur != end) |
|
|
1113 | error ("trailing garbage after BER value"); |
1044 | } |
1114 | } |
1045 | OUTPUT: RETVAL |
|
|
1046 | |
1115 | |
1047 | void |
1116 | void |
1048 | ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *constructed = &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) |
1049 | PPCODE: |
1118 | PPCODE: |
1050 | { |
1119 | { |
1051 | if (!SvOK (tuple)) |
1120 | if (!SvOK (tuple)) |
1052 | XSRETURN_NO; |
1121 | XSRETURN_NO; |
1053 | |
1122 | |
… | |
… | |
1055 | croak ("ber_is: tuple must be BER tuple (array-ref)"); |
1124 | croak ("ber_is: tuple must be BER tuple (array-ref)"); |
1056 | |
1125 | |
1057 | AV *av = (AV *)SvRV (tuple); |
1126 | AV *av = (AV *)SvRV (tuple); |
1058 | |
1127 | |
1059 | XPUSHs ( |
1128 | XPUSHs ( |
1060 | (!SvOK (klass) || SvIV (AvARRAY (av)[BER_CLASS ]) == SvIV (klass)) |
1129 | (!SvOK (klass) || SvIV (AvARRAY (av)[BER_CLASS]) == SvIV (klass)) |
1061 | && (!SvOK (tag) || SvIV (AvARRAY (av)[BER_TAG ]) == SvIV (tag)) |
1130 | && (!SvOK (tag) || SvIV (AvARRAY (av)[BER_TAG ]) == SvIV (tag)) |
1062 | && (!SvOK (constructed) || !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) == !SvIV (constructed)) |
1131 | && (!SvOK (flags) || !SvIV (AvARRAY (av)[BER_FLAGS]) == !SvIV (flags)) |
1063 | && (!SvOK (data) || sv_eq (AvARRAY (av)[BER_DATA ], data)) |
1132 | && (!SvOK (data) || sv_eq (AvARRAY (av)[BER_DATA ], data)) |
1064 | ? &PL_sv_yes : &PL_sv_undef); |
1133 | ? &PL_sv_yes : &PL_sv_undef); |
1065 | } |
1134 | } |
1066 | |
1135 | |
1067 | void |
1136 | void |
1068 | ber_is_seq (SV *tuple) |
1137 | ber_is_seq (SV *tuple) |
… | |
… | |
1072 | XSRETURN_UNDEF; |
1141 | XSRETURN_UNDEF; |
1073 | |
1142 | |
1074 | AV *av = ber_tuple (tuple); |
1143 | AV *av = ber_tuple (tuple); |
1075 | |
1144 | |
1076 | XPUSHs ( |
1145 | XPUSHs ( |
1077 | SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL |
1146 | SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL |
1078 | && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_SEQUENCE |
1147 | && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_SEQUENCE |
1079 | && SvIV (AvARRAY (av)[BER_CONSTRUCTED]) |
1148 | && SvIV (AvARRAY (av)[BER_FLAGS]) |
1080 | ? AvARRAY (av)[BER_DATA] : &PL_sv_undef); |
1149 | ? AvARRAY (av)[BER_DATA] : &PL_sv_undef); |
1081 | } |
1150 | } |
1082 | |
1151 | |
1083 | void |
1152 | void |
1084 | ber_is_i32 (SV *tuple, SV *value = &PL_sv_undef) |
1153 | ber_is_int (SV *tuple, SV *value = &PL_sv_undef) |
1085 | PPCODE: |
1154 | PPCODE: |
1086 | { |
1155 | { |
1087 | if (!SvOK (tuple)) |
1156 | if (!SvOK (tuple)) |
1088 | XSRETURN_NO; |
1157 | XSRETURN_NO; |
1089 | |
1158 | |
1090 | AV *av = ber_tuple (tuple); |
1159 | AV *av = ber_tuple (tuple); |
1091 | |
1160 | |
1092 | IV data = SvIV (AvARRAY (av)[BER_DATA]); |
1161 | UV data = SvUV (AvARRAY (av)[BER_DATA]); |
1093 | |
1162 | |
1094 | XPUSHs ( |
1163 | XPUSHs ( |
1095 | SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL |
1164 | SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL |
1096 | && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_INTEGER32 |
1165 | && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_INTEGER |
1097 | && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) |
1166 | && !SvIV (AvARRAY (av)[BER_FLAGS]) |
1098 | && (!SvOK (value) || data == SvIV (value)) |
1167 | && (!SvOK (value) || data == SvUV (value)) |
1099 | ? sv_2mortal (data ? newSViv (data) : newSVpv ("0 but true", 0)) |
1168 | ? sv_2mortal (data ? newSVsv (AvARRAY (av)[BER_DATA]) : newSVpv ("0 but true", 0)) |
1100 | : &PL_sv_undef); |
1169 | : &PL_sv_undef); |
1101 | } |
1170 | } |
1102 | |
1171 | |
1103 | void |
1172 | void |
1104 | ber_is_oid (SV *tuple, SV *oid = &PL_sv_undef) |
1173 | ber_is_oid (SV *tuple, SV *oid = &PL_sv_undef) |
… | |
… | |
1108 | XSRETURN_NO; |
1177 | XSRETURN_NO; |
1109 | |
1178 | |
1110 | AV *av = ber_tuple (tuple); |
1179 | AV *av = ber_tuple (tuple); |
1111 | |
1180 | |
1112 | XPUSHs ( |
1181 | XPUSHs ( |
1113 | SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL |
1182 | SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL |
1114 | && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_OBJECT_IDENTIFIER |
1183 | && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_OBJECT_IDENTIFIER |
1115 | && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) |
1184 | && !SvIV (AvARRAY (av)[BER_FLAGS]) |
1116 | && (!SvOK (oid) || sv_eq (AvARRAY (av)[BER_DATA], oid)) |
1185 | && (!SvOK (oid) || sv_eq (AvARRAY (av)[BER_DATA], oid)) |
1117 | ? newSVsv (AvARRAY (av)[BER_DATA]) : &PL_sv_undef); |
1186 | ? newSVsv (AvARRAY (av)[BER_DATA]) : &PL_sv_undef); |
1118 | } |
1187 | } |
1119 | |
1188 | |
1120 | ############################################################################# |
1189 | ############################################################################# |
… | |
… | |
1133 | SvCUR_set (buf_sv, cur - buf); |
1202 | SvCUR_set (buf_sv, cur - buf); |
1134 | XPUSHs (buf_sv); |
1203 | XPUSHs (buf_sv); |
1135 | } |
1204 | } |
1136 | |
1205 | |
1137 | SV * |
1206 | SV * |
1138 | ber_i32 (IV iv) |
1207 | ber_int (SV *sv) |
1139 | CODE: |
1208 | CODE: |
1140 | { |
1209 | { |
1141 | AV *av = newAV (); |
1210 | AV *av = newAV (); |
1142 | av_fill (av, BER_ARRAYSIZE - 1); |
1211 | av_fill (av, BER_ARRAYSIZE - 1); |
1143 | AvARRAY (av)[BER_CLASS ] = newSVcacheint (ASN_UNIVERSAL); |
1212 | AvARRAY (av)[BER_CLASS] = newSVcacheint (ASN_UNIVERSAL); |
1144 | AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_INTEGER32); |
1213 | AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_INTEGER); |
1145 | AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (0); |
1214 | AvARRAY (av)[BER_FLAGS] = newSVcacheint (0); |
1146 | AvARRAY (av)[BER_DATA ] = newSViv (iv); |
1215 | AvARRAY (av)[BER_DATA ] = newSVsv (sv); |
1147 | RETVAL = newRV_noinc ((SV *)av); |
1216 | RETVAL = newRV_noinc ((SV *)av); |
1148 | } |
1217 | } |
1149 | OUTPUT: RETVAL |
1218 | OUTPUT: RETVAL |
1150 | |
1219 | |
1151 | # TODO: not arrayref, but elements? |
1220 | # TODO: not arrayref, but elements? |
… | |
… | |
1153 | ber_seq (SV *arrayref) |
1222 | ber_seq (SV *arrayref) |
1154 | CODE: |
1223 | CODE: |
1155 | { |
1224 | { |
1156 | AV *av = newAV (); |
1225 | AV *av = newAV (); |
1157 | av_fill (av, BER_ARRAYSIZE - 1); |
1226 | av_fill (av, BER_ARRAYSIZE - 1); |
1158 | AvARRAY (av)[BER_CLASS ] = newSVcacheint (ASN_UNIVERSAL); |
1227 | AvARRAY (av)[BER_CLASS] = newSVcacheint (ASN_UNIVERSAL); |
1159 | AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_SEQUENCE); |
1228 | AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_SEQUENCE); |
1160 | AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (1); |
1229 | AvARRAY (av)[BER_FLAGS] = newSVcacheint (1); |
1161 | AvARRAY (av)[BER_DATA ] = newSVsv (arrayref); |
1230 | AvARRAY (av)[BER_DATA ] = newSVsv (arrayref); |
1162 | RETVAL = newRV_noinc ((SV *)av); |
1231 | RETVAL = newRV_noinc ((SV *)av); |
1163 | } |
1232 | } |
1164 | OUTPUT: RETVAL |
1233 | OUTPUT: RETVAL |
1165 | |
1234 | |
1166 | MODULE = Convert::BER::XS PACKAGE = Convert::BER::XS::Profile |
1235 | MODULE = Convert::BER::XS PACKAGE = Convert::BER::XS::Profile |