blob: 31f0c42236b0a702561ab5aaab1f737f99a9074a [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
Lev Walkinc61f3862005-02-14 17:21:22 +00002 * Copyright (c) 2003, 2004, 2005 Lev Walkin <vlm@lionet.info>.
3 * 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 Walkin4c36e302004-06-06 07:20:02 +00008#include <assert.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00009
10/*
11 * Number of bytes left for this structure.
12 * (ctx->left) indicates the number of bytes _transferred_ for the structure.
13 * (size) contains the number of bytes in the buffer passed.
14 */
Lev Walkinec1ffd42004-08-18 04:53:32 +000015#define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left)
Lev Walkinf15320b2004-06-03 03:38:44 +000016
17/*
18 * If the subprocessor function returns with an indication that it wants
19 * more data, it may well be a fatal decoding problem, because the
20 * size is constrained by the <TLV>'s L, even if the buffer size allows
21 * reading more data.
22 * For example, consider the buffer containing the following TLVs:
23 * <T:5><L:1><V> <T:6>...
24 * The TLV length clearly indicates that one byte is expected in V, but
25 * if the V processor returns with "want more data" even if the buffer
26 * contains way more data than the V processor have seen.
27 */
Lev Walkind9bd7752004-06-05 08:17:50 +000028#define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size)
Lev Walkinf15320b2004-06-03 03:38:44 +000029
30/*
31 * This macro "eats" the part of the buffer which is definitely "consumed",
32 * i.e. was correctly converted into local representation or rightfully skipped.
33 */
Lev Walkincc6a9102004-09-23 22:06:26 +000034#undef ADVANCE
Lev Walkinf15320b2004-06-03 03:38:44 +000035#define ADVANCE(num_bytes) do { \
36 size_t num = num_bytes; \
Lev Walkin4ce78ca2004-08-25 01:34:11 +000037 ptr = ((char *)ptr) + num; \
Lev Walkinf15320b2004-06-03 03:38:44 +000038 size -= num; \
39 if(ctx->left >= 0) \
40 ctx->left -= num; \
41 consumed_myself += num; \
42 } while(0)
43
44/*
45 * Switch to the next phase of parsing.
46 */
Lev Walkincc6a9102004-09-23 22:06:26 +000047#undef NEXT_PHASE
48#undef PHASE_OUT
Lev Walkinf15320b2004-06-03 03:38:44 +000049#define NEXT_PHASE(ctx) do { \
50 ctx->phase++; \
51 ctx->step = 0; \
52 } while(0)
53#define PHASE_OUT(ctx) do { ctx->phase = 10; } while(0)
54
55/*
56 * Return a standardized complex structure.
57 */
Lev Walkincc6a9102004-09-23 22:06:26 +000058#undef RETURN
Lev Walkinf15320b2004-06-03 03:38:44 +000059#define RETURN(_code) do { \
60 rval.code = _code; \
61 rval.consumed = consumed_myself;\
62 return rval; \
63 } while(0)
64
65/*
66 * Check whether we are inside the extensions group.
67 */
68#define IN_EXTENSION_GROUP(specs, memb_idx) \
69 ( ((memb_idx) > (specs)->ext_after) \
70 &&((memb_idx) < (specs)->ext_before))
71
Lev Walkin4c36e302004-06-06 07:20:02 +000072
73/*
74 * Tags are canonically sorted in the tag2element map.
75 */
76static int
77_t2e_cmp(const void *ap, const void *bp) {
Lev Walkin5e033762004-09-29 13:26:15 +000078 const asn_TYPE_tag2member_t *a = (const asn_TYPE_tag2member_t *)ap;
79 const asn_TYPE_tag2member_t *b = (const asn_TYPE_tag2member_t *)bp;
Lev Walkinc2346572004-08-11 09:07:36 +000080
Lev Walkin4c36e302004-06-06 07:20:02 +000081 int a_class = BER_TAG_CLASS(a->el_tag);
82 int b_class = BER_TAG_CLASS(b->el_tag);
83
84 if(a_class == b_class) {
85 ber_tlv_tag_t a_value = BER_TAG_VALUE(a->el_tag);
86 ber_tlv_tag_t b_value = BER_TAG_VALUE(b->el_tag);
87
Lev Walkin924fa032004-06-14 13:42:23 +000088 if(a_value == b_value) {
Lev Walkin50299c12004-06-14 14:11:14 +000089 if(a->el_no > b->el_no)
90 return 1;
Lev Walkin924fa032004-06-14 13:42:23 +000091 /*
92 * Important: we do not check
Lev Walkin50299c12004-06-14 14:11:14 +000093 * for a->el_no <= b->el_no!
Lev Walkin924fa032004-06-14 13:42:23 +000094 */
Lev Walkin4c36e302004-06-06 07:20:02 +000095 return 0;
Lev Walkin924fa032004-06-14 13:42:23 +000096 } else if(a_value < b_value)
Lev Walkin4c36e302004-06-06 07:20:02 +000097 return -1;
98 else
99 return 1;
100 } else if(a_class < b_class) {
101 return -1;
102 } else {
103 return 1;
104 }
105}
106
107
Lev Walkinf15320b2004-06-03 03:38:44 +0000108/*
109 * The decoder of the SEQUENCE type.
110 */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000111asn_dec_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000112SEQUENCE_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkinf15320b2004-06-03 03:38:44 +0000113 void **struct_ptr, void *ptr, size_t size, int tag_mode) {
114 /*
115 * Bring closer parts of structure description.
116 */
Lev Walkin5e033762004-09-29 13:26:15 +0000117 asn_SEQUENCE_specifics_t *specs = (asn_SEQUENCE_specifics_t *)td->specifics;
118 asn_TYPE_member_t *elements = td->elements;
Lev Walkinf15320b2004-06-03 03:38:44 +0000119
120 /*
121 * Parts of the structure being constructed.
122 */
123 void *st = *struct_ptr; /* Target structure. */
Lev Walkin5e033762004-09-29 13:26:15 +0000124 asn_struct_ctx_t *ctx; /* Decoder context */
Lev Walkinf15320b2004-06-03 03:38:44 +0000125
126 ber_tlv_tag_t tlv_tag; /* T from TLV */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000127 asn_dec_rval_t rval; /* Return code from subparsers */
Lev Walkinf15320b2004-06-03 03:38:44 +0000128
129 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
130 int edx; /* SEQUENCE element's index */
131
Lev Walkin449f8322004-08-20 13:23:42 +0000132 ASN_DEBUG("Decoding %s as SEQUENCE", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000133
134 /*
135 * Create the target structure if it is not present already.
136 */
137 if(st == 0) {
138 st = *struct_ptr = CALLOC(1, specs->struct_size);
139 if(st == 0) {
140 RETURN(RC_FAIL);
141 }
142 }
143
144 /*
145 * Restore parsing context.
146 */
Lev Walkin5e033762004-09-29 13:26:15 +0000147 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000148
149 /*
150 * Start to parse where left previously
151 */
152 switch(ctx->phase) {
153 case 0:
154 /*
155 * PHASE 0.
156 * Check that the set of tags associated with given structure
157 * perfectly fits our expectations.
158 */
159
Lev Walkin5e033762004-09-29 13:26:15 +0000160 rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size,
Lev Walkin8e8078a2004-09-26 13:10:40 +0000161 tag_mode, 1, &ctx->left, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000162 if(rval.code != RC_OK) {
163 ASN_DEBUG("%s tagging check failed: %d",
Lev Walkin449f8322004-08-20 13:23:42 +0000164 td->name, rval.code);
Lev Walkin566a5672004-10-05 06:36:57 +0000165 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000166 }
167
168 if(ctx->left >= 0)
169 ctx->left += rval.consumed; /* ?Substracted below! */
170 ADVANCE(rval.consumed);
171
172 NEXT_PHASE(ctx);
173
174 ASN_DEBUG("Structure consumes %ld bytes, buffer %ld",
175 (long)ctx->left, (long)size);
176
177 /* Fall through */
178 case 1:
179 /*
180 * PHASE 1.
181 * From the place where we've left it previously,
182 * try to decode the next member from the list of
183 * this structure's elements.
184 * (ctx->step) stores the member being processed
185 * between invocations and the microphase {0,1} of parsing
186 * that member:
187 * step = (<member_number> * 2 + <microphase>).
188 */
Lev Walkin449f8322004-08-20 13:23:42 +0000189 for(edx = (ctx->step >> 1); edx < td->elements_count;
Lev Walkinf15320b2004-06-03 03:38:44 +0000190 edx++, ctx->step = (ctx->step & ~1) + 2) {
191 void *memb_ptr; /* Pointer to the member */
Lev Walkinc2346572004-08-11 09:07:36 +0000192 void **memb_ptr2; /* Pointer to that pointer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000193 ssize_t tag_len; /* Length of TLV's T */
194 int opt_edx_end; /* Next non-optional element */
Lev Walkin4c36e302004-06-06 07:20:02 +0000195 int use_bsearch;
Lev Walkinf15320b2004-06-03 03:38:44 +0000196 int n;
197
198 if(ctx->step & 1)
199 goto microphase2;
200
201 /*
202 * MICROPHASE 1: Synchronize decoding.
203 */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000204 ASN_DEBUG("In %s SEQUENCE left %d, edx=%d flags=%d"
205 " opt=%d ec=%d",
206 td->name, (int)ctx->left, edx,
207 elements[edx].flags, elements[edx].optional,
208 td->elements_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000209
210 if(ctx->left == 0 /* No more stuff is expected */
211 && (
212 /* Explicit OPTIONAL specification reaches the end */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000213 (edx + elements[edx].optional
214 == td->elements_count)
Lev Walkinf15320b2004-06-03 03:38:44 +0000215 ||
216 /* All extensions are optional */
217 (IN_EXTENSION_GROUP(specs, edx)
Lev Walkin449f8322004-08-20 13:23:42 +0000218 && specs->ext_before > td->elements_count)
Lev Walkinf15320b2004-06-03 03:38:44 +0000219 )
220 ) {
Lev Walkin449f8322004-08-20 13:23:42 +0000221 ASN_DEBUG("End of SEQUENCE %s", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000222 /*
223 * Found the legitimate end of the structure.
224 */
225 PHASE_OUT(ctx);
226 RETURN(RC_OK);
227 }
228
229 /*
230 * Fetch the T from TLV.
231 */
232 tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
Lev Walkin906654e2004-09-10 15:49:15 +0000233 ASN_DEBUG("Current tag in %s SEQUENCE for element %d "
Lev Walkincc6a9102004-09-23 22:06:26 +0000234 "(%s) is %s encoded in %d bytes, of frame %ld",
Lev Walkin906654e2004-09-10 15:49:15 +0000235 td->name, edx, elements[edx].name,
236 ber_tlv_tag_string(tlv_tag), (int)tag_len, (long)LEFT);
Lev Walkinf15320b2004-06-03 03:38:44 +0000237 switch(tag_len) {
238 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
239 /* Fall through */
240 case -1: RETURN(RC_FAIL);
241 }
242
Lev Walkin566a5672004-10-05 06:36:57 +0000243 if(ctx->left < 0 && ((uint8_t *)ptr)[0] == 0) {
244 if(LEFT < 2) {
245 if(SIZE_VIOLATION)
246 RETURN(RC_FAIL);
247 else
248 RETURN(RC_WMORE);
249 } else if(((uint8_t *)ptr)[1] == 0) {
250 ASN_DEBUG("edx = %d, opt = %d, ec=%d",
251 edx, elements[edx].optional,
252 td->elements_count);
253 if((edx + elements[edx].optional
254 == td->elements_count)
255 || (IN_EXTENSION_GROUP(specs, edx)
256 && specs->ext_before
257 > td->elements_count)) {
258 /*
259 * Yeah, baby! Found the terminator
260 * of the indefinite length structure.
261 */
262 /*
263 * Proceed to the canonical
264 * finalization function.
265 * No advancing is necessary.
266 */
267 goto phase3;
268 }
269 }
270 }
271
Lev Walkinf15320b2004-06-03 03:38:44 +0000272 /*
273 * Find the next available type with this tag.
274 */
Lev Walkin4c36e302004-06-06 07:20:02 +0000275 use_bsearch = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000276 opt_edx_end = edx + elements[edx].optional + 1;
Lev Walkin449f8322004-08-20 13:23:42 +0000277 if(opt_edx_end > td->elements_count)
278 opt_edx_end = td->elements_count; /* Cap */
Lev Walkinf9dbd9a2004-07-01 00:48:04 +0000279 else if(opt_edx_end - edx > 8) {
Lev Walkin924fa032004-06-14 13:42:23 +0000280 /* Limit the scope of linear search... */
Lev Walkinf9dbd9a2004-07-01 00:48:04 +0000281 opt_edx_end = edx + 8;
Lev Walkin4c36e302004-06-06 07:20:02 +0000282 use_bsearch = 1;
Lev Walkin924fa032004-06-14 13:42:23 +0000283 /* ... and resort to bsearch() */
Lev Walkin4c36e302004-06-06 07:20:02 +0000284 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000285 for(n = edx; n < opt_edx_end; n++) {
286 if(BER_TAGS_EQUAL(tlv_tag, elements[n].tag)) {
287 /*
288 * Found element corresponding to the tag
289 * being looked at.
290 * Reposition over the right element.
291 */
292 edx = n;
Lev Walkin4c36e302004-06-06 07:20:02 +0000293 ctx->step = 1 + 2 * edx; /* Remember! */
294 goto microphase2;
Lev Walkinb9189732004-09-10 09:37:12 +0000295 } else if(elements[n].flags & ATF_OPEN_TYPE) {
296 /*
297 * This is the ANY type, which may bear
298 * any flag whatsoever.
299 */
300 edx = n;
301 ctx->step = 1 + 2 * edx; /* Remember! */
302 goto microphase2;
Lev Walkin4c36e302004-06-06 07:20:02 +0000303 } else if(elements[n].tag == (ber_tlv_tag_t)-1) {
304 use_bsearch = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000305 break;
306 }
307 }
Lev Walkin4c36e302004-06-06 07:20:02 +0000308 if(use_bsearch) {
309 /*
Lev Walkin4e0704f2004-09-04 06:17:35 +0000310 * Resort to a binary search over
Lev Walkin4c36e302004-06-06 07:20:02 +0000311 * sorted array of tags.
312 */
Lev Walkin5e033762004-09-29 13:26:15 +0000313 asn_TYPE_tag2member_t *t2m;
314 asn_TYPE_tag2member_t key;
Lev Walkin4c36e302004-06-06 07:20:02 +0000315 key.el_tag = tlv_tag;
Lev Walkin924fa032004-06-14 13:42:23 +0000316 key.el_no = edx;
Lev Walkinc17d90f2005-01-17 14:32:45 +0000317 t2m = (asn_TYPE_tag2member_t *)bsearch(&key,
Lev Walkinc2346572004-08-11 09:07:36 +0000318 specs->tag2el, specs->tag2el_count,
Lev Walkin4c36e302004-06-06 07:20:02 +0000319 sizeof(specs->tag2el[0]), _t2e_cmp);
Lev Walkin924fa032004-06-14 13:42:23 +0000320 if(t2m) {
Lev Walkin5e033762004-09-29 13:26:15 +0000321 asn_TYPE_tag2member_t *best = 0;
322 asn_TYPE_tag2member_t *t2m_f, *t2m_l;
Lev Walkin924fa032004-06-14 13:42:23 +0000323 int edx_max = edx + elements[edx].optional;
Lev Walkin4c36e302004-06-06 07:20:02 +0000324 /*
325 * Rewind to the first element with that tag,
326 * `cause bsearch() does not guarantee order.
327 */
Lev Walkin924fa032004-06-14 13:42:23 +0000328 t2m_f = t2m + t2m->toff_first;
329 t2m_l = t2m + t2m->toff_last;
330 for(t2m = t2m_f; t2m <= t2m_l; t2m++) {
331 if(t2m->el_no > edx_max) break;
332 if(t2m->el_no < edx) continue;
333 best = t2m;
334 }
335 if(best) {
336 edx = best->el_no;
337 ctx->step = 1 + 2 * edx;
338 goto microphase2;
339 }
Lev Walkin4c36e302004-06-06 07:20:02 +0000340 }
341 n = opt_edx_end;
342 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000343 if(n == opt_edx_end) {
344 /*
345 * If tag is unknown, it may be either
346 * an unknown (thus, incorrect) tag,
347 * or an extension (...),
348 * or an end of the indefinite-length structure.
349 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000350 if(!IN_EXTENSION_GROUP(specs, edx)) {
Lev Walkin566a5672004-10-05 06:36:57 +0000351 ASN_DEBUG("Unexpected tag %s (at %d)",
352 ber_tlv_tag_string(tlv_tag), edx);
Lev Walkin4c36e302004-06-06 07:20:02 +0000353 ASN_DEBUG("Expected tag %s (%s)%s",
Lev Walkinf15320b2004-06-03 03:38:44 +0000354 ber_tlv_tag_string(elements[edx].tag),
Lev Walkin4c36e302004-06-06 07:20:02 +0000355 elements[edx].name,
Lev Walkinf15320b2004-06-03 03:38:44 +0000356 elements[edx].optional
357 ?" or alternatives":"");
358 RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000359 } else {
360 /* Skip this tag */
361 ssize_t skip;
362
Lev Walkinbaaa24f2004-09-29 14:19:14 +0000363 skip = ber_skip_length(opt_codec_ctx,
Lev Walkinf15320b2004-06-03 03:38:44 +0000364 BER_TLV_CONSTRUCTED(ptr),
Lev Walkin4d9528c2004-08-11 08:10:13 +0000365 (char *)ptr + tag_len, LEFT - tag_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000366 ASN_DEBUG("Skip length %d in %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000367 (int)skip, td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000368 switch(skip) {
369 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
370 /* Fall through */
371 case -1: RETURN(RC_FAIL);
372 }
373
374 ADVANCE(skip + tag_len);
375 ctx->step -= 2;
376 edx--;
377 continue; /* Try again with the next tag */
378 }
379 }
380
381 /*
382 * MICROPHASE 2: Invoke the member-specific decoder.
383 */
384 ctx->step |= 1; /* Confirm entering next microphase */
385 microphase2:
Lev Walkin449f8322004-08-20 13:23:42 +0000386 ASN_DEBUG("Inside SEQUENCE %s MF2", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000387
388 /*
389 * Compute the position of the member inside a structure,
390 * and also a type of containment (it may be contained
391 * as pointer or using inline inclusion).
392 */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000393 if(elements[edx].flags & ATF_POINTER) {
394 /* Member is a pointer to another structure */
Lev Walkinc2346572004-08-11 09:07:36 +0000395 memb_ptr2 = (void **)((char *)st + elements[edx].memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000396 } else {
397 /*
398 * A pointer to a pointer
399 * holding the start of the structure
400 */
401 memb_ptr = (char *)st + elements[edx].memb_offset;
402 memb_ptr2 = &memb_ptr;
403 }
404 /*
405 * Invoke the member fetch routine according to member's type
406 */
Lev Walkin5e033762004-09-29 13:26:15 +0000407 rval = elements[edx].type->ber_decoder(opt_codec_ctx,
Lev Walkinc2346572004-08-11 09:07:36 +0000408 elements[edx].type,
Lev Walkinf15320b2004-06-03 03:38:44 +0000409 memb_ptr2, ptr, LEFT,
410 elements[edx].tag_mode);
Lev Walkin60b7cff2004-09-04 04:44:11 +0000411 ASN_DEBUG("In %s SEQUENCE decoded %d %s of %d "
Lev Walkin5beb7de2004-09-24 20:59:27 +0000412 "in %d bytes rval.code %d, size=%d",
Lev Walkin449f8322004-08-20 13:23:42 +0000413 td->name, edx, elements[edx].type->name,
Lev Walkin5beb7de2004-09-24 20:59:27 +0000414 (int)LEFT, (int)rval.consumed, rval.code, (int)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000415 switch(rval.code) {
416 case RC_OK:
417 break;
418 case RC_WMORE: /* More data expected */
419 if(!SIZE_VIOLATION) {
420 ADVANCE(rval.consumed);
421 RETURN(RC_WMORE);
422 }
Lev Walkin5beb7de2004-09-24 20:59:27 +0000423 ASN_DEBUG("Size violation (c->l=%ld <= s=%ld)",
424 (long)ctx->left, (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000425 /* Fall through */
426 case RC_FAIL: /* Fatal error */
427 RETURN(RC_FAIL);
428 } /* switch(rval) */
429
430 ADVANCE(rval.consumed);
431 } /* for(all structure members) */
432
433 phase3:
434 ctx->phase = 3;
435 case 3: /* 00 and other tags expected */
436 case 4: /* only 00's expected */
437
438 ASN_DEBUG("SEQUENCE %s Leftover: %ld, size = %ld",
Lev Walkin449f8322004-08-20 13:23:42 +0000439 td->name, (long)ctx->left, (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000440
441 /*
442 * Skip everything until the end of the SEQUENCE.
443 */
444 while(ctx->left) {
445 ssize_t tl, ll;
446
447 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
448 switch(tl) {
449 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
450 /* Fall through */
451 case -1: RETURN(RC_FAIL);
452 }
453
454 /*
455 * If expected <0><0>...
456 */
457 if(ctx->left < 0
458 && ((uint8_t *)ptr)[0] == 0) {
459 if(LEFT < 2) {
460 if(SIZE_VIOLATION)
461 RETURN(RC_FAIL);
462 else
463 RETURN(RC_WMORE);
464 } else if(((uint8_t *)ptr)[1] == 0) {
465 /*
466 * Correctly finished with <0><0>.
467 */
468 ADVANCE(2);
469 ctx->left++;
470 ctx->phase = 4;
471 continue;
472 }
473 }
474
Lev Walkin449f8322004-08-20 13:23:42 +0000475 if(!IN_EXTENSION_GROUP(specs, td->elements_count)
Lev Walkinf15320b2004-06-03 03:38:44 +0000476 || ctx->phase == 4) {
477 ASN_DEBUG("Unexpected continuation "
478 "of a non-extensible type "
479 "%s (SEQUENCE): %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000480 td->name,
Lev Walkinf15320b2004-06-03 03:38:44 +0000481 ber_tlv_tag_string(tlv_tag));
482 RETURN(RC_FAIL);
483 }
484
Lev Walkinbaaa24f2004-09-29 14:19:14 +0000485 ll = ber_skip_length(opt_codec_ctx,
Lev Walkinf15320b2004-06-03 03:38:44 +0000486 BER_TLV_CONSTRUCTED(ptr),
Lev Walkin4d9528c2004-08-11 08:10:13 +0000487 (char *)ptr + tl, LEFT - tl);
Lev Walkinf15320b2004-06-03 03:38:44 +0000488 switch(ll) {
489 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
490 /* Fall through */
491 case -1: RETURN(RC_FAIL);
492 }
493
494 ADVANCE(tl + ll);
495 }
496
497 PHASE_OUT(ctx);
498 }
499
500 RETURN(RC_OK);
501}
502
Lev Walkin4c36e302004-06-06 07:20:02 +0000503
Lev Walkinf15320b2004-06-03 03:38:44 +0000504/*
505 * The DER encoder of the SEQUENCE type.
506 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000507asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000508SEQUENCE_encode_der(asn_TYPE_descriptor_t *td,
Lev Walkinf15320b2004-06-03 03:38:44 +0000509 void *ptr, int tag_mode, ber_tlv_tag_t tag,
510 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000511 size_t computed_size = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000512 asn_enc_rval_t erval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000513 ssize_t ret;
514 int edx;
515
516 ASN_DEBUG("%s %s as SEQUENCE",
Lev Walkin449f8322004-08-20 13:23:42 +0000517 cb?"Encoding":"Estimating", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000518
519 /*
520 * Gather the length of the underlying members sequence.
521 */
Lev Walkin449f8322004-08-20 13:23:42 +0000522 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000523 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000524 void *memb_ptr;
Lev Walkincc93b0f2004-09-10 09:18:20 +0000525 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000526 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
527 if(!memb_ptr) continue;
528 } else {
529 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
530 }
531 erval = elm->type->der_encoder(elm->type, memb_ptr,
532 elm->tag_mode, elm->tag,
533 0, 0);
534 if(erval.encoded == -1)
535 return erval;
536 computed_size += erval.encoded;
537 ASN_DEBUG("Member %d %s estimated %ld bytes",
538 edx, elm->name, (long)erval.encoded);
539 }
540
541 /*
542 * Encode the TLV for the sequence itself.
543 */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000544 ret = der_write_tags(td, computed_size, tag_mode, 1, tag, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +0000545 ASN_DEBUG("Wrote tags: %ld (+%ld)", (long)ret, (long)computed_size);
546 if(ret == -1) {
547 erval.encoded = -1;
Lev Walkin449f8322004-08-20 13:23:42 +0000548 erval.failed_type = td;
Lev Walkinf15320b2004-06-03 03:38:44 +0000549 erval.structure_ptr = ptr;
550 return erval;
551 }
552 erval.encoded = computed_size + ret;
553
554 if(!cb) return erval;
555
556 /*
557 * Encode all members.
558 */
Lev Walkin449f8322004-08-20 13:23:42 +0000559 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000560 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkina9cc46e2004-09-22 16:06:28 +0000561 asn_enc_rval_t tmperval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000562 void *memb_ptr;
563
Lev Walkincc93b0f2004-09-10 09:18:20 +0000564 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000565 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
566 if(!memb_ptr) continue;
567 } else {
568 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
569 }
570 tmperval = elm->type->der_encoder(elm->type, memb_ptr,
571 elm->tag_mode, elm->tag,
572 cb, app_key);
573 if(tmperval.encoded == -1)
574 return tmperval;
575 computed_size -= tmperval.encoded;
576 ASN_DEBUG("Member %d %s of SEQUENCE %s encoded in %d bytes",
Lev Walkin449f8322004-08-20 13:23:42 +0000577 edx, elm->name, td->name, tmperval.encoded);
Lev Walkinf15320b2004-06-03 03:38:44 +0000578 }
579
580 if(computed_size != 0) {
581 /*
582 * Encoded size is not equal to the computed size.
583 */
584 erval.encoded = -1;
Lev Walkin449f8322004-08-20 13:23:42 +0000585 erval.failed_type = td;
Lev Walkinf15320b2004-06-03 03:38:44 +0000586 erval.structure_ptr = ptr;
587 }
588
589 return erval;
590}
591
Lev Walkinc61f3862005-02-14 17:21:22 +0000592
Lev Walkin1bbc2002004-10-23 13:27:30 +0000593#undef XER_ADVANCE
594#define XER_ADVANCE(num_bytes) do { \
595 size_t num = num_bytes; \
596 buf_ptr = ((char *)buf_ptr) + num; \
597 size -= num; \
598 consumed_myself += num; \
599 } while(0)
600
Lev Walkinc61f3862005-02-14 17:21:22 +0000601/*
602 * Decode the XER (XML) data.
603 */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000604asn_dec_rval_t
605SEQUENCE_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
606 void **struct_ptr, const char *opt_mname,
607 void *buf_ptr, size_t size) {
608 /*
609 * Bring closer parts of structure description.
610 */
Lev Walkinc61f3862005-02-14 17:21:22 +0000611 asn_SEQUENCE_specifics_t *specs
612 = (asn_SEQUENCE_specifics_t *)td->specifics;
Lev Walkin1bbc2002004-10-23 13:27:30 +0000613 asn_TYPE_member_t *elements = td->elements;
614 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
615
616 /*
Lev Walkinc61f3862005-02-14 17:21:22 +0000617 * ... and parts of the structure being constructed.
Lev Walkin1bbc2002004-10-23 13:27:30 +0000618 */
619 void *st = *struct_ptr; /* Target structure. */
620 asn_struct_ctx_t *ctx; /* Decoder context */
621
Lev Walkinc61f3862005-02-14 17:21:22 +0000622 asn_dec_rval_t rval; /* Return value from a decoder */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000623 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000624 int edx; /* Element index */
Lev Walkin904e65b2005-02-18 14:23:48 +0000625 int edx_end;
Lev Walkin1bbc2002004-10-23 13:27:30 +0000626
627 /*
628 * Create the target structure if it is not present already.
629 */
630 if(st == 0) {
631 st = *struct_ptr = CALLOC(1, specs->struct_size);
632 if(st == 0) RETURN(RC_FAIL);
633 }
634
635 /*
636 * Restore parsing context.
637 */
638 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
639
640
641 /*
642 * Phases of XER/XML processing:
643 * Phase 0: Check that the opening tag matches our expectations.
644 * Phase 1: Processing body and reacting on closing tag.
645 * Phase 2: Processing inner type.
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000646 * Phase 3: Skipping unknown extensions.
647 * Phase 4: PHASED OUT
Lev Walkin1bbc2002004-10-23 13:27:30 +0000648 */
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000649 for(edx = ctx->step; ctx->phase <= 3;) {
Lev Walkin1bbc2002004-10-23 13:27:30 +0000650 pxer_chunk_type_e ch_type; /* XER chunk type */
651 ssize_t ch_size; /* Chunk size */
652 xer_check_tag_e tcv; /* Tag check value */
653 asn_TYPE_member_t *elm;
Lev Walkin904e65b2005-02-18 14:23:48 +0000654 int n;
Lev Walkin1bbc2002004-10-23 13:27:30 +0000655
656 /*
Lev Walkinc61f3862005-02-14 17:21:22 +0000657 * Go inside the inner member of a sequence.
Lev Walkin1bbc2002004-10-23 13:27:30 +0000658 */
659 if(ctx->phase == 2) {
660 asn_dec_rval_t tmprval;
Lev Walkin1bbc2002004-10-23 13:27:30 +0000661 void *memb_ptr; /* Pointer to the member */
662 void **memb_ptr2; /* Pointer to that pointer */
663
Lev Walkinabf68892004-10-26 10:12:14 +0000664 elm = &td->elements[edx];
665
Lev Walkin1bbc2002004-10-23 13:27:30 +0000666 if(elm->flags & ATF_POINTER) {
667 /* Member is a pointer to another structure */
668 memb_ptr2 = (void **)((char *)st
669 + elm->memb_offset);
670 } else {
671 memb_ptr = (char *)st + elm->memb_offset;
672 memb_ptr2 = &memb_ptr;
673 }
674
Lev Walkinc61f3862005-02-14 17:21:22 +0000675 /* Invoke the inner type decoder, m.b. multiple times */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000676 tmprval = elm->type->xer_decoder(opt_codec_ctx,
677 elm->type, memb_ptr2, elm->name,
678 buf_ptr, size);
679 XER_ADVANCE(tmprval.consumed);
680 if(tmprval.code != RC_OK)
681 RETURN(tmprval.code);
Lev Walkinc61f3862005-02-14 17:21:22 +0000682 ctx->phase = 1; /* Back to body processing */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000683 ctx->step = ++edx;
Lev Walkinc61f3862005-02-14 17:21:22 +0000684 ASN_DEBUG("XER/SEQUENCE phase => %d, step => %d",
685 ctx->phase, ctx->step);
Lev Walkin1bbc2002004-10-23 13:27:30 +0000686 /* Fall through */
687 }
688
689 /*
690 * Get the next part of the XML stream.
691 */
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000692 ch_size = xer_next_token(buf_ptr, size, &ch_type);
Lev Walkin1bbc2002004-10-23 13:27:30 +0000693 switch(ch_size) {
694 case -1: RETURN(RC_FAIL);
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000695 case 0: RETURN(RC_WMORE);
Lev Walkin1bbc2002004-10-23 13:27:30 +0000696 default:
697 switch(ch_type) {
698 case PXER_COMMENT: /* Got XML comment */
699 case PXER_TEXT: /* Ignore free-standing text */
700 XER_ADVANCE(ch_size); /* Skip silently */
701 continue;
702 case PXER_TAG:
703 break; /* Check the rest down there */
704 }
705 }
706
Lev Walkinc61f3862005-02-14 17:21:22 +0000707 tcv = xer_check_tag(buf_ptr, ch_size, xml_tag);
708 ASN_DEBUG("XER/SEQUENCE: tcv = %d, ph=%d", tcv, ctx->phase);
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000709
710 /* Skip the extensions section */
711 if(ctx->phase == 3) {
712 switch(xer_skip_unknown(tcv, &ctx->left)) {
713 case -1:
714 ctx->phase = 4;
715 RETURN(RC_FAIL);
716 case 0:
717 XER_ADVANCE(ch_size);
718 continue;
719 case 1:
720 XER_ADVANCE(ch_size);
721 ctx->phase = 1;
722 continue;
723 case 2:
724 ctx->phase = 1;
725 break;
726 }
727 }
728
Lev Walkin1bbc2002004-10-23 13:27:30 +0000729 switch(tcv) {
730 case XCT_CLOSING:
731 if(ctx->phase == 0) break;
Lev Walkin1bbc2002004-10-23 13:27:30 +0000732 ctx->phase = 0;
733 /* Fall through */
734 case XCT_BOTH:
735 if(ctx->phase == 0) {
736 if(edx >= td->elements_count
737 ||
738 /* Explicit OPTIONAL specs reaches the end */
739 (edx + elements[edx].optional
740 == td->elements_count)
741 ||
742 /* All extensions are optional */
743 (IN_EXTENSION_GROUP(specs, edx)
744 && specs->ext_before
745 > td->elements_count)
746 ) {
747 XER_ADVANCE(ch_size);
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000748 ctx->phase = 4; /* Phase out */
Lev Walkinc61f3862005-02-14 17:21:22 +0000749 RETURN(RC_OK);
Lev Walkin1bbc2002004-10-23 13:27:30 +0000750 } else {
751 ASN_DEBUG("Premature end of XER SEQUENCE");
752 RETURN(RC_FAIL);
753 }
754 }
755 /* Fall through */
756 case XCT_OPENING:
757 if(ctx->phase == 0) {
758 XER_ADVANCE(ch_size);
759 ctx->phase = 1; /* Processing body phase */
760 continue;
761 }
Lev Walkinda3ca412004-10-23 14:58:15 +0000762 /* Fall through */
Lev Walkin904e65b2005-02-18 14:23:48 +0000763 case XCT_UNKNOWN_OP:
764 case XCT_UNKNOWN_BO:
Lev Walkin1bbc2002004-10-23 13:27:30 +0000765
Lev Walkinc61f3862005-02-14 17:21:22 +0000766 ASN_DEBUG("XER/SEQUENCE: tcv=%d, ph=%d",
767 tcv, ctx->phase);
768 if(ctx->phase != 1
Lev Walkinda3ca412004-10-23 14:58:15 +0000769 || edx >= td->elements_count)
Lev Walkin1bbc2002004-10-23 13:27:30 +0000770 break; /* Really unexpected */
771
772 /*
773 * Search which member corresponds to this tag.
774 */
775 edx_end = edx + elements[edx].optional + 1;
Lev Walkin904e65b2005-02-18 14:23:48 +0000776 if(edx_end > td->elements_count)
777 edx_end = td->elements_count;
Lev Walkin1bbc2002004-10-23 13:27:30 +0000778 for(n = edx; n < edx_end; n++) {
779 elm = &td->elements[n];
Lev Walkinc61f3862005-02-14 17:21:22 +0000780 tcv = xer_check_tag(buf_ptr,ch_size,elm->name);
Lev Walkin1bbc2002004-10-23 13:27:30 +0000781 switch(tcv) {
782 case XCT_BOTH:
783 case XCT_OPENING:
784 /*
785 * Process this member.
786 */
787 ctx->step = edx = n;
788 ctx->phase = 2;
789 break;
Lev Walkin904e65b2005-02-18 14:23:48 +0000790 case XCT_UNKNOWN_OP:
791 case XCT_UNKNOWN_BO:
Lev Walkin6c0df202005-02-14 19:03:17 +0000792 continue;
Lev Walkin1bbc2002004-10-23 13:27:30 +0000793 default:
794 n = edx_end;
795 break; /* Phase out */
796 }
797 break;
798 }
Lev Walkin904e65b2005-02-18 14:23:48 +0000799 if(n != edx_end)
800 continue;
801
802 /* It is expected extension */
803 if(IN_EXTENSION_GROUP(specs,
804 edx + elements[edx].optional)) {
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000805 ASN_DEBUG("Got anticipated extension at %d", edx);
Lev Walkin904e65b2005-02-18 14:23:48 +0000806 /*
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000807 * Check for (XCT_BOTH or XCT_UNKNOWN_BO)
808 * By using a mask. Only record a pure
809 * <opening> tags.
Lev Walkin904e65b2005-02-18 14:23:48 +0000810 */
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000811 if(tcv & XCT_CLOSING) {
812 /* Found </extension> without body */
813 } else {
814 ctx->left = 1;
815 ctx->phase = 3; /* Skip ...'s */
816 }
817 XER_ADVANCE(ch_size);
818 continue;
Lev Walkin904e65b2005-02-18 14:23:48 +0000819 }
820
821 /* Fall through */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000822 default:
823 break;
824 }
825
826 ASN_DEBUG("Unexpected XML tag in SEQUENCE");
827 break;
828 }
829
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000830 ctx->phase = 4; /* "Phase out" on hard failure */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000831 RETURN(RC_FAIL);
832}
833
Lev Walkina9cc46e2004-09-22 16:06:28 +0000834asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000835SEQUENCE_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000836 int ilevel, enum xer_encoder_flags_e flags,
837 asn_app_consume_bytes_f *cb, void *app_key) {
838 asn_enc_rval_t er;
839 int xcan = (flags & XER_F_CANONICAL);
840 int edx;
841
842 if(!sptr)
843 _ASN_ENCODE_FAILED;
844
845 er.encoded = 0;
846
847 for(edx = 0; edx < td->elements_count; edx++) {
848 asn_enc_rval_t tmper;
Lev Walkin5e033762004-09-29 13:26:15 +0000849 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkina9cc46e2004-09-22 16:06:28 +0000850 void *memb_ptr;
851 const char *mname = elm->name;
852 unsigned int mlen = strlen(mname);
853
854 if(elm->flags & ATF_POINTER) {
855 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
856 if(!memb_ptr) continue; /* OPTIONAL element? */
857 } else {
858 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
859 }
860
861 if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel);
862 _ASN_CALLBACK3("<", 1, mname, mlen, ">", 1);
863
864 /* Print the member itself */
865 tmper = elm->type->xer_encoder(elm->type, memb_ptr,
866 ilevel + 1, flags, cb, app_key);
867 if(tmper.encoded == -1) return tmper;
868
869 _ASN_CALLBACK3("</", 2, mname, mlen, ">", 1);
870 er.encoded += 5 + (2 * mlen) + tmper.encoded;
871 }
872
873 if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel - 1);
874
875 return er;
Lev Walkin942fd082004-10-03 09:13:02 +0000876cb_failed:
877 _ASN_ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000878}
879
Lev Walkinf15320b2004-06-03 03:38:44 +0000880int
Lev Walkin5e033762004-09-29 13:26:15 +0000881SEQUENCE_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000882 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000883 int edx;
884 int ret;
885
Lev Walkin8e8078a2004-09-26 13:10:40 +0000886 if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000887
888 /* Dump preamble */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000889 if(cb(td->name, strlen(td->name), app_key) < 0
890 || cb(" ::= {", 6, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000891 return -1;
892
Lev Walkin449f8322004-08-20 13:23:42 +0000893 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000894 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000895 const void *memb_ptr;
896
Lev Walkincc93b0f2004-09-10 09:18:20 +0000897 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000898 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
899 if(!memb_ptr) continue;
900 } else {
901 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
902 }
903
904 /* Indentation */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000905 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000906
907 /* Print the member's name and stuff */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000908 if(cb(elm->name, strlen(elm->name), app_key) < 0
909 || cb(": ", 2, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000910 return -1;
911
912 /* Print the member itself */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000913 ret = elm->type->print_struct(elm->type, memb_ptr, ilevel + 1,
Lev Walkinf15320b2004-06-03 03:38:44 +0000914 cb, app_key);
915 if(ret) return ret;
Lev Walkinf15320b2004-06-03 03:38:44 +0000916 }
917
Lev Walkin8e8078a2004-09-26 13:10:40 +0000918 ilevel--;
919 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000920
Lev Walkin8e8078a2004-09-26 13:10:40 +0000921 return (cb("}", 1, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000922}
923
924void
Lev Walkin5e033762004-09-29 13:26:15 +0000925SEQUENCE_free(asn_TYPE_descriptor_t *td, void *sptr, int contents_only) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000926 int edx;
927
928 if(!td || !sptr)
929 return;
930
931 ASN_DEBUG("Freeing %s as SEQUENCE", td->name);
932
Lev Walkin449f8322004-08-20 13:23:42 +0000933 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000934 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000935 void *memb_ptr;
Lev Walkincc93b0f2004-09-10 09:18:20 +0000936 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000937 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
938 if(memb_ptr)
939 elm->type->free_struct(elm->type, memb_ptr, 0);
940 } else {
941 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
942 elm->type->free_struct(elm->type, memb_ptr, 1);
943 }
944 }
945
946 if(!contents_only) {
947 FREEMEM(sptr);
948 }
949}
950
951int
Lev Walkin5e033762004-09-29 13:26:15 +0000952SEQUENCE_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000953 asn_app_consume_bytes_f *app_errlog, void *app_key) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000954 int edx;
955
956 if(!sptr) {
Lev Walkinba4e5182004-08-11 09:44:13 +0000957 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkin16835b62004-08-22 13:47:59 +0000958 "%s: value not given (%s:%d)",
959 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000960 return -1;
961 }
962
963 /*
964 * Iterate over structure members and check their validity.
965 */
Lev Walkin449f8322004-08-20 13:23:42 +0000966 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000967 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000968 const void *memb_ptr;
969
Lev Walkincc93b0f2004-09-10 09:18:20 +0000970 if(elm->flags & ATF_POINTER) {
Lev Walkin4c36e302004-06-06 07:20:02 +0000971 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000972 if(!memb_ptr) continue;
973 } else {
974 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
975 }
976
Lev Walkin449f8322004-08-20 13:23:42 +0000977 if(elm->memb_constraints) {
978 int ret = elm->memb_constraints(elm->type, memb_ptr,
979 app_errlog, app_key);
980 if(ret) return ret;
981 } else {
982 int ret = elm->type->check_constraints(elm->type,
983 memb_ptr, app_errlog, app_key);
984 if(ret) return ret;
985 /*
986 * Cannot inherit it earlier:
987 * need to make sure we get the updated version.
988 */
989 elm->memb_constraints = elm->type->check_constraints;
990 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000991 }
992
993 return 0;
994}