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. |
9 | |
11 | |
10 | enum { |
12 | enum { |
11 | // ASN_TAG |
13 | // ASN_TAG |
12 | ASN_BOOLEAN = 0x01, |
14 | ASN_BOOLEAN = 0x01, |
13 | ASN_INTEGER32 = 0x02, |
15 | ASN_INTEGER = 0x02, |
14 | ASN_BIT_STRING = 0x03, |
16 | ASN_BIT_STRING = 0x03, |
15 | ASN_OCTET_STRING = 0x04, |
17 | ASN_OCTET_STRING = 0x04, |
16 | ASN_NULL = 0x05, |
18 | ASN_NULL = 0x05, |
17 | ASN_OBJECT_IDENTIFIER = 0x06, |
19 | ASN_OBJECT_IDENTIFIER = 0x06, |
18 | ASN_OID = 0x06, |
20 | ASN_OID = 0x06, |
… | |
… | |
58 | ASN_CLASS_SHIFT = 6, |
60 | ASN_CLASS_SHIFT = 6, |
59 | |
61 | |
60 | // ASN_APPLICATION SNMP |
62 | // ASN_APPLICATION SNMP |
61 | SNMP_IPADDRESS = 0x00, |
63 | SNMP_IPADDRESS = 0x00, |
62 | SNMP_COUNTER32 = 0x01, |
64 | SNMP_COUNTER32 = 0x01, |
|
|
65 | SNMP_GAUGE32 = 0x02, |
63 | SNMP_UNSIGNED32 = 0x02, |
66 | SNMP_UNSIGNED32 = 0x02, |
64 | SNMP_TIMETICKS = 0x03, |
67 | SNMP_TIMETICKS = 0x03, |
65 | SNMP_OPAQUE = 0x04, |
68 | SNMP_OPAQUE = 0x04, |
66 | SNMP_COUNTER64 = 0x06, |
69 | SNMP_COUNTER64 = 0x06, |
67 | }; |
70 | }; |
68 | |
71 | |
|
|
72 | // low-level types this module can ecode the above (and more) into |
69 | enum { |
73 | enum { |
70 | BER_TYPE_BYTES, |
74 | BER_TYPE_BYTES, |
71 | BER_TYPE_UTF8, |
75 | BER_TYPE_UTF8, |
72 | BER_TYPE_UCS2, |
76 | BER_TYPE_UCS2, |
73 | BER_TYPE_UCS4, |
77 | BER_TYPE_UCS4, |
… | |
… | |
79 | BER_TYPE_REAL, |
83 | BER_TYPE_REAL, |
80 | BER_TYPE_IPADDRESS, |
84 | BER_TYPE_IPADDRESS, |
81 | BER_TYPE_CROAK, |
85 | BER_TYPE_CROAK, |
82 | }; |
86 | }; |
83 | |
87 | |
|
|
88 | // tuple array indices |
84 | enum { |
89 | enum { |
85 | BER_CLASS = 0, |
90 | BER_CLASS = 0, |
86 | BER_TAG = 1, |
91 | BER_TAG = 1, |
87 | BER_CONSTRUCTED = 2, |
92 | BER_FLAGS = 2, |
88 | BER_DATA = 3, |
93 | BER_DATA = 3, |
89 | BER_ARRAYSIZE |
94 | BER_ARRAYSIZE |
90 | }; |
95 | }; |
91 | |
96 | |
92 | #define MAX_OID_STRLEN 4096 |
97 | #define MAX_OID_STRLEN 4096 |
93 | |
98 | |
… | |
… | |
97 | static SV *buf_sv; // encoding buffer |
102 | static SV *buf_sv; // encoding buffer |
98 | static U8 *buf, *cur, *end; // buffer start, current, end |
103 | static U8 *buf, *cur, *end; // buffer start, current, end |
99 | |
104 | |
100 | #if PERL_VERSION < 18 |
105 | #if PERL_VERSION < 18 |
101 | # 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) |
|
|
107 | #endif |
|
|
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) |
102 | #endif |
114 | #endif |
103 | |
115 | |
104 | #if __GNUC__ >= 3 |
116 | #if __GNUC__ >= 3 |
105 | # define expect(expr,value) __builtin_expect ((expr), (value)) |
117 | # define expect(expr,value) __builtin_expect ((expr), (value)) |
106 | # define INLINE static inline |
118 | # define INLINE static inline |
… | |
… | |
140 | { |
152 | { |
141 | if (!SvOK (profile)) |
153 | if (!SvOK (profile)) |
142 | return default_profile; |
154 | return default_profile; |
143 | |
155 | |
144 | if (!SvROK (profile)) |
156 | if (!SvROK (profile)) |
145 | croak ("invalid profile"); |
157 | croak ("Convert::BER::XS::Profile expected"); |
146 | |
158 | |
147 | profile = SvRV (profile); |
159 | profile = SvRV (profile); |
148 | |
160 | |
149 | if (SvSTASH (profile) != profile_stash) |
161 | if (SvSTASH (profile) != profile_stash) |
150 | croak ("invalid profile object"); |
162 | croak ("Convert::BER::XS::Profile expected"); |
151 | |
163 | |
152 | return (void *)profile; |
164 | return (void *)profile; |
153 | } |
165 | } |
154 | |
166 | |
155 | static int |
167 | static int |
… | |
… | |
181 | |
193 | |
182 | SvPVX (sv)[idx] = type; |
194 | SvPVX (sv)[idx] = type; |
183 | } |
195 | } |
184 | |
196 | |
185 | static SV * |
197 | static SV * |
186 | profile_new () |
198 | profile_new (void) |
187 | { |
199 | { |
188 | SV *sv = newSVpvn ("", 0); |
200 | SV *sv = newSVpvn ("", 0); |
189 | |
201 | |
190 | static const struct { |
202 | static const struct { |
191 | int klass; |
203 | int klass; |
192 | int tag; |
204 | int tag; |
193 | int type; |
205 | int type; |
194 | } *celem, default_map[] = { |
206 | } *celem, default_map[] = { |
195 | { ASN_UNIVERSAL, ASN_BOOLEAN , BER_TYPE_BOOL }, |
207 | { ASN_UNIVERSAL, ASN_BOOLEAN , BER_TYPE_BOOL }, |
196 | { ASN_UNIVERSAL, ASN_INTEGER32 , BER_TYPE_INT }, |
208 | { ASN_UNIVERSAL, ASN_INTEGER , BER_TYPE_INT }, |
197 | { ASN_UNIVERSAL, ASN_NULL , BER_TYPE_NULL }, |
209 | { ASN_UNIVERSAL, ASN_NULL , BER_TYPE_NULL }, |
198 | { ASN_UNIVERSAL, ASN_OBJECT_IDENTIFIER, BER_TYPE_OID }, |
210 | { 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 }, |
211 | { ASN_UNIVERSAL, ASN_RELATIVE_OID , BER_TYPE_RELOID }, |
201 | { ASN_UNIVERSAL, ASN_REAL , BER_TYPE_REAL }, |
212 | { ASN_UNIVERSAL, ASN_REAL , BER_TYPE_REAL }, |
|
|
213 | { ASN_UNIVERSAL, ASN_ENUMERATED , BER_TYPE_INT }, |
202 | { ASN_UNIVERSAL, ASN_UTF8_STRING , BER_TYPE_UTF8 }, |
214 | { ASN_UNIVERSAL, ASN_UTF8_STRING , BER_TYPE_UTF8 }, |
203 | { ASN_UNIVERSAL, ASN_BMP_STRING , BER_TYPE_UCS2 }, |
215 | { ASN_UNIVERSAL, ASN_BMP_STRING , BER_TYPE_UCS2 }, |
204 | { ASN_UNIVERSAL, ASN_UNIVERSAL_STRING , BER_TYPE_UCS4 }, |
216 | { ASN_UNIVERSAL, ASN_UNIVERSAL_STRING , BER_TYPE_UCS4 }, |
205 | }; |
217 | }; |
206 | |
218 | |
… | |
… | |
227 | } |
239 | } |
228 | |
240 | |
229 | // get_* functions fetch something from the buffer |
241 | // get_* functions fetch something from the buffer |
230 | // decode_* functions use get_* fun ctions to decode ber values |
242 | // decode_* functions use get_* fun ctions to decode ber values |
231 | |
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 | |
232 | // get n octets |
254 | // get n octets |
233 | static U8 * |
255 | static U8 * |
234 | get_n (UV count) |
256 | get_n (UV count) |
235 | { |
257 | { |
236 | want (count); |
258 | want (count); |
237 | U8 *res = cur; |
259 | U8 *res = cur; |
238 | cur += count; |
260 | cur += count; |
239 | return res; |
261 | return res; |
240 | } |
262 | } |
241 | |
263 | |
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") |
264 | // get ber-encoded integer (i.e. pack "w") |
253 | static U32 |
265 | static UV |
254 | get_w (void) |
266 | get_w (void) |
255 | { |
267 | { |
256 | U32 res = 0; |
268 | UV res = 0; |
|
|
269 | U8 c = get_u8 (); |
|
|
270 | |
|
|
271 | if (expect_false (c == 0x80)) |
|
|
272 | error ("invalid BER padding (X.690 8.1.2.4.2, 8.19.2)"); |
257 | |
273 | |
258 | for (;;) |
274 | for (;;) |
259 | { |
275 | { |
260 | U8 c = get_u8 (); |
276 | if (expect_false (res >> UVSIZE * 8 - 7)) |
|
|
277 | error ("BER variable length integer overflow"); |
|
|
278 | |
261 | res = (res << 7) | (c & 0x7f); |
279 | res = (res << 7) | (c & 0x7f); |
262 | |
280 | |
263 | if (!(c & 0x80)) |
281 | if (expect_true (!(c & 0x80))) |
264 | return res; |
282 | return res; |
265 | } |
|
|
266 | } |
|
|
267 | |
283 | |
|
|
284 | c = get_u8 (); |
|
|
285 | } |
|
|
286 | } |
|
|
287 | |
268 | static U32 |
288 | static UV |
269 | get_length (void) |
289 | get_length (void) |
270 | { |
290 | { |
271 | U32 res = get_u8 (); |
291 | UV res = get_u8 (); |
272 | |
292 | |
273 | if (res & 0x80) |
293 | if (expect_false (res & 0x80)) |
274 | { |
294 | { |
275 | 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 | |
276 | res = 0; |
309 | res = 0; |
277 | |
310 | do |
278 | switch (cnt) |
311 | res = (res << 8) | *cur++; |
279 | { |
312 | 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 | } |
313 | } |
294 | |
314 | |
295 | return res; |
315 | return res; |
296 | } |
316 | } |
297 | |
317 | |
298 | static SV * |
318 | static SV * |
299 | decode_int () |
319 | decode_int (UV len) |
300 | { |
320 | { |
301 | int len = get_length (); |
|
|
302 | |
|
|
303 | if (len <= 0) |
321 | if (!len) |
304 | { |
322 | error ("invalid BER_TYPE_INT length zero (X.690 8.3.1)"); |
305 | error ("integer length equal to zero"); |
|
|
306 | return 0; |
|
|
307 | } |
|
|
308 | |
323 | |
309 | U8 *data = get_n (len); |
324 | U8 *data = get_n (len); |
310 | |
325 | |
|
|
326 | if (expect_false (len > 1)) |
|
|
327 | { |
|
|
328 | U16 mask = (data [0] << 8) | data [1] & 0xff80; |
|
|
329 | |
|
|
330 | if (expect_false (mask == 0xff80 || mask == 0x0000)) |
|
|
331 | error ("invalid padding in BER_TYPE_INT (X.690 8.3.2)"); |
|
|
332 | } |
|
|
333 | |
311 | int negative = data [0] & 0x80; |
334 | int negative = data [0] & 0x80; |
312 | |
335 | |
313 | UV val = negative ? -1 : 0; // copy signbit to all bits |
336 | UV val = negative ? -1 : 0; // copy signbit to all bits |
|
|
337 | |
|
|
338 | if (len > UVSIZE + (!negative && !*data)) |
|
|
339 | error ("BER_TYPE_INT overflow"); |
314 | |
340 | |
315 | do |
341 | do |
316 | val = (val << 8) | *data++; |
342 | val = (val << 8) | *data++; |
317 | while (--len); |
343 | while (--len); |
318 | |
344 | |
… | |
… | |
320 | // but that's ok, as perl relies on it as well. |
346 | // but that's ok, as perl relies on it as well. |
321 | return negative ? newSViv ((IV)val) : newSVuv (val); |
347 | return negative ? newSViv ((IV)val) : newSVuv (val); |
322 | } |
348 | } |
323 | |
349 | |
324 | static SV * |
350 | static SV * |
325 | decode_data (void) |
351 | decode_data (UV len) |
326 | { |
352 | { |
327 | U32 len = get_length (); |
|
|
328 | U8 *data = get_n (len); |
|
|
329 | return newSVpvn ((char *)data, len); |
353 | return newSVpvn ((char *)get_n (len), len); |
330 | } |
354 | } |
331 | |
355 | |
332 | // gelper for decode_object_identifier |
356 | // helper for decode_object_identifier |
333 | static char * |
357 | static char * |
334 | write_uv (char *buf, U32 u) |
358 | write_uv (char *buf, UV u) |
335 | { |
359 | { |
336 | // the one-digit case is absolutely predominant, so this pays off (hopefully) |
360 | // the one-digit case is absolutely predominant, so this pays off (hopefully) |
337 | if (expect_true (u < 10)) |
361 | if (expect_true (u < 10)) |
338 | *buf++ = u + '0'; |
362 | *buf++ = u + '0'; |
339 | else |
363 | else |
340 | { |
364 | { |
|
|
365 | // this *could* be done much faster using branchless fixed-point arithmetics |
341 | char *beg = buf; |
366 | char *beg = buf; |
342 | |
367 | |
343 | do |
368 | do |
344 | { |
369 | { |
345 | *buf++ = u % 10 + '0'; |
370 | *buf++ = u % 10 + '0'; |
… | |
… | |
347 | } |
372 | } |
348 | while (u); |
373 | while (u); |
349 | |
374 | |
350 | // reverse digits |
375 | // reverse digits |
351 | char *ptr = buf; |
376 | char *ptr = buf; |
352 | while (--ptr != beg) |
377 | while (--ptr > beg) |
353 | { |
378 | { |
354 | char c = *ptr; |
379 | char c = *ptr; |
355 | *ptr = *beg; |
380 | *ptr = *beg; |
356 | *beg = c; |
381 | *beg = c; |
357 | ++beg; |
382 | ++beg; |
… | |
… | |
360 | |
385 | |
361 | return buf; |
386 | return buf; |
362 | } |
387 | } |
363 | |
388 | |
364 | static SV * |
389 | static SV * |
365 | decode_oid (int relative) |
390 | decode_oid (UV len, int relative) |
366 | { |
391 | { |
367 | U32 len = get_length (); |
|
|
368 | |
|
|
369 | if (len <= 0) |
392 | if (len <= 0) |
370 | { |
393 | { |
371 | error ("OBJECT IDENTIFIER length equal to zero"); |
394 | error ("BER_TYPE_OID length must not be zero"); |
372 | return &PL_sv_undef; |
395 | return &PL_sv_undef; |
373 | } |
396 | } |
374 | |
397 | |
375 | U8 *end = cur + len; |
398 | U8 *end = cur + len; |
376 | U32 w = get_w (); |
399 | UV w = get_w (); |
377 | |
400 | |
378 | static char oid[MAX_OID_STRLEN]; // must be static |
401 | static char oid[MAX_OID_STRLEN]; // static, because too large for stack |
379 | char *app = oid; |
402 | char *app = oid; |
380 | |
403 | |
381 | if (relative) |
404 | if (relative) |
382 | app = write_uv (app, w); |
405 | app = write_uv (app, w); |
383 | else |
406 | else |
384 | { |
407 | { |
|
|
408 | UV w1, w2; |
|
|
409 | |
|
|
410 | if (w < 2 * 40) |
|
|
411 | (w1 = w / 40), (w2 = w % 40); |
|
|
412 | else |
|
|
413 | (w1 = 2), (w2 = w - 2 * 40); |
|
|
414 | |
385 | app = write_uv (app, (U8)w / 40); |
415 | app = write_uv (app, w1); |
386 | *app++ = '.'; |
416 | *app++ = '.'; |
387 | app = write_uv (app, (U8)w % 40); |
417 | app = write_uv (app, w2); |
|
|
418 | } |
|
|
419 | |
|
|
420 | while (cur < end) |
388 | } |
421 | { |
389 | |
|
|
390 | // we assume an oid component is never > 64 bytes |
422 | // we assume an oid component is never > 64 digits |
391 | while (cur < end && oid + sizeof (oid) - app > 64) |
423 | if (oid + sizeof (oid) - app < 64) |
392 | { |
424 | croak ("BER_TYPE_OID to long to decode"); |
|
|
425 | |
393 | w = get_w (); |
426 | w = get_w (); |
394 | *app++ = '.'; |
427 | *app++ = '.'; |
395 | app = write_uv (app, w); |
428 | app = write_uv (app, w); |
396 | } |
429 | } |
397 | |
430 | |
398 | return newSVpvn (oid, app - oid); |
431 | return newSVpvn (oid, app - oid); |
399 | } |
432 | } |
400 | |
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 | |
401 | // TODO: this is unacceptably slow |
514 | // TODO: this is unacceptably slow |
402 | static SV * |
515 | static SV * |
403 | decode_ucs (int chrsize) |
516 | decode_ucs (UV len, int chrsize) |
404 | { |
517 | { |
405 | SV *res = NEWSV (0, 0); |
|
|
406 | |
|
|
407 | U32 len = get_length (); |
|
|
408 | |
|
|
409 | if (len & (chrsize - 1)) |
518 | if (len & (chrsize - 1)) |
410 | 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); |
411 | |
522 | |
412 | while (len) |
523 | while (len) |
413 | { |
524 | { |
414 | U8 b1 = get_u8 (); |
525 | U8 b1 = get_u8 (); |
415 | U8 b2 = get_u8 (); |
526 | U8 b2 = get_u8 (); |
… | |
… | |
433 | |
544 | |
434 | return res; |
545 | return res; |
435 | } |
546 | } |
436 | |
547 | |
437 | static SV * |
548 | static SV * |
438 | decode_ber () |
549 | decode_ber (void) |
439 | { |
550 | { |
440 | int identifier = get_u8 (); |
551 | int identifier = get_u8 (); |
441 | |
552 | |
442 | SV *res; |
553 | SV *res; |
443 | |
554 | |
… | |
… | |
446 | int tag = identifier & ASN_TAG_MASK; |
557 | int tag = identifier & ASN_TAG_MASK; |
447 | |
558 | |
448 | if (tag == ASN_TAG_BER) |
559 | if (tag == ASN_TAG_BER) |
449 | tag = get_w (); |
560 | tag = get_w (); |
450 | |
561 | |
451 | if (tag == ASN_TAG_BER) |
|
|
452 | tag = get_w (); |
|
|
453 | |
|
|
454 | if (constructed) |
562 | if (constructed) |
455 | { |
563 | { |
456 | U32 len = get_length (); |
564 | want (1); |
457 | U32 seqend = (cur - buf) + len; |
|
|
458 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
565 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
459 | |
566 | |
460 | while (cur < buf + seqend) |
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 | |
461 | av_push (av, decode_ber ()); |
581 | av_push (av, decode_ber ()); |
|
|
582 | } |
|
|
583 | } |
|
|
584 | else |
|
|
585 | { |
|
|
586 | UV len = get_length (); |
|
|
587 | UV seqend = (cur - buf) + len; |
462 | |
588 | |
463 | if (cur > buf + seqend) |
589 | while (cur < buf + seqend) |
|
|
590 | av_push (av, decode_ber ()); |
|
|
591 | |
|
|
592 | if (expect_false (cur > buf + seqend)) |
464 | croak ("constructed type %02x overflow (%x %x)\n", identifier, cur - buf, seqend); |
593 | croak ("CONSTRUCTED type %02x length overflow (0x%x 0x%x)\n", identifier, (int)(cur - buf), (int)seqend); |
|
|
594 | } |
465 | |
595 | |
466 | res = newRV_inc ((SV *)av); |
596 | res = newRV_inc ((SV *)av); |
467 | } |
597 | } |
468 | else |
598 | else |
|
|
599 | { |
|
|
600 | UV len = get_length (); |
|
|
601 | |
469 | switch (profile_lookup (cur_profile, klass, tag)) |
602 | switch (profile_lookup (cur_profile, klass, tag)) |
470 | { |
603 | { |
471 | case BER_TYPE_NULL: |
604 | case BER_TYPE_NULL: |
|
|
605 | if (expect_false (len)) |
|
|
606 | croak ("BER_TYPE_NULL value with non-zero length %d encountered (X.690 8.8.2)", len); |
|
|
607 | |
472 | res = &PL_sv_undef; |
608 | res = &PL_sv_undef; |
473 | break; |
609 | break; |
474 | |
610 | |
475 | case BER_TYPE_BOOL: |
611 | case BER_TYPE_BOOL: |
476 | { |
|
|
477 | U32 len = get_length (); |
|
|
478 | |
|
|
479 | if (len != 1) |
612 | if (expect_false (len != 1)) |
480 | croak ("BER_TYPE_BOOLEAN type with invalid length %d encountered", len); |
613 | croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered (X.690 8.2.1)", len); |
481 | |
614 | |
482 | res = newSVcacheint (get_u8 () ? 0 : 1); |
615 | res = newSVcacheint (!!get_u8 ()); |
483 | } |
|
|
484 | break; |
616 | break; |
485 | |
617 | |
486 | case BER_TYPE_OID: |
618 | case BER_TYPE_OID: |
487 | res = decode_oid (0); |
619 | res = decode_oid (len, 0); |
488 | break; |
620 | break; |
489 | |
621 | |
490 | case BER_TYPE_RELOID: |
622 | case BER_TYPE_RELOID: |
491 | res = decode_oid (1); |
623 | res = decode_oid (len, 1); |
492 | break; |
624 | break; |
493 | |
625 | |
494 | case BER_TYPE_INT: |
626 | case BER_TYPE_INT: |
495 | res = decode_int (); |
627 | res = decode_int (len); |
496 | break; |
628 | break; |
497 | |
629 | |
498 | case BER_TYPE_UTF8: |
630 | case BER_TYPE_UTF8: |
499 | res = decode_data (); |
631 | res = decode_data (len); |
500 | SvUTF8_on (res); |
632 | SvUTF8_on (res); |
501 | break; |
633 | break; |
502 | |
634 | |
503 | case BER_TYPE_BYTES: |
635 | case BER_TYPE_BYTES: |
504 | res = decode_data (); |
636 | res = decode_data (len); |
505 | break; |
637 | break; |
506 | |
638 | |
507 | case BER_TYPE_IPADDRESS: |
639 | case BER_TYPE_IPADDRESS: |
508 | { |
640 | { |
509 | U32 len = get_length (); |
|
|
510 | |
|
|
511 | if (len != 4) |
641 | if (len != 4) |
512 | croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered", len); |
642 | croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered (RFC 2578 7.1.5)", len); |
513 | |
643 | |
514 | U8 c1 = get_u8 (); |
644 | U8 *data = get_n (4); |
515 | U8 c2 = get_u8 (); |
645 | 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 | } |
646 | } |
521 | break; |
647 | break; |
522 | |
648 | |
523 | case BER_TYPE_UCS2: |
649 | case BER_TYPE_UCS2: |
524 | res = decode_ucs (2); |
650 | res = decode_ucs (len, 2); |
525 | break; |
651 | break; |
526 | |
652 | |
527 | case BER_TYPE_UCS4: |
653 | case BER_TYPE_UCS4: |
528 | res = decode_ucs (4); |
654 | res = decode_ucs (len, 4); |
529 | break; |
655 | break; |
530 | |
656 | |
531 | case BER_TYPE_REAL: |
657 | case BER_TYPE_REAL: |
|
|
658 | res = decode_real (len); |
|
|
659 | break; |
|
|
660 | |
532 | case BER_TYPE_CROAK: |
661 | case BER_TYPE_CROAK: |
|
|
662 | croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag); |
|
|
663 | |
533 | default: |
664 | default: |
534 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
665 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
535 | } |
666 | } |
|
|
667 | } |
536 | |
668 | |
537 | AV *av = newAV (); |
669 | AV *av = newAV (); |
538 | av_fill (av, BER_ARRAYSIZE - 1); |
670 | av_fill (av, BER_ARRAYSIZE - 1); |
539 | AvARRAY (av)[BER_CLASS ] = newSVcacheint (klass); |
671 | AvARRAY (av)[BER_CLASS] = newSVcacheint (klass); |
540 | AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); |
672 | AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); |
541 | AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (constructed ? 1 : 0); |
673 | AvARRAY (av)[BER_FLAGS] = newSVcacheint (constructed ? 1 : 0); |
542 | AvARRAY (av)[BER_DATA ] = res; |
674 | AvARRAY (av)[BER_DATA ] = res; |
543 | |
675 | |
544 | return newRV_noinc ((SV *)av); |
676 | return newRV_noinc ((SV *)av); |
545 | } |
677 | } |
546 | |
678 | |
547 | ///////////////////////////////////////////////////////////////////////////// |
679 | ///////////////////////////////////////////////////////////////////////////// |
… | |
… | |
552 | strlen_sum (STRLEN l1, STRLEN l2) |
684 | strlen_sum (STRLEN l1, STRLEN l2) |
553 | { |
685 | { |
554 | size_t sum = l1 + l2; |
686 | size_t sum = l1 + l2; |
555 | |
687 | |
556 | if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) |
688 | if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) |
557 | croak ("JSON::XS: string size overflow"); |
689 | croak ("Convert::BER::XS: string size overflow"); |
558 | |
690 | |
559 | return sum; |
691 | return sum; |
560 | } |
692 | } |
561 | |
693 | |
562 | static void |
694 | static void |
… | |
… | |
600 | need (1); |
732 | need (1); |
601 | *cur++ = val; |
733 | *cur++ = val; |
602 | } |
734 | } |
603 | |
735 | |
604 | static void |
736 | static void |
605 | put_w_nocheck (U32 val) |
737 | put_w_nocheck (UV val) |
606 | { |
738 | { |
|
|
739 | #if UVSIZE > 4 |
|
|
740 | *cur = (val >> 7 * 9) | 0x80; cur += val >= ((UV)1 << (7 * 9)); |
|
|
741 | *cur = (val >> 7 * 8) | 0x80; cur += val >= ((UV)1 << (7 * 8)); |
|
|
742 | *cur = (val >> 7 * 7) | 0x80; cur += val >= ((UV)1 << (7 * 7)); |
|
|
743 | *cur = (val >> 7 * 6) | 0x80; cur += val >= ((UV)1 << (7 * 6)); |
|
|
744 | *cur = (val >> 7 * 5) | 0x80; cur += val >= ((UV)1 << (7 * 5)); |
|
|
745 | #endif |
607 | *cur = (val >> 7 * 4) | 0x80; cur += val >= (1 << (7 * 4)); |
746 | *cur = (val >> 7 * 4) | 0x80; cur += val >= ((UV)1 << (7 * 4)); |
608 | *cur = (val >> 7 * 3) | 0x80; cur += val >= (1 << (7 * 3)); |
747 | *cur = (val >> 7 * 3) | 0x80; cur += val >= ((UV)1 << (7 * 3)); |
609 | *cur = (val >> 7 * 2) | 0x80; cur += val >= (1 << (7 * 2)); |
748 | *cur = (val >> 7 * 2) | 0x80; cur += val >= ((UV)1 << (7 * 2)); |
610 | *cur = (val >> 7 * 1) | 0x80; cur += val >= (1 << (7 * 1)); |
749 | *cur = (val >> 7 * 1) | 0x80; cur += val >= ((UV)1 << (7 * 1)); |
611 | *cur = val & 0x7f; cur += 1; |
750 | *cur = val & 0x7f; cur += 1; |
612 | } |
751 | } |
613 | |
752 | |
614 | static void |
753 | static void |
615 | put_w (U32 val) |
754 | put_w (UV val) |
616 | { |
755 | { |
617 | need (5); // we only handle up to 5 bytes |
756 | need (5); // we only handle up to 5 bytes |
618 | |
757 | |
619 | put_w_nocheck (val); |
758 | put_w_nocheck (val); |
620 | } |
759 | } |
621 | |
760 | |
622 | static U8 * |
761 | static U8 * |
623 | put_length_at (U32 val, U8 *cur) |
762 | put_length_at (UV val, U8 *cur) |
624 | { |
763 | { |
625 | if (val < 0x7fU) |
764 | if (val <= 0x7fU) |
626 | *cur++ = val; |
765 | *cur++ = val; |
627 | else |
766 | else |
628 | { |
767 | { |
629 | U8 *lenb = cur++; |
768 | U8 *lenb = cur++; |
630 | |
769 | |
631 | *cur = val >> 24; cur += *cur > 0; |
770 | #if UVSIZE > 4 |
632 | *cur = val >> 16; cur += *cur > 0; |
771 | *cur = val >> 56; cur += val >= ((UV)1 << (8 * 7)); |
633 | *cur = val >> 8; cur += *cur > 0; |
772 | *cur = val >> 48; cur += val >= ((UV)1 << (8 * 6)); |
|
|
773 | *cur = val >> 40; cur += val >= ((UV)1 << (8 * 5)); |
|
|
774 | *cur = val >> 32; cur += val >= ((UV)1 << (8 * 4)); |
|
|
775 | #endif |
|
|
776 | *cur = val >> 24; cur += val >= ((UV)1 << (8 * 3)); |
|
|
777 | *cur = val >> 16; cur += val >= ((UV)1 << (8 * 2)); |
|
|
778 | *cur = val >> 8; cur += val >= ((UV)1 << (8 * 1)); |
634 | *cur = val ; cur += 1; |
779 | *cur = val ; cur += 1; |
635 | |
780 | |
636 | *lenb = 0x80 + cur - lenb - 1; |
781 | *lenb = 0x80 + cur - lenb - 1; |
637 | } |
782 | } |
638 | |
783 | |
639 | return cur; |
784 | return cur; |
640 | } |
785 | } |
641 | |
786 | |
642 | static void |
787 | static void |
643 | put_length (U32 val) |
788 | put_length (UV val) |
644 | { |
789 | { |
645 | need (5 + val); |
790 | need (9 + val); |
646 | cur = put_length_at (val, cur); |
791 | cur = put_length_at (val, cur); |
647 | } |
792 | } |
648 | |
793 | |
649 | // return how many bytes the encoded length requires |
794 | // return how many bytes the encoded length requires |
650 | static int length_length (U32 val) |
795 | static int length_length (UV val) |
651 | { |
796 | { |
652 | return val < 0x7fU |
797 | // use hashing with a DeBruin sequence, anyone? |
|
|
798 | return expect_true (val <= 0x7fU) |
653 | ? 1 |
799 | ? 1 |
654 | : 2 + (val > 0xffU) + (val > 0xffffU) + (val > 0xffffffU); |
800 | : 2 |
|
|
801 | + (val > 0x000000000000ffU) |
|
|
802 | + (val > 0x0000000000ffffU) |
|
|
803 | + (val > 0x00000000ffffffU) |
|
|
804 | #if UVSIZE > 4 |
|
|
805 | + (val > 0x000000ffffffffU) |
|
|
806 | + (val > 0x0000ffffffffffU) |
|
|
807 | + (val > 0x00ffffffffffffU) |
|
|
808 | + (val > 0xffffffffffffffU) |
|
|
809 | #endif |
|
|
810 | ; |
655 | } |
811 | } |
656 | |
812 | |
657 | static void |
813 | static void |
658 | encode_data (const char *ptr, STRLEN len) |
814 | encode_data (const char *ptr, STRLEN len) |
659 | { |
815 | { |
… | |
… | |
721 | |
877 | |
722 | *lenb = cur - lenb - 1; |
878 | *lenb = cur - lenb - 1; |
723 | } |
879 | } |
724 | |
880 | |
725 | // we don't know the length yet, so we optimistically |
881 | // we don't know the length yet, so we optimistically |
726 | // assume the length will need one octet later. if that |
882 | // assume the length will need one octet later. If that |
727 | // turns out to be wrong, we memove as needed. |
883 | // turns out to be wrong, we memmove as needed. |
728 | // mark the beginning |
884 | // mark the beginning |
729 | static STRLEN |
885 | static STRLEN |
730 | len_fixup_mark () |
886 | len_fixup_mark (void) |
731 | { |
887 | { |
732 | return cur++ - buf; |
888 | return cur++ - buf; |
733 | } |
889 | } |
734 | |
890 | |
735 | // patch up the length |
891 | // patch up the length |
… | |
… | |
792 | } |
948 | } |
793 | |
949 | |
794 | len_fixup (mark); |
950 | len_fixup (mark); |
795 | } |
951 | } |
796 | |
952 | |
797 | // check whether an SV is a BER tuple and returns its AV * |
953 | static void |
798 | static AV * |
954 | encode_real (SV *data) |
799 | ber_tuple (SV *tuple) |
|
|
800 | { |
955 | { |
801 | SV *rv; |
956 | NV nv = SvNV (data); |
802 | |
957 | |
803 | if (expect_false (!SvROK (tuple) || SvTYPE ((rv = SvRV (tuple))) != SVt_PVAV)) |
958 | if (expect_false (nv == (NV)0.)) |
804 | 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; |
805 | |
1001 | |
806 | if (expect_false (SvRMAGICAL (rv))) |
1002 | SV *sv = POPs; |
807 | croak ("BER tuple must not be tied"); |
1003 | STRLEN l; |
|
|
1004 | char *f = SvPV (sv, l); |
808 | |
1005 | |
809 | if (expect_false (AvFILL ((AV *)rv) != BER_ARRAYSIZE - 1)) |
1006 | put_length (l); |
810 | croak ("BER tuple must contain exactly %d elements, not %d", BER_ARRAYSIZE, AvFILL ((AV *)rv) + 1); |
1007 | memcpy (cur, f, l); |
|
|
1008 | cur += l; |
811 | |
1009 | |
812 | return (AV *)rv; |
1010 | PUTBACK; |
|
|
1011 | FREETMPS; |
|
|
1012 | } |
813 | } |
1013 | } |
814 | |
1014 | |
815 | static void |
1015 | static void |
816 | encode_ucs (SV *data, int chrsize) |
1016 | encode_ucs (SV *data, int chrsize) |
817 | { |
1017 | { |
… | |
… | |
837 | |
1037 | |
838 | *cur++ = uchr >> 8; |
1038 | *cur++ = uchr >> 8; |
839 | *cur++ = uchr; |
1039 | *cur++ = uchr; |
840 | } |
1040 | } |
841 | } |
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 | |
842 | static void |
1061 | static void |
843 | encode_ber (SV *tuple) |
1062 | encode_ber (SV *tuple) |
844 | { |
1063 | { |
845 | AV *av = ber_tuple (tuple); |
1064 | AV *av = ber_tuple (tuple); |
846 | |
1065 | |
847 | int klass = SvIV (AvARRAY (av)[BER_CLASS]); |
1066 | int klass = SvIV (AvARRAY (av)[BER_CLASS]); |
848 | int tag = SvIV (AvARRAY (av)[BER_TAG]); |
1067 | int tag = SvIV (AvARRAY (av)[BER_TAG]); |
849 | int constructed = SvIV (AvARRAY (av)[BER_CONSTRUCTED]) ? ASN_CONSTRUCTED : 0; |
1068 | int constructed = SvIV (AvARRAY (av)[BER_FLAGS]) & 1 ? ASN_CONSTRUCTED : 0; |
850 | SV *data = AvARRAY (av)[BER_DATA]; |
1069 | SV *data = AvARRAY (av)[BER_DATA]; |
851 | |
1070 | |
852 | int identifier = (klass << ASN_CLASS_SHIFT) | constructed; |
1071 | int identifier = (klass << ASN_CLASS_SHIFT) | constructed; |
853 | |
1072 | |
854 | if (expect_false (tag >= ASN_TAG_BER)) |
1073 | if (expect_false (tag >= ASN_TAG_BER)) |
… | |
… | |
865 | // and adjust later |
1084 | // and adjust later |
866 | need (1); |
1085 | need (1); |
867 | STRLEN mark = len_fixup_mark (); |
1086 | STRLEN mark = len_fixup_mark (); |
868 | |
1087 | |
869 | if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV)) |
1088 | if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV)) |
870 | croak ("BER constructed data must be array-reference"); |
1089 | croak ("BER CONSTRUCTED data must be array-reference"); |
871 | |
1090 | |
872 | AV *av = (AV *)SvRV (data); |
1091 | AV *av = (AV *)SvRV (data); |
873 | int fill = AvFILL (av); |
1092 | int fill = AvFILL (av); |
874 | |
1093 | |
875 | if (expect_false (SvRMAGICAL (av))) |
1094 | if (expect_false (SvRMAGICAL (av))) |
876 | croak ("BER constructed data must not be tied"); |
1095 | croak ("BER CONSTRUCTED data must not be tied"); |
877 | |
1096 | |
878 | int i; |
1097 | int i; |
879 | for (i = 0; i <= fill; ++i) |
1098 | for (i = 0; i <= fill; ++i) |
880 | encode_ber (AvARRAY (av)[i]); |
1099 | encode_ber (AvARRAY (av)[i]); |
881 | |
1100 | |
… | |
… | |
888 | put_length (0); |
1107 | put_length (0); |
889 | break; |
1108 | break; |
890 | |
1109 | |
891 | case BER_TYPE_BOOL: |
1110 | case BER_TYPE_BOOL: |
892 | put_length (1); |
1111 | put_length (1); |
893 | *cur++ = SvTRUE (data) ? 0xff : 0x00; |
1112 | *cur++ = SvTRUE (data) ? 0xff : 0x00; // 0xff = DER/CER |
894 | break; |
1113 | break; |
895 | |
1114 | |
896 | case BER_TYPE_OID: |
1115 | case BER_TYPE_OID: |
897 | encode_oid (data, 0); |
1116 | encode_oid (data, 0); |
898 | break; |
1117 | break; |
… | |
… | |
936 | case BER_TYPE_UCS4: |
1155 | case BER_TYPE_UCS4: |
937 | encode_ucs (data, 4); |
1156 | encode_ucs (data, 4); |
938 | break; |
1157 | break; |
939 | |
1158 | |
940 | case BER_TYPE_REAL: |
1159 | case BER_TYPE_REAL: |
|
|
1160 | encode_real (data); |
|
|
1161 | break; |
|
|
1162 | |
941 | case BER_TYPE_CROAK: |
1163 | case BER_TYPE_CROAK: |
|
|
1164 | croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag); |
|
|
1165 | |
942 | default: |
1166 | default: |
943 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
1167 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
944 | } |
1168 | } |
945 | |
1169 | |
946 | } |
1170 | } |
… | |
… | |
961 | const char *name; |
1185 | const char *name; |
962 | IV iv; |
1186 | IV iv; |
963 | } *civ, const_iv[] = { |
1187 | } *civ, const_iv[] = { |
964 | #define const_iv(name) { # name, name }, |
1188 | #define const_iv(name) { # name, name }, |
965 | const_iv (ASN_BOOLEAN) |
1189 | const_iv (ASN_BOOLEAN) |
966 | const_iv (ASN_INTEGER32) |
1190 | const_iv (ASN_INTEGER) |
967 | const_iv (ASN_BIT_STRING) |
1191 | const_iv (ASN_BIT_STRING) |
968 | const_iv (ASN_OCTET_STRING) |
1192 | const_iv (ASN_OCTET_STRING) |
969 | const_iv (ASN_NULL) |
1193 | const_iv (ASN_NULL) |
970 | const_iv (ASN_OBJECT_IDENTIFIER) |
1194 | const_iv (ASN_OBJECT_IDENTIFIER) |
971 | const_iv (ASN_OBJECT_DESCRIPTOR) |
1195 | const_iv (ASN_OBJECT_DESCRIPTOR) |
… | |
… | |
1000 | const_iv (ASN_CONTEXT) |
1224 | const_iv (ASN_CONTEXT) |
1001 | const_iv (ASN_PRIVATE) |
1225 | const_iv (ASN_PRIVATE) |
1002 | |
1226 | |
1003 | const_iv (BER_CLASS) |
1227 | const_iv (BER_CLASS) |
1004 | const_iv (BER_TAG) |
1228 | const_iv (BER_TAG) |
1005 | const_iv (BER_CONSTRUCTED) |
1229 | const_iv (BER_FLAGS) |
1006 | const_iv (BER_DATA) |
1230 | const_iv (BER_DATA) |
1007 | |
1231 | |
1008 | const_iv (BER_TYPE_BYTES) |
1232 | const_iv (BER_TYPE_BYTES) |
1009 | const_iv (BER_TYPE_UTF8) |
1233 | const_iv (BER_TYPE_UTF8) |
1010 | const_iv (BER_TYPE_UCS2) |
1234 | const_iv (BER_TYPE_UCS2) |
… | |
… | |
1018 | const_iv (BER_TYPE_IPADDRESS) |
1242 | const_iv (BER_TYPE_IPADDRESS) |
1019 | const_iv (BER_TYPE_CROAK) |
1243 | const_iv (BER_TYPE_CROAK) |
1020 | |
1244 | |
1021 | const_iv (SNMP_IPADDRESS) |
1245 | const_iv (SNMP_IPADDRESS) |
1022 | const_iv (SNMP_COUNTER32) |
1246 | const_iv (SNMP_COUNTER32) |
|
|
1247 | const_iv (SNMP_GAUGE32) |
1023 | const_iv (SNMP_UNSIGNED32) |
1248 | const_iv (SNMP_UNSIGNED32) |
1024 | const_iv (SNMP_TIMETICKS) |
1249 | const_iv (SNMP_TIMETICKS) |
1025 | const_iv (SNMP_OPAQUE) |
1250 | const_iv (SNMP_OPAQUE) |
1026 | const_iv (SNMP_COUNTER64) |
1251 | const_iv (SNMP_COUNTER64) |
1027 | }; |
1252 | }; |
1028 | |
1253 | |
1029 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) |
1254 | 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)); |
1255 | newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); |
1031 | } |
1256 | } |
1032 | |
1257 | |
1033 | SV * |
1258 | void |
1034 | ber_decode (SV *ber, SV *profile = &PL_sv_undef) |
1259 | ber_decode (SV *ber, SV *profile = &PL_sv_undef) |
|
|
1260 | ALIAS: |
|
|
1261 | ber_decode_prefix = 1 |
1035 | CODE: |
1262 | PPCODE: |
1036 | { |
1263 | { |
1037 | cur_profile = SvPROFILE (profile); |
1264 | cur_profile = SvPROFILE (profile); |
1038 | STRLEN len; |
1265 | STRLEN len; |
1039 | buf = (U8 *)SvPVbyte (ber, len); |
1266 | buf = (U8 *)SvPVbyte (ber, len); |
1040 | cur = buf; |
1267 | cur = buf; |
1041 | end = buf + len; |
1268 | end = buf + len; |
1042 | |
1269 | |
|
|
1270 | PUTBACK; |
1043 | RETVAL = decode_ber (); |
1271 | SV *tuple = decode_ber (); |
|
|
1272 | SPAGAIN; |
|
|
1273 | |
|
|
1274 | EXTEND (SP, 2); |
|
|
1275 | PUSHs (sv_2mortal (tuple)); |
|
|
1276 | |
|
|
1277 | if (ix) |
|
|
1278 | PUSHs (sv_2mortal (newSViv (cur - buf))); |
|
|
1279 | else if (cur != end) |
|
|
1280 | error ("trailing garbage after BER value"); |
1044 | } |
1281 | } |
1045 | OUTPUT: RETVAL |
|
|
1046 | |
1282 | |
1047 | void |
1283 | 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) |
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) |
1049 | PPCODE: |
1285 | PPCODE: |
1050 | { |
1286 | { |
1051 | if (!SvOK (tuple)) |
1287 | if (!SvOK (tuple)) |
1052 | XSRETURN_NO; |
1288 | XSRETURN_NO; |
1053 | |
1289 | |
… | |
… | |
1055 | croak ("ber_is: tuple must be BER tuple (array-ref)"); |
1291 | croak ("ber_is: tuple must be BER tuple (array-ref)"); |
1056 | |
1292 | |
1057 | AV *av = (AV *)SvRV (tuple); |
1293 | AV *av = (AV *)SvRV (tuple); |
1058 | |
1294 | |
1059 | XPUSHs ( |
1295 | XPUSHs ( |
1060 | (!SvOK (klass) || SvIV (AvARRAY (av)[BER_CLASS ]) == SvIV (klass)) |
1296 | (!SvOK (klass) || SvIV (AvARRAY (av)[BER_CLASS]) == SvIV (klass)) |
1061 | && (!SvOK (tag) || SvIV (AvARRAY (av)[BER_TAG ]) == SvIV (tag)) |
1297 | && (!SvOK (tag) || SvIV (AvARRAY (av)[BER_TAG ]) == SvIV (tag)) |
1062 | && (!SvOK (constructed) || !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) == !SvIV (constructed)) |
1298 | && (!SvOK (flags) || !SvIV (AvARRAY (av)[BER_FLAGS]) == !SvIV (flags)) |
1063 | && (!SvOK (data) || sv_eq (AvARRAY (av)[BER_DATA ], data)) |
1299 | && (!SvOK (data) || sv_eq (AvARRAY (av)[BER_DATA ], data)) |
1064 | ? &PL_sv_yes : &PL_sv_undef); |
1300 | ? &PL_sv_yes : &PL_sv_undef); |
1065 | } |
1301 | } |
1066 | |
1302 | |
1067 | void |
1303 | void |
1068 | ber_is_seq (SV *tuple) |
1304 | ber_is_seq (SV *tuple) |
… | |
… | |
1072 | XSRETURN_UNDEF; |
1308 | XSRETURN_UNDEF; |
1073 | |
1309 | |
1074 | AV *av = ber_tuple (tuple); |
1310 | AV *av = ber_tuple (tuple); |
1075 | |
1311 | |
1076 | XPUSHs ( |
1312 | XPUSHs ( |
1077 | SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL |
1313 | SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL |
1078 | && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_SEQUENCE |
1314 | && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_SEQUENCE |
1079 | && SvIV (AvARRAY (av)[BER_CONSTRUCTED]) |
1315 | && SvIV (AvARRAY (av)[BER_FLAGS]) |
1080 | ? AvARRAY (av)[BER_DATA] : &PL_sv_undef); |
1316 | ? AvARRAY (av)[BER_DATA] : &PL_sv_undef); |
1081 | } |
1317 | } |
1082 | |
1318 | |
1083 | void |
1319 | void |
1084 | ber_is_i32 (SV *tuple, SV *value = &PL_sv_undef) |
1320 | ber_is_int (SV *tuple, SV *value = &PL_sv_undef) |
1085 | PPCODE: |
1321 | PPCODE: |
1086 | { |
1322 | { |
1087 | if (!SvOK (tuple)) |
1323 | if (!SvOK (tuple)) |
1088 | XSRETURN_NO; |
1324 | XSRETURN_NO; |
1089 | |
1325 | |
1090 | AV *av = ber_tuple (tuple); |
1326 | AV *av = ber_tuple (tuple); |
1091 | |
1327 | |
1092 | IV data = SvIV (AvARRAY (av)[BER_DATA]); |
1328 | UV data = SvUV (AvARRAY (av)[BER_DATA]); |
1093 | |
1329 | |
1094 | XPUSHs ( |
1330 | XPUSHs ( |
1095 | SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL |
1331 | SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL |
1096 | && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_INTEGER32 |
1332 | && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_INTEGER |
1097 | && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) |
1333 | && !SvIV (AvARRAY (av)[BER_FLAGS]) |
1098 | && (!SvOK (value) || data == SvIV (value)) |
1334 | && (!SvOK (value) || data == SvUV (value)) |
1099 | ? sv_2mortal (data ? newSViv (data) : newSVpv ("0 but true", 0)) |
1335 | ? sv_2mortal (data ? newSVsv (AvARRAY (av)[BER_DATA]) : newSVpv ("0 but true", 0)) |
1100 | : &PL_sv_undef); |
1336 | : &PL_sv_undef); |
1101 | } |
1337 | } |
1102 | |
1338 | |
1103 | void |
1339 | void |
1104 | ber_is_oid (SV *tuple, SV *oid = &PL_sv_undef) |
1340 | ber_is_oid (SV *tuple, SV *oid = &PL_sv_undef) |
… | |
… | |
1108 | XSRETURN_NO; |
1344 | XSRETURN_NO; |
1109 | |
1345 | |
1110 | AV *av = ber_tuple (tuple); |
1346 | AV *av = ber_tuple (tuple); |
1111 | |
1347 | |
1112 | XPUSHs ( |
1348 | XPUSHs ( |
1113 | SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL |
1349 | SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL |
1114 | && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_OBJECT_IDENTIFIER |
1350 | && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_OBJECT_IDENTIFIER |
1115 | && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) |
1351 | && !SvIV (AvARRAY (av)[BER_FLAGS]) |
1116 | && (!SvOK (oid) || sv_eq (AvARRAY (av)[BER_DATA], oid)) |
1352 | && (!SvOK (oid) || sv_eq (AvARRAY (av)[BER_DATA], oid)) |
1117 | ? newSVsv (AvARRAY (av)[BER_DATA]) : &PL_sv_undef); |
1353 | ? newSVsv (AvARRAY (av)[BER_DATA]) : &PL_sv_undef); |
1118 | } |
1354 | } |
1119 | |
1355 | |
1120 | ############################################################################# |
1356 | ############################################################################# |
… | |
… | |
1126 | cur_profile = SvPROFILE (profile); |
1362 | cur_profile = SvPROFILE (profile); |
1127 | buf_sv = sv_2mortal (NEWSV (0, 256)); |
1363 | buf_sv = sv_2mortal (NEWSV (0, 256)); |
1128 | SvPOK_only (buf_sv); |
1364 | SvPOK_only (buf_sv); |
1129 | set_buf (buf_sv); |
1365 | set_buf (buf_sv); |
1130 | |
1366 | |
|
|
1367 | PUTBACK; |
1131 | encode_ber (tuple); |
1368 | encode_ber (tuple); |
|
|
1369 | SPAGAIN; |
1132 | |
1370 | |
1133 | SvCUR_set (buf_sv, cur - buf); |
1371 | SvCUR_set (buf_sv, cur - buf); |
1134 | XPUSHs (buf_sv); |
1372 | XPUSHs (buf_sv); |
1135 | } |
1373 | } |
1136 | |
1374 | |
1137 | SV * |
1375 | SV * |
1138 | ber_i32 (IV iv) |
1376 | ber_int (SV *sv) |
1139 | CODE: |
1377 | CODE: |
1140 | { |
1378 | { |
1141 | AV *av = newAV (); |
1379 | AV *av = newAV (); |
1142 | av_fill (av, BER_ARRAYSIZE - 1); |
1380 | av_fill (av, BER_ARRAYSIZE - 1); |
1143 | AvARRAY (av)[BER_CLASS ] = newSVcacheint (ASN_UNIVERSAL); |
1381 | AvARRAY (av)[BER_CLASS] = newSVcacheint (ASN_UNIVERSAL); |
1144 | AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_INTEGER32); |
1382 | AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_INTEGER); |
1145 | AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (0); |
1383 | AvARRAY (av)[BER_FLAGS] = newSVcacheint (0); |
1146 | AvARRAY (av)[BER_DATA ] = newSViv (iv); |
1384 | AvARRAY (av)[BER_DATA ] = newSVsv (sv); |
1147 | RETVAL = newRV_noinc ((SV *)av); |
1385 | RETVAL = newRV_noinc ((SV *)av); |
1148 | } |
1386 | } |
1149 | OUTPUT: RETVAL |
1387 | OUTPUT: RETVAL |
1150 | |
1388 | |
1151 | # TODO: not arrayref, but elements? |
1389 | # TODO: not arrayref, but elements? |
… | |
… | |
1153 | ber_seq (SV *arrayref) |
1391 | ber_seq (SV *arrayref) |
1154 | CODE: |
1392 | CODE: |
1155 | { |
1393 | { |
1156 | AV *av = newAV (); |
1394 | AV *av = newAV (); |
1157 | av_fill (av, BER_ARRAYSIZE - 1); |
1395 | av_fill (av, BER_ARRAYSIZE - 1); |
1158 | AvARRAY (av)[BER_CLASS ] = newSVcacheint (ASN_UNIVERSAL); |
1396 | AvARRAY (av)[BER_CLASS] = newSVcacheint (ASN_UNIVERSAL); |
1159 | AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_SEQUENCE); |
1397 | AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_SEQUENCE); |
1160 | AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (1); |
1398 | AvARRAY (av)[BER_FLAGS] = newSVcacheint (1); |
1161 | AvARRAY (av)[BER_DATA ] = newSVsv (arrayref); |
1399 | AvARRAY (av)[BER_DATA ] = newSVsv (arrayref); |
1162 | RETVAL = newRV_noinc ((SV *)av); |
1400 | RETVAL = newRV_noinc ((SV *)av); |
1163 | } |
1401 | } |
1164 | OUTPUT: RETVAL |
1402 | OUTPUT: RETVAL |
1165 | |
1403 | |
1166 | MODULE = Convert::BER::XS PACKAGE = Convert::BER::XS::Profile |
1404 | MODULE = Convert::BER::XS PACKAGE = Convert::BER::XS::Profile |