blob: 651a22bf659731df3119afbf6df9d3ce8524677e [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
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 */
69#define IN_EXTENSION_GROUP(specs, memb_idx) \
Lev Walkin494fb702017-08-07 20:07:00 -070070 ( (((signed)(memb_idx)) > (specs)->ext_after) \
71 &&(((signed)(memb_idx)) < (specs)->ext_before))
Lev Walkinf15320b2004-06-03 03:38:44 +000072
Lev Walkin4c36e302004-06-06 07:20:02 +000073
74/*
75 * Tags are canonically sorted in the tag2element map.
76 */
77static int
78_t2e_cmp(const void *ap, const void *bp) {
Lev Walkin5e033762004-09-29 13:26:15 +000079 const asn_TYPE_tag2member_t *a = (const asn_TYPE_tag2member_t *)ap;
80 const asn_TYPE_tag2member_t *b = (const asn_TYPE_tag2member_t *)bp;
Lev Walkinc2346572004-08-11 09:07:36 +000081
Lev Walkin4c36e302004-06-06 07:20:02 +000082 int a_class = BER_TAG_CLASS(a->el_tag);
83 int b_class = BER_TAG_CLASS(b->el_tag);
84
85 if(a_class == b_class) {
86 ber_tlv_tag_t a_value = BER_TAG_VALUE(a->el_tag);
87 ber_tlv_tag_t b_value = BER_TAG_VALUE(b->el_tag);
88
Lev Walkin924fa032004-06-14 13:42:23 +000089 if(a_value == b_value) {
Lev Walkin50299c12004-06-14 14:11:14 +000090 if(a->el_no > b->el_no)
91 return 1;
Lev Walkin924fa032004-06-14 13:42:23 +000092 /*
93 * Important: we do not check
Lev Walkin50299c12004-06-14 14:11:14 +000094 * for a->el_no <= b->el_no!
Lev Walkin924fa032004-06-14 13:42:23 +000095 */
Lev Walkin4c36e302004-06-06 07:20:02 +000096 return 0;
Lev Walkin924fa032004-06-14 13:42:23 +000097 } else if(a_value < b_value)
Lev Walkin4c36e302004-06-06 07:20:02 +000098 return -1;
99 else
100 return 1;
101 } else if(a_class < b_class) {
102 return -1;
103 } else {
104 return 1;
105 }
106}
107
108
Lev Walkinf15320b2004-06-03 03:38:44 +0000109/*
110 * The decoder of the SEQUENCE type.
111 */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000112asn_dec_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000113SEQUENCE_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000114 void **struct_ptr, const void *ptr, size_t size, int tag_mode) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000115 /*
116 * Bring closer parts of structure description.
117 */
Lev Walkin5e033762004-09-29 13:26:15 +0000118 asn_SEQUENCE_specifics_t *specs = (asn_SEQUENCE_specifics_t *)td->specifics;
119 asn_TYPE_member_t *elements = td->elements;
Lev Walkinf15320b2004-06-03 03:38:44 +0000120
121 /*
122 * 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 Walkin24810022017-08-25 12:16:11 -0700205 ASN_DEBUG("In %s SEQUENCE left %d, edx=%zu 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
211 if(ctx->left == 0 /* No more stuff is expected */
212 && (
213 /* Explicit OPTIONAL specification reaches the end */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000214 (edx + elements[edx].optional
215 == td->elements_count)
Lev Walkinf15320b2004-06-03 03:38:44 +0000216 ||
217 /* All extensions are optional */
218 (IN_EXTENSION_GROUP(specs, edx)
Lev Walkin494fb702017-08-07 20:07:00 -0700219 && specs->ext_before > (signed)td->elements_count)
Lev Walkinf15320b2004-06-03 03:38:44 +0000220 )
221 ) {
Lev Walkin449f8322004-08-20 13:23:42 +0000222 ASN_DEBUG("End of SEQUENCE %s", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000223 /*
224 * Found the legitimate end of the structure.
225 */
226 PHASE_OUT(ctx);
227 RETURN(RC_OK);
228 }
229
230 /*
231 * Fetch the T from TLV.
232 */
233 tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
Lev Walkin24810022017-08-25 12:16:11 -0700234 ASN_DEBUG("Current tag in %s SEQUENCE for element %zu "
Lev Walkincc6a9102004-09-23 22:06:26 +0000235 "(%s) is %s encoded in %d bytes, of frame %ld",
Lev Walkin906654e2004-09-10 15:49:15 +0000236 td->name, edx, elements[edx].name,
237 ber_tlv_tag_string(tlv_tag), (int)tag_len, (long)LEFT);
Lev Walkinf15320b2004-06-03 03:38:44 +0000238 switch(tag_len) {
239 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
240 /* Fall through */
241 case -1: RETURN(RC_FAIL);
242 }
243
Lev Walkin8c3b8542005-03-10 18:52:02 +0000244 if(ctx->left < 0 && ((const uint8_t *)ptr)[0] == 0) {
Lev Walkin566a5672004-10-05 06:36:57 +0000245 if(LEFT < 2) {
246 if(SIZE_VIOLATION)
247 RETURN(RC_FAIL);
248 else
249 RETURN(RC_WMORE);
Lev Walkin8c3b8542005-03-10 18:52:02 +0000250 } else if(((const uint8_t *)ptr)[1] == 0) {
Lev Walkin494fb702017-08-07 20:07:00 -0700251 ASN_DEBUG("edx = %zu, opt = %d, ec=%d",
Lev Walkin566a5672004-10-05 06:36:57 +0000252 edx, elements[edx].optional,
253 td->elements_count);
254 if((edx + elements[edx].optional
255 == td->elements_count)
256 || (IN_EXTENSION_GROUP(specs, edx)
257 && specs->ext_before
Lev Walkin494fb702017-08-07 20:07:00 -0700258 > (signed)td->elements_count)) {
Lev Walkin566a5672004-10-05 06:36:57 +0000259 /*
260 * Yeah, baby! Found the terminator
261 * of the indefinite length structure.
262 */
263 /*
264 * Proceed to the canonical
265 * finalization function.
266 * No advancing is necessary.
267 */
268 goto phase3;
269 }
270 }
271 }
272
Lev Walkinf15320b2004-06-03 03:38:44 +0000273 /*
274 * Find the next available type with this tag.
275 */
Lev Walkin4c36e302004-06-06 07:20:02 +0000276 use_bsearch = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000277 opt_edx_end = edx + elements[edx].optional + 1;
Lev Walkin449f8322004-08-20 13:23:42 +0000278 if(opt_edx_end > td->elements_count)
279 opt_edx_end = td->elements_count; /* Cap */
Lev Walkinf9dbd9a2004-07-01 00:48:04 +0000280 else if(opt_edx_end - edx > 8) {
Lev Walkin924fa032004-06-14 13:42:23 +0000281 /* Limit the scope of linear search... */
Lev Walkinf9dbd9a2004-07-01 00:48:04 +0000282 opt_edx_end = edx + 8;
Lev Walkin4c36e302004-06-06 07:20:02 +0000283 use_bsearch = 1;
Lev Walkin924fa032004-06-14 13:42:23 +0000284 /* ... and resort to bsearch() */
Lev Walkin4c36e302004-06-06 07:20:02 +0000285 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000286 for(n = edx; n < opt_edx_end; n++) {
287 if(BER_TAGS_EQUAL(tlv_tag, elements[n].tag)) {
288 /*
289 * Found element corresponding to the tag
290 * being looked at.
291 * Reposition over the right element.
292 */
293 edx = n;
Lev Walkin4c36e302004-06-06 07:20:02 +0000294 ctx->step = 1 + 2 * edx; /* Remember! */
295 goto microphase2;
Lev Walkin14fd3e52017-08-27 01:38:45 -0700296 } else if(elements[n].flags & ATF_ANY_TYPE) {
Lev Walkinb9189732004-09-10 09:37:12 +0000297 /*
298 * This is the ANY type, which may bear
299 * any flag whatsoever.
300 */
301 edx = n;
302 ctx->step = 1 + 2 * edx; /* Remember! */
303 goto microphase2;
Lev Walkin4c36e302004-06-06 07:20:02 +0000304 } else if(elements[n].tag == (ber_tlv_tag_t)-1) {
305 use_bsearch = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000306 break;
307 }
308 }
Lev Walkin4c36e302004-06-06 07:20:02 +0000309 if(use_bsearch) {
310 /*
Lev Walkin4e0704f2004-09-04 06:17:35 +0000311 * Resort to a binary search over
Lev Walkin4c36e302004-06-06 07:20:02 +0000312 * sorted array of tags.
313 */
Wim Lewisfb6344e2014-07-28 12:16:01 -0700314 const asn_TYPE_tag2member_t *t2m;
Lev Walkin878a5212017-08-27 12:39:16 -0700315 asn_TYPE_tag2member_t key = {tlv_tag, edx, 0, 0};
Wim Lewisfb6344e2014-07-28 12:16:01 -0700316 t2m = (const asn_TYPE_tag2member_t *)bsearch(&key,
Lev Walkinc2346572004-08-11 09:07:36 +0000317 specs->tag2el, specs->tag2el_count,
Lev Walkin4c36e302004-06-06 07:20:02 +0000318 sizeof(specs->tag2el[0]), _t2e_cmp);
Lev Walkin924fa032004-06-14 13:42:23 +0000319 if(t2m) {
Wim Lewisfb6344e2014-07-28 12:16:01 -0700320 const asn_TYPE_tag2member_t *best = 0;
321 const asn_TYPE_tag2member_t *t2m_f, *t2m_l;
Lev Walkin494fb702017-08-07 20:07:00 -0700322 size_t edx_max = edx + elements[edx].optional;
Lev Walkin4c36e302004-06-06 07:20:02 +0000323 /*
324 * Rewind to the first element with that tag,
325 * `cause bsearch() does not guarantee order.
326 */
Lev Walkin924fa032004-06-14 13:42:23 +0000327 t2m_f = t2m + t2m->toff_first;
328 t2m_l = t2m + t2m->toff_last;
329 for(t2m = t2m_f; t2m <= t2m_l; t2m++) {
330 if(t2m->el_no > edx_max) break;
331 if(t2m->el_no < edx) continue;
332 best = t2m;
333 }
334 if(best) {
335 edx = best->el_no;
336 ctx->step = 1 + 2 * edx;
337 goto microphase2;
338 }
Lev Walkin4c36e302004-06-06 07:20:02 +0000339 }
340 n = opt_edx_end;
341 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000342 if(n == opt_edx_end) {
343 /*
344 * If tag is unknown, it may be either
345 * an unknown (thus, incorrect) tag,
346 * or an extension (...),
347 * or an end of the indefinite-length structure.
348 */
Lev Walkin7cbbc902006-10-03 06:39:36 +0000349 if(!IN_EXTENSION_GROUP(specs,
350 edx + elements[edx].optional)) {
Lev Walkin24810022017-08-25 12:16:11 -0700351 ASN_DEBUG("Unexpected tag %s (at %zu)",
Lev Walkin566a5672004-10-05 06:36:57 +0000352 ber_tlv_tag_string(tlv_tag), edx);
Lev Walkin4c36e302004-06-06 07:20:02 +0000353 ASN_DEBUG("Expected tag %s (%s)%s",
Lev Walkinf15320b2004-06-03 03:38:44 +0000354 ber_tlv_tag_string(elements[edx].tag),
Lev Walkin4c36e302004-06-06 07:20:02 +0000355 elements[edx].name,
Lev Walkinf15320b2004-06-03 03:38:44 +0000356 elements[edx].optional
357 ?" or alternatives":"");
358 RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000359 } else {
360 /* Skip this tag */
361 ssize_t skip;
Lev Walkin7cbbc902006-10-03 06:39:36 +0000362 edx += elements[edx].optional;
Lev Walkinf15320b2004-06-03 03:38:44 +0000363
Lev Walkin24810022017-08-25 12:16:11 -0700364 ASN_DEBUG("Skipping unexpected %s (at %zu)",
Lev Walkin7cbbc902006-10-03 06:39:36 +0000365 ber_tlv_tag_string(tlv_tag), edx);
Lev Walkinbaaa24f2004-09-29 14:19:14 +0000366 skip = ber_skip_length(opt_codec_ctx,
Lev Walkinf15320b2004-06-03 03:38:44 +0000367 BER_TLV_CONSTRUCTED(ptr),
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800368 (const char *)ptr + tag_len,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000369 LEFT - tag_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000370 ASN_DEBUG("Skip length %d in %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000371 (int)skip, td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000372 switch(skip) {
373 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
374 /* Fall through */
375 case -1: RETURN(RC_FAIL);
376 }
377
378 ADVANCE(skip + tag_len);
379 ctx->step -= 2;
380 edx--;
381 continue; /* Try again with the next tag */
382 }
383 }
384
385 /*
386 * MICROPHASE 2: Invoke the member-specific decoder.
387 */
388 ctx->step |= 1; /* Confirm entering next microphase */
389 microphase2:
Lev Walkin449f8322004-08-20 13:23:42 +0000390 ASN_DEBUG("Inside SEQUENCE %s MF2", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000391
392 /*
393 * Compute the position of the member inside a structure,
394 * and also a type of containment (it may be contained
395 * as pointer or using inline inclusion).
396 */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000397 if(elements[edx].flags & ATF_POINTER) {
398 /* Member is a pointer to another structure */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800399 memb_ptr2 = (void **)((char *)st + elements[edx].memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000400 } else {
401 /*
402 * A pointer to a pointer
403 * holding the start of the structure
404 */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800405 memb_ptr = (char *)st + elements[edx].memb_offset;
Lev Walkinf15320b2004-06-03 03:38:44 +0000406 memb_ptr2 = &memb_ptr;
407 }
408 /*
409 * Invoke the member fetch routine according to member's type
410 */
Lev Walkin75b6fe42017-08-27 01:03:22 -0700411 if(elements[edx].flags & ATF_OPEN_TYPE) {
Lev Walkinc10d30a2017-08-27 00:38:21 -0700412 rval = OPEN_TYPE_ber_get(opt_codec_ctx, td, st, &elements[edx], ptr, LEFT);
413 } else {
414 rval = elements[edx].type->op->ber_decoder(opt_codec_ctx,
415 elements[edx].type,
416 memb_ptr2, ptr, LEFT,
417 elements[edx].tag_mode);
418 }
Lev Walkin24810022017-08-25 12:16:11 -0700419 ASN_DEBUG("In %s SEQUENCE decoded %zu %s of %d "
Lev Walkin5beb7de2004-09-24 20:59:27 +0000420 "in %d bytes rval.code %d, size=%d",
Lev Walkin449f8322004-08-20 13:23:42 +0000421 td->name, edx, elements[edx].type->name,
Lev Walkin5beb7de2004-09-24 20:59:27 +0000422 (int)LEFT, (int)rval.consumed, rval.code, (int)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000423 switch(rval.code) {
424 case RC_OK:
425 break;
426 case RC_WMORE: /* More data expected */
427 if(!SIZE_VIOLATION) {
428 ADVANCE(rval.consumed);
429 RETURN(RC_WMORE);
430 }
Lev Walkin5beb7de2004-09-24 20:59:27 +0000431 ASN_DEBUG("Size violation (c->l=%ld <= s=%ld)",
432 (long)ctx->left, (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000433 /* Fall through */
434 case RC_FAIL: /* Fatal error */
435 RETURN(RC_FAIL);
436 } /* switch(rval) */
437
438 ADVANCE(rval.consumed);
439 } /* for(all structure members) */
440
441 phase3:
442 ctx->phase = 3;
443 case 3: /* 00 and other tags expected */
444 case 4: /* only 00's expected */
445
446 ASN_DEBUG("SEQUENCE %s Leftover: %ld, size = %ld",
Lev Walkin449f8322004-08-20 13:23:42 +0000447 td->name, (long)ctx->left, (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000448
449 /*
450 * Skip everything until the end of the SEQUENCE.
451 */
452 while(ctx->left) {
453 ssize_t tl, ll;
454
455 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
456 switch(tl) {
457 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
458 /* Fall through */
459 case -1: RETURN(RC_FAIL);
460 }
461
462 /*
463 * If expected <0><0>...
464 */
465 if(ctx->left < 0
Lev Walkin8c3b8542005-03-10 18:52:02 +0000466 && ((const uint8_t *)ptr)[0] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000467 if(LEFT < 2) {
468 if(SIZE_VIOLATION)
469 RETURN(RC_FAIL);
470 else
471 RETURN(RC_WMORE);
Lev Walkin8c3b8542005-03-10 18:52:02 +0000472 } else if(((const uint8_t *)ptr)[1] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000473 /*
474 * Correctly finished with <0><0>.
475 */
476 ADVANCE(2);
477 ctx->left++;
478 ctx->phase = 4;
479 continue;
480 }
481 }
482
Lev Walkin449f8322004-08-20 13:23:42 +0000483 if(!IN_EXTENSION_GROUP(specs, td->elements_count)
Lev Walkinf15320b2004-06-03 03:38:44 +0000484 || ctx->phase == 4) {
485 ASN_DEBUG("Unexpected continuation "
486 "of a non-extensible type "
487 "%s (SEQUENCE): %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000488 td->name,
Lev Walkinf15320b2004-06-03 03:38:44 +0000489 ber_tlv_tag_string(tlv_tag));
490 RETURN(RC_FAIL);
491 }
492
Lev Walkinbaaa24f2004-09-29 14:19:14 +0000493 ll = ber_skip_length(opt_codec_ctx,
Lev Walkinf15320b2004-06-03 03:38:44 +0000494 BER_TLV_CONSTRUCTED(ptr),
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800495 (const char *)ptr + tl, LEFT - tl);
Lev Walkinf15320b2004-06-03 03:38:44 +0000496 switch(ll) {
497 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
498 /* Fall through */
499 case -1: RETURN(RC_FAIL);
500 }
501
502 ADVANCE(tl + ll);
503 }
504
505 PHASE_OUT(ctx);
506 }
507
508 RETURN(RC_OK);
509}
510
Lev Walkin4c36e302004-06-06 07:20:02 +0000511
Lev Walkinf15320b2004-06-03 03:38:44 +0000512/*
513 * The DER encoder of the SEQUENCE type.
514 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000515asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000516SEQUENCE_encode_der(asn_TYPE_descriptor_t *td,
Lev Walkinac589332005-08-22 14:19:28 +0000517 void *sptr, int tag_mode, ber_tlv_tag_t tag,
Lev Walkinf15320b2004-06-03 03:38:44 +0000518 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000519 size_t computed_size = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000520 asn_enc_rval_t erval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000521 ssize_t ret;
Lev Walkin494fb702017-08-07 20:07:00 -0700522 size_t edx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000523
524 ASN_DEBUG("%s %s as SEQUENCE",
Lev Walkin449f8322004-08-20 13:23:42 +0000525 cb?"Encoding":"Estimating", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000526
527 /*
528 * Gather the length of the underlying members sequence.
529 */
Lev Walkin449f8322004-08-20 13:23:42 +0000530 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000531 asn_TYPE_member_t *elm = &td->elements[edx];
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400532
533 void *memb_ptr; /* Pointer to the member */
534 void **memb_ptr2; /* Pointer to that pointer */
535
Lev Walkincc93b0f2004-09-10 09:18:20 +0000536 if(elm->flags & ATF_POINTER) {
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400537 memb_ptr2 = (void **)((char *)sptr + elm->memb_offset);
538 if(!*memb_ptr2) {
539 ASN_DEBUG("Element %s %d not present",
540 elm->name, edx);
541 if(elm->optional)
542 continue;
Lev Walkinac589332005-08-22 14:19:28 +0000543 /* Mandatory element is missing */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700544 ASN__ENCODE_FAILED;
Lev Walkinac589332005-08-22 14:19:28 +0000545 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000546 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800547 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400548 memb_ptr2 = &memb_ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000549 }
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400550
551 /* Eliminate default values */
552 if(elm->default_value && elm->default_value(0, memb_ptr2) == 1)
553 continue;
554
555 erval = elm->type->op->der_encoder(elm->type, *memb_ptr2,
Lev Walkinf15320b2004-06-03 03:38:44 +0000556 elm->tag_mode, elm->tag,
557 0, 0);
558 if(erval.encoded == -1)
559 return erval;
560 computed_size += erval.encoded;
Lev Walkin24810022017-08-25 12:16:11 -0700561 ASN_DEBUG("Member %zu %s estimated %ld bytes",
Lev Walkinf15320b2004-06-03 03:38:44 +0000562 edx, elm->name, (long)erval.encoded);
563 }
564
565 /*
566 * Encode the TLV for the sequence itself.
567 */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000568 ret = der_write_tags(td, computed_size, tag_mode, 1, tag, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +0000569 ASN_DEBUG("Wrote tags: %ld (+%ld)", (long)ret, (long)computed_size);
Lev Walkinac589332005-08-22 14:19:28 +0000570 if(ret == -1)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700571 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000572 erval.encoded = computed_size + ret;
573
Lev Walkin7c1dc052016-03-14 03:08:15 -0700574 if(!cb) ASN__ENCODED_OK(erval);
Lev Walkinf15320b2004-06-03 03:38:44 +0000575
576 /*
577 * Encode all members.
578 */
Lev Walkin449f8322004-08-20 13:23:42 +0000579 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000580 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkina9cc46e2004-09-22 16:06:28 +0000581 asn_enc_rval_t tmperval;
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400582 void *memb_ptr; /* Pointer to the member */
583 void **memb_ptr2; /* Pointer to that pointer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000584
Lev Walkincc93b0f2004-09-10 09:18:20 +0000585 if(elm->flags & ATF_POINTER) {
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400586 memb_ptr2 = (void **)((char *)sptr + elm->memb_offset);
587 if(!*memb_ptr2) continue;
Lev Walkinf15320b2004-06-03 03:38:44 +0000588 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800589 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400590 memb_ptr2 = &memb_ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000591 }
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400592
593 /* Eliminate default values */
594 if(elm->default_value && elm->default_value(0, memb_ptr2) == 1)
595 continue;
596
597 tmperval = elm->type->op->der_encoder(elm->type, *memb_ptr2,
598 elm->tag_mode, elm->tag, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +0000599 if(tmperval.encoded == -1)
600 return tmperval;
601 computed_size -= tmperval.encoded;
Lev Walkin24810022017-08-25 12:16:11 -0700602 ASN_DEBUG("Member %zu %s of SEQUENCE %s encoded in %ld bytes",
Lev Walkina6a926a2005-03-02 01:54:28 +0000603 edx, elm->name, td->name, (long)tmperval.encoded);
Lev Walkinf15320b2004-06-03 03:38:44 +0000604 }
605
Lev Walkinac589332005-08-22 14:19:28 +0000606 if(computed_size != 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000607 /*
608 * Encoded size is not equal to the computed size.
609 */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700610 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000611
Lev Walkin7c1dc052016-03-14 03:08:15 -0700612 ASN__ENCODED_OK(erval);
Lev Walkinf15320b2004-06-03 03:38:44 +0000613}
614
Lev Walkinc61f3862005-02-14 17:21:22 +0000615
Lev Walkin1bbc2002004-10-23 13:27:30 +0000616#undef XER_ADVANCE
Lev Walkinf59dac92017-08-26 21:26:51 -0700617#define XER_ADVANCE(num_bytes) \
618 do { \
619 size_t num = (num_bytes); \
620 ptr = ((const char *)ptr) + num; \
621 size -= num; \
622 consumed_myself += num; \
623 } while(0)
Lev Walkin1bbc2002004-10-23 13:27:30 +0000624
Lev Walkinc61f3862005-02-14 17:21:22 +0000625/*
626 * Decode the XER (XML) data.
627 */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000628asn_dec_rval_t
629SEQUENCE_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
630 void **struct_ptr, const char *opt_mname,
Lev Walkinf59dac92017-08-26 21:26:51 -0700631 const void *ptr, size_t size) {
Lev Walkin1bbc2002004-10-23 13:27:30 +0000632 /*
633 * Bring closer parts of structure description.
634 */
Lev Walkinc61f3862005-02-14 17:21:22 +0000635 asn_SEQUENCE_specifics_t *specs
636 = (asn_SEQUENCE_specifics_t *)td->specifics;
Lev Walkin1bbc2002004-10-23 13:27:30 +0000637 asn_TYPE_member_t *elements = td->elements;
638 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
639
640 /*
Lev Walkinc61f3862005-02-14 17:21:22 +0000641 * ... and parts of the structure being constructed.
Lev Walkin1bbc2002004-10-23 13:27:30 +0000642 */
643 void *st = *struct_ptr; /* Target structure. */
644 asn_struct_ctx_t *ctx; /* Decoder context */
645
Lev Walkinc61f3862005-02-14 17:21:22 +0000646 asn_dec_rval_t rval; /* Return value from a decoder */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000647 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
Lev Walkin494fb702017-08-07 20:07:00 -0700648 size_t edx; /* Element index */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000649
650 /*
651 * Create the target structure if it is not present already.
652 */
653 if(st == 0) {
654 st = *struct_ptr = CALLOC(1, specs->struct_size);
655 if(st == 0) RETURN(RC_FAIL);
656 }
657
658 /*
659 * Restore parsing context.
660 */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800661 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkin1bbc2002004-10-23 13:27:30 +0000662
663
664 /*
665 * Phases of XER/XML processing:
666 * Phase 0: Check that the opening tag matches our expectations.
667 * Phase 1: Processing body and reacting on closing tag.
668 * Phase 2: Processing inner type.
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000669 * Phase 3: Skipping unknown extensions.
670 * Phase 4: PHASED OUT
Lev Walkin1bbc2002004-10-23 13:27:30 +0000671 */
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000672 for(edx = ctx->step; ctx->phase <= 3;) {
Lev Walkin1bbc2002004-10-23 13:27:30 +0000673 pxer_chunk_type_e ch_type; /* XER chunk type */
674 ssize_t ch_size; /* Chunk size */
675 xer_check_tag_e tcv; /* Tag check value */
676 asn_TYPE_member_t *elm;
677
678 /*
Lev Walkinc61f3862005-02-14 17:21:22 +0000679 * Go inside the inner member of a sequence.
Lev Walkin1bbc2002004-10-23 13:27:30 +0000680 */
681 if(ctx->phase == 2) {
682 asn_dec_rval_t tmprval;
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400683 void *memb_ptr_dontuse; /* Pointer to the member */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000684 void **memb_ptr2; /* Pointer to that pointer */
685
Lev Walkinabf68892004-10-26 10:12:14 +0000686 elm = &td->elements[edx];
687
Lev Walkin1bbc2002004-10-23 13:27:30 +0000688 if(elm->flags & ATF_POINTER) {
689 /* Member is a pointer to another structure */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800690 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
Lev Walkin1bbc2002004-10-23 13:27:30 +0000691 } else {
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400692 memb_ptr_dontuse = (char *)st + elm->memb_offset;
693 memb_ptr2 = &memb_ptr_dontuse; /* Only use of memb_ptr_dontuse */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000694 }
695
Lev Walkin75b6fe42017-08-27 01:03:22 -0700696 if(elm->flags & ATF_OPEN_TYPE) {
Lev Walkinf59dac92017-08-26 21:26:51 -0700697 tmprval = OPEN_TYPE_xer_get(opt_codec_ctx, td, st, elm, ptr, size);
698 } else {
699 /* Invoke the inner type decoder, m.b. multiple times */
700 tmprval = elm->type->op->xer_decoder(opt_codec_ctx,
701 elm->type, memb_ptr2, elm->name,
702 ptr, size);
703 }
Lev Walkin1bbc2002004-10-23 13:27:30 +0000704 XER_ADVANCE(tmprval.consumed);
705 if(tmprval.code != RC_OK)
706 RETURN(tmprval.code);
Lev Walkinc61f3862005-02-14 17:21:22 +0000707 ctx->phase = 1; /* Back to body processing */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000708 ctx->step = ++edx;
Lev Walkinc61f3862005-02-14 17:21:22 +0000709 ASN_DEBUG("XER/SEQUENCE phase => %d, step => %d",
710 ctx->phase, ctx->step);
Lev Walkin1bbc2002004-10-23 13:27:30 +0000711 /* Fall through */
712 }
713
714 /*
715 * Get the next part of the XML stream.
716 */
Lev Walkinf59dac92017-08-26 21:26:51 -0700717 ch_size = xer_next_token(&ctx->context, ptr, size,
Lev Walkin1e443962005-02-18 18:06:36 +0000718 &ch_type);
Lev Walkin97363482016-01-24 19:23:02 -0800719 if(ch_size == -1) {
720 RETURN(RC_FAIL);
Lev Walkinf59dac92017-08-26 21:26:51 -0700721 } else {
Lev Walkin1bbc2002004-10-23 13:27:30 +0000722 switch(ch_type) {
Lev Walkinf59dac92017-08-26 21:26:51 -0700723 case PXER_WMORE:
724 RETURN(RC_WMORE);
Lev Walkin1bbc2002004-10-23 13:27:30 +0000725 case PXER_COMMENT: /* Got XML comment */
726 case PXER_TEXT: /* Ignore free-standing text */
727 XER_ADVANCE(ch_size); /* Skip silently */
728 continue;
729 case PXER_TAG:
730 break; /* Check the rest down there */
731 }
732 }
733
Lev Walkinf59dac92017-08-26 21:26:51 -0700734 tcv = xer_check_tag(ptr, ch_size, xml_tag);
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000735 ASN_DEBUG("XER/SEQUENCE: tcv = %d, ph=%d [%s]",
736 tcv, ctx->phase, xml_tag);
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000737
738 /* Skip the extensions section */
739 if(ctx->phase == 3) {
740 switch(xer_skip_unknown(tcv, &ctx->left)) {
741 case -1:
742 ctx->phase = 4;
743 RETURN(RC_FAIL);
744 case 0:
745 XER_ADVANCE(ch_size);
746 continue;
747 case 1:
748 XER_ADVANCE(ch_size);
749 ctx->phase = 1;
750 continue;
751 case 2:
752 ctx->phase = 1;
753 break;
754 }
755 }
756
Lev Walkin1bbc2002004-10-23 13:27:30 +0000757 switch(tcv) {
758 case XCT_CLOSING:
759 if(ctx->phase == 0) break;
Lev Walkin1bbc2002004-10-23 13:27:30 +0000760 ctx->phase = 0;
761 /* Fall through */
762 case XCT_BOTH:
763 if(ctx->phase == 0) {
764 if(edx >= td->elements_count
765 ||
766 /* Explicit OPTIONAL specs reaches the end */
767 (edx + elements[edx].optional
768 == td->elements_count)
769 ||
770 /* All extensions are optional */
771 (IN_EXTENSION_GROUP(specs, edx)
772 && specs->ext_before
Lev Walkin494fb702017-08-07 20:07:00 -0700773 > (signed)td->elements_count)
Lev Walkin1bbc2002004-10-23 13:27:30 +0000774 ) {
775 XER_ADVANCE(ch_size);
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000776 ctx->phase = 4; /* Phase out */
Lev Walkinc61f3862005-02-14 17:21:22 +0000777 RETURN(RC_OK);
Lev Walkin1bbc2002004-10-23 13:27:30 +0000778 } else {
779 ASN_DEBUG("Premature end of XER SEQUENCE");
780 RETURN(RC_FAIL);
781 }
782 }
783 /* Fall through */
784 case XCT_OPENING:
785 if(ctx->phase == 0) {
786 XER_ADVANCE(ch_size);
787 ctx->phase = 1; /* Processing body phase */
788 continue;
789 }
Lev Walkinda3ca412004-10-23 14:58:15 +0000790 /* Fall through */
Lev Walkin904e65b2005-02-18 14:23:48 +0000791 case XCT_UNKNOWN_OP:
792 case XCT_UNKNOWN_BO:
Lev Walkin1bbc2002004-10-23 13:27:30 +0000793
Lev Walkin24810022017-08-25 12:16:11 -0700794 ASN_DEBUG("XER/SEQUENCE: tcv=%d, ph=%d, edx=%zu",
Lev Walkin02666192005-07-03 05:31:02 +0000795 tcv, ctx->phase, edx);
796 if(ctx->phase != 1) {
Lev Walkin1bbc2002004-10-23 13:27:30 +0000797 break; /* Really unexpected */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000798 }
Lev Walkin02666192005-07-03 05:31:02 +0000799
800 if(edx < td->elements_count) {
801 /*
802 * Search which member corresponds to this tag.
803 */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800804 size_t n;
805 size_t edx_end = edx + elements[edx].optional + 1;
Lev Walkin02666192005-07-03 05:31:02 +0000806 if(edx_end > td->elements_count)
807 edx_end = td->elements_count;
808 for(n = edx; n < edx_end; n++) {
809 elm = &td->elements[n];
Lev Walkinf59dac92017-08-26 21:26:51 -0700810 tcv = xer_check_tag(ptr, ch_size, elm->name);
Lev Walkin02666192005-07-03 05:31:02 +0000811 switch(tcv) {
812 case XCT_BOTH:
813 case XCT_OPENING:
814 /*
815 * Process this member.
816 */
817 ctx->step = edx = n;
818 ctx->phase = 2;
819 break;
820 case XCT_UNKNOWN_OP:
821 case XCT_UNKNOWN_BO:
822 continue;
823 default:
824 n = edx_end;
825 break; /* Phase out */
826 }
827 break;
828 }
829 if(n != edx_end)
830 continue;
831 } else {
Lev Walkin24810022017-08-25 12:16:11 -0700832 ASN_DEBUG("Out of defined members: %zu/%u",
Lev Walkin02666192005-07-03 05:31:02 +0000833 edx, td->elements_count);
834 }
Lev Walkin904e65b2005-02-18 14:23:48 +0000835
836 /* It is expected extension */
837 if(IN_EXTENSION_GROUP(specs,
Lev Walkin02666192005-07-03 05:31:02 +0000838 edx + (edx < td->elements_count
839 ? elements[edx].optional : 0))) {
Lev Walkin24810022017-08-25 12:16:11 -0700840 ASN_DEBUG("Got anticipated extension at %zu",
Lev Walkin02666192005-07-03 05:31:02 +0000841 edx);
Lev Walkin904e65b2005-02-18 14:23:48 +0000842 /*
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000843 * Check for (XCT_BOTH or XCT_UNKNOWN_BO)
844 * By using a mask. Only record a pure
845 * <opening> tags.
Lev Walkin904e65b2005-02-18 14:23:48 +0000846 */
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000847 if(tcv & XCT_CLOSING) {
848 /* Found </extension> without body */
849 } else {
850 ctx->left = 1;
851 ctx->phase = 3; /* Skip ...'s */
852 }
853 XER_ADVANCE(ch_size);
854 continue;
Lev Walkin904e65b2005-02-18 14:23:48 +0000855 }
856
857 /* Fall through */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000858 default:
859 break;
860 }
861
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000862 ASN_DEBUG("Unexpected XML tag in SEQUENCE [%c%c%c%c%c%c]",
Lev Walkinf59dac92017-08-26 21:26:51 -0700863 size>0?((const char *)ptr)[0]:'.',
864 size>1?((const char *)ptr)[1]:'.',
865 size>2?((const char *)ptr)[2]:'.',
866 size>3?((const char *)ptr)[3]:'.',
867 size>4?((const char *)ptr)[4]:'.',
868 size>5?((const char *)ptr)[5]:'.');
Lev Walkin1bbc2002004-10-23 13:27:30 +0000869 break;
870 }
871
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000872 ctx->phase = 4; /* "Phase out" on hard failure */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000873 RETURN(RC_FAIL);
874}
875
Lev Walkina9cc46e2004-09-22 16:06:28 +0000876asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000877SEQUENCE_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000878 int ilevel, enum xer_encoder_flags_e flags,
879 asn_app_consume_bytes_f *cb, void *app_key) {
880 asn_enc_rval_t er;
881 int xcan = (flags & XER_F_CANONICAL);
Lev Walkin494fb702017-08-07 20:07:00 -0700882 size_t edx;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000883
884 if(!sptr)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700885 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000886
887 er.encoded = 0;
888
889 for(edx = 0; edx < td->elements_count; edx++) {
890 asn_enc_rval_t tmper;
Lev Walkin5e033762004-09-29 13:26:15 +0000891 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkina9cc46e2004-09-22 16:06:28 +0000892 void *memb_ptr;
893 const char *mname = elm->name;
894 unsigned int mlen = strlen(mname);
895
896 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800897 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
Lev Walkinac589332005-08-22 14:19:28 +0000898 if(!memb_ptr) {
899 if(elm->optional)
900 continue;
901 /* Mandatory element is missing */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700902 ASN__ENCODE_FAILED;
Lev Walkinac589332005-08-22 14:19:28 +0000903 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000904 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800905 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000906 }
907
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400908 /*
909 * X.693 (0112) #9.5.
910 * TODO: Default values' encodings shall always be present.
911 */
912
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700913 if(!xcan) ASN__TEXT_INDENT(1, ilevel);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700914 ASN__CALLBACK3("<", 1, mname, mlen, ">", 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000915
916 /* Print the member itself */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800917 tmper = elm->type->op->xer_encoder(elm->type, memb_ptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000918 ilevel + 1, flags, cb, app_key);
919 if(tmper.encoded == -1) return tmper;
920
Lev Walkin7c1dc052016-03-14 03:08:15 -0700921 ASN__CALLBACK3("</", 2, mname, mlen, ">", 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000922 er.encoded += 5 + (2 * mlen) + tmper.encoded;
923 }
924
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700925 if(!xcan) ASN__TEXT_INDENT(1, ilevel - 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000926
Lev Walkin7c1dc052016-03-14 03:08:15 -0700927 ASN__ENCODED_OK(er);
Lev Walkin942fd082004-10-03 09:13:02 +0000928cb_failed:
Lev Walkin7c1dc052016-03-14 03:08:15 -0700929 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000930}
931
Lev Walkinf15320b2004-06-03 03:38:44 +0000932int
Lev Walkin5e033762004-09-29 13:26:15 +0000933SEQUENCE_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000934 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin494fb702017-08-07 20:07:00 -0700935 size_t edx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000936 int ret;
937
Lev Walkin8e8078a2004-09-26 13:10:40 +0000938 if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000939
940 /* Dump preamble */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000941 if(cb(td->name, strlen(td->name), app_key) < 0
942 || cb(" ::= {", 6, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000943 return -1;
944
Lev Walkin449f8322004-08-20 13:23:42 +0000945 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000946 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000947 const void *memb_ptr;
948
Lev Walkincc93b0f2004-09-10 09:18:20 +0000949 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800950 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkinac589332005-08-22 14:19:28 +0000951 if(!memb_ptr) {
952 if(elm->optional) continue;
953 /* Print <absent> line */
954 /* Fall through */
955 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000956 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800957 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000958 }
959
960 /* Indentation */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000961 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000962
963 /* Print the member's name and stuff */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000964 if(cb(elm->name, strlen(elm->name), app_key) < 0
965 || cb(": ", 2, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000966 return -1;
967
968 /* Print the member itself */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800969 ret = elm->type->op->print_struct(elm->type, memb_ptr, ilevel + 1,
Lev Walkinf15320b2004-06-03 03:38:44 +0000970 cb, app_key);
971 if(ret) return ret;
Lev Walkinf15320b2004-06-03 03:38:44 +0000972 }
973
Lev Walkin8e8078a2004-09-26 13:10:40 +0000974 ilevel--;
975 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000976
Lev Walkin8e8078a2004-09-26 13:10:40 +0000977 return (cb("}", 1, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000978}
979
980void
Lev Walkin8d99d7b2017-08-25 01:06:00 -0700981SEQUENCE_free(const asn_TYPE_descriptor_t *td, void *sptr,
982 enum asn_struct_free_method method) {
Lev Walkinf6853ce2017-08-11 00:50:27 -0700983 size_t edx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000984
985 if(!td || !sptr)
986 return;
987
988 ASN_DEBUG("Freeing %s as SEQUENCE", td->name);
989
Lev Walkin449f8322004-08-20 13:23:42 +0000990 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000991 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000992 void *memb_ptr;
Lev Walkincc93b0f2004-09-10 09:18:20 +0000993 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800994 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000995 if(memb_ptr)
Lev Walkinadcb5862006-03-17 02:11:12 +0000996 ASN_STRUCT_FREE(*elm->type, memb_ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +0000997 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800998 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkinadcb5862006-03-17 02:11:12 +0000999 ASN_STRUCT_FREE_CONTENTS_ONLY(*elm->type, memb_ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +00001000 }
1001 }
1002
Lev Walkin8d99d7b2017-08-25 01:06:00 -07001003 switch(method) {
1004 case ASFM_FREE_EVERYTHING:
1005 FREEMEM(sptr);
1006 break;
1007 case ASFM_FREE_UNDERLYING:
1008 break;
1009 case ASFM_FREE_UNDERLYING_AND_RESET:
1010 memset(sptr, 0,
1011 ((asn_SEQUENCE_specifics_t *)(td->specifics))->struct_size);
1012 break;
1013 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001014}
1015
1016int
Lev Walkin5e033762004-09-29 13:26:15 +00001017SEQUENCE_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +00001018 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkin494fb702017-08-07 20:07:00 -07001019 size_t edx;
Lev Walkinf15320b2004-06-03 03:38:44 +00001020
1021 if(!sptr) {
Lev Walkin7c1dc052016-03-14 03:08:15 -07001022 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +00001023 "%s: value not given (%s:%d)",
1024 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +00001025 return -1;
1026 }
1027
1028 /*
1029 * Iterate over structure members and check their validity.
1030 */
Lev Walkin449f8322004-08-20 13:23:42 +00001031 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +00001032 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +00001033 const void *memb_ptr;
1034
Lev Walkincc93b0f2004-09-10 09:18:20 +00001035 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001036 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkinac589332005-08-22 14:19:28 +00001037 if(!memb_ptr) {
1038 if(elm->optional)
1039 continue;
Lev Walkin7c1dc052016-03-14 03:08:15 -07001040 ASN__CTFAIL(app_key, td, sptr,
Lev Walkinac589332005-08-22 14:19:28 +00001041 "%s: mandatory element %s absent (%s:%d)",
1042 td->name, elm->name, __FILE__, __LINE__);
1043 return -1;
1044 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001045 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001046 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +00001047 }
1048
Lev Walkin449f8322004-08-20 13:23:42 +00001049 if(elm->memb_constraints) {
1050 int ret = elm->memb_constraints(elm->type, memb_ptr,
Lev Walkin1eded352006-07-13 11:19:01 +00001051 ctfailcb, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +00001052 if(ret) return ret;
1053 } else {
1054 int ret = elm->type->check_constraints(elm->type,
Lev Walkin1eded352006-07-13 11:19:01 +00001055 memb_ptr, ctfailcb, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +00001056 if(ret) return ret;
1057 /*
1058 * Cannot inherit it earlier:
1059 * need to make sure we get the updated version.
1060 */
1061 elm->memb_constraints = elm->type->check_constraints;
1062 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001063 }
1064
1065 return 0;
1066}
Lev Walkin59b176e2005-11-26 11:25:14 +00001067
Lev Walkin24810022017-08-25 12:16:11 -07001068#ifndef ASN_DISABLE_PER_SUPPORT
1069
Lev Walkin59b176e2005-11-26 11:25:14 +00001070asn_dec_rval_t
1071SEQUENCE_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -07001072 const asn_per_constraints_t *constraints, void **sptr,
1073 asn_per_data_t *pd) {
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001074 asn_SEQUENCE_specifics_t *specs = (asn_SEQUENCE_specifics_t *)td->specifics;
Lev Walkin59b176e2005-11-26 11:25:14 +00001075 void *st = *sptr; /* Target structure. */
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001076 int extpresent; /* Extension additions are present */
Lev Walkin59b176e2005-11-26 11:25:14 +00001077 uint8_t *opres; /* Presence of optional root members */
1078 asn_per_data_t opmd;
1079 asn_dec_rval_t rv;
Lev Walkin494fb702017-08-07 20:07:00 -07001080 size_t edx;
Lev Walkin59b176e2005-11-26 11:25:14 +00001081
1082 (void)constraints;
1083
Lev Walkin7c1dc052016-03-14 03:08:15 -07001084 if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx))
1085 ASN__DECODE_FAILED;
Lev Walkin1d9e8dd2005-12-07 05:46:03 +00001086
Lev Walkin59b176e2005-11-26 11:25:14 +00001087 if(!st) {
1088 st = *sptr = CALLOC(1, specs->struct_size);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001089 if(!st) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +00001090 }
1091
1092 ASN_DEBUG("Decoding %s as SEQUENCE (UPER)", td->name);
1093
1094 /* Handle extensions */
1095 if(specs->ext_before >= 0) {
1096 extpresent = per_get_few_bits(pd, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001097 if(extpresent < 0) ASN__DECODE_STARVED;
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001098 } else {
1099 extpresent = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +00001100 }
1101
1102 /* Prepare a place and read-in the presence bitmap */
Lev Walkin73a5cc62007-06-24 08:45:31 +00001103 memset(&opmd, 0, sizeof(opmd));
Lev Walkin59b176e2005-11-26 11:25:14 +00001104 if(specs->roms_count) {
1105 opres = (uint8_t *)MALLOC(((specs->roms_count + 7) >> 3) + 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001106 if(!opres) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +00001107 /* Get the presence map */
1108 if(per_get_many_bits(pd, opres, 0, specs->roms_count)) {
1109 FREEMEM(opres);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001110 ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +00001111 }
1112 opmd.buffer = opres;
Lev Walkin59b176e2005-11-26 11:25:14 +00001113 opmd.nbits = specs->roms_count;
1114 ASN_DEBUG("Read in presence bitmap for %s of %d bits (%x..)",
1115 td->name, specs->roms_count, *opres);
1116 } else {
1117 opres = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +00001118 }
1119
1120 /*
1121 * Get the sequence ROOT elements.
1122 */
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001123 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001124 asn_TYPE_member_t *elm = &td->elements[edx];
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001125 void *memb_ptr; /* Pointer to the member */
Lev Walkin59b176e2005-11-26 11:25:14 +00001126 void **memb_ptr2; /* Pointer to that pointer */
1127
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001128 if(IN_EXTENSION_GROUP(specs, edx))
1129 continue;
1130
Lev Walkin59b176e2005-11-26 11:25:14 +00001131 /* Fetch the pointer to this member */
1132 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001133 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
Lev Walkin59b176e2005-11-26 11:25:14 +00001134 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001135 memb_ptr = (char *)st + elm->memb_offset;
Lev Walkin59b176e2005-11-26 11:25:14 +00001136 memb_ptr2 = &memb_ptr;
1137 }
1138
1139 /* Deal with optionality */
1140 if(elm->optional) {
1141 int present = per_get_few_bits(&opmd, 1);
1142 ASN_DEBUG("Member %s->%s is optional, p=%d (%d->%d)",
1143 td->name, elm->name, present,
1144 (int)opmd.nboff, (int)opmd.nbits);
1145 if(present == 0) {
1146 /* This element is not present */
1147 if(elm->default_value) {
1148 /* Fill-in DEFAULT */
Lev Walkin523de9e2006-08-18 01:34:18 +00001149 if(elm->default_value(1, memb_ptr2)) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001150 FREEMEM(opres);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001151 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +00001152 }
Lev Walkin8032f7a2007-06-27 01:54:57 +00001153 ASN_DEBUG("Filled-in default");
Lev Walkin59b176e2005-11-26 11:25:14 +00001154 }
1155 /* The member is just not present */
1156 continue;
1157 }
1158 /* Fall through */
1159 }
1160
1161 /* Fetch the member from the stream */
Lev Walkinf6853ce2017-08-11 00:50:27 -07001162 ASN_DEBUG("Decoding member \"%s\" in %s", elm->name, td->name);
Lev Walkin9de6cd82017-08-10 05:47:46 -07001163
Lev Walkin75b6fe42017-08-27 01:03:22 -07001164 if(elm->flags & ATF_OPEN_TYPE) {
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001165 rv = OPEN_TYPE_uper_get(opt_codec_ctx, td, st, elm, pd);
1166 } else {
Lev Walkinf59dac92017-08-26 21:26:51 -07001167 rv = elm->type->op->uper_decoder(opt_codec_ctx, elm->type,
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001168 elm->per_constraints, memb_ptr2, pd);
1169 }
1170 if(rv.code != RC_OK) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001171 ASN_DEBUG("Failed decode %s in %s",
1172 elm->name, td->name);
1173 FREEMEM(opres);
1174 return rv;
1175 }
1176 }
1177
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001178 /* Optionality map is not needed anymore */
1179 FREEMEM(opres);
1180
Lev Walkin59b176e2005-11-26 11:25:14 +00001181 /*
1182 * Deal with extensions.
1183 */
1184 if(extpresent) {
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001185 ssize_t bmlength;
1186 uint8_t *epres; /* Presence of extension members */
1187 asn_per_data_t epmd;
Lev Walkin59b176e2005-11-26 11:25:14 +00001188
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001189 bmlength = uper_get_nslength(pd);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001190 if(bmlength < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +00001191
Lev Walkinaaae9bf2010-10-24 16:56:30 -07001192 ASN_DEBUG("Extensions %ld present in %s", (long)bmlength, td->name);
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001193
1194 epres = (uint8_t *)MALLOC((bmlength + 15) >> 3);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001195 if(!epres) ASN__DECODE_STARVED;
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001196
1197 /* Get the extensions map */
Simo Sorcece71d912015-09-03 16:34:20 -04001198 if(per_get_many_bits(pd, epres, 0, bmlength)) {
1199 FREEMEM(epres);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001200 ASN__DECODE_STARVED;
Simo Sorcece71d912015-09-03 16:34:20 -04001201 }
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001202
Lev Walkin73a5cc62007-06-24 08:45:31 +00001203 memset(&epmd, 0, sizeof(epmd));
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001204 epmd.buffer = epres;
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001205 epmd.nbits = bmlength;
Lev Walkinaaae9bf2010-10-24 16:56:30 -07001206 ASN_DEBUG("Read in extensions bitmap for %s of %ld bits (%x..)",
1207 td->name, (long)bmlength, *epres);
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001208
1209 /* Go over extensions and read them in */
1210 for(edx = specs->ext_after + 1; edx < td->elements_count; edx++) {
1211 asn_TYPE_member_t *elm = &td->elements[edx];
1212 void *memb_ptr; /* Pointer to the member */
1213 void **memb_ptr2; /* Pointer to that pointer */
1214 int present;
1215
1216 if(!IN_EXTENSION_GROUP(specs, edx)) {
Lev Walkin24810022017-08-25 12:16:11 -07001217 ASN_DEBUG("%zu is not an extension", edx);
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001218 continue;
1219 }
1220
1221 /* Fetch the pointer to this member */
1222 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001223 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001224 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001225 memb_ptr = (void *)((char *)st + elm->memb_offset);
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001226 memb_ptr2 = &memb_ptr;
1227 }
1228
1229 present = per_get_few_bits(&epmd, 1);
1230 if(present <= 0) {
1231 if(present < 0) break; /* No more extensions */
1232 continue;
1233 }
1234
1235 ASN_DEBUG("Decoding member %s in %s %p", elm->name, td->name, *memb_ptr2);
Lev Walkin9218bc12007-06-27 04:09:37 +00001236 rv = uper_open_type_get(opt_codec_ctx, elm->type,
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001237 elm->per_constraints, memb_ptr2, pd);
1238 if(rv.code != RC_OK) {
1239 FREEMEM(epres);
1240 return rv;
1241 }
1242 }
1243
1244 /* Skip over overflow extensions which aren't present
1245 * in this system's version of the protocol */
Lev Walkin06230f72007-06-26 09:52:44 +00001246 for(;;) {
Lev Walkina2987ea2007-06-26 23:56:54 +00001247 ASN_DEBUG("Getting overflow extensions");
Lev Walkin06230f72007-06-26 09:52:44 +00001248 switch(per_get_few_bits(&epmd, 1)) {
1249 case -1: break;
1250 case 0: continue;
1251 default:
Lev Walkin9218bc12007-06-27 04:09:37 +00001252 if(uper_open_type_skip(opt_codec_ctx, pd)) {
Lev Walkin06230f72007-06-26 09:52:44 +00001253 FREEMEM(epres);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001254 ASN__DECODE_STARVED;
Lev Walkin06230f72007-06-26 09:52:44 +00001255 }
Lev Walkin59b176e2005-11-26 11:25:14 +00001256 }
Lev Walkin06230f72007-06-26 09:52:44 +00001257 break;
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001258 }
Lev Walkin59b176e2005-11-26 11:25:14 +00001259
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001260 FREEMEM(epres);
1261 }
1262
1263 /* Fill DEFAULT members in extensions */
1264 for(edx = specs->roms_count; edx < specs->roms_count
1265 + specs->aoms_count; edx++) {
1266 asn_TYPE_member_t *elm = &td->elements[edx];
1267 void **memb_ptr2; /* Pointer to member pointer */
1268
1269 if(!elm->default_value) continue;
1270
1271 /* Fetch the pointer to this member */
1272 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001273 memb_ptr2 = (void **)((char *)st
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001274 + elm->memb_offset);
1275 if(*memb_ptr2) continue;
1276 } else {
1277 continue; /* Extensions are all optionals */
1278 }
1279
1280 /* Set default value */
1281 if(elm->default_value(1, memb_ptr2)) {
Lev Walkin7c1dc052016-03-14 03:08:15 -07001282 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +00001283 }
1284 }
1285
1286 rv.consumed = 0;
1287 rv.code = RC_OK;
1288 return rv;
1289}
1290
Lev Walkin62258e22007-06-23 23:50:25 +00001291static int
1292SEQUENCE_handle_extensions(asn_TYPE_descriptor_t *td, void *sptr,
1293 asn_per_outp_t *po1, asn_per_outp_t *po2) {
1294 asn_SEQUENCE_specifics_t *specs
1295 = (asn_SEQUENCE_specifics_t *)td->specifics;
Lev Walkinf55a6dd2007-06-24 00:35:51 +00001296 int exts_present = 0;
1297 int exts_count = 0;
Lev Walkin494fb702017-08-07 20:07:00 -07001298 size_t edx;
Lev Walkin62258e22007-06-23 23:50:25 +00001299
1300 if(specs->ext_before < 0)
1301 return 0;
1302
1303 /* Find out which extensions are present */
1304 for(edx = specs->ext_after + 1; edx < td->elements_count; edx++) {
1305 asn_TYPE_member_t *elm = &td->elements[edx];
1306 void *memb_ptr; /* Pointer to the member */
1307 void **memb_ptr2; /* Pointer to that pointer */
1308 int present;
1309
Lev Walkinf55a6dd2007-06-24 00:35:51 +00001310 if(!IN_EXTENSION_GROUP(specs, edx)) {
Lev Walkin24810022017-08-25 12:16:11 -07001311 ASN_DEBUG("%s (@%zu) is not extension", elm->type->name, edx);
Lev Walkin62258e22007-06-23 23:50:25 +00001312 continue;
Lev Walkinf55a6dd2007-06-24 00:35:51 +00001313 }
Lev Walkin62258e22007-06-23 23:50:25 +00001314
1315 /* Fetch the pointer to this member */
1316 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001317 memb_ptr2 = (void **)((char *)sptr + elm->memb_offset);
Lev Walkin62258e22007-06-23 23:50:25 +00001318 present = (*memb_ptr2 != 0);
1319 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001320 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkin62258e22007-06-23 23:50:25 +00001321 memb_ptr2 = &memb_ptr;
1322 present = 1;
1323 }
1324
Lev Walkin24810022017-08-25 12:16:11 -07001325 ASN_DEBUG("checking %s (@%zu) present => %d",
Lev Walkin06230f72007-06-26 09:52:44 +00001326 elm->type->name, edx, present);
Lev Walkinf55a6dd2007-06-24 00:35:51 +00001327 exts_count++;
1328 exts_present += present;
Lev Walkin62258e22007-06-23 23:50:25 +00001329
1330 /* Encode as presence marker */
1331 if(po1 && per_put_few_bits(po1, present, 1))
1332 return -1;
1333 /* Encode as open type field */
Lev Walkin9218bc12007-06-27 04:09:37 +00001334 if(po2 && present && uper_open_type_put(elm->type,
Lev Walkin62258e22007-06-23 23:50:25 +00001335 elm->per_constraints, *memb_ptr2, po2))
1336 return -1;
1337
1338 }
1339
Lev Walkinf55a6dd2007-06-24 00:35:51 +00001340 return exts_present ? exts_count : 0;
Lev Walkin62258e22007-06-23 23:50:25 +00001341}
1342
Lev Walkin523de9e2006-08-18 01:34:18 +00001343asn_enc_rval_t
1344SEQUENCE_encode_uper(asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -07001345 const asn_per_constraints_t *constraints, void *sptr,
1346 asn_per_outp_t *po) {
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001347 asn_SEQUENCE_specifics_t *specs
Lev Walkin523de9e2006-08-18 01:34:18 +00001348 = (asn_SEQUENCE_specifics_t *)td->specifics;
1349 asn_enc_rval_t er;
Lev Walkin62258e22007-06-23 23:50:25 +00001350 int n_extensions;
Lev Walkin494fb702017-08-07 20:07:00 -07001351 size_t edx;
1352 size_t i;
Lev Walkin523de9e2006-08-18 01:34:18 +00001353
1354 (void)constraints;
1355
1356 if(!sptr)
Lev Walkin7c1dc052016-03-14 03:08:15 -07001357 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001358
1359 er.encoded = 0;
1360
1361 ASN_DEBUG("Encoding %s as SEQUENCE (UPER)", td->name);
Lev Walkin62258e22007-06-23 23:50:25 +00001362
1363
1364 /*
1365 * X.691#18.1 Whether structure is extensible
1366 * and whether to encode extensions
1367 */
1368 if(specs->ext_before >= 0) {
1369 n_extensions = SEQUENCE_handle_extensions(td, sptr, 0, 0);
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001370 if(n_extensions < 0)
Lev Walkin494fb702017-08-07 20:07:00 -07001371 ASN__ENCODE_FAILED;
1372 if(per_put_few_bits(po, n_extensions ? 1 : 0, 1))
1373 ASN__ENCODE_FAILED;
Lev Walkin62258e22007-06-23 23:50:25 +00001374 } else {
1375 n_extensions = 0; /* There are no extensions to encode */
1376 }
Lev Walkin523de9e2006-08-18 01:34:18 +00001377
1378 /* Encode a presence bitmap */
1379 for(i = 0; i < specs->roms_count; i++) {
Lev Walkinc46b7cb2006-08-18 02:27:55 +00001380 asn_TYPE_member_t *elm;
Lev Walkin523de9e2006-08-18 01:34:18 +00001381 void *memb_ptr; /* Pointer to the member */
1382 void **memb_ptr2; /* Pointer to that pointer */
Lev Walkin523de9e2006-08-18 01:34:18 +00001383 int present;
1384
Lev Walkinc46b7cb2006-08-18 02:27:55 +00001385 edx = specs->oms[i];
1386 elm = &td->elements[edx];
1387
Lev Walkin523de9e2006-08-18 01:34:18 +00001388 /* Fetch the pointer to this member */
1389 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001390 memb_ptr2 = (void **)((char *)sptr + elm->memb_offset);
Lev Walkin523de9e2006-08-18 01:34:18 +00001391 present = (*memb_ptr2 != 0);
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 present = 1;
1396 }
1397
1398 /* Eliminate default values */
1399 if(present && elm->default_value
1400 && elm->default_value(0, memb_ptr2) == 1)
1401 present = 0;
1402
1403 ASN_DEBUG("Element %s %s %s->%s is %s",
1404 elm->flags & ATF_POINTER ? "ptr" : "inline",
1405 elm->default_value ? "def" : "wtv",
1406 td->name, elm->name, present ? "present" : "absent");
1407 if(per_put_few_bits(po, present, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001408 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001409 }
1410
1411 /*
Lev Walkin62258e22007-06-23 23:50:25 +00001412 * Encode the sequence ROOT elements.
Lev Walkin523de9e2006-08-18 01:34:18 +00001413 */
Lev Walkin62258e22007-06-23 23:50:25 +00001414 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 +00001415 for(edx = 0; edx < ((specs->ext_after < 0)
1416 ? td->elements_count : specs->ext_before - 1); edx++) {
1417
Lev Walkin523de9e2006-08-18 01:34:18 +00001418 asn_TYPE_member_t *elm = &td->elements[edx];
1419 void *memb_ptr; /* Pointer to the member */
1420 void **memb_ptr2; /* Pointer to that pointer */
1421
Lev Walkin4d22b622007-07-23 09:58:25 +00001422 if(IN_EXTENSION_GROUP(specs, edx))
1423 continue;
1424
Lev Walkina2987ea2007-06-26 23:56:54 +00001425 ASN_DEBUG("About to encode %s", elm->type->name);
1426
Lev Walkin523de9e2006-08-18 01:34:18 +00001427 /* Fetch the pointer to this member */
1428 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001429 memb_ptr2 = (void **)((char *)sptr + elm->memb_offset);
Lev Walkin523de9e2006-08-18 01:34:18 +00001430 if(!*memb_ptr2) {
Lev Walkin24810022017-08-25 12:16:11 -07001431 ASN_DEBUG("Element %s %zu not present",
Lev Walkin523de9e2006-08-18 01:34:18 +00001432 elm->name, edx);
1433 if(elm->optional)
1434 continue;
1435 /* Mandatory element is missing */
Lev Walkin7c1dc052016-03-14 03:08:15 -07001436 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001437 }
1438 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001439 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkin523de9e2006-08-18 01:34:18 +00001440 memb_ptr2 = &memb_ptr;
1441 }
1442
1443 /* Eliminate default values */
Lev Walkindbd091a2007-06-27 02:11:08 +00001444 if(elm->default_value && elm->default_value(0, memb_ptr2) == 1)
Lev Walkin523de9e2006-08-18 01:34:18 +00001445 continue;
1446
Lev Walkin4d22b622007-07-23 09:58:25 +00001447 ASN_DEBUG("Encoding %s->%s", td->name, elm->name);
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001448 er = elm->type->op->uper_encoder(elm->type, elm->per_constraints,
Lev Walkin523de9e2006-08-18 01:34:18 +00001449 *memb_ptr2, po);
1450 if(er.encoded == -1)
1451 return er;
1452 }
1453
Lev Walkin62258e22007-06-23 23:50:25 +00001454 /* No extensions to encode */
Lev Walkin7c1dc052016-03-14 03:08:15 -07001455 if(!n_extensions) ASN__ENCODED_OK(er);
Lev Walkin62258e22007-06-23 23:50:25 +00001456
1457 ASN_DEBUG("Length of %d bit-map", n_extensions);
1458 /* #18.8. Write down the presence bit-map length. */
1459 if(uper_put_nslength(po, n_extensions))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001460 ASN__ENCODE_FAILED;
Lev Walkin62258e22007-06-23 23:50:25 +00001461
1462 ASN_DEBUG("Bit-map of %d elements", n_extensions);
1463 /* #18.7. Encoding the extensions presence bit-map. */
1464 /* TODO: act upon NOTE in #18.7 for canonical PER */
1465 if(SEQUENCE_handle_extensions(td, sptr, po, 0) != n_extensions)
Lev Walkin7c1dc052016-03-14 03:08:15 -07001466 ASN__ENCODE_FAILED;
Lev Walkin62258e22007-06-23 23:50:25 +00001467
1468 ASN_DEBUG("Writing %d extensions", n_extensions);
1469 /* #18.9. Encode extensions as open type fields. */
1470 if(SEQUENCE_handle_extensions(td, sptr, 0, po) != n_extensions)
Lev Walkin7c1dc052016-03-14 03:08:15 -07001471 ASN__ENCODE_FAILED;
Lev Walkin62258e22007-06-23 23:50:25 +00001472
Lev Walkin7c1dc052016-03-14 03:08:15 -07001473 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +00001474}
1475
Lev Walkin24810022017-08-25 12:16:11 -07001476#endif /* ASN_DISABLE_PER_SUPPORT */
1477
Lev Walkincd2f48e2017-08-10 02:14:59 -07001478int
1479SEQUENCE_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
1480 const void *bptr) {
1481 size_t edx;
1482
1483 for(edx = 0; edx < td->elements_count; edx++) {
1484 asn_TYPE_member_t *elm = &td->elements[edx];
1485 const void *amemb;
1486 const void *bmemb;
1487 int ret;
1488
1489 if(elm->flags & ATF_POINTER) {
1490 amemb =
1491 *(const void *const *)((const char *)aptr + elm->memb_offset);
1492 bmemb =
1493 *(const void *const *)((const char *)bptr + elm->memb_offset);
1494 if(!amemb) {
1495 if(!bmemb) continue;
1496 return -1;
1497 } else if(!bmemb) {
1498 return 1;
1499 }
1500 } else {
1501 amemb = (const void *)((const char *)aptr + elm->memb_offset);
1502 bmemb = (const void *)((const char *)bptr + elm->memb_offset);
1503 }
1504
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001505 ret = elm->type->op->compare_struct(elm->type, amemb, bmemb);
Lev Walkincd2f48e2017-08-10 02:14:59 -07001506 if(ret != 0) return ret;
1507 }
1508
1509 return 0;
1510}
1511
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001512asn_TYPE_operation_t asn_OP_SEQUENCE = {
1513 SEQUENCE_free,
1514 SEQUENCE_print,
1515 SEQUENCE_compare,
1516 SEQUENCE_constraint,
1517 SEQUENCE_decode_ber,
1518 SEQUENCE_encode_der,
1519 SEQUENCE_decode_xer,
1520 SEQUENCE_encode_xer,
1521#ifdef ASN_DISABLE_OER_SUPPORT
1522 0,
1523 0,
1524#else
1525 SEQUENCE_decode_oer,
1526 SEQUENCE_encode_oer,
1527#endif /* ASN_DISABLE_OER_SUPPORT */
1528#ifdef ASN_DISABLE_PER_SUPPORT
1529 0,
1530 0,
1531#else
1532 SEQUENCE_decode_uper,
1533 SEQUENCE_encode_uper,
1534#endif /* ASN_DISABLE_PER_SUPPORT */
1535 0 /* Use generic outmost tag fetcher */
1536};
1537