blob: f811ac5f7e828635bce994d6dacd549d0e7233ad [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 */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000110asn_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 Walkindc06f6b2004-10-20 15:50:55 +0000126 asn_dec_rval_t rval; /* Return code from subparsers */
Lev Walkinf15320b2004-06-03 03:38:44 +0000127
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 Walkin566a5672004-10-05 06:36:57 +0000164 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000165 }
166
167 if(ctx->left >= 0)
168 ctx->left += rval.consumed; /* ?Substracted below! */
169 ADVANCE(rval.consumed);
170
171 NEXT_PHASE(ctx);
172
173 ASN_DEBUG("Structure consumes %ld bytes, buffer %ld",
174 (long)ctx->left, (long)size);
175
176 /* Fall through */
177 case 1:
178 /*
179 * PHASE 1.
180 * From the place where we've left it previously,
181 * try to decode the next member from the list of
182 * this structure's elements.
183 * (ctx->step) stores the member being processed
184 * between invocations and the microphase {0,1} of parsing
185 * that member:
186 * step = (<member_number> * 2 + <microphase>).
187 */
Lev Walkin449f8322004-08-20 13:23:42 +0000188 for(edx = (ctx->step >> 1); edx < td->elements_count;
Lev Walkinf15320b2004-06-03 03:38:44 +0000189 edx++, ctx->step = (ctx->step & ~1) + 2) {
190 void *memb_ptr; /* Pointer to the member */
Lev Walkinc2346572004-08-11 09:07:36 +0000191 void **memb_ptr2; /* Pointer to that pointer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000192 ssize_t tag_len; /* Length of TLV's T */
193 int opt_edx_end; /* Next non-optional element */
Lev Walkin4c36e302004-06-06 07:20:02 +0000194 int use_bsearch;
Lev Walkinf15320b2004-06-03 03:38:44 +0000195 int n;
196
197 if(ctx->step & 1)
198 goto microphase2;
199
200 /*
201 * MICROPHASE 1: Synchronize decoding.
202 */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000203 ASN_DEBUG("In %s SEQUENCE left %d, edx=%d flags=%d"
204 " opt=%d ec=%d",
205 td->name, (int)ctx->left, edx,
206 elements[edx].flags, elements[edx].optional,
207 td->elements_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000208
209 if(ctx->left == 0 /* No more stuff is expected */
210 && (
211 /* Explicit OPTIONAL specification reaches the end */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000212 (edx + elements[edx].optional
213 == td->elements_count)
Lev Walkinf15320b2004-06-03 03:38:44 +0000214 ||
215 /* All extensions are optional */
216 (IN_EXTENSION_GROUP(specs, edx)
Lev Walkin449f8322004-08-20 13:23:42 +0000217 && specs->ext_before > td->elements_count)
Lev Walkinf15320b2004-06-03 03:38:44 +0000218 )
219 ) {
Lev Walkin449f8322004-08-20 13:23:42 +0000220 ASN_DEBUG("End of SEQUENCE %s", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000221 /*
222 * Found the legitimate end of the structure.
223 */
224 PHASE_OUT(ctx);
225 RETURN(RC_OK);
226 }
227
228 /*
229 * Fetch the T from TLV.
230 */
231 tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
Lev Walkin906654e2004-09-10 15:49:15 +0000232 ASN_DEBUG("Current tag in %s SEQUENCE for element %d "
Lev Walkincc6a9102004-09-23 22:06:26 +0000233 "(%s) is %s encoded in %d bytes, of frame %ld",
Lev Walkin906654e2004-09-10 15:49:15 +0000234 td->name, edx, elements[edx].name,
235 ber_tlv_tag_string(tlv_tag), (int)tag_len, (long)LEFT);
Lev Walkinf15320b2004-06-03 03:38:44 +0000236 switch(tag_len) {
237 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
238 /* Fall through */
239 case -1: RETURN(RC_FAIL);
240 }
241
Lev Walkin566a5672004-10-05 06:36:57 +0000242 if(ctx->left < 0 && ((uint8_t *)ptr)[0] == 0) {
243 if(LEFT < 2) {
244 if(SIZE_VIOLATION)
245 RETURN(RC_FAIL);
246 else
247 RETURN(RC_WMORE);
248 } else if(((uint8_t *)ptr)[1] == 0) {
249 ASN_DEBUG("edx = %d, opt = %d, ec=%d",
250 edx, elements[edx].optional,
251 td->elements_count);
252 if((edx + elements[edx].optional
253 == td->elements_count)
254 || (IN_EXTENSION_GROUP(specs, edx)
255 && specs->ext_before
256 > td->elements_count)) {
257 /*
258 * Yeah, baby! Found the terminator
259 * of the indefinite length structure.
260 */
261 /*
262 * Proceed to the canonical
263 * finalization function.
264 * No advancing is necessary.
265 */
266 goto phase3;
267 }
268 }
269 }
270
Lev Walkinf15320b2004-06-03 03:38:44 +0000271 /*
272 * Find the next available type with this tag.
273 */
Lev Walkin4c36e302004-06-06 07:20:02 +0000274 use_bsearch = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000275 opt_edx_end = edx + elements[edx].optional + 1;
Lev Walkin449f8322004-08-20 13:23:42 +0000276 if(opt_edx_end > td->elements_count)
277 opt_edx_end = td->elements_count; /* Cap */
Lev Walkinf9dbd9a2004-07-01 00:48:04 +0000278 else if(opt_edx_end - edx > 8) {
Lev Walkin924fa032004-06-14 13:42:23 +0000279 /* Limit the scope of linear search... */
Lev Walkinf9dbd9a2004-07-01 00:48:04 +0000280 opt_edx_end = edx + 8;
Lev Walkin4c36e302004-06-06 07:20:02 +0000281 use_bsearch = 1;
Lev Walkin924fa032004-06-14 13:42:23 +0000282 /* ... and resort to bsearch() */
Lev Walkin4c36e302004-06-06 07:20:02 +0000283 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000284 for(n = edx; n < opt_edx_end; n++) {
285 if(BER_TAGS_EQUAL(tlv_tag, elements[n].tag)) {
286 /*
287 * Found element corresponding to the tag
288 * being looked at.
289 * Reposition over the right element.
290 */
291 edx = n;
Lev Walkin4c36e302004-06-06 07:20:02 +0000292 ctx->step = 1 + 2 * edx; /* Remember! */
293 goto microphase2;
Lev Walkinb9189732004-09-10 09:37:12 +0000294 } else if(elements[n].flags & ATF_OPEN_TYPE) {
295 /*
296 * This is the ANY type, which may bear
297 * any flag whatsoever.
298 */
299 edx = n;
300 ctx->step = 1 + 2 * edx; /* Remember! */
301 goto microphase2;
Lev Walkin4c36e302004-06-06 07:20:02 +0000302 } else if(elements[n].tag == (ber_tlv_tag_t)-1) {
303 use_bsearch = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000304 break;
305 }
306 }
Lev Walkin4c36e302004-06-06 07:20:02 +0000307 if(use_bsearch) {
308 /*
Lev Walkin4e0704f2004-09-04 06:17:35 +0000309 * Resort to a binary search over
Lev Walkin4c36e302004-06-06 07:20:02 +0000310 * sorted array of tags.
311 */
Lev Walkin5e033762004-09-29 13:26:15 +0000312 asn_TYPE_tag2member_t *t2m;
313 asn_TYPE_tag2member_t key;
Lev Walkin4c36e302004-06-06 07:20:02 +0000314 key.el_tag = tlv_tag;
Lev Walkin924fa032004-06-14 13:42:23 +0000315 key.el_no = edx;
Lev Walkinc2346572004-08-11 09:07:36 +0000316 (void *)t2m = bsearch(&key,
317 specs->tag2el, specs->tag2el_count,
Lev Walkin4c36e302004-06-06 07:20:02 +0000318 sizeof(specs->tag2el[0]), _t2e_cmp);
Lev Walkin924fa032004-06-14 13:42:23 +0000319 if(t2m) {
Lev Walkin5e033762004-09-29 13:26:15 +0000320 asn_TYPE_tag2member_t *best = 0;
321 asn_TYPE_tag2member_t *t2m_f, *t2m_l;
Lev Walkin924fa032004-06-14 13:42:23 +0000322 int edx_max = edx + elements[edx].optional;
Lev Walkin4c36e302004-06-06 07:20:02 +0000323 /*
324 * Rewind to the first element with that tag,
325 * `cause bsearch() does not guarantee order.
326 */
Lev Walkin924fa032004-06-14 13:42:23 +0000327 t2m_f = t2m + t2m->toff_first;
328 t2m_l = t2m + t2m->toff_last;
329 for(t2m = t2m_f; t2m <= t2m_l; t2m++) {
330 if(t2m->el_no > edx_max) break;
331 if(t2m->el_no < edx) continue;
332 best = t2m;
333 }
334 if(best) {
335 edx = best->el_no;
336 ctx->step = 1 + 2 * edx;
337 goto microphase2;
338 }
Lev Walkin4c36e302004-06-06 07:20:02 +0000339 }
340 n = opt_edx_end;
341 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000342 if(n == opt_edx_end) {
343 /*
344 * If tag is unknown, it may be either
345 * an unknown (thus, incorrect) tag,
346 * or an extension (...),
347 * or an end of the indefinite-length structure.
348 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000349 if(!IN_EXTENSION_GROUP(specs, edx)) {
Lev Walkin566a5672004-10-05 06:36:57 +0000350 ASN_DEBUG("Unexpected tag %s (at %d)",
351 ber_tlv_tag_string(tlv_tag), edx);
Lev Walkin4c36e302004-06-06 07:20:02 +0000352 ASN_DEBUG("Expected tag %s (%s)%s",
Lev Walkinf15320b2004-06-03 03:38:44 +0000353 ber_tlv_tag_string(elements[edx].tag),
Lev Walkin4c36e302004-06-06 07:20:02 +0000354 elements[edx].name,
Lev Walkinf15320b2004-06-03 03:38:44 +0000355 elements[edx].optional
356 ?" or alternatives":"");
357 RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000358 } else {
359 /* Skip this tag */
360 ssize_t skip;
361
Lev Walkinbaaa24f2004-09-29 14:19:14 +0000362 skip = ber_skip_length(opt_codec_ctx,
Lev Walkinf15320b2004-06-03 03:38:44 +0000363 BER_TLV_CONSTRUCTED(ptr),
Lev Walkin4d9528c2004-08-11 08:10:13 +0000364 (char *)ptr + tag_len, LEFT - tag_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000365 ASN_DEBUG("Skip length %d in %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000366 (int)skip, td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000367 switch(skip) {
368 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
369 /* Fall through */
370 case -1: RETURN(RC_FAIL);
371 }
372
373 ADVANCE(skip + tag_len);
374 ctx->step -= 2;
375 edx--;
376 continue; /* Try again with the next tag */
377 }
378 }
379
380 /*
381 * MICROPHASE 2: Invoke the member-specific decoder.
382 */
383 ctx->step |= 1; /* Confirm entering next microphase */
384 microphase2:
Lev Walkin449f8322004-08-20 13:23:42 +0000385 ASN_DEBUG("Inside SEQUENCE %s MF2", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000386
387 /*
388 * Compute the position of the member inside a structure,
389 * and also a type of containment (it may be contained
390 * as pointer or using inline inclusion).
391 */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000392 if(elements[edx].flags & ATF_POINTER) {
393 /* Member is a pointer to another structure */
Lev Walkinc2346572004-08-11 09:07:36 +0000394 memb_ptr2 = (void **)((char *)st + elements[edx].memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000395 } else {
396 /*
397 * A pointer to a pointer
398 * holding the start of the structure
399 */
400 memb_ptr = (char *)st + elements[edx].memb_offset;
401 memb_ptr2 = &memb_ptr;
402 }
403 /*
404 * Invoke the member fetch routine according to member's type
405 */
Lev Walkin5e033762004-09-29 13:26:15 +0000406 rval = elements[edx].type->ber_decoder(opt_codec_ctx,
Lev Walkinc2346572004-08-11 09:07:36 +0000407 elements[edx].type,
Lev Walkinf15320b2004-06-03 03:38:44 +0000408 memb_ptr2, ptr, LEFT,
409 elements[edx].tag_mode);
Lev Walkin60b7cff2004-09-04 04:44:11 +0000410 ASN_DEBUG("In %s SEQUENCE decoded %d %s of %d "
Lev Walkin5beb7de2004-09-24 20:59:27 +0000411 "in %d bytes rval.code %d, size=%d",
Lev Walkin449f8322004-08-20 13:23:42 +0000412 td->name, edx, elements[edx].type->name,
Lev Walkin5beb7de2004-09-24 20:59:27 +0000413 (int)LEFT, (int)rval.consumed, rval.code, (int)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000414 switch(rval.code) {
415 case RC_OK:
416 break;
417 case RC_WMORE: /* More data expected */
418 if(!SIZE_VIOLATION) {
419 ADVANCE(rval.consumed);
420 RETURN(RC_WMORE);
421 }
Lev Walkin5beb7de2004-09-24 20:59:27 +0000422 ASN_DEBUG("Size violation (c->l=%ld <= s=%ld)",
423 (long)ctx->left, (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000424 /* Fall through */
425 case RC_FAIL: /* Fatal error */
426 RETURN(RC_FAIL);
427 } /* switch(rval) */
428
429 ADVANCE(rval.consumed);
430 } /* for(all structure members) */
431
432 phase3:
433 ctx->phase = 3;
434 case 3: /* 00 and other tags expected */
435 case 4: /* only 00's expected */
436
437 ASN_DEBUG("SEQUENCE %s Leftover: %ld, size = %ld",
Lev Walkin449f8322004-08-20 13:23:42 +0000438 td->name, (long)ctx->left, (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000439
440 /*
441 * Skip everything until the end of the SEQUENCE.
442 */
443 while(ctx->left) {
444 ssize_t tl, ll;
445
446 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
447 switch(tl) {
448 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
449 /* Fall through */
450 case -1: RETURN(RC_FAIL);
451 }
452
453 /*
454 * If expected <0><0>...
455 */
456 if(ctx->left < 0
457 && ((uint8_t *)ptr)[0] == 0) {
458 if(LEFT < 2) {
459 if(SIZE_VIOLATION)
460 RETURN(RC_FAIL);
461 else
462 RETURN(RC_WMORE);
463 } else if(((uint8_t *)ptr)[1] == 0) {
464 /*
465 * Correctly finished with <0><0>.
466 */
467 ADVANCE(2);
468 ctx->left++;
469 ctx->phase = 4;
470 continue;
471 }
472 }
473
Lev Walkin449f8322004-08-20 13:23:42 +0000474 if(!IN_EXTENSION_GROUP(specs, td->elements_count)
Lev Walkinf15320b2004-06-03 03:38:44 +0000475 || ctx->phase == 4) {
476 ASN_DEBUG("Unexpected continuation "
477 "of a non-extensible type "
478 "%s (SEQUENCE): %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000479 td->name,
Lev Walkinf15320b2004-06-03 03:38:44 +0000480 ber_tlv_tag_string(tlv_tag));
481 RETURN(RC_FAIL);
482 }
483
Lev Walkinbaaa24f2004-09-29 14:19:14 +0000484 ll = ber_skip_length(opt_codec_ctx,
Lev Walkinf15320b2004-06-03 03:38:44 +0000485 BER_TLV_CONSTRUCTED(ptr),
Lev Walkin4d9528c2004-08-11 08:10:13 +0000486 (char *)ptr + tl, LEFT - tl);
Lev Walkinf15320b2004-06-03 03:38:44 +0000487 switch(ll) {
488 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
489 /* Fall through */
490 case -1: RETURN(RC_FAIL);
491 }
492
493 ADVANCE(tl + ll);
494 }
495
496 PHASE_OUT(ctx);
497 }
498
499 RETURN(RC_OK);
500}
501
Lev Walkin4c36e302004-06-06 07:20:02 +0000502
Lev Walkinf15320b2004-06-03 03:38:44 +0000503/*
504 * The DER encoder of the SEQUENCE type.
505 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000506asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000507SEQUENCE_encode_der(asn_TYPE_descriptor_t *td,
Lev Walkinf15320b2004-06-03 03:38:44 +0000508 void *ptr, int tag_mode, ber_tlv_tag_t tag,
509 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000510 size_t computed_size = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000511 asn_enc_rval_t erval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000512 ssize_t ret;
513 int edx;
514
515 ASN_DEBUG("%s %s as SEQUENCE",
Lev Walkin449f8322004-08-20 13:23:42 +0000516 cb?"Encoding":"Estimating", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000517
518 /*
519 * Gather the length of the underlying members sequence.
520 */
Lev Walkin449f8322004-08-20 13:23:42 +0000521 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000522 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000523 void *memb_ptr;
Lev Walkincc93b0f2004-09-10 09:18:20 +0000524 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000525 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
526 if(!memb_ptr) continue;
527 } else {
528 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
529 }
530 erval = elm->type->der_encoder(elm->type, memb_ptr,
531 elm->tag_mode, elm->tag,
532 0, 0);
533 if(erval.encoded == -1)
534 return erval;
535 computed_size += erval.encoded;
536 ASN_DEBUG("Member %d %s estimated %ld bytes",
537 edx, elm->name, (long)erval.encoded);
538 }
539
540 /*
541 * Encode the TLV for the sequence itself.
542 */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000543 ret = der_write_tags(td, computed_size, tag_mode, 1, tag, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +0000544 ASN_DEBUG("Wrote tags: %ld (+%ld)", (long)ret, (long)computed_size);
545 if(ret == -1) {
546 erval.encoded = -1;
Lev Walkin449f8322004-08-20 13:23:42 +0000547 erval.failed_type = td;
Lev Walkinf15320b2004-06-03 03:38:44 +0000548 erval.structure_ptr = ptr;
549 return erval;
550 }
551 erval.encoded = computed_size + ret;
552
553 if(!cb) return erval;
554
555 /*
556 * Encode all members.
557 */
Lev Walkin449f8322004-08-20 13:23:42 +0000558 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000559 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkina9cc46e2004-09-22 16:06:28 +0000560 asn_enc_rval_t tmperval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000561 void *memb_ptr;
562
Lev Walkincc93b0f2004-09-10 09:18:20 +0000563 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000564 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
565 if(!memb_ptr) continue;
566 } else {
567 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
568 }
569 tmperval = elm->type->der_encoder(elm->type, memb_ptr,
570 elm->tag_mode, elm->tag,
571 cb, app_key);
572 if(tmperval.encoded == -1)
573 return tmperval;
574 computed_size -= tmperval.encoded;
575 ASN_DEBUG("Member %d %s of SEQUENCE %s encoded in %d bytes",
Lev Walkin449f8322004-08-20 13:23:42 +0000576 edx, elm->name, td->name, tmperval.encoded);
Lev Walkinf15320b2004-06-03 03:38:44 +0000577 }
578
579 if(computed_size != 0) {
580 /*
581 * Encoded size is not equal to the computed size.
582 */
583 erval.encoded = -1;
Lev Walkin449f8322004-08-20 13:23:42 +0000584 erval.failed_type = td;
Lev Walkinf15320b2004-06-03 03:38:44 +0000585 erval.structure_ptr = ptr;
586 }
587
588 return erval;
589}
590
Lev Walkin1bbc2002004-10-23 13:27:30 +0000591#undef ADVANCE /* Just in case */
592#undef XER_ADVANCE
593#define XER_ADVANCE(num_bytes) do { \
594 size_t num = num_bytes; \
595 buf_ptr = ((char *)buf_ptr) + num; \
596 size -= num; \
597 consumed_myself += num; \
598 } while(0)
599
600asn_dec_rval_t
601SEQUENCE_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
602 void **struct_ptr, const char *opt_mname,
603 void *buf_ptr, size_t size) {
604 /*
605 * Bring closer parts of structure description.
606 */
607 asn_SEQUENCE_specifics_t *specs = (asn_SEQUENCE_specifics_t *)td->specifics;
608 asn_TYPE_member_t *elements = td->elements;
609 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
610
611 /*
612 * Parts of the structure being constructed.
613 */
614 void *st = *struct_ptr; /* Target structure. */
615 asn_struct_ctx_t *ctx; /* Decoder context */
616
617 asn_dec_rval_t rval;
618 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
619 int xer_state; /* XER low level parsing context */
620 int edx; /* Element index */
621
622 /*
623 * Create the target structure if it is not present already.
624 */
625 if(st == 0) {
626 st = *struct_ptr = CALLOC(1, specs->struct_size);
627 if(st == 0) RETURN(RC_FAIL);
628 }
629
630 /*
631 * Restore parsing context.
632 */
633 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
634
635
636 /*
637 * Phases of XER/XML processing:
638 * Phase 0: Check that the opening tag matches our expectations.
639 * Phase 1: Processing body and reacting on closing tag.
640 * Phase 2: Processing inner type.
641 */
642
643 if(ctx->phase > 2) RETURN(RC_FAIL);
644 for(xer_state = ctx->left, edx = ctx->step;;) {
645 pxer_chunk_type_e ch_type; /* XER chunk type */
646 ssize_t ch_size; /* Chunk size */
647 xer_check_tag_e tcv; /* Tag check value */
648 asn_TYPE_member_t *elm;
649
650 /*
651 * Go inside the member.
652 */
653 if(ctx->phase == 2) {
654 asn_dec_rval_t tmprval;
655 elm = &td->elements[edx];
656 void *memb_ptr; /* Pointer to the member */
657 void **memb_ptr2; /* Pointer to that pointer */
658
659 if(elm->flags & ATF_POINTER) {
660 /* Member is a pointer to another structure */
661 memb_ptr2 = (void **)((char *)st
662 + elm->memb_offset);
663 } else {
664 memb_ptr = (char *)st + elm->memb_offset;
665 memb_ptr2 = &memb_ptr;
666 }
667
668 tmprval = elm->type->xer_decoder(opt_codec_ctx,
669 elm->type, memb_ptr2, elm->name,
670 buf_ptr, size);
671 XER_ADVANCE(tmprval.consumed);
672 if(tmprval.code != RC_OK)
673 RETURN(tmprval.code);
674 ctx->phase = 1;
675 ctx->left = xer_state = 0; /* New, clean state */
676 ctx->step = ++edx;
677 /* Fall through */
678 }
679
680 /*
681 * Get the next part of the XML stream.
682 */
683 ch_size = xer_next_token(&xer_state, buf_ptr, size, &ch_type);
684 switch(ch_size) {
685 case -1: RETURN(RC_FAIL);
686 case 0:
687 ctx->left = xer_state;
688 RETURN(RC_WMORE);
689 default:
690 switch(ch_type) {
691 case PXER_COMMENT: /* Got XML comment */
692 case PXER_TEXT: /* Ignore free-standing text */
693 XER_ADVANCE(ch_size); /* Skip silently */
694 continue;
695 case PXER_TAG:
696 break; /* Check the rest down there */
697 }
698 }
699
700 tcv = xer_check_tag(buf_ptr, size, xml_tag);
701 switch(tcv) {
702 case XCT_CLOSING:
703 if(ctx->phase == 0) break;
704 XER_ADVANCE(ch_size);
705 ctx->phase = 0;
706 /* Fall through */
707 case XCT_BOTH:
708 if(ctx->phase == 0) {
709 if(edx >= td->elements_count
710 ||
711 /* Explicit OPTIONAL specs reaches the end */
712 (edx + elements[edx].optional
713 == td->elements_count)
714 ||
715 /* All extensions are optional */
716 (IN_EXTENSION_GROUP(specs, edx)
717 && specs->ext_before
718 > td->elements_count)
719 ) {
720 XER_ADVANCE(ch_size);
721 ctx->phase = 3; /* Phase out */
722 continue;
723 } else {
724 ASN_DEBUG("Premature end of XER SEQUENCE");
725 RETURN(RC_FAIL);
726 }
727 }
728 /* Fall through */
729 case XCT_OPENING:
730 if(ctx->phase == 0) {
731 XER_ADVANCE(ch_size);
732 ctx->phase = 1; /* Processing body phase */
733 continue;
734 }
735 case XCT_UNEXPECTED: {
736 int edx_end;
737 int n;
738
739 if(!ctx->phase
740 || edx >= td->elements_count
741 || !IN_EXTENSION_GROUP(specs, td->elements_count))
742 break; /* Really unexpected */
743
744 /*
745 * Search which member corresponds to this tag.
746 */
747 edx_end = edx + elements[edx].optional + 1;
748 for(n = edx; n < edx_end; n++) {
749 elm = &td->elements[n];
750 tcv = xer_check_tag(buf_ptr, size, elm->name);
751 switch(tcv) {
752 case XCT_BOTH:
753 case XCT_OPENING:
754 /*
755 * Process this member.
756 */
757 ctx->step = edx = n;
758 ctx->phase = 2;
759 break;
760 case XCT_UNEXPECTED:
761 continue; /* or continue; */
762 case XCT_CLOSING:
763 default:
764 n = edx_end;
765 break; /* Phase out */
766 }
767 break;
768 }
769 if(n == edx_end) break;
770 continue;
771 }
772 default:
773 break;
774 }
775
776 ASN_DEBUG("Unexpected XML tag in SEQUENCE");
777 break;
778 }
779
780 ctx->phase = 3; /* Phase out, just in case */
781 RETURN(RC_FAIL);
782}
783
Lev Walkina9cc46e2004-09-22 16:06:28 +0000784asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000785SEQUENCE_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000786 int ilevel, enum xer_encoder_flags_e flags,
787 asn_app_consume_bytes_f *cb, void *app_key) {
788 asn_enc_rval_t er;
789 int xcan = (flags & XER_F_CANONICAL);
790 int edx;
791
792 if(!sptr)
793 _ASN_ENCODE_FAILED;
794
795 er.encoded = 0;
796
797 for(edx = 0; edx < td->elements_count; edx++) {
798 asn_enc_rval_t tmper;
Lev Walkin5e033762004-09-29 13:26:15 +0000799 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkina9cc46e2004-09-22 16:06:28 +0000800 void *memb_ptr;
801 const char *mname = elm->name;
802 unsigned int mlen = strlen(mname);
803
804 if(elm->flags & ATF_POINTER) {
805 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
806 if(!memb_ptr) continue; /* OPTIONAL element? */
807 } else {
808 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
809 }
810
811 if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel);
812 _ASN_CALLBACK3("<", 1, mname, mlen, ">", 1);
813
814 /* Print the member itself */
815 tmper = elm->type->xer_encoder(elm->type, memb_ptr,
816 ilevel + 1, flags, cb, app_key);
817 if(tmper.encoded == -1) return tmper;
818
819 _ASN_CALLBACK3("</", 2, mname, mlen, ">", 1);
820 er.encoded += 5 + (2 * mlen) + tmper.encoded;
821 }
822
823 if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel - 1);
824
825 return er;
Lev Walkin942fd082004-10-03 09:13:02 +0000826cb_failed:
827 _ASN_ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000828}
829
Lev Walkinf15320b2004-06-03 03:38:44 +0000830int
Lev Walkin5e033762004-09-29 13:26:15 +0000831SEQUENCE_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000832 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000833 int edx;
834 int ret;
835
Lev Walkin8e8078a2004-09-26 13:10:40 +0000836 if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000837
838 /* Dump preamble */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000839 if(cb(td->name, strlen(td->name), app_key) < 0
840 || cb(" ::= {", 6, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000841 return -1;
842
Lev Walkin449f8322004-08-20 13:23:42 +0000843 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000844 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000845 const void *memb_ptr;
846
Lev Walkincc93b0f2004-09-10 09:18:20 +0000847 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000848 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
849 if(!memb_ptr) continue;
850 } else {
851 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
852 }
853
854 /* Indentation */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000855 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000856
857 /* Print the member's name and stuff */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000858 if(cb(elm->name, strlen(elm->name), app_key) < 0
859 || cb(": ", 2, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000860 return -1;
861
862 /* Print the member itself */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000863 ret = elm->type->print_struct(elm->type, memb_ptr, ilevel + 1,
Lev Walkinf15320b2004-06-03 03:38:44 +0000864 cb, app_key);
865 if(ret) return ret;
Lev Walkinf15320b2004-06-03 03:38:44 +0000866 }
867
Lev Walkin8e8078a2004-09-26 13:10:40 +0000868 ilevel--;
869 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000870
Lev Walkin8e8078a2004-09-26 13:10:40 +0000871 return (cb("}", 1, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000872}
873
874void
Lev Walkin5e033762004-09-29 13:26:15 +0000875SEQUENCE_free(asn_TYPE_descriptor_t *td, void *sptr, int contents_only) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000876 int edx;
877
878 if(!td || !sptr)
879 return;
880
881 ASN_DEBUG("Freeing %s as SEQUENCE", td->name);
882
Lev Walkin449f8322004-08-20 13:23:42 +0000883 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000884 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000885 void *memb_ptr;
Lev Walkincc93b0f2004-09-10 09:18:20 +0000886 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000887 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
888 if(memb_ptr)
889 elm->type->free_struct(elm->type, memb_ptr, 0);
890 } else {
891 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
892 elm->type->free_struct(elm->type, memb_ptr, 1);
893 }
894 }
895
896 if(!contents_only) {
897 FREEMEM(sptr);
898 }
899}
900
901int
Lev Walkin5e033762004-09-29 13:26:15 +0000902SEQUENCE_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000903 asn_app_consume_bytes_f *app_errlog, void *app_key) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000904 int edx;
905
906 if(!sptr) {
Lev Walkinba4e5182004-08-11 09:44:13 +0000907 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkin16835b62004-08-22 13:47:59 +0000908 "%s: value not given (%s:%d)",
909 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000910 return -1;
911 }
912
913 /*
914 * Iterate over structure members and check their validity.
915 */
Lev Walkin449f8322004-08-20 13:23:42 +0000916 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000917 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000918 const void *memb_ptr;
919
Lev Walkincc93b0f2004-09-10 09:18:20 +0000920 if(elm->flags & ATF_POINTER) {
Lev Walkin4c36e302004-06-06 07:20:02 +0000921 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000922 if(!memb_ptr) continue;
923 } else {
924 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
925 }
926
Lev Walkin449f8322004-08-20 13:23:42 +0000927 if(elm->memb_constraints) {
928 int ret = elm->memb_constraints(elm->type, memb_ptr,
929 app_errlog, app_key);
930 if(ret) return ret;
931 } else {
932 int ret = elm->type->check_constraints(elm->type,
933 memb_ptr, app_errlog, app_key);
934 if(ret) return ret;
935 /*
936 * Cannot inherit it earlier:
937 * need to make sure we get the updated version.
938 */
939 elm->memb_constraints = elm->type->check_constraints;
940 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000941 }
942
943 return 0;
944}