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 | |
4 | |
5 | // C99 required |
5 | // C99 required! |
|
|
6 | // this is not just for comments, but also for |
|
|
7 | // integer constant semantics, |
|
|
8 | // sscanf format modifiers and more. |
6 | |
9 | |
7 | enum { |
10 | enum { |
8 | // ASN_TAG |
11 | // ASN_TAG |
9 | ASN_BOOLEAN = 0x01, |
12 | ASN_BOOLEAN = 0x01, |
10 | ASN_INTEGER32 = 0x02, |
13 | ASN_INTEGER = 0x02, |
11 | ASN_BIT_STRING = 0x03, |
14 | ASN_BIT_STRING = 0x03, |
12 | ASN_OCTET_STRING = 0x04, |
15 | ASN_OCTET_STRING = 0x04, |
13 | ASN_NULL = 0x05, |
16 | ASN_NULL = 0x05, |
14 | ASN_OBJECT_IDENTIFIER = 0x06, |
17 | ASN_OBJECT_IDENTIFIER = 0x06, |
15 | ASN_OID = 0x06, |
18 | ASN_OID = 0x06, |
… | |
… | |
55 | ASN_CLASS_SHIFT = 6, |
58 | ASN_CLASS_SHIFT = 6, |
56 | |
59 | |
57 | // ASN_APPLICATION SNMP |
60 | // ASN_APPLICATION SNMP |
58 | SNMP_IPADDRESS = 0x00, |
61 | SNMP_IPADDRESS = 0x00, |
59 | SNMP_COUNTER32 = 0x01, |
62 | SNMP_COUNTER32 = 0x01, |
|
|
63 | SNMP_GAUGE32 = 0x02, |
60 | SNMP_UNSIGNED32 = 0x02, |
64 | SNMP_UNSIGNED32 = 0x02, |
61 | SNMP_TIMETICKS = 0x03, |
65 | SNMP_TIMETICKS = 0x03, |
62 | SNMP_OPAQUE = 0x04, |
66 | SNMP_OPAQUE = 0x04, |
63 | SNMP_COUNTER64 = 0x06, |
67 | SNMP_COUNTER64 = 0x06, |
64 | }; |
68 | }; |
… | |
… | |
77 | BER_TYPE_IPADDRESS, |
81 | BER_TYPE_IPADDRESS, |
78 | BER_TYPE_CROAK, |
82 | BER_TYPE_CROAK, |
79 | }; |
83 | }; |
80 | |
84 | |
81 | enum { |
85 | enum { |
82 | BER_CLASS = 0, |
86 | BER_CLASS = 0, |
83 | BER_TAG = 1, |
87 | BER_TAG = 1, |
84 | BER_CONSTRUCTED = 2, |
88 | BER_FLAGS = 2, |
85 | BER_DATA = 3, |
89 | BER_DATA = 3, |
86 | BER_ARRAYSIZE |
90 | BER_ARRAYSIZE |
87 | }; |
91 | }; |
88 | |
92 | |
89 | #define MAX_OID_STRLEN 4096 |
93 | #define MAX_OID_STRLEN 4096 |
90 | |
94 | |
… | |
… | |
137 | { |
141 | { |
138 | if (!SvOK (profile)) |
142 | if (!SvOK (profile)) |
139 | return default_profile; |
143 | return default_profile; |
140 | |
144 | |
141 | if (!SvROK (profile)) |
145 | if (!SvROK (profile)) |
142 | croak ("invalid profile"); |
146 | croak ("Convert::BER::XS::Profile expected"); |
143 | |
147 | |
144 | profile = SvRV (profile); |
148 | profile = SvRV (profile); |
145 | |
149 | |
146 | if (SvSTASH (profile) != profile_stash) |
150 | if (SvSTASH (profile) != profile_stash) |
147 | croak ("invalid profile object"); |
151 | croak ("Convert::BER::XS::Profile expected"); |
148 | |
152 | |
149 | return (void *)profile; |
153 | return (void *)profile; |
150 | } |
154 | } |
151 | |
155 | |
152 | static int |
156 | static int |
… | |
… | |
159 | return BER_TYPE_BYTES; |
163 | return BER_TYPE_BYTES; |
160 | |
164 | |
161 | return SvPVX (sv)[idx]; |
165 | return SvPVX (sv)[idx]; |
162 | } |
166 | } |
163 | |
167 | |
164 | static int |
168 | static void |
165 | profile_set (profile_type *profile, int klass, int tag, int type) |
169 | profile_set (profile_type *profile, int klass, int tag, int type) |
166 | { |
170 | { |
167 | SV *sv = (SV *)profile; |
171 | SV *sv = (SV *)profile; |
168 | U32 idx = (tag << 2) + klass; |
172 | U32 idx = (tag << 2) + klass; |
169 | STRLEN oldlen = SvCUR (sv); |
173 | STRLEN oldlen = SvCUR (sv); |
… | |
… | |
178 | |
182 | |
179 | SvPVX (sv)[idx] = type; |
183 | SvPVX (sv)[idx] = type; |
180 | } |
184 | } |
181 | |
185 | |
182 | static SV * |
186 | static SV * |
183 | profile_new () |
187 | profile_new (void) |
184 | { |
188 | { |
185 | SV *sv = newSVpvn ("", 0); |
189 | SV *sv = newSVpvn ("", 0); |
186 | |
190 | |
187 | static const struct { |
191 | static const struct { |
188 | int klass; |
192 | int klass; |
189 | int tag; |
193 | int tag; |
190 | int type; |
194 | int type; |
191 | } *celem, default_map[] = { |
195 | } *celem, default_map[] = { |
192 | { ASN_UNIVERSAL, ASN_BOOLEAN , BER_TYPE_BOOL }, |
196 | { ASN_UNIVERSAL, ASN_BOOLEAN , BER_TYPE_BOOL }, |
193 | { ASN_UNIVERSAL, ASN_INTEGER32 , BER_TYPE_INT }, |
197 | { ASN_UNIVERSAL, ASN_INTEGER , BER_TYPE_INT }, |
194 | { ASN_UNIVERSAL, ASN_NULL , BER_TYPE_NULL }, |
198 | { ASN_UNIVERSAL, ASN_NULL , BER_TYPE_NULL }, |
195 | { ASN_UNIVERSAL, ASN_OBJECT_IDENTIFIER, BER_TYPE_OID }, |
199 | { ASN_UNIVERSAL, ASN_OBJECT_IDENTIFIER, BER_TYPE_OID }, |
196 | { ASN_UNIVERSAL, ASN_OBJECT_DESCRIPTOR, BER_TYPE_OID }, |
|
|
197 | { ASN_UNIVERSAL, ASN_RELATIVE_OID , BER_TYPE_RELOID }, |
200 | { ASN_UNIVERSAL, ASN_RELATIVE_OID , BER_TYPE_RELOID }, |
198 | { ASN_UNIVERSAL, ASN_REAL , BER_TYPE_REAL }, |
201 | { ASN_UNIVERSAL, ASN_REAL , BER_TYPE_REAL }, |
|
|
202 | { ASN_UNIVERSAL, ASN_ENUMERATED , BER_TYPE_INT }, |
199 | { ASN_UNIVERSAL, ASN_UTF8_STRING , BER_TYPE_UTF8 }, |
203 | { ASN_UNIVERSAL, ASN_UTF8_STRING , BER_TYPE_UTF8 }, |
200 | { ASN_UNIVERSAL, ASN_BMP_STRING , BER_TYPE_UCS2 }, |
204 | { ASN_UNIVERSAL, ASN_BMP_STRING , BER_TYPE_UCS2 }, |
201 | { ASN_UNIVERSAL, ASN_UNIVERSAL_STRING , BER_TYPE_UCS4 }, |
205 | { ASN_UNIVERSAL, ASN_UNIVERSAL_STRING , BER_TYPE_UCS4 }, |
202 | }; |
206 | }; |
203 | |
207 | |
204 | for (celem = default_map + sizeof (default_map) / sizeof (default_map [0]); celem > default_map; celem--) |
208 | for (celem = default_map + sizeof (default_map) / sizeof (default_map [0]); celem-- > default_map; ) |
205 | profile_set ((void *)sv, celem->klass, celem->tag, celem->type); |
209 | profile_set ((profile_type *)sv, celem->klass, celem->tag, celem->type); |
206 | |
210 | |
207 | return sv_bless (newRV_noinc (sv), profile_stash); |
211 | return sv_bless (newRV_noinc (sv), profile_stash); |
208 | } |
212 | } |
209 | |
213 | |
210 | ///////////////////////////////////////////////////////////////////////////// |
214 | ///////////////////////////////////////////////////////////////////////////// |
… | |
… | |
223 | error ("unexpected end of message buffer"); |
227 | error ("unexpected end of message buffer"); |
224 | } |
228 | } |
225 | |
229 | |
226 | // get_* functions fetch something from the buffer |
230 | // get_* functions fetch something from the buffer |
227 | // decode_* functions use get_* fun ctions to decode ber values |
231 | // decode_* functions use get_* fun ctions to decode ber values |
|
|
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 | } |
228 | |
242 | |
229 | // get n octets |
243 | // get n octets |
230 | static U8 * |
244 | static U8 * |
231 | get_n (UV count) |
245 | get_n (UV count) |
232 | { |
246 | { |
… | |
… | |
234 | U8 *res = cur; |
248 | U8 *res = cur; |
235 | cur += count; |
249 | cur += count; |
236 | return res; |
250 | return res; |
237 | } |
251 | } |
238 | |
252 | |
239 | // get single octet |
|
|
240 | static U8 |
|
|
241 | get_u8 (void) |
|
|
242 | { |
|
|
243 | if (cur == end) |
|
|
244 | error ("unexpected end of message buffer"); |
|
|
245 | |
|
|
246 | return *cur++; |
|
|
247 | } |
|
|
248 | |
|
|
249 | // get ber-encoded integer (i.e. pack "w") |
253 | // get ber-encoded integer (i.e. pack "w") |
250 | static U32 |
254 | static UV |
251 | get_w (void) |
255 | get_w (void) |
252 | { |
256 | { |
253 | 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)"); |
254 | |
262 | |
255 | for (;;) |
263 | for (;;) |
256 | { |
264 | { |
257 | U8 c = get_u8 (); |
265 | if (expect_false (res >> UVSIZE * 8 - 7)) |
|
|
266 | error ("BER variable length integer overflow"); |
|
|
267 | |
258 | res = (res << 7) | (c & 0x7f); |
268 | res = (res << 7) | (c & 0x7f); |
259 | |
269 | |
260 | if (!(c & 0x80)) |
270 | if (!(c & 0x80)) |
261 | return res; |
271 | return res; |
262 | } |
|
|
263 | } |
|
|
264 | |
272 | |
|
|
273 | c = get_u8 (); |
|
|
274 | } |
|
|
275 | } |
|
|
276 | |
265 | static U32 |
277 | static UV |
266 | get_length (void) |
278 | get_length (void) |
267 | { |
279 | { |
268 | U32 res = get_u8 (); |
280 | UV res = get_u8 (); |
269 | |
281 | |
270 | if (res & 0x80) |
282 | if (res & 0x80) |
271 | { |
283 | { |
272 | int cnt = res & 0x7f; |
284 | int cnt = res & 0x7f; |
273 | res = 0; |
285 | res = 0; |
274 | |
286 | |
275 | switch (cnt) |
287 | switch (cnt) |
276 | { |
288 | { |
277 | case 0: |
289 | case 0: |
278 | error ("indefinite ASN.1 lengths not supported"); |
290 | error ("indefinite BER value lengths not supported"); |
279 | return 0; |
291 | |
|
|
292 | case 0x7f: |
|
|
293 | error ("BER reserved value in length (X.690 8.1.3.5)"); |
280 | |
294 | |
281 | default: |
295 | default: |
282 | error ("ASN.1 length too long"); |
296 | error ("BER value length too long (must fit into UV)"); |
283 | return 0; |
|
|
284 | |
297 | |
|
|
298 | #if UVSIZE > 4 |
|
|
299 | case 8: res = (res << 8) | get_u8 (); |
|
|
300 | case 7: res = (res << 8) | get_u8 (); |
|
|
301 | case 6: res = (res << 8) | get_u8 (); |
|
|
302 | case 5: res = (res << 8) | get_u8 (); |
|
|
303 | #endif |
285 | case 4: res = (res << 8) | get_u8 (); |
304 | case 4: res = (res << 8) | get_u8 (); |
286 | case 3: res = (res << 8) | get_u8 (); |
305 | case 3: res = (res << 8) | get_u8 (); |
287 | case 2: res = (res << 8) | get_u8 (); |
306 | case 2: res = (res << 8) | get_u8 (); |
288 | case 1: res = (res << 8) | get_u8 (); |
307 | case 1: res = (res << 8) | get_u8 (); |
289 | } |
308 | } |
… | |
… | |
291 | |
310 | |
292 | return res; |
311 | return res; |
293 | } |
312 | } |
294 | |
313 | |
295 | static SV * |
314 | static SV * |
296 | decode_int () |
315 | decode_int (void) |
297 | { |
316 | { |
298 | int len = get_length (); |
317 | UV len = get_length (); |
299 | |
318 | |
300 | if (len <= 0) |
319 | if (!len) |
301 | { |
320 | error ("invalid BER_TYPE_INT length zero (X.690 8.3.1)"); |
302 | error ("integer length equal to zero"); |
|
|
303 | return 0; |
|
|
304 | } |
|
|
305 | |
321 | |
306 | U8 *data = get_n (len); |
322 | U8 *data = get_n (len); |
307 | |
323 | |
|
|
324 | if (expect_false (len > 1)) |
|
|
325 | { |
|
|
326 | U16 mask = (data [0] << 8) | data [1] & 0xff80; |
|
|
327 | |
|
|
328 | if (expect_false (mask == 0xff80 || mask == 0x0000)) |
|
|
329 | error ("illegal padding in BER_TYPE_INT (X.690 8.3.2)"); |
|
|
330 | } |
|
|
331 | |
308 | int negative = data [0] & 0x80; |
332 | int negative = data [0] & 0x80; |
309 | |
333 | |
310 | UV val = negative ? -1 : 0; // copy signbit to all bits |
334 | UV val = negative ? -1 : 0; // copy signbit to all bits |
|
|
335 | |
|
|
336 | if (len > UVSIZE + (!negative && !*data)) |
|
|
337 | error ("BER_TYPE_INT overflow"); |
311 | |
338 | |
312 | do |
339 | do |
313 | val = (val << 8) | *data++; |
340 | val = (val << 8) | *data++; |
314 | while (--len); |
341 | while (--len); |
315 | |
342 | |
… | |
… | |
319 | } |
346 | } |
320 | |
347 | |
321 | static SV * |
348 | static SV * |
322 | decode_data (void) |
349 | decode_data (void) |
323 | { |
350 | { |
324 | U32 len = get_length (); |
351 | UV len = get_length (); |
325 | U8 *data = get_n (len); |
|
|
326 | return newSVpvn ((char *)data, len); |
352 | return newSVpvn ((char *)get_n (len), len); |
327 | } |
353 | } |
328 | |
354 | |
329 | // gelper for decode_object_identifier |
355 | // helper for decode_object_identifier |
330 | static char * |
356 | static char * |
331 | write_uv (char *buf, U32 u) |
357 | write_uv (char *buf, UV u) |
332 | { |
358 | { |
333 | // the one-digit case is absolutely predominant, so this pays off (hopefully) |
359 | // the one-digit case is absolutely predominant, so this pays off (hopefully) |
334 | if (expect_true (u < 10)) |
360 | if (expect_true (u < 10)) |
335 | *buf++ = u + '0'; |
361 | *buf++ = u + '0'; |
336 | else |
362 | else |
337 | { |
363 | { |
|
|
364 | // this *could* be done much faster using branchless fixed-point arithmetics |
338 | char *beg = buf; |
365 | char *beg = buf; |
339 | |
366 | |
340 | do |
367 | do |
341 | { |
368 | { |
342 | *buf++ = u % 10 + '0'; |
369 | *buf++ = u % 10 + '0'; |
343 | u /= 10; |
370 | u /= 10; |
344 | } |
371 | } |
345 | while (u); |
372 | while (u); |
346 | |
373 | |
347 | // reverse digits |
374 | // reverse digits |
348 | for (char *ptr = buf; --ptr != beg; ++beg) |
375 | char *ptr = buf; |
|
|
376 | while (--ptr > beg) |
349 | { |
377 | { |
350 | char c = *ptr; |
378 | char c = *ptr; |
351 | *ptr = *beg; |
379 | *ptr = *beg; |
352 | *beg = c; |
380 | *beg = c; |
|
|
381 | ++beg; |
353 | } |
382 | } |
354 | } |
383 | } |
355 | |
384 | |
356 | return buf; |
385 | return buf; |
357 | } |
386 | } |
358 | |
387 | |
359 | static SV * |
388 | static SV * |
360 | decode_oid (int relative) |
389 | decode_oid (int relative) |
361 | { |
390 | { |
362 | U32 len = get_length (); |
391 | UV len = get_length (); |
363 | |
392 | |
364 | if (len <= 0) |
393 | if (len <= 0) |
365 | { |
394 | { |
366 | error ("OBJECT IDENTIFIER length equal to zero"); |
395 | error ("BER_TYPE_OID length must not be zero"); |
367 | return &PL_sv_undef; |
396 | return &PL_sv_undef; |
368 | } |
397 | } |
369 | |
398 | |
370 | U8 *end = cur + len; |
399 | U8 *end = cur + len; |
371 | U32 w = get_w (); |
400 | UV w = get_w (); |
372 | |
401 | |
373 | static char oid[MAX_OID_STRLEN]; // must be static |
402 | static char oid[MAX_OID_STRLEN]; // static, because too large for stack |
374 | char *app = oid; |
403 | char *app = oid; |
375 | |
404 | |
376 | if (relative) |
405 | if (relative) |
377 | app = write_uv (app, w); |
406 | app = write_uv (app, w); |
378 | else |
407 | else if (w < 2 * 40) |
379 | { |
408 | { |
380 | app = write_uv (app, (U8)w / 40); |
409 | app = write_uv (app, (U8)w / 40); |
381 | *app++ = '.'; |
410 | *app++ = '.'; |
382 | app = write_uv (app, (U8)w % 40); |
411 | app = write_uv (app, (U8)w % 40); |
383 | } |
412 | } |
|
|
413 | else |
|
|
414 | { |
|
|
415 | app = write_uv (app, 2); |
|
|
416 | *app++ = '.'; |
|
|
417 | app = write_uv (app, w - 2 * 40); |
|
|
418 | } |
384 | |
419 | |
|
|
420 | while (cur < end) |
|
|
421 | { |
385 | // we assume an oid component is never > 64 bytes |
422 | // we assume an oid component is never > 64 digits |
386 | while (cur < end && oid + sizeof (oid) - app > 64) |
423 | if (oid + sizeof (oid) - app < 64) |
387 | { |
424 | croak ("BER_TYPE_OID to long to decode"); |
|
|
425 | |
388 | w = get_w (); |
426 | w = get_w (); |
389 | *app++ = '.'; |
427 | *app++ = '.'; |
390 | app = write_uv (app, w); |
428 | app = write_uv (app, w); |
391 | } |
429 | } |
392 | |
430 | |
… | |
… | |
397 | static SV * |
435 | static SV * |
398 | decode_ucs (int chrsize) |
436 | decode_ucs (int chrsize) |
399 | { |
437 | { |
400 | SV *res = NEWSV (0, 0); |
438 | SV *res = NEWSV (0, 0); |
401 | |
439 | |
402 | U32 len = get_length (); |
440 | UV len = get_length (); |
403 | |
441 | |
404 | if (len & (chrsize - 1)) |
442 | if (len & (chrsize - 1)) |
405 | croak ("BER_TYPE_UCS has an invalid number of octets (%d)", len); |
443 | croak ("BER_TYPE_UCS has an invalid number of octets (%d)", len); |
406 | |
444 | |
407 | while (len) |
445 | while (len) |
… | |
… | |
428 | |
466 | |
429 | return res; |
467 | return res; |
430 | } |
468 | } |
431 | |
469 | |
432 | static SV * |
470 | static SV * |
433 | decode_ber () |
471 | decode_ber (void) |
434 | { |
472 | { |
435 | int identifier = get_u8 (); |
473 | int identifier = get_u8 (); |
436 | |
474 | |
437 | SV *res; |
475 | SV *res; |
438 | |
476 | |
… | |
… | |
441 | int tag = identifier & ASN_TAG_MASK; |
479 | int tag = identifier & ASN_TAG_MASK; |
442 | |
480 | |
443 | if (tag == ASN_TAG_BER) |
481 | if (tag == ASN_TAG_BER) |
444 | tag = get_w (); |
482 | tag = get_w (); |
445 | |
483 | |
446 | if (tag == ASN_TAG_BER) |
|
|
447 | tag = get_w (); |
|
|
448 | |
|
|
449 | if (constructed) |
484 | if (constructed) |
450 | { |
485 | { |
451 | U32 len = get_length (); |
486 | UV len = get_length (); |
452 | U32 seqend = (cur - buf) + len; |
487 | UV seqend = (cur - buf) + len; |
453 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
488 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
454 | |
489 | |
455 | while (cur < buf + seqend) |
490 | while (cur < buf + seqend) |
456 | av_push (av, decode_ber ()); |
491 | av_push (av, decode_ber ()); |
457 | |
492 | |
458 | if (cur > buf + seqend) |
493 | if (cur > buf + seqend) |
459 | croak ("constructed type %02x overflow (%x %x)\n", identifier, cur - buf, seqend); |
494 | croak ("CONSTRUCTED type %02x length overflow (0x%x 0x%x)\n", identifier, (int)(cur - buf), (int)seqend); |
460 | |
495 | |
461 | res = newRV_inc ((SV *)av); |
496 | res = newRV_inc ((SV *)av); |
462 | } |
497 | } |
463 | else |
498 | else |
464 | switch (profile_lookup (cur_profile, klass, tag)) |
499 | switch (profile_lookup (cur_profile, klass, tag)) |
465 | { |
500 | { |
466 | case BER_TYPE_NULL: |
501 | case BER_TYPE_NULL: |
|
|
502 | { |
|
|
503 | UV len = get_length (); |
|
|
504 | |
|
|
505 | if (len) |
|
|
506 | croak ("BER_TYPE_NULL value with non-zero length %d encountered (X.690 8.8.2)", len); |
|
|
507 | |
467 | res = &PL_sv_undef; |
508 | res = &PL_sv_undef; |
|
|
509 | } |
468 | break; |
510 | break; |
469 | |
511 | |
470 | case BER_TYPE_BOOL: |
512 | case BER_TYPE_BOOL: |
471 | { |
513 | { |
472 | U32 len = get_length (); |
514 | UV len = get_length (); |
473 | |
515 | |
474 | if (len != 1) |
516 | if (len != 1) |
475 | croak ("BER_TYPE_BOOLEAN type with invalid length %d encountered", len); |
517 | croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered (X.690 8.2.1)", len); |
476 | |
518 | |
477 | res = newSVcacheint (get_u8 () ? 0 : 1); |
519 | res = newSVcacheint (!!get_u8 ()); |
478 | } |
520 | } |
479 | break; |
521 | break; |
480 | |
522 | |
481 | case BER_TYPE_OID: |
523 | case BER_TYPE_OID: |
482 | res = decode_oid (0); |
524 | res = decode_oid (0); |
… | |
… | |
499 | res = decode_data (); |
541 | res = decode_data (); |
500 | break; |
542 | break; |
501 | |
543 | |
502 | case BER_TYPE_IPADDRESS: |
544 | case BER_TYPE_IPADDRESS: |
503 | { |
545 | { |
504 | U32 len = get_length (); |
546 | UV len = get_length (); |
505 | |
547 | |
506 | if (len != 4) |
548 | if (len != 4) |
507 | croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered", len); |
549 | croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered (RFC 2578 7.1.5)", len); |
508 | |
550 | |
509 | U8 c1 = get_u8 (); |
551 | U8 c1 = get_u8 (); |
510 | U8 c2 = get_u8 (); |
552 | U8 c2 = get_u8 (); |
511 | U8 c3 = get_u8 (); |
553 | U8 c3 = get_u8 (); |
512 | U8 c4 = get_u8 (); |
554 | U8 c4 = get_u8 (); |
… | |
… | |
522 | case BER_TYPE_UCS4: |
564 | case BER_TYPE_UCS4: |
523 | res = decode_ucs (4); |
565 | res = decode_ucs (4); |
524 | break; |
566 | break; |
525 | |
567 | |
526 | case BER_TYPE_REAL: |
568 | case BER_TYPE_REAL: |
|
|
569 | error ("BER_TYPE_REAL not implemented"); |
|
|
570 | |
527 | case BER_TYPE_CROAK: |
571 | case BER_TYPE_CROAK: |
|
|
572 | croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag); |
|
|
573 | |
528 | default: |
574 | default: |
529 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
575 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
530 | } |
576 | } |
531 | |
577 | |
532 | AV *av = newAV (); |
578 | AV *av = newAV (); |
533 | av_fill (av, BER_ARRAYSIZE - 1); |
579 | av_fill (av, BER_ARRAYSIZE - 1); |
534 | AvARRAY (av)[BER_CLASS ] = newSVcacheint (klass); |
580 | AvARRAY (av)[BER_CLASS] = newSVcacheint (klass); |
535 | AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); |
581 | AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); |
536 | AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (constructed ? 1 : 0); |
582 | AvARRAY (av)[BER_FLAGS] = newSVcacheint (constructed ? 1 : 0); |
537 | AvARRAY (av)[BER_DATA ] = res; |
583 | AvARRAY (av)[BER_DATA ] = res; |
538 | |
584 | |
539 | return newRV_noinc ((SV *)av); |
585 | return newRV_noinc ((SV *)av); |
540 | } |
586 | } |
541 | |
587 | |
542 | ///////////////////////////////////////////////////////////////////////////// |
588 | ///////////////////////////////////////////////////////////////////////////// |
… | |
… | |
547 | strlen_sum (STRLEN l1, STRLEN l2) |
593 | strlen_sum (STRLEN l1, STRLEN l2) |
548 | { |
594 | { |
549 | size_t sum = l1 + l2; |
595 | size_t sum = l1 + l2; |
550 | |
596 | |
551 | if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) |
597 | if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) |
552 | croak ("JSON::XS: string size overflow"); |
598 | croak ("Convert::BER::XS: string size overflow"); |
553 | |
599 | |
554 | return sum; |
600 | return sum; |
555 | } |
601 | } |
556 | |
602 | |
557 | static void |
603 | static void |
558 | set_buf (SV *sv) |
604 | set_buf (SV *sv) |
559 | { |
605 | { |
560 | STRLEN len; |
606 | STRLEN len; |
561 | buf_sv = sv; |
607 | buf_sv = sv; |
562 | buf = SvPVbyte (buf_sv, len); |
608 | buf = (U8 *)SvPVbyte (buf_sv, len); |
563 | cur = buf; |
609 | cur = buf; |
564 | end = buf + len; |
610 | end = buf + len; |
565 | } |
611 | } |
566 | |
612 | |
567 | /* similar to SvGROW, but somewhat safer and guarantees exponential realloc strategy */ |
613 | /* similar to SvGROW, but somewhat safer and guarantees exponential realloc strategy */ |
… | |
… | |
581 | need (STRLEN len) |
627 | need (STRLEN len) |
582 | { |
628 | { |
583 | if (expect_false ((uintptr_t)(end - cur) < len)) |
629 | if (expect_false ((uintptr_t)(end - cur) < len)) |
584 | { |
630 | { |
585 | STRLEN pos = cur - buf; |
631 | STRLEN pos = cur - buf; |
586 | buf = my_sv_grow (buf_sv, pos, len); |
632 | buf = (U8 *)my_sv_grow (buf_sv, pos, len); |
587 | cur = buf + pos; |
633 | cur = buf + pos; |
588 | end = buf + SvLEN (buf_sv) - 1; |
634 | end = buf + SvLEN (buf_sv) - 1; |
589 | } |
635 | } |
590 | } |
636 | } |
591 | |
637 | |
… | |
… | |
595 | need (1); |
641 | need (1); |
596 | *cur++ = val; |
642 | *cur++ = val; |
597 | } |
643 | } |
598 | |
644 | |
599 | static void |
645 | static void |
600 | put_w_nocheck (U32 val) |
646 | put_w_nocheck (UV val) |
601 | { |
647 | { |
|
|
648 | #if UVSIZE > 4 |
|
|
649 | *cur = (val >> 7 * 9) | 0x80; cur += val >= ((UV)1 << (7 * 9)); |
|
|
650 | *cur = (val >> 7 * 8) | 0x80; cur += val >= ((UV)1 << (7 * 8)); |
|
|
651 | *cur = (val >> 7 * 7) | 0x80; cur += val >= ((UV)1 << (7 * 7)); |
|
|
652 | *cur = (val >> 7 * 6) | 0x80; cur += val >= ((UV)1 << (7 * 6)); |
|
|
653 | *cur = (val >> 7 * 5) | 0x80; cur += val >= ((UV)1 << (7 * 5)); |
|
|
654 | #endif |
602 | *cur = (val >> 7 * 4) | 0x80; cur += val >= (1 << (7 * 4)); |
655 | *cur = (val >> 7 * 4) | 0x80; cur += val >= ((UV)1 << (7 * 4)); |
603 | *cur = (val >> 7 * 3) | 0x80; cur += val >= (1 << (7 * 3)); |
656 | *cur = (val >> 7 * 3) | 0x80; cur += val >= ((UV)1 << (7 * 3)); |
604 | *cur = (val >> 7 * 2) | 0x80; cur += val >= (1 << (7 * 2)); |
657 | *cur = (val >> 7 * 2) | 0x80; cur += val >= ((UV)1 << (7 * 2)); |
605 | *cur = (val >> 7 * 1) | 0x80; cur += val >= (1 << (7 * 1)); |
658 | *cur = (val >> 7 * 1) | 0x80; cur += val >= ((UV)1 << (7 * 1)); |
606 | *cur = val & 0x7f; cur += 1; |
659 | *cur = val & 0x7f; cur += 1; |
607 | } |
660 | } |
608 | |
661 | |
609 | static void |
662 | static void |
610 | put_w (U32 val) |
663 | put_w (UV val) |
611 | { |
664 | { |
612 | need (5); // we only handle up to 5 bytes |
665 | need (5); // we only handle up to 5 bytes |
613 | |
666 | |
614 | put_w_nocheck (val); |
667 | put_w_nocheck (val); |
615 | } |
668 | } |
616 | |
669 | |
617 | static U8 * |
670 | static U8 * |
618 | put_length_at (U32 val, U8 *cur) |
671 | put_length_at (UV val, U8 *cur) |
619 | { |
672 | { |
620 | if (val < 0x7fU) |
673 | if (val < 0x7fU) |
621 | *cur++ = val; |
674 | *cur++ = val; |
622 | else |
675 | else |
623 | { |
676 | { |
624 | U8 *lenb = cur++; |
677 | U8 *lenb = cur++; |
625 | |
678 | |
|
|
679 | #if UVSIZE > 4 |
|
|
680 | *cur = val >> 56; cur += *cur > 0; |
|
|
681 | *cur = val >> 48; cur += *cur > 0; |
|
|
682 | *cur = val >> 40; cur += *cur > 0; |
|
|
683 | *cur = val >> 32; cur += *cur > 0; |
|
|
684 | #endif |
626 | *cur = val >> 24; cur += *cur > 0; |
685 | *cur = val >> 24; cur += *cur > 0; |
627 | *cur = val >> 16; cur += *cur > 0; |
686 | *cur = val >> 16; cur += *cur > 0; |
628 | *cur = val >> 8; cur += *cur > 0; |
687 | *cur = val >> 8; cur += *cur > 0; |
629 | *cur = val ; cur += 1; |
688 | *cur = val ; cur += 1; |
630 | |
689 | |
… | |
… | |
633 | |
692 | |
634 | return cur; |
693 | return cur; |
635 | } |
694 | } |
636 | |
695 | |
637 | static void |
696 | static void |
638 | put_length (U32 val) |
697 | put_length (UV val) |
639 | { |
698 | { |
640 | need (5 + val); |
699 | need (5 + val); |
641 | cur = put_length_at (val, cur); |
700 | cur = put_length_at (val, cur); |
642 | } |
701 | } |
643 | |
702 | |
644 | // return how many bytes the encoded length requires |
703 | // return how many bytes the encoded length requires |
645 | static int length_length (U32 val) |
704 | static int length_length (UV val) |
646 | { |
705 | { |
647 | return val < 0x7fU |
706 | return val < 0x7fU |
648 | ? 1 |
707 | ? 1 |
649 | : 2 + (val > 0xffU) + (val > 0xffffU) + (val > 0xffffffU); |
708 | : 2 |
|
|
709 | + (val > 0xffU) |
|
|
710 | + (val > 0xffffU) |
|
|
711 | + (val > 0xffffffU) |
|
|
712 | #if UVSIZE > 4 |
|
|
713 | + (val > 0xffffffffU) |
|
|
714 | + (val > 0xffffffffffU) |
|
|
715 | + (val > 0xffffffffffffU) |
|
|
716 | + (val > 0xffffffffffffffU) |
|
|
717 | #endif |
|
|
718 | ; |
650 | } |
719 | } |
651 | |
720 | |
652 | static void |
721 | static void |
653 | encode_data (const char *ptr, STRLEN len) |
722 | encode_data (const char *ptr, STRLEN len) |
654 | { |
723 | { |
… | |
… | |
716 | |
785 | |
717 | *lenb = cur - lenb - 1; |
786 | *lenb = cur - lenb - 1; |
718 | } |
787 | } |
719 | |
788 | |
720 | // we don't know the length yet, so we optimistically |
789 | // we don't know the length yet, so we optimistically |
721 | // assume the length will need one octet later. if that |
790 | // assume the length will need one octet later. If that |
722 | // turns out to be wrong, we memove as needed. |
791 | // turns out to be wrong, we memmove as needed. |
723 | // mark the beginning |
792 | // mark the beginning |
724 | static STRLEN |
793 | static STRLEN |
725 | len_fixup_mark () |
794 | len_fixup_mark (void) |
726 | { |
795 | { |
727 | return cur++ - buf; |
796 | return cur++ - buf; |
728 | } |
797 | } |
729 | |
798 | |
730 | // patch up the length |
799 | // patch up the length |
… | |
… | |
817 | put_length (uchars * chrsize); |
886 | put_length (uchars * chrsize); |
818 | |
887 | |
819 | while (uchars--) |
888 | while (uchars--) |
820 | { |
889 | { |
821 | STRLEN uclen; |
890 | STRLEN uclen; |
822 | UV uchr = utf8_to_uvchr_buf (ptr, ptr + len, &uclen); |
891 | UV uchr = utf8_to_uvchr_buf ((U8 *)ptr, (U8 *)ptr + len, &uclen); |
823 | |
892 | |
824 | ptr += uclen; |
893 | ptr += uclen; |
825 | len -= uclen; |
894 | len -= uclen; |
826 | |
895 | |
827 | if (chrsize == 4) |
896 | if (chrsize == 4) |
… | |
… | |
839 | { |
908 | { |
840 | AV *av = ber_tuple (tuple); |
909 | AV *av = ber_tuple (tuple); |
841 | |
910 | |
842 | int klass = SvIV (AvARRAY (av)[BER_CLASS]); |
911 | int klass = SvIV (AvARRAY (av)[BER_CLASS]); |
843 | int tag = SvIV (AvARRAY (av)[BER_TAG]); |
912 | int tag = SvIV (AvARRAY (av)[BER_TAG]); |
844 | int constructed = SvIV (AvARRAY (av)[BER_CONSTRUCTED]) ? ASN_CONSTRUCTED : 0; |
913 | int constructed = SvIV (AvARRAY (av)[BER_FLAGS]) & 1 ? ASN_CONSTRUCTED : 0; |
845 | SV *data = AvARRAY (av)[BER_DATA]; |
914 | SV *data = AvARRAY (av)[BER_DATA]; |
846 | |
915 | |
847 | int identifier = (klass << ASN_CLASS_SHIFT) | constructed; |
916 | int identifier = (klass << ASN_CLASS_SHIFT) | constructed; |
848 | |
917 | |
849 | if (expect_false (tag >= ASN_TAG_BER)) |
918 | if (expect_false (tag >= ASN_TAG_BER)) |
… | |
… | |
860 | // and adjust later |
929 | // and adjust later |
861 | need (1); |
930 | need (1); |
862 | STRLEN mark = len_fixup_mark (); |
931 | STRLEN mark = len_fixup_mark (); |
863 | |
932 | |
864 | if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV)) |
933 | if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV)) |
865 | croak ("BER constructed data must be array-reference"); |
934 | croak ("BER CONSTRUCTED data must be array-reference"); |
866 | |
935 | |
867 | AV *av = (AV *)SvRV (data); |
936 | AV *av = (AV *)SvRV (data); |
868 | int fill = AvFILL (av); |
937 | int fill = AvFILL (av); |
869 | |
938 | |
870 | if (expect_false (SvRMAGICAL (av))) |
939 | if (expect_false (SvRMAGICAL (av))) |
871 | croak ("BER constructed data must not be tied"); |
940 | croak ("BER CONSTRUCTED data must not be tied"); |
872 | |
941 | |
|
|
942 | int i; |
873 | for (int i = 0; i <= fill; ++i) |
943 | for (i = 0; i <= fill; ++i) |
874 | encode_ber (AvARRAY (av)[i]); |
944 | encode_ber (AvARRAY (av)[i]); |
875 | |
945 | |
876 | len_fixup (mark); |
946 | len_fixup (mark); |
877 | } |
947 | } |
878 | else |
948 | else |
… | |
… | |
882 | put_length (0); |
952 | put_length (0); |
883 | break; |
953 | break; |
884 | |
954 | |
885 | case BER_TYPE_BOOL: |
955 | case BER_TYPE_BOOL: |
886 | put_length (1); |
956 | put_length (1); |
887 | *cur++ = SvTRUE (data) ? 0xff : 0x00; |
957 | *cur++ = SvTRUE (data) ? 0xff : 0x00; // 0xff = DER/CER |
888 | break; |
958 | break; |
889 | |
959 | |
890 | case BER_TYPE_OID: |
960 | case BER_TYPE_OID: |
891 | encode_oid (data, 0); |
961 | encode_oid (data, 0); |
892 | break; |
962 | break; |
… | |
… | |
930 | case BER_TYPE_UCS4: |
1000 | case BER_TYPE_UCS4: |
931 | encode_ucs (data, 4); |
1001 | encode_ucs (data, 4); |
932 | break; |
1002 | break; |
933 | |
1003 | |
934 | case BER_TYPE_REAL: |
1004 | case BER_TYPE_REAL: |
|
|
1005 | croak ("BER_TYPE_REAL not implemented"); |
|
|
1006 | |
935 | case BER_TYPE_CROAK: |
1007 | case BER_TYPE_CROAK: |
|
|
1008 | croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag); |
|
|
1009 | |
936 | default: |
1010 | default: |
937 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
1011 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
938 | } |
1012 | } |
939 | |
1013 | |
940 | } |
1014 | } |
… | |
… | |
955 | const char *name; |
1029 | const char *name; |
956 | IV iv; |
1030 | IV iv; |
957 | } *civ, const_iv[] = { |
1031 | } *civ, const_iv[] = { |
958 | #define const_iv(name) { # name, name }, |
1032 | #define const_iv(name) { # name, name }, |
959 | const_iv (ASN_BOOLEAN) |
1033 | const_iv (ASN_BOOLEAN) |
960 | const_iv (ASN_INTEGER32) |
1034 | const_iv (ASN_INTEGER) |
961 | const_iv (ASN_BIT_STRING) |
1035 | const_iv (ASN_BIT_STRING) |
962 | const_iv (ASN_OCTET_STRING) |
1036 | const_iv (ASN_OCTET_STRING) |
963 | const_iv (ASN_NULL) |
1037 | const_iv (ASN_NULL) |
964 | const_iv (ASN_OBJECT_IDENTIFIER) |
1038 | const_iv (ASN_OBJECT_IDENTIFIER) |
965 | const_iv (ASN_OBJECT_DESCRIPTOR) |
1039 | const_iv (ASN_OBJECT_DESCRIPTOR) |
… | |
… | |
994 | const_iv (ASN_CONTEXT) |
1068 | const_iv (ASN_CONTEXT) |
995 | const_iv (ASN_PRIVATE) |
1069 | const_iv (ASN_PRIVATE) |
996 | |
1070 | |
997 | const_iv (BER_CLASS) |
1071 | const_iv (BER_CLASS) |
998 | const_iv (BER_TAG) |
1072 | const_iv (BER_TAG) |
999 | const_iv (BER_CONSTRUCTED) |
1073 | const_iv (BER_FLAGS) |
1000 | const_iv (BER_DATA) |
1074 | const_iv (BER_DATA) |
1001 | |
1075 | |
1002 | const_iv (BER_TYPE_BYTES) |
1076 | const_iv (BER_TYPE_BYTES) |
1003 | const_iv (BER_TYPE_UTF8) |
1077 | const_iv (BER_TYPE_UTF8) |
1004 | const_iv (BER_TYPE_UCS2) |
1078 | const_iv (BER_TYPE_UCS2) |
… | |
… | |
1012 | const_iv (BER_TYPE_IPADDRESS) |
1086 | const_iv (BER_TYPE_IPADDRESS) |
1013 | const_iv (BER_TYPE_CROAK) |
1087 | const_iv (BER_TYPE_CROAK) |
1014 | |
1088 | |
1015 | const_iv (SNMP_IPADDRESS) |
1089 | const_iv (SNMP_IPADDRESS) |
1016 | const_iv (SNMP_COUNTER32) |
1090 | const_iv (SNMP_COUNTER32) |
|
|
1091 | const_iv (SNMP_GAUGE32) |
1017 | const_iv (SNMP_UNSIGNED32) |
1092 | const_iv (SNMP_UNSIGNED32) |
1018 | const_iv (SNMP_TIMETICKS) |
1093 | const_iv (SNMP_TIMETICKS) |
1019 | const_iv (SNMP_OPAQUE) |
1094 | const_iv (SNMP_OPAQUE) |
1020 | const_iv (SNMP_COUNTER64) |
1095 | const_iv (SNMP_COUNTER64) |
1021 | }; |
1096 | }; |
1022 | |
1097 | |
1023 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) |
1098 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) |
1024 | newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); |
1099 | newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); |
1025 | } |
1100 | } |
1026 | |
1101 | |
1027 | SV * |
1102 | void |
1028 | ber_decode (SV *ber, SV *profile = &PL_sv_undef) |
1103 | ber_decode (SV *ber, SV *profile = &PL_sv_undef) |
|
|
1104 | ALIAS: |
|
|
1105 | ber_decode_prefix = 1 |
1029 | CODE: |
1106 | PPCODE: |
1030 | { |
1107 | { |
1031 | cur_profile = SvPROFILE (profile); |
1108 | cur_profile = SvPROFILE (profile); |
1032 | STRLEN len; |
1109 | STRLEN len; |
1033 | buf = SvPVbyte (ber, len); |
1110 | buf = (U8 *)SvPVbyte (ber, len); |
1034 | cur = buf; |
1111 | cur = buf; |
1035 | end = buf + len; |
1112 | end = buf + len; |
1036 | |
1113 | |
1037 | RETVAL = decode_ber (); |
1114 | SV *tuple = decode_ber (); |
|
|
1115 | |
|
|
1116 | EXTEND (SP, 2); |
|
|
1117 | PUSHs (sv_2mortal (tuple)); |
|
|
1118 | |
|
|
1119 | if (ix) |
|
|
1120 | PUSHs (sv_2mortal (newSViv (cur - buf))); |
|
|
1121 | else if (cur != end) |
|
|
1122 | error ("trailing garbage after BER value"); |
1038 | } |
1123 | } |
1039 | OUTPUT: RETVAL |
|
|
1040 | |
1124 | |
1041 | void |
1125 | void |
1042 | ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *constructed = &PL_sv_undef, SV *data = &PL_sv_undef) |
1126 | ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *flags = &PL_sv_undef, SV *data = &PL_sv_undef) |
1043 | PPCODE: |
1127 | PPCODE: |
1044 | { |
1128 | { |
1045 | if (!SvOK (tuple)) |
1129 | if (!SvOK (tuple)) |
1046 | XSRETURN_NO; |
1130 | XSRETURN_NO; |
1047 | |
1131 | |
… | |
… | |
1049 | croak ("ber_is: tuple must be BER tuple (array-ref)"); |
1133 | croak ("ber_is: tuple must be BER tuple (array-ref)"); |
1050 | |
1134 | |
1051 | AV *av = (AV *)SvRV (tuple); |
1135 | AV *av = (AV *)SvRV (tuple); |
1052 | |
1136 | |
1053 | XPUSHs ( |
1137 | XPUSHs ( |
1054 | (!SvOK (klass) || SvIV (AvARRAY (av)[BER_CLASS ]) == SvIV (klass)) |
1138 | (!SvOK (klass) || SvIV (AvARRAY (av)[BER_CLASS]) == SvIV (klass)) |
1055 | && (!SvOK (tag) || SvIV (AvARRAY (av)[BER_TAG ]) == SvIV (tag)) |
1139 | && (!SvOK (tag) || SvIV (AvARRAY (av)[BER_TAG ]) == SvIV (tag)) |
1056 | && (!SvOK (constructed) || !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) == !SvIV (constructed)) |
1140 | && (!SvOK (flags) || !SvIV (AvARRAY (av)[BER_FLAGS]) == !SvIV (flags)) |
1057 | && (!SvOK (data) || sv_eq (AvARRAY (av)[BER_DATA ], data)) |
1141 | && (!SvOK (data) || sv_eq (AvARRAY (av)[BER_DATA ], data)) |
1058 | ? &PL_sv_yes : &PL_sv_undef); |
1142 | ? &PL_sv_yes : &PL_sv_undef); |
1059 | } |
1143 | } |
1060 | |
1144 | |
1061 | void |
1145 | void |
1062 | ber_is_seq (SV *tuple) |
1146 | ber_is_seq (SV *tuple) |
… | |
… | |
1066 | XSRETURN_UNDEF; |
1150 | XSRETURN_UNDEF; |
1067 | |
1151 | |
1068 | AV *av = ber_tuple (tuple); |
1152 | AV *av = ber_tuple (tuple); |
1069 | |
1153 | |
1070 | XPUSHs ( |
1154 | XPUSHs ( |
1071 | SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL |
1155 | SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL |
1072 | && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_SEQUENCE |
1156 | && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_SEQUENCE |
1073 | && SvIV (AvARRAY (av)[BER_CONSTRUCTED]) |
1157 | && SvIV (AvARRAY (av)[BER_FLAGS]) |
1074 | ? AvARRAY (av)[BER_DATA] : &PL_sv_undef); |
1158 | ? AvARRAY (av)[BER_DATA] : &PL_sv_undef); |
1075 | } |
1159 | } |
1076 | |
1160 | |
1077 | void |
1161 | void |
1078 | ber_is_i32 (SV *tuple, SV *value = &PL_sv_undef) |
1162 | ber_is_int (SV *tuple, SV *value = &PL_sv_undef) |
1079 | PPCODE: |
1163 | PPCODE: |
1080 | { |
1164 | { |
1081 | if (!SvOK (tuple)) |
1165 | if (!SvOK (tuple)) |
1082 | XSRETURN_NO; |
1166 | XSRETURN_NO; |
1083 | |
1167 | |
1084 | AV *av = ber_tuple (tuple); |
1168 | AV *av = ber_tuple (tuple); |
1085 | |
1169 | |
1086 | IV data = SvIV (AvARRAY (av)[BER_DATA]); |
1170 | UV data = SvUV (AvARRAY (av)[BER_DATA]); |
1087 | |
1171 | |
1088 | XPUSHs ( |
1172 | XPUSHs ( |
1089 | SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL |
1173 | SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL |
1090 | && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_INTEGER32 |
1174 | && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_INTEGER |
1091 | && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) |
1175 | && !SvIV (AvARRAY (av)[BER_FLAGS]) |
1092 | && (!SvOK (value) || data == SvIV (value)) |
1176 | && (!SvOK (value) || data == SvUV (value)) |
1093 | ? sv_2mortal (data ? newSViv (data) : newSVpv ("0 but true", 0)) |
1177 | ? sv_2mortal (data ? newSVsv (AvARRAY (av)[BER_DATA]) : newSVpv ("0 but true", 0)) |
1094 | : &PL_sv_undef); |
1178 | : &PL_sv_undef); |
1095 | } |
1179 | } |
1096 | |
1180 | |
1097 | void |
1181 | void |
1098 | ber_is_oid (SV *tuple, SV *oid = &PL_sv_undef) |
1182 | ber_is_oid (SV *tuple, SV *oid = &PL_sv_undef) |
… | |
… | |
1102 | XSRETURN_NO; |
1186 | XSRETURN_NO; |
1103 | |
1187 | |
1104 | AV *av = ber_tuple (tuple); |
1188 | AV *av = ber_tuple (tuple); |
1105 | |
1189 | |
1106 | XPUSHs ( |
1190 | XPUSHs ( |
1107 | SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL |
1191 | SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL |
1108 | && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_OBJECT_IDENTIFIER |
1192 | && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_OBJECT_IDENTIFIER |
1109 | && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) |
1193 | && !SvIV (AvARRAY (av)[BER_FLAGS]) |
1110 | && (!SvOK (oid) || sv_eq (AvARRAY (av)[BER_DATA], oid)) |
1194 | && (!SvOK (oid) || sv_eq (AvARRAY (av)[BER_DATA], oid)) |
1111 | ? newSVsv (AvARRAY (av)[BER_DATA]) : &PL_sv_undef); |
1195 | ? newSVsv (AvARRAY (av)[BER_DATA]) : &PL_sv_undef); |
1112 | } |
1196 | } |
1113 | |
1197 | |
1114 | ############################################################################# |
1198 | ############################################################################# |
… | |
… | |
1127 | SvCUR_set (buf_sv, cur - buf); |
1211 | SvCUR_set (buf_sv, cur - buf); |
1128 | XPUSHs (buf_sv); |
1212 | XPUSHs (buf_sv); |
1129 | } |
1213 | } |
1130 | |
1214 | |
1131 | SV * |
1215 | SV * |
1132 | ber_i32 (IV iv) |
1216 | ber_int (SV *sv) |
1133 | CODE: |
1217 | CODE: |
1134 | { |
1218 | { |
1135 | AV *av = newAV (); |
1219 | AV *av = newAV (); |
1136 | av_fill (av, BER_ARRAYSIZE - 1); |
1220 | av_fill (av, BER_ARRAYSIZE - 1); |
1137 | AvARRAY (av)[BER_CLASS ] = newSVcacheint (ASN_UNIVERSAL); |
1221 | AvARRAY (av)[BER_CLASS] = newSVcacheint (ASN_UNIVERSAL); |
1138 | AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_INTEGER32); |
1222 | AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_INTEGER); |
1139 | AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (0); |
1223 | AvARRAY (av)[BER_FLAGS] = newSVcacheint (0); |
1140 | AvARRAY (av)[BER_DATA ] = newSViv (iv); |
1224 | AvARRAY (av)[BER_DATA ] = newSVsv (sv); |
1141 | RETVAL = newRV_noinc ((SV *)av); |
1225 | RETVAL = newRV_noinc ((SV *)av); |
1142 | } |
1226 | } |
1143 | OUTPUT: RETVAL |
1227 | OUTPUT: RETVAL |
1144 | |
1228 | |
1145 | # TODO: not arrayref, but elements? |
1229 | # TODO: not arrayref, but elements? |
… | |
… | |
1147 | ber_seq (SV *arrayref) |
1231 | ber_seq (SV *arrayref) |
1148 | CODE: |
1232 | CODE: |
1149 | { |
1233 | { |
1150 | AV *av = newAV (); |
1234 | AV *av = newAV (); |
1151 | av_fill (av, BER_ARRAYSIZE - 1); |
1235 | av_fill (av, BER_ARRAYSIZE - 1); |
1152 | AvARRAY (av)[BER_CLASS ] = newSVcacheint (ASN_UNIVERSAL); |
1236 | AvARRAY (av)[BER_CLASS] = newSVcacheint (ASN_UNIVERSAL); |
1153 | AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_SEQUENCE); |
1237 | AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_SEQUENCE); |
1154 | AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (1); |
1238 | AvARRAY (av)[BER_FLAGS] = newSVcacheint (1); |
1155 | AvARRAY (av)[BER_DATA ] = newSVsv (arrayref); |
1239 | AvARRAY (av)[BER_DATA ] = newSVsv (arrayref); |
1156 | RETVAL = newRV_noinc ((SV *)av); |
1240 | RETVAL = newRV_noinc ((SV *)av); |
1157 | } |
1241 | } |
1158 | OUTPUT: RETVAL |
1242 | OUTPUT: RETVAL |
1159 | |
1243 | |
1160 | MODULE = Convert::BER::XS PACKAGE = Convert::BER::XS::Profile |
1244 | MODULE = Convert::BER::XS PACKAGE = Convert::BER::XS::Profile |