3 * Copyright (c) 2003-2017 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 <constr_CHOICE.h>
8 #include <per_opentype.h>
11 * Number of bytes left for this structure.
12 * (ctx->left) indicates the number of bytes _transferred_ for the structure.
13 * (size) contains the number of bytes in the buffer passed.
15 #define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left)
18 * If the subprocessor function returns with an indication that it wants
19 * more data, it may well be a fatal decoding problem, because the
20 * size is constrained by the <TLV>'s L, even if the buffer size allows
22 * For example, consider the buffer containing the following TLVs:
23 * <T:5><L:1><V> <T:6>...
24 * The TLV length clearly indicates that one byte is expected in V, but
25 * if the V processor returns with "want more data" even if the buffer
26 * contains way more data than the V processor have seen.
28 #define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size)
31 * This macro "eats" the part of the buffer which is definitely "consumed",
32 * i.e. was correctly converted into local representation or rightfully skipped.
35 #define ADVANCE(num_bytes) do { \
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) do { \
54 * Return a standardized complex structure.
57 #define RETURN(_code) do { \
59 rval.consumed = consumed_myself;\
64 * See the definitions.
66 static unsigned _fetch_present_idx(const void *struct_ptr, unsigned off,
68 static void _set_present_idx(void *sptr, unsigned offset, unsigned size,
70 static const void *_get_member_ptr(const asn_TYPE_descriptor_t *,
71 const void *sptr, asn_TYPE_member_t **elm,
75 * Tags are canonically sorted in the tag to member table.
78 _search4tag(const void *ap, const void *bp) {
79 const asn_TYPE_tag2member_t *a = (const asn_TYPE_tag2member_t *)ap;
80 const asn_TYPE_tag2member_t *b = (const asn_TYPE_tag2member_t *)bp;
82 int a_class = BER_TAG_CLASS(a->el_tag);
83 int b_class = BER_TAG_CLASS(b->el_tag);
85 if(a_class == b_class) {
86 ber_tlv_tag_t a_value = BER_TAG_VALUE(a->el_tag);
87 ber_tlv_tag_t b_value = BER_TAG_VALUE(b->el_tag);
89 if(a_value == b_value)
91 else if(a_value < b_value)
95 } else if(a_class < b_class) {
103 * The decoder of the CHOICE type.
106 CHOICE_decode_ber(const asn_codec_ctx_t *opt_codec_ctx,
107 const asn_TYPE_descriptor_t *td, void **struct_ptr,
108 const void *ptr, size_t size, int tag_mode) {
110 * Bring closer parts of structure description.
112 const asn_CHOICE_specifics_t *specs =
113 (const asn_CHOICE_specifics_t *)td->specifics;
114 asn_TYPE_member_t *elements = td->elements;
117 * Parts of the structure being constructed.
119 void *st = *struct_ptr; /* Target structure. */
120 asn_struct_ctx_t *ctx; /* Decoder context */
122 ber_tlv_tag_t tlv_tag; /* T from TLV */
123 ssize_t tag_len; /* Length of TLV's T */
124 asn_dec_rval_t rval; /* Return code from subparsers */
126 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
128 ASN_DEBUG("Decoding %s as CHOICE", td->name);
131 * Create the target structure if it is not present already.
134 st = *struct_ptr = CALLOC(1, specs->struct_size);
141 * Restore parsing context.
143 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
146 * Start to parse where left previously
152 * Check that the set of tags associated with given structure
153 * perfectly fits our expectations.
156 if(tag_mode || td->tags_count) {
157 rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size,
158 tag_mode, -1, &ctx->left, 0);
159 if(rval.code != RC_OK) {
160 ASN_DEBUG("%s tagging check failed: %d",
161 td->name, rval.code);
166 /* ?Substracted below! */
167 ctx->left += rval.consumed;
169 ADVANCE(rval.consumed);
176 ASN_DEBUG("Structure consumes %ld bytes, buffer %ld",
177 (long)ctx->left, (long)size);
182 * Fetch the T from TLV.
184 tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
185 ASN_DEBUG("In %s CHOICE tag length %d", td->name, (int)tag_len);
187 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
189 case -1: RETURN(RC_FAIL);
193 const asn_TYPE_tag2member_t *t2m;
194 asn_TYPE_tag2member_t key;
196 key.el_tag = tlv_tag;
197 t2m = (const asn_TYPE_tag2member_t *)bsearch(&key,
198 specs->tag2el, specs->tag2el_count,
199 sizeof(specs->tag2el[0]), _search4tag);
202 * Found the element corresponding to the tag.
205 ctx->step = t2m->el_no;
207 } else if(specs->ext_start == -1) {
208 ASN_DEBUG("Unexpected tag %s "
209 "in non-extensible CHOICE %s",
210 ber_tlv_tag_string(tlv_tag), td->name);
216 ASN_DEBUG("Skipping unknown tag %s",
217 ber_tlv_tag_string(tlv_tag));
219 skip = ber_skip_length(opt_codec_ctx,
220 BER_TLV_CONSTRUCTED(ptr),
221 (const char *)ptr + tag_len,
225 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
227 case -1: RETURN(RC_FAIL);
230 ADVANCE(skip + tag_len);
238 * Read in the element.
241 asn_TYPE_member_t *elm;/* CHOICE's element */
242 void *memb_ptr; /* Pointer to the member */
243 void **memb_ptr2; /* Pointer to that pointer */
245 elm = &elements[ctx->step];
248 * Compute the position of the member inside a structure,
249 * and also a type of containment (it may be contained
250 * as pointer or using inline inclusion).
252 if(elm->flags & ATF_POINTER) {
253 /* Member is a pointer to another structure */
254 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
257 * A pointer to a pointer
258 * holding the start of the structure
260 memb_ptr = (char *)st + elm->memb_offset;
261 memb_ptr2 = &memb_ptr;
263 /* Set presence to be able to free it properly at any time */
264 _set_present_idx(st, specs->pres_offset,
265 specs->pres_size, ctx->step + 1);
267 * Invoke the member fetch routine according to member's type
269 rval = elm->type->op->ber_decoder(opt_codec_ctx, elm->type,
270 memb_ptr2, ptr, LEFT, elm->tag_mode);
274 case RC_WMORE: /* More data expected */
275 if(!SIZE_VIOLATION) {
276 ADVANCE(rval.consumed);
280 case RC_FAIL: /* Fatal error */
284 ADVANCE(rval.consumed);
291 ASN_DEBUG("CHOICE %s Leftover: %ld, size = %ld, tm=%d, tc=%d",
292 td->name, (long)ctx->left, (long)size,
293 tag_mode, td->tags_count);
297 * The type must be fully decoded
298 * by the CHOICE member-specific decoder.
304 && !(tag_mode || td->tags_count)) {
306 * This is an untagged CHOICE.
307 * It doesn't contain nothing
308 * except for the member itself, including all its tags.
309 * The decoding is completed.
316 * Read in the "end of data chunks"'s.
318 while(ctx->left < 0) {
321 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
323 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
325 case -1: RETURN(RC_FAIL);
331 if(((const uint8_t *)ptr)[0] == 0) {
337 } else if(((const uint8_t *)ptr)[1] == 0) {
339 * Correctly finished with <0><0>.
346 ASN_DEBUG("Unexpected continuation in %s",
356 /* No meaningful work here */
364 CHOICE_encode_der(const asn_TYPE_descriptor_t *td, const void *sptr,
365 int tag_mode, ber_tlv_tag_t tag, asn_app_consume_bytes_f *cb,
367 const asn_CHOICE_specifics_t *specs = (const asn_CHOICE_specifics_t *)td->specifics;
368 asn_TYPE_member_t *elm; /* CHOICE element */
369 asn_enc_rval_t erval = {0,0,0};
370 const void *memb_ptr;
371 size_t computed_size = 0;
374 if(!sptr) ASN__ENCODE_FAILED;
376 ASN_DEBUG("%s %s as CHOICE",
377 cb?"Encoding":"Estimating", td->name);
379 present = _fetch_present_idx(sptr,
380 specs->pres_offset, specs->pres_size);
383 * If the structure was not initialized, it cannot be encoded:
384 * can't deduce what to encode in the choice type.
386 if(present == 0 || present > td->elements_count) {
387 if(present == 0 && td->elements_count == 0) {
388 /* The CHOICE is empty?! */
390 ASN__ENCODED_OK(erval);
396 * Seek over the present member of the structure.
398 elm = &td->elements[present-1];
399 if(elm->flags & ATF_POINTER) {
401 *(const void *const *)((const char *)sptr + elm->memb_offset);
405 ASN__ENCODED_OK(erval);
407 /* Mandatory element absent */
411 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
415 * If the CHOICE itself is tagged EXPLICIT:
416 * T ::= [2] EXPLICIT CHOICE { ... }
417 * Then emit the appropriate tags.
419 if(tag_mode == 1 || td->tags_count) {
421 * For this, we need to pre-compute the member.
425 /* Encode member with its tag */
426 erval = elm->type->op->der_encoder(elm->type, memb_ptr,
427 elm->tag_mode, elm->tag, 0, 0);
428 if(erval.encoded == -1)
431 /* Encode CHOICE with parent or my own tag */
432 ret = der_write_tags(td, erval.encoded, tag_mode, 1, tag,
436 computed_size += ret;
440 * Encode the single underlying member.
442 erval = elm->type->op->der_encoder(elm->type, memb_ptr,
443 elm->tag_mode, elm->tag, cb, app_key);
444 if(erval.encoded == -1)
447 ASN_DEBUG("Encoded CHOICE member in %ld bytes (+%ld)",
448 (long)erval.encoded, (long)computed_size);
450 erval.encoded += computed_size;
456 CHOICE_outmost_tag(const asn_TYPE_descriptor_t *td, const void *ptr, int tag_mode, ber_tlv_tag_t tag) {
457 const asn_CHOICE_specifics_t *specs = (const asn_CHOICE_specifics_t *)td->specifics;
460 assert(tag_mode == 0); (void)tag_mode;
461 assert(tag == 0); (void)tag;
464 * Figure out which CHOICE element is encoded.
466 present = _fetch_present_idx(ptr, specs->pres_offset, specs->pres_size);
468 if(present > 0 && present <= td->elements_count) {
469 const asn_TYPE_member_t *elm = &td->elements[present-1];
470 const void *memb_ptr;
472 if(elm->flags & ATF_POINTER) {
473 memb_ptr = *(const void * const *)
474 ((const char *)ptr + elm->memb_offset);
476 memb_ptr = (const void *)
477 ((const char *)ptr + elm->memb_offset);
480 return asn_TYPE_outmost_tag(elm->type, memb_ptr,
481 elm->tag_mode, elm->tag);
483 return (ber_tlv_tag_t)-1;
488 CHOICE_constraint(const asn_TYPE_descriptor_t *td, const void *sptr,
489 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
490 const asn_CHOICE_specifics_t *specs =
491 (const asn_CHOICE_specifics_t *)td->specifics;
495 ASN__CTFAIL(app_key, td, sptr,
496 "%s: value not given (%s:%d)",
497 td->name, __FILE__, __LINE__);
502 * Figure out which CHOICE element is encoded.
504 present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
505 if(present > 0 && present <= td->elements_count) {
506 asn_TYPE_member_t *elm = &td->elements[present-1];
507 const void *memb_ptr;
509 if(elm->flags & ATF_POINTER) {
510 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
514 ASN__CTFAIL(app_key, td, sptr,
515 "%s: mandatory CHOICE element %s absent (%s:%d)",
516 td->name, elm->name, __FILE__, __LINE__);
520 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
523 if(elm->encoding_constraints.general_constraints) {
524 return elm->encoding_constraints.general_constraints(elm->type, memb_ptr,
527 return elm->type->encoding_constraints.general_constraints(elm->type,
528 memb_ptr, ctfailcb, app_key);
531 ASN__CTFAIL(app_key, td, sptr,
532 "%s: no CHOICE element given (%s:%d)",
533 td->name, __FILE__, __LINE__);
539 #define XER_ADVANCE(num_bytes) do { \
540 size_t num = num_bytes; \
541 buf_ptr = (const void *)(((const char *)buf_ptr) + num); \
543 consumed_myself += num; \
547 * Decode the XER (XML) data.
550 CHOICE_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
551 const asn_TYPE_descriptor_t *td, void **struct_ptr,
552 const char *opt_mname, const void *buf_ptr, size_t size) {
554 * Bring closer parts of structure description.
556 const asn_CHOICE_specifics_t *specs = (const asn_CHOICE_specifics_t *)td->specifics;
557 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
560 * Parts of the structure being constructed.
562 void *st = *struct_ptr; /* Target structure. */
563 asn_struct_ctx_t *ctx; /* Decoder context */
565 asn_dec_rval_t rval; /* Return value of a decoder */
566 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
567 size_t edx; /* Element index */
570 * Create the target structure if it is not present already.
573 st = *struct_ptr = CALLOC(1, specs->struct_size);
574 if(st == 0) RETURN(RC_FAIL);
578 * Restore parsing context.
580 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
581 if(ctx->phase == 0 && !*xml_tag)
582 ctx->phase = 1; /* Skip the outer tag checking phase */
585 * Phases of XER/XML processing:
586 * Phase 0: Check that the opening tag matches our expectations.
587 * Phase 1: Processing body and reacting on closing tag.
588 * Phase 2: Processing inner type.
589 * Phase 3: Only waiting for closing tag.
590 * Phase 4: Skipping unknown extensions.
591 * Phase 5: PHASED OUT
593 for(edx = ctx->step; ctx->phase <= 4;) {
594 pxer_chunk_type_e ch_type; /* XER chunk type */
595 ssize_t ch_size; /* Chunk size */
596 xer_check_tag_e tcv; /* Tag check value */
597 asn_TYPE_member_t *elm;
600 * Go inside the member.
602 if(ctx->phase == 2) {
603 asn_dec_rval_t tmprval;
604 void *memb_ptr; /* Pointer to the member */
605 void **memb_ptr2; /* Pointer to that pointer */
606 unsigned old_present;
608 elm = &td->elements[edx];
610 if(elm->flags & ATF_POINTER) {
611 /* Member is a pointer to another structure */
612 memb_ptr2 = (void **)((char *)st
615 memb_ptr = (char *)st + elm->memb_offset;
616 memb_ptr2 = &memb_ptr;
619 /* Start/Continue decoding the inner member */
620 tmprval = elm->type->op->xer_decoder(opt_codec_ctx,
621 elm->type, memb_ptr2, elm->name,
623 XER_ADVANCE(tmprval.consumed);
624 ASN_DEBUG("XER/CHOICE: itdf: [%s] code=%d",
625 elm->type->name, tmprval.code);
626 old_present = _fetch_present_idx(st,
627 specs->pres_offset, specs->pres_size);
628 assert(old_present == 0 || old_present == edx + 1);
629 /* Record what we've got */
631 specs->pres_offset, specs->pres_size, edx + 1);
632 if(tmprval.code != RC_OK)
633 RETURN(tmprval.code);
638 /* No need to wait for closing tag; special mode. */
639 if(ctx->phase == 3 && !*xml_tag) {
640 ctx->phase = 5; /* Phase out */
645 * Get the next part of the XML stream.
647 ch_size = xer_next_token(&ctx->context, buf_ptr, size, &ch_type);
654 case PXER_COMMENT: /* Got XML comment */
655 case PXER_TEXT: /* Ignore free-standing text */
656 XER_ADVANCE(ch_size); /* Skip silently */
659 break; /* Check the rest down there */
663 tcv = xer_check_tag(buf_ptr, ch_size, xml_tag);
664 ASN_DEBUG("XER/CHOICE checked [%c%c%c%c] vs [%s], tcv=%d",
665 ch_size>0?((const uint8_t *)buf_ptr)[0]:'?',
666 ch_size>1?((const uint8_t *)buf_ptr)[1]:'?',
667 ch_size>2?((const uint8_t *)buf_ptr)[2]:'?',
668 ch_size>3?((const uint8_t *)buf_ptr)[3]:'?',
671 /* Skip the extensions section */
672 if(ctx->phase == 4) {
673 ASN_DEBUG("skip_unknown(%d, %ld)",
674 tcv, (long)ctx->left);
675 switch(xer_skip_unknown(tcv, &ctx->left)) {
683 XER_ADVANCE(ch_size);
693 break; /* No CHOICE? */
697 XER_ADVANCE(ch_size);
698 ctx->phase = 5; /* Phase out */
701 if(ctx->phase == 0) {
702 XER_ADVANCE(ch_size);
703 ctx->phase = 1; /* Processing body phase */
711 break; /* Really unexpected */
714 * Search which inner member corresponds to this tag.
716 for(edx = 0; edx < td->elements_count; edx++) {
717 elm = &td->elements[edx];
718 tcv = xer_check_tag(buf_ptr,ch_size,elm->name);
723 * Process this member.
732 edx = td->elements_count;
733 break; /* Phase out */
737 if(edx != td->elements_count)
740 /* It is expected extension */
741 if(specs->ext_start != -1) {
742 ASN_DEBUG("Got anticipated extension");
744 * Check for (XCT_BOTH or XCT_UNKNOWN_BO)
745 * By using a mask. Only record a pure
748 if(tcv & XCT_CLOSING) {
749 /* Found </extension> without body */
750 ctx->phase = 3; /* Terminating */
753 ctx->phase = 4; /* Skip ...'s */
755 XER_ADVANCE(ch_size);
764 ASN_DEBUG("Unexpected XML tag [%c%c%c%c] in CHOICE [%s]"
766 ch_size>0?((const uint8_t *)buf_ptr)[0]:'?',
767 ch_size>1?((const uint8_t *)buf_ptr)[1]:'?',
768 ch_size>2?((const uint8_t *)buf_ptr)[2]:'?',
769 ch_size>3?((const uint8_t *)buf_ptr)[3]:'?',
770 td->name, ctx->phase, xml_tag);
774 ctx->phase = 5; /* Phase out, just in case */
780 CHOICE_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
781 enum xer_encoder_flags_e flags, asn_app_consume_bytes_f *cb,
783 const asn_CHOICE_specifics_t *specs =
784 (const asn_CHOICE_specifics_t *)td->specifics;
785 asn_enc_rval_t er = {0,0,0};
786 unsigned present = 0;
792 * Figure out which CHOICE element is encoded.
794 present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
796 if(present == 0 || present > td->elements_count) {
799 asn_enc_rval_t tmper = {0,0,0};
800 asn_TYPE_member_t *elm = &td->elements[present-1];
801 const void *memb_ptr = NULL;
802 const char *mname = elm->name;
803 unsigned int mlen = strlen(mname);
805 if(elm->flags & ATF_POINTER) {
807 *(const void *const *)((const char *)sptr + elm->memb_offset);
808 if(!memb_ptr) ASN__ENCODE_FAILED;
810 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
815 if(!(flags & XER_F_CANONICAL)) ASN__TEXT_INDENT(1, ilevel);
816 ASN__CALLBACK3("<", 1, mname, mlen, ">", 1);
818 tmper = elm->type->op->xer_encoder(elm->type, memb_ptr,
819 ilevel + 1, flags, cb, app_key);
820 if(tmper.encoded == -1) return tmper;
821 er.encoded += tmper.encoded;
823 ASN__CALLBACK3("</", 2, mname, mlen, ">", 1);
826 if(!(flags & XER_F_CANONICAL)) ASN__TEXT_INDENT(1, ilevel - 1);
834 CHOICE_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
835 const asn_TYPE_descriptor_t *td,
836 const asn_per_constraints_t *constraints, void **sptr,
837 asn_per_data_t *pd) {
838 const asn_CHOICE_specifics_t *specs =
839 (const asn_CHOICE_specifics_t *)td->specifics;
841 const asn_per_constraint_t *ct;
842 asn_TYPE_member_t *elm; /* CHOICE's element */
848 if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx))
852 * Create the target structure if it is not present already.
855 st = *sptr = CALLOC(1, specs->struct_size);
856 if(!st) ASN__DECODE_FAILED;
859 if(constraints) ct = &constraints->value;
860 else if(td->encoding_constraints.per_constraints) ct = &td->encoding_constraints.per_constraints->value;
863 if(ct && ct->flags & APC_EXTENSIBLE) {
864 value = per_get_few_bits(pd, 1);
865 if(value < 0) ASN__DECODE_STARVED;
866 if(value) ct = 0; /* Not restricted */
869 if(ct && ct->range_bits >= 0) {
870 value = per_get_few_bits(pd, ct->range_bits);
871 if(value < 0) ASN__DECODE_STARVED;
872 ASN_DEBUG("CHOICE %s got index %d in range %d",
873 td->name, value, ct->range_bits);
874 if(value > ct->upper_bound)
877 if(specs->ext_start == -1)
879 value = uper_get_nsnnwn(pd);
880 if(value < 0) ASN__DECODE_STARVED;
881 value += specs->ext_start;
882 if((unsigned)value >= td->elements_count)
886 /* Adjust if canonical order is different from natural order */
887 if(specs->from_canonical_order) {
888 ASN_DEBUG("CHOICE presence from wire %d", value);
889 value = specs->from_canonical_order[value];
890 ASN_DEBUG("CHOICE presence index effective %d", value);
893 /* Set presence to be able to free it later */
894 _set_present_idx(st, specs->pres_offset, specs->pres_size, value + 1);
896 elm = &td->elements[value];
897 if(elm->flags & ATF_POINTER) {
898 /* Member is a pointer to another structure */
899 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
901 memb_ptr = (char *)st + elm->memb_offset;
902 memb_ptr2 = &memb_ptr;
904 ASN_DEBUG("Discovered CHOICE %s encodes %s", td->name, elm->name);
906 if(ct && ct->range_bits >= 0) {
907 rv = elm->type->op->uper_decoder(opt_codec_ctx, elm->type,
908 elm->encoding_constraints.per_constraints, memb_ptr2, pd);
910 rv = uper_open_type_get(opt_codec_ctx, elm->type,
911 elm->encoding_constraints.per_constraints, memb_ptr2, pd);
915 ASN_DEBUG("Failed to decode %s in %s (CHOICE) %d",
916 elm->name, td->name, rv.code);
921 CHOICE_encode_uper(const asn_TYPE_descriptor_t *td,
922 const asn_per_constraints_t *constraints, const void *sptr,
923 asn_per_outp_t *po) {
924 const asn_CHOICE_specifics_t *specs = (const asn_CHOICE_specifics_t *)td->specifics;
925 asn_TYPE_member_t *elm; /* CHOICE's element */
926 const asn_per_constraint_t *ct;
927 const void *memb_ptr;
931 if(!sptr) ASN__ENCODE_FAILED;
933 ASN_DEBUG("Encoding %s as CHOICE", td->name);
935 if(constraints) ct = &constraints->value;
936 else if(td->encoding_constraints.per_constraints)
937 ct = &td->encoding_constraints.per_constraints->value;
940 present = _fetch_present_idx(sptr, specs->pres_offset, specs->pres_size);
943 * If the structure was not initialized properly, it cannot be encoded:
944 * can't deduce what to encode in the choice type.
946 if(present == 0 || present > td->elements_count)
951 ASN_DEBUG("Encoding %s CHOICE element %d", td->name, present);
953 /* Adjust if canonical order is different from natural order */
954 if(specs->to_canonical_order)
955 present_enc = specs->to_canonical_order[present];
957 present_enc = present;
959 if(ct && ct->range_bits >= 0) {
960 if(present_enc < ct->lower_bound
961 || present_enc > ct->upper_bound) {
962 if(ct->flags & APC_EXTENSIBLE) {
964 "CHOICE member %d (enc %d) is an extension (%ld..%ld)",
965 present, present_enc, ct->lower_bound, ct->upper_bound);
966 if(per_put_few_bits(po, 1, 1))
974 if(ct && ct->flags & APC_EXTENSIBLE) {
975 ASN_DEBUG("CHOICE member %d (enc %d) is not an extension (%ld..%ld)",
976 present, present_enc, ct->lower_bound, ct->upper_bound);
977 if(per_put_few_bits(po, 0, 1))
982 elm = &td->elements[present];
983 ASN_DEBUG("CHOICE member \"%s\" %d (as %d)", elm->name, present,
985 if(elm->flags & ATF_POINTER) {
986 /* Member is a pointer to another structure */
988 *(const void *const *)((const char *)sptr + elm->memb_offset);
989 if(!memb_ptr) ASN__ENCODE_FAILED;
991 memb_ptr = (const char *)sptr + elm->memb_offset;
994 if(ct && ct->range_bits >= 0) {
995 if(per_put_few_bits(po, present_enc, ct->range_bits))
998 return elm->type->op->uper_encoder(
999 elm->type, elm->encoding_constraints.per_constraints, memb_ptr, po);
1001 asn_enc_rval_t rval = {0,0,0};
1002 if(specs->ext_start == -1) ASN__ENCODE_FAILED;
1003 if(uper_put_nsnnwn(po, present_enc - specs->ext_start))
1005 if(uper_open_type_put(elm->type,
1006 elm->encoding_constraints.per_constraints,
1010 ASN__ENCODED_OK(rval);
1015 CHOICE_decode_aper(const asn_codec_ctx_t *opt_codec_ctx,
1016 const asn_TYPE_descriptor_t *td,
1017 const asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
1018 const asn_CHOICE_specifics_t *specs = (const asn_CHOICE_specifics_t *)td->specifics;
1020 const asn_per_constraint_t *ct;
1021 const asn_per_constraint_t *ext_ct = NULL;
1022 asn_TYPE_member_t *elm; /* CHOICE's element */
1028 if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx))
1032 * Create the target structure if it is not present already.
1035 st = *sptr = CALLOC(1, specs->struct_size);
1036 if(!st) ASN__DECODE_FAILED;
1039 if(constraints) ct = &constraints->value;
1040 else if(td->encoding_constraints.per_constraints)
1041 ct = &td->encoding_constraints.per_constraints->value;
1044 if(ct && ct->flags & APC_EXTENSIBLE) {
1045 value = per_get_few_bits(pd, 1);
1046 if(value < 0) ASN__DECODE_STARVED;
1049 ct = 0; /* Not restricted */
1054 if(ct && ct->range_bits >= 0) {
1055 value = per_get_few_bits(pd, ct->range_bits);
1056 if(value < 0) ASN__DECODE_STARVED;
1057 ASN_DEBUG("CHOICE %s got index %d in range %d",
1058 td->name, value, ct->range_bits);
1059 if(value > ct->upper_bound)
1062 if(specs->ext_start == -1)
1064 value = aper_get_nsnnwn(pd, ext_ct->range_bits);
1065 if(value < 0) ASN__DECODE_STARVED;
1066 value += specs->ext_start;
1067 if((unsigned)value >= td->elements_count)
1071 /* Adjust if canonical order is different from natural order */
1072 if(specs->from_canonical_order)
1073 value = specs->from_canonical_order[value];
1075 /* Set presence to be able to free it later */
1076 _set_present_idx(st, specs->pres_offset, specs->pres_size, value + 1);
1078 elm = &td->elements[value];
1079 if(elm->flags & ATF_POINTER) {
1080 /* Member is a pointer to another structure */
1081 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
1083 memb_ptr = (char *)st + elm->memb_offset;
1084 memb_ptr2 = &memb_ptr;
1086 ASN_DEBUG("Discovered CHOICE %s encodes %s", td->name, elm->name);
1088 if(ct && ct->range_bits >= 0) {
1089 rv = elm->type->op->aper_decoder(opt_codec_ctx, elm->type,
1090 elm->encoding_constraints.per_constraints, memb_ptr2, pd);
1092 rv = aper_open_type_get(opt_codec_ctx, elm->type,
1093 elm->encoding_constraints.per_constraints, memb_ptr2, pd);
1096 if(rv.code != RC_OK)
1097 ASN_DEBUG("Failed to decode %s in %s (CHOICE) %d",
1098 elm->name, td->name, rv.code);
1103 CHOICE_encode_aper(const asn_TYPE_descriptor_t *td,
1104 const asn_per_constraints_t *constraints,
1105 const void *sptr, asn_per_outp_t *po) {
1106 const asn_CHOICE_specifics_t *specs = (const asn_CHOICE_specifics_t *)td->specifics;
1107 const asn_TYPE_member_t *elm; /* CHOICE's element */
1108 const asn_per_constraint_t *ct = NULL;
1109 const asn_per_constraint_t *ext_ct = NULL;
1110 const void *memb_ptr;
1114 if(!sptr) ASN__ENCODE_FAILED;
1116 ASN_DEBUG("Encoding %s as CHOICE using ALIGNED PER", td->name);
1118 if(constraints) ct = &constraints->value;
1119 else if(td->encoding_constraints.per_constraints)
1120 ct = &td->encoding_constraints.per_constraints->value;
1123 present = _fetch_present_idx(sptr,
1124 specs->pres_offset, specs->pres_size);
1127 * If the structure was not initialized properly, it cannot be encoded:
1128 * can't deduce what to encode in the choice type.
1130 if(present <= 0 || (unsigned)present > td->elements_count)
1135 /* Adjust if canonical order is different from natural order */
1136 if(specs->to_canonical_order)
1137 present_enc = specs->to_canonical_order[present];
1139 present_enc = present;
1141 ASN_DEBUG("Encoding %s CHOICE element %d", td->name, present);
1143 if(ct && (ct->range_bits >= 0)) {
1144 // Value is not within the range of the primary values ?
1145 if(present < ct->lower_bound || present > ct->upper_bound) {
1146 if(ct->flags & APC_EXTENSIBLE) {
1147 ASN_DEBUG("CHOICE member %d (enc %d) is an extension (%ld..%ld)",
1148 present, present_enc, ct->lower_bound, ct->upper_bound);
1149 // X691/23.5 Extension marker = 1
1150 if(per_put_few_bits(po, 1, 1)) {
1156 // no more need of constraint.
1162 if(ct && (ct->flags & APC_EXTENSIBLE)) {
1163 ASN_DEBUG("CHOICE member %d (enc %d) is not an extension (%ld..%ld)",
1164 present, present, ct->lower_bound, ct->upper_bound);
1165 // X691.23.5 Extension marker = 0
1166 if(per_put_few_bits(po, 0, 1)) {
1171 elm = &td->elements[present];
1172 if(elm->flags & ATF_POINTER) {
1173 /* Member is a pointer to another structure */
1174 memb_ptr = *(const void *const *)((const char *)sptr + elm->memb_offset);
1175 if(!memb_ptr) ASN__ENCODE_FAILED;
1177 memb_ptr = (const char *)sptr + elm->memb_offset;
1180 if(ct && (ct->range_bits >= 0)) {
1181 // By construction (ct != 0), the alternative value is a non extended one.
1182 // X691/23.7 X691/23.6 alternative value encoded as a range_bits bits value.
1183 if(per_put_few_bits(po, present_enc, ct->range_bits))
1186 return elm->type->op->aper_encoder(elm->type, elm->encoding_constraints.per_constraints,
1189 asn_enc_rval_t rval = {0,0,0};
1190 if(specs->ext_start == -1)
1192 // X691/23.8 normally encoded as a small non negative whole number
1194 if(ext_ct && aper_put_nsnnwn(po, ext_ct->range_bits, present_enc - specs->ext_start))
1196 if(aper_open_type_put(elm->type, elm->encoding_constraints.per_constraints,
1200 ASN__ENCODED_OK(rval);
1205 CHOICE_print(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
1206 asn_app_consume_bytes_f *cb, void *app_key) {
1207 const asn_CHOICE_specifics_t *specs = (const asn_CHOICE_specifics_t *)td->specifics;
1210 if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
1213 * Figure out which CHOICE element is encoded.
1215 present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
1218 * Print that element.
1220 if(present > 0 && present <= td->elements_count) {
1221 asn_TYPE_member_t *elm = &td->elements[present-1];
1222 const void *memb_ptr;
1224 if(elm->flags & ATF_POINTER) {
1225 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
1226 if(!memb_ptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
1228 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
1231 /* Print member's name and stuff */
1233 if(cb(elm->name, strlen(elm->name), app_key) < 0
1234 || cb(": ", 2, app_key) < 0)
1238 return elm->type->op->print_struct(elm->type, memb_ptr, ilevel,
1241 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
1246 CHOICE_free(const asn_TYPE_descriptor_t *td, void *ptr,
1247 enum asn_struct_free_method method) {
1248 const asn_CHOICE_specifics_t *specs =
1249 (const asn_CHOICE_specifics_t *)td->specifics;
1255 ASN_DEBUG("Freeing %s as CHOICE", td->name);
1258 * Figure out which CHOICE element is encoded.
1260 present = _fetch_present_idx(ptr, specs->pres_offset, specs->pres_size);
1263 * Free that element.
1265 if(present > 0 && present <= td->elements_count) {
1266 asn_TYPE_member_t *elm = &td->elements[present-1];
1269 if(elm->flags & ATF_POINTER) {
1270 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
1272 ASN_STRUCT_FREE(*elm->type, memb_ptr);
1274 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
1275 ASN_STRUCT_FREE_CONTENTS_ONLY(*elm->type, memb_ptr);
1280 case ASFM_FREE_EVERYTHING:
1283 case ASFM_FREE_UNDERLYING:
1285 case ASFM_FREE_UNDERLYING_AND_RESET:
1286 memset(ptr, 0, specs->struct_size);
1293 * The following functions functions offer protection against -fshort-enums,
1294 * compatible with little- and big-endian machines.
1295 * If assertion is triggered, either disable -fshort-enums, or add an entry
1296 * here with the ->pres_size of your target stracture.
1297 * Unless the target structure is packed, the ".present" member
1298 * is guaranteed to be aligned properly. ASN.1 compiler itself does not
1299 * produce packed code.
1302 _fetch_present_idx(const void *struct_ptr, unsigned pres_offset,
1303 unsigned pres_size) {
1304 const void *present_ptr;
1307 present_ptr = ((const char *)struct_ptr) + pres_offset;
1310 case sizeof(int): present = *(const unsigned int *)present_ptr; break;
1311 case sizeof(short): present = *(const unsigned short *)present_ptr; break;
1312 case sizeof(char): present = *(const unsigned char *)present_ptr; break;
1314 /* ANSI C mandates enum to be equivalent to integer */
1315 assert(pres_size != sizeof(int));
1316 return 0; /* If not aborted, pass back safe value */
1323 _set_present_idx(void *struct_ptr, unsigned pres_offset, unsigned pres_size,
1326 present_ptr = ((char *)struct_ptr) + pres_offset;
1329 case sizeof(int): *(unsigned int *)present_ptr = present; break;
1330 case sizeof(short): *(unsigned short *)present_ptr = present; break;
1331 case sizeof(char): *(unsigned char *)present_ptr = present; break;
1333 /* ANSI C mandates enum to be equivalent to integer */
1334 assert(pres_size != sizeof(int));
1339 _get_member_ptr(const asn_TYPE_descriptor_t *td, const void *sptr,
1340 asn_TYPE_member_t **elm_ptr, unsigned *present_out) {
1341 const asn_CHOICE_specifics_t *specs =
1342 (const asn_CHOICE_specifics_t *)td->specifics;
1352 * Figure out which CHOICE element is encoded.
1354 present = _fetch_present_idx(sptr, specs->pres_offset, specs->pres_size);
1355 *present_out = present;
1358 * The presence index is intentionally 1-based to avoid
1359 * treating zeroed structure as a valid one.
1361 if(present > 0 && present <= td->elements_count) {
1362 asn_TYPE_member_t *const elm = &td->elements[present - 1];
1363 const void *memb_ptr;
1365 if(elm->flags & ATF_POINTER) {
1367 *(const void *const *)((const char *)sptr + elm->memb_offset);
1369 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
1381 CHOICE_compare(const asn_TYPE_descriptor_t *td, const void *aptr, const void *bptr) {
1382 asn_TYPE_member_t *aelm;
1383 asn_TYPE_member_t *belm;
1384 unsigned apresent = 0;
1385 unsigned bpresent = 0;
1386 const void *amember = _get_member_ptr(td, aptr, &aelm, &apresent);
1387 const void *bmember = _get_member_ptr(td, bptr, &belm, &bpresent);
1389 if(amember && bmember) {
1390 if(apresent == bpresent) {
1391 assert(aelm == belm);
1392 return aelm->type->op->compare_struct(aelm->type, amember, bmember);
1393 } else if(apresent < bpresent) {
1398 } else if(!amember) {
1406 * Return the 1-based choice variant presence index.
1407 * Returns 0 in case of error.
1410 CHOICE_variant_get_presence(const asn_TYPE_descriptor_t *td, const void *sptr) {
1411 const asn_CHOICE_specifics_t *specs =
1412 (const asn_CHOICE_specifics_t *)td->specifics;
1413 return _fetch_present_idx(sptr, specs->pres_offset, specs->pres_size);
1417 * Sets or resets the 1-based choice variant presence index.
1418 * In case a previous index is not zero, the currently selected structure
1419 * member is freed and zeroed-out first.
1420 * Returns 0 on success and -1 on error.
1423 CHOICE_variant_set_presence(const asn_TYPE_descriptor_t *td, void *sptr,
1425 const asn_CHOICE_specifics_t *specs =
1426 (const asn_CHOICE_specifics_t *)td->specifics;
1427 unsigned old_present;
1433 if(present > td->elements_count)
1437 _fetch_present_idx(sptr, specs->pres_offset, specs->pres_size);
1438 if(present == old_present)
1441 if(old_present != 0) {
1442 assert(old_present <= td->elements_count);
1443 ASN_STRUCT_RESET(*td, sptr);
1446 _set_present_idx(sptr, specs->pres_offset, specs->pres_size, present);
1452 asn_random_fill_result_t
1453 CHOICE_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
1454 const asn_encoding_constraints_t *constr,
1455 size_t max_length) {
1456 const asn_CHOICE_specifics_t *specs =
1457 (const asn_CHOICE_specifics_t *)td->specifics;
1458 asn_random_fill_result_t res;
1459 asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
1460 asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
1461 const asn_TYPE_member_t *elm;
1463 void *memb_ptr; /* Pointer to the member */
1464 void **memb_ptr2; /* Pointer to that pointer */
1467 if(max_length == 0) return result_skipped;
1472 st = CALLOC(1, specs->struct_size);
1474 return result_failed;
1478 present = asn_random_between(1, td->elements_count);
1479 elm = &td->elements[present - 1];
1481 if(elm->flags & ATF_POINTER) {
1482 /* Member is a pointer to another structure */
1483 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
1485 memb_ptr = (char *)st + elm->memb_offset;
1486 memb_ptr2 = &memb_ptr;
1489 res = elm->type->op->random_fill(elm->type, memb_ptr2,
1490 &elm->encoding_constraints, max_length);
1491 _set_present_idx(st, specs->pres_offset, specs->pres_size, present);
1492 if(res.code == ARFILL_OK) {
1496 ASN_STRUCT_RESET(*td, st);
1498 ASN_STRUCT_FREE(*td, st);
1506 asn_TYPE_operation_t asn_OP_CHOICE = {
1514 #ifdef ASN_DISABLE_OER_SUPPORT
1520 #endif /* ASN_DISABLE_OER_SUPPORT */
1521 #ifdef ASN_DISABLE_PER_SUPPORT
1531 #endif /* ASN_DISABLE_PER_SUPPORT */