2 * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
4 * Redistribution and modifications are permitted subject to BSD license.
6 #include <asn_internal.h>
7 #include <constr_CHOICE.h>
10 * Number of bytes left for this structure.
11 * (ctx->left) indicates the number of bytes _transferred_ for the structure.
12 * (size) contains the number of bytes in the buffer passed.
14 #define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left)
17 * If the subprocessor function returns with an indication that it wants
18 * more data, it may well be a fatal decoding problem, because the
19 * size is constrained by the <TLV>'s L, even if the buffer size allows
21 * For example, consider the buffer containing the following TLVs:
22 * <T:5><L:1><V> <T:6>...
23 * The TLV length clearly indicates that one byte is expected in V, but
24 * if the V processor returns with "want more data" even if the buffer
25 * contains way more data than the V processor have seen.
27 #define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size)
30 * This macro "eats" the part of the buffer which is definitely "consumed",
31 * i.e. was correctly converted into local representation or rightfully skipped.
34 #define ADVANCE(num_bytes) \
36 size_t num = num_bytes; \
37 ptr = ((const char *)ptr) + num; \
41 consumed_myself += num; \
45 * Switch to the next phase of parsing.
48 #define NEXT_PHASE(ctx) \
55 * Return a standardized complex structure.
58 #define RETURN(_code) \
61 rval.consumed = consumed_myself; \
66 * Tags are canonically sorted in the tag to member table.
69 _search4tag(const void *ap, const void *bp) {
70 const asn_TYPE_tag2member_t *a = (const asn_TYPE_tag2member_t *)ap;
71 const asn_TYPE_tag2member_t *b = (const asn_TYPE_tag2member_t *)bp;
73 int a_class = BER_TAG_CLASS(a->el_tag);
74 int b_class = BER_TAG_CLASS(b->el_tag);
76 if(a_class == b_class) {
77 ber_tlv_tag_t a_value = BER_TAG_VALUE(a->el_tag);
78 ber_tlv_tag_t b_value = BER_TAG_VALUE(b->el_tag);
80 if(a_value == b_value)
82 else if(a_value < b_value)
86 } else if(a_class < b_class) {
94 * The decoder of the CHOICE type.
97 CHOICE_decode_ber(const asn_codec_ctx_t *opt_codec_ctx,
98 const asn_TYPE_descriptor_t *td, void **struct_ptr,
99 const void *ptr, size_t size, int tag_mode) {
101 * Bring closer parts of structure description.
103 const asn_CHOICE_specifics_t *specs =
104 (const asn_CHOICE_specifics_t *)td->specifics;
105 asn_TYPE_member_t *elements = td->elements;
108 * Parts of the structure being constructed.
110 void *st = *struct_ptr; /* Target structure. */
111 asn_struct_ctx_t *ctx; /* Decoder context */
113 ber_tlv_tag_t tlv_tag; /* T from TLV */
114 ssize_t tag_len; /* Length of TLV's T */
115 asn_dec_rval_t rval; /* Return code from subparsers */
117 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
119 ASN_DEBUG("Decoding %s as CHOICE", td->name);
122 * Create the target structure if it is not present already.
125 st = *struct_ptr = CALLOC(1, specs->struct_size);
132 * Restore parsing context.
134 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
137 * Start to parse where left previously
143 * Check that the set of tags associated with given structure
144 * perfectly fits our expectations.
147 if(tag_mode || td->tags_count) {
148 rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size,
149 tag_mode, -1, &ctx->left, 0);
150 if(rval.code != RC_OK) {
151 ASN_DEBUG("%s tagging check failed: %d",
152 td->name, rval.code);
157 /* ?Subtracted below! */
158 ctx->left += rval.consumed;
160 ADVANCE(rval.consumed);
167 ASN_DEBUG("Structure consumes %ld bytes, buffer %ld",
168 (long)ctx->left, (long)size);
173 * Fetch the T from TLV.
175 tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
176 ASN_DEBUG("In %s CHOICE tag length %d", td->name, (int)tag_len);
178 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
180 case -1: RETURN(RC_FAIL);
184 const asn_TYPE_tag2member_t *t2m;
185 asn_TYPE_tag2member_t key;
187 key.el_tag = tlv_tag;
188 t2m = (const asn_TYPE_tag2member_t *)bsearch(&key,
189 specs->tag2el, specs->tag2el_count,
190 sizeof(specs->tag2el[0]), _search4tag);
193 * Found the element corresponding to the tag.
196 ctx->step = t2m->el_no;
198 } else if(specs->ext_start == -1) {
199 ASN_DEBUG("Unexpected tag %s "
200 "in non-extensible CHOICE %s",
201 ber_tlv_tag_string(tlv_tag), td->name);
207 ASN_DEBUG("Skipping unknown tag %s",
208 ber_tlv_tag_string(tlv_tag));
210 skip = ber_skip_length(opt_codec_ctx,
211 BER_TLV_CONSTRUCTED(ptr),
212 (const char *)ptr + tag_len,
216 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
218 case -1: RETURN(RC_FAIL);
221 ADVANCE(skip + tag_len);
229 * Read in the element.
232 asn_TYPE_member_t *elm;/* CHOICE's element */
233 void *memb_ptr; /* Pointer to the member */
234 void **memb_ptr2; /* Pointer to that pointer */
236 elm = &elements[ctx->step];
239 * Compute the position of the member inside a structure,
240 * and also a type of containment (it may be contained
241 * as pointer or using inline inclusion).
243 if(elm->flags & ATF_POINTER) {
244 /* Member is a pointer to another structure */
245 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
248 * A pointer to a pointer
249 * holding the start of the structure
251 memb_ptr = (char *)st + elm->memb_offset;
252 memb_ptr2 = &memb_ptr;
254 /* Set presence to be able to free it properly at any time */
255 _set_present_idx(st, specs->pres_offset,
256 specs->pres_size, ctx->step + 1);
258 * Invoke the member fetch routine according to member's type
260 rval = elm->type->op->ber_decoder(opt_codec_ctx, elm->type,
261 memb_ptr2, ptr, LEFT,
266 case RC_WMORE: /* More data expected */
267 if(!SIZE_VIOLATION) {
268 ADVANCE(rval.consumed);
272 case RC_FAIL: /* Fatal error */
276 ADVANCE(rval.consumed);
283 ASN_DEBUG("CHOICE %s Leftover: %ld, size = %ld, tm=%d, tc=%d",
284 td->name, (long)ctx->left, (long)size,
285 tag_mode, td->tags_count);
289 * The type must be fully decoded
290 * by the CHOICE member-specific decoder.
296 && !(tag_mode || td->tags_count)) {
298 * This is an untagged CHOICE.
299 * It doesn't contain nothing
300 * except for the member itself, including all its tags.
301 * The decoding is completed.
308 * Read in the "end of data chunks"'s.
310 while(ctx->left < 0) {
313 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
315 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
317 case -1: RETURN(RC_FAIL);
323 if(((const uint8_t *)ptr)[0] == 0) {
329 } else if(((const uint8_t *)ptr)[1] == 0) {
331 * Correctly finished with <0><0>.
338 ASN_DEBUG("Unexpected continuation in %s",
348 /* No meaningful work here */
356 CHOICE_encode_der(const asn_TYPE_descriptor_t *td, const void *sptr,
357 int tag_mode, ber_tlv_tag_t tag, asn_app_consume_bytes_f *cb,
359 const asn_CHOICE_specifics_t *specs = (const asn_CHOICE_specifics_t *)td->specifics;
360 asn_TYPE_member_t *elm; /* CHOICE element */
361 asn_enc_rval_t erval = {0,0,0};
362 const void *memb_ptr;
363 size_t computed_size = 0;
366 if(!sptr) ASN__ENCODE_FAILED;
368 ASN_DEBUG("%s %s as CHOICE",
369 cb ? "Encoding" : "Estimating", td->name);
371 present = _fetch_present_idx(sptr,
372 specs->pres_offset, specs->pres_size);
375 * If the structure was not initialized, it cannot be encoded:
376 * can't deduce what to encode in the choice type.
378 if(present == 0 || present > td->elements_count) {
379 if(present == 0 && td->elements_count == 0) {
380 /* The CHOICE is empty?! */
382 ASN__ENCODED_OK(erval);
388 * Seek over the present member of the structure.
390 elm = &td->elements[present-1];
391 if(elm->flags & ATF_POINTER) {
393 *(const void *const *)((const char *)sptr + elm->memb_offset);
397 ASN__ENCODED_OK(erval);
399 /* Mandatory element absent */
403 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
407 * If the CHOICE itself is tagged EXPLICIT:
408 * T ::= [2] EXPLICIT CHOICE { ... }
409 * Then emit the appropriate tags.
411 if(tag_mode == 1 || td->tags_count) {
413 * For this, we need to pre-compute the member.
417 /* Encode member with its tag */
418 erval = elm->type->op->der_encoder(elm->type, memb_ptr,
421 if(erval.encoded == -1)
424 /* Encode CHOICE with parent or my own tag */
425 ret = der_write_tags(td, erval.encoded, tag_mode, 1, tag,
429 computed_size += ret;
433 * Encode the single underlying member.
435 erval = elm->type->op->der_encoder(elm->type, memb_ptr,
436 elm->tag_mode, elm->tag,
438 if(erval.encoded == -1)
441 ASN_DEBUG("Encoded CHOICE member in %ld bytes (+%ld)",
442 (long)erval.encoded, (long)computed_size);
444 erval.encoded += computed_size;