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.9 by root, Sat Apr 20 02:12:27 2019 UTC vs.
Revision 1.20 by root, Sat Apr 20 17:04:35 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
159 return BER_TYPE_BYTES; 162 return BER_TYPE_BYTES;
160 163
161 return SvPVX (sv)[idx]; 164 return SvPVX (sv)[idx];
162} 165}
163 166
164static int 167static void
165profile_set (profile_type *profile, int klass, int tag, int type) 168profile_set (profile_type *profile, int klass, int tag, int type)
166{ 169{
167 SV *sv = (SV *)profile; 170 SV *sv = (SV *)profile;
168 U32 idx = (tag << 2) + klass; 171 U32 idx = (tag << 2) + klass;
169 STRLEN oldlen = SvCUR (sv); 172 STRLEN oldlen = SvCUR (sv);
178 181
179 SvPVX (sv)[idx] = type; 182 SvPVX (sv)[idx] = type;
180} 183}
181 184
182static SV * 185static SV *
183profile_new () 186profile_new (void)
184{ 187{
185 SV *sv = newSVpvn ("", 0); 188 SV *sv = newSVpvn ("", 0);
186 189
187 static const struct { 190 static const struct {
188 int klass; 191 int klass;
189 int tag; 192 int tag;
190 int type; 193 int type;
191 } *celem, default_map[] = { 194 } *celem, default_map[] = {
192 { ASN_UNIVERSAL, ASN_BOOLEAN , BER_TYPE_BOOL }, 195 { ASN_UNIVERSAL, ASN_BOOLEAN , BER_TYPE_BOOL },
193 { ASN_UNIVERSAL, ASN_INTEGER32 , BER_TYPE_INT }, 196 { ASN_UNIVERSAL, ASN_INTEGER , BER_TYPE_INT },
194 { ASN_UNIVERSAL, ASN_NULL , BER_TYPE_NULL }, 197 { ASN_UNIVERSAL, ASN_NULL , BER_TYPE_NULL },
195 { ASN_UNIVERSAL, ASN_OBJECT_IDENTIFIER, BER_TYPE_OID }, 198 { ASN_UNIVERSAL, ASN_OBJECT_IDENTIFIER, BER_TYPE_OID },
196 { ASN_UNIVERSAL, ASN_OBJECT_DESCRIPTOR, BER_TYPE_OID },
197 { ASN_UNIVERSAL, ASN_RELATIVE_OID , BER_TYPE_RELOID }, 199 { ASN_UNIVERSAL, ASN_RELATIVE_OID , BER_TYPE_RELOID },
198 { ASN_UNIVERSAL, ASN_REAL , BER_TYPE_REAL }, 200 { ASN_UNIVERSAL, ASN_REAL , BER_TYPE_REAL },
201 { ASN_UNIVERSAL, ASN_ENUMERATED , BER_TYPE_INT },
199 { ASN_UNIVERSAL, ASN_UTF8_STRING , BER_TYPE_UTF8 }, 202 { ASN_UNIVERSAL, ASN_UTF8_STRING , BER_TYPE_UTF8 },
200 { ASN_UNIVERSAL, ASN_BMP_STRING , BER_TYPE_UCS2 }, 203 { ASN_UNIVERSAL, ASN_BMP_STRING , BER_TYPE_UCS2 },
201 { ASN_UNIVERSAL, ASN_UNIVERSAL_STRING , BER_TYPE_UCS4 }, 204 { ASN_UNIVERSAL, ASN_UNIVERSAL_STRING , BER_TYPE_UCS4 },
202 }; 205 };
203 206
204 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; )
205 profile_set ((void *)sv, celem->klass, celem->tag, celem->type); 208 profile_set ((profile_type *)sv, celem->klass, celem->tag, celem->type);
206 209
207 return sv_bless (newRV_noinc (sv), profile_stash); 210 return sv_bless (newRV_noinc (sv), profile_stash);
208} 211}
209 212
210///////////////////////////////////////////////////////////////////////////// 213/////////////////////////////////////////////////////////////////////////////
245 248
246 return *cur++; 249 return *cur++;
247} 250}
248 251
249// get ber-encoded integer (i.e. pack "w") 252// get ber-encoded integer (i.e. pack "w")
250static U32 253static UV
251get_w (void) 254get_w (void)
252{ 255{
253 U32 res = 0; 256 UV res = 0;
257 U8 c = get_u8 ();
258
259 if (expect_false (c == 0x80))
260 error ("illegal BER padding (X.690 8.1.2.4.2, 8.19.2)");
254 261
255 for (;;) 262 for (;;)
256 { 263 {
257 U8 c = get_u8 ();
258 res = (res << 7) | (c & 0x7f); 264 res = (res << 7) | (c & 0x7f);
259 265
260 if (!(c & 0x80)) 266 if (!(c & 0x80))
261 return res; 267 return res;
262 }
263}
264 268
269 c = get_u8 ();
270 }
271}
272
265static U32 273static UV
266get_length (void) 274get_length (void)
267{ 275{
268 U32 res = get_u8 (); 276 UV res = get_u8 ();
269 277
270 if (res & 0x80) 278 if (res & 0x80)
271 { 279 {
272 int cnt = res & 0x7f; 280 int cnt = res & 0x7f;
273 res = 0; 281 res = 0;
274 282
275 switch (cnt) 283 switch (cnt)
276 { 284 {
277 case 0: 285 case 0:
278 error ("indefinite ASN.1 lengths not supported"); 286 error ("indefinite ASN.1 lengths not supported");
279 return 0; 287
288 case 0x7f:
289 error ("ASN.1 reserved value in length (X.690 8.1.3.5)");
280 290
281 default: 291 default:
282 error ("ASN.1 length too long"); 292 error ("ASN.1 length too long (only up to 2**64 octets supported)");
283 return 0;
284 293
294 case 8: res = (res << 8) | get_u8 ();
295 case 7: res = (res << 8) | get_u8 ();
296 case 6: res = (res << 8) | get_u8 ();
297 case 5: res = (res << 8) | get_u8 ();
285 case 4: res = (res << 8) | get_u8 (); 298 case 4: res = (res << 8) | get_u8 ();
286 case 3: res = (res << 8) | get_u8 (); 299 case 3: res = (res << 8) | get_u8 ();
287 case 2: res = (res << 8) | get_u8 (); 300 case 2: res = (res << 8) | get_u8 ();
288 case 1: res = (res << 8) | get_u8 (); 301 case 1: res = (res << 8) | get_u8 ();
289 } 302 }
291 304
292 return res; 305 return res;
293} 306}
294 307
295static SV * 308static SV *
296decode_int () 309decode_int (void)
297{ 310{
298 int len = get_length (); 311 UV len = get_length ();
299 312
300 if (len <= 0) 313 if (!len)
301 {
302 error ("integer length equal to zero"); 314 error ("invalid integer length equal to zero (X.690 8.3.1)");
303 return 0;
304 }
305 315
306 U8 *data = get_n (len); 316 U8 *data = get_n (len);
317
318 if (expect_false (len > 1))
319 {
320 U16 mask = (data [0] << 8) | data [1] & 0xff80;
321
322 if (expect_false (mask == 0xff80 || mask == 0x0000))
323 error ("illegal padding in integer (X.690 8.3.2)");
324 }
307 325
308 int negative = data [0] & 0x80; 326 int negative = data [0] & 0x80;
309 327
310 UV val = negative ? -1 : 0; // copy signbit to all bits 328 UV val = negative ? -1 : 0; // copy signbit to all bits
311 329
319} 337}
320 338
321static SV * 339static SV *
322decode_data (void) 340decode_data (void)
323{ 341{
324 U32 len = get_length (); 342 UV len = get_length ();
325 U8 *data = get_n (len);
326 return newSVpvn ((char *)data, len); 343 return newSVpvn ((char *)get_n (len), len);
327} 344}
328 345
329// gelper for decode_object_identifier 346// helper for decode_object_identifier
330static char * 347static char *
331write_uv (char *buf, U32 u) 348write_uv (char *buf, UV u)
332{ 349{
333 // the one-digit case is absolutely predominant, so this pays off (hopefully) 350 // the one-digit case is absolutely predominant, so this pays off (hopefully)
334 if (expect_true (u < 10)) 351 if (expect_true (u < 10))
335 *buf++ = u + '0'; 352 *buf++ = u + '0';
336 else 353 else
337 { 354 {
355 // this *could* be done much faster using branchless fixed-point arithmetics
338 char *beg = buf; 356 char *beg = buf;
339 357
340 do 358 do
341 { 359 {
342 *buf++ = u % 10 + '0'; 360 *buf++ = u % 10 + '0';
343 u /= 10; 361 u /= 10;
344 } 362 }
345 while (u); 363 while (u);
346 364
347 // reverse digits 365 // reverse digits
348 for (char *ptr = buf; --ptr != beg; ++beg) 366 char *ptr = buf;
367 while (--ptr > beg)
349 { 368 {
350 char c = *ptr; 369 char c = *ptr;
351 *ptr = *beg; 370 *ptr = *beg;
352 *beg = c; 371 *beg = c;
372 ++beg;
353 } 373 }
354 } 374 }
355 375
356 return buf; 376 return buf;
357} 377}
358 378
359static SV * 379static SV *
360decode_oid (int relative) 380decode_oid (int relative)
361{ 381{
362 U32 len = get_length (); 382 UV len = get_length ();
363 383
364 if (len <= 0) 384 if (len <= 0)
365 { 385 {
366 error ("OBJECT IDENTIFIER length equal to zero"); 386 error ("OBJECT IDENTIFIER length equal to zero");
367 return &PL_sv_undef; 387 return &PL_sv_undef;
368 } 388 }
369 389
370 U8 *end = cur + len; 390 U8 *end = cur + len;
371 U32 w = get_w (); 391 UV w = get_w ();
372 392
373 static char oid[MAX_OID_STRLEN]; // must be static 393 static char oid[MAX_OID_STRLEN]; // static, becaueds too large for stack
374 char *app = oid; 394 char *app = oid;
375 395
376 if (relative) 396 if (relative)
377 app = write_uv (app, w); 397 app = write_uv (app, w);
378 else 398 else if (w < 2 * 40)
379 { 399 {
380 app = write_uv (app, (U8)w / 40); 400 app = write_uv (app, (U8)w / 40);
381 *app++ = '.'; 401 *app++ = '.';
382 app = write_uv (app, (U8)w % 40); 402 app = write_uv (app, (U8)w % 40);
383 } 403 }
404 else
405 {
406 app = write_uv (app, 2);
407 *app++ = '.';
408 app = write_uv (app, w - 2 * 40);
409 }
384 410
411 while (cur < end)
412 {
385 // we assume an oid component is never > 64 bytes 413 // we assume an oid component is never > 64 digits
386 while (cur < end && oid + sizeof (oid) - app > 64) 414 if (oid + sizeof (oid) - app < 64)
387 { 415 croak ("BER_TYPE_OID to long to decode");
416
388 w = get_w (); 417 w = get_w ();
389 *app++ = '.'; 418 *app++ = '.';
390 app = write_uv (app, w); 419 app = write_uv (app, w);
391 } 420 }
392 421
397static SV * 426static SV *
398decode_ucs (int chrsize) 427decode_ucs (int chrsize)
399{ 428{
400 SV *res = NEWSV (0, 0); 429 SV *res = NEWSV (0, 0);
401 430
402 U32 len = get_length (); 431 UV len = get_length ();
403 432
404 if (len & (chrsize - 1)) 433 if (len & (chrsize - 1))
405 croak ("BER_TYPE_UCS has an invalid number of octets (%d)", len); 434 croak ("BER_TYPE_UCS has an invalid number of octets (%d)", len);
406 435
407 while (len) 436 while (len)
428 457
429 return res; 458 return res;
430} 459}
431 460
432static SV * 461static SV *
433decode_ber () 462decode_ber (void)
434{ 463{
435 int identifier = get_u8 (); 464 int identifier = get_u8 ();
436 465
437 SV *res; 466 SV *res;
438 467
441 int tag = identifier & ASN_TAG_MASK; 470 int tag = identifier & ASN_TAG_MASK;
442 471
443 if (tag == ASN_TAG_BER) 472 if (tag == ASN_TAG_BER)
444 tag = get_w (); 473 tag = get_w ();
445 474
446 if (tag == ASN_TAG_BER)
447 tag = get_w ();
448
449 if (constructed) 475 if (constructed)
450 { 476 {
451 U32 len = get_length (); 477 UV len = get_length ();
452 U32 seqend = (cur - buf) + len; 478 UV seqend = (cur - buf) + len;
453 AV *av = (AV *)sv_2mortal ((SV *)newAV ()); 479 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
454 480
455 while (cur < buf + seqend) 481 while (cur < buf + seqend)
456 av_push (av, decode_ber ()); 482 av_push (av, decode_ber ());
457 483
458 if (cur > buf + seqend) 484 if (cur > buf + seqend)
459 croak ("constructed type %02x overflow (%x %x)\n", identifier, cur - buf, seqend); 485 croak ("constructed type %02x length overflow (0x%x 0x%x)\n", identifier, (int)(cur - buf), (int)seqend);
460 486
461 res = newRV_inc ((SV *)av); 487 res = newRV_inc ((SV *)av);
462 } 488 }
463 else 489 else
464 switch (profile_lookup (cur_profile, klass, tag)) 490 switch (profile_lookup (cur_profile, klass, tag))
465 { 491 {
466 case BER_TYPE_NULL: 492 case BER_TYPE_NULL:
493 {
494 UV len = get_length ();
495
496 if (len)
497 croak ("BER_TYPE_NULL value with non-zero length %d encountered (X.690 8.8.2)", len);
498
467 res = &PL_sv_undef; 499 res = &PL_sv_undef;
500 }
468 break; 501 break;
469 502
470 case BER_TYPE_BOOL: 503 case BER_TYPE_BOOL:
471 { 504 {
472 U32 len = get_length (); 505 UV len = get_length ();
473 506
474 if (len != 1) 507 if (len != 1)
475 croak ("BER_TYPE_BOOLEAN type with invalid length %d encountered", len); 508 croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered (X.690 8.2.1)", len);
476 509
477 res = newSVcacheint (get_u8 () ? 0 : 1); 510 res = newSVcacheint (!!get_u8 ());
478 } 511 }
479 break; 512 break;
480 513
481 case BER_TYPE_OID: 514 case BER_TYPE_OID:
482 res = decode_oid (0); 515 res = decode_oid (0);
499 res = decode_data (); 532 res = decode_data ();
500 break; 533 break;
501 534
502 case BER_TYPE_IPADDRESS: 535 case BER_TYPE_IPADDRESS:
503 { 536 {
504 U32 len = get_length (); 537 UV len = get_length ();
505 538
506 if (len != 4) 539 if (len != 4)
507 croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered", len); 540 croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered (RFC 2578 7.1.5)", len);
508 541
509 U8 c1 = get_u8 (); 542 U8 c1 = get_u8 ();
510 U8 c2 = get_u8 (); 543 U8 c2 = get_u8 ();
511 U8 c3 = get_u8 (); 544 U8 c3 = get_u8 ();
512 U8 c4 = get_u8 (); 545 U8 c4 = get_u8 ();
529 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 562 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
530 } 563 }
531 564
532 AV *av = newAV (); 565 AV *av = newAV ();
533 av_fill (av, BER_ARRAYSIZE - 1); 566 av_fill (av, BER_ARRAYSIZE - 1);
534 AvARRAY (av)[BER_CLASS ] = newSVcacheint (klass); 567 AvARRAY (av)[BER_CLASS] = newSVcacheint (klass);
535 AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); 568 AvARRAY (av)[BER_TAG ] = newSVcacheint (tag);
536 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (constructed ? 1 : 0); 569 AvARRAY (av)[BER_FLAGS] = newSVcacheint (constructed ? 1 : 0);
537 AvARRAY (av)[BER_DATA ] = res; 570 AvARRAY (av)[BER_DATA ] = res;
538 571
539 return newRV_noinc ((SV *)av); 572 return newRV_noinc ((SV *)av);
540} 573}
541 574
542///////////////////////////////////////////////////////////////////////////// 575/////////////////////////////////////////////////////////////////////////////
547strlen_sum (STRLEN l1, STRLEN l2) 580strlen_sum (STRLEN l1, STRLEN l2)
548{ 581{
549 size_t sum = l1 + l2; 582 size_t sum = l1 + l2;
550 583
551 if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) 584 if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum)
552 croak ("JSON::XS: string size overflow"); 585 croak ("Convert::BER::XS: string size overflow");
553 586
554 return sum; 587 return sum;
555} 588}
556 589
557static void 590static void
558set_buf (SV *sv) 591set_buf (SV *sv)
559{ 592{
560 STRLEN len; 593 STRLEN len;
561 buf_sv = sv; 594 buf_sv = sv;
562 buf = SvPVbyte (buf_sv, len); 595 buf = (U8 *)SvPVbyte (buf_sv, len);
563 cur = buf; 596 cur = buf;
564 end = buf + len; 597 end = buf + len;
565} 598}
566 599
567/* similar to SvGROW, but somewhat safer and guarantees exponential realloc strategy */ 600/* similar to SvGROW, but somewhat safer and guarantees exponential realloc strategy */
581need (STRLEN len) 614need (STRLEN len)
582{ 615{
583 if (expect_false ((uintptr_t)(end - cur) < len)) 616 if (expect_false ((uintptr_t)(end - cur) < len))
584 { 617 {
585 STRLEN pos = cur - buf; 618 STRLEN pos = cur - buf;
586 buf = my_sv_grow (buf_sv, pos, len); 619 buf = (U8 *)my_sv_grow (buf_sv, pos, len);
587 cur = buf + pos; 620 cur = buf + pos;
588 end = buf + SvLEN (buf_sv) - 1; 621 end = buf + SvLEN (buf_sv) - 1;
589 } 622 }
590} 623}
591 624
595 need (1); 628 need (1);
596 *cur++ = val; 629 *cur++ = val;
597} 630}
598 631
599static void 632static void
600put_w_nocheck (U32 val) 633put_w_nocheck (UV val)
601{ 634{
635#if UVSIZE > 4
636 *cur = (val >> 7 * 9) | 0x80; cur += val >= ((UV)1 << (7 * 9));
637 *cur = (val >> 7 * 8) | 0x80; cur += val >= ((UV)1 << (7 * 8));
638 *cur = (val >> 7 * 7) | 0x80; cur += val >= ((UV)1 << (7 * 7));
639 *cur = (val >> 7 * 6) | 0x80; cur += val >= ((UV)1 << (7 * 6));
640 *cur = (val >> 7 * 5) | 0x80; cur += val >= ((UV)1 << (7 * 5));
641#endif
602 *cur = (val >> 7 * 4) | 0x80; cur += val >= (1 << (7 * 4)); 642 *cur = (val >> 7 * 4) | 0x80; cur += val >= ((UV)1 << (7 * 4));
603 *cur = (val >> 7 * 3) | 0x80; cur += val >= (1 << (7 * 3)); 643 *cur = (val >> 7 * 3) | 0x80; cur += val >= ((UV)1 << (7 * 3));
604 *cur = (val >> 7 * 2) | 0x80; cur += val >= (1 << (7 * 2)); 644 *cur = (val >> 7 * 2) | 0x80; cur += val >= ((UV)1 << (7 * 2));
605 *cur = (val >> 7 * 1) | 0x80; cur += val >= (1 << (7 * 1)); 645 *cur = (val >> 7 * 1) | 0x80; cur += val >= ((UV)1 << (7 * 1));
606 *cur = val & 0x7f; cur += 1; 646 *cur = val & 0x7f; cur += 1;
607} 647}
608 648
609static void 649static void
610put_w (U32 val) 650put_w (UV val)
611{ 651{
612 need (5); // we only handle up to 5 bytes 652 need (5); // we only handle up to 5 bytes
613 653
614 put_w_nocheck (val); 654 put_w_nocheck (val);
615} 655}
616 656
617static U8 * 657static U8 *
618put_length_at (U32 val, U8 *cur) 658put_length_at (UV val, U8 *cur)
619{ 659{
620 if (val < 0x7fU) 660 if (val < 0x7fU)
621 *cur++ = val; 661 *cur++ = val;
622 else 662 else
623 { 663 {
624 U8 *lenb = cur++; 664 U8 *lenb = cur++;
625 665
666#if UVSIZE > 4
667 *cur = val >> 56; cur += *cur > 0;
668 *cur = val >> 48; cur += *cur > 0;
669 *cur = val >> 40; cur += *cur > 0;
670 *cur = val >> 32; cur += *cur > 0;
671#endif
626 *cur = val >> 24; cur += *cur > 0; 672 *cur = val >> 24; cur += *cur > 0;
627 *cur = val >> 16; cur += *cur > 0; 673 *cur = val >> 16; cur += *cur > 0;
628 *cur = val >> 8; cur += *cur > 0; 674 *cur = val >> 8; cur += *cur > 0;
629 *cur = val ; cur += 1; 675 *cur = val ; cur += 1;
630 676
633 679
634 return cur; 680 return cur;
635} 681}
636 682
637static void 683static void
638put_length (U32 val) 684put_length (UV val)
639{ 685{
640 need (5 + val); 686 need (5 + val);
641 cur = put_length_at (val, cur); 687 cur = put_length_at (val, cur);
642} 688}
643 689
644// return how many bytes the encoded length requires 690// return how many bytes the encoded length requires
645static int length_length (U32 val) 691static int length_length (UV val)
646{ 692{
647 return val < 0x7fU 693 return val < 0x7fU
648 ? 1 694 ? 1
649 : 2 + (val > 0xffU) + (val > 0xffffU) + (val > 0xffffffU); 695 : 2
696 + (val > 0xffU)
697 + (val > 0xffffU)
698 + (val > 0xffffffU)
699#if UVSIZE > 4
700 + (val > 0xffffffffU)
701 + (val > 0xffffffffffU)
702 + (val > 0xffffffffffffU)
703 + (val > 0xffffffffffffffU)
704#endif
705 ;
650} 706}
651 707
652static void 708static void
653encode_data (const char *ptr, STRLEN len) 709encode_data (const char *ptr, STRLEN len)
654{ 710{
716 772
717 *lenb = cur - lenb - 1; 773 *lenb = cur - lenb - 1;
718} 774}
719 775
720// we don't know the length yet, so we optimistically 776// we don't know the length yet, so we optimistically
721// assume the length will need one octet later. if that 777// assume the length will need one octet later. If that
722// turns out to be wrong, we memove as needed. 778// turns out to be wrong, we memmove as needed.
723// mark the beginning 779// mark the beginning
724static STRLEN 780static STRLEN
725len_fixup_mark () 781len_fixup_mark (void)
726{ 782{
727 return cur++ - buf; 783 return cur++ - buf;
728} 784}
729 785
730// patch up the length 786// patch up the length
817 put_length (uchars * chrsize); 873 put_length (uchars * chrsize);
818 874
819 while (uchars--) 875 while (uchars--)
820 { 876 {
821 STRLEN uclen; 877 STRLEN uclen;
822 UV uchr = utf8_to_uvchr_buf (ptr, ptr + len, &uclen); 878 UV uchr = utf8_to_uvchr_buf ((U8 *)ptr, (U8 *)ptr + len, &uclen);
823 879
824 ptr += uclen; 880 ptr += uclen;
825 len -= uclen; 881 len -= uclen;
826 882
827 if (chrsize == 4) 883 if (chrsize == 4)
839{ 895{
840 AV *av = ber_tuple (tuple); 896 AV *av = ber_tuple (tuple);
841 897
842 int klass = SvIV (AvARRAY (av)[BER_CLASS]); 898 int klass = SvIV (AvARRAY (av)[BER_CLASS]);
843 int tag = SvIV (AvARRAY (av)[BER_TAG]); 899 int tag = SvIV (AvARRAY (av)[BER_TAG]);
844 int constructed = SvIV (AvARRAY (av)[BER_CONSTRUCTED]) ? ASN_CONSTRUCTED : 0; 900 int constructed = SvIV (AvARRAY (av)[BER_FLAGS]) & 1 ? ASN_CONSTRUCTED : 0;
845 SV *data = AvARRAY (av)[BER_DATA]; 901 SV *data = AvARRAY (av)[BER_DATA];
846 902
847 int identifier = (klass << ASN_CLASS_SHIFT) | constructed; 903 int identifier = (klass << ASN_CLASS_SHIFT) | constructed;
848 904
849 if (expect_false (tag >= ASN_TAG_BER)) 905 if (expect_false (tag >= ASN_TAG_BER))
868 int fill = AvFILL (av); 924 int fill = AvFILL (av);
869 925
870 if (expect_false (SvRMAGICAL (av))) 926 if (expect_false (SvRMAGICAL (av)))
871 croak ("BER constructed data must not be tied"); 927 croak ("BER constructed data must not be tied");
872 928
929 int i;
873 for (int i = 0; i <= fill; ++i) 930 for (i = 0; i <= fill; ++i)
874 encode_ber (AvARRAY (av)[i]); 931 encode_ber (AvARRAY (av)[i]);
875 932
876 len_fixup (mark); 933 len_fixup (mark);
877 } 934 }
878 else 935 else
882 put_length (0); 939 put_length (0);
883 break; 940 break;
884 941
885 case BER_TYPE_BOOL: 942 case BER_TYPE_BOOL:
886 put_length (1); 943 put_length (1);
887 *cur++ = SvTRUE (data) ? 0xff : 0x00; 944 *cur++ = SvTRUE (data) ? 0xff : 0x00; // 0xff = DER/CER
888 break; 945 break;
889 946
890 case BER_TYPE_OID: 947 case BER_TYPE_OID:
891 encode_oid (data, 0); 948 encode_oid (data, 0);
892 break; 949 break;
955 const char *name; 1012 const char *name;
956 IV iv; 1013 IV iv;
957 } *civ, const_iv[] = { 1014 } *civ, const_iv[] = {
958#define const_iv(name) { # name, name }, 1015#define const_iv(name) { # name, name },
959 const_iv (ASN_BOOLEAN) 1016 const_iv (ASN_BOOLEAN)
960 const_iv (ASN_INTEGER32) 1017 const_iv (ASN_INTEGER)
961 const_iv (ASN_BIT_STRING) 1018 const_iv (ASN_BIT_STRING)
962 const_iv (ASN_OCTET_STRING) 1019 const_iv (ASN_OCTET_STRING)
963 const_iv (ASN_NULL) 1020 const_iv (ASN_NULL)
964 const_iv (ASN_OBJECT_IDENTIFIER) 1021 const_iv (ASN_OBJECT_IDENTIFIER)
965 const_iv (ASN_OBJECT_DESCRIPTOR) 1022 const_iv (ASN_OBJECT_DESCRIPTOR)
994 const_iv (ASN_CONTEXT) 1051 const_iv (ASN_CONTEXT)
995 const_iv (ASN_PRIVATE) 1052 const_iv (ASN_PRIVATE)
996 1053
997 const_iv (BER_CLASS) 1054 const_iv (BER_CLASS)
998 const_iv (BER_TAG) 1055 const_iv (BER_TAG)
999 const_iv (BER_CONSTRUCTED) 1056 const_iv (BER_FLAGS)
1000 const_iv (BER_DATA) 1057 const_iv (BER_DATA)
1001 1058
1002 const_iv (BER_TYPE_BYTES) 1059 const_iv (BER_TYPE_BYTES)
1003 const_iv (BER_TYPE_UTF8) 1060 const_iv (BER_TYPE_UTF8)
1004 const_iv (BER_TYPE_UCS2) 1061 const_iv (BER_TYPE_UCS2)
1028ber_decode (SV *ber, SV *profile = &PL_sv_undef) 1085ber_decode (SV *ber, SV *profile = &PL_sv_undef)
1029 CODE: 1086 CODE:
1030{ 1087{
1031 cur_profile = SvPROFILE (profile); 1088 cur_profile = SvPROFILE (profile);
1032 STRLEN len; 1089 STRLEN len;
1033 buf = SvPVbyte (ber, len); 1090 buf = (U8 *)SvPVbyte (ber, len);
1034 cur = buf; 1091 cur = buf;
1035 end = buf + len; 1092 end = buf + len;
1036 1093
1037 RETVAL = decode_ber (); 1094 RETVAL = decode_ber ();
1038} 1095}
1039 OUTPUT: RETVAL 1096 OUTPUT: RETVAL
1040 1097
1041void 1098void
1042ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *constructed = &PL_sv_undef, SV *data = &PL_sv_undef) 1099ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *flags = &PL_sv_undef, SV *data = &PL_sv_undef)
1043 PPCODE: 1100 PPCODE:
1044{ 1101{
1045 if (!SvOK (tuple)) 1102 if (!SvOK (tuple))
1046 XSRETURN_NO; 1103 XSRETURN_NO;
1047 1104
1049 croak ("ber_is: tuple must be BER tuple (array-ref)"); 1106 croak ("ber_is: tuple must be BER tuple (array-ref)");
1050 1107
1051 AV *av = (AV *)SvRV (tuple); 1108 AV *av = (AV *)SvRV (tuple);
1052 1109
1053 XPUSHs ( 1110 XPUSHs (
1054 (!SvOK (klass) || SvIV (AvARRAY (av)[BER_CLASS ]) == SvIV (klass)) 1111 (!SvOK (klass) || SvIV (AvARRAY (av)[BER_CLASS]) == SvIV (klass))
1055 && (!SvOK (tag) || SvIV (AvARRAY (av)[BER_TAG ]) == SvIV (tag)) 1112 && (!SvOK (tag) || SvIV (AvARRAY (av)[BER_TAG ]) == SvIV (tag))
1056 && (!SvOK (constructed) || !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) == !SvIV (constructed)) 1113 && (!SvOK (flags) || !SvIV (AvARRAY (av)[BER_FLAGS]) == !SvIV (flags))
1057 && (!SvOK (data) || sv_eq (AvARRAY (av)[BER_DATA ], data)) 1114 && (!SvOK (data) || sv_eq (AvARRAY (av)[BER_DATA ], data))
1058 ? &PL_sv_yes : &PL_sv_undef); 1115 ? &PL_sv_yes : &PL_sv_undef);
1059} 1116}
1060 1117
1061void 1118void
1062ber_is_seq (SV *tuple) 1119ber_is_seq (SV *tuple)
1066 XSRETURN_UNDEF; 1123 XSRETURN_UNDEF;
1067 1124
1068 AV *av = ber_tuple (tuple); 1125 AV *av = ber_tuple (tuple);
1069 1126
1070 XPUSHs ( 1127 XPUSHs (
1071 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL 1128 SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL
1072 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_SEQUENCE 1129 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_SEQUENCE
1073 && SvIV (AvARRAY (av)[BER_CONSTRUCTED]) 1130 && SvIV (AvARRAY (av)[BER_FLAGS])
1074 ? AvARRAY (av)[BER_DATA] : &PL_sv_undef); 1131 ? AvARRAY (av)[BER_DATA] : &PL_sv_undef);
1075} 1132}
1076 1133
1077void 1134void
1078ber_is_i32 (SV *tuple, SV *value = &PL_sv_undef) 1135ber_is_int (SV *tuple, SV *value = &PL_sv_undef)
1079 PPCODE: 1136 PPCODE:
1080{ 1137{
1081 if (!SvOK (tuple)) 1138 if (!SvOK (tuple))
1082 XSRETURN_NO; 1139 XSRETURN_NO;
1083 1140
1084 AV *av = ber_tuple (tuple); 1141 AV *av = ber_tuple (tuple);
1085 1142
1086 IV data = SvIV (AvARRAY (av)[BER_DATA]); 1143 UV data = SvUV (AvARRAY (av)[BER_DATA]);
1087 1144
1088 XPUSHs ( 1145 XPUSHs (
1089 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL 1146 SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL
1090 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_INTEGER32 1147 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_INTEGER
1091 && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) 1148 && !SvIV (AvARRAY (av)[BER_FLAGS])
1092 && (!SvOK (value) || data == SvIV (value)) 1149 && (!SvOK (value) || data == SvUV (value))
1093 ? sv_2mortal (data ? newSViv (data) : newSVpv ("0 but true", 0)) 1150 ? sv_2mortal (data ? newSVsv (AvARRAY (av)[BER_DATA]) : newSVpv ("0 but true", 0))
1094 : &PL_sv_undef); 1151 : &PL_sv_undef);
1095} 1152}
1096 1153
1097void 1154void
1098ber_is_oid (SV *tuple, SV *oid = &PL_sv_undef) 1155ber_is_oid (SV *tuple, SV *oid = &PL_sv_undef)
1102 XSRETURN_NO; 1159 XSRETURN_NO;
1103 1160
1104 AV *av = ber_tuple (tuple); 1161 AV *av = ber_tuple (tuple);
1105 1162
1106 XPUSHs ( 1163 XPUSHs (
1107 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL 1164 SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL
1108 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_OBJECT_IDENTIFIER 1165 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_OBJECT_IDENTIFIER
1109 && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) 1166 && !SvIV (AvARRAY (av)[BER_FLAGS])
1110 && (!SvOK (oid) || sv_eq (AvARRAY (av)[BER_DATA], oid)) 1167 && (!SvOK (oid) || sv_eq (AvARRAY (av)[BER_DATA], oid))
1111 ? newSVsv (AvARRAY (av)[BER_DATA]) : &PL_sv_undef); 1168 ? newSVsv (AvARRAY (av)[BER_DATA]) : &PL_sv_undef);
1112} 1169}
1113 1170
1114############################################################################# 1171#############################################################################
1127 SvCUR_set (buf_sv, cur - buf); 1184 SvCUR_set (buf_sv, cur - buf);
1128 XPUSHs (buf_sv); 1185 XPUSHs (buf_sv);
1129} 1186}
1130 1187
1131SV * 1188SV *
1132ber_i32 (IV iv) 1189ber_int (SV *sv)
1133 CODE: 1190 CODE:
1134{ 1191{
1135 AV *av = newAV (); 1192 AV *av = newAV ();
1136 av_fill (av, BER_ARRAYSIZE - 1); 1193 av_fill (av, BER_ARRAYSIZE - 1);
1137 AvARRAY (av)[BER_CLASS ] = newSVcacheint (ASN_UNIVERSAL); 1194 AvARRAY (av)[BER_CLASS] = newSVcacheint (ASN_UNIVERSAL);
1138 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_INTEGER32); 1195 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_INTEGER);
1139 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (0); 1196 AvARRAY (av)[BER_FLAGS] = newSVcacheint (0);
1140 AvARRAY (av)[BER_DATA ] = newSViv (iv); 1197 AvARRAY (av)[BER_DATA ] = newSVsv (sv);
1141 RETVAL = newRV_noinc ((SV *)av); 1198 RETVAL = newRV_noinc ((SV *)av);
1142} 1199}
1143 OUTPUT: RETVAL 1200 OUTPUT: RETVAL
1144 1201
1145# TODO: not arrayref, but elements? 1202# TODO: not arrayref, but elements?
1147ber_seq (SV *arrayref) 1204ber_seq (SV *arrayref)
1148 CODE: 1205 CODE:
1149{ 1206{
1150 AV *av = newAV (); 1207 AV *av = newAV ();
1151 av_fill (av, BER_ARRAYSIZE - 1); 1208 av_fill (av, BER_ARRAYSIZE - 1);
1152 AvARRAY (av)[BER_CLASS ] = newSVcacheint (ASN_UNIVERSAL); 1209 AvARRAY (av)[BER_CLASS] = newSVcacheint (ASN_UNIVERSAL);
1153 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_SEQUENCE); 1210 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_SEQUENCE);
1154 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (1); 1211 AvARRAY (av)[BER_FLAGS] = newSVcacheint (1);
1155 AvARRAY (av)[BER_DATA ] = newSVsv (arrayref); 1212 AvARRAY (av)[BER_DATA ] = newSVsv (arrayref);
1156 RETVAL = newRV_noinc ((SV *)av); 1213 RETVAL = newRV_noinc ((SV *)av);
1157} 1214}
1158 OUTPUT: RETVAL 1215 OUTPUT: RETVAL
1159 1216
1160MODULE = Convert::BER::XS PACKAGE = Convert::BER::XS::Profile 1217MODULE = Convert::BER::XS PACKAGE = Convert::BER::XS::Profile

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines