blob: b599bdfaeaf654aaa9b2ec1993b4b1a9139cce6b [file] [log] [blame]
vlmc4436cf2004-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>
vlmc4436cf2004-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,
vlmb02dcc62005-03-10 18:52:02 +000015 void **sptr, const void *buf_ptr, size_t size, int tag_mode) {
vlmc4436cf2004-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) {
vlm82e317b2004-12-14 13:31:01 +000024 st = (ASN__PRIMITIVE_TYPE_t *)CALLOC(1, sizeof(*st));
vlmc4436cf2004-10-21 11:20:50 +000025 if(st == NULL) {
26 rval.code = RC_FAIL;
27 rval.consumed = 0;
28 return rval;
29 }
vlm82e317b2004-12-14 13:31:01 +000030 *sptr = (void *)st;
vlmc4436cf2004-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 */
vlmb02dcc62005-03-10 18:52:02 +000049 buf_ptr = ((const char *)buf_ptr) + rval.consumed;
vlmc4436cf2004-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
vlm8a09e0f2005-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
vlmc4436cf2004-10-21 11:20:50 +000067 st->buf = (uint8_t *)MALLOC(length + 1);
vlm8a09e0f2005-02-25 14:20:30 +000068 if(!st->buf) {
69 st->size = 0;
vlmc4436cf2004-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;
vlm4df95e52005-07-03 05:28:29 +0000122 erval.structure_ptr = 0;
123 erval.failed_type = 0;
vlmc4436cf2004-10-21 11:20:50 +0000124 return erval;
125}
126
127void
128ASN__PRIMITIVE_TYPE_free(asn_TYPE_descriptor_t *td, void *sptr,
129 int contents_only) {
130 ASN__PRIMITIVE_TYPE_t *st = (ASN__PRIMITIVE_TYPE_t *)sptr;
131
132 if(!td || !sptr)
133 return;
134
135 ASN_DEBUG("Freeing %s as a primitive type", td->name);
136
137 if(st->buf)
138 FREEMEM(st->buf);
139
140 if(!contents_only)
141 FREEMEM(st);
142}
143
144
145/*
146 * Local internal type passed around as an argument.
147 */
148struct xdp_arg_s {
vlm80a48592005-02-25 12:10:27 +0000149 asn_TYPE_descriptor_t *type_descriptor;
vlmb848b2a2004-10-21 14:02:19 +0000150 void *struct_key;
vlm4df9cc12005-03-09 22:19:25 +0000151 xer_primitive_body_decoder_f *prim_body_decoder;
vlmc4436cf2004-10-21 11:20:50 +0000152 int decoded_something;
153 int want_more;
154};
155
vlme4f9cac2004-10-21 12:11:57 +0000156
vlm4df9cc12005-03-09 22:19:25 +0000157static int
158xer_decode__unexpected_tag(void *key, const void *chunk_buf, size_t chunk_size) {
vlmc4436cf2004-10-21 11:20:50 +0000159 struct xdp_arg_s *arg = (struct xdp_arg_s *)key;
vlm4df9cc12005-03-09 22:19:25 +0000160 enum xer_pbd_rval bret;
vlmc4436cf2004-10-21 11:20:50 +0000161
162 if(arg->decoded_something) {
vlm2a4245f2004-10-22 08:17:16 +0000163 if(xer_is_whitespace(chunk_buf, chunk_size))
vlm8a09e0f2005-02-25 14:20:30 +0000164 return 0; /* Skip it. */
vlmc4436cf2004-10-21 11:20:50 +0000165 /*
166 * Decoding was done once already. Prohibit doing it again.
167 */
168 return -1;
169 }
170
vlm4df9cc12005-03-09 22:19:25 +0000171 bret = arg->prim_body_decoder(arg->type_descriptor,
vlm80a48592005-02-25 12:10:27 +0000172 arg->struct_key, chunk_buf, chunk_size);
vlm4df9cc12005-03-09 22:19:25 +0000173 switch(bret) {
174 case XPBD_SYSTEM_FAILURE:
175 case XPBD_DECODER_LIMIT:
176 case XPBD_BROKEN_ENCODING:
177 break;
178 case XPBD_BODY_CONSUMED:
vlmc4436cf2004-10-21 11:20:50 +0000179 /* Tag decoded successfully */
180 arg->decoded_something = 1;
vlm4df9cc12005-03-09 22:19:25 +0000181 /* Fall through */
182 case XPBD_NOT_BODY_IGNORE: /* Safe to proceed further */
vlmc4436cf2004-10-21 11:20:50 +0000183 return 0;
184 }
vlm4df9cc12005-03-09 22:19:25 +0000185
186 return -1;
vlmc4436cf2004-10-21 11:20:50 +0000187}
188
189static ssize_t
vlm4df9cc12005-03-09 22:19:25 +0000190xer_decode__body(void *key, const void *chunk_buf, size_t chunk_size, int have_more) {
vlmc4436cf2004-10-21 11:20:50 +0000191 struct xdp_arg_s *arg = (struct xdp_arg_s *)key;
vlm4df9cc12005-03-09 22:19:25 +0000192 enum xer_pbd_rval bret;
vlmc4436cf2004-10-21 11:20:50 +0000193
194 if(arg->decoded_something) {
vlm2a4245f2004-10-22 08:17:16 +0000195 if(xer_is_whitespace(chunk_buf, chunk_size))
vlme4f9cac2004-10-21 12:11:57 +0000196 return chunk_size;
vlmc4436cf2004-10-21 11:20:50 +0000197 /*
198 * Decoding was done once already. Prohibit doing it again.
199 */
200 return -1;
201 }
202
203 if(!have_more) {
204 /*
205 * If we've received something like "1", we can't really
206 * tell whether it is really `1` or `123`, until we know
207 * that there is no more data coming.
208 * The have_more argument will be set to 1 once something
209 * like this is available to the caller of this callback:
210 * "1<tag_start..."
211 */
212 arg->want_more = 1;
213 return -1;
214 }
215
vlm4df9cc12005-03-09 22:19:25 +0000216 bret = arg->prim_body_decoder(arg->type_descriptor,
vlm80a48592005-02-25 12:10:27 +0000217 arg->struct_key, chunk_buf, chunk_size);
vlm4df9cc12005-03-09 22:19:25 +0000218 switch(bret) {
219 case XPBD_SYSTEM_FAILURE:
220 case XPBD_DECODER_LIMIT:
221 case XPBD_BROKEN_ENCODING:
222 break;
223 case XPBD_BODY_CONSUMED:
224 /* Tag decoded successfully */
vlmc4436cf2004-10-21 11:20:50 +0000225 arg->decoded_something = 1;
vlm4df9cc12005-03-09 22:19:25 +0000226 /* Fall through */
227 case XPBD_NOT_BODY_IGNORE: /* Safe to proceed further */
228 return chunk_size;
vlmc4436cf2004-10-21 11:20:50 +0000229 }
vlm4df9cc12005-03-09 22:19:25 +0000230
231 return -1;
vlmc4436cf2004-10-21 11:20:50 +0000232}
233
234
235asn_dec_rval_t
236xer_decode_primitive(asn_codec_ctx_t *opt_codec_ctx,
237 asn_TYPE_descriptor_t *td,
vlmb848b2a2004-10-21 14:02:19 +0000238 void **sptr,
239 size_t struct_size,
vlmc4436cf2004-10-21 11:20:50 +0000240 const char *opt_mname,
vlmb02dcc62005-03-10 18:52:02 +0000241 const void *buf_ptr, size_t size,
vlm4df9cc12005-03-09 22:19:25 +0000242 xer_primitive_body_decoder_f *prim_body_decoder
vlmc4436cf2004-10-21 11:20:50 +0000243) {
244 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
245 asn_struct_ctx_t s_ctx;
246 struct xdp_arg_s s_arg;
247 asn_dec_rval_t rc;
248
249 /*
250 * Create the structure if does not exist.
251 */
252 if(!*sptr) {
vlmb848b2a2004-10-21 14:02:19 +0000253 *sptr = CALLOC(1, struct_size);
vlmc4436cf2004-10-21 11:20:50 +0000254 if(!*sptr) {
255 asn_dec_rval_t rval;
256 rval.code = RC_FAIL;
257 rval.consumed = 0;
258 return rval;
259 }
260 }
261
262 memset(&s_ctx, 0, sizeof(s_ctx));
vlm80a48592005-02-25 12:10:27 +0000263 s_arg.type_descriptor = td;
vlmb848b2a2004-10-21 14:02:19 +0000264 s_arg.struct_key = *sptr;
vlm4df9cc12005-03-09 22:19:25 +0000265 s_arg.prim_body_decoder = prim_body_decoder;
vlmc4436cf2004-10-21 11:20:50 +0000266 s_arg.decoded_something = 0;
267 s_arg.want_more = 0;
268
269 rc = xer_decode_general(opt_codec_ctx, &s_ctx, &s_arg,
270 xml_tag, buf_ptr, size,
271 xer_decode__unexpected_tag, xer_decode__body);
272 switch(rc.code) {
273 case RC_OK:
274 if(!s_arg.decoded_something) {
vlm64f1ba22004-10-23 11:19:43 +0000275 char ch;
vlm4df9cc12005-03-09 22:19:25 +0000276 ASN_DEBUG("Primitive body is not recognized, "
277 "supplying empty one");
278 /*
279 * Decoding opportunity has come and gone.
280 * Where's the result?
281 * Try to feed with empty body, see if it eats it.
282 */
283 if(prim_body_decoder(s_arg.type_descriptor,
284 s_arg.struct_key, &ch, 0)
285 != XPBD_BODY_CONSUMED) {
vlmf7de4de2004-10-23 10:17:34 +0000286 /*
287 * This decoder does not like empty stuff.
288 */
289 rc.code = RC_FAIL;
290 rc.consumed = 0;
291 }
vlmc4436cf2004-10-21 11:20:50 +0000292 }
293 break;
294 case RC_WMORE:
295 /*
296 * Redo the whole thing later.
297 * We don't have a context to save intermediate parsing state.
298 */
299 rc.consumed = 0;
300 break;
301 case RC_FAIL:
302 rc.consumed = 0;
303 if(s_arg.want_more)
304 rc.code = RC_WMORE;
305 break;
306 }
307 return rc;
308}
309