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.6 by root, Sat Apr 20 01:03:59 2019 UTC vs.
Revision 1.34 by root, Thu Feb 6 11:51:40 2020 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#include <math.h>
6
5// C99 required 7// C99 required!
8// this is not just for comments, but also for
9// integer constant semantics,
10// sscanf format modifiers and more.
6 11
7enum { 12enum {
8 // ASN_TAG 13 // ASN_TAG
9 ASN_BOOLEAN = 0x01, 14 ASN_BOOLEAN = 0x01,
10 ASN_INTEGER32 = 0x02, 15 ASN_INTEGER = 0x02,
11 ASN_BIT_STRING = 0x03, 16 ASN_BIT_STRING = 0x03,
12 ASN_OCTET_STRING = 0x04, 17 ASN_OCTET_STRING = 0x04,
13 ASN_NULL = 0x05, 18 ASN_NULL = 0x05,
14 ASN_OBJECT_IDENTIFIER = 0x06, 19 ASN_OBJECT_IDENTIFIER = 0x06,
15 ASN_OID = 0x06, //X 20 ASN_OID = 0x06,
16 ASN_OBJECT_DESCRIPTOR = 0x07, //X 21 ASN_OBJECT_DESCRIPTOR = 0x07,
17 ASN_EXTERNAL = 0x08, //X 22 ASN_EXTERNAL = 0x08,
18 ASN_REAL = 0x09, //X 23 ASN_REAL = 0x09,
19 ASN_ENUMERATED = 0x0a, //X 24 ASN_ENUMERATED = 0x0a,
20 ASN_EMBEDDED_PDV = 0x0b, //X 25 ASN_EMBEDDED_PDV = 0x0b,
21 ASN_UTF8_STRING = 0x0c, //X 26 ASN_UTF8_STRING = 0x0c,
22 ASN_RELATIVE_OID = 0x0d, //X 27 ASN_RELATIVE_OID = 0x0d,
23 ASN_SEQUENCE = 0x10, 28 ASN_SEQUENCE = 0x10,
24 ASN_SET = 0x11, //X 29 ASN_SET = 0x11,
25 ASN_NUMERIC_STRING = 0x12, //X 30 ASN_NUMERIC_STRING = 0x12,
26 ASN_PRINTABLE_STRING = 0x13, //X 31 ASN_PRINTABLE_STRING = 0x13,
27 ASN_TELETEX_STRING = 0x14, //X 32 ASN_TELETEX_STRING = 0x14,
28 ASN_T61_STRING = 0x14, //X 33 ASN_T61_STRING = 0x14,
29 ASN_VIDEOTEX_STRING = 0x15, //X 34 ASN_VIDEOTEX_STRING = 0x15,
30 ASN_IA5_STRING = 0x16, //X 35 ASN_IA5_STRING = 0x16,
31 ASN_ASCII_STRING = 0x16, //X 36 ASN_ASCII_STRING = 0x16,
32 ASN_UTC_TIME = 0x17, //X 37 ASN_UTC_TIME = 0x17,
33 ASN_GENERALIZED_TIME = 0x18, //X 38 ASN_GENERALIZED_TIME = 0x18,
34 ASN_GRAPHIC_STRING = 0x19, //X 39 ASN_GRAPHIC_STRING = 0x19,
35 ASN_VISIBLE_STRING = 0x1a, //X 40 ASN_VISIBLE_STRING = 0x1a,
36 ASN_ISO646_STRING = 0x1a, //X 41 ASN_ISO646_STRING = 0x1a,
37 ASN_GENERAL_STRING = 0x1b, //X 42 ASN_GENERAL_STRING = 0x1b,
38 ASN_UNIVERSAL_STRING = 0x1c, //X 43 ASN_UNIVERSAL_STRING = 0x1c,
39 ASN_CHARACTER_STRING = 0x1d, //X 44 ASN_CHARACTER_STRING = 0x1d,
40 ASN_BMP_STRING = 0x1e, //X 45 ASN_BMP_STRING = 0x1e,
41 46
42 ASN_TAG_BER = 0x1f, 47 ASN_TAG_BER = 0x1f,
43 ASN_TAG_MASK = 0x1f, 48 ASN_TAG_MASK = 0x1f,
44 49
45 // primitive/constructed 50 // primitive/constructed
55 ASN_CLASS_SHIFT = 6, 60 ASN_CLASS_SHIFT = 6,
56 61
57 // ASN_APPLICATION SNMP 62 // ASN_APPLICATION SNMP
58 SNMP_IPADDRESS = 0x00, 63 SNMP_IPADDRESS = 0x00,
59 SNMP_COUNTER32 = 0x01, 64 SNMP_COUNTER32 = 0x01,
65 SNMP_GAUGE32 = 0x02,
60 SNMP_UNSIGNED32 = 0x02, 66 SNMP_UNSIGNED32 = 0x02,
61 SNMP_TIMETICKS = 0x03, 67 SNMP_TIMETICKS = 0x03,
62 SNMP_OPAQUE = 0x04, 68 SNMP_OPAQUE = 0x04,
63 SNMP_COUNTER64 = 0x06, 69 SNMP_COUNTER64 = 0x06,
64}; 70};
65 71
72// low-level types this module can ecode the above (and more) into
66enum { 73enum {
67 BER_TYPE_BYTES, 74 BER_TYPE_BYTES,
68 BER_TYPE_UTF8, 75 BER_TYPE_UTF8,
69 BER_TYPE_UCS2, 76 BER_TYPE_UCS2,
70 BER_TYPE_UCS4, 77 BER_TYPE_UCS4,
76 BER_TYPE_REAL, 83 BER_TYPE_REAL,
77 BER_TYPE_IPADDRESS, 84 BER_TYPE_IPADDRESS,
78 BER_TYPE_CROAK, 85 BER_TYPE_CROAK,
79}; 86};
80 87
88// tuple array indices
81enum { 89enum {
82 BER_CLASS = 0, 90 BER_CLASS = 0,
83 BER_TAG = 1, 91 BER_TAG = 1,
84 BER_CONSTRUCTED = 2, 92 BER_FLAGS = 2,
85 BER_DATA = 3, 93 BER_DATA = 3,
86 BER_ARRAYSIZE 94 BER_ARRAYSIZE
87}; 95};
88 96
89#define MAX_OID_STRLEN 4096 97#define MAX_OID_STRLEN 4096
90 98
91typedef void profile_type; 99typedef void profile_type;
92 100
93static profile_type *cur_profile, *default_profile; 101static profile_type *cur_profile, *default_profile;
94static SV *buf_sv; // encoding buffer 102static SV *buf_sv; // encoding buffer
95static U8 *buf, *cur, *end; // buffer start, current, end 103static U8 *buf, *cur, *end; // buffer start, current, end
104
105#if PERL_VERSION < 18
106# define utf8_to_uvchr_buf(s,e,l) utf8_to_uvchr (s, l)
107#endif
108
109#ifndef SvREFCNT_inc_NN
110#define SvREFCNT_inc_NN(x) SvREFCNT_inc (x)
111#endif
112#ifndef SvREFCNT_dec_NN
113#define SvREFCNT_dec_NN(x) SvREFCNT_dec (x)
114#endif
96 115
97#if __GNUC__ >= 3 116#if __GNUC__ >= 3
98# define expect(expr,value) __builtin_expect ((expr), (value)) 117# define expect(expr,value) __builtin_expect ((expr), (value))
99# define INLINE static inline 118# define INLINE static inline
100#else 119#else
133{ 152{
134 if (!SvOK (profile)) 153 if (!SvOK (profile))
135 return default_profile; 154 return default_profile;
136 155
137 if (!SvROK (profile)) 156 if (!SvROK (profile))
138 croak ("invalid profile"); 157 croak ("Convert::BER::XS::Profile expected");
139 158
140 profile = SvRV (profile); 159 profile = SvRV (profile);
141 160
142 if (SvSTASH (profile) != profile_stash) 161 if (SvSTASH (profile) != profile_stash)
143 croak ("invalid profile object"); 162 croak ("Convert::BER::XS::Profile expected");
144 163
145 return (void *)profile; 164 return (void *)profile;
146} 165}
147 166
148static int 167static int
155 return BER_TYPE_BYTES; 174 return BER_TYPE_BYTES;
156 175
157 return SvPVX (sv)[idx]; 176 return SvPVX (sv)[idx];
158} 177}
159 178
160static int 179static void
161profile_set (profile_type *profile, int klass, int tag, int type) 180profile_set (profile_type *profile, int klass, int tag, int type)
162{ 181{
163 SV *sv = (SV *)profile; 182 SV *sv = (SV *)profile;
164 U32 idx = (tag << 2) + klass; 183 U32 idx = (tag << 2) + klass;
165 STRLEN oldlen = SvCUR (sv); 184 STRLEN oldlen = SvCUR (sv);
174 193
175 SvPVX (sv)[idx] = type; 194 SvPVX (sv)[idx] = type;
176} 195}
177 196
178static SV * 197static SV *
179profile_new () 198profile_new (void)
180{ 199{
181 SV *sv = newSVpvn ("", 0); 200 SV *sv = newSVpvn ("", 0);
182 201
183 static const struct { 202 static const struct {
184 int klass; 203 int klass;
185 int tag; 204 int tag;
186 int type; 205 int type;
187 } *celem, default_map[] = { 206 } *celem, default_map[] = {
188 { ASN_UNIVERSAL, ASN_BOOLEAN , BER_TYPE_BOOL }, 207 { ASN_UNIVERSAL, ASN_BOOLEAN , BER_TYPE_BOOL },
189 { ASN_UNIVERSAL, ASN_INTEGER32 , BER_TYPE_INT }, 208 { ASN_UNIVERSAL, ASN_INTEGER , BER_TYPE_INT },
190 { ASN_UNIVERSAL, ASN_NULL , BER_TYPE_NULL }, 209 { ASN_UNIVERSAL, ASN_NULL , BER_TYPE_NULL },
191 { ASN_UNIVERSAL, ASN_OBJECT_IDENTIFIER, BER_TYPE_OID }, 210 { ASN_UNIVERSAL, ASN_OBJECT_IDENTIFIER, BER_TYPE_OID },
192 { ASN_UNIVERSAL, ASN_OBJECT_DESCRIPTOR, BER_TYPE_OID },
193 { ASN_UNIVERSAL, ASN_RELATIVE_OID , BER_TYPE_RELOID }, 211 { ASN_UNIVERSAL, ASN_RELATIVE_OID , BER_TYPE_RELOID },
194 { ASN_UNIVERSAL, ASN_REAL , BER_TYPE_REAL }, 212 { ASN_UNIVERSAL, ASN_REAL , BER_TYPE_REAL },
213 { ASN_UNIVERSAL, ASN_ENUMERATED , BER_TYPE_INT },
195 { ASN_UNIVERSAL, ASN_UTF8_STRING , BER_TYPE_UTF8 }, 214 { ASN_UNIVERSAL, ASN_UTF8_STRING , BER_TYPE_UTF8 },
196 { ASN_UNIVERSAL, ASN_BMP_STRING , BER_TYPE_UCS2 }, 215 { ASN_UNIVERSAL, ASN_BMP_STRING , BER_TYPE_UCS2 },
197 { ASN_UNIVERSAL, ASN_UNIVERSAL_STRING , BER_TYPE_UCS4 }, 216 { ASN_UNIVERSAL, ASN_UNIVERSAL_STRING , BER_TYPE_UCS4 },
198 }; 217 };
199 218
200 for (celem = default_map + sizeof (default_map) / sizeof (default_map [0]); celem > default_map; celem--) 219 for (celem = default_map + sizeof (default_map) / sizeof (default_map [0]); celem-- > default_map; )
201 profile_set ((void *)sv, celem->klass, celem->tag, celem->type); 220 profile_set ((profile_type *)sv, celem->klass, celem->tag, celem->type);
202 221
203 return sv_bless (newRV_noinc (sv), profile_stash); 222 return sv_bless (newRV_noinc (sv), profile_stash);
204} 223}
205 224
206///////////////////////////////////////////////////////////////////////////// 225/////////////////////////////////////////////////////////////////////////////
219 error ("unexpected end of message buffer"); 238 error ("unexpected end of message buffer");
220} 239}
221 240
222// get_* functions fetch something from the buffer 241// get_* functions fetch something from the buffer
223// decode_* functions use get_* fun ctions to decode ber values 242// decode_* functions use get_* fun ctions to decode ber values
243
244// get single octet
245static U8
246get_u8 (void)
247{
248 if (cur == end)
249 error ("unexpected end of message buffer");
250
251 return *cur++;
252}
224 253
225// get n octets 254// get n octets
226static U8 * 255static U8 *
227get_n (UV count) 256get_n (UV count)
228{ 257{
230 U8 *res = cur; 259 U8 *res = cur;
231 cur += count; 260 cur += count;
232 return res; 261 return res;
233} 262}
234 263
235// get single octet
236static U8
237get_u8 (void)
238{
239 if (cur == end)
240 error ("unexpected end of message buffer");
241
242 return *cur++;
243}
244
245// get ber-encoded integer (i.e. pack "w") 264// get ber-encoded integer (i.e. pack "w")
246static U32 265static UV
247get_w (void) 266get_w (void)
248{ 267{
249 U32 res = 0; 268 UV res = 0;
269 U8 c = get_u8 ();
270
271 if (expect_false (c == 0x80))
272 error ("invalid BER padding (X.690 8.1.2.4.2, 8.19.2)");
250 273
251 for (;;) 274 for (;;)
252 { 275 {
253 U8 c = get_u8 (); 276 if (expect_false (res >> UVSIZE * 8 - 7))
277 error ("BER variable length integer overflow");
278
254 res = (res << 7) | (c & 0x7f); 279 res = (res << 7) | (c & 0x7f);
255 280
256 if (!(c & 0x80)) 281 if (expect_true (!(c & 0x80)))
257 return res; 282 return res;
258 }
259}
260 283
284 c = get_u8 ();
285 }
286}
287
261static U32 288static UV
262get_length (void) 289get_length (void)
263{ 290{
264 U32 res = get_u8 (); 291 UV res = get_u8 ();
265 292
266 if (res & 0x80) 293 if (expect_false (res & 0x80))
267 { 294 {
268 int cnt = res & 0x7f; 295 U8 cnt = res & 0x7f;
296
297 // this genewrates quite ugly code, but the overhead
298 // of copying the bytes for these lengths is probably so high
299 // that a slightly inefficient get_length won't matter.
300
301 if (expect_false (cnt == 0))
302 error ("invalid use of indefinite BER length form in primitive encoding (X.690 8.1.3.2)");
303
304 if (expect_false (cnt > UVSIZE))
305 error ("BER value length too long (must fit into UV) or BER reserved value in length (X.690 8.1.3.5)");
306
307 want (cnt);
308
269 res = 0; 309 res = 0;
270 310 do
271 switch (cnt) 311 res = (res << 8) | *cur++;
272 { 312 while (--cnt);
273 case 0:
274 error ("indefinite ASN.1 lengths not supported");
275 return 0;
276
277 default:
278 error ("ASN.1 length too long");
279 return 0;
280
281 case 4: res = (res << 8) | get_u8 ();
282 case 3: res = (res << 8) | get_u8 ();
283 case 2: res = (res << 8) | get_u8 ();
284 case 1: res = (res << 8) | get_u8 ();
285 }
286 } 313 }
287 314
288 return res; 315 return res;
289} 316}
290 317
291static SV * 318static SV *
292decode_int () 319decode_int (UV len)
293{ 320{
294 int len = get_length ();
295
296 if (len <= 0) 321 if (!len)
297 { 322 error ("invalid BER_TYPE_INT length zero (X.690 8.3.1)");
298 error ("integer length equal to zero");
299 return 0;
300 }
301 323
302 U8 *data = get_n (len); 324 U8 *data = get_n (len);
303 325
326 if (expect_false (len > 1))
327 {
328 U16 mask = (data [0] << 8) | data [1] & 0xff80;
329
330 if (expect_false (mask == 0xff80 || mask == 0x0000))
331 error ("invalid padding in BER_TYPE_INT (X.690 8.3.2)");
332 }
333
304 int negative = data [0] & 0x80; 334 int negative = data [0] & 0x80;
305 335
306 UV val = negative ? -1 : 0; // copy signbit to all bits 336 UV val = negative ? -1 : 0; // copy signbit to all bits
337
338 if (len > UVSIZE + (!negative && !*data))
339 error ("BER_TYPE_INT overflow");
307 340
308 do 341 do
309 val = (val << 8) | *data++; 342 val = (val << 8) | *data++;
310 while (--len); 343 while (--len);
311 344
313 // but that's ok, as perl relies on it as well. 346 // but that's ok, as perl relies on it as well.
314 return negative ? newSViv ((IV)val) : newSVuv (val); 347 return negative ? newSViv ((IV)val) : newSVuv (val);
315} 348}
316 349
317static SV * 350static SV *
318decode_data (void) 351decode_data (UV len)
319{ 352{
320 U32 len = get_length ();
321 U8 *data = get_n (len);
322 return newSVpvn ((char *)data, len); 353 return newSVpvn ((char *)get_n (len), len);
323} 354}
324 355
325// gelper for decode_object_identifier 356// helper for decode_object_identifier
326static char * 357static char *
327write_uv (char *buf, U32 u) 358write_uv (char *buf, UV u)
328{ 359{
329 // the one-digit case is absolutely predominant, so this pays off (hopefully) 360 // the one-digit case is absolutely predominant, so this pays off (hopefully)
330 if (expect_true (u < 10)) 361 if (expect_true (u < 10))
331 *buf++ = u + '0'; 362 *buf++ = u + '0';
332 else 363 else
333 { 364 {
365 // this *could* be done much faster using branchless fixed-point arithmetics
334 char *beg = buf; 366 char *beg = buf;
335 367
336 do 368 do
337 { 369 {
338 *buf++ = u % 10 + '0'; 370 *buf++ = u % 10 + '0';
339 u /= 10; 371 u /= 10;
340 } 372 }
341 while (u); 373 while (u);
342 374
343 // reverse digits 375 // reverse digits
344 for (char *ptr = buf; --ptr != beg; ++beg) 376 char *ptr = buf;
377 while (--ptr > beg)
345 { 378 {
346 char c = *ptr; 379 char c = *ptr;
347 *ptr = *beg; 380 *ptr = *beg;
348 *beg = c; 381 *beg = c;
382 ++beg;
349 } 383 }
350 } 384 }
351 385
352 return buf; 386 return buf;
353} 387}
354 388
355static SV * 389static SV *
356decode_oid (int relative) 390decode_oid (UV len, int relative)
357{ 391{
358 U32 len = get_length ();
359
360 if (len <= 0) 392 if (len <= 0)
361 { 393 {
362 error ("OBJECT IDENTIFIER length equal to zero"); 394 error ("BER_TYPE_OID length must not be zero");
363 return &PL_sv_undef; 395 return &PL_sv_undef;
364 } 396 }
365 397
366 U8 *end = cur + len; 398 U8 *end = cur + len;
367 U32 w = get_w (); 399 UV w = get_w ();
368 400
369 static char oid[MAX_OID_STRLEN]; // must be static 401 static char oid[MAX_OID_STRLEN]; // static, because too large for stack
370 char *app = oid; 402 char *app = oid;
371 403
372 if (relative) 404 if (relative)
373 app = write_uv (app, w); 405 app = write_uv (app, w);
374 else 406 else
375 { 407 {
408 UV w1, w2;
409
410 if (w < 2 * 40)
411 (w1 = w / 40), (w2 = w % 40);
412 else
413 (w1 = 2), (w2 = w - 2 * 40);
414
376 app = write_uv (app, (U8)w / 40); 415 app = write_uv (app, w1);
377 *app++ = '.'; 416 *app++ = '.';
378 app = write_uv (app, (U8)w % 40); 417 app = write_uv (app, w2);
418 }
419
420 while (cur < end)
379 } 421 {
380
381 // we assume an oid component is never > 64 bytes 422 // we assume an oid component is never > 64 digits
382 while (cur < end && oid + sizeof (oid) - app > 64) 423 if (oid + sizeof (oid) - app < 64)
383 { 424 croak ("BER_TYPE_OID to long to decode");
425
384 w = get_w (); 426 w = get_w ();
385 *app++ = '.'; 427 *app++ = '.';
386 app = write_uv (app, w); 428 app = write_uv (app, w);
387 } 429 }
388 430
389 return newSVpvn (oid, app - oid); 431 return newSVpvn (oid, app - oid);
390} 432}
391 433
434// oh my, this is a total mess
392static SV * 435static SV *
436decode_real (UV len)
437{
438 SV *res;
439 U8 *beg = cur;
440
441 if (len == 0)
442 res = newSVnv (0.);
443 else
444 {
445 U8 info = get_u8 ();
446
447 if (info & 0x80)
448 {
449 // binary
450 static const U8 base[] = { 2, 8, 16, 0 };
451 NV S = info & 0x40 ? -1 : 1; // sign
452 NV B = base [(info >> 4) & 3]; // base
453 NV F = 1 << ((info >> 2) & 3); // scale factor ("shift")
454 int L = info & 3; // exponent length
455
456 if (!B)
457 croak ("BER_TYPE_REAL binary encoding uses invalid base (0x%02x)", info);
458
459 SAVETMPS;
460
461 SV *E = sv_2mortal (decode_int (L == 3 ? get_u8 () : L + 1));
462 SV *M = sv_2mortal (decode_int (len - (cur - beg)));
463
464 res = newSVnv (S * SvNV (M) * F * Perl_pow (B, SvNV (E)));
465
466 FREETMPS;
467 }
468 else if (info & 0x40)
469 {
470 // SpecialRealValue
471 U8 special = get_u8 ();
472 NV val;
473
474 switch (special)
475 {
476 case 0x40: val = NV_INF; break;
477 case 0x41: val = -NV_INF; break;
478 case 0x42: val = NV_NAN; break;
479 case 0x43: val = -(NV)0.; break;
480
481 default:
482 croak ("BER_TYPE_REAL SpecialRealValues invalid encoding 0x%02x (X.690 8.5.9)", special);
483 }
484
485 res = newSVnv (val);
486 }
487 else
488 {
489 // decimal
490 dSP;
491 SAVETMPS;
492 PUSHMARK (SP);
493 EXTEND (SP, 2);
494 PUSHs (sv_2mortal (newSVcacheint (info & 0x3f)));
495 PUSHs (sv_2mortal (newSVpvn (get_n (len - 1), len - 1)));
496 PUTBACK;
497 call_pv ("Convert::BER::XS::_decode_real_decimal", G_SCALAR);
498 SPAGAIN;
499 res = SvREFCNT_inc_NN (POPs);
500 PUTBACK;
501 FREETMPS;
502 }
503 }
504
505 if (cur - beg != len)
506 {
507 SvREFCNT_dec_NN (res);
508 croak ("BER_TYPE_REAL invalid content length (X.690 8,5)");
509 }
510
511 return res;
512}
513
514// TODO: this is unacceptably slow
515static SV *
516decode_ucs (UV len, int chrsize)
517{
518 if (len & (chrsize - 1))
519 croak ("BER_TYPE_UCS has an invalid number of octets (%d)", len);
520
521 SV *res = NEWSV (0, 0);
522
523 while (len)
524 {
525 U8 b1 = get_u8 ();
526 U8 b2 = get_u8 ();
527 U32 chr = (b1 << 8) | b2;
528
529 if (chrsize == 4)
530 {
531 U8 b3 = get_u8 ();
532 U8 b4 = get_u8 ();
533 chr = (chr << 16) | (b3 << 8) | b4;
534 }
535
536 U8 uchr [UTF8_MAXBYTES];
537 int uclen = uvuni_to_utf8 (uchr, chr) - uchr;
538
539 sv_catpvn (res, (const char *)uchr, uclen);
540 len -= chrsize;
541 }
542
543 SvUTF8_on (res);
544
545 return res;
546}
547
548static SV *
393decode_ber () 549decode_ber (void)
394{ 550{
395 int identifier = get_u8 (); 551 int identifier = get_u8 ();
396 552
397 SV *res; 553 SV *res;
398 554
401 int tag = identifier & ASN_TAG_MASK; 557 int tag = identifier & ASN_TAG_MASK;
402 558
403 if (tag == ASN_TAG_BER) 559 if (tag == ASN_TAG_BER)
404 tag = get_w (); 560 tag = get_w ();
405 561
406 if (tag == ASN_TAG_BER)
407 tag = get_w ();
408
409 if (constructed) 562 if (constructed)
410 { 563 {
411 U32 len = get_length (); 564 want (1);
412 U32 seqend = (cur - buf) + len;
413 AV *av = (AV *)sv_2mortal ((SV *)newAV ()); 565 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
414 566
415 while (cur < buf + seqend) 567 if (expect_false (*cur == 0x80))
568 {
569 // indefinite length
570 ++cur;
571
572 for (;;)
573 {
574 want (2);
575 if (!cur [0] && !cur [1])
576 {
577 cur += 2;
578 break;
579 }
580
416 av_push (av, decode_ber ()); 581 av_push (av, decode_ber ());
582 }
583 }
584 else
585 {
586 UV len = get_length ();
587 UV seqend = (cur - buf) + len;
417 588
418 if (cur > buf + seqend) 589 while (cur < buf + seqend)
590 av_push (av, decode_ber ());
591
592 if (expect_false (cur > buf + seqend))
419 croak ("constructed type %02x overflow (%x %x)\n", identifier, cur - buf, seqend); 593 croak ("CONSTRUCTED type %02x length overflow (0x%x 0x%x)\n", identifier, (int)(cur - buf), (int)seqend);
594 }
420 595
421 res = newRV_inc ((SV *)av); 596 res = newRV_inc ((SV *)av);
422 } 597 }
423 else 598 else
599 {
600 UV len = get_length ();
601
424 switch (profile_lookup (cur_profile, klass, tag)) 602 switch (profile_lookup (cur_profile, klass, tag))
425 { 603 {
426 case BER_TYPE_NULL: 604 case BER_TYPE_NULL:
605 if (expect_false (len))
606 croak ("BER_TYPE_NULL value with non-zero length %d encountered (X.690 8.8.2)", len);
607
427 res = &PL_sv_undef; 608 res = &PL_sv_undef;
428 break; 609 break;
429 610
430 case BER_TYPE_BOOL: 611 case BER_TYPE_BOOL:
431 {
432 U32 len = get_length ();
433
434 if (len != 1) 612 if (expect_false (len != 1))
435 croak ("BER_TYPE_BOOLEAN type with invalid length %d encountered", len); 613 croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered (X.690 8.2.1)", len);
436 614
437 res = newSVcacheint (get_u8 () ? 0 : 1); 615 res = newSVcacheint (!!get_u8 ());
438 }
439 break; 616 break;
440 617
441 case BER_TYPE_OID: 618 case BER_TYPE_OID:
442 res = decode_oid (0); 619 res = decode_oid (len, 0);
443 break; 620 break;
444 621
445 case BER_TYPE_RELOID: 622 case BER_TYPE_RELOID:
446 res = decode_oid (1); 623 res = decode_oid (len, 1);
447 break; 624 break;
448 625
449 case BER_TYPE_INT: 626 case BER_TYPE_INT:
450 res = decode_int (); 627 res = decode_int (len);
451 break; 628 break;
452 629
453 case BER_TYPE_UTF8: 630 case BER_TYPE_UTF8:
454 res = decode_data (); 631 res = decode_data (len);
455 SvUTF8_on (res); 632 SvUTF8_on (res);
456 break; 633 break;
457 634
458 case BER_TYPE_BYTES: 635 case BER_TYPE_BYTES:
459 res = decode_data (); 636 res = decode_data (len);
460 break; 637 break;
461 638
462 case BER_TYPE_IPADDRESS: 639 case BER_TYPE_IPADDRESS:
463 { 640 {
464 U32 len = get_length ();
465
466 if (len != 4) 641 if (len != 4)
467 croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered", len); 642 croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered (RFC 2578 7.1.5)", len);
468 643
469 U8 c1 = get_u8 (); 644 U8 *data = get_n (4);
470 U8 c2 = get_u8 (); 645 res = newSVpvf ("%d.%d.%d.%d", data [0], data [1], data [2], data [3]);
471 U8 c3 = get_u8 ();
472 U8 c4 = get_u8 ();
473
474 res = newSVpvf ("%d.%d.%d.%d", c1, c2, c3, c4);
475 } 646 }
476 break; 647 break;
477 648
478 case BER_TYPE_REAL:
479 case BER_TYPE_UCS2: 649 case BER_TYPE_UCS2:
650 res = decode_ucs (len, 2);
651 break;
652
480 case BER_TYPE_UCS4: 653 case BER_TYPE_UCS4:
654 res = decode_ucs (len, 4);
655 break;
656
657 case BER_TYPE_REAL:
658 res = decode_real (len);
659 break;
660
481 case BER_TYPE_CROAK: 661 case BER_TYPE_CROAK:
662 croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag);
663
482 default: 664 default:
483 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 665 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
484 } 666 }
667 }
485 668
486 AV *av = newAV (); 669 AV *av = newAV ();
487 av_fill (av, BER_ARRAYSIZE - 1); 670 av_fill (av, BER_ARRAYSIZE - 1);
488 AvARRAY (av)[BER_CLASS ] = newSVcacheint (klass); 671 AvARRAY (av)[BER_CLASS] = newSVcacheint (klass);
489 AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); 672 AvARRAY (av)[BER_TAG ] = newSVcacheint (tag);
490 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (constructed ? 1 : 0); 673 AvARRAY (av)[BER_FLAGS] = newSVcacheint (constructed ? 1 : 0);
491 AvARRAY (av)[BER_DATA ] = res; 674 AvARRAY (av)[BER_DATA ] = res;
492 675
493 return newRV_noinc ((SV *)av); 676 return newRV_noinc ((SV *)av);
494} 677}
495 678
496///////////////////////////////////////////////////////////////////////////// 679/////////////////////////////////////////////////////////////////////////////
501strlen_sum (STRLEN l1, STRLEN l2) 684strlen_sum (STRLEN l1, STRLEN l2)
502{ 685{
503 size_t sum = l1 + l2; 686 size_t sum = l1 + l2;
504 687
505 if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) 688 if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum)
506 croak ("JSON::XS: string size overflow"); 689 croak ("Convert::BER::XS: string size overflow");
507 690
508 return sum; 691 return sum;
509} 692}
510 693
511static void 694static void
512set_buf (SV *sv) 695set_buf (SV *sv)
513{ 696{
514 STRLEN len; 697 STRLEN len;
515 buf_sv = sv; 698 buf_sv = sv;
516 buf = SvPVbyte (buf_sv, len); 699 buf = (U8 *)SvPVbyte (buf_sv, len);
517 cur = buf; 700 cur = buf;
518 end = buf + len; 701 end = buf + len;
519} 702}
520 703
521/* similar to SvGROW, but somewhat safer and guarantees exponential realloc strategy */ 704/* similar to SvGROW, but somewhat safer and guarantees exponential realloc strategy */
535need (STRLEN len) 718need (STRLEN len)
536{ 719{
537 if (expect_false ((uintptr_t)(end - cur) < len)) 720 if (expect_false ((uintptr_t)(end - cur) < len))
538 { 721 {
539 STRLEN pos = cur - buf; 722 STRLEN pos = cur - buf;
540 buf = my_sv_grow (buf_sv, pos, len); 723 buf = (U8 *)my_sv_grow (buf_sv, pos, len);
541 cur = buf + pos; 724 cur = buf + pos;
542 end = buf + SvLEN (buf_sv) - 1; 725 end = buf + SvLEN (buf_sv) - 1;
543 } 726 }
544} 727}
545 728
549 need (1); 732 need (1);
550 *cur++ = val; 733 *cur++ = val;
551} 734}
552 735
553static void 736static void
554put_w_nocheck (U32 val) 737put_w_nocheck (UV val)
555{ 738{
739#if UVSIZE > 4
740 *cur = (val >> 7 * 9) | 0x80; cur += val >= ((UV)1 << (7 * 9));
741 *cur = (val >> 7 * 8) | 0x80; cur += val >= ((UV)1 << (7 * 8));
742 *cur = (val >> 7 * 7) | 0x80; cur += val >= ((UV)1 << (7 * 7));
743 *cur = (val >> 7 * 6) | 0x80; cur += val >= ((UV)1 << (7 * 6));
744 *cur = (val >> 7 * 5) | 0x80; cur += val >= ((UV)1 << (7 * 5));
745#endif
556 *cur = (val >> 7 * 4) | 0x80; cur += val >= (1 << (7 * 4)); 746 *cur = (val >> 7 * 4) | 0x80; cur += val >= ((UV)1 << (7 * 4));
557 *cur = (val >> 7 * 3) | 0x80; cur += val >= (1 << (7 * 3)); 747 *cur = (val >> 7 * 3) | 0x80; cur += val >= ((UV)1 << (7 * 3));
558 *cur = (val >> 7 * 2) | 0x80; cur += val >= (1 << (7 * 2)); 748 *cur = (val >> 7 * 2) | 0x80; cur += val >= ((UV)1 << (7 * 2));
559 *cur = (val >> 7 * 1) | 0x80; cur += val >= (1 << (7 * 1)); 749 *cur = (val >> 7 * 1) | 0x80; cur += val >= ((UV)1 << (7 * 1));
560 *cur = val & 0x7f; cur += 1; 750 *cur = val & 0x7f; cur += 1;
561} 751}
562 752
563static void 753static void
564put_w (U32 val) 754put_w (UV val)
565{ 755{
566 need (5); // we only handle up to 5 bytes 756 need (5); // we only handle up to 5 bytes
567 757
568 put_w_nocheck (val); 758 put_w_nocheck (val);
569} 759}
570 760
571static U8 * 761static U8 *
572put_length_at (U32 val, U8 *cur) 762put_length_at (UV val, U8 *cur)
573{ 763{
574 if (val < 0x7fU) 764 if (val <= 0x7fU)
575 *cur++ = val; 765 *cur++ = val;
576 else 766 else
577 { 767 {
578 U8 *lenb = cur++; 768 U8 *lenb = cur++;
579 769
580 *cur = val >> 24; cur += *cur > 0; 770#if UVSIZE > 4
581 *cur = val >> 16; cur += *cur > 0; 771 *cur = val >> 56; cur += val >= ((UV)1 << (8 * 7));
582 *cur = val >> 8; cur += *cur > 0; 772 *cur = val >> 48; cur += val >= ((UV)1 << (8 * 6));
773 *cur = val >> 40; cur += val >= ((UV)1 << (8 * 5));
774 *cur = val >> 32; cur += val >= ((UV)1 << (8 * 4));
775#endif
776 *cur = val >> 24; cur += val >= ((UV)1 << (8 * 3));
777 *cur = val >> 16; cur += val >= ((UV)1 << (8 * 2));
778 *cur = val >> 8; cur += val >= ((UV)1 << (8 * 1));
583 *cur = val ; cur += 1; 779 *cur = val ; cur += 1;
584 780
585 *lenb = 0x80 + cur - lenb - 1; 781 *lenb = 0x80 + cur - lenb - 1;
586 } 782 }
587 783
588 return cur; 784 return cur;
589} 785}
590 786
591static void 787static void
592put_length (U32 val) 788put_length (UV val)
593{ 789{
594 need (5); 790 need (9 + val);
595 cur = put_length_at (val, cur); 791 cur = put_length_at (val, cur);
596} 792}
597 793
598// return how many bytes the encoded length requires 794// return how many bytes the encoded length requires
599static int length_length (U32 val) 795static int length_length (UV val)
600{ 796{
601 return val < 0x7fU 797 // use hashing with a DeBruin sequence, anyone?
798 return expect_true (val <= 0x7fU)
602 ? 1 799 ? 1
603 : 2 + (val > 0xffU) + (val > 0xffffU) + (val > 0xffffffU); 800 : 2
801 + (val > 0x000000000000ffU)
802 + (val > 0x0000000000ffffU)
803 + (val > 0x00000000ffffffU)
804#if UVSIZE > 4
805 + (val > 0x000000ffffffffU)
806 + (val > 0x0000ffffffffffU)
807 + (val > 0x00ffffffffffffU)
808 + (val > 0xffffffffffffffU)
809#endif
810 ;
604} 811}
605 812
606static void 813static void
607encode_data (const char *ptr, STRLEN len) 814encode_data (const char *ptr, STRLEN len)
608{ 815{
609 put_length (len); 816 put_length (len);
610 need (len);
611 memcpy (cur, ptr, len); 817 memcpy (cur, ptr, len);
612 cur += len; 818 cur += len;
613} 819}
614 820
615static void 821static void
671 877
672 *lenb = cur - lenb - 1; 878 *lenb = cur - lenb - 1;
673} 879}
674 880
675// we don't know the length yet, so we optimistically 881// we don't know the length yet, so we optimistically
676// assume the length will need one octet later. if that 882// assume the length will need one octet later. If that
677// turns out to be wrong, we memove as needed. 883// turns out to be wrong, we memmove as needed.
678// mark the beginning 884// mark the beginning
679static STRLEN 885static STRLEN
680len_fixup_mark () 886len_fixup_mark (void)
681{ 887{
682 return cur++ - buf; 888 return cur++ - buf;
683} 889}
684 890
685// patch up the length 891// patch up the length
742 } 948 }
743 949
744 len_fixup (mark); 950 len_fixup (mark);
745} 951}
746 952
953static void
954encode_real (SV *data)
955{
956 NV nv = SvNV (data);
957
958 if (expect_false (nv == (NV)0.))
959 {
960 if (signbit (nv))
961 {
962 // negative zero
963 need (3);
964 *cur++ = 2;
965 *cur++ = 0x40;
966 *cur++ = 0x43;
967 }
968 else
969 {
970 // positive zero
971 need (1);
972 *cur++ = 0;
973 }
974 }
975 else if (expect_false (Perl_isinf (nv)))
976 {
977 need (3);
978 *cur++ = 2;
979 *cur++ = 0x40;
980 *cur++ = nv < (NV)0. ? 0x41 : 0x40;
981 }
982 else if (expect_false (Perl_isnan (nv)))
983 {
984 need (3);
985 *cur++ = 2;
986 *cur++ = 0x40;
987 *cur++ = 0x42;
988 }
989 else
990 {
991 // use decimal encoding
992 dSP;
993 SAVETMPS;
994 PUSHMARK (SP);
995 EXTEND (SP, 2);
996 PUSHs (data);
997 PUSHs (sv_2mortal (newSVcacheint (NV_DIG)));
998 PUTBACK;
999 call_pv ("Convert::BER::XS::_encode_real_decimal", G_SCALAR);
1000 SPAGAIN;
1001
1002 SV *sv = POPs;
1003 STRLEN l;
1004 char *f = SvPV (sv, l);
1005
1006 put_length (l);
1007 memcpy (cur, f, l);
1008 cur += l;
1009
1010 PUTBACK;
1011 FREETMPS;
1012 }
1013}
1014
1015static void
1016encode_ucs (SV *data, int chrsize)
1017{
1018 STRLEN uchars = sv_len_utf8 (data);
1019 STRLEN len;;
1020 char *ptr = SvPVutf8 (data, len);
1021
1022 put_length (uchars * chrsize);
1023
1024 while (uchars--)
1025 {
1026 STRLEN uclen;
1027 UV uchr = utf8_to_uvchr_buf ((U8 *)ptr, (U8 *)ptr + len, &uclen);
1028
1029 ptr += uclen;
1030 len -= uclen;
1031
1032 if (chrsize == 4)
1033 {
1034 *cur++ = uchr >> 24;
1035 *cur++ = uchr >> 16;
1036 }
1037
1038 *cur++ = uchr >> 8;
1039 *cur++ = uchr;
1040 }
1041}
1042
747// check whether an SV is a BER tuple and returns its AV * 1043// check whether an SV is a BER tuple and returns its AV *
748static AV * 1044static AV *
749ber_tuple (SV *tuple) 1045ber_tuple (SV *tuple)
750{ 1046{
751 SV *rv; 1047 SV *rv;
767{ 1063{
768 AV *av = ber_tuple (tuple); 1064 AV *av = ber_tuple (tuple);
769 1065
770 int klass = SvIV (AvARRAY (av)[BER_CLASS]); 1066 int klass = SvIV (AvARRAY (av)[BER_CLASS]);
771 int tag = SvIV (AvARRAY (av)[BER_TAG]); 1067 int tag = SvIV (AvARRAY (av)[BER_TAG]);
772 int constructed = SvIV (AvARRAY (av)[BER_CONSTRUCTED]) ? ASN_CONSTRUCTED : 0; 1068 int constructed = SvIV (AvARRAY (av)[BER_FLAGS]) & 1 ? ASN_CONSTRUCTED : 0;
773 SV *data = AvARRAY (av)[BER_DATA]; 1069 SV *data = AvARRAY (av)[BER_DATA];
774 1070
775 int identifier = (klass << ASN_CLASS_SHIFT) | constructed; 1071 int identifier = (klass << ASN_CLASS_SHIFT) | constructed;
776 1072
777 if (expect_false (tag >= ASN_TAG_BER)) 1073 if (expect_false (tag >= ASN_TAG_BER))
788 // and adjust later 1084 // and adjust later
789 need (1); 1085 need (1);
790 STRLEN mark = len_fixup_mark (); 1086 STRLEN mark = len_fixup_mark ();
791 1087
792 if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV)) 1088 if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV))
793 croak ("BER constructed data must be array-reference"); 1089 croak ("BER CONSTRUCTED data must be array-reference");
794 1090
795 AV *av = (AV *)SvRV (data); 1091 AV *av = (AV *)SvRV (data);
796 int fill = AvFILL (av); 1092 int fill = AvFILL (av);
797 1093
798 if (expect_false (SvRMAGICAL (av))) 1094 if (expect_false (SvRMAGICAL (av)))
799 croak ("BER constructed data must not be tied"); 1095 croak ("BER CONSTRUCTED data must not be tied");
800 1096
1097 int i;
801 for (int i = 0; i <= fill; ++i) 1098 for (i = 0; i <= fill; ++i)
802 encode_ber (AvARRAY (av)[i]); 1099 encode_ber (AvARRAY (av)[i]);
803 1100
804 len_fixup (mark); 1101 len_fixup (mark);
805 } 1102 }
806 else 1103 else
810 put_length (0); 1107 put_length (0);
811 break; 1108 break;
812 1109
813 case BER_TYPE_BOOL: 1110 case BER_TYPE_BOOL:
814 put_length (1); 1111 put_length (1);
815 put_u8 (SvTRUE (data) ? 0xff : 0x00); 1112 *cur++ = SvTRUE (data) ? 0xff : 0x00; // 0xff = DER/CER
816 break; 1113 break;
817 1114
818 case BER_TYPE_OID: 1115 case BER_TYPE_OID:
819 encode_oid (data, 0); 1116 encode_oid (data, 0);
820 break; 1117 break;
849 sscanf (SvPV_nolen (data), "%hhu.%hhu.%hhu.%hhu", ip + 0, ip + 1, ip + 2, ip + 3); 1146 sscanf (SvPV_nolen (data), "%hhu.%hhu.%hhu.%hhu", ip + 0, ip + 1, ip + 2, ip + 3);
850 encode_data ((const char *)ip, sizeof (ip)); 1147 encode_data ((const char *)ip, sizeof (ip));
851 } 1148 }
852 break; 1149 break;
853 1150
1151 case BER_TYPE_UCS2:
1152 encode_ucs (data, 2);
1153 break;
1154
1155 case BER_TYPE_UCS4:
1156 encode_ucs (data, 4);
1157 break;
1158
854 case BER_TYPE_REAL: 1159 case BER_TYPE_REAL:
855 case BER_TYPE_UCS2: 1160 encode_real (data);
856 case BER_TYPE_UCS4: 1161 break;
1162
857 case BER_TYPE_CROAK: 1163 case BER_TYPE_CROAK:
1164 croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag);
1165
858 default: 1166 default:
859 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 1167 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
860 } 1168 }
861 1169
862} 1170}
877 const char *name; 1185 const char *name;
878 IV iv; 1186 IV iv;
879 } *civ, const_iv[] = { 1187 } *civ, const_iv[] = {
880#define const_iv(name) { # name, name }, 1188#define const_iv(name) { # name, name },
881 const_iv (ASN_BOOLEAN) 1189 const_iv (ASN_BOOLEAN)
882 const_iv (ASN_INTEGER32) 1190 const_iv (ASN_INTEGER)
883 const_iv (ASN_BIT_STRING) 1191 const_iv (ASN_BIT_STRING)
884 const_iv (ASN_OCTET_STRING) 1192 const_iv (ASN_OCTET_STRING)
885 const_iv (ASN_NULL) 1193 const_iv (ASN_NULL)
886 const_iv (ASN_OBJECT_IDENTIFIER) 1194 const_iv (ASN_OBJECT_IDENTIFIER)
887 const_iv (ASN_OBJECT_DESCRIPTOR) 1195 const_iv (ASN_OBJECT_DESCRIPTOR)
916 const_iv (ASN_CONTEXT) 1224 const_iv (ASN_CONTEXT)
917 const_iv (ASN_PRIVATE) 1225 const_iv (ASN_PRIVATE)
918 1226
919 const_iv (BER_CLASS) 1227 const_iv (BER_CLASS)
920 const_iv (BER_TAG) 1228 const_iv (BER_TAG)
921 const_iv (BER_CONSTRUCTED) 1229 const_iv (BER_FLAGS)
922 const_iv (BER_DATA) 1230 const_iv (BER_DATA)
923 1231
924 const_iv (BER_TYPE_BYTES) 1232 const_iv (BER_TYPE_BYTES)
925 const_iv (BER_TYPE_UTF8) 1233 const_iv (BER_TYPE_UTF8)
926 const_iv (BER_TYPE_UCS2) 1234 const_iv (BER_TYPE_UCS2)
934 const_iv (BER_TYPE_IPADDRESS) 1242 const_iv (BER_TYPE_IPADDRESS)
935 const_iv (BER_TYPE_CROAK) 1243 const_iv (BER_TYPE_CROAK)
936 1244
937 const_iv (SNMP_IPADDRESS) 1245 const_iv (SNMP_IPADDRESS)
938 const_iv (SNMP_COUNTER32) 1246 const_iv (SNMP_COUNTER32)
1247 const_iv (SNMP_GAUGE32)
939 const_iv (SNMP_UNSIGNED32) 1248 const_iv (SNMP_UNSIGNED32)
940 const_iv (SNMP_TIMETICKS) 1249 const_iv (SNMP_TIMETICKS)
941 const_iv (SNMP_OPAQUE) 1250 const_iv (SNMP_OPAQUE)
942 const_iv (SNMP_COUNTER64) 1251 const_iv (SNMP_COUNTER64)
943 }; 1252 };
944 1253
945 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) 1254 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--)
946 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); 1255 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv));
947} 1256}
948 1257
949SV * 1258void
950ber_decode (SV *ber, SV *profile = &PL_sv_undef) 1259ber_decode (SV *ber, SV *profile = &PL_sv_undef)
1260 ALIAS:
1261 ber_decode_prefix = 1
951 CODE: 1262 PPCODE:
952{ 1263{
953 cur_profile = SvPROFILE (profile); 1264 cur_profile = SvPROFILE (profile);
954 STRLEN len; 1265 STRLEN len;
955 buf = SvPVbyte (ber, len); 1266 buf = (U8 *)SvPVbyte (ber, len);
956 cur = buf; 1267 cur = buf;
957 end = buf + len; 1268 end = buf + len;
958 1269
1270 PUTBACK;
959 RETVAL = decode_ber (); 1271 SV *tuple = decode_ber ();
1272 SPAGAIN;
1273
1274 EXTEND (SP, 2);
1275 PUSHs (sv_2mortal (tuple));
1276
1277 if (ix)
1278 PUSHs (sv_2mortal (newSViv (cur - buf)));
1279 else if (cur != end)
1280 error ("trailing garbage after BER value");
960} 1281}
961 OUTPUT: RETVAL
962 1282
963void 1283void
964ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *constructed = &PL_sv_undef, SV *data = &PL_sv_undef) 1284ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *flags = &PL_sv_undef, SV *data = &PL_sv_undef)
965 PPCODE: 1285 PPCODE:
966{ 1286{
967 if (!SvOK (tuple)) 1287 if (!SvOK (tuple))
968 XSRETURN_NO; 1288 XSRETURN_NO;
969 1289
971 croak ("ber_is: tuple must be BER tuple (array-ref)"); 1291 croak ("ber_is: tuple must be BER tuple (array-ref)");
972 1292
973 AV *av = (AV *)SvRV (tuple); 1293 AV *av = (AV *)SvRV (tuple);
974 1294
975 XPUSHs ( 1295 XPUSHs (
976 (!SvOK (klass) || SvIV (AvARRAY (av)[BER_CLASS ]) == SvIV (klass)) 1296 (!SvOK (klass) || SvIV (AvARRAY (av)[BER_CLASS]) == SvIV (klass))
977 && (!SvOK (tag) || SvIV (AvARRAY (av)[BER_TAG ]) == SvIV (tag)) 1297 && (!SvOK (tag) || SvIV (AvARRAY (av)[BER_TAG ]) == SvIV (tag))
978 && (!SvOK (constructed) || !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) == !SvIV (constructed)) 1298 && (!SvOK (flags) || !SvIV (AvARRAY (av)[BER_FLAGS]) == !SvIV (flags))
979 && (!SvOK (data) || sv_eq (AvARRAY (av)[BER_DATA ], data)) 1299 && (!SvOK (data) || sv_eq (AvARRAY (av)[BER_DATA ], data))
980 ? &PL_sv_yes : &PL_sv_undef); 1300 ? &PL_sv_yes : &PL_sv_undef);
981} 1301}
982 1302
983void 1303void
984ber_is_seq (SV *tuple) 1304ber_is_seq (SV *tuple)
988 XSRETURN_UNDEF; 1308 XSRETURN_UNDEF;
989 1309
990 AV *av = ber_tuple (tuple); 1310 AV *av = ber_tuple (tuple);
991 1311
992 XPUSHs ( 1312 XPUSHs (
993 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL 1313 SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL
994 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_SEQUENCE 1314 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_SEQUENCE
995 && SvIV (AvARRAY (av)[BER_CONSTRUCTED]) 1315 && SvIV (AvARRAY (av)[BER_FLAGS])
996 ? AvARRAY (av)[BER_DATA] : &PL_sv_undef); 1316 ? AvARRAY (av)[BER_DATA] : &PL_sv_undef);
997} 1317}
998 1318
999void 1319void
1000ber_is_i32 (SV *tuple, SV *value = &PL_sv_undef) 1320ber_is_int (SV *tuple, SV *value = &PL_sv_undef)
1001 PPCODE: 1321 PPCODE:
1002{ 1322{
1003 if (!SvOK (tuple)) 1323 if (!SvOK (tuple))
1004 XSRETURN_NO; 1324 XSRETURN_NO;
1005 1325
1006 AV *av = ber_tuple (tuple); 1326 AV *av = ber_tuple (tuple);
1007 1327
1008 IV data = SvIV (AvARRAY (av)[BER_DATA]); 1328 UV data = SvUV (AvARRAY (av)[BER_DATA]);
1009 1329
1010 XPUSHs ( 1330 XPUSHs (
1011 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL 1331 SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL
1012 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_INTEGER32 1332 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_INTEGER
1013 && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) 1333 && !SvIV (AvARRAY (av)[BER_FLAGS])
1014 && (!SvOK (value) || data == SvIV (value)) 1334 && (!SvOK (value) || data == SvUV (value))
1015 ? sv_2mortal (data ? newSViv (data) : newSVpv ("0 but true", 0)) 1335 ? sv_2mortal (data ? newSVsv (AvARRAY (av)[BER_DATA]) : newSVpv ("0 but true", 0))
1016 : &PL_sv_undef); 1336 : &PL_sv_undef);
1017} 1337}
1018 1338
1019void 1339void
1020ber_is_oid (SV *tuple, SV *oid = &PL_sv_undef) 1340ber_is_oid (SV *tuple, SV *oid = &PL_sv_undef)
1024 XSRETURN_NO; 1344 XSRETURN_NO;
1025 1345
1026 AV *av = ber_tuple (tuple); 1346 AV *av = ber_tuple (tuple);
1027 1347
1028 XPUSHs ( 1348 XPUSHs (
1029 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL 1349 SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL
1030 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_OBJECT_IDENTIFIER 1350 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_OBJECT_IDENTIFIER
1031 && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) 1351 && !SvIV (AvARRAY (av)[BER_FLAGS])
1032 && (!SvOK (oid) || sv_eq (AvARRAY (av)[BER_DATA], oid)) 1352 && (!SvOK (oid) || sv_eq (AvARRAY (av)[BER_DATA], oid))
1033 ? newSVsv (AvARRAY (av)[BER_DATA]) : &PL_sv_undef); 1353 ? newSVsv (AvARRAY (av)[BER_DATA]) : &PL_sv_undef);
1034} 1354}
1035 1355
1036############################################################################# 1356#############################################################################
1042 cur_profile = SvPROFILE (profile); 1362 cur_profile = SvPROFILE (profile);
1043 buf_sv = sv_2mortal (NEWSV (0, 256)); 1363 buf_sv = sv_2mortal (NEWSV (0, 256));
1044 SvPOK_only (buf_sv); 1364 SvPOK_only (buf_sv);
1045 set_buf (buf_sv); 1365 set_buf (buf_sv);
1046 1366
1367 PUTBACK;
1047 encode_ber (tuple); 1368 encode_ber (tuple);
1369 SPAGAIN;
1048 1370
1049 SvCUR_set (buf_sv, cur - buf); 1371 SvCUR_set (buf_sv, cur - buf);
1050 XPUSHs (buf_sv); 1372 XPUSHs (buf_sv);
1051} 1373}
1052 1374
1053SV * 1375SV *
1054ber_i32 (IV iv) 1376ber_int (SV *sv)
1055 CODE: 1377 CODE:
1056{ 1378{
1057 AV *av = newAV (); 1379 AV *av = newAV ();
1058 av_fill (av, BER_ARRAYSIZE - 1); 1380 av_fill (av, BER_ARRAYSIZE - 1);
1059 AvARRAY (av)[BER_CLASS ] = newSVcacheint (ASN_UNIVERSAL); 1381 AvARRAY (av)[BER_CLASS] = newSVcacheint (ASN_UNIVERSAL);
1060 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_INTEGER32); 1382 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_INTEGER);
1061 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (0); 1383 AvARRAY (av)[BER_FLAGS] = newSVcacheint (0);
1062 AvARRAY (av)[BER_DATA ] = newSViv (iv); 1384 AvARRAY (av)[BER_DATA ] = newSVsv (sv);
1063 RETVAL = newRV_noinc ((SV *)av); 1385 RETVAL = newRV_noinc ((SV *)av);
1064} 1386}
1065 OUTPUT: RETVAL 1387 OUTPUT: RETVAL
1066 1388
1067# TODO: not arrayref, but elements? 1389# TODO: not arrayref, but elements?
1069ber_seq (SV *arrayref) 1391ber_seq (SV *arrayref)
1070 CODE: 1392 CODE:
1071{ 1393{
1072 AV *av = newAV (); 1394 AV *av = newAV ();
1073 av_fill (av, BER_ARRAYSIZE - 1); 1395 av_fill (av, BER_ARRAYSIZE - 1);
1074 AvARRAY (av)[BER_CLASS ] = newSVcacheint (ASN_UNIVERSAL); 1396 AvARRAY (av)[BER_CLASS] = newSVcacheint (ASN_UNIVERSAL);
1075 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_SEQUENCE); 1397 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_SEQUENCE);
1076 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (1); 1398 AvARRAY (av)[BER_FLAGS] = newSVcacheint (1);
1077 AvARRAY (av)[BER_DATA ] = newSVsv (arrayref); 1399 AvARRAY (av)[BER_DATA ] = newSVsv (arrayref);
1078 RETVAL = newRV_noinc ((SV *)av); 1400 RETVAL = newRV_noinc ((SV *)av);
1079} 1401}
1080 OUTPUT: RETVAL 1402 OUTPUT: RETVAL
1081 1403
1082MODULE = Convert::BER::XS PACKAGE = Convert::BER::XS::Profile 1404MODULE = Convert::BER::XS PACKAGE = Convert::BER::XS::Profile

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines