blob: d4c16a6fb384a71ec3eed6b7fe686ea51805840e [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 Walkinafbf2a92017-09-12 23:30:27 -0700113SEQUENCE_decode_ber(const 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 Walkind84f6032017-10-03 16:33:59 -0700118 const asn_SEQUENCE_specifics_t *specs = (const asn_SEQUENCE_specifics_t *)td->specifics;
Lev Walkin5e033762004-09-29 13:26:15 +0000119 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 Walkineff98a52017-09-13 22:24:35 +0000315 asn_TYPE_tag2member_t key = {0, 0, 0, 0};
316 key.el_tag = tlv_tag;
317 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 */
Lev Walkin75b6fe42017-08-27 01:03:22 -0700413 if(elements[edx].flags & ATF_OPEN_TYPE) {
Lev Walkinc10d30a2017-08-27 00:38:21 -0700414 rval = OPEN_TYPE_ber_get(opt_codec_ctx, td, st, &elements[edx], ptr, LEFT);
415 } else {
416 rval = elements[edx].type->op->ber_decoder(opt_codec_ctx,
417 elements[edx].type,
418 memb_ptr2, ptr, LEFT,
419 elements[edx].tag_mode);
420 }
Lev Walkin24810022017-08-25 12:16:11 -0700421 ASN_DEBUG("In %s SEQUENCE decoded %zu %s of %d "
Lev Walkin5beb7de2004-09-24 20:59:27 +0000422 "in %d bytes rval.code %d, size=%d",
Lev Walkin449f8322004-08-20 13:23:42 +0000423 td->name, edx, elements[edx].type->name,
Lev Walkin5beb7de2004-09-24 20:59:27 +0000424 (int)LEFT, (int)rval.consumed, rval.code, (int)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000425 switch(rval.code) {
426 case RC_OK:
427 break;
428 case RC_WMORE: /* More data expected */
429 if(!SIZE_VIOLATION) {
430 ADVANCE(rval.consumed);
431 RETURN(RC_WMORE);
432 }
Lev Walkin5beb7de2004-09-24 20:59:27 +0000433 ASN_DEBUG("Size violation (c->l=%ld <= s=%ld)",
434 (long)ctx->left, (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000435 /* Fall through */
436 case RC_FAIL: /* Fatal error */
437 RETURN(RC_FAIL);
438 } /* switch(rval) */
439
440 ADVANCE(rval.consumed);
441 } /* for(all structure members) */
442
443 phase3:
444 ctx->phase = 3;
445 case 3: /* 00 and other tags expected */
446 case 4: /* only 00's expected */
447
448 ASN_DEBUG("SEQUENCE %s Leftover: %ld, size = %ld",
Lev Walkin449f8322004-08-20 13:23:42 +0000449 td->name, (long)ctx->left, (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000450
451 /*
452 * Skip everything until the end of the SEQUENCE.
453 */
454 while(ctx->left) {
455 ssize_t tl, ll;
456
457 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
458 switch(tl) {
459 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
460 /* Fall through */
461 case -1: RETURN(RC_FAIL);
462 }
463
464 /*
465 * If expected <0><0>...
466 */
467 if(ctx->left < 0
Lev Walkin8c3b8542005-03-10 18:52:02 +0000468 && ((const uint8_t *)ptr)[0] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000469 if(LEFT < 2) {
470 if(SIZE_VIOLATION)
471 RETURN(RC_FAIL);
472 else
473 RETURN(RC_WMORE);
Lev Walkin8c3b8542005-03-10 18:52:02 +0000474 } else if(((const uint8_t *)ptr)[1] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000475 /*
476 * Correctly finished with <0><0>.
477 */
478 ADVANCE(2);
479 ctx->left++;
480 ctx->phase = 4;
481 continue;
482 }
483 }
484
Lev Walkin449f8322004-08-20 13:23:42 +0000485 if(!IN_EXTENSION_GROUP(specs, td->elements_count)
Lev Walkinf15320b2004-06-03 03:38:44 +0000486 || ctx->phase == 4) {
487 ASN_DEBUG("Unexpected continuation "
488 "of a non-extensible type "
489 "%s (SEQUENCE): %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000490 td->name,
Lev Walkinf15320b2004-06-03 03:38:44 +0000491 ber_tlv_tag_string(tlv_tag));
492 RETURN(RC_FAIL);
493 }
494
Lev Walkinbaaa24f2004-09-29 14:19:14 +0000495 ll = ber_skip_length(opt_codec_ctx,
Lev Walkinf15320b2004-06-03 03:38:44 +0000496 BER_TLV_CONSTRUCTED(ptr),
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800497 (const char *)ptr + tl, LEFT - tl);
Lev Walkinf15320b2004-06-03 03:38:44 +0000498 switch(ll) {
499 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
500 /* Fall through */
501 case -1: RETURN(RC_FAIL);
502 }
503
504 ADVANCE(tl + ll);
505 }
506
507 PHASE_OUT(ctx);
508 }
509
510 RETURN(RC_OK);
511}
512
Lev Walkin4c36e302004-06-06 07:20:02 +0000513
Lev Walkinf15320b2004-06-03 03:38:44 +0000514/*
515 * The DER encoder of the SEQUENCE type.
516 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000517asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000518SEQUENCE_encode_der(asn_TYPE_descriptor_t *td,
Lev Walkinac589332005-08-22 14:19:28 +0000519 void *sptr, int tag_mode, ber_tlv_tag_t tag,
Lev Walkinf15320b2004-06-03 03:38:44 +0000520 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000521 size_t computed_size = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000522 asn_enc_rval_t erval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000523 ssize_t ret;
Lev Walkin494fb702017-08-07 20:07:00 -0700524 size_t edx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000525
526 ASN_DEBUG("%s %s as SEQUENCE",
Lev Walkin449f8322004-08-20 13:23:42 +0000527 cb?"Encoding":"Estimating", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000528
529 /*
530 * Gather the length of the underlying members sequence.
531 */
Lev Walkin449f8322004-08-20 13:23:42 +0000532 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000533 asn_TYPE_member_t *elm = &td->elements[edx];
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400534
535 void *memb_ptr; /* Pointer to the member */
536 void **memb_ptr2; /* Pointer to that pointer */
537
Lev Walkincc93b0f2004-09-10 09:18:20 +0000538 if(elm->flags & ATF_POINTER) {
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400539 memb_ptr2 = (void **)((char *)sptr + elm->memb_offset);
540 if(!*memb_ptr2) {
Lev Walkin28ba9db2017-08-31 02:06:58 -0700541 ASN_DEBUG("Element %s %zu not present",
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400542 elm->name, edx);
543 if(elm->optional)
544 continue;
Lev Walkinac589332005-08-22 14:19:28 +0000545 /* Mandatory element is missing */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700546 ASN__ENCODE_FAILED;
Lev Walkinac589332005-08-22 14:19:28 +0000547 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000548 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800549 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400550 memb_ptr2 = &memb_ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000551 }
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400552
553 /* Eliminate default values */
554 if(elm->default_value && elm->default_value(0, memb_ptr2) == 1)
555 continue;
556
557 erval = elm->type->op->der_encoder(elm->type, *memb_ptr2,
Lev Walkinf15320b2004-06-03 03:38:44 +0000558 elm->tag_mode, elm->tag,
559 0, 0);
560 if(erval.encoded == -1)
561 return erval;
562 computed_size += erval.encoded;
Lev Walkin24810022017-08-25 12:16:11 -0700563 ASN_DEBUG("Member %zu %s estimated %ld bytes",
Lev Walkinf15320b2004-06-03 03:38:44 +0000564 edx, elm->name, (long)erval.encoded);
565 }
566
567 /*
568 * Encode the TLV for the sequence itself.
569 */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000570 ret = der_write_tags(td, computed_size, tag_mode, 1, tag, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +0000571 ASN_DEBUG("Wrote tags: %ld (+%ld)", (long)ret, (long)computed_size);
Lev Walkinac589332005-08-22 14:19:28 +0000572 if(ret == -1)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700573 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000574 erval.encoded = computed_size + ret;
575
Lev Walkin7c1dc052016-03-14 03:08:15 -0700576 if(!cb) ASN__ENCODED_OK(erval);
Lev Walkinf15320b2004-06-03 03:38:44 +0000577
578 /*
579 * Encode all members.
580 */
Lev Walkin449f8322004-08-20 13:23:42 +0000581 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000582 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkina9cc46e2004-09-22 16:06:28 +0000583 asn_enc_rval_t tmperval;
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400584 void *memb_ptr; /* Pointer to the member */
585 void **memb_ptr2; /* Pointer to that pointer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000586
Lev Walkincc93b0f2004-09-10 09:18:20 +0000587 if(elm->flags & ATF_POINTER) {
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400588 memb_ptr2 = (void **)((char *)sptr + elm->memb_offset);
589 if(!*memb_ptr2) continue;
Lev Walkinf15320b2004-06-03 03:38:44 +0000590 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800591 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400592 memb_ptr2 = &memb_ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000593 }
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400594
595 /* Eliminate default values */
596 if(elm->default_value && elm->default_value(0, memb_ptr2) == 1)
597 continue;
598
599 tmperval = elm->type->op->der_encoder(elm->type, *memb_ptr2,
600 elm->tag_mode, elm->tag, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +0000601 if(tmperval.encoded == -1)
602 return tmperval;
603 computed_size -= tmperval.encoded;
Lev Walkin24810022017-08-25 12:16:11 -0700604 ASN_DEBUG("Member %zu %s of SEQUENCE %s encoded in %ld bytes",
Lev Walkina6a926a2005-03-02 01:54:28 +0000605 edx, elm->name, td->name, (long)tmperval.encoded);
Lev Walkinf15320b2004-06-03 03:38:44 +0000606 }
607
Lev Walkinac589332005-08-22 14:19:28 +0000608 if(computed_size != 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000609 /*
610 * Encoded size is not equal to the computed size.
611 */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700612 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000613
Lev Walkin7c1dc052016-03-14 03:08:15 -0700614 ASN__ENCODED_OK(erval);
Lev Walkinf15320b2004-06-03 03:38:44 +0000615}
616
Lev Walkinc61f3862005-02-14 17:21:22 +0000617
Lev Walkin1bbc2002004-10-23 13:27:30 +0000618#undef XER_ADVANCE
Lev Walkinf59dac92017-08-26 21:26:51 -0700619#define XER_ADVANCE(num_bytes) \
620 do { \
621 size_t num = (num_bytes); \
622 ptr = ((const char *)ptr) + num; \
623 size -= num; \
624 consumed_myself += num; \
625 } while(0)
Lev Walkin1bbc2002004-10-23 13:27:30 +0000626
Lev Walkinc61f3862005-02-14 17:21:22 +0000627/*
628 * Decode the XER (XML) data.
629 */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000630asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -0700631SEQUENCE_decode_xer(const asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkin1bbc2002004-10-23 13:27:30 +0000632 void **struct_ptr, const char *opt_mname,
Lev Walkinf59dac92017-08-26 21:26:51 -0700633 const void *ptr, size_t size) {
Lev Walkin1bbc2002004-10-23 13:27:30 +0000634 /*
635 * Bring closer parts of structure description.
636 */
Lev Walkind84f6032017-10-03 16:33:59 -0700637 const asn_SEQUENCE_specifics_t *specs
638 = (const asn_SEQUENCE_specifics_t *)td->specifics;
Lev Walkin1bbc2002004-10-23 13:27:30 +0000639 asn_TYPE_member_t *elements = td->elements;
640 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
641
642 /*
Lev Walkinc61f3862005-02-14 17:21:22 +0000643 * ... and parts of the structure being constructed.
Lev Walkin1bbc2002004-10-23 13:27:30 +0000644 */
645 void *st = *struct_ptr; /* Target structure. */
646 asn_struct_ctx_t *ctx; /* Decoder context */
647
Lev Walkinc61f3862005-02-14 17:21:22 +0000648 asn_dec_rval_t rval; /* Return value from a decoder */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000649 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
Lev Walkin494fb702017-08-07 20:07:00 -0700650 size_t edx; /* Element index */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000651
652 /*
653 * Create the target structure if it is not present already.
654 */
655 if(st == 0) {
656 st = *struct_ptr = CALLOC(1, specs->struct_size);
657 if(st == 0) RETURN(RC_FAIL);
658 }
659
660 /*
661 * Restore parsing context.
662 */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800663 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkin1bbc2002004-10-23 13:27:30 +0000664
665
666 /*
667 * Phases of XER/XML processing:
668 * Phase 0: Check that the opening tag matches our expectations.
669 * Phase 1: Processing body and reacting on closing tag.
670 * Phase 2: Processing inner type.
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000671 * Phase 3: Skipping unknown extensions.
672 * Phase 4: PHASED OUT
Lev Walkin1bbc2002004-10-23 13:27:30 +0000673 */
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000674 for(edx = ctx->step; ctx->phase <= 3;) {
Lev Walkin1bbc2002004-10-23 13:27:30 +0000675 pxer_chunk_type_e ch_type; /* XER chunk type */
676 ssize_t ch_size; /* Chunk size */
677 xer_check_tag_e tcv; /* Tag check value */
678 asn_TYPE_member_t *elm;
679
680 /*
Lev Walkinc61f3862005-02-14 17:21:22 +0000681 * Go inside the inner member of a sequence.
Lev Walkin1bbc2002004-10-23 13:27:30 +0000682 */
683 if(ctx->phase == 2) {
684 asn_dec_rval_t tmprval;
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400685 void *memb_ptr_dontuse; /* Pointer to the member */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000686 void **memb_ptr2; /* Pointer to that pointer */
687
Lev Walkinabf68892004-10-26 10:12:14 +0000688 elm = &td->elements[edx];
689
Lev Walkin1bbc2002004-10-23 13:27:30 +0000690 if(elm->flags & ATF_POINTER) {
691 /* Member is a pointer to another structure */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800692 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
Lev Walkin1bbc2002004-10-23 13:27:30 +0000693 } else {
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400694 memb_ptr_dontuse = (char *)st + elm->memb_offset;
695 memb_ptr2 = &memb_ptr_dontuse; /* Only use of memb_ptr_dontuse */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000696 }
697
Lev Walkin75b6fe42017-08-27 01:03:22 -0700698 if(elm->flags & ATF_OPEN_TYPE) {
Lev Walkinf59dac92017-08-26 21:26:51 -0700699 tmprval = OPEN_TYPE_xer_get(opt_codec_ctx, td, st, elm, ptr, size);
700 } else {
701 /* Invoke the inner type decoder, m.b. multiple times */
702 tmprval = elm->type->op->xer_decoder(opt_codec_ctx,
703 elm->type, memb_ptr2, elm->name,
704 ptr, size);
705 }
Lev Walkin1bbc2002004-10-23 13:27:30 +0000706 XER_ADVANCE(tmprval.consumed);
707 if(tmprval.code != RC_OK)
708 RETURN(tmprval.code);
Lev Walkinc61f3862005-02-14 17:21:22 +0000709 ctx->phase = 1; /* Back to body processing */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000710 ctx->step = ++edx;
Lev Walkinc61f3862005-02-14 17:21:22 +0000711 ASN_DEBUG("XER/SEQUENCE phase => %d, step => %d",
712 ctx->phase, ctx->step);
Lev Walkin1bbc2002004-10-23 13:27:30 +0000713 /* Fall through */
714 }
715
716 /*
717 * Get the next part of the XML stream.
718 */
Lev Walkinf59dac92017-08-26 21:26:51 -0700719 ch_size = xer_next_token(&ctx->context, ptr, size,
Lev Walkin1e443962005-02-18 18:06:36 +0000720 &ch_type);
Lev Walkin97363482016-01-24 19:23:02 -0800721 if(ch_size == -1) {
722 RETURN(RC_FAIL);
Lev Walkinf59dac92017-08-26 21:26:51 -0700723 } else {
Lev Walkin1bbc2002004-10-23 13:27:30 +0000724 switch(ch_type) {
Lev Walkinf59dac92017-08-26 21:26:51 -0700725 case PXER_WMORE:
726 RETURN(RC_WMORE);
Lev Walkin1bbc2002004-10-23 13:27:30 +0000727 case PXER_COMMENT: /* Got XML comment */
728 case PXER_TEXT: /* Ignore free-standing text */
729 XER_ADVANCE(ch_size); /* Skip silently */
730 continue;
731 case PXER_TAG:
732 break; /* Check the rest down there */
733 }
734 }
735
Lev Walkinf59dac92017-08-26 21:26:51 -0700736 tcv = xer_check_tag(ptr, ch_size, xml_tag);
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000737 ASN_DEBUG("XER/SEQUENCE: tcv = %d, ph=%d [%s]",
738 tcv, ctx->phase, xml_tag);
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000739
740 /* Skip the extensions section */
741 if(ctx->phase == 3) {
742 switch(xer_skip_unknown(tcv, &ctx->left)) {
743 case -1:
744 ctx->phase = 4;
745 RETURN(RC_FAIL);
746 case 0:
747 XER_ADVANCE(ch_size);
748 continue;
749 case 1:
750 XER_ADVANCE(ch_size);
751 ctx->phase = 1;
752 continue;
753 case 2:
754 ctx->phase = 1;
755 break;
756 }
757 }
758
Lev Walkin1bbc2002004-10-23 13:27:30 +0000759 switch(tcv) {
760 case XCT_CLOSING:
761 if(ctx->phase == 0) break;
Lev Walkin1bbc2002004-10-23 13:27:30 +0000762 ctx->phase = 0;
763 /* Fall through */
764 case XCT_BOTH:
765 if(ctx->phase == 0) {
766 if(edx >= td->elements_count
767 ||
768 /* Explicit OPTIONAL specs reaches the end */
769 (edx + elements[edx].optional
770 == td->elements_count)
771 ||
772 /* All extensions are optional */
773 (IN_EXTENSION_GROUP(specs, edx)
774 && specs->ext_before
Lev Walkin494fb702017-08-07 20:07:00 -0700775 > (signed)td->elements_count)
Lev Walkin1bbc2002004-10-23 13:27:30 +0000776 ) {
777 XER_ADVANCE(ch_size);
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000778 ctx->phase = 4; /* Phase out */
Lev Walkinc61f3862005-02-14 17:21:22 +0000779 RETURN(RC_OK);
Lev Walkin1bbc2002004-10-23 13:27:30 +0000780 } else {
781 ASN_DEBUG("Premature end of XER SEQUENCE");
782 RETURN(RC_FAIL);
783 }
784 }
785 /* Fall through */
786 case XCT_OPENING:
787 if(ctx->phase == 0) {
788 XER_ADVANCE(ch_size);
789 ctx->phase = 1; /* Processing body phase */
790 continue;
791 }
Lev Walkinda3ca412004-10-23 14:58:15 +0000792 /* Fall through */
Lev Walkin904e65b2005-02-18 14:23:48 +0000793 case XCT_UNKNOWN_OP:
794 case XCT_UNKNOWN_BO:
Lev Walkin1bbc2002004-10-23 13:27:30 +0000795
Lev Walkin24810022017-08-25 12:16:11 -0700796 ASN_DEBUG("XER/SEQUENCE: tcv=%d, ph=%d, edx=%zu",
Lev Walkin02666192005-07-03 05:31:02 +0000797 tcv, ctx->phase, edx);
798 if(ctx->phase != 1) {
Lev Walkin1bbc2002004-10-23 13:27:30 +0000799 break; /* Really unexpected */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000800 }
Lev Walkin02666192005-07-03 05:31:02 +0000801
802 if(edx < td->elements_count) {
803 /*
804 * Search which member corresponds to this tag.
805 */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800806 size_t n;
807 size_t edx_end = edx + elements[edx].optional + 1;
Lev Walkin02666192005-07-03 05:31:02 +0000808 if(edx_end > td->elements_count)
809 edx_end = td->elements_count;
810 for(n = edx; n < edx_end; n++) {
811 elm = &td->elements[n];
Lev Walkinf59dac92017-08-26 21:26:51 -0700812 tcv = xer_check_tag(ptr, ch_size, elm->name);
Lev Walkin02666192005-07-03 05:31:02 +0000813 switch(tcv) {
814 case XCT_BOTH:
815 case XCT_OPENING:
816 /*
817 * Process this member.
818 */
819 ctx->step = edx = n;
820 ctx->phase = 2;
821 break;
822 case XCT_UNKNOWN_OP:
823 case XCT_UNKNOWN_BO:
824 continue;
825 default:
826 n = edx_end;
827 break; /* Phase out */
828 }
829 break;
830 }
831 if(n != edx_end)
832 continue;
833 } else {
Lev Walkin24810022017-08-25 12:16:11 -0700834 ASN_DEBUG("Out of defined members: %zu/%u",
Lev Walkin02666192005-07-03 05:31:02 +0000835 edx, td->elements_count);
836 }
Lev Walkin904e65b2005-02-18 14:23:48 +0000837
838 /* It is expected extension */
839 if(IN_EXTENSION_GROUP(specs,
Lev Walkin02666192005-07-03 05:31:02 +0000840 edx + (edx < td->elements_count
841 ? elements[edx].optional : 0))) {
Lev Walkin24810022017-08-25 12:16:11 -0700842 ASN_DEBUG("Got anticipated extension at %zu",
Lev Walkin02666192005-07-03 05:31:02 +0000843 edx);
Lev Walkin904e65b2005-02-18 14:23:48 +0000844 /*
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000845 * Check for (XCT_BOTH or XCT_UNKNOWN_BO)
846 * By using a mask. Only record a pure
847 * <opening> tags.
Lev Walkin904e65b2005-02-18 14:23:48 +0000848 */
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000849 if(tcv & XCT_CLOSING) {
850 /* Found </extension> without body */
851 } else {
852 ctx->left = 1;
853 ctx->phase = 3; /* Skip ...'s */
854 }
855 XER_ADVANCE(ch_size);
856 continue;
Lev Walkin904e65b2005-02-18 14:23:48 +0000857 }
858
859 /* Fall through */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000860 default:
861 break;
862 }
863
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000864 ASN_DEBUG("Unexpected XML tag in SEQUENCE [%c%c%c%c%c%c]",
Lev Walkinf59dac92017-08-26 21:26:51 -0700865 size>0?((const char *)ptr)[0]:'.',
866 size>1?((const char *)ptr)[1]:'.',
867 size>2?((const char *)ptr)[2]:'.',
868 size>3?((const char *)ptr)[3]:'.',
869 size>4?((const char *)ptr)[4]:'.',
870 size>5?((const char *)ptr)[5]:'.');
Lev Walkin1bbc2002004-10-23 13:27:30 +0000871 break;
872 }
873
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000874 ctx->phase = 4; /* "Phase out" on hard failure */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000875 RETURN(RC_FAIL);
876}
877
Lev Walkina9cc46e2004-09-22 16:06:28 +0000878asn_enc_rval_t
Lev Walkin28ba9db2017-08-31 02:06:58 -0700879SEQUENCE_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, int ilevel,
880 enum xer_encoder_flags_e flags, asn_app_consume_bytes_f *cb,
881 void *app_key) {
882 asn_enc_rval_t er;
883 int xcan = (flags & XER_F_CANONICAL);
884 asn_TYPE_descriptor_t *tmp_def_val_td = 0;
885 void *tmp_def_val = 0;
Lev Walkin494fb702017-08-07 20:07:00 -0700886 size_t edx;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000887
Lev Walkin28ba9db2017-08-31 02:06:58 -0700888 if(!sptr) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000889
Lev Walkin28ba9db2017-08-31 02:06:58 -0700890 er.encoded = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000891
Lev Walkin28ba9db2017-08-31 02:06:58 -0700892 for(edx = 0; edx < td->elements_count; edx++) {
893 asn_enc_rval_t tmper;
894 asn_TYPE_member_t *elm = &td->elements[edx];
895 void *memb_ptr;
896 const char *mname = elm->name;
897 unsigned int mlen = strlen(mname);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000898
Lev Walkin28ba9db2017-08-31 02:06:58 -0700899 if(elm->flags & ATF_POINTER) {
900 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
901 if(!memb_ptr) {
902 assert(tmp_def_val == 0);
903 if(elm->default_value) {
904 if(elm->default_value(1, &tmp_def_val)) {
905 ASN__ENCODE_FAILED;
906 } else {
907 memb_ptr = tmp_def_val;
908 tmp_def_val_td = elm->type;
909 }
910 } else if(elm->optional) {
911 continue;
912 } else {
913 /* Mandatory element is missing */
914 ASN__ENCODE_FAILED;
915 }
916 }
917 } else {
918 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
919 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000920
Lev Walkin28ba9db2017-08-31 02:06:58 -0700921 if(!xcan) ASN__TEXT_INDENT(1, ilevel);
922 ASN__CALLBACK3("<", 1, mname, mlen, ">", 1);
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400923
Lev Walkin28ba9db2017-08-31 02:06:58 -0700924 /* Print the member itself */
925 tmper = elm->type->op->xer_encoder(elm->type, memb_ptr, ilevel + 1,
926 flags, cb, app_key);
927 if(tmp_def_val) {
928 ASN_STRUCT_FREE(*tmp_def_val_td, tmp_def_val);
929 tmp_def_val = 0;
930 }
931 if(tmper.encoded == -1) return tmper;
Lev Walkin4fe28822017-09-18 02:57:34 -0700932 er.encoded += tmper.encoded;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000933
Lev Walkin28ba9db2017-08-31 02:06:58 -0700934 ASN__CALLBACK3("</", 2, mname, mlen, ">", 1);
Lev Walkin28ba9db2017-08-31 02:06:58 -0700935 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000936
Lev Walkin28ba9db2017-08-31 02:06:58 -0700937 if(!xcan) ASN__TEXT_INDENT(1, ilevel - 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000938
Lev Walkin28ba9db2017-08-31 02:06:58 -0700939 ASN__ENCODED_OK(er);
Lev Walkin942fd082004-10-03 09:13:02 +0000940cb_failed:
Lev Walkin28ba9db2017-08-31 02:06:58 -0700941 if(tmp_def_val) ASN_STRUCT_FREE(*tmp_def_val_td, tmp_def_val);
942 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000943}
944
Lev Walkinf15320b2004-06-03 03:38:44 +0000945int
Lev Walkin5e033762004-09-29 13:26:15 +0000946SEQUENCE_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000947 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin494fb702017-08-07 20:07:00 -0700948 size_t edx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000949 int ret;
950
Lev Walkin8e8078a2004-09-26 13:10:40 +0000951 if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000952
953 /* Dump preamble */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000954 if(cb(td->name, strlen(td->name), app_key) < 0
955 || cb(" ::= {", 6, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000956 return -1;
957
Lev Walkin449f8322004-08-20 13:23:42 +0000958 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000959 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000960 const void *memb_ptr;
961
Lev Walkincc93b0f2004-09-10 09:18:20 +0000962 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800963 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkinac589332005-08-22 14:19:28 +0000964 if(!memb_ptr) {
965 if(elm->optional) continue;
966 /* Print <absent> line */
967 /* Fall through */
968 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000969 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800970 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000971 }
972
973 /* Indentation */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000974 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000975
976 /* Print the member's name and stuff */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000977 if(cb(elm->name, strlen(elm->name), app_key) < 0
978 || cb(": ", 2, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000979 return -1;
980
981 /* Print the member itself */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800982 ret = elm->type->op->print_struct(elm->type, memb_ptr, ilevel + 1,
Lev Walkinf15320b2004-06-03 03:38:44 +0000983 cb, app_key);
984 if(ret) return ret;
Lev Walkinf15320b2004-06-03 03:38:44 +0000985 }
986
Lev Walkin8e8078a2004-09-26 13:10:40 +0000987 ilevel--;
988 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000989
Lev Walkin8e8078a2004-09-26 13:10:40 +0000990 return (cb("}", 1, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000991}
992
993void
Lev Walkin8d99d7b2017-08-25 01:06:00 -0700994SEQUENCE_free(const asn_TYPE_descriptor_t *td, void *sptr,
995 enum asn_struct_free_method method) {
Lev Walkinf6853ce2017-08-11 00:50:27 -0700996 size_t edx;
Vasil Velichkovaead8c52017-10-10 05:34:48 +0300997 const asn_SEQUENCE_specifics_t *specs =
998 (const asn_SEQUENCE_specifics_t *)td->specifics;
999 asn_struct_ctx_t *ctx; /* Decoder context */
Lev Walkinf15320b2004-06-03 03:38:44 +00001000
1001 if(!td || !sptr)
1002 return;
1003
1004 ASN_DEBUG("Freeing %s as SEQUENCE", td->name);
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 void *memb_ptr;
Lev Walkincc93b0f2004-09-10 09:18:20 +00001009 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001010 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +00001011 if(memb_ptr)
Lev Walkinadcb5862006-03-17 02:11:12 +00001012 ASN_STRUCT_FREE(*elm->type, memb_ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +00001013 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001014 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkinadcb5862006-03-17 02:11:12 +00001015 ASN_STRUCT_FREE_CONTENTS_ONLY(*elm->type, memb_ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +00001016 }
1017 }
1018
Vasil Velichkovaead8c52017-10-10 05:34:48 +03001019 /* Clean parsing context */
1020 ctx = (asn_struct_ctx_t *)((char *)sptr + specs->ctx_offset);
1021 FREEMEM(ctx->ptr);
1022
Lev Walkin8d99d7b2017-08-25 01:06:00 -07001023 switch(method) {
1024 case ASFM_FREE_EVERYTHING:
1025 FREEMEM(sptr);
1026 break;
1027 case ASFM_FREE_UNDERLYING:
1028 break;
1029 case ASFM_FREE_UNDERLYING_AND_RESET:
Lev Walkind84f6032017-10-03 16:33:59 -07001030 memset(
1031 sptr, 0,
1032 ((const asn_SEQUENCE_specifics_t *)(td->specifics))->struct_size);
Lev Walkin8d99d7b2017-08-25 01:06:00 -07001033 break;
1034 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001035}
1036
1037int
Lev Walkin5e033762004-09-29 13:26:15 +00001038SEQUENCE_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +00001039 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkin494fb702017-08-07 20:07:00 -07001040 size_t edx;
Lev Walkinf15320b2004-06-03 03:38:44 +00001041
1042 if(!sptr) {
Lev Walkin7c1dc052016-03-14 03:08:15 -07001043 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +00001044 "%s: value not given (%s:%d)",
1045 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +00001046 return -1;
1047 }
1048
1049 /*
1050 * Iterate over structure members and check their validity.
1051 */
Lev Walkin449f8322004-08-20 13:23:42 +00001052 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +00001053 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +00001054 const void *memb_ptr;
1055
Lev Walkincc93b0f2004-09-10 09:18:20 +00001056 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001057 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkinac589332005-08-22 14:19:28 +00001058 if(!memb_ptr) {
1059 if(elm->optional)
1060 continue;
Lev Walkin7c1dc052016-03-14 03:08:15 -07001061 ASN__CTFAIL(app_key, td, sptr,
Lev Walkinac589332005-08-22 14:19:28 +00001062 "%s: mandatory element %s absent (%s:%d)",
1063 td->name, elm->name, __FILE__, __LINE__);
1064 return -1;
1065 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001066 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001067 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +00001068 }
1069
Lev Walkina5972be2017-09-29 23:15:58 -07001070 if(elm->encoding_constraints.general_constraints) {
1071 int ret = elm->encoding_constraints.general_constraints(elm->type, memb_ptr,
Lev Walkin1eded352006-07-13 11:19:01 +00001072 ctfailcb, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +00001073 if(ret) return ret;
1074 } else {
Lev Walkina5972be2017-09-29 23:15:58 -07001075 return elm->type->encoding_constraints.general_constraints(elm->type,
Lev Walkin1eded352006-07-13 11:19:01 +00001076 memb_ptr, ctfailcb, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +00001077 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001078 }
1079
1080 return 0;
1081}
Lev Walkin59b176e2005-11-26 11:25:14 +00001082
Lev Walkin24810022017-08-25 12:16:11 -07001083#ifndef ASN_DISABLE_PER_SUPPORT
1084
Lev Walkin59b176e2005-11-26 11:25:14 +00001085asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -07001086SEQUENCE_decode_uper(const asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -07001087 const asn_per_constraints_t *constraints, void **sptr,
1088 asn_per_data_t *pd) {
Lev Walkind84f6032017-10-03 16:33:59 -07001089 const asn_SEQUENCE_specifics_t *specs = (const asn_SEQUENCE_specifics_t *)td->specifics;
Lev Walkin59b176e2005-11-26 11:25:14 +00001090 void *st = *sptr; /* Target structure. */
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001091 int extpresent; /* Extension additions are present */
Lev Walkin59b176e2005-11-26 11:25:14 +00001092 uint8_t *opres; /* Presence of optional root members */
1093 asn_per_data_t opmd;
1094 asn_dec_rval_t rv;
Lev Walkin494fb702017-08-07 20:07:00 -07001095 size_t edx;
Lev Walkin59b176e2005-11-26 11:25:14 +00001096
1097 (void)constraints;
1098
Lev Walkin7c1dc052016-03-14 03:08:15 -07001099 if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx))
1100 ASN__DECODE_FAILED;
Lev Walkin1d9e8dd2005-12-07 05:46:03 +00001101
Lev Walkin59b176e2005-11-26 11:25:14 +00001102 if(!st) {
1103 st = *sptr = CALLOC(1, specs->struct_size);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001104 if(!st) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +00001105 }
1106
1107 ASN_DEBUG("Decoding %s as SEQUENCE (UPER)", td->name);
1108
1109 /* Handle extensions */
1110 if(specs->ext_before >= 0) {
1111 extpresent = per_get_few_bits(pd, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001112 if(extpresent < 0) ASN__DECODE_STARVED;
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001113 } else {
1114 extpresent = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +00001115 }
1116
1117 /* Prepare a place and read-in the presence bitmap */
Lev Walkin73a5cc62007-06-24 08:45:31 +00001118 memset(&opmd, 0, sizeof(opmd));
Lev Walkin59b176e2005-11-26 11:25:14 +00001119 if(specs->roms_count) {
1120 opres = (uint8_t *)MALLOC(((specs->roms_count + 7) >> 3) + 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001121 if(!opres) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +00001122 /* Get the presence map */
1123 if(per_get_many_bits(pd, opres, 0, specs->roms_count)) {
1124 FREEMEM(opres);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001125 ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +00001126 }
1127 opmd.buffer = opres;
Lev Walkin59b176e2005-11-26 11:25:14 +00001128 opmd.nbits = specs->roms_count;
1129 ASN_DEBUG("Read in presence bitmap for %s of %d bits (%x..)",
1130 td->name, specs->roms_count, *opres);
1131 } else {
1132 opres = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +00001133 }
1134
1135 /*
1136 * Get the sequence ROOT elements.
1137 */
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001138 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001139 asn_TYPE_member_t *elm = &td->elements[edx];
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001140 void *memb_ptr; /* Pointer to the member */
Lev Walkin59b176e2005-11-26 11:25:14 +00001141 void **memb_ptr2; /* Pointer to that pointer */
1142
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001143 if(IN_EXTENSION_GROUP(specs, edx))
1144 continue;
1145
Lev Walkin59b176e2005-11-26 11:25:14 +00001146 /* Fetch the pointer to this member */
1147 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001148 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
Lev Walkin59b176e2005-11-26 11:25:14 +00001149 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001150 memb_ptr = (char *)st + elm->memb_offset;
Lev Walkin59b176e2005-11-26 11:25:14 +00001151 memb_ptr2 = &memb_ptr;
1152 }
1153
1154 /* Deal with optionality */
1155 if(elm->optional) {
1156 int present = per_get_few_bits(&opmd, 1);
1157 ASN_DEBUG("Member %s->%s is optional, p=%d (%d->%d)",
1158 td->name, elm->name, present,
1159 (int)opmd.nboff, (int)opmd.nbits);
1160 if(present == 0) {
1161 /* This element is not present */
1162 if(elm->default_value) {
1163 /* Fill-in DEFAULT */
Lev Walkin523de9e2006-08-18 01:34:18 +00001164 if(elm->default_value(1, memb_ptr2)) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001165 FREEMEM(opres);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001166 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +00001167 }
Lev Walkin8032f7a2007-06-27 01:54:57 +00001168 ASN_DEBUG("Filled-in default");
Lev Walkin59b176e2005-11-26 11:25:14 +00001169 }
1170 /* The member is just not present */
1171 continue;
1172 }
1173 /* Fall through */
1174 }
1175
1176 /* Fetch the member from the stream */
Lev Walkinf6853ce2017-08-11 00:50:27 -07001177 ASN_DEBUG("Decoding member \"%s\" in %s", elm->name, td->name);
Lev Walkin9de6cd82017-08-10 05:47:46 -07001178
Lev Walkin75b6fe42017-08-27 01:03:22 -07001179 if(elm->flags & ATF_OPEN_TYPE) {
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001180 rv = OPEN_TYPE_uper_get(opt_codec_ctx, td, st, elm, pd);
1181 } else {
Lev Walkinf59dac92017-08-26 21:26:51 -07001182 rv = elm->type->op->uper_decoder(opt_codec_ctx, elm->type,
Lev Walkina5972be2017-09-29 23:15:58 -07001183 elm->encoding_constraints.per_constraints, memb_ptr2, pd);
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001184 }
1185 if(rv.code != RC_OK) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001186 ASN_DEBUG("Failed decode %s in %s",
1187 elm->name, td->name);
1188 FREEMEM(opres);
1189 return rv;
1190 }
1191 }
1192
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001193 /* Optionality map is not needed anymore */
1194 FREEMEM(opres);
1195
Lev Walkin59b176e2005-11-26 11:25:14 +00001196 /*
1197 * Deal with extensions.
1198 */
1199 if(extpresent) {
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001200 ssize_t bmlength;
1201 uint8_t *epres; /* Presence of extension members */
1202 asn_per_data_t epmd;
Lev Walkin59b176e2005-11-26 11:25:14 +00001203
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001204 bmlength = uper_get_nslength(pd);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001205 if(bmlength < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +00001206
Lev Walkinaaae9bf2010-10-24 16:56:30 -07001207 ASN_DEBUG("Extensions %ld present in %s", (long)bmlength, td->name);
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001208
1209 epres = (uint8_t *)MALLOC((bmlength + 15) >> 3);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001210 if(!epres) ASN__DECODE_STARVED;
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001211
1212 /* Get the extensions map */
Simo Sorcece71d912015-09-03 16:34:20 -04001213 if(per_get_many_bits(pd, epres, 0, bmlength)) {
1214 FREEMEM(epres);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001215 ASN__DECODE_STARVED;
Simo Sorcece71d912015-09-03 16:34:20 -04001216 }
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001217
Lev Walkin73a5cc62007-06-24 08:45:31 +00001218 memset(&epmd, 0, sizeof(epmd));
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001219 epmd.buffer = epres;
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001220 epmd.nbits = bmlength;
Lev Walkinaaae9bf2010-10-24 16:56:30 -07001221 ASN_DEBUG("Read in extensions bitmap for %s of %ld bits (%x..)",
1222 td->name, (long)bmlength, *epres);
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001223
1224 /* Go over extensions and read them in */
1225 for(edx = specs->ext_after + 1; edx < td->elements_count; edx++) {
1226 asn_TYPE_member_t *elm = &td->elements[edx];
1227 void *memb_ptr; /* Pointer to the member */
1228 void **memb_ptr2; /* Pointer to that pointer */
1229 int present;
1230
1231 if(!IN_EXTENSION_GROUP(specs, edx)) {
Lev Walkin24810022017-08-25 12:16:11 -07001232 ASN_DEBUG("%zu is not an extension", edx);
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001233 continue;
1234 }
1235
1236 /* Fetch the pointer to this member */
1237 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001238 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001239 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001240 memb_ptr = (void *)((char *)st + elm->memb_offset);
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001241 memb_ptr2 = &memb_ptr;
1242 }
1243
1244 present = per_get_few_bits(&epmd, 1);
1245 if(present <= 0) {
1246 if(present < 0) break; /* No more extensions */
1247 continue;
1248 }
1249
1250 ASN_DEBUG("Decoding member %s in %s %p", elm->name, td->name, *memb_ptr2);
Lev Walkin9218bc12007-06-27 04:09:37 +00001251 rv = uper_open_type_get(opt_codec_ctx, elm->type,
Lev Walkina5972be2017-09-29 23:15:58 -07001252 elm->encoding_constraints.per_constraints, memb_ptr2, pd);
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001253 if(rv.code != RC_OK) {
1254 FREEMEM(epres);
1255 return rv;
1256 }
1257 }
1258
1259 /* Skip over overflow extensions which aren't present
1260 * in this system's version of the protocol */
Lev Walkin06230f72007-06-26 09:52:44 +00001261 for(;;) {
Lev Walkina2987ea2007-06-26 23:56:54 +00001262 ASN_DEBUG("Getting overflow extensions");
Lev Walkin06230f72007-06-26 09:52:44 +00001263 switch(per_get_few_bits(&epmd, 1)) {
1264 case -1: break;
1265 case 0: continue;
1266 default:
Lev Walkin9218bc12007-06-27 04:09:37 +00001267 if(uper_open_type_skip(opt_codec_ctx, pd)) {
Lev Walkin06230f72007-06-26 09:52:44 +00001268 FREEMEM(epres);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001269 ASN__DECODE_STARVED;
Lev Walkin06230f72007-06-26 09:52:44 +00001270 }
Lev Walkin59b176e2005-11-26 11:25:14 +00001271 }
Lev Walkin06230f72007-06-26 09:52:44 +00001272 break;
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001273 }
Lev Walkin59b176e2005-11-26 11:25:14 +00001274
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001275 FREEMEM(epres);
1276 }
1277
1278 /* Fill DEFAULT members in extensions */
1279 for(edx = specs->roms_count; edx < specs->roms_count
1280 + specs->aoms_count; edx++) {
1281 asn_TYPE_member_t *elm = &td->elements[edx];
1282 void **memb_ptr2; /* Pointer to member pointer */
1283
1284 if(!elm->default_value) continue;
1285
1286 /* Fetch the pointer to this member */
1287 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001288 memb_ptr2 = (void **)((char *)st
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001289 + elm->memb_offset);
1290 if(*memb_ptr2) continue;
1291 } else {
1292 continue; /* Extensions are all optionals */
1293 }
1294
1295 /* Set default value */
1296 if(elm->default_value(1, memb_ptr2)) {
Lev Walkin7c1dc052016-03-14 03:08:15 -07001297 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +00001298 }
1299 }
1300
1301 rv.consumed = 0;
1302 rv.code = RC_OK;
1303 return rv;
1304}
1305
Lev Walkin62258e22007-06-23 23:50:25 +00001306static int
1307SEQUENCE_handle_extensions(asn_TYPE_descriptor_t *td, void *sptr,
1308 asn_per_outp_t *po1, asn_per_outp_t *po2) {
Lev Walkind84f6032017-10-03 16:33:59 -07001309 const asn_SEQUENCE_specifics_t *specs
1310 = (const asn_SEQUENCE_specifics_t *)td->specifics;
Lev Walkinf55a6dd2007-06-24 00:35:51 +00001311 int exts_present = 0;
1312 int exts_count = 0;
Lev Walkin494fb702017-08-07 20:07:00 -07001313 size_t edx;
Lev Walkin62258e22007-06-23 23:50:25 +00001314
1315 if(specs->ext_before < 0)
1316 return 0;
1317
1318 /* Find out which extensions are present */
1319 for(edx = specs->ext_after + 1; edx < td->elements_count; edx++) {
1320 asn_TYPE_member_t *elm = &td->elements[edx];
1321 void *memb_ptr; /* Pointer to the member */
1322 void **memb_ptr2; /* Pointer to that pointer */
1323 int present;
1324
Lev Walkinf55a6dd2007-06-24 00:35:51 +00001325 if(!IN_EXTENSION_GROUP(specs, edx)) {
Lev Walkin24810022017-08-25 12:16:11 -07001326 ASN_DEBUG("%s (@%zu) is not extension", elm->type->name, edx);
Lev Walkin62258e22007-06-23 23:50:25 +00001327 continue;
Lev Walkinf55a6dd2007-06-24 00:35:51 +00001328 }
Lev Walkin62258e22007-06-23 23:50:25 +00001329
1330 /* Fetch the pointer to this member */
1331 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001332 memb_ptr2 = (void **)((char *)sptr + elm->memb_offset);
Lev Walkin62258e22007-06-23 23:50:25 +00001333 present = (*memb_ptr2 != 0);
1334 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001335 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkin62258e22007-06-23 23:50:25 +00001336 memb_ptr2 = &memb_ptr;
1337 present = 1;
1338 }
1339
Lev Walkin24810022017-08-25 12:16:11 -07001340 ASN_DEBUG("checking %s (@%zu) present => %d",
Lev Walkin06230f72007-06-26 09:52:44 +00001341 elm->type->name, edx, present);
Lev Walkinf55a6dd2007-06-24 00:35:51 +00001342 exts_count++;
1343 exts_present += present;
Lev Walkin62258e22007-06-23 23:50:25 +00001344
1345 /* Encode as presence marker */
1346 if(po1 && per_put_few_bits(po1, present, 1))
1347 return -1;
1348 /* Encode as open type field */
Lev Walkin9218bc12007-06-27 04:09:37 +00001349 if(po2 && present && uper_open_type_put(elm->type,
Lev Walkina5972be2017-09-29 23:15:58 -07001350 elm->encoding_constraints.per_constraints, *memb_ptr2, po2))
Lev Walkin62258e22007-06-23 23:50:25 +00001351 return -1;
1352
1353 }
1354
Lev Walkinf55a6dd2007-06-24 00:35:51 +00001355 return exts_present ? exts_count : 0;
Lev Walkin62258e22007-06-23 23:50:25 +00001356}
1357
Lev Walkin523de9e2006-08-18 01:34:18 +00001358asn_enc_rval_t
1359SEQUENCE_encode_uper(asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -07001360 const asn_per_constraints_t *constraints, void *sptr,
1361 asn_per_outp_t *po) {
Lev Walkind84f6032017-10-03 16:33:59 -07001362 const asn_SEQUENCE_specifics_t *specs
1363 = (const asn_SEQUENCE_specifics_t *)td->specifics;
Lev Walkin523de9e2006-08-18 01:34:18 +00001364 asn_enc_rval_t er;
Lev Walkin62258e22007-06-23 23:50:25 +00001365 int n_extensions;
Lev Walkin494fb702017-08-07 20:07:00 -07001366 size_t edx;
1367 size_t i;
Lev Walkin523de9e2006-08-18 01:34:18 +00001368
1369 (void)constraints;
1370
1371 if(!sptr)
Lev Walkin7c1dc052016-03-14 03:08:15 -07001372 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001373
1374 er.encoded = 0;
1375
1376 ASN_DEBUG("Encoding %s as SEQUENCE (UPER)", td->name);
Lev Walkin62258e22007-06-23 23:50:25 +00001377
1378
1379 /*
1380 * X.691#18.1 Whether structure is extensible
1381 * and whether to encode extensions
1382 */
1383 if(specs->ext_before >= 0) {
1384 n_extensions = SEQUENCE_handle_extensions(td, sptr, 0, 0);
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001385 if(n_extensions < 0)
Lev Walkin494fb702017-08-07 20:07:00 -07001386 ASN__ENCODE_FAILED;
1387 if(per_put_few_bits(po, n_extensions ? 1 : 0, 1))
1388 ASN__ENCODE_FAILED;
Lev Walkin62258e22007-06-23 23:50:25 +00001389 } else {
1390 n_extensions = 0; /* There are no extensions to encode */
1391 }
Lev Walkin523de9e2006-08-18 01:34:18 +00001392
1393 /* Encode a presence bitmap */
1394 for(i = 0; i < specs->roms_count; i++) {
Lev Walkinc46b7cb2006-08-18 02:27:55 +00001395 asn_TYPE_member_t *elm;
Lev Walkin523de9e2006-08-18 01:34:18 +00001396 void *memb_ptr; /* Pointer to the member */
1397 void **memb_ptr2; /* Pointer to that pointer */
Lev Walkin523de9e2006-08-18 01:34:18 +00001398 int present;
1399
Lev Walkinc46b7cb2006-08-18 02:27:55 +00001400 edx = specs->oms[i];
1401 elm = &td->elements[edx];
1402
Lev Walkin523de9e2006-08-18 01:34:18 +00001403 /* Fetch the pointer to this member */
1404 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001405 memb_ptr2 = (void **)((char *)sptr + elm->memb_offset);
Lev Walkin523de9e2006-08-18 01:34:18 +00001406 present = (*memb_ptr2 != 0);
1407 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001408 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkin523de9e2006-08-18 01:34:18 +00001409 memb_ptr2 = &memb_ptr;
1410 present = 1;
1411 }
1412
1413 /* Eliminate default values */
1414 if(present && elm->default_value
1415 && elm->default_value(0, memb_ptr2) == 1)
1416 present = 0;
1417
1418 ASN_DEBUG("Element %s %s %s->%s is %s",
1419 elm->flags & ATF_POINTER ? "ptr" : "inline",
1420 elm->default_value ? "def" : "wtv",
1421 td->name, elm->name, present ? "present" : "absent");
1422 if(per_put_few_bits(po, present, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001423 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001424 }
1425
1426 /*
Lev Walkin62258e22007-06-23 23:50:25 +00001427 * Encode the sequence ROOT elements.
Lev Walkin523de9e2006-08-18 01:34:18 +00001428 */
Lev Walkin62258e22007-06-23 23:50:25 +00001429 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 +00001430 for(edx = 0; edx < ((specs->ext_after < 0)
1431 ? td->elements_count : specs->ext_before - 1); edx++) {
1432
Lev Walkin523de9e2006-08-18 01:34:18 +00001433 asn_TYPE_member_t *elm = &td->elements[edx];
1434 void *memb_ptr; /* Pointer to the member */
1435 void **memb_ptr2; /* Pointer to that pointer */
1436
Lev Walkin4d22b622007-07-23 09:58:25 +00001437 if(IN_EXTENSION_GROUP(specs, edx))
1438 continue;
1439
Lev Walkina2987ea2007-06-26 23:56:54 +00001440 ASN_DEBUG("About to encode %s", elm->type->name);
1441
Lev Walkin523de9e2006-08-18 01:34:18 +00001442 /* Fetch the pointer to this member */
1443 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001444 memb_ptr2 = (void **)((char *)sptr + elm->memb_offset);
Lev Walkin523de9e2006-08-18 01:34:18 +00001445 if(!*memb_ptr2) {
Lev Walkin24810022017-08-25 12:16:11 -07001446 ASN_DEBUG("Element %s %zu not present",
Lev Walkin523de9e2006-08-18 01:34:18 +00001447 elm->name, edx);
1448 if(elm->optional)
1449 continue;
1450 /* Mandatory element is missing */
Lev Walkin7c1dc052016-03-14 03:08:15 -07001451 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001452 }
1453 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001454 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkin523de9e2006-08-18 01:34:18 +00001455 memb_ptr2 = &memb_ptr;
1456 }
1457
1458 /* Eliminate default values */
Lev Walkindbd091a2007-06-27 02:11:08 +00001459 if(elm->default_value && elm->default_value(0, memb_ptr2) == 1)
Lev Walkin523de9e2006-08-18 01:34:18 +00001460 continue;
1461
Lev Walkin4d22b622007-07-23 09:58:25 +00001462 ASN_DEBUG("Encoding %s->%s", td->name, elm->name);
Lev Walkina5972be2017-09-29 23:15:58 -07001463 er = elm->type->op->uper_encoder(elm->type, elm->encoding_constraints.per_constraints,
Lev Walkin523de9e2006-08-18 01:34:18 +00001464 *memb_ptr2, po);
1465 if(er.encoded == -1)
1466 return er;
1467 }
1468
Lev Walkin62258e22007-06-23 23:50:25 +00001469 /* No extensions to encode */
Lev Walkin7c1dc052016-03-14 03:08:15 -07001470 if(!n_extensions) ASN__ENCODED_OK(er);
Lev Walkin62258e22007-06-23 23:50:25 +00001471
1472 ASN_DEBUG("Length of %d bit-map", n_extensions);
1473 /* #18.8. Write down the presence bit-map length. */
1474 if(uper_put_nslength(po, n_extensions))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001475 ASN__ENCODE_FAILED;
Lev Walkin62258e22007-06-23 23:50:25 +00001476
1477 ASN_DEBUG("Bit-map of %d elements", n_extensions);
1478 /* #18.7. Encoding the extensions presence bit-map. */
1479 /* TODO: act upon NOTE in #18.7 for canonical PER */
1480 if(SEQUENCE_handle_extensions(td, sptr, po, 0) != n_extensions)
Lev Walkin7c1dc052016-03-14 03:08:15 -07001481 ASN__ENCODE_FAILED;
Lev Walkin62258e22007-06-23 23:50:25 +00001482
1483 ASN_DEBUG("Writing %d extensions", n_extensions);
1484 /* #18.9. Encode extensions as open type fields. */
1485 if(SEQUENCE_handle_extensions(td, sptr, 0, po) != n_extensions)
Lev Walkin7c1dc052016-03-14 03:08:15 -07001486 ASN__ENCODE_FAILED;
Lev Walkin62258e22007-06-23 23:50:25 +00001487
Lev Walkin7c1dc052016-03-14 03:08:15 -07001488 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +00001489}
1490
Lev Walkin24810022017-08-25 12:16:11 -07001491#endif /* ASN_DISABLE_PER_SUPPORT */
1492
Lev Walkincd2f48e2017-08-10 02:14:59 -07001493int
1494SEQUENCE_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
1495 const void *bptr) {
1496 size_t edx;
1497
1498 for(edx = 0; edx < td->elements_count; edx++) {
1499 asn_TYPE_member_t *elm = &td->elements[edx];
1500 const void *amemb;
1501 const void *bmemb;
1502 int ret;
1503
1504 if(elm->flags & ATF_POINTER) {
1505 amemb =
1506 *(const void *const *)((const char *)aptr + elm->memb_offset);
1507 bmemb =
1508 *(const void *const *)((const char *)bptr + elm->memb_offset);
1509 if(!amemb) {
1510 if(!bmemb) continue;
1511 return -1;
1512 } else if(!bmemb) {
1513 return 1;
1514 }
1515 } else {
1516 amemb = (const void *)((const char *)aptr + elm->memb_offset);
1517 bmemb = (const void *)((const char *)bptr + elm->memb_offset);
1518 }
1519
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001520 ret = elm->type->op->compare_struct(elm->type, amemb, bmemb);
Lev Walkincd2f48e2017-08-10 02:14:59 -07001521 if(ret != 0) return ret;
1522 }
1523
1524 return 0;
1525}
1526
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001527asn_TYPE_operation_t asn_OP_SEQUENCE = {
1528 SEQUENCE_free,
1529 SEQUENCE_print,
1530 SEQUENCE_compare,
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001531 SEQUENCE_decode_ber,
1532 SEQUENCE_encode_der,
1533 SEQUENCE_decode_xer,
1534 SEQUENCE_encode_xer,
1535#ifdef ASN_DISABLE_OER_SUPPORT
1536 0,
1537 0,
1538#else
1539 SEQUENCE_decode_oer,
1540 SEQUENCE_encode_oer,
1541#endif /* ASN_DISABLE_OER_SUPPORT */
1542#ifdef ASN_DISABLE_PER_SUPPORT
1543 0,
1544 0,
1545#else
1546 SEQUENCE_decode_uper,
1547 SEQUENCE_encode_uper,
1548#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkina5972be2017-09-29 23:15:58 -07001549 SEQUENCE_random_fill,
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001550 0 /* Use generic outmost tag fetcher */
1551};
1552
Lev Walkina5972be2017-09-29 23:15:58 -07001553
1554asn_random_fill_result_t
1555SEQUENCE_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
1556 const asn_encoding_constraints_t *constr,
1557 size_t max_length) {
1558 const asn_SEQUENCE_specifics_t *specs =
1559 (const asn_SEQUENCE_specifics_t *)td->specifics;
1560 asn_random_fill_result_t result_ok = {ARFILL_OK, 0};
1561 asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
1562 asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
1563 void *st = *sptr;
1564 size_t edx;
1565
1566 if(max_length == 0) return result_skipped;
1567
1568 (void)constr;
1569
1570 if(st == NULL) {
1571 st = CALLOC(1, specs->struct_size);
1572 if(st == NULL) {
1573 return result_failed;
1574 }
1575 }
1576
1577 for(edx = 0; edx < td->elements_count; edx++) {
1578 const asn_TYPE_member_t *elm = &td->elements[edx];
1579 void *memb_ptr; /* Pointer to the member */
1580 void **memb_ptr2; /* Pointer to that pointer */
1581 asn_random_fill_result_t tmpres;
1582
1583 if(elm->optional && asn_random_between(0, 4) == 2) {
1584 /* Sometimes decide not to fill the optional value */
1585 continue;
1586 }
1587
1588 if(elm->flags & ATF_POINTER) {
1589 /* Member is a pointer to another structure */
1590 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
1591 } else {
1592 memb_ptr = (char *)st + elm->memb_offset;
1593 memb_ptr2 = &memb_ptr;
1594 }
1595
1596 tmpres = elm->type->op->random_fill(
1597 elm->type, memb_ptr2, &elm->encoding_constraints,
1598 max_length > result_ok.length ? max_length - result_ok.length : 0);
1599 switch(tmpres.code) {
1600 case ARFILL_OK:
1601 result_ok.length += tmpres.length;
1602 continue;
1603 case ARFILL_SKIPPED:
1604 assert(!(elm->flags & ATF_POINTER) || *memb_ptr2 == NULL);
1605 continue;
1606 case ARFILL_FAILED:
1607 if(st == *sptr) {
1608 ASN_STRUCT_RESET(*td, st);
1609 } else {
1610 ASN_STRUCT_FREE(*td, st);
1611 }
1612 return tmpres;
1613 }
1614 }
1615
1616 *sptr = st;
1617
1618 return result_ok;
1619}
1620