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 | |
91 | typedef void profile_type; |
95 | typedef void profile_type; |
92 | |
96 | |
93 | static profile_type *cur_profile, *default_profile; |
97 | static profile_type *cur_profile, *default_profile; |
94 | static SV *buf_sv; // encoding buffer |
98 | static SV *buf_sv; // encoding buffer |
95 | static U8 *buf, *cur, *end; // buffer start, current, end |
99 | static U8 *buf, *cur, *end; // buffer start, current, end |
|
|
100 | |
|
|
101 | #if PERL_VERSION < 18 |
|
|
102 | # define utf8_to_uvchr_buf(s,e,l) utf8_to_uvchr (s, l) |
|
|
103 | #endif |
96 | |
104 | |
97 | #if __GNUC__ >= 3 |
105 | #if __GNUC__ >= 3 |
98 | # define expect(expr,value) __builtin_expect ((expr), (value)) |
106 | # define expect(expr,value) __builtin_expect ((expr), (value)) |
99 | # define INLINE static inline |
107 | # define INLINE static inline |
100 | #else |
108 | #else |
… | |
… | |
133 | { |
141 | { |
134 | if (!SvOK (profile)) |
142 | if (!SvOK (profile)) |
135 | return default_profile; |
143 | return default_profile; |
136 | |
144 | |
137 | if (!SvROK (profile)) |
145 | if (!SvROK (profile)) |
138 | croak ("invalid profile"); |
146 | croak ("Convert::BER::XS::Profile expected"); |
139 | |
147 | |
140 | profile = SvRV (profile); |
148 | profile = SvRV (profile); |
141 | |
149 | |
142 | if (SvSTASH (profile) != profile_stash) |
150 | if (SvSTASH (profile) != profile_stash) |
143 | croak ("invalid profile object"); |
151 | croak ("Convert::BER::XS::Profile expected"); |
144 | |
152 | |
145 | return (void *)profile; |
153 | return (void *)profile; |
146 | } |
154 | } |
147 | |
155 | |
148 | static int |
156 | static int |
… | |
… | |
155 | return BER_TYPE_BYTES; |
163 | return BER_TYPE_BYTES; |
156 | |
164 | |
157 | return SvPVX (sv)[idx]; |
165 | return SvPVX (sv)[idx]; |
158 | } |
166 | } |
159 | |
167 | |
160 | static int |
168 | static void |
161 | profile_set (profile_type *profile, int klass, int tag, int type) |
169 | profile_set (profile_type *profile, int klass, int tag, int type) |
162 | { |
170 | { |
163 | SV *sv = (SV *)profile; |
171 | SV *sv = (SV *)profile; |
164 | U32 idx = (tag << 2) + klass; |
172 | U32 idx = (tag << 2) + klass; |
165 | STRLEN oldlen = SvCUR (sv); |
173 | STRLEN oldlen = SvCUR (sv); |
… | |
… | |
174 | |
182 | |
175 | SvPVX (sv)[idx] = type; |
183 | SvPVX (sv)[idx] = type; |
176 | } |
184 | } |
177 | |
185 | |
178 | static SV * |
186 | static SV * |
179 | profile_new () |
187 | profile_new (void) |
180 | { |
188 | { |
181 | SV *sv = newSVpvn ("", 0); |
189 | SV *sv = newSVpvn ("", 0); |
182 | |
190 | |
183 | static const struct { |
191 | static const struct { |
184 | int klass; |
192 | int klass; |
185 | int tag; |
193 | int tag; |
186 | int type; |
194 | int type; |
187 | } *celem, default_map[] = { |
195 | } *celem, default_map[] = { |
188 | { ASN_UNIVERSAL, ASN_BOOLEAN , BER_TYPE_BOOL }, |
196 | { ASN_UNIVERSAL, ASN_BOOLEAN , BER_TYPE_BOOL }, |
189 | { ASN_UNIVERSAL, ASN_INTEGER32 , BER_TYPE_INT }, |
197 | { ASN_UNIVERSAL, ASN_INTEGER , BER_TYPE_INT }, |
190 | { ASN_UNIVERSAL, ASN_NULL , BER_TYPE_NULL }, |
198 | { ASN_UNIVERSAL, ASN_NULL , BER_TYPE_NULL }, |
191 | { ASN_UNIVERSAL, ASN_OBJECT_IDENTIFIER, BER_TYPE_OID }, |
199 | { ASN_UNIVERSAL, ASN_OBJECT_IDENTIFIER, BER_TYPE_OID }, |
192 | { ASN_UNIVERSAL, ASN_OBJECT_DESCRIPTOR, BER_TYPE_OID }, |
|
|
193 | { ASN_UNIVERSAL, ASN_RELATIVE_OID , BER_TYPE_RELOID }, |
200 | { ASN_UNIVERSAL, ASN_RELATIVE_OID , BER_TYPE_RELOID }, |
194 | { ASN_UNIVERSAL, ASN_REAL , BER_TYPE_REAL }, |
201 | { ASN_UNIVERSAL, ASN_REAL , BER_TYPE_REAL }, |
|
|
202 | { ASN_UNIVERSAL, ASN_ENUMERATED , BER_TYPE_INT }, |
195 | { ASN_UNIVERSAL, ASN_UTF8_STRING , BER_TYPE_UTF8 }, |
203 | { ASN_UNIVERSAL, ASN_UTF8_STRING , BER_TYPE_UTF8 }, |
196 | { ASN_UNIVERSAL, ASN_BMP_STRING , BER_TYPE_UCS2 }, |
204 | { ASN_UNIVERSAL, ASN_BMP_STRING , BER_TYPE_UCS2 }, |
197 | { ASN_UNIVERSAL, ASN_UNIVERSAL_STRING , BER_TYPE_UCS4 }, |
205 | { ASN_UNIVERSAL, ASN_UNIVERSAL_STRING , BER_TYPE_UCS4 }, |
198 | }; |
206 | }; |
199 | |
207 | |
200 | 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; ) |
201 | profile_set ((void *)sv, celem->klass, celem->tag, celem->type); |
209 | profile_set ((profile_type *)sv, celem->klass, celem->tag, celem->type); |
202 | |
210 | |
203 | return sv_bless (newRV_noinc (sv), profile_stash); |
211 | return sv_bless (newRV_noinc (sv), profile_stash); |
204 | } |
212 | } |
205 | |
213 | |
206 | ///////////////////////////////////////////////////////////////////////////// |
214 | ///////////////////////////////////////////////////////////////////////////// |
… | |
… | |
219 | error ("unexpected end of message buffer"); |
227 | error ("unexpected end of message buffer"); |
220 | } |
228 | } |
221 | |
229 | |
222 | // get_* functions fetch something from the buffer |
230 | // get_* functions fetch something from the buffer |
223 | // 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 | } |
224 | |
242 | |
225 | // get n octets |
243 | // get n octets |
226 | static U8 * |
244 | static U8 * |
227 | get_n (UV count) |
245 | get_n (UV count) |
228 | { |
246 | { |
… | |
… | |
230 | U8 *res = cur; |
248 | U8 *res = cur; |
231 | cur += count; |
249 | cur += count; |
232 | return res; |
250 | return res; |
233 | } |
251 | } |
234 | |
252 | |
235 | // get single octet |
|
|
236 | static U8 |
|
|
237 | get_u8 (void) |
|
|
238 | { |
|
|
239 | if (cur == end) |
|
|
240 | error ("unexpected end of message buffer"); |
|
|
241 | |
|
|
242 | return *cur++; |
|
|
243 | } |
|
|
244 | |
|
|
245 | // get ber-encoded integer (i.e. pack "w") |
253 | // get ber-encoded integer (i.e. pack "w") |
246 | static U32 |
254 | static UV |
247 | get_w (void) |
255 | get_w (void) |
248 | { |
256 | { |
249 | 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)"); |
250 | |
262 | |
251 | for (;;) |
263 | for (;;) |
252 | { |
264 | { |
253 | U8 c = get_u8 (); |
265 | if (expect_false (res >> UVSIZE * 8 - 7)) |
|
|
266 | error ("BER variable length integer overflow"); |
|
|
267 | |
254 | res = (res << 7) | (c & 0x7f); |
268 | res = (res << 7) | (c & 0x7f); |
255 | |
269 | |
256 | if (!(c & 0x80)) |
270 | if (expect_true (!(c & 0x80))) |
257 | return res; |
271 | return res; |
258 | } |
|
|
259 | } |
|
|
260 | |
272 | |
|
|
273 | c = get_u8 (); |
|
|
274 | } |
|
|
275 | } |
|
|
276 | |
261 | static U32 |
277 | static UV |
262 | get_length (void) |
278 | get_length (void) |
263 | { |
279 | { |
264 | U32 res = get_u8 (); |
280 | UV res = get_u8 (); |
265 | |
281 | |
266 | if (res & 0x80) |
282 | if (expect_false (res & 0x80)) |
267 | { |
283 | { |
268 | 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 | |
269 | res = 0; |
298 | res = 0; |
270 | |
299 | do |
271 | switch (cnt) |
300 | res = (res << 8) | *cur++; |
272 | { |
301 | while (--cnt); |
273 | case 0: |
|
|
274 | error ("indefinite ASN.1 lengths not supported"); |
|
|
275 | return 0; |
|
|
276 | |
|
|
277 | default: |
|
|
278 | error ("ASN.1 length too long"); |
|
|
279 | return 0; |
|
|
280 | |
|
|
281 | case 4: res = (res << 8) | get_u8 (); |
|
|
282 | case 3: res = (res << 8) | get_u8 (); |
|
|
283 | case 2: res = (res << 8) | get_u8 (); |
|
|
284 | case 1: res = (res << 8) | get_u8 (); |
|
|
285 | } |
|
|
286 | } |
302 | } |
287 | |
303 | |
288 | return res; |
304 | return res; |
289 | } |
305 | } |
290 | |
306 | |
291 | static SV * |
307 | static SV * |
292 | decode_int () |
308 | decode_int (void) |
293 | { |
309 | { |
294 | int len = get_length (); |
310 | UV len = get_length (); |
295 | |
311 | |
296 | if (len <= 0) |
312 | if (!len) |
297 | { |
313 | error ("invalid BER_TYPE_INT length zero (X.690 8.3.1)"); |
298 | error ("integer length equal to zero"); |
|
|
299 | return 0; |
|
|
300 | } |
|
|
301 | |
314 | |
302 | U8 *data = get_n (len); |
315 | U8 *data = get_n (len); |
303 | |
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 | |
304 | int negative = data [0] & 0x80; |
325 | int negative = data [0] & 0x80; |
305 | |
326 | |
306 | 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"); |
307 | |
331 | |
308 | do |
332 | do |
309 | val = (val << 8) | *data++; |
333 | val = (val << 8) | *data++; |
310 | while (--len); |
334 | while (--len); |
311 | |
335 | |
… | |
… | |
315 | } |
339 | } |
316 | |
340 | |
317 | static SV * |
341 | static SV * |
318 | decode_data (void) |
342 | decode_data (void) |
319 | { |
343 | { |
320 | U32 len = get_length (); |
344 | UV len = get_length (); |
321 | U8 *data = get_n (len); |
|
|
322 | return newSVpvn ((char *)data, len); |
345 | return newSVpvn ((char *)get_n (len), len); |
323 | } |
346 | } |
324 | |
347 | |
325 | // gelper for decode_object_identifier |
348 | // helper for decode_object_identifier |
326 | static char * |
349 | static char * |
327 | write_uv (char *buf, U32 u) |
350 | write_uv (char *buf, UV u) |
328 | { |
351 | { |
329 | // 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) |
330 | if (expect_true (u < 10)) |
353 | if (expect_true (u < 10)) |
331 | *buf++ = u + '0'; |
354 | *buf++ = u + '0'; |
332 | else |
355 | else |
333 | { |
356 | { |
|
|
357 | // this *could* be done much faster using branchless fixed-point arithmetics |
334 | char *beg = buf; |
358 | char *beg = buf; |
335 | |
359 | |
336 | do |
360 | do |
337 | { |
361 | { |
338 | *buf++ = u % 10 + '0'; |
362 | *buf++ = u % 10 + '0'; |
339 | u /= 10; |
363 | u /= 10; |
340 | } |
364 | } |
341 | while (u); |
365 | while (u); |
342 | |
366 | |
343 | // reverse digits |
367 | // reverse digits |
344 | for (char *ptr = buf; --ptr != beg; ++beg) |
368 | char *ptr = buf; |
|
|
369 | while (--ptr > beg) |
345 | { |
370 | { |
346 | char c = *ptr; |
371 | char c = *ptr; |
347 | *ptr = *beg; |
372 | *ptr = *beg; |
348 | *beg = c; |
373 | *beg = c; |
|
|
374 | ++beg; |
349 | } |
375 | } |
350 | } |
376 | } |
351 | |
377 | |
352 | return buf; |
378 | return buf; |
353 | } |
379 | } |
354 | |
380 | |
355 | static SV * |
381 | static SV * |
356 | decode_oid (int relative) |
382 | decode_oid (int relative) |
357 | { |
383 | { |
358 | U32 len = get_length (); |
384 | UV len = get_length (); |
359 | |
385 | |
360 | if (len <= 0) |
386 | if (len <= 0) |
361 | { |
387 | { |
362 | error ("OBJECT IDENTIFIER length equal to zero"); |
388 | error ("BER_TYPE_OID length must not be zero"); |
363 | return &PL_sv_undef; |
389 | return &PL_sv_undef; |
364 | } |
390 | } |
365 | |
391 | |
366 | U8 *end = cur + len; |
392 | U8 *end = cur + len; |
367 | U32 w = get_w (); |
393 | UV w = get_w (); |
368 | |
394 | |
369 | static char oid[MAX_OID_STRLEN]; // must be static |
395 | static char oid[MAX_OID_STRLEN]; // static, because too large for stack |
370 | char *app = oid; |
396 | char *app = oid; |
371 | |
397 | |
372 | if (relative) |
398 | if (relative) |
373 | app = write_uv (app, w); |
399 | app = write_uv (app, w); |
374 | else |
400 | else |
375 | { |
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 | |
376 | app = write_uv (app, (U8)w / 40); |
409 | app = write_uv (app, w1); |
377 | *app++ = '.'; |
410 | *app++ = '.'; |
378 | app = write_uv (app, (U8)w % 40); |
411 | app = write_uv (app, w2); |
|
|
412 | } |
|
|
413 | |
|
|
414 | while (cur < end) |
379 | } |
415 | { |
380 | |
|
|
381 | // we assume an oid component is never > 64 bytes |
416 | // we assume an oid component is never > 64 digits |
382 | while (cur < end && oid + sizeof (oid) - app > 64) |
417 | if (oid + sizeof (oid) - app < 64) |
383 | { |
418 | croak ("BER_TYPE_OID to long to decode"); |
|
|
419 | |
384 | w = get_w (); |
420 | w = get_w (); |
385 | *app++ = '.'; |
421 | *app++ = '.'; |
386 | app = write_uv (app, w); |
422 | app = write_uv (app, w); |
387 | } |
423 | } |
388 | |
424 | |
… | |
… | |
393 | static SV * |
429 | static SV * |
394 | decode_ucs (int chrsize) |
430 | decode_ucs (int chrsize) |
395 | { |
431 | { |
396 | SV *res = NEWSV (0, 0); |
432 | SV *res = NEWSV (0, 0); |
397 | |
433 | |
398 | U32 len = get_length (); |
434 | UV len = get_length (); |
399 | |
435 | |
400 | if (len & (chrsize - 1)) |
436 | if (len & (chrsize - 1)) |
401 | 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); |
402 | |
438 | |
403 | while (len) |
439 | while (len) |
… | |
… | |
424 | |
460 | |
425 | return res; |
461 | return res; |
426 | } |
462 | } |
427 | |
463 | |
428 | static SV * |
464 | static SV * |
429 | decode_ber () |
465 | decode_ber (void) |
430 | { |
466 | { |
431 | int identifier = get_u8 (); |
467 | int identifier = get_u8 (); |
432 | |
468 | |
433 | SV *res; |
469 | SV *res; |
434 | |
470 | |
… | |
… | |
437 | int tag = identifier & ASN_TAG_MASK; |
473 | int tag = identifier & ASN_TAG_MASK; |
438 | |
474 | |
439 | if (tag == ASN_TAG_BER) |
475 | if (tag == ASN_TAG_BER) |
440 | tag = get_w (); |
476 | tag = get_w (); |
441 | |
477 | |
442 | if (tag == ASN_TAG_BER) |
|
|
443 | tag = get_w (); |
|
|
444 | |
|
|
445 | if (constructed) |
478 | if (constructed) |
446 | { |
479 | { |
447 | U32 len = get_length (); |
480 | UV len = get_length (); |
448 | U32 seqend = (cur - buf) + len; |
481 | UV seqend = (cur - buf) + len; |
449 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
482 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
450 | |
483 | |
451 | while (cur < buf + seqend) |
484 | while (cur < buf + seqend) |
452 | av_push (av, decode_ber ()); |
485 | av_push (av, decode_ber ()); |
453 | |
486 | |
454 | if (cur > buf + seqend) |
487 | if (cur > buf + seqend) |
455 | 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); |
456 | |
489 | |
457 | res = newRV_inc ((SV *)av); |
490 | res = newRV_inc ((SV *)av); |
458 | } |
491 | } |
459 | else |
492 | else |
460 | switch (profile_lookup (cur_profile, klass, tag)) |
493 | switch (profile_lookup (cur_profile, klass, tag)) |
461 | { |
494 | { |
462 | 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 | |
463 | res = &PL_sv_undef; |
502 | res = &PL_sv_undef; |
|
|
503 | } |
464 | break; |
504 | break; |
465 | |
505 | |
466 | case BER_TYPE_BOOL: |
506 | case BER_TYPE_BOOL: |
467 | { |
507 | { |
468 | U32 len = get_length (); |
508 | UV len = get_length (); |
469 | |
509 | |
470 | if (len != 1) |
510 | if (len != 1) |
471 | 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); |
472 | |
512 | |
473 | res = newSVcacheint (get_u8 () ? 0 : 1); |
513 | res = newSVcacheint (!!get_u8 ()); |
474 | } |
514 | } |
475 | break; |
515 | break; |
476 | |
516 | |
477 | case BER_TYPE_OID: |
517 | case BER_TYPE_OID: |
478 | res = decode_oid (0); |
518 | res = decode_oid (0); |
… | |
… | |
495 | res = decode_data (); |
535 | res = decode_data (); |
496 | break; |
536 | break; |
497 | |
537 | |
498 | case BER_TYPE_IPADDRESS: |
538 | case BER_TYPE_IPADDRESS: |
499 | { |
539 | { |
500 | U32 len = get_length (); |
540 | UV len = get_length (); |
501 | |
541 | |
502 | if (len != 4) |
542 | if (len != 4) |
503 | 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); |
504 | |
544 | |
505 | U8 c1 = get_u8 (); |
545 | U8 *data = get_n (4); |
506 | U8 c2 = get_u8 (); |
546 | res = newSVpvf ("%d.%d.%d.%d", data [0], data [1], data [2], data [3]); |
507 | U8 c3 = get_u8 (); |
|
|
508 | U8 c4 = get_u8 (); |
|
|
509 | |
|
|
510 | res = newSVpvf ("%d.%d.%d.%d", c1, c2, c3, c4); |
|
|
511 | } |
547 | } |
512 | break; |
548 | break; |
513 | |
549 | |
514 | case BER_TYPE_UCS2: |
550 | case BER_TYPE_UCS2: |
515 | res = decode_ucs (2); |
551 | res = decode_ucs (2); |
… | |
… | |
518 | case BER_TYPE_UCS4: |
554 | case BER_TYPE_UCS4: |
519 | res = decode_ucs (4); |
555 | res = decode_ucs (4); |
520 | break; |
556 | break; |
521 | |
557 | |
522 | case BER_TYPE_REAL: |
558 | case BER_TYPE_REAL: |
|
|
559 | error ("BER_TYPE_REAL not implemented"); |
|
|
560 | |
523 | case BER_TYPE_CROAK: |
561 | case BER_TYPE_CROAK: |
|
|
562 | croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag); |
|
|
563 | |
524 | default: |
564 | default: |
525 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
565 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
526 | } |
566 | } |
527 | |
567 | |
528 | AV *av = newAV (); |
568 | AV *av = newAV (); |
529 | av_fill (av, BER_ARRAYSIZE - 1); |
569 | av_fill (av, BER_ARRAYSIZE - 1); |
530 | AvARRAY (av)[BER_CLASS ] = newSVcacheint (klass); |
570 | AvARRAY (av)[BER_CLASS] = newSVcacheint (klass); |
531 | AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); |
571 | AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); |
532 | AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (constructed ? 1 : 0); |
572 | AvARRAY (av)[BER_FLAGS] = newSVcacheint (constructed ? 1 : 0); |
533 | AvARRAY (av)[BER_DATA ] = res; |
573 | AvARRAY (av)[BER_DATA ] = res; |
534 | |
574 | |
535 | return newRV_noinc ((SV *)av); |
575 | return newRV_noinc ((SV *)av); |
536 | } |
576 | } |
537 | |
577 | |
538 | ///////////////////////////////////////////////////////////////////////////// |
578 | ///////////////////////////////////////////////////////////////////////////// |
… | |
… | |
543 | strlen_sum (STRLEN l1, STRLEN l2) |
583 | strlen_sum (STRLEN l1, STRLEN l2) |
544 | { |
584 | { |
545 | size_t sum = l1 + l2; |
585 | size_t sum = l1 + l2; |
546 | |
586 | |
547 | if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) |
587 | if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) |
548 | croak ("JSON::XS: string size overflow"); |
588 | croak ("Convert::BER::XS: string size overflow"); |
549 | |
589 | |
550 | return sum; |
590 | return sum; |
551 | } |
591 | } |
552 | |
592 | |
553 | static void |
593 | static void |
554 | set_buf (SV *sv) |
594 | set_buf (SV *sv) |
555 | { |
595 | { |
556 | STRLEN len; |
596 | STRLEN len; |
557 | buf_sv = sv; |
597 | buf_sv = sv; |
558 | buf = SvPVbyte (buf_sv, len); |
598 | buf = (U8 *)SvPVbyte (buf_sv, len); |
559 | cur = buf; |
599 | cur = buf; |
560 | end = buf + len; |
600 | end = buf + len; |
561 | } |
601 | } |
562 | |
602 | |
563 | /* similar to SvGROW, but somewhat safer and guarantees exponential realloc strategy */ |
603 | /* similar to SvGROW, but somewhat safer and guarantees exponential realloc strategy */ |
… | |
… | |
577 | need (STRLEN len) |
617 | need (STRLEN len) |
578 | { |
618 | { |
579 | if (expect_false ((uintptr_t)(end - cur) < len)) |
619 | if (expect_false ((uintptr_t)(end - cur) < len)) |
580 | { |
620 | { |
581 | STRLEN pos = cur - buf; |
621 | STRLEN pos = cur - buf; |
582 | buf = my_sv_grow (buf_sv, pos, len); |
622 | buf = (U8 *)my_sv_grow (buf_sv, pos, len); |
583 | cur = buf + pos; |
623 | cur = buf + pos; |
584 | end = buf + SvLEN (buf_sv) - 1; |
624 | end = buf + SvLEN (buf_sv) - 1; |
585 | } |
625 | } |
586 | } |
626 | } |
587 | |
627 | |
… | |
… | |
591 | need (1); |
631 | need (1); |
592 | *cur++ = val; |
632 | *cur++ = val; |
593 | } |
633 | } |
594 | |
634 | |
595 | static void |
635 | static void |
596 | put_w_nocheck (U32 val) |
636 | put_w_nocheck (UV val) |
597 | { |
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 |
598 | *cur = (val >> 7 * 4) | 0x80; cur += val >= (1 << (7 * 4)); |
645 | *cur = (val >> 7 * 4) | 0x80; cur += val >= ((UV)1 << (7 * 4)); |
599 | *cur = (val >> 7 * 3) | 0x80; cur += val >= (1 << (7 * 3)); |
646 | *cur = (val >> 7 * 3) | 0x80; cur += val >= ((UV)1 << (7 * 3)); |
600 | *cur = (val >> 7 * 2) | 0x80; cur += val >= (1 << (7 * 2)); |
647 | *cur = (val >> 7 * 2) | 0x80; cur += val >= ((UV)1 << (7 * 2)); |
601 | *cur = (val >> 7 * 1) | 0x80; cur += val >= (1 << (7 * 1)); |
648 | *cur = (val >> 7 * 1) | 0x80; cur += val >= ((UV)1 << (7 * 1)); |
602 | *cur = val & 0x7f; cur += 1; |
649 | *cur = val & 0x7f; cur += 1; |
603 | } |
650 | } |
604 | |
651 | |
605 | static void |
652 | static void |
606 | put_w (U32 val) |
653 | put_w (UV val) |
607 | { |
654 | { |
608 | need (5); // we only handle up to 5 bytes |
655 | need (5); // we only handle up to 5 bytes |
609 | |
656 | |
610 | put_w_nocheck (val); |
657 | put_w_nocheck (val); |
611 | } |
658 | } |
612 | |
659 | |
613 | static U8 * |
660 | static U8 * |
614 | put_length_at (U32 val, U8 *cur) |
661 | put_length_at (UV val, U8 *cur) |
615 | { |
662 | { |
616 | if (val < 0x7fU) |
663 | if (val <= 0x7fU) |
617 | *cur++ = val; |
664 | *cur++ = val; |
618 | else |
665 | else |
619 | { |
666 | { |
620 | U8 *lenb = cur++; |
667 | U8 *lenb = cur++; |
621 | |
668 | |
622 | *cur = val >> 24; cur += *cur > 0; |
669 | #if UVSIZE > 4 |
623 | *cur = val >> 16; cur += *cur > 0; |
670 | *cur = val >> 56; cur += val >= ((UV)1 << (8 * 7)); |
624 | *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)); |
625 | *cur = val ; cur += 1; |
678 | *cur = val ; cur += 1; |
626 | |
679 | |
627 | *lenb = 0x80 + cur - lenb - 1; |
680 | *lenb = 0x80 + cur - lenb - 1; |
628 | } |
681 | } |
629 | |
682 | |
630 | return cur; |
683 | return cur; |
631 | } |
684 | } |
632 | |
685 | |
633 | static void |
686 | static void |
634 | put_length (U32 val) |
687 | put_length (UV val) |
635 | { |
688 | { |
636 | need (5 + val); |
689 | need (9 + val); |
637 | cur = put_length_at (val, cur); |
690 | cur = put_length_at (val, cur); |
638 | } |
691 | } |
639 | |
692 | |
640 | // return how many bytes the encoded length requires |
693 | // return how many bytes the encoded length requires |
641 | static int length_length (U32 val) |
694 | static int length_length (UV val) |
642 | { |
695 | { |
643 | return val < 0x7fU |
696 | // use hashing with a DeBruin sequence, anyone? |
|
|
697 | return expect_true (val <= 0x7fU) |
644 | ? 1 |
698 | ? 1 |
645 | : 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 | ; |
646 | } |
710 | } |
647 | |
711 | |
648 | static void |
712 | static void |
649 | encode_data (const char *ptr, STRLEN len) |
713 | encode_data (const char *ptr, STRLEN len) |
650 | { |
714 | { |
… | |
… | |
712 | |
776 | |
713 | *lenb = cur - lenb - 1; |
777 | *lenb = cur - lenb - 1; |
714 | } |
778 | } |
715 | |
779 | |
716 | // we don't know the length yet, so we optimistically |
780 | // we don't know the length yet, so we optimistically |
717 | // assume the length will need one octet later. if that |
781 | // assume the length will need one octet later. If that |
718 | // turns out to be wrong, we memove as needed. |
782 | // turns out to be wrong, we memmove as needed. |
719 | // mark the beginning |
783 | // mark the beginning |
720 | static STRLEN |
784 | static STRLEN |
721 | len_fixup_mark () |
785 | len_fixup_mark (void) |
722 | { |
786 | { |
723 | return cur++ - buf; |
787 | return cur++ - buf; |
724 | } |
788 | } |
725 | |
789 | |
726 | // patch up the length |
790 | // patch up the length |
… | |
… | |
813 | put_length (uchars * chrsize); |
877 | put_length (uchars * chrsize); |
814 | |
878 | |
815 | while (uchars--) |
879 | while (uchars--) |
816 | { |
880 | { |
817 | STRLEN uclen; |
881 | STRLEN uclen; |
818 | UV uchr = utf8_to_uvchr_buf (ptr, ptr + len, &uclen); |
882 | UV uchr = utf8_to_uvchr_buf ((U8 *)ptr, (U8 *)ptr + len, &uclen); |
819 | |
883 | |
820 | ptr += uclen; |
884 | ptr += uclen; |
821 | len -= uclen; |
885 | len -= uclen; |
822 | |
886 | |
823 | if (chrsize == 4) |
887 | if (chrsize == 4) |
… | |
… | |
835 | { |
899 | { |
836 | AV *av = ber_tuple (tuple); |
900 | AV *av = ber_tuple (tuple); |
837 | |
901 | |
838 | int klass = SvIV (AvARRAY (av)[BER_CLASS]); |
902 | int klass = SvIV (AvARRAY (av)[BER_CLASS]); |
839 | int tag = SvIV (AvARRAY (av)[BER_TAG]); |
903 | int tag = SvIV (AvARRAY (av)[BER_TAG]); |
840 | int constructed = SvIV (AvARRAY (av)[BER_CONSTRUCTED]) ? ASN_CONSTRUCTED : 0; |
904 | int constructed = SvIV (AvARRAY (av)[BER_FLAGS]) & 1 ? ASN_CONSTRUCTED : 0; |
841 | SV *data = AvARRAY (av)[BER_DATA]; |
905 | SV *data = AvARRAY (av)[BER_DATA]; |
842 | |
906 | |
843 | int identifier = (klass << ASN_CLASS_SHIFT) | constructed; |
907 | int identifier = (klass << ASN_CLASS_SHIFT) | constructed; |
844 | |
908 | |
845 | if (expect_false (tag >= ASN_TAG_BER)) |
909 | if (expect_false (tag >= ASN_TAG_BER)) |
… | |
… | |
856 | // and adjust later |
920 | // and adjust later |
857 | need (1); |
921 | need (1); |
858 | STRLEN mark = len_fixup_mark (); |
922 | STRLEN mark = len_fixup_mark (); |
859 | |
923 | |
860 | if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV)) |
924 | if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV)) |
861 | croak ("BER constructed data must be array-reference"); |
925 | croak ("BER CONSTRUCTED data must be array-reference"); |
862 | |
926 | |
863 | AV *av = (AV *)SvRV (data); |
927 | AV *av = (AV *)SvRV (data); |
864 | int fill = AvFILL (av); |
928 | int fill = AvFILL (av); |
865 | |
929 | |
866 | if (expect_false (SvRMAGICAL (av))) |
930 | if (expect_false (SvRMAGICAL (av))) |
867 | croak ("BER constructed data must not be tied"); |
931 | croak ("BER CONSTRUCTED data must not be tied"); |
868 | |
932 | |
|
|
933 | int i; |
869 | for (int i = 0; i <= fill; ++i) |
934 | for (i = 0; i <= fill; ++i) |
870 | encode_ber (AvARRAY (av)[i]); |
935 | encode_ber (AvARRAY (av)[i]); |
871 | |
936 | |
872 | len_fixup (mark); |
937 | len_fixup (mark); |
873 | } |
938 | } |
874 | else |
939 | else |
… | |
… | |
878 | put_length (0); |
943 | put_length (0); |
879 | break; |
944 | break; |
880 | |
945 | |
881 | case BER_TYPE_BOOL: |
946 | case BER_TYPE_BOOL: |
882 | put_length (1); |
947 | put_length (1); |
883 | *cur++ = SvTRUE (data) ? 0xff : 0x00; |
948 | *cur++ = SvTRUE (data) ? 0xff : 0x00; // 0xff = DER/CER |
884 | break; |
949 | break; |
885 | |
950 | |
886 | case BER_TYPE_OID: |
951 | case BER_TYPE_OID: |
887 | encode_oid (data, 0); |
952 | encode_oid (data, 0); |
888 | break; |
953 | break; |
… | |
… | |
926 | case BER_TYPE_UCS4: |
991 | case BER_TYPE_UCS4: |
927 | encode_ucs (data, 4); |
992 | encode_ucs (data, 4); |
928 | break; |
993 | break; |
929 | |
994 | |
930 | case BER_TYPE_REAL: |
995 | case BER_TYPE_REAL: |
|
|
996 | croak ("BER_TYPE_REAL not implemented"); |
|
|
997 | |
931 | case BER_TYPE_CROAK: |
998 | case BER_TYPE_CROAK: |
|
|
999 | croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag); |
|
|
1000 | |
932 | default: |
1001 | default: |
933 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
1002 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
934 | } |
1003 | } |
935 | |
1004 | |
936 | } |
1005 | } |
… | |
… | |
951 | const char *name; |
1020 | const char *name; |
952 | IV iv; |
1021 | IV iv; |
953 | } *civ, const_iv[] = { |
1022 | } *civ, const_iv[] = { |
954 | #define const_iv(name) { # name, name }, |
1023 | #define const_iv(name) { # name, name }, |
955 | const_iv (ASN_BOOLEAN) |
1024 | const_iv (ASN_BOOLEAN) |
956 | const_iv (ASN_INTEGER32) |
1025 | const_iv (ASN_INTEGER) |
957 | const_iv (ASN_BIT_STRING) |
1026 | const_iv (ASN_BIT_STRING) |
958 | const_iv (ASN_OCTET_STRING) |
1027 | const_iv (ASN_OCTET_STRING) |
959 | const_iv (ASN_NULL) |
1028 | const_iv (ASN_NULL) |
960 | const_iv (ASN_OBJECT_IDENTIFIER) |
1029 | const_iv (ASN_OBJECT_IDENTIFIER) |
961 | const_iv (ASN_OBJECT_DESCRIPTOR) |
1030 | const_iv (ASN_OBJECT_DESCRIPTOR) |
… | |
… | |
990 | const_iv (ASN_CONTEXT) |
1059 | const_iv (ASN_CONTEXT) |
991 | const_iv (ASN_PRIVATE) |
1060 | const_iv (ASN_PRIVATE) |
992 | |
1061 | |
993 | const_iv (BER_CLASS) |
1062 | const_iv (BER_CLASS) |
994 | const_iv (BER_TAG) |
1063 | const_iv (BER_TAG) |
995 | const_iv (BER_CONSTRUCTED) |
1064 | const_iv (BER_FLAGS) |
996 | const_iv (BER_DATA) |
1065 | const_iv (BER_DATA) |
997 | |
1066 | |
998 | const_iv (BER_TYPE_BYTES) |
1067 | const_iv (BER_TYPE_BYTES) |
999 | const_iv (BER_TYPE_UTF8) |
1068 | const_iv (BER_TYPE_UTF8) |
1000 | const_iv (BER_TYPE_UCS2) |
1069 | const_iv (BER_TYPE_UCS2) |
… | |
… | |
1008 | const_iv (BER_TYPE_IPADDRESS) |
1077 | const_iv (BER_TYPE_IPADDRESS) |
1009 | const_iv (BER_TYPE_CROAK) |
1078 | const_iv (BER_TYPE_CROAK) |
1010 | |
1079 | |
1011 | const_iv (SNMP_IPADDRESS) |
1080 | const_iv (SNMP_IPADDRESS) |
1012 | const_iv (SNMP_COUNTER32) |
1081 | const_iv (SNMP_COUNTER32) |
|
|
1082 | const_iv (SNMP_GAUGE32) |
1013 | const_iv (SNMP_UNSIGNED32) |
1083 | const_iv (SNMP_UNSIGNED32) |
1014 | const_iv (SNMP_TIMETICKS) |
1084 | const_iv (SNMP_TIMETICKS) |
1015 | const_iv (SNMP_OPAQUE) |
1085 | const_iv (SNMP_OPAQUE) |
1016 | const_iv (SNMP_COUNTER64) |
1086 | const_iv (SNMP_COUNTER64) |
1017 | }; |
1087 | }; |
1018 | |
1088 | |
1019 | 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--) |
1020 | newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); |
1090 | newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); |
1021 | } |
1091 | } |
1022 | |
1092 | |
1023 | SV * |
1093 | void |
1024 | 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 |
1025 | CODE: |
1097 | PPCODE: |
1026 | { |
1098 | { |
1027 | cur_profile = SvPROFILE (profile); |
1099 | cur_profile = SvPROFILE (profile); |
1028 | STRLEN len; |
1100 | STRLEN len; |
1029 | buf = SvPVbyte (ber, len); |
1101 | buf = (U8 *)SvPVbyte (ber, len); |
1030 | cur = buf; |
1102 | cur = buf; |
1031 | end = buf + len; |
1103 | end = buf + len; |
1032 | |
1104 | |
1033 | 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"); |
1034 | } |
1114 | } |
1035 | OUTPUT: RETVAL |
|
|
1036 | |
1115 | |
1037 | void |
1116 | void |
1038 | 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) |
1039 | PPCODE: |
1118 | PPCODE: |
1040 | { |
1119 | { |
1041 | if (!SvOK (tuple)) |
1120 | if (!SvOK (tuple)) |
1042 | XSRETURN_NO; |
1121 | XSRETURN_NO; |
1043 | |
1122 | |
… | |
… | |
1045 | croak ("ber_is: tuple must be BER tuple (array-ref)"); |
1124 | croak ("ber_is: tuple must be BER tuple (array-ref)"); |
1046 | |
1125 | |
1047 | AV *av = (AV *)SvRV (tuple); |
1126 | AV *av = (AV *)SvRV (tuple); |
1048 | |
1127 | |
1049 | XPUSHs ( |
1128 | XPUSHs ( |
1050 | (!SvOK (klass) || SvIV (AvARRAY (av)[BER_CLASS ]) == SvIV (klass)) |
1129 | (!SvOK (klass) || SvIV (AvARRAY (av)[BER_CLASS]) == SvIV (klass)) |
1051 | && (!SvOK (tag) || SvIV (AvARRAY (av)[BER_TAG ]) == SvIV (tag)) |
1130 | && (!SvOK (tag) || SvIV (AvARRAY (av)[BER_TAG ]) == SvIV (tag)) |
1052 | && (!SvOK (constructed) || !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) == !SvIV (constructed)) |
1131 | && (!SvOK (flags) || !SvIV (AvARRAY (av)[BER_FLAGS]) == !SvIV (flags)) |
1053 | && (!SvOK (data) || sv_eq (AvARRAY (av)[BER_DATA ], data)) |
1132 | && (!SvOK (data) || sv_eq (AvARRAY (av)[BER_DATA ], data)) |
1054 | ? &PL_sv_yes : &PL_sv_undef); |
1133 | ? &PL_sv_yes : &PL_sv_undef); |
1055 | } |
1134 | } |
1056 | |
1135 | |
1057 | void |
1136 | void |
1058 | ber_is_seq (SV *tuple) |
1137 | ber_is_seq (SV *tuple) |
… | |
… | |
1062 | XSRETURN_UNDEF; |
1141 | XSRETURN_UNDEF; |
1063 | |
1142 | |
1064 | AV *av = ber_tuple (tuple); |
1143 | AV *av = ber_tuple (tuple); |
1065 | |
1144 | |
1066 | XPUSHs ( |
1145 | XPUSHs ( |
1067 | SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL |
1146 | SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL |
1068 | && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_SEQUENCE |
1147 | && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_SEQUENCE |
1069 | && SvIV (AvARRAY (av)[BER_CONSTRUCTED]) |
1148 | && SvIV (AvARRAY (av)[BER_FLAGS]) |
1070 | ? AvARRAY (av)[BER_DATA] : &PL_sv_undef); |
1149 | ? AvARRAY (av)[BER_DATA] : &PL_sv_undef); |
1071 | } |
1150 | } |
1072 | |
1151 | |
1073 | void |
1152 | void |
1074 | ber_is_i32 (SV *tuple, SV *value = &PL_sv_undef) |
1153 | ber_is_int (SV *tuple, SV *value = &PL_sv_undef) |
1075 | PPCODE: |
1154 | PPCODE: |
1076 | { |
1155 | { |
1077 | if (!SvOK (tuple)) |
1156 | if (!SvOK (tuple)) |
1078 | XSRETURN_NO; |
1157 | XSRETURN_NO; |
1079 | |
1158 | |
1080 | AV *av = ber_tuple (tuple); |
1159 | AV *av = ber_tuple (tuple); |
1081 | |
1160 | |
1082 | IV data = SvIV (AvARRAY (av)[BER_DATA]); |
1161 | UV data = SvUV (AvARRAY (av)[BER_DATA]); |
1083 | |
1162 | |
1084 | XPUSHs ( |
1163 | XPUSHs ( |
1085 | SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL |
1164 | SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL |
1086 | && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_INTEGER32 |
1165 | && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_INTEGER |
1087 | && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) |
1166 | && !SvIV (AvARRAY (av)[BER_FLAGS]) |
1088 | && (!SvOK (value) || data == SvIV (value)) |
1167 | && (!SvOK (value) || data == SvUV (value)) |
1089 | ? sv_2mortal (data ? newSViv (data) : newSVpv ("0 but true", 0)) |
1168 | ? sv_2mortal (data ? newSVsv (AvARRAY (av)[BER_DATA]) : newSVpv ("0 but true", 0)) |
1090 | : &PL_sv_undef); |
1169 | : &PL_sv_undef); |
1091 | } |
1170 | } |
1092 | |
1171 | |
1093 | void |
1172 | void |
1094 | ber_is_oid (SV *tuple, SV *oid = &PL_sv_undef) |
1173 | ber_is_oid (SV *tuple, SV *oid = &PL_sv_undef) |
… | |
… | |
1098 | XSRETURN_NO; |
1177 | XSRETURN_NO; |
1099 | |
1178 | |
1100 | AV *av = ber_tuple (tuple); |
1179 | AV *av = ber_tuple (tuple); |
1101 | |
1180 | |
1102 | XPUSHs ( |
1181 | XPUSHs ( |
1103 | SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL |
1182 | SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL |
1104 | && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_OBJECT_IDENTIFIER |
1183 | && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_OBJECT_IDENTIFIER |
1105 | && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) |
1184 | && !SvIV (AvARRAY (av)[BER_FLAGS]) |
1106 | && (!SvOK (oid) || sv_eq (AvARRAY (av)[BER_DATA], oid)) |
1185 | && (!SvOK (oid) || sv_eq (AvARRAY (av)[BER_DATA], oid)) |
1107 | ? newSVsv (AvARRAY (av)[BER_DATA]) : &PL_sv_undef); |
1186 | ? newSVsv (AvARRAY (av)[BER_DATA]) : &PL_sv_undef); |
1108 | } |
1187 | } |
1109 | |
1188 | |
1110 | ############################################################################# |
1189 | ############################################################################# |
… | |
… | |
1123 | SvCUR_set (buf_sv, cur - buf); |
1202 | SvCUR_set (buf_sv, cur - buf); |
1124 | XPUSHs (buf_sv); |
1203 | XPUSHs (buf_sv); |
1125 | } |
1204 | } |
1126 | |
1205 | |
1127 | SV * |
1206 | SV * |
1128 | ber_i32 (IV iv) |
1207 | ber_int (SV *sv) |
1129 | CODE: |
1208 | CODE: |
1130 | { |
1209 | { |
1131 | AV *av = newAV (); |
1210 | AV *av = newAV (); |
1132 | av_fill (av, BER_ARRAYSIZE - 1); |
1211 | av_fill (av, BER_ARRAYSIZE - 1); |
1133 | AvARRAY (av)[BER_CLASS ] = newSVcacheint (ASN_UNIVERSAL); |
1212 | AvARRAY (av)[BER_CLASS] = newSVcacheint (ASN_UNIVERSAL); |
1134 | AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_INTEGER32); |
1213 | AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_INTEGER); |
1135 | AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (0); |
1214 | AvARRAY (av)[BER_FLAGS] = newSVcacheint (0); |
1136 | AvARRAY (av)[BER_DATA ] = newSViv (iv); |
1215 | AvARRAY (av)[BER_DATA ] = newSVsv (sv); |
1137 | RETVAL = newRV_noinc ((SV *)av); |
1216 | RETVAL = newRV_noinc ((SV *)av); |
1138 | } |
1217 | } |
1139 | OUTPUT: RETVAL |
1218 | OUTPUT: RETVAL |
1140 | |
1219 | |
1141 | # TODO: not arrayref, but elements? |
1220 | # TODO: not arrayref, but elements? |
… | |
… | |
1143 | ber_seq (SV *arrayref) |
1222 | ber_seq (SV *arrayref) |
1144 | CODE: |
1223 | CODE: |
1145 | { |
1224 | { |
1146 | AV *av = newAV (); |
1225 | AV *av = newAV (); |
1147 | av_fill (av, BER_ARRAYSIZE - 1); |
1226 | av_fill (av, BER_ARRAYSIZE - 1); |
1148 | AvARRAY (av)[BER_CLASS ] = newSVcacheint (ASN_UNIVERSAL); |
1227 | AvARRAY (av)[BER_CLASS] = newSVcacheint (ASN_UNIVERSAL); |
1149 | AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_SEQUENCE); |
1228 | AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_SEQUENCE); |
1150 | AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (1); |
1229 | AvARRAY (av)[BER_FLAGS] = newSVcacheint (1); |
1151 | AvARRAY (av)[BER_DATA ] = newSVsv (arrayref); |
1230 | AvARRAY (av)[BER_DATA ] = newSVsv (arrayref); |
1152 | RETVAL = newRV_noinc ((SV *)av); |
1231 | RETVAL = newRV_noinc ((SV *)av); |
1153 | } |
1232 | } |
1154 | OUTPUT: RETVAL |
1233 | OUTPUT: RETVAL |
1155 | |
1234 | |
1156 | MODULE = Convert::BER::XS PACKAGE = Convert::BER::XS::Profile |
1235 | MODULE = Convert::BER::XS PACKAGE = Convert::BER::XS::Profile |