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.26 by root, Sun Apr 21 10:42:22 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
133{ 141{
134 if (!SvOK (profile)) 142 if (!SvOK (profile))
135 return default_profile; 143 return default_profile;
136 144
137 if (!SvROK (profile)) 145 if (!SvROK (profile))
138 croak ("invalid profile"); 146 croak ("Convert::BER::XS::Profile expected");
139 147
140 profile = SvRV (profile); 148 profile = SvRV (profile);
141 149
142 if (SvSTASH (profile) != profile_stash) 150 if (SvSTASH (profile) != profile_stash)
143 croak ("invalid profile object"); 151 croak ("Convert::BER::XS::Profile expected");
144 152
145 return (void *)profile; 153 return (void *)profile;
146} 154}
147 155
148static int 156static int
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/////////////////////////////////////////////////////////////////////////////
219 error ("unexpected end of message buffer"); 227 error ("unexpected end of message buffer");
220} 228}
221 229
222// get_* functions fetch something from the buffer 230// get_* functions fetch something from the buffer
223// decode_* functions use get_* fun ctions to decode ber values 231// decode_* functions use get_* fun ctions to decode ber values
232
233// get single octet
234static U8
235get_u8 (void)
236{
237 if (cur == end)
238 error ("unexpected end of message buffer");
239
240 return *cur++;
241}
224 242
225// get n octets 243// get n octets
226static U8 * 244static U8 *
227get_n (UV count) 245get_n (UV count)
228{ 246{
230 U8 *res = cur; 248 U8 *res = cur;
231 cur += count; 249 cur += count;
232 return res; 250 return res;
233} 251}
234 252
235// get single octet
236static U8
237get_u8 (void)
238{
239 if (cur == end)
240 error ("unexpected end of message buffer");
241
242 return *cur++;
243}
244
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 length 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 BER value lengths not supported");
275 return 0; 291
292 case 0x7f:
293 error ("BER 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 ("BER value length too long (must fit into UV)");
279 return 0;
280 297
298#if UVSIZE > 4
299 case 8: res = (res << 8) | get_u8 ();
300 case 7: res = (res << 8) | get_u8 ();
301 case 6: res = (res << 8) | get_u8 ();
302 case 5: res = (res << 8) | get_u8 ();
303#endif
281 case 4: res = (res << 8) | get_u8 (); 304 case 4: res = (res << 8) | get_u8 ();
282 case 3: res = (res << 8) | get_u8 (); 305 case 3: res = (res << 8) | get_u8 ();
283 case 2: res = (res << 8) | get_u8 (); 306 case 2: res = (res << 8) | get_u8 ();
284 case 1: res = (res << 8) | get_u8 (); 307 case 1: res = (res << 8) | get_u8 ();
285 } 308 }
287 310
288 return res; 311 return res;
289} 312}
290 313
291static SV * 314static SV *
292decode_int () 315decode_int (void)
293{ 316{
294 int len = get_length (); 317 UV len = get_length ();
295 318
296 if (len <= 0) 319 if (!len)
297 { 320 error ("invalid BER_TYPE_INT length zero (X.690 8.3.1)");
298 error ("integer length equal to zero");
299 return 0;
300 }
301 321
302 U8 *data = get_n (len); 322 U8 *data = get_n (len);
303 323
324 if (expect_false (len > 1))
325 {
326 U16 mask = (data [0] << 8) | data [1] & 0xff80;
327
328 if (expect_false (mask == 0xff80 || mask == 0x0000))
329 error ("illegal padding in BER_TYPE_INT (X.690 8.3.2)");
330 }
331
304 int negative = data [0] & 0x80; 332 int negative = data [0] & 0x80;
305 333
306 UV val = negative ? -1 : 0; // copy signbit to all bits 334 UV val = negative ? -1 : 0; // copy signbit to all bits
335
336 if (len > UVSIZE + (!negative && !*data))
337 error ("BER_TYPE_INT overflow");
307 338
308 do 339 do
309 val = (val << 8) | *data++; 340 val = (val << 8) | *data++;
310 while (--len); 341 while (--len);
311 342
315} 346}
316 347
317static SV * 348static SV *
318decode_data (void) 349decode_data (void)
319{ 350{
320 U32 len = get_length (); 351 UV len = get_length ();
321 U8 *data = get_n (len);
322 return newSVpvn ((char *)data, len); 352 return newSVpvn ((char *)get_n (len), len);
323} 353}
324 354
325// gelper for decode_object_identifier 355// helper for decode_object_identifier
326static char * 356static char *
327write_uv (char *buf, U32 u) 357write_uv (char *buf, UV u)
328{ 358{
329 // the one-digit case is absolutely predominant, so this pays off (hopefully) 359 // the one-digit case is absolutely predominant, so this pays off (hopefully)
330 if (expect_true (u < 10)) 360 if (expect_true (u < 10))
331 *buf++ = u + '0'; 361 *buf++ = u + '0';
332 else 362 else
333 { 363 {
364 // this *could* be done much faster using branchless fixed-point arithmetics
334 char *beg = buf; 365 char *beg = buf;
335 366
336 do 367 do
337 { 368 {
338 *buf++ = u % 10 + '0'; 369 *buf++ = u % 10 + '0';
339 u /= 10; 370 u /= 10;
340 } 371 }
341 while (u); 372 while (u);
342 373
343 // reverse digits 374 // reverse digits
344 for (char *ptr = buf; --ptr != beg; ++beg) 375 char *ptr = buf;
376 while (--ptr > beg)
345 { 377 {
346 char c = *ptr; 378 char c = *ptr;
347 *ptr = *beg; 379 *ptr = *beg;
348 *beg = c; 380 *beg = c;
381 ++beg;
349 } 382 }
350 } 383 }
351 384
352 return buf; 385 return buf;
353} 386}
354 387
355static SV * 388static SV *
356decode_oid (int relative) 389decode_oid (int relative)
357{ 390{
358 U32 len = get_length (); 391 UV len = get_length ();
359 392
360 if (len <= 0) 393 if (len <= 0)
361 { 394 {
362 error ("OBJECT IDENTIFIER length equal to zero"); 395 error ("BER_TYPE_OID length must not be zero");
363 return &PL_sv_undef; 396 return &PL_sv_undef;
364 } 397 }
365 398
366 U8 *end = cur + len; 399 U8 *end = cur + len;
367 U32 w = get_w (); 400 UV w = get_w ();
368 401
369 static char oid[MAX_OID_STRLEN]; // must be static 402 static char oid[MAX_OID_STRLEN]; // static, because too large for stack
370 char *app = oid; 403 char *app = oid;
371 404
372 if (relative) 405 if (relative)
373 app = write_uv (app, w); 406 app = write_uv (app, w);
374 else 407 else if (w < 2 * 40)
375 { 408 {
376 app = write_uv (app, (U8)w / 40); 409 app = write_uv (app, (U8)w / 40);
377 *app++ = '.'; 410 *app++ = '.';
378 app = write_uv (app, (U8)w % 40); 411 app = write_uv (app, (U8)w % 40);
379 } 412 }
413 else
414 {
415 app = write_uv (app, 2);
416 *app++ = '.';
417 app = write_uv (app, w - 2 * 40);
418 }
380 419
420 while (cur < end)
421 {
381 // we assume an oid component is never > 64 bytes 422 // we assume an oid component is never > 64 digits
382 while (cur < end && oid + sizeof (oid) - app > 64) 423 if (oid + sizeof (oid) - app < 64)
383 { 424 croak ("BER_TYPE_OID to long to decode");
425
384 w = get_w (); 426 w = get_w ();
385 *app++ = '.'; 427 *app++ = '.';
386 app = write_uv (app, w); 428 app = write_uv (app, w);
387 } 429 }
388 430
393static SV * 435static SV *
394decode_ucs (int chrsize) 436decode_ucs (int chrsize)
395{ 437{
396 SV *res = NEWSV (0, 0); 438 SV *res = NEWSV (0, 0);
397 439
398 U32 len = get_length (); 440 UV len = get_length ();
399 441
400 if (len & (chrsize - 1)) 442 if (len & (chrsize - 1))
401 croak ("BER_TYPE_UCS has an invalid number of octets (%d)", len); 443 croak ("BER_TYPE_UCS has an invalid number of octets (%d)", len);
402 444
403 while (len) 445 while (len)
424 466
425 return res; 467 return res;
426} 468}
427 469
428static SV * 470static SV *
429decode_ber () 471decode_ber (void)
430{ 472{
431 int identifier = get_u8 (); 473 int identifier = get_u8 ();
432 474
433 SV *res; 475 SV *res;
434 476
437 int tag = identifier & ASN_TAG_MASK; 479 int tag = identifier & ASN_TAG_MASK;
438 480
439 if (tag == ASN_TAG_BER) 481 if (tag == ASN_TAG_BER)
440 tag = get_w (); 482 tag = get_w ();
441 483
442 if (tag == ASN_TAG_BER)
443 tag = get_w ();
444
445 if (constructed) 484 if (constructed)
446 { 485 {
447 U32 len = get_length (); 486 UV len = get_length ();
448 U32 seqend = (cur - buf) + len; 487 UV seqend = (cur - buf) + len;
449 AV *av = (AV *)sv_2mortal ((SV *)newAV ()); 488 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
450 489
451 while (cur < buf + seqend) 490 while (cur < buf + seqend)
452 av_push (av, decode_ber ()); 491 av_push (av, decode_ber ());
453 492
454 if (cur > buf + seqend) 493 if (cur > buf + seqend)
455 croak ("constructed type %02x overflow (%x %x)\n", identifier, cur - buf, seqend); 494 croak ("CONSTRUCTED type %02x length overflow (0x%x 0x%x)\n", identifier, (int)(cur - buf), (int)seqend);
456 495
457 res = newRV_inc ((SV *)av); 496 res = newRV_inc ((SV *)av);
458 } 497 }
459 else 498 else
460 switch (profile_lookup (cur_profile, klass, tag)) 499 switch (profile_lookup (cur_profile, klass, tag))
461 { 500 {
462 case BER_TYPE_NULL: 501 case BER_TYPE_NULL:
502 {
503 UV len = get_length ();
504
505 if (len)
506 croak ("BER_TYPE_NULL value with non-zero length %d encountered (X.690 8.8.2)", len);
507
463 res = &PL_sv_undef; 508 res = &PL_sv_undef;
509 }
464 break; 510 break;
465 511
466 case BER_TYPE_BOOL: 512 case BER_TYPE_BOOL:
467 { 513 {
468 U32 len = get_length (); 514 UV len = get_length ();
469 515
470 if (len != 1) 516 if (len != 1)
471 croak ("BER_TYPE_BOOLEAN type with invalid length %d encountered", len); 517 croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered (X.690 8.2.1)", len);
472 518
473 res = newSVcacheint (get_u8 () ? 0 : 1); 519 res = newSVcacheint (!!get_u8 ());
474 } 520 }
475 break; 521 break;
476 522
477 case BER_TYPE_OID: 523 case BER_TYPE_OID:
478 res = decode_oid (0); 524 res = decode_oid (0);
495 res = decode_data (); 541 res = decode_data ();
496 break; 542 break;
497 543
498 case BER_TYPE_IPADDRESS: 544 case BER_TYPE_IPADDRESS:
499 { 545 {
500 U32 len = get_length (); 546 UV len = get_length ();
501 547
502 if (len != 4) 548 if (len != 4)
503 croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered", len); 549 croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered (RFC 2578 7.1.5)", len);
504 550
505 U8 c1 = get_u8 (); 551 U8 c1 = get_u8 ();
506 U8 c2 = get_u8 (); 552 U8 c2 = get_u8 ();
507 U8 c3 = get_u8 (); 553 U8 c3 = get_u8 ();
508 U8 c4 = get_u8 (); 554 U8 c4 = get_u8 ();
518 case BER_TYPE_UCS4: 564 case BER_TYPE_UCS4:
519 res = decode_ucs (4); 565 res = decode_ucs (4);
520 break; 566 break;
521 567
522 case BER_TYPE_REAL: 568 case BER_TYPE_REAL:
569 error ("BER_TYPE_REAL not implemented");
570
523 case BER_TYPE_CROAK: 571 case BER_TYPE_CROAK:
572 croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag);
573
524 default: 574 default:
525 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 575 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
526 } 576 }
527 577
528 AV *av = newAV (); 578 AV *av = newAV ();
529 av_fill (av, BER_ARRAYSIZE - 1); 579 av_fill (av, BER_ARRAYSIZE - 1);
530 AvARRAY (av)[BER_CLASS ] = newSVcacheint (klass); 580 AvARRAY (av)[BER_CLASS] = newSVcacheint (klass);
531 AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); 581 AvARRAY (av)[BER_TAG ] = newSVcacheint (tag);
532 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (constructed ? 1 : 0); 582 AvARRAY (av)[BER_FLAGS] = newSVcacheint (constructed ? 1 : 0);
533 AvARRAY (av)[BER_DATA ] = res; 583 AvARRAY (av)[BER_DATA ] = res;
534 584
535 return newRV_noinc ((SV *)av); 585 return newRV_noinc ((SV *)av);
536} 586}
537 587
538///////////////////////////////////////////////////////////////////////////// 588/////////////////////////////////////////////////////////////////////////////
543strlen_sum (STRLEN l1, STRLEN l2) 593strlen_sum (STRLEN l1, STRLEN l2)
544{ 594{
545 size_t sum = l1 + l2; 595 size_t sum = l1 + l2;
546 596
547 if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) 597 if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum)
548 croak ("JSON::XS: string size overflow"); 598 croak ("Convert::BER::XS: string size overflow");
549 599
550 return sum; 600 return sum;
551} 601}
552 602
553static void 603static void
554set_buf (SV *sv) 604set_buf (SV *sv)
555{ 605{
556 STRLEN len; 606 STRLEN len;
557 buf_sv = sv; 607 buf_sv = sv;
558 buf = SvPVbyte (buf_sv, len); 608 buf = (U8 *)SvPVbyte (buf_sv, len);
559 cur = buf; 609 cur = buf;
560 end = buf + len; 610 end = buf + len;
561} 611}
562 612
563/* similar to SvGROW, but somewhat safer and guarantees exponential realloc strategy */ 613/* similar to SvGROW, but somewhat safer and guarantees exponential realloc strategy */
577need (STRLEN len) 627need (STRLEN len)
578{ 628{
579 if (expect_false ((uintptr_t)(end - cur) < len)) 629 if (expect_false ((uintptr_t)(end - cur) < len))
580 { 630 {
581 STRLEN pos = cur - buf; 631 STRLEN pos = cur - buf;
582 buf = my_sv_grow (buf_sv, pos, len); 632 buf = (U8 *)my_sv_grow (buf_sv, pos, len);
583 cur = buf + pos; 633 cur = buf + pos;
584 end = buf + SvLEN (buf_sv) - 1; 634 end = buf + SvLEN (buf_sv) - 1;
585 } 635 }
586} 636}
587 637
591 need (1); 641 need (1);
592 *cur++ = val; 642 *cur++ = val;
593} 643}
594 644
595static void 645static void
596put_w_nocheck (U32 val) 646put_w_nocheck (UV val)
597{ 647{
648#if UVSIZE > 4
649 *cur = (val >> 7 * 9) | 0x80; cur += val >= ((UV)1 << (7 * 9));
650 *cur = (val >> 7 * 8) | 0x80; cur += val >= ((UV)1 << (7 * 8));
651 *cur = (val >> 7 * 7) | 0x80; cur += val >= ((UV)1 << (7 * 7));
652 *cur = (val >> 7 * 6) | 0x80; cur += val >= ((UV)1 << (7 * 6));
653 *cur = (val >> 7 * 5) | 0x80; cur += val >= ((UV)1 << (7 * 5));
654#endif
598 *cur = (val >> 7 * 4) | 0x80; cur += val >= (1 << (7 * 4)); 655 *cur = (val >> 7 * 4) | 0x80; cur += val >= ((UV)1 << (7 * 4));
599 *cur = (val >> 7 * 3) | 0x80; cur += val >= (1 << (7 * 3)); 656 *cur = (val >> 7 * 3) | 0x80; cur += val >= ((UV)1 << (7 * 3));
600 *cur = (val >> 7 * 2) | 0x80; cur += val >= (1 << (7 * 2)); 657 *cur = (val >> 7 * 2) | 0x80; cur += val >= ((UV)1 << (7 * 2));
601 *cur = (val >> 7 * 1) | 0x80; cur += val >= (1 << (7 * 1)); 658 *cur = (val >> 7 * 1) | 0x80; cur += val >= ((UV)1 << (7 * 1));
602 *cur = val & 0x7f; cur += 1; 659 *cur = val & 0x7f; cur += 1;
603} 660}
604 661
605static void 662static void
606put_w (U32 val) 663put_w (UV val)
607{ 664{
608 need (5); // we only handle up to 5 bytes 665 need (5); // we only handle up to 5 bytes
609 666
610 put_w_nocheck (val); 667 put_w_nocheck (val);
611} 668}
612 669
613static U8 * 670static U8 *
614put_length_at (U32 val, U8 *cur) 671put_length_at (UV val, U8 *cur)
615{ 672{
616 if (val < 0x7fU) 673 if (val < 0x7fU)
617 *cur++ = val; 674 *cur++ = val;
618 else 675 else
619 { 676 {
620 U8 *lenb = cur++; 677 U8 *lenb = cur++;
621 678
679#if UVSIZE > 4
680 *cur = val >> 56; cur += *cur > 0;
681 *cur = val >> 48; cur += *cur > 0;
682 *cur = val >> 40; cur += *cur > 0;
683 *cur = val >> 32; cur += *cur > 0;
684#endif
622 *cur = val >> 24; cur += *cur > 0; 685 *cur = val >> 24; cur += *cur > 0;
623 *cur = val >> 16; cur += *cur > 0; 686 *cur = val >> 16; cur += *cur > 0;
624 *cur = val >> 8; cur += *cur > 0; 687 *cur = val >> 8; cur += *cur > 0;
625 *cur = val ; cur += 1; 688 *cur = val ; cur += 1;
626 689
629 692
630 return cur; 693 return cur;
631} 694}
632 695
633static void 696static void
634put_length (U32 val) 697put_length (UV val)
635{ 698{
636 need (5 + val); 699 need (5 + val);
637 cur = put_length_at (val, cur); 700 cur = put_length_at (val, cur);
638} 701}
639 702
640// return how many bytes the encoded length requires 703// return how many bytes the encoded length requires
641static int length_length (U32 val) 704static int length_length (UV val)
642{ 705{
643 return val < 0x7fU 706 return val < 0x7fU
644 ? 1 707 ? 1
645 : 2 + (val > 0xffU) + (val > 0xffffU) + (val > 0xffffffU); 708 : 2
709 + (val > 0xffU)
710 + (val > 0xffffU)
711 + (val > 0xffffffU)
712#if UVSIZE > 4
713 + (val > 0xffffffffU)
714 + (val > 0xffffffffffU)
715 + (val > 0xffffffffffffU)
716 + (val > 0xffffffffffffffU)
717#endif
718 ;
646} 719}
647 720
648static void 721static void
649encode_data (const char *ptr, STRLEN len) 722encode_data (const char *ptr, STRLEN len)
650{ 723{
712 785
713 *lenb = cur - lenb - 1; 786 *lenb = cur - lenb - 1;
714} 787}
715 788
716// we don't know the length yet, so we optimistically 789// we don't know the length yet, so we optimistically
717// assume the length will need one octet later. if that 790// assume the length will need one octet later. If that
718// turns out to be wrong, we memove as needed. 791// turns out to be wrong, we memmove as needed.
719// mark the beginning 792// mark the beginning
720static STRLEN 793static STRLEN
721len_fixup_mark () 794len_fixup_mark (void)
722{ 795{
723 return cur++ - buf; 796 return cur++ - buf;
724} 797}
725 798
726// patch up the length 799// patch up the length
813 put_length (uchars * chrsize); 886 put_length (uchars * chrsize);
814 887
815 while (uchars--) 888 while (uchars--)
816 { 889 {
817 STRLEN uclen; 890 STRLEN uclen;
818 UV uchr = utf8_to_uvchr_buf (ptr, ptr + len, &uclen); 891 UV uchr = utf8_to_uvchr_buf ((U8 *)ptr, (U8 *)ptr + len, &uclen);
819 892
820 ptr += uclen; 893 ptr += uclen;
821 len -= uclen; 894 len -= uclen;
822 895
823 if (chrsize == 4) 896 if (chrsize == 4)
835{ 908{
836 AV *av = ber_tuple (tuple); 909 AV *av = ber_tuple (tuple);
837 910
838 int klass = SvIV (AvARRAY (av)[BER_CLASS]); 911 int klass = SvIV (AvARRAY (av)[BER_CLASS]);
839 int tag = SvIV (AvARRAY (av)[BER_TAG]); 912 int tag = SvIV (AvARRAY (av)[BER_TAG]);
840 int constructed = SvIV (AvARRAY (av)[BER_CONSTRUCTED]) ? ASN_CONSTRUCTED : 0; 913 int constructed = SvIV (AvARRAY (av)[BER_FLAGS]) & 1 ? ASN_CONSTRUCTED : 0;
841 SV *data = AvARRAY (av)[BER_DATA]; 914 SV *data = AvARRAY (av)[BER_DATA];
842 915
843 int identifier = (klass << ASN_CLASS_SHIFT) | constructed; 916 int identifier = (klass << ASN_CLASS_SHIFT) | constructed;
844 917
845 if (expect_false (tag >= ASN_TAG_BER)) 918 if (expect_false (tag >= ASN_TAG_BER))
856 // and adjust later 929 // and adjust later
857 need (1); 930 need (1);
858 STRLEN mark = len_fixup_mark (); 931 STRLEN mark = len_fixup_mark ();
859 932
860 if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV)) 933 if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV))
861 croak ("BER constructed data must be array-reference"); 934 croak ("BER CONSTRUCTED data must be array-reference");
862 935
863 AV *av = (AV *)SvRV (data); 936 AV *av = (AV *)SvRV (data);
864 int fill = AvFILL (av); 937 int fill = AvFILL (av);
865 938
866 if (expect_false (SvRMAGICAL (av))) 939 if (expect_false (SvRMAGICAL (av)))
867 croak ("BER constructed data must not be tied"); 940 croak ("BER CONSTRUCTED data must not be tied");
868 941
942 int i;
869 for (int i = 0; i <= fill; ++i) 943 for (i = 0; i <= fill; ++i)
870 encode_ber (AvARRAY (av)[i]); 944 encode_ber (AvARRAY (av)[i]);
871 945
872 len_fixup (mark); 946 len_fixup (mark);
873 } 947 }
874 else 948 else
878 put_length (0); 952 put_length (0);
879 break; 953 break;
880 954
881 case BER_TYPE_BOOL: 955 case BER_TYPE_BOOL:
882 put_length (1); 956 put_length (1);
883 *cur++ = SvTRUE (data) ? 0xff : 0x00; 957 *cur++ = SvTRUE (data) ? 0xff : 0x00; // 0xff = DER/CER
884 break; 958 break;
885 959
886 case BER_TYPE_OID: 960 case BER_TYPE_OID:
887 encode_oid (data, 0); 961 encode_oid (data, 0);
888 break; 962 break;
926 case BER_TYPE_UCS4: 1000 case BER_TYPE_UCS4:
927 encode_ucs (data, 4); 1001 encode_ucs (data, 4);
928 break; 1002 break;
929 1003
930 case BER_TYPE_REAL: 1004 case BER_TYPE_REAL:
1005 croak ("BER_TYPE_REAL not implemented");
1006
931 case BER_TYPE_CROAK: 1007 case BER_TYPE_CROAK:
1008 croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag);
1009
932 default: 1010 default:
933 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 1011 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
934 } 1012 }
935 1013
936} 1014}
951 const char *name; 1029 const char *name;
952 IV iv; 1030 IV iv;
953 } *civ, const_iv[] = { 1031 } *civ, const_iv[] = {
954#define const_iv(name) { # name, name }, 1032#define const_iv(name) { # name, name },
955 const_iv (ASN_BOOLEAN) 1033 const_iv (ASN_BOOLEAN)
956 const_iv (ASN_INTEGER32) 1034 const_iv (ASN_INTEGER)
957 const_iv (ASN_BIT_STRING) 1035 const_iv (ASN_BIT_STRING)
958 const_iv (ASN_OCTET_STRING) 1036 const_iv (ASN_OCTET_STRING)
959 const_iv (ASN_NULL) 1037 const_iv (ASN_NULL)
960 const_iv (ASN_OBJECT_IDENTIFIER) 1038 const_iv (ASN_OBJECT_IDENTIFIER)
961 const_iv (ASN_OBJECT_DESCRIPTOR) 1039 const_iv (ASN_OBJECT_DESCRIPTOR)
990 const_iv (ASN_CONTEXT) 1068 const_iv (ASN_CONTEXT)
991 const_iv (ASN_PRIVATE) 1069 const_iv (ASN_PRIVATE)
992 1070
993 const_iv (BER_CLASS) 1071 const_iv (BER_CLASS)
994 const_iv (BER_TAG) 1072 const_iv (BER_TAG)
995 const_iv (BER_CONSTRUCTED) 1073 const_iv (BER_FLAGS)
996 const_iv (BER_DATA) 1074 const_iv (BER_DATA)
997 1075
998 const_iv (BER_TYPE_BYTES) 1076 const_iv (BER_TYPE_BYTES)
999 const_iv (BER_TYPE_UTF8) 1077 const_iv (BER_TYPE_UTF8)
1000 const_iv (BER_TYPE_UCS2) 1078 const_iv (BER_TYPE_UCS2)
1008 const_iv (BER_TYPE_IPADDRESS) 1086 const_iv (BER_TYPE_IPADDRESS)
1009 const_iv (BER_TYPE_CROAK) 1087 const_iv (BER_TYPE_CROAK)
1010 1088
1011 const_iv (SNMP_IPADDRESS) 1089 const_iv (SNMP_IPADDRESS)
1012 const_iv (SNMP_COUNTER32) 1090 const_iv (SNMP_COUNTER32)
1091 const_iv (SNMP_GAUGE32)
1013 const_iv (SNMP_UNSIGNED32) 1092 const_iv (SNMP_UNSIGNED32)
1014 const_iv (SNMP_TIMETICKS) 1093 const_iv (SNMP_TIMETICKS)
1015 const_iv (SNMP_OPAQUE) 1094 const_iv (SNMP_OPAQUE)
1016 const_iv (SNMP_COUNTER64) 1095 const_iv (SNMP_COUNTER64)
1017 }; 1096 };
1018 1097
1019 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) 1098 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)); 1099 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv));
1021} 1100}
1022 1101
1023SV * 1102void
1024ber_decode (SV *ber, SV *profile = &PL_sv_undef) 1103ber_decode (SV *ber, SV *profile = &PL_sv_undef)
1104 ALIAS:
1105 ber_decode_prefix = 1
1025 CODE: 1106 PPCODE:
1026{ 1107{
1027 cur_profile = SvPROFILE (profile); 1108 cur_profile = SvPROFILE (profile);
1028 STRLEN len; 1109 STRLEN len;
1029 buf = SvPVbyte (ber, len); 1110 buf = (U8 *)SvPVbyte (ber, len);
1030 cur = buf; 1111 cur = buf;
1031 end = buf + len; 1112 end = buf + len;
1032 1113
1033 RETVAL = decode_ber (); 1114 SV *tuple = decode_ber ();
1115
1116 EXTEND (SP, 2);
1117 PUSHs (sv_2mortal (tuple));
1118
1119 if (ix)
1120 PUSHs (sv_2mortal (newSViv (cur - buf)));
1121 else if (cur != end)
1122 error ("trailing garbage after BER value");
1034} 1123}
1035 OUTPUT: RETVAL
1036 1124
1037void 1125void
1038ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *constructed = &PL_sv_undef, SV *data = &PL_sv_undef) 1126ber_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: 1127 PPCODE:
1040{ 1128{
1041 if (!SvOK (tuple)) 1129 if (!SvOK (tuple))
1042 XSRETURN_NO; 1130 XSRETURN_NO;
1043 1131
1045 croak ("ber_is: tuple must be BER tuple (array-ref)"); 1133 croak ("ber_is: tuple must be BER tuple (array-ref)");
1046 1134
1047 AV *av = (AV *)SvRV (tuple); 1135 AV *av = (AV *)SvRV (tuple);
1048 1136
1049 XPUSHs ( 1137 XPUSHs (
1050 (!SvOK (klass) || SvIV (AvARRAY (av)[BER_CLASS ]) == SvIV (klass)) 1138 (!SvOK (klass) || SvIV (AvARRAY (av)[BER_CLASS]) == SvIV (klass))
1051 && (!SvOK (tag) || SvIV (AvARRAY (av)[BER_TAG ]) == SvIV (tag)) 1139 && (!SvOK (tag) || SvIV (AvARRAY (av)[BER_TAG ]) == SvIV (tag))
1052 && (!SvOK (constructed) || !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) == !SvIV (constructed)) 1140 && (!SvOK (flags) || !SvIV (AvARRAY (av)[BER_FLAGS]) == !SvIV (flags))
1053 && (!SvOK (data) || sv_eq (AvARRAY (av)[BER_DATA ], data)) 1141 && (!SvOK (data) || sv_eq (AvARRAY (av)[BER_DATA ], data))
1054 ? &PL_sv_yes : &PL_sv_undef); 1142 ? &PL_sv_yes : &PL_sv_undef);
1055} 1143}
1056 1144
1057void 1145void
1058ber_is_seq (SV *tuple) 1146ber_is_seq (SV *tuple)
1062 XSRETURN_UNDEF; 1150 XSRETURN_UNDEF;
1063 1151
1064 AV *av = ber_tuple (tuple); 1152 AV *av = ber_tuple (tuple);
1065 1153
1066 XPUSHs ( 1154 XPUSHs (
1067 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL 1155 SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL
1068 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_SEQUENCE 1156 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_SEQUENCE
1069 && SvIV (AvARRAY (av)[BER_CONSTRUCTED]) 1157 && SvIV (AvARRAY (av)[BER_FLAGS])
1070 ? AvARRAY (av)[BER_DATA] : &PL_sv_undef); 1158 ? AvARRAY (av)[BER_DATA] : &PL_sv_undef);
1071} 1159}
1072 1160
1073void 1161void
1074ber_is_i32 (SV *tuple, SV *value = &PL_sv_undef) 1162ber_is_int (SV *tuple, SV *value = &PL_sv_undef)
1075 PPCODE: 1163 PPCODE:
1076{ 1164{
1077 if (!SvOK (tuple)) 1165 if (!SvOK (tuple))
1078 XSRETURN_NO; 1166 XSRETURN_NO;
1079 1167
1080 AV *av = ber_tuple (tuple); 1168 AV *av = ber_tuple (tuple);
1081 1169
1082 IV data = SvIV (AvARRAY (av)[BER_DATA]); 1170 UV data = SvUV (AvARRAY (av)[BER_DATA]);
1083 1171
1084 XPUSHs ( 1172 XPUSHs (
1085 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL 1173 SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL
1086 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_INTEGER32 1174 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_INTEGER
1087 && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) 1175 && !SvIV (AvARRAY (av)[BER_FLAGS])
1088 && (!SvOK (value) || data == SvIV (value)) 1176 && (!SvOK (value) || data == SvUV (value))
1089 ? sv_2mortal (data ? newSViv (data) : newSVpv ("0 but true", 0)) 1177 ? sv_2mortal (data ? newSVsv (AvARRAY (av)[BER_DATA]) : newSVpv ("0 but true", 0))
1090 : &PL_sv_undef); 1178 : &PL_sv_undef);
1091} 1179}
1092 1180
1093void 1181void
1094ber_is_oid (SV *tuple, SV *oid = &PL_sv_undef) 1182ber_is_oid (SV *tuple, SV *oid = &PL_sv_undef)
1098 XSRETURN_NO; 1186 XSRETURN_NO;
1099 1187
1100 AV *av = ber_tuple (tuple); 1188 AV *av = ber_tuple (tuple);
1101 1189
1102 XPUSHs ( 1190 XPUSHs (
1103 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL 1191 SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL
1104 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_OBJECT_IDENTIFIER 1192 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_OBJECT_IDENTIFIER
1105 && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) 1193 && !SvIV (AvARRAY (av)[BER_FLAGS])
1106 && (!SvOK (oid) || sv_eq (AvARRAY (av)[BER_DATA], oid)) 1194 && (!SvOK (oid) || sv_eq (AvARRAY (av)[BER_DATA], oid))
1107 ? newSVsv (AvARRAY (av)[BER_DATA]) : &PL_sv_undef); 1195 ? newSVsv (AvARRAY (av)[BER_DATA]) : &PL_sv_undef);
1108} 1196}
1109 1197
1110############################################################################# 1198#############################################################################
1123 SvCUR_set (buf_sv, cur - buf); 1211 SvCUR_set (buf_sv, cur - buf);
1124 XPUSHs (buf_sv); 1212 XPUSHs (buf_sv);
1125} 1213}
1126 1214
1127SV * 1215SV *
1128ber_i32 (IV iv) 1216ber_int (SV *sv)
1129 CODE: 1217 CODE:
1130{ 1218{
1131 AV *av = newAV (); 1219 AV *av = newAV ();
1132 av_fill (av, BER_ARRAYSIZE - 1); 1220 av_fill (av, BER_ARRAYSIZE - 1);
1133 AvARRAY (av)[BER_CLASS ] = newSVcacheint (ASN_UNIVERSAL); 1221 AvARRAY (av)[BER_CLASS] = newSVcacheint (ASN_UNIVERSAL);
1134 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_INTEGER32); 1222 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_INTEGER);
1135 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (0); 1223 AvARRAY (av)[BER_FLAGS] = newSVcacheint (0);
1136 AvARRAY (av)[BER_DATA ] = newSViv (iv); 1224 AvARRAY (av)[BER_DATA ] = newSVsv (sv);
1137 RETVAL = newRV_noinc ((SV *)av); 1225 RETVAL = newRV_noinc ((SV *)av);
1138} 1226}
1139 OUTPUT: RETVAL 1227 OUTPUT: RETVAL
1140 1228
1141# TODO: not arrayref, but elements? 1229# TODO: not arrayref, but elements?
1143ber_seq (SV *arrayref) 1231ber_seq (SV *arrayref)
1144 CODE: 1232 CODE:
1145{ 1233{
1146 AV *av = newAV (); 1234 AV *av = newAV ();
1147 av_fill (av, BER_ARRAYSIZE - 1); 1235 av_fill (av, BER_ARRAYSIZE - 1);
1148 AvARRAY (av)[BER_CLASS ] = newSVcacheint (ASN_UNIVERSAL); 1236 AvARRAY (av)[BER_CLASS] = newSVcacheint (ASN_UNIVERSAL);
1149 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_SEQUENCE); 1237 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_SEQUENCE);
1150 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (1); 1238 AvARRAY (av)[BER_FLAGS] = newSVcacheint (1);
1151 AvARRAY (av)[BER_DATA ] = newSVsv (arrayref); 1239 AvARRAY (av)[BER_DATA ] = newSVsv (arrayref);
1152 RETVAL = newRV_noinc ((SV *)av); 1240 RETVAL = newRV_noinc ((SV *)av);
1153} 1241}
1154 OUTPUT: RETVAL 1242 OUTPUT: RETVAL
1155 1243
1156MODULE = Convert::BER::XS PACKAGE = Convert::BER::XS::Profile 1244MODULE = Convert::BER::XS PACKAGE = Convert::BER::XS::Profile

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines