blob: ba74067f10ea925d59edbb916dc35089d113f16f [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 Walkinb9189732004-09-10 09:37:12 +0000296 } else if(elements[n].flags & ATF_OPEN_TYPE) {
297 /*
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 Walkin5e033762004-09-29 13:26:15 +0000315 asn_TYPE_tag2member_t key;
Lev Walkin4c36e302004-06-06 07:20:02 +0000316 key.el_tag = tlv_tag;
Lev Walkin924fa032004-06-14 13:42:23 +0000317 key.el_no = edx;
Wim Lewisfb6344e2014-07-28 12:16:01 -0700318 t2m = (const asn_TYPE_tag2member_t *)bsearch(&key,
Lev Walkinc2346572004-08-11 09:07:36 +0000319 specs->tag2el, specs->tag2el_count,
Lev Walkin4c36e302004-06-06 07:20:02 +0000320 sizeof(specs->tag2el[0]), _t2e_cmp);
Lev Walkin924fa032004-06-14 13:42:23 +0000321 if(t2m) {
Wim Lewisfb6344e2014-07-28 12:16:01 -0700322 const asn_TYPE_tag2member_t *best = 0;
323 const asn_TYPE_tag2member_t *t2m_f, *t2m_l;
Lev Walkin494fb702017-08-07 20:07:00 -0700324 size_t edx_max = edx + elements[edx].optional;
Lev Walkin4c36e302004-06-06 07:20:02 +0000325 /*
326 * Rewind to the first element with that tag,
327 * `cause bsearch() does not guarantee order.
328 */
Lev Walkin924fa032004-06-14 13:42:23 +0000329 t2m_f = t2m + t2m->toff_first;
330 t2m_l = t2m + t2m->toff_last;
331 for(t2m = t2m_f; t2m <= t2m_l; t2m++) {
332 if(t2m->el_no > edx_max) break;
333 if(t2m->el_no < edx) continue;
334 best = t2m;
335 }
336 if(best) {
337 edx = best->el_no;
338 ctx->step = 1 + 2 * edx;
339 goto microphase2;
340 }
Lev Walkin4c36e302004-06-06 07:20:02 +0000341 }
342 n = opt_edx_end;
343 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000344 if(n == opt_edx_end) {
345 /*
346 * If tag is unknown, it may be either
347 * an unknown (thus, incorrect) tag,
348 * or an extension (...),
349 * or an end of the indefinite-length structure.
350 */
Lev Walkin7cbbc902006-10-03 06:39:36 +0000351 if(!IN_EXTENSION_GROUP(specs,
352 edx + elements[edx].optional)) {
Lev Walkin24810022017-08-25 12:16:11 -0700353 ASN_DEBUG("Unexpected tag %s (at %zu)",
Lev Walkin566a5672004-10-05 06:36:57 +0000354 ber_tlv_tag_string(tlv_tag), edx);
Lev Walkin4c36e302004-06-06 07:20:02 +0000355 ASN_DEBUG("Expected tag %s (%s)%s",
Lev Walkinf15320b2004-06-03 03:38:44 +0000356 ber_tlv_tag_string(elements[edx].tag),
Lev Walkin4c36e302004-06-06 07:20:02 +0000357 elements[edx].name,
Lev Walkinf15320b2004-06-03 03:38:44 +0000358 elements[edx].optional
359 ?" or alternatives":"");
360 RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000361 } else {
362 /* Skip this tag */
363 ssize_t skip;
Lev Walkin7cbbc902006-10-03 06:39:36 +0000364 edx += elements[edx].optional;
Lev Walkinf15320b2004-06-03 03:38:44 +0000365
Lev Walkin24810022017-08-25 12:16:11 -0700366 ASN_DEBUG("Skipping unexpected %s (at %zu)",
Lev Walkin7cbbc902006-10-03 06:39:36 +0000367 ber_tlv_tag_string(tlv_tag), edx);
Lev Walkinbaaa24f2004-09-29 14:19:14 +0000368 skip = ber_skip_length(opt_codec_ctx,
Lev Walkinf15320b2004-06-03 03:38:44 +0000369 BER_TLV_CONSTRUCTED(ptr),
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800370 (const char *)ptr + tag_len,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000371 LEFT - tag_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000372 ASN_DEBUG("Skip length %d in %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000373 (int)skip, td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000374 switch(skip) {
375 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
376 /* Fall through */
377 case -1: RETURN(RC_FAIL);
378 }
379
380 ADVANCE(skip + tag_len);
381 ctx->step -= 2;
382 edx--;
383 continue; /* Try again with the next tag */
384 }
385 }
386
387 /*
388 * MICROPHASE 2: Invoke the member-specific decoder.
389 */
390 ctx->step |= 1; /* Confirm entering next microphase */
391 microphase2:
Lev Walkin449f8322004-08-20 13:23:42 +0000392 ASN_DEBUG("Inside SEQUENCE %s MF2", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000393
394 /*
395 * Compute the position of the member inside a structure,
396 * and also a type of containment (it may be contained
397 * as pointer or using inline inclusion).
398 */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000399 if(elements[edx].flags & ATF_POINTER) {
400 /* Member is a pointer to another structure */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800401 memb_ptr2 = (void **)((char *)st + elements[edx].memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000402 } else {
403 /*
404 * A pointer to a pointer
405 * holding the start of the structure
406 */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800407 memb_ptr = (char *)st + elements[edx].memb_offset;
Lev Walkinf15320b2004-06-03 03:38:44 +0000408 memb_ptr2 = &memb_ptr;
409 }
410 /*
411 * Invoke the member fetch routine according to member's type
412 */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800413 rval = elements[edx].type->op->ber_decoder(opt_codec_ctx,
Lev Walkinc2346572004-08-11 09:07:36 +0000414 elements[edx].type,
Lev Walkinf15320b2004-06-03 03:38:44 +0000415 memb_ptr2, ptr, LEFT,
416 elements[edx].tag_mode);
Lev Walkin24810022017-08-25 12:16:11 -0700417 ASN_DEBUG("In %s SEQUENCE decoded %zu %s of %d "
Lev Walkin5beb7de2004-09-24 20:59:27 +0000418 "in %d bytes rval.code %d, size=%d",
Lev Walkin449f8322004-08-20 13:23:42 +0000419 td->name, edx, elements[edx].type->name,
Lev Walkin5beb7de2004-09-24 20:59:27 +0000420 (int)LEFT, (int)rval.consumed, rval.code, (int)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000421 switch(rval.code) {
422 case RC_OK:
423 break;
424 case RC_WMORE: /* More data expected */
425 if(!SIZE_VIOLATION) {
426 ADVANCE(rval.consumed);
427 RETURN(RC_WMORE);
428 }
Lev Walkin5beb7de2004-09-24 20:59:27 +0000429 ASN_DEBUG("Size violation (c->l=%ld <= s=%ld)",
430 (long)ctx->left, (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000431 /* Fall through */
432 case RC_FAIL: /* Fatal error */
433 RETURN(RC_FAIL);
434 } /* switch(rval) */
435
436 ADVANCE(rval.consumed);
437 } /* for(all structure members) */
438
439 phase3:
440 ctx->phase = 3;
441 case 3: /* 00 and other tags expected */
442 case 4: /* only 00's expected */
443
444 ASN_DEBUG("SEQUENCE %s Leftover: %ld, size = %ld",
Lev Walkin449f8322004-08-20 13:23:42 +0000445 td->name, (long)ctx->left, (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000446
447 /*
448 * Skip everything until the end of the SEQUENCE.
449 */
450 while(ctx->left) {
451 ssize_t tl, ll;
452
453 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
454 switch(tl) {
455 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
456 /* Fall through */
457 case -1: RETURN(RC_FAIL);
458 }
459
460 /*
461 * If expected <0><0>...
462 */
463 if(ctx->left < 0
Lev Walkin8c3b8542005-03-10 18:52:02 +0000464 && ((const uint8_t *)ptr)[0] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000465 if(LEFT < 2) {
466 if(SIZE_VIOLATION)
467 RETURN(RC_FAIL);
468 else
469 RETURN(RC_WMORE);
Lev Walkin8c3b8542005-03-10 18:52:02 +0000470 } else if(((const uint8_t *)ptr)[1] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000471 /*
472 * Correctly finished with <0><0>.
473 */
474 ADVANCE(2);
475 ctx->left++;
476 ctx->phase = 4;
477 continue;
478 }
479 }
480
Lev Walkin449f8322004-08-20 13:23:42 +0000481 if(!IN_EXTENSION_GROUP(specs, td->elements_count)
Lev Walkinf15320b2004-06-03 03:38:44 +0000482 || ctx->phase == 4) {
483 ASN_DEBUG("Unexpected continuation "
484 "of a non-extensible type "
485 "%s (SEQUENCE): %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000486 td->name,
Lev Walkinf15320b2004-06-03 03:38:44 +0000487 ber_tlv_tag_string(tlv_tag));
488 RETURN(RC_FAIL);
489 }
490
Lev Walkinbaaa24f2004-09-29 14:19:14 +0000491 ll = ber_skip_length(opt_codec_ctx,
Lev Walkinf15320b2004-06-03 03:38:44 +0000492 BER_TLV_CONSTRUCTED(ptr),
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800493 (const char *)ptr + tl, LEFT - tl);
Lev Walkinf15320b2004-06-03 03:38:44 +0000494 switch(ll) {
495 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
496 /* Fall through */
497 case -1: RETURN(RC_FAIL);
498 }
499
500 ADVANCE(tl + ll);
501 }
502
503 PHASE_OUT(ctx);
504 }
505
506 RETURN(RC_OK);
507}
508
Lev Walkin4c36e302004-06-06 07:20:02 +0000509
Lev Walkinf15320b2004-06-03 03:38:44 +0000510/*
511 * The DER encoder of the SEQUENCE type.
512 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000513asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000514SEQUENCE_encode_der(asn_TYPE_descriptor_t *td,
Lev Walkinac589332005-08-22 14:19:28 +0000515 void *sptr, int tag_mode, ber_tlv_tag_t tag,
Lev Walkinf15320b2004-06-03 03:38:44 +0000516 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000517 size_t computed_size = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000518 asn_enc_rval_t erval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000519 ssize_t ret;
Lev Walkin494fb702017-08-07 20:07:00 -0700520 size_t edx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000521
522 ASN_DEBUG("%s %s as SEQUENCE",
Lev Walkin449f8322004-08-20 13:23:42 +0000523 cb?"Encoding":"Estimating", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000524
525 /*
526 * Gather the length of the underlying members sequence.
527 */
Lev Walkin449f8322004-08-20 13:23:42 +0000528 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000529 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000530 void *memb_ptr;
Lev Walkincc93b0f2004-09-10 09:18:20 +0000531 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800532 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
Lev Walkinac589332005-08-22 14:19:28 +0000533 if(!memb_ptr) {
534 if(elm->optional) continue;
535 /* Mandatory element is missing */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700536 ASN__ENCODE_FAILED;
Lev Walkinac589332005-08-22 14:19:28 +0000537 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000538 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800539 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000540 }
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800541 erval = elm->type->op->der_encoder(elm->type, memb_ptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000542 elm->tag_mode, elm->tag,
543 0, 0);
544 if(erval.encoded == -1)
545 return erval;
546 computed_size += erval.encoded;
Lev Walkin24810022017-08-25 12:16:11 -0700547 ASN_DEBUG("Member %zu %s estimated %ld bytes",
Lev Walkinf15320b2004-06-03 03:38:44 +0000548 edx, elm->name, (long)erval.encoded);
549 }
550
551 /*
552 * Encode the TLV for the sequence itself.
553 */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000554 ret = der_write_tags(td, computed_size, tag_mode, 1, tag, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +0000555 ASN_DEBUG("Wrote tags: %ld (+%ld)", (long)ret, (long)computed_size);
Lev Walkinac589332005-08-22 14:19:28 +0000556 if(ret == -1)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700557 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000558 erval.encoded = computed_size + ret;
559
Lev Walkin7c1dc052016-03-14 03:08:15 -0700560 if(!cb) ASN__ENCODED_OK(erval);
Lev Walkinf15320b2004-06-03 03:38:44 +0000561
562 /*
563 * Encode all members.
564 */
Lev Walkin449f8322004-08-20 13:23:42 +0000565 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000566 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkina9cc46e2004-09-22 16:06:28 +0000567 asn_enc_rval_t tmperval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000568 void *memb_ptr;
569
Lev Walkincc93b0f2004-09-10 09:18:20 +0000570 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800571 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000572 if(!memb_ptr) continue;
573 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800574 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000575 }
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800576 tmperval = elm->type->op->der_encoder(elm->type, memb_ptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000577 elm->tag_mode, elm->tag,
578 cb, app_key);
579 if(tmperval.encoded == -1)
580 return tmperval;
581 computed_size -= tmperval.encoded;
Lev Walkin24810022017-08-25 12:16:11 -0700582 ASN_DEBUG("Member %zu %s of SEQUENCE %s encoded in %ld bytes",
Lev Walkina6a926a2005-03-02 01:54:28 +0000583 edx, elm->name, td->name, (long)tmperval.encoded);
Lev Walkinf15320b2004-06-03 03:38:44 +0000584 }
585
Lev Walkinac589332005-08-22 14:19:28 +0000586 if(computed_size != 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000587 /*
588 * Encoded size is not equal to the computed size.
589 */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700590 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000591
Lev Walkin7c1dc052016-03-14 03:08:15 -0700592 ASN__ENCODED_OK(erval);
Lev Walkinf15320b2004-06-03 03:38:44 +0000593}
594
Lev Walkinc61f3862005-02-14 17:21:22 +0000595
Lev Walkin1bbc2002004-10-23 13:27:30 +0000596#undef XER_ADVANCE
Lev Walkinf59dac92017-08-26 21:26:51 -0700597#define XER_ADVANCE(num_bytes) \
598 do { \
599 size_t num = (num_bytes); \
600 ptr = ((const char *)ptr) + num; \
601 size -= num; \
602 consumed_myself += num; \
603 } while(0)
Lev Walkin1bbc2002004-10-23 13:27:30 +0000604
Lev Walkinc61f3862005-02-14 17:21:22 +0000605/*
606 * Decode the XER (XML) data.
607 */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000608asn_dec_rval_t
609SEQUENCE_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
610 void **struct_ptr, const char *opt_mname,
Lev Walkinf59dac92017-08-26 21:26:51 -0700611 const void *ptr, size_t size) {
Lev Walkin1bbc2002004-10-23 13:27:30 +0000612 /*
613 * Bring closer parts of structure description.
614 */
Lev Walkinc61f3862005-02-14 17:21:22 +0000615 asn_SEQUENCE_specifics_t *specs
616 = (asn_SEQUENCE_specifics_t *)td->specifics;
Lev Walkin1bbc2002004-10-23 13:27:30 +0000617 asn_TYPE_member_t *elements = td->elements;
618 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
619
620 /*
Lev Walkinc61f3862005-02-14 17:21:22 +0000621 * ... and parts of the structure being constructed.
Lev Walkin1bbc2002004-10-23 13:27:30 +0000622 */
623 void *st = *struct_ptr; /* Target structure. */
624 asn_struct_ctx_t *ctx; /* Decoder context */
625
Lev Walkinc61f3862005-02-14 17:21:22 +0000626 asn_dec_rval_t rval; /* Return value from a decoder */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000627 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
Lev Walkin494fb702017-08-07 20:07:00 -0700628 size_t edx; /* Element index */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000629
630 /*
631 * Create the target structure if it is not present already.
632 */
633 if(st == 0) {
634 st = *struct_ptr = CALLOC(1, specs->struct_size);
635 if(st == 0) RETURN(RC_FAIL);
636 }
637
638 /*
639 * Restore parsing context.
640 */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800641 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkin1bbc2002004-10-23 13:27:30 +0000642
643
644 /*
645 * Phases of XER/XML processing:
646 * Phase 0: Check that the opening tag matches our expectations.
647 * Phase 1: Processing body and reacting on closing tag.
648 * Phase 2: Processing inner type.
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000649 * Phase 3: Skipping unknown extensions.
650 * Phase 4: PHASED OUT
Lev Walkin1bbc2002004-10-23 13:27:30 +0000651 */
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000652 for(edx = ctx->step; ctx->phase <= 3;) {
Lev Walkin1bbc2002004-10-23 13:27:30 +0000653 pxer_chunk_type_e ch_type; /* XER chunk type */
654 ssize_t ch_size; /* Chunk size */
655 xer_check_tag_e tcv; /* Tag check value */
656 asn_TYPE_member_t *elm;
657
658 /*
Lev Walkinc61f3862005-02-14 17:21:22 +0000659 * Go inside the inner member of a sequence.
Lev Walkin1bbc2002004-10-23 13:27:30 +0000660 */
661 if(ctx->phase == 2) {
662 asn_dec_rval_t tmprval;
Lev Walkin1bbc2002004-10-23 13:27:30 +0000663 void *memb_ptr; /* Pointer to the member */
664 void **memb_ptr2; /* Pointer to that pointer */
665
Lev Walkinabf68892004-10-26 10:12:14 +0000666 elm = &td->elements[edx];
667
Lev Walkin1bbc2002004-10-23 13:27:30 +0000668 if(elm->flags & ATF_POINTER) {
669 /* Member is a pointer to another structure */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800670 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
Lev Walkin1bbc2002004-10-23 13:27:30 +0000671 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800672 memb_ptr = (char *)st + elm->memb_offset;
Lev Walkin1bbc2002004-10-23 13:27:30 +0000673 memb_ptr2 = &memb_ptr;
674 }
675
Lev Walkinf59dac92017-08-26 21:26:51 -0700676 if((elm->flags & ATF_OPEN_TYPE) && elm->type_selector) {
677 tmprval = OPEN_TYPE_xer_get(opt_codec_ctx, td, st, elm, ptr, size);
678 } else {
679 /* Invoke the inner type decoder, m.b. multiple times */
680 tmprval = elm->type->op->xer_decoder(opt_codec_ctx,
681 elm->type, memb_ptr2, elm->name,
682 ptr, size);
683 }
Lev Walkin1bbc2002004-10-23 13:27:30 +0000684 XER_ADVANCE(tmprval.consumed);
685 if(tmprval.code != RC_OK)
686 RETURN(tmprval.code);
Lev Walkinc61f3862005-02-14 17:21:22 +0000687 ctx->phase = 1; /* Back to body processing */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000688 ctx->step = ++edx;
Lev Walkinc61f3862005-02-14 17:21:22 +0000689 ASN_DEBUG("XER/SEQUENCE phase => %d, step => %d",
690 ctx->phase, ctx->step);
Lev Walkin1bbc2002004-10-23 13:27:30 +0000691 /* Fall through */
692 }
693
694 /*
695 * Get the next part of the XML stream.
696 */
Lev Walkinf59dac92017-08-26 21:26:51 -0700697 ch_size = xer_next_token(&ctx->context, ptr, size,
Lev Walkin1e443962005-02-18 18:06:36 +0000698 &ch_type);
Lev Walkin97363482016-01-24 19:23:02 -0800699 if(ch_size == -1) {
700 RETURN(RC_FAIL);
Lev Walkinf59dac92017-08-26 21:26:51 -0700701 } else {
Lev Walkin1bbc2002004-10-23 13:27:30 +0000702 switch(ch_type) {
Lev Walkinf59dac92017-08-26 21:26:51 -0700703 case PXER_WMORE:
704 RETURN(RC_WMORE);
Lev Walkin1bbc2002004-10-23 13:27:30 +0000705 case PXER_COMMENT: /* Got XML comment */
706 case PXER_TEXT: /* Ignore free-standing text */
707 XER_ADVANCE(ch_size); /* Skip silently */
708 continue;
709 case PXER_TAG:
710 break; /* Check the rest down there */
711 }
712 }
713
Lev Walkinf59dac92017-08-26 21:26:51 -0700714 tcv = xer_check_tag(ptr, ch_size, xml_tag);
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000715 ASN_DEBUG("XER/SEQUENCE: tcv = %d, ph=%d [%s]",
716 tcv, ctx->phase, xml_tag);
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000717
718 /* Skip the extensions section */
719 if(ctx->phase == 3) {
720 switch(xer_skip_unknown(tcv, &ctx->left)) {
721 case -1:
722 ctx->phase = 4;
723 RETURN(RC_FAIL);
724 case 0:
725 XER_ADVANCE(ch_size);
726 continue;
727 case 1:
728 XER_ADVANCE(ch_size);
729 ctx->phase = 1;
730 continue;
731 case 2:
732 ctx->phase = 1;
733 break;
734 }
735 }
736
Lev Walkin1bbc2002004-10-23 13:27:30 +0000737 switch(tcv) {
738 case XCT_CLOSING:
739 if(ctx->phase == 0) break;
Lev Walkin1bbc2002004-10-23 13:27:30 +0000740 ctx->phase = 0;
741 /* Fall through */
742 case XCT_BOTH:
743 if(ctx->phase == 0) {
744 if(edx >= td->elements_count
745 ||
746 /* Explicit OPTIONAL specs reaches the end */
747 (edx + elements[edx].optional
748 == td->elements_count)
749 ||
750 /* All extensions are optional */
751 (IN_EXTENSION_GROUP(specs, edx)
752 && specs->ext_before
Lev Walkin494fb702017-08-07 20:07:00 -0700753 > (signed)td->elements_count)
Lev Walkin1bbc2002004-10-23 13:27:30 +0000754 ) {
755 XER_ADVANCE(ch_size);
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000756 ctx->phase = 4; /* Phase out */
Lev Walkinc61f3862005-02-14 17:21:22 +0000757 RETURN(RC_OK);
Lev Walkin1bbc2002004-10-23 13:27:30 +0000758 } else {
759 ASN_DEBUG("Premature end of XER SEQUENCE");
760 RETURN(RC_FAIL);
761 }
762 }
763 /* Fall through */
764 case XCT_OPENING:
765 if(ctx->phase == 0) {
766 XER_ADVANCE(ch_size);
767 ctx->phase = 1; /* Processing body phase */
768 continue;
769 }
Lev Walkinda3ca412004-10-23 14:58:15 +0000770 /* Fall through */
Lev Walkin904e65b2005-02-18 14:23:48 +0000771 case XCT_UNKNOWN_OP:
772 case XCT_UNKNOWN_BO:
Lev Walkin1bbc2002004-10-23 13:27:30 +0000773
Lev Walkin24810022017-08-25 12:16:11 -0700774 ASN_DEBUG("XER/SEQUENCE: tcv=%d, ph=%d, edx=%zu",
Lev Walkin02666192005-07-03 05:31:02 +0000775 tcv, ctx->phase, edx);
776 if(ctx->phase != 1) {
Lev Walkin1bbc2002004-10-23 13:27:30 +0000777 break; /* Really unexpected */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000778 }
Lev Walkin02666192005-07-03 05:31:02 +0000779
780 if(edx < td->elements_count) {
781 /*
782 * Search which member corresponds to this tag.
783 */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800784 size_t n;
785 size_t edx_end = edx + elements[edx].optional + 1;
Lev Walkin02666192005-07-03 05:31:02 +0000786 if(edx_end > td->elements_count)
787 edx_end = td->elements_count;
788 for(n = edx; n < edx_end; n++) {
789 elm = &td->elements[n];
Lev Walkinf59dac92017-08-26 21:26:51 -0700790 tcv = xer_check_tag(ptr, ch_size, elm->name);
Lev Walkin02666192005-07-03 05:31:02 +0000791 switch(tcv) {
792 case XCT_BOTH:
793 case XCT_OPENING:
794 /*
795 * Process this member.
796 */
797 ctx->step = edx = n;
798 ctx->phase = 2;
799 break;
800 case XCT_UNKNOWN_OP:
801 case XCT_UNKNOWN_BO:
802 continue;
803 default:
804 n = edx_end;
805 break; /* Phase out */
806 }
807 break;
808 }
809 if(n != edx_end)
810 continue;
811 } else {
Lev Walkin24810022017-08-25 12:16:11 -0700812 ASN_DEBUG("Out of defined members: %zu/%u",
Lev Walkin02666192005-07-03 05:31:02 +0000813 edx, td->elements_count);
814 }
Lev Walkin904e65b2005-02-18 14:23:48 +0000815
816 /* It is expected extension */
817 if(IN_EXTENSION_GROUP(specs,
Lev Walkin02666192005-07-03 05:31:02 +0000818 edx + (edx < td->elements_count
819 ? elements[edx].optional : 0))) {
Lev Walkin24810022017-08-25 12:16:11 -0700820 ASN_DEBUG("Got anticipated extension at %zu",
Lev Walkin02666192005-07-03 05:31:02 +0000821 edx);
Lev Walkin904e65b2005-02-18 14:23:48 +0000822 /*
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000823 * Check for (XCT_BOTH or XCT_UNKNOWN_BO)
824 * By using a mask. Only record a pure
825 * <opening> tags.
Lev Walkin904e65b2005-02-18 14:23:48 +0000826 */
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000827 if(tcv & XCT_CLOSING) {
828 /* Found </extension> without body */
829 } else {
830 ctx->left = 1;
831 ctx->phase = 3; /* Skip ...'s */
832 }
833 XER_ADVANCE(ch_size);
834 continue;
Lev Walkin904e65b2005-02-18 14:23:48 +0000835 }
836
837 /* Fall through */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000838 default:
839 break;
840 }
841
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000842 ASN_DEBUG("Unexpected XML tag in SEQUENCE [%c%c%c%c%c%c]",
Lev Walkinf59dac92017-08-26 21:26:51 -0700843 size>0?((const char *)ptr)[0]:'.',
844 size>1?((const char *)ptr)[1]:'.',
845 size>2?((const char *)ptr)[2]:'.',
846 size>3?((const char *)ptr)[3]:'.',
847 size>4?((const char *)ptr)[4]:'.',
848 size>5?((const char *)ptr)[5]:'.');
Lev Walkin1bbc2002004-10-23 13:27:30 +0000849 break;
850 }
851
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000852 ctx->phase = 4; /* "Phase out" on hard failure */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000853 RETURN(RC_FAIL);
854}
855
Lev Walkina9cc46e2004-09-22 16:06:28 +0000856asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000857SEQUENCE_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000858 int ilevel, enum xer_encoder_flags_e flags,
859 asn_app_consume_bytes_f *cb, void *app_key) {
860 asn_enc_rval_t er;
861 int xcan = (flags & XER_F_CANONICAL);
Lev Walkin494fb702017-08-07 20:07:00 -0700862 size_t edx;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000863
864 if(!sptr)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700865 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000866
867 er.encoded = 0;
868
869 for(edx = 0; edx < td->elements_count; edx++) {
870 asn_enc_rval_t tmper;
Lev Walkin5e033762004-09-29 13:26:15 +0000871 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkina9cc46e2004-09-22 16:06:28 +0000872 void *memb_ptr;
873 const char *mname = elm->name;
874 unsigned int mlen = strlen(mname);
875
876 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800877 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
Lev Walkinac589332005-08-22 14:19:28 +0000878 if(!memb_ptr) {
879 if(elm->optional)
880 continue;
881 /* Mandatory element is missing */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700882 ASN__ENCODE_FAILED;
Lev Walkinac589332005-08-22 14:19:28 +0000883 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000884 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800885 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000886 }
887
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700888 if(!xcan) ASN__TEXT_INDENT(1, ilevel);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700889 ASN__CALLBACK3("<", 1, mname, mlen, ">", 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000890
891 /* Print the member itself */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800892 tmper = elm->type->op->xer_encoder(elm->type, memb_ptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000893 ilevel + 1, flags, cb, app_key);
894 if(tmper.encoded == -1) return tmper;
895
Lev Walkin7c1dc052016-03-14 03:08:15 -0700896 ASN__CALLBACK3("</", 2, mname, mlen, ">", 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000897 er.encoded += 5 + (2 * mlen) + tmper.encoded;
898 }
899
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700900 if(!xcan) ASN__TEXT_INDENT(1, ilevel - 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000901
Lev Walkin7c1dc052016-03-14 03:08:15 -0700902 ASN__ENCODED_OK(er);
Lev Walkin942fd082004-10-03 09:13:02 +0000903cb_failed:
Lev Walkin7c1dc052016-03-14 03:08:15 -0700904 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000905}
906
Lev Walkinf15320b2004-06-03 03:38:44 +0000907int
Lev Walkin5e033762004-09-29 13:26:15 +0000908SEQUENCE_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000909 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin494fb702017-08-07 20:07:00 -0700910 size_t edx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000911 int ret;
912
Lev Walkin8e8078a2004-09-26 13:10:40 +0000913 if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000914
915 /* Dump preamble */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000916 if(cb(td->name, strlen(td->name), app_key) < 0
917 || cb(" ::= {", 6, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000918 return -1;
919
Lev Walkin449f8322004-08-20 13:23:42 +0000920 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000921 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000922 const void *memb_ptr;
923
Lev Walkincc93b0f2004-09-10 09:18:20 +0000924 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800925 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkinac589332005-08-22 14:19:28 +0000926 if(!memb_ptr) {
927 if(elm->optional) continue;
928 /* Print <absent> line */
929 /* Fall through */
930 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000931 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800932 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000933 }
934
935 /* Indentation */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000936 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000937
938 /* Print the member's name and stuff */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000939 if(cb(elm->name, strlen(elm->name), app_key) < 0
940 || cb(": ", 2, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000941 return -1;
942
943 /* Print the member itself */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800944 ret = elm->type->op->print_struct(elm->type, memb_ptr, ilevel + 1,
Lev Walkinf15320b2004-06-03 03:38:44 +0000945 cb, app_key);
946 if(ret) return ret;
Lev Walkinf15320b2004-06-03 03:38:44 +0000947 }
948
Lev Walkin8e8078a2004-09-26 13:10:40 +0000949 ilevel--;
950 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000951
Lev Walkin8e8078a2004-09-26 13:10:40 +0000952 return (cb("}", 1, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000953}
954
955void
Lev Walkin8d99d7b2017-08-25 01:06:00 -0700956SEQUENCE_free(const asn_TYPE_descriptor_t *td, void *sptr,
957 enum asn_struct_free_method method) {
Lev Walkinf6853ce2017-08-11 00:50:27 -0700958 size_t edx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000959
960 if(!td || !sptr)
961 return;
962
963 ASN_DEBUG("Freeing %s as SEQUENCE", td->name);
964
Lev Walkin449f8322004-08-20 13:23:42 +0000965 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000966 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000967 void *memb_ptr;
Lev Walkincc93b0f2004-09-10 09:18:20 +0000968 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800969 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000970 if(memb_ptr)
Lev Walkinadcb5862006-03-17 02:11:12 +0000971 ASN_STRUCT_FREE(*elm->type, memb_ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +0000972 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800973 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkinadcb5862006-03-17 02:11:12 +0000974 ASN_STRUCT_FREE_CONTENTS_ONLY(*elm->type, memb_ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +0000975 }
976 }
977
Lev Walkin8d99d7b2017-08-25 01:06:00 -0700978 switch(method) {
979 case ASFM_FREE_EVERYTHING:
980 FREEMEM(sptr);
981 break;
982 case ASFM_FREE_UNDERLYING:
983 break;
984 case ASFM_FREE_UNDERLYING_AND_RESET:
985 memset(sptr, 0,
986 ((asn_SEQUENCE_specifics_t *)(td->specifics))->struct_size);
987 break;
988 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000989}
990
991int
Lev Walkin5e033762004-09-29 13:26:15 +0000992SEQUENCE_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +0000993 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkin494fb702017-08-07 20:07:00 -0700994 size_t edx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000995
996 if(!sptr) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700997 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +0000998 "%s: value not given (%s:%d)",
999 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +00001000 return -1;
1001 }
1002
1003 /*
1004 * Iterate over structure members and check their validity.
1005 */
Lev Walkin449f8322004-08-20 13:23:42 +00001006 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +00001007 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +00001008 const void *memb_ptr;
1009
Lev Walkincc93b0f2004-09-10 09:18:20 +00001010 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001011 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkinac589332005-08-22 14:19:28 +00001012 if(!memb_ptr) {
1013 if(elm->optional)
1014 continue;
Lev Walkin7c1dc052016-03-14 03:08:15 -07001015 ASN__CTFAIL(app_key, td, sptr,
Lev Walkinac589332005-08-22 14:19:28 +00001016 "%s: mandatory element %s absent (%s:%d)",
1017 td->name, elm->name, __FILE__, __LINE__);
1018 return -1;
1019 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001020 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001021 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +00001022 }
1023
Lev Walkin449f8322004-08-20 13:23:42 +00001024 if(elm->memb_constraints) {
1025 int ret = elm->memb_constraints(elm->type, memb_ptr,
Lev Walkin1eded352006-07-13 11:19:01 +00001026 ctfailcb, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +00001027 if(ret) return ret;
1028 } else {
1029 int ret = elm->type->check_constraints(elm->type,
Lev Walkin1eded352006-07-13 11:19:01 +00001030 memb_ptr, ctfailcb, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +00001031 if(ret) return ret;
1032 /*
1033 * Cannot inherit it earlier:
1034 * need to make sure we get the updated version.
1035 */
1036 elm->memb_constraints = elm->type->check_constraints;
1037 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001038 }
1039
1040 return 0;
1041}
Lev Walkin59b176e2005-11-26 11:25:14 +00001042
Lev Walkin24810022017-08-25 12:16:11 -07001043#ifndef ASN_DISABLE_PER_SUPPORT
1044
Lev Walkin59b176e2005-11-26 11:25:14 +00001045asn_dec_rval_t
1046SEQUENCE_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -07001047 const asn_per_constraints_t *constraints, void **sptr,
1048 asn_per_data_t *pd) {
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001049 asn_SEQUENCE_specifics_t *specs = (asn_SEQUENCE_specifics_t *)td->specifics;
Lev Walkin59b176e2005-11-26 11:25:14 +00001050 void *st = *sptr; /* Target structure. */
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001051 int extpresent; /* Extension additions are present */
Lev Walkin59b176e2005-11-26 11:25:14 +00001052 uint8_t *opres; /* Presence of optional root members */
1053 asn_per_data_t opmd;
1054 asn_dec_rval_t rv;
Lev Walkin494fb702017-08-07 20:07:00 -07001055 size_t edx;
Lev Walkin59b176e2005-11-26 11:25:14 +00001056
1057 (void)constraints;
1058
Lev Walkin7c1dc052016-03-14 03:08:15 -07001059 if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx))
1060 ASN__DECODE_FAILED;
Lev Walkin1d9e8dd2005-12-07 05:46:03 +00001061
Lev Walkin59b176e2005-11-26 11:25:14 +00001062 if(!st) {
1063 st = *sptr = CALLOC(1, specs->struct_size);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001064 if(!st) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +00001065 }
1066
1067 ASN_DEBUG("Decoding %s as SEQUENCE (UPER)", td->name);
1068
1069 /* Handle extensions */
1070 if(specs->ext_before >= 0) {
1071 extpresent = per_get_few_bits(pd, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001072 if(extpresent < 0) ASN__DECODE_STARVED;
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001073 } else {
1074 extpresent = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +00001075 }
1076
1077 /* Prepare a place and read-in the presence bitmap */
Lev Walkin73a5cc62007-06-24 08:45:31 +00001078 memset(&opmd, 0, sizeof(opmd));
Lev Walkin59b176e2005-11-26 11:25:14 +00001079 if(specs->roms_count) {
1080 opres = (uint8_t *)MALLOC(((specs->roms_count + 7) >> 3) + 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001081 if(!opres) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +00001082 /* Get the presence map */
1083 if(per_get_many_bits(pd, opres, 0, specs->roms_count)) {
1084 FREEMEM(opres);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001085 ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +00001086 }
1087 opmd.buffer = opres;
Lev Walkin59b176e2005-11-26 11:25:14 +00001088 opmd.nbits = specs->roms_count;
1089 ASN_DEBUG("Read in presence bitmap for %s of %d bits (%x..)",
1090 td->name, specs->roms_count, *opres);
1091 } else {
1092 opres = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +00001093 }
1094
1095 /*
1096 * Get the sequence ROOT elements.
1097 */
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001098 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001099 asn_TYPE_member_t *elm = &td->elements[edx];
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001100 void *memb_ptr; /* Pointer to the member */
Lev Walkin59b176e2005-11-26 11:25:14 +00001101 void **memb_ptr2; /* Pointer to that pointer */
1102
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001103 if(IN_EXTENSION_GROUP(specs, edx))
1104 continue;
1105
Lev Walkin59b176e2005-11-26 11:25:14 +00001106 /* Fetch the pointer to this member */
1107 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001108 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
Lev Walkin59b176e2005-11-26 11:25:14 +00001109 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001110 memb_ptr = (char *)st + elm->memb_offset;
Lev Walkin59b176e2005-11-26 11:25:14 +00001111 memb_ptr2 = &memb_ptr;
1112 }
1113
1114 /* Deal with optionality */
1115 if(elm->optional) {
1116 int present = per_get_few_bits(&opmd, 1);
1117 ASN_DEBUG("Member %s->%s is optional, p=%d (%d->%d)",
1118 td->name, elm->name, present,
1119 (int)opmd.nboff, (int)opmd.nbits);
1120 if(present == 0) {
1121 /* This element is not present */
1122 if(elm->default_value) {
1123 /* Fill-in DEFAULT */
Lev Walkin523de9e2006-08-18 01:34:18 +00001124 if(elm->default_value(1, memb_ptr2)) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001125 FREEMEM(opres);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001126 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +00001127 }
Lev Walkin8032f7a2007-06-27 01:54:57 +00001128 ASN_DEBUG("Filled-in default");
Lev Walkin59b176e2005-11-26 11:25:14 +00001129 }
1130 /* The member is just not present */
1131 continue;
1132 }
1133 /* Fall through */
1134 }
1135
1136 /* Fetch the member from the stream */
Lev Walkinf6853ce2017-08-11 00:50:27 -07001137 ASN_DEBUG("Decoding member \"%s\" in %s", elm->name, td->name);
Lev Walkin9de6cd82017-08-10 05:47:46 -07001138
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001139 if((elm->flags & ATF_OPEN_TYPE) && elm->type_selector) {
1140 rv = OPEN_TYPE_uper_get(opt_codec_ctx, td, st, elm, pd);
1141 } else {
Lev Walkinf59dac92017-08-26 21:26:51 -07001142 rv = elm->type->op->uper_decoder(opt_codec_ctx, elm->type,
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001143 elm->per_constraints, memb_ptr2, pd);
1144 }
1145 if(rv.code != RC_OK) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001146 ASN_DEBUG("Failed decode %s in %s",
1147 elm->name, td->name);
1148 FREEMEM(opres);
1149 return rv;
1150 }
1151 }
1152
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001153 /* Optionality map is not needed anymore */
1154 FREEMEM(opres);
1155
Lev Walkin59b176e2005-11-26 11:25:14 +00001156 /*
1157 * Deal with extensions.
1158 */
1159 if(extpresent) {
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001160 ssize_t bmlength;
1161 uint8_t *epres; /* Presence of extension members */
1162 asn_per_data_t epmd;
Lev Walkin59b176e2005-11-26 11:25:14 +00001163
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001164 bmlength = uper_get_nslength(pd);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001165 if(bmlength < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +00001166
Lev Walkinaaae9bf2010-10-24 16:56:30 -07001167 ASN_DEBUG("Extensions %ld present in %s", (long)bmlength, td->name);
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001168
1169 epres = (uint8_t *)MALLOC((bmlength + 15) >> 3);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001170 if(!epres) ASN__DECODE_STARVED;
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001171
1172 /* Get the extensions map */
Simo Sorcece71d912015-09-03 16:34:20 -04001173 if(per_get_many_bits(pd, epres, 0, bmlength)) {
1174 FREEMEM(epres);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001175 ASN__DECODE_STARVED;
Simo Sorcece71d912015-09-03 16:34:20 -04001176 }
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001177
Lev Walkin73a5cc62007-06-24 08:45:31 +00001178 memset(&epmd, 0, sizeof(epmd));
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001179 epmd.buffer = epres;
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001180 epmd.nbits = bmlength;
Lev Walkinaaae9bf2010-10-24 16:56:30 -07001181 ASN_DEBUG("Read in extensions bitmap for %s of %ld bits (%x..)",
1182 td->name, (long)bmlength, *epres);
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001183
1184 /* Go over extensions and read them in */
1185 for(edx = specs->ext_after + 1; edx < td->elements_count; edx++) {
1186 asn_TYPE_member_t *elm = &td->elements[edx];
1187 void *memb_ptr; /* Pointer to the member */
1188 void **memb_ptr2; /* Pointer to that pointer */
1189 int present;
1190
1191 if(!IN_EXTENSION_GROUP(specs, edx)) {
Lev Walkin24810022017-08-25 12:16:11 -07001192 ASN_DEBUG("%zu is not an extension", edx);
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001193 continue;
1194 }
1195
1196 /* Fetch the pointer to this member */
1197 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001198 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001199 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001200 memb_ptr = (void *)((char *)st + elm->memb_offset);
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001201 memb_ptr2 = &memb_ptr;
1202 }
1203
1204 present = per_get_few_bits(&epmd, 1);
1205 if(present <= 0) {
1206 if(present < 0) break; /* No more extensions */
1207 continue;
1208 }
1209
1210 ASN_DEBUG("Decoding member %s in %s %p", elm->name, td->name, *memb_ptr2);
Lev Walkin9218bc12007-06-27 04:09:37 +00001211 rv = uper_open_type_get(opt_codec_ctx, elm->type,
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001212 elm->per_constraints, memb_ptr2, pd);
1213 if(rv.code != RC_OK) {
1214 FREEMEM(epres);
1215 return rv;
1216 }
1217 }
1218
1219 /* Skip over overflow extensions which aren't present
1220 * in this system's version of the protocol */
Lev Walkin06230f72007-06-26 09:52:44 +00001221 for(;;) {
Lev Walkina2987ea2007-06-26 23:56:54 +00001222 ASN_DEBUG("Getting overflow extensions");
Lev Walkin06230f72007-06-26 09:52:44 +00001223 switch(per_get_few_bits(&epmd, 1)) {
1224 case -1: break;
1225 case 0: continue;
1226 default:
Lev Walkin9218bc12007-06-27 04:09:37 +00001227 if(uper_open_type_skip(opt_codec_ctx, pd)) {
Lev Walkin06230f72007-06-26 09:52:44 +00001228 FREEMEM(epres);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001229 ASN__DECODE_STARVED;
Lev Walkin06230f72007-06-26 09:52:44 +00001230 }
Lev Walkin59b176e2005-11-26 11:25:14 +00001231 }
Lev Walkin06230f72007-06-26 09:52:44 +00001232 break;
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001233 }
Lev Walkin59b176e2005-11-26 11:25:14 +00001234
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001235 FREEMEM(epres);
1236 }
1237
1238 /* Fill DEFAULT members in extensions */
1239 for(edx = specs->roms_count; edx < specs->roms_count
1240 + specs->aoms_count; edx++) {
1241 asn_TYPE_member_t *elm = &td->elements[edx];
1242 void **memb_ptr2; /* Pointer to member pointer */
1243
1244 if(!elm->default_value) continue;
1245
1246 /* Fetch the pointer to this member */
1247 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001248 memb_ptr2 = (void **)((char *)st
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001249 + elm->memb_offset);
1250 if(*memb_ptr2) continue;
1251 } else {
1252 continue; /* Extensions are all optionals */
1253 }
1254
1255 /* Set default value */
1256 if(elm->default_value(1, memb_ptr2)) {
Lev Walkin7c1dc052016-03-14 03:08:15 -07001257 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +00001258 }
1259 }
1260
1261 rv.consumed = 0;
1262 rv.code = RC_OK;
1263 return rv;
1264}
1265
Lev Walkin62258e22007-06-23 23:50:25 +00001266static int
1267SEQUENCE_handle_extensions(asn_TYPE_descriptor_t *td, void *sptr,
1268 asn_per_outp_t *po1, asn_per_outp_t *po2) {
1269 asn_SEQUENCE_specifics_t *specs
1270 = (asn_SEQUENCE_specifics_t *)td->specifics;
Lev Walkinf55a6dd2007-06-24 00:35:51 +00001271 int exts_present = 0;
1272 int exts_count = 0;
Lev Walkin494fb702017-08-07 20:07:00 -07001273 size_t edx;
Lev Walkin62258e22007-06-23 23:50:25 +00001274
1275 if(specs->ext_before < 0)
1276 return 0;
1277
1278 /* Find out which extensions are present */
1279 for(edx = specs->ext_after + 1; edx < td->elements_count; edx++) {
1280 asn_TYPE_member_t *elm = &td->elements[edx];
1281 void *memb_ptr; /* Pointer to the member */
1282 void **memb_ptr2; /* Pointer to that pointer */
1283 int present;
1284
Lev Walkinf55a6dd2007-06-24 00:35:51 +00001285 if(!IN_EXTENSION_GROUP(specs, edx)) {
Lev Walkin24810022017-08-25 12:16:11 -07001286 ASN_DEBUG("%s (@%zu) is not extension", elm->type->name, edx);
Lev Walkin62258e22007-06-23 23:50:25 +00001287 continue;
Lev Walkinf55a6dd2007-06-24 00:35:51 +00001288 }
Lev Walkin62258e22007-06-23 23:50:25 +00001289
1290 /* Fetch the pointer to this member */
1291 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001292 memb_ptr2 = (void **)((char *)sptr + elm->memb_offset);
Lev Walkin62258e22007-06-23 23:50:25 +00001293 present = (*memb_ptr2 != 0);
1294 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001295 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkin62258e22007-06-23 23:50:25 +00001296 memb_ptr2 = &memb_ptr;
1297 present = 1;
1298 }
1299
Lev Walkin24810022017-08-25 12:16:11 -07001300 ASN_DEBUG("checking %s (@%zu) present => %d",
Lev Walkin06230f72007-06-26 09:52:44 +00001301 elm->type->name, edx, present);
Lev Walkinf55a6dd2007-06-24 00:35:51 +00001302 exts_count++;
1303 exts_present += present;
Lev Walkin62258e22007-06-23 23:50:25 +00001304
1305 /* Encode as presence marker */
1306 if(po1 && per_put_few_bits(po1, present, 1))
1307 return -1;
1308 /* Encode as open type field */
Lev Walkin9218bc12007-06-27 04:09:37 +00001309 if(po2 && present && uper_open_type_put(elm->type,
Lev Walkin62258e22007-06-23 23:50:25 +00001310 elm->per_constraints, *memb_ptr2, po2))
1311 return -1;
1312
1313 }
1314
Lev Walkinf55a6dd2007-06-24 00:35:51 +00001315 return exts_present ? exts_count : 0;
Lev Walkin62258e22007-06-23 23:50:25 +00001316}
1317
Lev Walkin523de9e2006-08-18 01:34:18 +00001318asn_enc_rval_t
1319SEQUENCE_encode_uper(asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -07001320 const asn_per_constraints_t *constraints, void *sptr,
1321 asn_per_outp_t *po) {
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001322 asn_SEQUENCE_specifics_t *specs
Lev Walkin523de9e2006-08-18 01:34:18 +00001323 = (asn_SEQUENCE_specifics_t *)td->specifics;
1324 asn_enc_rval_t er;
Lev Walkin62258e22007-06-23 23:50:25 +00001325 int n_extensions;
Lev Walkin494fb702017-08-07 20:07:00 -07001326 size_t edx;
1327 size_t i;
Lev Walkin523de9e2006-08-18 01:34:18 +00001328
1329 (void)constraints;
1330
1331 if(!sptr)
Lev Walkin7c1dc052016-03-14 03:08:15 -07001332 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001333
1334 er.encoded = 0;
1335
1336 ASN_DEBUG("Encoding %s as SEQUENCE (UPER)", td->name);
Lev Walkin62258e22007-06-23 23:50:25 +00001337
1338
1339 /*
1340 * X.691#18.1 Whether structure is extensible
1341 * and whether to encode extensions
1342 */
1343 if(specs->ext_before >= 0) {
1344 n_extensions = SEQUENCE_handle_extensions(td, sptr, 0, 0);
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001345 if(n_extensions < 0)
Lev Walkin494fb702017-08-07 20:07:00 -07001346 ASN__ENCODE_FAILED;
1347 if(per_put_few_bits(po, n_extensions ? 1 : 0, 1))
1348 ASN__ENCODE_FAILED;
Lev Walkin62258e22007-06-23 23:50:25 +00001349 } else {
1350 n_extensions = 0; /* There are no extensions to encode */
1351 }
Lev Walkin523de9e2006-08-18 01:34:18 +00001352
1353 /* Encode a presence bitmap */
1354 for(i = 0; i < specs->roms_count; i++) {
Lev Walkinc46b7cb2006-08-18 02:27:55 +00001355 asn_TYPE_member_t *elm;
Lev Walkin523de9e2006-08-18 01:34:18 +00001356 void *memb_ptr; /* Pointer to the member */
1357 void **memb_ptr2; /* Pointer to that pointer */
Lev Walkin523de9e2006-08-18 01:34:18 +00001358 int present;
1359
Lev Walkinc46b7cb2006-08-18 02:27:55 +00001360 edx = specs->oms[i];
1361 elm = &td->elements[edx];
1362
Lev Walkin523de9e2006-08-18 01:34:18 +00001363 /* Fetch the pointer to this member */
1364 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001365 memb_ptr2 = (void **)((char *)sptr + elm->memb_offset);
Lev Walkin523de9e2006-08-18 01:34:18 +00001366 present = (*memb_ptr2 != 0);
1367 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001368 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkin523de9e2006-08-18 01:34:18 +00001369 memb_ptr2 = &memb_ptr;
1370 present = 1;
1371 }
1372
1373 /* Eliminate default values */
1374 if(present && elm->default_value
1375 && elm->default_value(0, memb_ptr2) == 1)
1376 present = 0;
1377
1378 ASN_DEBUG("Element %s %s %s->%s is %s",
1379 elm->flags & ATF_POINTER ? "ptr" : "inline",
1380 elm->default_value ? "def" : "wtv",
1381 td->name, elm->name, present ? "present" : "absent");
1382 if(per_put_few_bits(po, present, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001383 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001384 }
1385
1386 /*
Lev Walkin62258e22007-06-23 23:50:25 +00001387 * Encode the sequence ROOT elements.
Lev Walkin523de9e2006-08-18 01:34:18 +00001388 */
Lev Walkin62258e22007-06-23 23:50:25 +00001389 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 +00001390 for(edx = 0; edx < ((specs->ext_after < 0)
1391 ? td->elements_count : specs->ext_before - 1); edx++) {
1392
Lev Walkin523de9e2006-08-18 01:34:18 +00001393 asn_TYPE_member_t *elm = &td->elements[edx];
1394 void *memb_ptr; /* Pointer to the member */
1395 void **memb_ptr2; /* Pointer to that pointer */
1396
Lev Walkin4d22b622007-07-23 09:58:25 +00001397 if(IN_EXTENSION_GROUP(specs, edx))
1398 continue;
1399
Lev Walkina2987ea2007-06-26 23:56:54 +00001400 ASN_DEBUG("About to encode %s", elm->type->name);
1401
Lev Walkin523de9e2006-08-18 01:34:18 +00001402 /* Fetch the pointer to this member */
1403 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001404 memb_ptr2 = (void **)((char *)sptr + elm->memb_offset);
Lev Walkin523de9e2006-08-18 01:34:18 +00001405 if(!*memb_ptr2) {
Lev Walkin24810022017-08-25 12:16:11 -07001406 ASN_DEBUG("Element %s %zu not present",
Lev Walkin523de9e2006-08-18 01:34:18 +00001407 elm->name, edx);
1408 if(elm->optional)
1409 continue;
1410 /* Mandatory element is missing */
Lev Walkin7c1dc052016-03-14 03:08:15 -07001411 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001412 }
1413 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001414 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkin523de9e2006-08-18 01:34:18 +00001415 memb_ptr2 = &memb_ptr;
1416 }
1417
1418 /* Eliminate default values */
Lev Walkindbd091a2007-06-27 02:11:08 +00001419 if(elm->default_value && elm->default_value(0, memb_ptr2) == 1)
Lev Walkin523de9e2006-08-18 01:34:18 +00001420 continue;
1421
Lev Walkin4d22b622007-07-23 09:58:25 +00001422 ASN_DEBUG("Encoding %s->%s", td->name, elm->name);
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001423 er = elm->type->op->uper_encoder(elm->type, elm->per_constraints,
Lev Walkin523de9e2006-08-18 01:34:18 +00001424 *memb_ptr2, po);
1425 if(er.encoded == -1)
1426 return er;
1427 }
1428
Lev Walkin62258e22007-06-23 23:50:25 +00001429 /* No extensions to encode */
Lev Walkin7c1dc052016-03-14 03:08:15 -07001430 if(!n_extensions) ASN__ENCODED_OK(er);
Lev Walkin62258e22007-06-23 23:50:25 +00001431
1432 ASN_DEBUG("Length of %d bit-map", n_extensions);
1433 /* #18.8. Write down the presence bit-map length. */
1434 if(uper_put_nslength(po, n_extensions))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001435 ASN__ENCODE_FAILED;
Lev Walkin62258e22007-06-23 23:50:25 +00001436
1437 ASN_DEBUG("Bit-map of %d elements", n_extensions);
1438 /* #18.7. Encoding the extensions presence bit-map. */
1439 /* TODO: act upon NOTE in #18.7 for canonical PER */
1440 if(SEQUENCE_handle_extensions(td, sptr, po, 0) != n_extensions)
Lev Walkin7c1dc052016-03-14 03:08:15 -07001441 ASN__ENCODE_FAILED;
Lev Walkin62258e22007-06-23 23:50:25 +00001442
1443 ASN_DEBUG("Writing %d extensions", n_extensions);
1444 /* #18.9. Encode extensions as open type fields. */
1445 if(SEQUENCE_handle_extensions(td, sptr, 0, po) != n_extensions)
Lev Walkin7c1dc052016-03-14 03:08:15 -07001446 ASN__ENCODE_FAILED;
Lev Walkin62258e22007-06-23 23:50:25 +00001447
Lev Walkin7c1dc052016-03-14 03:08:15 -07001448 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +00001449}
1450
Lev Walkin24810022017-08-25 12:16:11 -07001451#endif /* ASN_DISABLE_PER_SUPPORT */
1452
Lev Walkincd2f48e2017-08-10 02:14:59 -07001453int
1454SEQUENCE_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
1455 const void *bptr) {
1456 size_t edx;
1457
1458 for(edx = 0; edx < td->elements_count; edx++) {
1459 asn_TYPE_member_t *elm = &td->elements[edx];
1460 const void *amemb;
1461 const void *bmemb;
1462 int ret;
1463
1464 if(elm->flags & ATF_POINTER) {
1465 amemb =
1466 *(const void *const *)((const char *)aptr + elm->memb_offset);
1467 bmemb =
1468 *(const void *const *)((const char *)bptr + elm->memb_offset);
1469 if(!amemb) {
1470 if(!bmemb) continue;
1471 return -1;
1472 } else if(!bmemb) {
1473 return 1;
1474 }
1475 } else {
1476 amemb = (const void *)((const char *)aptr + elm->memb_offset);
1477 bmemb = (const void *)((const char *)bptr + elm->memb_offset);
1478 }
1479
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001480 ret = elm->type->op->compare_struct(elm->type, amemb, bmemb);
Lev Walkincd2f48e2017-08-10 02:14:59 -07001481 if(ret != 0) return ret;
1482 }
1483
1484 return 0;
1485}
1486
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001487asn_TYPE_operation_t asn_OP_SEQUENCE = {
1488 SEQUENCE_free,
1489 SEQUENCE_print,
1490 SEQUENCE_compare,
1491 SEQUENCE_constraint,
1492 SEQUENCE_decode_ber,
1493 SEQUENCE_encode_der,
1494 SEQUENCE_decode_xer,
1495 SEQUENCE_encode_xer,
1496#ifdef ASN_DISABLE_OER_SUPPORT
1497 0,
1498 0,
1499#else
1500 SEQUENCE_decode_oer,
1501 SEQUENCE_encode_oer,
1502#endif /* ASN_DISABLE_OER_SUPPORT */
1503#ifdef ASN_DISABLE_PER_SUPPORT
1504 0,
1505 0,
1506#else
1507 SEQUENCE_decode_uper,
1508 SEQUENCE_encode_uper,
1509#endif /* ASN_DISABLE_PER_SUPPORT */
1510 0 /* Use generic outmost tag fetcher */
1511};
1512