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.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, //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;
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
389 return newSVpvn (oid, app - oid); 422 return newSVpvn (oid, app - oid);
390} 423}
391 424
425// TODO: this is unacceptably slow
392static SV * 426static SV *
427decode_ucs (int chrsize)
428{
429 SV *res = NEWSV (0, 0);
430
431 UV len = get_length ();
432
433 if (len & (chrsize - 1))
434 croak ("BER_TYPE_UCS has an invalid number of octets (%d)", len);
435
436 while (len)
437 {
438 U8 b1 = get_u8 ();
439 U8 b2 = get_u8 ();
440 U32 chr = (b1 << 8) | b2;
441
442 if (chrsize == 4)
443 {
444 U8 b3 = get_u8 ();
445 U8 b4 = get_u8 ();
446 chr = (chr << 16) | (b3 << 8) | b4;
447 }
448
449 U8 uchr [UTF8_MAXBYTES];
450 int uclen = uvuni_to_utf8 (uchr, chr) - uchr;
451
452 sv_catpvn (res, (const char *)uchr, uclen);
453 len -= chrsize;
454 }
455
456 SvUTF8_on (res);
457
458 return res;
459}
460
461static SV *
393decode_ber () 462decode_ber (void)
394{ 463{
395 int identifier = get_u8 (); 464 int identifier = get_u8 ();
396 465
397 SV *res; 466 SV *res;
398 467
401 int tag = identifier & ASN_TAG_MASK; 470 int tag = identifier & ASN_TAG_MASK;
402 471
403 if (tag == ASN_TAG_BER) 472 if (tag == ASN_TAG_BER)
404 tag = get_w (); 473 tag = get_w ();
405 474
406 if (tag == ASN_TAG_BER)
407 tag = get_w ();
408
409 if (constructed) 475 if (constructed)
410 { 476 {
411 U32 len = get_length (); 477 UV len = get_length ();
412 U32 seqend = (cur - buf) + len; 478 UV seqend = (cur - buf) + len;
413 AV *av = (AV *)sv_2mortal ((SV *)newAV ()); 479 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
414 480
415 while (cur < buf + seqend) 481 while (cur < buf + seqend)
416 av_push (av, decode_ber ()); 482 av_push (av, decode_ber ());
417 483
418 if (cur > buf + seqend) 484 if (cur > buf + seqend)
419 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);
420 486
421 res = newRV_inc ((SV *)av); 487 res = newRV_inc ((SV *)av);
422 } 488 }
423 else 489 else
424 switch (profile_lookup (cur_profile, klass, tag)) 490 switch (profile_lookup (cur_profile, klass, tag))
425 { 491 {
426 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
427 res = &PL_sv_undef; 499 res = &PL_sv_undef;
500 }
428 break; 501 break;
429 502
430 case BER_TYPE_BOOL: 503 case BER_TYPE_BOOL:
431 { 504 {
432 U32 len = get_length (); 505 UV len = get_length ();
433 506
434 if (len != 1) 507 if (len != 1)
435 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);
436 509
437 res = newSVcacheint (get_u8 () ? 0 : 1); 510 res = newSVcacheint (!!get_u8 ());
438 } 511 }
439 break; 512 break;
440 513
441 case BER_TYPE_OID: 514 case BER_TYPE_OID:
442 res = decode_oid (0); 515 res = decode_oid (0);
459 res = decode_data (); 532 res = decode_data ();
460 break; 533 break;
461 534
462 case BER_TYPE_IPADDRESS: 535 case BER_TYPE_IPADDRESS:
463 { 536 {
464 U32 len = get_length (); 537 UV len = get_length ();
465 538
466 if (len != 4) 539 if (len != 4)
467 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);
468 541
469 U8 c1 = get_u8 (); 542 U8 c1 = get_u8 ();
470 U8 c2 = get_u8 (); 543 U8 c2 = get_u8 ();
471 U8 c3 = get_u8 (); 544 U8 c3 = get_u8 ();
472 U8 c4 = get_u8 (); 545 U8 c4 = get_u8 ();
473 546
474 res = newSVpvf ("%d.%d.%d.%d", c1, c2, c3, c4); 547 res = newSVpvf ("%d.%d.%d.%d", c1, c2, c3, c4);
475 } 548 }
476 break; 549 break;
477 550
551 case BER_TYPE_UCS2:
552 res = decode_ucs (2);
553 break;
554
555 case BER_TYPE_UCS4:
556 res = decode_ucs (4);
557 break;
558
478 case BER_TYPE_REAL: 559 case BER_TYPE_REAL:
479 case BER_TYPE_UCS2:
480 case BER_TYPE_UCS4:
481 case BER_TYPE_CROAK: 560 case BER_TYPE_CROAK:
482 default: 561 default:
483 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 562 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
484 } 563 }
485 564
486 AV *av = newAV (); 565 AV *av = newAV ();
487 av_fill (av, BER_ARRAYSIZE - 1); 566 av_fill (av, BER_ARRAYSIZE - 1);
488 AvARRAY (av)[BER_CLASS ] = newSVcacheint (klass); 567 AvARRAY (av)[BER_CLASS] = newSVcacheint (klass);
489 AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); 568 AvARRAY (av)[BER_TAG ] = newSVcacheint (tag);
490 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (constructed ? 1 : 0); 569 AvARRAY (av)[BER_FLAGS] = newSVcacheint (constructed ? 1 : 0);
491 AvARRAY (av)[BER_DATA ] = res; 570 AvARRAY (av)[BER_DATA ] = res;
492 571
493 return newRV_noinc ((SV *)av); 572 return newRV_noinc ((SV *)av);
494} 573}
495 574
496///////////////////////////////////////////////////////////////////////////// 575/////////////////////////////////////////////////////////////////////////////
501strlen_sum (STRLEN l1, STRLEN l2) 580strlen_sum (STRLEN l1, STRLEN l2)
502{ 581{
503 size_t sum = l1 + l2; 582 size_t sum = l1 + l2;
504 583
505 if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) 584 if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum)
506 croak ("JSON::XS: string size overflow"); 585 croak ("Convert::BER::XS: string size overflow");
507 586
508 return sum; 587 return sum;
509} 588}
510 589
511static void 590static void
512set_buf (SV *sv) 591set_buf (SV *sv)
513{ 592{
514 STRLEN len; 593 STRLEN len;
515 buf_sv = sv; 594 buf_sv = sv;
516 buf = SvPVbyte (buf_sv, len); 595 buf = (U8 *)SvPVbyte (buf_sv, len);
517 cur = buf; 596 cur = buf;
518 end = buf + len; 597 end = buf + len;
519} 598}
520 599
521/* similar to SvGROW, but somewhat safer and guarantees exponential realloc strategy */ 600/* similar to SvGROW, but somewhat safer and guarantees exponential realloc strategy */
535need (STRLEN len) 614need (STRLEN len)
536{ 615{
537 if (expect_false ((uintptr_t)(end - cur) < len)) 616 if (expect_false ((uintptr_t)(end - cur) < len))
538 { 617 {
539 STRLEN pos = cur - buf; 618 STRLEN pos = cur - buf;
540 buf = my_sv_grow (buf_sv, pos, len); 619 buf = (U8 *)my_sv_grow (buf_sv, pos, len);
541 cur = buf + pos; 620 cur = buf + pos;
542 end = buf + SvLEN (buf_sv) - 1; 621 end = buf + SvLEN (buf_sv) - 1;
543 } 622 }
544} 623}
545 624
549 need (1); 628 need (1);
550 *cur++ = val; 629 *cur++ = val;
551} 630}
552 631
553static void 632static void
554put_w_nocheck (U32 val) 633put_w_nocheck (UV val)
555{ 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
556 *cur = (val >> 7 * 4) | 0x80; cur += val >= (1 << (7 * 4)); 642 *cur = (val >> 7 * 4) | 0x80; cur += val >= ((UV)1 << (7 * 4));
557 *cur = (val >> 7 * 3) | 0x80; cur += val >= (1 << (7 * 3)); 643 *cur = (val >> 7 * 3) | 0x80; cur += val >= ((UV)1 << (7 * 3));
558 *cur = (val >> 7 * 2) | 0x80; cur += val >= (1 << (7 * 2)); 644 *cur = (val >> 7 * 2) | 0x80; cur += val >= ((UV)1 << (7 * 2));
559 *cur = (val >> 7 * 1) | 0x80; cur += val >= (1 << (7 * 1)); 645 *cur = (val >> 7 * 1) | 0x80; cur += val >= ((UV)1 << (7 * 1));
560 *cur = val & 0x7f; cur += 1; 646 *cur = val & 0x7f; cur += 1;
561} 647}
562 648
563static void 649static void
564put_w (U32 val) 650put_w (UV val)
565{ 651{
566 need (5); // we only handle up to 5 bytes 652 need (5); // we only handle up to 5 bytes
567 653
568 put_w_nocheck (val); 654 put_w_nocheck (val);
569} 655}
570 656
571static U8 * 657static U8 *
572put_length_at (U32 val, U8 *cur) 658put_length_at (UV val, U8 *cur)
573{ 659{
574 if (val < 0x7fU) 660 if (val < 0x7fU)
575 *cur++ = val; 661 *cur++ = val;
576 else 662 else
577 { 663 {
578 U8 *lenb = cur++; 664 U8 *lenb = cur++;
579 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
580 *cur = val >> 24; cur += *cur > 0; 672 *cur = val >> 24; cur += *cur > 0;
581 *cur = val >> 16; cur += *cur > 0; 673 *cur = val >> 16; cur += *cur > 0;
582 *cur = val >> 8; cur += *cur > 0; 674 *cur = val >> 8; cur += *cur > 0;
583 *cur = val ; cur += 1; 675 *cur = val ; cur += 1;
584 676
587 679
588 return cur; 680 return cur;
589} 681}
590 682
591static void 683static void
592put_length (U32 val) 684put_length (UV val)
593{ 685{
594 need (5); 686 need (5 + val);
595 cur = put_length_at (val, cur); 687 cur = put_length_at (val, cur);
596} 688}
597 689
598// return how many bytes the encoded length requires 690// return how many bytes the encoded length requires
599static int length_length (U32 val) 691static int length_length (UV val)
600{ 692{
601 return val < 0x7fU 693 return val < 0x7fU
602 ? 1 694 ? 1
603 : 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 ;
604} 706}
605 707
606static void 708static void
607encode_data (const char *ptr, STRLEN len) 709encode_data (const char *ptr, STRLEN len)
608{ 710{
609 put_length (len); 711 put_length (len);
610 need (len);
611 memcpy (cur, ptr, len); 712 memcpy (cur, ptr, len);
612 cur += len; 713 cur += len;
613} 714}
614 715
615static void 716static void
671 772
672 *lenb = cur - lenb - 1; 773 *lenb = cur - lenb - 1;
673} 774}
674 775
675// we don't know the length yet, so we optimistically 776// we don't know the length yet, so we optimistically
676// assume the length will need one octet later. if that 777// assume the length will need one octet later. If that
677// turns out to be wrong, we memove as needed. 778// turns out to be wrong, we memmove as needed.
678// mark the beginning 779// mark the beginning
679static STRLEN 780static STRLEN
680len_fixup_mark () 781len_fixup_mark (void)
681{ 782{
682 return cur++ - buf; 783 return cur++ - buf;
683} 784}
684 785
685// patch up the length 786// patch up the length
761 862
762 return (AV *)rv; 863 return (AV *)rv;
763} 864}
764 865
765static void 866static void
867encode_ucs (SV *data, int chrsize)
868{
869 STRLEN uchars = sv_len_utf8 (data);
870 STRLEN len;;
871 char *ptr = SvPVutf8 (data, len);
872
873 put_length (uchars * chrsize);
874
875 while (uchars--)
876 {
877 STRLEN uclen;
878 UV uchr = utf8_to_uvchr_buf ((U8 *)ptr, (U8 *)ptr + len, &uclen);
879
880 ptr += uclen;
881 len -= uclen;
882
883 if (chrsize == 4)
884 {
885 *cur++ = uchr >> 24;
886 *cur++ = uchr >> 16;
887 }
888
889 *cur++ = uchr >> 8;
890 *cur++ = uchr;
891 }
892}
893static void
766encode_ber (SV *tuple) 894encode_ber (SV *tuple)
767{ 895{
768 AV *av = ber_tuple (tuple); 896 AV *av = ber_tuple (tuple);
769 897
770 int klass = SvIV (AvARRAY (av)[BER_CLASS]); 898 int klass = SvIV (AvARRAY (av)[BER_CLASS]);
771 int tag = SvIV (AvARRAY (av)[BER_TAG]); 899 int tag = SvIV (AvARRAY (av)[BER_TAG]);
772 int constructed = SvIV (AvARRAY (av)[BER_CONSTRUCTED]) ? ASN_CONSTRUCTED : 0; 900 int constructed = SvIV (AvARRAY (av)[BER_FLAGS]) & 1 ? ASN_CONSTRUCTED : 0;
773 SV *data = AvARRAY (av)[BER_DATA]; 901 SV *data = AvARRAY (av)[BER_DATA];
774 902
775 int identifier = (klass << ASN_CLASS_SHIFT) | constructed; 903 int identifier = (klass << ASN_CLASS_SHIFT) | constructed;
776 904
777 if (expect_false (tag >= ASN_TAG_BER)) 905 if (expect_false (tag >= ASN_TAG_BER))
796 int fill = AvFILL (av); 924 int fill = AvFILL (av);
797 925
798 if (expect_false (SvRMAGICAL (av))) 926 if (expect_false (SvRMAGICAL (av)))
799 croak ("BER constructed data must not be tied"); 927 croak ("BER constructed data must not be tied");
800 928
929 int i;
801 for (int i = 0; i <= fill; ++i) 930 for (i = 0; i <= fill; ++i)
802 encode_ber (AvARRAY (av)[i]); 931 encode_ber (AvARRAY (av)[i]);
803 932
804 len_fixup (mark); 933 len_fixup (mark);
805 } 934 }
806 else 935 else
810 put_length (0); 939 put_length (0);
811 break; 940 break;
812 941
813 case BER_TYPE_BOOL: 942 case BER_TYPE_BOOL:
814 put_length (1); 943 put_length (1);
815 put_u8 (SvTRUE (data) ? 0xff : 0x00); 944 *cur++ = SvTRUE (data) ? 0xff : 0x00; // 0xff = DER/CER
816 break; 945 break;
817 946
818 case BER_TYPE_OID: 947 case BER_TYPE_OID:
819 encode_oid (data, 0); 948 encode_oid (data, 0);
820 break; 949 break;
849 sscanf (SvPV_nolen (data), "%hhu.%hhu.%hhu.%hhu", ip + 0, ip + 1, ip + 2, ip + 3); 978 sscanf (SvPV_nolen (data), "%hhu.%hhu.%hhu.%hhu", ip + 0, ip + 1, ip + 2, ip + 3);
850 encode_data ((const char *)ip, sizeof (ip)); 979 encode_data ((const char *)ip, sizeof (ip));
851 } 980 }
852 break; 981 break;
853 982
983 case BER_TYPE_UCS2:
984 encode_ucs (data, 2);
985 break;
986
987 case BER_TYPE_UCS4:
988 encode_ucs (data, 4);
989 break;
990
854 case BER_TYPE_REAL: 991 case BER_TYPE_REAL:
855 case BER_TYPE_UCS2:
856 case BER_TYPE_UCS4:
857 case BER_TYPE_CROAK: 992 case BER_TYPE_CROAK:
858 default: 993 default:
859 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 994 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
860 } 995 }
861 996
877 const char *name; 1012 const char *name;
878 IV iv; 1013 IV iv;
879 } *civ, const_iv[] = { 1014 } *civ, const_iv[] = {
880#define const_iv(name) { # name, name }, 1015#define const_iv(name) { # name, name },
881 const_iv (ASN_BOOLEAN) 1016 const_iv (ASN_BOOLEAN)
882 const_iv (ASN_INTEGER32) 1017 const_iv (ASN_INTEGER)
883 const_iv (ASN_BIT_STRING) 1018 const_iv (ASN_BIT_STRING)
884 const_iv (ASN_OCTET_STRING) 1019 const_iv (ASN_OCTET_STRING)
885 const_iv (ASN_NULL) 1020 const_iv (ASN_NULL)
886 const_iv (ASN_OBJECT_IDENTIFIER) 1021 const_iv (ASN_OBJECT_IDENTIFIER)
887 const_iv (ASN_OBJECT_DESCRIPTOR) 1022 const_iv (ASN_OBJECT_DESCRIPTOR)
916 const_iv (ASN_CONTEXT) 1051 const_iv (ASN_CONTEXT)
917 const_iv (ASN_PRIVATE) 1052 const_iv (ASN_PRIVATE)
918 1053
919 const_iv (BER_CLASS) 1054 const_iv (BER_CLASS)
920 const_iv (BER_TAG) 1055 const_iv (BER_TAG)
921 const_iv (BER_CONSTRUCTED) 1056 const_iv (BER_FLAGS)
922 const_iv (BER_DATA) 1057 const_iv (BER_DATA)
923 1058
924 const_iv (BER_TYPE_BYTES) 1059 const_iv (BER_TYPE_BYTES)
925 const_iv (BER_TYPE_UTF8) 1060 const_iv (BER_TYPE_UTF8)
926 const_iv (BER_TYPE_UCS2) 1061 const_iv (BER_TYPE_UCS2)
950ber_decode (SV *ber, SV *profile = &PL_sv_undef) 1085ber_decode (SV *ber, SV *profile = &PL_sv_undef)
951 CODE: 1086 CODE:
952{ 1087{
953 cur_profile = SvPROFILE (profile); 1088 cur_profile = SvPROFILE (profile);
954 STRLEN len; 1089 STRLEN len;
955 buf = SvPVbyte (ber, len); 1090 buf = (U8 *)SvPVbyte (ber, len);
956 cur = buf; 1091 cur = buf;
957 end = buf + len; 1092 end = buf + len;
958 1093
959 RETVAL = decode_ber (); 1094 RETVAL = decode_ber ();
960} 1095}
961 OUTPUT: RETVAL 1096 OUTPUT: RETVAL
962 1097
963void 1098void
964ber_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)
965 PPCODE: 1100 PPCODE:
966{ 1101{
967 if (!SvOK (tuple)) 1102 if (!SvOK (tuple))
968 XSRETURN_NO; 1103 XSRETURN_NO;
969 1104
971 croak ("ber_is: tuple must be BER tuple (array-ref)"); 1106 croak ("ber_is: tuple must be BER tuple (array-ref)");
972 1107
973 AV *av = (AV *)SvRV (tuple); 1108 AV *av = (AV *)SvRV (tuple);
974 1109
975 XPUSHs ( 1110 XPUSHs (
976 (!SvOK (klass) || SvIV (AvARRAY (av)[BER_CLASS ]) == SvIV (klass)) 1111 (!SvOK (klass) || SvIV (AvARRAY (av)[BER_CLASS]) == SvIV (klass))
977 && (!SvOK (tag) || SvIV (AvARRAY (av)[BER_TAG ]) == SvIV (tag)) 1112 && (!SvOK (tag) || SvIV (AvARRAY (av)[BER_TAG ]) == SvIV (tag))
978 && (!SvOK (constructed) || !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) == !SvIV (constructed)) 1113 && (!SvOK (flags) || !SvIV (AvARRAY (av)[BER_FLAGS]) == !SvIV (flags))
979 && (!SvOK (data) || sv_eq (AvARRAY (av)[BER_DATA ], data)) 1114 && (!SvOK (data) || sv_eq (AvARRAY (av)[BER_DATA ], data))
980 ? &PL_sv_yes : &PL_sv_undef); 1115 ? &PL_sv_yes : &PL_sv_undef);
981} 1116}
982 1117
983void 1118void
984ber_is_seq (SV *tuple) 1119ber_is_seq (SV *tuple)
988 XSRETURN_UNDEF; 1123 XSRETURN_UNDEF;
989 1124
990 AV *av = ber_tuple (tuple); 1125 AV *av = ber_tuple (tuple);
991 1126
992 XPUSHs ( 1127 XPUSHs (
993 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL 1128 SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL
994 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_SEQUENCE 1129 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_SEQUENCE
995 && SvIV (AvARRAY (av)[BER_CONSTRUCTED]) 1130 && SvIV (AvARRAY (av)[BER_FLAGS])
996 ? AvARRAY (av)[BER_DATA] : &PL_sv_undef); 1131 ? AvARRAY (av)[BER_DATA] : &PL_sv_undef);
997} 1132}
998 1133
999void 1134void
1000ber_is_i32 (SV *tuple, SV *value = &PL_sv_undef) 1135ber_is_int (SV *tuple, SV *value = &PL_sv_undef)
1001 PPCODE: 1136 PPCODE:
1002{ 1137{
1003 if (!SvOK (tuple)) 1138 if (!SvOK (tuple))
1004 XSRETURN_NO; 1139 XSRETURN_NO;
1005 1140
1006 AV *av = ber_tuple (tuple); 1141 AV *av = ber_tuple (tuple);
1007 1142
1008 IV data = SvIV (AvARRAY (av)[BER_DATA]); 1143 UV data = SvUV (AvARRAY (av)[BER_DATA]);
1009 1144
1010 XPUSHs ( 1145 XPUSHs (
1011 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL 1146 SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL
1012 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_INTEGER32 1147 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_INTEGER
1013 && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) 1148 && !SvIV (AvARRAY (av)[BER_FLAGS])
1014 && (!SvOK (value) || data == SvIV (value)) 1149 && (!SvOK (value) || data == SvUV (value))
1015 ? sv_2mortal (data ? newSViv (data) : newSVpv ("0 but true", 0)) 1150 ? sv_2mortal (data ? newSVsv (AvARRAY (av)[BER_DATA]) : newSVpv ("0 but true", 0))
1016 : &PL_sv_undef); 1151 : &PL_sv_undef);
1017} 1152}
1018 1153
1019void 1154void
1020ber_is_oid (SV *tuple, SV *oid = &PL_sv_undef) 1155ber_is_oid (SV *tuple, SV *oid = &PL_sv_undef)
1024 XSRETURN_NO; 1159 XSRETURN_NO;
1025 1160
1026 AV *av = ber_tuple (tuple); 1161 AV *av = ber_tuple (tuple);
1027 1162
1028 XPUSHs ( 1163 XPUSHs (
1029 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL 1164 SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL
1030 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_OBJECT_IDENTIFIER 1165 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_OBJECT_IDENTIFIER
1031 && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) 1166 && !SvIV (AvARRAY (av)[BER_FLAGS])
1032 && (!SvOK (oid) || sv_eq (AvARRAY (av)[BER_DATA], oid)) 1167 && (!SvOK (oid) || sv_eq (AvARRAY (av)[BER_DATA], oid))
1033 ? newSVsv (AvARRAY (av)[BER_DATA]) : &PL_sv_undef); 1168 ? newSVsv (AvARRAY (av)[BER_DATA]) : &PL_sv_undef);
1034} 1169}
1035 1170
1036############################################################################# 1171#############################################################################
1049 SvCUR_set (buf_sv, cur - buf); 1184 SvCUR_set (buf_sv, cur - buf);
1050 XPUSHs (buf_sv); 1185 XPUSHs (buf_sv);
1051} 1186}
1052 1187
1053SV * 1188SV *
1054ber_i32 (IV iv) 1189ber_int (SV *sv)
1055 CODE: 1190 CODE:
1056{ 1191{
1057 AV *av = newAV (); 1192 AV *av = newAV ();
1058 av_fill (av, BER_ARRAYSIZE - 1); 1193 av_fill (av, BER_ARRAYSIZE - 1);
1059 AvARRAY (av)[BER_CLASS ] = newSVcacheint (ASN_UNIVERSAL); 1194 AvARRAY (av)[BER_CLASS] = newSVcacheint (ASN_UNIVERSAL);
1060 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_INTEGER32); 1195 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_INTEGER);
1061 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (0); 1196 AvARRAY (av)[BER_FLAGS] = newSVcacheint (0);
1062 AvARRAY (av)[BER_DATA ] = newSViv (iv); 1197 AvARRAY (av)[BER_DATA ] = newSVsv (sv);
1063 RETVAL = newRV_noinc ((SV *)av); 1198 RETVAL = newRV_noinc ((SV *)av);
1064} 1199}
1065 OUTPUT: RETVAL 1200 OUTPUT: RETVAL
1066 1201
1067# TODO: not arrayref, but elements? 1202# TODO: not arrayref, but elements?
1069ber_seq (SV *arrayref) 1204ber_seq (SV *arrayref)
1070 CODE: 1205 CODE:
1071{ 1206{
1072 AV *av = newAV (); 1207 AV *av = newAV ();
1073 av_fill (av, BER_ARRAYSIZE - 1); 1208 av_fill (av, BER_ARRAYSIZE - 1);
1074 AvARRAY (av)[BER_CLASS ] = newSVcacheint (ASN_UNIVERSAL); 1209 AvARRAY (av)[BER_CLASS] = newSVcacheint (ASN_UNIVERSAL);
1075 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_SEQUENCE); 1210 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_SEQUENCE);
1076 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (1); 1211 AvARRAY (av)[BER_FLAGS] = newSVcacheint (1);
1077 AvARRAY (av)[BER_DATA ] = newSVsv (arrayref); 1212 AvARRAY (av)[BER_DATA ] = newSVsv (arrayref);
1078 RETVAL = newRV_noinc ((SV *)av); 1213 RETVAL = newRV_noinc ((SV *)av);
1079} 1214}
1080 OUTPUT: RETVAL 1215 OUTPUT: RETVAL
1081 1216
1082MODULE = 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