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.6 by root, Sat Apr 20 01:03:59 2019 UTC

35 ASN_VISIBLE_STRING = 0x1a, //X 35 ASN_VISIBLE_STRING = 0x1a, //X
36 ASN_ISO646_STRING = 0x1a, //X 36 ASN_ISO646_STRING = 0x1a, //X
37 ASN_GENERAL_STRING = 0x1b, //X 37 ASN_GENERAL_STRING = 0x1b, //X
38 ASN_UNIVERSAL_STRING = 0x1c, //X 38 ASN_UNIVERSAL_STRING = 0x1c, //X
39 ASN_CHARACTER_STRING = 0x1d, //X 39 ASN_CHARACTER_STRING = 0x1d, //X
40 ASN_BMPSTRING = 0x1e, //X 40 ASN_BMP_STRING = 0x1e, //X
41 41
42 ASN_TAG_BER = 0x1f, 42 ASN_TAG_BER = 0x1f,
43 ASN_TAG_MASK = 0x1f, 43 ASN_TAG_MASK = 0x1f,
44 44
45 // primitive/constructed 45 // primitive/constructed
46 ASN_CONSTRUCTED = 0x20, 46 ASN_CONSTRUCTED = 0x20,
47 47
48 // ASN_CLASS 48 // ASN_CLASS
49 ASN_UNIVERSAL = 0x00, 49 ASN_UNIVERSAL = 0x00,
50 ASN_APPLICATION = 0x40, 50 ASN_APPLICATION = 0x01,
51 ASN_CONTEXT = 0x80, 51 ASN_CONTEXT = 0x02,
52 ASN_PRIVATE = 0xc0, 52 ASN_PRIVATE = 0x03,
53 53
54 ASN_CLASS_MASK = 0xc0, 54 ASN_CLASS_MASK = 0xc0,
55 ASN_CLASS_SHIFT = 6, 55 ASN_CLASS_SHIFT = 6,
56 56
57 // ASN_APPLICATION SNMP 57 // ASN_APPLICATION SNMP
62 SNMP_OPAQUE = 0x04, 62 SNMP_OPAQUE = 0x04,
63 SNMP_COUNTER64 = 0x06, 63 SNMP_COUNTER64 = 0x06,
64}; 64};
65 65
66enum { 66enum {
67 BER_TYPE_BYTES,
68 BER_TYPE_UTF8,
69 BER_TYPE_UCS2,
70 BER_TYPE_UCS4,
71 BER_TYPE_INT,
72 BER_TYPE_OID,
73 BER_TYPE_RELOID,
74 BER_TYPE_NULL,
75 BER_TYPE_BOOL,
76 BER_TYPE_REAL,
77 BER_TYPE_IPADDRESS,
78 BER_TYPE_CROAK,
79};
80
81enum {
67 BER_CLASS = 0, 82 BER_CLASS = 0,
68 BER_TAG = 1, 83 BER_TAG = 1,
69 BER_CONSTRUCTED = 2, 84 BER_CONSTRUCTED = 2,
70 BER_DATA = 3, 85 BER_DATA = 3,
71 BER_ARRAYSIZE 86 BER_ARRAYSIZE
72}; 87};
73 88
74#define MAX_OID_STRLEN 4096 89#define MAX_OID_STRLEN 4096
75 90
91typedef void profile_type;
92
93static profile_type *cur_profile, *default_profile;
76static SV *buf_sv; // encoding buffer 94static SV *buf_sv; // encoding buffer
77static U8 *buf, *cur, *end; // buffer start, current, end 95static U8 *buf, *cur, *end; // buffer start, current, end
78 96
79#if __GNUC__ >= 3 97#if __GNUC__ >= 3
80# define expect(expr,value) __builtin_expect ((expr), (value)) 98# define expect(expr,value) __builtin_expect ((expr), (value))
85#endif 103#endif
86 104
87#define expect_false(expr) expect ((expr) != 0, 0) 105#define expect_false(expr) expect ((expr) != 0, 0)
88#define expect_true(expr) expect ((expr) != 0, 1) 106#define expect_true(expr) expect ((expr) != 0, 1)
89 107
108/////////////////////////////////////////////////////////////////////////////
109
110static SV *sviv_cache[32];
111
90// for "small" integers, return a readonly sv, otherwise create a new one 112// for "small" integers, return a readonly sv, otherwise create a new one
91static SV *newSVcacheint (int val) 113static SV *newSVcacheint (int val)
92{ 114{
93 static SV *cache[32];
94
95 if (expect_false (val < 0 || val >= sizeof (cache))) 115 if (expect_false (val < 0 || val >= sizeof (sviv_cache)))
96 return newSViv (val); 116 return newSViv (val);
97 117
98 if (expect_false (!cache [val])) 118 if (expect_false (!sviv_cache [val]))
99 { 119 {
100 cache [val] = newSVuv (val); 120 sviv_cache [val] = newSVuv (val);
101 SvREADONLY_on (cache [val]); 121 SvREADONLY_on (sviv_cache [val]);
102 } 122 }
103 123
104 return SvREFCNT_inc_NN (cache [val]); 124 return SvREFCNT_inc_NN (sviv_cache [val]);
125}
126
127/////////////////////////////////////////////////////////////////////////////
128
129static HV *profile_stash;
130
131static profile_type *
132SvPROFILE (SV *profile)
133{
134 if (!SvOK (profile))
135 return default_profile;
136
137 if (!SvROK (profile))
138 croak ("invalid profile");
139
140 profile = SvRV (profile);
141
142 if (SvSTASH (profile) != profile_stash)
143 croak ("invalid profile object");
144
145 return (void *)profile;
146}
147
148static int
149profile_lookup (profile_type *profile, int klass, int tag)
150{
151 SV *sv = (SV *)profile;
152 U32 idx = (tag << 2) + klass;
153
154 if (expect_false (idx >= SvCUR (sv)))
155 return BER_TYPE_BYTES;
156
157 return SvPVX (sv)[idx];
158}
159
160static int
161profile_set (profile_type *profile, int klass, int tag, int type)
162{
163 SV *sv = (SV *)profile;
164 U32 idx = (tag << 2) + klass;
165 STRLEN oldlen = SvCUR (sv);
166 STRLEN newlen = idx + 2;
167
168 if (idx >= oldlen)
169 {
170 sv_grow (sv, newlen);
171 memset (SvPVX (sv) + oldlen, BER_TYPE_BYTES, newlen - oldlen);
172 SvCUR_set (sv, newlen);
173 }
174
175 SvPVX (sv)[idx] = type;
176}
177
178static SV *
179profile_new ()
180{
181 SV *sv = newSVpvn ("", 0);
182
183 static const struct {
184 int klass;
185 int tag;
186 int type;
187 } *celem, default_map[] = {
188 { ASN_UNIVERSAL, ASN_BOOLEAN , BER_TYPE_BOOL },
189 { ASN_UNIVERSAL, ASN_INTEGER32 , BER_TYPE_INT },
190 { ASN_UNIVERSAL, ASN_NULL , BER_TYPE_NULL },
191 { 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 },
194 { ASN_UNIVERSAL, ASN_REAL , BER_TYPE_REAL },
195 { ASN_UNIVERSAL, ASN_UTF8_STRING , BER_TYPE_UTF8 },
196 { ASN_UNIVERSAL, ASN_BMP_STRING , BER_TYPE_UCS2 },
197 { ASN_UNIVERSAL, ASN_UNIVERSAL_STRING , BER_TYPE_UCS4 },
198 };
199
200 for (celem = default_map + sizeof (default_map) / sizeof (default_map [0]); celem > default_map; celem--)
201 profile_set ((void *)sv, celem->klass, celem->tag, celem->type);
202
203 return sv_bless (newRV_noinc (sv), profile_stash);
105} 204}
106 205
107///////////////////////////////////////////////////////////////////////////// 206/////////////////////////////////////////////////////////////////////////////
108// decoder 207// decoder
109 208
187 } 286 }
188 287
189 return res; 288 return res;
190} 289}
191 290
192static U32 291static SV *
193get_integer32 (void) 292decode_int ()
194{ 293{
195 U32 length = get_length (); 294 int len = get_length ();
196 295
197 if (length <= 0) 296 if (len <= 0)
198 { 297 {
199 error ("INTEGER32 length equal to zero"); 298 error ("integer length equal to zero");
200 return 0; 299 return 0;
201 } 300 }
202 301
203 U8 *data = get_n (length); 302 U8 *data = get_n (len);
204 303
205 if (length > 5 || (length > 4 && data [0])) 304 int negative = data [0] & 0x80;
206 {
207 error ("INTEGER32 length too long");
208 return 0;
209 }
210 305
211 U32 res = data [0] & 0x80 ? 0xffffffff : 0; 306 UV val = negative ? -1 : 0; // copy signbit to all bits
212 307
213 while (length--) 308 do
214 res = (res << 8) | *data++; 309 val = (val << 8) | *data++;
310 while (--len);
215 311
216 return res; 312 // the cast to IV relies on implementation-defined behaviour (two's complement cast)
313 // but that's ok, as perl relies on it as well.
314 return negative ? newSViv ((IV)val) : newSVuv (val);
217} 315}
218 316
219static SV * 317static SV *
220decode_integer32 (void) 318decode_data (void)
221{ 319{
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 (); 320 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); 321 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); 322 return newSVpvn ((char *)data, len);
280} 323}
281 324
282// gelper for decode_object_identifier 325// gelper for decode_object_identifier
283static char * 326static char *
284write_uv (char *buf, U32 u) 327write_uv (char *buf, U32 u)
285{ 328{
286 // the one-digit case is absolutely predominant, so this pays off (hopefully) 329 // the one-digit case is absolutely predominant, so this pays off (hopefully)
287 if (u < 10) 330 if (expect_true (u < 10))
288 *buf++ = u + '0'; 331 *buf++ = u + '0';
289 else 332 else
290 { 333 {
291 char *beg = buf; 334 char *beg = buf;
292 335
308 351
309 return buf; 352 return buf;
310} 353}
311 354
312static SV * 355static SV *
313decode_object_identifier (void) 356decode_oid (int relative)
314{ 357{
315 U32 length = get_length (); 358 U32 len = get_length ();
316 359
317 if (length <= 0) 360 if (len <= 0)
318 { 361 {
319 error ("OBJECT IDENTIFIER length equal to zero"); 362 error ("OBJECT IDENTIFIER length equal to zero");
320 return &PL_sv_undef; 363 return &PL_sv_undef;
321 } 364 }
322 365
323 U8 *end = cur + length; 366 U8 *end = cur + len;
324 U32 w = get_w (); 367 U32 w = get_w ();
325 368
326 static char oid[MAX_OID_STRLEN]; // must be static 369 static char oid[MAX_OID_STRLEN]; // must be static
327 char *app = oid; 370 char *app = oid;
328 371
372 if (relative)
373 app = write_uv (app, w);
374 else
375 {
329 app = write_uv (app, (U8)w / 40); 376 app = write_uv (app, (U8)w / 40);
330 *app++ = '.'; 377 *app++ = '.';
331 app = write_uv (app, (U8)w % 40); 378 app = write_uv (app, (U8)w % 40);
379 }
332 380
333 // we assume an oid component is never > 64 bytes 381 // we assume an oid component is never > 64 bytes
334 while (cur < end && oid + sizeof (oid) - app > 64) 382 while (cur < end && oid + sizeof (oid) - app > 64)
335 { 383 {
336 w = get_w (); 384 w = get_w ();
346{ 394{
347 int identifier = get_u8 (); 395 int identifier = get_u8 ();
348 396
349 SV *res; 397 SV *res;
350 398
351 int constructed = identifier & ASN_CONSTRUCTED; 399 int constructed = identifier & ASN_CONSTRUCTED;
352 int klass = identifier & ASN_CLASS_MASK; 400 int klass = (identifier & ASN_CLASS_MASK) >> ASN_CLASS_SHIFT;
353 int tag = identifier & ASN_TAG_MASK; 401 int tag = identifier & ASN_TAG_MASK;
354 402
355 if (tag == ASN_TAG_BER) 403 if (tag == ASN_TAG_BER)
356 tag = get_w (); 404 tag = get_w ();
357 405
358 if (tag == ASN_TAG_BER) 406 if (tag == ASN_TAG_BER)
371 croak ("constructed type %02x overflow (%x %x)\n", identifier, cur - buf, seqend); 419 croak ("constructed type %02x overflow (%x %x)\n", identifier, cur - buf, seqend);
372 420
373 res = newRV_inc ((SV *)av); 421 res = newRV_inc ((SV *)av);
374 } 422 }
375 else 423 else
376 switch (identifier) 424 switch (profile_lookup (cur_profile, klass, tag))
377 { 425 {
378 case ASN_NULL: 426 case BER_TYPE_NULL:
379 res = &PL_sv_undef; 427 res = &PL_sv_undef;
380 break; 428 break;
381 429
382 case ASN_OBJECT_IDENTIFIER: 430 case BER_TYPE_BOOL:
431 {
432 U32 len = get_length ();
433
434 if (len != 1)
435 croak ("BER_TYPE_BOOLEAN type with invalid length %d encountered", len);
436
437 res = newSVcacheint (get_u8 () ? 0 : 1);
438 }
439 break;
440
441 case BER_TYPE_OID:
383 res = decode_object_identifier (); 442 res = decode_oid (0);
384 break; 443 break;
385 444
386 case ASN_INTEGER32: 445 case BER_TYPE_RELOID:
446 res = decode_oid (1);
447 break;
448
449 case BER_TYPE_INT:
387 res = decode_integer32 (); 450 res = decode_int ();
388 break; 451 break;
389 452
390 case ASN_APPLICATION | SNMP_UNSIGNED32: 453 case BER_TYPE_UTF8:
391 case ASN_APPLICATION | SNMP_COUNTER32:
392 case ASN_APPLICATION | SNMP_TIMETICKS:
393 res = decode_unsigned32 ();
394 break;
395
396#if 0 // handled by default case
397 case ASN_OCTET_STRING:
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 (); 454 res = decode_data ();
455 SvUTF8_on (res);
406 break; 456 break;
407 457
458 case BER_TYPE_BYTES:
459 res = decode_data ();
460 break;
461
462 case BER_TYPE_IPADDRESS:
463 {
464 U32 len = get_length ();
465
466 if (len != 4)
467 croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered", len);
468
469 U8 c1 = get_u8 ();
470 U8 c2 = get_u8 ();
471 U8 c3 = get_u8 ();
472 U8 c4 = get_u8 ();
473
474 res = newSVpvf ("%d.%d.%d.%d", c1, c2, c3, c4);
475 }
476 break;
477
478 case BER_TYPE_REAL:
479 case BER_TYPE_UCS2:
480 case BER_TYPE_UCS4:
481 case BER_TYPE_CROAK:
408 default: 482 default:
409 res = decode_octet_string (); 483 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
410 break;
411 } 484 }
412 485
413 AV *av = newAV (); 486 AV *av = newAV ();
414 av_fill (av, BER_ARRAYSIZE - 1); 487 av_fill (av, BER_ARRAYSIZE - 1);
415 AvARRAY (av)[BER_CLASS ] = newSVcacheint (klass >> ASN_CLASS_SHIFT); 488 AvARRAY (av)[BER_CLASS ] = newSVcacheint (klass);
416 AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); 489 AvARRAY (av)[BER_TAG ] = newSVcacheint (tag);
417 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (constructed ? 1 : 0); 490 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (constructed ? 1 : 0);
418 AvARRAY (av)[BER_DATA ] = res; 491 AvARRAY (av)[BER_DATA ] = res;
419 492
420 return newRV_noinc ((SV *)av); 493 return newRV_noinc ((SV *)av);
529 ? 1 602 ? 1
530 : 2 + (val > 0xffU) + (val > 0xffffU) + (val > 0xffffffU); 603 : 2 + (val > 0xffU) + (val > 0xffffU) + (val > 0xffffffU);
531} 604}
532 605
533static void 606static void
534encode_octet_string (SV *sv) 607encode_data (const char *ptr, STRLEN len)
535{ 608{
536 STRLEN len;
537 char *ptr = SvPVbyte (sv, len);
538
539 put_length (len); 609 put_length (len);
540 need (len); 610 need (len);
541 memcpy (cur, ptr, len); 611 memcpy (cur, ptr, len);
542 cur += len; 612 cur += len;
543} 613}
544 614
545static void 615static void
546encode_integer32 (IV iv) 616encode_uv (UV uv)
547{ 617{
548 need (5); 618}
619
620static void
621encode_int (SV *sv)
622{
623 need (8 + 1 + 1); // 64 bit + length + extra 0
624
625 if (expect_false (!SvIOK (sv)))
626 sv_2iv_flags (sv, 0);
549 627
550 U8 *lenb = cur++; 628 U8 *lenb = cur++;
551 629
552 if (iv < 0) 630 if (SvIOK_notUV (sv))
553 { 631 {
632 IV iv = SvIVX (sv);
633
634 if (expect_false (iv < 0))
635 {
554 // get two's complement bit pattern - works even on hypthetical non-2c machines 636 // get two's complement bit pattern - works even on hypothetical non-2c machines
555 U32 uv = iv; 637 UV uv = iv;
556 638
639#if UVSIZE > 4
640 *cur = uv >> 56; cur += !!(~uv & 0xff80000000000000U);
641 *cur = uv >> 48; cur += !!(~uv & 0xffff800000000000U);
642 *cur = uv >> 40; cur += !!(~uv & 0xffffff8000000000U);
643 *cur = uv >> 32; cur += !!(~uv & 0xffffffff80000000U);
644#endif
557 *cur = uv >> 24; cur += !!(~uv & 0xff800000U); 645 *cur = uv >> 24; cur += !!(~uv & 0xffffffffff800000U);
558 *cur = uv >> 16; cur += !!(~uv & 0xffff8000U); 646 *cur = uv >> 16; cur += !!(~uv & 0xffffffffffff8000U);
559 *cur = uv >> 8; cur += !!(~uv & 0xffffff80U); 647 *cur = uv >> 8; cur += !!(~uv & 0xffffffffffffff80U);
560 *cur = uv ; cur += 1; 648 *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 649
570 *lenb = cur - lenb - 1; 650 *lenb = cur - lenb - 1;
571}
572 651
573static void 652 return;
574encode_unsigned64 (U64TYPE uv) 653 }
575{ 654 }
576 need (9);
577 655
578 U8 *lenb = cur++; 656 UV uv = SvUV (sv);
579 657
580 *cur = uv >> 56; cur += *cur > 0; 658 // prepend an extra 0 if the high bit is 1
581 *cur = uv >> 48; cur += *cur > 0; 659 *cur = 0; cur += !!(uv & ((UV)1 << (UVSIZE * 8 - 1)));
582 *cur = uv >> 40; cur += *cur > 0; 660
583 *cur = uv >> 32; cur += *cur > 0; 661#if UVSIZE > 4
584 *cur = uv >> 24; cur += *cur > 0; 662 *cur = uv >> 56; cur += !!(uv & 0xff80000000000000U);
585 *cur = uv >> 16; cur += *cur > 0; 663 *cur = uv >> 48; cur += !!(uv & 0xffff800000000000U);
586 *cur = uv >> 8; cur += *cur > 0; 664 *cur = uv >> 40; cur += !!(uv & 0xffffff8000000000U);
665 *cur = uv >> 32; cur += !!(uv & 0xffffffff80000000U);
666#endif
667 *cur = uv >> 24; cur += !!(uv & 0xffffffffff800000U);
668 *cur = uv >> 16; cur += !!(uv & 0xffffffffffff8000U);
669 *cur = uv >> 8; cur += !!(uv & 0xffffffffffffff80U);
587 *cur = uv ; cur += 1; 670 *cur = uv ; cur += 1;
588 671
589 *lenb = cur - lenb - 1; 672 *lenb = cur - lenb - 1;
590} 673}
591 674
631 714
632 return str; 715 return str;
633} 716}
634 717
635static void 718static void
636encode_object_identifier (SV *oid) 719encode_oid (SV *oid, int relative)
637{ 720{
638 STRLEN slen; 721 STRLEN len;
639 char *ptr = SvPV (oid, slen); // utf8 vs. bytes does not matter 722 char *ptr = SvPV (oid, len); // utf8 vs. bytes does not matter
640 723
641 // we need at most as many octets as the string form 724 // we need at most as many octets as the string form
642 need (slen + 1); 725 need (len + 1);
643 STRLEN mark = len_fixup_mark (); 726 STRLEN mark = len_fixup_mark ();
644 727
645 UV w1, w2; 728 UV w1, w2;
646 729
730 if (!relative)
731 {
647 ptr = read_uv (ptr, &w1); 732 ptr = read_uv (ptr, &w1);
648 ptr = read_uv (ptr, &w2); 733 ptr = read_uv (ptr, &w2);
649 734
650 put_w_nocheck (w1 * 40 + w2); 735 put_w_nocheck (w1 * 40 + w2);
736 }
651 737
652 while (*ptr) 738 while (*ptr)
653 { 739 {
654 ptr = read_uv (ptr, &w1); 740 ptr = read_uv (ptr, &w1);
655 put_w_nocheck (w1); 741 put_w_nocheck (w1);
656 } 742 }
657 743
658 len_fixup (mark); 744 len_fixup (mark);
659} 745}
660 746
747// check whether an SV is a BER tuple and returns its AV *
748static AV *
749ber_tuple (SV *tuple)
750{
751 SV *rv;
752
753 if (expect_false (!SvROK (tuple) || SvTYPE ((rv = SvRV (tuple))) != SVt_PVAV))
754 croak ("BER tuple must be array-reference");
755
756 if (expect_false (SvRMAGICAL (rv)))
757 croak ("BER tuple must not be tied");
758
759 if (expect_false (AvFILL ((AV *)rv) != BER_ARRAYSIZE - 1))
760 croak ("BER tuple must contain exactly %d elements, not %d", BER_ARRAYSIZE, AvFILL ((AV *)rv) + 1);
761
762 return (AV *)rv;
763}
764
661static void 765static void
662encode_ber (SV *tuple) 766encode_ber (SV *tuple)
663{ 767{
664 if (expect_false (!SvROK (tuple) || SvTYPE (SvRV (tuple)) != SVt_PVAV)) 768 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 769
675 int klass = SvIV (AvARRAY (av)[BER_CLASS]); 770 int klass = SvIV (AvARRAY (av)[BER_CLASS]);
676 int tag = SvIV (AvARRAY (av)[BER_TAG]); 771 int tag = SvIV (AvARRAY (av)[BER_TAG]);
677 int constructed = SvIV (AvARRAY (av)[BER_CONSTRUCTED]) ? ASN_CONSTRUCTED : 0; 772 int constructed = SvIV (AvARRAY (av)[BER_CONSTRUCTED]) ? ASN_CONSTRUCTED : 0;
678 SV *data = AvARRAY (av)[BER_DATA]; 773 SV *data = AvARRAY (av)[BER_DATA];
707 encode_ber (AvARRAY (av)[i]); 802 encode_ber (AvARRAY (av)[i]);
708 803
709 len_fixup (mark); 804 len_fixup (mark);
710 } 805 }
711 else 806 else
712 switch (identifier | tag) 807 switch (profile_lookup (cur_profile, klass, tag))
713 { 808 {
714 case ASN_NULL: 809 case BER_TYPE_NULL:
715 put_length (0); 810 put_length (0);
716 break; 811 break;
717 812
718 case ASN_OBJECT_IDENTIFIER: 813 case BER_TYPE_BOOL:
814 put_length (1);
815 put_u8 (SvTRUE (data) ? 0xff : 0x00);
816 break;
817
818 case BER_TYPE_OID:
719 encode_object_identifier (data); 819 encode_oid (data, 0);
720 break; 820 break;
721 821
722 case ASN_INTEGER32: 822 case BER_TYPE_RELOID:
823 encode_oid (data, 1);
824 break;
825
826 case BER_TYPE_INT:
723 encode_integer32 (SvIV (data)); 827 encode_int (data);
724 break; 828 break;
725 829
726 case ASN_APPLICATION | SNMP_UNSIGNED32: 830 case BER_TYPE_BYTES:
727 case ASN_APPLICATION | SNMP_COUNTER32: 831 {
728 case ASN_APPLICATION | SNMP_TIMETICKS: 832 STRLEN len;
729 case ASN_APPLICATION | SNMP_COUNTER64: 833 const char *ptr = SvPVbyte (data, len);
730 encode_unsigned64 (SvUV (data)); 834 encode_data (ptr, len);
835 }
731 break; 836 break;
732 837
838 case BER_TYPE_UTF8:
839 {
840 STRLEN len;
841 const char *ptr = SvPVutf8 (data, len);
842 encode_data (ptr, len);
843 }
844 break;
845
846 case BER_TYPE_IPADDRESS:
847 {
848 U8 ip[4];
849 sscanf (SvPV_nolen (data), "%hhu.%hhu.%hhu.%hhu", ip + 0, ip + 1, ip + 2, ip + 3);
850 encode_data ((const char *)ip, sizeof (ip));
851 }
852 break;
853
854 case BER_TYPE_REAL:
855 case BER_TYPE_UCS2:
856 case BER_TYPE_UCS4:
857 case BER_TYPE_CROAK:
733 default: 858 default:
734 encode_octet_string (data); 859 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
735 break;
736 } 860 }
737 861
738} 862}
739 863
740///////////////////////////////////////////////////////////////////////////// 864/////////////////////////////////////////////////////////////////////////////
744PROTOTYPES: ENABLE 868PROTOTYPES: ENABLE
745 869
746BOOT: 870BOOT:
747{ 871{
748 HV *stash = gv_stashpv ("Convert::BER::XS", 1); 872 HV *stash = gv_stashpv ("Convert::BER::XS", 1);
873
874 profile_stash = gv_stashpv ("Convert::BER::XS::Profile", 1);
749 875
750 static const struct { 876 static const struct {
751 const char *name; 877 const char *name;
752 IV iv; 878 IV iv;
753 } *civ, const_iv[] = { 879 } *civ, const_iv[] = {
754 { "ASN_BOOLEAN", ASN_BOOLEAN }, 880#define const_iv(name) { # name, name },
755 { "ASN_INTEGER32", ASN_INTEGER32 }, 881 const_iv (ASN_BOOLEAN)
756 { "ASN_BIT_STRING", ASN_BIT_STRING }, 882 const_iv (ASN_INTEGER32)
757 { "ASN_OCTET_STRING", ASN_OCTET_STRING }, 883 const_iv (ASN_BIT_STRING)
758 { "ASN_NULL", ASN_NULL }, 884 const_iv (ASN_OCTET_STRING)
759 { "ASN_OBJECT_IDENTIFIER", ASN_OBJECT_IDENTIFIER }, 885 const_iv (ASN_NULL)
760 { "ASN_TAG_BER", ASN_TAG_BER }, 886 const_iv (ASN_OBJECT_IDENTIFIER)
761 { "ASN_TAG_MASK", ASN_TAG_MASK }, 887 const_iv (ASN_OBJECT_DESCRIPTOR)
762 { "ASN_CONSTRUCTED", ASN_CONSTRUCTED }, 888 const_iv (ASN_OID)
763 { "ASN_UNIVERSAL", ASN_UNIVERSAL >> ASN_CLASS_SHIFT }, 889 const_iv (ASN_EXTERNAL)
764 { "ASN_APPLICATION", ASN_APPLICATION >> ASN_CLASS_SHIFT }, 890 const_iv (ASN_REAL)
765 { "ASN_CONTEXT", ASN_CONTEXT >> ASN_CLASS_SHIFT }, 891 const_iv (ASN_SEQUENCE)
766 { "ASN_PRIVATE", ASN_PRIVATE >> ASN_CLASS_SHIFT }, 892 const_iv (ASN_ENUMERATED)
767 { "ASN_CLASS_MASK", ASN_CLASS_MASK }, 893 const_iv (ASN_EMBEDDED_PDV)
768 { "ASN_CLASS_SHIFT", ASN_CLASS_SHIFT }, 894 const_iv (ASN_UTF8_STRING)
769 { "ASN_SEQUENCE", ASN_SEQUENCE }, 895 const_iv (ASN_RELATIVE_OID)
770 { "SNMP_IPADDRESS", SNMP_IPADDRESS }, 896 const_iv (ASN_SET)
771 { "SNMP_COUNTER32", SNMP_COUNTER32 }, 897 const_iv (ASN_NUMERIC_STRING)
772 { "SNMP_UNSIGNED32", SNMP_UNSIGNED32 }, 898 const_iv (ASN_PRINTABLE_STRING)
773 { "SNMP_TIMETICKS", SNMP_TIMETICKS }, 899 const_iv (ASN_TELETEX_STRING)
774 { "SNMP_OPAQUE", SNMP_OPAQUE }, 900 const_iv (ASN_T61_STRING)
775 { "SNMP_COUNTER64", SNMP_COUNTER64 }, 901 const_iv (ASN_VIDEOTEX_STRING)
902 const_iv (ASN_IA5_STRING)
903 const_iv (ASN_ASCII_STRING)
904 const_iv (ASN_UTC_TIME)
905 const_iv (ASN_GENERALIZED_TIME)
906 const_iv (ASN_GRAPHIC_STRING)
907 const_iv (ASN_VISIBLE_STRING)
908 const_iv (ASN_ISO646_STRING)
909 const_iv (ASN_GENERAL_STRING)
910 const_iv (ASN_UNIVERSAL_STRING)
911 const_iv (ASN_CHARACTER_STRING)
912 const_iv (ASN_BMP_STRING)
776 913
777 { "BER_CLASS" , BER_CLASS }, 914 const_iv (ASN_UNIVERSAL)
778 { "BER_TAG" , BER_TAG }, 915 const_iv (ASN_APPLICATION)
779 { "BER_CONSTRUCTED", BER_CONSTRUCTED }, 916 const_iv (ASN_CONTEXT)
780 { "BER_DATA" , BER_DATA }, 917 const_iv (ASN_PRIVATE)
918
919 const_iv (BER_CLASS)
920 const_iv (BER_TAG)
921 const_iv (BER_CONSTRUCTED)
922 const_iv (BER_DATA)
923
924 const_iv (BER_TYPE_BYTES)
925 const_iv (BER_TYPE_UTF8)
926 const_iv (BER_TYPE_UCS2)
927 const_iv (BER_TYPE_UCS4)
928 const_iv (BER_TYPE_INT)
929 const_iv (BER_TYPE_OID)
930 const_iv (BER_TYPE_RELOID)
931 const_iv (BER_TYPE_NULL)
932 const_iv (BER_TYPE_BOOL)
933 const_iv (BER_TYPE_REAL)
934 const_iv (BER_TYPE_IPADDRESS)
935 const_iv (BER_TYPE_CROAK)
936
937 const_iv (SNMP_IPADDRESS)
938 const_iv (SNMP_COUNTER32)
939 const_iv (SNMP_UNSIGNED32)
940 const_iv (SNMP_TIMETICKS)
941 const_iv (SNMP_OPAQUE)
942 const_iv (SNMP_COUNTER64)
781 }; 943 };
782 944
783 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) 945 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)); 946 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv));
785} 947}
786 948
787SV * 949SV *
788ber_decode (SV *ber) 950ber_decode (SV *ber, SV *profile = &PL_sv_undef)
789 CODE: 951 CODE:
790{ 952{
953 cur_profile = SvPROFILE (profile);
791 STRLEN len; 954 STRLEN len;
792
793 buf = SvPVbyte (ber, len); 955 buf = SvPVbyte (ber, len);
794 cur = buf; 956 cur = buf;
795 end = buf + len; 957 end = buf + len;
796 958
797 RETVAL = decode_ber (); 959 RETVAL = decode_ber ();
798} 960}
799 OUTPUT: RETVAL 961 OUTPUT: RETVAL
800 962
801void 963void
802ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *constructed = &PL_sv_undef, SV *data = &PL_sv_undef) 964ber_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: 965 PPCODE:
805{ 966{
806 if (!SvOK (tuple)) 967 if (!SvOK (tuple))
807 XSRETURN_NO; 968 XSRETURN_NO;
808 969
809 if (!SvROK (tuple) || SvTYPE (SvRV (tuple)) != SVt_PVAV) 970 if (!SvROK (tuple) || SvTYPE (SvRV (tuple)) != SVt_PVAV)
810 croak ("ber_seq: tuple must be ber tuple (array-ref)"); 971 croak ("ber_is: tuple must be BER tuple (array-ref)");
811 972
812 AV *av = (AV *)SvRV (tuple); 973 AV *av = (AV *)SvRV (tuple);
813 974
814 XPUSHs ( 975 XPUSHs (
815 (!SvOK (klass) || SvIV (AvARRAY (av)[BER_CLASS ]) == SvIV (klass)) 976 (!SvOK (klass) || SvIV (AvARRAY (av)[BER_CLASS ]) == SvIV (klass))
816 && (!SvOK (tag) || SvIV (AvARRAY (av)[BER_TAG ]) == SvIV (tag)) 977 && (!SvOK (tag) || SvIV (AvARRAY (av)[BER_TAG ]) == SvIV (tag))
817 && (!SvOK (constructed) || !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) == !SvIV (constructed)) 978 && (!SvOK (constructed) || !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) == !SvIV (constructed))
818 && (!SvOK (data) || sv_eq (AvARRAY (av)[BER_DATA ], data)) 979 && (!SvOK (data) || sv_eq (AvARRAY (av)[BER_DATA ], data))
819 ? &PL_sv_yes : &PL_sv_no); 980 ? &PL_sv_yes : &PL_sv_undef);
820} 981}
821 982
822void 983void
823ber_is_seq (SV *tuple) 984ber_is_seq (SV *tuple)
824 PPCODE: 985 PPCODE:
825{ 986{
826 if (!SvOK (tuple)) 987 if (!SvOK (tuple))
827 XSRETURN_UNDEF; 988 XSRETURN_UNDEF;
828 989
829 if (!SvROK (tuple) || SvTYPE (SvRV (tuple)) != SVt_PVAV) 990 AV *av = ber_tuple (tuple);
830 croak ("ber_seq: tuple must be ber tuple (array-ref)");
831
832 AV *av = (AV *)SvRV (tuple);
833 991
834 XPUSHs ( 992 XPUSHs (
835 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL 993 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL
836 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_SEQUENCE 994 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_SEQUENCE
837 && SvIV (AvARRAY (av)[BER_CONSTRUCTED]) 995 && SvIV (AvARRAY (av)[BER_CONSTRUCTED])
838 ? AvARRAY (av)[BER_DATA] : &PL_sv_undef); 996 ? AvARRAY (av)[BER_DATA] : &PL_sv_undef);
839} 997}
840 998
841void 999void
842ber_is_i32 (SV *tuple, IV value) 1000ber_is_i32 (SV *tuple, SV *value = &PL_sv_undef)
843 PPCODE: 1001 PPCODE:
844{ 1002{
845 if (!SvOK (tuple)) 1003 if (!SvOK (tuple))
846 XSRETURN_NO; 1004 XSRETURN_NO;
847 1005
848 if (!SvROK (tuple) || SvTYPE (SvRV (tuple)) != SVt_PVAV) 1006 AV *av = ber_tuple (tuple);
849 croak ("ber_seq: tuple must be ber tuple (array-ref)");
850 1007
851 AV *av = (AV *)SvRV (tuple); 1008 IV data = SvIV (AvARRAY (av)[BER_DATA]);
852 1009
853 XPUSHs ( 1010 XPUSHs (
854 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL 1011 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL
855 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_INTEGER32 1012 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_INTEGER32
856 && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) 1013 && !SvIV (AvARRAY (av)[BER_CONSTRUCTED])
857 && SvIV (AvARRAY (av)[BER_DATA ]) == value 1014 && (!SvOK (value) || data == SvIV (value))
858 ? &PL_sv_yes : &PL_sv_no); 1015 ? sv_2mortal (data ? newSViv (data) : newSVpv ("0 but true", 0))
1016 : &PL_sv_undef);
859} 1017}
860 1018
861void 1019void
862ber_is_oid (SV *tuple, SV *oid) 1020ber_is_oid (SV *tuple, SV *oid = &PL_sv_undef)
863 PPCODE: 1021 PPCODE:
864{ 1022{
865 if (!SvOK (tuple)) 1023 if (!SvOK (tuple))
866 XSRETURN_NO; 1024 XSRETURN_NO;
867 1025
868 if (!SvROK (tuple) || SvTYPE (SvRV (tuple)) != SVt_PVAV) 1026 AV *av = ber_tuple (tuple);
869 croak ("ber_seq: tuple must be ber tuple (array-ref)");
870
871 AV *av = (AV *)SvRV (tuple);
872 1027
873 XPUSHs ( 1028 XPUSHs (
874 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL 1029 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL
875 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_OBJECT_IDENTIFIER 1030 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_OBJECT_IDENTIFIER
876 && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) 1031 && !SvIV (AvARRAY (av)[BER_CONSTRUCTED])
877 && sv_eq (AvARRAY (av)[BER_DATA], oid) 1032 && (!SvOK (oid) || sv_eq (AvARRAY (av)[BER_DATA], oid))
878 ? &PL_sv_yes : &PL_sv_no); 1033 ? newSVsv (AvARRAY (av)[BER_DATA]) : &PL_sv_undef);
879} 1034}
880 1035
881############################################################################# 1036#############################################################################
882 1037
883void 1038void
884ber_encode (SV *tuple) 1039ber_encode (SV *tuple, SV *profile = &PL_sv_undef)
885 PPCODE: 1040 PPCODE:
886{ 1041{
1042 cur_profile = SvPROFILE (profile);
887 buf_sv = sv_2mortal (NEWSV (0, 256)); 1043 buf_sv = sv_2mortal (NEWSV (0, 256));
888 SvPOK_only (buf_sv); 1044 SvPOK_only (buf_sv);
889 set_buf (buf_sv); 1045 set_buf (buf_sv);
890 1046
891 encode_ber (tuple); 1047 encode_ber (tuple);
892 1048
893 SvCUR_set (buf_sv, cur - buf); 1049 SvCUR_set (buf_sv, cur - buf);
894 XPUSHs (buf_sv); 1050 XPUSHs (buf_sv);
895} 1051}
896 1052
1053SV *
1054ber_i32 (IV iv)
1055 CODE:
1056{
1057 AV *av = newAV ();
1058 av_fill (av, BER_ARRAYSIZE - 1);
1059 AvARRAY (av)[BER_CLASS ] = newSVcacheint (ASN_UNIVERSAL);
1060 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_INTEGER32);
1061 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (0);
1062 AvARRAY (av)[BER_DATA ] = newSViv (iv);
1063 RETVAL = newRV_noinc ((SV *)av);
1064}
1065 OUTPUT: RETVAL
1066
1067# TODO: not arrayref, but elements?
1068SV *
1069ber_seq (SV *arrayref)
1070 CODE:
1071{
1072 AV *av = newAV ();
1073 av_fill (av, BER_ARRAYSIZE - 1);
1074 AvARRAY (av)[BER_CLASS ] = newSVcacheint (ASN_UNIVERSAL);
1075 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_SEQUENCE);
1076 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (1);
1077 AvARRAY (av)[BER_DATA ] = newSVsv (arrayref);
1078 RETVAL = newRV_noinc ((SV *)av);
1079}
1080 OUTPUT: RETVAL
1081
1082MODULE = Convert::BER::XS PACKAGE = Convert::BER::XS::Profile
1083
1084SV *
1085new (SV *klass)
1086 CODE:
1087 RETVAL = profile_new ();
1088 OUTPUT: RETVAL
1089
1090void
1091set (SV *profile, int klass, int tag, int type)
1092 CODE:
1093 profile_set (SvPROFILE (profile), klass, tag, type);
1094
1095IV
1096get (SV *profile, int klass, int tag)
1097 CODE:
1098 RETVAL = profile_lookup (SvPROFILE (profile), klass, tag);
1099 OUTPUT: RETVAL
1100
1101void
1102_set_default (SV *profile)
1103 CODE:
1104 default_profile = SvPROFILE (profile);
1105
1106

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines