ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Convert-BER-XS/XS.xs
(Generate patch)

Comparing Convert-BER-XS/XS.xs (file contents):
Revision 1.3 by root, Fri Apr 19 19:46:29 2019 UTC vs.
Revision 1.13 by root, Sat Apr 20 13:46:14 2019 UTC

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
7enum { 10enum {
8 // ASN_TAG 11 // ASN_TAG
9 ASN_BOOLEAN = 0x01, 12 ASN_BOOLEAN = 0x01,
10 ASN_INTEGER32 = 0x02, 13 ASN_INTEGER32 = 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, //X 18 ASN_OID = 0x06,
16 ASN_OBJECT_DESCRIPTOR = 0x07, //X 19 ASN_OBJECT_DESCRIPTOR = 0x07,
17 ASN_EXTERNAL = 0x08, //X 20 ASN_EXTERNAL = 0x08,
18 ASN_REAL = 0x09, //X 21 ASN_REAL = 0x09,
19 ASN_ENUMERATED = 0x0a, //X 22 ASN_ENUMERATED = 0x0a,
20 ASN_EMBEDDED_PDV = 0x0b, //X 23 ASN_EMBEDDED_PDV = 0x0b,
21 ASN_UTF8_STRING = 0x0c, //X 24 ASN_UTF8_STRING = 0x0c,
22 ASN_RELATIVE_OID = 0x0d, //X 25 ASN_RELATIVE_OID = 0x0d,
23 ASN_SEQUENCE = 0x10, 26 ASN_SEQUENCE = 0x10,
24 ASN_SET = 0x11, //X 27 ASN_SET = 0x11,
25 ASN_NUMERIC_STRING = 0x12, //X 28 ASN_NUMERIC_STRING = 0x12,
26 ASN_PRINTABLE_STRING = 0x13, //X 29 ASN_PRINTABLE_STRING = 0x13,
27 ASN_TELETEX_STRING = 0x14, //X 30 ASN_TELETEX_STRING = 0x14,
28 ASN_T61_STRING = 0x14, //X 31 ASN_T61_STRING = 0x14,
29 ASN_VIDEOTEX_STRING = 0x15, //X 32 ASN_VIDEOTEX_STRING = 0x15,
30 ASN_IA5_STRING = 0x16, //X 33 ASN_IA5_STRING = 0x16,
31 ASN_ASCII_STRING = 0x16, //X 34 ASN_ASCII_STRING = 0x16,
32 ASN_UTC_TIME = 0x17, //X 35 ASN_UTC_TIME = 0x17,
33 ASN_GENERALIZED_TIME = 0x18, //X 36 ASN_GENERALIZED_TIME = 0x18,
34 ASN_GRAPHIC_STRING = 0x19, //X 37 ASN_GRAPHIC_STRING = 0x19,
35 ASN_VISIBLE_STRING = 0x1a, //X 38 ASN_VISIBLE_STRING = 0x1a,
36 ASN_ISO646_STRING = 0x1a, //X 39 ASN_ISO646_STRING = 0x1a,
37 ASN_GENERAL_STRING = 0x1b, //X 40 ASN_GENERAL_STRING = 0x1b,
38 ASN_UNIVERSAL_STRING = 0x1c, //X 41 ASN_UNIVERSAL_STRING = 0x1c,
39 ASN_CHARACTER_STRING = 0x1d, //X 42 ASN_CHARACTER_STRING = 0x1d,
40 ASN_BMPSTRING = 0x1e, //X 43 ASN_BMP_STRING = 0x1e,
41 44
42 ASN_TAG_BER = 0x1f, 45 ASN_TAG_BER = 0x1f,
43 ASN_TAG_MASK = 0x1f, 46 ASN_TAG_MASK = 0x1f,
44 47
45 // primitive/constructed 48 // primitive/constructed
46 ASN_CONSTRUCTED = 0x20, 49 ASN_CONSTRUCTED = 0x20,
47 50
48 // ASN_CLASS 51 // ASN_CLASS
49 ASN_UNIVERSAL = 0x00, 52 ASN_UNIVERSAL = 0x00,
50 ASN_APPLICATION = 0x40, 53 ASN_APPLICATION = 0x01,
51 ASN_CONTEXT = 0x80, 54 ASN_CONTEXT = 0x02,
52 ASN_PRIVATE = 0xc0, 55 ASN_PRIVATE = 0x03,
53 56
54 ASN_CLASS_MASK = 0xc0, 57 ASN_CLASS_MASK = 0xc0,
55 ASN_CLASS_SHIFT = 6, 58 ASN_CLASS_SHIFT = 6,
56 59
57 // ASN_APPLICATION SNMP 60 // ASN_APPLICATION SNMP
62 SNMP_OPAQUE = 0x04, 65 SNMP_OPAQUE = 0x04,
63 SNMP_COUNTER64 = 0x06, 66 SNMP_COUNTER64 = 0x06,
64}; 67};
65 68
66enum { 69enum {
70 BER_TYPE_BYTES,
71 BER_TYPE_UTF8,
72 BER_TYPE_UCS2,
73 BER_TYPE_UCS4,
74 BER_TYPE_INT,
75 BER_TYPE_OID,
76 BER_TYPE_RELOID,
77 BER_TYPE_NULL,
78 BER_TYPE_BOOL,
79 BER_TYPE_REAL,
80 BER_TYPE_IPADDRESS,
81 BER_TYPE_CROAK,
82};
83
84enum {
67 BER_CLASS = 0, 85 BER_CLASS = 0,
68 BER_TAG = 1, 86 BER_TAG = 1,
69 BER_CONSTRUCTED = 2, 87 BER_CONSTRUCTED = 2,
70 BER_DATA = 3, 88 BER_DATA = 3,
71 BER_ARRAYSIZE 89 BER_ARRAYSIZE
72}; 90};
73 91
74#define MAX_OID_STRLEN 4096 92#define MAX_OID_STRLEN 4096
75 93
94typedef void profile_type;
95
96static profile_type *cur_profile, *default_profile;
76static SV *buf_sv; // encoding buffer 97static SV *buf_sv; // encoding buffer
77static U8 *buf, *cur, *end; // buffer start, current, end 98static U8 *buf, *cur, *end; // buffer start, current, end
99
100#if PERL_VERSION < 18
101# define utf8_to_uvchr_buf(s,e,l) utf8_to_uvchr (s, l)
102#endif
78 103
79#if __GNUC__ >= 3 104#if __GNUC__ >= 3
80# define expect(expr,value) __builtin_expect ((expr), (value)) 105# define expect(expr,value) __builtin_expect ((expr), (value))
81# define INLINE static inline 106# define INLINE static inline
82#else 107#else
85#endif 110#endif
86 111
87#define expect_false(expr) expect ((expr) != 0, 0) 112#define expect_false(expr) expect ((expr) != 0, 0)
88#define expect_true(expr) expect ((expr) != 0, 1) 113#define expect_true(expr) expect ((expr) != 0, 1)
89 114
115/////////////////////////////////////////////////////////////////////////////
116
117static SV *sviv_cache[32];
118
90// for "small" integers, return a readonly sv, otherwise create a new one 119// for "small" integers, return a readonly sv, otherwise create a new one
91static SV *newSVcacheint (int val) 120static SV *newSVcacheint (int val)
92{ 121{
93 static SV *cache[32];
94
95 if (expect_false (val < 0 || val >= sizeof (cache))) 122 if (expect_false (val < 0 || val >= sizeof (sviv_cache)))
96 return newSViv (val); 123 return newSViv (val);
97 124
98 if (expect_false (!cache [val])) 125 if (expect_false (!sviv_cache [val]))
99 { 126 {
100 cache [val] = newSVuv (val); 127 sviv_cache [val] = newSVuv (val);
101 SvREADONLY_on (cache [val]); 128 SvREADONLY_on (sviv_cache [val]);
102 } 129 }
103 130
104 return SvREFCNT_inc_NN (cache [val]); 131 return SvREFCNT_inc_NN (sviv_cache [val]);
132}
133
134/////////////////////////////////////////////////////////////////////////////
135
136static HV *profile_stash;
137
138static profile_type *
139SvPROFILE (SV *profile)
140{
141 if (!SvOK (profile))
142 return default_profile;
143
144 if (!SvROK (profile))
145 croak ("invalid profile");
146
147 profile = SvRV (profile);
148
149 if (SvSTASH (profile) != profile_stash)
150 croak ("invalid profile object");
151
152 return (void *)profile;
153}
154
155static int
156profile_lookup (profile_type *profile, int klass, int tag)
157{
158 SV *sv = (SV *)profile;
159 U32 idx = (tag << 2) + klass;
160
161 if (expect_false (idx >= SvCUR (sv)))
162 return BER_TYPE_BYTES;
163
164 return SvPVX (sv)[idx];
165}
166
167static void
168profile_set (profile_type *profile, int klass, int tag, int type)
169{
170 SV *sv = (SV *)profile;
171 U32 idx = (tag << 2) + klass;
172 STRLEN oldlen = SvCUR (sv);
173 STRLEN newlen = idx + 2;
174
175 if (idx >= oldlen)
176 {
177 sv_grow (sv, newlen);
178 memset (SvPVX (sv) + oldlen, BER_TYPE_BYTES, newlen - oldlen);
179 SvCUR_set (sv, newlen);
180 }
181
182 SvPVX (sv)[idx] = type;
183}
184
185static SV *
186profile_new ()
187{
188 SV *sv = newSVpvn ("", 0);
189
190 static const struct {
191 int klass;
192 int tag;
193 int type;
194 } *celem, default_map[] = {
195 { ASN_UNIVERSAL, ASN_BOOLEAN , BER_TYPE_BOOL },
196 { ASN_UNIVERSAL, ASN_INTEGER32 , BER_TYPE_INT },
197 { ASN_UNIVERSAL, ASN_NULL , BER_TYPE_NULL },
198 { ASN_UNIVERSAL, ASN_OBJECT_IDENTIFIER, BER_TYPE_OID },
199 { ASN_UNIVERSAL, ASN_RELATIVE_OID , BER_TYPE_RELOID },
200 { ASN_UNIVERSAL, ASN_REAL , BER_TYPE_REAL },
201 { ASN_UNIVERSAL, ASN_ENUMERATED , BER_TYPE_INT },
202 { ASN_UNIVERSAL, ASN_UTF8_STRING , BER_TYPE_UTF8 },
203 { ASN_UNIVERSAL, ASN_BMP_STRING , BER_TYPE_UCS2 },
204 { ASN_UNIVERSAL, ASN_UNIVERSAL_STRING , BER_TYPE_UCS4 },
205 };
206
207 for (celem = default_map + sizeof (default_map) / sizeof (default_map [0]); celem-- > default_map; )
208 profile_set ((profile_type *)sv, celem->klass, celem->tag, celem->type);
209
210 return sv_bless (newRV_noinc (sv), profile_stash);
105} 211}
106 212
107///////////////////////////////////////////////////////////////////////////// 213/////////////////////////////////////////////////////////////////////////////
108// decoder 214// decoder
109 215
187 } 293 }
188 294
189 return res; 295 return res;
190} 296}
191 297
192static U32 298static SV *
193get_integer32 (void) 299decode_int ()
194{ 300{
195 U32 length = get_length (); 301 int len = get_length ();
196 302
197 if (length <= 0) 303 if (len <= 0)
198 { 304 {
199 error ("INTEGER32 length equal to zero"); 305 error ("integer length equal to zero");
200 return 0; 306 return 0;
201 } 307 }
202 308
203 U8 *data = get_n (length); 309 U8 *data = get_n (len);
204 310
205 if (length > 5 || (length > 4 && data [0])) 311 int negative = data [0] & 0x80;
206 {
207 error ("INTEGER32 length too long");
208 return 0;
209 }
210 312
211 U32 res = data [0] & 0x80 ? 0xffffffff : 0; 313 UV val = negative ? -1 : 0; // copy signbit to all bits
212 314
213 while (length--) 315 do
214 res = (res << 8) | *data++; 316 val = (val << 8) | *data++;
317 while (--len);
215 318
216 return res; 319 // the cast to IV relies on implementation-defined behaviour (two's complement cast)
320 // but that's ok, as perl relies on it as well.
321 return negative ? newSViv ((IV)val) : newSVuv (val);
217} 322}
218 323
219static SV * 324static SV *
220decode_integer32 (void) 325decode_data (void)
221{ 326{
222 return newSViv ((I32)get_integer32 ());
223}
224
225static SV *
226decode_unsigned32 (void)
227{
228 return newSVuv ((U32)get_integer32 ());
229}
230
231#if IVSIZE >= 8
232
233static U64TYPE
234get_integer64 (void)
235{
236 U32 length = get_length (); 327 U32 len = get_length ();
237
238 if (length <= 0)
239 {
240 error ("INTEGER64 length equal to zero");
241 return 0;
242 }
243
244 U8 *data = get_n (length); 328 U8 *data = get_n (len);
245
246 if (length > 9 || (length > 8 && data [0]))
247 {
248 error ("INTEGER64 length too long");
249 return 0;
250 }
251
252 U64TYPE res = data [0] & 0x80 ? 0xffffffffffffffff : 0;
253
254 while (length--)
255 res = (res << 8) | *data++;
256
257 return res;
258}
259
260static SV *
261decode_integer64 (void)
262{
263 return newSViv ((I64TYPE)get_integer64 ());
264}
265
266static SV *
267decode_unsigned64 (void)
268{
269 return newSVuv ((U64TYPE)get_integer64 ());
270}
271
272#endif
273
274static SV *
275decode_octet_string (void)
276{
277 U32 length = get_length ();
278 U8 *data = get_n (length);
279 return newSVpvn (data, length); 329 return newSVpvn ((char *)data, len);
280} 330}
281 331
282// gelper for decode_object_identifier 332// gelper for decode_object_identifier
283static char * 333static char *
284write_uv (char *buf, U32 u) 334write_uv (char *buf, U32 u)
285{ 335{
286 // the one-digit case is absolutely predominant, so this pays off (hopefully) 336 // the one-digit case is absolutely predominant, so this pays off (hopefully)
287 if (u < 10) 337 if (expect_true (u < 10))
288 *buf++ = u + '0'; 338 *buf++ = u + '0';
289 else 339 else
290 { 340 {
341 // this *could* be done much faster using branchless fixed-ppint arithmetics
291 char *beg = buf; 342 char *beg = buf;
292 343
293 do 344 do
294 { 345 {
295 *buf++ = u % 10 + '0'; 346 *buf++ = u % 10 + '0';
296 u /= 10; 347 u /= 10;
297 } 348 }
298 while (u); 349 while (u);
299 350
300 // reverse digits 351 // reverse digits
301 for (char *ptr = buf; --ptr != beg; ++beg) 352 char *ptr = buf;
353 while (--ptr > beg)
302 { 354 {
303 char c = *ptr; 355 char c = *ptr;
304 *ptr = *beg; 356 *ptr = *beg;
305 *beg = c; 357 *beg = c;
358 ++beg;
306 } 359 }
307 } 360 }
308 361
309 return buf; 362 return buf;
310} 363}
311 364
312static SV * 365static SV *
313decode_object_identifier (void) 366decode_oid (int relative)
314{ 367{
315 U32 length = get_length (); 368 U32 len = get_length ();
316 369
317 if (length <= 0) 370 if (len <= 0)
318 { 371 {
319 error ("OBJECT IDENTIFIER length equal to zero"); 372 error ("OBJECT IDENTIFIER length equal to zero");
320 return &PL_sv_undef; 373 return &PL_sv_undef;
321 } 374 }
322 375
323 U8 *end = cur + length; 376 U8 *end = cur + len;
324 U32 w = get_w (); 377 U32 w = get_w ();
325 378
326 static char oid[MAX_OID_STRLEN]; // must be static 379 static char oid[MAX_OID_STRLEN]; // static, becaueds too large for stack
327 char *app = oid; 380 char *app = oid;
328 381
382 if (relative)
383 app = write_uv (app, w);
384 else
385 {
329 app = write_uv (app, (U8)w / 40); 386 app = write_uv (app, (U8)w / 40);
330 *app++ = '.'; 387 *app++ = '.';
331 app = write_uv (app, (U8)w % 40); 388 app = write_uv (app, (U8)w % 40);
389 }
332 390
391 while (cur < end)
392 {
333 // we assume an oid component is never > 64 bytes 393 // we assume an oid component is never > 64 digits
334 while (cur < end && oid + sizeof (oid) - app > 64) 394 if (oid + sizeof (oid) - app < 64)
335 { 395 croak ("BER_TYPE_OID to long to decode");
396
336 w = get_w (); 397 w = get_w ();
337 *app++ = '.'; 398 *app++ = '.';
338 app = write_uv (app, w); 399 app = write_uv (app, w);
339 } 400 }
340 401
341 return newSVpvn (oid, app - oid); 402 return newSVpvn (oid, app - oid);
342} 403}
343 404
405// TODO: this is unacceptably slow
406static SV *
407decode_ucs (int chrsize)
408{
409 SV *res = NEWSV (0, 0);
410
411 U32 len = get_length ();
412
413 if (len & (chrsize - 1))
414 croak ("BER_TYPE_UCS has an invalid number of octets (%d)", len);
415
416 while (len)
417 {
418 U8 b1 = get_u8 ();
419 U8 b2 = get_u8 ();
420 U32 chr = (b1 << 8) | b2;
421
422 if (chrsize == 4)
423 {
424 U8 b3 = get_u8 ();
425 U8 b4 = get_u8 ();
426 chr = (chr << 16) | (b3 << 8) | b4;
427 }
428
429 U8 uchr [UTF8_MAXBYTES];
430 int uclen = uvuni_to_utf8 (uchr, chr) - uchr;
431
432 sv_catpvn (res, (const char *)uchr, uclen);
433 len -= chrsize;
434 }
435
436 SvUTF8_on (res);
437
438 return res;
439}
440
344static SV * 441static SV *
345decode_ber () 442decode_ber ()
346{ 443{
347 int identifier = get_u8 (); 444 int identifier = get_u8 ();
348 445
349 SV *res; 446 SV *res;
350 447
351 int constructed = identifier & ASN_CONSTRUCTED; 448 int constructed = identifier & ASN_CONSTRUCTED;
352 int klass = identifier & ASN_CLASS_MASK; 449 int klass = (identifier & ASN_CLASS_MASK) >> ASN_CLASS_SHIFT;
353 int tag = identifier & ASN_TAG_MASK; 450 int tag = identifier & ASN_TAG_MASK;
354 451
355 if (tag == ASN_TAG_BER) 452 if (tag == ASN_TAG_BER)
356 tag = get_w (); 453 tag = get_w ();
357 454
358 if (tag == ASN_TAG_BER) 455 if (tag == ASN_TAG_BER)
371 croak ("constructed type %02x overflow (%x %x)\n", identifier, cur - buf, seqend); 468 croak ("constructed type %02x overflow (%x %x)\n", identifier, cur - buf, seqend);
372 469
373 res = newRV_inc ((SV *)av); 470 res = newRV_inc ((SV *)av);
374 } 471 }
375 else 472 else
376 switch (identifier) 473 switch (profile_lookup (cur_profile, klass, tag))
377 { 474 {
378 case ASN_NULL: 475 case BER_TYPE_NULL:
476 {
477 U32 len = get_length ();
478
479 if (len)
480 croak ("BER_TYPE_NULL value with non-zero length %d encountered", len);
481
379 res = &PL_sv_undef; 482 res = &PL_sv_undef;
483 }
380 break; 484 break;
381 485
382 case ASN_OBJECT_IDENTIFIER: 486 case BER_TYPE_BOOL:
487 {
488 U32 len = get_length ();
489
490 if (len != 1)
491 croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered", len);
492
493 res = newSVcacheint (!!get_u8 ());
494 }
495 break;
496
497 case BER_TYPE_OID:
383 res = decode_object_identifier (); 498 res = decode_oid (0);
384 break; 499 break;
385 500
386 case ASN_INTEGER32: 501 case BER_TYPE_RELOID:
502 res = decode_oid (1);
503 break;
504
505 case BER_TYPE_INT:
387 res = decode_integer32 (); 506 res = decode_int ();
388 break; 507 break;
389 508
390 case ASN_APPLICATION | SNMP_UNSIGNED32: 509 case BER_TYPE_UTF8:
391 case ASN_APPLICATION | SNMP_COUNTER32: 510 res = decode_data ();
392 case ASN_APPLICATION | SNMP_TIMETICKS: 511 SvUTF8_on (res);
512 break;
513
514 case BER_TYPE_BYTES:
515 res = decode_data ();
516 break;
517
518 case BER_TYPE_IPADDRESS:
519 {
520 U32 len = get_length ();
521
522 if (len != 4)
523 croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered", len);
524
525 U8 c1 = get_u8 ();
526 U8 c2 = get_u8 ();
527 U8 c3 = get_u8 ();
528 U8 c4 = get_u8 ();
529
530 res = newSVpvf ("%d.%d.%d.%d", c1, c2, c3, c4);
531 }
532 break;
533
534 case BER_TYPE_UCS2:
535 res = decode_ucs (2);
536 break;
537
538 case BER_TYPE_UCS4:
393 res = decode_unsigned32 (); 539 res = decode_ucs (4);
394 break; 540 break;
395 541
396#if 0 // handled by default case 542 case BER_TYPE_REAL:
397 case ASN_OCTET_STRING: 543 case BER_TYPE_CROAK:
398 case ASN_APPLICATION | ASN_IPADDRESS:
399 case ASN_APPLICATION | ASN_OPAQUE:
400 res = decode_octet_string ();
401 break;
402#endif
403
404 case ASN_APPLICATION | SNMP_COUNTER64:
405 res = decode_integer64 ();
406 break;
407
408 default: 544 default:
409 res = decode_octet_string (); 545 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
410 break;
411 } 546 }
412 547
413 AV *av = newAV (); 548 AV *av = newAV ();
414 av_fill (av, BER_ARRAYSIZE - 1); 549 av_fill (av, BER_ARRAYSIZE - 1);
415 AvARRAY (av)[BER_CLASS ] = newSVcacheint (klass >> ASN_CLASS_SHIFT); 550 AvARRAY (av)[BER_CLASS ] = newSVcacheint (klass);
416 AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); 551 AvARRAY (av)[BER_TAG ] = newSVcacheint (tag);
417 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (constructed ? 1 : 0); 552 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (constructed ? 1 : 0);
418 AvARRAY (av)[BER_DATA ] = res; 553 AvARRAY (av)[BER_DATA ] = res;
419 554
420 return newRV_noinc ((SV *)av); 555 return newRV_noinc ((SV *)av);
438static void 573static void
439set_buf (SV *sv) 574set_buf (SV *sv)
440{ 575{
441 STRLEN len; 576 STRLEN len;
442 buf_sv = sv; 577 buf_sv = sv;
443 buf = SvPVbyte (buf_sv, len); 578 buf = (U8 *)SvPVbyte (buf_sv, len);
444 cur = buf; 579 cur = buf;
445 end = buf + len; 580 end = buf + len;
446} 581}
447 582
448/* similar to SvGROW, but somewhat safer and guarantees exponential realloc strategy */ 583/* similar to SvGROW, but somewhat safer and guarantees exponential realloc strategy */
462need (STRLEN len) 597need (STRLEN len)
463{ 598{
464 if (expect_false ((uintptr_t)(end - cur) < len)) 599 if (expect_false ((uintptr_t)(end - cur) < len))
465 { 600 {
466 STRLEN pos = cur - buf; 601 STRLEN pos = cur - buf;
467 buf = my_sv_grow (buf_sv, pos, len); 602 buf = (U8 *)my_sv_grow (buf_sv, pos, len);
468 cur = buf + pos; 603 cur = buf + pos;
469 end = buf + SvLEN (buf_sv) - 1; 604 end = buf + SvLEN (buf_sv) - 1;
470 } 605 }
471} 606}
472 607
516} 651}
517 652
518static void 653static void
519put_length (U32 val) 654put_length (U32 val)
520{ 655{
521 need (5); 656 need (5 + val);
522 cur = put_length_at (val, cur); 657 cur = put_length_at (val, cur);
523} 658}
524 659
525// return how many bytes the encoded length requires 660// return how many bytes the encoded length requires
526static int length_length (U32 val) 661static int length_length (U32 val)
529 ? 1 664 ? 1
530 : 2 + (val > 0xffU) + (val > 0xffffU) + (val > 0xffffffU); 665 : 2 + (val > 0xffU) + (val > 0xffffU) + (val > 0xffffffU);
531} 666}
532 667
533static void 668static void
534encode_octet_string (SV *sv) 669encode_data (const char *ptr, STRLEN len)
535{ 670{
536 STRLEN len;
537 char *ptr = SvPVbyte (sv, len);
538
539 put_length (len); 671 put_length (len);
540 need (len);
541 memcpy (cur, ptr, len); 672 memcpy (cur, ptr, len);
542 cur += len; 673 cur += len;
543} 674}
544 675
545static void 676static void
546encode_integer32 (IV iv) 677encode_uv (UV uv)
547{ 678{
548 need (5); 679}
680
681static void
682encode_int (SV *sv)
683{
684 need (8 + 1 + 1); // 64 bit + length + extra 0
685
686 if (expect_false (!SvIOK (sv)))
687 sv_2iv_flags (sv, 0);
549 688
550 U8 *lenb = cur++; 689 U8 *lenb = cur++;
551 690
552 if (iv < 0) 691 if (SvIOK_notUV (sv))
553 { 692 {
693 IV iv = SvIVX (sv);
694
695 if (expect_false (iv < 0))
696 {
554 // get two's complement bit pattern - works even on hypthetical non-2c machines 697 // get two's complement bit pattern - works even on hypothetical non-2c machines
555 U32 uv = iv; 698 UV uv = iv;
556 699
700#if UVSIZE > 4
701 *cur = uv >> 56; cur += !!(~uv & 0xff80000000000000U);
702 *cur = uv >> 48; cur += !!(~uv & 0xffff800000000000U);
703 *cur = uv >> 40; cur += !!(~uv & 0xffffff8000000000U);
704 *cur = uv >> 32; cur += !!(~uv & 0xffffffff80000000U);
705#endif
557 *cur = uv >> 24; cur += !!(~uv & 0xff800000U); 706 *cur = uv >> 24; cur += !!(~uv & 0xffffffffff800000U);
558 *cur = uv >> 16; cur += !!(~uv & 0xffff8000U); 707 *cur = uv >> 16; cur += !!(~uv & 0xffffffffffff8000U);
559 *cur = uv >> 8; cur += !!(~uv & 0xffffff80U); 708 *cur = uv >> 8; cur += !!(~uv & 0xffffffffffffff80U);
560 *cur = uv ; cur += 1; 709 *cur = uv ; cur += 1;
561 }
562 else
563 {
564 *cur = iv >> 24; cur += *cur > 0;
565 *cur = iv >> 16; cur += *cur > 0;
566 *cur = iv >> 8; cur += *cur > 0;
567 *cur = iv ; cur += 1;
568 }
569 710
570 *lenb = cur - lenb - 1; 711 *lenb = cur - lenb - 1;
571}
572 712
573static void 713 return;
574encode_unsigned64 (U64TYPE uv) 714 }
575{ 715 }
576 need (9);
577 716
578 U8 *lenb = cur++; 717 UV uv = SvUV (sv);
579 718
580 *cur = uv >> 56; cur += *cur > 0; 719 // prepend an extra 0 if the high bit is 1
581 *cur = uv >> 48; cur += *cur > 0; 720 *cur = 0; cur += !!(uv & ((UV)1 << (UVSIZE * 8 - 1)));
582 *cur = uv >> 40; cur += *cur > 0; 721
583 *cur = uv >> 32; cur += *cur > 0; 722#if UVSIZE > 4
584 *cur = uv >> 24; cur += *cur > 0; 723 *cur = uv >> 56; cur += !!(uv & 0xff80000000000000U);
585 *cur = uv >> 16; cur += *cur > 0; 724 *cur = uv >> 48; cur += !!(uv & 0xffff800000000000U);
586 *cur = uv >> 8; cur += *cur > 0; 725 *cur = uv >> 40; cur += !!(uv & 0xffffff8000000000U);
726 *cur = uv >> 32; cur += !!(uv & 0xffffffff80000000U);
727#endif
728 *cur = uv >> 24; cur += !!(uv & 0xffffffffff800000U);
729 *cur = uv >> 16; cur += !!(uv & 0xffffffffffff8000U);
730 *cur = uv >> 8; cur += !!(uv & 0xffffffffffffff80U);
587 *cur = uv ; cur += 1; 731 *cur = uv ; cur += 1;
588 732
589 *lenb = cur - lenb - 1; 733 *lenb = cur - lenb - 1;
590} 734}
591 735
631 775
632 return str; 776 return str;
633} 777}
634 778
635static void 779static void
636encode_object_identifier (SV *oid) 780encode_oid (SV *oid, int relative)
637{ 781{
638 STRLEN slen; 782 STRLEN len;
639 char *ptr = SvPV (oid, slen); // utf8 vs. bytes does not matter 783 char *ptr = SvPV (oid, len); // utf8 vs. bytes does not matter
640 784
641 // we need at most as many octets as the string form 785 // we need at most as many octets as the string form
642 need (slen + 1); 786 need (len + 1);
643 STRLEN mark = len_fixup_mark (); 787 STRLEN mark = len_fixup_mark ();
644 788
645 UV w1, w2; 789 UV w1, w2;
646 790
791 if (!relative)
792 {
647 ptr = read_uv (ptr, &w1); 793 ptr = read_uv (ptr, &w1);
648 ptr = read_uv (ptr, &w2); 794 ptr = read_uv (ptr, &w2);
649 795
650 put_w_nocheck (w1 * 40 + w2); 796 put_w_nocheck (w1 * 40 + w2);
797 }
651 798
652 while (*ptr) 799 while (*ptr)
653 { 800 {
654 ptr = read_uv (ptr, &w1); 801 ptr = read_uv (ptr, &w1);
655 put_w_nocheck (w1); 802 put_w_nocheck (w1);
656 } 803 }
657 804
658 len_fixup (mark); 805 len_fixup (mark);
659} 806}
660 807
808// check whether an SV is a BER tuple and returns its AV *
809static AV *
810ber_tuple (SV *tuple)
811{
812 SV *rv;
813
814 if (expect_false (!SvROK (tuple) || SvTYPE ((rv = SvRV (tuple))) != SVt_PVAV))
815 croak ("BER tuple must be array-reference");
816
817 if (expect_false (SvRMAGICAL (rv)))
818 croak ("BER tuple must not be tied");
819
820 if (expect_false (AvFILL ((AV *)rv) != BER_ARRAYSIZE - 1))
821 croak ("BER tuple must contain exactly %d elements, not %d", BER_ARRAYSIZE, AvFILL ((AV *)rv) + 1);
822
823 return (AV *)rv;
824}
825
826static void
827encode_ucs (SV *data, int chrsize)
828{
829 STRLEN uchars = sv_len_utf8 (data);
830 STRLEN len;;
831 char *ptr = SvPVutf8 (data, len);
832
833 put_length (uchars * chrsize);
834
835 while (uchars--)
836 {
837 STRLEN uclen;
838 UV uchr = utf8_to_uvchr_buf ((U8 *)ptr, (U8 *)ptr + len, &uclen);
839
840 ptr += uclen;
841 len -= uclen;
842
843 if (chrsize == 4)
844 {
845 *cur++ = uchr >> 24;
846 *cur++ = uchr >> 16;
847 }
848
849 *cur++ = uchr >> 8;
850 *cur++ = uchr;
851 }
852}
661static void 853static void
662encode_ber (SV *tuple) 854encode_ber (SV *tuple)
663{ 855{
664 if (expect_false (!SvROK (tuple) || SvTYPE (SvRV (tuple)) != SVt_PVAV)) 856 AV *av = ber_tuple (tuple);
665 croak ("BER tuple must be array-reference");
666
667 AV *av = (AV *)SvRV (tuple);
668
669 if (expect_false (SvRMAGICAL (av)))
670 croak ("BER tuple must not be tied");
671
672 if (expect_false (AvFILL (av) != BER_ARRAYSIZE - 1))
673 croak ("BER tuple must contain exactly %d elements, not %d", BER_ARRAYSIZE, AvFILL (av) + 1);
674 857
675 int klass = SvIV (AvARRAY (av)[BER_CLASS]); 858 int klass = SvIV (AvARRAY (av)[BER_CLASS]);
676 int tag = SvIV (AvARRAY (av)[BER_TAG]); 859 int tag = SvIV (AvARRAY (av)[BER_TAG]);
677 int constructed = SvIV (AvARRAY (av)[BER_CONSTRUCTED]) ? ASN_CONSTRUCTED : 0; 860 int constructed = SvIV (AvARRAY (av)[BER_CONSTRUCTED]) ? ASN_CONSTRUCTED : 0;
678 SV *data = AvARRAY (av)[BER_DATA]; 861 SV *data = AvARRAY (av)[BER_DATA];
701 int fill = AvFILL (av); 884 int fill = AvFILL (av);
702 885
703 if (expect_false (SvRMAGICAL (av))) 886 if (expect_false (SvRMAGICAL (av)))
704 croak ("BER constructed data must not be tied"); 887 croak ("BER constructed data must not be tied");
705 888
889 int i;
706 for (int i = 0; i <= fill; ++i) 890 for (i = 0; i <= fill; ++i)
707 encode_ber (AvARRAY (av)[i]); 891 encode_ber (AvARRAY (av)[i]);
708 892
709 len_fixup (mark); 893 len_fixup (mark);
710 } 894 }
711 else 895 else
712 switch (identifier | tag) 896 switch (profile_lookup (cur_profile, klass, tag))
713 { 897 {
714 case ASN_NULL: 898 case BER_TYPE_NULL:
715 put_length (0); 899 put_length (0);
716 break; 900 break;
717 901
718 case ASN_OBJECT_IDENTIFIER: 902 case BER_TYPE_BOOL:
903 put_length (1);
904 *cur++ = SvTRUE (data) ? 0xff : 0x00;
905 break;
906
907 case BER_TYPE_OID:
719 encode_object_identifier (data); 908 encode_oid (data, 0);
720 break; 909 break;
721 910
722 case ASN_INTEGER32: 911 case BER_TYPE_RELOID:
912 encode_oid (data, 1);
913 break;
914
915 case BER_TYPE_INT:
723 encode_integer32 (SvIV (data)); 916 encode_int (data);
724 break; 917 break;
725 918
726 case ASN_APPLICATION | SNMP_UNSIGNED32: 919 case BER_TYPE_BYTES:
727 case ASN_APPLICATION | SNMP_COUNTER32: 920 {
728 case ASN_APPLICATION | SNMP_TIMETICKS: 921 STRLEN len;
729 case ASN_APPLICATION | SNMP_COUNTER64: 922 const char *ptr = SvPVbyte (data, len);
730 encode_unsigned64 (SvUV (data)); 923 encode_data (ptr, len);
924 }
731 break; 925 break;
732 926
927 case BER_TYPE_UTF8:
928 {
929 STRLEN len;
930 const char *ptr = SvPVutf8 (data, len);
931 encode_data (ptr, len);
932 }
933 break;
934
935 case BER_TYPE_IPADDRESS:
936 {
937 U8 ip[4];
938 sscanf (SvPV_nolen (data), "%hhu.%hhu.%hhu.%hhu", ip + 0, ip + 1, ip + 2, ip + 3);
939 encode_data ((const char *)ip, sizeof (ip));
940 }
941 break;
942
943 case BER_TYPE_UCS2:
944 encode_ucs (data, 2);
945 break;
946
947 case BER_TYPE_UCS4:
948 encode_ucs (data, 4);
949 break;
950
951 case BER_TYPE_REAL:
952 case BER_TYPE_CROAK:
733 default: 953 default:
734 encode_octet_string (data); 954 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
735 break;
736 } 955 }
737 956
738} 957}
739 958
740///////////////////////////////////////////////////////////////////////////// 959/////////////////////////////////////////////////////////////////////////////
744PROTOTYPES: ENABLE 963PROTOTYPES: ENABLE
745 964
746BOOT: 965BOOT:
747{ 966{
748 HV *stash = gv_stashpv ("Convert::BER::XS", 1); 967 HV *stash = gv_stashpv ("Convert::BER::XS", 1);
968
969 profile_stash = gv_stashpv ("Convert::BER::XS::Profile", 1);
749 970
750 static const struct { 971 static const struct {
751 const char *name; 972 const char *name;
752 IV iv; 973 IV iv;
753 } *civ, const_iv[] = { 974 } *civ, const_iv[] = {
754 { "ASN_BOOLEAN", ASN_BOOLEAN }, 975#define const_iv(name) { # name, name },
755 { "ASN_INTEGER32", ASN_INTEGER32 }, 976 const_iv (ASN_BOOLEAN)
756 { "ASN_BIT_STRING", ASN_BIT_STRING }, 977 const_iv (ASN_INTEGER32)
757 { "ASN_OCTET_STRING", ASN_OCTET_STRING }, 978 const_iv (ASN_BIT_STRING)
758 { "ASN_NULL", ASN_NULL }, 979 const_iv (ASN_OCTET_STRING)
759 { "ASN_OBJECT_IDENTIFIER", ASN_OBJECT_IDENTIFIER }, 980 const_iv (ASN_NULL)
760 { "ASN_TAG_BER", ASN_TAG_BER }, 981 const_iv (ASN_OBJECT_IDENTIFIER)
761 { "ASN_TAG_MASK", ASN_TAG_MASK }, 982 const_iv (ASN_OBJECT_DESCRIPTOR)
762 { "ASN_CONSTRUCTED", ASN_CONSTRUCTED }, 983 const_iv (ASN_OID)
763 { "ASN_UNIVERSAL", ASN_UNIVERSAL >> ASN_CLASS_SHIFT }, 984 const_iv (ASN_EXTERNAL)
764 { "ASN_APPLICATION", ASN_APPLICATION >> ASN_CLASS_SHIFT }, 985 const_iv (ASN_REAL)
765 { "ASN_CONTEXT", ASN_CONTEXT >> ASN_CLASS_SHIFT }, 986 const_iv (ASN_SEQUENCE)
766 { "ASN_PRIVATE", ASN_PRIVATE >> ASN_CLASS_SHIFT }, 987 const_iv (ASN_ENUMERATED)
767 { "ASN_CLASS_MASK", ASN_CLASS_MASK }, 988 const_iv (ASN_EMBEDDED_PDV)
768 { "ASN_CLASS_SHIFT", ASN_CLASS_SHIFT }, 989 const_iv (ASN_UTF8_STRING)
769 { "ASN_SEQUENCE", ASN_SEQUENCE }, 990 const_iv (ASN_RELATIVE_OID)
770 { "SNMP_IPADDRESS", SNMP_IPADDRESS }, 991 const_iv (ASN_SET)
771 { "SNMP_COUNTER32", SNMP_COUNTER32 }, 992 const_iv (ASN_NUMERIC_STRING)
772 { "SNMP_UNSIGNED32", SNMP_UNSIGNED32 }, 993 const_iv (ASN_PRINTABLE_STRING)
773 { "SNMP_TIMETICKS", SNMP_TIMETICKS }, 994 const_iv (ASN_TELETEX_STRING)
774 { "SNMP_OPAQUE", SNMP_OPAQUE }, 995 const_iv (ASN_T61_STRING)
775 { "SNMP_COUNTER64", SNMP_COUNTER64 }, 996 const_iv (ASN_VIDEOTEX_STRING)
997 const_iv (ASN_IA5_STRING)
998 const_iv (ASN_ASCII_STRING)
999 const_iv (ASN_UTC_TIME)
1000 const_iv (ASN_GENERALIZED_TIME)
1001 const_iv (ASN_GRAPHIC_STRING)
1002 const_iv (ASN_VISIBLE_STRING)
1003 const_iv (ASN_ISO646_STRING)
1004 const_iv (ASN_GENERAL_STRING)
1005 const_iv (ASN_UNIVERSAL_STRING)
1006 const_iv (ASN_CHARACTER_STRING)
1007 const_iv (ASN_BMP_STRING)
776 1008
777 { "BER_CLASS" , BER_CLASS }, 1009 const_iv (ASN_UNIVERSAL)
778 { "BER_TAG" , BER_TAG }, 1010 const_iv (ASN_APPLICATION)
779 { "BER_CONSTRUCTED", BER_CONSTRUCTED }, 1011 const_iv (ASN_CONTEXT)
780 { "BER_DATA" , BER_DATA }, 1012 const_iv (ASN_PRIVATE)
1013
1014 const_iv (BER_CLASS)
1015 const_iv (BER_TAG)
1016 const_iv (BER_CONSTRUCTED)
1017 const_iv (BER_DATA)
1018
1019 const_iv (BER_TYPE_BYTES)
1020 const_iv (BER_TYPE_UTF8)
1021 const_iv (BER_TYPE_UCS2)
1022 const_iv (BER_TYPE_UCS4)
1023 const_iv (BER_TYPE_INT)
1024 const_iv (BER_TYPE_OID)
1025 const_iv (BER_TYPE_RELOID)
1026 const_iv (BER_TYPE_NULL)
1027 const_iv (BER_TYPE_BOOL)
1028 const_iv (BER_TYPE_REAL)
1029 const_iv (BER_TYPE_IPADDRESS)
1030 const_iv (BER_TYPE_CROAK)
1031
1032 const_iv (SNMP_IPADDRESS)
1033 const_iv (SNMP_COUNTER32)
1034 const_iv (SNMP_UNSIGNED32)
1035 const_iv (SNMP_TIMETICKS)
1036 const_iv (SNMP_OPAQUE)
1037 const_iv (SNMP_COUNTER64)
781 }; 1038 };
782 1039
783 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) 1040 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--)
784 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); 1041 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv));
785} 1042}
786 1043
787SV * 1044SV *
788ber_decode (SV *ber) 1045ber_decode (SV *ber, SV *profile = &PL_sv_undef)
789 CODE: 1046 CODE:
790{ 1047{
1048 cur_profile = SvPROFILE (profile);
791 STRLEN len; 1049 STRLEN len;
792
793 buf = SvPVbyte (ber, len); 1050 buf = (U8 *)SvPVbyte (ber, len);
794 cur = buf; 1051 cur = buf;
795 end = buf + len; 1052 end = buf + len;
796 1053
797 RETVAL = decode_ber (); 1054 RETVAL = decode_ber ();
798} 1055}
799 OUTPUT: RETVAL 1056 OUTPUT: RETVAL
800 1057
801void 1058void
802ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *constructed = &PL_sv_undef, SV *data = &PL_sv_undef) 1059ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *constructed = &PL_sv_undef, SV *data = &PL_sv_undef)
803 PROTOTYPE: $;$$$
804 PPCODE: 1060 PPCODE:
805{ 1061{
806 if (!SvOK (tuple)) 1062 if (!SvOK (tuple))
807 XSRETURN_NO; 1063 XSRETURN_NO;
808 1064
809 if (!SvROK (tuple) || SvTYPE (SvRV (tuple)) != SVt_PVAV) 1065 if (!SvROK (tuple) || SvTYPE (SvRV (tuple)) != SVt_PVAV)
810 croak ("ber_seq: tuple must be ber tuple (array-ref)"); 1066 croak ("ber_is: tuple must be BER tuple (array-ref)");
811 1067
812 AV *av = (AV *)SvRV (tuple); 1068 AV *av = (AV *)SvRV (tuple);
813 1069
814 XPUSHs ( 1070 XPUSHs (
815 (!SvOK (klass) || SvIV (AvARRAY (av)[BER_CLASS ]) == SvIV (klass)) 1071 (!SvOK (klass) || SvIV (AvARRAY (av)[BER_CLASS ]) == SvIV (klass))
816 && (!SvOK (tag) || SvIV (AvARRAY (av)[BER_TAG ]) == SvIV (tag)) 1072 && (!SvOK (tag) || SvIV (AvARRAY (av)[BER_TAG ]) == SvIV (tag))
817 && (!SvOK (constructed) || !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) == !SvIV (constructed)) 1073 && (!SvOK (constructed) || !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) == !SvIV (constructed))
818 && (!SvOK (data) || sv_eq (AvARRAY (av)[BER_DATA ], data)) 1074 && (!SvOK (data) || sv_eq (AvARRAY (av)[BER_DATA ], data))
819 ? &PL_sv_yes : &PL_sv_no); 1075 ? &PL_sv_yes : &PL_sv_undef);
820} 1076}
821 1077
822void 1078void
823ber_is_seq (SV *tuple) 1079ber_is_seq (SV *tuple)
824 PPCODE: 1080 PPCODE:
825{ 1081{
826 if (!SvOK (tuple)) 1082 if (!SvOK (tuple))
827 XSRETURN_UNDEF; 1083 XSRETURN_UNDEF;
828 1084
829 if (!SvROK (tuple) || SvTYPE (SvRV (tuple)) != SVt_PVAV) 1085 AV *av = ber_tuple (tuple);
830 croak ("ber_seq: tuple must be ber tuple (array-ref)");
831
832 AV *av = (AV *)SvRV (tuple);
833 1086
834 XPUSHs ( 1087 XPUSHs (
835 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL 1088 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL
836 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_SEQUENCE 1089 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_SEQUENCE
837 && SvIV (AvARRAY (av)[BER_CONSTRUCTED]) 1090 && SvIV (AvARRAY (av)[BER_CONSTRUCTED])
838 ? AvARRAY (av)[BER_DATA] : &PL_sv_undef); 1091 ? AvARRAY (av)[BER_DATA] : &PL_sv_undef);
839} 1092}
840 1093
841void 1094void
842ber_is_i32 (SV *tuple, IV value) 1095ber_is_i32 (SV *tuple, SV *value = &PL_sv_undef)
843 PPCODE: 1096 PPCODE:
844{ 1097{
845 if (!SvOK (tuple)) 1098 if (!SvOK (tuple))
846 XSRETURN_NO; 1099 XSRETURN_NO;
847 1100
848 if (!SvROK (tuple) || SvTYPE (SvRV (tuple)) != SVt_PVAV) 1101 AV *av = ber_tuple (tuple);
849 croak ("ber_seq: tuple must be ber tuple (array-ref)");
850 1102
851 AV *av = (AV *)SvRV (tuple); 1103 IV data = SvIV (AvARRAY (av)[BER_DATA]);
852 1104
853 XPUSHs ( 1105 XPUSHs (
854 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL 1106 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL
855 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_INTEGER32 1107 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_INTEGER32
856 && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) 1108 && !SvIV (AvARRAY (av)[BER_CONSTRUCTED])
857 && SvIV (AvARRAY (av)[BER_DATA ]) == value 1109 && (!SvOK (value) || data == SvIV (value))
858 ? &PL_sv_yes : &PL_sv_no); 1110 ? sv_2mortal (data ? newSViv (data) : newSVpv ("0 but true", 0))
1111 : &PL_sv_undef);
859} 1112}
860 1113
861void 1114void
862ber_is_oid (SV *tuple, SV *oid) 1115ber_is_oid (SV *tuple, SV *oid = &PL_sv_undef)
863 PPCODE: 1116 PPCODE:
864{ 1117{
865 if (!SvOK (tuple)) 1118 if (!SvOK (tuple))
866 XSRETURN_NO; 1119 XSRETURN_NO;
867 1120
868 if (!SvROK (tuple) || SvTYPE (SvRV (tuple)) != SVt_PVAV) 1121 AV *av = ber_tuple (tuple);
869 croak ("ber_seq: tuple must be ber tuple (array-ref)");
870
871 AV *av = (AV *)SvRV (tuple);
872 1122
873 XPUSHs ( 1123 XPUSHs (
874 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL 1124 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL
875 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_OBJECT_IDENTIFIER 1125 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_OBJECT_IDENTIFIER
876 && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) 1126 && !SvIV (AvARRAY (av)[BER_CONSTRUCTED])
877 && sv_eq (AvARRAY (av)[BER_DATA], oid) 1127 && (!SvOK (oid) || sv_eq (AvARRAY (av)[BER_DATA], oid))
878 ? &PL_sv_yes : &PL_sv_no); 1128 ? newSVsv (AvARRAY (av)[BER_DATA]) : &PL_sv_undef);
879} 1129}
880 1130
881############################################################################# 1131#############################################################################
882 1132
883void 1133void
884ber_encode (SV *tuple) 1134ber_encode (SV *tuple, SV *profile = &PL_sv_undef)
885 PPCODE: 1135 PPCODE:
886{ 1136{
1137 cur_profile = SvPROFILE (profile);
887 buf_sv = sv_2mortal (NEWSV (0, 256)); 1138 buf_sv = sv_2mortal (NEWSV (0, 256));
888 SvPOK_only (buf_sv); 1139 SvPOK_only (buf_sv);
889 set_buf (buf_sv); 1140 set_buf (buf_sv);
890 1141
891 encode_ber (tuple); 1142 encode_ber (tuple);
892 1143
893 SvCUR_set (buf_sv, cur - buf); 1144 SvCUR_set (buf_sv, cur - buf);
894 XPUSHs (buf_sv); 1145 XPUSHs (buf_sv);
895} 1146}
896 1147
1148SV *
1149ber_i32 (IV iv)
1150 CODE:
1151{
1152 AV *av = newAV ();
1153 av_fill (av, BER_ARRAYSIZE - 1);
1154 AvARRAY (av)[BER_CLASS ] = newSVcacheint (ASN_UNIVERSAL);
1155 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_INTEGER32);
1156 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (0);
1157 AvARRAY (av)[BER_DATA ] = newSViv (iv);
1158 RETVAL = newRV_noinc ((SV *)av);
1159}
1160 OUTPUT: RETVAL
1161
1162# TODO: not arrayref, but elements?
1163SV *
1164ber_seq (SV *arrayref)
1165 CODE:
1166{
1167 AV *av = newAV ();
1168 av_fill (av, BER_ARRAYSIZE - 1);
1169 AvARRAY (av)[BER_CLASS ] = newSVcacheint (ASN_UNIVERSAL);
1170 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_SEQUENCE);
1171 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (1);
1172 AvARRAY (av)[BER_DATA ] = newSVsv (arrayref);
1173 RETVAL = newRV_noinc ((SV *)av);
1174}
1175 OUTPUT: RETVAL
1176
1177MODULE = Convert::BER::XS PACKAGE = Convert::BER::XS::Profile
1178
1179SV *
1180new (SV *klass)
1181 CODE:
1182 RETVAL = profile_new ();
1183 OUTPUT: RETVAL
1184
1185void
1186set (SV *profile, int klass, int tag, int type)
1187 CODE:
1188 profile_set (SvPROFILE (profile), klass, tag, type);
1189
1190IV
1191get (SV *profile, int klass, int tag)
1192 CODE:
1193 RETVAL = profile_lookup (SvPROFILE (profile), klass, tag);
1194 OUTPUT: RETVAL
1195
1196void
1197_set_default (SV *profile)
1198 CODE:
1199 default_profile = SvPROFILE (profile);
1200
1201

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines