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.23 by root, Sun Apr 21 01:51:12 2019 UTC vs.
Revision 1.28 by root, Tue Apr 23 17:55:54 2019 UTC

141{ 141{
142 if (!SvOK (profile)) 142 if (!SvOK (profile))
143 return default_profile; 143 return default_profile;
144 144
145 if (!SvROK (profile)) 145 if (!SvROK (profile))
146 croak ("invalid profile"); 146 croak ("Convert::BER::XS::Profile expected");
147 147
148 profile = SvRV (profile); 148 profile = SvRV (profile);
149 149
150 if (SvSTASH (profile) != profile_stash) 150 if (SvSTASH (profile) != profile_stash)
151 croak ("invalid profile object"); 151 croak ("Convert::BER::XS::Profile expected");
152 152
153 return (void *)profile; 153 return (void *)profile;
154} 154}
155 155
156static int 156static int
228} 228}
229 229
230// get_* functions fetch something from the buffer 230// get_* functions fetch something from the buffer
231// decode_* functions use get_* fun ctions to decode ber values 231// decode_* functions use get_* fun ctions to decode ber values
232 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}
242
233// get n octets 243// get n octets
234static U8 * 244static U8 *
235get_n (UV count) 245get_n (UV count)
236{ 246{
237 want (count); 247 want (count);
238 U8 *res = cur; 248 U8 *res = cur;
239 cur += count; 249 cur += count;
240 return res; 250 return res;
241} 251}
242 252
243// get single octet
244static U8
245get_u8 (void)
246{
247 if (cur == end)
248 error ("unexpected end of message buffer");
249
250 return *cur++;
251}
252
253// get ber-encoded integer (i.e. pack "w") 253// get ber-encoded integer (i.e. pack "w")
254static UV 254static UV
255get_w (void) 255get_w (void)
256{ 256{
257 UV res = 0; 257 UV res = 0;
261 error ("illegal BER padding (X.690 8.1.2.4.2, 8.19.2)"); 261 error ("illegal BER padding (X.690 8.1.2.4.2, 8.19.2)");
262 262
263 for (;;) 263 for (;;)
264 { 264 {
265 if (expect_false (res >> UVSIZE * 8 - 7)) 265 if (expect_false (res >> UVSIZE * 8 - 7))
266 error ("BER variable integer overflow"); 266 error ("BER variable length integer overflow");
267 267
268 res = (res << 7) | (c & 0x7f); 268 res = (res << 7) | (c & 0x7f);
269 269
270 if (!(c & 0x80)) 270 if (!(c & 0x80))
271 return res; 271 return res;
285 res = 0; 285 res = 0;
286 286
287 switch (cnt) 287 switch (cnt)
288 { 288 {
289 case 0: 289 case 0:
290 error ("indefinite ASN.1 lengths not supported"); 290 error ("indefinite BER value lengths not supported");
291 291
292 case 0x7f: 292 case 0x7f:
293 error ("ASN.1 reserved value in length (X.690 8.1.3.5)"); 293 error ("BER reserved value in length (X.690 8.1.3.5)");
294 294
295 default: 295 default:
296 error ("ASN.1 length too long (only up to 2**64 octets supported)"); 296 error ("BER value length too long (must fit into UV)");
297 297
298#if UVSIZE > 4
298 case 8: res = (res << 8) | get_u8 (); 299 case 8: res = (res << 8) | get_u8 ();
299 case 7: res = (res << 8) | get_u8 (); 300 case 7: res = (res << 8) | get_u8 ();
300 case 6: res = (res << 8) | get_u8 (); 301 case 6: res = (res << 8) | get_u8 ();
301 case 5: res = (res << 8) | get_u8 (); 302 case 5: res = (res << 8) | get_u8 ();
303#endif
302 case 4: res = (res << 8) | get_u8 (); 304 case 4: res = (res << 8) | get_u8 ();
303 case 3: res = (res << 8) | get_u8 (); 305 case 3: res = (res << 8) | get_u8 ();
304 case 2: res = (res << 8) | get_u8 (); 306 case 2: res = (res << 8) | get_u8 ();
305 case 1: res = (res << 8) | get_u8 (); 307 case 1: res = (res << 8) | get_u8 ();
306 } 308 }
313decode_int (void) 315decode_int (void)
314{ 316{
315 UV len = get_length (); 317 UV len = get_length ();
316 318
317 if (!len) 319 if (!len)
318 error ("invalid integer length equal to zero (X.690 8.3.1)"); 320 error ("invalid BER_TYPE_INT length zero (X.690 8.3.1)");
319 321
320 U8 *data = get_n (len); 322 U8 *data = get_n (len);
321 323
322 if (expect_false (len > 1)) 324 if (expect_false (len > 1))
323 { 325 {
324 U16 mask = (data [0] << 8) | data [1] & 0xff80; 326 U16 mask = (data [0] << 8) | data [1] & 0xff80;
325 327
326 if (expect_false (mask == 0xff80 || mask == 0x0000)) 328 if (expect_false (mask == 0xff80 || mask == 0x0000))
327 error ("illegal padding in integer (X.690 8.3.2)"); 329 error ("illegal padding in BER_TYPE_INT (X.690 8.3.2)");
328 } 330 }
329 331
330 int negative = data [0] & 0x80; 332 int negative = data [0] & 0x80;
331 333
332 UV val = negative ? -1 : 0; // copy signbit to all bits 334 UV val = negative ? -1 : 0; // copy signbit to all bits
333 335
334 if (len > UVSIZE + (!negative && !*data)) 336 if (len > UVSIZE + (!negative && !*data))
335 //printf ("len %d > %d + (!%d && !%d) = %d\n", len, UVSIZE, negative, *data, UVSIZE + (!negative && !*data));//D
336 error ("INTEGER overflow"); 337 error ("BER_TYPE_INT overflow");
337 338
338 do 339 do
339 val = (val << 8) | *data++; 340 val = (val << 8) | *data++;
340 while (--len); 341 while (--len);
341 342
389{ 390{
390 UV len = get_length (); 391 UV len = get_length ();
391 392
392 if (len <= 0) 393 if (len <= 0)
393 { 394 {
394 error ("OBJECT IDENTIFIER length equal to zero"); 395 error ("BER_TYPE_OID length must not be zero");
395 return &PL_sv_undef; 396 return &PL_sv_undef;
396 } 397 }
397 398
398 U8 *end = cur + len; 399 U8 *end = cur + len;
399 UV w = get_w (); 400 UV w = get_w ();
401 static char oid[MAX_OID_STRLEN]; // static, because too large for stack 402 static char oid[MAX_OID_STRLEN]; // static, because too large for stack
402 char *app = oid; 403 char *app = oid;
403 404
404 if (relative) 405 if (relative)
405 app = write_uv (app, w); 406 app = write_uv (app, w);
407 else
408 {
409 UV w1, w2;
410
406 else if (w < 2 * 40) 411 if (w < 2 * 40)
407 { 412 (w1 = w / 40), (w2 = w % 40);
413 else
414 (w1 = 2), (w2 = w - 2 * 40);
415
408 app = write_uv (app, (U8)w / 40); 416 app = write_uv (app, w1);
409 *app++ = '.'; 417 *app++ = '.';
410 app = write_uv (app, (U8)w % 40);
411 }
412 else
413 {
414 app = write_uv (app, 2); 418 app = write_uv (app, w2);
415 *app++ = '.';
416 app = write_uv (app, w - 2 * 40);
417 } 419 }
418 420
419 while (cur < end) 421 while (cur < end)
420 { 422 {
421 // we assume an oid component is never > 64 digits 423 // we assume an oid component is never > 64 digits
488 490
489 while (cur < buf + seqend) 491 while (cur < buf + seqend)
490 av_push (av, decode_ber ()); 492 av_push (av, decode_ber ());
491 493
492 if (cur > buf + seqend) 494 if (cur > buf + seqend)
493 croak ("constructed type %02x length overflow (0x%x 0x%x)\n", identifier, (int)(cur - buf), (int)seqend); 495 croak ("CONSTRUCTED type %02x length overflow (0x%x 0x%x)\n", identifier, (int)(cur - buf), (int)seqend);
494 496
495 res = newRV_inc ((SV *)av); 497 res = newRV_inc ((SV *)av);
496 } 498 }
497 else 499 else
498 switch (profile_lookup (cur_profile, klass, tag)) 500 switch (profile_lookup (cur_profile, klass, tag))
563 case BER_TYPE_UCS4: 565 case BER_TYPE_UCS4:
564 res = decode_ucs (4); 566 res = decode_ucs (4);
565 break; 567 break;
566 568
567 case BER_TYPE_REAL: 569 case BER_TYPE_REAL:
570 error ("BER_TYPE_REAL not implemented");
571
568 case BER_TYPE_CROAK: 572 case BER_TYPE_CROAK:
573 croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag);
574
569 default: 575 default:
570 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 576 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
571 } 577 }
572 578
573 AV *av = newAV (); 579 AV *av = newAV ();
670 else 676 else
671 { 677 {
672 U8 *lenb = cur++; 678 U8 *lenb = cur++;
673 679
674#if UVSIZE > 4 680#if UVSIZE > 4
675 *cur = val >> 56; cur += *cur > 0; 681 *cur = val >> 56; cur += val >= ((UV)1 << (8 * 7));
676 *cur = val >> 48; cur += *cur > 0; 682 *cur = val >> 48; cur += val >= ((UV)1 << (8 * 6));
677 *cur = val >> 40; cur += *cur > 0; 683 *cur = val >> 40; cur += val >= ((UV)1 << (8 * 5));
678 *cur = val >> 32; cur += *cur > 0; 684 *cur = val >> 32; cur += val >= ((UV)1 << (8 * 4));
679#endif 685#endif
680 *cur = val >> 24; cur += *cur > 0; 686 *cur = val >> 24; cur += val >= ((UV)1 << (8 * 3));
681 *cur = val >> 16; cur += *cur > 0; 687 *cur = val >> 16; cur += val >= ((UV)1 << (8 * 2));
682 *cur = val >> 8; cur += *cur > 0; 688 *cur = val >> 8; cur += val >= ((UV)1 << (8 * 1));
683 *cur = val ; cur += 1; 689 *cur = val ; cur += 1;
684 690
685 *lenb = 0x80 + cur - lenb - 1; 691 *lenb = 0x80 + cur - lenb - 1;
686 } 692 }
687 693
924 // and adjust later 930 // and adjust later
925 need (1); 931 need (1);
926 STRLEN mark = len_fixup_mark (); 932 STRLEN mark = len_fixup_mark ();
927 933
928 if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV)) 934 if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV))
929 croak ("BER constructed data must be array-reference"); 935 croak ("BER CONSTRUCTED data must be array-reference");
930 936
931 AV *av = (AV *)SvRV (data); 937 AV *av = (AV *)SvRV (data);
932 int fill = AvFILL (av); 938 int fill = AvFILL (av);
933 939
934 if (expect_false (SvRMAGICAL (av))) 940 if (expect_false (SvRMAGICAL (av)))
935 croak ("BER constructed data must not be tied"); 941 croak ("BER CONSTRUCTED data must not be tied");
936 942
937 int i; 943 int i;
938 for (i = 0; i <= fill; ++i) 944 for (i = 0; i <= fill; ++i)
939 encode_ber (AvARRAY (av)[i]); 945 encode_ber (AvARRAY (av)[i]);
940 946
995 case BER_TYPE_UCS4: 1001 case BER_TYPE_UCS4:
996 encode_ucs (data, 4); 1002 encode_ucs (data, 4);
997 break; 1003 break;
998 1004
999 case BER_TYPE_REAL: 1005 case BER_TYPE_REAL:
1006 croak ("BER_TYPE_REAL not implemented");
1007
1000 case BER_TYPE_CROAK: 1008 case BER_TYPE_CROAK:
1009 croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag);
1010
1001 default: 1011 default:
1002 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 1012 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
1003 } 1013 }
1004 1014
1005} 1015}
1108 PUSHs (sv_2mortal (tuple)); 1118 PUSHs (sv_2mortal (tuple));
1109 1119
1110 if (ix) 1120 if (ix)
1111 PUSHs (sv_2mortal (newSViv (cur - buf))); 1121 PUSHs (sv_2mortal (newSViv (cur - buf)));
1112 else if (cur != end) 1122 else if (cur != end)
1113 error ("trailing garbage after BER data"); 1123 error ("trailing garbage after BER value");
1114} 1124}
1115 1125
1116void 1126void
1117ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *flags = &PL_sv_undef, SV *data = &PL_sv_undef) 1127ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *flags = &PL_sv_undef, SV *data = &PL_sv_undef)
1118 PPCODE: 1128 PPCODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines