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.24 by root, Sun Apr 21 01:58:15 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,
55 ASN_CLASS_SHIFT = 6, 58 ASN_CLASS_SHIFT = 6,
56 59
57 // ASN_APPLICATION SNMP 60 // ASN_APPLICATION SNMP
58 SNMP_IPADDRESS = 0x00, 61 SNMP_IPADDRESS = 0x00,
59 SNMP_COUNTER32 = 0x01, 62 SNMP_COUNTER32 = 0x01,
63 SNMP_GAUGE32 = 0x02,
60 SNMP_UNSIGNED32 = 0x02, 64 SNMP_UNSIGNED32 = 0x02,
61 SNMP_TIMETICKS = 0x03, 65 SNMP_TIMETICKS = 0x03,
62 SNMP_OPAQUE = 0x04, 66 SNMP_OPAQUE = 0x04,
63 SNMP_COUNTER64 = 0x06, 67 SNMP_COUNTER64 = 0x06,
64}; 68};
77 BER_TYPE_IPADDRESS, 81 BER_TYPE_IPADDRESS,
78 BER_TYPE_CROAK, 82 BER_TYPE_CROAK,
79}; 83};
80 84
81enum { 85enum {
82 BER_CLASS = 0, 86 BER_CLASS = 0,
83 BER_TAG = 1, 87 BER_TAG = 1,
84 BER_CONSTRUCTED = 2, 88 BER_FLAGS = 2,
85 BER_DATA = 3, 89 BER_DATA = 3,
86 BER_ARRAYSIZE 90 BER_ARRAYSIZE
87}; 91};
88 92
89#define MAX_OID_STRLEN 4096 93#define MAX_OID_STRLEN 4096
90 94
91typedef void profile_type; 95typedef void profile_type;
92 96
93static profile_type *cur_profile, *default_profile; 97static profile_type *cur_profile, *default_profile;
94static SV *buf_sv; // encoding buffer 98static SV *buf_sv; // encoding buffer
95static U8 *buf, *cur, *end; // buffer start, current, end 99static U8 *buf, *cur, *end; // buffer start, current, end
100
101#if PERL_VERSION < 18
102# define utf8_to_uvchr_buf(s,e,l) utf8_to_uvchr (s, l)
103#endif
96 104
97#if __GNUC__ >= 3 105#if __GNUC__ >= 3
98# define expect(expr,value) __builtin_expect ((expr), (value)) 106# define expect(expr,value) __builtin_expect ((expr), (value))
99# define INLINE static inline 107# define INLINE static inline
100#else 108#else
155 return BER_TYPE_BYTES; 163 return BER_TYPE_BYTES;
156 164
157 return SvPVX (sv)[idx]; 165 return SvPVX (sv)[idx];
158} 166}
159 167
160static int 168static void
161profile_set (profile_type *profile, int klass, int tag, int type) 169profile_set (profile_type *profile, int klass, int tag, int type)
162{ 170{
163 SV *sv = (SV *)profile; 171 SV *sv = (SV *)profile;
164 U32 idx = (tag << 2) + klass; 172 U32 idx = (tag << 2) + klass;
165 STRLEN oldlen = SvCUR (sv); 173 STRLEN oldlen = SvCUR (sv);
174 182
175 SvPVX (sv)[idx] = type; 183 SvPVX (sv)[idx] = type;
176} 184}
177 185
178static SV * 186static SV *
179profile_new () 187profile_new (void)
180{ 188{
181 SV *sv = newSVpvn ("", 0); 189 SV *sv = newSVpvn ("", 0);
182 190
183 static const struct { 191 static const struct {
184 int klass; 192 int klass;
185 int tag; 193 int tag;
186 int type; 194 int type;
187 } *celem, default_map[] = { 195 } *celem, default_map[] = {
188 { ASN_UNIVERSAL, ASN_BOOLEAN , BER_TYPE_BOOL }, 196 { ASN_UNIVERSAL, ASN_BOOLEAN , BER_TYPE_BOOL },
189 { ASN_UNIVERSAL, ASN_INTEGER32 , BER_TYPE_INT }, 197 { ASN_UNIVERSAL, ASN_INTEGER , BER_TYPE_INT },
190 { ASN_UNIVERSAL, ASN_NULL , BER_TYPE_NULL }, 198 { ASN_UNIVERSAL, ASN_NULL , BER_TYPE_NULL },
191 { ASN_UNIVERSAL, ASN_OBJECT_IDENTIFIER, BER_TYPE_OID }, 199 { 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 }, 200 { ASN_UNIVERSAL, ASN_RELATIVE_OID , BER_TYPE_RELOID },
194 { ASN_UNIVERSAL, ASN_REAL , BER_TYPE_REAL }, 201 { ASN_UNIVERSAL, ASN_REAL , BER_TYPE_REAL },
202 { ASN_UNIVERSAL, ASN_ENUMERATED , BER_TYPE_INT },
195 { ASN_UNIVERSAL, ASN_UTF8_STRING , BER_TYPE_UTF8 }, 203 { ASN_UNIVERSAL, ASN_UTF8_STRING , BER_TYPE_UTF8 },
196 { ASN_UNIVERSAL, ASN_BMP_STRING , BER_TYPE_UCS2 }, 204 { ASN_UNIVERSAL, ASN_BMP_STRING , BER_TYPE_UCS2 },
197 { ASN_UNIVERSAL, ASN_UNIVERSAL_STRING , BER_TYPE_UCS4 }, 205 { ASN_UNIVERSAL, ASN_UNIVERSAL_STRING , BER_TYPE_UCS4 },
198 }; 206 };
199 207
200 for (celem = default_map + sizeof (default_map) / sizeof (default_map [0]); celem > default_map; celem--) 208 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); 209 profile_set ((profile_type *)sv, celem->klass, celem->tag, celem->type);
202 210
203 return sv_bless (newRV_noinc (sv), profile_stash); 211 return sv_bless (newRV_noinc (sv), profile_stash);
204} 212}
205 213
206///////////////////////////////////////////////////////////////////////////// 214/////////////////////////////////////////////////////////////////////////////
241 249
242 return *cur++; 250 return *cur++;
243} 251}
244 252
245// get ber-encoded integer (i.e. pack "w") 253// get ber-encoded integer (i.e. pack "w")
246static U32 254static UV
247get_w (void) 255get_w (void)
248{ 256{
249 U32 res = 0; 257 UV res = 0;
258 U8 c = get_u8 ();
259
260 if (expect_false (c == 0x80))
261 error ("illegal BER padding (X.690 8.1.2.4.2, 8.19.2)");
250 262
251 for (;;) 263 for (;;)
252 { 264 {
253 U8 c = get_u8 (); 265 if (expect_false (res >> UVSIZE * 8 - 7))
266 error ("BER variable integer overflow");
267
254 res = (res << 7) | (c & 0x7f); 268 res = (res << 7) | (c & 0x7f);
255 269
256 if (!(c & 0x80)) 270 if (!(c & 0x80))
257 return res; 271 return res;
258 }
259}
260 272
273 c = get_u8 ();
274 }
275}
276
261static U32 277static UV
262get_length (void) 278get_length (void)
263{ 279{
264 U32 res = get_u8 (); 280 UV res = get_u8 ();
265 281
266 if (res & 0x80) 282 if (res & 0x80)
267 { 283 {
268 int cnt = res & 0x7f; 284 int cnt = res & 0x7f;
269 res = 0; 285 res = 0;
270 286
271 switch (cnt) 287 switch (cnt)
272 { 288 {
273 case 0: 289 case 0:
274 error ("indefinite ASN.1 lengths not supported"); 290 error ("indefinite ASN.1 lengths not supported");
275 return 0; 291
292 case 0x7f:
293 error ("ASN.1 reserved value in length (X.690 8.1.3.5)");
276 294
277 default: 295 default:
278 error ("ASN.1 length too long"); 296 error ("ASN.1 length too long (only up to 2**64 octets supported)");
279 return 0;
280 297
298 case 8: res = (res << 8) | get_u8 ();
299 case 7: res = (res << 8) | get_u8 ();
300 case 6: res = (res << 8) | get_u8 ();
301 case 5: res = (res << 8) | get_u8 ();
281 case 4: res = (res << 8) | get_u8 (); 302 case 4: res = (res << 8) | get_u8 ();
282 case 3: res = (res << 8) | get_u8 (); 303 case 3: res = (res << 8) | get_u8 ();
283 case 2: res = (res << 8) | get_u8 (); 304 case 2: res = (res << 8) | get_u8 ();
284 case 1: res = (res << 8) | get_u8 (); 305 case 1: res = (res << 8) | get_u8 ();
285 } 306 }
287 308
288 return res; 309 return res;
289} 310}
290 311
291static SV * 312static SV *
292decode_int () 313decode_int (void)
293{ 314{
294 int len = get_length (); 315 UV len = get_length ();
295 316
296 if (len <= 0) 317 if (!len)
297 {
298 error ("integer length equal to zero"); 318 error ("invalid integer length equal to zero (X.690 8.3.1)");
299 return 0;
300 }
301 319
302 U8 *data = get_n (len); 320 U8 *data = get_n (len);
303 321
322 if (expect_false (len > 1))
323 {
324 U16 mask = (data [0] << 8) | data [1] & 0xff80;
325
326 if (expect_false (mask == 0xff80 || mask == 0x0000))
327 error ("illegal padding in integer (X.690 8.3.2)");
328 }
329
304 int negative = data [0] & 0x80; 330 int negative = data [0] & 0x80;
305 331
306 UV val = negative ? -1 : 0; // copy signbit to all bits 332 UV val = negative ? -1 : 0; // copy signbit to all bits
333
334 if (len > UVSIZE + (!negative && !*data))
335 error ("INTEGER overflow");
307 336
308 do 337 do
309 val = (val << 8) | *data++; 338 val = (val << 8) | *data++;
310 while (--len); 339 while (--len);
311 340
315} 344}
316 345
317static SV * 346static SV *
318decode_data (void) 347decode_data (void)
319{ 348{
320 U32 len = get_length (); 349 UV len = get_length ();
321 U8 *data = get_n (len);
322 return newSVpvn ((char *)data, len); 350 return newSVpvn ((char *)get_n (len), len);
323} 351}
324 352
325// gelper for decode_object_identifier 353// helper for decode_object_identifier
326static char * 354static char *
327write_uv (char *buf, U32 u) 355write_uv (char *buf, UV u)
328{ 356{
329 // the one-digit case is absolutely predominant, so this pays off (hopefully) 357 // the one-digit case is absolutely predominant, so this pays off (hopefully)
330 if (expect_true (u < 10)) 358 if (expect_true (u < 10))
331 *buf++ = u + '0'; 359 *buf++ = u + '0';
332 else 360 else
333 { 361 {
362 // this *could* be done much faster using branchless fixed-point arithmetics
334 char *beg = buf; 363 char *beg = buf;
335 364
336 do 365 do
337 { 366 {
338 *buf++ = u % 10 + '0'; 367 *buf++ = u % 10 + '0';
339 u /= 10; 368 u /= 10;
340 } 369 }
341 while (u); 370 while (u);
342 371
343 // reverse digits 372 // reverse digits
344 for (char *ptr = buf; --ptr != beg; ++beg) 373 char *ptr = buf;
374 while (--ptr > beg)
345 { 375 {
346 char c = *ptr; 376 char c = *ptr;
347 *ptr = *beg; 377 *ptr = *beg;
348 *beg = c; 378 *beg = c;
379 ++beg;
349 } 380 }
350 } 381 }
351 382
352 return buf; 383 return buf;
353} 384}
354 385
355static SV * 386static SV *
356decode_oid (int relative) 387decode_oid (int relative)
357{ 388{
358 U32 len = get_length (); 389 UV len = get_length ();
359 390
360 if (len <= 0) 391 if (len <= 0)
361 { 392 {
362 error ("OBJECT IDENTIFIER length equal to zero"); 393 error ("OBJECT IDENTIFIER length equal to zero");
363 return &PL_sv_undef; 394 return &PL_sv_undef;
364 } 395 }
365 396
366 U8 *end = cur + len; 397 U8 *end = cur + len;
367 U32 w = get_w (); 398 UV w = get_w ();
368 399
369 static char oid[MAX_OID_STRLEN]; // must be static 400 static char oid[MAX_OID_STRLEN]; // static, because too large for stack
370 char *app = oid; 401 char *app = oid;
371 402
372 if (relative) 403 if (relative)
373 app = write_uv (app, w); 404 app = write_uv (app, w);
374 else 405 else if (w < 2 * 40)
375 { 406 {
376 app = write_uv (app, (U8)w / 40); 407 app = write_uv (app, (U8)w / 40);
377 *app++ = '.'; 408 *app++ = '.';
378 app = write_uv (app, (U8)w % 40); 409 app = write_uv (app, (U8)w % 40);
379 } 410 }
411 else
412 {
413 app = write_uv (app, 2);
414 *app++ = '.';
415 app = write_uv (app, w - 2 * 40);
416 }
380 417
418 while (cur < end)
419 {
381 // we assume an oid component is never > 64 bytes 420 // we assume an oid component is never > 64 digits
382 while (cur < end && oid + sizeof (oid) - app > 64) 421 if (oid + sizeof (oid) - app < 64)
383 { 422 croak ("BER_TYPE_OID to long to decode");
423
384 w = get_w (); 424 w = get_w ();
385 *app++ = '.'; 425 *app++ = '.';
386 app = write_uv (app, w); 426 app = write_uv (app, w);
387 } 427 }
388 428
393static SV * 433static SV *
394decode_ucs (int chrsize) 434decode_ucs (int chrsize)
395{ 435{
396 SV *res = NEWSV (0, 0); 436 SV *res = NEWSV (0, 0);
397 437
398 U32 len = get_length (); 438 UV len = get_length ();
399 439
400 if (len & (chrsize - 1)) 440 if (len & (chrsize - 1))
401 croak ("BER_TYPE_UCS has an invalid number of octets (%d)", len); 441 croak ("BER_TYPE_UCS has an invalid number of octets (%d)", len);
402 442
403 while (len) 443 while (len)
424 464
425 return res; 465 return res;
426} 466}
427 467
428static SV * 468static SV *
429decode_ber () 469decode_ber (void)
430{ 470{
431 int identifier = get_u8 (); 471 int identifier = get_u8 ();
432 472
433 SV *res; 473 SV *res;
434 474
437 int tag = identifier & ASN_TAG_MASK; 477 int tag = identifier & ASN_TAG_MASK;
438 478
439 if (tag == ASN_TAG_BER) 479 if (tag == ASN_TAG_BER)
440 tag = get_w (); 480 tag = get_w ();
441 481
442 if (tag == ASN_TAG_BER)
443 tag = get_w ();
444
445 if (constructed) 482 if (constructed)
446 { 483 {
447 U32 len = get_length (); 484 UV len = get_length ();
448 U32 seqend = (cur - buf) + len; 485 UV seqend = (cur - buf) + len;
449 AV *av = (AV *)sv_2mortal ((SV *)newAV ()); 486 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
450 487
451 while (cur < buf + seqend) 488 while (cur < buf + seqend)
452 av_push (av, decode_ber ()); 489 av_push (av, decode_ber ());
453 490
454 if (cur > buf + seqend) 491 if (cur > buf + seqend)
455 croak ("constructed type %02x overflow (%x %x)\n", identifier, cur - buf, seqend); 492 croak ("constructed type %02x length overflow (0x%x 0x%x)\n", identifier, (int)(cur - buf), (int)seqend);
456 493
457 res = newRV_inc ((SV *)av); 494 res = newRV_inc ((SV *)av);
458 } 495 }
459 else 496 else
460 switch (profile_lookup (cur_profile, klass, tag)) 497 switch (profile_lookup (cur_profile, klass, tag))
461 { 498 {
462 case BER_TYPE_NULL: 499 case BER_TYPE_NULL:
500 {
501 UV len = get_length ();
502
503 if (len)
504 croak ("BER_TYPE_NULL value with non-zero length %d encountered (X.690 8.8.2)", len);
505
463 res = &PL_sv_undef; 506 res = &PL_sv_undef;
507 }
464 break; 508 break;
465 509
466 case BER_TYPE_BOOL: 510 case BER_TYPE_BOOL:
467 { 511 {
468 U32 len = get_length (); 512 UV len = get_length ();
469 513
470 if (len != 1) 514 if (len != 1)
471 croak ("BER_TYPE_BOOLEAN type with invalid length %d encountered", len); 515 croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered (X.690 8.2.1)", len);
472 516
473 res = newSVcacheint (get_u8 () ? 0 : 1); 517 res = newSVcacheint (!!get_u8 ());
474 } 518 }
475 break; 519 break;
476 520
477 case BER_TYPE_OID: 521 case BER_TYPE_OID:
478 res = decode_oid (0); 522 res = decode_oid (0);
495 res = decode_data (); 539 res = decode_data ();
496 break; 540 break;
497 541
498 case BER_TYPE_IPADDRESS: 542 case BER_TYPE_IPADDRESS:
499 { 543 {
500 U32 len = get_length (); 544 UV len = get_length ();
501 545
502 if (len != 4) 546 if (len != 4)
503 croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered", len); 547 croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered (RFC 2578 7.1.5)", len);
504 548
505 U8 c1 = get_u8 (); 549 U8 c1 = get_u8 ();
506 U8 c2 = get_u8 (); 550 U8 c2 = get_u8 ();
507 U8 c3 = get_u8 (); 551 U8 c3 = get_u8 ();
508 U8 c4 = get_u8 (); 552 U8 c4 = get_u8 ();
525 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 569 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
526 } 570 }
527 571
528 AV *av = newAV (); 572 AV *av = newAV ();
529 av_fill (av, BER_ARRAYSIZE - 1); 573 av_fill (av, BER_ARRAYSIZE - 1);
530 AvARRAY (av)[BER_CLASS ] = newSVcacheint (klass); 574 AvARRAY (av)[BER_CLASS] = newSVcacheint (klass);
531 AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); 575 AvARRAY (av)[BER_TAG ] = newSVcacheint (tag);
532 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (constructed ? 1 : 0); 576 AvARRAY (av)[BER_FLAGS] = newSVcacheint (constructed ? 1 : 0);
533 AvARRAY (av)[BER_DATA ] = res; 577 AvARRAY (av)[BER_DATA ] = res;
534 578
535 return newRV_noinc ((SV *)av); 579 return newRV_noinc ((SV *)av);
536} 580}
537 581
538///////////////////////////////////////////////////////////////////////////// 582/////////////////////////////////////////////////////////////////////////////
543strlen_sum (STRLEN l1, STRLEN l2) 587strlen_sum (STRLEN l1, STRLEN l2)
544{ 588{
545 size_t sum = l1 + l2; 589 size_t sum = l1 + l2;
546 590
547 if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) 591 if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum)
548 croak ("JSON::XS: string size overflow"); 592 croak ("Convert::BER::XS: string size overflow");
549 593
550 return sum; 594 return sum;
551} 595}
552 596
553static void 597static void
554set_buf (SV *sv) 598set_buf (SV *sv)
555{ 599{
556 STRLEN len; 600 STRLEN len;
557 buf_sv = sv; 601 buf_sv = sv;
558 buf = SvPVbyte (buf_sv, len); 602 buf = (U8 *)SvPVbyte (buf_sv, len);
559 cur = buf; 603 cur = buf;
560 end = buf + len; 604 end = buf + len;
561} 605}
562 606
563/* similar to SvGROW, but somewhat safer and guarantees exponential realloc strategy */ 607/* similar to SvGROW, but somewhat safer and guarantees exponential realloc strategy */
577need (STRLEN len) 621need (STRLEN len)
578{ 622{
579 if (expect_false ((uintptr_t)(end - cur) < len)) 623 if (expect_false ((uintptr_t)(end - cur) < len))
580 { 624 {
581 STRLEN pos = cur - buf; 625 STRLEN pos = cur - buf;
582 buf = my_sv_grow (buf_sv, pos, len); 626 buf = (U8 *)my_sv_grow (buf_sv, pos, len);
583 cur = buf + pos; 627 cur = buf + pos;
584 end = buf + SvLEN (buf_sv) - 1; 628 end = buf + SvLEN (buf_sv) - 1;
585 } 629 }
586} 630}
587 631
591 need (1); 635 need (1);
592 *cur++ = val; 636 *cur++ = val;
593} 637}
594 638
595static void 639static void
596put_w_nocheck (U32 val) 640put_w_nocheck (UV val)
597{ 641{
642#if UVSIZE > 4
643 *cur = (val >> 7 * 9) | 0x80; cur += val >= ((UV)1 << (7 * 9));
644 *cur = (val >> 7 * 8) | 0x80; cur += val >= ((UV)1 << (7 * 8));
645 *cur = (val >> 7 * 7) | 0x80; cur += val >= ((UV)1 << (7 * 7));
646 *cur = (val >> 7 * 6) | 0x80; cur += val >= ((UV)1 << (7 * 6));
647 *cur = (val >> 7 * 5) | 0x80; cur += val >= ((UV)1 << (7 * 5));
648#endif
598 *cur = (val >> 7 * 4) | 0x80; cur += val >= (1 << (7 * 4)); 649 *cur = (val >> 7 * 4) | 0x80; cur += val >= ((UV)1 << (7 * 4));
599 *cur = (val >> 7 * 3) | 0x80; cur += val >= (1 << (7 * 3)); 650 *cur = (val >> 7 * 3) | 0x80; cur += val >= ((UV)1 << (7 * 3));
600 *cur = (val >> 7 * 2) | 0x80; cur += val >= (1 << (7 * 2)); 651 *cur = (val >> 7 * 2) | 0x80; cur += val >= ((UV)1 << (7 * 2));
601 *cur = (val >> 7 * 1) | 0x80; cur += val >= (1 << (7 * 1)); 652 *cur = (val >> 7 * 1) | 0x80; cur += val >= ((UV)1 << (7 * 1));
602 *cur = val & 0x7f; cur += 1; 653 *cur = val & 0x7f; cur += 1;
603} 654}
604 655
605static void 656static void
606put_w (U32 val) 657put_w (UV val)
607{ 658{
608 need (5); // we only handle up to 5 bytes 659 need (5); // we only handle up to 5 bytes
609 660
610 put_w_nocheck (val); 661 put_w_nocheck (val);
611} 662}
612 663
613static U8 * 664static U8 *
614put_length_at (U32 val, U8 *cur) 665put_length_at (UV val, U8 *cur)
615{ 666{
616 if (val < 0x7fU) 667 if (val < 0x7fU)
617 *cur++ = val; 668 *cur++ = val;
618 else 669 else
619 { 670 {
620 U8 *lenb = cur++; 671 U8 *lenb = cur++;
621 672
673#if UVSIZE > 4
674 *cur = val >> 56; cur += *cur > 0;
675 *cur = val >> 48; cur += *cur > 0;
676 *cur = val >> 40; cur += *cur > 0;
677 *cur = val >> 32; cur += *cur > 0;
678#endif
622 *cur = val >> 24; cur += *cur > 0; 679 *cur = val >> 24; cur += *cur > 0;
623 *cur = val >> 16; cur += *cur > 0; 680 *cur = val >> 16; cur += *cur > 0;
624 *cur = val >> 8; cur += *cur > 0; 681 *cur = val >> 8; cur += *cur > 0;
625 *cur = val ; cur += 1; 682 *cur = val ; cur += 1;
626 683
629 686
630 return cur; 687 return cur;
631} 688}
632 689
633static void 690static void
634put_length (U32 val) 691put_length (UV val)
635{ 692{
636 need (5 + val); 693 need (5 + val);
637 cur = put_length_at (val, cur); 694 cur = put_length_at (val, cur);
638} 695}
639 696
640// return how many bytes the encoded length requires 697// return how many bytes the encoded length requires
641static int length_length (U32 val) 698static int length_length (UV val)
642{ 699{
643 return val < 0x7fU 700 return val < 0x7fU
644 ? 1 701 ? 1
645 : 2 + (val > 0xffU) + (val > 0xffffU) + (val > 0xffffffU); 702 : 2
703 + (val > 0xffU)
704 + (val > 0xffffU)
705 + (val > 0xffffffU)
706#if UVSIZE > 4
707 + (val > 0xffffffffU)
708 + (val > 0xffffffffffU)
709 + (val > 0xffffffffffffU)
710 + (val > 0xffffffffffffffU)
711#endif
712 ;
646} 713}
647 714
648static void 715static void
649encode_data (const char *ptr, STRLEN len) 716encode_data (const char *ptr, STRLEN len)
650{ 717{
712 779
713 *lenb = cur - lenb - 1; 780 *lenb = cur - lenb - 1;
714} 781}
715 782
716// we don't know the length yet, so we optimistically 783// we don't know the length yet, so we optimistically
717// assume the length will need one octet later. if that 784// assume the length will need one octet later. If that
718// turns out to be wrong, we memove as needed. 785// turns out to be wrong, we memmove as needed.
719// mark the beginning 786// mark the beginning
720static STRLEN 787static STRLEN
721len_fixup_mark () 788len_fixup_mark (void)
722{ 789{
723 return cur++ - buf; 790 return cur++ - buf;
724} 791}
725 792
726// patch up the length 793// patch up the length
813 put_length (uchars * chrsize); 880 put_length (uchars * chrsize);
814 881
815 while (uchars--) 882 while (uchars--)
816 { 883 {
817 STRLEN uclen; 884 STRLEN uclen;
818 UV uchr = utf8_to_uvchr_buf (ptr, ptr + len, &uclen); 885 UV uchr = utf8_to_uvchr_buf ((U8 *)ptr, (U8 *)ptr + len, &uclen);
819 886
820 ptr += uclen; 887 ptr += uclen;
821 len -= uclen; 888 len -= uclen;
822 889
823 if (chrsize == 4) 890 if (chrsize == 4)
835{ 902{
836 AV *av = ber_tuple (tuple); 903 AV *av = ber_tuple (tuple);
837 904
838 int klass = SvIV (AvARRAY (av)[BER_CLASS]); 905 int klass = SvIV (AvARRAY (av)[BER_CLASS]);
839 int tag = SvIV (AvARRAY (av)[BER_TAG]); 906 int tag = SvIV (AvARRAY (av)[BER_TAG]);
840 int constructed = SvIV (AvARRAY (av)[BER_CONSTRUCTED]) ? ASN_CONSTRUCTED : 0; 907 int constructed = SvIV (AvARRAY (av)[BER_FLAGS]) & 1 ? ASN_CONSTRUCTED : 0;
841 SV *data = AvARRAY (av)[BER_DATA]; 908 SV *data = AvARRAY (av)[BER_DATA];
842 909
843 int identifier = (klass << ASN_CLASS_SHIFT) | constructed; 910 int identifier = (klass << ASN_CLASS_SHIFT) | constructed;
844 911
845 if (expect_false (tag >= ASN_TAG_BER)) 912 if (expect_false (tag >= ASN_TAG_BER))
864 int fill = AvFILL (av); 931 int fill = AvFILL (av);
865 932
866 if (expect_false (SvRMAGICAL (av))) 933 if (expect_false (SvRMAGICAL (av)))
867 croak ("BER constructed data must not be tied"); 934 croak ("BER constructed data must not be tied");
868 935
936 int i;
869 for (int i = 0; i <= fill; ++i) 937 for (i = 0; i <= fill; ++i)
870 encode_ber (AvARRAY (av)[i]); 938 encode_ber (AvARRAY (av)[i]);
871 939
872 len_fixup (mark); 940 len_fixup (mark);
873 } 941 }
874 else 942 else
878 put_length (0); 946 put_length (0);
879 break; 947 break;
880 948
881 case BER_TYPE_BOOL: 949 case BER_TYPE_BOOL:
882 put_length (1); 950 put_length (1);
883 *cur++ = SvTRUE (data) ? 0xff : 0x00; 951 *cur++ = SvTRUE (data) ? 0xff : 0x00; // 0xff = DER/CER
884 break; 952 break;
885 953
886 case BER_TYPE_OID: 954 case BER_TYPE_OID:
887 encode_oid (data, 0); 955 encode_oid (data, 0);
888 break; 956 break;
951 const char *name; 1019 const char *name;
952 IV iv; 1020 IV iv;
953 } *civ, const_iv[] = { 1021 } *civ, const_iv[] = {
954#define const_iv(name) { # name, name }, 1022#define const_iv(name) { # name, name },
955 const_iv (ASN_BOOLEAN) 1023 const_iv (ASN_BOOLEAN)
956 const_iv (ASN_INTEGER32) 1024 const_iv (ASN_INTEGER)
957 const_iv (ASN_BIT_STRING) 1025 const_iv (ASN_BIT_STRING)
958 const_iv (ASN_OCTET_STRING) 1026 const_iv (ASN_OCTET_STRING)
959 const_iv (ASN_NULL) 1027 const_iv (ASN_NULL)
960 const_iv (ASN_OBJECT_IDENTIFIER) 1028 const_iv (ASN_OBJECT_IDENTIFIER)
961 const_iv (ASN_OBJECT_DESCRIPTOR) 1029 const_iv (ASN_OBJECT_DESCRIPTOR)
990 const_iv (ASN_CONTEXT) 1058 const_iv (ASN_CONTEXT)
991 const_iv (ASN_PRIVATE) 1059 const_iv (ASN_PRIVATE)
992 1060
993 const_iv (BER_CLASS) 1061 const_iv (BER_CLASS)
994 const_iv (BER_TAG) 1062 const_iv (BER_TAG)
995 const_iv (BER_CONSTRUCTED) 1063 const_iv (BER_FLAGS)
996 const_iv (BER_DATA) 1064 const_iv (BER_DATA)
997 1065
998 const_iv (BER_TYPE_BYTES) 1066 const_iv (BER_TYPE_BYTES)
999 const_iv (BER_TYPE_UTF8) 1067 const_iv (BER_TYPE_UTF8)
1000 const_iv (BER_TYPE_UCS2) 1068 const_iv (BER_TYPE_UCS2)
1008 const_iv (BER_TYPE_IPADDRESS) 1076 const_iv (BER_TYPE_IPADDRESS)
1009 const_iv (BER_TYPE_CROAK) 1077 const_iv (BER_TYPE_CROAK)
1010 1078
1011 const_iv (SNMP_IPADDRESS) 1079 const_iv (SNMP_IPADDRESS)
1012 const_iv (SNMP_COUNTER32) 1080 const_iv (SNMP_COUNTER32)
1081 const_iv (SNMP_GAUGE32)
1013 const_iv (SNMP_UNSIGNED32) 1082 const_iv (SNMP_UNSIGNED32)
1014 const_iv (SNMP_TIMETICKS) 1083 const_iv (SNMP_TIMETICKS)
1015 const_iv (SNMP_OPAQUE) 1084 const_iv (SNMP_OPAQUE)
1016 const_iv (SNMP_COUNTER64) 1085 const_iv (SNMP_COUNTER64)
1017 }; 1086 };
1018 1087
1019 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) 1088 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--)
1020 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); 1089 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv));
1021} 1090}
1022 1091
1023SV * 1092void
1024ber_decode (SV *ber, SV *profile = &PL_sv_undef) 1093ber_decode (SV *ber, SV *profile = &PL_sv_undef)
1094 ALIAS:
1095 ber_decode_prefix = 1
1025 CODE: 1096 PPCODE:
1026{ 1097{
1027 cur_profile = SvPROFILE (profile); 1098 cur_profile = SvPROFILE (profile);
1028 STRLEN len; 1099 STRLEN len;
1029 buf = SvPVbyte (ber, len); 1100 buf = (U8 *)SvPVbyte (ber, len);
1030 cur = buf; 1101 cur = buf;
1031 end = buf + len; 1102 end = buf + len;
1032 1103
1033 RETVAL = decode_ber (); 1104 SV *tuple = decode_ber ();
1105
1106 EXTEND (SP, 2);
1107 PUSHs (sv_2mortal (tuple));
1108
1109 if (ix)
1110 PUSHs (sv_2mortal (newSViv (cur - buf)));
1111 else if (cur != end)
1112 error ("trailing garbage after BER data");
1034} 1113}
1035 OUTPUT: RETVAL
1036 1114
1037void 1115void
1038ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *constructed = &PL_sv_undef, SV *data = &PL_sv_undef) 1116ber_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: 1117 PPCODE:
1040{ 1118{
1041 if (!SvOK (tuple)) 1119 if (!SvOK (tuple))
1042 XSRETURN_NO; 1120 XSRETURN_NO;
1043 1121
1045 croak ("ber_is: tuple must be BER tuple (array-ref)"); 1123 croak ("ber_is: tuple must be BER tuple (array-ref)");
1046 1124
1047 AV *av = (AV *)SvRV (tuple); 1125 AV *av = (AV *)SvRV (tuple);
1048 1126
1049 XPUSHs ( 1127 XPUSHs (
1050 (!SvOK (klass) || SvIV (AvARRAY (av)[BER_CLASS ]) == SvIV (klass)) 1128 (!SvOK (klass) || SvIV (AvARRAY (av)[BER_CLASS]) == SvIV (klass))
1051 && (!SvOK (tag) || SvIV (AvARRAY (av)[BER_TAG ]) == SvIV (tag)) 1129 && (!SvOK (tag) || SvIV (AvARRAY (av)[BER_TAG ]) == SvIV (tag))
1052 && (!SvOK (constructed) || !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) == !SvIV (constructed)) 1130 && (!SvOK (flags) || !SvIV (AvARRAY (av)[BER_FLAGS]) == !SvIV (flags))
1053 && (!SvOK (data) || sv_eq (AvARRAY (av)[BER_DATA ], data)) 1131 && (!SvOK (data) || sv_eq (AvARRAY (av)[BER_DATA ], data))
1054 ? &PL_sv_yes : &PL_sv_undef); 1132 ? &PL_sv_yes : &PL_sv_undef);
1055} 1133}
1056 1134
1057void 1135void
1058ber_is_seq (SV *tuple) 1136ber_is_seq (SV *tuple)
1062 XSRETURN_UNDEF; 1140 XSRETURN_UNDEF;
1063 1141
1064 AV *av = ber_tuple (tuple); 1142 AV *av = ber_tuple (tuple);
1065 1143
1066 XPUSHs ( 1144 XPUSHs (
1067 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL 1145 SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL
1068 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_SEQUENCE 1146 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_SEQUENCE
1069 && SvIV (AvARRAY (av)[BER_CONSTRUCTED]) 1147 && SvIV (AvARRAY (av)[BER_FLAGS])
1070 ? AvARRAY (av)[BER_DATA] : &PL_sv_undef); 1148 ? AvARRAY (av)[BER_DATA] : &PL_sv_undef);
1071} 1149}
1072 1150
1073void 1151void
1074ber_is_i32 (SV *tuple, SV *value = &PL_sv_undef) 1152ber_is_int (SV *tuple, SV *value = &PL_sv_undef)
1075 PPCODE: 1153 PPCODE:
1076{ 1154{
1077 if (!SvOK (tuple)) 1155 if (!SvOK (tuple))
1078 XSRETURN_NO; 1156 XSRETURN_NO;
1079 1157
1080 AV *av = ber_tuple (tuple); 1158 AV *av = ber_tuple (tuple);
1081 1159
1082 IV data = SvIV (AvARRAY (av)[BER_DATA]); 1160 UV data = SvUV (AvARRAY (av)[BER_DATA]);
1083 1161
1084 XPUSHs ( 1162 XPUSHs (
1085 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL 1163 SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL
1086 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_INTEGER32 1164 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_INTEGER
1087 && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) 1165 && !SvIV (AvARRAY (av)[BER_FLAGS])
1088 && (!SvOK (value) || data == SvIV (value)) 1166 && (!SvOK (value) || data == SvUV (value))
1089 ? sv_2mortal (data ? newSViv (data) : newSVpv ("0 but true", 0)) 1167 ? sv_2mortal (data ? newSVsv (AvARRAY (av)[BER_DATA]) : newSVpv ("0 but true", 0))
1090 : &PL_sv_undef); 1168 : &PL_sv_undef);
1091} 1169}
1092 1170
1093void 1171void
1094ber_is_oid (SV *tuple, SV *oid = &PL_sv_undef) 1172ber_is_oid (SV *tuple, SV *oid = &PL_sv_undef)
1098 XSRETURN_NO; 1176 XSRETURN_NO;
1099 1177
1100 AV *av = ber_tuple (tuple); 1178 AV *av = ber_tuple (tuple);
1101 1179
1102 XPUSHs ( 1180 XPUSHs (
1103 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL 1181 SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL
1104 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_OBJECT_IDENTIFIER 1182 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_OBJECT_IDENTIFIER
1105 && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) 1183 && !SvIV (AvARRAY (av)[BER_FLAGS])
1106 && (!SvOK (oid) || sv_eq (AvARRAY (av)[BER_DATA], oid)) 1184 && (!SvOK (oid) || sv_eq (AvARRAY (av)[BER_DATA], oid))
1107 ? newSVsv (AvARRAY (av)[BER_DATA]) : &PL_sv_undef); 1185 ? newSVsv (AvARRAY (av)[BER_DATA]) : &PL_sv_undef);
1108} 1186}
1109 1187
1110############################################################################# 1188#############################################################################
1123 SvCUR_set (buf_sv, cur - buf); 1201 SvCUR_set (buf_sv, cur - buf);
1124 XPUSHs (buf_sv); 1202 XPUSHs (buf_sv);
1125} 1203}
1126 1204
1127SV * 1205SV *
1128ber_i32 (IV iv) 1206ber_int (SV *sv)
1129 CODE: 1207 CODE:
1130{ 1208{
1131 AV *av = newAV (); 1209 AV *av = newAV ();
1132 av_fill (av, BER_ARRAYSIZE - 1); 1210 av_fill (av, BER_ARRAYSIZE - 1);
1133 AvARRAY (av)[BER_CLASS ] = newSVcacheint (ASN_UNIVERSAL); 1211 AvARRAY (av)[BER_CLASS] = newSVcacheint (ASN_UNIVERSAL);
1134 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_INTEGER32); 1212 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_INTEGER);
1135 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (0); 1213 AvARRAY (av)[BER_FLAGS] = newSVcacheint (0);
1136 AvARRAY (av)[BER_DATA ] = newSViv (iv); 1214 AvARRAY (av)[BER_DATA ] = newSVsv (sv);
1137 RETVAL = newRV_noinc ((SV *)av); 1215 RETVAL = newRV_noinc ((SV *)av);
1138} 1216}
1139 OUTPUT: RETVAL 1217 OUTPUT: RETVAL
1140 1218
1141# TODO: not arrayref, but elements? 1219# TODO: not arrayref, but elements?
1143ber_seq (SV *arrayref) 1221ber_seq (SV *arrayref)
1144 CODE: 1222 CODE:
1145{ 1223{
1146 AV *av = newAV (); 1224 AV *av = newAV ();
1147 av_fill (av, BER_ARRAYSIZE - 1); 1225 av_fill (av, BER_ARRAYSIZE - 1);
1148 AvARRAY (av)[BER_CLASS ] = newSVcacheint (ASN_UNIVERSAL); 1226 AvARRAY (av)[BER_CLASS] = newSVcacheint (ASN_UNIVERSAL);
1149 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_SEQUENCE); 1227 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_SEQUENCE);
1150 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (1); 1228 AvARRAY (av)[BER_FLAGS] = newSVcacheint (1);
1151 AvARRAY (av)[BER_DATA ] = newSVsv (arrayref); 1229 AvARRAY (av)[BER_DATA ] = newSVsv (arrayref);
1152 RETVAL = newRV_noinc ((SV *)av); 1230 RETVAL = newRV_noinc ((SV *)av);
1153} 1231}
1154 OUTPUT: RETVAL 1232 OUTPUT: RETVAL
1155 1233
1156MODULE = Convert::BER::XS PACKAGE = Convert::BER::XS::Profile 1234MODULE = Convert::BER::XS PACKAGE = Convert::BER::XS::Profile

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines