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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines