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.4 by root, Fri Apr 19 20:38:38 2019 UTC vs.
Revision 1.9 by root, Sat Apr 20 02:12:27 2019 UTC

10 ASN_INTEGER32 = 0x02, 10 ASN_INTEGER32 = 0x02,
11 ASN_BIT_STRING = 0x03, 11 ASN_BIT_STRING = 0x03,
12 ASN_OCTET_STRING = 0x04, 12 ASN_OCTET_STRING = 0x04,
13 ASN_NULL = 0x05, 13 ASN_NULL = 0x05,
14 ASN_OBJECT_IDENTIFIER = 0x06, 14 ASN_OBJECT_IDENTIFIER = 0x06,
15 ASN_OID = 0x06, //X 15 ASN_OID = 0x06,
16 ASN_OBJECT_DESCRIPTOR = 0x07, //X 16 ASN_OBJECT_DESCRIPTOR = 0x07,
17 ASN_EXTERNAL = 0x08, //X 17 ASN_EXTERNAL = 0x08,
18 ASN_REAL = 0x09, //X 18 ASN_REAL = 0x09,
19 ASN_ENUMERATED = 0x0a, //X 19 ASN_ENUMERATED = 0x0a,
20 ASN_EMBEDDED_PDV = 0x0b, //X 20 ASN_EMBEDDED_PDV = 0x0b,
21 ASN_UTF8_STRING = 0x0c, //X 21 ASN_UTF8_STRING = 0x0c,
22 ASN_RELATIVE_OID = 0x0d, //X 22 ASN_RELATIVE_OID = 0x0d,
23 ASN_SEQUENCE = 0x10, 23 ASN_SEQUENCE = 0x10,
24 ASN_SET = 0x11, //X 24 ASN_SET = 0x11,
25 ASN_NUMERIC_STRING = 0x12, //X 25 ASN_NUMERIC_STRING = 0x12,
26 ASN_PRINTABLE_STRING = 0x13, //X 26 ASN_PRINTABLE_STRING = 0x13,
27 ASN_TELETEX_STRING = 0x14, //X 27 ASN_TELETEX_STRING = 0x14,
28 ASN_T61_STRING = 0x14, //X 28 ASN_T61_STRING = 0x14,
29 ASN_VIDEOTEX_STRING = 0x15, //X 29 ASN_VIDEOTEX_STRING = 0x15,
30 ASN_IA5_STRING = 0x16, //X 30 ASN_IA5_STRING = 0x16,
31 ASN_ASCII_STRING = 0x16, //X 31 ASN_ASCII_STRING = 0x16,
32 ASN_UTC_TIME = 0x17, //X 32 ASN_UTC_TIME = 0x17,
33 ASN_GENERALIZED_TIME = 0x18, //X 33 ASN_GENERALIZED_TIME = 0x18,
34 ASN_GRAPHIC_STRING = 0x19, //X 34 ASN_GRAPHIC_STRING = 0x19,
35 ASN_VISIBLE_STRING = 0x1a, //X 35 ASN_VISIBLE_STRING = 0x1a,
36 ASN_ISO646_STRING = 0x1a, //X 36 ASN_ISO646_STRING = 0x1a,
37 ASN_GENERAL_STRING = 0x1b, //X 37 ASN_GENERAL_STRING = 0x1b,
38 ASN_UNIVERSAL_STRING = 0x1c, //X 38 ASN_UNIVERSAL_STRING = 0x1c,
39 ASN_CHARACTER_STRING = 0x1d, //X 39 ASN_CHARACTER_STRING = 0x1d,
40 ASN_BMPSTRING = 0x1e, //X 40 ASN_BMP_STRING = 0x1e,
41 41
42 ASN_TAG_BER = 0x1f, 42 ASN_TAG_BER = 0x1f,
43 ASN_TAG_MASK = 0x1f, 43 ASN_TAG_MASK = 0x1f,
44 44
45 // primitive/constructed 45 // primitive/constructed
46 ASN_CONSTRUCTED = 0x20, 46 ASN_CONSTRUCTED = 0x20,
47 47
48 // ASN_CLASS 48 // ASN_CLASS
49 ASN_UNIVERSAL = 0x00, 49 ASN_UNIVERSAL = 0x00,
50 ASN_APPLICATION = 0x40, 50 ASN_APPLICATION = 0x01,
51 ASN_CONTEXT = 0x80, 51 ASN_CONTEXT = 0x02,
52 ASN_PRIVATE = 0xc0, 52 ASN_PRIVATE = 0x03,
53 53
54 ASN_CLASS_MASK = 0xc0, 54 ASN_CLASS_MASK = 0xc0,
55 ASN_CLASS_SHIFT = 6, 55 ASN_CLASS_SHIFT = 6,
56 56
57 // ASN_APPLICATION SNMP 57 // ASN_APPLICATION SNMP
62 SNMP_OPAQUE = 0x04, 62 SNMP_OPAQUE = 0x04,
63 SNMP_COUNTER64 = 0x06, 63 SNMP_COUNTER64 = 0x06,
64}; 64};
65 65
66enum { 66enum {
67 BER_TYPE_BYTES,
68 BER_TYPE_UTF8,
69 BER_TYPE_UCS2,
70 BER_TYPE_UCS4,
71 BER_TYPE_INT,
72 BER_TYPE_OID,
73 BER_TYPE_RELOID,
74 BER_TYPE_NULL,
75 BER_TYPE_BOOL,
76 BER_TYPE_REAL,
77 BER_TYPE_IPADDRESS,
78 BER_TYPE_CROAK,
79};
80
81enum {
67 BER_CLASS = 0, 82 BER_CLASS = 0,
68 BER_TAG = 1, 83 BER_TAG = 1,
69 BER_CONSTRUCTED = 2, 84 BER_CONSTRUCTED = 2,
70 BER_DATA = 3, 85 BER_DATA = 3,
71 BER_ARRAYSIZE 86 BER_ARRAYSIZE
72}; 87};
73 88
74#define MAX_OID_STRLEN 4096 89#define MAX_OID_STRLEN 4096
75 90
91typedef void profile_type;
92
93static profile_type *cur_profile, *default_profile;
76static SV *buf_sv; // encoding buffer 94static SV *buf_sv; // encoding buffer
77static U8 *buf, *cur, *end; // buffer start, current, end 95static U8 *buf, *cur, *end; // buffer start, current, end
96
97#if PERL_VERSION < 18
98# define utf8_to_uvchr_buf(s,e,l) utf8_to_uvchr (s, l)
99#endif
78 100
79#if __GNUC__ >= 3 101#if __GNUC__ >= 3
80# define expect(expr,value) __builtin_expect ((expr), (value)) 102# define expect(expr,value) __builtin_expect ((expr), (value))
81# define INLINE static inline 103# define INLINE static inline
82#else 104#else
85#endif 107#endif
86 108
87#define expect_false(expr) expect ((expr) != 0, 0) 109#define expect_false(expr) expect ((expr) != 0, 0)
88#define expect_true(expr) expect ((expr) != 0, 1) 110#define expect_true(expr) expect ((expr) != 0, 1)
89 111
112/////////////////////////////////////////////////////////////////////////////
113
114static SV *sviv_cache[32];
115
90// for "small" integers, return a readonly sv, otherwise create a new one 116// for "small" integers, return a readonly sv, otherwise create a new one
91static SV *newSVcacheint (int val) 117static SV *newSVcacheint (int val)
92{ 118{
93 static SV *cache[32];
94
95 if (expect_false (val < 0 || val >= sizeof (cache))) 119 if (expect_false (val < 0 || val >= sizeof (sviv_cache)))
96 return newSViv (val); 120 return newSViv (val);
97 121
98 if (expect_false (!cache [val])) 122 if (expect_false (!sviv_cache [val]))
99 { 123 {
100 cache [val] = newSVuv (val); 124 sviv_cache [val] = newSVuv (val);
101 SvREADONLY_on (cache [val]); 125 SvREADONLY_on (sviv_cache [val]);
102 } 126 }
103 127
104 return SvREFCNT_inc_NN (cache [val]); 128 return SvREFCNT_inc_NN (sviv_cache [val]);
129}
130
131/////////////////////////////////////////////////////////////////////////////
132
133static HV *profile_stash;
134
135static profile_type *
136SvPROFILE (SV *profile)
137{
138 if (!SvOK (profile))
139 return default_profile;
140
141 if (!SvROK (profile))
142 croak ("invalid profile");
143
144 profile = SvRV (profile);
145
146 if (SvSTASH (profile) != profile_stash)
147 croak ("invalid profile object");
148
149 return (void *)profile;
150}
151
152static int
153profile_lookup (profile_type *profile, int klass, int tag)
154{
155 SV *sv = (SV *)profile;
156 U32 idx = (tag << 2) + klass;
157
158 if (expect_false (idx >= SvCUR (sv)))
159 return BER_TYPE_BYTES;
160
161 return SvPVX (sv)[idx];
162}
163
164static int
165profile_set (profile_type *profile, int klass, int tag, int type)
166{
167 SV *sv = (SV *)profile;
168 U32 idx = (tag << 2) + klass;
169 STRLEN oldlen = SvCUR (sv);
170 STRLEN newlen = idx + 2;
171
172 if (idx >= oldlen)
173 {
174 sv_grow (sv, newlen);
175 memset (SvPVX (sv) + oldlen, BER_TYPE_BYTES, newlen - oldlen);
176 SvCUR_set (sv, newlen);
177 }
178
179 SvPVX (sv)[idx] = type;
180}
181
182static SV *
183profile_new ()
184{
185 SV *sv = newSVpvn ("", 0);
186
187 static const struct {
188 int klass;
189 int tag;
190 int type;
191 } *celem, default_map[] = {
192 { ASN_UNIVERSAL, ASN_BOOLEAN , BER_TYPE_BOOL },
193 { ASN_UNIVERSAL, ASN_INTEGER32 , BER_TYPE_INT },
194 { ASN_UNIVERSAL, ASN_NULL , BER_TYPE_NULL },
195 { ASN_UNIVERSAL, ASN_OBJECT_IDENTIFIER, BER_TYPE_OID },
196 { ASN_UNIVERSAL, ASN_OBJECT_DESCRIPTOR, BER_TYPE_OID },
197 { ASN_UNIVERSAL, ASN_RELATIVE_OID , BER_TYPE_RELOID },
198 { ASN_UNIVERSAL, ASN_REAL , BER_TYPE_REAL },
199 { ASN_UNIVERSAL, ASN_UTF8_STRING , BER_TYPE_UTF8 },
200 { ASN_UNIVERSAL, ASN_BMP_STRING , BER_TYPE_UCS2 },
201 { ASN_UNIVERSAL, ASN_UNIVERSAL_STRING , BER_TYPE_UCS4 },
202 };
203
204 for (celem = default_map + sizeof (default_map) / sizeof (default_map [0]); celem > default_map; celem--)
205 profile_set ((void *)sv, celem->klass, celem->tag, celem->type);
206
207 return sv_bless (newRV_noinc (sv), profile_stash);
105} 208}
106 209
107///////////////////////////////////////////////////////////////////////////// 210/////////////////////////////////////////////////////////////////////////////
108// decoder 211// decoder
109 212
187 } 290 }
188 291
189 return res; 292 return res;
190} 293}
191 294
192static U32 295static SV *
193get_integer32 (void) 296decode_int ()
194{ 297{
195 U32 length = get_length (); 298 int len = get_length ();
196 299
197 if (length <= 0) 300 if (len <= 0)
198 { 301 {
199 error ("INTEGER32 length equal to zero"); 302 error ("integer length equal to zero");
200 return 0; 303 return 0;
201 } 304 }
202 305
203 U8 *data = get_n (length); 306 U8 *data = get_n (len);
204 307
205 if (length > 5 || (length > 4 && data [0])) 308 int negative = data [0] & 0x80;
206 {
207 error ("INTEGER32 length too long");
208 return 0;
209 }
210 309
211 U32 res = data [0] & 0x80 ? 0xffffffff : 0; 310 UV val = negative ? -1 : 0; // copy signbit to all bits
212 311
213 while (length--) 312 do
214 res = (res << 8) | *data++; 313 val = (val << 8) | *data++;
314 while (--len);
215 315
216 return res; 316 // the cast to IV relies on implementation-defined behaviour (two's complement cast)
317 // but that's ok, as perl relies on it as well.
318 return negative ? newSViv ((IV)val) : newSVuv (val);
217} 319}
218 320
219static SV * 321static SV *
220decode_integer32 (void) 322decode_data (void)
221{ 323{
222 return newSViv ((I32)get_integer32 ());
223}
224
225static SV *
226decode_unsigned32 (void)
227{
228 return newSVuv ((U32)get_integer32 ());
229}
230
231#if IVSIZE >= 8
232
233static U64TYPE
234get_integer64 (void)
235{
236 U32 length = get_length (); 324 U32 len = get_length ();
237
238 if (length <= 0)
239 {
240 error ("INTEGER64 length equal to zero");
241 return 0;
242 }
243
244 U8 *data = get_n (length); 325 U8 *data = get_n (len);
245
246 if (length > 9 || (length > 8 && data [0]))
247 {
248 error ("INTEGER64 length too long");
249 return 0;
250 }
251
252 U64TYPE res = data [0] & 0x80 ? 0xffffffffffffffff : 0;
253
254 while (length--)
255 res = (res << 8) | *data++;
256
257 return res;
258}
259
260static SV *
261decode_integer64 (void)
262{
263 return newSViv ((I64TYPE)get_integer64 ());
264}
265
266static SV *
267decode_unsigned64 (void)
268{
269 return newSVuv ((U64TYPE)get_integer64 ());
270}
271
272#endif
273
274static SV *
275decode_octet_string (void)
276{
277 U32 length = get_length ();
278 U8 *data = get_n (length);
279 return newSVpvn (data, length); 326 return newSVpvn ((char *)data, len);
280} 327}
281 328
282// gelper for decode_object_identifier 329// gelper for decode_object_identifier
283static char * 330static char *
284write_uv (char *buf, U32 u) 331write_uv (char *buf, U32 u)
285{ 332{
286 // the one-digit case is absolutely predominant, so this pays off (hopefully) 333 // the one-digit case is absolutely predominant, so this pays off (hopefully)
287 if (u < 10) 334 if (expect_true (u < 10))
288 *buf++ = u + '0'; 335 *buf++ = u + '0';
289 else 336 else
290 { 337 {
291 char *beg = buf; 338 char *beg = buf;
292 339
308 355
309 return buf; 356 return buf;
310} 357}
311 358
312static SV * 359static SV *
313decode_object_identifier (void) 360decode_oid (int relative)
314{ 361{
315 U32 length = get_length (); 362 U32 len = get_length ();
316 363
317 if (length <= 0) 364 if (len <= 0)
318 { 365 {
319 error ("OBJECT IDENTIFIER length equal to zero"); 366 error ("OBJECT IDENTIFIER length equal to zero");
320 return &PL_sv_undef; 367 return &PL_sv_undef;
321 } 368 }
322 369
323 U8 *end = cur + length; 370 U8 *end = cur + len;
324 U32 w = get_w (); 371 U32 w = get_w ();
325 372
326 static char oid[MAX_OID_STRLEN]; // must be static 373 static char oid[MAX_OID_STRLEN]; // must be static
327 char *app = oid; 374 char *app = oid;
328 375
376 if (relative)
377 app = write_uv (app, w);
378 else
379 {
329 app = write_uv (app, (U8)w / 40); 380 app = write_uv (app, (U8)w / 40);
330 *app++ = '.'; 381 *app++ = '.';
331 app = write_uv (app, (U8)w % 40); 382 app = write_uv (app, (U8)w % 40);
383 }
332 384
333 // we assume an oid component is never > 64 bytes 385 // we assume an oid component is never > 64 bytes
334 while (cur < end && oid + sizeof (oid) - app > 64) 386 while (cur < end && oid + sizeof (oid) - app > 64)
335 { 387 {
336 w = get_w (); 388 w = get_w ();
339 } 391 }
340 392
341 return newSVpvn (oid, app - oid); 393 return newSVpvn (oid, app - oid);
342} 394}
343 395
396// TODO: this is unacceptably slow
397static SV *
398decode_ucs (int chrsize)
399{
400 SV *res = NEWSV (0, 0);
401
402 U32 len = get_length ();
403
404 if (len & (chrsize - 1))
405 croak ("BER_TYPE_UCS has an invalid number of octets (%d)", len);
406
407 while (len)
408 {
409 U8 b1 = get_u8 ();
410 U8 b2 = get_u8 ();
411 U32 chr = (b1 << 8) | b2;
412
413 if (chrsize == 4)
414 {
415 U8 b3 = get_u8 ();
416 U8 b4 = get_u8 ();
417 chr = (chr << 16) | (b3 << 8) | b4;
418 }
419
420 U8 uchr [UTF8_MAXBYTES];
421 int uclen = uvuni_to_utf8 (uchr, chr) - uchr;
422
423 sv_catpvn (res, (const char *)uchr, uclen);
424 len -= chrsize;
425 }
426
427 SvUTF8_on (res);
428
429 return res;
430}
431
344static SV * 432static SV *
345decode_ber () 433decode_ber ()
346{ 434{
347 int identifier = get_u8 (); 435 int identifier = get_u8 ();
348 436
349 SV *res; 437 SV *res;
350 438
351 int constructed = identifier & ASN_CONSTRUCTED; 439 int constructed = identifier & ASN_CONSTRUCTED;
352 int klass = identifier & ASN_CLASS_MASK; 440 int klass = (identifier & ASN_CLASS_MASK) >> ASN_CLASS_SHIFT;
353 int tag = identifier & ASN_TAG_MASK; 441 int tag = identifier & ASN_TAG_MASK;
354 442
355 if (tag == ASN_TAG_BER) 443 if (tag == ASN_TAG_BER)
356 tag = get_w (); 444 tag = get_w ();
357 445
358 if (tag == ASN_TAG_BER) 446 if (tag == ASN_TAG_BER)
371 croak ("constructed type %02x overflow (%x %x)\n", identifier, cur - buf, seqend); 459 croak ("constructed type %02x overflow (%x %x)\n", identifier, cur - buf, seqend);
372 460
373 res = newRV_inc ((SV *)av); 461 res = newRV_inc ((SV *)av);
374 } 462 }
375 else 463 else
376 switch (identifier) 464 switch (profile_lookup (cur_profile, klass, tag))
377 { 465 {
378 case ASN_NULL: 466 case BER_TYPE_NULL:
379 res = &PL_sv_undef; 467 res = &PL_sv_undef;
380 break; 468 break;
381 469
382 case ASN_OBJECT_IDENTIFIER: 470 case BER_TYPE_BOOL:
471 {
472 U32 len = get_length ();
473
474 if (len != 1)
475 croak ("BER_TYPE_BOOLEAN type with invalid length %d encountered", len);
476
477 res = newSVcacheint (get_u8 () ? 0 : 1);
478 }
479 break;
480
481 case BER_TYPE_OID:
383 res = decode_object_identifier (); 482 res = decode_oid (0);
384 break; 483 break;
385 484
386 case ASN_INTEGER32: 485 case BER_TYPE_RELOID:
486 res = decode_oid (1);
487 break;
488
489 case BER_TYPE_INT:
387 res = decode_integer32 (); 490 res = decode_int ();
388 break; 491 break;
389 492
390 case ASN_APPLICATION | SNMP_UNSIGNED32: 493 case BER_TYPE_UTF8:
391 case ASN_APPLICATION | SNMP_COUNTER32: 494 res = decode_data ();
392 case ASN_APPLICATION | SNMP_TIMETICKS: 495 SvUTF8_on (res);
496 break;
497
498 case BER_TYPE_BYTES:
499 res = decode_data ();
500 break;
501
502 case BER_TYPE_IPADDRESS:
503 {
504 U32 len = get_length ();
505
506 if (len != 4)
507 croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered", len);
508
509 U8 c1 = get_u8 ();
510 U8 c2 = get_u8 ();
511 U8 c3 = get_u8 ();
512 U8 c4 = get_u8 ();
513
514 res = newSVpvf ("%d.%d.%d.%d", c1, c2, c3, c4);
515 }
516 break;
517
518 case BER_TYPE_UCS2:
519 res = decode_ucs (2);
520 break;
521
522 case BER_TYPE_UCS4:
393 res = decode_unsigned32 (); 523 res = decode_ucs (4);
394 break; 524 break;
395 525
396#if 0 // handled by default case 526 case BER_TYPE_REAL:
397 case ASN_OCTET_STRING: 527 case BER_TYPE_CROAK:
398 case ASN_APPLICATION | ASN_IPADDRESS:
399 case ASN_APPLICATION | ASN_OPAQUE:
400 res = decode_octet_string ();
401 break;
402#endif
403
404 case ASN_APPLICATION | SNMP_COUNTER64:
405 res = decode_integer64 ();
406 break;
407
408 default: 528 default:
409 res = decode_octet_string (); 529 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
410 break;
411 } 530 }
412 531
413 AV *av = newAV (); 532 AV *av = newAV ();
414 av_fill (av, BER_ARRAYSIZE - 1); 533 av_fill (av, BER_ARRAYSIZE - 1);
415 AvARRAY (av)[BER_CLASS ] = newSVcacheint (klass >> ASN_CLASS_SHIFT); 534 AvARRAY (av)[BER_CLASS ] = newSVcacheint (klass);
416 AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); 535 AvARRAY (av)[BER_TAG ] = newSVcacheint (tag);
417 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (constructed ? 1 : 0); 536 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (constructed ? 1 : 0);
418 AvARRAY (av)[BER_DATA ] = res; 537 AvARRAY (av)[BER_DATA ] = res;
419 538
420 return newRV_noinc ((SV *)av); 539 return newRV_noinc ((SV *)av);
516} 635}
517 636
518static void 637static void
519put_length (U32 val) 638put_length (U32 val)
520{ 639{
521 need (5); 640 need (5 + val);
522 cur = put_length_at (val, cur); 641 cur = put_length_at (val, cur);
523} 642}
524 643
525// return how many bytes the encoded length requires 644// return how many bytes the encoded length requires
526static int length_length (U32 val) 645static int length_length (U32 val)
529 ? 1 648 ? 1
530 : 2 + (val > 0xffU) + (val > 0xffffU) + (val > 0xffffffU); 649 : 2 + (val > 0xffU) + (val > 0xffffU) + (val > 0xffffffU);
531} 650}
532 651
533static void 652static void
534encode_octet_string (SV *sv) 653encode_data (const char *ptr, STRLEN len)
535{ 654{
536 STRLEN len;
537 char *ptr = SvPVbyte (sv, len);
538
539 put_length (len); 655 put_length (len);
540 need (len);
541 memcpy (cur, ptr, len); 656 memcpy (cur, ptr, len);
542 cur += len; 657 cur += len;
543} 658}
544 659
545static void 660static void
546encode_integer32 (IV iv) 661encode_uv (UV uv)
547{ 662{
548 need (5); 663}
664
665static void
666encode_int (SV *sv)
667{
668 need (8 + 1 + 1); // 64 bit + length + extra 0
669
670 if (expect_false (!SvIOK (sv)))
671 sv_2iv_flags (sv, 0);
549 672
550 U8 *lenb = cur++; 673 U8 *lenb = cur++;
551 674
552 if (iv < 0) 675 if (SvIOK_notUV (sv))
553 { 676 {
677 IV iv = SvIVX (sv);
678
679 if (expect_false (iv < 0))
680 {
554 // get two's complement bit pattern - works even on hypthetical non-2c machines 681 // get two's complement bit pattern - works even on hypothetical non-2c machines
555 U32 uv = iv; 682 UV uv = iv;
556 683
684#if UVSIZE > 4
685 *cur = uv >> 56; cur += !!(~uv & 0xff80000000000000U);
686 *cur = uv >> 48; cur += !!(~uv & 0xffff800000000000U);
687 *cur = uv >> 40; cur += !!(~uv & 0xffffff8000000000U);
688 *cur = uv >> 32; cur += !!(~uv & 0xffffffff80000000U);
689#endif
557 *cur = uv >> 24; cur += !!(~uv & 0xff800000U); 690 *cur = uv >> 24; cur += !!(~uv & 0xffffffffff800000U);
558 *cur = uv >> 16; cur += !!(~uv & 0xffff8000U); 691 *cur = uv >> 16; cur += !!(~uv & 0xffffffffffff8000U);
559 *cur = uv >> 8; cur += !!(~uv & 0xffffff80U); 692 *cur = uv >> 8; cur += !!(~uv & 0xffffffffffffff80U);
560 *cur = uv ; cur += 1; 693 *cur = uv ; cur += 1;
561 }
562 else
563 {
564 *cur = iv >> 24; cur += *cur > 0;
565 *cur = iv >> 16; cur += *cur > 0;
566 *cur = iv >> 8; cur += *cur > 0;
567 *cur = iv ; cur += 1;
568 }
569 694
570 *lenb = cur - lenb - 1; 695 *lenb = cur - lenb - 1;
571}
572 696
573static void 697 return;
574encode_unsigned64 (U64TYPE uv) 698 }
575{ 699 }
576 need (9);
577 700
578 U8 *lenb = cur++; 701 UV uv = SvUV (sv);
579 702
580 *cur = uv >> 56; cur += *cur > 0; 703 // prepend an extra 0 if the high bit is 1
581 *cur = uv >> 48; cur += *cur > 0; 704 *cur = 0; cur += !!(uv & ((UV)1 << (UVSIZE * 8 - 1)));
582 *cur = uv >> 40; cur += *cur > 0; 705
583 *cur = uv >> 32; cur += *cur > 0; 706#if UVSIZE > 4
584 *cur = uv >> 24; cur += *cur > 0; 707 *cur = uv >> 56; cur += !!(uv & 0xff80000000000000U);
585 *cur = uv >> 16; cur += *cur > 0; 708 *cur = uv >> 48; cur += !!(uv & 0xffff800000000000U);
586 *cur = uv >> 8; cur += *cur > 0; 709 *cur = uv >> 40; cur += !!(uv & 0xffffff8000000000U);
710 *cur = uv >> 32; cur += !!(uv & 0xffffffff80000000U);
711#endif
712 *cur = uv >> 24; cur += !!(uv & 0xffffffffff800000U);
713 *cur = uv >> 16; cur += !!(uv & 0xffffffffffff8000U);
714 *cur = uv >> 8; cur += !!(uv & 0xffffffffffffff80U);
587 *cur = uv ; cur += 1; 715 *cur = uv ; cur += 1;
588 716
589 *lenb = cur - lenb - 1; 717 *lenb = cur - lenb - 1;
590} 718}
591 719
631 759
632 return str; 760 return str;
633} 761}
634 762
635static void 763static void
636encode_object_identifier (SV *oid) 764encode_oid (SV *oid, int relative)
637{ 765{
638 STRLEN slen; 766 STRLEN len;
639 char *ptr = SvPV (oid, slen); // utf8 vs. bytes does not matter 767 char *ptr = SvPV (oid, len); // utf8 vs. bytes does not matter
640 768
641 // we need at most as many octets as the string form 769 // we need at most as many octets as the string form
642 need (slen + 1); 770 need (len + 1);
643 STRLEN mark = len_fixup_mark (); 771 STRLEN mark = len_fixup_mark ();
644 772
645 UV w1, w2; 773 UV w1, w2;
646 774
775 if (!relative)
776 {
647 ptr = read_uv (ptr, &w1); 777 ptr = read_uv (ptr, &w1);
648 ptr = read_uv (ptr, &w2); 778 ptr = read_uv (ptr, &w2);
649 779
650 put_w_nocheck (w1 * 40 + w2); 780 put_w_nocheck (w1 * 40 + w2);
781 }
651 782
652 while (*ptr) 783 while (*ptr)
653 { 784 {
654 ptr = read_uv (ptr, &w1); 785 ptr = read_uv (ptr, &w1);
655 put_w_nocheck (w1); 786 put_w_nocheck (w1);
656 } 787 }
657 788
658 len_fixup (mark); 789 len_fixup (mark);
659} 790}
660 791
661// checkl whether an SV is a BER tuple and returns its AV * 792// check whether an SV is a BER tuple and returns its AV *
662static AV * 793static AV *
663ber_tuple (SV *tuple) 794ber_tuple (SV *tuple)
664{ 795{
665 SV *rv; 796 SV *rv;
666 797
674 croak ("BER tuple must contain exactly %d elements, not %d", BER_ARRAYSIZE, AvFILL ((AV *)rv) + 1); 805 croak ("BER tuple must contain exactly %d elements, not %d", BER_ARRAYSIZE, AvFILL ((AV *)rv) + 1);
675 806
676 return (AV *)rv; 807 return (AV *)rv;
677} 808}
678 809
810static void
811encode_ucs (SV *data, int chrsize)
812{
813 STRLEN uchars = sv_len_utf8 (data);
814 STRLEN len;;
815 char *ptr = SvPVutf8 (data, len);
816
817 put_length (uchars * chrsize);
818
819 while (uchars--)
820 {
821 STRLEN uclen;
822 UV uchr = utf8_to_uvchr_buf (ptr, ptr + len, &uclen);
823
824 ptr += uclen;
825 len -= uclen;
826
827 if (chrsize == 4)
828 {
829 *cur++ = uchr >> 24;
830 *cur++ = uchr >> 16;
831 }
832
833 *cur++ = uchr >> 8;
834 *cur++ = uchr;
835 }
836}
679static void 837static void
680encode_ber (SV *tuple) 838encode_ber (SV *tuple)
681{ 839{
682 AV *av = ber_tuple (tuple); 840 AV *av = ber_tuple (tuple);
683 841
716 encode_ber (AvARRAY (av)[i]); 874 encode_ber (AvARRAY (av)[i]);
717 875
718 len_fixup (mark); 876 len_fixup (mark);
719 } 877 }
720 else 878 else
721 switch (identifier | tag) 879 switch (profile_lookup (cur_profile, klass, tag))
722 { 880 {
723 case ASN_NULL: 881 case BER_TYPE_NULL:
724 put_length (0); 882 put_length (0);
725 break; 883 break;
726 884
727 case ASN_OBJECT_IDENTIFIER: 885 case BER_TYPE_BOOL:
886 put_length (1);
887 *cur++ = SvTRUE (data) ? 0xff : 0x00;
888 break;
889
890 case BER_TYPE_OID:
728 encode_object_identifier (data); 891 encode_oid (data, 0);
729 break; 892 break;
730 893
731 case ASN_INTEGER32: 894 case BER_TYPE_RELOID:
895 encode_oid (data, 1);
896 break;
897
898 case BER_TYPE_INT:
732 encode_integer32 (SvIV (data)); 899 encode_int (data);
733 break; 900 break;
734 901
735 case ASN_APPLICATION | SNMP_UNSIGNED32: 902 case BER_TYPE_BYTES:
736 case ASN_APPLICATION | SNMP_COUNTER32: 903 {
737 case ASN_APPLICATION | SNMP_TIMETICKS: 904 STRLEN len;
738 case ASN_APPLICATION | SNMP_COUNTER64: 905 const char *ptr = SvPVbyte (data, len);
739 encode_unsigned64 (SvUV (data)); 906 encode_data (ptr, len);
907 }
740 break; 908 break;
741 909
910 case BER_TYPE_UTF8:
911 {
912 STRLEN len;
913 const char *ptr = SvPVutf8 (data, len);
914 encode_data (ptr, len);
915 }
916 break;
917
918 case BER_TYPE_IPADDRESS:
919 {
920 U8 ip[4];
921 sscanf (SvPV_nolen (data), "%hhu.%hhu.%hhu.%hhu", ip + 0, ip + 1, ip + 2, ip + 3);
922 encode_data ((const char *)ip, sizeof (ip));
923 }
924 break;
925
926 case BER_TYPE_UCS2:
927 encode_ucs (data, 2);
928 break;
929
930 case BER_TYPE_UCS4:
931 encode_ucs (data, 4);
932 break;
933
934 case BER_TYPE_REAL:
935 case BER_TYPE_CROAK:
742 default: 936 default:
743 encode_octet_string (data); 937 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
744 break;
745 } 938 }
746 939
747} 940}
748 941
749///////////////////////////////////////////////////////////////////////////// 942/////////////////////////////////////////////////////////////////////////////
753PROTOTYPES: ENABLE 946PROTOTYPES: ENABLE
754 947
755BOOT: 948BOOT:
756{ 949{
757 HV *stash = gv_stashpv ("Convert::BER::XS", 1); 950 HV *stash = gv_stashpv ("Convert::BER::XS", 1);
951
952 profile_stash = gv_stashpv ("Convert::BER::XS::Profile", 1);
758 953
759 static const struct { 954 static const struct {
760 const char *name; 955 const char *name;
761 IV iv; 956 IV iv;
762 } *civ, const_iv[] = { 957 } *civ, const_iv[] = {
763 { "ASN_BOOLEAN", ASN_BOOLEAN }, 958#define const_iv(name) { # name, name },
764 { "ASN_INTEGER32", ASN_INTEGER32 }, 959 const_iv (ASN_BOOLEAN)
765 { "ASN_BIT_STRING", ASN_BIT_STRING }, 960 const_iv (ASN_INTEGER32)
766 { "ASN_OCTET_STRING", ASN_OCTET_STRING }, 961 const_iv (ASN_BIT_STRING)
767 { "ASN_NULL", ASN_NULL }, 962 const_iv (ASN_OCTET_STRING)
768 { "ASN_OBJECT_IDENTIFIER", ASN_OBJECT_IDENTIFIER }, 963 const_iv (ASN_NULL)
769 { "ASN_TAG_BER", ASN_TAG_BER }, 964 const_iv (ASN_OBJECT_IDENTIFIER)
770 { "ASN_TAG_MASK", ASN_TAG_MASK }, 965 const_iv (ASN_OBJECT_DESCRIPTOR)
771 { "ASN_CONSTRUCTED", ASN_CONSTRUCTED }, 966 const_iv (ASN_OID)
772 { "ASN_UNIVERSAL", ASN_UNIVERSAL >> ASN_CLASS_SHIFT }, 967 const_iv (ASN_EXTERNAL)
773 { "ASN_APPLICATION", ASN_APPLICATION >> ASN_CLASS_SHIFT }, 968 const_iv (ASN_REAL)
774 { "ASN_CONTEXT", ASN_CONTEXT >> ASN_CLASS_SHIFT }, 969 const_iv (ASN_SEQUENCE)
775 { "ASN_PRIVATE", ASN_PRIVATE >> ASN_CLASS_SHIFT }, 970 const_iv (ASN_ENUMERATED)
776 { "ASN_CLASS_MASK", ASN_CLASS_MASK }, 971 const_iv (ASN_EMBEDDED_PDV)
777 { "ASN_CLASS_SHIFT", ASN_CLASS_SHIFT }, 972 const_iv (ASN_UTF8_STRING)
778 { "ASN_SEQUENCE", ASN_SEQUENCE }, 973 const_iv (ASN_RELATIVE_OID)
779 { "SNMP_IPADDRESS", SNMP_IPADDRESS }, 974 const_iv (ASN_SET)
780 { "SNMP_COUNTER32", SNMP_COUNTER32 }, 975 const_iv (ASN_NUMERIC_STRING)
781 { "SNMP_UNSIGNED32", SNMP_UNSIGNED32 }, 976 const_iv (ASN_PRINTABLE_STRING)
782 { "SNMP_TIMETICKS", SNMP_TIMETICKS }, 977 const_iv (ASN_TELETEX_STRING)
783 { "SNMP_OPAQUE", SNMP_OPAQUE }, 978 const_iv (ASN_T61_STRING)
784 { "SNMP_COUNTER64", SNMP_COUNTER64 }, 979 const_iv (ASN_VIDEOTEX_STRING)
980 const_iv (ASN_IA5_STRING)
981 const_iv (ASN_ASCII_STRING)
982 const_iv (ASN_UTC_TIME)
983 const_iv (ASN_GENERALIZED_TIME)
984 const_iv (ASN_GRAPHIC_STRING)
985 const_iv (ASN_VISIBLE_STRING)
986 const_iv (ASN_ISO646_STRING)
987 const_iv (ASN_GENERAL_STRING)
988 const_iv (ASN_UNIVERSAL_STRING)
989 const_iv (ASN_CHARACTER_STRING)
990 const_iv (ASN_BMP_STRING)
785 991
786 { "BER_CLASS" , BER_CLASS }, 992 const_iv (ASN_UNIVERSAL)
787 { "BER_TAG" , BER_TAG }, 993 const_iv (ASN_APPLICATION)
788 { "BER_CONSTRUCTED", BER_CONSTRUCTED }, 994 const_iv (ASN_CONTEXT)
789 { "BER_DATA" , BER_DATA }, 995 const_iv (ASN_PRIVATE)
996
997 const_iv (BER_CLASS)
998 const_iv (BER_TAG)
999 const_iv (BER_CONSTRUCTED)
1000 const_iv (BER_DATA)
1001
1002 const_iv (BER_TYPE_BYTES)
1003 const_iv (BER_TYPE_UTF8)
1004 const_iv (BER_TYPE_UCS2)
1005 const_iv (BER_TYPE_UCS4)
1006 const_iv (BER_TYPE_INT)
1007 const_iv (BER_TYPE_OID)
1008 const_iv (BER_TYPE_RELOID)
1009 const_iv (BER_TYPE_NULL)
1010 const_iv (BER_TYPE_BOOL)
1011 const_iv (BER_TYPE_REAL)
1012 const_iv (BER_TYPE_IPADDRESS)
1013 const_iv (BER_TYPE_CROAK)
1014
1015 const_iv (SNMP_IPADDRESS)
1016 const_iv (SNMP_COUNTER32)
1017 const_iv (SNMP_UNSIGNED32)
1018 const_iv (SNMP_TIMETICKS)
1019 const_iv (SNMP_OPAQUE)
1020 const_iv (SNMP_COUNTER64)
790 }; 1021 };
791 1022
792 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) 1023 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--)
793 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); 1024 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv));
794} 1025}
795 1026
796SV * 1027SV *
797ber_decode (SV *ber) 1028ber_decode (SV *ber, SV *profile = &PL_sv_undef)
798 CODE: 1029 CODE:
799{ 1030{
1031 cur_profile = SvPROFILE (profile);
800 STRLEN len; 1032 STRLEN len;
801
802 buf = SvPVbyte (ber, len); 1033 buf = SvPVbyte (ber, len);
803 cur = buf; 1034 cur = buf;
804 end = buf + len; 1035 end = buf + len;
805 1036
806 RETVAL = decode_ber (); 1037 RETVAL = decode_ber ();
881} 1112}
882 1113
883############################################################################# 1114#############################################################################
884 1115
885void 1116void
886ber_encode (SV *tuple) 1117ber_encode (SV *tuple, SV *profile = &PL_sv_undef)
887 PPCODE: 1118 PPCODE:
888{ 1119{
1120 cur_profile = SvPROFILE (profile);
889 buf_sv = sv_2mortal (NEWSV (0, 256)); 1121 buf_sv = sv_2mortal (NEWSV (0, 256));
890 SvPOK_only (buf_sv); 1122 SvPOK_only (buf_sv);
891 set_buf (buf_sv); 1123 set_buf (buf_sv);
892 1124
893 encode_ber (tuple); 1125 encode_ber (tuple);
923 AvARRAY (av)[BER_DATA ] = newSVsv (arrayref); 1155 AvARRAY (av)[BER_DATA ] = newSVsv (arrayref);
924 RETVAL = newRV_noinc ((SV *)av); 1156 RETVAL = newRV_noinc ((SV *)av);
925} 1157}
926 OUTPUT: RETVAL 1158 OUTPUT: RETVAL
927 1159
1160MODULE = Convert::BER::XS PACKAGE = Convert::BER::XS::Profile
1161
1162SV *
1163new (SV *klass)
1164 CODE:
1165 RETVAL = profile_new ();
1166 OUTPUT: RETVAL
1167
1168void
1169set (SV *profile, int klass, int tag, int type)
1170 CODE:
1171 profile_set (SvPROFILE (profile), klass, tag, type);
1172
1173IV
1174get (SV *profile, int klass, int tag)
1175 CODE:
1176 RETVAL = profile_lookup (SvPROFILE (profile), klass, tag);
1177 OUTPUT: RETVAL
1178
1179void
1180_set_default (SV *profile)
1181 CODE:
1182 default_profile = SvPROFILE (profile);
1183
1184

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines