3 * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
4 * Redistribution and modifications are permitted subject to BSD license.
6 #include <asn_internal.h>
7 #include <asn_codecs_prim.h>
11 * Decode an always-primitive type.
14 ber_decode_primitive(const asn_codec_ctx_t *opt_codec_ctx,
15 const asn_TYPE_descriptor_t *td, void **sptr,
16 const void *buf_ptr, size_t size, int tag_mode) {
17 ASN__PRIMITIVE_TYPE_t *st = (ASN__PRIMITIVE_TYPE_t *)*sptr;
19 ber_tlv_len_t length = 0; /* =0 to avoid [incorrect] warning. */
22 * If the structure is not there, allocate it.
25 st = (ASN__PRIMITIVE_TYPE_t *)CALLOC(1, sizeof(*st));
26 if(st == NULL) ASN__DECODE_FAILED;
30 ASN_DEBUG("Decoding %s as plain primitive (tm=%d)",
34 * Check tags and extract value length.
36 rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size,
37 tag_mode, 0, &length, 0);
38 if(rval.code != RC_OK)
41 ASN_DEBUG("%s length is %d bytes", td->name, (int)length);
44 * Make sure we have this length.
46 buf_ptr = ((const char *)buf_ptr) + rval.consumed;
47 size -= rval.consumed;
48 if(length > (ber_tlv_len_t)size) {
54 st->size = (int)length;
55 /* The following better be optimized away. */
56 if(sizeof(st->size) != sizeof(length)
57 && (ber_tlv_len_t)st->size != length) {
62 st->buf = (uint8_t *)MALLOC(length + 1);
68 memcpy(st->buf, buf_ptr, length);
69 st->buf[length] = '\0'; /* Just in case */
72 rval.consumed += length;
74 ASN_DEBUG("Took %ld/%ld bytes to encode %s",
76 (long)length, td->name);
82 * Encode an always-primitive type using DER.
85 der_encode_primitive(const asn_TYPE_descriptor_t *td, const void *sptr,
86 int tag_mode, ber_tlv_tag_t tag,
87 asn_app_consume_bytes_f *cb, void *app_key) {
88 asn_enc_rval_t erval = {0,0,0};
89 const ASN__PRIMITIVE_TYPE_t *st = (const ASN__PRIMITIVE_TYPE_t *)sptr;
91 ASN_DEBUG("%s %s as a primitive type (tm=%d)",
92 cb?"Encoding":"Estimating", td->name, tag_mode);
94 erval.encoded = der_write_tags(td, st->size, tag_mode, 0, tag,
96 ASN_DEBUG("%s wrote tags %d", td->name, (int)erval.encoded);
97 if(erval.encoded == -1) {
98 erval.failed_type = td;
99 erval.structure_ptr = sptr;
104 if(cb(st->buf, st->size, app_key) < 0) {
106 erval.failed_type = td;
107 erval.structure_ptr = sptr;
111 assert(st->buf || st->size == 0);
114 erval.encoded += st->size;
115 ASN__ENCODED_OK(erval);
119 ASN__PRIMITIVE_TYPE_free(const asn_TYPE_descriptor_t *td, void *sptr,
120 enum asn_struct_free_method method) {
121 ASN__PRIMITIVE_TYPE_t *st = (ASN__PRIMITIVE_TYPE_t *)sptr;
126 ASN_DEBUG("Freeing %s as a primitive type", td->name);
132 case ASFM_FREE_EVERYTHING:
135 case ASFM_FREE_UNDERLYING:
137 case ASFM_FREE_UNDERLYING_AND_RESET:
138 memset(sptr, 0, sizeof(ASN__PRIMITIVE_TYPE_t));
145 * Local internal type passed around as an argument.
148 const asn_TYPE_descriptor_t *type_descriptor;
150 xer_primitive_body_decoder_f *prim_body_decoder;
151 int decoded_something;
156 * Since some kinds of primitive values can be encoded using value-specific
157 * tags (<MINUS-INFINITY>, <enum-element>, etc), the primitive decoder must
158 * be supplied with such tags to parse them as needed.
161 xer_decode__unexpected_tag(void *key, const void *chunk_buf, size_t chunk_size) {
162 struct xdp_arg_s *arg = (struct xdp_arg_s *)key;
163 enum xer_pbd_rval bret;
166 * The chunk_buf is guaranteed to start at '<'.
168 assert(chunk_size && ((const char *)chunk_buf)[0] == 0x3c);
171 * Decoding was performed once already. Prohibit doing it again.
173 if(arg->decoded_something)
176 bret = arg->prim_body_decoder(arg->type_descriptor,
177 arg->struct_key, chunk_buf, chunk_size);
179 case XPBD_SYSTEM_FAILURE:
180 case XPBD_DECODER_LIMIT:
181 case XPBD_BROKEN_ENCODING:
183 case XPBD_BODY_CONSUMED:
184 /* Tag decoded successfully */
185 arg->decoded_something = 1;
187 case XPBD_NOT_BODY_IGNORE: /* Safe to proceed further */
195 xer_decode__primitive_body(void *key, const void *chunk_buf, size_t chunk_size, int have_more) {
196 struct xdp_arg_s *arg = (struct xdp_arg_s *)key;
197 enum xer_pbd_rval bret;
198 size_t lead_wsp_size;
200 if(arg->decoded_something) {
201 if(xer_whitespace_span(chunk_buf, chunk_size) == chunk_size) {
204 * "<INTEGER>123<!--/--> </INTEGER>"
205 * ^- chunk_buf position.
210 * Decoding was done once already. Prohibit doing it again.
217 * If we've received something like "1", we can't really
218 * tell whether it is really `1` or `123`, until we know
219 * that there is no more data coming.
220 * The have_more argument will be set to 1 once something
221 * like this is available to the caller of this callback:
228 lead_wsp_size = xer_whitespace_span(chunk_buf, chunk_size);
229 chunk_buf = (const char *)chunk_buf + lead_wsp_size;
230 chunk_size -= lead_wsp_size;
232 bret = arg->prim_body_decoder(arg->type_descriptor,
233 arg->struct_key, chunk_buf, chunk_size);
235 case XPBD_SYSTEM_FAILURE:
236 case XPBD_DECODER_LIMIT:
237 case XPBD_BROKEN_ENCODING:
239 case XPBD_BODY_CONSUMED:
240 /* Tag decoded successfully */
241 arg->decoded_something = 1;
243 case XPBD_NOT_BODY_IGNORE: /* Safe to proceed further */
244 return lead_wsp_size + chunk_size;
252 xer_decode_primitive(const asn_codec_ctx_t *opt_codec_ctx,
253 const asn_TYPE_descriptor_t *td, void **sptr,
254 size_t struct_size, const char *opt_mname,
255 const void *buf_ptr, size_t size,
256 xer_primitive_body_decoder_f *prim_body_decoder) {
257 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
258 asn_struct_ctx_t s_ctx;
259 struct xdp_arg_s s_arg;
263 * Create the structure if does not exist.
266 *sptr = CALLOC(1, struct_size);
267 if(!*sptr) ASN__DECODE_FAILED;
270 memset(&s_ctx, 0, sizeof(s_ctx));
271 s_arg.type_descriptor = td;
272 s_arg.struct_key = *sptr;
273 s_arg.prim_body_decoder = prim_body_decoder;
274 s_arg.decoded_something = 0;
277 rc = xer_decode_general(opt_codec_ctx, &s_ctx, &s_arg,
278 xml_tag, buf_ptr, size,
279 xer_decode__unexpected_tag, xer_decode__primitive_body);
282 if(!s_arg.decoded_something) {
284 ASN_DEBUG("Primitive body is not recognized, "
285 "supplying empty one");
287 * Decoding opportunity has come and gone.
288 * Where's the result?
289 * Try to feed with empty body, see if it eats it.
291 if(prim_body_decoder(s_arg.type_descriptor,
292 s_arg.struct_key, &ch, 0)
293 != XPBD_BODY_CONSUMED) {
295 * This decoder does not like empty stuff.
303 * Redo the whole thing later.
304 * We don't have a context to save intermediate parsing state.