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.19 by root, Sat Apr 20 16:34:34 2019 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines