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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines