blob: 46af34f32276c5c9afcd386eb2e14dd57a984f17 [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 Walkin20696a42017-10-17 21:27:33 -0700113SEQUENCE_decode_ber(const asn_codec_ctx_t *opt_codec_ctx,
114 const asn_TYPE_descriptor_t *td, void **struct_ptr,
115 const void *ptr, size_t size, int tag_mode) {
116 /*
Lev Walkinf15320b2004-06-03 03:38:44 +0000117 * Bring closer parts of structure description.
118 */
Lev Walkind84f6032017-10-03 16:33:59 -0700119 const asn_SEQUENCE_specifics_t *specs = (const asn_SEQUENCE_specifics_t *)td->specifics;
Lev Walkin20696a42017-10-17 21:27:33 -0700120 const asn_TYPE_member_t *elements = td->elements;
Lev Walkinf15320b2004-06-03 03:38:44 +0000121
Lev Walkin20696a42017-10-17 21:27:33 -0700122 /*
Lev Walkinf15320b2004-06-03 03:38:44 +0000123 * Parts of the structure being constructed.
124 */
125 void *st = *struct_ptr; /* Target structure. */
Lev Walkin5e033762004-09-29 13:26:15 +0000126 asn_struct_ctx_t *ctx; /* Decoder context */
Lev Walkinf15320b2004-06-03 03:38:44 +0000127
128 ber_tlv_tag_t tlv_tag; /* T from TLV */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000129 asn_dec_rval_t rval; /* Return code from subparsers */
Lev Walkinf15320b2004-06-03 03:38:44 +0000130
131 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
Lev Walkin494fb702017-08-07 20:07:00 -0700132 size_t edx; /* SEQUENCE element's index */
Lev Walkinf15320b2004-06-03 03:38:44 +0000133
Lev Walkin449f8322004-08-20 13:23:42 +0000134 ASN_DEBUG("Decoding %s as SEQUENCE", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000135
136 /*
137 * Create the target structure if it is not present already.
138 */
139 if(st == 0) {
140 st = *struct_ptr = CALLOC(1, specs->struct_size);
141 if(st == 0) {
142 RETURN(RC_FAIL);
143 }
144 }
145
146 /*
147 * Restore parsing context.
148 */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800149 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000150
151 /*
152 * Start to parse where left previously
153 */
154 switch(ctx->phase) {
155 case 0:
156 /*
157 * PHASE 0.
158 * Check that the set of tags associated with given structure
159 * perfectly fits our expectations.
160 */
161
Lev Walkin5e033762004-09-29 13:26:15 +0000162 rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size,
Lev Walkin8e8078a2004-09-26 13:10:40 +0000163 tag_mode, 1, &ctx->left, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000164 if(rval.code != RC_OK) {
165 ASN_DEBUG("%s tagging check failed: %d",
Lev Walkin449f8322004-08-20 13:23:42 +0000166 td->name, rval.code);
Lev Walkin566a5672004-10-05 06:36:57 +0000167 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000168 }
169
170 if(ctx->left >= 0)
171 ctx->left += rval.consumed; /* ?Substracted below! */
172 ADVANCE(rval.consumed);
173
174 NEXT_PHASE(ctx);
175
176 ASN_DEBUG("Structure consumes %ld bytes, buffer %ld",
177 (long)ctx->left, (long)size);
178
179 /* Fall through */
180 case 1:
181 /*
182 * PHASE 1.
183 * From the place where we've left it previously,
184 * try to decode the next member from the list of
185 * this structure's elements.
186 * (ctx->step) stores the member being processed
187 * between invocations and the microphase {0,1} of parsing
188 * that member:
189 * step = (<member_number> * 2 + <microphase>).
190 */
Lev Walkin494fb702017-08-07 20:07:00 -0700191 for(edx = ((size_t)ctx->step >> 1); edx < td->elements_count;
Lev Walkinf15320b2004-06-03 03:38:44 +0000192 edx++, ctx->step = (ctx->step & ~1) + 2) {
193 void *memb_ptr; /* Pointer to the member */
Lev Walkinc2346572004-08-11 09:07:36 +0000194 void **memb_ptr2; /* Pointer to that pointer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000195 ssize_t tag_len; /* Length of TLV's T */
Lev Walkin494fb702017-08-07 20:07:00 -0700196 size_t opt_edx_end; /* Next non-optional element */
197 size_t n;
Lev Walkin4c36e302004-06-06 07:20:02 +0000198 int use_bsearch;
Lev Walkinf15320b2004-06-03 03:38:44 +0000199
200 if(ctx->step & 1)
201 goto microphase2;
202
203 /*
204 * MICROPHASE 1: Synchronize decoding.
205 */
Lev Walkin24810022017-08-25 12:16:11 -0700206 ASN_DEBUG("In %s SEQUENCE left %d, edx=%zu flags=%d"
Lev Walkincc93b0f2004-09-10 09:18:20 +0000207 " opt=%d ec=%d",
208 td->name, (int)ctx->left, edx,
209 elements[edx].flags, elements[edx].optional,
210 td->elements_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000211
212 if(ctx->left == 0 /* No more stuff is expected */
213 && (
214 /* Explicit OPTIONAL specification reaches the end */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000215 (edx + elements[edx].optional
216 == td->elements_count)
Lev Walkinf15320b2004-06-03 03:38:44 +0000217 ||
218 /* All extensions are optional */
219 (IN_EXTENSION_GROUP(specs, edx)
Lev Walkin494fb702017-08-07 20:07:00 -0700220 && specs->ext_before > (signed)td->elements_count)
Lev Walkinf15320b2004-06-03 03:38:44 +0000221 )
222 ) {
Lev Walkin449f8322004-08-20 13:23:42 +0000223 ASN_DEBUG("End of SEQUENCE %s", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000224 /*
225 * Found the legitimate end of the structure.
226 */
227 PHASE_OUT(ctx);
228 RETURN(RC_OK);
229 }
230
231 /*
232 * Fetch the T from TLV.
233 */
234 tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
Lev Walkin24810022017-08-25 12:16:11 -0700235 ASN_DEBUG("Current tag in %s SEQUENCE for element %zu "
Lev Walkincc6a9102004-09-23 22:06:26 +0000236 "(%s) is %s encoded in %d bytes, of frame %ld",
Lev Walkin906654e2004-09-10 15:49:15 +0000237 td->name, edx, elements[edx].name,
238 ber_tlv_tag_string(tlv_tag), (int)tag_len, (long)LEFT);
Lev Walkinf15320b2004-06-03 03:38:44 +0000239 switch(tag_len) {
240 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
241 /* Fall through */
242 case -1: RETURN(RC_FAIL);
243 }
244
Lev Walkin8c3b8542005-03-10 18:52:02 +0000245 if(ctx->left < 0 && ((const uint8_t *)ptr)[0] == 0) {
Lev Walkin566a5672004-10-05 06:36:57 +0000246 if(LEFT < 2) {
247 if(SIZE_VIOLATION)
248 RETURN(RC_FAIL);
249 else
250 RETURN(RC_WMORE);
Lev Walkin8c3b8542005-03-10 18:52:02 +0000251 } else if(((const uint8_t *)ptr)[1] == 0) {
Lev Walkin494fb702017-08-07 20:07:00 -0700252 ASN_DEBUG("edx = %zu, opt = %d, ec=%d",
Lev Walkin566a5672004-10-05 06:36:57 +0000253 edx, elements[edx].optional,
254 td->elements_count);
255 if((edx + elements[edx].optional
256 == td->elements_count)
257 || (IN_EXTENSION_GROUP(specs, edx)
258 && specs->ext_before
Lev Walkin494fb702017-08-07 20:07:00 -0700259 > (signed)td->elements_count)) {
Lev Walkin566a5672004-10-05 06:36:57 +0000260 /*
261 * Yeah, baby! Found the terminator
262 * of the indefinite length structure.
263 */
264 /*
265 * Proceed to the canonical
266 * finalization function.
267 * No advancing is necessary.
268 */
269 goto phase3;
270 }
271 }
272 }
273
Lev Walkinf15320b2004-06-03 03:38:44 +0000274 /*
275 * Find the next available type with this tag.
276 */
Lev Walkin4c36e302004-06-06 07:20:02 +0000277 use_bsearch = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000278 opt_edx_end = edx + elements[edx].optional + 1;
Lev Walkin449f8322004-08-20 13:23:42 +0000279 if(opt_edx_end > td->elements_count)
280 opt_edx_end = td->elements_count; /* Cap */
Lev Walkinf9dbd9a2004-07-01 00:48:04 +0000281 else if(opt_edx_end - edx > 8) {
Lev Walkin924fa032004-06-14 13:42:23 +0000282 /* Limit the scope of linear search... */
Lev Walkinf9dbd9a2004-07-01 00:48:04 +0000283 opt_edx_end = edx + 8;
Lev Walkin4c36e302004-06-06 07:20:02 +0000284 use_bsearch = 1;
Lev Walkin924fa032004-06-14 13:42:23 +0000285 /* ... and resort to bsearch() */
Lev Walkin4c36e302004-06-06 07:20:02 +0000286 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000287 for(n = edx; n < opt_edx_end; n++) {
288 if(BER_TAGS_EQUAL(tlv_tag, elements[n].tag)) {
289 /*
290 * Found element corresponding to the tag
291 * being looked at.
292 * Reposition over the right element.
293 */
294 edx = n;
Lev Walkin4c36e302004-06-06 07:20:02 +0000295 ctx->step = 1 + 2 * edx; /* Remember! */
296 goto microphase2;
Lev Walkin14fd3e52017-08-27 01:38:45 -0700297 } else if(elements[n].flags & ATF_ANY_TYPE) {
Lev Walkinb9189732004-09-10 09:37:12 +0000298 /*
299 * This is the ANY type, which may bear
300 * any flag whatsoever.
301 */
302 edx = n;
303 ctx->step = 1 + 2 * edx; /* Remember! */
304 goto microphase2;
Lev Walkin4c36e302004-06-06 07:20:02 +0000305 } else if(elements[n].tag == (ber_tlv_tag_t)-1) {
306 use_bsearch = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000307 break;
308 }
309 }
Lev Walkin4c36e302004-06-06 07:20:02 +0000310 if(use_bsearch) {
311 /*
Lev Walkin4e0704f2004-09-04 06:17:35 +0000312 * Resort to a binary search over
Lev Walkin4c36e302004-06-06 07:20:02 +0000313 * sorted array of tags.
314 */
Wim Lewisfb6344e2014-07-28 12:16:01 -0700315 const asn_TYPE_tag2member_t *t2m;
Lev Walkineff98a52017-09-13 22:24:35 +0000316 asn_TYPE_tag2member_t key = {0, 0, 0, 0};
317 key.el_tag = tlv_tag;
318 key.el_no = edx;
Wim Lewisfb6344e2014-07-28 12:16:01 -0700319 t2m = (const asn_TYPE_tag2member_t *)bsearch(&key,
Lev Walkinc2346572004-08-11 09:07:36 +0000320 specs->tag2el, specs->tag2el_count,
Lev Walkin4c36e302004-06-06 07:20:02 +0000321 sizeof(specs->tag2el[0]), _t2e_cmp);
Lev Walkin924fa032004-06-14 13:42:23 +0000322 if(t2m) {
Wim Lewisfb6344e2014-07-28 12:16:01 -0700323 const asn_TYPE_tag2member_t *best = 0;
324 const asn_TYPE_tag2member_t *t2m_f, *t2m_l;
Lev Walkin494fb702017-08-07 20:07:00 -0700325 size_t edx_max = edx + elements[edx].optional;
Lev Walkin4c36e302004-06-06 07:20:02 +0000326 /*
327 * Rewind to the first element with that tag,
328 * `cause bsearch() does not guarantee order.
329 */
Lev Walkin924fa032004-06-14 13:42:23 +0000330 t2m_f = t2m + t2m->toff_first;
331 t2m_l = t2m + t2m->toff_last;
332 for(t2m = t2m_f; t2m <= t2m_l; t2m++) {
333 if(t2m->el_no > edx_max) break;
334 if(t2m->el_no < edx) continue;
335 best = t2m;
336 }
337 if(best) {
338 edx = best->el_no;
339 ctx->step = 1 + 2 * edx;
340 goto microphase2;
341 }
Lev Walkin4c36e302004-06-06 07:20:02 +0000342 }
343 n = opt_edx_end;
344 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000345 if(n == opt_edx_end) {
346 /*
347 * If tag is unknown, it may be either
348 * an unknown (thus, incorrect) tag,
349 * or an extension (...),
350 * or an end of the indefinite-length structure.
351 */
Lev Walkin7cbbc902006-10-03 06:39:36 +0000352 if(!IN_EXTENSION_GROUP(specs,
353 edx + elements[edx].optional)) {
Lev Walkin24810022017-08-25 12:16:11 -0700354 ASN_DEBUG("Unexpected tag %s (at %zu)",
Lev Walkin566a5672004-10-05 06:36:57 +0000355 ber_tlv_tag_string(tlv_tag), edx);
Lev Walkin4c36e302004-06-06 07:20:02 +0000356 ASN_DEBUG("Expected tag %s (%s)%s",
Lev Walkinf15320b2004-06-03 03:38:44 +0000357 ber_tlv_tag_string(elements[edx].tag),
Lev Walkin4c36e302004-06-06 07:20:02 +0000358 elements[edx].name,
Lev Walkinf15320b2004-06-03 03:38:44 +0000359 elements[edx].optional
360 ?" or alternatives":"");
361 RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000362 } else {
363 /* Skip this tag */
364 ssize_t skip;
Lev Walkin7cbbc902006-10-03 06:39:36 +0000365 edx += elements[edx].optional;
Lev Walkinf15320b2004-06-03 03:38:44 +0000366
Lev Walkin24810022017-08-25 12:16:11 -0700367 ASN_DEBUG("Skipping unexpected %s (at %zu)",
Lev Walkin7cbbc902006-10-03 06:39:36 +0000368 ber_tlv_tag_string(tlv_tag), edx);
Lev Walkinbaaa24f2004-09-29 14:19:14 +0000369 skip = ber_skip_length(opt_codec_ctx,
Lev Walkinf15320b2004-06-03 03:38:44 +0000370 BER_TLV_CONSTRUCTED(ptr),
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800371 (const char *)ptr + tag_len,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000372 LEFT - tag_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000373 ASN_DEBUG("Skip length %d in %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000374 (int)skip, td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000375 switch(skip) {
376 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
377 /* Fall through */
378 case -1: RETURN(RC_FAIL);
379 }
380
381 ADVANCE(skip + tag_len);
382 ctx->step -= 2;
383 edx--;
384 continue; /* Try again with the next tag */
385 }
386 }
387
388 /*
389 * MICROPHASE 2: Invoke the member-specific decoder.
390 */
391 ctx->step |= 1; /* Confirm entering next microphase */
392 microphase2:
Lev Walkin449f8322004-08-20 13:23:42 +0000393 ASN_DEBUG("Inside SEQUENCE %s MF2", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000394
395 /*
396 * Compute the position of the member inside a structure,
397 * and also a type of containment (it may be contained
398 * as pointer or using inline inclusion).
399 */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000400 if(elements[edx].flags & ATF_POINTER) {
401 /* Member is a pointer to another structure */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800402 memb_ptr2 = (void **)((char *)st + elements[edx].memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000403 } else {
404 /*
405 * A pointer to a pointer
406 * holding the start of the structure
407 */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800408 memb_ptr = (char *)st + elements[edx].memb_offset;
Lev Walkinf15320b2004-06-03 03:38:44 +0000409 memb_ptr2 = &memb_ptr;
410 }
411 /*
412 * Invoke the member fetch routine according to member's type
413 */
Lev Walkin75b6fe42017-08-27 01:03:22 -0700414 if(elements[edx].flags & ATF_OPEN_TYPE) {
Lev Walkinc10d30a2017-08-27 00:38:21 -0700415 rval = OPEN_TYPE_ber_get(opt_codec_ctx, td, st, &elements[edx], ptr, LEFT);
416 } else {
417 rval = elements[edx].type->op->ber_decoder(opt_codec_ctx,
418 elements[edx].type,
419 memb_ptr2, ptr, LEFT,
420 elements[edx].tag_mode);
421 }
Lev Walkin24810022017-08-25 12:16:11 -0700422 ASN_DEBUG("In %s SEQUENCE decoded %zu %s of %d "
Lev Walkin5beb7de2004-09-24 20:59:27 +0000423 "in %d bytes rval.code %d, size=%d",
Lev Walkin449f8322004-08-20 13:23:42 +0000424 td->name, edx, elements[edx].type->name,
Lev Walkin5beb7de2004-09-24 20:59:27 +0000425 (int)LEFT, (int)rval.consumed, rval.code, (int)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000426 switch(rval.code) {
427 case RC_OK:
428 break;
429 case RC_WMORE: /* More data expected */
430 if(!SIZE_VIOLATION) {
431 ADVANCE(rval.consumed);
432 RETURN(RC_WMORE);
433 }
Lev Walkin5beb7de2004-09-24 20:59:27 +0000434 ASN_DEBUG("Size violation (c->l=%ld <= s=%ld)",
435 (long)ctx->left, (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000436 /* Fall through */
437 case RC_FAIL: /* Fatal error */
438 RETURN(RC_FAIL);
439 } /* switch(rval) */
440
441 ADVANCE(rval.consumed);
442 } /* for(all structure members) */
443
444 phase3:
445 ctx->phase = 3;
446 case 3: /* 00 and other tags expected */
447 case 4: /* only 00's expected */
448
449 ASN_DEBUG("SEQUENCE %s Leftover: %ld, size = %ld",
Lev Walkin449f8322004-08-20 13:23:42 +0000450 td->name, (long)ctx->left, (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000451
452 /*
453 * Skip everything until the end of the SEQUENCE.
454 */
455 while(ctx->left) {
456 ssize_t tl, ll;
457
458 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
459 switch(tl) {
460 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
461 /* Fall through */
462 case -1: RETURN(RC_FAIL);
463 }
464
465 /*
466 * If expected <0><0>...
467 */
468 if(ctx->left < 0
Lev Walkin8c3b8542005-03-10 18:52:02 +0000469 && ((const uint8_t *)ptr)[0] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000470 if(LEFT < 2) {
471 if(SIZE_VIOLATION)
472 RETURN(RC_FAIL);
473 else
474 RETURN(RC_WMORE);
Lev Walkin8c3b8542005-03-10 18:52:02 +0000475 } else if(((const uint8_t *)ptr)[1] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000476 /*
477 * Correctly finished with <0><0>.
478 */
479 ADVANCE(2);
480 ctx->left++;
481 ctx->phase = 4;
482 continue;
483 }
484 }
485
Lev Walkin449f8322004-08-20 13:23:42 +0000486 if(!IN_EXTENSION_GROUP(specs, td->elements_count)
Lev Walkinf15320b2004-06-03 03:38:44 +0000487 || ctx->phase == 4) {
488 ASN_DEBUG("Unexpected continuation "
489 "of a non-extensible type "
490 "%s (SEQUENCE): %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000491 td->name,
Lev Walkinf15320b2004-06-03 03:38:44 +0000492 ber_tlv_tag_string(tlv_tag));
493 RETURN(RC_FAIL);
494 }
495
Lev Walkinbaaa24f2004-09-29 14:19:14 +0000496 ll = ber_skip_length(opt_codec_ctx,
Lev Walkinf15320b2004-06-03 03:38:44 +0000497 BER_TLV_CONSTRUCTED(ptr),
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800498 (const char *)ptr + tl, LEFT - tl);
Lev Walkinf15320b2004-06-03 03:38:44 +0000499 switch(ll) {
500 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
501 /* Fall through */
502 case -1: RETURN(RC_FAIL);
503 }
504
505 ADVANCE(tl + ll);
506 }
507
508 PHASE_OUT(ctx);
509 }
510
511 RETURN(RC_OK);
512}
513
Lev Walkin4c36e302004-06-06 07:20:02 +0000514
Lev Walkinf15320b2004-06-03 03:38:44 +0000515/*
516 * The DER encoder of the SEQUENCE type.
517 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000518asn_enc_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -0700519SEQUENCE_encode_der(const asn_TYPE_descriptor_t *td, const void *sptr,
520 int tag_mode, ber_tlv_tag_t tag,
521 asn_app_consume_bytes_f *cb, void *app_key) {
522 size_t computed_size = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000523 asn_enc_rval_t erval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000524 ssize_t ret;
Lev Walkin494fb702017-08-07 20:07:00 -0700525 size_t edx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000526
527 ASN_DEBUG("%s %s as SEQUENCE",
Lev Walkin449f8322004-08-20 13:23:42 +0000528 cb?"Encoding":"Estimating", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000529
530 /*
531 * Gather the length of the underlying members sequence.
532 */
Lev Walkin449f8322004-08-20 13:23:42 +0000533 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000534 asn_TYPE_member_t *elm = &td->elements[edx];
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400535
Lev Walkin20696a42017-10-17 21:27:33 -0700536 const void *memb_ptr; /* Pointer to the member */
537 const void *const *memb_ptr2; /* Pointer to that pointer */
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400538
Lev Walkin20696a42017-10-17 21:27:33 -0700539 if(elm->flags & ATF_POINTER) {
540 memb_ptr2 =
541 (const void *const *)((const char *)sptr + elm->memb_offset);
542 if(!*memb_ptr2) {
Lev Walkin28ba9db2017-08-31 02:06:58 -0700543 ASN_DEBUG("Element %s %zu not present",
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400544 elm->name, edx);
545 if(elm->optional)
546 continue;
Lev Walkinac589332005-08-22 14:19:28 +0000547 /* Mandatory element is missing */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700548 ASN__ENCODE_FAILED;
Lev Walkinac589332005-08-22 14:19:28 +0000549 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000550 } else {
Lev Walkin20696a42017-10-17 21:27:33 -0700551 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
552 memb_ptr2 = &memb_ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000553 }
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400554
555 /* Eliminate default values */
Lev Walkin20696a42017-10-17 21:27:33 -0700556 if(elm->default_value_cmp && elm->default_value_cmp(*memb_ptr2) == 0)
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400557 continue;
558
559 erval = elm->type->op->der_encoder(elm->type, *memb_ptr2,
Lev Walkinf15320b2004-06-03 03:38:44 +0000560 elm->tag_mode, elm->tag,
561 0, 0);
562 if(erval.encoded == -1)
563 return erval;
564 computed_size += erval.encoded;
Lev Walkin24810022017-08-25 12:16:11 -0700565 ASN_DEBUG("Member %zu %s estimated %ld bytes",
Lev Walkinf15320b2004-06-03 03:38:44 +0000566 edx, elm->name, (long)erval.encoded);
567 }
568
569 /*
570 * Encode the TLV for the sequence itself.
571 */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000572 ret = der_write_tags(td, computed_size, tag_mode, 1, tag, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +0000573 ASN_DEBUG("Wrote tags: %ld (+%ld)", (long)ret, (long)computed_size);
Lev Walkinac589332005-08-22 14:19:28 +0000574 if(ret == -1)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700575 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000576 erval.encoded = computed_size + ret;
577
Lev Walkin7c1dc052016-03-14 03:08:15 -0700578 if(!cb) ASN__ENCODED_OK(erval);
Lev Walkinf15320b2004-06-03 03:38:44 +0000579
580 /*
581 * Encode all members.
582 */
Lev Walkin449f8322004-08-20 13:23:42 +0000583 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000584 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkina9cc46e2004-09-22 16:06:28 +0000585 asn_enc_rval_t tmperval;
Lev Walkin20696a42017-10-17 21:27:33 -0700586 const void *memb_ptr; /* Pointer to the member */
587 const void *const *memb_ptr2; /* Pointer to that pointer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000588
Lev Walkin20696a42017-10-17 21:27:33 -0700589 if(elm->flags & ATF_POINTER) {
590 memb_ptr2 =
591 (const void *const *)((const char *)sptr + elm->memb_offset);
592 if(!*memb_ptr2) continue;
Lev Walkinf15320b2004-06-03 03:38:44 +0000593 } else {
Lev Walkin20696a42017-10-17 21:27:33 -0700594 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
595 memb_ptr2 = &memb_ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000596 }
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400597
598 /* Eliminate default values */
Lev Walkin20696a42017-10-17 21:27:33 -0700599 if(elm->default_value_cmp && elm->default_value_cmp(*memb_ptr2) == 0)
600 continue;
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400601
602 tmperval = elm->type->op->der_encoder(elm->type, *memb_ptr2,
603 elm->tag_mode, elm->tag, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +0000604 if(tmperval.encoded == -1)
605 return tmperval;
606 computed_size -= tmperval.encoded;
Lev Walkin24810022017-08-25 12:16:11 -0700607 ASN_DEBUG("Member %zu %s of SEQUENCE %s encoded in %ld bytes",
Lev Walkina6a926a2005-03-02 01:54:28 +0000608 edx, elm->name, td->name, (long)tmperval.encoded);
Lev Walkinf15320b2004-06-03 03:38:44 +0000609 }
610
Lev Walkinac589332005-08-22 14:19:28 +0000611 if(computed_size != 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000612 /*
613 * Encoded size is not equal to the computed size.
614 */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700615 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000616
Lev Walkin7c1dc052016-03-14 03:08:15 -0700617 ASN__ENCODED_OK(erval);
Lev Walkinf15320b2004-06-03 03:38:44 +0000618}
619
Lev Walkinc61f3862005-02-14 17:21:22 +0000620
Lev Walkin1bbc2002004-10-23 13:27:30 +0000621#undef XER_ADVANCE
Lev Walkinf59dac92017-08-26 21:26:51 -0700622#define XER_ADVANCE(num_bytes) \
623 do { \
624 size_t num = (num_bytes); \
625 ptr = ((const char *)ptr) + num; \
626 size -= num; \
627 consumed_myself += num; \
628 } while(0)
Lev Walkin1bbc2002004-10-23 13:27:30 +0000629
Lev Walkinc61f3862005-02-14 17:21:22 +0000630/*
631 * Decode the XER (XML) data.
632 */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000633asn_dec_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -0700634SEQUENCE_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
635 const asn_TYPE_descriptor_t *td, void **struct_ptr,
636 const char *opt_mname, const void *ptr, size_t size) {
637 /*
Lev Walkin1bbc2002004-10-23 13:27:30 +0000638 * Bring closer parts of structure description.
639 */
Lev Walkind84f6032017-10-03 16:33:59 -0700640 const asn_SEQUENCE_specifics_t *specs
641 = (const asn_SEQUENCE_specifics_t *)td->specifics;
Lev Walkin1bbc2002004-10-23 13:27:30 +0000642 asn_TYPE_member_t *elements = td->elements;
643 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
644
645 /*
Lev Walkinc61f3862005-02-14 17:21:22 +0000646 * ... and parts of the structure being constructed.
Lev Walkin1bbc2002004-10-23 13:27:30 +0000647 */
648 void *st = *struct_ptr; /* Target structure. */
649 asn_struct_ctx_t *ctx; /* Decoder context */
650
Lev Walkinc61f3862005-02-14 17:21:22 +0000651 asn_dec_rval_t rval; /* Return value from a decoder */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000652 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
Lev Walkin494fb702017-08-07 20:07:00 -0700653 size_t edx; /* Element index */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000654
655 /*
656 * Create the target structure if it is not present already.
657 */
658 if(st == 0) {
659 st = *struct_ptr = CALLOC(1, specs->struct_size);
660 if(st == 0) RETURN(RC_FAIL);
661 }
662
663 /*
664 * Restore parsing context.
665 */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800666 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkin1bbc2002004-10-23 13:27:30 +0000667
668
669 /*
670 * Phases of XER/XML processing:
671 * Phase 0: Check that the opening tag matches our expectations.
672 * Phase 1: Processing body and reacting on closing tag.
673 * Phase 2: Processing inner type.
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000674 * Phase 3: Skipping unknown extensions.
675 * Phase 4: PHASED OUT
Lev Walkin1bbc2002004-10-23 13:27:30 +0000676 */
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000677 for(edx = ctx->step; ctx->phase <= 3;) {
Lev Walkin1bbc2002004-10-23 13:27:30 +0000678 pxer_chunk_type_e ch_type; /* XER chunk type */
679 ssize_t ch_size; /* Chunk size */
680 xer_check_tag_e tcv; /* Tag check value */
681 asn_TYPE_member_t *elm;
682
683 /*
Lev Walkinc61f3862005-02-14 17:21:22 +0000684 * Go inside the inner member of a sequence.
Lev Walkin1bbc2002004-10-23 13:27:30 +0000685 */
686 if(ctx->phase == 2) {
687 asn_dec_rval_t tmprval;
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400688 void *memb_ptr_dontuse; /* Pointer to the member */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000689 void **memb_ptr2; /* Pointer to that pointer */
690
Lev Walkinabf68892004-10-26 10:12:14 +0000691 elm = &td->elements[edx];
692
Lev Walkin1bbc2002004-10-23 13:27:30 +0000693 if(elm->flags & ATF_POINTER) {
694 /* Member is a pointer to another structure */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800695 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
Lev Walkin1bbc2002004-10-23 13:27:30 +0000696 } else {
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400697 memb_ptr_dontuse = (char *)st + elm->memb_offset;
698 memb_ptr2 = &memb_ptr_dontuse; /* Only use of memb_ptr_dontuse */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000699 }
700
Lev Walkin75b6fe42017-08-27 01:03:22 -0700701 if(elm->flags & ATF_OPEN_TYPE) {
Lev Walkinf59dac92017-08-26 21:26:51 -0700702 tmprval = OPEN_TYPE_xer_get(opt_codec_ctx, td, st, elm, ptr, size);
703 } else {
704 /* Invoke the inner type decoder, m.b. multiple times */
705 tmprval = elm->type->op->xer_decoder(opt_codec_ctx,
706 elm->type, memb_ptr2, elm->name,
707 ptr, size);
708 }
Lev Walkin1bbc2002004-10-23 13:27:30 +0000709 XER_ADVANCE(tmprval.consumed);
710 if(tmprval.code != RC_OK)
711 RETURN(tmprval.code);
Lev Walkinc61f3862005-02-14 17:21:22 +0000712 ctx->phase = 1; /* Back to body processing */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000713 ctx->step = ++edx;
Lev Walkinc61f3862005-02-14 17:21:22 +0000714 ASN_DEBUG("XER/SEQUENCE phase => %d, step => %d",
715 ctx->phase, ctx->step);
Lev Walkin1bbc2002004-10-23 13:27:30 +0000716 /* Fall through */
717 }
718
719 /*
720 * Get the next part of the XML stream.
721 */
Lev Walkinf59dac92017-08-26 21:26:51 -0700722 ch_size = xer_next_token(&ctx->context, ptr, size,
Lev Walkin1e443962005-02-18 18:06:36 +0000723 &ch_type);
Lev Walkin97363482016-01-24 19:23:02 -0800724 if(ch_size == -1) {
725 RETURN(RC_FAIL);
Lev Walkinf59dac92017-08-26 21:26:51 -0700726 } else {
Lev Walkin1bbc2002004-10-23 13:27:30 +0000727 switch(ch_type) {
Lev Walkinf59dac92017-08-26 21:26:51 -0700728 case PXER_WMORE:
729 RETURN(RC_WMORE);
Lev Walkin1bbc2002004-10-23 13:27:30 +0000730 case PXER_COMMENT: /* Got XML comment */
731 case PXER_TEXT: /* Ignore free-standing text */
732 XER_ADVANCE(ch_size); /* Skip silently */
733 continue;
734 case PXER_TAG:
735 break; /* Check the rest down there */
736 }
737 }
738
Lev Walkinf59dac92017-08-26 21:26:51 -0700739 tcv = xer_check_tag(ptr, ch_size, xml_tag);
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000740 ASN_DEBUG("XER/SEQUENCE: tcv = %d, ph=%d [%s]",
741 tcv, ctx->phase, xml_tag);
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000742
743 /* Skip the extensions section */
744 if(ctx->phase == 3) {
745 switch(xer_skip_unknown(tcv, &ctx->left)) {
746 case -1:
747 ctx->phase = 4;
748 RETURN(RC_FAIL);
749 case 0:
750 XER_ADVANCE(ch_size);
751 continue;
752 case 1:
753 XER_ADVANCE(ch_size);
754 ctx->phase = 1;
755 continue;
756 case 2:
757 ctx->phase = 1;
758 break;
759 }
760 }
761
Lev Walkin1bbc2002004-10-23 13:27:30 +0000762 switch(tcv) {
763 case XCT_CLOSING:
764 if(ctx->phase == 0) break;
Lev Walkin1bbc2002004-10-23 13:27:30 +0000765 ctx->phase = 0;
766 /* Fall through */
767 case XCT_BOTH:
768 if(ctx->phase == 0) {
769 if(edx >= td->elements_count
770 ||
771 /* Explicit OPTIONAL specs reaches the end */
772 (edx + elements[edx].optional
773 == td->elements_count)
774 ||
775 /* All extensions are optional */
776 (IN_EXTENSION_GROUP(specs, edx)
777 && specs->ext_before
Lev Walkin494fb702017-08-07 20:07:00 -0700778 > (signed)td->elements_count)
Lev Walkin1bbc2002004-10-23 13:27:30 +0000779 ) {
780 XER_ADVANCE(ch_size);
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000781 ctx->phase = 4; /* Phase out */
Lev Walkinc61f3862005-02-14 17:21:22 +0000782 RETURN(RC_OK);
Lev Walkin1bbc2002004-10-23 13:27:30 +0000783 } else {
784 ASN_DEBUG("Premature end of XER SEQUENCE");
785 RETURN(RC_FAIL);
786 }
787 }
788 /* Fall through */
789 case XCT_OPENING:
790 if(ctx->phase == 0) {
791 XER_ADVANCE(ch_size);
792 ctx->phase = 1; /* Processing body phase */
793 continue;
794 }
Lev Walkinda3ca412004-10-23 14:58:15 +0000795 /* Fall through */
Lev Walkin904e65b2005-02-18 14:23:48 +0000796 case XCT_UNKNOWN_OP:
797 case XCT_UNKNOWN_BO:
Lev Walkin1bbc2002004-10-23 13:27:30 +0000798
Lev Walkin24810022017-08-25 12:16:11 -0700799 ASN_DEBUG("XER/SEQUENCE: tcv=%d, ph=%d, edx=%zu",
Lev Walkin02666192005-07-03 05:31:02 +0000800 tcv, ctx->phase, edx);
801 if(ctx->phase != 1) {
Lev Walkin1bbc2002004-10-23 13:27:30 +0000802 break; /* Really unexpected */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000803 }
Lev Walkin02666192005-07-03 05:31:02 +0000804
805 if(edx < td->elements_count) {
806 /*
807 * Search which member corresponds to this tag.
808 */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800809 size_t n;
810 size_t edx_end = edx + elements[edx].optional + 1;
Lev Walkin02666192005-07-03 05:31:02 +0000811 if(edx_end > td->elements_count)
812 edx_end = td->elements_count;
813 for(n = edx; n < edx_end; n++) {
814 elm = &td->elements[n];
Lev Walkinf59dac92017-08-26 21:26:51 -0700815 tcv = xer_check_tag(ptr, ch_size, elm->name);
Lev Walkin02666192005-07-03 05:31:02 +0000816 switch(tcv) {
817 case XCT_BOTH:
818 case XCT_OPENING:
819 /*
820 * Process this member.
821 */
822 ctx->step = edx = n;
823 ctx->phase = 2;
824 break;
825 case XCT_UNKNOWN_OP:
826 case XCT_UNKNOWN_BO:
827 continue;
828 default:
829 n = edx_end;
830 break; /* Phase out */
831 }
832 break;
833 }
834 if(n != edx_end)
835 continue;
836 } else {
Lev Walkin24810022017-08-25 12:16:11 -0700837 ASN_DEBUG("Out of defined members: %zu/%u",
Lev Walkin02666192005-07-03 05:31:02 +0000838 edx, td->elements_count);
839 }
Lev Walkin904e65b2005-02-18 14:23:48 +0000840
841 /* It is expected extension */
842 if(IN_EXTENSION_GROUP(specs,
Lev Walkin02666192005-07-03 05:31:02 +0000843 edx + (edx < td->elements_count
844 ? elements[edx].optional : 0))) {
Lev Walkin24810022017-08-25 12:16:11 -0700845 ASN_DEBUG("Got anticipated extension at %zu",
Lev Walkin02666192005-07-03 05:31:02 +0000846 edx);
Lev Walkin904e65b2005-02-18 14:23:48 +0000847 /*
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000848 * Check for (XCT_BOTH or XCT_UNKNOWN_BO)
849 * By using a mask. Only record a pure
850 * <opening> tags.
Lev Walkin904e65b2005-02-18 14:23:48 +0000851 */
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000852 if(tcv & XCT_CLOSING) {
853 /* Found </extension> without body */
854 } else {
855 ctx->left = 1;
856 ctx->phase = 3; /* Skip ...'s */
857 }
858 XER_ADVANCE(ch_size);
859 continue;
Lev Walkin904e65b2005-02-18 14:23:48 +0000860 }
861
862 /* Fall through */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000863 default:
864 break;
865 }
866
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000867 ASN_DEBUG("Unexpected XML tag in SEQUENCE [%c%c%c%c%c%c]",
Lev Walkinf59dac92017-08-26 21:26:51 -0700868 size>0?((const char *)ptr)[0]:'.',
869 size>1?((const char *)ptr)[1]:'.',
870 size>2?((const char *)ptr)[2]:'.',
871 size>3?((const char *)ptr)[3]:'.',
872 size>4?((const char *)ptr)[4]:'.',
873 size>5?((const char *)ptr)[5]:'.');
Lev Walkin1bbc2002004-10-23 13:27:30 +0000874 break;
875 }
876
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000877 ctx->phase = 4; /* "Phase out" on hard failure */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000878 RETURN(RC_FAIL);
879}
880
Lev Walkina9cc46e2004-09-22 16:06:28 +0000881asn_enc_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -0700882SEQUENCE_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr,
883 int ilevel, enum xer_encoder_flags_e flags,
884 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin28ba9db2017-08-31 02:06:58 -0700885 asn_enc_rval_t er;
886 int xcan = (flags & XER_F_CANONICAL);
887 asn_TYPE_descriptor_t *tmp_def_val_td = 0;
888 void *tmp_def_val = 0;
Lev Walkin494fb702017-08-07 20:07:00 -0700889 size_t edx;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000890
Lev Walkin28ba9db2017-08-31 02:06:58 -0700891 if(!sptr) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000892
Lev Walkin28ba9db2017-08-31 02:06:58 -0700893 er.encoded = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000894
Lev Walkin28ba9db2017-08-31 02:06:58 -0700895 for(edx = 0; edx < td->elements_count; edx++) {
896 asn_enc_rval_t tmper;
897 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkin20696a42017-10-17 21:27:33 -0700898 const void *memb_ptr;
Lev Walkin28ba9db2017-08-31 02:06:58 -0700899 const char *mname = elm->name;
900 unsigned int mlen = strlen(mname);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000901
Lev Walkin28ba9db2017-08-31 02:06:58 -0700902 if(elm->flags & ATF_POINTER) {
Lev Walkin20696a42017-10-17 21:27:33 -0700903 memb_ptr =
904 *(const void *const *)((const char *)sptr + elm->memb_offset);
Lev Walkin28ba9db2017-08-31 02:06:58 -0700905 if(!memb_ptr) {
906 assert(tmp_def_val == 0);
Lev Walkin20696a42017-10-17 21:27:33 -0700907 if(elm->default_value_set) {
908 if(elm->default_value_set(&tmp_def_val)) {
Lev Walkin28ba9db2017-08-31 02:06:58 -0700909 ASN__ENCODE_FAILED;
910 } else {
911 memb_ptr = tmp_def_val;
912 tmp_def_val_td = elm->type;
913 }
914 } else if(elm->optional) {
915 continue;
916 } else {
917 /* Mandatory element is missing */
918 ASN__ENCODE_FAILED;
919 }
920 }
921 } else {
Lev Walkin20696a42017-10-17 21:27:33 -0700922 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
Lev Walkin28ba9db2017-08-31 02:06:58 -0700923 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000924
Lev Walkin28ba9db2017-08-31 02:06:58 -0700925 if(!xcan) ASN__TEXT_INDENT(1, ilevel);
926 ASN__CALLBACK3("<", 1, mname, mlen, ">", 1);
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400927
Lev Walkin28ba9db2017-08-31 02:06:58 -0700928 /* Print the member itself */
929 tmper = elm->type->op->xer_encoder(elm->type, memb_ptr, ilevel + 1,
930 flags, cb, app_key);
931 if(tmp_def_val) {
932 ASN_STRUCT_FREE(*tmp_def_val_td, tmp_def_val);
933 tmp_def_val = 0;
934 }
935 if(tmper.encoded == -1) return tmper;
Lev Walkin4fe28822017-09-18 02:57:34 -0700936 er.encoded += tmper.encoded;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000937
Lev Walkin28ba9db2017-08-31 02:06:58 -0700938 ASN__CALLBACK3("</", 2, mname, mlen, ">", 1);
Lev Walkin28ba9db2017-08-31 02:06:58 -0700939 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000940
Lev Walkin28ba9db2017-08-31 02:06:58 -0700941 if(!xcan) ASN__TEXT_INDENT(1, ilevel - 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000942
Lev Walkin28ba9db2017-08-31 02:06:58 -0700943 ASN__ENCODED_OK(er);
Lev Walkin942fd082004-10-03 09:13:02 +0000944cb_failed:
Lev Walkin28ba9db2017-08-31 02:06:58 -0700945 if(tmp_def_val) ASN_STRUCT_FREE(*tmp_def_val_td, tmp_def_val);
946 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000947}
948
Lev Walkinf15320b2004-06-03 03:38:44 +0000949int
Lev Walkin20696a42017-10-17 21:27:33 -0700950SEQUENCE_print(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
951 asn_app_consume_bytes_f *cb, void *app_key) {
952 size_t edx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000953 int ret;
954
Lev Walkin8e8078a2004-09-26 13:10:40 +0000955 if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000956
957 /* Dump preamble */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000958 if(cb(td->name, strlen(td->name), app_key) < 0
959 || cb(" ::= {", 6, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000960 return -1;
961
Lev Walkin449f8322004-08-20 13:23:42 +0000962 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000963 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000964 const void *memb_ptr;
965
Lev Walkincc93b0f2004-09-10 09:18:20 +0000966 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800967 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkinac589332005-08-22 14:19:28 +0000968 if(!memb_ptr) {
969 if(elm->optional) continue;
970 /* Print <absent> line */
971 /* Fall through */
972 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000973 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800974 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000975 }
976
977 /* Indentation */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000978 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000979
980 /* Print the member's name and stuff */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000981 if(cb(elm->name, strlen(elm->name), app_key) < 0
982 || cb(": ", 2, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000983 return -1;
984
985 /* Print the member itself */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800986 ret = elm->type->op->print_struct(elm->type, memb_ptr, ilevel + 1,
Lev Walkinf15320b2004-06-03 03:38:44 +0000987 cb, app_key);
988 if(ret) return ret;
Lev Walkinf15320b2004-06-03 03:38:44 +0000989 }
990
Lev Walkin8e8078a2004-09-26 13:10:40 +0000991 ilevel--;
992 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000993
Lev Walkin8e8078a2004-09-26 13:10:40 +0000994 return (cb("}", 1, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000995}
996
997void
Lev Walkin8d99d7b2017-08-25 01:06:00 -0700998SEQUENCE_free(const asn_TYPE_descriptor_t *td, void *sptr,
999 enum asn_struct_free_method method) {
Lev Walkinf6853ce2017-08-11 00:50:27 -07001000 size_t edx;
Vasil Velichkovaead8c52017-10-10 05:34:48 +03001001 const asn_SEQUENCE_specifics_t *specs =
1002 (const asn_SEQUENCE_specifics_t *)td->specifics;
1003 asn_struct_ctx_t *ctx; /* Decoder context */
Lev Walkinf15320b2004-06-03 03:38:44 +00001004
1005 if(!td || !sptr)
1006 return;
1007
1008 ASN_DEBUG("Freeing %s as SEQUENCE", td->name);
1009
Lev Walkin449f8322004-08-20 13:23:42 +00001010 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +00001011 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +00001012 void *memb_ptr;
Lev Walkincc93b0f2004-09-10 09:18:20 +00001013 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001014 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +00001015 if(memb_ptr)
Lev Walkinadcb5862006-03-17 02:11:12 +00001016 ASN_STRUCT_FREE(*elm->type, memb_ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +00001017 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001018 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkinadcb5862006-03-17 02:11:12 +00001019 ASN_STRUCT_FREE_CONTENTS_ONLY(*elm->type, memb_ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +00001020 }
1021 }
1022
Vasil Velichkovaead8c52017-10-10 05:34:48 +03001023 /* Clean parsing context */
1024 ctx = (asn_struct_ctx_t *)((char *)sptr + specs->ctx_offset);
1025 FREEMEM(ctx->ptr);
1026
Lev Walkin8d99d7b2017-08-25 01:06:00 -07001027 switch(method) {
1028 case ASFM_FREE_EVERYTHING:
1029 FREEMEM(sptr);
1030 break;
1031 case ASFM_FREE_UNDERLYING:
1032 break;
1033 case ASFM_FREE_UNDERLYING_AND_RESET:
Lev Walkind84f6032017-10-03 16:33:59 -07001034 memset(
1035 sptr, 0,
1036 ((const asn_SEQUENCE_specifics_t *)(td->specifics))->struct_size);
Lev Walkin8d99d7b2017-08-25 01:06:00 -07001037 break;
1038 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001039}
1040
1041int
Lev Walkin20696a42017-10-17 21:27:33 -07001042SEQUENCE_constraint(const asn_TYPE_descriptor_t *td, const void *sptr,
1043 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
1044 size_t edx;
Lev Walkinf15320b2004-06-03 03:38:44 +00001045
1046 if(!sptr) {
Lev Walkin7c1dc052016-03-14 03:08:15 -07001047 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +00001048 "%s: value not given (%s:%d)",
1049 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +00001050 return -1;
1051 }
1052
1053 /*
1054 * Iterate over structure members and check their validity.
1055 */
Lev Walkin449f8322004-08-20 13:23:42 +00001056 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +00001057 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +00001058 const void *memb_ptr;
1059
Lev Walkincc93b0f2004-09-10 09:18:20 +00001060 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001061 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkinac589332005-08-22 14:19:28 +00001062 if(!memb_ptr) {
1063 if(elm->optional)
1064 continue;
Lev Walkin7c1dc052016-03-14 03:08:15 -07001065 ASN__CTFAIL(app_key, td, sptr,
Lev Walkinac589332005-08-22 14:19:28 +00001066 "%s: mandatory element %s absent (%s:%d)",
1067 td->name, elm->name, __FILE__, __LINE__);
1068 return -1;
1069 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001070 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001071 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +00001072 }
1073
Lev Walkina5972be2017-09-29 23:15:58 -07001074 if(elm->encoding_constraints.general_constraints) {
1075 int ret = elm->encoding_constraints.general_constraints(elm->type, memb_ptr,
Lev Walkin1eded352006-07-13 11:19:01 +00001076 ctfailcb, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +00001077 if(ret) return ret;
1078 } else {
Lev Walkina5972be2017-09-29 23:15:58 -07001079 return elm->type->encoding_constraints.general_constraints(elm->type,
Lev Walkin1eded352006-07-13 11:19:01 +00001080 memb_ptr, ctfailcb, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +00001081 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001082 }
1083
1084 return 0;
1085}
Lev Walkin59b176e2005-11-26 11:25:14 +00001086
Lev Walkin24810022017-08-25 12:16:11 -07001087#ifndef ASN_DISABLE_PER_SUPPORT
1088
Lev Walkin59b176e2005-11-26 11:25:14 +00001089asn_dec_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -07001090SEQUENCE_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
1091 const asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -07001092 const asn_per_constraints_t *constraints, void **sptr,
1093 asn_per_data_t *pd) {
Lev Walkin20696a42017-10-17 21:27:33 -07001094 const asn_SEQUENCE_specifics_t *specs = (const asn_SEQUENCE_specifics_t *)td->specifics;
Lev Walkin59b176e2005-11-26 11:25:14 +00001095 void *st = *sptr; /* Target structure. */
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001096 int extpresent; /* Extension additions are present */
Lev Walkin59b176e2005-11-26 11:25:14 +00001097 uint8_t *opres; /* Presence of optional root members */
1098 asn_per_data_t opmd;
1099 asn_dec_rval_t rv;
Lev Walkin494fb702017-08-07 20:07:00 -07001100 size_t edx;
Lev Walkin59b176e2005-11-26 11:25:14 +00001101
1102 (void)constraints;
1103
Lev Walkin7c1dc052016-03-14 03:08:15 -07001104 if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx))
1105 ASN__DECODE_FAILED;
Lev Walkin1d9e8dd2005-12-07 05:46:03 +00001106
Lev Walkin59b176e2005-11-26 11:25:14 +00001107 if(!st) {
1108 st = *sptr = CALLOC(1, specs->struct_size);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001109 if(!st) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +00001110 }
1111
1112 ASN_DEBUG("Decoding %s as SEQUENCE (UPER)", td->name);
1113
1114 /* Handle extensions */
1115 if(specs->ext_before >= 0) {
1116 extpresent = per_get_few_bits(pd, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001117 if(extpresent < 0) ASN__DECODE_STARVED;
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001118 } else {
1119 extpresent = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +00001120 }
1121
1122 /* Prepare a place and read-in the presence bitmap */
Lev Walkin73a5cc62007-06-24 08:45:31 +00001123 memset(&opmd, 0, sizeof(opmd));
Lev Walkin59b176e2005-11-26 11:25:14 +00001124 if(specs->roms_count) {
1125 opres = (uint8_t *)MALLOC(((specs->roms_count + 7) >> 3) + 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001126 if(!opres) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +00001127 /* Get the presence map */
1128 if(per_get_many_bits(pd, opres, 0, specs->roms_count)) {
1129 FREEMEM(opres);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001130 ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +00001131 }
1132 opmd.buffer = opres;
Lev Walkin59b176e2005-11-26 11:25:14 +00001133 opmd.nbits = specs->roms_count;
1134 ASN_DEBUG("Read in presence bitmap for %s of %d bits (%x..)",
1135 td->name, specs->roms_count, *opres);
1136 } else {
1137 opres = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +00001138 }
1139
1140 /*
1141 * Get the sequence ROOT elements.
1142 */
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001143 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001144 asn_TYPE_member_t *elm = &td->elements[edx];
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001145 void *memb_ptr; /* Pointer to the member */
Lev Walkin59b176e2005-11-26 11:25:14 +00001146 void **memb_ptr2; /* Pointer to that pointer */
1147
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001148 if(IN_EXTENSION_GROUP(specs, edx))
1149 continue;
1150
Lev Walkin59b176e2005-11-26 11:25:14 +00001151 /* Fetch the pointer to this member */
1152 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001153 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
Lev Walkin59b176e2005-11-26 11:25:14 +00001154 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001155 memb_ptr = (char *)st + elm->memb_offset;
Lev Walkin59b176e2005-11-26 11:25:14 +00001156 memb_ptr2 = &memb_ptr;
1157 }
1158
1159 /* Deal with optionality */
1160 if(elm->optional) {
1161 int present = per_get_few_bits(&opmd, 1);
1162 ASN_DEBUG("Member %s->%s is optional, p=%d (%d->%d)",
1163 td->name, elm->name, present,
1164 (int)opmd.nboff, (int)opmd.nbits);
1165 if(present == 0) {
1166 /* This element is not present */
Lev Walkin20696a42017-10-17 21:27:33 -07001167 if(elm->default_value_set) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001168 /* Fill-in DEFAULT */
Lev Walkin20696a42017-10-17 21:27:33 -07001169 if(elm->default_value_set(memb_ptr2)) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001170 FREEMEM(opres);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001171 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +00001172 }
Lev Walkin8032f7a2007-06-27 01:54:57 +00001173 ASN_DEBUG("Filled-in default");
Lev Walkin59b176e2005-11-26 11:25:14 +00001174 }
1175 /* The member is just not present */
1176 continue;
1177 }
1178 /* Fall through */
1179 }
1180
1181 /* Fetch the member from the stream */
Lev Walkinf6853ce2017-08-11 00:50:27 -07001182 ASN_DEBUG("Decoding member \"%s\" in %s", elm->name, td->name);
Lev Walkin9de6cd82017-08-10 05:47:46 -07001183
Lev Walkin75b6fe42017-08-27 01:03:22 -07001184 if(elm->flags & ATF_OPEN_TYPE) {
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001185 rv = OPEN_TYPE_uper_get(opt_codec_ctx, td, st, elm, pd);
1186 } else {
Lev Walkinf59dac92017-08-26 21:26:51 -07001187 rv = elm->type->op->uper_decoder(opt_codec_ctx, elm->type,
Lev Walkina5972be2017-09-29 23:15:58 -07001188 elm->encoding_constraints.per_constraints, memb_ptr2, pd);
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001189 }
1190 if(rv.code != RC_OK) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001191 ASN_DEBUG("Failed decode %s in %s",
1192 elm->name, td->name);
1193 FREEMEM(opres);
1194 return rv;
1195 }
1196 }
1197
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001198 /* Optionality map is not needed anymore */
1199 FREEMEM(opres);
1200
Lev Walkin59b176e2005-11-26 11:25:14 +00001201 /*
1202 * Deal with extensions.
1203 */
1204 if(extpresent) {
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001205 ssize_t bmlength;
1206 uint8_t *epres; /* Presence of extension members */
1207 asn_per_data_t epmd;
Lev Walkin59b176e2005-11-26 11:25:14 +00001208
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001209 bmlength = uper_get_nslength(pd);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001210 if(bmlength < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +00001211
Lev Walkinaaae9bf2010-10-24 16:56:30 -07001212 ASN_DEBUG("Extensions %ld present in %s", (long)bmlength, td->name);
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001213
1214 epres = (uint8_t *)MALLOC((bmlength + 15) >> 3);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001215 if(!epres) ASN__DECODE_STARVED;
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001216
1217 /* Get the extensions map */
Simo Sorcece71d912015-09-03 16:34:20 -04001218 if(per_get_many_bits(pd, epres, 0, bmlength)) {
1219 FREEMEM(epres);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001220 ASN__DECODE_STARVED;
Simo Sorcece71d912015-09-03 16:34:20 -04001221 }
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001222
Lev Walkin73a5cc62007-06-24 08:45:31 +00001223 memset(&epmd, 0, sizeof(epmd));
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001224 epmd.buffer = epres;
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001225 epmd.nbits = bmlength;
Lev Walkinaaae9bf2010-10-24 16:56:30 -07001226 ASN_DEBUG("Read in extensions bitmap for %s of %ld bits (%x..)",
1227 td->name, (long)bmlength, *epres);
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001228
1229 /* Go over extensions and read them in */
1230 for(edx = specs->ext_after + 1; edx < td->elements_count; edx++) {
1231 asn_TYPE_member_t *elm = &td->elements[edx];
1232 void *memb_ptr; /* Pointer to the member */
1233 void **memb_ptr2; /* Pointer to that pointer */
1234 int present;
1235
1236 if(!IN_EXTENSION_GROUP(specs, edx)) {
Lev Walkin24810022017-08-25 12:16:11 -07001237 ASN_DEBUG("%zu is not an extension", edx);
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001238 continue;
1239 }
1240
1241 /* Fetch the pointer to this member */
1242 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001243 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001244 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001245 memb_ptr = (void *)((char *)st + elm->memb_offset);
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001246 memb_ptr2 = &memb_ptr;
1247 }
1248
1249 present = per_get_few_bits(&epmd, 1);
1250 if(present <= 0) {
1251 if(present < 0) break; /* No more extensions */
1252 continue;
1253 }
1254
1255 ASN_DEBUG("Decoding member %s in %s %p", elm->name, td->name, *memb_ptr2);
Lev Walkin9218bc12007-06-27 04:09:37 +00001256 rv = uper_open_type_get(opt_codec_ctx, elm->type,
Lev Walkina5972be2017-09-29 23:15:58 -07001257 elm->encoding_constraints.per_constraints, memb_ptr2, pd);
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001258 if(rv.code != RC_OK) {
1259 FREEMEM(epres);
1260 return rv;
1261 }
1262 }
1263
1264 /* Skip over overflow extensions which aren't present
1265 * in this system's version of the protocol */
Lev Walkin06230f72007-06-26 09:52:44 +00001266 for(;;) {
Lev Walkina2987ea2007-06-26 23:56:54 +00001267 ASN_DEBUG("Getting overflow extensions");
Lev Walkin06230f72007-06-26 09:52:44 +00001268 switch(per_get_few_bits(&epmd, 1)) {
1269 case -1: break;
1270 case 0: continue;
1271 default:
Lev Walkin9218bc12007-06-27 04:09:37 +00001272 if(uper_open_type_skip(opt_codec_ctx, pd)) {
Lev Walkin06230f72007-06-26 09:52:44 +00001273 FREEMEM(epres);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001274 ASN__DECODE_STARVED;
Lev Walkin06230f72007-06-26 09:52:44 +00001275 }
Lev Walkin59b176e2005-11-26 11:25:14 +00001276 }
Lev Walkin06230f72007-06-26 09:52:44 +00001277 break;
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001278 }
Lev Walkin59b176e2005-11-26 11:25:14 +00001279
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001280 FREEMEM(epres);
1281 }
1282
1283 /* Fill DEFAULT members in extensions */
1284 for(edx = specs->roms_count; edx < specs->roms_count
1285 + specs->aoms_count; edx++) {
1286 asn_TYPE_member_t *elm = &td->elements[edx];
1287 void **memb_ptr2; /* Pointer to member pointer */
1288
Lev Walkin20696a42017-10-17 21:27:33 -07001289 if(!elm->default_value_set) continue;
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001290
1291 /* Fetch the pointer to this member */
1292 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001293 memb_ptr2 = (void **)((char *)st
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001294 + elm->memb_offset);
1295 if(*memb_ptr2) continue;
1296 } else {
1297 continue; /* Extensions are all optionals */
1298 }
1299
1300 /* Set default value */
Lev Walkin20696a42017-10-17 21:27:33 -07001301 if(elm->default_value_set(memb_ptr2)) {
1302 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +00001303 }
1304 }
1305
1306 rv.consumed = 0;
1307 rv.code = RC_OK;
1308 return rv;
1309}
1310
Lev Walkin62258e22007-06-23 23:50:25 +00001311static int
Lev Walkin20696a42017-10-17 21:27:33 -07001312SEQUENCE__handle_extensions(const asn_TYPE_descriptor_t *td, const void *sptr,
1313 asn_per_outp_t *po1, asn_per_outp_t *po2) {
1314 const asn_SEQUENCE_specifics_t *specs
Lev Walkind84f6032017-10-03 16:33:59 -07001315 = (const asn_SEQUENCE_specifics_t *)td->specifics;
Lev Walkinf55a6dd2007-06-24 00:35:51 +00001316 int exts_present = 0;
1317 int exts_count = 0;
Lev Walkin494fb702017-08-07 20:07:00 -07001318 size_t edx;
Lev Walkin62258e22007-06-23 23:50:25 +00001319
1320 if(specs->ext_before < 0)
1321 return 0;
1322
1323 /* Find out which extensions are present */
1324 for(edx = specs->ext_after + 1; edx < td->elements_count; edx++) {
1325 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkin20696a42017-10-17 21:27:33 -07001326 const void *memb_ptr; /* Pointer to the member */
1327 const void *const *memb_ptr2; /* Pointer to that pointer */
1328 int present;
Lev Walkin62258e22007-06-23 23:50:25 +00001329
Lev Walkinf55a6dd2007-06-24 00:35:51 +00001330 if(!IN_EXTENSION_GROUP(specs, edx)) {
Lev Walkin24810022017-08-25 12:16:11 -07001331 ASN_DEBUG("%s (@%zu) is not extension", elm->type->name, edx);
Lev Walkin62258e22007-06-23 23:50:25 +00001332 continue;
Lev Walkinf55a6dd2007-06-24 00:35:51 +00001333 }
Lev Walkin62258e22007-06-23 23:50:25 +00001334
1335 /* Fetch the pointer to this member */
1336 if(elm->flags & ATF_POINTER) {
Lev Walkin20696a42017-10-17 21:27:33 -07001337 memb_ptr2 =
1338 (const void *const *)((const char *)sptr + elm->memb_offset);
1339 present = (*memb_ptr2 != 0);
Lev Walkin62258e22007-06-23 23:50:25 +00001340 } else {
Lev Walkin20696a42017-10-17 21:27:33 -07001341 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
1342 memb_ptr2 = &memb_ptr;
Lev Walkin62258e22007-06-23 23:50:25 +00001343 present = 1;
1344 }
1345
Lev Walkin24810022017-08-25 12:16:11 -07001346 ASN_DEBUG("checking %s (@%zu) present => %d",
Lev Walkin06230f72007-06-26 09:52:44 +00001347 elm->type->name, edx, present);
Lev Walkinf55a6dd2007-06-24 00:35:51 +00001348 exts_count++;
1349 exts_present += present;
Lev Walkin62258e22007-06-23 23:50:25 +00001350
1351 /* Encode as presence marker */
1352 if(po1 && per_put_few_bits(po1, present, 1))
1353 return -1;
1354 /* Encode as open type field */
Lev Walkin9218bc12007-06-27 04:09:37 +00001355 if(po2 && present && uper_open_type_put(elm->type,
Lev Walkina5972be2017-09-29 23:15:58 -07001356 elm->encoding_constraints.per_constraints, *memb_ptr2, po2))
Lev Walkin62258e22007-06-23 23:50:25 +00001357 return -1;
1358
1359 }
1360
Lev Walkinf55a6dd2007-06-24 00:35:51 +00001361 return exts_present ? exts_count : 0;
Lev Walkin62258e22007-06-23 23:50:25 +00001362}
1363
Lev Walkin523de9e2006-08-18 01:34:18 +00001364asn_enc_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -07001365SEQUENCE_encode_uper(const asn_TYPE_descriptor_t *td,
1366 const asn_per_constraints_t *constraints, const void *sptr,
Lev Walkin494fb702017-08-07 20:07:00 -07001367 asn_per_outp_t *po) {
Lev Walkin20696a42017-10-17 21:27:33 -07001368 const asn_SEQUENCE_specifics_t *specs
Lev Walkind84f6032017-10-03 16:33:59 -07001369 = (const asn_SEQUENCE_specifics_t *)td->specifics;
Lev Walkin523de9e2006-08-18 01:34:18 +00001370 asn_enc_rval_t er;
Lev Walkin62258e22007-06-23 23:50:25 +00001371 int n_extensions;
Lev Walkin494fb702017-08-07 20:07:00 -07001372 size_t edx;
1373 size_t i;
Lev Walkin523de9e2006-08-18 01:34:18 +00001374
1375 (void)constraints;
1376
1377 if(!sptr)
Lev Walkin7c1dc052016-03-14 03:08:15 -07001378 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001379
1380 er.encoded = 0;
1381
1382 ASN_DEBUG("Encoding %s as SEQUENCE (UPER)", td->name);
Lev Walkin62258e22007-06-23 23:50:25 +00001383
1384
1385 /*
1386 * X.691#18.1 Whether structure is extensible
1387 * and whether to encode extensions
1388 */
1389 if(specs->ext_before >= 0) {
Lev Walkin20696a42017-10-17 21:27:33 -07001390 n_extensions = SEQUENCE__handle_extensions(td, sptr, 0, 0);
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001391 if(n_extensions < 0)
Lev Walkin494fb702017-08-07 20:07:00 -07001392 ASN__ENCODE_FAILED;
1393 if(per_put_few_bits(po, n_extensions ? 1 : 0, 1))
1394 ASN__ENCODE_FAILED;
Lev Walkin62258e22007-06-23 23:50:25 +00001395 } else {
1396 n_extensions = 0; /* There are no extensions to encode */
1397 }
Lev Walkin523de9e2006-08-18 01:34:18 +00001398
1399 /* Encode a presence bitmap */
1400 for(i = 0; i < specs->roms_count; i++) {
Lev Walkinc46b7cb2006-08-18 02:27:55 +00001401 asn_TYPE_member_t *elm;
Lev Walkin20696a42017-10-17 21:27:33 -07001402 const void *memb_ptr; /* Pointer to the member */
1403 const void *const *memb_ptr2; /* Pointer to that pointer */
1404 int present;
Lev Walkin523de9e2006-08-18 01:34:18 +00001405
Lev Walkinc46b7cb2006-08-18 02:27:55 +00001406 edx = specs->oms[i];
1407 elm = &td->elements[edx];
1408
Lev Walkin523de9e2006-08-18 01:34:18 +00001409 /* Fetch the pointer to this member */
1410 if(elm->flags & ATF_POINTER) {
Lev Walkin20696a42017-10-17 21:27:33 -07001411 memb_ptr2 =
1412 (const void *const *)((const char *)sptr + elm->memb_offset);
1413 present = (*memb_ptr2 != 0);
Lev Walkin523de9e2006-08-18 01:34:18 +00001414 } else {
Lev Walkin20696a42017-10-17 21:27:33 -07001415 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
1416 memb_ptr2 = &memb_ptr;
Lev Walkin523de9e2006-08-18 01:34:18 +00001417 present = 1;
1418 }
1419
1420 /* Eliminate default values */
Lev Walkin20696a42017-10-17 21:27:33 -07001421 if(present && elm->default_value_cmp
1422 && elm->default_value_cmp(*memb_ptr2) == 0)
1423 present = 0;
Lev Walkin523de9e2006-08-18 01:34:18 +00001424
1425 ASN_DEBUG("Element %s %s %s->%s is %s",
1426 elm->flags & ATF_POINTER ? "ptr" : "inline",
Lev Walkin20696a42017-10-17 21:27:33 -07001427 elm->default_value_cmp ? "def" : "wtv",
Lev Walkin523de9e2006-08-18 01:34:18 +00001428 td->name, elm->name, present ? "present" : "absent");
1429 if(per_put_few_bits(po, present, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001430 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001431 }
1432
1433 /*
Lev Walkin62258e22007-06-23 23:50:25 +00001434 * Encode the sequence ROOT elements.
Lev Walkin523de9e2006-08-18 01:34:18 +00001435 */
Lev Walkin62258e22007-06-23 23:50:25 +00001436 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 +00001437 for(edx = 0; edx < ((specs->ext_after < 0)
1438 ? td->elements_count : specs->ext_before - 1); edx++) {
1439
Lev Walkin523de9e2006-08-18 01:34:18 +00001440 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkin20696a42017-10-17 21:27:33 -07001441 const void *memb_ptr; /* Pointer to the member */
1442 const void *const *memb_ptr2; /* Pointer to that pointer */
Lev Walkin523de9e2006-08-18 01:34:18 +00001443
Lev Walkin20696a42017-10-17 21:27:33 -07001444 if(IN_EXTENSION_GROUP(specs, edx))
Lev Walkin4d22b622007-07-23 09:58:25 +00001445 continue;
1446
Lev Walkina2987ea2007-06-26 23:56:54 +00001447 ASN_DEBUG("About to encode %s", elm->type->name);
1448
Lev Walkin523de9e2006-08-18 01:34:18 +00001449 /* Fetch the pointer to this member */
1450 if(elm->flags & ATF_POINTER) {
Lev Walkin20696a42017-10-17 21:27:33 -07001451 memb_ptr2 =
1452 (const void *const *)((const char *)sptr + elm->memb_offset);
1453 if(!*memb_ptr2) {
Lev Walkin24810022017-08-25 12:16:11 -07001454 ASN_DEBUG("Element %s %zu not present",
Lev Walkin523de9e2006-08-18 01:34:18 +00001455 elm->name, edx);
1456 if(elm->optional)
1457 continue;
1458 /* Mandatory element is missing */
Lev Walkin7c1dc052016-03-14 03:08:15 -07001459 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001460 }
1461 } else {
Lev Walkin20696a42017-10-17 21:27:33 -07001462 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
1463 memb_ptr2 = &memb_ptr;
Lev Walkin523de9e2006-08-18 01:34:18 +00001464 }
1465
1466 /* Eliminate default values */
Lev Walkin20696a42017-10-17 21:27:33 -07001467 if(elm->default_value_cmp && elm->default_value_cmp(*memb_ptr2) == 0)
Lev Walkin523de9e2006-08-18 01:34:18 +00001468 continue;
1469
Lev Walkin4d22b622007-07-23 09:58:25 +00001470 ASN_DEBUG("Encoding %s->%s", td->name, elm->name);
Lev Walkina5972be2017-09-29 23:15:58 -07001471 er = elm->type->op->uper_encoder(elm->type, elm->encoding_constraints.per_constraints,
Lev Walkin523de9e2006-08-18 01:34:18 +00001472 *memb_ptr2, po);
1473 if(er.encoded == -1)
1474 return er;
1475 }
1476
Lev Walkin62258e22007-06-23 23:50:25 +00001477 /* No extensions to encode */
Lev Walkin7c1dc052016-03-14 03:08:15 -07001478 if(!n_extensions) ASN__ENCODED_OK(er);
Lev Walkin62258e22007-06-23 23:50:25 +00001479
1480 ASN_DEBUG("Length of %d bit-map", n_extensions);
1481 /* #18.8. Write down the presence bit-map length. */
1482 if(uper_put_nslength(po, n_extensions))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001483 ASN__ENCODE_FAILED;
Lev Walkin62258e22007-06-23 23:50:25 +00001484
1485 ASN_DEBUG("Bit-map of %d elements", n_extensions);
1486 /* #18.7. Encoding the extensions presence bit-map. */
1487 /* TODO: act upon NOTE in #18.7 for canonical PER */
Lev Walkin20696a42017-10-17 21:27:33 -07001488 if(SEQUENCE__handle_extensions(td, sptr, po, 0) != n_extensions)
Lev Walkin7c1dc052016-03-14 03:08:15 -07001489 ASN__ENCODE_FAILED;
Lev Walkin62258e22007-06-23 23:50:25 +00001490
1491 ASN_DEBUG("Writing %d extensions", n_extensions);
1492 /* #18.9. Encode extensions as open type fields. */
Lev Walkin20696a42017-10-17 21:27:33 -07001493 if(SEQUENCE__handle_extensions(td, sptr, 0, po) != n_extensions)
Lev Walkin7c1dc052016-03-14 03:08:15 -07001494 ASN__ENCODE_FAILED;
Lev Walkin62258e22007-06-23 23:50:25 +00001495
Lev Walkin7c1dc052016-03-14 03:08:15 -07001496 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +00001497}
1498
Lev Walkin24810022017-08-25 12:16:11 -07001499#endif /* ASN_DISABLE_PER_SUPPORT */
1500
Lev Walkincd2f48e2017-08-10 02:14:59 -07001501int
1502SEQUENCE_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
1503 const void *bptr) {
1504 size_t edx;
1505
1506 for(edx = 0; edx < td->elements_count; edx++) {
1507 asn_TYPE_member_t *elm = &td->elements[edx];
1508 const void *amemb;
1509 const void *bmemb;
1510 int ret;
1511
1512 if(elm->flags & ATF_POINTER) {
1513 amemb =
1514 *(const void *const *)((const char *)aptr + elm->memb_offset);
1515 bmemb =
1516 *(const void *const *)((const char *)bptr + elm->memb_offset);
1517 if(!amemb) {
1518 if(!bmemb) continue;
1519 return -1;
1520 } else if(!bmemb) {
1521 return 1;
1522 }
1523 } else {
1524 amemb = (const void *)((const char *)aptr + elm->memb_offset);
1525 bmemb = (const void *)((const char *)bptr + elm->memb_offset);
1526 }
1527
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001528 ret = elm->type->op->compare_struct(elm->type, amemb, bmemb);
Lev Walkincd2f48e2017-08-10 02:14:59 -07001529 if(ret != 0) return ret;
1530 }
1531
1532 return 0;
1533}
1534
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001535asn_TYPE_operation_t asn_OP_SEQUENCE = {
1536 SEQUENCE_free,
1537 SEQUENCE_print,
1538 SEQUENCE_compare,
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001539 SEQUENCE_decode_ber,
1540 SEQUENCE_encode_der,
1541 SEQUENCE_decode_xer,
1542 SEQUENCE_encode_xer,
1543#ifdef ASN_DISABLE_OER_SUPPORT
1544 0,
1545 0,
1546#else
1547 SEQUENCE_decode_oer,
1548 SEQUENCE_encode_oer,
1549#endif /* ASN_DISABLE_OER_SUPPORT */
1550#ifdef ASN_DISABLE_PER_SUPPORT
1551 0,
1552 0,
1553#else
1554 SEQUENCE_decode_uper,
1555 SEQUENCE_encode_uper,
1556#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkina5972be2017-09-29 23:15:58 -07001557 SEQUENCE_random_fill,
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001558 0 /* Use generic outmost tag fetcher */
1559};
1560
Lev Walkina5972be2017-09-29 23:15:58 -07001561
1562asn_random_fill_result_t
1563SEQUENCE_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
1564 const asn_encoding_constraints_t *constr,
1565 size_t max_length) {
1566 const asn_SEQUENCE_specifics_t *specs =
1567 (const asn_SEQUENCE_specifics_t *)td->specifics;
1568 asn_random_fill_result_t result_ok = {ARFILL_OK, 0};
1569 asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
1570 asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
1571 void *st = *sptr;
1572 size_t edx;
1573
1574 if(max_length == 0) return result_skipped;
1575
1576 (void)constr;
1577
1578 if(st == NULL) {
1579 st = CALLOC(1, specs->struct_size);
1580 if(st == NULL) {
1581 return result_failed;
1582 }
1583 }
1584
1585 for(edx = 0; edx < td->elements_count; edx++) {
1586 const asn_TYPE_member_t *elm = &td->elements[edx];
1587 void *memb_ptr; /* Pointer to the member */
1588 void **memb_ptr2; /* Pointer to that pointer */
1589 asn_random_fill_result_t tmpres;
1590
1591 if(elm->optional && asn_random_between(0, 4) == 2) {
1592 /* Sometimes decide not to fill the optional value */
1593 continue;
1594 }
1595
1596 if(elm->flags & ATF_POINTER) {
1597 /* Member is a pointer to another structure */
1598 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
1599 } else {
1600 memb_ptr = (char *)st + elm->memb_offset;
1601 memb_ptr2 = &memb_ptr;
1602 }
1603
1604 tmpres = elm->type->op->random_fill(
1605 elm->type, memb_ptr2, &elm->encoding_constraints,
1606 max_length > result_ok.length ? max_length - result_ok.length : 0);
1607 switch(tmpres.code) {
1608 case ARFILL_OK:
1609 result_ok.length += tmpres.length;
1610 continue;
1611 case ARFILL_SKIPPED:
1612 assert(!(elm->flags & ATF_POINTER) || *memb_ptr2 == NULL);
1613 continue;
1614 case ARFILL_FAILED:
1615 if(st == *sptr) {
1616 ASN_STRUCT_RESET(*td, st);
1617 } else {
1618 ASN_STRUCT_FREE(*td, st);
1619 }
1620 return tmpres;
1621 }
1622 }
1623
1624 *sptr = st;
1625
1626 return result_ok;
1627}
1628