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.8 by root, Sat Apr 20 01:50:13 2019 UTC vs.
Revision 1.18 by root, Sat Apr 20 16:12:53 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_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,
77 BER_TYPE_IPADDRESS, 80 BER_TYPE_IPADDRESS,
78 BER_TYPE_CROAK, 81 BER_TYPE_CROAK,
79}; 82};
80 83
81enum { 84enum {
82 BER_CLASS = 0, 85 BER_CLASS = 0,
83 BER_TAG = 1, 86 BER_TAG = 1,
84 BER_CONSTRUCTED = 2, 87 BER_FLAGS = 2,
85 BER_DATA = 3, 88 BER_DATA = 3,
86 BER_ARRAYSIZE 89 BER_ARRAYSIZE
87}; 90};
88 91
89#define MAX_OID_STRLEN 4096 92#define MAX_OID_STRLEN 4096
90 93
91typedef void profile_type; 94typedef void profile_type;
92 95
93static profile_type *cur_profile, *default_profile; 96static profile_type *cur_profile, *default_profile;
94static SV *buf_sv; // encoding buffer 97static SV *buf_sv; // encoding buffer
95static 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
96 103
97#if __GNUC__ >= 3 104#if __GNUC__ >= 3
98# define expect(expr,value) __builtin_expect ((expr), (value)) 105# define expect(expr,value) __builtin_expect ((expr), (value))
99# define INLINE static inline 106# define INLINE static inline
100#else 107#else
155 return BER_TYPE_BYTES; 162 return BER_TYPE_BYTES;
156 163
157 return SvPVX (sv)[idx]; 164 return SvPVX (sv)[idx];
158} 165}
159 166
160static int 167static void
161profile_set (profile_type *profile, int klass, int tag, int type) 168profile_set (profile_type *profile, int klass, int tag, int type)
162{ 169{
163 SV *sv = (SV *)profile; 170 SV *sv = (SV *)profile;
164 U32 idx = (tag << 2) + klass; 171 U32 idx = (tag << 2) + klass;
165 STRLEN oldlen = SvCUR (sv); 172 STRLEN oldlen = SvCUR (sv);
174 181
175 SvPVX (sv)[idx] = type; 182 SvPVX (sv)[idx] = type;
176} 183}
177 184
178static SV * 185static SV *
179profile_new () 186profile_new (void)
180{ 187{
181 SV *sv = newSVpvn ("", 0); 188 SV *sv = newSVpvn ("", 0);
182 189
183 static const struct { 190 static const struct {
184 int klass; 191 int klass;
185 int tag; 192 int tag;
186 int type; 193 int type;
187 } *celem, default_map[] = { 194 } *celem, default_map[] = {
188 { ASN_UNIVERSAL, ASN_BOOLEAN , BER_TYPE_BOOL }, 195 { ASN_UNIVERSAL, ASN_BOOLEAN , BER_TYPE_BOOL },
189 { ASN_UNIVERSAL, ASN_INTEGER32 , BER_TYPE_INT }, 196 { ASN_UNIVERSAL, ASN_INTEGER , BER_TYPE_INT },
190 { ASN_UNIVERSAL, ASN_NULL , BER_TYPE_NULL }, 197 { ASN_UNIVERSAL, ASN_NULL , BER_TYPE_NULL },
191 { ASN_UNIVERSAL, ASN_OBJECT_IDENTIFIER, BER_TYPE_OID }, 198 { 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 }, 199 { ASN_UNIVERSAL, ASN_RELATIVE_OID , BER_TYPE_RELOID },
194 { ASN_UNIVERSAL, ASN_REAL , BER_TYPE_REAL }, 200 { ASN_UNIVERSAL, ASN_REAL , BER_TYPE_REAL },
201 { ASN_UNIVERSAL, ASN_ENUMERATED , BER_TYPE_INT },
195 { ASN_UNIVERSAL, ASN_UTF8_STRING , BER_TYPE_UTF8 }, 202 { ASN_UNIVERSAL, ASN_UTF8_STRING , BER_TYPE_UTF8 },
196 { ASN_UNIVERSAL, ASN_BMP_STRING , BER_TYPE_UCS2 }, 203 { ASN_UNIVERSAL, ASN_BMP_STRING , BER_TYPE_UCS2 },
197 { ASN_UNIVERSAL, ASN_UNIVERSAL_STRING , BER_TYPE_UCS4 }, 204 { ASN_UNIVERSAL, ASN_UNIVERSAL_STRING , BER_TYPE_UCS4 },
198 }; 205 };
199 206
200 for (celem = default_map + sizeof (default_map) / sizeof (default_map [0]); celem > default_map; celem--) 207 for (celem = default_map + sizeof (default_map) / sizeof (default_map [0]); celem-- > default_map; )
201 profile_set ((void *)sv, celem->klass, celem->tag, celem->type); 208 profile_set ((profile_type *)sv, celem->klass, celem->tag, celem->type);
202 209
203 return sv_bless (newRV_noinc (sv), profile_stash); 210 return sv_bless (newRV_noinc (sv), profile_stash);
204} 211}
205 212
206///////////////////////////////////////////////////////////////////////////// 213/////////////////////////////////////////////////////////////////////////////
241 248
242 return *cur++; 249 return *cur++;
243} 250}
244 251
245// get ber-encoded integer (i.e. pack "w") 252// get ber-encoded integer (i.e. pack "w")
246static U32 253static UV
247get_w (void) 254get_w (void)
248{ 255{
249 U32 res = 0; 256 UV res = 0;
250 257
251 for (;;) 258 for (;;)
252 { 259 {
253 U8 c = get_u8 (); 260 U8 c = get_u8 ();
254 res = (res << 7) | (c & 0x7f); 261 res = (res << 7) | (c & 0x7f);
256 if (!(c & 0x80)) 263 if (!(c & 0x80))
257 return res; 264 return res;
258 } 265 }
259} 266}
260 267
261static U32 268static UV
262get_length (void) 269get_length (void)
263{ 270{
264 U32 res = get_u8 (); 271 UV res = get_u8 ();
265 272
266 if (res & 0x80) 273 if (res & 0x80)
267 { 274 {
268 int cnt = res & 0x7f; 275 int cnt = res & 0x7f;
269 res = 0; 276 res = 0;
272 { 279 {
273 case 0: 280 case 0:
274 error ("indefinite ASN.1 lengths not supported"); 281 error ("indefinite ASN.1 lengths not supported");
275 return 0; 282 return 0;
276 283
284 //case 0x80: // indefinite length
285
286 //case 0xff: reserved
277 default: 287 default:
278 error ("ASN.1 length too long"); 288 error ("ASN.1 length too long");
279 return 0; 289 return 0;
280 290
291 case 8: res = (res << 8) | get_u8 ();
292 case 7: res = (res << 8) | get_u8 ();
293 case 6: res = (res << 8) | get_u8 ();
294 case 5: res = (res << 8) | get_u8 ();
281 case 4: res = (res << 8) | get_u8 (); 295 case 4: res = (res << 8) | get_u8 ();
282 case 3: res = (res << 8) | get_u8 (); 296 case 3: res = (res << 8) | get_u8 ();
283 case 2: res = (res << 8) | get_u8 (); 297 case 2: res = (res << 8) | get_u8 ();
284 case 1: res = (res << 8) | get_u8 (); 298 case 1: res = (res << 8) | get_u8 ();
285 } 299 }
287 301
288 return res; 302 return res;
289} 303}
290 304
291static SV * 305static SV *
292decode_int () 306decode_int (void)
293{ 307{
294 int len = get_length (); 308 UV len = get_length ();
295 309
296 if (len <= 0) 310 if (!len)
297 { 311 {
298 error ("integer length equal to zero"); 312 error ("invalid integer length equal to zero");
299 return 0; 313 return 0;
300 } 314 }
301 315
302 U8 *data = get_n (len); 316 U8 *data = get_n (len);
303 317
315} 329}
316 330
317static SV * 331static SV *
318decode_data (void) 332decode_data (void)
319{ 333{
320 U32 len = get_length (); 334 UV len = get_length ();
321 U8 *data = get_n (len);
322 return newSVpvn ((char *)data, len); 335 return newSVpvn ((char *)get_n (len), len);
323} 336}
324 337
325// gelper for decode_object_identifier 338// helper for decode_object_identifier
326static char * 339static char *
327write_uv (char *buf, U32 u) 340write_uv (char *buf, UV u)
328{ 341{
329 // the one-digit case is absolutely predominant, so this pays off (hopefully) 342 // the one-digit case is absolutely predominant, so this pays off (hopefully)
330 if (expect_true (u < 10)) 343 if (expect_true (u < 10))
331 *buf++ = u + '0'; 344 *buf++ = u + '0';
332 else 345 else
333 { 346 {
347 // this *could* be done much faster using branchless fixed-point arithmetics
334 char *beg = buf; 348 char *beg = buf;
335 349
336 do 350 do
337 { 351 {
338 *buf++ = u % 10 + '0'; 352 *buf++ = u % 10 + '0';
339 u /= 10; 353 u /= 10;
340 } 354 }
341 while (u); 355 while (u);
342 356
343 // reverse digits 357 // reverse digits
344 for (char *ptr = buf; --ptr != beg; ++beg) 358 char *ptr = buf;
359 while (--ptr > beg)
345 { 360 {
346 char c = *ptr; 361 char c = *ptr;
347 *ptr = *beg; 362 *ptr = *beg;
348 *beg = c; 363 *beg = c;
364 ++beg;
349 } 365 }
350 } 366 }
351 367
352 return buf; 368 return buf;
353} 369}
354 370
355static SV * 371static SV *
356decode_oid (int relative) 372decode_oid (int relative)
357{ 373{
358 U32 len = get_length (); 374 UV len = get_length ();
359 375
360 if (len <= 0) 376 if (len <= 0)
361 { 377 {
362 error ("OBJECT IDENTIFIER length equal to zero"); 378 error ("OBJECT IDENTIFIER length equal to zero");
363 return &PL_sv_undef; 379 return &PL_sv_undef;
364 } 380 }
365 381
366 U8 *end = cur + len; 382 U8 *end = cur + len;
367 U32 w = get_w (); 383 UV w = get_w ();
368 384
369 static char oid[MAX_OID_STRLEN]; // must be static 385 static char oid[MAX_OID_STRLEN]; // static, becaueds too large for stack
370 char *app = oid; 386 char *app = oid;
371 387
372 if (relative) 388 if (relative)
373 app = write_uv (app, w); 389 app = write_uv (app, w);
374 else 390 else if (w < 2 * 40)
375 { 391 {
376 app = write_uv (app, (U8)w / 40); 392 app = write_uv (app, (U8)w / 40);
377 *app++ = '.'; 393 *app++ = '.';
378 app = write_uv (app, (U8)w % 40); 394 app = write_uv (app, (U8)w % 40);
379 } 395 }
396 else
397 {
398 app = write_uv (app, 2);
399 *app++ = '.';
400 app = write_uv (app, w - 2 * 40);
401 }
380 402
403 while (cur < end)
404 {
381 // we assume an oid component is never > 64 bytes 405 // we assume an oid component is never > 64 digits
382 while (cur < end && oid + sizeof (oid) - app > 64) 406 if (oid + sizeof (oid) - app < 64)
383 { 407 croak ("BER_TYPE_OID to long to decode");
408
384 w = get_w (); 409 w = get_w ();
385 *app++ = '.'; 410 *app++ = '.';
386 app = write_uv (app, w); 411 app = write_uv (app, w);
387 } 412 }
388 413
393static SV * 418static SV *
394decode_ucs (int chrsize) 419decode_ucs (int chrsize)
395{ 420{
396 SV *res = NEWSV (0, 0); 421 SV *res = NEWSV (0, 0);
397 422
398 U32 len = get_length (); 423 UV len = get_length ();
399 424
400 if (len & (chrsize - 1)) 425 if (len & (chrsize - 1))
401 croak ("BER_TYPE_UCS has an invalid number of octets (%d)", len); 426 croak ("BER_TYPE_UCS has an invalid number of octets (%d)", len);
402 427
403 while (len) 428 while (len)
424 449
425 return res; 450 return res;
426} 451}
427 452
428static SV * 453static SV *
429decode_ber () 454decode_ber (void)
430{ 455{
431 int identifier = get_u8 (); 456 int identifier = get_u8 ();
432 457
433 SV *res; 458 SV *res;
434 459
442 if (tag == ASN_TAG_BER) 467 if (tag == ASN_TAG_BER)
443 tag = get_w (); 468 tag = get_w ();
444 469
445 if (constructed) 470 if (constructed)
446 { 471 {
447 U32 len = get_length (); 472 UV len = get_length ();
448 U32 seqend = (cur - buf) + len; 473 UV seqend = (cur - buf) + len;
449 AV *av = (AV *)sv_2mortal ((SV *)newAV ()); 474 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
450 475
451 while (cur < buf + seqend) 476 while (cur < buf + seqend)
452 av_push (av, decode_ber ()); 477 av_push (av, decode_ber ());
453 478
458 } 483 }
459 else 484 else
460 switch (profile_lookup (cur_profile, klass, tag)) 485 switch (profile_lookup (cur_profile, klass, tag))
461 { 486 {
462 case BER_TYPE_NULL: 487 case BER_TYPE_NULL:
488 {
489 UV len = get_length ();
490
491 if (len)
492 croak ("BER_TYPE_NULL value with non-zero length %d encountered", len);
493
463 res = &PL_sv_undef; 494 res = &PL_sv_undef;
495 }
464 break; 496 break;
465 497
466 case BER_TYPE_BOOL: 498 case BER_TYPE_BOOL:
467 { 499 {
468 U32 len = get_length (); 500 UV len = get_length ();
469 501
470 if (len != 1) 502 if (len != 1)
471 croak ("BER_TYPE_BOOLEAN type with invalid length %d encountered", len); 503 croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered", len);
472 504
473 res = newSVcacheint (get_u8 () ? 0 : 1); 505 res = newSVcacheint (!!get_u8 ());
474 } 506 }
475 break; 507 break;
476 508
477 case BER_TYPE_OID: 509 case BER_TYPE_OID:
478 res = decode_oid (0); 510 res = decode_oid (0);
495 res = decode_data (); 527 res = decode_data ();
496 break; 528 break;
497 529
498 case BER_TYPE_IPADDRESS: 530 case BER_TYPE_IPADDRESS:
499 { 531 {
500 U32 len = get_length (); 532 UV len = get_length ();
501 533
502 if (len != 4) 534 if (len != 4)
503 croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered", len); 535 croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered", len);
504 536
505 U8 c1 = get_u8 (); 537 U8 c1 = get_u8 ();
525 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 557 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
526 } 558 }
527 559
528 AV *av = newAV (); 560 AV *av = newAV ();
529 av_fill (av, BER_ARRAYSIZE - 1); 561 av_fill (av, BER_ARRAYSIZE - 1);
530 AvARRAY (av)[BER_CLASS ] = newSVcacheint (klass); 562 AvARRAY (av)[BER_CLASS] = newSVcacheint (klass);
531 AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); 563 AvARRAY (av)[BER_TAG ] = newSVcacheint (tag);
532 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (constructed ? 1 : 0); 564 AvARRAY (av)[BER_FLAGS] = newSVcacheint (constructed ? 1 : 0);
533 AvARRAY (av)[BER_DATA ] = res; 565 AvARRAY (av)[BER_DATA ] = res;
534 566
535 return newRV_noinc ((SV *)av); 567 return newRV_noinc ((SV *)av);
536} 568}
537 569
538///////////////////////////////////////////////////////////////////////////// 570/////////////////////////////////////////////////////////////////////////////
553static void 585static void
554set_buf (SV *sv) 586set_buf (SV *sv)
555{ 587{
556 STRLEN len; 588 STRLEN len;
557 buf_sv = sv; 589 buf_sv = sv;
558 buf = SvPVbyte (buf_sv, len); 590 buf = (U8 *)SvPVbyte (buf_sv, len);
559 cur = buf; 591 cur = buf;
560 end = buf + len; 592 end = buf + len;
561} 593}
562 594
563/* similar to SvGROW, but somewhat safer and guarantees exponential realloc strategy */ 595/* similar to SvGROW, but somewhat safer and guarantees exponential realloc strategy */
577need (STRLEN len) 609need (STRLEN len)
578{ 610{
579 if (expect_false ((uintptr_t)(end - cur) < len)) 611 if (expect_false ((uintptr_t)(end - cur) < len))
580 { 612 {
581 STRLEN pos = cur - buf; 613 STRLEN pos = cur - buf;
582 buf = my_sv_grow (buf_sv, pos, len); 614 buf = (U8 *)my_sv_grow (buf_sv, pos, len);
583 cur = buf + pos; 615 cur = buf + pos;
584 end = buf + SvLEN (buf_sv) - 1; 616 end = buf + SvLEN (buf_sv) - 1;
585 } 617 }
586} 618}
587 619
591 need (1); 623 need (1);
592 *cur++ = val; 624 *cur++ = val;
593} 625}
594 626
595static void 627static void
596put_w_nocheck (U32 val) 628put_w_nocheck (UV val)
597{ 629{
630#if UVSIZE > 4
631 *cur = (val >> 7 * 9) | 0x80; cur += val >= ((UV)1 << (7 * 9));
632 *cur = (val >> 7 * 8) | 0x80; cur += val >= ((UV)1 << (7 * 8));
633 *cur = (val >> 7 * 7) | 0x80; cur += val >= ((UV)1 << (7 * 7));
634 *cur = (val >> 7 * 6) | 0x80; cur += val >= ((UV)1 << (7 * 6));
635 *cur = (val >> 7 * 5) | 0x80; cur += val >= ((UV)1 << (7 * 5));
636#endif
598 *cur = (val >> 7 * 4) | 0x80; cur += val >= (1 << (7 * 4)); 637 *cur = (val >> 7 * 4) | 0x80; cur += val >= ((UV)1 << (7 * 4));
599 *cur = (val >> 7 * 3) | 0x80; cur += val >= (1 << (7 * 3)); 638 *cur = (val >> 7 * 3) | 0x80; cur += val >= ((UV)1 << (7 * 3));
600 *cur = (val >> 7 * 2) | 0x80; cur += val >= (1 << (7 * 2)); 639 *cur = (val >> 7 * 2) | 0x80; cur += val >= ((UV)1 << (7 * 2));
601 *cur = (val >> 7 * 1) | 0x80; cur += val >= (1 << (7 * 1)); 640 *cur = (val >> 7 * 1) | 0x80; cur += val >= ((UV)1 << (7 * 1));
602 *cur = val & 0x7f; cur += 1; 641 *cur = val & 0x7f; cur += 1;
603} 642}
604 643
605static void 644static void
606put_w (U32 val) 645put_w (UV val)
607{ 646{
608 need (5); // we only handle up to 5 bytes 647 need (5); // we only handle up to 5 bytes
609 648
610 put_w_nocheck (val); 649 put_w_nocheck (val);
611} 650}
612 651
613static U8 * 652static U8 *
614put_length_at (U32 val, U8 *cur) 653put_length_at (UV val, U8 *cur)
615{ 654{
616 if (val < 0x7fU) 655 if (val < 0x7fU)
617 *cur++ = val; 656 *cur++ = val;
618 else 657 else
619 { 658 {
620 U8 *lenb = cur++; 659 U8 *lenb = cur++;
621 660
661#if UVSIZE > 4
662 *cur = val >> 56; cur += *cur > 0;
663 *cur = val >> 48; cur += *cur > 0;
664 *cur = val >> 40; cur += *cur > 0;
665 *cur = val >> 32; cur += *cur > 0;
666#endif
622 *cur = val >> 24; cur += *cur > 0; 667 *cur = val >> 24; cur += *cur > 0;
623 *cur = val >> 16; cur += *cur > 0; 668 *cur = val >> 16; cur += *cur > 0;
624 *cur = val >> 8; cur += *cur > 0; 669 *cur = val >> 8; cur += *cur > 0;
625 *cur = val ; cur += 1; 670 *cur = val ; cur += 1;
626 671
629 674
630 return cur; 675 return cur;
631} 676}
632 677
633static void 678static void
634put_length (U32 val) 679put_length (UV val)
635{ 680{
636 need (5 + val); 681 need (5 + val);
637 cur = put_length_at (val, cur); 682 cur = put_length_at (val, cur);
638} 683}
639 684
640// return how many bytes the encoded length requires 685// return how many bytes the encoded length requires
641static int length_length (U32 val) 686static int length_length (UV val)
642{ 687{
643 return val < 0x7fU 688 return val < 0x7fU
644 ? 1 689 ? 1
645 : 2 + (val > 0xffU) + (val > 0xffffU) + (val > 0xffffffU); 690 : 2
691 + (val > 0xffU)
692 + (val > 0xffffU)
693 + (val > 0xffffffU)
694#if UVSIZE > 4
695 + (val > 0xffffffffU)
696 + (val > 0xffffffffffU)
697 + (val > 0xffffffffffffU)
698 + (val > 0xffffffffffffffU)
699#endif
700 ;
646} 701}
647 702
648static void 703static void
649encode_data (const char *ptr, STRLEN len) 704encode_data (const char *ptr, STRLEN len)
650{ 705{
712 767
713 *lenb = cur - lenb - 1; 768 *lenb = cur - lenb - 1;
714} 769}
715 770
716// we don't know the length yet, so we optimistically 771// we don't know the length yet, so we optimistically
717// assume the length will need one octet later. if that 772// assume the length will need one octet later. If that
718// turns out to be wrong, we memove as needed. 773// turns out to be wrong, we memmove as needed.
719// mark the beginning 774// mark the beginning
720static STRLEN 775static STRLEN
721len_fixup_mark () 776len_fixup_mark (void)
722{ 777{
723 return cur++ - buf; 778 return cur++ - buf;
724} 779}
725 780
726// patch up the length 781// patch up the length
813 put_length (uchars * chrsize); 868 put_length (uchars * chrsize);
814 869
815 while (uchars--) 870 while (uchars--)
816 { 871 {
817 STRLEN uclen; 872 STRLEN uclen;
818 UV uchr = utf8_to_uvchr_buf (ptr, ptr + len, &uclen); 873 UV uchr = utf8_to_uvchr_buf ((U8 *)ptr, (U8 *)ptr + len, &uclen);
819 874
820 ptr += uclen; 875 ptr += uclen;
821 len -= uclen; 876 len -= uclen;
822 877
823 if (chrsize == 4) 878 if (chrsize == 4)
835{ 890{
836 AV *av = ber_tuple (tuple); 891 AV *av = ber_tuple (tuple);
837 892
838 int klass = SvIV (AvARRAY (av)[BER_CLASS]); 893 int klass = SvIV (AvARRAY (av)[BER_CLASS]);
839 int tag = SvIV (AvARRAY (av)[BER_TAG]); 894 int tag = SvIV (AvARRAY (av)[BER_TAG]);
840 int constructed = SvIV (AvARRAY (av)[BER_CONSTRUCTED]) ? ASN_CONSTRUCTED : 0; 895 int constructed = SvIV (AvARRAY (av)[BER_FLAGS]) & 1 ? ASN_CONSTRUCTED : 0;
841 SV *data = AvARRAY (av)[BER_DATA]; 896 SV *data = AvARRAY (av)[BER_DATA];
842 897
843 int identifier = (klass << ASN_CLASS_SHIFT) | constructed; 898 int identifier = (klass << ASN_CLASS_SHIFT) | constructed;
844 899
845 if (expect_false (tag >= ASN_TAG_BER)) 900 if (expect_false (tag >= ASN_TAG_BER))
864 int fill = AvFILL (av); 919 int fill = AvFILL (av);
865 920
866 if (expect_false (SvRMAGICAL (av))) 921 if (expect_false (SvRMAGICAL (av)))
867 croak ("BER constructed data must not be tied"); 922 croak ("BER constructed data must not be tied");
868 923
924 int i;
869 for (int i = 0; i <= fill; ++i) 925 for (i = 0; i <= fill; ++i)
870 encode_ber (AvARRAY (av)[i]); 926 encode_ber (AvARRAY (av)[i]);
871 927
872 len_fixup (mark); 928 len_fixup (mark);
873 } 929 }
874 else 930 else
878 put_length (0); 934 put_length (0);
879 break; 935 break;
880 936
881 case BER_TYPE_BOOL: 937 case BER_TYPE_BOOL:
882 put_length (1); 938 put_length (1);
883 *cur++ = SvTRUE (data) ? 0xff : 0x00; 939 *cur++ = SvTRUE (data) ? 0xff : 0x00; // 0xff = DER/CER
884 break; 940 break;
885 941
886 case BER_TYPE_OID: 942 case BER_TYPE_OID:
887 encode_oid (data, 0); 943 encode_oid (data, 0);
888 break; 944 break;
951 const char *name; 1007 const char *name;
952 IV iv; 1008 IV iv;
953 } *civ, const_iv[] = { 1009 } *civ, const_iv[] = {
954#define const_iv(name) { # name, name }, 1010#define const_iv(name) { # name, name },
955 const_iv (ASN_BOOLEAN) 1011 const_iv (ASN_BOOLEAN)
956 const_iv (ASN_INTEGER32) 1012 const_iv (ASN_INTEGER)
957 const_iv (ASN_BIT_STRING) 1013 const_iv (ASN_BIT_STRING)
958 const_iv (ASN_OCTET_STRING) 1014 const_iv (ASN_OCTET_STRING)
959 const_iv (ASN_NULL) 1015 const_iv (ASN_NULL)
960 const_iv (ASN_OBJECT_IDENTIFIER) 1016 const_iv (ASN_OBJECT_IDENTIFIER)
961 const_iv (ASN_OBJECT_DESCRIPTOR) 1017 const_iv (ASN_OBJECT_DESCRIPTOR)
990 const_iv (ASN_CONTEXT) 1046 const_iv (ASN_CONTEXT)
991 const_iv (ASN_PRIVATE) 1047 const_iv (ASN_PRIVATE)
992 1048
993 const_iv (BER_CLASS) 1049 const_iv (BER_CLASS)
994 const_iv (BER_TAG) 1050 const_iv (BER_TAG)
995 const_iv (BER_CONSTRUCTED) 1051 const_iv (BER_FLAGS)
996 const_iv (BER_DATA) 1052 const_iv (BER_DATA)
997 1053
998 const_iv (BER_TYPE_BYTES) 1054 const_iv (BER_TYPE_BYTES)
999 const_iv (BER_TYPE_UTF8) 1055 const_iv (BER_TYPE_UTF8)
1000 const_iv (BER_TYPE_UCS2) 1056 const_iv (BER_TYPE_UCS2)
1024ber_decode (SV *ber, SV *profile = &PL_sv_undef) 1080ber_decode (SV *ber, SV *profile = &PL_sv_undef)
1025 CODE: 1081 CODE:
1026{ 1082{
1027 cur_profile = SvPROFILE (profile); 1083 cur_profile = SvPROFILE (profile);
1028 STRLEN len; 1084 STRLEN len;
1029 buf = SvPVbyte (ber, len); 1085 buf = (U8 *)SvPVbyte (ber, len);
1030 cur = buf; 1086 cur = buf;
1031 end = buf + len; 1087 end = buf + len;
1032 1088
1033 RETVAL = decode_ber (); 1089 RETVAL = decode_ber ();
1034} 1090}
1035 OUTPUT: RETVAL 1091 OUTPUT: RETVAL
1036 1092
1037void 1093void
1038ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *constructed = &PL_sv_undef, SV *data = &PL_sv_undef) 1094ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *flags = &PL_sv_undef, SV *data = &PL_sv_undef)
1039 PPCODE: 1095 PPCODE:
1040{ 1096{
1041 if (!SvOK (tuple)) 1097 if (!SvOK (tuple))
1042 XSRETURN_NO; 1098 XSRETURN_NO;
1043 1099
1045 croak ("ber_is: tuple must be BER tuple (array-ref)"); 1101 croak ("ber_is: tuple must be BER tuple (array-ref)");
1046 1102
1047 AV *av = (AV *)SvRV (tuple); 1103 AV *av = (AV *)SvRV (tuple);
1048 1104
1049 XPUSHs ( 1105 XPUSHs (
1050 (!SvOK (klass) || SvIV (AvARRAY (av)[BER_CLASS ]) == SvIV (klass)) 1106 (!SvOK (klass) || SvIV (AvARRAY (av)[BER_CLASS]) == SvIV (klass))
1051 && (!SvOK (tag) || SvIV (AvARRAY (av)[BER_TAG ]) == SvIV (tag)) 1107 && (!SvOK (tag) || SvIV (AvARRAY (av)[BER_TAG ]) == SvIV (tag))
1052 && (!SvOK (constructed) || !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) == !SvIV (constructed)) 1108 && (!SvOK (flags) || !SvIV (AvARRAY (av)[BER_FLAGS]) == !SvIV (flags))
1053 && (!SvOK (data) || sv_eq (AvARRAY (av)[BER_DATA ], data)) 1109 && (!SvOK (data) || sv_eq (AvARRAY (av)[BER_DATA ], data))
1054 ? &PL_sv_yes : &PL_sv_undef); 1110 ? &PL_sv_yes : &PL_sv_undef);
1055} 1111}
1056 1112
1057void 1113void
1058ber_is_seq (SV *tuple) 1114ber_is_seq (SV *tuple)
1062 XSRETURN_UNDEF; 1118 XSRETURN_UNDEF;
1063 1119
1064 AV *av = ber_tuple (tuple); 1120 AV *av = ber_tuple (tuple);
1065 1121
1066 XPUSHs ( 1122 XPUSHs (
1067 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL 1123 SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL
1068 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_SEQUENCE 1124 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_SEQUENCE
1069 && SvIV (AvARRAY (av)[BER_CONSTRUCTED]) 1125 && SvIV (AvARRAY (av)[BER_FLAGS])
1070 ? AvARRAY (av)[BER_DATA] : &PL_sv_undef); 1126 ? AvARRAY (av)[BER_DATA] : &PL_sv_undef);
1071} 1127}
1072 1128
1073void 1129void
1074ber_is_i32 (SV *tuple, SV *value = &PL_sv_undef) 1130ber_is_int (SV *tuple, SV *value = &PL_sv_undef)
1075 PPCODE: 1131 PPCODE:
1076{ 1132{
1077 if (!SvOK (tuple)) 1133 if (!SvOK (tuple))
1078 XSRETURN_NO; 1134 XSRETURN_NO;
1079 1135
1080 AV *av = ber_tuple (tuple); 1136 AV *av = ber_tuple (tuple);
1081 1137
1082 IV data = SvIV (AvARRAY (av)[BER_DATA]); 1138 UV data = SvUV (AvARRAY (av)[BER_DATA]);
1083 1139
1084 XPUSHs ( 1140 XPUSHs (
1085 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL 1141 SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL
1086 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_INTEGER32 1142 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_INTEGER
1087 && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) 1143 && !SvIV (AvARRAY (av)[BER_FLAGS])
1088 && (!SvOK (value) || data == SvIV (value)) 1144 && (!SvOK (value) || data == SvUV (value))
1089 ? sv_2mortal (data ? newSViv (data) : newSVpv ("0 but true", 0)) 1145 ? sv_2mortal (data ? newSVsv (AvARRAY (av)[BER_DATA]) : newSVpv ("0 but true", 0))
1090 : &PL_sv_undef); 1146 : &PL_sv_undef);
1091} 1147}
1092 1148
1093void 1149void
1094ber_is_oid (SV *tuple, SV *oid = &PL_sv_undef) 1150ber_is_oid (SV *tuple, SV *oid = &PL_sv_undef)
1098 XSRETURN_NO; 1154 XSRETURN_NO;
1099 1155
1100 AV *av = ber_tuple (tuple); 1156 AV *av = ber_tuple (tuple);
1101 1157
1102 XPUSHs ( 1158 XPUSHs (
1103 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL 1159 SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL
1104 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_OBJECT_IDENTIFIER 1160 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_OBJECT_IDENTIFIER
1105 && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) 1161 && !SvIV (AvARRAY (av)[BER_FLAGS])
1106 && (!SvOK (oid) || sv_eq (AvARRAY (av)[BER_DATA], oid)) 1162 && (!SvOK (oid) || sv_eq (AvARRAY (av)[BER_DATA], oid))
1107 ? newSVsv (AvARRAY (av)[BER_DATA]) : &PL_sv_undef); 1163 ? newSVsv (AvARRAY (av)[BER_DATA]) : &PL_sv_undef);
1108} 1164}
1109 1165
1110############################################################################# 1166#############################################################################
1123 SvCUR_set (buf_sv, cur - buf); 1179 SvCUR_set (buf_sv, cur - buf);
1124 XPUSHs (buf_sv); 1180 XPUSHs (buf_sv);
1125} 1181}
1126 1182
1127SV * 1183SV *
1128ber_i32 (IV iv) 1184ber_int (SV *sv)
1129 CODE: 1185 CODE:
1130{ 1186{
1131 AV *av = newAV (); 1187 AV *av = newAV ();
1132 av_fill (av, BER_ARRAYSIZE - 1); 1188 av_fill (av, BER_ARRAYSIZE - 1);
1133 AvARRAY (av)[BER_CLASS ] = newSVcacheint (ASN_UNIVERSAL); 1189 AvARRAY (av)[BER_CLASS] = newSVcacheint (ASN_UNIVERSAL);
1134 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_INTEGER32); 1190 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_INTEGER);
1135 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (0); 1191 AvARRAY (av)[BER_FLAGS] = newSVcacheint (0);
1136 AvARRAY (av)[BER_DATA ] = newSViv (iv); 1192 AvARRAY (av)[BER_DATA ] = newSVsv (sv);
1137 RETVAL = newRV_noinc ((SV *)av); 1193 RETVAL = newRV_noinc ((SV *)av);
1138} 1194}
1139 OUTPUT: RETVAL 1195 OUTPUT: RETVAL
1140 1196
1141# TODO: not arrayref, but elements? 1197# TODO: not arrayref, but elements?
1143ber_seq (SV *arrayref) 1199ber_seq (SV *arrayref)
1144 CODE: 1200 CODE:
1145{ 1201{
1146 AV *av = newAV (); 1202 AV *av = newAV ();
1147 av_fill (av, BER_ARRAYSIZE - 1); 1203 av_fill (av, BER_ARRAYSIZE - 1);
1148 AvARRAY (av)[BER_CLASS ] = newSVcacheint (ASN_UNIVERSAL); 1204 AvARRAY (av)[BER_CLASS] = newSVcacheint (ASN_UNIVERSAL);
1149 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_SEQUENCE); 1205 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_SEQUENCE);
1150 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (1); 1206 AvARRAY (av)[BER_FLAGS] = newSVcacheint (1);
1151 AvARRAY (av)[BER_DATA ] = newSVsv (arrayref); 1207 AvARRAY (av)[BER_DATA ] = newSVsv (arrayref);
1152 RETVAL = newRV_noinc ((SV *)av); 1208 RETVAL = newRV_noinc ((SV *)av);
1153} 1209}
1154 OUTPUT: RETVAL 1210 OUTPUT: RETVAL
1155 1211
1156MODULE = Convert::BER::XS PACKAGE = Convert::BER::XS::Profile 1212MODULE = Convert::BER::XS PACKAGE = Convert::BER::XS::Profile

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines