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.6 by root, Sat Apr 20 01:03:59 2019 UTC vs.
Revision 1.16 by root, Sat Apr 20 15:37:27 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, //X 18 ASN_OID = 0x06,
16 ASN_OBJECT_DESCRIPTOR = 0x07, //X 19 ASN_OBJECT_DESCRIPTOR = 0x07,
17 ASN_EXTERNAL = 0x08, //X 20 ASN_EXTERNAL = 0x08,
18 ASN_REAL = 0x09, //X 21 ASN_REAL = 0x09,
19 ASN_ENUMERATED = 0x0a, //X 22 ASN_ENUMERATED = 0x0a,
20 ASN_EMBEDDED_PDV = 0x0b, //X 23 ASN_EMBEDDED_PDV = 0x0b,
21 ASN_UTF8_STRING = 0x0c, //X 24 ASN_UTF8_STRING = 0x0c,
22 ASN_RELATIVE_OID = 0x0d, //X 25 ASN_RELATIVE_OID = 0x0d,
23 ASN_SEQUENCE = 0x10, 26 ASN_SEQUENCE = 0x10,
24 ASN_SET = 0x11, //X 27 ASN_SET = 0x11,
25 ASN_NUMERIC_STRING = 0x12, //X 28 ASN_NUMERIC_STRING = 0x12,
26 ASN_PRINTABLE_STRING = 0x13, //X 29 ASN_PRINTABLE_STRING = 0x13,
27 ASN_TELETEX_STRING = 0x14, //X 30 ASN_TELETEX_STRING = 0x14,
28 ASN_T61_STRING = 0x14, //X 31 ASN_T61_STRING = 0x14,
29 ASN_VIDEOTEX_STRING = 0x15, //X 32 ASN_VIDEOTEX_STRING = 0x15,
30 ASN_IA5_STRING = 0x16, //X 33 ASN_IA5_STRING = 0x16,
31 ASN_ASCII_STRING = 0x16, //X 34 ASN_ASCII_STRING = 0x16,
32 ASN_UTC_TIME = 0x17, //X 35 ASN_UTC_TIME = 0x17,
33 ASN_GENERALIZED_TIME = 0x18, //X 36 ASN_GENERALIZED_TIME = 0x18,
34 ASN_GRAPHIC_STRING = 0x19, //X 37 ASN_GRAPHIC_STRING = 0x19,
35 ASN_VISIBLE_STRING = 0x1a, //X 38 ASN_VISIBLE_STRING = 0x1a,
36 ASN_ISO646_STRING = 0x1a, //X 39 ASN_ISO646_STRING = 0x1a,
37 ASN_GENERAL_STRING = 0x1b, //X 40 ASN_GENERAL_STRING = 0x1b,
38 ASN_UNIVERSAL_STRING = 0x1c, //X 41 ASN_UNIVERSAL_STRING = 0x1c,
39 ASN_CHARACTER_STRING = 0x1d, //X 42 ASN_CHARACTER_STRING = 0x1d,
40 ASN_BMP_STRING = 0x1e, //X 43 ASN_BMP_STRING = 0x1e,
41 44
42 ASN_TAG_BER = 0x1f, 45 ASN_TAG_BER = 0x1f,
43 ASN_TAG_MASK = 0x1f, 46 ASN_TAG_MASK = 0x1f,
44 47
45 // primitive/constructed 48 // primitive/constructed
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;
276 283
277 default: 284 default:
278 error ("ASN.1 length too long"); 285 error ("ASN.1 length too long");
279 return 0; 286 return 0;
280 287
288 case 8: res = (res << 8) | get_u8 ();
289 case 7: res = (res << 8) | get_u8 ();
290 case 6: res = (res << 8) | get_u8 ();
291 case 5: res = (res << 8) | get_u8 ();
281 case 4: res = (res << 8) | get_u8 (); 292 case 4: res = (res << 8) | get_u8 ();
282 case 3: res = (res << 8) | get_u8 (); 293 case 3: res = (res << 8) | get_u8 ();
283 case 2: res = (res << 8) | get_u8 (); 294 case 2: res = (res << 8) | get_u8 ();
284 case 1: res = (res << 8) | get_u8 (); 295 case 1: res = (res << 8) | get_u8 ();
285 } 296 }
287 298
288 return res; 299 return res;
289} 300}
290 301
291static SV * 302static SV *
292decode_int () 303decode_int (void)
293{ 304{
294 int len = get_length (); 305 UV len = get_length ();
295 306
296 if (len <= 0) 307 if (!len)
297 { 308 {
298 error ("integer length equal to zero"); 309 error ("invalid integer length equal to zero");
299 return 0; 310 return 0;
300 } 311 }
301 312
302 U8 *data = get_n (len); 313 U8 *data = get_n (len);
303 314
315} 326}
316 327
317static SV * 328static SV *
318decode_data (void) 329decode_data (void)
319{ 330{
320 U32 len = get_length (); 331 UV len = get_length ();
321 U8 *data = get_n (len);
322 return newSVpvn ((char *)data, len); 332 return newSVpvn ((char *)get_n (len), len);
323} 333}
324 334
325// gelper for decode_object_identifier 335// helper for decode_object_identifier
326static char * 336static char *
327write_uv (char *buf, U32 u) 337write_uv (char *buf, UV u)
328{ 338{
329 // the one-digit case is absolutely predominant, so this pays off (hopefully) 339 // the one-digit case is absolutely predominant, so this pays off (hopefully)
330 if (expect_true (u < 10)) 340 if (expect_true (u < 10))
331 *buf++ = u + '0'; 341 *buf++ = u + '0';
332 else 342 else
333 { 343 {
344 // this *could* be done much faster using branchless fixed-point arithmetics
334 char *beg = buf; 345 char *beg = buf;
335 346
336 do 347 do
337 { 348 {
338 *buf++ = u % 10 + '0'; 349 *buf++ = u % 10 + '0';
339 u /= 10; 350 u /= 10;
340 } 351 }
341 while (u); 352 while (u);
342 353
343 // reverse digits 354 // reverse digits
344 for (char *ptr = buf; --ptr != beg; ++beg) 355 char *ptr = buf;
356 while (--ptr > beg)
345 { 357 {
346 char c = *ptr; 358 char c = *ptr;
347 *ptr = *beg; 359 *ptr = *beg;
348 *beg = c; 360 *beg = c;
361 ++beg;
349 } 362 }
350 } 363 }
351 364
352 return buf; 365 return buf;
353} 366}
354 367
355static SV * 368static SV *
356decode_oid (int relative) 369decode_oid (int relative)
357{ 370{
358 U32 len = get_length (); 371 UV len = get_length ();
359 372
360 if (len <= 0) 373 if (len <= 0)
361 { 374 {
362 error ("OBJECT IDENTIFIER length equal to zero"); 375 error ("OBJECT IDENTIFIER length equal to zero");
363 return &PL_sv_undef; 376 return &PL_sv_undef;
364 } 377 }
365 378
366 U8 *end = cur + len; 379 U8 *end = cur + len;
367 U32 w = get_w (); 380 UV w = get_w ();
368 381
369 static char oid[MAX_OID_STRLEN]; // must be static 382 static char oid[MAX_OID_STRLEN]; // static, becaueds too large for stack
370 char *app = oid; 383 char *app = oid;
371 384
372 if (relative) 385 if (relative)
373 app = write_uv (app, w); 386 app = write_uv (app, w);
374 else 387 else
376 app = write_uv (app, (U8)w / 40); 389 app = write_uv (app, (U8)w / 40);
377 *app++ = '.'; 390 *app++ = '.';
378 app = write_uv (app, (U8)w % 40); 391 app = write_uv (app, (U8)w % 40);
379 } 392 }
380 393
394 while (cur < end)
395 {
381 // we assume an oid component is never > 64 bytes 396 // we assume an oid component is never > 64 digits
382 while (cur < end && oid + sizeof (oid) - app > 64) 397 if (oid + sizeof (oid) - app < 64)
383 { 398 croak ("BER_TYPE_OID to long to decode");
399
384 w = get_w (); 400 w = get_w ();
385 *app++ = '.'; 401 *app++ = '.';
386 app = write_uv (app, w); 402 app = write_uv (app, w);
387 } 403 }
388 404
389 return newSVpvn (oid, app - oid); 405 return newSVpvn (oid, app - oid);
390} 406}
391 407
408// TODO: this is unacceptably slow
392static SV * 409static SV *
410decode_ucs (int chrsize)
411{
412 SV *res = NEWSV (0, 0);
413
414 UV len = get_length ();
415
416 if (len & (chrsize - 1))
417 croak ("BER_TYPE_UCS has an invalid number of octets (%d)", len);
418
419 while (len)
420 {
421 U8 b1 = get_u8 ();
422 U8 b2 = get_u8 ();
423 U32 chr = (b1 << 8) | b2;
424
425 if (chrsize == 4)
426 {
427 U8 b3 = get_u8 ();
428 U8 b4 = get_u8 ();
429 chr = (chr << 16) | (b3 << 8) | b4;
430 }
431
432 U8 uchr [UTF8_MAXBYTES];
433 int uclen = uvuni_to_utf8 (uchr, chr) - uchr;
434
435 sv_catpvn (res, (const char *)uchr, uclen);
436 len -= chrsize;
437 }
438
439 SvUTF8_on (res);
440
441 return res;
442}
443
444static SV *
393decode_ber () 445decode_ber (void)
394{ 446{
395 int identifier = get_u8 (); 447 int identifier = get_u8 ();
396 448
397 SV *res; 449 SV *res;
398 450
406 if (tag == ASN_TAG_BER) 458 if (tag == ASN_TAG_BER)
407 tag = get_w (); 459 tag = get_w ();
408 460
409 if (constructed) 461 if (constructed)
410 { 462 {
411 U32 len = get_length (); 463 UV len = get_length ();
412 U32 seqend = (cur - buf) + len; 464 UV seqend = (cur - buf) + len;
413 AV *av = (AV *)sv_2mortal ((SV *)newAV ()); 465 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
414 466
415 while (cur < buf + seqend) 467 while (cur < buf + seqend)
416 av_push (av, decode_ber ()); 468 av_push (av, decode_ber ());
417 469
422 } 474 }
423 else 475 else
424 switch (profile_lookup (cur_profile, klass, tag)) 476 switch (profile_lookup (cur_profile, klass, tag))
425 { 477 {
426 case BER_TYPE_NULL: 478 case BER_TYPE_NULL:
479 {
480 UV len = get_length ();
481
482 if (len)
483 croak ("BER_TYPE_NULL value with non-zero length %d encountered", len);
484
427 res = &PL_sv_undef; 485 res = &PL_sv_undef;
486 }
428 break; 487 break;
429 488
430 case BER_TYPE_BOOL: 489 case BER_TYPE_BOOL:
431 { 490 {
432 U32 len = get_length (); 491 UV len = get_length ();
433 492
434 if (len != 1) 493 if (len != 1)
435 croak ("BER_TYPE_BOOLEAN type with invalid length %d encountered", len); 494 croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered", len);
436 495
437 res = newSVcacheint (get_u8 () ? 0 : 1); 496 res = newSVcacheint (!!get_u8 ());
438 } 497 }
439 break; 498 break;
440 499
441 case BER_TYPE_OID: 500 case BER_TYPE_OID:
442 res = decode_oid (0); 501 res = decode_oid (0);
459 res = decode_data (); 518 res = decode_data ();
460 break; 519 break;
461 520
462 case BER_TYPE_IPADDRESS: 521 case BER_TYPE_IPADDRESS:
463 { 522 {
464 U32 len = get_length (); 523 UV len = get_length ();
465 524
466 if (len != 4) 525 if (len != 4)
467 croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered", len); 526 croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered", len);
468 527
469 U8 c1 = get_u8 (); 528 U8 c1 = get_u8 ();
473 532
474 res = newSVpvf ("%d.%d.%d.%d", c1, c2, c3, c4); 533 res = newSVpvf ("%d.%d.%d.%d", c1, c2, c3, c4);
475 } 534 }
476 break; 535 break;
477 536
537 case BER_TYPE_UCS2:
538 res = decode_ucs (2);
539 break;
540
541 case BER_TYPE_UCS4:
542 res = decode_ucs (4);
543 break;
544
478 case BER_TYPE_REAL: 545 case BER_TYPE_REAL:
479 case BER_TYPE_UCS2:
480 case BER_TYPE_UCS4:
481 case BER_TYPE_CROAK: 546 case BER_TYPE_CROAK:
482 default: 547 default:
483 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 548 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
484 } 549 }
485 550
486 AV *av = newAV (); 551 AV *av = newAV ();
487 av_fill (av, BER_ARRAYSIZE - 1); 552 av_fill (av, BER_ARRAYSIZE - 1);
488 AvARRAY (av)[BER_CLASS ] = newSVcacheint (klass); 553 AvARRAY (av)[BER_CLASS] = newSVcacheint (klass);
489 AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); 554 AvARRAY (av)[BER_TAG ] = newSVcacheint (tag);
490 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (constructed ? 1 : 0); 555 AvARRAY (av)[BER_FLAGS] = newSVcacheint (constructed ? 1 : 0);
491 AvARRAY (av)[BER_DATA ] = res; 556 AvARRAY (av)[BER_DATA ] = res;
492 557
493 return newRV_noinc ((SV *)av); 558 return newRV_noinc ((SV *)av);
494} 559}
495 560
496///////////////////////////////////////////////////////////////////////////// 561/////////////////////////////////////////////////////////////////////////////
511static void 576static void
512set_buf (SV *sv) 577set_buf (SV *sv)
513{ 578{
514 STRLEN len; 579 STRLEN len;
515 buf_sv = sv; 580 buf_sv = sv;
516 buf = SvPVbyte (buf_sv, len); 581 buf = (U8 *)SvPVbyte (buf_sv, len);
517 cur = buf; 582 cur = buf;
518 end = buf + len; 583 end = buf + len;
519} 584}
520 585
521/* similar to SvGROW, but somewhat safer and guarantees exponential realloc strategy */ 586/* similar to SvGROW, but somewhat safer and guarantees exponential realloc strategy */
535need (STRLEN len) 600need (STRLEN len)
536{ 601{
537 if (expect_false ((uintptr_t)(end - cur) < len)) 602 if (expect_false ((uintptr_t)(end - cur) < len))
538 { 603 {
539 STRLEN pos = cur - buf; 604 STRLEN pos = cur - buf;
540 buf = my_sv_grow (buf_sv, pos, len); 605 buf = (U8 *)my_sv_grow (buf_sv, pos, len);
541 cur = buf + pos; 606 cur = buf + pos;
542 end = buf + SvLEN (buf_sv) - 1; 607 end = buf + SvLEN (buf_sv) - 1;
543 } 608 }
544} 609}
545 610
549 need (1); 614 need (1);
550 *cur++ = val; 615 *cur++ = val;
551} 616}
552 617
553static void 618static void
554put_w_nocheck (U32 val) 619put_w_nocheck (UV val)
555{ 620{
621#if UVSIZE > 4
622 *cur = (val >> 7 * 9) | 0x80; cur += val >= ((UV)1 << (7 * 9));
623 *cur = (val >> 7 * 8) | 0x80; cur += val >= ((UV)1 << (7 * 8));
624 *cur = (val >> 7 * 7) | 0x80; cur += val >= ((UV)1 << (7 * 7));
625 *cur = (val >> 7 * 6) | 0x80; cur += val >= ((UV)1 << (7 * 6));
626 *cur = (val >> 7 * 5) | 0x80; cur += val >= ((UV)1 << (7 * 5));
627#endif
556 *cur = (val >> 7 * 4) | 0x80; cur += val >= (1 << (7 * 4)); 628 *cur = (val >> 7 * 4) | 0x80; cur += val >= ((UV)1 << (7 * 4));
557 *cur = (val >> 7 * 3) | 0x80; cur += val >= (1 << (7 * 3)); 629 *cur = (val >> 7 * 3) | 0x80; cur += val >= ((UV)1 << (7 * 3));
558 *cur = (val >> 7 * 2) | 0x80; cur += val >= (1 << (7 * 2)); 630 *cur = (val >> 7 * 2) | 0x80; cur += val >= ((UV)1 << (7 * 2));
559 *cur = (val >> 7 * 1) | 0x80; cur += val >= (1 << (7 * 1)); 631 *cur = (val >> 7 * 1) | 0x80; cur += val >= ((UV)1 << (7 * 1));
560 *cur = val & 0x7f; cur += 1; 632 *cur = val & 0x7f; cur += 1;
561} 633}
562 634
563static void 635static void
564put_w (U32 val) 636put_w (UV val)
565{ 637{
566 need (5); // we only handle up to 5 bytes 638 need (5); // we only handle up to 5 bytes
567 639
568 put_w_nocheck (val); 640 put_w_nocheck (val);
569} 641}
570 642
571static U8 * 643static U8 *
572put_length_at (U32 val, U8 *cur) 644put_length_at (UV val, U8 *cur)
573{ 645{
574 if (val < 0x7fU) 646 if (val < 0x7fU)
575 *cur++ = val; 647 *cur++ = val;
576 else 648 else
577 { 649 {
578 U8 *lenb = cur++; 650 U8 *lenb = cur++;
579 651
652#if UVSIZE > 4
653 *cur = val >> 56; cur += *cur > 0;
654 *cur = val >> 48; cur += *cur > 0;
655 *cur = val >> 40; cur += *cur > 0;
656 *cur = val >> 32; cur += *cur > 0;
657#endif
580 *cur = val >> 24; cur += *cur > 0; 658 *cur = val >> 24; cur += *cur > 0;
581 *cur = val >> 16; cur += *cur > 0; 659 *cur = val >> 16; cur += *cur > 0;
582 *cur = val >> 8; cur += *cur > 0; 660 *cur = val >> 8; cur += *cur > 0;
583 *cur = val ; cur += 1; 661 *cur = val ; cur += 1;
584 662
587 665
588 return cur; 666 return cur;
589} 667}
590 668
591static void 669static void
592put_length (U32 val) 670put_length (UV val)
593{ 671{
594 need (5); 672 need (5 + val);
595 cur = put_length_at (val, cur); 673 cur = put_length_at (val, cur);
596} 674}
597 675
598// return how many bytes the encoded length requires 676// return how many bytes the encoded length requires
599static int length_length (U32 val) 677static int length_length (UV val)
600{ 678{
601 return val < 0x7fU 679 return val < 0x7fU
602 ? 1 680 ? 1
603 : 2 + (val > 0xffU) + (val > 0xffffU) + (val > 0xffffffU); 681 : 2
682 + (val > 0xffU)
683 + (val > 0xffffU)
684 + (val > 0xffffffU)
685#if UVSIZE > 4
686 + (val > 0xffffffffU)
687 + (val > 0xffffffffffU)
688 + (val > 0xffffffffffffU)
689 + (val > 0xffffffffffffffU)
690#endif
691 ;
604} 692}
605 693
606static void 694static void
607encode_data (const char *ptr, STRLEN len) 695encode_data (const char *ptr, STRLEN len)
608{ 696{
609 put_length (len); 697 put_length (len);
610 need (len);
611 memcpy (cur, ptr, len); 698 memcpy (cur, ptr, len);
612 cur += len; 699 cur += len;
613} 700}
614 701
615static void 702static void
671 758
672 *lenb = cur - lenb - 1; 759 *lenb = cur - lenb - 1;
673} 760}
674 761
675// we don't know the length yet, so we optimistically 762// we don't know the length yet, so we optimistically
676// assume the length will need one octet later. if that 763// assume the length will need one octet later. If that
677// turns out to be wrong, we memove as needed. 764// turns out to be wrong, we memmove as needed.
678// mark the beginning 765// mark the beginning
679static STRLEN 766static STRLEN
680len_fixup_mark () 767len_fixup_mark (void)
681{ 768{
682 return cur++ - buf; 769 return cur++ - buf;
683} 770}
684 771
685// patch up the length 772// patch up the length
761 848
762 return (AV *)rv; 849 return (AV *)rv;
763} 850}
764 851
765static void 852static void
853encode_ucs (SV *data, int chrsize)
854{
855 STRLEN uchars = sv_len_utf8 (data);
856 STRLEN len;;
857 char *ptr = SvPVutf8 (data, len);
858
859 put_length (uchars * chrsize);
860
861 while (uchars--)
862 {
863 STRLEN uclen;
864 UV uchr = utf8_to_uvchr_buf ((U8 *)ptr, (U8 *)ptr + len, &uclen);
865
866 ptr += uclen;
867 len -= uclen;
868
869 if (chrsize == 4)
870 {
871 *cur++ = uchr >> 24;
872 *cur++ = uchr >> 16;
873 }
874
875 *cur++ = uchr >> 8;
876 *cur++ = uchr;
877 }
878}
879static void
766encode_ber (SV *tuple) 880encode_ber (SV *tuple)
767{ 881{
768 AV *av = ber_tuple (tuple); 882 AV *av = ber_tuple (tuple);
769 883
770 int klass = SvIV (AvARRAY (av)[BER_CLASS]); 884 int klass = SvIV (AvARRAY (av)[BER_CLASS]);
771 int tag = SvIV (AvARRAY (av)[BER_TAG]); 885 int tag = SvIV (AvARRAY (av)[BER_TAG]);
772 int constructed = SvIV (AvARRAY (av)[BER_CONSTRUCTED]) ? ASN_CONSTRUCTED : 0; 886 int constructed = SvIV (AvARRAY (av)[BER_FLAGS]) & 1 ? ASN_CONSTRUCTED : 0;
773 SV *data = AvARRAY (av)[BER_DATA]; 887 SV *data = AvARRAY (av)[BER_DATA];
774 888
775 int identifier = (klass << ASN_CLASS_SHIFT) | constructed; 889 int identifier = (klass << ASN_CLASS_SHIFT) | constructed;
776 890
777 if (expect_false (tag >= ASN_TAG_BER)) 891 if (expect_false (tag >= ASN_TAG_BER))
796 int fill = AvFILL (av); 910 int fill = AvFILL (av);
797 911
798 if (expect_false (SvRMAGICAL (av))) 912 if (expect_false (SvRMAGICAL (av)))
799 croak ("BER constructed data must not be tied"); 913 croak ("BER constructed data must not be tied");
800 914
915 int i;
801 for (int i = 0; i <= fill; ++i) 916 for (i = 0; i <= fill; ++i)
802 encode_ber (AvARRAY (av)[i]); 917 encode_ber (AvARRAY (av)[i]);
803 918
804 len_fixup (mark); 919 len_fixup (mark);
805 } 920 }
806 else 921 else
810 put_length (0); 925 put_length (0);
811 break; 926 break;
812 927
813 case BER_TYPE_BOOL: 928 case BER_TYPE_BOOL:
814 put_length (1); 929 put_length (1);
815 put_u8 (SvTRUE (data) ? 0xff : 0x00); 930 *cur++ = SvTRUE (data) ? 0xff : 0x00;
816 break; 931 break;
817 932
818 case BER_TYPE_OID: 933 case BER_TYPE_OID:
819 encode_oid (data, 0); 934 encode_oid (data, 0);
820 break; 935 break;
849 sscanf (SvPV_nolen (data), "%hhu.%hhu.%hhu.%hhu", ip + 0, ip + 1, ip + 2, ip + 3); 964 sscanf (SvPV_nolen (data), "%hhu.%hhu.%hhu.%hhu", ip + 0, ip + 1, ip + 2, ip + 3);
850 encode_data ((const char *)ip, sizeof (ip)); 965 encode_data ((const char *)ip, sizeof (ip));
851 } 966 }
852 break; 967 break;
853 968
969 case BER_TYPE_UCS2:
970 encode_ucs (data, 2);
971 break;
972
973 case BER_TYPE_UCS4:
974 encode_ucs (data, 4);
975 break;
976
854 case BER_TYPE_REAL: 977 case BER_TYPE_REAL:
855 case BER_TYPE_UCS2:
856 case BER_TYPE_UCS4:
857 case BER_TYPE_CROAK: 978 case BER_TYPE_CROAK:
858 default: 979 default:
859 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 980 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
860 } 981 }
861 982
877 const char *name; 998 const char *name;
878 IV iv; 999 IV iv;
879 } *civ, const_iv[] = { 1000 } *civ, const_iv[] = {
880#define const_iv(name) { # name, name }, 1001#define const_iv(name) { # name, name },
881 const_iv (ASN_BOOLEAN) 1002 const_iv (ASN_BOOLEAN)
882 const_iv (ASN_INTEGER32) 1003 const_iv (ASN_INTEGER)
883 const_iv (ASN_BIT_STRING) 1004 const_iv (ASN_BIT_STRING)
884 const_iv (ASN_OCTET_STRING) 1005 const_iv (ASN_OCTET_STRING)
885 const_iv (ASN_NULL) 1006 const_iv (ASN_NULL)
886 const_iv (ASN_OBJECT_IDENTIFIER) 1007 const_iv (ASN_OBJECT_IDENTIFIER)
887 const_iv (ASN_OBJECT_DESCRIPTOR) 1008 const_iv (ASN_OBJECT_DESCRIPTOR)
916 const_iv (ASN_CONTEXT) 1037 const_iv (ASN_CONTEXT)
917 const_iv (ASN_PRIVATE) 1038 const_iv (ASN_PRIVATE)
918 1039
919 const_iv (BER_CLASS) 1040 const_iv (BER_CLASS)
920 const_iv (BER_TAG) 1041 const_iv (BER_TAG)
921 const_iv (BER_CONSTRUCTED) 1042 const_iv (BER_FLAGS)
922 const_iv (BER_DATA) 1043 const_iv (BER_DATA)
923 1044
924 const_iv (BER_TYPE_BYTES) 1045 const_iv (BER_TYPE_BYTES)
925 const_iv (BER_TYPE_UTF8) 1046 const_iv (BER_TYPE_UTF8)
926 const_iv (BER_TYPE_UCS2) 1047 const_iv (BER_TYPE_UCS2)
950ber_decode (SV *ber, SV *profile = &PL_sv_undef) 1071ber_decode (SV *ber, SV *profile = &PL_sv_undef)
951 CODE: 1072 CODE:
952{ 1073{
953 cur_profile = SvPROFILE (profile); 1074 cur_profile = SvPROFILE (profile);
954 STRLEN len; 1075 STRLEN len;
955 buf = SvPVbyte (ber, len); 1076 buf = (U8 *)SvPVbyte (ber, len);
956 cur = buf; 1077 cur = buf;
957 end = buf + len; 1078 end = buf + len;
958 1079
959 RETVAL = decode_ber (); 1080 RETVAL = decode_ber ();
960} 1081}
961 OUTPUT: RETVAL 1082 OUTPUT: RETVAL
962 1083
963void 1084void
964ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *constructed = &PL_sv_undef, SV *data = &PL_sv_undef) 1085ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *flags = &PL_sv_undef, SV *data = &PL_sv_undef)
965 PPCODE: 1086 PPCODE:
966{ 1087{
967 if (!SvOK (tuple)) 1088 if (!SvOK (tuple))
968 XSRETURN_NO; 1089 XSRETURN_NO;
969 1090
971 croak ("ber_is: tuple must be BER tuple (array-ref)"); 1092 croak ("ber_is: tuple must be BER tuple (array-ref)");
972 1093
973 AV *av = (AV *)SvRV (tuple); 1094 AV *av = (AV *)SvRV (tuple);
974 1095
975 XPUSHs ( 1096 XPUSHs (
976 (!SvOK (klass) || SvIV (AvARRAY (av)[BER_CLASS ]) == SvIV (klass)) 1097 (!SvOK (klass) || SvIV (AvARRAY (av)[BER_CLASS]) == SvIV (klass))
977 && (!SvOK (tag) || SvIV (AvARRAY (av)[BER_TAG ]) == SvIV (tag)) 1098 && (!SvOK (tag) || SvIV (AvARRAY (av)[BER_TAG ]) == SvIV (tag))
978 && (!SvOK (constructed) || !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) == !SvIV (constructed)) 1099 && (!SvOK (flags) || !SvIV (AvARRAY (av)[BER_FLAGS]) == !SvIV (flags))
979 && (!SvOK (data) || sv_eq (AvARRAY (av)[BER_DATA ], data)) 1100 && (!SvOK (data) || sv_eq (AvARRAY (av)[BER_DATA ], data))
980 ? &PL_sv_yes : &PL_sv_undef); 1101 ? &PL_sv_yes : &PL_sv_undef);
981} 1102}
982 1103
983void 1104void
984ber_is_seq (SV *tuple) 1105ber_is_seq (SV *tuple)
988 XSRETURN_UNDEF; 1109 XSRETURN_UNDEF;
989 1110
990 AV *av = ber_tuple (tuple); 1111 AV *av = ber_tuple (tuple);
991 1112
992 XPUSHs ( 1113 XPUSHs (
993 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL 1114 SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL
994 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_SEQUENCE 1115 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_SEQUENCE
995 && SvIV (AvARRAY (av)[BER_CONSTRUCTED]) 1116 && SvIV (AvARRAY (av)[BER_FLAGS])
996 ? AvARRAY (av)[BER_DATA] : &PL_sv_undef); 1117 ? AvARRAY (av)[BER_DATA] : &PL_sv_undef);
997} 1118}
998 1119
999void 1120void
1000ber_is_i32 (SV *tuple, SV *value = &PL_sv_undef) 1121ber_is_int (SV *tuple, SV *value = &PL_sv_undef)
1001 PPCODE: 1122 PPCODE:
1002{ 1123{
1003 if (!SvOK (tuple)) 1124 if (!SvOK (tuple))
1004 XSRETURN_NO; 1125 XSRETURN_NO;
1005 1126
1006 AV *av = ber_tuple (tuple); 1127 AV *av = ber_tuple (tuple);
1007 1128
1008 IV data = SvIV (AvARRAY (av)[BER_DATA]); 1129 UV data = SvUV (AvARRAY (av)[BER_DATA]);
1009 1130
1010 XPUSHs ( 1131 XPUSHs (
1011 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL 1132 SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL
1012 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_INTEGER32 1133 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_INTEGER
1013 && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) 1134 && !SvIV (AvARRAY (av)[BER_FLAGS])
1014 && (!SvOK (value) || data == SvIV (value)) 1135 && (!SvOK (value) || data == SvUV (value))
1015 ? sv_2mortal (data ? newSViv (data) : newSVpv ("0 but true", 0)) 1136 ? sv_2mortal (data ? newSVsv (AvARRAY (av)[BER_DATA]) : newSVpv ("0 but true", 0))
1016 : &PL_sv_undef); 1137 : &PL_sv_undef);
1017} 1138}
1018 1139
1019void 1140void
1020ber_is_oid (SV *tuple, SV *oid = &PL_sv_undef) 1141ber_is_oid (SV *tuple, SV *oid = &PL_sv_undef)
1024 XSRETURN_NO; 1145 XSRETURN_NO;
1025 1146
1026 AV *av = ber_tuple (tuple); 1147 AV *av = ber_tuple (tuple);
1027 1148
1028 XPUSHs ( 1149 XPUSHs (
1029 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL 1150 SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL
1030 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_OBJECT_IDENTIFIER 1151 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_OBJECT_IDENTIFIER
1031 && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) 1152 && !SvIV (AvARRAY (av)[BER_FLAGS])
1032 && (!SvOK (oid) || sv_eq (AvARRAY (av)[BER_DATA], oid)) 1153 && (!SvOK (oid) || sv_eq (AvARRAY (av)[BER_DATA], oid))
1033 ? newSVsv (AvARRAY (av)[BER_DATA]) : &PL_sv_undef); 1154 ? newSVsv (AvARRAY (av)[BER_DATA]) : &PL_sv_undef);
1034} 1155}
1035 1156
1036############################################################################# 1157#############################################################################
1049 SvCUR_set (buf_sv, cur - buf); 1170 SvCUR_set (buf_sv, cur - buf);
1050 XPUSHs (buf_sv); 1171 XPUSHs (buf_sv);
1051} 1172}
1052 1173
1053SV * 1174SV *
1054ber_i32 (IV iv) 1175ber_int (SV *sv)
1055 CODE: 1176 CODE:
1056{ 1177{
1057 AV *av = newAV (); 1178 AV *av = newAV ();
1058 av_fill (av, BER_ARRAYSIZE - 1); 1179 av_fill (av, BER_ARRAYSIZE - 1);
1059 AvARRAY (av)[BER_CLASS ] = newSVcacheint (ASN_UNIVERSAL); 1180 AvARRAY (av)[BER_CLASS] = newSVcacheint (ASN_UNIVERSAL);
1060 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_INTEGER32); 1181 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_INTEGER);
1061 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (0); 1182 AvARRAY (av)[BER_FLAGS] = newSVcacheint (0);
1062 AvARRAY (av)[BER_DATA ] = newSViv (iv); 1183 AvARRAY (av)[BER_DATA ] = newSVsv (sv);
1063 RETVAL = newRV_noinc ((SV *)av); 1184 RETVAL = newRV_noinc ((SV *)av);
1064} 1185}
1065 OUTPUT: RETVAL 1186 OUTPUT: RETVAL
1066 1187
1067# TODO: not arrayref, but elements? 1188# TODO: not arrayref, but elements?
1069ber_seq (SV *arrayref) 1190ber_seq (SV *arrayref)
1070 CODE: 1191 CODE:
1071{ 1192{
1072 AV *av = newAV (); 1193 AV *av = newAV ();
1073 av_fill (av, BER_ARRAYSIZE - 1); 1194 av_fill (av, BER_ARRAYSIZE - 1);
1074 AvARRAY (av)[BER_CLASS ] = newSVcacheint (ASN_UNIVERSAL); 1195 AvARRAY (av)[BER_CLASS] = newSVcacheint (ASN_UNIVERSAL);
1075 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_SEQUENCE); 1196 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_SEQUENCE);
1076 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (1); 1197 AvARRAY (av)[BER_FLAGS] = newSVcacheint (1);
1077 AvARRAY (av)[BER_DATA ] = newSVsv (arrayref); 1198 AvARRAY (av)[BER_DATA ] = newSVsv (arrayref);
1078 RETVAL = newRV_noinc ((SV *)av); 1199 RETVAL = newRV_noinc ((SV *)av);
1079} 1200}
1080 OUTPUT: RETVAL 1201 OUTPUT: RETVAL
1081 1202
1082MODULE = Convert::BER::XS PACKAGE = Convert::BER::XS::Profile 1203MODULE = Convert::BER::XS PACKAGE = Convert::BER::XS::Profile

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines