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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines