2 * Copyright (c) 2003-2017 Lev Walkin <vlm@lionet.info>.
4 * Redistribution and modifications are permitted subject to BSD license.
6 #include <asn_internal.h>
7 #include <constr_SET.h>
9 /* Check that all the mandatory members are present */
10 static int _SET_is_populated(const asn_TYPE_descriptor_t *td, const void *st);
13 * Number of bytes left for this structure.
14 * (ctx->left) indicates the number of bytes _transferred_ for the structure.
15 * (size) contains the number of bytes in the buffer passed.
17 #define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left)
20 * If the subprocessor function returns with an indication that it wants
21 * more data, it may well be a fatal decoding problem, because the
22 * size is constrained by the <TLV>'s L, even if the buffer size allows
24 * For example, consider the buffer containing the following TLVs:
25 * <T:5><L:1><V> <T:6>...
26 * The TLV length clearly indicates that one byte is expected in V, but
27 * if the V processor returns with "want more data" even if the buffer
28 * contains way more data than the V processor have seen.
30 #define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size)
33 * This macro "eats" the part of the buffer which is definitely "consumed",
34 * i.e. was correctly converted into local representation or rightfully skipped.
37 #define ADVANCE(num_bytes) do { \
38 size_t num = num_bytes; \
39 ptr = ((const char *)ptr) + num;\
43 consumed_myself += num; \
47 * Switch to the next phase of parsing.
50 #define NEXT_PHASE(ctx) do { \
56 * Return a standardized complex structure.
59 #define RETURN(_code) do { \
61 rval.consumed = consumed_myself;\
66 * Tags are canonically sorted in the tag2element map.
69 _t2e_cmp(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 SET type.
97 SET_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_SET_specifics_t *specs = (const asn_SET_specifics_t *)td->specifics;
104 const asn_TYPE_member_t *elements = td->elements;
107 * Parts of the structure being constructed.
109 void *st = *struct_ptr; /* Target structure. */
110 asn_struct_ctx_t *ctx; /* Decoder context */
112 ber_tlv_tag_t tlv_tag; /* T from TLV */
113 asn_dec_rval_t rval; /* Return code from subparsers */
115 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
116 size_t edx; /* SET element's index */
118 ASN_DEBUG("Decoding %s as SET", td->name);
120 if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx))
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 rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size,
150 tag_mode, 1, &ctx->left, 0);
151 if(rval.code != RC_OK) {
152 ASN_DEBUG("%s tagging check failed: %d",
153 td->name, rval.code);
158 ctx->left += rval.consumed; /* ?Substracted below! */
159 ADVANCE(rval.consumed);
163 ASN_DEBUG("Structure advertised %ld bytes, "
164 "buffer contains %ld", (long)ctx->left, (long)size);
170 * From the place where we've left it previously,
171 * try to decode the next member from the list of
172 * this structure's elements.
173 * Note that elements in BER may arrive out of
174 * order, yet DER mandates that they shall arive in the
175 * canonical order of their tags. So, there is a room
178 for(;; ctx->step = 0) {
179 const asn_TYPE_tag2member_t *t2m;
180 asn_TYPE_tag2member_t key;
181 void *memb_ptr; /* Pointer to the member */
182 void **memb_ptr2; /* Pointer to that pointer */
183 ssize_t tag_len; /* Length of TLV's T */
186 edx = ctx->step >> 1;
191 * MICROPHASE 1: Synchronize decoding.
196 * No more things to decode.
197 * Exit out of here and check whether all mandatory
198 * elements have been received (in the next phase).
203 * Fetch the T from TLV.
205 tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
207 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
209 case -1: RETURN(RC_FAIL);
212 if(ctx->left < 0 && ((const uint8_t *)ptr)[0] == 0) {
218 } else if(((const uint8_t *)ptr)[1] == 0) {
220 * Found the terminator of the
221 * indefinite length structure.
222 * Invoke the generic finalization function.
228 key.el_tag = tlv_tag;
229 t2m = (const asn_TYPE_tag2member_t *)bsearch(&key,
230 specs->tag2el, specs->tag2el_count,
231 sizeof(specs->tag2el[0]), _t2e_cmp);
234 * Found the element corresponding to the tag.
237 ctx->step = (edx << 1) + 1;
238 ASN_DEBUG("Got tag %s (%s), edx %" ASN_PRI_SSIZE "",
239 ber_tlv_tag_string(tlv_tag), td->name, edx);
240 } else if(specs->extensible == 0) {
241 ASN_DEBUG("Unexpected tag %s "
242 "in non-extensible SET %s",
243 ber_tlv_tag_string(tlv_tag), td->name);
249 ASN_DEBUG("Skipping unknown tag %s",
250 ber_tlv_tag_string(tlv_tag));
252 skip = ber_skip_length(opt_codec_ctx,
253 BER_TLV_CONSTRUCTED(ptr),
254 (const char *)ptr + tag_len, LEFT - tag_len);
257 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
259 case -1: RETURN(RC_FAIL);
262 ADVANCE(skip + tag_len);
263 continue; /* Try again with the next tag */
267 * MICROPHASE 2: Invoke the member-specific decoder.
272 * Check for duplications: must not overwrite
273 * already decoded elements.
275 if(ASN_SET_ISPRESENT2((char *)st + specs->pres_offset, edx)) {
276 ASN_DEBUG("SET %s: Duplicate element %s (%" ASN_PRI_SSIZE ")",
277 td->name, elements[edx].name, edx);
282 * Compute the position of the member inside a structure,
283 * and also a type of containment (it may be contained
284 * as pointer or using inline inclusion).
286 if(elements[edx].flags & ATF_POINTER) {
287 /* Member is a pointer to another structure */
288 memb_ptr2 = (void **)((char *)st + elements[edx].memb_offset);
291 * A pointer to a pointer
292 * holding the start of the structure
294 memb_ptr = (char *)st + elements[edx].memb_offset;
295 memb_ptr2 = &memb_ptr;
298 * Invoke the member fetch routine according to member's type
300 rval = elements[edx].type->op->ber_decoder(opt_codec_ctx,
302 memb_ptr2, ptr, LEFT,
303 elements[edx].tag_mode);
306 ASN_SET_MKPRESENT((char *)st + specs->pres_offset, edx);
308 case RC_WMORE: /* More data expected */
309 if(!SIZE_VIOLATION) {
310 ADVANCE(rval.consumed);
314 case RC_FAIL: /* Fatal error */
318 ADVANCE(rval.consumed);
319 } /* for(all structure members) */
325 case 4: /* Only 00 is expected */
326 ASN_DEBUG("SET %s Leftover: %ld, size = %ld",
327 td->name, (long)ctx->left, (long)size);
330 * Skip everything until the end of the SET.
335 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
337 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
339 case -1: RETURN(RC_FAIL);
343 * If expected <0><0>...
346 && ((const uint8_t *)ptr)[0] == 0) {
352 } else if(((const uint8_t *)ptr)[1] == 0) {
354 * Correctly finished with <0><0>.
363 if(specs->extensible == 0 || ctx->phase == 4) {
364 ASN_DEBUG("Unexpected continuation "
365 "of a non-extensible type %s "
367 td->name, *(const uint8_t *)ptr);
371 ll = ber_skip_length(opt_codec_ctx,
372 BER_TLV_CONSTRUCTED(ptr),
373 (const char *)ptr + tl, LEFT - tl);
375 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
377 case -1: RETURN(RC_FAIL);
386 /* Check that all mandatory elements are present. */
387 if(!_SET_is_populated(td, st))
397 _SET_is_populated(const asn_TYPE_descriptor_t *td, const void *st) {
398 const asn_SET_specifics_t *specs = (const asn_SET_specifics_t *)td->specifics;
402 * Check that all mandatory elements are present.
404 for(edx = 0; edx < td->elements_count;
405 edx += (8 * sizeof(specs->_mandatory_elements[0]))) {
406 unsigned int midx, pres, must;
408 midx = edx/(8 * sizeof(specs->_mandatory_elements[0]));
409 pres = ((const unsigned int *)((const char *)st
410 + specs->pres_offset))[midx];
411 must = sys_ntohl(specs->_mandatory_elements[midx]);
413 if((pres & must) == must) {
415 * Yes, everything seems to be in place.
418 ASN_DEBUG("One or more mandatory elements "
419 "of a SET %s %d (%08x.%08x)=%08x "
425 (~(pres & must) & must)
435 * The DER encoder of the SET type.
438 SET_encode_der(const asn_TYPE_descriptor_t *td, const void *sptr, int tag_mode,
439 ber_tlv_tag_t tag, asn_app_consume_bytes_f *cb, void *app_key) {
440 const asn_SET_specifics_t *specs = (const asn_SET_specifics_t *)td->specifics;
441 size_t computed_size = 0;
443 int t2m_build_own = (specs->tag2el_count != td->elements_count);
444 const asn_TYPE_tag2member_t *t2m;
445 asn_TYPE_tag2member_t *t2m_build;
451 * Use existing, or build our own tags map.
454 t2m_build = (asn_TYPE_tag2member_t *)CALLOC(td->elements_count,
455 sizeof(t2m_build[0]));
456 if(!t2m_build) ASN__ENCODE_FAILED;
461 * There is no untagged CHOICE in this SET.
462 * Employ existing table.
467 * Gather the length of the underlying members sequence.
469 for(edx = 0; edx < td->elements_count; edx++) {
470 asn_TYPE_member_t *elm = &td->elements[edx];
471 asn_enc_rval_t tmper;
472 const void *memb_ptr_dontuse; /* Pointer to the member */
473 const void *const *memb_ptr2; /* Pointer to that pointer */
476 * Compute the length of the encoding of this member.
478 if(elm->flags & ATF_POINTER) {
480 (const void *const *)((const char *)sptr + elm->memb_offset);
483 /* Mandatory elements missing */
488 t2m_build[t2m_count].el_no = edx;
489 t2m_build[t2m_count].el_tag = 0;
496 (const void *)((const char *)sptr + elm->memb_offset);
497 memb_ptr2 = &memb_ptr_dontuse; /* Only use of memb_ptr_dontuse */
500 /* Eliminate default values */
501 if(elm->default_value_cmp && elm->default_value_cmp(*memb_ptr2) == 0) {
503 t2m_build[t2m_count].el_no = edx;
504 t2m_build[t2m_count].el_tag = 0;
510 tmper = elm->type->op->der_encoder(elm->type, *memb_ptr2,
511 elm->tag_mode, elm->tag,
513 if(tmper.encoded == -1)
515 computed_size += tmper.encoded;
518 * Remember the outmost tag of this member.
521 t2m_build[t2m_count].el_no = edx;
522 t2m_build[t2m_count].el_tag = asn_TYPE_outmost_tag(
523 elm->type, *memb_ptr2, elm->tag_mode, elm->tag);
527 * No dynamic sorting is necessary.
533 * Finalize order of the components.
537 * Sort the underlying members according to their
538 * canonical tags order. DER encoding mandates it.
540 qsort(t2m_build, t2m_count, sizeof(specs->tag2el[0]), _t2e_cmp);
544 * Tags are already sorted by the compiler.
547 t2m_count = specs->tag2el_count;
549 assert(t2m_count == td->elements_count);
552 * Encode the TLV for the sequence itself.
554 ret = der_write_tags(td, computed_size, tag_mode, 1, tag, cb, app_key);
559 er.encoded = computed_size + ret;
567 * Encode all members.
569 for(edx = 0; edx < td->elements_count; edx++) {
570 asn_TYPE_member_t *elm;
571 asn_enc_rval_t tmper;
573 const void *memb_ptr_dontuse; /* Pointer to the member */
574 const void *const *memb_ptr2; /* Pointer to that pointer */
576 /* Encode according to the tag order */
577 elm = &td->elements[t2m[edx].el_no];
579 if(elm->flags & ATF_POINTER) {
581 (const void *const *)((const char *)sptr + elm->memb_offset);
582 if(!*memb_ptr2) continue;
585 (const void *)((const char *)sptr + elm->memb_offset);
586 memb_ptr2 = &memb_ptr_dontuse; /* Only use of memb_ptr_dontuse */
589 /* Eliminate default values */
590 if(elm->default_value_cmp && elm->default_value_cmp(*memb_ptr2) == 0)
593 tmper = elm->type->op->der_encoder(elm->type, *memb_ptr2,
594 elm->tag_mode, elm->tag, cb, app_key);
595 if(tmper.encoded == -1)
597 computed_size -= tmper.encoded;
600 if(computed_size != 0) {
602 * Encoded size is not equal to the computed size.
613 #define XER_ADVANCE(num_bytes) do { \
614 size_t num = num_bytes; \
615 buf_ptr = ((const char *)buf_ptr) + num;\
617 consumed_myself += num; \
621 * Decode the XER (XML) data.
624 SET_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
625 const asn_TYPE_descriptor_t *td, void **struct_ptr,
626 const char *opt_mname, const void *buf_ptr, size_t size) {
628 * Bring closer parts of structure description.
630 const asn_SET_specifics_t *specs = (const asn_SET_specifics_t *)td->specifics;
631 const asn_TYPE_member_t *elements = td->elements;
632 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
635 * ... and parts of the structure being constructed.
637 void *st = *struct_ptr; /* Target structure. */
638 asn_struct_ctx_t *ctx; /* Decoder context */
640 asn_dec_rval_t rval; /* Return value from a decoder */
641 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
642 size_t edx; /* Element index */
645 * Create the target structure if it is not present already.
648 st = *struct_ptr = CALLOC(1, specs->struct_size);
649 if(st == 0) RETURN(RC_FAIL);
653 * Restore parsing context.
655 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
658 * Phases of XER/XML processing:
659 * Phase 0: Check that the opening tag matches our expectations.
660 * Phase 1: Processing body and reacting on closing tag.
661 * Phase 2: Processing inner type.
662 * Phase 3: Skipping unknown extensions.
663 * Phase 4: PHASED OUT
665 for(edx = ctx->step; ctx->phase <= 3;) {
666 pxer_chunk_type_e ch_type; /* XER chunk type */
667 ssize_t ch_size; /* Chunk size */
668 xer_check_tag_e tcv; /* Tag check value */
669 const asn_TYPE_member_t *elm;
672 * Go inside the inner member of a set.
674 if(ctx->phase == 2) {
675 asn_dec_rval_t tmprval;
676 void *memb_ptr_dontuse; /* Pointer to the member */
677 void **memb_ptr2; /* Pointer to that pointer */
679 if(ASN_SET_ISPRESENT2((char *)st + specs->pres_offset,
681 ASN_DEBUG("SET %s: Duplicate element %s (%" ASN_PRI_SSIZE ")",
682 td->name, elements[edx].name, edx);
686 elm = &elements[edx];
688 if(elm->flags & ATF_POINTER) {
689 /* Member is a pointer to another structure */
690 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
692 memb_ptr_dontuse = (char *)st + elm->memb_offset;
693 memb_ptr2 = &memb_ptr_dontuse; /* Only use of memb_ptr_dontuse */
696 /* Invoke the inner type decoder, m.b. multiple times */
697 tmprval = elm->type->op->xer_decoder(opt_codec_ctx,
698 elm->type, memb_ptr2, elm->name,
700 XER_ADVANCE(tmprval.consumed);
701 if(tmprval.code != RC_OK)
702 RETURN(tmprval.code);
703 ctx->phase = 1; /* Back to body processing */
704 ASN_SET_MKPRESENT((char *)st + specs->pres_offset, edx);
705 ASN_DEBUG("XER/SET phase => %d", ctx->phase);
710 * Get the next part of the XML stream.
712 ch_size = xer_next_token(&ctx->context,
713 buf_ptr, size, &ch_type);
720 case PXER_COMMENT: /* Got XML comment */
721 case PXER_TEXT: /* Ignore free-standing text */
722 XER_ADVANCE(ch_size); /* Skip silently */
725 break; /* Check the rest down there */
729 tcv = xer_check_tag(buf_ptr, ch_size, xml_tag);
730 ASN_DEBUG("XER/SET: tcv = %d, ph=%d", tcv, ctx->phase);
732 /* Skip the extensions section */
733 if(ctx->phase == 3) {
734 switch(xer_skip_unknown(tcv, &ctx->left)) {
742 XER_ADVANCE(ch_size);
752 if(ctx->phase == 0) break;
756 if(ctx->phase == 0) {
757 if(_SET_is_populated(td, st)) {
758 XER_ADVANCE(ch_size);
759 ctx->phase = 4; /* Phase out */
762 ASN_DEBUG("Premature end of XER SET");
768 if(ctx->phase == 0) {
769 XER_ADVANCE(ch_size);
770 ctx->phase = 1; /* Processing body phase */
777 ASN_DEBUG("XER/SET: tcv=%d, ph=%d", tcv, ctx->phase);
779 break; /* Really unexpected */
782 * Search which member corresponds to this tag.
784 for(edx = 0; edx < td->elements_count; edx++) {
785 switch(xer_check_tag(buf_ptr, ch_size,
786 elements[edx].name)) {
790 * Process this member.
799 edx = td->elements_count;
800 break; /* Phase out */
804 if(edx != td->elements_count)
807 /* It is expected extension */
808 if(specs->extensible) {
809 ASN_DEBUG("Got anticipated extension");
811 * Check for (XCT_BOTH or XCT_UNKNOWN_BO)
812 * By using a mask. Only record a pure
815 if(tcv & XCT_CLOSING) {
816 /* Found </extension> without body */
819 ctx->phase = 3; /* Skip ...'s */
821 XER_ADVANCE(ch_size);
830 ASN_DEBUG("Unexpected XML tag in SET, expected \"%s\"",
835 ctx->phase = 4; /* "Phase out" on hard failure */
840 SET_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
841 enum xer_encoder_flags_e flags, asn_app_consume_bytes_f *cb,
843 const asn_SET_specifics_t *specs = (const asn_SET_specifics_t *)td->specifics;
845 int xcan = (flags & XER_F_CANONICAL);
846 const asn_TYPE_tag2member_t *t2m = specs->tag2el_cxer;
847 size_t t2m_count = specs->tag2el_cxer_count;
853 assert(t2m_count == td->elements_count);
857 for(edx = 0; edx < t2m_count; edx++) {
858 asn_enc_rval_t tmper;
859 asn_TYPE_member_t *elm;
860 const void *memb_ptr;
864 elm = &td->elements[t2m[edx].el_no];
866 mlen = strlen(elm->name);
868 if(elm->flags & ATF_POINTER) {
870 *(const void *const *)((const char *)sptr + elm->memb_offset);
874 /* Mandatory element missing */
878 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
882 ASN__TEXT_INDENT(1, ilevel);
883 ASN__CALLBACK3("<", 1, mname, mlen, ">", 1);
885 /* Print the member itself */
886 tmper = elm->type->op->xer_encoder(elm->type, memb_ptr,
887 ilevel + 1, flags, cb, app_key);
888 if(tmper.encoded == -1) return tmper;
889 er.encoded += tmper.encoded;
891 ASN__CALLBACK3("</", 2, mname, mlen, ">", 1);
894 if(!xcan) ASN__TEXT_INDENT(1, ilevel - 1);
902 SET_print(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
903 asn_app_consume_bytes_f *cb, void *app_key) {
907 if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
910 if(cb(td->name, strlen(td->name), app_key) < 0
911 || cb(" ::= {", 6, app_key) < 0)
914 for(edx = 0; edx < td->elements_count; edx++) {
915 asn_TYPE_member_t *elm = &td->elements[edx];
916 const void *memb_ptr;
918 if(elm->flags & ATF_POINTER) {
919 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
921 if(elm->optional) continue;
922 /* Print <absent> line */
926 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
931 /* Print the member's name and stuff */
932 if(cb(elm->name, strlen(elm->name), app_key) < 0
933 || cb(": ", 2, app_key) < 0)
936 /* Print the member itself */
937 ret = elm->type->op->print_struct(elm->type, memb_ptr, ilevel + 1,
945 return (cb("}", 1, app_key) < 0) ? -1 : 0;
949 SET_free(const asn_TYPE_descriptor_t *td, void *ptr,
950 enum asn_struct_free_method method) {
956 ASN_DEBUG("Freeing %s as SET", td->name);
958 for(edx = 0; edx < td->elements_count; edx++) {
959 asn_TYPE_member_t *elm = &td->elements[edx];
961 if(elm->flags & ATF_POINTER) {
962 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
964 ASN_STRUCT_FREE(*elm->type, memb_ptr);
966 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
967 ASN_STRUCT_FREE_CONTENTS_ONLY(*elm->type, memb_ptr);
972 case ASFM_FREE_EVERYTHING:
975 case ASFM_FREE_UNDERLYING:
977 case ASFM_FREE_UNDERLYING_AND_RESET:
979 ((const asn_SET_specifics_t *)(td->specifics))->struct_size);
985 SET_constraint(const asn_TYPE_descriptor_t *td, const void *sptr,
986 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
990 ASN__CTFAIL(app_key, td, sptr,
991 "%s: value not given (%s:%d)",
992 td->name, __FILE__, __LINE__);
997 * Iterate over structure members and check their validity.
999 for(edx = 0; edx < td->elements_count; edx++) {
1000 asn_TYPE_member_t *elm = &td->elements[edx];
1001 const void *memb_ptr;
1003 if(elm->flags & ATF_POINTER) {
1004 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
1008 ASN__CTFAIL(app_key, td, sptr,
1009 "%s: mandatory element %s absent (%s:%d)",
1010 td->name, elm->name, __FILE__, __LINE__);
1014 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
1017 if(elm->encoding_constraints.general_constraints) {
1018 return elm->encoding_constraints.general_constraints(
1019 elm->type, memb_ptr, ctfailcb, app_key);
1021 return elm->type->encoding_constraints.general_constraints(
1022 elm->type, memb_ptr, ctfailcb, app_key);
1030 SET_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
1034 for(edx = 0; edx < td->elements_count; edx++) {
1035 asn_TYPE_member_t *elm = &td->elements[edx];
1040 if(elm->flags & ATF_POINTER) {
1042 *(const void *const *)((const char *)aptr + elm->memb_offset);
1044 *(const void *const *)((const char *)bptr + elm->memb_offset);
1046 if(!bmemb) continue;
1052 amemb = (const void *)((const char *)aptr + elm->memb_offset);
1053 bmemb = (const void *)((const char *)bptr + elm->memb_offset);
1056 ret = elm->type->op->compare_struct(elm->type, amemb, bmemb);
1057 if(ret != 0) return ret;
1064 asn_TYPE_operation_t asn_OP_SET = {
1072 0, /* SET_decode_oer */
1073 0, /* SET_encode_oer */
1074 0, /* SET_decode_uper */
1075 0, /* SET_encode_uper */
1076 0, /* SET_decode_aper */
1077 0, /* SET_encode_aper */
1079 0 /* Use generic outmost tag fetcher */
1083 asn_random_fill_result_t
1084 SET_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
1085 const asn_encoding_constraints_t *constr,
1086 size_t max_length) {
1087 const asn_SET_specifics_t *specs =
1088 (const asn_SET_specifics_t *)td->specifics;
1089 asn_random_fill_result_t result_ok = {ARFILL_OK, 0};
1090 asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
1091 asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
1095 if(max_length == 0) return result_skipped;
1100 st = CALLOC(1, specs->struct_size);
1102 return result_failed;
1106 for(edx = 0; edx < td->elements_count; edx++) {
1107 const asn_TYPE_member_t *elm = &td->elements[edx];
1108 void *memb_ptr; /* Pointer to the member */
1109 void **memb_ptr2; /* Pointer to that pointer */
1110 asn_random_fill_result_t tmpres;
1112 if(elm->optional && asn_random_between(0, 4) == 2) {
1113 /* Sometimes decide not to fill the optional value */
1117 if(elm->flags & ATF_POINTER) {
1118 /* Member is a pointer to another structure */
1119 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
1121 memb_ptr = (char *)st + elm->memb_offset;
1122 memb_ptr2 = &memb_ptr;
1125 tmpres = elm->type->op->random_fill(
1126 elm->type, memb_ptr2, &elm->encoding_constraints,
1127 max_length > result_ok.length ? max_length - result_ok.length : 0);
1128 switch(tmpres.code) {
1130 result_ok.length += tmpres.length;
1132 case ARFILL_SKIPPED:
1133 assert(!(elm->flags & ATF_POINTER) || *memb_ptr2 == NULL);
1137 ASN_STRUCT_RESET(*td, st);
1139 ASN_STRUCT_FREE(*td, st);