2 * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
4 * Redistribution and modifications are permitted subject to BSD license.
6 #ifndef ASN_DISABLE_OER_SUPPORT
8 #include <asn_internal.h>
9 #include <constr_CHOICE.h>
13 * Return a standardized complex structure.
16 #define RETURN(_code) \
18 asn_dec_rval_t rval; \
20 rval.consumed = consumed_myself; \
25 #define ADVANCE(num_bytes) \
27 size_t num = num_bytes; \
28 ptr = ((const char *)ptr) + num; \
30 consumed_myself += num; \
34 * Switch to the next phase of parsing.
37 #define NEXT_PHASE(ctx) \
43 #define SET_PHASE(ctx, value) \
50 * Tags are canonically sorted in the tag to member table.
53 _search4tag(const void *ap, const void *bp) {
54 const asn_TYPE_tag2member_t *a = (const asn_TYPE_tag2member_t *)ap;
55 const asn_TYPE_tag2member_t *b = (const asn_TYPE_tag2member_t *)bp;
57 int a_class = BER_TAG_CLASS(a->el_tag);
58 int b_class = BER_TAG_CLASS(b->el_tag);
60 if(a_class == b_class) {
61 ber_tlv_tag_t a_value = BER_TAG_VALUE(a->el_tag);
62 ber_tlv_tag_t b_value = BER_TAG_VALUE(b->el_tag);
64 if(a_value == b_value)
66 else if(a_value < b_value)
70 } else if(a_class < b_class) {
78 * X.696 (08/2015) #8.7 Encoding of tags
81 oer_fetch_tag(const void *ptr, size_t size, ber_tlv_tag_t *tag_r) {
89 val = *(const uint8_t *)ptr;
91 if((val & 0x3F) != 0x3F) {
93 *tag_r = ((val & 0x3F) << 2) | tclass;
98 * Each octet contains 7 bits of useful information.
99 * The MSB is 0 if it is the last octet of the tag.
101 for(val = 0, ptr = ((const char *)ptr) + 1, skipped = 2; skipped <= size;
102 ptr = ((const char *)ptr) + 1, skipped++) {
103 unsigned int oct = *(const uint8_t *)ptr;
105 val = (val << 7) | (oct & 0x7F);
107 * Make sure there are at least 9 bits spare
108 * at the MS side of a value.
110 if(val >> ((8 * sizeof(val)) - 9)) {
112 * We would not be able to accomodate
118 val = (val << 7) | oct;
119 *tag_r = (val << 2) | tclass;
124 return 0; /* Want more */
128 CHOICE_decode_oer(const asn_codec_ctx_t *opt_codec_ctx,
129 const asn_TYPE_descriptor_t *td,
130 const asn_oer_constraints_t *constraints, void **struct_ptr,
131 const void *ptr, size_t size) {
133 * Bring closer parts of structure description.
135 const asn_CHOICE_specifics_t *specs =
136 (const asn_CHOICE_specifics_t *)td->specifics;
137 asn_TYPE_member_t *elements = td->elements;
140 * Parts of the structure being constructed.
142 void *st = *struct_ptr; /* Target structure. */
143 asn_struct_ctx_t *ctx; /* Decoder context */
145 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
149 ASN_DEBUG("Decoding %s as CHOICE", td->name);
152 * Create the target structure if it is not present already.
155 st = *struct_ptr = CALLOC(1, specs->struct_size);
162 * Restore parsing context.
164 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
170 ber_tlv_tag_t tlv_tag; /* T from TLV */
171 ssize_t tag_len; /* Length of TLV's T */
173 tag_len = oer_fetch_tag(ptr, size, &tlv_tag);
182 const asn_TYPE_tag2member_t *t2m;
183 asn_TYPE_tag2member_t key = {0, 0, 0, 0};
184 key.el_tag = tlv_tag;
186 t2m = (const asn_TYPE_tag2member_t *)bsearch(
187 &key, specs->tag2el, specs->tag2el_count,
188 sizeof(specs->tag2el[0]), _search4tag);
191 * Found the element corresponding to the tag.
194 ctx->step = t2m->el_no;
196 } else if(specs->ext_start == -1) {
199 "in non-extensible CHOICE %s",
200 ber_tlv_tag_string(tlv_tag), td->name);
203 /* Skip open type extension */
205 "Not implemented skipping open type extension for tag %s",
206 ber_tlv_tag_string(tlv_tag));
216 asn_TYPE_member_t *elm = &elements[ctx->step]; /* CHOICE's element */
217 void *memb_ptr; /* Pointer to the member */
218 void **memb_ptr2; /* Pointer to that pointer */
222 * Compute the position of the member inside a structure,
223 * and also a type of containment (it may be contained
224 * as pointer or using inline inclusion).
226 if(elm->flags & ATF_POINTER) {
227 /* Member is a pointer to another structure */
228 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
231 * A pointer to a pointer
232 * holding the start of the structure
234 memb_ptr = (char *)st + elm->memb_offset;
235 memb_ptr2 = &memb_ptr;
238 /* Set presence to be able to free it properly at any time */
239 (void)CHOICE_variant_set_presence(td, st, ctx->step + 1);
241 if(specs->ext_start >= 0 && specs->ext_start <= ctx->step) {
243 oer_open_type_get(opt_codec_ctx, elm->type,
244 elm->encoding_constraints.oer_constraints,
245 memb_ptr2, ptr, size);
246 if(got < 0) ASN__DECODE_FAILED;
247 if(got == 0) ASN__DECODE_STARVED;
251 rval = elm->type->op->oer_decoder(
252 opt_codec_ctx, elm->type,
253 elm->encoding_constraints.oer_constraints, memb_ptr2, ptr,
256 rval.consumed += consumed_myself;
263 SET_PHASE(ctx, 3); /* => 3 */
268 /* Already decoded everything */
271 /* Failed to decode, after all */
279 * X.696 (08/2015) #8.7 Encoding of tags
282 oer_put_tag(ber_tlv_tag_t tag, asn_app_consume_bytes_f *cb, void *app_key) {
283 uint8_t tclass = BER_TAG_CLASS(tag);
284 ber_tlv_tag_t tval = BER_TAG_VALUE(tag);
287 uint8_t b = (uint8_t)((tclass << 6) | tval);
288 if(cb(&b, 1, app_key) < 0) {
293 uint8_t buf[1 + 2 * sizeof(tval)];
294 uint8_t *b = &buf[sizeof(buf)-1]; /* Last addressable */
296 for(; ; tval >>= 7) {
298 *b-- = 0x80 | (tval & 0x7f);
304 *b = (uint8_t)((tclass << 6) | 0x3F);
305 encoded = sizeof(buf) - (b - buf);
306 if(cb(b, encoded, app_key) < 0) {
315 * Encode as Canonical OER.
318 CHOICE_encode_oer(const asn_TYPE_descriptor_t *td,
319 const asn_oer_constraints_t *constraints, const void *sptr,
320 asn_app_consume_bytes_f *cb, void *app_key) {
321 const asn_CHOICE_specifics_t *specs =
322 (const asn_CHOICE_specifics_t *)td->specifics;
323 asn_TYPE_member_t *elm; /* CHOICE element */
325 const void *memb_ptr;
328 asn_enc_rval_t er = {0, 0, 0};
332 if(!sptr) ASN__ENCODE_FAILED;
334 ASN_DEBUG("OER %s encoding as CHOICE", td->name);
336 present = CHOICE_variant_get_presence(td, sptr);
337 if(present == 0 || present > td->elements_count) {
338 ASN_DEBUG("CHOICE %s member is not selected", td->name);
342 elm = &td->elements[present-1];
343 if(elm->flags & ATF_POINTER) {
345 *(const void *const *)((const char *)sptr + elm->memb_offset);
347 /* Mandatory element absent */
351 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
354 tag = asn_TYPE_outmost_tag(elm->type, memb_ptr, elm->tag_mode, elm->tag);
359 tag_len = oer_put_tag(tag, cb, app_key);
364 if(specs->ext_start >= 0 && (unsigned)specs->ext_start <= (present-1)) {
365 ssize_t encoded = oer_open_type_put(elm->type,
366 elm->encoding_constraints.oer_constraints,
367 memb_ptr, cb, app_key);
368 if(encoded < 0) ASN__ENCODE_FAILED;
369 er.encoded = tag_len + encoded;
371 er = elm->type->op->oer_encoder(
372 elm->type, elm->encoding_constraints.oer_constraints, memb_ptr, cb,
374 if(er.encoded >= 0) er.encoded += tag_len;
380 #endif /* ASN_DISABLE_OER_SUPPORT */