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