2 * Copyright (c) 2003, 2004, 2005, 2006 Lev Walkin <vlm@lionet.info>.
4 * Redistribution and modifications are permitted subject to BSD license.
6 #include <asn_internal.h>
7 #include <constr_SEQUENCE.h>
10 * Number of bytes left for this structure.
11 * (ctx->left) indicates the number of bytes _transferred_ for the structure.
12 * (size) contains the number of bytes in the buffer passed.
14 #define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left)
17 * If the subprocessor function returns with an indication that it wants
18 * more data, it may well be a fatal decoding problem, because the
19 * size is constrained by the <TLV>'s L, even if the buffer size allows
21 * For example, consider the buffer containing the following TLVs:
22 * <T:5><L:1><V> <T:6>...
23 * The TLV length clearly indicates that one byte is expected in V, but
24 * if the V processor returns with "want more data" even if the buffer
25 * contains way more data than the V processor have seen.
27 #define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size)
30 * This macro "eats" the part of the buffer which is definitely "consumed",
31 * i.e. was correctly converted into local representation or rightfully skipped.
34 #define ADVANCE(num_bytes) do { \
35 size_t num = num_bytes; \
36 ptr = ((const char *)ptr) + num;\
40 consumed_myself += num; \
44 * Switch to the next phase of parsing.
48 #define NEXT_PHASE(ctx) do { \
52 #define PHASE_OUT(ctx) do { ctx->phase = 10; } while(0)
55 * Return a standardized complex structure.
58 #define RETURN(_code) do { \
60 rval.consumed = consumed_myself;\
65 * Check whether we are inside the extensions group.
67 #define IN_EXTENSION_GROUP(specs, memb_idx) \
68 ( ((memb_idx) > (specs)->ext_after) \
69 &&((memb_idx) < (specs)->ext_before))
73 * Tags are canonically sorted in the tag2element map.
76 _t2e_cmp(const void *ap, const void *bp) {
77 const asn_TYPE_tag2member_t *a = (const asn_TYPE_tag2member_t *)ap;
78 const asn_TYPE_tag2member_t *b = (const asn_TYPE_tag2member_t *)bp;
80 int a_class = BER_TAG_CLASS(a->el_tag);
81 int b_class = BER_TAG_CLASS(b->el_tag);
83 if(a_class == b_class) {
84 ber_tlv_tag_t a_value = BER_TAG_VALUE(a->el_tag);
85 ber_tlv_tag_t b_value = BER_TAG_VALUE(b->el_tag);
87 if(a_value == b_value) {
88 if(a->el_no > b->el_no)
91 * Important: we do not check
92 * for a->el_no <= b->el_no!
95 } else if(a_value < b_value)
99 } else if(a_class < b_class) {
108 * The decoder of the SEQUENCE type.
111 SEQUENCE_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
112 void **struct_ptr, const void *ptr, size_t size, int tag_mode) {
114 * Bring closer parts of structure description.
116 asn_SEQUENCE_specifics_t *specs = (asn_SEQUENCE_specifics_t *)td->specifics;
117 asn_TYPE_member_t *elements = td->elements;
120 * Parts of the structure being constructed.
122 void *st = *struct_ptr; /* Target structure. */
123 asn_struct_ctx_t *ctx; /* Decoder context */
125 ber_tlv_tag_t tlv_tag; /* T from TLV */
126 asn_dec_rval_t rval; /* Return code from subparsers */
128 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
129 int edx; /* SEQUENCE element's index */
131 ASN_DEBUG("Decoding %s as SEQUENCE", td->name);
134 * Create the target structure if it is not present already.
137 st = *struct_ptr = CALLOC(1, specs->struct_size);
144 * Restore parsing context.
146 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
149 * Start to parse where left previously
155 * Check that the set of tags associated with given structure
156 * perfectly fits our expectations.
159 rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size,
160 tag_mode, 1, &ctx->left, 0);
161 if(rval.code != RC_OK) {
162 ASN_DEBUG("%s tagging check failed: %d",
163 td->name, rval.code);
168 ctx->left += rval.consumed; /* ?Substracted below! */
169 ADVANCE(rval.consumed);
173 ASN_DEBUG("Structure consumes %ld bytes, buffer %ld",
174 (long)ctx->left, (long)size);
180 * From the place where we've left it previously,
181 * try to decode the next member from the list of
182 * this structure's elements.
183 * (ctx->step) stores the member being processed
184 * between invocations and the microphase {0,1} of parsing
186 * step = (<member_number> * 2 + <microphase>).
188 for(edx = (ctx->step >> 1); edx < td->elements_count;
189 edx++, ctx->step = (ctx->step & ~1) + 2) {
190 void *memb_ptr; /* Pointer to the member */
191 void **memb_ptr2; /* Pointer to that pointer */
192 ssize_t tag_len; /* Length of TLV's T */
193 int opt_edx_end; /* Next non-optional element */
201 * MICROPHASE 1: Synchronize decoding.
203 ASN_DEBUG("In %s SEQUENCE left %d, edx=%d flags=%d"
205 td->name, (int)ctx->left, edx,
206 elements[edx].flags, elements[edx].optional,
209 if(ctx->left == 0 /* No more stuff is expected */
211 /* Explicit OPTIONAL specification reaches the end */
212 (edx + elements[edx].optional
213 == td->elements_count)
215 /* All extensions are optional */
216 (IN_EXTENSION_GROUP(specs, edx)
217 && specs->ext_before > td->elements_count)
220 ASN_DEBUG("End of SEQUENCE %s", td->name);
222 * Found the legitimate end of the structure.
229 * Fetch the T from TLV.
231 tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
232 ASN_DEBUG("Current tag in %s SEQUENCE for element %d "
233 "(%s) is %s encoded in %d bytes, of frame %ld",
234 td->name, edx, elements[edx].name,
235 ber_tlv_tag_string(tlv_tag), (int)tag_len, (long)LEFT);
237 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
239 case -1: RETURN(RC_FAIL);
242 if(ctx->left < 0 && ((const uint8_t *)ptr)[0] == 0) {
248 } else if(((const uint8_t *)ptr)[1] == 0) {
249 ASN_DEBUG("edx = %d, opt = %d, ec=%d",
250 edx, elements[edx].optional,
252 if((edx + elements[edx].optional
253 == td->elements_count)
254 || (IN_EXTENSION_GROUP(specs, edx)
256 > td->elements_count)) {
258 * Yeah, baby! Found the terminator
259 * of the indefinite length structure.
262 * Proceed to the canonical
263 * finalization function.
264 * No advancing is necessary.
272 * Find the next available type with this tag.
275 opt_edx_end = edx + elements[edx].optional + 1;
276 if(opt_edx_end > td->elements_count)
277 opt_edx_end = td->elements_count; /* Cap */
278 else if(opt_edx_end - edx > 8) {
279 /* Limit the scope of linear search... */
280 opt_edx_end = edx + 8;
282 /* ... and resort to bsearch() */
284 for(n = edx; n < opt_edx_end; n++) {
285 if(BER_TAGS_EQUAL(tlv_tag, elements[n].tag)) {
287 * Found element corresponding to the tag
289 * Reposition over the right element.
292 ctx->step = 1 + 2 * edx; /* Remember! */
294 } else if(elements[n].flags & ATF_OPEN_TYPE) {
296 * This is the ANY type, which may bear
297 * any flag whatsoever.
300 ctx->step = 1 + 2 * edx; /* Remember! */
302 } else if(elements[n].tag == (ber_tlv_tag_t)-1) {
309 * Resort to a binary search over
310 * sorted array of tags.
312 asn_TYPE_tag2member_t *t2m;
313 asn_TYPE_tag2member_t key;
314 key.el_tag = tlv_tag;
316 t2m = (asn_TYPE_tag2member_t *)bsearch(&key,
317 specs->tag2el, specs->tag2el_count,
318 sizeof(specs->tag2el[0]), _t2e_cmp);
320 asn_TYPE_tag2member_t *best = 0;
321 asn_TYPE_tag2member_t *t2m_f, *t2m_l;
322 int edx_max = edx + elements[edx].optional;
324 * Rewind to the first element with that tag,
325 * `cause bsearch() does not guarantee order.
327 t2m_f = t2m + t2m->toff_first;
328 t2m_l = t2m + t2m->toff_last;
329 for(t2m = t2m_f; t2m <= t2m_l; t2m++) {
330 if(t2m->el_no > edx_max) break;
331 if(t2m->el_no < edx) continue;
336 ctx->step = 1 + 2 * edx;
342 if(n == opt_edx_end) {
344 * If tag is unknown, it may be either
345 * an unknown (thus, incorrect) tag,
346 * or an extension (...),
347 * or an end of the indefinite-length structure.
349 if(!IN_EXTENSION_GROUP(specs, edx)) {
350 ASN_DEBUG("Unexpected tag %s (at %d)",
351 ber_tlv_tag_string(tlv_tag), edx);
352 ASN_DEBUG("Expected tag %s (%s)%s",
353 ber_tlv_tag_string(elements[edx].tag),
355 elements[edx].optional
356 ?" or alternatives":"");
362 skip = ber_skip_length(opt_codec_ctx,
363 BER_TLV_CONSTRUCTED(ptr),
364 (const char *)ptr + tag_len,
366 ASN_DEBUG("Skip length %d in %s",
367 (int)skip, td->name);
369 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
371 case -1: RETURN(RC_FAIL);
374 ADVANCE(skip + tag_len);
377 continue; /* Try again with the next tag */
382 * MICROPHASE 2: Invoke the member-specific decoder.
384 ctx->step |= 1; /* Confirm entering next microphase */
386 ASN_DEBUG("Inside SEQUENCE %s MF2", td->name);
389 * Compute the position of the member inside a structure,
390 * and also a type of containment (it may be contained
391 * as pointer or using inline inclusion).
393 if(elements[edx].flags & ATF_POINTER) {
394 /* Member is a pointer to another structure */
395 memb_ptr2 = (void **)((char *)st + elements[edx].memb_offset);
398 * A pointer to a pointer
399 * holding the start of the structure
401 memb_ptr = (char *)st + elements[edx].memb_offset;
402 memb_ptr2 = &memb_ptr;
405 * Invoke the member fetch routine according to member's type
407 rval = elements[edx].type->ber_decoder(opt_codec_ctx,
409 memb_ptr2, ptr, LEFT,
410 elements[edx].tag_mode);
411 ASN_DEBUG("In %s SEQUENCE decoded %d %s of %d "
412 "in %d bytes rval.code %d, size=%d",
413 td->name, edx, elements[edx].type->name,
414 (int)LEFT, (int)rval.consumed, rval.code, (int)size);
418 case RC_WMORE: /* More data expected */
419 if(!SIZE_VIOLATION) {
420 ADVANCE(rval.consumed);
423 ASN_DEBUG("Size violation (c->l=%ld <= s=%ld)",
424 (long)ctx->left, (long)size);
426 case RC_FAIL: /* Fatal error */
430 ADVANCE(rval.consumed);
431 } /* for(all structure members) */
435 case 3: /* 00 and other tags expected */
436 case 4: /* only 00's expected */
438 ASN_DEBUG("SEQUENCE %s Leftover: %ld, size = %ld",
439 td->name, (long)ctx->left, (long)size);
442 * Skip everything until the end of the SEQUENCE.
447 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
449 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
451 case -1: RETURN(RC_FAIL);
455 * If expected <0><0>...
458 && ((const uint8_t *)ptr)[0] == 0) {
464 } else if(((const uint8_t *)ptr)[1] == 0) {
466 * Correctly finished with <0><0>.
475 if(!IN_EXTENSION_GROUP(specs, td->elements_count)
476 || ctx->phase == 4) {
477 ASN_DEBUG("Unexpected continuation "
478 "of a non-extensible type "
481 ber_tlv_tag_string(tlv_tag));
485 ll = ber_skip_length(opt_codec_ctx,
486 BER_TLV_CONSTRUCTED(ptr),
487 (const char *)ptr + tl, LEFT - tl);
489 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
491 case -1: RETURN(RC_FAIL);
505 * The DER encoder of the SEQUENCE type.
508 SEQUENCE_encode_der(asn_TYPE_descriptor_t *td,
509 void *sptr, int tag_mode, ber_tlv_tag_t tag,
510 asn_app_consume_bytes_f *cb, void *app_key) {
511 size_t computed_size = 0;
512 asn_enc_rval_t erval;
516 ASN_DEBUG("%s %s as SEQUENCE",
517 cb?"Encoding":"Estimating", td->name);
520 * Gather the length of the underlying members sequence.
522 for(edx = 0; edx < td->elements_count; edx++) {
523 asn_TYPE_member_t *elm = &td->elements[edx];
525 if(elm->flags & ATF_POINTER) {
526 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
528 if(elm->optional) continue;
529 /* Mandatory element is missing */
533 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
535 erval = elm->type->der_encoder(elm->type, memb_ptr,
536 elm->tag_mode, elm->tag,
538 if(erval.encoded == -1)
540 computed_size += erval.encoded;
541 ASN_DEBUG("Member %d %s estimated %ld bytes",
542 edx, elm->name, (long)erval.encoded);
546 * Encode the TLV for the sequence itself.
548 ret = der_write_tags(td, computed_size, tag_mode, 1, tag, cb, app_key);
549 ASN_DEBUG("Wrote tags: %ld (+%ld)", (long)ret, (long)computed_size);
552 erval.encoded = computed_size + ret;
554 if(!cb) _ASN_ENCODED_OK(erval);
557 * Encode all members.
559 for(edx = 0; edx < td->elements_count; edx++) {
560 asn_TYPE_member_t *elm = &td->elements[edx];
561 asn_enc_rval_t tmperval;
564 if(elm->flags & ATF_POINTER) {
565 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
566 if(!memb_ptr) continue;
568 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
570 tmperval = elm->type->der_encoder(elm->type, memb_ptr,
571 elm->tag_mode, elm->tag,
573 if(tmperval.encoded == -1)
575 computed_size -= tmperval.encoded;
576 ASN_DEBUG("Member %d %s of SEQUENCE %s encoded in %ld bytes",
577 edx, elm->name, td->name, (long)tmperval.encoded);
580 if(computed_size != 0)
582 * Encoded size is not equal to the computed size.
586 _ASN_ENCODED_OK(erval);
591 #define XER_ADVANCE(num_bytes) do { \
592 size_t num = num_bytes; \
593 buf_ptr = ((const char *)buf_ptr) + num;\
595 consumed_myself += num; \
599 * Decode the XER (XML) data.
602 SEQUENCE_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
603 void **struct_ptr, const char *opt_mname,
604 const void *buf_ptr, size_t size) {
606 * Bring closer parts of structure description.
608 asn_SEQUENCE_specifics_t *specs
609 = (asn_SEQUENCE_specifics_t *)td->specifics;
610 asn_TYPE_member_t *elements = td->elements;
611 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
614 * ... and parts of the structure being constructed.
616 void *st = *struct_ptr; /* Target structure. */
617 asn_struct_ctx_t *ctx; /* Decoder context */
619 asn_dec_rval_t rval; /* Return value from a decoder */
620 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
621 int edx; /* Element index */
625 * Create the target structure if it is not present already.
628 st = *struct_ptr = CALLOC(1, specs->struct_size);
629 if(st == 0) RETURN(RC_FAIL);
633 * Restore parsing context.
635 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
639 * Phases of XER/XML processing:
640 * Phase 0: Check that the opening tag matches our expectations.
641 * Phase 1: Processing body and reacting on closing tag.
642 * Phase 2: Processing inner type.
643 * Phase 3: Skipping unknown extensions.
644 * Phase 4: PHASED OUT
646 for(edx = ctx->step; ctx->phase <= 3;) {
647 pxer_chunk_type_e ch_type; /* XER chunk type */
648 ssize_t ch_size; /* Chunk size */
649 xer_check_tag_e tcv; /* Tag check value */
650 asn_TYPE_member_t *elm;
653 * Go inside the inner member of a sequence.
655 if(ctx->phase == 2) {
656 asn_dec_rval_t tmprval;
657 void *memb_ptr; /* Pointer to the member */
658 void **memb_ptr2; /* Pointer to that pointer */
660 elm = &td->elements[edx];
662 if(elm->flags & ATF_POINTER) {
663 /* Member is a pointer to another structure */
664 memb_ptr2 = (void **)((char *)st
667 memb_ptr = (char *)st + elm->memb_offset;
668 memb_ptr2 = &memb_ptr;
671 /* Invoke the inner type decoder, m.b. multiple times */
672 tmprval = elm->type->xer_decoder(opt_codec_ctx,
673 elm->type, memb_ptr2, elm->name,
675 XER_ADVANCE(tmprval.consumed);
676 if(tmprval.code != RC_OK)
677 RETURN(tmprval.code);
678 ctx->phase = 1; /* Back to body processing */
680 ASN_DEBUG("XER/SEQUENCE phase => %d, step => %d",
681 ctx->phase, ctx->step);
686 * Get the next part of the XML stream.
688 ch_size = xer_next_token(&ctx->context, buf_ptr, size,
691 case -1: RETURN(RC_FAIL);
692 case 0: RETURN(RC_WMORE);
695 case PXER_COMMENT: /* Got XML comment */
696 case PXER_TEXT: /* Ignore free-standing text */
697 XER_ADVANCE(ch_size); /* Skip silently */
700 break; /* Check the rest down there */
704 tcv = xer_check_tag(buf_ptr, ch_size, xml_tag);
705 ASN_DEBUG("XER/SEQUENCE: tcv = %d, ph=%d [%s]",
706 tcv, ctx->phase, xml_tag);
708 /* Skip the extensions section */
709 if(ctx->phase == 3) {
710 switch(xer_skip_unknown(tcv, &ctx->left)) {
715 XER_ADVANCE(ch_size);
718 XER_ADVANCE(ch_size);
729 if(ctx->phase == 0) break;
733 if(ctx->phase == 0) {
734 if(edx >= td->elements_count
736 /* Explicit OPTIONAL specs reaches the end */
737 (edx + elements[edx].optional
738 == td->elements_count)
740 /* All extensions are optional */
741 (IN_EXTENSION_GROUP(specs, edx)
743 > td->elements_count)
745 XER_ADVANCE(ch_size);
746 ctx->phase = 4; /* Phase out */
749 ASN_DEBUG("Premature end of XER SEQUENCE");
755 if(ctx->phase == 0) {
756 XER_ADVANCE(ch_size);
757 ctx->phase = 1; /* Processing body phase */
764 ASN_DEBUG("XER/SEQUENCE: tcv=%d, ph=%d, edx=%d",
765 tcv, ctx->phase, edx);
766 if(ctx->phase != 1) {
767 break; /* Really unexpected */
770 if(edx < td->elements_count) {
772 * Search which member corresponds to this tag.
774 edx_end = edx + elements[edx].optional + 1;
775 if(edx_end > td->elements_count)
776 edx_end = td->elements_count;
778 for(n = edx; n < edx_end; n++) {
779 elm = &td->elements[n];
780 tcv = xer_check_tag(buf_ptr,
786 * Process this member.
796 break; /* Phase out */
803 ASN_DEBUG("Out of defined members: %d/%d",
804 edx, td->elements_count);
807 /* It is expected extension */
808 if(IN_EXTENSION_GROUP(specs,
809 edx + (edx < td->elements_count
810 ? elements[edx].optional : 0))) {
811 ASN_DEBUG("Got anticipated extension at %d",
814 * Check for (XCT_BOTH or XCT_UNKNOWN_BO)
815 * By using a mask. Only record a pure
818 if(tcv & XCT_CLOSING) {
819 /* Found </extension> without body */
822 ctx->phase = 3; /* Skip ...'s */
824 XER_ADVANCE(ch_size);
833 ASN_DEBUG("Unexpected XML tag in SEQUENCE [%c%c%c%c%c%c]",
834 size>0?((const char *)buf_ptr)[0]:'.',
835 size>1?((const char *)buf_ptr)[1]:'.',
836 size>2?((const char *)buf_ptr)[2]:'.',
837 size>3?((const char *)buf_ptr)[3]:'.',
838 size>4?((const char *)buf_ptr)[4]:'.',
839 size>5?((const char *)buf_ptr)[5]:'.');
843 ctx->phase = 4; /* "Phase out" on hard failure */
848 SEQUENCE_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
849 int ilevel, enum xer_encoder_flags_e flags,
850 asn_app_consume_bytes_f *cb, void *app_key) {
852 int xcan = (flags & XER_F_CANONICAL);
860 for(edx = 0; edx < td->elements_count; edx++) {
861 asn_enc_rval_t tmper;
862 asn_TYPE_member_t *elm = &td->elements[edx];
864 const char *mname = elm->name;
865 unsigned int mlen = strlen(mname);
867 if(elm->flags & ATF_POINTER) {
868 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
872 /* Mandatory element is missing */
876 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
879 if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel);
880 _ASN_CALLBACK3("<", 1, mname, mlen, ">", 1);
882 /* Print the member itself */
883 tmper = elm->type->xer_encoder(elm->type, memb_ptr,
884 ilevel + 1, flags, cb, app_key);
885 if(tmper.encoded == -1) return tmper;
887 _ASN_CALLBACK3("</", 2, mname, mlen, ">", 1);
888 er.encoded += 5 + (2 * mlen) + tmper.encoded;
891 if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel - 1);
899 SEQUENCE_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
900 asn_app_consume_bytes_f *cb, void *app_key) {
904 if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
907 if(cb(td->name, strlen(td->name), app_key) < 0
908 || cb(" ::= {", 6, app_key) < 0)
911 for(edx = 0; edx < td->elements_count; edx++) {
912 asn_TYPE_member_t *elm = &td->elements[edx];
913 const void *memb_ptr;
915 if(elm->flags & ATF_POINTER) {
916 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
918 if(elm->optional) continue;
919 /* Print <absent> line */
923 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
929 /* Print the member's name and stuff */
930 if(cb(elm->name, strlen(elm->name), app_key) < 0
931 || cb(": ", 2, app_key) < 0)
934 /* Print the member itself */
935 ret = elm->type->print_struct(elm->type, memb_ptr, ilevel + 1,
943 return (cb("}", 1, app_key) < 0) ? -1 : 0;
947 SEQUENCE_free(asn_TYPE_descriptor_t *td, void *sptr, int contents_only) {
953 ASN_DEBUG("Freeing %s as SEQUENCE", td->name);
955 for(edx = 0; edx < td->elements_count; edx++) {
956 asn_TYPE_member_t *elm = &td->elements[edx];
958 if(elm->flags & ATF_POINTER) {
959 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
961 ASN_STRUCT_FREE(*elm->type, memb_ptr);
963 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
964 ASN_STRUCT_FREE_CONTENTS_ONLY(*elm->type, memb_ptr);
974 SEQUENCE_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
975 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
979 _ASN_CTFAIL(app_key, td,
980 "%s: value not given (%s:%d)",
981 td->name, __FILE__, __LINE__);
986 * Iterate over structure members and check their validity.
988 for(edx = 0; edx < td->elements_count; edx++) {
989 asn_TYPE_member_t *elm = &td->elements[edx];
990 const void *memb_ptr;
992 if(elm->flags & ATF_POINTER) {
993 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
997 _ASN_CTFAIL(app_key, td,
998 "%s: mandatory element %s absent (%s:%d)",
999 td->name, elm->name, __FILE__, __LINE__);
1003 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
1006 if(elm->memb_constraints) {
1007 int ret = elm->memb_constraints(elm->type, memb_ptr,
1011 int ret = elm->type->check_constraints(elm->type,
1012 memb_ptr, ctfailcb, app_key);
1015 * Cannot inherit it earlier:
1016 * need to make sure we get the updated version.
1018 elm->memb_constraints = elm->type->check_constraints;
1026 SEQUENCE_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
1027 asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
1028 asn_SEQUENCE_specifics_t *specs = (asn_SEQUENCE_specifics_t *)td->specifics;
1029 void *st = *sptr; /* Target structure. */
1030 int extpresent = 0; /* Extension additions are present */
1031 uint8_t *opres; /* Presence of optional root members */
1032 asn_per_data_t opmd;
1038 if(_ASN_STACK_OVERFLOW_CHECK(opt_codec_ctx))
1042 st = *sptr = CALLOC(1, specs->struct_size);
1043 if(!st) _ASN_DECODE_FAILED;
1046 ASN_DEBUG("Decoding %s as SEQUENCE (UPER)", td->name);
1048 /* Handle extensions */
1049 if(specs->ext_before >= 0) {
1050 extpresent = per_get_few_bits(pd, 1);
1051 if(extpresent < 0) _ASN_DECODE_STARVED;
1054 /* Prepare a place and read-in the presence bitmap */
1055 if(specs->roms_count) {
1056 opres = (uint8_t *)MALLOC(((specs->roms_count + 7) >> 3) + 1);
1057 if(!opres) _ASN_DECODE_FAILED;
1058 /* Get the presence map */
1059 if(per_get_many_bits(pd, opres, 0, specs->roms_count)) {
1061 _ASN_DECODE_STARVED;
1063 opmd.buffer = opres;
1065 opmd.nbits = specs->roms_count;
1066 ASN_DEBUG("Read in presence bitmap for %s of %d bits (%x..)",
1067 td->name, specs->roms_count, *opres);
1070 memset(&opmd, 0, sizeof opmd);
1074 * Get the sequence ROOT elements.
1076 for(edx = 0; edx < ((specs->ext_before < 0)
1077 ? td->elements_count : specs->ext_before + 1); edx++) {
1078 asn_TYPE_member_t *elm = &td->elements[edx];
1079 void *memb_ptr; /* Pointer to the member */
1080 void **memb_ptr2; /* Pointer to that pointer */
1082 /* Fetch the pointer to this member */
1083 if(elm->flags & ATF_POINTER) {
1084 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
1086 memb_ptr = (char *)st + elm->memb_offset;
1087 memb_ptr2 = &memb_ptr;
1090 /* Deal with optionality */
1092 int present = per_get_few_bits(&opmd, 1);
1093 ASN_DEBUG("Member %s->%s is optional, p=%d (%d->%d)",
1094 td->name, elm->name, present,
1095 (int)opmd.nboff, (int)opmd.nbits);
1097 /* This element is not present */
1098 if(elm->default_value) {
1099 /* Fill-in DEFAULT */
1100 if(elm->default_value(1, memb_ptr2)) {
1105 /* The member is just not present */
1111 /* Fetch the member from the stream */
1112 ASN_DEBUG("Decoding member %s in %s", elm->name, td->name);
1113 rv = elm->type->uper_decoder(opt_codec_ctx, elm->type,
1114 elm->per_constraints, memb_ptr2, pd);
1115 if(rv.code != RC_OK) {
1116 ASN_DEBUG("Failed decode %s in %s",
1117 elm->name, td->name);
1124 * Deal with extensions.
1127 ASN_DEBUG("Extensibility for %s: NOT IMPLEMENTED", td->name);
1130 for(edx = specs->roms_count; edx < specs->roms_count
1131 + specs->aoms_count; edx++) {
1132 asn_TYPE_member_t *elm = &td->elements[edx];
1133 void *memb_ptr; /* Pointer to the member */
1134 void **memb_ptr2; /* Pointer to that pointer */
1136 if(!elm->default_value) continue;
1138 /* Fetch the pointer to this member */
1139 if(elm->flags & ATF_POINTER) {
1140 memb_ptr2 = (void **)((char *)st
1141 + elm->memb_offset);
1143 memb_ptr = (char *)st + elm->memb_offset;
1144 memb_ptr2 = &memb_ptr;
1147 /* Set default value */
1148 if(elm->default_value(1, memb_ptr2)) {
1162 SEQUENCE_encode_uper(asn_TYPE_descriptor_t *td,
1163 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
1164 asn_SEQUENCE_specifics_t *specs
1165 = (asn_SEQUENCE_specifics_t *)td->specifics;
1177 ASN_DEBUG("Encoding %s as SEQUENCE (UPER)", td->name);
1178 if(specs->ext_before >= 0)
1179 _ASN_ENCODE_FAILED; /* We don't encode extensions yet */
1181 /* Encode a presence bitmap */
1182 for(i = 0; i < specs->roms_count; i++) {
1183 asn_TYPE_member_t *elm;
1184 void *memb_ptr; /* Pointer to the member */
1185 void **memb_ptr2; /* Pointer to that pointer */
1188 edx = specs->oms[i];
1189 elm = &td->elements[edx];
1191 /* Fetch the pointer to this member */
1192 if(elm->flags & ATF_POINTER) {
1193 memb_ptr2 = (void **)((char *)sptr + elm->memb_offset);
1194 present = (*memb_ptr2 != 0);
1196 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
1197 memb_ptr2 = &memb_ptr;
1201 /* Eliminate default values */
1202 if(present && elm->default_value
1203 && elm->default_value(0, memb_ptr2) == 1)
1206 ASN_DEBUG("Element %s %s %s->%s is %s",
1207 elm->flags & ATF_POINTER ? "ptr" : "inline",
1208 elm->default_value ? "def" : "wtv",
1209 td->name, elm->name, present ? "present" : "absent");
1210 if(per_put_few_bits(po, present, 1))
1215 * Get the sequence ROOT elements.
1217 for(edx = 0; edx < ((specs->ext_before < 0)
1218 ? td->elements_count : specs->ext_before + 1); edx++) {
1219 asn_TYPE_member_t *elm = &td->elements[edx];
1220 void *memb_ptr; /* Pointer to the member */
1221 void **memb_ptr2; /* Pointer to that pointer */
1223 /* Fetch the pointer to this member */
1224 if(elm->flags & ATF_POINTER) {
1225 memb_ptr2 = (void **)((char *)sptr + elm->memb_offset);
1227 ASN_DEBUG("Element %s %d not present",
1231 /* Mandatory element is missing */
1235 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
1236 memb_ptr2 = &memb_ptr;
1239 /* Eliminate default values */
1240 if(elm->default_value && elm->default_value(0, memb_ptr2) == 1)
1243 er = elm->type->uper_encoder(elm->type, elm->per_constraints,
1245 if(er.encoded == -1)
1249 _ASN_ENCODED_OK(er);