blob: 15c12e21a2a5879342e398162737fd0b3a657af6 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
Lev Walkin62258e22007-06-23 23:50:25 +00002 * Copyright (c) 2003, 2004, 2005, 2006, 2007 Lev Walkin <vlm@lionet.info>.
Lev Walkinc61f3862005-02-14 17:21:22 +00003 * All rights reserved.
Lev Walkinf15320b2004-06-03 03:38:44 +00004 * Redistribution and modifications are permitted subject to BSD license.
5 */
Lev Walkina9cc46e2004-09-22 16:06:28 +00006#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00007#include <constr_SEQUENCE.h>
Lev Walkin9218bc12007-06-27 04:09:37 +00008#include <per_opentype.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00009
10/*
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.
14 */
Lev Walkinec1ffd42004-08-18 04:53:32 +000015#define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left)
Lev Walkinf15320b2004-06-03 03:38:44 +000016
17/*
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
21 * reading more data.
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.
27 */
Lev Walkind9bd7752004-06-05 08:17:50 +000028#define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size)
Lev Walkinf15320b2004-06-03 03:38:44 +000029
30/*
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.
33 */
Lev Walkincc6a9102004-09-23 22:06:26 +000034#undef ADVANCE
Lev Walkinf15320b2004-06-03 03:38:44 +000035#define ADVANCE(num_bytes) do { \
36 size_t num = num_bytes; \
Lev Walkinaa61a0f2014-01-13 23:08:47 -080037 ptr = ((const char *)ptr) + num; \
Lev Walkinf15320b2004-06-03 03:38:44 +000038 size -= num; \
39 if(ctx->left >= 0) \
40 ctx->left -= num; \
41 consumed_myself += num; \
42 } while(0)
43
44/*
45 * Switch to the next phase of parsing.
46 */
Lev Walkincc6a9102004-09-23 22:06:26 +000047#undef NEXT_PHASE
48#undef PHASE_OUT
Lev Walkinf15320b2004-06-03 03:38:44 +000049#define NEXT_PHASE(ctx) do { \
50 ctx->phase++; \
51 ctx->step = 0; \
52 } while(0)
53#define PHASE_OUT(ctx) do { ctx->phase = 10; } while(0)
54
55/*
56 * Return a standardized complex structure.
57 */
Lev Walkincc6a9102004-09-23 22:06:26 +000058#undef RETURN
Lev Walkinf15320b2004-06-03 03:38:44 +000059#define RETURN(_code) do { \
60 rval.code = _code; \
61 rval.consumed = consumed_myself;\
62 return rval; \
63 } while(0)
64
65/*
66 * Check whether we are inside the extensions group.
67 */
68#define IN_EXTENSION_GROUP(specs, memb_idx) \
Lev Walkin494fb702017-08-07 20:07:00 -070069 ( (((signed)(memb_idx)) > (specs)->ext_after) \
70 &&(((signed)(memb_idx)) < (specs)->ext_before))
Lev Walkinf15320b2004-06-03 03:38:44 +000071
Lev Walkin4c36e302004-06-06 07:20:02 +000072
73/*
74 * Tags are canonically sorted in the tag2element map.
75 */
76static int
77_t2e_cmp(const void *ap, const void *bp) {
Lev Walkin5e033762004-09-29 13:26:15 +000078 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;
Lev Walkinc2346572004-08-11 09:07:36 +000080
Lev Walkin4c36e302004-06-06 07:20:02 +000081 int a_class = BER_TAG_CLASS(a->el_tag);
82 int b_class = BER_TAG_CLASS(b->el_tag);
83
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);
87
Lev Walkin924fa032004-06-14 13:42:23 +000088 if(a_value == b_value) {
Lev Walkin50299c12004-06-14 14:11:14 +000089 if(a->el_no > b->el_no)
90 return 1;
Lev Walkin924fa032004-06-14 13:42:23 +000091 /*
92 * Important: we do not check
Lev Walkin50299c12004-06-14 14:11:14 +000093 * for a->el_no <= b->el_no!
Lev Walkin924fa032004-06-14 13:42:23 +000094 */
Lev Walkin4c36e302004-06-06 07:20:02 +000095 return 0;
Lev Walkin924fa032004-06-14 13:42:23 +000096 } else if(a_value < b_value)
Lev Walkin4c36e302004-06-06 07:20:02 +000097 return -1;
98 else
99 return 1;
100 } else if(a_class < b_class) {
101 return -1;
102 } else {
103 return 1;
104 }
105}
106
107
Lev Walkinf15320b2004-06-03 03:38:44 +0000108/*
109 * The decoder of the SEQUENCE type.
110 */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000111asn_dec_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000112SEQUENCE_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000113 void **struct_ptr, const void *ptr, size_t size, int tag_mode) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000114 /*
115 * Bring closer parts of structure description.
116 */
Lev Walkin5e033762004-09-29 13:26:15 +0000117 asn_SEQUENCE_specifics_t *specs = (asn_SEQUENCE_specifics_t *)td->specifics;
118 asn_TYPE_member_t *elements = td->elements;
Lev Walkinf15320b2004-06-03 03:38:44 +0000119
120 /*
121 * Parts of the structure being constructed.
122 */
123 void *st = *struct_ptr; /* Target structure. */
Lev Walkin5e033762004-09-29 13:26:15 +0000124 asn_struct_ctx_t *ctx; /* Decoder context */
Lev Walkinf15320b2004-06-03 03:38:44 +0000125
126 ber_tlv_tag_t tlv_tag; /* T from TLV */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000127 asn_dec_rval_t rval; /* Return code from subparsers */
Lev Walkinf15320b2004-06-03 03:38:44 +0000128
129 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
Lev Walkin494fb702017-08-07 20:07:00 -0700130 size_t edx; /* SEQUENCE element's index */
Lev Walkinf15320b2004-06-03 03:38:44 +0000131
Lev Walkin449f8322004-08-20 13:23:42 +0000132 ASN_DEBUG("Decoding %s as SEQUENCE", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000133
134 /*
135 * Create the target structure if it is not present already.
136 */
137 if(st == 0) {
138 st = *struct_ptr = CALLOC(1, specs->struct_size);
139 if(st == 0) {
140 RETURN(RC_FAIL);
141 }
142 }
143
144 /*
145 * Restore parsing context.
146 */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800147 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000148
149 /*
150 * Start to parse where left previously
151 */
152 switch(ctx->phase) {
153 case 0:
154 /*
155 * PHASE 0.
156 * Check that the set of tags associated with given structure
157 * perfectly fits our expectations.
158 */
159
Lev Walkin5e033762004-09-29 13:26:15 +0000160 rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size,
Lev Walkin8e8078a2004-09-26 13:10:40 +0000161 tag_mode, 1, &ctx->left, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000162 if(rval.code != RC_OK) {
163 ASN_DEBUG("%s tagging check failed: %d",
Lev Walkin449f8322004-08-20 13:23:42 +0000164 td->name, rval.code);
Lev Walkin566a5672004-10-05 06:36:57 +0000165 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000166 }
167
168 if(ctx->left >= 0)
169 ctx->left += rval.consumed; /* ?Substracted below! */
170 ADVANCE(rval.consumed);
171
172 NEXT_PHASE(ctx);
173
174 ASN_DEBUG("Structure consumes %ld bytes, buffer %ld",
175 (long)ctx->left, (long)size);
176
177 /* Fall through */
178 case 1:
179 /*
180 * PHASE 1.
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
186 * that member:
187 * step = (<member_number> * 2 + <microphase>).
188 */
Lev Walkin494fb702017-08-07 20:07:00 -0700189 for(edx = ((size_t)ctx->step >> 1); edx < td->elements_count;
Lev Walkinf15320b2004-06-03 03:38:44 +0000190 edx++, ctx->step = (ctx->step & ~1) + 2) {
191 void *memb_ptr; /* Pointer to the member */
Lev Walkinc2346572004-08-11 09:07:36 +0000192 void **memb_ptr2; /* Pointer to that pointer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000193 ssize_t tag_len; /* Length of TLV's T */
Lev Walkin494fb702017-08-07 20:07:00 -0700194 size_t opt_edx_end; /* Next non-optional element */
195 size_t n;
Lev Walkin4c36e302004-06-06 07:20:02 +0000196 int use_bsearch;
Lev Walkinf15320b2004-06-03 03:38:44 +0000197
198 if(ctx->step & 1)
199 goto microphase2;
200
201 /*
202 * MICROPHASE 1: Synchronize decoding.
203 */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000204 ASN_DEBUG("In %s SEQUENCE left %d, edx=%d flags=%d"
205 " opt=%d ec=%d",
206 td->name, (int)ctx->left, edx,
207 elements[edx].flags, elements[edx].optional,
208 td->elements_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000209
210 if(ctx->left == 0 /* No more stuff is expected */
211 && (
212 /* Explicit OPTIONAL specification reaches the end */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000213 (edx + elements[edx].optional
214 == td->elements_count)
Lev Walkinf15320b2004-06-03 03:38:44 +0000215 ||
216 /* All extensions are optional */
217 (IN_EXTENSION_GROUP(specs, edx)
Lev Walkin494fb702017-08-07 20:07:00 -0700218 && specs->ext_before > (signed)td->elements_count)
Lev Walkinf15320b2004-06-03 03:38:44 +0000219 )
220 ) {
Lev Walkin449f8322004-08-20 13:23:42 +0000221 ASN_DEBUG("End of SEQUENCE %s", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000222 /*
223 * Found the legitimate end of the structure.
224 */
225 PHASE_OUT(ctx);
226 RETURN(RC_OK);
227 }
228
229 /*
230 * Fetch the T from TLV.
231 */
232 tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
Lev Walkin906654e2004-09-10 15:49:15 +0000233 ASN_DEBUG("Current tag in %s SEQUENCE for element %d "
Lev Walkincc6a9102004-09-23 22:06:26 +0000234 "(%s) is %s encoded in %d bytes, of frame %ld",
Lev Walkin906654e2004-09-10 15:49:15 +0000235 td->name, edx, elements[edx].name,
236 ber_tlv_tag_string(tlv_tag), (int)tag_len, (long)LEFT);
Lev Walkinf15320b2004-06-03 03:38:44 +0000237 switch(tag_len) {
238 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
239 /* Fall through */
240 case -1: RETURN(RC_FAIL);
241 }
242
Lev Walkin8c3b8542005-03-10 18:52:02 +0000243 if(ctx->left < 0 && ((const uint8_t *)ptr)[0] == 0) {
Lev Walkin566a5672004-10-05 06:36:57 +0000244 if(LEFT < 2) {
245 if(SIZE_VIOLATION)
246 RETURN(RC_FAIL);
247 else
248 RETURN(RC_WMORE);
Lev Walkin8c3b8542005-03-10 18:52:02 +0000249 } else if(((const uint8_t *)ptr)[1] == 0) {
Lev Walkin494fb702017-08-07 20:07:00 -0700250 ASN_DEBUG("edx = %zu, opt = %d, ec=%d",
Lev Walkin566a5672004-10-05 06:36:57 +0000251 edx, elements[edx].optional,
252 td->elements_count);
253 if((edx + elements[edx].optional
254 == td->elements_count)
255 || (IN_EXTENSION_GROUP(specs, edx)
256 && specs->ext_before
Lev Walkin494fb702017-08-07 20:07:00 -0700257 > (signed)td->elements_count)) {
Lev Walkin566a5672004-10-05 06:36:57 +0000258 /*
259 * Yeah, baby! Found the terminator
260 * of the indefinite length structure.
261 */
262 /*
263 * Proceed to the canonical
264 * finalization function.
265 * No advancing is necessary.
266 */
267 goto phase3;
268 }
269 }
270 }
271
Lev Walkinf15320b2004-06-03 03:38:44 +0000272 /*
273 * Find the next available type with this tag.
274 */
Lev Walkin4c36e302004-06-06 07:20:02 +0000275 use_bsearch = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000276 opt_edx_end = edx + elements[edx].optional + 1;
Lev Walkin449f8322004-08-20 13:23:42 +0000277 if(opt_edx_end > td->elements_count)
278 opt_edx_end = td->elements_count; /* Cap */
Lev Walkinf9dbd9a2004-07-01 00:48:04 +0000279 else if(opt_edx_end - edx > 8) {
Lev Walkin924fa032004-06-14 13:42:23 +0000280 /* Limit the scope of linear search... */
Lev Walkinf9dbd9a2004-07-01 00:48:04 +0000281 opt_edx_end = edx + 8;
Lev Walkin4c36e302004-06-06 07:20:02 +0000282 use_bsearch = 1;
Lev Walkin924fa032004-06-14 13:42:23 +0000283 /* ... and resort to bsearch() */
Lev Walkin4c36e302004-06-06 07:20:02 +0000284 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000285 for(n = edx; n < opt_edx_end; n++) {
286 if(BER_TAGS_EQUAL(tlv_tag, elements[n].tag)) {
287 /*
288 * Found element corresponding to the tag
289 * being looked at.
290 * Reposition over the right element.
291 */
292 edx = n;
Lev Walkin4c36e302004-06-06 07:20:02 +0000293 ctx->step = 1 + 2 * edx; /* Remember! */
294 goto microphase2;
Lev Walkinb9189732004-09-10 09:37:12 +0000295 } else if(elements[n].flags & ATF_OPEN_TYPE) {
296 /*
297 * This is the ANY type, which may bear
298 * any flag whatsoever.
299 */
300 edx = n;
301 ctx->step = 1 + 2 * edx; /* Remember! */
302 goto microphase2;
Lev Walkin4c36e302004-06-06 07:20:02 +0000303 } else if(elements[n].tag == (ber_tlv_tag_t)-1) {
304 use_bsearch = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000305 break;
306 }
307 }
Lev Walkin4c36e302004-06-06 07:20:02 +0000308 if(use_bsearch) {
309 /*
Lev Walkin4e0704f2004-09-04 06:17:35 +0000310 * Resort to a binary search over
Lev Walkin4c36e302004-06-06 07:20:02 +0000311 * sorted array of tags.
312 */
Wim Lewisfb6344e2014-07-28 12:16:01 -0700313 const asn_TYPE_tag2member_t *t2m;
Lev Walkin5e033762004-09-29 13:26:15 +0000314 asn_TYPE_tag2member_t key;
Lev Walkin4c36e302004-06-06 07:20:02 +0000315 key.el_tag = tlv_tag;
Lev Walkin924fa032004-06-14 13:42:23 +0000316 key.el_no = edx;
Wim Lewisfb6344e2014-07-28 12:16:01 -0700317 t2m = (const asn_TYPE_tag2member_t *)bsearch(&key,
Lev Walkinc2346572004-08-11 09:07:36 +0000318 specs->tag2el, specs->tag2el_count,
Lev Walkin4c36e302004-06-06 07:20:02 +0000319 sizeof(specs->tag2el[0]), _t2e_cmp);
Lev Walkin924fa032004-06-14 13:42:23 +0000320 if(t2m) {
Wim Lewisfb6344e2014-07-28 12:16:01 -0700321 const asn_TYPE_tag2member_t *best = 0;
322 const asn_TYPE_tag2member_t *t2m_f, *t2m_l;
Lev Walkin494fb702017-08-07 20:07:00 -0700323 size_t edx_max = edx + elements[edx].optional;
Lev Walkin4c36e302004-06-06 07:20:02 +0000324 /*
325 * Rewind to the first element with that tag,
326 * `cause bsearch() does not guarantee order.
327 */
Lev Walkin924fa032004-06-14 13:42:23 +0000328 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;
333 best = t2m;
334 }
335 if(best) {
336 edx = best->el_no;
337 ctx->step = 1 + 2 * edx;
338 goto microphase2;
339 }
Lev Walkin4c36e302004-06-06 07:20:02 +0000340 }
341 n = opt_edx_end;
342 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000343 if(n == opt_edx_end) {
344 /*
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.
349 */
Lev Walkin7cbbc902006-10-03 06:39:36 +0000350 if(!IN_EXTENSION_GROUP(specs,
351 edx + elements[edx].optional)) {
Lev Walkin566a5672004-10-05 06:36:57 +0000352 ASN_DEBUG("Unexpected tag %s (at %d)",
353 ber_tlv_tag_string(tlv_tag), edx);
Lev Walkin4c36e302004-06-06 07:20:02 +0000354 ASN_DEBUG("Expected tag %s (%s)%s",
Lev Walkinf15320b2004-06-03 03:38:44 +0000355 ber_tlv_tag_string(elements[edx].tag),
Lev Walkin4c36e302004-06-06 07:20:02 +0000356 elements[edx].name,
Lev Walkinf15320b2004-06-03 03:38:44 +0000357 elements[edx].optional
358 ?" or alternatives":"");
359 RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000360 } else {
361 /* Skip this tag */
362 ssize_t skip;
Lev Walkin7cbbc902006-10-03 06:39:36 +0000363 edx += elements[edx].optional;
Lev Walkinf15320b2004-06-03 03:38:44 +0000364
Lev Walkin7cbbc902006-10-03 06:39:36 +0000365 ASN_DEBUG("Skipping unexpected %s (at %d)",
366 ber_tlv_tag_string(tlv_tag), edx);
Lev Walkinbaaa24f2004-09-29 14:19:14 +0000367 skip = ber_skip_length(opt_codec_ctx,
Lev Walkinf15320b2004-06-03 03:38:44 +0000368 BER_TLV_CONSTRUCTED(ptr),
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800369 (const char *)ptr + tag_len,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000370 LEFT - tag_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000371 ASN_DEBUG("Skip length %d in %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000372 (int)skip, td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000373 switch(skip) {
374 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
375 /* Fall through */
376 case -1: RETURN(RC_FAIL);
377 }
378
379 ADVANCE(skip + tag_len);
380 ctx->step -= 2;
381 edx--;
382 continue; /* Try again with the next tag */
383 }
384 }
385
386 /*
387 * MICROPHASE 2: Invoke the member-specific decoder.
388 */
389 ctx->step |= 1; /* Confirm entering next microphase */
390 microphase2:
Lev Walkin449f8322004-08-20 13:23:42 +0000391 ASN_DEBUG("Inside SEQUENCE %s MF2", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000392
393 /*
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).
397 */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000398 if(elements[edx].flags & ATF_POINTER) {
399 /* Member is a pointer to another structure */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800400 memb_ptr2 = (void **)((char *)st + elements[edx].memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000401 } else {
402 /*
403 * A pointer to a pointer
404 * holding the start of the structure
405 */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800406 memb_ptr = (char *)st + elements[edx].memb_offset;
Lev Walkinf15320b2004-06-03 03:38:44 +0000407 memb_ptr2 = &memb_ptr;
408 }
409 /*
410 * Invoke the member fetch routine according to member's type
411 */
Lev Walkin5e033762004-09-29 13:26:15 +0000412 rval = elements[edx].type->ber_decoder(opt_codec_ctx,
Lev Walkinc2346572004-08-11 09:07:36 +0000413 elements[edx].type,
Lev Walkinf15320b2004-06-03 03:38:44 +0000414 memb_ptr2, ptr, LEFT,
415 elements[edx].tag_mode);
Lev Walkin60b7cff2004-09-04 04:44:11 +0000416 ASN_DEBUG("In %s SEQUENCE decoded %d %s of %d "
Lev Walkin5beb7de2004-09-24 20:59:27 +0000417 "in %d bytes rval.code %d, size=%d",
Lev Walkin449f8322004-08-20 13:23:42 +0000418 td->name, edx, elements[edx].type->name,
Lev Walkin5beb7de2004-09-24 20:59:27 +0000419 (int)LEFT, (int)rval.consumed, rval.code, (int)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000420 switch(rval.code) {
421 case RC_OK:
422 break;
423 case RC_WMORE: /* More data expected */
424 if(!SIZE_VIOLATION) {
425 ADVANCE(rval.consumed);
426 RETURN(RC_WMORE);
427 }
Lev Walkin5beb7de2004-09-24 20:59:27 +0000428 ASN_DEBUG("Size violation (c->l=%ld <= s=%ld)",
429 (long)ctx->left, (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000430 /* Fall through */
431 case RC_FAIL: /* Fatal error */
432 RETURN(RC_FAIL);
433 } /* switch(rval) */
434
435 ADVANCE(rval.consumed);
436 } /* for(all structure members) */
437
438 phase3:
439 ctx->phase = 3;
440 case 3: /* 00 and other tags expected */
441 case 4: /* only 00's expected */
442
443 ASN_DEBUG("SEQUENCE %s Leftover: %ld, size = %ld",
Lev Walkin449f8322004-08-20 13:23:42 +0000444 td->name, (long)ctx->left, (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000445
446 /*
447 * Skip everything until the end of the SEQUENCE.
448 */
449 while(ctx->left) {
450 ssize_t tl, ll;
451
452 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
453 switch(tl) {
454 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
455 /* Fall through */
456 case -1: RETURN(RC_FAIL);
457 }
458
459 /*
460 * If expected <0><0>...
461 */
462 if(ctx->left < 0
Lev Walkin8c3b8542005-03-10 18:52:02 +0000463 && ((const uint8_t *)ptr)[0] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000464 if(LEFT < 2) {
465 if(SIZE_VIOLATION)
466 RETURN(RC_FAIL);
467 else
468 RETURN(RC_WMORE);
Lev Walkin8c3b8542005-03-10 18:52:02 +0000469 } else if(((const uint8_t *)ptr)[1] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000470 /*
471 * Correctly finished with <0><0>.
472 */
473 ADVANCE(2);
474 ctx->left++;
475 ctx->phase = 4;
476 continue;
477 }
478 }
479
Lev Walkin449f8322004-08-20 13:23:42 +0000480 if(!IN_EXTENSION_GROUP(specs, td->elements_count)
Lev Walkinf15320b2004-06-03 03:38:44 +0000481 || ctx->phase == 4) {
482 ASN_DEBUG("Unexpected continuation "
483 "of a non-extensible type "
484 "%s (SEQUENCE): %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000485 td->name,
Lev Walkinf15320b2004-06-03 03:38:44 +0000486 ber_tlv_tag_string(tlv_tag));
487 RETURN(RC_FAIL);
488 }
489
Lev Walkinbaaa24f2004-09-29 14:19:14 +0000490 ll = ber_skip_length(opt_codec_ctx,
Lev Walkinf15320b2004-06-03 03:38:44 +0000491 BER_TLV_CONSTRUCTED(ptr),
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800492 (const char *)ptr + tl, LEFT - tl);
Lev Walkinf15320b2004-06-03 03:38:44 +0000493 switch(ll) {
494 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
495 /* Fall through */
496 case -1: RETURN(RC_FAIL);
497 }
498
499 ADVANCE(tl + ll);
500 }
501
502 PHASE_OUT(ctx);
503 }
504
505 RETURN(RC_OK);
506}
507
Lev Walkin4c36e302004-06-06 07:20:02 +0000508
Lev Walkinf15320b2004-06-03 03:38:44 +0000509/*
510 * The DER encoder of the SEQUENCE type.
511 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000512asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000513SEQUENCE_encode_der(asn_TYPE_descriptor_t *td,
Lev Walkinac589332005-08-22 14:19:28 +0000514 void *sptr, int tag_mode, ber_tlv_tag_t tag,
Lev Walkinf15320b2004-06-03 03:38:44 +0000515 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000516 size_t computed_size = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000517 asn_enc_rval_t erval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000518 ssize_t ret;
Lev Walkin494fb702017-08-07 20:07:00 -0700519 size_t edx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000520
521 ASN_DEBUG("%s %s as SEQUENCE",
Lev Walkin449f8322004-08-20 13:23:42 +0000522 cb?"Encoding":"Estimating", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000523
524 /*
525 * Gather the length of the underlying members sequence.
526 */
Lev Walkin449f8322004-08-20 13:23:42 +0000527 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000528 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000529 void *memb_ptr;
Lev Walkincc93b0f2004-09-10 09:18:20 +0000530 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800531 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
Lev Walkinac589332005-08-22 14:19:28 +0000532 if(!memb_ptr) {
533 if(elm->optional) continue;
534 /* Mandatory element is missing */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700535 ASN__ENCODE_FAILED;
Lev Walkinac589332005-08-22 14:19:28 +0000536 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000537 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800538 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000539 }
540 erval = elm->type->der_encoder(elm->type, memb_ptr,
541 elm->tag_mode, elm->tag,
542 0, 0);
543 if(erval.encoded == -1)
544 return erval;
545 computed_size += erval.encoded;
546 ASN_DEBUG("Member %d %s estimated %ld bytes",
547 edx, elm->name, (long)erval.encoded);
548 }
549
550 /*
551 * Encode the TLV for the sequence itself.
552 */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000553 ret = der_write_tags(td, computed_size, tag_mode, 1, tag, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +0000554 ASN_DEBUG("Wrote tags: %ld (+%ld)", (long)ret, (long)computed_size);
Lev Walkinac589332005-08-22 14:19:28 +0000555 if(ret == -1)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700556 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000557 erval.encoded = computed_size + ret;
558
Lev Walkin7c1dc052016-03-14 03:08:15 -0700559 if(!cb) ASN__ENCODED_OK(erval);
Lev Walkinf15320b2004-06-03 03:38:44 +0000560
561 /*
562 * Encode all members.
563 */
Lev Walkin449f8322004-08-20 13:23:42 +0000564 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000565 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkina9cc46e2004-09-22 16:06:28 +0000566 asn_enc_rval_t tmperval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000567 void *memb_ptr;
568
Lev Walkincc93b0f2004-09-10 09:18:20 +0000569 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800570 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000571 if(!memb_ptr) continue;
572 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800573 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000574 }
575 tmperval = elm->type->der_encoder(elm->type, memb_ptr,
576 elm->tag_mode, elm->tag,
577 cb, app_key);
578 if(tmperval.encoded == -1)
579 return tmperval;
580 computed_size -= tmperval.encoded;
Lev Walkina6a926a2005-03-02 01:54:28 +0000581 ASN_DEBUG("Member %d %s of SEQUENCE %s encoded in %ld bytes",
582 edx, elm->name, td->name, (long)tmperval.encoded);
Lev Walkinf15320b2004-06-03 03:38:44 +0000583 }
584
Lev Walkinac589332005-08-22 14:19:28 +0000585 if(computed_size != 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000586 /*
587 * Encoded size is not equal to the computed size.
588 */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700589 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000590
Lev Walkin7c1dc052016-03-14 03:08:15 -0700591 ASN__ENCODED_OK(erval);
Lev Walkinf15320b2004-06-03 03:38:44 +0000592}
593
Lev Walkinc61f3862005-02-14 17:21:22 +0000594
Lev Walkin1bbc2002004-10-23 13:27:30 +0000595#undef XER_ADVANCE
596#define XER_ADVANCE(num_bytes) do { \
597 size_t num = num_bytes; \
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800598 buf_ptr = ((const char *)buf_ptr) + num;\
Lev Walkin1bbc2002004-10-23 13:27:30 +0000599 size -= num; \
600 consumed_myself += num; \
601 } while(0)
602
Lev Walkinc61f3862005-02-14 17:21:22 +0000603/*
604 * Decode the XER (XML) data.
605 */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000606asn_dec_rval_t
607SEQUENCE_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
608 void **struct_ptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000609 const void *buf_ptr, size_t size) {
Lev Walkin1bbc2002004-10-23 13:27:30 +0000610 /*
611 * Bring closer parts of structure description.
612 */
Lev Walkinc61f3862005-02-14 17:21:22 +0000613 asn_SEQUENCE_specifics_t *specs
614 = (asn_SEQUENCE_specifics_t *)td->specifics;
Lev Walkin1bbc2002004-10-23 13:27:30 +0000615 asn_TYPE_member_t *elements = td->elements;
616 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
617
618 /*
Lev Walkinc61f3862005-02-14 17:21:22 +0000619 * ... and parts of the structure being constructed.
Lev Walkin1bbc2002004-10-23 13:27:30 +0000620 */
621 void *st = *struct_ptr; /* Target structure. */
622 asn_struct_ctx_t *ctx; /* Decoder context */
623
Lev Walkinc61f3862005-02-14 17:21:22 +0000624 asn_dec_rval_t rval; /* Return value from a decoder */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000625 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
Lev Walkin494fb702017-08-07 20:07:00 -0700626 size_t edx; /* Element index */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000627
628 /*
629 * Create the target structure if it is not present already.
630 */
631 if(st == 0) {
632 st = *struct_ptr = CALLOC(1, specs->struct_size);
633 if(st == 0) RETURN(RC_FAIL);
634 }
635
636 /*
637 * Restore parsing context.
638 */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800639 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkin1bbc2002004-10-23 13:27:30 +0000640
641
642 /*
643 * Phases of XER/XML processing:
644 * Phase 0: Check that the opening tag matches our expectations.
645 * Phase 1: Processing body and reacting on closing tag.
646 * Phase 2: Processing inner type.
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000647 * Phase 3: Skipping unknown extensions.
648 * Phase 4: PHASED OUT
Lev Walkin1bbc2002004-10-23 13:27:30 +0000649 */
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000650 for(edx = ctx->step; ctx->phase <= 3;) {
Lev Walkin1bbc2002004-10-23 13:27:30 +0000651 pxer_chunk_type_e ch_type; /* XER chunk type */
652 ssize_t ch_size; /* Chunk size */
653 xer_check_tag_e tcv; /* Tag check value */
654 asn_TYPE_member_t *elm;
655
656 /*
Lev Walkinc61f3862005-02-14 17:21:22 +0000657 * Go inside the inner member of a sequence.
Lev Walkin1bbc2002004-10-23 13:27:30 +0000658 */
659 if(ctx->phase == 2) {
660 asn_dec_rval_t tmprval;
Lev Walkin1bbc2002004-10-23 13:27:30 +0000661 void *memb_ptr; /* Pointer to the member */
662 void **memb_ptr2; /* Pointer to that pointer */
663
Lev Walkinabf68892004-10-26 10:12:14 +0000664 elm = &td->elements[edx];
665
Lev Walkin1bbc2002004-10-23 13:27:30 +0000666 if(elm->flags & ATF_POINTER) {
667 /* Member is a pointer to another structure */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800668 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
Lev Walkin1bbc2002004-10-23 13:27:30 +0000669 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800670 memb_ptr = (char *)st + elm->memb_offset;
Lev Walkin1bbc2002004-10-23 13:27:30 +0000671 memb_ptr2 = &memb_ptr;
672 }
673
Lev Walkinc61f3862005-02-14 17:21:22 +0000674 /* Invoke the inner type decoder, m.b. multiple times */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000675 tmprval = elm->type->xer_decoder(opt_codec_ctx,
676 elm->type, memb_ptr2, elm->name,
677 buf_ptr, size);
678 XER_ADVANCE(tmprval.consumed);
679 if(tmprval.code != RC_OK)
680 RETURN(tmprval.code);
Lev Walkinc61f3862005-02-14 17:21:22 +0000681 ctx->phase = 1; /* Back to body processing */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000682 ctx->step = ++edx;
Lev Walkinc61f3862005-02-14 17:21:22 +0000683 ASN_DEBUG("XER/SEQUENCE phase => %d, step => %d",
684 ctx->phase, ctx->step);
Lev Walkin1bbc2002004-10-23 13:27:30 +0000685 /* Fall through */
686 }
687
688 /*
689 * Get the next part of the XML stream.
690 */
Lev Walkin1e443962005-02-18 18:06:36 +0000691 ch_size = xer_next_token(&ctx->context, buf_ptr, size,
692 &ch_type);
Lev Walkin97363482016-01-24 19:23:02 -0800693 if(ch_size == -1) {
694 RETURN(RC_FAIL);
695 } else {
Lev Walkin1bbc2002004-10-23 13:27:30 +0000696 switch(ch_type) {
Lev Walkin97363482016-01-24 19:23:02 -0800697 case PXER_WMORE:
698 RETURN(RC_WMORE);
Lev Walkin1bbc2002004-10-23 13:27:30 +0000699 case PXER_COMMENT: /* Got XML comment */
700 case PXER_TEXT: /* Ignore free-standing text */
701 XER_ADVANCE(ch_size); /* Skip silently */
702 continue;
703 case PXER_TAG:
704 break; /* Check the rest down there */
705 }
706 }
707
Lev Walkinc61f3862005-02-14 17:21:22 +0000708 tcv = xer_check_tag(buf_ptr, ch_size, xml_tag);
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000709 ASN_DEBUG("XER/SEQUENCE: tcv = %d, ph=%d [%s]",
710 tcv, ctx->phase, xml_tag);
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000711
712 /* Skip the extensions section */
713 if(ctx->phase == 3) {
714 switch(xer_skip_unknown(tcv, &ctx->left)) {
715 case -1:
716 ctx->phase = 4;
717 RETURN(RC_FAIL);
718 case 0:
719 XER_ADVANCE(ch_size);
720 continue;
721 case 1:
722 XER_ADVANCE(ch_size);
723 ctx->phase = 1;
724 continue;
725 case 2:
726 ctx->phase = 1;
727 break;
728 }
729 }
730
Lev Walkin1bbc2002004-10-23 13:27:30 +0000731 switch(tcv) {
732 case XCT_CLOSING:
733 if(ctx->phase == 0) break;
Lev Walkin1bbc2002004-10-23 13:27:30 +0000734 ctx->phase = 0;
735 /* Fall through */
736 case XCT_BOTH:
737 if(ctx->phase == 0) {
738 if(edx >= td->elements_count
739 ||
740 /* Explicit OPTIONAL specs reaches the end */
741 (edx + elements[edx].optional
742 == td->elements_count)
743 ||
744 /* All extensions are optional */
745 (IN_EXTENSION_GROUP(specs, edx)
746 && specs->ext_before
Lev Walkin494fb702017-08-07 20:07:00 -0700747 > (signed)td->elements_count)
Lev Walkin1bbc2002004-10-23 13:27:30 +0000748 ) {
749 XER_ADVANCE(ch_size);
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000750 ctx->phase = 4; /* Phase out */
Lev Walkinc61f3862005-02-14 17:21:22 +0000751 RETURN(RC_OK);
Lev Walkin1bbc2002004-10-23 13:27:30 +0000752 } else {
753 ASN_DEBUG("Premature end of XER SEQUENCE");
754 RETURN(RC_FAIL);
755 }
756 }
757 /* Fall through */
758 case XCT_OPENING:
759 if(ctx->phase == 0) {
760 XER_ADVANCE(ch_size);
761 ctx->phase = 1; /* Processing body phase */
762 continue;
763 }
Lev Walkinda3ca412004-10-23 14:58:15 +0000764 /* Fall through */
Lev Walkin904e65b2005-02-18 14:23:48 +0000765 case XCT_UNKNOWN_OP:
766 case XCT_UNKNOWN_BO:
Lev Walkin1bbc2002004-10-23 13:27:30 +0000767
Lev Walkin02666192005-07-03 05:31:02 +0000768 ASN_DEBUG("XER/SEQUENCE: tcv=%d, ph=%d, edx=%d",
769 tcv, ctx->phase, edx);
770 if(ctx->phase != 1) {
Lev Walkin1bbc2002004-10-23 13:27:30 +0000771 break; /* Really unexpected */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000772 }
Lev Walkin02666192005-07-03 05:31:02 +0000773
774 if(edx < td->elements_count) {
775 /*
776 * Search which member corresponds to this tag.
777 */
Lev Walkin494fb702017-08-07 20:07:00 -0700778 size_t n;
779 size_t edx_end = edx + elements[edx].optional + 1;
Lev Walkin02666192005-07-03 05:31:02 +0000780 if(edx_end > td->elements_count)
781 edx_end = td->elements_count;
782 for(n = edx; n < edx_end; n++) {
783 elm = &td->elements[n];
784 tcv = xer_check_tag(buf_ptr,
785 ch_size, elm->name);
786 switch(tcv) {
787 case XCT_BOTH:
788 case XCT_OPENING:
789 /*
790 * Process this member.
791 */
792 ctx->step = edx = n;
793 ctx->phase = 2;
794 break;
795 case XCT_UNKNOWN_OP:
796 case XCT_UNKNOWN_BO:
797 continue;
798 default:
799 n = edx_end;
800 break; /* Phase out */
801 }
802 break;
803 }
804 if(n != edx_end)
805 continue;
806 } else {
807 ASN_DEBUG("Out of defined members: %d/%d",
808 edx, td->elements_count);
809 }
Lev Walkin904e65b2005-02-18 14:23:48 +0000810
811 /* It is expected extension */
812 if(IN_EXTENSION_GROUP(specs,
Lev Walkin02666192005-07-03 05:31:02 +0000813 edx + (edx < td->elements_count
814 ? elements[edx].optional : 0))) {
815 ASN_DEBUG("Got anticipated extension at %d",
816 edx);
Lev Walkin904e65b2005-02-18 14:23:48 +0000817 /*
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000818 * Check for (XCT_BOTH or XCT_UNKNOWN_BO)
819 * By using a mask. Only record a pure
820 * <opening> tags.
Lev Walkin904e65b2005-02-18 14:23:48 +0000821 */
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000822 if(tcv & XCT_CLOSING) {
823 /* Found </extension> without body */
824 } else {
825 ctx->left = 1;
826 ctx->phase = 3; /* Skip ...'s */
827 }
828 XER_ADVANCE(ch_size);
829 continue;
Lev Walkin904e65b2005-02-18 14:23:48 +0000830 }
831
832 /* Fall through */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000833 default:
834 break;
835 }
836
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000837 ASN_DEBUG("Unexpected XML tag in SEQUENCE [%c%c%c%c%c%c]",
838 size>0?((const char *)buf_ptr)[0]:'.',
839 size>1?((const char *)buf_ptr)[1]:'.',
840 size>2?((const char *)buf_ptr)[2]:'.',
841 size>3?((const char *)buf_ptr)[3]:'.',
842 size>4?((const char *)buf_ptr)[4]:'.',
843 size>5?((const char *)buf_ptr)[5]:'.');
Lev Walkin1bbc2002004-10-23 13:27:30 +0000844 break;
845 }
846
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000847 ctx->phase = 4; /* "Phase out" on hard failure */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000848 RETURN(RC_FAIL);
849}
850
Lev Walkina9cc46e2004-09-22 16:06:28 +0000851asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000852SEQUENCE_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000853 int ilevel, enum xer_encoder_flags_e flags,
854 asn_app_consume_bytes_f *cb, void *app_key) {
855 asn_enc_rval_t er;
856 int xcan = (flags & XER_F_CANONICAL);
Lev Walkin494fb702017-08-07 20:07:00 -0700857 size_t edx;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000858
859 if(!sptr)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700860 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000861
862 er.encoded = 0;
863
864 for(edx = 0; edx < td->elements_count; edx++) {
865 asn_enc_rval_t tmper;
Lev Walkin5e033762004-09-29 13:26:15 +0000866 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkina9cc46e2004-09-22 16:06:28 +0000867 void *memb_ptr;
868 const char *mname = elm->name;
869 unsigned int mlen = strlen(mname);
870
871 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800872 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
Lev Walkinac589332005-08-22 14:19:28 +0000873 if(!memb_ptr) {
874 if(elm->optional)
875 continue;
876 /* Mandatory element is missing */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700877 ASN__ENCODE_FAILED;
Lev Walkinac589332005-08-22 14:19:28 +0000878 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000879 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800880 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000881 }
882
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700883 if(!xcan) ASN__TEXT_INDENT(1, ilevel);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700884 ASN__CALLBACK3("<", 1, mname, mlen, ">", 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000885
886 /* Print the member itself */
887 tmper = elm->type->xer_encoder(elm->type, memb_ptr,
888 ilevel + 1, flags, cb, app_key);
889 if(tmper.encoded == -1) return tmper;
890
Lev Walkin7c1dc052016-03-14 03:08:15 -0700891 ASN__CALLBACK3("</", 2, mname, mlen, ">", 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000892 er.encoded += 5 + (2 * mlen) + tmper.encoded;
893 }
894
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700895 if(!xcan) ASN__TEXT_INDENT(1, ilevel - 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000896
Lev Walkin7c1dc052016-03-14 03:08:15 -0700897 ASN__ENCODED_OK(er);
Lev Walkin942fd082004-10-03 09:13:02 +0000898cb_failed:
Lev Walkin7c1dc052016-03-14 03:08:15 -0700899 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000900}
901
Lev Walkinf15320b2004-06-03 03:38:44 +0000902int
Lev Walkin5e033762004-09-29 13:26:15 +0000903SEQUENCE_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000904 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin494fb702017-08-07 20:07:00 -0700905 size_t edx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000906 int ret;
907
Lev Walkin8e8078a2004-09-26 13:10:40 +0000908 if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000909
910 /* Dump preamble */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000911 if(cb(td->name, strlen(td->name), app_key) < 0
912 || cb(" ::= {", 6, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000913 return -1;
914
Lev Walkin449f8322004-08-20 13:23:42 +0000915 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000916 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000917 const void *memb_ptr;
918
Lev Walkincc93b0f2004-09-10 09:18:20 +0000919 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800920 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkinac589332005-08-22 14:19:28 +0000921 if(!memb_ptr) {
922 if(elm->optional) continue;
923 /* Print <absent> line */
924 /* Fall through */
925 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000926 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800927 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000928 }
929
930 /* Indentation */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000931 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000932
933 /* Print the member's name and stuff */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000934 if(cb(elm->name, strlen(elm->name), app_key) < 0
935 || cb(": ", 2, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000936 return -1;
937
938 /* Print the member itself */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000939 ret = elm->type->print_struct(elm->type, memb_ptr, ilevel + 1,
Lev Walkinf15320b2004-06-03 03:38:44 +0000940 cb, app_key);
941 if(ret) return ret;
Lev Walkinf15320b2004-06-03 03:38:44 +0000942 }
943
Lev Walkin8e8078a2004-09-26 13:10:40 +0000944 ilevel--;
945 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000946
Lev Walkin8e8078a2004-09-26 13:10:40 +0000947 return (cb("}", 1, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000948}
949
950void
Lev Walkin5e033762004-09-29 13:26:15 +0000951SEQUENCE_free(asn_TYPE_descriptor_t *td, void *sptr, int contents_only) {
Lev Walkin494fb702017-08-07 20:07:00 -0700952 size_t edx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000953
954 if(!td || !sptr)
955 return;
956
957 ASN_DEBUG("Freeing %s as SEQUENCE", td->name);
958
Lev Walkin449f8322004-08-20 13:23:42 +0000959 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000960 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000961 void *memb_ptr;
Lev Walkincc93b0f2004-09-10 09:18:20 +0000962 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800963 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000964 if(memb_ptr)
Lev Walkinadcb5862006-03-17 02:11:12 +0000965 ASN_STRUCT_FREE(*elm->type, memb_ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +0000966 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800967 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkinadcb5862006-03-17 02:11:12 +0000968 ASN_STRUCT_FREE_CONTENTS_ONLY(*elm->type, memb_ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +0000969 }
970 }
971
972 if(!contents_only) {
973 FREEMEM(sptr);
974 }
975}
976
977int
Lev Walkin5e033762004-09-29 13:26:15 +0000978SEQUENCE_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +0000979 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkin494fb702017-08-07 20:07:00 -0700980 size_t edx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000981
982 if(!sptr) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700983 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +0000984 "%s: value not given (%s:%d)",
985 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000986 return -1;
987 }
988
989 /*
990 * Iterate over structure members and check their validity.
991 */
Lev Walkin449f8322004-08-20 13:23:42 +0000992 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000993 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000994 const void *memb_ptr;
995
Lev Walkincc93b0f2004-09-10 09:18:20 +0000996 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800997 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkinac589332005-08-22 14:19:28 +0000998 if(!memb_ptr) {
999 if(elm->optional)
1000 continue;
Lev Walkin7c1dc052016-03-14 03:08:15 -07001001 ASN__CTFAIL(app_key, td, sptr,
Lev Walkinac589332005-08-22 14:19:28 +00001002 "%s: mandatory element %s absent (%s:%d)",
1003 td->name, elm->name, __FILE__, __LINE__);
1004 return -1;
1005 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001006 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001007 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +00001008 }
1009
Lev Walkin449f8322004-08-20 13:23:42 +00001010 if(elm->memb_constraints) {
1011 int ret = elm->memb_constraints(elm->type, memb_ptr,
Lev Walkin1eded352006-07-13 11:19:01 +00001012 ctfailcb, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +00001013 if(ret) return ret;
1014 } else {
1015 int ret = elm->type->check_constraints(elm->type,
Lev Walkin1eded352006-07-13 11:19:01 +00001016 memb_ptr, ctfailcb, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +00001017 if(ret) return ret;
1018 /*
1019 * Cannot inherit it earlier:
1020 * need to make sure we get the updated version.
1021 */
1022 elm->memb_constraints = elm->type->check_constraints;
1023 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001024 }
1025
1026 return 0;
1027}
Lev Walkin59b176e2005-11-26 11:25:14 +00001028
1029asn_dec_rval_t
1030SEQUENCE_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -07001031 const asn_per_constraints_t *constraints, void **sptr,
1032 asn_per_data_t *pd) {
1033 asn_SEQUENCE_specifics_t *specs = (asn_SEQUENCE_specifics_t *)td->specifics;
Lev Walkin59b176e2005-11-26 11:25:14 +00001034 void *st = *sptr; /* Target structure. */
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001035 int extpresent; /* Extension additions are present */
Lev Walkin59b176e2005-11-26 11:25:14 +00001036 uint8_t *opres; /* Presence of optional root members */
1037 asn_per_data_t opmd;
1038 asn_dec_rval_t rv;
Lev Walkin494fb702017-08-07 20:07:00 -07001039 size_t edx;
Lev Walkin59b176e2005-11-26 11:25:14 +00001040
1041 (void)constraints;
1042
Lev Walkin7c1dc052016-03-14 03:08:15 -07001043 if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx))
1044 ASN__DECODE_FAILED;
Lev Walkin1d9e8dd2005-12-07 05:46:03 +00001045
Lev Walkin59b176e2005-11-26 11:25:14 +00001046 if(!st) {
1047 st = *sptr = CALLOC(1, specs->struct_size);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001048 if(!st) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +00001049 }
1050
1051 ASN_DEBUG("Decoding %s as SEQUENCE (UPER)", td->name);
1052
1053 /* Handle extensions */
1054 if(specs->ext_before >= 0) {
1055 extpresent = per_get_few_bits(pd, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001056 if(extpresent < 0) ASN__DECODE_STARVED;
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001057 } else {
1058 extpresent = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +00001059 }
1060
1061 /* Prepare a place and read-in the presence bitmap */
Lev Walkin73a5cc62007-06-24 08:45:31 +00001062 memset(&opmd, 0, sizeof(opmd));
Lev Walkin59b176e2005-11-26 11:25:14 +00001063 if(specs->roms_count) {
1064 opres = (uint8_t *)MALLOC(((specs->roms_count + 7) >> 3) + 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001065 if(!opres) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +00001066 /* Get the presence map */
1067 if(per_get_many_bits(pd, opres, 0, specs->roms_count)) {
1068 FREEMEM(opres);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001069 ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +00001070 }
1071 opmd.buffer = opres;
Lev Walkin59b176e2005-11-26 11:25:14 +00001072 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);
1075 } else {
1076 opres = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +00001077 }
1078
1079 /*
1080 * Get the sequence ROOT elements.
1081 */
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001082 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001083 asn_TYPE_member_t *elm = &td->elements[edx];
1084 void *memb_ptr; /* Pointer to the member */
1085 void **memb_ptr2; /* Pointer to that pointer */
1086
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001087 if(IN_EXTENSION_GROUP(specs, edx))
1088 continue;
1089
Lev Walkin59b176e2005-11-26 11:25:14 +00001090 /* Fetch the pointer to this member */
1091 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001092 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
Lev Walkin59b176e2005-11-26 11:25:14 +00001093 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001094 memb_ptr = (char *)st + elm->memb_offset;
Lev Walkin59b176e2005-11-26 11:25:14 +00001095 memb_ptr2 = &memb_ptr;
1096 }
1097
1098 /* Deal with optionality */
1099 if(elm->optional) {
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);
1104 if(present == 0) {
1105 /* This element is not present */
1106 if(elm->default_value) {
1107 /* Fill-in DEFAULT */
Lev Walkin523de9e2006-08-18 01:34:18 +00001108 if(elm->default_value(1, memb_ptr2)) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001109 FREEMEM(opres);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001110 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +00001111 }
Lev Walkin8032f7a2007-06-27 01:54:57 +00001112 ASN_DEBUG("Filled-in default");
Lev Walkin59b176e2005-11-26 11:25:14 +00001113 }
1114 /* The member is just not present */
1115 continue;
1116 }
1117 /* Fall through */
1118 }
1119
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);
1127 FREEMEM(opres);
1128 return rv;
1129 }
1130 }
1131
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001132 /* Optionality map is not needed anymore */
1133 FREEMEM(opres);
1134
Lev Walkin59b176e2005-11-26 11:25:14 +00001135 /*
1136 * Deal with extensions.
1137 */
1138 if(extpresent) {
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001139 ssize_t bmlength;
1140 uint8_t *epres; /* Presence of extension members */
1141 asn_per_data_t epmd;
Lev Walkin59b176e2005-11-26 11:25:14 +00001142
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001143 bmlength = uper_get_nslength(pd);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001144 if(bmlength < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +00001145
Lev Walkinaaae9bf2010-10-24 16:56:30 -07001146 ASN_DEBUG("Extensions %ld present in %s", (long)bmlength, td->name);
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001147
1148 epres = (uint8_t *)MALLOC((bmlength + 15) >> 3);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001149 if(!epres) ASN__DECODE_STARVED;
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001150
1151 /* Get the extensions map */
Simo Sorcece71d912015-09-03 16:34:20 -04001152 if(per_get_many_bits(pd, epres, 0, bmlength)) {
1153 FREEMEM(epres);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001154 ASN__DECODE_STARVED;
Simo Sorcece71d912015-09-03 16:34:20 -04001155 }
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001156
Lev Walkin73a5cc62007-06-24 08:45:31 +00001157 memset(&epmd, 0, sizeof(epmd));
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001158 epmd.buffer = epres;
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001159 epmd.nbits = bmlength;
Lev Walkinaaae9bf2010-10-24 16:56:30 -07001160 ASN_DEBUG("Read in extensions bitmap for %s of %ld bits (%x..)",
1161 td->name, (long)bmlength, *epres);
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001162
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 */
1168 int present;
1169
1170 if(!IN_EXTENSION_GROUP(specs, edx)) {
1171 ASN_DEBUG("%d is not extension", edx);
1172 continue;
1173 }
1174
1175 /* Fetch the pointer to this member */
1176 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001177 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001178 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001179 memb_ptr = (void *)((char *)st + elm->memb_offset);
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001180 memb_ptr2 = &memb_ptr;
1181 }
1182
1183 present = per_get_few_bits(&epmd, 1);
1184 if(present <= 0) {
1185 if(present < 0) break; /* No more extensions */
1186 continue;
1187 }
1188
1189 ASN_DEBUG("Decoding member %s in %s %p", elm->name, td->name, *memb_ptr2);
Lev Walkin9218bc12007-06-27 04:09:37 +00001190 rv = uper_open_type_get(opt_codec_ctx, elm->type,
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001191 elm->per_constraints, memb_ptr2, pd);
1192 if(rv.code != RC_OK) {
1193 FREEMEM(epres);
1194 return rv;
1195 }
1196 }
1197
1198 /* Skip over overflow extensions which aren't present
1199 * in this system's version of the protocol */
Lev Walkin06230f72007-06-26 09:52:44 +00001200 for(;;) {
Lev Walkina2987ea2007-06-26 23:56:54 +00001201 ASN_DEBUG("Getting overflow extensions");
Lev Walkin06230f72007-06-26 09:52:44 +00001202 switch(per_get_few_bits(&epmd, 1)) {
1203 case -1: break;
1204 case 0: continue;
1205 default:
Lev Walkin9218bc12007-06-27 04:09:37 +00001206 if(uper_open_type_skip(opt_codec_ctx, pd)) {
Lev Walkin06230f72007-06-26 09:52:44 +00001207 FREEMEM(epres);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001208 ASN__DECODE_STARVED;
Lev Walkin06230f72007-06-26 09:52:44 +00001209 }
Lev Walkin59b176e2005-11-26 11:25:14 +00001210 }
Lev Walkin06230f72007-06-26 09:52:44 +00001211 break;
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001212 }
Lev Walkin59b176e2005-11-26 11:25:14 +00001213
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001214 FREEMEM(epres);
1215 }
1216
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 */
1222
1223 if(!elm->default_value) continue;
1224
1225 /* Fetch the pointer to this member */
1226 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001227 memb_ptr2 = (void **)((char *)st
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001228 + elm->memb_offset);
1229 if(*memb_ptr2) continue;
1230 } else {
1231 continue; /* Extensions are all optionals */
1232 }
1233
1234 /* Set default value */
1235 if(elm->default_value(1, memb_ptr2)) {
Lev Walkin7c1dc052016-03-14 03:08:15 -07001236 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +00001237 }
1238 }
1239
1240 rv.consumed = 0;
1241 rv.code = RC_OK;
1242 return rv;
1243}
1244
Lev Walkin62258e22007-06-23 23:50:25 +00001245static int
1246SEQUENCE_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;
Lev Walkinf55a6dd2007-06-24 00:35:51 +00001250 int exts_present = 0;
1251 int exts_count = 0;
Lev Walkin494fb702017-08-07 20:07:00 -07001252 size_t edx;
Lev Walkin62258e22007-06-23 23:50:25 +00001253
1254 if(specs->ext_before < 0)
1255 return 0;
1256
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 */
1262 int present;
1263
Lev Walkinf55a6dd2007-06-24 00:35:51 +00001264 if(!IN_EXTENSION_GROUP(specs, edx)) {
Lev Walkin06230f72007-06-26 09:52:44 +00001265 ASN_DEBUG("%s (@%d) is not extension", elm->type->name, edx);
Lev Walkin62258e22007-06-23 23:50:25 +00001266 continue;
Lev Walkinf55a6dd2007-06-24 00:35:51 +00001267 }
Lev Walkin62258e22007-06-23 23:50:25 +00001268
1269 /* Fetch the pointer to this member */
1270 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001271 memb_ptr2 = (void **)((char *)sptr + elm->memb_offset);
Lev Walkin62258e22007-06-23 23:50:25 +00001272 present = (*memb_ptr2 != 0);
1273 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001274 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkin62258e22007-06-23 23:50:25 +00001275 memb_ptr2 = &memb_ptr;
1276 present = 1;
1277 }
1278
Lev Walkin06230f72007-06-26 09:52:44 +00001279 ASN_DEBUG("checking %s (@%d) present => %d",
1280 elm->type->name, edx, present);
Lev Walkinf55a6dd2007-06-24 00:35:51 +00001281 exts_count++;
1282 exts_present += present;
Lev Walkin62258e22007-06-23 23:50:25 +00001283
1284 /* Encode as presence marker */
1285 if(po1 && per_put_few_bits(po1, present, 1))
1286 return -1;
1287 /* Encode as open type field */
Lev Walkin9218bc12007-06-27 04:09:37 +00001288 if(po2 && present && uper_open_type_put(elm->type,
Lev Walkin62258e22007-06-23 23:50:25 +00001289 elm->per_constraints, *memb_ptr2, po2))
1290 return -1;
1291
1292 }
1293
Lev Walkinf55a6dd2007-06-24 00:35:51 +00001294 return exts_present ? exts_count : 0;
Lev Walkin62258e22007-06-23 23:50:25 +00001295}
1296
Lev Walkin523de9e2006-08-18 01:34:18 +00001297asn_enc_rval_t
1298SEQUENCE_encode_uper(asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -07001299 const asn_per_constraints_t *constraints, void *sptr,
1300 asn_per_outp_t *po) {
1301 asn_SEQUENCE_specifics_t *specs
Lev Walkin523de9e2006-08-18 01:34:18 +00001302 = (asn_SEQUENCE_specifics_t *)td->specifics;
1303 asn_enc_rval_t er;
Lev Walkin62258e22007-06-23 23:50:25 +00001304 int n_extensions;
Lev Walkin494fb702017-08-07 20:07:00 -07001305 size_t edx;
1306 size_t i;
Lev Walkin523de9e2006-08-18 01:34:18 +00001307
1308 (void)constraints;
1309
1310 if(!sptr)
Lev Walkin7c1dc052016-03-14 03:08:15 -07001311 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001312
1313 er.encoded = 0;
1314
1315 ASN_DEBUG("Encoding %s as SEQUENCE (UPER)", td->name);
Lev Walkin62258e22007-06-23 23:50:25 +00001316
1317
1318 /*
1319 * X.691#18.1 Whether structure is extensible
1320 * and whether to encode extensions
1321 */
1322 if(specs->ext_before >= 0) {
1323 n_extensions = SEQUENCE_handle_extensions(td, sptr, 0, 0);
Lev Walkin494fb702017-08-07 20:07:00 -07001324 if(n_extensions < 0)
1325 ASN__ENCODE_FAILED;
1326 if(per_put_few_bits(po, n_extensions ? 1 : 0, 1))
1327 ASN__ENCODE_FAILED;
Lev Walkin62258e22007-06-23 23:50:25 +00001328 } else {
1329 n_extensions = 0; /* There are no extensions to encode */
1330 }
Lev Walkin523de9e2006-08-18 01:34:18 +00001331
1332 /* Encode a presence bitmap */
1333 for(i = 0; i < specs->roms_count; i++) {
Lev Walkinc46b7cb2006-08-18 02:27:55 +00001334 asn_TYPE_member_t *elm;
Lev Walkin523de9e2006-08-18 01:34:18 +00001335 void *memb_ptr; /* Pointer to the member */
1336 void **memb_ptr2; /* Pointer to that pointer */
Lev Walkin523de9e2006-08-18 01:34:18 +00001337 int present;
1338
Lev Walkinc46b7cb2006-08-18 02:27:55 +00001339 edx = specs->oms[i];
1340 elm = &td->elements[edx];
1341
Lev Walkin523de9e2006-08-18 01:34:18 +00001342 /* Fetch the pointer to this member */
1343 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001344 memb_ptr2 = (void **)((char *)sptr + elm->memb_offset);
Lev Walkin523de9e2006-08-18 01:34:18 +00001345 present = (*memb_ptr2 != 0);
1346 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001347 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkin523de9e2006-08-18 01:34:18 +00001348 memb_ptr2 = &memb_ptr;
1349 present = 1;
1350 }
1351
1352 /* Eliminate default values */
1353 if(present && elm->default_value
1354 && elm->default_value(0, memb_ptr2) == 1)
1355 present = 0;
1356
1357 ASN_DEBUG("Element %s %s %s->%s is %s",
1358 elm->flags & ATF_POINTER ? "ptr" : "inline",
1359 elm->default_value ? "def" : "wtv",
1360 td->name, elm->name, present ? "present" : "absent");
1361 if(per_put_few_bits(po, present, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001362 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001363 }
1364
1365 /*
Lev Walkin62258e22007-06-23 23:50:25 +00001366 * Encode the sequence ROOT elements.
Lev Walkin523de9e2006-08-18 01:34:18 +00001367 */
Lev Walkin62258e22007-06-23 23:50:25 +00001368 ASN_DEBUG("ext_after = %d, ec = %d, eb = %d", specs->ext_after, td->elements_count, specs->ext_before);
Lev Walkin4d22b622007-07-23 09:58:25 +00001369 for(edx = 0; edx < ((specs->ext_after < 0)
1370 ? td->elements_count : specs->ext_before - 1); edx++) {
1371
Lev Walkin523de9e2006-08-18 01:34:18 +00001372 asn_TYPE_member_t *elm = &td->elements[edx];
1373 void *memb_ptr; /* Pointer to the member */
1374 void **memb_ptr2; /* Pointer to that pointer */
1375
Lev Walkin4d22b622007-07-23 09:58:25 +00001376 if(IN_EXTENSION_GROUP(specs, edx))
1377 continue;
1378
Lev Walkina2987ea2007-06-26 23:56:54 +00001379 ASN_DEBUG("About to encode %s", elm->type->name);
1380
Lev Walkin523de9e2006-08-18 01:34:18 +00001381 /* Fetch the pointer to this member */
1382 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001383 memb_ptr2 = (void **)((char *)sptr + elm->memb_offset);
Lev Walkin523de9e2006-08-18 01:34:18 +00001384 if(!*memb_ptr2) {
1385 ASN_DEBUG("Element %s %d not present",
1386 elm->name, edx);
1387 if(elm->optional)
1388 continue;
1389 /* Mandatory element is missing */
Lev Walkin7c1dc052016-03-14 03:08:15 -07001390 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001391 }
1392 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001393 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkin523de9e2006-08-18 01:34:18 +00001394 memb_ptr2 = &memb_ptr;
1395 }
1396
1397 /* Eliminate default values */
Lev Walkindbd091a2007-06-27 02:11:08 +00001398 if(elm->default_value && elm->default_value(0, memb_ptr2) == 1)
Lev Walkin523de9e2006-08-18 01:34:18 +00001399 continue;
1400
Lev Walkin4d22b622007-07-23 09:58:25 +00001401 ASN_DEBUG("Encoding %s->%s", td->name, elm->name);
Lev Walkin523de9e2006-08-18 01:34:18 +00001402 er = elm->type->uper_encoder(elm->type, elm->per_constraints,
1403 *memb_ptr2, po);
1404 if(er.encoded == -1)
1405 return er;
1406 }
1407
Lev Walkin62258e22007-06-23 23:50:25 +00001408 /* No extensions to encode */
Lev Walkin7c1dc052016-03-14 03:08:15 -07001409 if(!n_extensions) ASN__ENCODED_OK(er);
Lev Walkin62258e22007-06-23 23:50:25 +00001410
1411 ASN_DEBUG("Length of %d bit-map", n_extensions);
1412 /* #18.8. Write down the presence bit-map length. */
1413 if(uper_put_nslength(po, n_extensions))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001414 ASN__ENCODE_FAILED;
Lev Walkin62258e22007-06-23 23:50:25 +00001415
1416 ASN_DEBUG("Bit-map of %d elements", n_extensions);
1417 /* #18.7. Encoding the extensions presence bit-map. */
1418 /* TODO: act upon NOTE in #18.7 for canonical PER */
1419 if(SEQUENCE_handle_extensions(td, sptr, po, 0) != n_extensions)
Lev Walkin7c1dc052016-03-14 03:08:15 -07001420 ASN__ENCODE_FAILED;
Lev Walkin62258e22007-06-23 23:50:25 +00001421
1422 ASN_DEBUG("Writing %d extensions", n_extensions);
1423 /* #18.9. Encode extensions as open type fields. */
1424 if(SEQUENCE_handle_extensions(td, sptr, 0, po) != n_extensions)
Lev Walkin7c1dc052016-03-14 03:08:15 -07001425 ASN__ENCODE_FAILED;
Lev Walkin62258e22007-06-23 23:50:25 +00001426
Lev Walkin7c1dc052016-03-14 03:08:15 -07001427 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +00001428}
1429