blob: 07c7772893b029cc5cc7fc6cc7b8b5776334182d [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
2 * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
4 */
Lev Walkina9cc46e2004-09-22 16:06:28 +00005#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00006#include <constr_SEQUENCE.h>
Lev Walkin4c36e302004-06-06 07:20:02 +00007#include <assert.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00008
9/*
10 * Number of bytes left for this structure.
11 * (ctx->left) indicates the number of bytes _transferred_ for the structure.
12 * (size) contains the number of bytes in the buffer passed.
13 */
Lev Walkinec1ffd42004-08-18 04:53:32 +000014#define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left)
Lev Walkinf15320b2004-06-03 03:38:44 +000015
16/*
17 * If the subprocessor function returns with an indication that it wants
18 * more data, it may well be a fatal decoding problem, because the
19 * size is constrained by the <TLV>'s L, even if the buffer size allows
20 * reading more data.
21 * For example, consider the buffer containing the following TLVs:
22 * <T:5><L:1><V> <T:6>...
23 * The TLV length clearly indicates that one byte is expected in V, but
24 * if the V processor returns with "want more data" even if the buffer
25 * contains way more data than the V processor have seen.
26 */
Lev Walkind9bd7752004-06-05 08:17:50 +000027#define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size)
Lev Walkinf15320b2004-06-03 03:38:44 +000028
29/*
30 * This macro "eats" the part of the buffer which is definitely "consumed",
31 * i.e. was correctly converted into local representation or rightfully skipped.
32 */
Lev Walkincc6a9102004-09-23 22:06:26 +000033#undef ADVANCE
Lev Walkinf15320b2004-06-03 03:38:44 +000034#define ADVANCE(num_bytes) do { \
35 size_t num = num_bytes; \
Lev Walkin4ce78ca2004-08-25 01:34:11 +000036 ptr = ((char *)ptr) + num; \
Lev Walkinf15320b2004-06-03 03:38:44 +000037 size -= num; \
38 if(ctx->left >= 0) \
39 ctx->left -= num; \
40 consumed_myself += num; \
41 } while(0)
42
43/*
44 * Switch to the next phase of parsing.
45 */
Lev Walkincc6a9102004-09-23 22:06:26 +000046#undef NEXT_PHASE
47#undef PHASE_OUT
Lev Walkinf15320b2004-06-03 03:38:44 +000048#define NEXT_PHASE(ctx) do { \
49 ctx->phase++; \
50 ctx->step = 0; \
51 } while(0)
52#define PHASE_OUT(ctx) do { ctx->phase = 10; } while(0)
53
54/*
55 * Return a standardized complex structure.
56 */
Lev Walkincc6a9102004-09-23 22:06:26 +000057#undef RETURN
Lev Walkinf15320b2004-06-03 03:38:44 +000058#define RETURN(_code) do { \
59 rval.code = _code; \
60 rval.consumed = consumed_myself;\
61 return rval; \
62 } while(0)
63
64/*
65 * Check whether we are inside the extensions group.
66 */
67#define IN_EXTENSION_GROUP(specs, memb_idx) \
68 ( ((memb_idx) > (specs)->ext_after) \
69 &&((memb_idx) < (specs)->ext_before))
70
Lev Walkin4c36e302004-06-06 07:20:02 +000071
72/*
73 * Tags are canonically sorted in the tag2element map.
74 */
75static int
76_t2e_cmp(const void *ap, const void *bp) {
Lev Walkin5e033762004-09-29 13:26:15 +000077 const asn_TYPE_tag2member_t *a = (const asn_TYPE_tag2member_t *)ap;
78 const asn_TYPE_tag2member_t *b = (const asn_TYPE_tag2member_t *)bp;
Lev Walkinc2346572004-08-11 09:07:36 +000079
Lev Walkin4c36e302004-06-06 07:20:02 +000080 int a_class = BER_TAG_CLASS(a->el_tag);
81 int b_class = BER_TAG_CLASS(b->el_tag);
82
83 if(a_class == b_class) {
84 ber_tlv_tag_t a_value = BER_TAG_VALUE(a->el_tag);
85 ber_tlv_tag_t b_value = BER_TAG_VALUE(b->el_tag);
86
Lev Walkin924fa032004-06-14 13:42:23 +000087 if(a_value == b_value) {
Lev Walkin50299c12004-06-14 14:11:14 +000088 if(a->el_no > b->el_no)
89 return 1;
Lev Walkin924fa032004-06-14 13:42:23 +000090 /*
91 * Important: we do not check
Lev Walkin50299c12004-06-14 14:11:14 +000092 * for a->el_no <= b->el_no!
Lev Walkin924fa032004-06-14 13:42:23 +000093 */
Lev Walkin4c36e302004-06-06 07:20:02 +000094 return 0;
Lev Walkin924fa032004-06-14 13:42:23 +000095 } else if(a_value < b_value)
Lev Walkin4c36e302004-06-06 07:20:02 +000096 return -1;
97 else
98 return 1;
99 } else if(a_class < b_class) {
100 return -1;
101 } else {
102 return 1;
103 }
104}
105
106
Lev Walkinf15320b2004-06-03 03:38:44 +0000107/*
108 * The decoder of the SEQUENCE type.
109 */
110ber_dec_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000111SEQUENCE_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkinf15320b2004-06-03 03:38:44 +0000112 void **struct_ptr, void *ptr, size_t size, int tag_mode) {
113 /*
114 * Bring closer parts of structure description.
115 */
Lev Walkin5e033762004-09-29 13:26:15 +0000116 asn_SEQUENCE_specifics_t *specs = (asn_SEQUENCE_specifics_t *)td->specifics;
117 asn_TYPE_member_t *elements = td->elements;
Lev Walkinf15320b2004-06-03 03:38:44 +0000118
119 /*
120 * Parts of the structure being constructed.
121 */
122 void *st = *struct_ptr; /* Target structure. */
Lev Walkin5e033762004-09-29 13:26:15 +0000123 asn_struct_ctx_t *ctx; /* Decoder context */
Lev Walkinf15320b2004-06-03 03:38:44 +0000124
125 ber_tlv_tag_t tlv_tag; /* T from TLV */
Lev Walkinf15320b2004-06-03 03:38:44 +0000126 ber_dec_rval_t rval; /* Return code from subparsers */
127
128 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
129 int edx; /* SEQUENCE element's index */
130
Lev Walkin449f8322004-08-20 13:23:42 +0000131 ASN_DEBUG("Decoding %s as SEQUENCE", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000132
133 /*
134 * Create the target structure if it is not present already.
135 */
136 if(st == 0) {
137 st = *struct_ptr = CALLOC(1, specs->struct_size);
138 if(st == 0) {
139 RETURN(RC_FAIL);
140 }
141 }
142
143 /*
144 * Restore parsing context.
145 */
Lev Walkin5e033762004-09-29 13:26:15 +0000146 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000147
148 /*
149 * Start to parse where left previously
150 */
151 switch(ctx->phase) {
152 case 0:
153 /*
154 * PHASE 0.
155 * Check that the set of tags associated with given structure
156 * perfectly fits our expectations.
157 */
158
Lev Walkin5e033762004-09-29 13:26:15 +0000159 rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size,
Lev Walkin8e8078a2004-09-26 13:10:40 +0000160 tag_mode, 1, &ctx->left, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000161 if(rval.code != RC_OK) {
162 ASN_DEBUG("%s tagging check failed: %d",
Lev Walkin449f8322004-08-20 13:23:42 +0000163 td->name, rval.code);
Lev Walkinf15320b2004-06-03 03:38:44 +0000164 consumed_myself += rval.consumed;
165 RETURN(rval.code);
166 }
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
243 /*
244 * Find the next available type with this tag.
245 */
Lev Walkin4c36e302004-06-06 07:20:02 +0000246 use_bsearch = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000247 opt_edx_end = edx + elements[edx].optional + 1;
Lev Walkin449f8322004-08-20 13:23:42 +0000248 if(opt_edx_end > td->elements_count)
249 opt_edx_end = td->elements_count; /* Cap */
Lev Walkinf9dbd9a2004-07-01 00:48:04 +0000250 else if(opt_edx_end - edx > 8) {
Lev Walkin924fa032004-06-14 13:42:23 +0000251 /* Limit the scope of linear search... */
Lev Walkinf9dbd9a2004-07-01 00:48:04 +0000252 opt_edx_end = edx + 8;
Lev Walkin4c36e302004-06-06 07:20:02 +0000253 use_bsearch = 1;
Lev Walkin924fa032004-06-14 13:42:23 +0000254 /* ... and resort to bsearch() */
Lev Walkin4c36e302004-06-06 07:20:02 +0000255 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000256 for(n = edx; n < opt_edx_end; n++) {
257 if(BER_TAGS_EQUAL(tlv_tag, elements[n].tag)) {
258 /*
259 * Found element corresponding to the tag
260 * being looked at.
261 * Reposition over the right element.
262 */
263 edx = n;
Lev Walkin4c36e302004-06-06 07:20:02 +0000264 ctx->step = 1 + 2 * edx; /* Remember! */
265 goto microphase2;
Lev Walkinb9189732004-09-10 09:37:12 +0000266 } else if(elements[n].flags & ATF_OPEN_TYPE) {
267 /*
268 * This is the ANY type, which may bear
269 * any flag whatsoever.
270 */
271 edx = n;
272 ctx->step = 1 + 2 * edx; /* Remember! */
273 goto microphase2;
Lev Walkin4c36e302004-06-06 07:20:02 +0000274 } else if(elements[n].tag == (ber_tlv_tag_t)-1) {
275 use_bsearch = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000276 break;
277 }
278 }
Lev Walkin4c36e302004-06-06 07:20:02 +0000279 if(use_bsearch) {
280 /*
Lev Walkin4e0704f2004-09-04 06:17:35 +0000281 * Resort to a binary search over
Lev Walkin4c36e302004-06-06 07:20:02 +0000282 * sorted array of tags.
283 */
Lev Walkin5e033762004-09-29 13:26:15 +0000284 asn_TYPE_tag2member_t *t2m;
285 asn_TYPE_tag2member_t key;
Lev Walkin4c36e302004-06-06 07:20:02 +0000286 key.el_tag = tlv_tag;
Lev Walkin924fa032004-06-14 13:42:23 +0000287 key.el_no = edx;
Lev Walkinc2346572004-08-11 09:07:36 +0000288 (void *)t2m = bsearch(&key,
289 specs->tag2el, specs->tag2el_count,
Lev Walkin4c36e302004-06-06 07:20:02 +0000290 sizeof(specs->tag2el[0]), _t2e_cmp);
Lev Walkin924fa032004-06-14 13:42:23 +0000291 if(t2m) {
Lev Walkin5e033762004-09-29 13:26:15 +0000292 asn_TYPE_tag2member_t *best = 0;
293 asn_TYPE_tag2member_t *t2m_f, *t2m_l;
Lev Walkin924fa032004-06-14 13:42:23 +0000294 int edx_max = edx + elements[edx].optional;
Lev Walkin4c36e302004-06-06 07:20:02 +0000295 /*
296 * Rewind to the first element with that tag,
297 * `cause bsearch() does not guarantee order.
298 */
Lev Walkin924fa032004-06-14 13:42:23 +0000299 t2m_f = t2m + t2m->toff_first;
300 t2m_l = t2m + t2m->toff_last;
301 for(t2m = t2m_f; t2m <= t2m_l; t2m++) {
302 if(t2m->el_no > edx_max) break;
303 if(t2m->el_no < edx) continue;
304 best = t2m;
305 }
306 if(best) {
307 edx = best->el_no;
308 ctx->step = 1 + 2 * edx;
309 goto microphase2;
310 }
Lev Walkin4c36e302004-06-06 07:20:02 +0000311 }
312 n = opt_edx_end;
313 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000314 if(n == opt_edx_end) {
315 /*
316 * If tag is unknown, it may be either
317 * an unknown (thus, incorrect) tag,
318 * or an extension (...),
319 * or an end of the indefinite-length structure.
320 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000321 if(!IN_EXTENSION_GROUP(specs, edx)) {
322 ASN_DEBUG("Unexpected tag %s",
323 ber_tlv_tag_string(tlv_tag));
Lev Walkin4c36e302004-06-06 07:20:02 +0000324 ASN_DEBUG("Expected tag %s (%s)%s",
Lev Walkinf15320b2004-06-03 03:38:44 +0000325 ber_tlv_tag_string(elements[edx].tag),
Lev Walkin4c36e302004-06-06 07:20:02 +0000326 elements[edx].name,
Lev Walkinf15320b2004-06-03 03:38:44 +0000327 elements[edx].optional
328 ?" or alternatives":"");
329 RETURN(RC_FAIL);
330 }
331
332 if(ctx->left < 0
333 && ((uint8_t *)ptr)[0] == 0) {
334 if(LEFT < 2) {
335 if(SIZE_VIOLATION)
336 RETURN(RC_FAIL);
337 else
338 RETURN(RC_WMORE);
339 } else if(((uint8_t *)ptr)[1] == 0) {
340 /*
341 * Yeah, baby! Found the terminator
342 * of the indefinite length structure.
343 */
344 /*
345 * Proceed to the canonical
346 * finalization function.
347 * No advancing is necessary.
348 */
349 goto phase3;
350 }
351 } else {
352 /* Skip this tag */
353 ssize_t skip;
354
Lev Walkinbaaa24f2004-09-29 14:19:14 +0000355 skip = ber_skip_length(opt_codec_ctx,
Lev Walkinf15320b2004-06-03 03:38:44 +0000356 BER_TLV_CONSTRUCTED(ptr),
Lev Walkin4d9528c2004-08-11 08:10:13 +0000357 (char *)ptr + tag_len, LEFT - tag_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000358 ASN_DEBUG("Skip length %d in %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000359 (int)skip, td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000360 switch(skip) {
361 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
362 /* Fall through */
363 case -1: RETURN(RC_FAIL);
364 }
365
366 ADVANCE(skip + tag_len);
367 ctx->step -= 2;
368 edx--;
369 continue; /* Try again with the next tag */
370 }
371 }
372
373 /*
374 * MICROPHASE 2: Invoke the member-specific decoder.
375 */
376 ctx->step |= 1; /* Confirm entering next microphase */
377 microphase2:
Lev Walkin449f8322004-08-20 13:23:42 +0000378 ASN_DEBUG("Inside SEQUENCE %s MF2", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000379
380 /*
381 * Compute the position of the member inside a structure,
382 * and also a type of containment (it may be contained
383 * as pointer or using inline inclusion).
384 */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000385 if(elements[edx].flags & ATF_POINTER) {
386 /* Member is a pointer to another structure */
Lev Walkinc2346572004-08-11 09:07:36 +0000387 memb_ptr2 = (void **)((char *)st + elements[edx].memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000388 } else {
389 /*
390 * A pointer to a pointer
391 * holding the start of the structure
392 */
393 memb_ptr = (char *)st + elements[edx].memb_offset;
394 memb_ptr2 = &memb_ptr;
395 }
396 /*
397 * Invoke the member fetch routine according to member's type
398 */
Lev Walkin5e033762004-09-29 13:26:15 +0000399 rval = elements[edx].type->ber_decoder(opt_codec_ctx,
Lev Walkinc2346572004-08-11 09:07:36 +0000400 elements[edx].type,
Lev Walkinf15320b2004-06-03 03:38:44 +0000401 memb_ptr2, ptr, LEFT,
402 elements[edx].tag_mode);
Lev Walkin60b7cff2004-09-04 04:44:11 +0000403 ASN_DEBUG("In %s SEQUENCE decoded %d %s of %d "
Lev Walkin5beb7de2004-09-24 20:59:27 +0000404 "in %d bytes rval.code %d, size=%d",
Lev Walkin449f8322004-08-20 13:23:42 +0000405 td->name, edx, elements[edx].type->name,
Lev Walkin5beb7de2004-09-24 20:59:27 +0000406 (int)LEFT, (int)rval.consumed, rval.code, (int)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000407 switch(rval.code) {
408 case RC_OK:
409 break;
410 case RC_WMORE: /* More data expected */
411 if(!SIZE_VIOLATION) {
412 ADVANCE(rval.consumed);
413 RETURN(RC_WMORE);
414 }
Lev Walkin5beb7de2004-09-24 20:59:27 +0000415 ASN_DEBUG("Size violation (c->l=%ld <= s=%ld)",
416 (long)ctx->left, (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000417 /* Fall through */
418 case RC_FAIL: /* Fatal error */
419 RETURN(RC_FAIL);
420 } /* switch(rval) */
421
422 ADVANCE(rval.consumed);
423 } /* for(all structure members) */
424
425 phase3:
426 ctx->phase = 3;
427 case 3: /* 00 and other tags expected */
428 case 4: /* only 00's expected */
429
430 ASN_DEBUG("SEQUENCE %s Leftover: %ld, size = %ld",
Lev Walkin449f8322004-08-20 13:23:42 +0000431 td->name, (long)ctx->left, (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000432
433 /*
434 * Skip everything until the end of the SEQUENCE.
435 */
436 while(ctx->left) {
437 ssize_t tl, ll;
438
439 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
440 switch(tl) {
441 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
442 /* Fall through */
443 case -1: RETURN(RC_FAIL);
444 }
445
446 /*
447 * If expected <0><0>...
448 */
449 if(ctx->left < 0
450 && ((uint8_t *)ptr)[0] == 0) {
451 if(LEFT < 2) {
452 if(SIZE_VIOLATION)
453 RETURN(RC_FAIL);
454 else
455 RETURN(RC_WMORE);
456 } else if(((uint8_t *)ptr)[1] == 0) {
457 /*
458 * Correctly finished with <0><0>.
459 */
460 ADVANCE(2);
461 ctx->left++;
462 ctx->phase = 4;
463 continue;
464 }
465 }
466
Lev Walkin449f8322004-08-20 13:23:42 +0000467 if(!IN_EXTENSION_GROUP(specs, td->elements_count)
Lev Walkinf15320b2004-06-03 03:38:44 +0000468 || ctx->phase == 4) {
469 ASN_DEBUG("Unexpected continuation "
470 "of a non-extensible type "
471 "%s (SEQUENCE): %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000472 td->name,
Lev Walkinf15320b2004-06-03 03:38:44 +0000473 ber_tlv_tag_string(tlv_tag));
474 RETURN(RC_FAIL);
475 }
476
Lev Walkinbaaa24f2004-09-29 14:19:14 +0000477 ll = ber_skip_length(opt_codec_ctx,
Lev Walkinf15320b2004-06-03 03:38:44 +0000478 BER_TLV_CONSTRUCTED(ptr),
Lev Walkin4d9528c2004-08-11 08:10:13 +0000479 (char *)ptr + tl, LEFT - tl);
Lev Walkinf15320b2004-06-03 03:38:44 +0000480 switch(ll) {
481 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
482 /* Fall through */
483 case -1: RETURN(RC_FAIL);
484 }
485
486 ADVANCE(tl + ll);
487 }
488
489 PHASE_OUT(ctx);
490 }
491
492 RETURN(RC_OK);
493}
494
Lev Walkin4c36e302004-06-06 07:20:02 +0000495
Lev Walkinf15320b2004-06-03 03:38:44 +0000496/*
497 * The DER encoder of the SEQUENCE type.
498 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000499asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000500SEQUENCE_encode_der(asn_TYPE_descriptor_t *td,
Lev Walkinf15320b2004-06-03 03:38:44 +0000501 void *ptr, int tag_mode, ber_tlv_tag_t tag,
502 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000503 size_t computed_size = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000504 asn_enc_rval_t erval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000505 ssize_t ret;
506 int edx;
507
508 ASN_DEBUG("%s %s as SEQUENCE",
Lev Walkin449f8322004-08-20 13:23:42 +0000509 cb?"Encoding":"Estimating", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000510
511 /*
512 * Gather the length of the underlying members sequence.
513 */
Lev Walkin449f8322004-08-20 13:23:42 +0000514 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000515 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000516 void *memb_ptr;
Lev Walkincc93b0f2004-09-10 09:18:20 +0000517 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000518 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
519 if(!memb_ptr) continue;
520 } else {
521 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
522 }
523 erval = elm->type->der_encoder(elm->type, memb_ptr,
524 elm->tag_mode, elm->tag,
525 0, 0);
526 if(erval.encoded == -1)
527 return erval;
528 computed_size += erval.encoded;
529 ASN_DEBUG("Member %d %s estimated %ld bytes",
530 edx, elm->name, (long)erval.encoded);
531 }
532
533 /*
534 * Encode the TLV for the sequence itself.
535 */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000536 ret = der_write_tags(td, computed_size, tag_mode, 1, tag, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +0000537 ASN_DEBUG("Wrote tags: %ld (+%ld)", (long)ret, (long)computed_size);
538 if(ret == -1) {
539 erval.encoded = -1;
Lev Walkin449f8322004-08-20 13:23:42 +0000540 erval.failed_type = td;
Lev Walkinf15320b2004-06-03 03:38:44 +0000541 erval.structure_ptr = ptr;
542 return erval;
543 }
544 erval.encoded = computed_size + ret;
545
546 if(!cb) return erval;
547
548 /*
549 * Encode all members.
550 */
Lev Walkin449f8322004-08-20 13:23:42 +0000551 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000552 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkina9cc46e2004-09-22 16:06:28 +0000553 asn_enc_rval_t tmperval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000554 void *memb_ptr;
555
Lev Walkincc93b0f2004-09-10 09:18:20 +0000556 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000557 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
558 if(!memb_ptr) continue;
559 } else {
560 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
561 }
562 tmperval = elm->type->der_encoder(elm->type, memb_ptr,
563 elm->tag_mode, elm->tag,
564 cb, app_key);
565 if(tmperval.encoded == -1)
566 return tmperval;
567 computed_size -= tmperval.encoded;
568 ASN_DEBUG("Member %d %s of SEQUENCE %s encoded in %d bytes",
Lev Walkin449f8322004-08-20 13:23:42 +0000569 edx, elm->name, td->name, tmperval.encoded);
Lev Walkinf15320b2004-06-03 03:38:44 +0000570 }
571
572 if(computed_size != 0) {
573 /*
574 * Encoded size is not equal to the computed size.
575 */
576 erval.encoded = -1;
Lev Walkin449f8322004-08-20 13:23:42 +0000577 erval.failed_type = td;
Lev Walkinf15320b2004-06-03 03:38:44 +0000578 erval.structure_ptr = ptr;
579 }
580
581 return erval;
582}
583
Lev Walkina9cc46e2004-09-22 16:06:28 +0000584asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000585SEQUENCE_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000586 int ilevel, enum xer_encoder_flags_e flags,
587 asn_app_consume_bytes_f *cb, void *app_key) {
588 asn_enc_rval_t er;
589 int xcan = (flags & XER_F_CANONICAL);
590 int edx;
591
592 if(!sptr)
593 _ASN_ENCODE_FAILED;
594
595 er.encoded = 0;
596
597 for(edx = 0; edx < td->elements_count; edx++) {
598 asn_enc_rval_t tmper;
Lev Walkin5e033762004-09-29 13:26:15 +0000599 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkina9cc46e2004-09-22 16:06:28 +0000600 void *memb_ptr;
601 const char *mname = elm->name;
602 unsigned int mlen = strlen(mname);
603
604 if(elm->flags & ATF_POINTER) {
605 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
606 if(!memb_ptr) continue; /* OPTIONAL element? */
607 } else {
608 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
609 }
610
611 if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel);
612 _ASN_CALLBACK3("<", 1, mname, mlen, ">", 1);
613
614 /* Print the member itself */
615 tmper = elm->type->xer_encoder(elm->type, memb_ptr,
616 ilevel + 1, flags, cb, app_key);
617 if(tmper.encoded == -1) return tmper;
618
619 _ASN_CALLBACK3("</", 2, mname, mlen, ">", 1);
620 er.encoded += 5 + (2 * mlen) + tmper.encoded;
621 }
622
623 if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel - 1);
624
625 return er;
626}
627
Lev Walkinf15320b2004-06-03 03:38:44 +0000628int
Lev Walkin5e033762004-09-29 13:26:15 +0000629SEQUENCE_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000630 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000631 int edx;
632 int ret;
633
Lev Walkin8e8078a2004-09-26 13:10:40 +0000634 if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000635
636 /* Dump preamble */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000637 if(cb(td->name, strlen(td->name), app_key) < 0
638 || cb(" ::= {", 6, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000639 return -1;
640
Lev Walkin449f8322004-08-20 13:23:42 +0000641 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000642 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000643 const void *memb_ptr;
644
Lev Walkincc93b0f2004-09-10 09:18:20 +0000645 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000646 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
647 if(!memb_ptr) continue;
648 } else {
649 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
650 }
651
652 /* Indentation */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000653 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000654
655 /* Print the member's name and stuff */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000656 if(cb(elm->name, strlen(elm->name), app_key) < 0
657 || cb(": ", 2, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000658 return -1;
659
660 /* Print the member itself */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000661 ret = elm->type->print_struct(elm->type, memb_ptr, ilevel + 1,
Lev Walkinf15320b2004-06-03 03:38:44 +0000662 cb, app_key);
663 if(ret) return ret;
Lev Walkinf15320b2004-06-03 03:38:44 +0000664 }
665
Lev Walkin8e8078a2004-09-26 13:10:40 +0000666 ilevel--;
667 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000668
Lev Walkin8e8078a2004-09-26 13:10:40 +0000669 return (cb("}", 1, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000670}
671
672void
Lev Walkin5e033762004-09-29 13:26:15 +0000673SEQUENCE_free(asn_TYPE_descriptor_t *td, void *sptr, int contents_only) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000674 int edx;
675
676 if(!td || !sptr)
677 return;
678
679 ASN_DEBUG("Freeing %s as SEQUENCE", td->name);
680
Lev Walkin449f8322004-08-20 13:23:42 +0000681 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000682 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000683 void *memb_ptr;
Lev Walkincc93b0f2004-09-10 09:18:20 +0000684 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000685 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
686 if(memb_ptr)
687 elm->type->free_struct(elm->type, memb_ptr, 0);
688 } else {
689 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
690 elm->type->free_struct(elm->type, memb_ptr, 1);
691 }
692 }
693
694 if(!contents_only) {
695 FREEMEM(sptr);
696 }
697}
698
699int
Lev Walkin5e033762004-09-29 13:26:15 +0000700SEQUENCE_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000701 asn_app_consume_bytes_f *app_errlog, void *app_key) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000702 int edx;
703
704 if(!sptr) {
Lev Walkinba4e5182004-08-11 09:44:13 +0000705 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkin16835b62004-08-22 13:47:59 +0000706 "%s: value not given (%s:%d)",
707 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000708 return -1;
709 }
710
711 /*
712 * Iterate over structure members and check their validity.
713 */
Lev Walkin449f8322004-08-20 13:23:42 +0000714 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000715 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000716 const void *memb_ptr;
717
Lev Walkincc93b0f2004-09-10 09:18:20 +0000718 if(elm->flags & ATF_POINTER) {
Lev Walkin4c36e302004-06-06 07:20:02 +0000719 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000720 if(!memb_ptr) continue;
721 } else {
722 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
723 }
724
Lev Walkin449f8322004-08-20 13:23:42 +0000725 if(elm->memb_constraints) {
726 int ret = elm->memb_constraints(elm->type, memb_ptr,
727 app_errlog, app_key);
728 if(ret) return ret;
729 } else {
730 int ret = elm->type->check_constraints(elm->type,
731 memb_ptr, app_errlog, app_key);
732 if(ret) return ret;
733 /*
734 * Cannot inherit it earlier:
735 * need to make sure we get the updated version.
736 */
737 elm->memb_constraints = elm->type->check_constraints;
738 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000739 }
740
741 return 0;
742}