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