]> git.stg.codes - stg.git/blob - stglibs/smux.lib/constr_SEQUENCE.c
Minor ChangeLog fix
[stg.git] / stglibs / smux.lib / constr_SEQUENCE.c
1 /*-
2  * Copyright (c) 2003, 2004, 2005, 2006 Lev Walkin <vlm@lionet.info>.
3  * All rights reserved.
4  * Redistribution and modifications are permitted subject to BSD license.
5  */
6 #include <asn_internal.h>
7 #include <constr_SEQUENCE.h>
8
9 /*
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.
13  */
14 #define LEFT    ((size<(size_t)ctx->left)?size:(size_t)ctx->left)
15
16 /*
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
20  * reading more data.
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.
26  */
27 #define SIZE_VIOLATION  (ctx->left >= 0 && (size_t)ctx->left <= size)
28
29 /*
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.
32  */
33 #undef  ADVANCE
34 #define ADVANCE(num_bytes)      do {            \
35                 size_t num = num_bytes;         \
36                 ptr = ((const char *)ptr) + num;\
37                 size -= num;                    \
38                 if(ctx->left >= 0)              \
39                         ctx->left -= num;       \
40                 consumed_myself += num;         \
41         } while(0)
42
43 /*
44  * Switch to the next phase of parsing.
45  */
46 #undef  NEXT_PHASE
47 #undef  PHASE_OUT
48 #define NEXT_PHASE(ctx) do {                    \
49                 ctx->phase++;                   \
50                 ctx->step = 0;                  \
51         } while(0)
52 #define PHASE_OUT(ctx)  do { ctx->phase = 10; } while(0)
53
54 /*
55  * Return a standardized complex structure.
56  */
57 #undef  RETURN
58 #define RETURN(_code)   do {                    \
59                 rval.code = _code;              \
60                 rval.consumed = consumed_myself;\
61                 return rval;                    \
62         } while(0)
63
64 /*
65  * Check whether we are inside the extensions group.
66  */
67 #define IN_EXTENSION_GROUP(specs, memb_idx)     \
68         ( ((memb_idx) > (specs)->ext_after)     \
69         &&((memb_idx) < (specs)->ext_before))
70
71
72 /*
73  * Tags are canonically sorted in the tag2element map.
74  */
75 static int
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;
79
80         int a_class = BER_TAG_CLASS(a->el_tag);
81         int b_class = BER_TAG_CLASS(b->el_tag);
82
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);
86
87                 if(a_value == b_value) {
88                         if(a->el_no > b->el_no)
89                                 return 1;
90                         /*
91                          * Important: we do not check
92                          * for a->el_no <= b->el_no!
93                          */
94                         return 0;
95                 } else if(a_value < b_value)
96                         return -1;
97                 else
98                         return 1;
99         } else if(a_class < b_class) {
100                 return -1;
101         } else {
102                 return 1;
103         }
104 }
105
106
107 /*
108  * The decoder of the SEQUENCE type.
109  */
110 asn_dec_rval_t
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) {
113         /*
114          * Bring closer parts of structure description.
115          */
116         asn_SEQUENCE_specifics_t *specs = (asn_SEQUENCE_specifics_t *)td->specifics;
117         asn_TYPE_member_t *elements = td->elements;
118
119         /*
120          * Parts of the structure being constructed.
121          */
122         void *st = *struct_ptr; /* Target structure. */
123         asn_struct_ctx_t *ctx;  /* Decoder context */
124
125         ber_tlv_tag_t tlv_tag;  /* T from TLV */
126         asn_dec_rval_t rval;    /* Return code from subparsers */
127
128         ssize_t consumed_myself = 0;    /* Consumed bytes from ptr */
129         int edx;                        /* SEQUENCE element's index */
130
131         ASN_DEBUG("Decoding %s as SEQUENCE", td->name);
132         
133         /*
134          * Create the target structure if it is not present already.
135          */
136         if(st == 0) {
137                 st = *struct_ptr = CALLOC(1, specs->struct_size);
138                 if(st == 0) {
139                         RETURN(RC_FAIL);
140                 }
141         }
142
143         /*
144          * Restore parsing context.
145          */
146         ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
147         
148         /*
149          * Start to parse where left previously
150          */
151         switch(ctx->phase) {
152         case 0:
153                 /*
154                  * PHASE 0.
155                  * Check that the set of tags associated with given structure
156                  * perfectly fits our expectations.
157                  */
158
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);
164                         return rval;
165                 }
166
167                 if(ctx->left >= 0)
168                         ctx->left += rval.consumed; /* ?Substracted below! */
169                 ADVANCE(rval.consumed);
170
171                 NEXT_PHASE(ctx);
172
173                 ASN_DEBUG("Structure consumes %ld bytes, buffer %ld",
174                         (long)ctx->left, (long)size);
175
176                 /* Fall through */
177         case 1:
178                 /*
179                  * PHASE 1.
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
185                  * that member:
186                  *      step = (<member_number> * 2 + <microphase>).
187                  */
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 */
194                 int use_bsearch;
195                 int n;
196
197                 if(ctx->step & 1)
198                         goto microphase2;
199
200                 /*
201                  * MICROPHASE 1: Synchronize decoding.
202                  */
203                 ASN_DEBUG("In %s SEQUENCE left %d, edx=%d flags=%d"
204                                 " opt=%d ec=%d",
205                         td->name, (int)ctx->left, edx,
206                         elements[edx].flags, elements[edx].optional,
207                         td->elements_count);
208
209                 if(ctx->left == 0       /* No more stuff is expected */
210                 && (
211                         /* Explicit OPTIONAL specification reaches the end */
212                         (edx + elements[edx].optional
213                                         == td->elements_count)
214                         ||
215                         /* All extensions are optional */
216                         (IN_EXTENSION_GROUP(specs, edx)
217                                 && specs->ext_before > td->elements_count)
218                    )
219                 ) {
220                         ASN_DEBUG("End of SEQUENCE %s", td->name);
221                         /*
222                          * Found the legitimate end of the structure.
223                          */
224                         PHASE_OUT(ctx);
225                         RETURN(RC_OK);
226                 }
227
228                 /*
229                  * Fetch the T from TLV.
230                  */
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);
236                 switch(tag_len) {
237                 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
238                         /* Fall through */
239                 case -1: RETURN(RC_FAIL);
240                 }
241
242                 if(ctx->left < 0 && ((const uint8_t *)ptr)[0] == 0) {
243                         if(LEFT < 2) {
244                                 if(SIZE_VIOLATION)
245                                         RETURN(RC_FAIL);
246                                 else
247                                         RETURN(RC_WMORE);
248                         } else if(((const uint8_t *)ptr)[1] == 0) {
249                         ASN_DEBUG("edx = %d, opt = %d, ec=%d",
250                                 edx, elements[edx].optional,
251                                 td->elements_count);
252                                 if((edx + elements[edx].optional
253                                         == td->elements_count)
254                                 || (IN_EXTENSION_GROUP(specs, edx)
255                                         && specs->ext_before
256                                                 > td->elements_count)) {
257                                         /*
258                                          * Yeah, baby! Found the terminator
259                                          * of the indefinite length structure.
260                                          */
261                                         /*
262                                          * Proceed to the canonical
263                                          * finalization function.
264                                          * No advancing is necessary.
265                                          */
266                                         goto phase3;
267                                 }
268                         }
269                 }
270
271                 /*
272                  * Find the next available type with this tag.
273                  */
274                 use_bsearch = 0;
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;
281                         use_bsearch = 1;
282                         /* ... and resort to bsearch() */
283                 }
284                 for(n = edx; n < opt_edx_end; n++) {
285                         if(BER_TAGS_EQUAL(tlv_tag, elements[n].tag)) {
286                                 /*
287                                  * Found element corresponding to the tag
288                                  * being looked at.
289                                  * Reposition over the right element.
290                                  */
291                                 edx = n;
292                                 ctx->step = 1 + 2 * edx;        /* Remember! */
293                                 goto microphase2;
294                         } else if(elements[n].flags & ATF_OPEN_TYPE) {
295                                 /*
296                                  * This is the ANY type, which may bear
297                                  * any flag whatsoever.
298                                  */
299                                 edx = n;
300                                 ctx->step = 1 + 2 * edx;        /* Remember! */
301                                 goto microphase2;
302                         } else if(elements[n].tag == (ber_tlv_tag_t)-1) {
303                                 use_bsearch = 1;
304                                 break;
305                         }
306                 }
307                 if(use_bsearch) {
308                         /*
309                          * Resort to a binary search over
310                          * sorted array of tags.
311                          */
312                         asn_TYPE_tag2member_t *t2m;
313                         asn_TYPE_tag2member_t key;
314                         key.el_tag = tlv_tag;
315                         key.el_no = edx;
316                         t2m = (asn_TYPE_tag2member_t *)bsearch(&key,
317                                 specs->tag2el, specs->tag2el_count,
318                                 sizeof(specs->tag2el[0]), _t2e_cmp);
319                         if(t2m) {
320                                 asn_TYPE_tag2member_t *best = 0;
321                                 asn_TYPE_tag2member_t *t2m_f, *t2m_l;
322                                 int edx_max = edx + elements[edx].optional;
323                                 /*
324                                  * Rewind to the first element with that tag,
325                                  * `cause bsearch() does not guarantee order.
326                                  */
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;
332                                         best = t2m;
333                                 }
334                                 if(best) {
335                                         edx = best->el_no;
336                                         ctx->step = 1 + 2 * edx;
337                                         goto microphase2;
338                                 }
339                         }
340                         n = opt_edx_end;
341                 }
342                 if(n == opt_edx_end) {
343                         /*
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.
348                          */
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),
354                                         elements[edx].name,
355                                         elements[edx].optional
356                                                 ?" or alternatives":"");
357                                 RETURN(RC_FAIL);
358                         } else {
359                                 /* Skip this tag */
360                                 ssize_t skip;
361
362                                 skip = ber_skip_length(opt_codec_ctx,
363                                         BER_TLV_CONSTRUCTED(ptr),
364                                         (const char *)ptr + tag_len,
365                                         LEFT - tag_len);
366                                 ASN_DEBUG("Skip length %d in %s",
367                                         (int)skip, td->name);
368                                 switch(skip) {
369                                 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
370                                         /* Fall through */
371                                 case -1: RETURN(RC_FAIL);
372                                 }
373
374                                 ADVANCE(skip + tag_len);
375                                 ctx->step -= 2;
376                                 edx--;
377                                 continue;  /* Try again with the next tag */
378                         }
379                 }
380
381                 /*
382                  * MICROPHASE 2: Invoke the member-specific decoder.
383                  */
384                 ctx->step |= 1;         /* Confirm entering next microphase */
385         microphase2:
386                 ASN_DEBUG("Inside SEQUENCE %s MF2", td->name);
387                 
388                 /*
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).
392                  */
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);
396                 } else {
397                         /*
398                          * A pointer to a pointer
399                          * holding the start of the structure
400                          */
401                         memb_ptr = (char *)st + elements[edx].memb_offset;
402                         memb_ptr2 = &memb_ptr;
403                 }
404                 /*
405                  * Invoke the member fetch routine according to member's type
406                  */
407                 rval = elements[edx].type->ber_decoder(opt_codec_ctx,
408                                 elements[edx].type,
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);
415                 switch(rval.code) {
416                 case RC_OK:
417                         break;
418                 case RC_WMORE: /* More data expected */
419                         if(!SIZE_VIOLATION) {
420                                 ADVANCE(rval.consumed);
421                                 RETURN(RC_WMORE);
422                         }
423                         ASN_DEBUG("Size violation (c->l=%ld <= s=%ld)",
424                                 (long)ctx->left, (long)size);
425                         /* Fall through */
426                 case RC_FAIL: /* Fatal error */
427                         RETURN(RC_FAIL);
428                 } /* switch(rval) */
429                 
430                 ADVANCE(rval.consumed);
431           }     /* for(all structure members) */
432
433         phase3:
434                 ctx->phase = 3;
435         case 3: /* 00 and other tags expected */
436         case 4: /* only 00's expected */
437
438                 ASN_DEBUG("SEQUENCE %s Leftover: %ld, size = %ld",
439                         td->name, (long)ctx->left, (long)size);
440
441                 /*
442                  * Skip everything until the end of the SEQUENCE.
443                  */
444                 while(ctx->left) {
445                         ssize_t tl, ll;
446
447                         tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
448                         switch(tl) {
449                         case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
450                                 /* Fall through */
451                         case -1: RETURN(RC_FAIL);
452                         }
453
454                         /*
455                          * If expected <0><0>...
456                          */
457                         if(ctx->left < 0
458                                 && ((const uint8_t *)ptr)[0] == 0) {
459                                 if(LEFT < 2) {
460                                         if(SIZE_VIOLATION)
461                                                 RETURN(RC_FAIL);
462                                         else
463                                                 RETURN(RC_WMORE);
464                                 } else if(((const uint8_t *)ptr)[1] == 0) {
465                                         /*
466                                          * Correctly finished with <0><0>.
467                                          */
468                                         ADVANCE(2);
469                                         ctx->left++;
470                                         ctx->phase = 4;
471                                         continue;
472                                 }
473                         }
474
475                         if(!IN_EXTENSION_GROUP(specs, td->elements_count)
476                         || ctx->phase == 4) {
477                                 ASN_DEBUG("Unexpected continuation "
478                                         "of a non-extensible type "
479                                         "%s (SEQUENCE): %s",
480                                         td->name,
481                                         ber_tlv_tag_string(tlv_tag));
482                                 RETURN(RC_FAIL);
483                         }
484
485                         ll = ber_skip_length(opt_codec_ctx,
486                                 BER_TLV_CONSTRUCTED(ptr),
487                                 (const char *)ptr + tl, LEFT - tl);
488                         switch(ll) {
489                         case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
490                                 /* Fall through */
491                         case -1: RETURN(RC_FAIL);
492                         }
493
494                         ADVANCE(tl + ll);
495                 }
496
497                 PHASE_OUT(ctx);
498         }
499         
500         RETURN(RC_OK);
501 }
502
503
504 /*
505  * The DER encoder of the SEQUENCE type.
506  */
507 asn_enc_rval_t
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;
513         ssize_t ret;
514         int edx;
515
516         ASN_DEBUG("%s %s as SEQUENCE",
517                 cb?"Encoding":"Estimating", td->name);
518
519         /*
520          * Gather the length of the underlying members sequence.
521          */
522         for(edx = 0; edx < td->elements_count; edx++) {
523                 asn_TYPE_member_t *elm = &td->elements[edx];
524                 void *memb_ptr;
525                 if(elm->flags & ATF_POINTER) {
526                         memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
527                         if(!memb_ptr) {
528                                 if(elm->optional) continue;
529                                 /* Mandatory element is missing */
530                                 _ASN_ENCODE_FAILED;
531                         }
532                 } else {
533                         memb_ptr = (void *)((char *)sptr + elm->memb_offset);
534                 }
535                 erval = elm->type->der_encoder(elm->type, memb_ptr,
536                         elm->tag_mode, elm->tag,
537                         0, 0);
538                 if(erval.encoded == -1)
539                         return erval;
540                 computed_size += erval.encoded;
541                 ASN_DEBUG("Member %d %s estimated %ld bytes",
542                         edx, elm->name, (long)erval.encoded);
543         }
544
545         /*
546          * Encode the TLV for the sequence itself.
547          */
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);
550         if(ret == -1)
551                 _ASN_ENCODE_FAILED;
552         erval.encoded = computed_size + ret;
553
554         if(!cb) _ASN_ENCODED_OK(erval);
555
556         /*
557          * Encode all members.
558          */
559         for(edx = 0; edx < td->elements_count; edx++) {
560                 asn_TYPE_member_t *elm = &td->elements[edx];
561                 asn_enc_rval_t tmperval;
562                 void *memb_ptr;
563
564                 if(elm->flags & ATF_POINTER) {
565                         memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
566                         if(!memb_ptr) continue;
567                 } else {
568                         memb_ptr = (void *)((char *)sptr + elm->memb_offset);
569                 }
570                 tmperval = elm->type->der_encoder(elm->type, memb_ptr,
571                         elm->tag_mode, elm->tag,
572                         cb, app_key);
573                 if(tmperval.encoded == -1)
574                         return tmperval;
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);
578         }
579
580         if(computed_size != 0)
581                 /*
582                  * Encoded size is not equal to the computed size.
583                  */
584                 _ASN_ENCODE_FAILED;
585
586         _ASN_ENCODED_OK(erval);
587 }
588
589
590 #undef  XER_ADVANCE
591 #define XER_ADVANCE(num_bytes)  do {                    \
592                 size_t num = num_bytes;                 \
593                 buf_ptr = ((const char *)buf_ptr) + num;\
594                 size -= num;                            \
595                 consumed_myself += num;                 \
596         } while(0)
597
598 /*
599  * Decode the XER (XML) data.
600  */
601 asn_dec_rval_t
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) {
605         /*
606          * Bring closer parts of structure description.
607          */
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;
612
613         /*
614          * ... and parts of the structure being constructed.
615          */
616         void *st = *struct_ptr; /* Target structure. */
617         asn_struct_ctx_t *ctx;  /* Decoder context */
618
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 */
622         int edx_end;
623
624         /*
625          * Create the target structure if it is not present already.
626          */
627         if(st == 0) {
628                 st = *struct_ptr = CALLOC(1, specs->struct_size);
629                 if(st == 0) RETURN(RC_FAIL);
630         }
631
632         /*
633          * Restore parsing context.
634          */
635         ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
636
637
638         /*
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
645          */
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;
651                 int n;
652
653                 /*
654                  * Go inside the inner member of a sequence.
655                  */
656                 if(ctx->phase == 2) {
657                         asn_dec_rval_t tmprval;
658                         void *memb_ptr;         /* Pointer to the member */
659                         void **memb_ptr2;       /* Pointer to that pointer */
660
661                         elm = &td->elements[edx];
662
663                         if(elm->flags & ATF_POINTER) {
664                                 /* Member is a pointer to another structure */
665                                 memb_ptr2 = (void **)((char *)st
666                                         + elm->memb_offset);
667                         } else {
668                                 memb_ptr = (char *)st + elm->memb_offset;
669                                 memb_ptr2 = &memb_ptr;
670                         }
671
672                         /* Invoke the inner type decoder, m.b. multiple times */
673                         tmprval = elm->type->xer_decoder(opt_codec_ctx,
674                                         elm->type, memb_ptr2, elm->name,
675                                         buf_ptr, size);
676                         XER_ADVANCE(tmprval.consumed);
677                         if(tmprval.code != RC_OK)
678                                 RETURN(tmprval.code);
679                         ctx->phase = 1; /* Back to body processing */
680                         ctx->step = ++edx;
681                         ASN_DEBUG("XER/SEQUENCE phase => %d, step => %d",
682                                 ctx->phase, ctx->step);
683                         /* Fall through */
684                 }
685
686                 /*
687                  * Get the next part of the XML stream.
688                  */
689                 ch_size = xer_next_token(&ctx->context, buf_ptr, size,
690                         &ch_type);
691                 switch(ch_size) {
692                 case -1: RETURN(RC_FAIL);
693                 case 0:  RETURN(RC_WMORE);
694                 default:
695                         switch(ch_type) {
696                         case PXER_COMMENT:      /* Got XML comment */
697                         case PXER_TEXT:         /* Ignore free-standing text */
698                                 XER_ADVANCE(ch_size);   /* Skip silently */
699                                 continue;
700                         case PXER_TAG:
701                                 break;  /* Check the rest down there */
702                         }
703                 }
704
705                 tcv = xer_check_tag(buf_ptr, ch_size, xml_tag);
706                 ASN_DEBUG("XER/SEQUENCE: tcv = %d, ph=%d [%s]",
707                         tcv, ctx->phase, xml_tag);
708
709                 /* Skip the extensions section */
710                 if(ctx->phase == 3) {
711                         switch(xer_skip_unknown(tcv, &ctx->left)) {
712                         case -1:
713                                 ctx->phase = 4;
714                                 RETURN(RC_FAIL);
715                         case 0:
716                                 XER_ADVANCE(ch_size);
717                                 continue;
718                         case 1:
719                                 XER_ADVANCE(ch_size);
720                                 ctx->phase = 1;
721                                 continue;
722                         case 2:
723                                 ctx->phase = 1;
724                                 break;
725                         }
726                 }
727
728                 switch(tcv) {
729                 case XCT_CLOSING:
730                         if(ctx->phase == 0) break;
731                         ctx->phase = 0;
732                         /* Fall through */
733                 case XCT_BOTH:
734                         if(ctx->phase == 0) {
735                                 if(edx >= td->elements_count
736                                    ||
737                                    /* Explicit OPTIONAL specs reaches the end */
738                                     (edx + elements[edx].optional
739                                         == td->elements_count)
740                                    ||
741                                    /* All extensions are optional */
742                                    (IN_EXTENSION_GROUP(specs, edx)
743                                         && specs->ext_before
744                                                 > td->elements_count)
745                                 ) {
746                                         XER_ADVANCE(ch_size);
747                                         ctx->phase = 4; /* Phase out */
748                                         RETURN(RC_OK);
749                                 } else {
750                                         ASN_DEBUG("Premature end of XER SEQUENCE");
751                                         RETURN(RC_FAIL);
752                                 }
753                         }
754                         /* Fall through */
755                 case XCT_OPENING:
756                         if(ctx->phase == 0) {
757                                 XER_ADVANCE(ch_size);
758                                 ctx->phase = 1; /* Processing body phase */
759                                 continue;
760                         }
761                         /* Fall through */
762                 case XCT_UNKNOWN_OP:
763                 case XCT_UNKNOWN_BO:
764
765                         ASN_DEBUG("XER/SEQUENCE: tcv=%d, ph=%d, edx=%d",
766                                 tcv, ctx->phase, edx);
767                         if(ctx->phase != 1) {
768                                 break;  /* Really unexpected */
769                         }
770
771                         if(edx < td->elements_count) {
772                                 /*
773                                  * Search which member corresponds to this tag.
774                                  */
775                                 edx_end = edx + elements[edx].optional + 1;
776                                 if(edx_end > td->elements_count)
777                                         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,
781                                                 ch_size, elm->name);
782                                         switch(tcv) {
783                                         case XCT_BOTH:
784                                         case XCT_OPENING:
785                                                 /*
786                                                  * Process this member.
787                                                  */
788                                                 ctx->step = edx = n;
789                                                 ctx->phase = 2;
790                                                 break;
791                                         case XCT_UNKNOWN_OP:
792                                         case XCT_UNKNOWN_BO:
793                                                 continue;
794                                         default:
795                                                 n = edx_end;
796                                                 break;  /* Phase out */
797                                         }
798                                         break;
799                                 }
800                                 if(n != edx_end)
801                                         continue;
802                         } else {
803                                 ASN_DEBUG("Out of defined members: %d/%d",
804                                         edx, td->elements_count);
805                         }
806
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",
812                                         edx);
813                                 /*
814                                  * Check for (XCT_BOTH or XCT_UNKNOWN_BO)
815                                  * By using a mask. Only record a pure
816                                  * <opening> tags.
817                                  */
818                                 if(tcv & XCT_CLOSING) {
819                                         /* Found </extension> without body */
820                                 } else {
821                                         ctx->left = 1;
822                                         ctx->phase = 3; /* Skip ...'s */
823                                 }
824                                 XER_ADVANCE(ch_size);
825                                 continue;
826                         }
827
828                         /* Fall through */
829                 default:
830                         break;
831                 }
832
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]:'.');
840                 break;
841         }
842
843         ctx->phase = 4; /* "Phase out" on hard failure */
844         RETURN(RC_FAIL);
845 }
846
847 asn_enc_rval_t
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) {
851         asn_enc_rval_t er;
852         int xcan = (flags & XER_F_CANONICAL);
853         int edx;
854
855         if(!sptr)
856                 _ASN_ENCODE_FAILED;
857
858         er.encoded = 0;
859
860         for(edx = 0; edx < td->elements_count; edx++) {
861                 asn_enc_rval_t tmper;
862                 asn_TYPE_member_t *elm = &td->elements[edx];
863                 void *memb_ptr;
864                 const char *mname = elm->name;
865                 unsigned int mlen = strlen(mname);
866
867                 if(elm->flags & ATF_POINTER) {
868                         memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
869                         if(!memb_ptr) {
870                                 if(elm->optional)
871                                         continue;
872                                 /* Mandatory element is missing */
873                                 _ASN_ENCODE_FAILED;
874                         }
875                 } else {
876                         memb_ptr = (void *)((char *)sptr + elm->memb_offset);
877                 }
878
879                 if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel);
880                 _ASN_CALLBACK3("<", 1, mname, mlen, ">", 1);
881
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;
886
887                 _ASN_CALLBACK3("</", 2, mname, mlen, ">", 1);
888                 er.encoded += 5 + (2 * mlen) + tmper.encoded;
889         }
890
891         if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel - 1);
892
893         _ASN_ENCODED_OK(er);
894 cb_failed:
895         _ASN_ENCODE_FAILED;
896 }
897
898 int
899 SEQUENCE_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
900                 asn_app_consume_bytes_f *cb, void *app_key) {
901         int edx;
902         int ret;
903
904         if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
905
906         /* Dump preamble */
907         if(cb(td->name, strlen(td->name), app_key) < 0
908         || cb(" ::= {", 6, app_key) < 0)
909                 return -1;
910
911         for(edx = 0; edx < td->elements_count; edx++) {
912                 asn_TYPE_member_t *elm = &td->elements[edx];
913                 const void *memb_ptr;
914
915                 if(elm->flags & ATF_POINTER) {
916                         memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
917                         if(!memb_ptr) {
918                                 if(elm->optional) continue;
919                                 /* Print <absent> line */
920                                 /* Fall through */
921                         }
922                 } else {
923                         memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
924                 }
925
926                 /* Indentation */
927                 _i_INDENT(1);
928
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)
932                         return -1;
933
934                 /* Print the member itself */
935                 ret = elm->type->print_struct(elm->type, memb_ptr, ilevel + 1,
936                         cb, app_key);
937                 if(ret) return ret;
938         }
939
940         ilevel--;
941         _i_INDENT(1);
942
943         return (cb("}", 1, app_key) < 0) ? -1 : 0;
944 }
945
946 void
947 SEQUENCE_free(asn_TYPE_descriptor_t *td, void *sptr, int contents_only) {
948         int edx;
949
950         if(!td || !sptr)
951                 return;
952
953         ASN_DEBUG("Freeing %s as SEQUENCE", td->name);
954
955         for(edx = 0; edx < td->elements_count; edx++) {
956                 asn_TYPE_member_t *elm = &td->elements[edx];
957                 void *memb_ptr;
958                 if(elm->flags & ATF_POINTER) {
959                         memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
960                         if(memb_ptr)
961                                 ASN_STRUCT_FREE(*elm->type, memb_ptr);
962                 } else {
963                         memb_ptr = (void *)((char *)sptr + elm->memb_offset);
964                         ASN_STRUCT_FREE_CONTENTS_ONLY(*elm->type, memb_ptr);
965                 }
966         }
967
968         if(!contents_only) {
969                 FREEMEM(sptr);
970         }
971 }
972
973 int
974 SEQUENCE_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
975                 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
976         int edx;
977
978         if(!sptr) {
979                 _ASN_CTFAIL(app_key, td,
980                         "%s: value not given (%s:%d)",
981                         td->name, __FILE__, __LINE__);
982                 return -1;
983         }
984
985         /*
986          * Iterate over structure members and check their validity.
987          */
988         for(edx = 0; edx < td->elements_count; edx++) {
989                 asn_TYPE_member_t *elm = &td->elements[edx];
990                 const void *memb_ptr;
991
992                 if(elm->flags & ATF_POINTER) {
993                         memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
994                         if(!memb_ptr) {
995                                 if(elm->optional)
996                                         continue;
997                                 _ASN_CTFAIL(app_key, td,
998                                 "%s: mandatory element %s absent (%s:%d)",
999                                 td->name, elm->name, __FILE__, __LINE__);
1000                                 return -1;
1001                         }
1002                 } else {
1003                         memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
1004                 }
1005
1006                 if(elm->memb_constraints) {
1007                         int ret = elm->memb_constraints(elm->type, memb_ptr,
1008                                 ctfailcb, app_key);
1009                         if(ret) return ret;
1010                 } else {
1011                         int ret = elm->type->check_constraints(elm->type,
1012                                 memb_ptr, ctfailcb, app_key);
1013                         if(ret) return ret;
1014                         /*
1015                          * Cannot inherit it earlier:
1016                          * need to make sure we get the updated version.
1017                          */
1018                         elm->memb_constraints = elm->type->check_constraints;
1019                 }
1020         }
1021
1022         return 0;
1023 }
1024
1025 asn_dec_rval_t
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;
1033         asn_dec_rval_t rv;
1034         int edx;
1035
1036         (void)constraints;
1037
1038         if(_ASN_STACK_OVERFLOW_CHECK(opt_codec_ctx))
1039                 _ASN_DECODE_FAILED;
1040
1041         if(!st) {
1042                 st = *sptr = CALLOC(1, specs->struct_size);
1043                 if(!st) _ASN_DECODE_FAILED;
1044         }
1045
1046         ASN_DEBUG("Decoding %s as SEQUENCE (UPER)", td->name);
1047
1048         /* Handle extensions */
1049         if(specs->ext_before >= 0) {
1050                 extpresent = per_get_few_bits(pd, 1);
1051                 if(extpresent < 0) _ASN_DECODE_STARVED;
1052         }
1053
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)) {
1060                         FREEMEM(opres);
1061                         _ASN_DECODE_STARVED;
1062                 }
1063                 opmd.buffer = opres;
1064                 opmd.nboff = 0;
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);
1068         } else {
1069                 opres = 0;
1070                 memset(&opmd, 0, sizeof opmd);
1071         }
1072
1073         /*
1074          * Get the sequence ROOT elements.
1075          */
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 */
1081
1082                 /* Fetch the pointer to this member */
1083                 if(elm->flags & ATF_POINTER) {
1084                         memb_ptr2 = (void **)((char *)st + elm->memb_offset);
1085                 } else {
1086                         memb_ptr = (char *)st + elm->memb_offset;
1087                         memb_ptr2 = &memb_ptr;
1088                 }
1089
1090                 /* Deal with optionality */
1091                 if(elm->optional) {
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);
1096                         if(present == 0) {
1097                                 /* This element is not present */
1098                                 if(elm->default_value) {
1099                                         /* Fill-in DEFAULT */
1100                                         if(elm->default_value(1, memb_ptr2)) {
1101                                                 FREEMEM(opres);
1102                                                 _ASN_DECODE_FAILED;
1103                                         }
1104                                 }
1105                                 /* The member is just not present */
1106                                 continue;
1107                         }
1108                         /* Fall through */
1109                 }
1110
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);
1118                         FREEMEM(opres);
1119                         return rv;
1120                 }
1121         }
1122
1123         /*
1124          * Deal with extensions.
1125          */
1126         if(extpresent) {
1127                 ASN_DEBUG("Extensibility for %s: NOT IMPLEMENTED", td->name);
1128                 _ASN_DECODE_FAILED;
1129         } else {
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 */
1135
1136                         if(!elm->default_value) continue;
1137
1138                         /* Fetch the pointer to this member */
1139                         if(elm->flags & ATF_POINTER) {
1140                                 memb_ptr2 = (void **)((char *)st
1141                                                 + elm->memb_offset);
1142                         } else {
1143                                 memb_ptr = (char *)st + elm->memb_offset;
1144                                 memb_ptr2 = &memb_ptr;
1145                         }
1146
1147                         /* Set default value */
1148                         if(elm->default_value(1, memb_ptr2)) {
1149                                 FREEMEM(opres);
1150                                 _ASN_DECODE_FAILED;
1151                         }
1152                 }
1153         }
1154
1155         rv.consumed = 0;
1156         rv.code = RC_OK;
1157         FREEMEM(opres);
1158         return rv;
1159 }
1160
1161 asn_enc_rval_t
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;
1166         asn_enc_rval_t er;
1167         int edx;
1168         int i;
1169
1170         (void)constraints;
1171
1172         if(!sptr)
1173                 _ASN_ENCODE_FAILED;
1174
1175         er.encoded = 0;
1176
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 */
1180
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 */
1186                 int present;
1187
1188                 edx = specs->oms[i];
1189                 elm = &td->elements[edx];
1190
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);
1195                 } else {
1196                         memb_ptr = (void *)((char *)sptr + elm->memb_offset);
1197                         memb_ptr2 = &memb_ptr;
1198                         present = 1;
1199                 }
1200
1201                 /* Eliminate default values */
1202                 if(present && elm->default_value
1203                 && elm->default_value(0, memb_ptr2) == 1)
1204                         present = 0;
1205
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))
1211                         _ASN_ENCODE_FAILED;
1212         }
1213
1214         /*
1215          * Get the sequence ROOT elements.
1216          */
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 */
1222
1223                 /* Fetch the pointer to this member */
1224                 if(elm->flags & ATF_POINTER) {
1225                         memb_ptr2 = (void **)((char *)sptr + elm->memb_offset);
1226                         if(!*memb_ptr2) {
1227                                 ASN_DEBUG("Element %s %d not present",
1228                                         elm->name, edx);
1229                                 if(elm->optional)
1230                                         continue;
1231                                 /* Mandatory element is missing */
1232                                 _ASN_ENCODE_FAILED;
1233                         }
1234                 } else {
1235                         memb_ptr = (void *)((char *)sptr + elm->memb_offset);
1236                         memb_ptr2 = &memb_ptr;
1237                 }
1238
1239                 /* Eliminate default values */
1240                 if(elm->default_value && elm->default_value(0, memb_ptr2) == 1)
1241                         continue;
1242
1243                 er = elm->type->uper_encoder(elm->type, elm->per_constraints,
1244                         *memb_ptr2, po);
1245                 if(er.encoded == -1)
1246                         return er;
1247         }
1248
1249         _ASN_ENCODED_OK(er);
1250 }
1251