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_SEQUENCE.h>
10 #include <OPEN_TYPE.h>
14 * This macro "eats" the part of the buffer which is definitely "consumed",
15 * i.e. was correctly converted into local representation or rightfully skipped.
18 #define ADVANCE(num_bytes) \
20 size_t num = num_bytes; \
21 ptr = ((const char *)ptr) + num; \
23 consumed_myself += num; \
27 * Switch to the next phase of parsing.
31 #define NEXT_PHASE(ctx) \
38 * Check whether we are inside the extensions group.
40 #define IN_EXTENSION_GROUP(specs, memb_idx) \
41 ((specs)->first_extension >= 0 \
42 && (unsigned)(specs)->first_extension <= (memb_idx))
44 #define IN_ROOT_GROUP_PRED(edx) \
45 edx < (specs->first_extension < 0 ? td->elements_count \
46 : (size_t)specs->first_extension)
48 #define FOR_IN_ROOT_GROUP(edx) for(edx = 0; IN_ROOT_GROUP_PRED(edx); edx++)
51 * Return a standardized complex structure.
54 #define RETURN(_code) do { \
56 rval.consumed = consumed_myself;\
61 * Return pointer to a member.
64 element_ptrptr(void *struct_ptr, asn_TYPE_member_t *elm, void **tmp_save_ptr) {
65 if(elm->flags & ATF_POINTER) {
66 /* Member is a pointer to another structure */
67 return (void **)((char *)struct_ptr + elm->memb_offset);
70 *tmp_save_ptr = (void *)((char *)struct_ptr + elm->memb_offset);
76 element_ptr(const void *struct_ptr, const asn_TYPE_member_t *elm) {
77 if(elm->flags & ATF_POINTER) {
78 /* Member is a pointer to another structure */
79 return *(const void *const *)((const char *)struct_ptr
82 return (const void *)((const char *)struct_ptr + elm->memb_offset);
87 SEQUENCE_decode_oer(const asn_codec_ctx_t *opt_codec_ctx,
88 const asn_TYPE_descriptor_t *td,
89 const asn_oer_constraints_t *constraints, void **struct_ptr,
90 const void *ptr, size_t size) {
91 const asn_SEQUENCE_specifics_t *specs =
92 (const asn_SEQUENCE_specifics_t *)td->specifics;
93 asn_dec_rval_t rval = {RC_OK, 0};
94 void *st = *struct_ptr; /* Target structure */
95 asn_struct_ctx_t *ctx; /* Decoder context */
96 size_t consumed_myself = 0; /* Consumed bytes from ptr. */
100 if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx))
104 * Create the target structure if it is not present already.
107 st = *struct_ptr = CALLOC(1, specs->struct_size);
114 * Restore parsing context.
116 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
119 * Start to parse where left previously.
126 asn_bit_data_t *preamble;
127 int has_extensions_bit = (specs->first_extension >= 0);
128 size_t preamble_bits = (has_extensions_bit + specs->roms_count);
129 size_t preamble_bytes = ((7 + preamble_bits) >> 3);
131 ASN_DEBUG("OER SEQUENCE %s Decoding PHASE 0", td->name);
134 "Expecting preamble bits %" ASN_PRI_SIZE " for %s (including %d extension bits)",
135 preamble_bits, td->name, has_extensions_bit);
137 if(preamble_bytes > size) {
141 preamble = asn_bit_data_new_contiguous(ptr, preamble_bits);
145 preamble->nboff = has_extensions_bit;
147 ADVANCE(preamble_bytes);
152 /* Decode components of the extension root */
153 asn_bit_data_t *preamble = ctx->ptr;
156 ASN_DEBUG("OER SEQUENCE %s Decoding PHASE 1 (Root)", td->name);
160 for(edx = (ctx->step >> 1); IN_ROOT_GROUP_PRED(edx);
161 edx++, ctx->step = (ctx->step & ~1) + 2) {
162 asn_TYPE_member_t *elm = &td->elements[edx];
164 ASN_DEBUG("Decoding %s->%s", td->name, elm->name);
166 assert(!IN_EXTENSION_GROUP(specs, edx));
169 goto microphase2_decode_continues;
174 int32_t present = asn_get_few_bits(preamble, 1);
176 ASN_DEBUG("Presence map ended prematurely: %d", present);
178 } else if(present == 0) {
179 if(elm->default_value_set) {
180 /* Fill-in DEFAULT */
182 if(elm->default_value_set(
183 element_ptrptr(st, elm, &tmp))) {
187 /* The member is not present. */
190 /* Present OPTIONAL or DEFAULT component. */
194 * MICROPHASE 2: Invoke the member-specific decoder.
196 ctx->step |= 1; /* Confirm entering next microphase */
197 microphase2_decode_continues:
198 if(elm->flags & ATF_OPEN_TYPE) {
199 rval = OPEN_TYPE_oer_get(opt_codec_ctx, td, st, elm, ptr, size);
201 void *save_memb_ptr; /* Temporary reference. */
202 void **memb_ptr2; /* Pointer to a pointer to a memmber */
204 memb_ptr2 = element_ptrptr(st, elm, &save_memb_ptr);
206 rval = elm->type->op->oer_decoder(
207 opt_codec_ctx, elm->type,
208 elm->encoding_constraints.oer_constraints, memb_ptr2, ptr,
213 ADVANCE(rval.consumed);
216 ASN_DEBUG("More bytes needed at element %s \"%s\"", td->name,
218 ADVANCE(rval.consumed);
221 ASN_DEBUG("Decoding failed at element %s \"%s\"", td->name,
225 } /* for(all root members) */
233 /* Cleanup preamble. */
234 asn_bit_data_t *preamble = ctx->ptr;
235 asn_bit_data_t *extadds;
236 int has_extensions_bit = (specs->first_extension >= 0);
237 int extensions_present =
239 && (preamble->buffer == NULL
240 || (((const uint8_t *)preamble->buffer)[0] & 0x80));
245 ASN_DEBUG("OER SEQUENCE %s Decoding PHASE 2", td->name);
247 preamble->buffer = 0; /* Will do extensions_present==1 next time. */
249 if(!extensions_present) {
255 * X.696 (08/2015) #16.1 (c), #16.4
256 * Read in the extension addition presence bitmap.
259 len_len = oer_fetch_length(ptr, size, &len);
262 } else if(len_len < 0) {
271 } else if(len > size) {
275 /* Account for unused bits */
276 unused_bits = 0x7 & *(const uint8_t *)ptr;
279 if(unused_bits && len == 0) {
283 /* Get the extensions map */
284 extadds = asn_bit_data_new_contiguous(ptr, len * 8 - unused_bits);
294 (specs->first_extension < 0 ? td->elements_count
295 : (size_t)specs->first_extension);
298 ASN_DEBUG("OER SEQUENCE %s Decoding PHASE 3 (Extensions)", td->name);
299 for(; ctx->step < (signed)td->elements_count; ctx->step++) {
300 asn_bit_data_t *extadds = ctx->ptr;
301 size_t edx = ctx->step;
302 asn_TYPE_member_t *elm = &td->elements[edx];
304 void **memb_ptr2 = element_ptrptr(st, elm, &tmp_memb_ptr);
306 switch(asn_get_few_bits(extadds, 1)) {
309 * Not every one of our extensions is known to the remote side.
310 * Continue filling in their defaults though.
314 /* Fill-in DEFAULT */
315 if(elm->default_value_set
316 && elm->default_value_set(memb_ptr2)) {
321 /* Read OER open type */
323 oer_open_type_get(opt_codec_ctx, elm->type,
324 elm->encoding_constraints.oer_constraints,
325 memb_ptr2, ptr, size);
326 assert(ot_size <= (ssize_t)size);
329 } else if(ot_size < 0) {
332 /* Roll back open type parsing */
333 asn_get_undo(extadds, 1);
346 ASN_DEBUG("OER SEQUENCE %s Decoding PHASE 4", td->name);
347 /* Read in the rest of Open Types while ignoring them */
349 asn_bit_data_t *extadds = ctx->ptr;
350 switch(asn_get_few_bits(extadds, 1)) {
354 ssize_t skipped = oer_open_type_skip(ptr, size);
357 } else if(skipped < 0) {
360 asn_get_undo(extadds, 1);
366 /* No more Open Type encoded components */
379 * Encode as Canonical OER.
382 SEQUENCE_encode_oer(const asn_TYPE_descriptor_t *td,
383 const asn_oer_constraints_t *constraints, const void *sptr,
384 asn_app_consume_bytes_f *cb, void *app_key) {
385 const asn_SEQUENCE_specifics_t *specs = (const asn_SEQUENCE_specifics_t *)td->specifics;
386 size_t computed_size = 0;
387 int has_extensions_bit = (specs->first_extension >= 0);
388 size_t preamble_bits = (has_extensions_bit + specs->roms_count);
389 uint32_t has_extensions = 0;
396 asn_bit_outp_t preamble;
398 memset(&preamble, 0, sizeof(preamble));
399 preamble.output = cb;
400 preamble.op_key = app_key;
402 if(has_extensions_bit) {
403 for(edx = specs->first_extension; edx < td->elements_count; edx++) {
404 asn_TYPE_member_t *elm = &td->elements[edx];
405 const void *memb_ptr = element_ptr(sptr, elm);
407 if(elm->default_value_cmp
408 && elm->default_value_cmp(memb_ptr) == 0) {
409 /* Do not encode default values in extensions */
416 ret = asn_put_few_bits(&preamble, has_extensions, 1);
424 * Encode optional components bitmap.
426 if(specs->roms_count) {
427 FOR_IN_ROOT_GROUP(edx) {
428 asn_TYPE_member_t *elm = &td->elements[edx];
430 if(IN_EXTENSION_GROUP(specs, edx)) break;
433 const void *memb_ptr = element_ptr(sptr, elm);
434 uint32_t has_component = memb_ptr != NULL;
435 if(has_component && elm->default_value_cmp
436 && elm->default_value_cmp(memb_ptr) == 0) {
439 ret = asn_put_few_bits(&preamble, has_component, 1);
447 asn_put_aligned_flush(&preamble);
448 computed_size += preamble.flushed_bytes;
449 } /* if(preamble_bits) */
452 * Put root components and extensions root.
454 for(edx = 0; edx < td->elements_count; edx++) {
455 asn_TYPE_member_t *elm = &td->elements[edx];
457 const void *memb_ptr;
459 if(IN_EXTENSION_GROUP(specs, edx)) break;
461 memb_ptr = element_ptr(sptr, elm);
463 if(elm->default_value_cmp
464 && elm->default_value_cmp(memb_ptr) == 0) {
465 /* Skip default values in encoding */
469 if(elm->optional) continue;
470 /* Mandatory element is missing */
473 if(!elm->type->op->oer_encoder) {
474 ASN_DEBUG("OER encoder is not defined for type %s", elm->type->name);
477 er = elm->type->op->oer_encoder(
478 elm->type, elm->encoding_constraints.oer_constraints, memb_ptr, cb,
480 if(er.encoded == -1) {
481 ASN_DEBUG("... while encoding %s member \"%s\"\n", td->name,
485 computed_size += er.encoded;
489 * Before encode extensions, encode extensions additions presense bitmap
490 # X.696 (08/2015) #16.4.
493 asn_bit_outp_t extadds;
495 /* Special case allowing us to use exactly one byte for #8.6 */
496 size_t aoms_length_bits = specs->aoms_count;
497 size_t aoms_length_bytes = (7 + aoms_length_bits) >> 3;
498 uint8_t unused_bits = 0x07 & (8 - (aoms_length_bits & 0x07));
500 assert(1 + aoms_length_bytes <= 127);
502 memset(&extadds, 0, sizeof(extadds));
504 extadds.op_key = app_key;
506 /* #8.6 length determinant */
507 ret = asn_put_few_bits(&extadds, (1 + aoms_length_bytes), 8);
508 if(ret < 0) ASN__ENCODE_FAILED;
510 /* Number of unused bytes, #16.4.2 */
511 ret = asn_put_few_bits(&extadds, unused_bits, 8);
512 if(ret < 0) ASN__ENCODE_FAILED;
514 /* Encode presence bitmap #16.4.3 */
515 for(edx = specs->first_extension; edx < td->elements_count; edx++) {
516 asn_TYPE_member_t *elm = &td->elements[edx];
517 const void *memb_ptr = element_ptr(sptr, elm);
518 if(memb_ptr && elm->default_value_cmp
519 && elm->default_value_cmp(memb_ptr) == 0) {
520 memb_ptr = 0; /* Do not encode default value. */
522 ret |= asn_put_few_bits(&extadds, memb_ptr ? 1 : 0, 1);
524 if(ret < 0) ASN__ENCODE_FAILED;
526 asn_put_aligned_flush(&extadds);
527 computed_size += extadds.flushed_bytes;
529 /* Now, encode extensions */
530 for(edx = specs->first_extension; edx < td->elements_count; edx++) {
531 asn_TYPE_member_t *elm = &td->elements[edx];
532 const void *memb_ptr = element_ptr(sptr, elm);
535 if(elm->default_value_cmp
536 && elm->default_value_cmp(memb_ptr) == 0) {
537 /* Do not encode default value. */
539 ssize_t wrote = oer_open_type_put(
540 elm->type, elm->encoding_constraints.oer_constraints,
541 memb_ptr, cb, app_key);
545 computed_size += wrote;
547 } else if(!elm->optional) {
551 } /* if(has_extensions) */
555 asn_enc_rval_t er = {0, 0, 0};
556 er.encoded = computed_size;
561 #endif /* ASN_DISABLE_OER_SUPPORT */