… | |
… | |
35 | ASN_VISIBLE_STRING = 0x1a, //X |
35 | ASN_VISIBLE_STRING = 0x1a, //X |
36 | ASN_ISO646_STRING = 0x1a, //X |
36 | ASN_ISO646_STRING = 0x1a, //X |
37 | ASN_GENERAL_STRING = 0x1b, //X |
37 | ASN_GENERAL_STRING = 0x1b, //X |
38 | ASN_UNIVERSAL_STRING = 0x1c, //X |
38 | ASN_UNIVERSAL_STRING = 0x1c, //X |
39 | ASN_CHARACTER_STRING = 0x1d, //X |
39 | ASN_CHARACTER_STRING = 0x1d, //X |
40 | ASN_BMPSTRING = 0x1e, //X |
40 | ASN_BMP_STRING = 0x1e, //X |
41 | |
41 | |
42 | ASN_TAG_BER = 0x1f, |
42 | ASN_TAG_BER = 0x1f, |
43 | ASN_TAG_MASK = 0x1f, |
43 | ASN_TAG_MASK = 0x1f, |
44 | |
44 | |
45 | // primitive/constructed |
45 | // primitive/constructed |
46 | ASN_CONSTRUCTED = 0x20, |
46 | ASN_CONSTRUCTED = 0x20, |
47 | |
47 | |
48 | // ASN_CLASS |
48 | // ASN_CLASS |
49 | ASN_UNIVERSAL = 0x00, |
49 | ASN_UNIVERSAL = 0x00, |
50 | ASN_APPLICATION = 0x40, |
50 | ASN_APPLICATION = 0x01, |
51 | ASN_CONTEXT = 0x80, |
51 | ASN_CONTEXT = 0x02, |
52 | ASN_PRIVATE = 0xc0, |
52 | ASN_PRIVATE = 0x03, |
53 | |
53 | |
54 | ASN_CLASS_MASK = 0xc0, |
54 | ASN_CLASS_MASK = 0xc0, |
55 | ASN_CLASS_SHIFT = 6, |
55 | ASN_CLASS_SHIFT = 6, |
56 | |
56 | |
57 | // ASN_APPLICATION SNMP |
57 | // ASN_APPLICATION SNMP |
… | |
… | |
62 | SNMP_OPAQUE = 0x04, |
62 | SNMP_OPAQUE = 0x04, |
63 | SNMP_COUNTER64 = 0x06, |
63 | SNMP_COUNTER64 = 0x06, |
64 | }; |
64 | }; |
65 | |
65 | |
66 | enum { |
66 | enum { |
|
|
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_CROAK, |
|
|
78 | }; |
|
|
79 | |
|
|
80 | enum { |
67 | BER_CLASS = 0, |
81 | BER_CLASS = 0, |
68 | BER_TAG = 1, |
82 | BER_TAG = 1, |
69 | BER_CONSTRUCTED = 2, |
83 | BER_CONSTRUCTED = 2, |
70 | BER_DATA = 3, |
84 | BER_DATA = 3, |
71 | BER_ARRAYSIZE |
85 | BER_ARRAYSIZE |
72 | }; |
86 | }; |
73 | |
87 | |
74 | #define MAX_OID_STRLEN 4096 |
88 | #define MAX_OID_STRLEN 4096 |
75 | |
89 | |
|
|
90 | typedef void profile_type; |
|
|
91 | |
|
|
92 | static profile_type *cur_profile, *default_profile; |
76 | static SV *buf_sv; // encoding buffer |
93 | static SV *buf_sv; // encoding buffer |
77 | static U8 *buf, *cur, *end; // buffer start, current, end |
94 | static U8 *buf, *cur, *end; // buffer start, current, end |
78 | |
95 | |
79 | #if __GNUC__ >= 3 |
96 | #if __GNUC__ >= 3 |
80 | # define expect(expr,value) __builtin_expect ((expr), (value)) |
97 | # define expect(expr,value) __builtin_expect ((expr), (value)) |
… | |
… | |
85 | #endif |
102 | #endif |
86 | |
103 | |
87 | #define expect_false(expr) expect ((expr) != 0, 0) |
104 | #define expect_false(expr) expect ((expr) != 0, 0) |
88 | #define expect_true(expr) expect ((expr) != 0, 1) |
105 | #define expect_true(expr) expect ((expr) != 0, 1) |
89 | |
106 | |
|
|
107 | ///////////////////////////////////////////////////////////////////////////// |
|
|
108 | |
|
|
109 | static SV *sviv_cache[32]; |
|
|
110 | |
90 | // for "small" integers, return a readonly sv, otherwise create a new one |
111 | // for "small" integers, return a readonly sv, otherwise create a new one |
91 | static SV *newSVcacheint (int val) |
112 | static SV *newSVcacheint (int val) |
92 | { |
113 | { |
93 | static SV *cache[32]; |
|
|
94 | |
|
|
95 | if (expect_false (val < 0 || val >= sizeof (cache))) |
114 | if (expect_false (val < 0 || val >= sizeof (sviv_cache))) |
96 | return newSViv (val); |
115 | return newSViv (val); |
97 | |
116 | |
98 | if (expect_false (!cache [val])) |
117 | if (expect_false (!sviv_cache [val])) |
99 | { |
118 | { |
100 | cache [val] = newSVuv (val); |
119 | sviv_cache [val] = newSVuv (val); |
101 | SvREADONLY_on (cache [val]); |
120 | SvREADONLY_on (sviv_cache [val]); |
102 | } |
121 | } |
103 | |
122 | |
104 | return SvREFCNT_inc_NN (cache [val]); |
123 | return SvREFCNT_inc_NN (sviv_cache [val]); |
|
|
124 | } |
|
|
125 | |
|
|
126 | ///////////////////////////////////////////////////////////////////////////// |
|
|
127 | |
|
|
128 | static HV *profile_stash; |
|
|
129 | |
|
|
130 | static profile_type * |
|
|
131 | SvPROFILE (SV *profile) |
|
|
132 | { |
|
|
133 | if (!SvOK (profile)) |
|
|
134 | profile = default_profile; |
|
|
135 | |
|
|
136 | if (!SvROK (profile)) |
|
|
137 | croak ("invalid profile"); |
|
|
138 | |
|
|
139 | profile = SvRV (profile); |
|
|
140 | |
|
|
141 | if (SvSTASH (profile) != profile_stash) |
|
|
142 | croak ("invalid profile object"); |
|
|
143 | |
|
|
144 | return (void *)profile; |
|
|
145 | } |
|
|
146 | |
|
|
147 | static int |
|
|
148 | profile_lookup (profile_type *profile, int klass, int tag) |
|
|
149 | { |
|
|
150 | SV *sv = (SV *)profile; |
|
|
151 | U32 idx = (tag << 2) + klass; |
|
|
152 | |
|
|
153 | if (expect_false (idx >= SvCUR (sv))) |
|
|
154 | return BER_TYPE_BYTES; |
|
|
155 | |
|
|
156 | return SvPVX (sv)[idx]; |
|
|
157 | } |
|
|
158 | |
|
|
159 | static int |
|
|
160 | profile_set (profile_type *profile, int klass, int tag, int type) |
|
|
161 | { |
|
|
162 | SV *sv = (SV *)profile; |
|
|
163 | U32 idx = (tag << 2) + klass; |
|
|
164 | STRLEN oldlen = SvCUR (sv); |
|
|
165 | STRLEN newlen = idx + 2; |
|
|
166 | |
|
|
167 | if (idx >= oldlen) |
|
|
168 | { |
|
|
169 | sv_grow (sv, newlen); |
|
|
170 | memset (SvPVX (sv) + oldlen, BER_TYPE_BYTES, newlen - oldlen); |
|
|
171 | SvCUR_set (sv, newlen); |
|
|
172 | } |
|
|
173 | |
|
|
174 | SvPVX (sv)[idx] = type; |
|
|
175 | } |
|
|
176 | |
|
|
177 | static SV * |
|
|
178 | profile_new () |
|
|
179 | { |
|
|
180 | SV *sv = newSVpvn ("", 0); |
|
|
181 | |
|
|
182 | static const struct { |
|
|
183 | int klass; |
|
|
184 | int tag; |
|
|
185 | int type; |
|
|
186 | } *celem, default_map[] = { |
|
|
187 | { ASN_UNIVERSAL, ASN_BOOLEAN , BER_TYPE_BOOL }, |
|
|
188 | { ASN_UNIVERSAL, ASN_INTEGER32 , BER_TYPE_INT }, |
|
|
189 | { ASN_UNIVERSAL, ASN_NULL , BER_TYPE_NULL }, |
|
|
190 | { ASN_UNIVERSAL, ASN_OBJECT_IDENTIFIER, BER_TYPE_OID }, |
|
|
191 | { ASN_UNIVERSAL, ASN_OBJECT_DESCRIPTOR, BER_TYPE_OID }, |
|
|
192 | { ASN_UNIVERSAL, ASN_RELATIVE_OID , BER_TYPE_RELOID }, |
|
|
193 | { ASN_UNIVERSAL, ASN_REAL , BER_TYPE_REAL }, |
|
|
194 | { ASN_UNIVERSAL, ASN_UTF8_STRING , BER_TYPE_UTF8 }, |
|
|
195 | { ASN_UNIVERSAL, ASN_BMP_STRING , BER_TYPE_UCS2 }, |
|
|
196 | { ASN_UNIVERSAL, ASN_UNIVERSAL_STRING , BER_TYPE_UCS4 }, |
|
|
197 | }; |
|
|
198 | |
|
|
199 | //const_iv (SNMP_IPADDRESS) |
|
|
200 | //const_iv (SNMP_COUNTER32) |
|
|
201 | //const_iv (SNMP_UNSIGNED32) |
|
|
202 | //const_iv (SNMP_TIMETICKS) |
|
|
203 | //const_iv (SNMP_OPAQUE) |
|
|
204 | //const_iv (SNMP_COUNTER64) |
|
|
205 | |
|
|
206 | for (celem = default_map + sizeof (default_map) / sizeof (default_map [0]); celem > default_map; celem--) |
|
|
207 | profile_set ((void *)sv, celem->klass, celem->tag, celem->type); |
|
|
208 | |
|
|
209 | return sv_bless (newRV_noinc (sv), profile_stash); |
105 | } |
210 | } |
106 | |
211 | |
107 | ///////////////////////////////////////////////////////////////////////////// |
212 | ///////////////////////////////////////////////////////////////////////////// |
108 | // decoder |
213 | // decoder |
109 | |
214 | |
… | |
… | |
187 | } |
292 | } |
188 | |
293 | |
189 | return res; |
294 | return res; |
190 | } |
295 | } |
191 | |
296 | |
192 | static U32 |
297 | static SV * |
193 | get_integer32 (void) |
298 | decode_int () |
194 | { |
299 | { |
195 | U32 length = get_length (); |
300 | int len = get_length (); |
196 | |
301 | |
197 | if (length <= 0) |
302 | if (len <= 0) |
198 | { |
303 | { |
199 | error ("INTEGER32 length equal to zero"); |
304 | error ("integer length equal to zero"); |
200 | return 0; |
305 | return 0; |
201 | } |
306 | } |
202 | |
307 | |
203 | U8 *data = get_n (length); |
308 | U8 *data = get_n (len); |
204 | |
309 | |
205 | if (length > 5 || (length > 4 && data [0])) |
310 | int negative = data [0] & 0x80; |
206 | { |
|
|
207 | error ("INTEGER32 length too long"); |
|
|
208 | return 0; |
|
|
209 | } |
|
|
210 | |
311 | |
211 | U32 res = data [0] & 0x80 ? 0xffffffff : 0; |
312 | UV val = negative ? -1 : 0; // copy signbit to all bits |
212 | |
313 | |
213 | while (length--) |
314 | do |
214 | res = (res << 8) | *data++; |
315 | val = (val << 8) | *data++; |
|
|
316 | while (--len); |
215 | |
317 | |
216 | return res; |
318 | // the cats to IV relies on implementation-defined behaviour (two's complement cast) |
|
|
319 | // but that's ok, as perl relies on it as well. |
|
|
320 | return negative ? newSViv ((IV)val) : newSVuv (val); |
217 | } |
321 | } |
218 | |
322 | |
219 | static SV * |
323 | static SV * |
220 | decode_integer32 (void) |
324 | decode_data (void) |
221 | { |
325 | { |
222 | return newSViv ((I32)get_integer32 ()); |
|
|
223 | } |
|
|
224 | |
|
|
225 | static SV * |
|
|
226 | decode_unsigned32 (void) |
|
|
227 | { |
|
|
228 | return newSVuv ((U32)get_integer32 ()); |
|
|
229 | } |
|
|
230 | |
|
|
231 | #if IVSIZE >= 8 |
|
|
232 | |
|
|
233 | static U64TYPE |
|
|
234 | get_integer64 (void) |
|
|
235 | { |
|
|
236 | U32 length = get_length (); |
326 | U32 len = get_length (); |
237 | |
|
|
238 | if (length <= 0) |
|
|
239 | { |
|
|
240 | error ("INTEGER64 length equal to zero"); |
|
|
241 | return 0; |
|
|
242 | } |
|
|
243 | |
|
|
244 | U8 *data = get_n (length); |
327 | U8 *data = get_n (len); |
245 | |
|
|
246 | if (length > 9 || (length > 8 && data [0])) |
|
|
247 | { |
|
|
248 | error ("INTEGER64 length too long"); |
|
|
249 | return 0; |
|
|
250 | } |
|
|
251 | |
|
|
252 | U64TYPE res = data [0] & 0x80 ? 0xffffffffffffffff : 0; |
|
|
253 | |
|
|
254 | while (length--) |
|
|
255 | res = (res << 8) | *data++; |
|
|
256 | |
|
|
257 | return res; |
|
|
258 | } |
|
|
259 | |
|
|
260 | static SV * |
|
|
261 | decode_integer64 (void) |
|
|
262 | { |
|
|
263 | return newSViv ((I64TYPE)get_integer64 ()); |
|
|
264 | } |
|
|
265 | |
|
|
266 | static SV * |
|
|
267 | decode_unsigned64 (void) |
|
|
268 | { |
|
|
269 | return newSVuv ((U64TYPE)get_integer64 ()); |
|
|
270 | } |
|
|
271 | |
|
|
272 | #endif |
|
|
273 | |
|
|
274 | static SV * |
|
|
275 | decode_octet_string (void) |
|
|
276 | { |
|
|
277 | U32 length = get_length (); |
|
|
278 | U8 *data = get_n (length); |
|
|
279 | return newSVpvn (data, length); |
328 | return newSVpvn ((char *)data, len); |
280 | } |
329 | } |
281 | |
330 | |
282 | // gelper for decode_object_identifier |
331 | // gelper for decode_object_identifier |
283 | static char * |
332 | static char * |
284 | write_uv (char *buf, U32 u) |
333 | write_uv (char *buf, U32 u) |
285 | { |
334 | { |
286 | // the one-digit case is absolutely predominant, so this pays off (hopefully) |
335 | // the one-digit case is absolutely predominant, so this pays off (hopefully) |
287 | if (u < 10) |
336 | if (expect_true (u < 10)) |
288 | *buf++ = u + '0'; |
337 | *buf++ = u + '0'; |
289 | else |
338 | else |
290 | { |
339 | { |
291 | char *beg = buf; |
340 | char *beg = buf; |
292 | |
341 | |
… | |
… | |
308 | |
357 | |
309 | return buf; |
358 | return buf; |
310 | } |
359 | } |
311 | |
360 | |
312 | static SV * |
361 | static SV * |
313 | decode_object_identifier (void) |
362 | decode_oid (int relative) |
314 | { |
363 | { |
315 | U32 length = get_length (); |
364 | U32 len = get_length (); |
316 | |
365 | |
317 | if (length <= 0) |
366 | if (len <= 0) |
318 | { |
367 | { |
319 | error ("OBJECT IDENTIFIER length equal to zero"); |
368 | error ("OBJECT IDENTIFIER length equal to zero"); |
320 | return &PL_sv_undef; |
369 | return &PL_sv_undef; |
321 | } |
370 | } |
322 | |
371 | |
323 | U8 *end = cur + length; |
372 | U8 *end = cur + len; |
324 | U32 w = get_w (); |
373 | U32 w = get_w (); |
325 | |
374 | |
326 | static char oid[MAX_OID_STRLEN]; // must be static |
375 | static char oid[MAX_OID_STRLEN]; // must be static |
327 | char *app = oid; |
376 | char *app = oid; |
328 | |
377 | |
|
|
378 | if (relative) |
|
|
379 | app = write_uv (app, w); |
|
|
380 | else |
|
|
381 | { |
329 | app = write_uv (app, (U8)w / 40); |
382 | app = write_uv (app, (U8)w / 40); |
330 | *app++ = '.'; |
383 | *app++ = '.'; |
331 | app = write_uv (app, (U8)w % 40); |
384 | app = write_uv (app, (U8)w % 40); |
|
|
385 | } |
332 | |
386 | |
333 | // we assume an oid component is never > 64 bytes |
387 | // we assume an oid component is never > 64 bytes |
334 | while (cur < end && oid + sizeof (oid) - app > 64) |
388 | while (cur < end && oid + sizeof (oid) - app > 64) |
335 | { |
389 | { |
336 | w = get_w (); |
390 | w = get_w (); |
… | |
… | |
346 | { |
400 | { |
347 | int identifier = get_u8 (); |
401 | int identifier = get_u8 (); |
348 | |
402 | |
349 | SV *res; |
403 | SV *res; |
350 | |
404 | |
351 | int constructed = identifier & ASN_CONSTRUCTED; |
405 | int constructed = identifier & ASN_CONSTRUCTED; |
352 | int klass = identifier & ASN_CLASS_MASK; |
406 | int klass = (identifier & ASN_CLASS_MASK) >> ASN_CLASS_SHIFT; |
353 | int tag = identifier & ASN_TAG_MASK; |
407 | int tag = identifier & ASN_TAG_MASK; |
354 | |
408 | |
355 | if (tag == ASN_TAG_BER) |
409 | if (tag == ASN_TAG_BER) |
356 | tag = get_w (); |
410 | tag = get_w (); |
357 | |
411 | |
358 | if (tag == ASN_TAG_BER) |
412 | if (tag == ASN_TAG_BER) |
… | |
… | |
371 | croak ("constructed type %02x overflow (%x %x)\n", identifier, cur - buf, seqend); |
425 | croak ("constructed type %02x overflow (%x %x)\n", identifier, cur - buf, seqend); |
372 | |
426 | |
373 | res = newRV_inc ((SV *)av); |
427 | res = newRV_inc ((SV *)av); |
374 | } |
428 | } |
375 | else |
429 | else |
376 | switch (identifier) |
430 | switch (profile_lookup (cur_profile, klass, tag)) |
377 | { |
431 | { |
378 | case ASN_NULL: |
432 | case BER_TYPE_NULL: |
379 | res = &PL_sv_undef; |
433 | res = &PL_sv_undef; |
380 | break; |
434 | break; |
381 | |
435 | |
382 | case ASN_OBJECT_IDENTIFIER: |
436 | case BER_TYPE_BOOL: |
|
|
437 | { |
|
|
438 | U32 len = get_length (); |
|
|
439 | |
|
|
440 | if (len != 1) |
|
|
441 | croak ("BOOLEAN type with invalid length %d encountered", len); |
|
|
442 | |
|
|
443 | res = newSVcacheint (get_u8 () ? 0 : 1); |
|
|
444 | } |
|
|
445 | break; |
|
|
446 | |
|
|
447 | case BER_TYPE_OID: |
383 | res = decode_object_identifier (); |
448 | res = decode_oid (0); |
384 | break; |
449 | break; |
385 | |
450 | |
386 | case ASN_INTEGER32: |
451 | case BER_TYPE_RELOID: |
|
|
452 | res = decode_oid (1); |
|
|
453 | break; |
|
|
454 | |
|
|
455 | case BER_TYPE_INT: |
387 | res = decode_integer32 (); |
456 | res = decode_int (); |
388 | break; |
457 | break; |
389 | |
458 | |
390 | case ASN_APPLICATION | SNMP_UNSIGNED32: |
459 | case BER_TYPE_UTF8: |
391 | case ASN_APPLICATION | SNMP_COUNTER32: |
|
|
392 | case ASN_APPLICATION | SNMP_TIMETICKS: |
|
|
393 | res = decode_unsigned32 (); |
|
|
394 | break; |
|
|
395 | |
|
|
396 | #if 0 // handled by default case |
|
|
397 | case ASN_OCTET_STRING: |
|
|
398 | case ASN_APPLICATION | ASN_IPADDRESS: |
|
|
399 | case ASN_APPLICATION | ASN_OPAQUE: |
|
|
400 | res = decode_octet_string (); |
|
|
401 | break; |
|
|
402 | #endif |
|
|
403 | |
|
|
404 | case ASN_APPLICATION | SNMP_COUNTER64: |
|
|
405 | res = decode_integer64 (); |
460 | res = decode_data (); |
|
|
461 | SvUTF8_on (res); |
406 | break; |
462 | break; |
407 | |
463 | |
|
|
464 | case BER_TYPE_BYTES: |
|
|
465 | res = decode_data (); |
|
|
466 | break; |
|
|
467 | |
|
|
468 | case BER_TYPE_REAL: |
|
|
469 | case BER_TYPE_UCS2: |
|
|
470 | case BER_TYPE_UCS4: |
|
|
471 | case BER_TYPE_CROAK: |
408 | default: |
472 | default: |
409 | res = decode_octet_string (); |
473 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
410 | break; |
|
|
411 | } |
474 | } |
412 | |
475 | |
413 | AV *av = newAV (); |
476 | AV *av = newAV (); |
414 | av_fill (av, BER_ARRAYSIZE - 1); |
477 | av_fill (av, BER_ARRAYSIZE - 1); |
415 | AvARRAY (av)[BER_CLASS ] = newSVcacheint (klass >> ASN_CLASS_SHIFT); |
478 | AvARRAY (av)[BER_CLASS ] = newSVcacheint (klass); |
416 | AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); |
479 | AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); |
417 | AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (constructed ? 1 : 0); |
480 | AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (constructed ? 1 : 0); |
418 | AvARRAY (av)[BER_DATA ] = res; |
481 | AvARRAY (av)[BER_DATA ] = res; |
419 | |
482 | |
420 | return newRV_noinc ((SV *)av); |
483 | return newRV_noinc ((SV *)av); |
… | |
… | |
529 | ? 1 |
592 | ? 1 |
530 | : 2 + (val > 0xffU) + (val > 0xffffU) + (val > 0xffffffU); |
593 | : 2 + (val > 0xffU) + (val > 0xffffU) + (val > 0xffffffU); |
531 | } |
594 | } |
532 | |
595 | |
533 | static void |
596 | static void |
534 | encode_octet_string (SV *sv) |
597 | encode_data (const char *ptr, STRLEN len) |
535 | { |
598 | { |
536 | STRLEN len; |
|
|
537 | char *ptr = SvPVbyte (sv, len); |
|
|
538 | |
|
|
539 | put_length (len); |
599 | put_length (len); |
540 | need (len); |
600 | need (len); |
541 | memcpy (cur, ptr, len); |
601 | memcpy (cur, ptr, len); |
542 | cur += len; |
602 | cur += len; |
543 | } |
603 | } |
544 | |
604 | |
545 | static void |
605 | static void |
546 | encode_integer32 (IV iv) |
606 | encode_uv (UV uv) |
547 | { |
607 | { |
548 | need (5); |
608 | } |
|
|
609 | |
|
|
610 | static void |
|
|
611 | encode_int (SV *sv) |
|
|
612 | { |
|
|
613 | need (8 + 1 + 1); // 64 bit + length + extra 0 |
|
|
614 | |
|
|
615 | if (expect_false (!SvIOK (sv))) |
|
|
616 | sv_2iv_flags (sv, 0); |
549 | |
617 | |
550 | U8 *lenb = cur++; |
618 | U8 *lenb = cur++; |
551 | |
619 | |
552 | if (iv < 0) |
620 | if (SvIOK_notUV (sv)) |
553 | { |
621 | { |
|
|
622 | IV iv = SvIVX (sv); |
|
|
623 | |
|
|
624 | if (expect_false (iv < 0)) |
|
|
625 | { |
554 | // get two's complement bit pattern - works even on hypthetical non-2c machines |
626 | // get two's complement bit pattern - works even on hypothetical non-2c machines |
555 | U32 uv = iv; |
627 | UV uv = iv; |
556 | |
628 | |
|
|
629 | #if UVSIZE > 4 |
|
|
630 | *cur = uv >> 56; cur += !!(~uv & 0xff80000000000000U); |
|
|
631 | *cur = uv >> 48; cur += !!(~uv & 0xffff800000000000U); |
|
|
632 | *cur = uv >> 40; cur += !!(~uv & 0xffffff8000000000U); |
|
|
633 | *cur = uv >> 32; cur += !!(~uv & 0xffffffff80000000U); |
|
|
634 | #endif |
557 | *cur = uv >> 24; cur += !!(~uv & 0xff800000U); |
635 | *cur = uv >> 24; cur += !!(~uv & 0xffffffffff800000U); |
558 | *cur = uv >> 16; cur += !!(~uv & 0xffff8000U); |
636 | *cur = uv >> 16; cur += !!(~uv & 0xffffffffffff8000U); |
559 | *cur = uv >> 8; cur += !!(~uv & 0xffffff80U); |
637 | *cur = uv >> 8; cur += !!(~uv & 0xffffffffffffff80U); |
560 | *cur = uv ; cur += 1; |
638 | *cur = uv ; cur += 1; |
561 | } |
|
|
562 | else |
|
|
563 | { |
|
|
564 | *cur = iv >> 24; cur += *cur > 0; |
|
|
565 | *cur = iv >> 16; cur += *cur > 0; |
|
|
566 | *cur = iv >> 8; cur += *cur > 0; |
|
|
567 | *cur = iv ; cur += 1; |
|
|
568 | } |
|
|
569 | |
639 | |
570 | *lenb = cur - lenb - 1; |
640 | *lenb = cur - lenb - 1; |
571 | } |
|
|
572 | |
641 | |
573 | static void |
642 | return; |
574 | encode_unsigned64 (U64TYPE uv) |
643 | } |
575 | { |
644 | } |
576 | need (9); |
|
|
577 | |
645 | |
578 | U8 *lenb = cur++; |
646 | UV uv = SvUV (sv); |
579 | |
647 | |
580 | *cur = uv >> 56; cur += *cur > 0; |
648 | // prepend an extra 0 if the high bit is 1 |
581 | *cur = uv >> 48; cur += *cur > 0; |
649 | *cur = 0; cur += !!(uv & ((UV)1 << (UVSIZE * 8 - 1))); |
582 | *cur = uv >> 40; cur += *cur > 0; |
650 | |
583 | *cur = uv >> 32; cur += *cur > 0; |
651 | #if UVSIZE > 4 |
584 | *cur = uv >> 24; cur += *cur > 0; |
652 | *cur = uv >> 56; cur += !!(uv & 0xff80000000000000U); |
585 | *cur = uv >> 16; cur += *cur > 0; |
653 | *cur = uv >> 48; cur += !!(uv & 0xffff800000000000U); |
586 | *cur = uv >> 8; cur += *cur > 0; |
654 | *cur = uv >> 40; cur += !!(uv & 0xffffff8000000000U); |
|
|
655 | *cur = uv >> 32; cur += !!(uv & 0xffffffff80000000U); |
|
|
656 | #endif |
|
|
657 | *cur = uv >> 24; cur += !!(uv & 0xffffffffff800000U); |
|
|
658 | *cur = uv >> 16; cur += !!(uv & 0xffffffffffff8000U); |
|
|
659 | *cur = uv >> 8; cur += !!(uv & 0xffffffffffffff80U); |
587 | *cur = uv ; cur += 1; |
660 | *cur = uv ; cur += 1; |
588 | |
661 | |
589 | *lenb = cur - lenb - 1; |
662 | *lenb = cur - lenb - 1; |
590 | } |
663 | } |
591 | |
664 | |
… | |
… | |
631 | |
704 | |
632 | return str; |
705 | return str; |
633 | } |
706 | } |
634 | |
707 | |
635 | static void |
708 | static void |
636 | encode_object_identifier (SV *oid) |
709 | encode_oid (SV *oid, int relative) |
637 | { |
710 | { |
638 | STRLEN slen; |
711 | STRLEN len; |
639 | char *ptr = SvPV (oid, slen); // utf8 vs. bytes does not matter |
712 | char *ptr = SvPV (oid, len); // utf8 vs. bytes does not matter |
640 | |
713 | |
641 | // we need at most as many octets as the string form |
714 | // we need at most as many octets as the string form |
642 | need (slen + 1); |
715 | need (len + 1); |
643 | STRLEN mark = len_fixup_mark (); |
716 | STRLEN mark = len_fixup_mark (); |
644 | |
717 | |
645 | UV w1, w2; |
718 | UV w1, w2; |
646 | |
719 | |
|
|
720 | if (!relative) |
|
|
721 | { |
647 | ptr = read_uv (ptr, &w1); |
722 | ptr = read_uv (ptr, &w1); |
648 | ptr = read_uv (ptr, &w2); |
723 | ptr = read_uv (ptr, &w2); |
649 | |
724 | |
650 | put_w_nocheck (w1 * 40 + w2); |
725 | put_w_nocheck (w1 * 40 + w2); |
|
|
726 | } |
651 | |
727 | |
652 | while (*ptr) |
728 | while (*ptr) |
653 | { |
729 | { |
654 | ptr = read_uv (ptr, &w1); |
730 | ptr = read_uv (ptr, &w1); |
655 | put_w_nocheck (w1); |
731 | put_w_nocheck (w1); |
656 | } |
732 | } |
657 | |
733 | |
658 | len_fixup (mark); |
734 | len_fixup (mark); |
659 | } |
735 | } |
660 | |
736 | |
661 | // checkl whether an SV is a BER tuple and returns its AV * |
737 | // check whether an SV is a BER tuple and returns its AV * |
662 | static AV * |
738 | static AV * |
663 | ber_tuple (SV *tuple) |
739 | ber_tuple (SV *tuple) |
664 | { |
740 | { |
665 | SV *rv; |
741 | SV *rv; |
666 | |
742 | |
… | |
… | |
716 | encode_ber (AvARRAY (av)[i]); |
792 | encode_ber (AvARRAY (av)[i]); |
717 | |
793 | |
718 | len_fixup (mark); |
794 | len_fixup (mark); |
719 | } |
795 | } |
720 | else |
796 | else |
721 | switch (identifier | tag) |
797 | switch (profile_lookup (cur_profile, klass, tag)) |
722 | { |
798 | { |
723 | case ASN_NULL: |
799 | case BER_TYPE_NULL: |
724 | put_length (0); |
800 | put_length (0); |
725 | break; |
801 | break; |
726 | |
802 | |
727 | case ASN_OBJECT_IDENTIFIER: |
803 | case BER_TYPE_BOOL: |
|
|
804 | put_length (1); |
|
|
805 | put_u8 (SvTRUE (data) ? 0xff : 0x00); |
|
|
806 | break; |
|
|
807 | |
|
|
808 | case BER_TYPE_OID: |
728 | encode_object_identifier (data); |
809 | encode_oid (data, 0); |
729 | break; |
810 | break; |
730 | |
811 | |
731 | case ASN_INTEGER32: |
812 | case BER_TYPE_RELOID: |
|
|
813 | encode_oid (data, 1); |
|
|
814 | break; |
|
|
815 | |
|
|
816 | case BER_TYPE_INT: |
732 | encode_integer32 (SvIV (data)); |
817 | encode_int (data); |
733 | break; |
818 | break; |
734 | |
819 | |
735 | case ASN_APPLICATION | SNMP_UNSIGNED32: |
820 | case BER_TYPE_BYTES: |
736 | case ASN_APPLICATION | SNMP_COUNTER32: |
821 | { |
737 | case ASN_APPLICATION | SNMP_TIMETICKS: |
822 | STRLEN len; |
738 | case ASN_APPLICATION | SNMP_COUNTER64: |
823 | const char *ptr = SvPVbyte (data, len); |
739 | encode_unsigned64 (SvUV (data)); |
824 | encode_data (ptr, len); |
|
|
825 | } |
740 | break; |
826 | break; |
741 | |
827 | |
|
|
828 | case BER_TYPE_UTF8: |
|
|
829 | { |
|
|
830 | STRLEN len; |
|
|
831 | const char *ptr = SvPVutf8 (data, len); |
|
|
832 | encode_data (ptr, len); |
|
|
833 | } |
|
|
834 | break; |
|
|
835 | |
|
|
836 | case BER_TYPE_REAL: |
|
|
837 | case BER_TYPE_UCS2: |
|
|
838 | case BER_TYPE_UCS4: |
|
|
839 | case BER_TYPE_CROAK: |
742 | default: |
840 | default: |
743 | encode_octet_string (data); |
841 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
744 | break; |
|
|
745 | } |
842 | } |
746 | |
843 | |
747 | } |
844 | } |
748 | |
845 | |
749 | ///////////////////////////////////////////////////////////////////////////// |
846 | ///////////////////////////////////////////////////////////////////////////// |
… | |
… | |
753 | PROTOTYPES: ENABLE |
850 | PROTOTYPES: ENABLE |
754 | |
851 | |
755 | BOOT: |
852 | BOOT: |
756 | { |
853 | { |
757 | HV *stash = gv_stashpv ("Convert::BER::XS", 1); |
854 | HV *stash = gv_stashpv ("Convert::BER::XS", 1); |
|
|
855 | |
|
|
856 | profile_stash = gv_stashpv ("Convert::BER::XS::Profile", 1); |
758 | |
857 | |
759 | static const struct { |
858 | static const struct { |
760 | const char *name; |
859 | const char *name; |
761 | IV iv; |
860 | IV iv; |
762 | } *civ, const_iv[] = { |
861 | } *civ, const_iv[] = { |
763 | { "ASN_BOOLEAN", ASN_BOOLEAN }, |
862 | #define const_iv(name) { # name, name }, |
764 | { "ASN_INTEGER32", ASN_INTEGER32 }, |
863 | const_iv (ASN_BOOLEAN) |
765 | { "ASN_BIT_STRING", ASN_BIT_STRING }, |
864 | const_iv (ASN_INTEGER32) |
766 | { "ASN_OCTET_STRING", ASN_OCTET_STRING }, |
865 | const_iv (ASN_BIT_STRING) |
767 | { "ASN_NULL", ASN_NULL }, |
866 | const_iv (ASN_OCTET_STRING) |
768 | { "ASN_OBJECT_IDENTIFIER", ASN_OBJECT_IDENTIFIER }, |
867 | const_iv (ASN_NULL) |
769 | { "ASN_TAG_BER", ASN_TAG_BER }, |
868 | const_iv (ASN_OBJECT_IDENTIFIER) |
770 | { "ASN_TAG_MASK", ASN_TAG_MASK }, |
869 | const_iv (ASN_OBJECT_DESCRIPTOR) |
771 | { "ASN_CONSTRUCTED", ASN_CONSTRUCTED }, |
870 | const_iv (ASN_OID) |
772 | { "ASN_UNIVERSAL", ASN_UNIVERSAL >> ASN_CLASS_SHIFT }, |
871 | const_iv (ASN_EXTERNAL) |
773 | { "ASN_APPLICATION", ASN_APPLICATION >> ASN_CLASS_SHIFT }, |
872 | const_iv (ASN_REAL) |
774 | { "ASN_CONTEXT", ASN_CONTEXT >> ASN_CLASS_SHIFT }, |
873 | const_iv (ASN_SEQUENCE) |
775 | { "ASN_PRIVATE", ASN_PRIVATE >> ASN_CLASS_SHIFT }, |
874 | const_iv (ASN_ENUMERATED) |
776 | { "ASN_CLASS_MASK", ASN_CLASS_MASK }, |
875 | const_iv (ASN_EMBEDDED_PDV) |
777 | { "ASN_CLASS_SHIFT", ASN_CLASS_SHIFT }, |
876 | const_iv (ASN_UTF8_STRING) |
778 | { "ASN_SEQUENCE", ASN_SEQUENCE }, |
877 | const_iv (ASN_RELATIVE_OID) |
779 | { "SNMP_IPADDRESS", SNMP_IPADDRESS }, |
878 | const_iv (ASN_SET) |
780 | { "SNMP_COUNTER32", SNMP_COUNTER32 }, |
879 | const_iv (ASN_NUMERIC_STRING) |
781 | { "SNMP_UNSIGNED32", SNMP_UNSIGNED32 }, |
880 | const_iv (ASN_PRINTABLE_STRING) |
782 | { "SNMP_TIMETICKS", SNMP_TIMETICKS }, |
881 | const_iv (ASN_TELETEX_STRING) |
783 | { "SNMP_OPAQUE", SNMP_OPAQUE }, |
882 | const_iv (ASN_T61_STRING) |
784 | { "SNMP_COUNTER64", SNMP_COUNTER64 }, |
883 | const_iv (ASN_VIDEOTEX_STRING) |
|
|
884 | const_iv (ASN_IA5_STRING) |
|
|
885 | const_iv (ASN_ASCII_STRING) |
|
|
886 | const_iv (ASN_UTC_TIME) |
|
|
887 | const_iv (ASN_GENERALIZED_TIME) |
|
|
888 | const_iv (ASN_GRAPHIC_STRING) |
|
|
889 | const_iv (ASN_VISIBLE_STRING) |
|
|
890 | const_iv (ASN_ISO646_STRING) |
|
|
891 | const_iv (ASN_GENERAL_STRING) |
|
|
892 | const_iv (ASN_UNIVERSAL_STRING) |
|
|
893 | const_iv (ASN_CHARACTER_STRING) |
|
|
894 | const_iv (ASN_BMP_STRING) |
785 | |
895 | |
786 | { "BER_CLASS" , BER_CLASS }, |
896 | const_iv (ASN_UNIVERSAL) |
787 | { "BER_TAG" , BER_TAG }, |
897 | const_iv (ASN_APPLICATION) |
788 | { "BER_CONSTRUCTED", BER_CONSTRUCTED }, |
898 | const_iv (ASN_CONTEXT) |
789 | { "BER_DATA" , BER_DATA }, |
899 | const_iv (ASN_PRIVATE) |
|
|
900 | |
|
|
901 | const_iv (BER_CLASS) |
|
|
902 | const_iv (BER_TAG) |
|
|
903 | const_iv (BER_CONSTRUCTED) |
|
|
904 | const_iv (BER_DATA) |
|
|
905 | |
|
|
906 | const_iv (BER_TYPE_BYTES) |
|
|
907 | const_iv (BER_TYPE_UTF8) |
|
|
908 | const_iv (BER_TYPE_UCS2) |
|
|
909 | const_iv (BER_TYPE_UCS4) |
|
|
910 | const_iv (BER_TYPE_INT) |
|
|
911 | const_iv (BER_TYPE_OID) |
|
|
912 | const_iv (BER_TYPE_RELOID) |
|
|
913 | const_iv (BER_TYPE_NULL) |
|
|
914 | const_iv (BER_TYPE_BOOL) |
|
|
915 | const_iv (BER_TYPE_REAL) |
|
|
916 | const_iv (BER_TYPE_CROAK) |
|
|
917 | |
|
|
918 | const_iv (SNMP_IPADDRESS) |
|
|
919 | const_iv (SNMP_COUNTER32) |
|
|
920 | const_iv (SNMP_UNSIGNED32) |
|
|
921 | const_iv (SNMP_TIMETICKS) |
|
|
922 | const_iv (SNMP_OPAQUE) |
|
|
923 | const_iv (SNMP_COUNTER64) |
|
|
924 | |
790 | }; |
925 | }; |
791 | |
926 | |
792 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) |
927 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) |
793 | newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); |
928 | newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); |
|
|
929 | |
|
|
930 | default_profile = profile_new (); |
794 | } |
931 | } |
795 | |
932 | |
796 | SV * |
933 | SV * |
797 | ber_decode (SV *ber) |
934 | ber_decode (SV *ber, SV *profile = &PL_sv_undef) |
798 | CODE: |
935 | CODE: |
799 | { |
936 | { |
|
|
937 | cur_profile = SvPROFILE (profile); |
800 | STRLEN len; |
938 | STRLEN len; |
801 | |
|
|
802 | buf = SvPVbyte (ber, len); |
939 | buf = SvPVbyte (ber, len); |
803 | cur = buf; |
940 | cur = buf; |
804 | end = buf + len; |
941 | end = buf + len; |
805 | |
942 | |
806 | RETVAL = decode_ber (); |
943 | RETVAL = decode_ber (); |
… | |
… | |
881 | } |
1018 | } |
882 | |
1019 | |
883 | ############################################################################# |
1020 | ############################################################################# |
884 | |
1021 | |
885 | void |
1022 | void |
886 | ber_encode (SV *tuple) |
1023 | ber_encode (SV *tuple, SV *profile = &PL_sv_undef) |
887 | PPCODE: |
1024 | PPCODE: |
888 | { |
1025 | { |
|
|
1026 | cur_profile = SvPROFILE (profile); |
889 | buf_sv = sv_2mortal (NEWSV (0, 256)); |
1027 | buf_sv = sv_2mortal (NEWSV (0, 256)); |
890 | SvPOK_only (buf_sv); |
1028 | SvPOK_only (buf_sv); |
891 | set_buf (buf_sv); |
1029 | set_buf (buf_sv); |
892 | |
1030 | |
893 | encode_ber (tuple); |
1031 | encode_ber (tuple); |
… | |
… | |
923 | AvARRAY (av)[BER_DATA ] = newSVsv (arrayref); |
1061 | AvARRAY (av)[BER_DATA ] = newSVsv (arrayref); |
924 | RETVAL = newRV_noinc ((SV *)av); |
1062 | RETVAL = newRV_noinc ((SV *)av); |
925 | } |
1063 | } |
926 | OUTPUT: RETVAL |
1064 | OUTPUT: RETVAL |
927 | |
1065 | |
|
|
1066 | MODULE = Convert::BER::XS PACKAGE = Convert::BER::XS::Profile |
|
|
1067 | |
|
|
1068 | SV * |
|
|
1069 | new (SV *klass) |
|
|
1070 | CODE: |
|
|
1071 | RETVAL = profile_new (); |
|
|
1072 | OUTPUT: RETVAL |
|
|
1073 | |