blob: 97dc22afffe9233820763328ea85521853f6c477 [file] [log] [blame]
Lev Walkin4fd73182004-10-21 11:20:50 +00001/*-
2 * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
4 */
5#include <asn_internal.h>
6#include <asn_codecs_prim.h>
Lev Walkin4fd73182004-10-21 11:20:50 +00007#include <errno.h>
8
9/*
10 * Decode an always-primitive type.
11 */
12asn_dec_rval_t
13ber_decode_primitive(asn_codec_ctx_t *opt_codec_ctx,
14 asn_TYPE_descriptor_t *td,
Lev Walkin8c3b8542005-03-10 18:52:02 +000015 void **sptr, const void *buf_ptr, size_t size, int tag_mode) {
Lev Walkin4fd73182004-10-21 11:20:50 +000016 ASN__PRIMITIVE_TYPE_t *st = (ASN__PRIMITIVE_TYPE_t *)*sptr;
17 asn_dec_rval_t rval;
18 ber_tlv_len_t length;
19
20 /*
21 * If the structure is not there, allocate it.
22 */
23 if(st == NULL) {
Lev Walkin8484ed82004-12-14 13:31:01 +000024 st = (ASN__PRIMITIVE_TYPE_t *)CALLOC(1, sizeof(*st));
Lev Walkin4fd73182004-10-21 11:20:50 +000025 if(st == NULL) {
26 rval.code = RC_FAIL;
27 rval.consumed = 0;
28 return rval;
29 }
Lev Walkin8484ed82004-12-14 13:31:01 +000030 *sptr = (void *)st;
Lev Walkin4fd73182004-10-21 11:20:50 +000031 }
32
33 ASN_DEBUG("Decoding %s as plain primitive (tm=%d)",
34 td->name, tag_mode);
35
36 /*
37 * Check tags and extract value length.
38 */
39 rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size,
40 tag_mode, 0, &length, 0);
41 if(rval.code != RC_OK)
42 return rval;
43
44 ASN_DEBUG("%s length is %d bytes", td->name, (int)length);
45
46 /*
47 * Make sure we have this length.
48 */
Lev Walkin8c3b8542005-03-10 18:52:02 +000049 buf_ptr = ((const char *)buf_ptr) + rval.consumed;
Lev Walkin4fd73182004-10-21 11:20:50 +000050 size -= rval.consumed;
51 if(length > (ber_tlv_len_t)size) {
52 rval.code = RC_WMORE;
53 rval.consumed = 0;
54 return rval;
55 }
56
Lev Walkin4efbfb72005-02-25 14:20:30 +000057 st->size = (int)length;
58 /* The following better be optimized away. */
59 if(sizeof(st->size) != sizeof(length)
60 && (ber_tlv_len_t)st->size != length) {
61 st->size = 0;
62 rval.code = RC_FAIL;
63 rval.consumed = 0;
64 return rval;
65 }
66
Lev Walkin4fd73182004-10-21 11:20:50 +000067 st->buf = (uint8_t *)MALLOC(length + 1);
Lev Walkin4efbfb72005-02-25 14:20:30 +000068 if(!st->buf) {
69 st->size = 0;
Lev Walkin4fd73182004-10-21 11:20:50 +000070 rval.code = RC_FAIL;
71 rval.consumed = 0;
72 return rval;
73 }
74
75 memcpy(st->buf, buf_ptr, length);
76 st->buf[length] = '\0'; /* Just in case */
77
78 rval.code = RC_OK;
79 rval.consumed += length;
80
81 ASN_DEBUG("Took %ld/%ld bytes to encode %s",
82 (long)rval.consumed,
83 (long)length, td->name);
84
85 return rval;
86}
87
88/*
89 * Encode an always-primitive type using DER.
90 */
91asn_enc_rval_t
92der_encode_primitive(asn_TYPE_descriptor_t *td, void *sptr,
93 int tag_mode, ber_tlv_tag_t tag,
94 asn_app_consume_bytes_f *cb, void *app_key) {
95 asn_enc_rval_t erval;
96 ASN__PRIMITIVE_TYPE_t *st = (ASN__PRIMITIVE_TYPE_t *)sptr;
97
98 ASN_DEBUG("%s %s as a primitive type (tm=%d)",
99 cb?"Encoding":"Estimating", td->name, tag_mode);
100
101 erval.encoded = der_write_tags(td, st->size, tag_mode, 0, tag,
102 cb, app_key);
103 ASN_DEBUG("%s wrote tags %d", td->name, (int)erval.encoded);
104 if(erval.encoded == -1) {
105 erval.failed_type = td;
106 erval.structure_ptr = sptr;
107 return erval;
108 }
109
110 if(cb && st->buf) {
111 if(cb(st->buf, st->size, app_key) < 0) {
112 erval.encoded = -1;
113 erval.failed_type = td;
114 erval.structure_ptr = sptr;
115 return erval;
116 }
117 } else {
118 assert(st->buf || st->size == 0);
119 }
120
121 erval.encoded += st->size;
Lev Walkin59b176e2005-11-26 11:25:14 +0000122 _ASN_ENCODED_OK(erval);
Lev Walkin4fd73182004-10-21 11:20:50 +0000123}
124
125void
126ASN__PRIMITIVE_TYPE_free(asn_TYPE_descriptor_t *td, void *sptr,
127 int contents_only) {
128 ASN__PRIMITIVE_TYPE_t *st = (ASN__PRIMITIVE_TYPE_t *)sptr;
129
130 if(!td || !sptr)
131 return;
132
133 ASN_DEBUG("Freeing %s as a primitive type", td->name);
134
135 if(st->buf)
136 FREEMEM(st->buf);
137
138 if(!contents_only)
139 FREEMEM(st);
140}
141
142
143/*
144 * Local internal type passed around as an argument.
145 */
146struct xdp_arg_s {
Lev Walkine0b56e02005-02-25 12:10:27 +0000147 asn_TYPE_descriptor_t *type_descriptor;
Lev Walkin8471cec2004-10-21 14:02:19 +0000148 void *struct_key;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000149 xer_primitive_body_decoder_f *prim_body_decoder;
Lev Walkin4fd73182004-10-21 11:20:50 +0000150 int decoded_something;
151 int want_more;
152};
153
Lev Walkin0be3a992004-10-21 12:11:57 +0000154
Lev Walkin0fab1a62005-03-09 22:19:25 +0000155static int
156xer_decode__unexpected_tag(void *key, const void *chunk_buf, size_t chunk_size) {
Lev Walkin4fd73182004-10-21 11:20:50 +0000157 struct xdp_arg_s *arg = (struct xdp_arg_s *)key;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000158 enum xer_pbd_rval bret;
Lev Walkin4fd73182004-10-21 11:20:50 +0000159
160 if(arg->decoded_something) {
Lev Walkindde25b32004-10-22 08:17:16 +0000161 if(xer_is_whitespace(chunk_buf, chunk_size))
Lev Walkin4efbfb72005-02-25 14:20:30 +0000162 return 0; /* Skip it. */
Lev Walkin4fd73182004-10-21 11:20:50 +0000163 /*
164 * Decoding was done once already. Prohibit doing it again.
165 */
166 return -1;
167 }
168
Lev Walkin0fab1a62005-03-09 22:19:25 +0000169 bret = arg->prim_body_decoder(arg->type_descriptor,
Lev Walkine0b56e02005-02-25 12:10:27 +0000170 arg->struct_key, chunk_buf, chunk_size);
Lev Walkin0fab1a62005-03-09 22:19:25 +0000171 switch(bret) {
172 case XPBD_SYSTEM_FAILURE:
173 case XPBD_DECODER_LIMIT:
174 case XPBD_BROKEN_ENCODING:
175 break;
176 case XPBD_BODY_CONSUMED:
Lev Walkin4fd73182004-10-21 11:20:50 +0000177 /* Tag decoded successfully */
178 arg->decoded_something = 1;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000179 /* Fall through */
180 case XPBD_NOT_BODY_IGNORE: /* Safe to proceed further */
Lev Walkin4fd73182004-10-21 11:20:50 +0000181 return 0;
182 }
Lev Walkin0fab1a62005-03-09 22:19:25 +0000183
184 return -1;
Lev Walkin4fd73182004-10-21 11:20:50 +0000185}
186
187static ssize_t
Lev Walkin0fab1a62005-03-09 22:19:25 +0000188xer_decode__body(void *key, const void *chunk_buf, size_t chunk_size, int have_more) {
Lev Walkin4fd73182004-10-21 11:20:50 +0000189 struct xdp_arg_s *arg = (struct xdp_arg_s *)key;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000190 enum xer_pbd_rval bret;
Lev Walkin4fd73182004-10-21 11:20:50 +0000191
192 if(arg->decoded_something) {
Lev Walkindde25b32004-10-22 08:17:16 +0000193 if(xer_is_whitespace(chunk_buf, chunk_size))
Lev Walkin0be3a992004-10-21 12:11:57 +0000194 return chunk_size;
Lev Walkin4fd73182004-10-21 11:20:50 +0000195 /*
196 * Decoding was done once already. Prohibit doing it again.
197 */
198 return -1;
199 }
200
201 if(!have_more) {
202 /*
203 * If we've received something like "1", we can't really
204 * tell whether it is really `1` or `123`, until we know
205 * that there is no more data coming.
206 * The have_more argument will be set to 1 once something
207 * like this is available to the caller of this callback:
208 * "1<tag_start..."
209 */
210 arg->want_more = 1;
211 return -1;
212 }
213
Lev Walkin0fab1a62005-03-09 22:19:25 +0000214 bret = arg->prim_body_decoder(arg->type_descriptor,
Lev Walkine0b56e02005-02-25 12:10:27 +0000215 arg->struct_key, chunk_buf, chunk_size);
Lev Walkin0fab1a62005-03-09 22:19:25 +0000216 switch(bret) {
217 case XPBD_SYSTEM_FAILURE:
218 case XPBD_DECODER_LIMIT:
219 case XPBD_BROKEN_ENCODING:
220 break;
221 case XPBD_BODY_CONSUMED:
222 /* Tag decoded successfully */
Lev Walkin4fd73182004-10-21 11:20:50 +0000223 arg->decoded_something = 1;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000224 /* Fall through */
225 case XPBD_NOT_BODY_IGNORE: /* Safe to proceed further */
226 return chunk_size;
Lev Walkin4fd73182004-10-21 11:20:50 +0000227 }
Lev Walkin0fab1a62005-03-09 22:19:25 +0000228
229 return -1;
Lev Walkin4fd73182004-10-21 11:20:50 +0000230}
231
232
233asn_dec_rval_t
234xer_decode_primitive(asn_codec_ctx_t *opt_codec_ctx,
235 asn_TYPE_descriptor_t *td,
Lev Walkin8471cec2004-10-21 14:02:19 +0000236 void **sptr,
237 size_t struct_size,
Lev Walkin4fd73182004-10-21 11:20:50 +0000238 const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000239 const void *buf_ptr, size_t size,
Lev Walkin0fab1a62005-03-09 22:19:25 +0000240 xer_primitive_body_decoder_f *prim_body_decoder
Lev Walkin4fd73182004-10-21 11:20:50 +0000241) {
242 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
243 asn_struct_ctx_t s_ctx;
244 struct xdp_arg_s s_arg;
245 asn_dec_rval_t rc;
246
247 /*
248 * Create the structure if does not exist.
249 */
250 if(!*sptr) {
Lev Walkin8471cec2004-10-21 14:02:19 +0000251 *sptr = CALLOC(1, struct_size);
Lev Walkin4fd73182004-10-21 11:20:50 +0000252 if(!*sptr) {
253 asn_dec_rval_t rval;
254 rval.code = RC_FAIL;
255 rval.consumed = 0;
256 return rval;
257 }
258 }
259
260 memset(&s_ctx, 0, sizeof(s_ctx));
Lev Walkine0b56e02005-02-25 12:10:27 +0000261 s_arg.type_descriptor = td;
Lev Walkin8471cec2004-10-21 14:02:19 +0000262 s_arg.struct_key = *sptr;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000263 s_arg.prim_body_decoder = prim_body_decoder;
Lev Walkin4fd73182004-10-21 11:20:50 +0000264 s_arg.decoded_something = 0;
265 s_arg.want_more = 0;
266
267 rc = xer_decode_general(opt_codec_ctx, &s_ctx, &s_arg,
268 xml_tag, buf_ptr, size,
269 xer_decode__unexpected_tag, xer_decode__body);
270 switch(rc.code) {
271 case RC_OK:
272 if(!s_arg.decoded_something) {
Lev Walkin36b8b822004-10-23 11:19:43 +0000273 char ch;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000274 ASN_DEBUG("Primitive body is not recognized, "
275 "supplying empty one");
276 /*
277 * Decoding opportunity has come and gone.
278 * Where's the result?
279 * Try to feed with empty body, see if it eats it.
280 */
281 if(prim_body_decoder(s_arg.type_descriptor,
282 s_arg.struct_key, &ch, 0)
283 != XPBD_BODY_CONSUMED) {
Lev Walkin28eee292004-10-23 10:17:34 +0000284 /*
285 * This decoder does not like empty stuff.
286 */
287 rc.code = RC_FAIL;
288 rc.consumed = 0;
289 }
Lev Walkin4fd73182004-10-21 11:20:50 +0000290 }
291 break;
292 case RC_WMORE:
293 /*
294 * Redo the whole thing later.
295 * We don't have a context to save intermediate parsing state.
296 */
297 rc.consumed = 0;
298 break;
299 case RC_FAIL:
300 rc.consumed = 0;
301 if(s_arg.want_more)
302 rc.code = RC_WMORE;
303 break;
304 }
305 return rc;
306}
307