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.2 by root, Fri Apr 19 16:49:02 2019 UTC vs.
Revision 1.5 by root, Fri Apr 19 23:50:53 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
16 ASN_OBJECT_DESCRIPTOR = 0x07, //X
17 ASN_EXTERNAL = 0x08, //X
15 ASN_REAL = 0x09, //X 18 ASN_REAL = 0x09, //X
16 ASN_ENUMERATED = 0x0a, //X 19 ASN_ENUMERATED = 0x0a, //X
20 ASN_EMBEDDED_PDV = 0x0b, //X
21 ASN_UTF8_STRING = 0x0c, //X
22 ASN_RELATIVE_OID = 0x0d, //X
17 ASN_SEQUENCE = 0x10, 23 ASN_SEQUENCE = 0x10,
18 ASN_SET = 0x11, //X 24 ASN_SET = 0x11, //X
25 ASN_NUMERIC_STRING = 0x12, //X
26 ASN_PRINTABLE_STRING = 0x13, //X
27 ASN_TELETEX_STRING = 0x14, //X
28 ASN_T61_STRING = 0x14, //X
29 ASN_VIDEOTEX_STRING = 0x15, //X
30 ASN_IA5_STRING = 0x16, //X
31 ASN_ASCII_STRING = 0x16, //X
19 ASN_UTC_TIME = 0x17, //X 32 ASN_UTC_TIME = 0x17, //X
20 ASN_GENERAL_TIME = 0x18, //X 33 ASN_GENERALIZED_TIME = 0x18, //X
34 ASN_GRAPHIC_STRING = 0x19, //X
35 ASN_VISIBLE_STRING = 0x1a, //X
36 ASN_ISO646_STRING = 0x1a, //X
37 ASN_GENERAL_STRING = 0x1b, //X
38 ASN_UNIVERSAL_STRING = 0x1c, //X
39 ASN_CHARACTER_STRING = 0x1d, //X
40 ASN_BMP_STRING = 0x1e, //X
21 41
22 ASN_TAG_BER = 0x1f, 42 ASN_TAG_BER = 0x1f,
23 ASN_TAG_MASK = 0x1f, 43 ASN_TAG_MASK = 0x1f,
24 44
25 // primitive/constructed 45 // primitive/constructed
26 ASN_CONSTRUCTED = 0x20, 46 ASN_CONSTRUCTED = 0x20,
27 47
28 // ASN_CLASS 48 // ASN_CLASS
29 ASN_UNIVERSAL = 0x00, 49 ASN_UNIVERSAL = 0x00,
30 ASN_APPLICATION = 0x40, 50 ASN_APPLICATION = 0x01,
31 ASN_CONTEXT = 0x80, 51 ASN_CONTEXT = 0x02,
32 ASN_PRIVATE = 0xc0, 52 ASN_PRIVATE = 0x03,
33 53
34 ASN_CLASS_MASK = 0xc0, 54 ASN_CLASS_MASK = 0xc0,
35 ASN_CLASS_SHIFT = 6, 55 ASN_CLASS_SHIFT = 6,
36 56
37 // ASN_APPLICATION SNMP 57 // ASN_APPLICATION SNMP
38 ASN_IPADDRESS = 0x00, 58 SNMP_IPADDRESS = 0x00,
39 ASN_COUNTER32 = 0x01, 59 SNMP_COUNTER32 = 0x01,
40 ASN_UNSIGNED32 = 0x02, 60 SNMP_UNSIGNED32 = 0x02,
41 ASN_TIMETICKS = 0x03, 61 SNMP_TIMETICKS = 0x03,
42 ASN_OPAQUE = 0x04, 62 SNMP_OPAQUE = 0x04,
43 ASN_COUNTER64 = 0x06, 63 SNMP_COUNTER64 = 0x06,
64};
65
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_CROAK,
44}; 78};
45 79
46enum { 80enum {
47 BER_CLASS = 0, 81 BER_CLASS = 0,
48 BER_TAG = 1, 82 BER_TAG = 1,
51 BER_ARRAYSIZE 85 BER_ARRAYSIZE
52}; 86};
53 87
54#define MAX_OID_STRLEN 4096 88#define MAX_OID_STRLEN 4096
55 89
56static U8 *buf, *cur; 90typedef void profile_type;
57static STRLEN len, rem; 91
92static profile_type *cur_profile, *default_profile;
93static SV *buf_sv; // encoding buffer
94static U8 *buf, *cur, *end; // buffer start, current, end
95
96#if __GNUC__ >= 3
97# define expect(expr,value) __builtin_expect ((expr), (value))
98# define INLINE static inline
99#else
100# define expect(expr,value) (expr)
101# define INLINE static
102#endif
103
104#define expect_false(expr) expect ((expr) != 0, 0)
105#define expect_true(expr) expect ((expr) != 0, 1)
106
107/////////////////////////////////////////////////////////////////////////////
108
109static SV *sviv_cache[32];
58 110
59// for "small" integers, return a readonly sv, otherwise create a new one 111// for "small" integers, return a readonly sv, otherwise create a new one
60static SV *newSVcacheint (int val) 112static SV *newSVcacheint (int val)
61{ 113{
62 static SV *cache[32];
63
64 if (val < 0 || val >= sizeof (cache)) 114 if (expect_false (val < 0 || val >= sizeof (sviv_cache)))
65 return newSViv (val); 115 return newSViv (val);
66 116
67 if (!cache [val]) 117 if (expect_false (!sviv_cache [val]))
68 { 118 {
69 cache [val] = newSVuv (val); 119 sviv_cache [val] = newSVuv (val);
70 SvREADONLY_on (cache [val]); 120 SvREADONLY_on (sviv_cache [val]);
71 } 121 }
72 122
73 return SvREFCNT_inc_NN (cache [val]); 123 return SvREFCNT_inc_NN (sviv_cache [val]);
74} 124}
75 125
76///////////////////////////////////////////////////////////////////////////// 126/////////////////////////////////////////////////////////////////////////////
77 127
128static HV *profile_stash;
129
130static profile_type *
131SvPROFILE (SV *profile)
132{
133 if (!SvOK (profile))
134 profile = default_profile;
135
136 if (!SvROK (profile))
137 croak ("invalid profile");
138
139 profile = SvRV (profile);
140
141 if (SvSTASH (profile) != profile_stash)
142 croak ("invalid profile object");
143
144 return (void *)profile;
145}
146
147static int
148profile_lookup (profile_type *profile, int klass, int tag)
149{
150 SV *sv = (SV *)profile;
151 U32 idx = (tag << 2) + klass;
152
153 if (expect_false (idx >= SvCUR (sv)))
154 return BER_TYPE_BYTES;
155
156 return SvPVX (sv)[idx];
157}
158
159static int
160profile_set (profile_type *profile, int klass, int tag, int type)
161{
162 SV *sv = (SV *)profile;
163 U32 idx = (tag << 2) + klass;
164 STRLEN oldlen = SvCUR (sv);
165 STRLEN newlen = idx + 2;
166
167 if (idx >= oldlen)
168 {
169 sv_grow (sv, newlen);
170 memset (SvPVX (sv) + oldlen, BER_TYPE_BYTES, newlen - oldlen);
171 SvCUR_set (sv, newlen);
172 }
173
174 SvPVX (sv)[idx] = type;
175}
176
177static SV *
178profile_new ()
179{
180 SV *sv = newSVpvn ("", 0);
181
182 static const struct {
183 int klass;
184 int tag;
185 int type;
186 } *celem, default_map[] = {
187 { ASN_UNIVERSAL, ASN_BOOLEAN , BER_TYPE_BOOL },
188 { ASN_UNIVERSAL, ASN_INTEGER32 , BER_TYPE_INT },
189 { ASN_UNIVERSAL, ASN_NULL , BER_TYPE_NULL },
190 { ASN_UNIVERSAL, ASN_OBJECT_IDENTIFIER, BER_TYPE_OID },
191 { ASN_UNIVERSAL, ASN_OBJECT_DESCRIPTOR, BER_TYPE_OID },
192 { ASN_UNIVERSAL, ASN_RELATIVE_OID , BER_TYPE_RELOID },
193 { ASN_UNIVERSAL, ASN_REAL , BER_TYPE_REAL },
194 { ASN_UNIVERSAL, ASN_UTF8_STRING , BER_TYPE_UTF8 },
195 { ASN_UNIVERSAL, ASN_BMP_STRING , BER_TYPE_UCS2 },
196 { ASN_UNIVERSAL, ASN_UNIVERSAL_STRING , BER_TYPE_UCS4 },
197 };
198
199 //const_iv (SNMP_IPADDRESS)
200 //const_iv (SNMP_COUNTER32)
201 //const_iv (SNMP_UNSIGNED32)
202 //const_iv (SNMP_TIMETICKS)
203 //const_iv (SNMP_OPAQUE)
204 //const_iv (SNMP_COUNTER64)
205
206 for (celem = default_map + sizeof (default_map) / sizeof (default_map [0]); celem > default_map; celem--)
207 profile_set ((void *)sv, celem->klass, celem->tag, celem->type);
208
209 return sv_bless (newRV_noinc (sv), profile_stash);
210}
211
212/////////////////////////////////////////////////////////////////////////////
213// decoder
214
78static void 215static void
79error (const char *errmsg) 216error (const char *errmsg)
80{ 217{
81 croak ("%s at offset 0x%04x", errmsg, cur - buf); 218 croak ("%s at offset 0x%04x", errmsg, cur - buf);
82} 219}
83 220
84static int 221static void
85need (int count) 222want (UV count)
86{ 223{
87 if (count < 0 || (int)rem < count) 224 if (expect_false ((uintptr_t)(end - cur) < count))
88 {
89 error ("unexpected end of message buffer"); 225 error ("unexpected end of message buffer");
90 return 0;
91 }
92
93 return 1;
94} 226}
95 227
96// get_* functions fetch something from the buffer 228// get_* functions fetch something from the buffer
97// decode_* functions use get_* fun ctions to decode ber values 229// decode_* functions use get_* fun ctions to decode ber values
98 230
231// get n octets
99static U8 * 232static U8 *
100get_n (int count, const U8 *errres) 233get_n (UV count)
101{ 234{
102 if (!need (count)) 235 want (count);
103 return (U8 *)errres;
104
105 U8 *res = cur; 236 U8 *res = cur;
106
107 cur += count; 237 cur += count;
108 rem -= count;
109
110 return res; 238 return res;
111} 239}
112 240
241// get single octet
113static U8 242static U8
114get_u8 (void) 243get_u8 (void)
115{ 244{
116 if (rem <= 0) 245 if (cur == end)
117 {
118 error ("unexpected end of message buffer"); 246 error ("unexpected end of message buffer");
119 return 0;
120 }
121 247
122 rem--;
123 return *cur++; 248 return *cur++;
124} 249}
125 250
251// get ber-encoded integer (i.e. pack "w")
126static U32 252static U32
127get_ber (void) 253get_w (void)
128{ 254{
129 U32 res = 0; 255 U32 res = 0;
130 256
131 for (;;) 257 for (;;)
132 { 258 {
166 } 292 }
167 293
168 return res; 294 return res;
169} 295}
170 296
171static U32 297static SV *
172get_integer32 (void) 298decode_int ()
173{ 299{
174 U32 length = get_length (); 300 int len = get_length ();
175 301
176 if (length <= 0) 302 if (len <= 0)
177 { 303 {
178 error ("INTEGER32 length equal to zero"); 304 error ("integer length equal to zero");
179 return 0; 305 return 0;
180 } 306 }
181 307
182 U8 *data = get_n (length, 0); 308 U8 *data = get_n (len);
183 309
184 if (!data) 310 int negative = data [0] & 0x80;
185 return 0;
186 311
187 if (length > 5 || (length > 4 && data [0])) 312 UV val = negative ? -1 : 0; // copy signbit to all bits
188 {
189 error ("INTEGER32 length too long");
190 return 0;
191 }
192 313
193 U32 res = data [0] & 0x80 ? 0xffffffff : 0; 314 do
194
195 while (length--)
196 res = (res << 8) | *data++; 315 val = (val << 8) | *data++;
316 while (--len);
197 317
198 return res; 318 // the cats to IV relies on implementation-defined behaviour (two's complement cast)
319 // but that's ok, as perl relies on it as well.
320 return negative ? newSViv ((IV)val) : newSVuv (val);
199} 321}
200 322
201static SV * 323static SV *
202decode_integer32 (void) 324decode_data (void)
203{ 325{
204 return newSViv ((I32)get_integer32 ());
205}
206
207static SV *
208decode_unsigned32 (void)
209{
210 return newSVuv ((U32)get_integer32 ());
211}
212
213#if IVSIZE >= 8
214
215static U64TYPE
216get_integer64 (void)
217{
218 U32 length = get_length (); 326 U32 len = get_length ();
219
220 if (length <= 0)
221 {
222 error ("INTEGER64 length equal to zero");
223 return 0;
224 }
225
226 U8 *data = get_n (length, 0); 327 U8 *data = get_n (len);
227
228 if (!data)
229 return 0;
230
231 if (length > 9 || (length > 8 && data [0]))
232 {
233 error ("INTEGER64 length too long");
234 return 0;
235 }
236
237 U64TYPE res = data [0] & 0x80 ? 0xffffffffffffffff : 0;
238
239 while (length--)
240 res = (res << 8) | *data++;
241
242 return res;
243}
244
245static SV *
246decode_integer64 (void)
247{
248 return newSViv ((I64TYPE)get_integer64 ());
249}
250
251static SV *
252decode_unsigned64 (void)
253{
254 return newSVuv ((U64TYPE)get_integer64 ());
255}
256
257#endif
258
259static SV *
260decode_octet_string (void)
261{
262 U32 length = get_length ();
263
264 U8 *data = get_n (length, 0);
265 if (!data)
266 {
267 error ("OCTET STRING too long");
268 return &PL_sv_undef;
269 }
270
271 return newSVpvn (data, length); 328 return newSVpvn ((char *)data, len);
272} 329}
273 330
274// gelper for decode_object_identifier 331// gelper for decode_object_identifier
275static char * 332static char *
276write_uv (char *buf, U32 u) 333write_uv (char *buf, U32 u)
277{ 334{
278 // the one-digit case is absolutely predominant, so this pays off (hopefully) 335 // the one-digit case is absolutely predominant, so this pays off (hopefully)
279 if (u < 10) 336 if (expect_true (u < 10))
280 *buf++ = u + '0'; 337 *buf++ = u + '0';
281 else 338 else
282 { 339 {
283 char *beg = buf; 340 char *beg = buf;
284 341
300 357
301 return buf; 358 return buf;
302} 359}
303 360
304static SV * 361static SV *
305decode_object_identifier (void) 362decode_oid (int relative)
306{ 363{
307 U32 length = get_length (); 364 U32 len = get_length ();
308 365
309 if (length <= 0) 366 if (len <= 0)
310 { 367 {
311 error ("OBJECT IDENTIFIER length equal to zero"); 368 error ("OBJECT IDENTIFIER length equal to zero");
312 return &PL_sv_undef; 369 return &PL_sv_undef;
313 } 370 }
314 371
315 U8 *end = cur + length; 372 U8 *end = cur + len;
316 U32 w = get_ber (); 373 U32 w = get_w ();
317 374
318 static char oid[MAX_OID_STRLEN]; // must be static 375 static char oid[MAX_OID_STRLEN]; // must be static
319 char *app = oid; 376 char *app = oid;
320 377
378 if (relative)
379 app = write_uv (app, w);
380 else
381 {
321 app = write_uv (app, (U8)w / 40); 382 app = write_uv (app, (U8)w / 40);
322 *app++ = '.'; 383 *app++ = '.';
323 app = write_uv (app, (U8)w % 40); 384 app = write_uv (app, (U8)w % 40);
385 }
324 386
325 // we assume an oid component is never > 64 bytes 387 // we assume an oid component is never > 64 bytes
326 while (cur < end && oid + sizeof (oid) - app > 64) 388 while (cur < end && oid + sizeof (oid) - app > 64)
327 { 389 {
328 w = get_ber (); 390 w = get_w ();
329 *app++ = '.'; 391 *app++ = '.';
330 app = write_uv (app, w); 392 app = write_uv (app, w);
331 } 393 }
332 394
333 return newSVpvn (oid, app - oid); 395 return newSVpvn (oid, app - oid);
338{ 400{
339 int identifier = get_u8 (); 401 int identifier = get_u8 ();
340 402
341 SV *res; 403 SV *res;
342 404
343 int constructed = identifier & ASN_CONSTRUCTED; 405 int constructed = identifier & ASN_CONSTRUCTED;
344 int klass = identifier & ASN_CLASS_MASK; 406 int klass = (identifier & ASN_CLASS_MASK) >> ASN_CLASS_SHIFT;
345 int tag = identifier & ASN_TAG_MASK; 407 int tag = identifier & ASN_TAG_MASK;
346 408
347 if (tag == ASN_TAG_BER) 409 if (tag == ASN_TAG_BER)
348 tag = get_ber (); 410 tag = get_w ();
349 411
350 if (tag == ASN_TAG_BER) 412 if (tag == ASN_TAG_BER)
351 tag = get_ber (); 413 tag = get_w ();
352 414
353 if (constructed) 415 if (constructed)
354 { 416 {
355 U32 len = get_length (); 417 U32 len = get_length ();
356 U32 seqend = (cur - buf) + len; 418 U32 seqend = (cur - buf) + len;
363 croak ("constructed type %02x overflow (%x %x)\n", identifier, cur - buf, seqend); 425 croak ("constructed type %02x overflow (%x %x)\n", identifier, cur - buf, seqend);
364 426
365 res = newRV_inc ((SV *)av); 427 res = newRV_inc ((SV *)av);
366 } 428 }
367 else 429 else
368 switch (identifier) 430 switch (profile_lookup (cur_profile, klass, tag))
369 { 431 {
370 case ASN_NULL: 432 case BER_TYPE_NULL:
371 res = &PL_sv_undef; 433 res = &PL_sv_undef;
372 break; 434 break;
373 435
374 case ASN_OBJECT_IDENTIFIER: 436 case BER_TYPE_BOOL:
437 {
438 U32 len = get_length ();
439
440 if (len != 1)
441 croak ("BOOLEAN type with invalid length %d encountered", len);
442
443 res = newSVcacheint (get_u8 () ? 0 : 1);
444 }
445 break;
446
447 case BER_TYPE_OID:
375 res = decode_object_identifier (); 448 res = decode_oid (0);
376 break; 449 break;
377 450
378 case ASN_INTEGER32: 451 case BER_TYPE_RELOID:
452 res = decode_oid (1);
453 break;
454
455 case BER_TYPE_INT:
379 res = decode_integer32 (); 456 res = decode_int ();
380 break; 457 break;
381 458
382 case ASN_APPLICATION | ASN_UNSIGNED32: 459 case BER_TYPE_UTF8:
383 case ASN_APPLICATION | ASN_COUNTER32:
384 case ASN_APPLICATION | ASN_TIMETICKS:
385 res = decode_unsigned32 ();
386 break;
387
388#if 0 // handled by default case
389 case ASN_OCTET_STRING:
390 case ASN_APPLICATION | ASN_IPADDRESS:
391 case ASN_APPLICATION | ASN_OPAQUE:
392 res = decode_octet_string ();
393 break;
394#endif
395
396 case ASN_APPLICATION | ASN_COUNTER64:
397 res = decode_integer64 (); 460 res = decode_data ();
461 SvUTF8_on (res);
398 break; 462 break;
399 463
464 case BER_TYPE_BYTES:
465 res = decode_data ();
466 break;
467
468 case BER_TYPE_REAL:
469 case BER_TYPE_UCS2:
470 case BER_TYPE_UCS4:
471 case BER_TYPE_CROAK:
400 default: 472 default:
401 res = decode_octet_string (); 473 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
402 break;
403 } 474 }
404 475
405 AV *av = newAV (); 476 AV *av = newAV ();
406 av_fill (av, BER_ARRAYSIZE - 1); 477 av_fill (av, BER_ARRAYSIZE - 1);
407 AvARRAY (av)[BER_CLASS ] = newSVcacheint (klass >> ASN_CLASS_SHIFT); 478 AvARRAY (av)[BER_CLASS ] = newSVcacheint (klass);
408 AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); 479 AvARRAY (av)[BER_TAG ] = newSVcacheint (tag);
409 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (constructed ? 1 : 0); 480 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (constructed ? 1 : 0);
410 AvARRAY (av)[BER_DATA ] = res; 481 AvARRAY (av)[BER_DATA ] = res;
411 482
412 return newRV_noinc ((SV *)av); 483 return newRV_noinc ((SV *)av);
413} 484}
414 485
486/////////////////////////////////////////////////////////////////////////////
487// encoder
488
489/* adds two STRLENs together, slow, and with paranoia */
490static STRLEN
491strlen_sum (STRLEN l1, STRLEN l2)
492{
493 size_t sum = l1 + l2;
494
495 if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum)
496 croak ("JSON::XS: string size overflow");
497
498 return sum;
499}
500
501static void
502set_buf (SV *sv)
503{
504 STRLEN len;
505 buf_sv = sv;
506 buf = SvPVbyte (buf_sv, len);
507 cur = buf;
508 end = buf + len;
509}
510
511/* similar to SvGROW, but somewhat safer and guarantees exponential realloc strategy */
512static char *
513my_sv_grow (SV *sv, size_t len1, size_t len2)
514{
515 len1 = strlen_sum (len1, len2);
516 len1 = strlen_sum (len1, len1 >> 1);
517
518 if (len1 > 4096 - 24)
519 len1 = (len1 | 4095) - 24;
520
521 return SvGROW (sv, len1);
522}
523
524static void
525need (STRLEN len)
526{
527 if (expect_false ((uintptr_t)(end - cur) < len))
528 {
529 STRLEN pos = cur - buf;
530 buf = my_sv_grow (buf_sv, pos, len);
531 cur = buf + pos;
532 end = buf + SvLEN (buf_sv) - 1;
533 }
534}
535
536static void
537put_u8 (int val)
538{
539 need (1);
540 *cur++ = val;
541}
542
543static void
544put_w_nocheck (U32 val)
545{
546 *cur = (val >> 7 * 4) | 0x80; cur += val >= (1 << (7 * 4));
547 *cur = (val >> 7 * 3) | 0x80; cur += val >= (1 << (7 * 3));
548 *cur = (val >> 7 * 2) | 0x80; cur += val >= (1 << (7 * 2));
549 *cur = (val >> 7 * 1) | 0x80; cur += val >= (1 << (7 * 1));
550 *cur = val & 0x7f; cur += 1;
551}
552
553static void
554put_w (U32 val)
555{
556 need (5); // we only handle up to 5 bytes
557
558 put_w_nocheck (val);
559}
560
561static U8 *
562put_length_at (U32 val, U8 *cur)
563{
564 if (val < 0x7fU)
565 *cur++ = val;
566 else
567 {
568 U8 *lenb = cur++;
569
570 *cur = val >> 24; cur += *cur > 0;
571 *cur = val >> 16; cur += *cur > 0;
572 *cur = val >> 8; cur += *cur > 0;
573 *cur = val ; cur += 1;
574
575 *lenb = 0x80 + cur - lenb - 1;
576 }
577
578 return cur;
579}
580
581static void
582put_length (U32 val)
583{
584 need (5);
585 cur = put_length_at (val, cur);
586}
587
588// return how many bytes the encoded length requires
589static int length_length (U32 val)
590{
591 return val < 0x7fU
592 ? 1
593 : 2 + (val > 0xffU) + (val > 0xffffU) + (val > 0xffffffU);
594}
595
596static void
597encode_data (const char *ptr, STRLEN len)
598{
599 put_length (len);
600 need (len);
601 memcpy (cur, ptr, len);
602 cur += len;
603}
604
605static void
606encode_uv (UV uv)
607{
608}
609
610static void
611encode_int (SV *sv)
612{
613 need (8 + 1 + 1); // 64 bit + length + extra 0
614
615 if (expect_false (!SvIOK (sv)))
616 sv_2iv_flags (sv, 0);
617
618 U8 *lenb = cur++;
619
620 if (SvIOK_notUV (sv))
621 {
622 IV iv = SvIVX (sv);
623
624 if (expect_false (iv < 0))
625 {
626 // get two's complement bit pattern - works even on hypothetical non-2c machines
627 UV uv = iv;
628
629#if UVSIZE > 4
630 *cur = uv >> 56; cur += !!(~uv & 0xff80000000000000U);
631 *cur = uv >> 48; cur += !!(~uv & 0xffff800000000000U);
632 *cur = uv >> 40; cur += !!(~uv & 0xffffff8000000000U);
633 *cur = uv >> 32; cur += !!(~uv & 0xffffffff80000000U);
634#endif
635 *cur = uv >> 24; cur += !!(~uv & 0xffffffffff800000U);
636 *cur = uv >> 16; cur += !!(~uv & 0xffffffffffff8000U);
637 *cur = uv >> 8; cur += !!(~uv & 0xffffffffffffff80U);
638 *cur = uv ; cur += 1;
639
640 *lenb = cur - lenb - 1;
641
642 return;
643 }
644 }
645
646 UV uv = SvUV (sv);
647
648 // prepend an extra 0 if the high bit is 1
649 *cur = 0; cur += !!(uv & ((UV)1 << (UVSIZE * 8 - 1)));
650
651#if UVSIZE > 4
652 *cur = uv >> 56; cur += !!(uv & 0xff80000000000000U);
653 *cur = uv >> 48; cur += !!(uv & 0xffff800000000000U);
654 *cur = uv >> 40; cur += !!(uv & 0xffffff8000000000U);
655 *cur = uv >> 32; cur += !!(uv & 0xffffffff80000000U);
656#endif
657 *cur = uv >> 24; cur += !!(uv & 0xffffffffff800000U);
658 *cur = uv >> 16; cur += !!(uv & 0xffffffffffff8000U);
659 *cur = uv >> 8; cur += !!(uv & 0xffffffffffffff80U);
660 *cur = uv ; cur += 1;
661
662 *lenb = cur - lenb - 1;
663}
664
665// we don't know the length yet, so we optimistically
666// assume the length will need one octet later. if that
667// turns out to be wrong, we memove as needed.
668// mark the beginning
669static STRLEN
670len_fixup_mark ()
671{
672 return cur++ - buf;
673}
674
675// patch up the length
676static void
677len_fixup (STRLEN mark)
678{
679 STRLEN reallen = (cur - buf) - mark - 1;
680 int lenlen = length_length (reallen);
681
682 if (expect_false (lenlen > 1))
683 {
684 // bad luck, we have to shift the bytes to make room for the length
685 need (5);
686 memmove (buf + mark + lenlen, buf + mark + 1, reallen);
687 cur += lenlen - 1;
688 }
689
690 put_length_at (reallen, buf + mark);
691}
692
693static char *
694read_uv (char *str, UV *uv)
695{
696 UV r = 0;
697
698 while (*str >= '0')
699 r = r * 10 + *str++ - '0';
700
701 *uv = r;
702
703 str += !!*str; // advance over any non-zero byte
704
705 return str;
706}
707
708static void
709encode_oid (SV *oid, int relative)
710{
711 STRLEN len;
712 char *ptr = SvPV (oid, len); // utf8 vs. bytes does not matter
713
714 // we need at most as many octets as the string form
715 need (len + 1);
716 STRLEN mark = len_fixup_mark ();
717
718 UV w1, w2;
719
720 if (!relative)
721 {
722 ptr = read_uv (ptr, &w1);
723 ptr = read_uv (ptr, &w2);
724
725 put_w_nocheck (w1 * 40 + w2);
726 }
727
728 while (*ptr)
729 {
730 ptr = read_uv (ptr, &w1);
731 put_w_nocheck (w1);
732 }
733
734 len_fixup (mark);
735}
736
737// check whether an SV is a BER tuple and returns its AV *
738static AV *
739ber_tuple (SV *tuple)
740{
741 SV *rv;
742
743 if (expect_false (!SvROK (tuple) || SvTYPE ((rv = SvRV (tuple))) != SVt_PVAV))
744 croak ("BER tuple must be array-reference");
745
746 if (expect_false (SvRMAGICAL (rv)))
747 croak ("BER tuple must not be tied");
748
749 if (expect_false (AvFILL ((AV *)rv) != BER_ARRAYSIZE - 1))
750 croak ("BER tuple must contain exactly %d elements, not %d", BER_ARRAYSIZE, AvFILL ((AV *)rv) + 1);
751
752 return (AV *)rv;
753}
754
755static void
756encode_ber (SV *tuple)
757{
758 AV *av = ber_tuple (tuple);
759
760 int klass = SvIV (AvARRAY (av)[BER_CLASS]);
761 int tag = SvIV (AvARRAY (av)[BER_TAG]);
762 int constructed = SvIV (AvARRAY (av)[BER_CONSTRUCTED]) ? ASN_CONSTRUCTED : 0;
763 SV *data = AvARRAY (av)[BER_DATA];
764
765 int identifier = (klass << ASN_CLASS_SHIFT) | constructed;
766
767 if (expect_false (tag >= ASN_TAG_BER))
768 {
769 put_u8 (identifier | ASN_TAG_BER);
770 put_w (tag);
771 }
772 else
773 put_u8 (identifier | tag);
774
775 if (constructed)
776 {
777 // we optimistically assume that only one length byte is needed
778 // and adjust later
779 need (1);
780 STRLEN mark = len_fixup_mark ();
781
782 if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV))
783 croak ("BER constructed data must be array-reference");
784
785 AV *av = (AV *)SvRV (data);
786 int fill = AvFILL (av);
787
788 if (expect_false (SvRMAGICAL (av)))
789 croak ("BER constructed data must not be tied");
790
791 for (int i = 0; i <= fill; ++i)
792 encode_ber (AvARRAY (av)[i]);
793
794 len_fixup (mark);
795 }
796 else
797 switch (profile_lookup (cur_profile, klass, tag))
798 {
799 case BER_TYPE_NULL:
800 put_length (0);
801 break;
802
803 case BER_TYPE_BOOL:
804 put_length (1);
805 put_u8 (SvTRUE (data) ? 0xff : 0x00);
806 break;
807
808 case BER_TYPE_OID:
809 encode_oid (data, 0);
810 break;
811
812 case BER_TYPE_RELOID:
813 encode_oid (data, 1);
814 break;
815
816 case BER_TYPE_INT:
817 encode_int (data);
818 break;
819
820 case BER_TYPE_BYTES:
821 {
822 STRLEN len;
823 const char *ptr = SvPVbyte (data, len);
824 encode_data (ptr, len);
825 }
826 break;
827
828 case BER_TYPE_UTF8:
829 {
830 STRLEN len;
831 const char *ptr = SvPVutf8 (data, len);
832 encode_data (ptr, len);
833 }
834 break;
835
836 case BER_TYPE_REAL:
837 case BER_TYPE_UCS2:
838 case BER_TYPE_UCS4:
839 case BER_TYPE_CROAK:
840 default:
841 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
842 }
843
844}
845
846/////////////////////////////////////////////////////////////////////////////
847
415MODULE = Convert::BER::XS PACKAGE = Convert::BER::XS 848MODULE = Convert::BER::XS PACKAGE = Convert::BER::XS
416 849
417PROTOTYPES: ENABLE 850PROTOTYPES: ENABLE
418 851
419BOOT: 852BOOT:
420{ 853{
421 HV *stash = gv_stashpv ("Convert::BER::XS", 1); 854 HV *stash = gv_stashpv ("Convert::BER::XS", 1);
855
856 profile_stash = gv_stashpv ("Convert::BER::XS::Profile", 1);
422 857
423 static const struct { 858 static const struct {
424 const char *name; 859 const char *name;
425 IV iv; 860 IV iv;
426 } *civ, const_iv[] = { 861 } *civ, const_iv[] = {
427 { "ASN_BOOLEAN", ASN_BOOLEAN }, 862#define const_iv(name) { # name, name },
428 { "ASN_INTEGER32", ASN_INTEGER32 }, 863 const_iv (ASN_BOOLEAN)
429 { "ASN_BIT_STRING", ASN_BIT_STRING }, 864 const_iv (ASN_INTEGER32)
430 { "ASN_OCTET_STRING", ASN_OCTET_STRING }, 865 const_iv (ASN_BIT_STRING)
431 { "ASN_NULL", ASN_NULL }, 866 const_iv (ASN_OCTET_STRING)
432 { "ASN_OBJECT_IDENTIFIER", ASN_OBJECT_IDENTIFIER }, 867 const_iv (ASN_NULL)
433 { "ASN_TAG_BER", ASN_TAG_BER }, 868 const_iv (ASN_OBJECT_IDENTIFIER)
434 { "ASN_TAG_MASK", ASN_TAG_MASK }, 869 const_iv (ASN_OBJECT_DESCRIPTOR)
435 { "ASN_CONSTRUCTED", ASN_CONSTRUCTED }, 870 const_iv (ASN_OID)
436 { "ASN_UNIVERSAL", ASN_UNIVERSAL >> ASN_CLASS_SHIFT }, 871 const_iv (ASN_EXTERNAL)
437 { "ASN_APPLICATION", ASN_APPLICATION >> ASN_CLASS_SHIFT }, 872 const_iv (ASN_REAL)
438 { "ASN_CONTEXT", ASN_CONTEXT >> ASN_CLASS_SHIFT }, 873 const_iv (ASN_SEQUENCE)
439 { "ASN_PRIVATE", ASN_PRIVATE >> ASN_CLASS_SHIFT }, 874 const_iv (ASN_ENUMERATED)
440 { "ASN_CLASS_MASK", ASN_CLASS_MASK }, 875 const_iv (ASN_EMBEDDED_PDV)
441 { "ASN_CLASS_SHIFT", ASN_CLASS_SHIFT }, 876 const_iv (ASN_UTF8_STRING)
442 { "ASN_SEQUENCE", ASN_SEQUENCE }, 877 const_iv (ASN_RELATIVE_OID)
443 { "ASN_IPADDRESS", ASN_IPADDRESS }, 878 const_iv (ASN_SET)
444 { "ASN_COUNTER32", ASN_COUNTER32 }, 879 const_iv (ASN_NUMERIC_STRING)
445 { "ASN_UNSIGNED32", ASN_UNSIGNED32 }, 880 const_iv (ASN_PRINTABLE_STRING)
446 { "ASN_TIMETICKS", ASN_TIMETICKS }, 881 const_iv (ASN_TELETEX_STRING)
447 { "ASN_OPAQUE", ASN_OPAQUE }, 882 const_iv (ASN_T61_STRING)
448 { "ASN_COUNTER64", ASN_COUNTER64 }, 883 const_iv (ASN_VIDEOTEX_STRING)
884 const_iv (ASN_IA5_STRING)
885 const_iv (ASN_ASCII_STRING)
886 const_iv (ASN_UTC_TIME)
887 const_iv (ASN_GENERALIZED_TIME)
888 const_iv (ASN_GRAPHIC_STRING)
889 const_iv (ASN_VISIBLE_STRING)
890 const_iv (ASN_ISO646_STRING)
891 const_iv (ASN_GENERAL_STRING)
892 const_iv (ASN_UNIVERSAL_STRING)
893 const_iv (ASN_CHARACTER_STRING)
894 const_iv (ASN_BMP_STRING)
449 895
450 { "BER_CLASS" , BER_CLASS }, 896 const_iv (ASN_UNIVERSAL)
451 { "BER_TAG" , BER_TAG }, 897 const_iv (ASN_APPLICATION)
452 { "BER_CONSTRUCTED", BER_CONSTRUCTED }, 898 const_iv (ASN_CONTEXT)
453 { "BER_DATA" , BER_DATA }, 899 const_iv (ASN_PRIVATE)
900
901 const_iv (BER_CLASS)
902 const_iv (BER_TAG)
903 const_iv (BER_CONSTRUCTED)
904 const_iv (BER_DATA)
905
906 const_iv (BER_TYPE_BYTES)
907 const_iv (BER_TYPE_UTF8)
908 const_iv (BER_TYPE_UCS2)
909 const_iv (BER_TYPE_UCS4)
910 const_iv (BER_TYPE_INT)
911 const_iv (BER_TYPE_OID)
912 const_iv (BER_TYPE_RELOID)
913 const_iv (BER_TYPE_NULL)
914 const_iv (BER_TYPE_BOOL)
915 const_iv (BER_TYPE_REAL)
916 const_iv (BER_TYPE_CROAK)
917
918 const_iv (SNMP_IPADDRESS)
919 const_iv (SNMP_COUNTER32)
920 const_iv (SNMP_UNSIGNED32)
921 const_iv (SNMP_TIMETICKS)
922 const_iv (SNMP_OPAQUE)
923 const_iv (SNMP_COUNTER64)
924
454 }; 925 };
455 926
456 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) 927 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--)
457 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); 928 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv));
929
930 default_profile = profile_new ();
458} 931}
459 932
460SV * 933SV *
461ber_decode (SV *ber) 934ber_decode (SV *ber, SV *profile = &PL_sv_undef)
462 CODE: 935 CODE:
463{ 936{
937 cur_profile = SvPROFILE (profile);
938 STRLEN len;
464 buf = SvPVbyte (ber, len); 939 buf = SvPVbyte (ber, len);
465 cur = buf; 940 cur = buf;
466 rem = len; 941 end = buf + len;
467 942
468 RETVAL = decode_ber (); 943 RETVAL = decode_ber ();
469} 944}
470 OUTPUT: RETVAL 945 OUTPUT: RETVAL
471 946
472void 947void
473ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *constructed = &PL_sv_undef, SV *data = &PL_sv_undef) 948ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *constructed = &PL_sv_undef, SV *data = &PL_sv_undef)
474 PROTOTYPE: $;$$$
475 PPCODE: 949 PPCODE:
476{ 950{
477 if (!SvOK (tuple)) 951 if (!SvOK (tuple))
478 XSRETURN_NO; 952 XSRETURN_NO;
479 953
480 if (!SvROK (tuple) || SvTYPE (SvRV (tuple)) != SVt_PVAV) 954 if (!SvROK (tuple) || SvTYPE (SvRV (tuple)) != SVt_PVAV)
481 croak ("ber_seq: tuple must be ber tuple (array-ref)"); 955 croak ("ber_is: tuple must be BER tuple (array-ref)");
482 956
483 AV *av = (AV *)SvRV (tuple); 957 AV *av = (AV *)SvRV (tuple);
484 958
485 XPUSHs ( 959 XPUSHs (
486 (!SvOK (klass) || SvIV (AvARRAY (av)[BER_CLASS ]) == SvIV (klass)) 960 (!SvOK (klass) || SvIV (AvARRAY (av)[BER_CLASS ]) == SvIV (klass))
487 && (!SvOK (tag) || SvIV (AvARRAY (av)[BER_TAG ]) == SvIV (tag)) 961 && (!SvOK (tag) || SvIV (AvARRAY (av)[BER_TAG ]) == SvIV (tag))
488 && (!SvOK (constructed) || !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) == !SvIV (constructed)) 962 && (!SvOK (constructed) || !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) == !SvIV (constructed))
489 && (!SvOK (data) || sv_eq (AvARRAY (av)[BER_DATA ], data)) 963 && (!SvOK (data) || sv_eq (AvARRAY (av)[BER_DATA ], data))
490 ? &PL_sv_yes : &PL_sv_no); 964 ? &PL_sv_yes : &PL_sv_undef);
491} 965}
492 966
493void 967void
494ber_is_seq (SV *tuple) 968ber_is_seq (SV *tuple)
495 PROTOTYPE: $
496 PPCODE: 969 PPCODE:
497{ 970{
498 if (!SvOK (tuple)) 971 if (!SvOK (tuple))
499 XSRETURN_UNDEF; 972 XSRETURN_UNDEF;
500 973
501 if (!SvROK (tuple) || SvTYPE (SvRV (tuple)) != SVt_PVAV) 974 AV *av = ber_tuple (tuple);
502 croak ("ber_seq: tuple must be ber tuple (array-ref)");
503
504 AV *av = (AV *)SvRV (tuple);
505 975
506 XPUSHs ( 976 XPUSHs (
507 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL 977 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL
508 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_SEQUENCE 978 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_SEQUENCE
509 && SvIV (AvARRAY (av)[BER_CONSTRUCTED]) 979 && SvIV (AvARRAY (av)[BER_CONSTRUCTED])
510 ? AvARRAY (av)[BER_DATA] : &PL_sv_undef); 980 ? AvARRAY (av)[BER_DATA] : &PL_sv_undef);
511} 981}
512 982
513void 983void
514ber_is_i32 (SV *tuple, IV value) 984ber_is_i32 (SV *tuple, SV *value = &PL_sv_undef)
515 PROTOTYPE: $$
516 PPCODE: 985 PPCODE:
517{ 986{
518 if (!SvOK (tuple)) 987 if (!SvOK (tuple))
519 XSRETURN_NO; 988 XSRETURN_NO;
520 989
521 if (!SvROK (tuple) || SvTYPE (SvRV (tuple)) != SVt_PVAV) 990 AV *av = ber_tuple (tuple);
522 croak ("ber_seq: tuple must be ber tuple (array-ref)");
523 991
524 AV *av = (AV *)SvRV (tuple); 992 IV data = SvIV (AvARRAY (av)[BER_DATA]);
525 993
526 XPUSHs ( 994 XPUSHs (
527 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL 995 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL
528 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_INTEGER32 996 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_INTEGER32
529 && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) 997 && !SvIV (AvARRAY (av)[BER_CONSTRUCTED])
530 && SvIV (AvARRAY (av)[BER_DATA ]) == value 998 && (!SvOK (value) || data == SvIV (value))
531 ? &PL_sv_yes : &PL_sv_no); 999 ? sv_2mortal (data ? newSViv (data) : newSVpv ("0 but true", 0))
1000 : &PL_sv_undef);
532} 1001}
533 1002
534void 1003void
535ber_is_oid (SV *tuple, SV *oid) 1004ber_is_oid (SV *tuple, SV *oid = &PL_sv_undef)
536 PROTOTYPE: $$
537 PPCODE: 1005 PPCODE:
538{ 1006{
539 if (!SvOK (tuple)) 1007 if (!SvOK (tuple))
540 XSRETURN_NO; 1008 XSRETURN_NO;
541 1009
542 if (!SvROK (tuple) || SvTYPE (SvRV (tuple)) != SVt_PVAV) 1010 AV *av = ber_tuple (tuple);
543 croak ("ber_seq: tuple must be ber tuple (array-ref)");
544
545 AV *av = (AV *)SvRV (tuple);
546 1011
547 XPUSHs ( 1012 XPUSHs (
548 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL 1013 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL
549 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_OBJECT_IDENTIFIER 1014 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_OBJECT_IDENTIFIER
550 && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) 1015 && !SvIV (AvARRAY (av)[BER_CONSTRUCTED])
551 && sv_eq (AvARRAY (av)[BER_DATA], oid) 1016 && (!SvOK (oid) || sv_eq (AvARRAY (av)[BER_DATA], oid))
552 ? &PL_sv_yes : &PL_sv_no); 1017 ? newSVsv (AvARRAY (av)[BER_DATA]) : &PL_sv_undef);
553} 1018}
554 1019
1020#############################################################################
1021
1022void
1023ber_encode (SV *tuple, SV *profile = &PL_sv_undef)
1024 PPCODE:
1025{
1026 cur_profile = SvPROFILE (profile);
1027 buf_sv = sv_2mortal (NEWSV (0, 256));
1028 SvPOK_only (buf_sv);
1029 set_buf (buf_sv);
1030
1031 encode_ber (tuple);
1032
1033 SvCUR_set (buf_sv, cur - buf);
1034 XPUSHs (buf_sv);
1035}
1036
1037SV *
1038ber_i32 (IV iv)
1039 CODE:
1040{
1041 AV *av = newAV ();
1042 av_fill (av, BER_ARRAYSIZE - 1);
1043 AvARRAY (av)[BER_CLASS ] = newSVcacheint (ASN_UNIVERSAL);
1044 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_INTEGER32);
1045 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (0);
1046 AvARRAY (av)[BER_DATA ] = newSViv (iv);
1047 RETVAL = newRV_noinc ((SV *)av);
1048}
1049 OUTPUT: RETVAL
1050
1051# TODO: not arrayref, but elements?
1052SV *
1053ber_seq (SV *arrayref)
1054 CODE:
1055{
1056 AV *av = newAV ();
1057 av_fill (av, BER_ARRAYSIZE - 1);
1058 AvARRAY (av)[BER_CLASS ] = newSVcacheint (ASN_UNIVERSAL);
1059 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_SEQUENCE);
1060 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (1);
1061 AvARRAY (av)[BER_DATA ] = newSVsv (arrayref);
1062 RETVAL = newRV_noinc ((SV *)av);
1063}
1064 OUTPUT: RETVAL
1065
1066MODULE = Convert::BER::XS PACKAGE = Convert::BER::XS::Profile
1067
1068SV *
1069new (SV *klass)
1070 CODE:
1071 RETVAL = profile_new ();
1072 OUTPUT: RETVAL
1073

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines