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