blob: 2fbf97b67150a405dccd94e59f06a452dae3cd99 [file] [log] [blame]
Lev Walkin83668612017-10-21 00:24:31 -07001/*
Lev Walkincd2f48e2017-08-10 02:14:59 -07002 * Copyright (c) 2003-2017 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 Walkinf6853ce2017-08-11 00:50:27 -07008#include <OPEN_TYPE.h>
Lev Walkin9218bc12007-06-27 04:09:37 +00009#include <per_opentype.h>
Lev Walkinf15320b2004-06-03 03:38:44 +000010
11/*
12 * Number of bytes left for this structure.
13 * (ctx->left) indicates the number of bytes _transferred_ for the structure.
14 * (size) contains the number of bytes in the buffer passed.
15 */
Lev Walkinec1ffd42004-08-18 04:53:32 +000016#define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left)
Lev Walkinf15320b2004-06-03 03:38:44 +000017
18/*
19 * If the subprocessor function returns with an indication that it wants
20 * more data, it may well be a fatal decoding problem, because the
21 * size is constrained by the <TLV>'s L, even if the buffer size allows
22 * reading more data.
23 * For example, consider the buffer containing the following TLVs:
24 * <T:5><L:1><V> <T:6>...
25 * The TLV length clearly indicates that one byte is expected in V, but
26 * if the V processor returns with "want more data" even if the buffer
27 * contains way more data than the V processor have seen.
28 */
Lev Walkind9bd7752004-06-05 08:17:50 +000029#define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size)
Lev Walkinf15320b2004-06-03 03:38:44 +000030
31/*
32 * This macro "eats" the part of the buffer which is definitely "consumed",
33 * i.e. was correctly converted into local representation or rightfully skipped.
34 */
Lev Walkincc6a9102004-09-23 22:06:26 +000035#undef ADVANCE
Lev Walkinf15320b2004-06-03 03:38:44 +000036#define ADVANCE(num_bytes) do { \
37 size_t num = num_bytes; \
Lev Walkinaa61a0f2014-01-13 23:08:47 -080038 ptr = ((const char *)ptr) + num; \
Lev Walkinf15320b2004-06-03 03:38:44 +000039 size -= num; \
40 if(ctx->left >= 0) \
41 ctx->left -= num; \
42 consumed_myself += num; \
43 } while(0)
44
45/*
46 * Switch to the next phase of parsing.
47 */
Lev Walkincc6a9102004-09-23 22:06:26 +000048#undef NEXT_PHASE
49#undef PHASE_OUT
Lev Walkinf15320b2004-06-03 03:38:44 +000050#define NEXT_PHASE(ctx) do { \
51 ctx->phase++; \
52 ctx->step = 0; \
53 } while(0)
54#define PHASE_OUT(ctx) do { ctx->phase = 10; } while(0)
55
56/*
57 * Return a standardized complex structure.
58 */
Lev Walkincc6a9102004-09-23 22:06:26 +000059#undef RETURN
Lev Walkinf15320b2004-06-03 03:38:44 +000060#define RETURN(_code) do { \
61 rval.code = _code; \
62 rval.consumed = consumed_myself;\
63 return rval; \
64 } while(0)
65
66/*
67 * Check whether we are inside the extensions group.
68 */
Lev Walkin83668612017-10-21 00:24:31 -070069#define IN_EXTENSION_GROUP(specs, memb_idx) \
70 ((specs)->first_extension >= 0 \
71 && (unsigned)(specs)->first_extension <= (memb_idx))
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 Walkin20696a42017-10-17 21:27:33 -0700112SEQUENCE_decode_ber(const asn_codec_ctx_t *opt_codec_ctx,
113 const asn_TYPE_descriptor_t *td, void **struct_ptr,
114 const void *ptr, size_t size, int tag_mode) {
115 /*
Lev Walkinf15320b2004-06-03 03:38:44 +0000116 * Bring closer parts of structure description.
117 */
Lev Walkind84f6032017-10-03 16:33:59 -0700118 const asn_SEQUENCE_specifics_t *specs = (const asn_SEQUENCE_specifics_t *)td->specifics;
Lev Walkin20696a42017-10-17 21:27:33 -0700119 const asn_TYPE_member_t *elements = td->elements;
Lev Walkinf15320b2004-06-03 03:38:44 +0000120
Lev Walkin20696a42017-10-17 21:27:33 -0700121 /*
Lev Walkinf15320b2004-06-03 03:38:44 +0000122 * Parts of the structure being constructed.
123 */
124 void *st = *struct_ptr; /* Target structure. */
Lev Walkin5e033762004-09-29 13:26:15 +0000125 asn_struct_ctx_t *ctx; /* Decoder context */
Lev Walkinf15320b2004-06-03 03:38:44 +0000126
127 ber_tlv_tag_t tlv_tag; /* T from TLV */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000128 asn_dec_rval_t rval; /* Return code from subparsers */
Lev Walkinf15320b2004-06-03 03:38:44 +0000129
130 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
Lev Walkin494fb702017-08-07 20:07:00 -0700131 size_t edx; /* SEQUENCE element's index */
Lev Walkinf15320b2004-06-03 03:38:44 +0000132
Lev Walkin449f8322004-08-20 13:23:42 +0000133 ASN_DEBUG("Decoding %s as SEQUENCE", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000134
135 /*
136 * Create the target structure if it is not present already.
137 */
138 if(st == 0) {
139 st = *struct_ptr = CALLOC(1, specs->struct_size);
140 if(st == 0) {
141 RETURN(RC_FAIL);
142 }
143 }
144
145 /*
146 * Restore parsing context.
147 */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800148 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000149
150 /*
151 * Start to parse where left previously
152 */
153 switch(ctx->phase) {
154 case 0:
155 /*
156 * PHASE 0.
157 * Check that the set of tags associated with given structure
158 * perfectly fits our expectations.
159 */
160
Lev Walkin5e033762004-09-29 13:26:15 +0000161 rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size,
Lev Walkin8e8078a2004-09-26 13:10:40 +0000162 tag_mode, 1, &ctx->left, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000163 if(rval.code != RC_OK) {
164 ASN_DEBUG("%s tagging check failed: %d",
Lev Walkin449f8322004-08-20 13:23:42 +0000165 td->name, rval.code);
Lev Walkin566a5672004-10-05 06:36:57 +0000166 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000167 }
168
169 if(ctx->left >= 0)
170 ctx->left += rval.consumed; /* ?Substracted below! */
171 ADVANCE(rval.consumed);
172
173 NEXT_PHASE(ctx);
174
175 ASN_DEBUG("Structure consumes %ld bytes, buffer %ld",
176 (long)ctx->left, (long)size);
177
178 /* Fall through */
179 case 1:
180 /*
181 * PHASE 1.
182 * From the place where we've left it previously,
183 * try to decode the next member from the list of
184 * this structure's elements.
185 * (ctx->step) stores the member being processed
186 * between invocations and the microphase {0,1} of parsing
187 * that member:
188 * step = (<member_number> * 2 + <microphase>).
189 */
Lev Walkin494fb702017-08-07 20:07:00 -0700190 for(edx = ((size_t)ctx->step >> 1); edx < td->elements_count;
Lev Walkinf15320b2004-06-03 03:38:44 +0000191 edx++, ctx->step = (ctx->step & ~1) + 2) {
192 void *memb_ptr; /* Pointer to the member */
Lev Walkinc2346572004-08-11 09:07:36 +0000193 void **memb_ptr2; /* Pointer to that pointer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000194 ssize_t tag_len; /* Length of TLV's T */
Lev Walkin494fb702017-08-07 20:07:00 -0700195 size_t opt_edx_end; /* Next non-optional element */
196 size_t n;
Lev Walkin4c36e302004-06-06 07:20:02 +0000197 int use_bsearch;
Lev Walkinf15320b2004-06-03 03:38:44 +0000198
199 if(ctx->step & 1)
200 goto microphase2;
201
202 /*
203 * MICROPHASE 1: Synchronize decoding.
204 */
Lev Walkin9f470d62017-10-21 16:27:08 -0700205 ASN_DEBUG("In %s SEQUENCE left %d, edx=%" ASN_PRI_SIZE " flags=%d"
Lev Walkincc93b0f2004-09-10 09:18:20 +0000206 " opt=%d ec=%d",
207 td->name, (int)ctx->left, edx,
208 elements[edx].flags, elements[edx].optional,
209 td->elements_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000210
Lev Walkin83668612017-10-21 00:24:31 -0700211 if(ctx->left == 0 /* No more stuff is expected */
212 && (
213 /* Explicit OPTIONAL specification reaches the end */
214 (edx + elements[edx].optional == td->elements_count) ||
215 /* All extensions are optional */
216 IN_EXTENSION_GROUP(specs, edx))) {
217 ASN_DEBUG("End of SEQUENCE %s", td->name);
218 /*
219 * Found the legitimate end of the structure.
220 */
221 PHASE_OUT(ctx);
222 RETURN(RC_OK);
223 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000224
225 /*
226 * Fetch the T from TLV.
227 */
228 tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
Lev Walkin9f470d62017-10-21 16:27:08 -0700229 ASN_DEBUG("Current tag in %s SEQUENCE for element %" ASN_PRI_SIZE " "
Lev Walkincc6a9102004-09-23 22:06:26 +0000230 "(%s) is %s encoded in %d bytes, of frame %ld",
Lev Walkin906654e2004-09-10 15:49:15 +0000231 td->name, edx, elements[edx].name,
232 ber_tlv_tag_string(tlv_tag), (int)tag_len, (long)LEFT);
Lev Walkinf15320b2004-06-03 03:38:44 +0000233 switch(tag_len) {
234 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
235 /* Fall through */
236 case -1: RETURN(RC_FAIL);
237 }
238
Lev Walkin83668612017-10-21 00:24:31 -0700239 if(ctx->left < 0 && ((const uint8_t *)ptr)[0] == 0) {
240 if(LEFT < 2) {
241 if(SIZE_VIOLATION) {
242 RETURN(RC_FAIL);
243 } else {
244 RETURN(RC_WMORE);
245 }
246 } else if(((const uint8_t *)ptr)[1] == 0) {
Lev Walkin9f470d62017-10-21 16:27:08 -0700247 ASN_DEBUG("edx = %" ASN_PRI_SIZE ", opt = %d, ec=%d", edx,
Lev Walkin83668612017-10-21 00:24:31 -0700248 elements[edx].optional, td->elements_count);
249 if((edx + elements[edx].optional == td->elements_count)
250 || IN_EXTENSION_GROUP(specs, edx)) {
251 /*
252 * Yeah, baby! Found the terminator
253 * of the indefinite length structure.
254 */
255 /*
256 * Proceed to the canonical
257 * finalization function.
258 * No advancing is necessary.
259 */
260 goto phase3;
261 }
262 }
263 }
Lev Walkin566a5672004-10-05 06:36:57 +0000264
Lev Walkinf15320b2004-06-03 03:38:44 +0000265 /*
266 * Find the next available type with this tag.
267 */
Lev Walkin4c36e302004-06-06 07:20:02 +0000268 use_bsearch = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000269 opt_edx_end = edx + elements[edx].optional + 1;
Lev Walkin449f8322004-08-20 13:23:42 +0000270 if(opt_edx_end > td->elements_count)
271 opt_edx_end = td->elements_count; /* Cap */
Lev Walkinf9dbd9a2004-07-01 00:48:04 +0000272 else if(opt_edx_end - edx > 8) {
Lev Walkin924fa032004-06-14 13:42:23 +0000273 /* Limit the scope of linear search... */
Lev Walkinf9dbd9a2004-07-01 00:48:04 +0000274 opt_edx_end = edx + 8;
Lev Walkin4c36e302004-06-06 07:20:02 +0000275 use_bsearch = 1;
Lev Walkin924fa032004-06-14 13:42:23 +0000276 /* ... and resort to bsearch() */
Lev Walkin4c36e302004-06-06 07:20:02 +0000277 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000278 for(n = edx; n < opt_edx_end; n++) {
279 if(BER_TAGS_EQUAL(tlv_tag, elements[n].tag)) {
280 /*
281 * Found element corresponding to the tag
282 * being looked at.
283 * Reposition over the right element.
284 */
285 edx = n;
Lev Walkin4c36e302004-06-06 07:20:02 +0000286 ctx->step = 1 + 2 * edx; /* Remember! */
287 goto microphase2;
Lev Walkin14fd3e52017-08-27 01:38:45 -0700288 } else if(elements[n].flags & ATF_ANY_TYPE) {
Lev Walkinb9189732004-09-10 09:37:12 +0000289 /*
290 * This is the ANY type, which may bear
291 * any flag whatsoever.
292 */
293 edx = n;
294 ctx->step = 1 + 2 * edx; /* Remember! */
295 goto microphase2;
Lev Walkin4c36e302004-06-06 07:20:02 +0000296 } else if(elements[n].tag == (ber_tlv_tag_t)-1) {
297 use_bsearch = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000298 break;
299 }
300 }
Lev Walkin4c36e302004-06-06 07:20:02 +0000301 if(use_bsearch) {
302 /*
Lev Walkin4e0704f2004-09-04 06:17:35 +0000303 * Resort to a binary search over
Lev Walkin4c36e302004-06-06 07:20:02 +0000304 * sorted array of tags.
305 */
Wim Lewisfb6344e2014-07-28 12:16:01 -0700306 const asn_TYPE_tag2member_t *t2m;
Lev Walkineff98a52017-09-13 22:24:35 +0000307 asn_TYPE_tag2member_t key = {0, 0, 0, 0};
308 key.el_tag = tlv_tag;
309 key.el_no = edx;
Wim Lewisfb6344e2014-07-28 12:16:01 -0700310 t2m = (const asn_TYPE_tag2member_t *)bsearch(&key,
Lev Walkinc2346572004-08-11 09:07:36 +0000311 specs->tag2el, specs->tag2el_count,
Lev Walkin4c36e302004-06-06 07:20:02 +0000312 sizeof(specs->tag2el[0]), _t2e_cmp);
Lev Walkin924fa032004-06-14 13:42:23 +0000313 if(t2m) {
Wim Lewisfb6344e2014-07-28 12:16:01 -0700314 const asn_TYPE_tag2member_t *best = 0;
315 const asn_TYPE_tag2member_t *t2m_f, *t2m_l;
Lev Walkin494fb702017-08-07 20:07:00 -0700316 size_t edx_max = edx + elements[edx].optional;
Lev Walkin4c36e302004-06-06 07:20:02 +0000317 /*
318 * Rewind to the first element with that tag,
319 * `cause bsearch() does not guarantee order.
320 */
Lev Walkin924fa032004-06-14 13:42:23 +0000321 t2m_f = t2m + t2m->toff_first;
322 t2m_l = t2m + t2m->toff_last;
323 for(t2m = t2m_f; t2m <= t2m_l; t2m++) {
324 if(t2m->el_no > edx_max) break;
325 if(t2m->el_no < edx) continue;
326 best = t2m;
327 }
328 if(best) {
329 edx = best->el_no;
330 ctx->step = 1 + 2 * edx;
331 goto microphase2;
332 }
Lev Walkin4c36e302004-06-06 07:20:02 +0000333 }
334 n = opt_edx_end;
335 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000336 if(n == opt_edx_end) {
337 /*
338 * If tag is unknown, it may be either
339 * an unknown (thus, incorrect) tag,
340 * or an extension (...),
341 * or an end of the indefinite-length structure.
342 */
Lev Walkin7cbbc902006-10-03 06:39:36 +0000343 if(!IN_EXTENSION_GROUP(specs,
344 edx + elements[edx].optional)) {
Lev Walkin9f470d62017-10-21 16:27:08 -0700345 ASN_DEBUG("Unexpected tag %s (at %" ASN_PRI_SIZE ")",
Lev Walkin566a5672004-10-05 06:36:57 +0000346 ber_tlv_tag_string(tlv_tag), edx);
Lev Walkin4c36e302004-06-06 07:20:02 +0000347 ASN_DEBUG("Expected tag %s (%s)%s",
Lev Walkinf15320b2004-06-03 03:38:44 +0000348 ber_tlv_tag_string(elements[edx].tag),
Lev Walkin4c36e302004-06-06 07:20:02 +0000349 elements[edx].name,
Lev Walkinf15320b2004-06-03 03:38:44 +0000350 elements[edx].optional
351 ?" or alternatives":"");
352 RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000353 } else {
354 /* Skip this tag */
355 ssize_t skip;
Lev Walkin7cbbc902006-10-03 06:39:36 +0000356 edx += elements[edx].optional;
Lev Walkinf15320b2004-06-03 03:38:44 +0000357
Lev Walkin9f470d62017-10-21 16:27:08 -0700358 ASN_DEBUG("Skipping unexpected %s (at %" ASN_PRI_SIZE ")",
Lev Walkin7cbbc902006-10-03 06:39:36 +0000359 ber_tlv_tag_string(tlv_tag), edx);
Lev Walkinbaaa24f2004-09-29 14:19:14 +0000360 skip = ber_skip_length(opt_codec_ctx,
Lev Walkinf15320b2004-06-03 03:38:44 +0000361 BER_TLV_CONSTRUCTED(ptr),
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800362 (const char *)ptr + tag_len,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000363 LEFT - tag_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000364 ASN_DEBUG("Skip length %d in %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000365 (int)skip, td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000366 switch(skip) {
367 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
368 /* Fall through */
369 case -1: RETURN(RC_FAIL);
370 }
371
372 ADVANCE(skip + tag_len);
373 ctx->step -= 2;
374 edx--;
375 continue; /* Try again with the next tag */
376 }
377 }
378
379 /*
380 * MICROPHASE 2: Invoke the member-specific decoder.
381 */
382 ctx->step |= 1; /* Confirm entering next microphase */
383 microphase2:
Lev Walkin449f8322004-08-20 13:23:42 +0000384 ASN_DEBUG("Inside SEQUENCE %s MF2", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000385
386 /*
387 * Compute the position of the member inside a structure,
388 * and also a type of containment (it may be contained
389 * as pointer or using inline inclusion).
390 */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000391 if(elements[edx].flags & ATF_POINTER) {
392 /* Member is a pointer to another structure */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800393 memb_ptr2 = (void **)((char *)st + elements[edx].memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000394 } else {
395 /*
396 * A pointer to a pointer
397 * holding the start of the structure
398 */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800399 memb_ptr = (char *)st + elements[edx].memb_offset;
Lev Walkinf15320b2004-06-03 03:38:44 +0000400 memb_ptr2 = &memb_ptr;
401 }
402 /*
403 * Invoke the member fetch routine according to member's type
404 */
Lev Walkin75b6fe42017-08-27 01:03:22 -0700405 if(elements[edx].flags & ATF_OPEN_TYPE) {
Lev Walkinc10d30a2017-08-27 00:38:21 -0700406 rval = OPEN_TYPE_ber_get(opt_codec_ctx, td, st, &elements[edx], ptr, LEFT);
407 } else {
408 rval = elements[edx].type->op->ber_decoder(opt_codec_ctx,
409 elements[edx].type,
410 memb_ptr2, ptr, LEFT,
411 elements[edx].tag_mode);
412 }
Lev Walkin9f470d62017-10-21 16:27:08 -0700413 ASN_DEBUG("In %s SEQUENCE decoded %" ASN_PRI_SIZE " %s of %d "
Lev Walkin5beb7de2004-09-24 20:59:27 +0000414 "in %d bytes rval.code %d, size=%d",
Lev Walkin449f8322004-08-20 13:23:42 +0000415 td->name, edx, elements[edx].type->name,
Lev Walkin5beb7de2004-09-24 20:59:27 +0000416 (int)LEFT, (int)rval.consumed, rval.code, (int)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000417 switch(rval.code) {
418 case RC_OK:
419 break;
420 case RC_WMORE: /* More data expected */
421 if(!SIZE_VIOLATION) {
422 ADVANCE(rval.consumed);
423 RETURN(RC_WMORE);
424 }
Lev Walkin5beb7de2004-09-24 20:59:27 +0000425 ASN_DEBUG("Size violation (c->l=%ld <= s=%ld)",
426 (long)ctx->left, (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000427 /* Fall through */
428 case RC_FAIL: /* Fatal error */
429 RETURN(RC_FAIL);
430 } /* switch(rval) */
431
432 ADVANCE(rval.consumed);
433 } /* for(all structure members) */
434
435 phase3:
436 ctx->phase = 3;
Lev Walkin48e82d12017-10-19 03:06:35 -0700437 /* Fall through */
Lev Walkinf15320b2004-06-03 03:38:44 +0000438 case 3: /* 00 and other tags expected */
439 case 4: /* only 00's expected */
440
441 ASN_DEBUG("SEQUENCE %s Leftover: %ld, size = %ld",
Lev Walkin449f8322004-08-20 13:23:42 +0000442 td->name, (long)ctx->left, (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000443
444 /*
445 * Skip everything until the end of the SEQUENCE.
446 */
447 while(ctx->left) {
448 ssize_t tl, ll;
449
450 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
451 switch(tl) {
452 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
453 /* Fall through */
454 case -1: RETURN(RC_FAIL);
455 }
456
457 /*
458 * If expected <0><0>...
459 */
460 if(ctx->left < 0
Lev Walkin8c3b8542005-03-10 18:52:02 +0000461 && ((const uint8_t *)ptr)[0] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000462 if(LEFT < 2) {
463 if(SIZE_VIOLATION)
464 RETURN(RC_FAIL);
465 else
466 RETURN(RC_WMORE);
Lev Walkin8c3b8542005-03-10 18:52:02 +0000467 } else if(((const uint8_t *)ptr)[1] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000468 /*
469 * Correctly finished with <0><0>.
470 */
471 ADVANCE(2);
472 ctx->left++;
473 ctx->phase = 4;
474 continue;
475 }
476 }
477
Lev Walkin449f8322004-08-20 13:23:42 +0000478 if(!IN_EXTENSION_GROUP(specs, td->elements_count)
Lev Walkinf15320b2004-06-03 03:38:44 +0000479 || ctx->phase == 4) {
480 ASN_DEBUG("Unexpected continuation "
481 "of a non-extensible type "
482 "%s (SEQUENCE): %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000483 td->name,
Lev Walkinf15320b2004-06-03 03:38:44 +0000484 ber_tlv_tag_string(tlv_tag));
485 RETURN(RC_FAIL);
486 }
487
Lev Walkinbaaa24f2004-09-29 14:19:14 +0000488 ll = ber_skip_length(opt_codec_ctx,
Lev Walkinf15320b2004-06-03 03:38:44 +0000489 BER_TLV_CONSTRUCTED(ptr),
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800490 (const char *)ptr + tl, LEFT - tl);
Lev Walkinf15320b2004-06-03 03:38:44 +0000491 switch(ll) {
492 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
493 /* Fall through */
494 case -1: RETURN(RC_FAIL);
495 }
496
497 ADVANCE(tl + ll);
498 }
499
500 PHASE_OUT(ctx);
501 }
502
503 RETURN(RC_OK);
504}
505
Lev Walkin4c36e302004-06-06 07:20:02 +0000506
Lev Walkinf15320b2004-06-03 03:38:44 +0000507/*
508 * The DER encoder of the SEQUENCE type.
509 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000510asn_enc_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -0700511SEQUENCE_encode_der(const asn_TYPE_descriptor_t *td, const void *sptr,
512 int tag_mode, ber_tlv_tag_t tag,
513 asn_app_consume_bytes_f *cb, void *app_key) {
514 size_t computed_size = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000515 asn_enc_rval_t erval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000516 ssize_t ret;
Lev Walkin494fb702017-08-07 20:07:00 -0700517 size_t edx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000518
519 ASN_DEBUG("%s %s as SEQUENCE",
Lev Walkin449f8322004-08-20 13:23:42 +0000520 cb?"Encoding":"Estimating", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000521
522 /*
523 * Gather the length of the underlying members sequence.
524 */
Lev Walkin449f8322004-08-20 13:23:42 +0000525 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000526 asn_TYPE_member_t *elm = &td->elements[edx];
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400527
Lev Walkin20696a42017-10-17 21:27:33 -0700528 const void *memb_ptr; /* Pointer to the member */
529 const void *const *memb_ptr2; /* Pointer to that pointer */
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400530
Lev Walkin20696a42017-10-17 21:27:33 -0700531 if(elm->flags & ATF_POINTER) {
532 memb_ptr2 =
533 (const void *const *)((const char *)sptr + elm->memb_offset);
534 if(!*memb_ptr2) {
Lev Walkin9f470d62017-10-21 16:27:08 -0700535 ASN_DEBUG("Element %s %" ASN_PRI_SIZE " not present",
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400536 elm->name, edx);
537 if(elm->optional)
538 continue;
Lev Walkinac589332005-08-22 14:19:28 +0000539 /* Mandatory element is missing */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700540 ASN__ENCODE_FAILED;
Lev Walkinac589332005-08-22 14:19:28 +0000541 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000542 } else {
Lev Walkin20696a42017-10-17 21:27:33 -0700543 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
544 memb_ptr2 = &memb_ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000545 }
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400546
547 /* Eliminate default values */
Lev Walkin20696a42017-10-17 21:27:33 -0700548 if(elm->default_value_cmp && elm->default_value_cmp(*memb_ptr2) == 0)
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400549 continue;
550
551 erval = elm->type->op->der_encoder(elm->type, *memb_ptr2,
Lev Walkinf15320b2004-06-03 03:38:44 +0000552 elm->tag_mode, elm->tag,
553 0, 0);
554 if(erval.encoded == -1)
555 return erval;
556 computed_size += erval.encoded;
Lev Walkin9f470d62017-10-21 16:27:08 -0700557 ASN_DEBUG("Member %" ASN_PRI_SIZE " %s estimated %ld bytes",
Lev Walkinf15320b2004-06-03 03:38:44 +0000558 edx, elm->name, (long)erval.encoded);
559 }
560
561 /*
562 * Encode the TLV for the sequence itself.
563 */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000564 ret = der_write_tags(td, computed_size, tag_mode, 1, tag, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +0000565 ASN_DEBUG("Wrote tags: %ld (+%ld)", (long)ret, (long)computed_size);
Lev Walkinac589332005-08-22 14:19:28 +0000566 if(ret == -1)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700567 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000568 erval.encoded = computed_size + ret;
569
Lev Walkin7c1dc052016-03-14 03:08:15 -0700570 if(!cb) ASN__ENCODED_OK(erval);
Lev Walkinf15320b2004-06-03 03:38:44 +0000571
572 /*
573 * Encode all members.
574 */
Lev Walkin449f8322004-08-20 13:23:42 +0000575 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000576 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkina9cc46e2004-09-22 16:06:28 +0000577 asn_enc_rval_t tmperval;
Lev Walkin20696a42017-10-17 21:27:33 -0700578 const void *memb_ptr; /* Pointer to the member */
579 const void *const *memb_ptr2; /* Pointer to that pointer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000580
Lev Walkin20696a42017-10-17 21:27:33 -0700581 if(elm->flags & ATF_POINTER) {
582 memb_ptr2 =
583 (const void *const *)((const char *)sptr + elm->memb_offset);
584 if(!*memb_ptr2) continue;
Lev Walkinf15320b2004-06-03 03:38:44 +0000585 } else {
Lev Walkin20696a42017-10-17 21:27:33 -0700586 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
587 memb_ptr2 = &memb_ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000588 }
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400589
590 /* Eliminate default values */
Lev Walkin20696a42017-10-17 21:27:33 -0700591 if(elm->default_value_cmp && elm->default_value_cmp(*memb_ptr2) == 0)
592 continue;
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400593
594 tmperval = elm->type->op->der_encoder(elm->type, *memb_ptr2,
595 elm->tag_mode, elm->tag, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +0000596 if(tmperval.encoded == -1)
597 return tmperval;
598 computed_size -= tmperval.encoded;
Lev Walkin9f470d62017-10-21 16:27:08 -0700599 ASN_DEBUG("Member %" ASN_PRI_SIZE " %s of SEQUENCE %s encoded in %ld bytes",
Lev Walkina6a926a2005-03-02 01:54:28 +0000600 edx, elm->name, td->name, (long)tmperval.encoded);
Lev Walkinf15320b2004-06-03 03:38:44 +0000601 }
602
Lev Walkinac589332005-08-22 14:19:28 +0000603 if(computed_size != 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000604 /*
605 * Encoded size is not equal to the computed size.
606 */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700607 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000608
Lev Walkin7c1dc052016-03-14 03:08:15 -0700609 ASN__ENCODED_OK(erval);
Lev Walkinf15320b2004-06-03 03:38:44 +0000610}
611
Lev Walkinc61f3862005-02-14 17:21:22 +0000612
Lev Walkin1bbc2002004-10-23 13:27:30 +0000613#undef XER_ADVANCE
Lev Walkinf59dac92017-08-26 21:26:51 -0700614#define XER_ADVANCE(num_bytes) \
615 do { \
616 size_t num = (num_bytes); \
617 ptr = ((const char *)ptr) + num; \
618 size -= num; \
619 consumed_myself += num; \
620 } while(0)
Lev Walkin1bbc2002004-10-23 13:27:30 +0000621
Lev Walkinc61f3862005-02-14 17:21:22 +0000622/*
623 * Decode the XER (XML) data.
624 */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000625asn_dec_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -0700626SEQUENCE_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
627 const asn_TYPE_descriptor_t *td, void **struct_ptr,
628 const char *opt_mname, const void *ptr, size_t size) {
629 /*
Lev Walkin1bbc2002004-10-23 13:27:30 +0000630 * Bring closer parts of structure description.
631 */
Lev Walkind84f6032017-10-03 16:33:59 -0700632 const asn_SEQUENCE_specifics_t *specs
633 = (const asn_SEQUENCE_specifics_t *)td->specifics;
Lev Walkin1bbc2002004-10-23 13:27:30 +0000634 asn_TYPE_member_t *elements = td->elements;
635 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
636
637 /*
Lev Walkinc61f3862005-02-14 17:21:22 +0000638 * ... and parts of the structure being constructed.
Lev Walkin1bbc2002004-10-23 13:27:30 +0000639 */
640 void *st = *struct_ptr; /* Target structure. */
641 asn_struct_ctx_t *ctx; /* Decoder context */
642
Lev Walkinc61f3862005-02-14 17:21:22 +0000643 asn_dec_rval_t rval; /* Return value from a decoder */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000644 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
Lev Walkin494fb702017-08-07 20:07:00 -0700645 size_t edx; /* Element index */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000646
647 /*
648 * Create the target structure if it is not present already.
649 */
650 if(st == 0) {
651 st = *struct_ptr = CALLOC(1, specs->struct_size);
652 if(st == 0) RETURN(RC_FAIL);
653 }
654
655 /*
656 * Restore parsing context.
657 */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800658 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkin1bbc2002004-10-23 13:27:30 +0000659
660
661 /*
662 * Phases of XER/XML processing:
663 * Phase 0: Check that the opening tag matches our expectations.
664 * Phase 1: Processing body and reacting on closing tag.
665 * Phase 2: Processing inner type.
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000666 * Phase 3: Skipping unknown extensions.
667 * Phase 4: PHASED OUT
Lev Walkin1bbc2002004-10-23 13:27:30 +0000668 */
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000669 for(edx = ctx->step; ctx->phase <= 3;) {
Lev Walkin1bbc2002004-10-23 13:27:30 +0000670 pxer_chunk_type_e ch_type; /* XER chunk type */
671 ssize_t ch_size; /* Chunk size */
672 xer_check_tag_e tcv; /* Tag check value */
673 asn_TYPE_member_t *elm;
674
675 /*
Lev Walkinc61f3862005-02-14 17:21:22 +0000676 * Go inside the inner member of a sequence.
Lev Walkin1bbc2002004-10-23 13:27:30 +0000677 */
678 if(ctx->phase == 2) {
679 asn_dec_rval_t tmprval;
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400680 void *memb_ptr_dontuse; /* Pointer to the member */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000681 void **memb_ptr2; /* Pointer to that pointer */
682
Lev Walkinabf68892004-10-26 10:12:14 +0000683 elm = &td->elements[edx];
684
Lev Walkin1bbc2002004-10-23 13:27:30 +0000685 if(elm->flags & ATF_POINTER) {
686 /* Member is a pointer to another structure */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800687 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
Lev Walkin1bbc2002004-10-23 13:27:30 +0000688 } else {
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400689 memb_ptr_dontuse = (char *)st + elm->memb_offset;
690 memb_ptr2 = &memb_ptr_dontuse; /* Only use of memb_ptr_dontuse */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000691 }
692
Lev Walkin75b6fe42017-08-27 01:03:22 -0700693 if(elm->flags & ATF_OPEN_TYPE) {
Lev Walkinf59dac92017-08-26 21:26:51 -0700694 tmprval = OPEN_TYPE_xer_get(opt_codec_ctx, td, st, elm, ptr, size);
695 } else {
696 /* Invoke the inner type decoder, m.b. multiple times */
697 tmprval = elm->type->op->xer_decoder(opt_codec_ctx,
698 elm->type, memb_ptr2, elm->name,
699 ptr, size);
700 }
Lev Walkin1bbc2002004-10-23 13:27:30 +0000701 XER_ADVANCE(tmprval.consumed);
702 if(tmprval.code != RC_OK)
703 RETURN(tmprval.code);
Lev Walkinc61f3862005-02-14 17:21:22 +0000704 ctx->phase = 1; /* Back to body processing */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000705 ctx->step = ++edx;
Lev Walkinc61f3862005-02-14 17:21:22 +0000706 ASN_DEBUG("XER/SEQUENCE phase => %d, step => %d",
707 ctx->phase, ctx->step);
Lev Walkin1bbc2002004-10-23 13:27:30 +0000708 /* Fall through */
709 }
710
711 /*
712 * Get the next part of the XML stream.
713 */
Lev Walkinf59dac92017-08-26 21:26:51 -0700714 ch_size = xer_next_token(&ctx->context, ptr, size,
Lev Walkin1e443962005-02-18 18:06:36 +0000715 &ch_type);
Lev Walkin97363482016-01-24 19:23:02 -0800716 if(ch_size == -1) {
717 RETURN(RC_FAIL);
Lev Walkinf59dac92017-08-26 21:26:51 -0700718 } else {
Lev Walkin1bbc2002004-10-23 13:27:30 +0000719 switch(ch_type) {
Lev Walkinf59dac92017-08-26 21:26:51 -0700720 case PXER_WMORE:
721 RETURN(RC_WMORE);
Lev Walkin1bbc2002004-10-23 13:27:30 +0000722 case PXER_COMMENT: /* Got XML comment */
723 case PXER_TEXT: /* Ignore free-standing text */
724 XER_ADVANCE(ch_size); /* Skip silently */
725 continue;
726 case PXER_TAG:
727 break; /* Check the rest down there */
728 }
729 }
730
Lev Walkinf59dac92017-08-26 21:26:51 -0700731 tcv = xer_check_tag(ptr, ch_size, xml_tag);
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000732 ASN_DEBUG("XER/SEQUENCE: tcv = %d, ph=%d [%s]",
733 tcv, ctx->phase, xml_tag);
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000734
735 /* Skip the extensions section */
736 if(ctx->phase == 3) {
737 switch(xer_skip_unknown(tcv, &ctx->left)) {
738 case -1:
739 ctx->phase = 4;
740 RETURN(RC_FAIL);
741 case 0:
742 XER_ADVANCE(ch_size);
743 continue;
744 case 1:
745 XER_ADVANCE(ch_size);
746 ctx->phase = 1;
747 continue;
748 case 2:
749 ctx->phase = 1;
750 break;
751 }
752 }
753
Lev Walkin1bbc2002004-10-23 13:27:30 +0000754 switch(tcv) {
755 case XCT_CLOSING:
756 if(ctx->phase == 0) break;
Lev Walkin1bbc2002004-10-23 13:27:30 +0000757 ctx->phase = 0;
758 /* Fall through */
759 case XCT_BOTH:
Lev Walkin83668612017-10-21 00:24:31 -0700760 if(ctx->phase == 0) {
761 if(edx >= td->elements_count ||
762 /* Explicit OPTIONAL specs reaches the end */
763 (edx + elements[edx].optional == td->elements_count) ||
764 /* All extensions are optional */
765 IN_EXTENSION_GROUP(specs, edx)) {
766 XER_ADVANCE(ch_size);
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000767 ctx->phase = 4; /* Phase out */
Lev Walkinc61f3862005-02-14 17:21:22 +0000768 RETURN(RC_OK);
Lev Walkin1bbc2002004-10-23 13:27:30 +0000769 } else {
770 ASN_DEBUG("Premature end of XER SEQUENCE");
771 RETURN(RC_FAIL);
772 }
773 }
774 /* Fall through */
775 case XCT_OPENING:
776 if(ctx->phase == 0) {
777 XER_ADVANCE(ch_size);
778 ctx->phase = 1; /* Processing body phase */
779 continue;
780 }
Lev Walkinda3ca412004-10-23 14:58:15 +0000781 /* Fall through */
Lev Walkin904e65b2005-02-18 14:23:48 +0000782 case XCT_UNKNOWN_OP:
783 case XCT_UNKNOWN_BO:
Lev Walkin1bbc2002004-10-23 13:27:30 +0000784
Lev Walkin9f470d62017-10-21 16:27:08 -0700785 ASN_DEBUG("XER/SEQUENCE: tcv=%d, ph=%d, edx=%" ASN_PRI_SIZE "",
Lev Walkin02666192005-07-03 05:31:02 +0000786 tcv, ctx->phase, edx);
787 if(ctx->phase != 1) {
Lev Walkin1bbc2002004-10-23 13:27:30 +0000788 break; /* Really unexpected */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000789 }
Lev Walkin02666192005-07-03 05:31:02 +0000790
791 if(edx < td->elements_count) {
792 /*
793 * Search which member corresponds to this tag.
794 */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800795 size_t n;
796 size_t edx_end = edx + elements[edx].optional + 1;
Lev Walkin02666192005-07-03 05:31:02 +0000797 if(edx_end > td->elements_count)
798 edx_end = td->elements_count;
799 for(n = edx; n < edx_end; n++) {
800 elm = &td->elements[n];
Lev Walkinf59dac92017-08-26 21:26:51 -0700801 tcv = xer_check_tag(ptr, ch_size, elm->name);
Lev Walkin02666192005-07-03 05:31:02 +0000802 switch(tcv) {
803 case XCT_BOTH:
804 case XCT_OPENING:
805 /*
806 * Process this member.
807 */
808 ctx->step = edx = n;
809 ctx->phase = 2;
810 break;
811 case XCT_UNKNOWN_OP:
812 case XCT_UNKNOWN_BO:
813 continue;
814 default:
815 n = edx_end;
816 break; /* Phase out */
817 }
818 break;
819 }
820 if(n != edx_end)
821 continue;
822 } else {
Lev Walkin9f470d62017-10-21 16:27:08 -0700823 ASN_DEBUG("Out of defined members: %" ASN_PRI_SIZE "/%u",
Lev Walkin02666192005-07-03 05:31:02 +0000824 edx, td->elements_count);
825 }
Lev Walkin904e65b2005-02-18 14:23:48 +0000826
827 /* It is expected extension */
828 if(IN_EXTENSION_GROUP(specs,
Lev Walkin02666192005-07-03 05:31:02 +0000829 edx + (edx < td->elements_count
830 ? elements[edx].optional : 0))) {
Lev Walkin9f470d62017-10-21 16:27:08 -0700831 ASN_DEBUG("Got anticipated extension at %" ASN_PRI_SIZE "",
Lev Walkin02666192005-07-03 05:31:02 +0000832 edx);
Lev Walkin904e65b2005-02-18 14:23:48 +0000833 /*
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000834 * Check for (XCT_BOTH or XCT_UNKNOWN_BO)
835 * By using a mask. Only record a pure
836 * <opening> tags.
Lev Walkin904e65b2005-02-18 14:23:48 +0000837 */
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000838 if(tcv & XCT_CLOSING) {
839 /* Found </extension> without body */
840 } else {
841 ctx->left = 1;
842 ctx->phase = 3; /* Skip ...'s */
843 }
844 XER_ADVANCE(ch_size);
845 continue;
Lev Walkin904e65b2005-02-18 14:23:48 +0000846 }
847
848 /* Fall through */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000849 default:
850 break;
851 }
852
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000853 ASN_DEBUG("Unexpected XML tag in SEQUENCE [%c%c%c%c%c%c]",
Lev Walkinf59dac92017-08-26 21:26:51 -0700854 size>0?((const char *)ptr)[0]:'.',
855 size>1?((const char *)ptr)[1]:'.',
856 size>2?((const char *)ptr)[2]:'.',
857 size>3?((const char *)ptr)[3]:'.',
858 size>4?((const char *)ptr)[4]:'.',
859 size>5?((const char *)ptr)[5]:'.');
Lev Walkin1bbc2002004-10-23 13:27:30 +0000860 break;
861 }
862
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000863 ctx->phase = 4; /* "Phase out" on hard failure */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000864 RETURN(RC_FAIL);
865}
866
Lev Walkina9cc46e2004-09-22 16:06:28 +0000867asn_enc_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -0700868SEQUENCE_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr,
869 int ilevel, enum xer_encoder_flags_e flags,
870 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin28ba9db2017-08-31 02:06:58 -0700871 asn_enc_rval_t er;
872 int xcan = (flags & XER_F_CANONICAL);
873 asn_TYPE_descriptor_t *tmp_def_val_td = 0;
874 void *tmp_def_val = 0;
Lev Walkin494fb702017-08-07 20:07:00 -0700875 size_t edx;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000876
Lev Walkin28ba9db2017-08-31 02:06:58 -0700877 if(!sptr) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000878
Lev Walkin28ba9db2017-08-31 02:06:58 -0700879 er.encoded = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000880
Lev Walkin28ba9db2017-08-31 02:06:58 -0700881 for(edx = 0; edx < td->elements_count; edx++) {
882 asn_enc_rval_t tmper;
883 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkin20696a42017-10-17 21:27:33 -0700884 const void *memb_ptr;
Lev Walkin28ba9db2017-08-31 02:06:58 -0700885 const char *mname = elm->name;
886 unsigned int mlen = strlen(mname);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000887
Lev Walkin28ba9db2017-08-31 02:06:58 -0700888 if(elm->flags & ATF_POINTER) {
Lev Walkin20696a42017-10-17 21:27:33 -0700889 memb_ptr =
890 *(const void *const *)((const char *)sptr + elm->memb_offset);
Lev Walkin28ba9db2017-08-31 02:06:58 -0700891 if(!memb_ptr) {
892 assert(tmp_def_val == 0);
Lev Walkin20696a42017-10-17 21:27:33 -0700893 if(elm->default_value_set) {
894 if(elm->default_value_set(&tmp_def_val)) {
Lev Walkin28ba9db2017-08-31 02:06:58 -0700895 ASN__ENCODE_FAILED;
896 } else {
897 memb_ptr = tmp_def_val;
898 tmp_def_val_td = elm->type;
899 }
900 } else if(elm->optional) {
901 continue;
902 } else {
903 /* Mandatory element is missing */
904 ASN__ENCODE_FAILED;
905 }
906 }
907 } else {
Lev Walkin20696a42017-10-17 21:27:33 -0700908 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
Lev Walkin28ba9db2017-08-31 02:06:58 -0700909 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000910
Lev Walkin28ba9db2017-08-31 02:06:58 -0700911 if(!xcan) ASN__TEXT_INDENT(1, ilevel);
912 ASN__CALLBACK3("<", 1, mname, mlen, ">", 1);
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400913
Lev Walkin28ba9db2017-08-31 02:06:58 -0700914 /* Print the member itself */
915 tmper = elm->type->op->xer_encoder(elm->type, memb_ptr, ilevel + 1,
916 flags, cb, app_key);
917 if(tmp_def_val) {
918 ASN_STRUCT_FREE(*tmp_def_val_td, tmp_def_val);
919 tmp_def_val = 0;
920 }
921 if(tmper.encoded == -1) return tmper;
Lev Walkin4fe28822017-09-18 02:57:34 -0700922 er.encoded += tmper.encoded;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000923
Lev Walkin28ba9db2017-08-31 02:06:58 -0700924 ASN__CALLBACK3("</", 2, mname, mlen, ">", 1);
Lev Walkin28ba9db2017-08-31 02:06:58 -0700925 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000926
Lev Walkin28ba9db2017-08-31 02:06:58 -0700927 if(!xcan) ASN__TEXT_INDENT(1, ilevel - 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000928
Lev Walkin28ba9db2017-08-31 02:06:58 -0700929 ASN__ENCODED_OK(er);
Lev Walkin942fd082004-10-03 09:13:02 +0000930cb_failed:
Lev Walkin28ba9db2017-08-31 02:06:58 -0700931 if(tmp_def_val) ASN_STRUCT_FREE(*tmp_def_val_td, tmp_def_val);
932 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000933}
934
Lev Walkinf15320b2004-06-03 03:38:44 +0000935int
Lev Walkin20696a42017-10-17 21:27:33 -0700936SEQUENCE_print(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
937 asn_app_consume_bytes_f *cb, void *app_key) {
938 size_t edx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000939 int ret;
940
Lev Walkin8e8078a2004-09-26 13:10:40 +0000941 if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000942
943 /* Dump preamble */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000944 if(cb(td->name, strlen(td->name), app_key) < 0
945 || cb(" ::= {", 6, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000946 return -1;
947
Lev Walkin449f8322004-08-20 13:23:42 +0000948 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000949 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000950 const void *memb_ptr;
951
Lev Walkincc93b0f2004-09-10 09:18:20 +0000952 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800953 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkinac589332005-08-22 14:19:28 +0000954 if(!memb_ptr) {
955 if(elm->optional) continue;
956 /* Print <absent> line */
957 /* Fall through */
958 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000959 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800960 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000961 }
962
963 /* Indentation */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000964 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000965
966 /* Print the member's name and stuff */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000967 if(cb(elm->name, strlen(elm->name), app_key) < 0
968 || cb(": ", 2, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000969 return -1;
970
971 /* Print the member itself */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800972 ret = elm->type->op->print_struct(elm->type, memb_ptr, ilevel + 1,
Lev Walkinf15320b2004-06-03 03:38:44 +0000973 cb, app_key);
974 if(ret) return ret;
Lev Walkinf15320b2004-06-03 03:38:44 +0000975 }
976
Lev Walkin8e8078a2004-09-26 13:10:40 +0000977 ilevel--;
978 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000979
Lev Walkin8e8078a2004-09-26 13:10:40 +0000980 return (cb("}", 1, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000981}
982
983void
Lev Walkin8d99d7b2017-08-25 01:06:00 -0700984SEQUENCE_free(const asn_TYPE_descriptor_t *td, void *sptr,
985 enum asn_struct_free_method method) {
Lev Walkinf6853ce2017-08-11 00:50:27 -0700986 size_t edx;
Vasil Velichkovaead8c52017-10-10 05:34:48 +0300987 const asn_SEQUENCE_specifics_t *specs =
988 (const asn_SEQUENCE_specifics_t *)td->specifics;
989 asn_struct_ctx_t *ctx; /* Decoder context */
Lev Walkinf15320b2004-06-03 03:38:44 +0000990
991 if(!td || !sptr)
992 return;
993
994 ASN_DEBUG("Freeing %s as SEQUENCE", td->name);
995
Lev Walkin449f8322004-08-20 13:23:42 +0000996 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000997 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000998 void *memb_ptr;
Lev Walkincc93b0f2004-09-10 09:18:20 +0000999 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001000 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +00001001 if(memb_ptr)
Lev Walkinadcb5862006-03-17 02:11:12 +00001002 ASN_STRUCT_FREE(*elm->type, memb_ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +00001003 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001004 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkinadcb5862006-03-17 02:11:12 +00001005 ASN_STRUCT_FREE_CONTENTS_ONLY(*elm->type, memb_ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +00001006 }
1007 }
1008
Vasil Velichkovaead8c52017-10-10 05:34:48 +03001009 /* Clean parsing context */
1010 ctx = (asn_struct_ctx_t *)((char *)sptr + specs->ctx_offset);
1011 FREEMEM(ctx->ptr);
1012
Lev Walkin8d99d7b2017-08-25 01:06:00 -07001013 switch(method) {
1014 case ASFM_FREE_EVERYTHING:
1015 FREEMEM(sptr);
1016 break;
1017 case ASFM_FREE_UNDERLYING:
1018 break;
1019 case ASFM_FREE_UNDERLYING_AND_RESET:
Lev Walkind84f6032017-10-03 16:33:59 -07001020 memset(
1021 sptr, 0,
1022 ((const asn_SEQUENCE_specifics_t *)(td->specifics))->struct_size);
Lev Walkin8d99d7b2017-08-25 01:06:00 -07001023 break;
1024 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001025}
1026
1027int
Lev Walkin20696a42017-10-17 21:27:33 -07001028SEQUENCE_constraint(const asn_TYPE_descriptor_t *td, const void *sptr,
1029 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
1030 size_t edx;
Lev Walkinf15320b2004-06-03 03:38:44 +00001031
1032 if(!sptr) {
Lev Walkin7c1dc052016-03-14 03:08:15 -07001033 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +00001034 "%s: value not given (%s:%d)",
1035 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +00001036 return -1;
1037 }
1038
1039 /*
1040 * Iterate over structure members and check their validity.
1041 */
Lev Walkin449f8322004-08-20 13:23:42 +00001042 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +00001043 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +00001044 const void *memb_ptr;
1045
Lev Walkincc93b0f2004-09-10 09:18:20 +00001046 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001047 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkinac589332005-08-22 14:19:28 +00001048 if(!memb_ptr) {
1049 if(elm->optional)
1050 continue;
Lev Walkin7c1dc052016-03-14 03:08:15 -07001051 ASN__CTFAIL(app_key, td, sptr,
Lev Walkinac589332005-08-22 14:19:28 +00001052 "%s: mandatory element %s absent (%s:%d)",
1053 td->name, elm->name, __FILE__, __LINE__);
1054 return -1;
1055 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001056 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001057 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +00001058 }
1059
Lev Walkina5972be2017-09-29 23:15:58 -07001060 if(elm->encoding_constraints.general_constraints) {
1061 int ret = elm->encoding_constraints.general_constraints(elm->type, memb_ptr,
Lev Walkin1eded352006-07-13 11:19:01 +00001062 ctfailcb, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +00001063 if(ret) return ret;
1064 } else {
Lev Walkina5972be2017-09-29 23:15:58 -07001065 return elm->type->encoding_constraints.general_constraints(elm->type,
Lev Walkin1eded352006-07-13 11:19:01 +00001066 memb_ptr, ctfailcb, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +00001067 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001068 }
1069
1070 return 0;
1071}
Lev Walkin59b176e2005-11-26 11:25:14 +00001072
Lev Walkin24810022017-08-25 12:16:11 -07001073#ifndef ASN_DISABLE_PER_SUPPORT
1074
Lev Walkin59b176e2005-11-26 11:25:14 +00001075asn_dec_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -07001076SEQUENCE_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
1077 const asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -07001078 const asn_per_constraints_t *constraints, void **sptr,
1079 asn_per_data_t *pd) {
Lev Walkin20696a42017-10-17 21:27:33 -07001080 const asn_SEQUENCE_specifics_t *specs = (const asn_SEQUENCE_specifics_t *)td->specifics;
Lev Walkin59b176e2005-11-26 11:25:14 +00001081 void *st = *sptr; /* Target structure. */
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001082 int extpresent; /* Extension additions are present */
Lev Walkin59b176e2005-11-26 11:25:14 +00001083 uint8_t *opres; /* Presence of optional root members */
1084 asn_per_data_t opmd;
1085 asn_dec_rval_t rv;
Lev Walkin494fb702017-08-07 20:07:00 -07001086 size_t edx;
Lev Walkin59b176e2005-11-26 11:25:14 +00001087
1088 (void)constraints;
1089
Lev Walkin7c1dc052016-03-14 03:08:15 -07001090 if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx))
1091 ASN__DECODE_FAILED;
Lev Walkin1d9e8dd2005-12-07 05:46:03 +00001092
Lev Walkin59b176e2005-11-26 11:25:14 +00001093 if(!st) {
1094 st = *sptr = CALLOC(1, specs->struct_size);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001095 if(!st) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +00001096 }
1097
1098 ASN_DEBUG("Decoding %s as SEQUENCE (UPER)", td->name);
1099
1100 /* Handle extensions */
Lev Walkin83668612017-10-21 00:24:31 -07001101 if(specs->first_extension < 0) {
1102 extpresent = 0;
1103 } else {
Lev Walkin59b176e2005-11-26 11:25:14 +00001104 extpresent = per_get_few_bits(pd, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001105 if(extpresent < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +00001106 }
1107
1108 /* Prepare a place and read-in the presence bitmap */
Lev Walkin73a5cc62007-06-24 08:45:31 +00001109 memset(&opmd, 0, sizeof(opmd));
Lev Walkin59b176e2005-11-26 11:25:14 +00001110 if(specs->roms_count) {
1111 opres = (uint8_t *)MALLOC(((specs->roms_count + 7) >> 3) + 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001112 if(!opres) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +00001113 /* Get the presence map */
1114 if(per_get_many_bits(pd, opres, 0, specs->roms_count)) {
1115 FREEMEM(opres);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001116 ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +00001117 }
1118 opmd.buffer = opres;
Lev Walkin59b176e2005-11-26 11:25:14 +00001119 opmd.nbits = specs->roms_count;
1120 ASN_DEBUG("Read in presence bitmap for %s of %d bits (%x..)",
1121 td->name, specs->roms_count, *opres);
1122 } else {
1123 opres = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +00001124 }
1125
1126 /*
1127 * Get the sequence ROOT elements.
1128 */
Lev Walkin83668612017-10-21 00:24:31 -07001129 for(edx = 0;
1130 edx < (specs->first_extension < 0 ? td->elements_count
1131 : (size_t)specs->first_extension);
1132 edx++) {
1133 asn_TYPE_member_t *elm = &td->elements[edx];
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001134 void *memb_ptr; /* Pointer to the member */
Lev Walkin59b176e2005-11-26 11:25:14 +00001135 void **memb_ptr2; /* Pointer to that pointer */
1136
Lev Walkin83668612017-10-21 00:24:31 -07001137 assert(!IN_EXTENSION_GROUP(specs, edx));
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001138
Lev Walkin59b176e2005-11-26 11:25:14 +00001139 /* Fetch the pointer to this member */
1140 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001141 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
Lev Walkin59b176e2005-11-26 11:25:14 +00001142 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001143 memb_ptr = (char *)st + elm->memb_offset;
Lev Walkin59b176e2005-11-26 11:25:14 +00001144 memb_ptr2 = &memb_ptr;
1145 }
1146
1147 /* Deal with optionality */
1148 if(elm->optional) {
1149 int present = per_get_few_bits(&opmd, 1);
1150 ASN_DEBUG("Member %s->%s is optional, p=%d (%d->%d)",
1151 td->name, elm->name, present,
1152 (int)opmd.nboff, (int)opmd.nbits);
1153 if(present == 0) {
1154 /* This element is not present */
Lev Walkin20696a42017-10-17 21:27:33 -07001155 if(elm->default_value_set) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001156 /* Fill-in DEFAULT */
Lev Walkin20696a42017-10-17 21:27:33 -07001157 if(elm->default_value_set(memb_ptr2)) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001158 FREEMEM(opres);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001159 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +00001160 }
Lev Walkin8032f7a2007-06-27 01:54:57 +00001161 ASN_DEBUG("Filled-in default");
Lev Walkin59b176e2005-11-26 11:25:14 +00001162 }
1163 /* The member is just not present */
1164 continue;
1165 }
1166 /* Fall through */
1167 }
1168
1169 /* Fetch the member from the stream */
Lev Walkinf6853ce2017-08-11 00:50:27 -07001170 ASN_DEBUG("Decoding member \"%s\" in %s", elm->name, td->name);
Lev Walkin9de6cd82017-08-10 05:47:46 -07001171
Lev Walkin75b6fe42017-08-27 01:03:22 -07001172 if(elm->flags & ATF_OPEN_TYPE) {
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001173 rv = OPEN_TYPE_uper_get(opt_codec_ctx, td, st, elm, pd);
1174 } else {
Lev Walkinf59dac92017-08-26 21:26:51 -07001175 rv = elm->type->op->uper_decoder(opt_codec_ctx, elm->type,
Lev Walkina5972be2017-09-29 23:15:58 -07001176 elm->encoding_constraints.per_constraints, memb_ptr2, pd);
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001177 }
1178 if(rv.code != RC_OK) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001179 ASN_DEBUG("Failed decode %s in %s",
1180 elm->name, td->name);
1181 FREEMEM(opres);
1182 return rv;
1183 }
1184 }
1185
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001186 /* Optionality map is not needed anymore */
1187 FREEMEM(opres);
1188
Lev Walkin59b176e2005-11-26 11:25:14 +00001189 /*
1190 * Deal with extensions.
1191 */
1192 if(extpresent) {
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001193 ssize_t bmlength;
1194 uint8_t *epres; /* Presence of extension members */
1195 asn_per_data_t epmd;
Lev Walkin59b176e2005-11-26 11:25:14 +00001196
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001197 bmlength = uper_get_nslength(pd);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001198 if(bmlength < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +00001199
Lev Walkin9f470d62017-10-21 16:27:08 -07001200 ASN_DEBUG("Extensions %" ASN_PRI_SSIZE " present in %s", bmlength, td->name);
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001201
1202 epres = (uint8_t *)MALLOC((bmlength + 15) >> 3);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001203 if(!epres) ASN__DECODE_STARVED;
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001204
1205 /* Get the extensions map */
Simo Sorcece71d912015-09-03 16:34:20 -04001206 if(per_get_many_bits(pd, epres, 0, bmlength)) {
1207 FREEMEM(epres);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001208 ASN__DECODE_STARVED;
Simo Sorcece71d912015-09-03 16:34:20 -04001209 }
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001210
Lev Walkin73a5cc62007-06-24 08:45:31 +00001211 memset(&epmd, 0, sizeof(epmd));
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001212 epmd.buffer = epres;
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001213 epmd.nbits = bmlength;
Lev Walkinaaae9bf2010-10-24 16:56:30 -07001214 ASN_DEBUG("Read in extensions bitmap for %s of %ld bits (%x..)",
1215 td->name, (long)bmlength, *epres);
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001216
1217 /* Go over extensions and read them in */
Lev Walkin83668612017-10-21 00:24:31 -07001218 for(edx = specs->first_extension; edx < td->elements_count; 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 int present;
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001223
Lev Walkin83668612017-10-21 00:24:31 -07001224 /* Fetch the pointer to this member */
1225 if(elm->flags & ATF_POINTER) {
1226 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
1227 } else {
1228 memb_ptr = (void *)((char *)st + elm->memb_offset);
1229 memb_ptr2 = &memb_ptr;
1230 }
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001231
Lev Walkin83668612017-10-21 00:24:31 -07001232 present = per_get_few_bits(&epmd, 1);
1233 if(present <= 0) {
1234 if(present < 0) break; /* No more extensions */
1235 continue;
1236 }
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001237
Lev Walkin83668612017-10-21 00:24:31 -07001238 ASN_DEBUG("Decoding member %s in %s %p", elm->name, td->name,
1239 *memb_ptr2);
1240 rv = uper_open_type_get(opt_codec_ctx, elm->type,
1241 elm->encoding_constraints.per_constraints,
1242 memb_ptr2, pd);
1243 if(rv.code != RC_OK) {
1244 FREEMEM(epres);
1245 return rv;
1246 }
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001247 }
1248
1249 /* Skip over overflow extensions which aren't present
1250 * in this system's version of the protocol */
Lev Walkin06230f72007-06-26 09:52:44 +00001251 for(;;) {
Lev Walkina2987ea2007-06-26 23:56:54 +00001252 ASN_DEBUG("Getting overflow extensions");
Lev Walkin06230f72007-06-26 09:52:44 +00001253 switch(per_get_few_bits(&epmd, 1)) {
1254 case -1: break;
1255 case 0: continue;
1256 default:
Lev Walkin9218bc12007-06-27 04:09:37 +00001257 if(uper_open_type_skip(opt_codec_ctx, pd)) {
Lev Walkin06230f72007-06-26 09:52:44 +00001258 FREEMEM(epres);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001259 ASN__DECODE_STARVED;
Lev Walkin06230f72007-06-26 09:52:44 +00001260 }
Lev Walkin83668612017-10-21 00:24:31 -07001261 ASN_DEBUG("Skipped overflow extension");
1262 continue;
Lev Walkin59b176e2005-11-26 11:25:14 +00001263 }
Lev Walkin06230f72007-06-26 09:52:44 +00001264 break;
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001265 }
Lev Walkin59b176e2005-11-26 11:25:14 +00001266
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001267 FREEMEM(epres);
1268 }
1269
Lev Walkin83668612017-10-21 00:24:31 -07001270 if(specs->first_extension >= 0) {
1271 unsigned i;
1272 /* Fill DEFAULT members in extensions */
1273 for(i = specs->roms_count; i < specs->roms_count + specs->aoms_count;
1274 i++) {
1275 asn_TYPE_member_t *elm;
1276 void **memb_ptr2; /* Pointer to member pointer */
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001277
Lev Walkin83668612017-10-21 00:24:31 -07001278 edx = specs->oms[i];
1279 elm = &td->elements[edx];
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001280
Lev Walkin83668612017-10-21 00:24:31 -07001281 if(!elm->default_value_set) continue;
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001282
Lev Walkin83668612017-10-21 00:24:31 -07001283 /* Fetch the pointer to this member */
1284 if(elm->flags & ATF_POINTER) {
1285 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
1286 if(*memb_ptr2) continue;
1287 } else {
1288 continue; /* Extensions are all optionals */
1289 }
1290
1291 /* Set default value */
1292 if(elm->default_value_set(memb_ptr2)) {
1293 ASN__DECODE_FAILED;
1294 }
1295 }
1296 }
Lev Walkin59b176e2005-11-26 11:25:14 +00001297
1298 rv.consumed = 0;
1299 rv.code = RC_OK;
1300 return rv;
1301}
1302
Lev Walkin62258e22007-06-23 23:50:25 +00001303static int
Lev Walkin20696a42017-10-17 21:27:33 -07001304SEQUENCE__handle_extensions(const asn_TYPE_descriptor_t *td, const void *sptr,
1305 asn_per_outp_t *po1, asn_per_outp_t *po2) {
Lev Walkin83668612017-10-21 00:24:31 -07001306 const asn_SEQUENCE_specifics_t *specs =
1307 (const asn_SEQUENCE_specifics_t *)td->specifics;
1308 int exts_present = 0;
1309 int exts_count = 0;
1310 size_t edx;
Lev Walkin62258e22007-06-23 23:50:25 +00001311
Lev Walkin83668612017-10-21 00:24:31 -07001312 if(specs->first_extension < 0) {
1313 return 0;
1314 }
Lev Walkin62258e22007-06-23 23:50:25 +00001315
Lev Walkin83668612017-10-21 00:24:31 -07001316 /* Find out which extensions are present */
1317 for(edx = specs->first_extension; edx < td->elements_count; edx++) {
1318 asn_TYPE_member_t *elm = &td->elements[edx];
1319 const void *memb_ptr; /* Pointer to the member */
Lev Walkin20696a42017-10-17 21:27:33 -07001320 const void *const *memb_ptr2; /* Pointer to that pointer */
1321 int present;
Lev Walkin62258e22007-06-23 23:50:25 +00001322
Lev Walkin83668612017-10-21 00:24:31 -07001323 /* Fetch the pointer to this member */
1324 if(elm->flags & ATF_POINTER) {
Lev Walkin20696a42017-10-17 21:27:33 -07001325 memb_ptr2 =
1326 (const void *const *)((const char *)sptr + elm->memb_offset);
1327 present = (*memb_ptr2 != 0);
Lev Walkin83668612017-10-21 00:24:31 -07001328 } else {
Lev Walkin20696a42017-10-17 21:27:33 -07001329 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
1330 memb_ptr2 = &memb_ptr;
Lev Walkin62258e22007-06-23 23:50:25 +00001331 present = 1;
1332 }
1333
Lev Walkin9f470d62017-10-21 16:27:08 -07001334 ASN_DEBUG("checking %s:%s (@%" ASN_PRI_SIZE ") present => %d", elm->name,
Lev Walkin83668612017-10-21 00:24:31 -07001335 elm->type->name, edx, present);
1336 exts_count++;
1337 exts_present += present;
Lev Walkin62258e22007-06-23 23:50:25 +00001338
Lev Walkin83668612017-10-21 00:24:31 -07001339 /* Encode as presence marker */
1340 if(po1 && per_put_few_bits(po1, present, 1)) {
1341 return -1;
1342 }
1343 /* Encode as open type field */
1344 if(po2 && present
1345 && uper_open_type_put(elm->type,
1346 elm->encoding_constraints.per_constraints,
1347 *memb_ptr2, po2))
1348 return -1;
1349 }
Lev Walkin62258e22007-06-23 23:50:25 +00001350
Lev Walkin83668612017-10-21 00:24:31 -07001351 return exts_present ? exts_count : 0;
Lev Walkin62258e22007-06-23 23:50:25 +00001352}
1353
Lev Walkin523de9e2006-08-18 01:34:18 +00001354asn_enc_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -07001355SEQUENCE_encode_uper(const asn_TYPE_descriptor_t *td,
1356 const asn_per_constraints_t *constraints, const void *sptr,
Lev Walkin494fb702017-08-07 20:07:00 -07001357 asn_per_outp_t *po) {
Lev Walkin20696a42017-10-17 21:27:33 -07001358 const asn_SEQUENCE_specifics_t *specs
Lev Walkind84f6032017-10-03 16:33:59 -07001359 = (const asn_SEQUENCE_specifics_t *)td->specifics;
Lev Walkin523de9e2006-08-18 01:34:18 +00001360 asn_enc_rval_t er;
Lev Walkin62258e22007-06-23 23:50:25 +00001361 int n_extensions;
Lev Walkin494fb702017-08-07 20:07:00 -07001362 size_t edx;
1363 size_t i;
Lev Walkin523de9e2006-08-18 01:34:18 +00001364
1365 (void)constraints;
1366
1367 if(!sptr)
Lev Walkin7c1dc052016-03-14 03:08:15 -07001368 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001369
1370 er.encoded = 0;
1371
1372 ASN_DEBUG("Encoding %s as SEQUENCE (UPER)", td->name);
Lev Walkin62258e22007-06-23 23:50:25 +00001373
Lev Walkin62258e22007-06-23 23:50:25 +00001374 /*
1375 * X.691#18.1 Whether structure is extensible
1376 * and whether to encode extensions
1377 */
Lev Walkin83668612017-10-21 00:24:31 -07001378 if(specs->first_extension < 0) {
1379 n_extensions = 0; /* There are no extensions to encode */
1380 } else {
1381 n_extensions = SEQUENCE__handle_extensions(td, sptr, 0, 0);
1382 if(n_extensions < 0) ASN__ENCODE_FAILED;
1383 if(per_put_few_bits(po, n_extensions ? 1 : 0, 1)) {
1384 ASN__ENCODE_FAILED;
1385 }
1386 }
Lev Walkin523de9e2006-08-18 01:34:18 +00001387
1388 /* Encode a presence bitmap */
1389 for(i = 0; i < specs->roms_count; i++) {
Lev Walkinc46b7cb2006-08-18 02:27:55 +00001390 asn_TYPE_member_t *elm;
Lev Walkin20696a42017-10-17 21:27:33 -07001391 const void *memb_ptr; /* Pointer to the member */
1392 const void *const *memb_ptr2; /* Pointer to that pointer */
1393 int present;
Lev Walkin523de9e2006-08-18 01:34:18 +00001394
Lev Walkinc46b7cb2006-08-18 02:27:55 +00001395 edx = specs->oms[i];
1396 elm = &td->elements[edx];
1397
Lev Walkin523de9e2006-08-18 01:34:18 +00001398 /* Fetch the pointer to this member */
1399 if(elm->flags & ATF_POINTER) {
Lev Walkin20696a42017-10-17 21:27:33 -07001400 memb_ptr2 =
1401 (const void *const *)((const char *)sptr + elm->memb_offset);
1402 present = (*memb_ptr2 != 0);
Lev Walkin523de9e2006-08-18 01:34:18 +00001403 } else {
Lev Walkin20696a42017-10-17 21:27:33 -07001404 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
1405 memb_ptr2 = &memb_ptr;
Lev Walkin523de9e2006-08-18 01:34:18 +00001406 present = 1;
1407 }
1408
1409 /* Eliminate default values */
Lev Walkin20696a42017-10-17 21:27:33 -07001410 if(present && elm->default_value_cmp
1411 && elm->default_value_cmp(*memb_ptr2) == 0)
1412 present = 0;
Lev Walkin523de9e2006-08-18 01:34:18 +00001413
1414 ASN_DEBUG("Element %s %s %s->%s is %s",
1415 elm->flags & ATF_POINTER ? "ptr" : "inline",
Lev Walkin20696a42017-10-17 21:27:33 -07001416 elm->default_value_cmp ? "def" : "wtv",
Lev Walkin523de9e2006-08-18 01:34:18 +00001417 td->name, elm->name, present ? "present" : "absent");
1418 if(per_put_few_bits(po, present, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001419 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001420 }
1421
1422 /*
Lev Walkin62258e22007-06-23 23:50:25 +00001423 * Encode the sequence ROOT elements.
Lev Walkin523de9e2006-08-18 01:34:18 +00001424 */
Lev Walkin83668612017-10-21 00:24:31 -07001425 ASN_DEBUG("first_extension = %d, elements = %d", specs->first_extension,
1426 td->elements_count);
1427 for(edx = 0;
1428 edx < ((specs->first_extension < 0) ? td->elements_count
1429 : (size_t)specs->first_extension);
1430 edx++) {
Lev Walkin523de9e2006-08-18 01:34:18 +00001431 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkin83668612017-10-21 00:24:31 -07001432 const void *memb_ptr; /* Pointer to the member */
1433 const void *const *memb_ptr2; /* Pointer to that pointer */
Lev Walkin4d22b622007-07-23 09:58:25 +00001434
Lev Walkina2987ea2007-06-26 23:56:54 +00001435 ASN_DEBUG("About to encode %s", elm->type->name);
1436
Lev Walkin523de9e2006-08-18 01:34:18 +00001437 /* Fetch the pointer to this member */
1438 if(elm->flags & ATF_POINTER) {
Lev Walkin20696a42017-10-17 21:27:33 -07001439 memb_ptr2 =
1440 (const void *const *)((const char *)sptr + elm->memb_offset);
1441 if(!*memb_ptr2) {
Lev Walkin9f470d62017-10-21 16:27:08 -07001442 ASN_DEBUG("Element %s %" ASN_PRI_SIZE " not present",
Lev Walkin523de9e2006-08-18 01:34:18 +00001443 elm->name, edx);
1444 if(elm->optional)
1445 continue;
1446 /* Mandatory element is missing */
Lev Walkin7c1dc052016-03-14 03:08:15 -07001447 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001448 }
1449 } else {
Lev Walkin20696a42017-10-17 21:27:33 -07001450 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
1451 memb_ptr2 = &memb_ptr;
Lev Walkin523de9e2006-08-18 01:34:18 +00001452 }
1453
1454 /* Eliminate default values */
Lev Walkin20696a42017-10-17 21:27:33 -07001455 if(elm->default_value_cmp && elm->default_value_cmp(*memb_ptr2) == 0)
Lev Walkin523de9e2006-08-18 01:34:18 +00001456 continue;
1457
Lev Walkin83668612017-10-21 00:24:31 -07001458 ASN_DEBUG("Encoding %s->%s:%s", td->name, elm->name, elm->type->name);
1459 er = elm->type->op->uper_encoder(
1460 elm->type, elm->encoding_constraints.per_constraints, *memb_ptr2,
1461 po);
1462 if(er.encoded == -1) return er;
1463 }
Lev Walkin523de9e2006-08-18 01:34:18 +00001464
Lev Walkin62258e22007-06-23 23:50:25 +00001465 /* No extensions to encode */
Lev Walkin7c1dc052016-03-14 03:08:15 -07001466 if(!n_extensions) ASN__ENCODED_OK(er);
Lev Walkin62258e22007-06-23 23:50:25 +00001467
Lev Walkin83668612017-10-21 00:24:31 -07001468 ASN_DEBUG("Length of extensions %d bit-map", n_extensions);
Lev Walkin62258e22007-06-23 23:50:25 +00001469 /* #18.8. Write down the presence bit-map length. */
1470 if(uper_put_nslength(po, n_extensions))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001471 ASN__ENCODE_FAILED;
Lev Walkin62258e22007-06-23 23:50:25 +00001472
1473 ASN_DEBUG("Bit-map of %d elements", n_extensions);
1474 /* #18.7. Encoding the extensions presence bit-map. */
1475 /* TODO: act upon NOTE in #18.7 for canonical PER */
Lev Walkin20696a42017-10-17 21:27:33 -07001476 if(SEQUENCE__handle_extensions(td, sptr, po, 0) != n_extensions)
Lev Walkin7c1dc052016-03-14 03:08:15 -07001477 ASN__ENCODE_FAILED;
Lev Walkin62258e22007-06-23 23:50:25 +00001478
1479 ASN_DEBUG("Writing %d extensions", n_extensions);
1480 /* #18.9. Encode extensions as open type fields. */
Lev Walkin20696a42017-10-17 21:27:33 -07001481 if(SEQUENCE__handle_extensions(td, sptr, 0, po) != n_extensions)
Lev Walkin7c1dc052016-03-14 03:08:15 -07001482 ASN__ENCODE_FAILED;
Lev Walkin62258e22007-06-23 23:50:25 +00001483
Lev Walkin7c1dc052016-03-14 03:08:15 -07001484 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +00001485}
1486
Lev Walkin24810022017-08-25 12:16:11 -07001487#endif /* ASN_DISABLE_PER_SUPPORT */
1488
Lev Walkincd2f48e2017-08-10 02:14:59 -07001489int
1490SEQUENCE_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
1491 const void *bptr) {
1492 size_t edx;
1493
1494 for(edx = 0; edx < td->elements_count; edx++) {
1495 asn_TYPE_member_t *elm = &td->elements[edx];
1496 const void *amemb;
1497 const void *bmemb;
1498 int ret;
1499
1500 if(elm->flags & ATF_POINTER) {
1501 amemb =
1502 *(const void *const *)((const char *)aptr + elm->memb_offset);
1503 bmemb =
1504 *(const void *const *)((const char *)bptr + elm->memb_offset);
1505 if(!amemb) {
1506 if(!bmemb) continue;
Lev Walkin42f6c882017-10-20 02:39:08 -07001507 if(elm->default_value_cmp
1508 && elm->default_value_cmp(bmemb) == 0) {
1509 /* A is absent, but B is present and equal to DEFAULT */
1510 continue;
1511 }
Lev Walkincd2f48e2017-08-10 02:14:59 -07001512 return -1;
1513 } else if(!bmemb) {
Lev Walkin42f6c882017-10-20 02:39:08 -07001514 if(elm->default_value_cmp
1515 && elm->default_value_cmp(amemb) == 0) {
1516 /* B is absent, but A is present and equal to DEFAULT */
1517 continue;
1518 }
Lev Walkincd2f48e2017-08-10 02:14:59 -07001519 return 1;
1520 }
1521 } else {
1522 amemb = (const void *)((const char *)aptr + elm->memb_offset);
1523 bmemb = (const void *)((const char *)bptr + elm->memb_offset);
1524 }
1525
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001526 ret = elm->type->op->compare_struct(elm->type, amemb, bmemb);
Lev Walkincd2f48e2017-08-10 02:14:59 -07001527 if(ret != 0) return ret;
1528 }
1529
1530 return 0;
1531}
1532
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001533asn_TYPE_operation_t asn_OP_SEQUENCE = {
1534 SEQUENCE_free,
1535 SEQUENCE_print,
1536 SEQUENCE_compare,
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001537 SEQUENCE_decode_ber,
1538 SEQUENCE_encode_der,
1539 SEQUENCE_decode_xer,
1540 SEQUENCE_encode_xer,
1541#ifdef ASN_DISABLE_OER_SUPPORT
1542 0,
1543 0,
1544#else
1545 SEQUENCE_decode_oer,
1546 SEQUENCE_encode_oer,
1547#endif /* ASN_DISABLE_OER_SUPPORT */
1548#ifdef ASN_DISABLE_PER_SUPPORT
1549 0,
1550 0,
1551#else
1552 SEQUENCE_decode_uper,
1553 SEQUENCE_encode_uper,
1554#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkina5972be2017-09-29 23:15:58 -07001555 SEQUENCE_random_fill,
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001556 0 /* Use generic outmost tag fetcher */
1557};
1558
Lev Walkina5972be2017-09-29 23:15:58 -07001559
1560asn_random_fill_result_t
1561SEQUENCE_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
1562 const asn_encoding_constraints_t *constr,
1563 size_t max_length) {
1564 const asn_SEQUENCE_specifics_t *specs =
1565 (const asn_SEQUENCE_specifics_t *)td->specifics;
1566 asn_random_fill_result_t result_ok = {ARFILL_OK, 0};
1567 asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
1568 asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
1569 void *st = *sptr;
1570 size_t edx;
1571
1572 if(max_length == 0) return result_skipped;
1573
1574 (void)constr;
1575
1576 if(st == NULL) {
1577 st = CALLOC(1, specs->struct_size);
1578 if(st == NULL) {
1579 return result_failed;
1580 }
1581 }
1582
1583 for(edx = 0; edx < td->elements_count; edx++) {
1584 const asn_TYPE_member_t *elm = &td->elements[edx];
1585 void *memb_ptr; /* Pointer to the member */
1586 void **memb_ptr2; /* Pointer to that pointer */
1587 asn_random_fill_result_t tmpres;
1588
1589 if(elm->optional && asn_random_between(0, 4) == 2) {
1590 /* Sometimes decide not to fill the optional value */
1591 continue;
1592 }
1593
1594 if(elm->flags & ATF_POINTER) {
1595 /* Member is a pointer to another structure */
1596 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
1597 } else {
1598 memb_ptr = (char *)st + elm->memb_offset;
1599 memb_ptr2 = &memb_ptr;
1600 }
1601
1602 tmpres = elm->type->op->random_fill(
1603 elm->type, memb_ptr2, &elm->encoding_constraints,
1604 max_length > result_ok.length ? max_length - result_ok.length : 0);
1605 switch(tmpres.code) {
1606 case ARFILL_OK:
1607 result_ok.length += tmpres.length;
1608 continue;
1609 case ARFILL_SKIPPED:
1610 assert(!(elm->flags & ATF_POINTER) || *memb_ptr2 == NULL);
1611 continue;
1612 case ARFILL_FAILED:
1613 if(st == *sptr) {
1614 ASN_STRUCT_RESET(*td, st);
1615 } else {
1616 ASN_STRUCT_FREE(*td, st);
1617 }
1618 return tmpres;
1619 }
1620 }
1621
1622 *sptr = st;
1623
1624 return result_ok;
1625}
1626