2 * Copyright (c) 2003, 2004, 2005, 2006, 2007 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>
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 int _fetch_present_idx(const void *struct_ptr, int off, int size);
67 static void _set_present_idx(void *sptr, int offset, int size, int pres);
70 * Tags are canonically sorted in the tag to member table.
73 _search4tag(const void *ap, const void *bp) {
74 const asn_TYPE_tag2member_t *a = (const asn_TYPE_tag2member_t *)ap;
75 const asn_TYPE_tag2member_t *b = (const asn_TYPE_tag2member_t *)bp;
77 int a_class = BER_TAG_CLASS(a->el_tag);
78 int b_class = BER_TAG_CLASS(b->el_tag);
80 if(a_class == b_class) {
81 ber_tlv_tag_t a_value = BER_TAG_VALUE(a->el_tag);
82 ber_tlv_tag_t b_value = BER_TAG_VALUE(b->el_tag);
84 if(a_value == b_value)
86 else if(a_value < b_value)
90 } else if(a_class < b_class) {
98 * The decoder of the CHOICE type.
101 CHOICE_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
102 void **struct_ptr, const void *ptr, size_t size, int tag_mode) {
104 * Bring closer parts of structure description.
106 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
107 asn_TYPE_member_t *elements = td->elements;
110 * Parts of the structure being constructed.
112 void *st = *struct_ptr; /* Target structure. */
113 asn_struct_ctx_t *ctx; /* Decoder context */
115 ber_tlv_tag_t tlv_tag; /* T from TLV */
116 ssize_t tag_len; /* Length of TLV's T */
117 asn_dec_rval_t rval; /* Return code from subparsers */
119 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
121 ASN_DEBUG("Decoding %s as CHOICE", td->name);
124 * Create the target structure if it is not present already.
127 st = *struct_ptr = CALLOC(1, specs->struct_size);
134 * Restore parsing context.
136 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
139 * Start to parse where left previously
145 * Check that the set of tags associated with given structure
146 * perfectly fits our expectations.
149 if(tag_mode || td->tags_count) {
150 rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size,
151 tag_mode, -1, &ctx->left, 0);
152 if(rval.code != RC_OK) {
153 ASN_DEBUG("%s tagging check failed: %d",
154 td->name, rval.code);
159 /* ?Substracted below! */
160 ctx->left += rval.consumed;
162 ADVANCE(rval.consumed);
169 ASN_DEBUG("Structure consumes %ld bytes, buffer %ld",
170 (long)ctx->left, (long)size);
175 * Fetch the T from TLV.
177 tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
178 ASN_DEBUG("In %s CHOICE tag length %d", td->name, (int)tag_len);
180 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
182 case -1: RETURN(RC_FAIL);
186 const asn_TYPE_tag2member_t *t2m;
187 asn_TYPE_tag2member_t key;
189 key.el_tag = tlv_tag;
190 t2m = (const asn_TYPE_tag2member_t *)bsearch(&key,
191 specs->tag2el, specs->tag2el_count,
192 sizeof(specs->tag2el[0]), _search4tag);
195 * Found the element corresponding to the tag.
198 ctx->step = t2m->el_no;
200 } else if(specs->ext_start == -1) {
201 ASN_DEBUG("Unexpected tag %s "
202 "in non-extensible CHOICE %s",
203 ber_tlv_tag_string(tlv_tag), td->name);
209 ASN_DEBUG("Skipping unknown tag %s",
210 ber_tlv_tag_string(tlv_tag));
212 skip = ber_skip_length(opt_codec_ctx,
213 BER_TLV_CONSTRUCTED(ptr),
214 (const char *)ptr + tag_len,
218 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
220 case -1: RETURN(RC_FAIL);
223 ADVANCE(skip + tag_len);
231 * Read in the element.
234 asn_TYPE_member_t *elm;/* CHOICE's element */
235 void *memb_ptr; /* Pointer to the member */
236 void **memb_ptr2; /* Pointer to that pointer */
238 elm = &elements[ctx->step];
241 * Compute the position of the member inside a structure,
242 * and also a type of containment (it may be contained
243 * as pointer or using inline inclusion).
245 if(elm->flags & ATF_POINTER) {
246 /* Member is a pointer to another structure */
247 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
250 * A pointer to a pointer
251 * holding the start of the structure
253 memb_ptr = (char *)st + elm->memb_offset;
254 memb_ptr2 = &memb_ptr;
256 /* Set presence to be able to free it properly at any time */
257 _set_present_idx(st, specs->pres_offset,
258 specs->pres_size, ctx->step + 1);
260 * Invoke the member fetch routine according to member's type
262 rval = elm->type->ber_decoder(opt_codec_ctx, elm->type,
263 memb_ptr2, ptr, LEFT, elm->tag_mode);
267 case RC_WMORE: /* More data expected */
268 if(!SIZE_VIOLATION) {
269 ADVANCE(rval.consumed);
273 case RC_FAIL: /* Fatal error */
277 ADVANCE(rval.consumed);
284 ASN_DEBUG("CHOICE %s Leftover: %ld, size = %ld, tm=%d, tc=%d",
285 td->name, (long)ctx->left, (long)size,
286 tag_mode, td->tags_count);
290 * The type must be fully decoded
291 * by the CHOICE member-specific decoder.
297 && !(tag_mode || td->tags_count)) {
299 * This is an untagged CHOICE.
300 * It doesn't contain nothing
301 * except for the member itself, including all its tags.
302 * The decoding is completed.
309 * Read in the "end of data chunks"'s.
311 while(ctx->left < 0) {
314 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
316 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
318 case -1: RETURN(RC_FAIL);
324 if(((const uint8_t *)ptr)[0] == 0) {
330 } else if(((const uint8_t *)ptr)[1] == 0) {
332 * Correctly finished with <0><0>.
339 ASN_DEBUG("Unexpected continuation in %s",
349 /* No meaningful work here */
357 CHOICE_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
358 int tag_mode, ber_tlv_tag_t tag,
359 asn_app_consume_bytes_f *cb, void *app_key) {
360 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
361 asn_TYPE_member_t *elm; /* CHOICE element */
362 asn_enc_rval_t erval;
364 size_t computed_size = 0;
367 if(!sptr) ASN__ENCODE_FAILED;
369 ASN_DEBUG("%s %s as CHOICE",
370 cb?"Encoding":"Estimating", td->name);
372 present = _fetch_present_idx(sptr,
373 specs->pres_offset, specs->pres_size);
376 * If the structure was not initialized, it cannot be encoded:
377 * can't deduce what to encode in the choice type.
379 if(present <= 0 || present > td->elements_count) {
380 if(present == 0 && td->elements_count == 0) {
381 /* The CHOICE is empty?! */
383 ASN__ENCODED_OK(erval);
389 * Seek over the present member of the structure.
391 elm = &td->elements[present-1];
392 if(elm->flags & ATF_POINTER) {
393 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
397 ASN__ENCODED_OK(erval);
399 /* Mandatory element absent */
403 memb_ptr = (void *)((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->der_encoder(elm->type, memb_ptr,
419 elm->tag_mode, elm->tag, 0, 0);
420 if(erval.encoded == -1)
423 /* Encode CHOICE with parent or my own tag */
424 ret = der_write_tags(td, erval.encoded, tag_mode, 1, tag,
428 computed_size += ret;
432 * Encode the single underlying member.
434 erval = elm->type->der_encoder(elm->type, memb_ptr,
435 elm->tag_mode, elm->tag, cb, app_key);
436 if(erval.encoded == -1)
439 ASN_DEBUG("Encoded CHOICE member in %ld bytes (+%ld)",
440 (long)erval.encoded, (long)computed_size);
442 erval.encoded += computed_size;
448 CHOICE_outmost_tag(const asn_TYPE_descriptor_t *td, const void *ptr, int tag_mode, ber_tlv_tag_t tag) {
449 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
452 assert(tag_mode == 0); (void)tag_mode;
453 assert(tag == 0); (void)tag;
456 * Figure out which CHOICE element is encoded.
458 present = _fetch_present_idx(ptr, specs->pres_offset, specs->pres_size);
460 if(present > 0 || present <= td->elements_count) {
461 const asn_TYPE_member_t *elm = &td->elements[present-1];
462 const void *memb_ptr;
464 if(elm->flags & ATF_POINTER) {
465 memb_ptr = *(const void * const *)
466 ((const char *)ptr + elm->memb_offset);
468 memb_ptr = (const void *)
469 ((const char *)ptr + elm->memb_offset);
472 return asn_TYPE_outmost_tag(elm->type, memb_ptr,
473 elm->tag_mode, elm->tag);
475 return (ber_tlv_tag_t)-1;
480 CHOICE_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
481 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
482 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
486 ASN__CTFAIL(app_key, td, sptr,
487 "%s: value not given (%s:%d)",
488 td->name, __FILE__, __LINE__);
493 * Figure out which CHOICE element is encoded.
495 present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
496 if(present > 0 && present <= td->elements_count) {
497 asn_TYPE_member_t *elm = &td->elements[present-1];
498 const void *memb_ptr;
500 if(elm->flags & ATF_POINTER) {
501 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
505 ASN__CTFAIL(app_key, td, sptr,
506 "%s: mandatory CHOICE element %s absent (%s:%d)",
507 td->name, elm->name, __FILE__, __LINE__);
511 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
514 if(elm->memb_constraints) {
515 return elm->memb_constraints(elm->type, memb_ptr,
518 int ret = elm->type->check_constraints(elm->type,
519 memb_ptr, ctfailcb, app_key);
521 * Cannot inherit it eralier:
522 * need to make sure we get the updated version.
524 elm->memb_constraints = elm->type->check_constraints;
528 ASN__CTFAIL(app_key, td, sptr,
529 "%s: no CHOICE element given (%s:%d)",
530 td->name, __FILE__, __LINE__);
536 #define XER_ADVANCE(num_bytes) do { \
537 size_t num = num_bytes; \
538 buf_ptr = (const void *)(((const char *)buf_ptr) + num); \
540 consumed_myself += num; \
544 * Decode the XER (XML) data.
547 CHOICE_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
548 void **struct_ptr, const char *opt_mname,
549 const void *buf_ptr, size_t size) {
551 * Bring closer parts of structure description.
553 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
554 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
557 * Parts of the structure being constructed.
559 void *st = *struct_ptr; /* Target structure. */
560 asn_struct_ctx_t *ctx; /* Decoder context */
562 asn_dec_rval_t rval; /* Return value of a decoder */
563 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
564 int edx; /* Element index */
567 * Create the target structure if it is not present already.
570 st = *struct_ptr = CALLOC(1, specs->struct_size);
571 if(st == 0) RETURN(RC_FAIL);
575 * Restore parsing context.
577 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
578 if(ctx->phase == 0 && !*xml_tag)
579 ctx->phase = 1; /* Skip the outer tag checking phase */
582 * Phases of XER/XML processing:
583 * Phase 0: Check that the opening tag matches our expectations.
584 * Phase 1: Processing body and reacting on closing tag.
585 * Phase 2: Processing inner type.
586 * Phase 3: Only waiting for closing tag.
587 * Phase 4: Skipping unknown extensions.
588 * Phase 5: PHASED OUT
590 for(edx = ctx->step; ctx->phase <= 4;) {
591 pxer_chunk_type_e ch_type; /* XER chunk type */
592 ssize_t ch_size; /* Chunk size */
593 xer_check_tag_e tcv; /* Tag check value */
594 asn_TYPE_member_t *elm;
597 * Go inside the member.
599 if(ctx->phase == 2) {
600 asn_dec_rval_t tmprval;
601 void *memb_ptr; /* Pointer to the member */
602 void **memb_ptr2; /* Pointer to that pointer */
604 elm = &td->elements[edx];
606 if(elm->flags & ATF_POINTER) {
607 /* Member is a pointer to another structure */
608 memb_ptr2 = (void **)((char *)st
611 memb_ptr = (char *)st + elm->memb_offset;
612 memb_ptr2 = &memb_ptr;
615 /* Start/Continue decoding the inner member */
616 tmprval = elm->type->xer_decoder(opt_codec_ctx,
617 elm->type, memb_ptr2, elm->name,
619 XER_ADVANCE(tmprval.consumed);
620 ASN_DEBUG("XER/CHOICE: itdf: [%s] code=%d",
621 elm->type->name, tmprval.code);
622 if(tmprval.code != RC_OK)
623 RETURN(tmprval.code);
624 assert(_fetch_present_idx(st,
625 specs->pres_offset, specs->pres_size) == 0);
626 /* Record what we've got */
628 specs->pres_offset, specs->pres_size, edx + 1);
633 /* No need to wait for closing tag; special mode. */
634 if(ctx->phase == 3 && !*xml_tag) {
635 ctx->phase = 5; /* Phase out */
640 * Get the next part of the XML stream.
642 ch_size = xer_next_token(&ctx->context, buf_ptr, size, &ch_type);
649 case PXER_COMMENT: /* Got XML comment */
650 case PXER_TEXT: /* Ignore free-standing text */
651 XER_ADVANCE(ch_size); /* Skip silently */
654 break; /* Check the rest down there */
658 tcv = xer_check_tag(buf_ptr, ch_size, xml_tag);
659 ASN_DEBUG("XER/CHOICE checked [%c%c%c%c] vs [%s], tcv=%d",
660 ch_size>0?((const uint8_t *)buf_ptr)[0]:'?',
661 ch_size>1?((const uint8_t *)buf_ptr)[1]:'?',
662 ch_size>2?((const uint8_t *)buf_ptr)[2]:'?',
663 ch_size>3?((const uint8_t *)buf_ptr)[3]:'?',
666 /* Skip the extensions section */
667 if(ctx->phase == 4) {
668 ASN_DEBUG("skip_unknown(%d, %ld)",
669 tcv, (long)ctx->left);
670 switch(xer_skip_unknown(tcv, &ctx->left)) {
679 XER_ADVANCE(ch_size);
689 break; /* No CHOICE? */
693 XER_ADVANCE(ch_size);
694 ctx->phase = 5; /* Phase out */
697 if(ctx->phase == 0) {
698 XER_ADVANCE(ch_size);
699 ctx->phase = 1; /* Processing body phase */
707 break; /* Really unexpected */
710 * Search which inner member corresponds to this tag.
712 for(edx = 0; edx < td->elements_count; edx++) {
713 elm = &td->elements[edx];
714 tcv = xer_check_tag(buf_ptr,ch_size,elm->name);
719 * Process this member.
728 edx = td->elements_count;
729 break; /* Phase out */
733 if(edx != td->elements_count)
736 /* It is expected extension */
737 if(specs->ext_start != -1) {
738 ASN_DEBUG("Got anticipated extension");
740 * Check for (XCT_BOTH or XCT_UNKNOWN_BO)
741 * By using a mask. Only record a pure
744 if(tcv & XCT_CLOSING) {
745 /* Found </extension> without body */
746 ctx->phase = 3; /* Terminating */
749 ctx->phase = 4; /* Skip ...'s */
751 XER_ADVANCE(ch_size);
760 ASN_DEBUG("Unexpected XML tag [%c%c%c%c] in CHOICE [%s]"
762 ch_size>0?((const uint8_t *)buf_ptr)[0]:'?',
763 ch_size>1?((const uint8_t *)buf_ptr)[1]:'?',
764 ch_size>2?((const uint8_t *)buf_ptr)[2]:'?',
765 ch_size>3?((const uint8_t *)buf_ptr)[3]:'?',
766 td->name, ctx->phase, xml_tag);
770 ctx->phase = 5; /* Phase out, just in case */
776 CHOICE_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
777 int ilevel, enum xer_encoder_flags_e flags,
778 asn_app_consume_bytes_f *cb, void *app_key) {
779 asn_CHOICE_specifics_t *specs=(asn_CHOICE_specifics_t *)td->specifics;
787 * Figure out which CHOICE element is encoded.
789 present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
791 if(present <= 0 || present > td->elements_count) {
794 asn_enc_rval_t tmper;
795 asn_TYPE_member_t *elm = &td->elements[present-1];
797 const char *mname = elm->name;
798 unsigned int mlen = strlen(mname);
800 if(elm->flags & ATF_POINTER) {
801 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
802 if(!memb_ptr) ASN__ENCODE_FAILED;
804 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
809 if(!(flags & XER_F_CANONICAL)) ASN__TEXT_INDENT(1, ilevel);
810 ASN__CALLBACK3("<", 1, mname, mlen, ">", 1);
812 tmper = elm->type->xer_encoder(elm->type, memb_ptr,
813 ilevel + 1, flags, cb, app_key);
814 if(tmper.encoded == -1) return tmper;
816 ASN__CALLBACK3("</", 2, mname, mlen, ">", 1);
818 er.encoded += 5 + (2 * mlen) + tmper.encoded;
821 if(!(flags & XER_F_CANONICAL)) ASN__TEXT_INDENT(1, ilevel - 1);
829 CHOICE_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
830 asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
831 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
833 asn_per_constraint_t *ct;
834 asn_TYPE_member_t *elm; /* CHOICE's element */
840 if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx))
844 * Create the target structure if it is not present already.
847 st = *sptr = CALLOC(1, specs->struct_size);
848 if(!st) ASN__DECODE_FAILED;
851 if(constraints) ct = &constraints->value;
852 else if(td->per_constraints) ct = &td->per_constraints->value;
855 if(ct && ct->flags & APC_EXTENSIBLE) {
856 value = per_get_few_bits(pd, 1);
857 if(value < 0) ASN__DECODE_STARVED;
858 if(value) ct = 0; /* Not restricted */
861 if(ct && ct->range_bits >= 0) {
862 value = per_get_few_bits(pd, ct->range_bits);
863 if(value < 0) ASN__DECODE_STARVED;
864 ASN_DEBUG("CHOICE %s got index %d in range %d",
865 td->name, value, ct->range_bits);
866 if(value > ct->upper_bound)
869 if(specs->ext_start == -1)
871 value = uper_get_nsnnwn(pd);
872 if(value < 0) ASN__DECODE_STARVED;
873 value += specs->ext_start;
874 if(value >= td->elements_count)
878 /* Adjust if canonical order is different from natural order */
879 if(specs->canonical_order)
880 value = specs->canonical_order[value];
882 /* Set presence to be able to free it later */
883 _set_present_idx(st, specs->pres_offset, specs->pres_size, value + 1);
885 elm = &td->elements[value];
886 if(elm->flags & ATF_POINTER) {
887 /* Member is a pointer to another structure */
888 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
890 memb_ptr = (char *)st + elm->memb_offset;
891 memb_ptr2 = &memb_ptr;
893 ASN_DEBUG("Discovered CHOICE %s encodes %s", td->name, elm->name);
895 if(ct && ct->range_bits >= 0) {
896 rv = elm->type->uper_decoder(opt_codec_ctx, elm->type,
897 elm->per_constraints, memb_ptr2, pd);
899 rv = uper_open_type_get(opt_codec_ctx, elm->type,
900 elm->per_constraints, memb_ptr2, pd);
904 ASN_DEBUG("Failed to decode %s in %s (CHOICE) %d",
905 elm->name, td->name, rv.code);
910 CHOICE_encode_uper(asn_TYPE_descriptor_t *td,
911 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
912 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
913 asn_TYPE_member_t *elm; /* CHOICE's element */
914 asn_per_constraint_t *ct;
919 if(!sptr) ASN__ENCODE_FAILED;
921 ASN_DEBUG("Encoding %s as CHOICE", td->name);
923 if(constraints) ct = &constraints->value;
924 else if(td->per_constraints) ct = &td->per_constraints->value;
927 present = _fetch_present_idx(sptr,
928 specs->pres_offset, specs->pres_size);
931 * If the structure was not initialized properly, it cannot be encoded:
932 * can't deduce what to encode in the choice type.
934 if(present <= 0 || present > td->elements_count)
939 ASN_DEBUG("Encoding %s CHOICE element %d", td->name, present);
941 /* Adjust if canonical order is different from natural order */
942 if(specs->canonical_order)
943 present_enc = specs->canonical_order[present];
945 present_enc = present;
947 if(ct && ct->range_bits >= 0) {
948 if(present_enc < ct->lower_bound
949 || present_enc > ct->upper_bound) {
950 if(ct->flags & APC_EXTENSIBLE) {
951 if(per_put_few_bits(po, 1, 1))
959 if(ct && ct->flags & APC_EXTENSIBLE)
960 if(per_put_few_bits(po, 0, 1))
963 elm = &td->elements[present];
964 if(elm->flags & ATF_POINTER) {
965 /* Member is a pointer to another structure */
966 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
967 if(!memb_ptr) ASN__ENCODE_FAILED;
969 memb_ptr = (char *)sptr + elm->memb_offset;
972 if(ct && ct->range_bits >= 0) {
973 if(per_put_few_bits(po, present_enc, ct->range_bits))
976 return elm->type->uper_encoder(elm->type, elm->per_constraints,
980 if(specs->ext_start == -1)
982 if(uper_put_nsnnwn(po, present_enc - specs->ext_start))
984 if(uper_open_type_put(elm->type, elm->per_constraints,
988 ASN__ENCODED_OK(rval);
994 CHOICE_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
995 asn_app_consume_bytes_f *cb, void *app_key) {
996 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
999 if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
1002 * Figure out which CHOICE element is encoded.
1004 present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
1007 * Print that element.
1009 if(present > 0 && present <= td->elements_count) {
1010 asn_TYPE_member_t *elm = &td->elements[present-1];
1011 const void *memb_ptr;
1013 if(elm->flags & ATF_POINTER) {
1014 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
1015 if(!memb_ptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
1017 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
1020 /* Print member's name and stuff */
1022 if(cb(elm->name, strlen(elm->name), app_key) < 0
1023 || cb(": ", 2, app_key) < 0)
1027 return elm->type->print_struct(elm->type, memb_ptr, ilevel,
1030 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
1035 CHOICE_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
1036 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
1042 ASN_DEBUG("Freeing %s as CHOICE", td->name);
1045 * Figure out which CHOICE element is encoded.
1047 present = _fetch_present_idx(ptr, specs->pres_offset, specs->pres_size);
1050 * Free that element.
1052 if(present > 0 && present <= td->elements_count) {
1053 asn_TYPE_member_t *elm = &td->elements[present-1];
1056 if(elm->flags & ATF_POINTER) {
1057 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
1059 ASN_STRUCT_FREE(*elm->type, memb_ptr);
1061 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
1062 ASN_STRUCT_FREE_CONTENTS_ONLY(*elm->type, memb_ptr);
1066 if(!contents_only) {
1073 * The following functions functions offer protection against -fshort-enums,
1074 * compatible with little- and big-endian machines.
1075 * If assertion is triggered, either disable -fshort-enums, or add an entry
1076 * here with the ->pres_size of your target stracture.
1077 * Unless the target structure is packed, the ".present" member
1078 * is guaranteed to be aligned properly. ASN.1 compiler itself does not
1079 * produce packed code.
1082 _fetch_present_idx(const void *struct_ptr, int pres_offset, int pres_size) {
1083 const void *present_ptr;
1086 present_ptr = ((const char *)struct_ptr) + pres_offset;
1089 case sizeof(int): present = *(const int *)present_ptr; break;
1090 case sizeof(short): present = *(const short *)present_ptr; break;
1091 case sizeof(char): present = *(const char *)present_ptr; break;
1093 /* ANSI C mandates enum to be equivalent to integer */
1094 assert(pres_size != sizeof(int));
1095 return 0; /* If not aborted, pass back safe value */
1102 _set_present_idx(void *struct_ptr, int pres_offset, int pres_size, int present) {
1104 present_ptr = ((char *)struct_ptr) + pres_offset;
1107 case sizeof(int): *(int *)present_ptr = present; break;
1108 case sizeof(short): *(short *)present_ptr = present; break;
1109 case sizeof(char): *(char *)present_ptr = present; break;
1111 /* ANSI C mandates enum to be equivalent to integer */
1112 assert(pres_size != sizeof(int));