… | |
… | |
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, //X |
|
|
16 | ASN_OBJECT_DESCRIPTOR = 0x07, //X |
|
|
17 | ASN_EXTERNAL = 0x08, //X |
|
|
18 | ASN_REAL = 0x09, //X |
|
|
19 | ASN_ENUMERATED = 0x0a, //X |
|
|
20 | ASN_EMBEDDED_PDV = 0x0b, //X |
|
|
21 | ASN_UTF8_STRING = 0x0c, //X |
|
|
22 | ASN_RELATIVE_OID = 0x0d, //X |
15 | ASN_SEQUENCE = 0x10, |
23 | ASN_SEQUENCE = 0x10, |
|
|
24 | ASN_SET = 0x11, //X |
|
|
25 | ASN_NUMERIC_STRING = 0x12, //X |
|
|
26 | ASN_PRINTABLE_STRING = 0x13, //X |
|
|
27 | ASN_TELETEX_STRING = 0x14, //X |
|
|
28 | ASN_T61_STRING = 0x14, //X |
|
|
29 | ASN_VIDEOTEX_STRING = 0x15, //X |
|
|
30 | ASN_IA5_STRING = 0x16, //X |
|
|
31 | ASN_ASCII_STRING = 0x16, //X |
|
|
32 | ASN_UTC_TIME = 0x17, //X |
|
|
33 | ASN_GENERALIZED_TIME = 0x18, //X |
|
|
34 | ASN_GRAPHIC_STRING = 0x19, //X |
|
|
35 | ASN_VISIBLE_STRING = 0x1a, //X |
|
|
36 | ASN_ISO646_STRING = 0x1a, //X |
|
|
37 | ASN_GENERAL_STRING = 0x1b, //X |
|
|
38 | ASN_UNIVERSAL_STRING = 0x1c, //X |
|
|
39 | ASN_CHARACTER_STRING = 0x1d, //X |
|
|
40 | ASN_BMP_STRING = 0x1e, //X |
16 | |
41 | |
17 | ASN_TAG_BER = 0x1f, |
42 | ASN_TAG_BER = 0x1f, |
18 | ASN_TAG_MASK = 0x1f, |
43 | ASN_TAG_MASK = 0x1f, |
19 | |
44 | |
20 | // primitive/constructed |
45 | // primitive/constructed |
21 | ASN_CONSTRUCTED = 0x20, |
46 | ASN_CONSTRUCTED = 0x20, |
22 | |
47 | |
23 | // ASN_CLASS |
48 | // ASN_CLASS |
24 | ASN_UNIVERSAL = 0x00, |
49 | ASN_UNIVERSAL = 0x00, |
25 | ASN_APPLICATION = 0x40, |
50 | ASN_APPLICATION = 0x01, |
26 | ASN_CONTEXT = 0x80, |
51 | ASN_CONTEXT = 0x02, |
27 | ASN_PRIVATE = 0xc0, |
52 | ASN_PRIVATE = 0x03, |
28 | |
53 | |
29 | ASN_CLASS_MASK = 0xc0, |
54 | ASN_CLASS_MASK = 0xc0, |
30 | ASN_CLASS_SHIFT = 6, |
55 | ASN_CLASS_SHIFT = 6, |
31 | |
56 | |
32 | // ASN_APPLICATION |
57 | // ASN_APPLICATION SNMP |
33 | ASN_IPADDRESS = 0x00, |
58 | SNMP_IPADDRESS = 0x00, |
34 | ASN_COUNTER32 = 0x01, |
59 | SNMP_COUNTER32 = 0x01, |
35 | ASN_UNSIGNED32 = 0x02, |
60 | SNMP_UNSIGNED32 = 0x02, |
36 | ASN_TIMETICKS = 0x03, |
61 | SNMP_TIMETICKS = 0x03, |
37 | ASN_OPAQUE = 0x04, |
62 | SNMP_OPAQUE = 0x04, |
38 | ASN_COUNTER64 = 0x06, |
63 | SNMP_COUNTER64 = 0x06, |
|
|
64 | }; |
|
|
65 | |
|
|
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_IPADDRESS, |
|
|
78 | BER_TYPE_CROAK, |
39 | }; |
79 | }; |
40 | |
80 | |
41 | enum { |
81 | enum { |
42 | BER_CLASS = 0, |
82 | BER_CLASS = 0, |
43 | BER_TAG = 1, |
83 | BER_TAG = 1, |
… | |
… | |
46 | BER_ARRAYSIZE |
86 | BER_ARRAYSIZE |
47 | }; |
87 | }; |
48 | |
88 | |
49 | #define MAX_OID_STRLEN 4096 |
89 | #define MAX_OID_STRLEN 4096 |
50 | |
90 | |
51 | static U8 *buf, *cur; |
91 | typedef void profile_type; |
52 | static STRLEN len, rem; |
92 | |
|
|
93 | static profile_type *cur_profile, *default_profile; |
|
|
94 | static SV *buf_sv; // encoding buffer |
|
|
95 | static 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 | |
|
|
110 | static SV *sviv_cache[32]; |
53 | |
111 | |
54 | // 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 |
55 | static SV *newSVcacheint (int val) |
113 | static SV *newSVcacheint (int val) |
56 | { |
114 | { |
57 | static SV *cache[32]; |
|
|
58 | |
|
|
59 | if (val < 0 || val >= sizeof (cache)) |
115 | if (expect_false (val < 0 || val >= sizeof (sviv_cache))) |
60 | return newSViv (val); |
116 | return newSViv (val); |
61 | |
117 | |
62 | if (!cache [val]) |
118 | if (expect_false (!sviv_cache [val])) |
63 | { |
119 | { |
64 | cache [val] = newSVuv (val); |
120 | sviv_cache [val] = newSVuv (val); |
65 | SvREADONLY_on (cache [val]); |
121 | SvREADONLY_on (sviv_cache [val]); |
66 | } |
122 | } |
67 | |
123 | |
68 | return SvREFCNT_inc_NN (cache [val]); |
124 | return SvREFCNT_inc_NN (sviv_cache [val]); |
69 | } |
125 | } |
70 | |
126 | |
71 | ///////////////////////////////////////////////////////////////////////////// |
127 | ///////////////////////////////////////////////////////////////////////////// |
72 | |
128 | |
|
|
129 | static HV *profile_stash; |
|
|
130 | |
|
|
131 | static profile_type * |
|
|
132 | SvPROFILE (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 | |
|
|
148 | static int |
|
|
149 | profile_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 | |
|
|
160 | static int |
|
|
161 | profile_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 | |
|
|
178 | static SV * |
|
|
179 | profile_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 | |
73 | static void |
209 | static void |
74 | error (const char *errmsg) |
210 | error (const char *errmsg) |
75 | { |
211 | { |
76 | croak ("%s at offset 0x%04x", errmsg, cur - buf); |
212 | croak ("%s at offset 0x%04x", errmsg, cur - buf); |
77 | } |
213 | } |
78 | |
214 | |
79 | static int |
215 | static void |
80 | need (int count) |
216 | want (UV count) |
81 | { |
217 | { |
82 | if (count < 0 || (int)rem < count) |
218 | if (expect_false ((uintptr_t)(end - cur) < count)) |
83 | { |
|
|
84 | error ("unexpected end of message buffer"); |
219 | error ("unexpected end of message buffer"); |
85 | return 0; |
|
|
86 | } |
|
|
87 | |
|
|
88 | return 1; |
|
|
89 | } |
220 | } |
90 | |
221 | |
|
|
222 | // get_* functions fetch something from the buffer |
|
|
223 | // decode_* functions use get_* fun ctions to decode ber values |
|
|
224 | |
|
|
225 | // get n octets |
91 | static U8 * |
226 | static U8 * |
92 | getn (int count, const U8 *errres) |
227 | get_n (UV count) |
93 | { |
228 | { |
94 | if (!need (count)) |
229 | want (count); |
95 | return (U8 *)errres; |
|
|
96 | |
|
|
97 | U8 *res = cur; |
230 | U8 *res = cur; |
98 | |
|
|
99 | cur += count; |
231 | cur += count; |
100 | rem -= count; |
|
|
101 | |
|
|
102 | return res; |
232 | return res; |
103 | } |
233 | } |
104 | |
234 | |
|
|
235 | // get single octet |
105 | static U8 |
236 | static U8 |
106 | get8 (void) |
237 | get_u8 (void) |
107 | { |
238 | { |
108 | if (rem <= 0) |
239 | if (cur == end) |
109 | { |
|
|
110 | error ("unexpected end of message buffer"); |
240 | error ("unexpected end of message buffer"); |
111 | return 0; |
|
|
112 | } |
|
|
113 | |
241 | |
114 | rem--; |
|
|
115 | return *cur++; |
242 | return *cur++; |
116 | } |
243 | } |
117 | |
244 | |
|
|
245 | // get ber-encoded integer (i.e. pack "w") |
118 | static U32 |
246 | static U32 |
119 | getb (void) |
247 | get_w (void) |
120 | { |
248 | { |
121 | U32 res = 0; |
249 | U32 res = 0; |
122 | |
250 | |
123 | for (;;) |
251 | for (;;) |
124 | { |
252 | { |
125 | U8 c = get8 (); |
253 | U8 c = get_u8 (); |
126 | res = (res << 7) | (c & 0x7f); |
254 | res = (res << 7) | (c & 0x7f); |
127 | |
255 | |
128 | if (!(c & 0x80)) |
256 | if (!(c & 0x80)) |
129 | return res; |
257 | return res; |
130 | } |
258 | } |
131 | } |
259 | } |
132 | |
260 | |
133 | static U32 |
261 | static U32 |
134 | process_length (void) |
262 | get_length (void) |
135 | { |
263 | { |
136 | U32 res = get8 (); |
264 | U32 res = get_u8 (); |
137 | |
265 | |
138 | if (res & 0x80) |
266 | if (res & 0x80) |
139 | { |
267 | { |
140 | int cnt = res & 0x7f; |
268 | int cnt = res & 0x7f; |
141 | res = 0; |
269 | res = 0; |
… | |
… | |
148 | |
276 | |
149 | default: |
277 | default: |
150 | error ("ASN.1 length too long"); |
278 | error ("ASN.1 length too long"); |
151 | return 0; |
279 | return 0; |
152 | |
280 | |
153 | case 4: res = (res << 8) | get8 (); |
281 | case 4: res = (res << 8) | get_u8 (); |
154 | case 3: res = (res << 8) | get8 (); |
282 | case 3: res = (res << 8) | get_u8 (); |
155 | case 2: res = (res << 8) | get8 (); |
283 | case 2: res = (res << 8) | get_u8 (); |
156 | case 1: res = (res << 8) | get8 (); |
284 | case 1: res = (res << 8) | get_u8 (); |
157 | } |
285 | } |
158 | } |
286 | } |
159 | |
287 | |
160 | return res; |
288 | return res; |
161 | } |
289 | } |
162 | |
290 | |
163 | static U32 |
291 | static SV * |
164 | process_integer32 (void) |
292 | decode_int () |
165 | { |
293 | { |
166 | U32 length = process_length (); |
294 | int len = get_length (); |
167 | |
295 | |
168 | if (length <= 0) |
296 | if (len <= 0) |
169 | { |
297 | { |
170 | error ("INTEGER32 length equal to zero"); |
298 | error ("integer length equal to zero"); |
171 | return 0; |
299 | return 0; |
172 | } |
300 | } |
173 | |
301 | |
174 | U8 *data = getn (length, 0); |
302 | U8 *data = get_n (len); |
175 | |
303 | |
176 | if (!data) |
304 | int negative = data [0] & 0x80; |
177 | return 0; |
|
|
178 | |
305 | |
179 | if (length > 5 || (length > 4 && data [0])) |
306 | UV val = negative ? -1 : 0; // copy signbit to all bits |
180 | { |
|
|
181 | error ("INTEGER32 length too long"); |
|
|
182 | return 0; |
|
|
183 | } |
|
|
184 | |
307 | |
185 | U32 res = data [0] & 0x80 ? 0xffffffff : 0; |
308 | do |
186 | |
|
|
187 | while (length--) |
|
|
188 | res = (res << 8) | *data++; |
309 | val = (val << 8) | *data++; |
|
|
310 | while (--len); |
189 | |
311 | |
190 | 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); |
191 | } |
315 | } |
192 | |
316 | |
193 | static SV * |
317 | static SV * |
194 | process_integer32_sv (void) |
318 | decode_data (void) |
195 | { |
319 | { |
196 | return newSViv ((I32)process_integer32 ()); |
320 | U32 len = get_length (); |
197 | } |
|
|
198 | |
|
|
199 | static SV * |
|
|
200 | process_unsigned32_sv (void) |
|
|
201 | { |
|
|
202 | return newSVuv ((U32)process_integer32 ()); |
|
|
203 | } |
|
|
204 | |
|
|
205 | #if IVSIZE >= 8 |
|
|
206 | |
|
|
207 | static U64TYPE |
|
|
208 | process_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); |
321 | U8 *data = get_n (len); |
219 | |
|
|
220 | if (!data) |
|
|
221 | return 0; |
|
|
222 | |
|
|
223 | if (length > 9 || (length > 8 && data [0])) |
|
|
224 | { |
|
|
225 | error ("INTEGER64 length too long"); |
|
|
226 | return 0; |
|
|
227 | } |
|
|
228 | |
|
|
229 | U64TYPE res = data [0] & 0x80 ? 0xffffffffffffffff : 0; |
|
|
230 | |
|
|
231 | while (length--) |
|
|
232 | res = (res << 8) | *data++; |
|
|
233 | |
|
|
234 | return res; |
|
|
235 | } |
|
|
236 | |
|
|
237 | static SV * |
|
|
238 | process_integer64_sv (void) |
|
|
239 | { |
|
|
240 | return newSViv ((I64TYPE)process_integer64 ()); |
|
|
241 | } |
|
|
242 | |
|
|
243 | static SV * |
|
|
244 | process_unsigned64_sv (void) |
|
|
245 | { |
|
|
246 | return newSVuv ((U64TYPE)process_integer64 ()); |
|
|
247 | } |
|
|
248 | |
|
|
249 | #endif |
|
|
250 | |
|
|
251 | static SV * |
|
|
252 | process_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); |
322 | return newSVpvn ((char *)data, len); |
264 | } |
323 | } |
265 | |
324 | |
|
|
325 | // gelper for decode_object_identifier |
266 | static char * |
326 | static char * |
267 | write_uv (char *buf, U32 u) |
327 | write_uv (char *buf, U32 u) |
268 | { |
328 | { |
269 | // 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) |
270 | if (u < 10) |
330 | if (expect_true (u < 10)) |
271 | *buf++ = u + '0'; |
331 | *buf++ = u + '0'; |
272 | else |
332 | else |
273 | { |
333 | { |
274 | char *beg = buf; |
334 | char *beg = buf; |
275 | |
335 | |
… | |
… | |
291 | |
351 | |
292 | return buf; |
352 | return buf; |
293 | } |
353 | } |
294 | |
354 | |
295 | static SV * |
355 | static SV * |
296 | process_object_identifier_sv (void) |
356 | decode_oid (int relative) |
297 | { |
357 | { |
298 | U32 length = process_length (); |
358 | U32 len = get_length (); |
299 | |
359 | |
300 | if (length <= 0) |
360 | if (len <= 0) |
301 | { |
361 | { |
302 | error ("OBJECT IDENTIFIER length equal to zero"); |
362 | error ("OBJECT IDENTIFIER length equal to zero"); |
303 | return &PL_sv_undef; |
363 | return &PL_sv_undef; |
304 | } |
364 | } |
305 | |
365 | |
306 | U8 *end = cur + length; |
366 | U8 *end = cur + len; |
307 | U32 w = getb (); |
367 | U32 w = get_w (); |
308 | |
368 | |
309 | static char oid[MAX_OID_STRLEN]; // must be static |
369 | static char oid[MAX_OID_STRLEN]; // must be static |
310 | char *app = oid; |
370 | char *app = oid; |
311 | |
371 | |
|
|
372 | if (relative) |
|
|
373 | app = write_uv (app, w); |
|
|
374 | else |
|
|
375 | { |
312 | app = write_uv (app, (U8)w / 40); |
376 | app = write_uv (app, (U8)w / 40); |
313 | *app++ = '.'; |
377 | *app++ = '.'; |
314 | app = write_uv (app, (U8)w % 40); |
378 | app = write_uv (app, (U8)w % 40); |
|
|
379 | } |
315 | |
380 | |
316 | // we assume an oid component is never > 64 bytes |
381 | // we assume an oid component is never > 64 bytes |
317 | while (cur < end && oid + sizeof (oid) - app > 64) |
382 | while (cur < end && oid + sizeof (oid) - app > 64) |
318 | { |
383 | { |
319 | w = getb (); |
384 | w = get_w (); |
320 | *app++ = '.'; |
385 | *app++ = '.'; |
321 | app = write_uv (app, w); |
386 | app = write_uv (app, w); |
322 | } |
387 | } |
323 | |
388 | |
324 | return newSVpvn (oid, app - oid); |
389 | return newSVpvn (oid, app - oid); |
325 | } |
390 | } |
326 | |
391 | |
|
|
392 | // TODO: this is unacceptably slow |
327 | static SV * |
393 | static SV * |
328 | ber_decode () |
394 | decode_ucs (int chrsize) |
329 | { |
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 | |
|
|
428 | static SV * |
|
|
429 | decode_ber () |
|
|
430 | { |
330 | int identifier = get8 (); |
431 | int identifier = get_u8 (); |
331 | |
432 | |
332 | SV *res; |
433 | SV *res; |
333 | |
434 | |
334 | int constructed = identifier & ASN_CONSTRUCTED; |
435 | int constructed = identifier & ASN_CONSTRUCTED; |
335 | int klass = identifier & ASN_CLASS_MASK; |
436 | int klass = (identifier & ASN_CLASS_MASK) >> ASN_CLASS_SHIFT; |
336 | int tag = identifier & ASN_TAG_MASK; |
437 | int tag = identifier & ASN_TAG_MASK; |
337 | |
438 | |
338 | if (tag == ASN_TAG_BER) |
439 | if (tag == ASN_TAG_BER) |
339 | tag = getb (); |
440 | tag = get_w (); |
340 | |
441 | |
341 | if (tag == ASN_TAG_BER) |
442 | if (tag == ASN_TAG_BER) |
342 | tag = getb (); |
443 | tag = get_w (); |
343 | |
444 | |
344 | if (constructed) |
445 | if (constructed) |
345 | { |
446 | { |
346 | U32 len = process_length (); |
447 | U32 len = get_length (); |
347 | U32 seqend = (cur - buf) + len; |
448 | U32 seqend = (cur - buf) + len; |
348 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
449 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
349 | |
450 | |
350 | while (cur < buf + seqend) |
451 | while (cur < buf + seqend) |
351 | av_push (av, ber_decode ()); |
452 | av_push (av, decode_ber ()); |
352 | |
453 | |
353 | if (cur > buf + seqend) |
454 | if (cur > buf + seqend) |
354 | 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); |
355 | |
456 | |
356 | res = newRV_inc ((SV *)av); |
457 | res = newRV_inc ((SV *)av); |
357 | } |
458 | } |
358 | else |
459 | else |
359 | switch (identifier) |
460 | switch (profile_lookup (cur_profile, klass, tag)) |
360 | { |
461 | { |
361 | case ASN_NULL: |
462 | case BER_TYPE_NULL: |
362 | res = &PL_sv_undef; |
463 | res = &PL_sv_undef; |
363 | break; |
464 | break; |
364 | |
465 | |
365 | case ASN_OBJECT_IDENTIFIER: |
466 | case BER_TYPE_BOOL: |
366 | res = process_object_identifier_sv (); |
467 | { |
367 | break; |
468 | U32 len = get_length (); |
368 | |
469 | |
369 | case ASN_INTEGER32: |
470 | if (len != 1) |
370 | res = process_integer32_sv (); |
471 | croak ("BER_TYPE_BOOLEAN type with invalid length %d encountered", len); |
371 | break; |
|
|
372 | |
472 | |
373 | case ASN_APPLICATION | ASN_UNSIGNED32: |
473 | res = newSVcacheint (get_u8 () ? 0 : 1); |
374 | case ASN_APPLICATION | ASN_COUNTER32: |
474 | } |
375 | case ASN_APPLICATION | ASN_TIMETICKS: |
|
|
376 | res = process_unsigned32_sv (); |
|
|
377 | break; |
475 | break; |
378 | |
476 | |
379 | #if 0 // handled by default case |
477 | case BER_TYPE_OID: |
380 | case ASN_OCTET_STRING: |
478 | res = decode_oid (0); |
381 | case ASN_APPLICATION | ASN_IPADDRESS: |
|
|
382 | case ASN_APPLICATION | ASN_OPAQUE: |
|
|
383 | res = process_octet_string_sv (); |
|
|
384 | break; |
479 | break; |
385 | #endif |
|
|
386 | |
480 | |
387 | case ASN_APPLICATION | ASN_COUNTER64: |
481 | case BER_TYPE_RELOID: |
388 | res = process_integer64_sv (); |
482 | res = decode_oid (1); |
389 | break; |
483 | break; |
390 | |
484 | |
|
|
485 | case BER_TYPE_INT: |
|
|
486 | res = decode_int (); |
|
|
487 | break; |
|
|
488 | |
|
|
489 | case BER_TYPE_UTF8: |
|
|
490 | res = decode_data (); |
|
|
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: |
|
|
519 | res = decode_ucs (4); |
|
|
520 | break; |
|
|
521 | |
|
|
522 | case BER_TYPE_REAL: |
|
|
523 | case BER_TYPE_CROAK: |
391 | default: |
524 | default: |
392 | res = process_octet_string_sv (); |
525 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
393 | break; |
|
|
394 | } |
526 | } |
395 | |
527 | |
396 | AV *av = newAV (); |
528 | AV *av = newAV (); |
397 | av_fill (av, BER_ARRAYSIZE - 1); |
529 | av_fill (av, BER_ARRAYSIZE - 1); |
398 | AvARRAY (av)[BER_CLASS ] = newSVcacheint (klass >> ASN_CLASS_SHIFT); |
530 | AvARRAY (av)[BER_CLASS ] = newSVcacheint (klass); |
399 | AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); |
531 | AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); |
400 | AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (constructed ? 1 : 0); |
532 | AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (constructed ? 1 : 0); |
401 | AvARRAY (av)[BER_DATA ] = res; |
533 | AvARRAY (av)[BER_DATA ] = res; |
402 | |
534 | |
403 | return newRV_noinc ((SV *)av); |
535 | return newRV_noinc ((SV *)av); |
404 | } |
536 | } |
405 | |
537 | |
|
|
538 | ///////////////////////////////////////////////////////////////////////////// |
|
|
539 | // encoder |
|
|
540 | |
|
|
541 | /* adds two STRLENs together, slow, and with paranoia */ |
|
|
542 | static STRLEN |
|
|
543 | strlen_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 | |
|
|
553 | static void |
|
|
554 | set_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 */ |
|
|
564 | static char * |
|
|
565 | my_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 | |
|
|
576 | static void |
|
|
577 | need (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 | |
|
|
588 | static void |
|
|
589 | put_u8 (int val) |
|
|
590 | { |
|
|
591 | need (1); |
|
|
592 | *cur++ = val; |
|
|
593 | } |
|
|
594 | |
|
|
595 | static void |
|
|
596 | put_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 | |
|
|
605 | static void |
|
|
606 | put_w (U32 val) |
|
|
607 | { |
|
|
608 | need (5); // we only handle up to 5 bytes |
|
|
609 | |
|
|
610 | put_w_nocheck (val); |
|
|
611 | } |
|
|
612 | |
|
|
613 | static U8 * |
|
|
614 | put_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 | |
|
|
633 | static void |
|
|
634 | put_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 |
|
|
641 | static int length_length (U32 val) |
|
|
642 | { |
|
|
643 | return val < 0x7fU |
|
|
644 | ? 1 |
|
|
645 | : 2 + (val > 0xffU) + (val > 0xffffU) + (val > 0xffffffU); |
|
|
646 | } |
|
|
647 | |
|
|
648 | static void |
|
|
649 | encode_data (const char *ptr, STRLEN len) |
|
|
650 | { |
|
|
651 | put_length (len); |
|
|
652 | memcpy (cur, ptr, len); |
|
|
653 | cur += len; |
|
|
654 | } |
|
|
655 | |
|
|
656 | static void |
|
|
657 | encode_uv (UV uv) |
|
|
658 | { |
|
|
659 | } |
|
|
660 | |
|
|
661 | static void |
|
|
662 | encode_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 |
|
|
720 | static STRLEN |
|
|
721 | len_fixup_mark () |
|
|
722 | { |
|
|
723 | return cur++ - buf; |
|
|
724 | } |
|
|
725 | |
|
|
726 | // patch up the length |
|
|
727 | static void |
|
|
728 | len_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 | |
|
|
744 | static char * |
|
|
745 | read_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 | |
|
|
759 | static void |
|
|
760 | encode_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 * |
|
|
789 | static AV * |
|
|
790 | ber_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 | |
|
|
806 | static void |
|
|
807 | encode_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 | } |
|
|
833 | static void |
|
|
834 | encode_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 | |
406 | MODULE = Convert::BER::XS PACKAGE = Convert::BER::XS |
940 | MODULE = Convert::BER::XS PACKAGE = Convert::BER::XS |
407 | |
941 | |
408 | PROTOTYPES: ENABLE |
942 | PROTOTYPES: ENABLE |
409 | |
943 | |
410 | BOOT: |
944 | BOOT: |
411 | { |
945 | { |
412 | 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); |
413 | |
949 | |
414 | static const struct { |
950 | static const struct { |
415 | const char *name; |
951 | const char *name; |
416 | IV iv; |
952 | IV iv; |
417 | } *civ, const_iv[] = { |
953 | } *civ, const_iv[] = { |
418 | { "ASN_BOOLEAN", ASN_BOOLEAN }, |
954 | #define const_iv(name) { # name, name }, |
419 | { "ASN_INTEGER32", ASN_INTEGER32 }, |
955 | const_iv (ASN_BOOLEAN) |
420 | { "ASN_BIT_STRING", ASN_BIT_STRING }, |
956 | const_iv (ASN_INTEGER32) |
421 | { "ASN_OCTET_STRING", ASN_OCTET_STRING }, |
957 | const_iv (ASN_BIT_STRING) |
422 | { "ASN_NULL", ASN_NULL }, |
958 | const_iv (ASN_OCTET_STRING) |
423 | { "ASN_OBJECT_IDENTIFIER", ASN_OBJECT_IDENTIFIER }, |
959 | const_iv (ASN_NULL) |
424 | { "ASN_TAG_BER", ASN_TAG_BER }, |
960 | const_iv (ASN_OBJECT_IDENTIFIER) |
425 | { "ASN_TAG_MASK", ASN_TAG_MASK }, |
961 | const_iv (ASN_OBJECT_DESCRIPTOR) |
426 | { "ASN_CONSTRUCTED", ASN_CONSTRUCTED }, |
962 | const_iv (ASN_OID) |
427 | { "ASN_UNIVERSAL", ASN_UNIVERSAL >> ASN_CLASS_SHIFT }, |
963 | const_iv (ASN_EXTERNAL) |
428 | { "ASN_APPLICATION", ASN_APPLICATION >> ASN_CLASS_SHIFT }, |
964 | const_iv (ASN_REAL) |
429 | { "ASN_CONTEXT", ASN_CONTEXT >> ASN_CLASS_SHIFT }, |
965 | const_iv (ASN_SEQUENCE) |
430 | { "ASN_PRIVATE", ASN_PRIVATE >> ASN_CLASS_SHIFT }, |
966 | const_iv (ASN_ENUMERATED) |
431 | { "ASN_CLASS_MASK", ASN_CLASS_MASK }, |
967 | const_iv (ASN_EMBEDDED_PDV) |
432 | { "ASN_CLASS_SHIFT", ASN_CLASS_SHIFT }, |
968 | const_iv (ASN_UTF8_STRING) |
433 | { "ASN_SEQUENCE", ASN_SEQUENCE }, |
969 | const_iv (ASN_RELATIVE_OID) |
434 | { "ASN_IPADDRESS", ASN_IPADDRESS }, |
970 | const_iv (ASN_SET) |
435 | { "ASN_COUNTER32", ASN_COUNTER32 }, |
971 | const_iv (ASN_NUMERIC_STRING) |
436 | { "ASN_UNSIGNED32", ASN_UNSIGNED32 }, |
972 | const_iv (ASN_PRINTABLE_STRING) |
437 | { "ASN_TIMETICKS", ASN_TIMETICKS }, |
973 | const_iv (ASN_TELETEX_STRING) |
438 | { "ASN_OPAQUE", ASN_OPAQUE }, |
974 | const_iv (ASN_T61_STRING) |
439 | { "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) |
440 | |
987 | |
441 | { "BER_CLASS" , BER_CLASS }, |
988 | const_iv (ASN_UNIVERSAL) |
442 | { "BER_TAG" , BER_TAG }, |
989 | const_iv (ASN_APPLICATION) |
443 | { "BER_CONSTRUCTED", BER_CONSTRUCTED }, |
990 | const_iv (ASN_CONTEXT) |
444 | { "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) |
445 | }; |
1017 | }; |
446 | |
1018 | |
447 | 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--) |
448 | newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); |
1020 | newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); |
449 | } |
1021 | } |
450 | |
1022 | |
451 | SV * |
1023 | SV * |
452 | ber_decode (SV *ber) |
1024 | ber_decode (SV *ber, SV *profile = &PL_sv_undef) |
453 | CODE: |
1025 | CODE: |
454 | { |
1026 | { |
|
|
1027 | cur_profile = SvPROFILE (profile); |
|
|
1028 | STRLEN len; |
455 | buf = SvPVbyte (ber, len); |
1029 | buf = SvPVbyte (ber, len); |
456 | cur = buf; |
1030 | cur = buf; |
457 | rem = len; |
1031 | end = buf + len; |
458 | |
1032 | |
459 | RETVAL = ber_decode (); |
1033 | RETVAL = decode_ber (); |
460 | } |
1034 | } |
461 | OUTPUT: RETVAL |
1035 | OUTPUT: RETVAL |
462 | |
1036 | |
463 | void |
1037 | void |
464 | ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *constructed = &PL_sv_undef, SV *data = &PL_sv_undef) |
1038 | ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *constructed = &PL_sv_undef, SV *data = &PL_sv_undef) |
465 | PROTOTYPE: $;$$$ |
|
|
466 | PPCODE: |
1039 | PPCODE: |
467 | { |
1040 | { |
468 | if (!SvOK (tuple)) |
1041 | if (!SvOK (tuple)) |
469 | XSRETURN_NO; |
1042 | XSRETURN_NO; |
470 | |
1043 | |
471 | if (!SvROK (tuple) || SvTYPE (SvRV (tuple)) != SVt_PVAV) |
1044 | if (!SvROK (tuple) || SvTYPE (SvRV (tuple)) != SVt_PVAV) |
472 | croak ("ber_seq: tuple must be ber tuple (array-ref)"); |
1045 | croak ("ber_is: tuple must be BER tuple (array-ref)"); |
473 | |
1046 | |
474 | AV *av = (AV *)SvRV (tuple); |
1047 | AV *av = (AV *)SvRV (tuple); |
475 | |
1048 | |
476 | XPUSHs ( |
1049 | XPUSHs ( |
477 | (!SvOK (klass) || SvIV (AvARRAY (av)[BER_CLASS ]) == SvIV (klass)) |
1050 | (!SvOK (klass) || SvIV (AvARRAY (av)[BER_CLASS ]) == SvIV (klass)) |
478 | && (!SvOK (tag) || SvIV (AvARRAY (av)[BER_TAG ]) == SvIV (tag)) |
1051 | && (!SvOK (tag) || SvIV (AvARRAY (av)[BER_TAG ]) == SvIV (tag)) |
479 | && (!SvOK (constructed) || !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) == !SvIV (constructed)) |
1052 | && (!SvOK (constructed) || !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) == !SvIV (constructed)) |
480 | && (!SvOK (data) || sv_eq (AvARRAY (av)[BER_DATA ], data)) |
1053 | && (!SvOK (data) || sv_eq (AvARRAY (av)[BER_DATA ], data)) |
481 | ? &PL_sv_yes : &PL_sv_no); |
1054 | ? &PL_sv_yes : &PL_sv_undef); |
482 | } |
1055 | } |
483 | |
1056 | |
484 | void |
1057 | void |
485 | ber_is_seq (SV *tuple) |
1058 | ber_is_seq (SV *tuple) |
486 | PROTOTYPE: $ |
|
|
487 | PPCODE: |
1059 | PPCODE: |
488 | { |
1060 | { |
489 | if (!SvOK (tuple)) |
1061 | if (!SvOK (tuple)) |
490 | XSRETURN_UNDEF; |
1062 | XSRETURN_UNDEF; |
491 | |
1063 | |
492 | if (!SvROK (tuple) || SvTYPE (SvRV (tuple)) != SVt_PVAV) |
1064 | AV *av = ber_tuple (tuple); |
493 | croak ("ber_seq: tuple must be ber tuple (array-ref)"); |
|
|
494 | |
|
|
495 | AV *av = (AV *)SvRV (tuple); |
|
|
496 | |
1065 | |
497 | XPUSHs ( |
1066 | XPUSHs ( |
498 | SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL |
1067 | SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL |
499 | && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_SEQUENCE |
1068 | && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_SEQUENCE |
500 | && SvIV (AvARRAY (av)[BER_CONSTRUCTED]) |
1069 | && SvIV (AvARRAY (av)[BER_CONSTRUCTED]) |
501 | ? AvARRAY (av)[BER_DATA] : &PL_sv_undef); |
1070 | ? AvARRAY (av)[BER_DATA] : &PL_sv_undef); |
502 | } |
1071 | } |
503 | |
1072 | |
504 | void |
1073 | void |
505 | ber_is_i32 (SV *tuple, IV value) |
1074 | ber_is_i32 (SV *tuple, SV *value = &PL_sv_undef) |
506 | PROTOTYPE: $$ |
|
|
507 | PPCODE: |
1075 | PPCODE: |
508 | { |
1076 | { |
509 | if (!SvOK (tuple)) |
1077 | if (!SvOK (tuple)) |
510 | XSRETURN_NO; |
1078 | XSRETURN_NO; |
511 | |
1079 | |
512 | if (!SvROK (tuple) || SvTYPE (SvRV (tuple)) != SVt_PVAV) |
1080 | AV *av = ber_tuple (tuple); |
513 | croak ("ber_seq: tuple must be ber tuple (array-ref)"); |
|
|
514 | |
1081 | |
515 | AV *av = (AV *)SvRV (tuple); |
1082 | IV data = SvIV (AvARRAY (av)[BER_DATA]); |
516 | |
1083 | |
517 | XPUSHs ( |
1084 | XPUSHs ( |
518 | SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL |
1085 | SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL |
519 | && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_INTEGER32 |
1086 | && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_INTEGER32 |
520 | && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) |
1087 | && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) |
521 | && SvIV (AvARRAY (av)[BER_DATA ]) == value |
1088 | && (!SvOK (value) || data == SvIV (value)) |
522 | ? &PL_sv_yes : &PL_sv_no); |
1089 | ? sv_2mortal (data ? newSViv (data) : newSVpv ("0 but true", 0)) |
|
|
1090 | : &PL_sv_undef); |
523 | } |
1091 | } |
524 | |
1092 | |
525 | void |
1093 | void |
526 | ber_is_oid (SV *tuple, SV *oid) |
1094 | ber_is_oid (SV *tuple, SV *oid = &PL_sv_undef) |
527 | PROTOTYPE: $$ |
|
|
528 | PPCODE: |
1095 | PPCODE: |
529 | { |
1096 | { |
530 | if (!SvOK (tuple)) |
1097 | if (!SvOK (tuple)) |
531 | XSRETURN_NO; |
1098 | XSRETURN_NO; |
532 | |
1099 | |
533 | if (!SvROK (tuple) || SvTYPE (SvRV (tuple)) != SVt_PVAV) |
1100 | AV *av = ber_tuple (tuple); |
534 | croak ("ber_seq: tuple must be ber tuple (array-ref)"); |
|
|
535 | |
|
|
536 | AV *av = (AV *)SvRV (tuple); |
|
|
537 | |
1101 | |
538 | XPUSHs ( |
1102 | XPUSHs ( |
539 | SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL |
1103 | SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL |
540 | && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_OBJECT_IDENTIFIER |
1104 | && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_OBJECT_IDENTIFIER |
541 | && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) |
1105 | && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) |
542 | && sv_eq (AvARRAY (av)[BER_DATA], oid) |
1106 | && (!SvOK (oid) || sv_eq (AvARRAY (av)[BER_DATA], oid)) |
543 | ? &PL_sv_yes : &PL_sv_no); |
1107 | ? newSVsv (AvARRAY (av)[BER_DATA]) : &PL_sv_undef); |
544 | } |
1108 | } |
545 | |
1109 | |
|
|
1110 | ############################################################################# |
|
|
1111 | |
|
|
1112 | void |
|
|
1113 | ber_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 | |
|
|
1127 | SV * |
|
|
1128 | ber_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? |
|
|
1142 | SV * |
|
|
1143 | ber_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 | |
|
|
1156 | MODULE = Convert::BER::XS PACKAGE = Convert::BER::XS::Profile |
|
|
1157 | |
|
|
1158 | SV * |
|
|
1159 | new (SV *klass) |
|
|
1160 | CODE: |
|
|
1161 | RETVAL = profile_new (); |
|
|
1162 | OUTPUT: RETVAL |
|
|
1163 | |
|
|
1164 | void |
|
|
1165 | set (SV *profile, int klass, int tag, int type) |
|
|
1166 | CODE: |
|
|
1167 | profile_set (SvPROFILE (profile), klass, tag, type); |
|
|
1168 | |
|
|
1169 | IV |
|
|
1170 | get (SV *profile, int klass, int tag) |
|
|
1171 | CODE: |
|
|
1172 | RETVAL = profile_lookup (SvPROFILE (profile), klass, tag); |
|
|
1173 | OUTPUT: RETVAL |
|
|
1174 | |
|
|
1175 | void |
|
|
1176 | _set_default (SV *profile) |
|
|
1177 | CODE: |
|
|
1178 | default_profile = SvPROFILE (profile); |
|
|
1179 | |
|
|
1180 | |