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.20 by root, Sat Apr 20 17:04:35 2019 UTC vs.
Revision 1.25 by root, Sun Apr 21 10:40:30 2019 UTC

58 ASN_CLASS_SHIFT = 6, 58 ASN_CLASS_SHIFT = 6,
59 59
60 // ASN_APPLICATION SNMP 60 // ASN_APPLICATION SNMP
61 SNMP_IPADDRESS = 0x00, 61 SNMP_IPADDRESS = 0x00,
62 SNMP_COUNTER32 = 0x01, 62 SNMP_COUNTER32 = 0x01,
63 SNMP_GAUGE32 = 0x02,
63 SNMP_UNSIGNED32 = 0x02, 64 SNMP_UNSIGNED32 = 0x02,
64 SNMP_TIMETICKS = 0x03, 65 SNMP_TIMETICKS = 0x03,
65 SNMP_OPAQUE = 0x04, 66 SNMP_OPAQUE = 0x04,
66 SNMP_COUNTER64 = 0x06, 67 SNMP_COUNTER64 = 0x06,
67}; 68};
227} 228}
228 229
229// get_* functions fetch something from the buffer 230// get_* functions fetch something from the buffer
230// decode_* functions use get_* fun ctions to decode ber values 231// decode_* functions use get_* fun ctions to decode ber values
231 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
232// get n octets 243// get n octets
233static U8 * 244static U8 *
234get_n (UV count) 245get_n (UV count)
235{ 246{
236 want (count); 247 want (count);
237 U8 *res = cur; 248 U8 *res = cur;
238 cur += count; 249 cur += count;
239 return res; 250 return res;
240} 251}
241 252
242// get single octet
243static U8
244get_u8 (void)
245{
246 if (cur == end)
247 error ("unexpected end of message buffer");
248
249 return *cur++;
250}
251
252// get ber-encoded integer (i.e. pack "w") 253// get ber-encoded integer (i.e. pack "w")
253static UV 254static UV
254get_w (void) 255get_w (void)
255{ 256{
256 UV res = 0; 257 UV res = 0;
259 if (expect_false (c == 0x80)) 260 if (expect_false (c == 0x80))
260 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)");
261 262
262 for (;;) 263 for (;;)
263 { 264 {
265 if (expect_false (res >> UVSIZE * 8 - 7))
266 error ("BER variable length integer overflow");
267
264 res = (res << 7) | (c & 0x7f); 268 res = (res << 7) | (c & 0x7f);
265 269
266 if (!(c & 0x80)) 270 if (!(c & 0x80))
267 return res; 271 return res;
268 272
281 res = 0; 285 res = 0;
282 286
283 switch (cnt) 287 switch (cnt)
284 { 288 {
285 case 0: 289 case 0:
286 error ("indefinite ASN.1 lengths not supported"); 290 error ("indefinite BER value lengths not supported");
287 291
288 case 0x7f: 292 case 0x7f:
289 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)");
290 294
291 default: 295 default:
292 error ("ASN.1 length too long (only up to 2**64 octets supported)"); 296 error ("BER value length too long (must fit into UV)");
293 297
298#if UVSIZE > 4
294 case 8: res = (res << 8) | get_u8 (); 299 case 8: res = (res << 8) | get_u8 ();
295 case 7: res = (res << 8) | get_u8 (); 300 case 7: res = (res << 8) | get_u8 ();
296 case 6: res = (res << 8) | get_u8 (); 301 case 6: res = (res << 8) | get_u8 ();
297 case 5: res = (res << 8) | get_u8 (); 302 case 5: res = (res << 8) | get_u8 ();
303#endif
298 case 4: res = (res << 8) | get_u8 (); 304 case 4: res = (res << 8) | get_u8 ();
299 case 3: res = (res << 8) | get_u8 (); 305 case 3: res = (res << 8) | get_u8 ();
300 case 2: res = (res << 8) | get_u8 (); 306 case 2: res = (res << 8) | get_u8 ();
301 case 1: res = (res << 8) | get_u8 (); 307 case 1: res = (res << 8) | get_u8 ();
302 } 308 }
309decode_int (void) 315decode_int (void)
310{ 316{
311 UV len = get_length (); 317 UV len = get_length ();
312 318
313 if (!len) 319 if (!len)
314 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)");
315 321
316 U8 *data = get_n (len); 322 U8 *data = get_n (len);
317 323
318 if (expect_false (len > 1)) 324 if (expect_false (len > 1))
319 { 325 {
320 U16 mask = (data [0] << 8) | data [1] & 0xff80; 326 U16 mask = (data [0] << 8) | data [1] & 0xff80;
321 327
322 if (expect_false (mask == 0xff80 || mask == 0x0000)) 328 if (expect_false (mask == 0xff80 || mask == 0x0000))
323 error ("illegal padding in integer (X.690 8.3.2)"); 329 error ("illegal padding in BER_TYPE_INT (X.690 8.3.2)");
324 } 330 }
325 331
326 int negative = data [0] & 0x80; 332 int negative = data [0] & 0x80;
327 333
328 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");
329 338
330 do 339 do
331 val = (val << 8) | *data++; 340 val = (val << 8) | *data++;
332 while (--len); 341 while (--len);
333 342
381{ 390{
382 UV len = get_length (); 391 UV len = get_length ();
383 392
384 if (len <= 0) 393 if (len <= 0)
385 { 394 {
386 error ("OBJECT IDENTIFIER length equal to zero"); 395 error ("BER_TYPE_OID length must not be zero");
387 return &PL_sv_undef; 396 return &PL_sv_undef;
388 } 397 }
389 398
390 U8 *end = cur + len; 399 U8 *end = cur + len;
391 UV w = get_w (); 400 UV w = get_w ();
392 401
393 static char oid[MAX_OID_STRLEN]; // static, becaueds too large for stack 402 static char oid[MAX_OID_STRLEN]; // static, because too large for stack
394 char *app = oid; 403 char *app = oid;
395 404
396 if (relative) 405 if (relative)
397 app = write_uv (app, w); 406 app = write_uv (app, w);
398 else if (w < 2 * 40) 407 else if (w < 2 * 40)
480 489
481 while (cur < buf + seqend) 490 while (cur < buf + seqend)
482 av_push (av, decode_ber ()); 491 av_push (av, decode_ber ());
483 492
484 if (cur > buf + seqend) 493 if (cur > buf + seqend)
485 croak ("constructed type %02x length overflow (0x%x 0x%x)\n", identifier, (int)(cur - buf), (int)seqend); 494 croak ("CONSTRUCTED type %02x length overflow (0x%x 0x%x)\n", identifier, (int)(cur - buf), (int)seqend);
486 495
487 res = newRV_inc ((SV *)av); 496 res = newRV_inc ((SV *)av);
488 } 497 }
489 else 498 else
490 switch (profile_lookup (cur_profile, klass, tag)) 499 switch (profile_lookup (cur_profile, klass, tag))
555 case BER_TYPE_UCS4: 564 case BER_TYPE_UCS4:
556 res = decode_ucs (4); 565 res = decode_ucs (4);
557 break; 566 break;
558 567
559 case BER_TYPE_REAL: 568 case BER_TYPE_REAL:
569 error ("BER_TYPE_REAL not implemented");
570
560 case BER_TYPE_CROAK: 571 case BER_TYPE_CROAK:
572 croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag);
573
561 default: 574 default:
562 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 575 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
563 } 576 }
564 577
565 AV *av = newAV (); 578 AV *av = newAV ();
916 // and adjust later 929 // and adjust later
917 need (1); 930 need (1);
918 STRLEN mark = len_fixup_mark (); 931 STRLEN mark = len_fixup_mark ();
919 932
920 if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV)) 933 if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV))
921 croak ("BER constructed data must be array-reference"); 934 croak ("BER CONSTRUCTED data must be array-reference");
922 935
923 AV *av = (AV *)SvRV (data); 936 AV *av = (AV *)SvRV (data);
924 int fill = AvFILL (av); 937 int fill = AvFILL (av);
925 938
926 if (expect_false (SvRMAGICAL (av))) 939 if (expect_false (SvRMAGICAL (av)))
927 croak ("BER constructed data must not be tied"); 940 croak ("BER CONSTRUCTED data must not be tied");
928 941
929 int i; 942 int i;
930 for (i = 0; i <= fill; ++i) 943 for (i = 0; i <= fill; ++i)
931 encode_ber (AvARRAY (av)[i]); 944 encode_ber (AvARRAY (av)[i]);
932 945
987 case BER_TYPE_UCS4: 1000 case BER_TYPE_UCS4:
988 encode_ucs (data, 4); 1001 encode_ucs (data, 4);
989 break; 1002 break;
990 1003
991 case BER_TYPE_REAL: 1004 case BER_TYPE_REAL:
1005 croak ("BER_TYPE_REAL not implemented");
1006
992 case BER_TYPE_CROAK: 1007 case BER_TYPE_CROAK:
1008 croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag);
1009
993 default: 1010 default:
994 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 1011 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
995 } 1012 }
996 1013
997} 1014}
1069 const_iv (BER_TYPE_IPADDRESS) 1086 const_iv (BER_TYPE_IPADDRESS)
1070 const_iv (BER_TYPE_CROAK) 1087 const_iv (BER_TYPE_CROAK)
1071 1088
1072 const_iv (SNMP_IPADDRESS) 1089 const_iv (SNMP_IPADDRESS)
1073 const_iv (SNMP_COUNTER32) 1090 const_iv (SNMP_COUNTER32)
1091 const_iv (SNMP_GAUGE32)
1074 const_iv (SNMP_UNSIGNED32) 1092 const_iv (SNMP_UNSIGNED32)
1075 const_iv (SNMP_TIMETICKS) 1093 const_iv (SNMP_TIMETICKS)
1076 const_iv (SNMP_OPAQUE) 1094 const_iv (SNMP_OPAQUE)
1077 const_iv (SNMP_COUNTER64) 1095 const_iv (SNMP_COUNTER64)
1078 }; 1096 };
1079 1097
1080 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--)
1081 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); 1099 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv));
1082} 1100}
1083 1101
1084SV * 1102void
1085ber_decode (SV *ber, SV *profile = &PL_sv_undef) 1103ber_decode (SV *ber, SV *profile = &PL_sv_undef)
1104 ALIAS:
1105 ber_decode_prefix = 1
1086 CODE: 1106 PPCODE:
1087{ 1107{
1088 cur_profile = SvPROFILE (profile); 1108 cur_profile = SvPROFILE (profile);
1089 STRLEN len; 1109 STRLEN len;
1090 buf = (U8 *)SvPVbyte (ber, len); 1110 buf = (U8 *)SvPVbyte (ber, len);
1091 cur = buf; 1111 cur = buf;
1092 end = buf + len; 1112 end = buf + len;
1093 1113
1094 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");
1095} 1123}
1096 OUTPUT: RETVAL
1097 1124
1098void 1125void
1099ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *flags = &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)
1100 PPCODE: 1127 PPCODE:
1101{ 1128{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines