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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines