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_SEQUENCE.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.
49 #define NEXT_PHASE(ctx) do { \
53 #define PHASE_OUT(ctx) do { ctx->phase = 10; } while(0)
56 * Return a standardized complex structure.
59 #define RETURN(_code) do { \
61 rval.consumed = consumed_myself;\
66 * Check whether we are inside the extensions group.
68 #define IN_EXTENSION_GROUP(specs, memb_idx) \
69 ( ((memb_idx) > (specs)->ext_after) \
70 &&((memb_idx) < (specs)->ext_before))
74 * Tags are canonically sorted in the tag2element map.
77 _t2e_cmp(const void *ap, const void *bp) {
78 const asn_TYPE_tag2member_t *a = (const asn_TYPE_tag2member_t *)ap;
79 const asn_TYPE_tag2member_t *b = (const asn_TYPE_tag2member_t *)bp;
81 int a_class = BER_TAG_CLASS(a->el_tag);
82 int b_class = BER_TAG_CLASS(b->el_tag);
84 if(a_class == b_class) {
85 ber_tlv_tag_t a_value = BER_TAG_VALUE(a->el_tag);
86 ber_tlv_tag_t b_value = BER_TAG_VALUE(b->el_tag);
88 if(a_value == b_value) {
89 if(a->el_no > b->el_no)
92 * Important: we do not check
93 * for a->el_no <= b->el_no!
96 } else if(a_value < b_value)
100 } else if(a_class < b_class) {
109 * The decoder of the SEQUENCE type.
112 SEQUENCE_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
113 void **struct_ptr, const void *ptr, size_t size, int tag_mode) {
115 * Bring closer parts of structure description.
117 asn_SEQUENCE_specifics_t *specs = (asn_SEQUENCE_specifics_t *)td->specifics;
118 asn_TYPE_member_t *elements = td->elements;
121 * Parts of the structure being constructed.
123 void *st = *struct_ptr; /* Target structure. */
124 asn_struct_ctx_t *ctx; /* Decoder context */
126 ber_tlv_tag_t tlv_tag; /* T from TLV */
127 asn_dec_rval_t rval; /* Return code from subparsers */
129 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
130 int edx; /* SEQUENCE element's index */
132 ASN_DEBUG("Decoding %s as SEQUENCE", td->name);
135 * Create the target structure if it is not present already.
138 st = *struct_ptr = CALLOC(1, specs->struct_size);
145 * Restore parsing context.
147 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
150 * Start to parse where left previously
156 * Check that the set of tags associated with given structure
157 * perfectly fits our expectations.
160 rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size,
161 tag_mode, 1, &ctx->left, 0);
162 if(rval.code != RC_OK) {
163 ASN_DEBUG("%s tagging check failed: %d",
164 td->name, rval.code);
169 ctx->left += rval.consumed; /* ?Substracted below! */
170 ADVANCE(rval.consumed);
174 ASN_DEBUG("Structure consumes %ld bytes, buffer %ld",
175 (long)ctx->left, (long)size);
181 * From the place where we've left it previously,
182 * try to decode the next member from the list of
183 * this structure's elements.
184 * (ctx->step) stores the member being processed
185 * between invocations and the microphase {0,1} of parsing
187 * step = (<member_number> * 2 + <microphase>).
189 for(edx = (ctx->step >> 1); edx < td->elements_count;
190 edx++, ctx->step = (ctx->step & ~1) + 2) {
191 void *memb_ptr; /* Pointer to the member */
192 void **memb_ptr2; /* Pointer to that pointer */
193 ssize_t tag_len; /* Length of TLV's T */
194 int opt_edx_end; /* Next non-optional element */
202 * MICROPHASE 1: Synchronize decoding.
204 ASN_DEBUG("In %s SEQUENCE left %d, edx=%d flags=%d"
206 td->name, (int)ctx->left, edx,
207 elements[edx].flags, elements[edx].optional,
210 if(ctx->left == 0 /* No more stuff is expected */
212 /* Explicit OPTIONAL specification reaches the end */
213 (edx + elements[edx].optional
214 == td->elements_count)
216 /* All extensions are optional */
217 (IN_EXTENSION_GROUP(specs, edx)
218 && specs->ext_before > td->elements_count)
221 ASN_DEBUG("End of SEQUENCE %s", td->name);
223 * Found the legitimate end of the structure.
230 * Fetch the T from TLV.
232 tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
233 ASN_DEBUG("Current tag in %s SEQUENCE for element %d "
234 "(%s) is %s encoded in %d bytes, of frame %ld",
235 td->name, edx, elements[edx].name,
236 ber_tlv_tag_string(tlv_tag), (int)tag_len, (long)LEFT);
238 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
240 case -1: RETURN(RC_FAIL);
243 if(ctx->left < 0 && ((const uint8_t *)ptr)[0] == 0) {
249 } else if(((const uint8_t *)ptr)[1] == 0) {
250 ASN_DEBUG("edx = %d, opt = %d, ec=%d",
251 edx, elements[edx].optional,
253 if((edx + elements[edx].optional
254 == td->elements_count)
255 || (IN_EXTENSION_GROUP(specs, edx)
257 > td->elements_count)) {
259 * Yeah, baby! Found the terminator
260 * of the indefinite length structure.
263 * Proceed to the canonical
264 * finalization function.
265 * No advancing is necessary.
273 * Find the next available type with this tag.
276 opt_edx_end = edx + elements[edx].optional + 1;
277 if(opt_edx_end > td->elements_count)
278 opt_edx_end = td->elements_count; /* Cap */
279 else if(opt_edx_end - edx > 8) {
280 /* Limit the scope of linear search... */
281 opt_edx_end = edx + 8;
283 /* ... and resort to bsearch() */
285 for(n = edx; n < opt_edx_end; n++) {
286 if(BER_TAGS_EQUAL(tlv_tag, elements[n].tag)) {
288 * Found element corresponding to the tag
290 * Reposition over the right element.
293 ctx->step = 1 + 2 * edx; /* Remember! */
295 } else if(elements[n].flags & ATF_OPEN_TYPE) {
297 * This is the ANY type, which may bear
298 * any flag whatsoever.
301 ctx->step = 1 + 2 * edx; /* Remember! */
303 } else if(elements[n].tag == (ber_tlv_tag_t)-1) {
310 * Resort to a binary search over
311 * sorted array of tags.
313 const asn_TYPE_tag2member_t *t2m;
314 asn_TYPE_tag2member_t key;
315 key.el_tag = tlv_tag;
317 t2m = (const asn_TYPE_tag2member_t *)bsearch(&key,
318 specs->tag2el, specs->tag2el_count,
319 sizeof(specs->tag2el[0]), _t2e_cmp);
321 const asn_TYPE_tag2member_t *best = 0;
322 const asn_TYPE_tag2member_t *t2m_f, *t2m_l;
323 int edx_max = edx + elements[edx].optional;
325 * Rewind to the first element with that tag,
326 * `cause bsearch() does not guarantee order.
328 t2m_f = t2m + t2m->toff_first;
329 t2m_l = t2m + t2m->toff_last;
330 for(t2m = t2m_f; t2m <= t2m_l; t2m++) {
331 if(t2m->el_no > edx_max) break;
332 if(t2m->el_no < edx) continue;
337 ctx->step = 1 + 2 * edx;
343 if(n == opt_edx_end) {
345 * If tag is unknown, it may be either
346 * an unknown (thus, incorrect) tag,
347 * or an extension (...),
348 * or an end of the indefinite-length structure.
350 if(!IN_EXTENSION_GROUP(specs,
351 edx + elements[edx].optional)) {
352 ASN_DEBUG("Unexpected tag %s (at %d)",
353 ber_tlv_tag_string(tlv_tag), edx);
354 ASN_DEBUG("Expected tag %s (%s)%s",
355 ber_tlv_tag_string(elements[edx].tag),
357 elements[edx].optional
358 ?" or alternatives":"");
363 edx += elements[edx].optional;
365 ASN_DEBUG("Skipping unexpected %s (at %d)",
366 ber_tlv_tag_string(tlv_tag), edx);
367 skip = ber_skip_length(opt_codec_ctx,
368 BER_TLV_CONSTRUCTED(ptr),
369 (const char *)ptr + tag_len,
371 ASN_DEBUG("Skip length %d in %s",
372 (int)skip, td->name);
374 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
376 case -1: RETURN(RC_FAIL);
379 ADVANCE(skip + tag_len);
382 continue; /* Try again with the next tag */
387 * MICROPHASE 2: Invoke the member-specific decoder.
389 ctx->step |= 1; /* Confirm entering next microphase */
391 ASN_DEBUG("Inside SEQUENCE %s MF2", td->name);
394 * Compute the position of the member inside a structure,
395 * and also a type of containment (it may be contained
396 * as pointer or using inline inclusion).
398 if(elements[edx].flags & ATF_POINTER) {
399 /* Member is a pointer to another structure */
400 memb_ptr2 = (void **)((char *)st + elements[edx].memb_offset);
403 * A pointer to a pointer
404 * holding the start of the structure
406 memb_ptr = (char *)st + elements[edx].memb_offset;
407 memb_ptr2 = &memb_ptr;
410 * Invoke the member fetch routine according to member's type
412 rval = elements[edx].type->ber_decoder(opt_codec_ctx,
414 memb_ptr2, ptr, LEFT,
415 elements[edx].tag_mode);
416 ASN_DEBUG("In %s SEQUENCE decoded %d %s of %d "
417 "in %d bytes rval.code %d, size=%d",
418 td->name, edx, elements[edx].type->name,
419 (int)LEFT, (int)rval.consumed, rval.code, (int)size);
423 case RC_WMORE: /* More data expected */
424 if(!SIZE_VIOLATION) {
425 ADVANCE(rval.consumed);
428 ASN_DEBUG("Size violation (c->l=%ld <= s=%ld)",
429 (long)ctx->left, (long)size);
431 case RC_FAIL: /* Fatal error */
435 ADVANCE(rval.consumed);
436 } /* for(all structure members) */
440 case 3: /* 00 and other tags expected */
441 case 4: /* only 00's expected */
443 ASN_DEBUG("SEQUENCE %s Leftover: %ld, size = %ld",
444 td->name, (long)ctx->left, (long)size);
447 * Skip everything until the end of the SEQUENCE.
452 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
454 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
456 case -1: RETURN(RC_FAIL);
460 * If expected <0><0>...
463 && ((const uint8_t *)ptr)[0] == 0) {
469 } else if(((const uint8_t *)ptr)[1] == 0) {
471 * Correctly finished with <0><0>.
480 if(!IN_EXTENSION_GROUP(specs, td->elements_count)
481 || ctx->phase == 4) {
482 ASN_DEBUG("Unexpected continuation "
483 "of a non-extensible type "
486 ber_tlv_tag_string(tlv_tag));
490 ll = ber_skip_length(opt_codec_ctx,
491 BER_TLV_CONSTRUCTED(ptr),
492 (const char *)ptr + tl, LEFT - tl);
494 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
496 case -1: RETURN(RC_FAIL);
510 * The DER encoder of the SEQUENCE type.
513 SEQUENCE_encode_der(asn_TYPE_descriptor_t *td,
514 void *sptr, int tag_mode, ber_tlv_tag_t tag,
515 asn_app_consume_bytes_f *cb, void *app_key) {
516 size_t computed_size = 0;
517 asn_enc_rval_t erval;
521 ASN_DEBUG("%s %s as SEQUENCE",
522 cb?"Encoding":"Estimating", td->name);
525 * Gather the length of the underlying members sequence.
527 for(edx = 0; edx < td->elements_count; edx++) {
528 asn_TYPE_member_t *elm = &td->elements[edx];
530 if(elm->flags & ATF_POINTER) {
531 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
533 if(elm->optional) continue;
534 /* Mandatory element is missing */
538 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
540 erval = elm->type->der_encoder(elm->type, memb_ptr,
541 elm->tag_mode, elm->tag,
543 if(erval.encoded == -1)
545 computed_size += erval.encoded;
546 ASN_DEBUG("Member %d %s estimated %ld bytes",
547 edx, elm->name, (long)erval.encoded);
551 * Encode the TLV for the sequence itself.
553 ret = der_write_tags(td, computed_size, tag_mode, 1, tag, cb, app_key);
554 ASN_DEBUG("Wrote tags: %ld (+%ld)", (long)ret, (long)computed_size);
557 erval.encoded = computed_size + ret;
559 if(!cb) ASN__ENCODED_OK(erval);
562 * Encode all members.
564 for(edx = 0; edx < td->elements_count; edx++) {
565 asn_TYPE_member_t *elm = &td->elements[edx];
566 asn_enc_rval_t tmperval;
569 if(elm->flags & ATF_POINTER) {
570 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
571 if(!memb_ptr) continue;
573 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
575 tmperval = elm->type->der_encoder(elm->type, memb_ptr,
576 elm->tag_mode, elm->tag,
578 if(tmperval.encoded == -1)
580 computed_size -= tmperval.encoded;
581 ASN_DEBUG("Member %d %s of SEQUENCE %s encoded in %ld bytes",
582 edx, elm->name, td->name, (long)tmperval.encoded);
585 if(computed_size != 0)
587 * Encoded size is not equal to the computed size.
591 ASN__ENCODED_OK(erval);
596 #define XER_ADVANCE(num_bytes) do { \
597 size_t num = num_bytes; \
598 buf_ptr = ((const char *)buf_ptr) + num;\
600 consumed_myself += num; \
604 * Decode the XER (XML) data.
607 SEQUENCE_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
608 void **struct_ptr, const char *opt_mname,
609 const void *buf_ptr, size_t size) {
611 * Bring closer parts of structure description.
613 asn_SEQUENCE_specifics_t *specs
614 = (asn_SEQUENCE_specifics_t *)td->specifics;
615 asn_TYPE_member_t *elements = td->elements;
616 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
619 * ... and parts of the structure being constructed.
621 void *st = *struct_ptr; /* Target structure. */
622 asn_struct_ctx_t *ctx; /* Decoder context */
624 asn_dec_rval_t rval; /* Return value from a decoder */
625 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
626 int edx; /* Element index */
630 * Create the target structure if it is not present already.
633 st = *struct_ptr = CALLOC(1, specs->struct_size);
634 if(st == 0) RETURN(RC_FAIL);
638 * Restore parsing context.
640 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
644 * Phases of XER/XML processing:
645 * Phase 0: Check that the opening tag matches our expectations.
646 * Phase 1: Processing body and reacting on closing tag.
647 * Phase 2: Processing inner type.
648 * Phase 3: Skipping unknown extensions.
649 * Phase 4: PHASED OUT
651 for(edx = ctx->step; ctx->phase <= 3;) {
652 pxer_chunk_type_e ch_type; /* XER chunk type */
653 ssize_t ch_size; /* Chunk size */
654 xer_check_tag_e tcv; /* Tag check value */
655 asn_TYPE_member_t *elm;
659 * Go inside the inner member of a sequence.
661 if(ctx->phase == 2) {
662 asn_dec_rval_t tmprval;
663 void *memb_ptr; /* Pointer to the member */
664 void **memb_ptr2; /* Pointer to that pointer */
666 elm = &td->elements[edx];
668 if(elm->flags & ATF_POINTER) {
669 /* Member is a pointer to another structure */
670 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
672 memb_ptr = (char *)st + elm->memb_offset;
673 memb_ptr2 = &memb_ptr;
676 /* Invoke the inner type decoder, m.b. multiple times */
677 tmprval = elm->type->xer_decoder(opt_codec_ctx,
678 elm->type, memb_ptr2, elm->name,
680 XER_ADVANCE(tmprval.consumed);
681 if(tmprval.code != RC_OK)
682 RETURN(tmprval.code);
683 ctx->phase = 1; /* Back to body processing */
685 ASN_DEBUG("XER/SEQUENCE phase => %d, step => %d",
686 ctx->phase, ctx->step);
691 * Get the next part of the XML stream.
693 ch_size = xer_next_token(&ctx->context, buf_ptr, size,
701 case PXER_COMMENT: /* Got XML comment */
702 case PXER_TEXT: /* Ignore free-standing text */
703 XER_ADVANCE(ch_size); /* Skip silently */
706 break; /* Check the rest down there */
710 tcv = xer_check_tag(buf_ptr, ch_size, xml_tag);
711 ASN_DEBUG("XER/SEQUENCE: tcv = %d, ph=%d [%s]",
712 tcv, ctx->phase, xml_tag);
714 /* Skip the extensions section */
715 if(ctx->phase == 3) {
716 switch(xer_skip_unknown(tcv, &ctx->left)) {
721 XER_ADVANCE(ch_size);
724 XER_ADVANCE(ch_size);
735 if(ctx->phase == 0) break;
739 if(ctx->phase == 0) {
740 if(edx >= td->elements_count
742 /* Explicit OPTIONAL specs reaches the end */
743 (edx + elements[edx].optional
744 == td->elements_count)
746 /* All extensions are optional */
747 (IN_EXTENSION_GROUP(specs, edx)
749 > td->elements_count)
751 XER_ADVANCE(ch_size);
752 ctx->phase = 4; /* Phase out */
755 ASN_DEBUG("Premature end of XER SEQUENCE");
761 if(ctx->phase == 0) {
762 XER_ADVANCE(ch_size);
763 ctx->phase = 1; /* Processing body phase */
770 ASN_DEBUG("XER/SEQUENCE: tcv=%d, ph=%d, edx=%d",
771 tcv, ctx->phase, edx);
772 if(ctx->phase != 1) {
773 break; /* Really unexpected */
776 if(edx < td->elements_count) {
778 * Search which member corresponds to this tag.
780 edx_end = edx + elements[edx].optional + 1;
781 if(edx_end > td->elements_count)
782 edx_end = td->elements_count;
783 for(n = edx; n < edx_end; n++) {
784 elm = &td->elements[n];
785 tcv = xer_check_tag(buf_ptr,
791 * Process this member.
801 break; /* Phase out */
808 ASN_DEBUG("Out of defined members: %d/%d",
809 edx, td->elements_count);
812 /* It is expected extension */
813 if(IN_EXTENSION_GROUP(specs,
814 edx + (edx < td->elements_count
815 ? elements[edx].optional : 0))) {
816 ASN_DEBUG("Got anticipated extension at %d",
819 * Check for (XCT_BOTH or XCT_UNKNOWN_BO)
820 * By using a mask. Only record a pure
823 if(tcv & XCT_CLOSING) {
824 /* Found </extension> without body */
827 ctx->phase = 3; /* Skip ...'s */
829 XER_ADVANCE(ch_size);
838 ASN_DEBUG("Unexpected XML tag in SEQUENCE [%c%c%c%c%c%c]",
839 size>0?((const char *)buf_ptr)[0]:'.',
840 size>1?((const char *)buf_ptr)[1]:'.',
841 size>2?((const char *)buf_ptr)[2]:'.',
842 size>3?((const char *)buf_ptr)[3]:'.',
843 size>4?((const char *)buf_ptr)[4]:'.',
844 size>5?((const char *)buf_ptr)[5]:'.');
848 ctx->phase = 4; /* "Phase out" on hard failure */
853 SEQUENCE_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
854 int ilevel, enum xer_encoder_flags_e flags,
855 asn_app_consume_bytes_f *cb, void *app_key) {
857 int xcan = (flags & XER_F_CANONICAL);
865 for(edx = 0; edx < td->elements_count; edx++) {
866 asn_enc_rval_t tmper;
867 asn_TYPE_member_t *elm = &td->elements[edx];
869 const char *mname = elm->name;
870 unsigned int mlen = strlen(mname);
872 if(elm->flags & ATF_POINTER) {
873 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
877 /* Mandatory element is missing */
881 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
884 if(!xcan) ASN__TEXT_INDENT(1, ilevel);
885 ASN__CALLBACK3("<", 1, mname, mlen, ">", 1);
887 /* Print the member itself */
888 tmper = elm->type->xer_encoder(elm->type, memb_ptr,
889 ilevel + 1, flags, cb, app_key);
890 if(tmper.encoded == -1) return tmper;
892 ASN__CALLBACK3("</", 2, mname, mlen, ">", 1);
893 er.encoded += 5 + (2 * mlen) + tmper.encoded;
896 if(!xcan) ASN__TEXT_INDENT(1, ilevel - 1);
904 SEQUENCE_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
905 asn_app_consume_bytes_f *cb, void *app_key) {
909 if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
912 if(cb(td->name, strlen(td->name), app_key) < 0
913 || cb(" ::= {", 6, app_key) < 0)
916 for(edx = 0; edx < td->elements_count; edx++) {
917 asn_TYPE_member_t *elm = &td->elements[edx];
918 const void *memb_ptr;
920 if(elm->flags & ATF_POINTER) {
921 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
923 if(elm->optional) continue;
924 /* Print <absent> line */
928 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
934 /* Print the member's name and stuff */
935 if(cb(elm->name, strlen(elm->name), app_key) < 0
936 || cb(": ", 2, app_key) < 0)
939 /* Print the member itself */
940 ret = elm->type->print_struct(elm->type, memb_ptr, ilevel + 1,
948 return (cb("}", 1, app_key) < 0) ? -1 : 0;
952 SEQUENCE_free(asn_TYPE_descriptor_t *td, void *sptr, int contents_only) {
958 ASN_DEBUG("Freeing %s as SEQUENCE", td->name);
960 for(edx = 0; edx < td->elements_count; edx++) {
961 asn_TYPE_member_t *elm = &td->elements[edx];
963 if(elm->flags & ATF_POINTER) {
964 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
966 ASN_STRUCT_FREE(*elm->type, memb_ptr);
968 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
969 ASN_STRUCT_FREE_CONTENTS_ONLY(*elm->type, memb_ptr);
979 SEQUENCE_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
980 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
984 ASN__CTFAIL(app_key, td, sptr,
985 "%s: value not given (%s:%d)",
986 td->name, __FILE__, __LINE__);
991 * Iterate over structure members and check their validity.
993 for(edx = 0; edx < td->elements_count; edx++) {
994 asn_TYPE_member_t *elm = &td->elements[edx];
995 const void *memb_ptr;
997 if(elm->flags & ATF_POINTER) {
998 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
1002 ASN__CTFAIL(app_key, td, sptr,
1003 "%s: mandatory element %s absent (%s:%d)",
1004 td->name, elm->name, __FILE__, __LINE__);
1008 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
1011 if(elm->memb_constraints) {
1012 int ret = elm->memb_constraints(elm->type, memb_ptr,
1016 int ret = elm->type->check_constraints(elm->type,
1017 memb_ptr, ctfailcb, app_key);
1020 * Cannot inherit it earlier:
1021 * need to make sure we get the updated version.
1023 elm->memb_constraints = elm->type->check_constraints;
1031 SEQUENCE_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
1032 asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
1033 asn_SEQUENCE_specifics_t *specs = (asn_SEQUENCE_specifics_t *)td->specifics;
1034 void *st = *sptr; /* Target structure. */
1035 int extpresent; /* Extension additions are present */
1036 uint8_t *opres; /* Presence of optional root members */
1037 asn_per_data_t opmd;
1043 if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx))
1047 st = *sptr = CALLOC(1, specs->struct_size);
1048 if(!st) ASN__DECODE_FAILED;
1051 ASN_DEBUG("Decoding %s as SEQUENCE (UPER)", td->name);
1053 /* Handle extensions */
1054 if(specs->ext_before >= 0) {
1055 extpresent = per_get_few_bits(pd, 1);
1056 if(extpresent < 0) ASN__DECODE_STARVED;
1061 /* Prepare a place and read-in the presence bitmap */
1062 memset(&opmd, 0, sizeof(opmd));
1063 if(specs->roms_count) {
1064 opres = (uint8_t *)MALLOC(((specs->roms_count + 7) >> 3) + 1);
1065 if(!opres) ASN__DECODE_FAILED;
1066 /* Get the presence map */
1067 if(per_get_many_bits(pd, opres, 0, specs->roms_count)) {
1069 ASN__DECODE_STARVED;
1071 opmd.buffer = opres;
1072 opmd.nbits = specs->roms_count;
1073 ASN_DEBUG("Read in presence bitmap for %s of %d bits (%x..)",
1074 td->name, specs->roms_count, *opres);
1080 * Get the sequence ROOT elements.
1082 for(edx = 0; edx < td->elements_count; edx++) {
1083 asn_TYPE_member_t *elm = &td->elements[edx];
1084 void *memb_ptr; /* Pointer to the member */
1085 void **memb_ptr2; /* Pointer to that pointer */
1087 if(IN_EXTENSION_GROUP(specs, edx))
1090 /* Fetch the pointer to this member */
1091 if(elm->flags & ATF_POINTER) {
1092 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
1094 memb_ptr = (char *)st + elm->memb_offset;
1095 memb_ptr2 = &memb_ptr;
1098 /* Deal with optionality */
1100 int present = per_get_few_bits(&opmd, 1);
1101 ASN_DEBUG("Member %s->%s is optional, p=%d (%d->%d)",
1102 td->name, elm->name, present,
1103 (int)opmd.nboff, (int)opmd.nbits);
1105 /* This element is not present */
1106 if(elm->default_value) {
1107 /* Fill-in DEFAULT */
1108 if(elm->default_value(1, memb_ptr2)) {
1112 ASN_DEBUG("Filled-in default");
1114 /* The member is just not present */
1120 /* Fetch the member from the stream */
1121 ASN_DEBUG("Decoding member %s in %s", elm->name, td->name);
1122 rv = elm->type->uper_decoder(opt_codec_ctx, elm->type,
1123 elm->per_constraints, memb_ptr2, pd);
1124 if(rv.code != RC_OK) {
1125 ASN_DEBUG("Failed decode %s in %s",
1126 elm->name, td->name);
1132 /* Optionality map is not needed anymore */
1136 * Deal with extensions.
1140 uint8_t *epres; /* Presence of extension members */
1141 asn_per_data_t epmd;
1143 bmlength = uper_get_nslength(pd);
1144 if(bmlength < 0) ASN__DECODE_STARVED;
1146 ASN_DEBUG("Extensions %ld present in %s", (long)bmlength, td->name);
1148 epres = (uint8_t *)MALLOC((bmlength + 15) >> 3);
1149 if(!epres) ASN__DECODE_STARVED;
1151 /* Get the extensions map */
1152 if(per_get_many_bits(pd, epres, 0, bmlength)) {
1154 ASN__DECODE_STARVED;
1157 memset(&epmd, 0, sizeof(epmd));
1158 epmd.buffer = epres;
1159 epmd.nbits = bmlength;
1160 ASN_DEBUG("Read in extensions bitmap for %s of %ld bits (%x..)",
1161 td->name, (long)bmlength, *epres);
1163 /* Go over extensions and read them in */
1164 for(edx = specs->ext_after + 1; edx < td->elements_count; edx++) {
1165 asn_TYPE_member_t *elm = &td->elements[edx];
1166 void *memb_ptr; /* Pointer to the member */
1167 void **memb_ptr2; /* Pointer to that pointer */
1170 if(!IN_EXTENSION_GROUP(specs, edx)) {
1171 ASN_DEBUG("%d is not extension", edx);
1175 /* Fetch the pointer to this member */
1176 if(elm->flags & ATF_POINTER) {
1177 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
1179 memb_ptr = (void *)((char *)st + elm->memb_offset);
1180 memb_ptr2 = &memb_ptr;
1183 present = per_get_few_bits(&epmd, 1);
1185 if(present < 0) break; /* No more extensions */
1189 ASN_DEBUG("Decoding member %s in %s %p", elm->name, td->name, *memb_ptr2);
1190 rv = uper_open_type_get(opt_codec_ctx, elm->type,
1191 elm->per_constraints, memb_ptr2, pd);
1192 if(rv.code != RC_OK) {
1198 /* Skip over overflow extensions which aren't present
1199 * in this system's version of the protocol */
1201 ASN_DEBUG("Getting overflow extensions");
1202 switch(per_get_few_bits(&epmd, 1)) {
1206 if(uper_open_type_skip(opt_codec_ctx, pd)) {
1208 ASN__DECODE_STARVED;
1217 /* Fill DEFAULT members in extensions */
1218 for(edx = specs->roms_count; edx < specs->roms_count
1219 + specs->aoms_count; edx++) {
1220 asn_TYPE_member_t *elm = &td->elements[edx];
1221 void **memb_ptr2; /* Pointer to member pointer */
1223 if(!elm->default_value) continue;
1225 /* Fetch the pointer to this member */
1226 if(elm->flags & ATF_POINTER) {
1227 memb_ptr2 = (void **)((char *)st
1228 + elm->memb_offset);
1229 if(*memb_ptr2) continue;
1231 continue; /* Extensions are all optionals */
1234 /* Set default value */
1235 if(elm->default_value(1, memb_ptr2)) {
1246 SEQUENCE_handle_extensions(asn_TYPE_descriptor_t *td, void *sptr,
1247 asn_per_outp_t *po1, asn_per_outp_t *po2) {
1248 asn_SEQUENCE_specifics_t *specs
1249 = (asn_SEQUENCE_specifics_t *)td->specifics;
1250 int exts_present = 0;
1254 if(specs->ext_before < 0)
1257 /* Find out which extensions are present */
1258 for(edx = specs->ext_after + 1; edx < td->elements_count; edx++) {
1259 asn_TYPE_member_t *elm = &td->elements[edx];
1260 void *memb_ptr; /* Pointer to the member */
1261 void **memb_ptr2; /* Pointer to that pointer */
1264 if(!IN_EXTENSION_GROUP(specs, edx)) {
1265 ASN_DEBUG("%s (@%d) is not extension", elm->type->name, edx);
1269 /* Fetch the pointer to this member */
1270 if(elm->flags & ATF_POINTER) {
1271 memb_ptr2 = (void **)((char *)sptr + elm->memb_offset);
1272 present = (*memb_ptr2 != 0);
1274 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
1275 memb_ptr2 = &memb_ptr;
1279 ASN_DEBUG("checking %s (@%d) present => %d",
1280 elm->type->name, edx, present);
1282 exts_present += present;
1284 /* Encode as presence marker */
1285 if(po1 && per_put_few_bits(po1, present, 1))
1287 /* Encode as open type field */
1288 if(po2 && present && uper_open_type_put(elm->type,
1289 elm->per_constraints, *memb_ptr2, po2))
1294 return exts_present ? exts_count : 0;
1298 SEQUENCE_encode_uper(asn_TYPE_descriptor_t *td,
1299 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
1300 asn_SEQUENCE_specifics_t *specs
1301 = (asn_SEQUENCE_specifics_t *)td->specifics;
1314 ASN_DEBUG("Encoding %s as SEQUENCE (UPER)", td->name);
1318 * X.691#18.1 Whether structure is extensible
1319 * and whether to encode extensions
1321 if(specs->ext_before >= 0) {
1322 n_extensions = SEQUENCE_handle_extensions(td, sptr, 0, 0);
1323 per_put_few_bits(po, n_extensions ? 1 : 0, 1);
1325 n_extensions = 0; /* There are no extensions to encode */
1328 /* Encode a presence bitmap */
1329 for(i = 0; i < specs->roms_count; i++) {
1330 asn_TYPE_member_t *elm;
1331 void *memb_ptr; /* Pointer to the member */
1332 void **memb_ptr2; /* Pointer to that pointer */
1335 edx = specs->oms[i];
1336 elm = &td->elements[edx];
1338 /* Fetch the pointer to this member */
1339 if(elm->flags & ATF_POINTER) {
1340 memb_ptr2 = (void **)((char *)sptr + elm->memb_offset);
1341 present = (*memb_ptr2 != 0);
1343 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
1344 memb_ptr2 = &memb_ptr;
1348 /* Eliminate default values */
1349 if(present && elm->default_value
1350 && elm->default_value(0, memb_ptr2) == 1)
1353 ASN_DEBUG("Element %s %s %s->%s is %s",
1354 elm->flags & ATF_POINTER ? "ptr" : "inline",
1355 elm->default_value ? "def" : "wtv",
1356 td->name, elm->name, present ? "present" : "absent");
1357 if(per_put_few_bits(po, present, 1))
1362 * Encode the sequence ROOT elements.
1364 ASN_DEBUG("ext_after = %d, ec = %d, eb = %d", specs->ext_after, td->elements_count, specs->ext_before);
1365 for(edx = 0; edx < ((specs->ext_after < 0)
1366 ? td->elements_count : specs->ext_before - 1); edx++) {
1368 asn_TYPE_member_t *elm = &td->elements[edx];
1369 void *memb_ptr; /* Pointer to the member */
1370 void **memb_ptr2; /* Pointer to that pointer */
1372 if(IN_EXTENSION_GROUP(specs, edx))
1375 ASN_DEBUG("About to encode %s", elm->type->name);
1377 /* Fetch the pointer to this member */
1378 if(elm->flags & ATF_POINTER) {
1379 memb_ptr2 = (void **)((char *)sptr + elm->memb_offset);
1381 ASN_DEBUG("Element %s %d not present",
1385 /* Mandatory element is missing */
1389 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
1390 memb_ptr2 = &memb_ptr;
1393 /* Eliminate default values */
1394 if(elm->default_value && elm->default_value(0, memb_ptr2) == 1)
1397 ASN_DEBUG("Encoding %s->%s", td->name, elm->name);
1398 er = elm->type->uper_encoder(elm->type, elm->per_constraints,
1400 if(er.encoded == -1)
1404 /* No extensions to encode */
1405 if(!n_extensions) ASN__ENCODED_OK(er);
1407 ASN_DEBUG("Length of %d bit-map", n_extensions);
1408 /* #18.8. Write down the presence bit-map length. */
1409 if(uper_put_nslength(po, n_extensions))
1412 ASN_DEBUG("Bit-map of %d elements", n_extensions);
1413 /* #18.7. Encoding the extensions presence bit-map. */
1414 /* TODO: act upon NOTE in #18.7 for canonical PER */
1415 if(SEQUENCE_handle_extensions(td, sptr, po, 0) != n_extensions)
1418 ASN_DEBUG("Writing %d extensions", n_extensions);
1419 /* #18.9. Encode extensions as open type fields. */
1420 if(SEQUENCE_handle_extensions(td, sptr, 0, po) != n_extensions)
1423 ASN__ENCODED_OK(er);