blob: b00ae6fa8fa54189c7edad1ff3fefd9a49285327 [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 Walkinc2346572004-08-11 09:07:36 +000077 const asn1_TYPE_tag2member_t *a = (const asn1_TYPE_tag2member_t *)ap;
78 const asn1_TYPE_tag2member_t *b = (const asn1_TYPE_tag2member_t *)bp;
79
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 Walkin449f8322004-08-20 13:23:42 +0000111SEQUENCE_decode_ber(asn1_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 Walkin449f8322004-08-20 13:23:42 +0000116 asn1_SEQUENCE_specifics_t *specs = (asn1_SEQUENCE_specifics_t *)td->specifics;
117 asn1_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. */
123 ber_dec_ctx_t *ctx; /* Decoder context */
124
125 ber_tlv_tag_t tlv_tag; /* T from TLV */
126 //ber_tlv_len_t tlv_len; /* L from TLV */
127 ber_dec_rval_t rval; /* Return code from subparsers */
128
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 Walkin4d9528c2004-08-11 08:10:13 +0000147 ctx = (ber_dec_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 Walkin449f8322004-08-20 13:23:42 +0000160 rval = ber_check_tags(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 Walkinf15320b2004-06-03 03:38:44 +0000165 consumed_myself += rval.consumed;
166 RETURN(rval.code);
167 }
168
169 if(ctx->left >= 0)
170 ctx->left += rval.consumed; /* ?Substracted below! */
171 ADVANCE(rval.consumed);
172
173 NEXT_PHASE(ctx);
174
175 ASN_DEBUG("Structure consumes %ld bytes, buffer %ld",
176 (long)ctx->left, (long)size);
177
178 /* Fall through */
179 case 1:
180 /*
181 * PHASE 1.
182 * From the place where we've left it previously,
183 * try to decode the next member from the list of
184 * this structure's elements.
185 * (ctx->step) stores the member being processed
186 * between invocations and the microphase {0,1} of parsing
187 * that member:
188 * step = (<member_number> * 2 + <microphase>).
189 */
Lev Walkin449f8322004-08-20 13:23:42 +0000190 for(edx = (ctx->step >> 1); edx < td->elements_count;
Lev Walkinf15320b2004-06-03 03:38:44 +0000191 edx++, ctx->step = (ctx->step & ~1) + 2) {
192 void *memb_ptr; /* Pointer to the member */
Lev Walkinc2346572004-08-11 09:07:36 +0000193 void **memb_ptr2; /* Pointer to that pointer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000194 ssize_t tag_len; /* Length of TLV's T */
195 int opt_edx_end; /* Next non-optional element */
Lev Walkin4c36e302004-06-06 07:20:02 +0000196 int use_bsearch;
Lev Walkinf15320b2004-06-03 03:38:44 +0000197 int n;
198
199 if(ctx->step & 1)
200 goto microphase2;
201
202 /*
203 * MICROPHASE 1: Synchronize decoding.
204 */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000205 ASN_DEBUG("In %s SEQUENCE left %d, edx=%d flags=%d"
206 " opt=%d ec=%d",
207 td->name, (int)ctx->left, edx,
208 elements[edx].flags, elements[edx].optional,
209 td->elements_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000210
211 if(ctx->left == 0 /* No more stuff is expected */
212 && (
213 /* Explicit OPTIONAL specification reaches the end */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000214 (edx + elements[edx].optional
215 == td->elements_count)
Lev Walkinf15320b2004-06-03 03:38:44 +0000216 ||
217 /* All extensions are optional */
218 (IN_EXTENSION_GROUP(specs, edx)
Lev Walkin449f8322004-08-20 13:23:42 +0000219 && specs->ext_before > td->elements_count)
Lev Walkinf15320b2004-06-03 03:38:44 +0000220 )
221 ) {
Lev Walkin449f8322004-08-20 13:23:42 +0000222 ASN_DEBUG("End of SEQUENCE %s", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000223 /*
224 * Found the legitimate end of the structure.
225 */
226 PHASE_OUT(ctx);
227 RETURN(RC_OK);
228 }
229
230 /*
231 * Fetch the T from TLV.
232 */
233 tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
Lev Walkin906654e2004-09-10 15:49:15 +0000234 ASN_DEBUG("Current tag in %s SEQUENCE for element %d "
Lev Walkincc6a9102004-09-23 22:06:26 +0000235 "(%s) is %s encoded in %d bytes, of frame %ld",
Lev Walkin906654e2004-09-10 15:49:15 +0000236 td->name, edx, elements[edx].name,
237 ber_tlv_tag_string(tlv_tag), (int)tag_len, (long)LEFT);
Lev Walkinf15320b2004-06-03 03:38:44 +0000238 switch(tag_len) {
239 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
240 /* Fall through */
241 case -1: RETURN(RC_FAIL);
242 }
243
244 /*
245 * Find the next available type with this tag.
246 */
Lev Walkin4c36e302004-06-06 07:20:02 +0000247 use_bsearch = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000248 opt_edx_end = edx + elements[edx].optional + 1;
Lev Walkin449f8322004-08-20 13:23:42 +0000249 if(opt_edx_end > td->elements_count)
250 opt_edx_end = td->elements_count; /* Cap */
Lev Walkinf9dbd9a2004-07-01 00:48:04 +0000251 else if(opt_edx_end - edx > 8) {
Lev Walkin924fa032004-06-14 13:42:23 +0000252 /* Limit the scope of linear search... */
Lev Walkinf9dbd9a2004-07-01 00:48:04 +0000253 opt_edx_end = edx + 8;
Lev Walkin4c36e302004-06-06 07:20:02 +0000254 use_bsearch = 1;
Lev Walkin924fa032004-06-14 13:42:23 +0000255 /* ... and resort to bsearch() */
Lev Walkin4c36e302004-06-06 07:20:02 +0000256 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000257 for(n = edx; n < opt_edx_end; n++) {
258 if(BER_TAGS_EQUAL(tlv_tag, elements[n].tag)) {
259 /*
260 * Found element corresponding to the tag
261 * being looked at.
262 * Reposition over the right element.
263 */
264 edx = n;
Lev Walkin4c36e302004-06-06 07:20:02 +0000265 ctx->step = 1 + 2 * edx; /* Remember! */
266 goto microphase2;
Lev Walkinb9189732004-09-10 09:37:12 +0000267 } else if(elements[n].flags & ATF_OPEN_TYPE) {
268 /*
269 * This is the ANY type, which may bear
270 * any flag whatsoever.
271 */
272 edx = n;
273 ctx->step = 1 + 2 * edx; /* Remember! */
274 goto microphase2;
Lev Walkin4c36e302004-06-06 07:20:02 +0000275 } else if(elements[n].tag == (ber_tlv_tag_t)-1) {
276 use_bsearch = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000277 break;
278 }
279 }
Lev Walkin4c36e302004-06-06 07:20:02 +0000280 if(use_bsearch) {
281 /*
Lev Walkin4e0704f2004-09-04 06:17:35 +0000282 * Resort to a binary search over
Lev Walkin4c36e302004-06-06 07:20:02 +0000283 * sorted array of tags.
284 */
285 asn1_TYPE_tag2member_t *t2m;
286 asn1_TYPE_tag2member_t key;
287 key.el_tag = tlv_tag;
Lev Walkin924fa032004-06-14 13:42:23 +0000288 key.el_no = edx;
Lev Walkinc2346572004-08-11 09:07:36 +0000289 (void *)t2m = bsearch(&key,
290 specs->tag2el, specs->tag2el_count,
Lev Walkin4c36e302004-06-06 07:20:02 +0000291 sizeof(specs->tag2el[0]), _t2e_cmp);
Lev Walkin924fa032004-06-14 13:42:23 +0000292 if(t2m) {
293 asn1_TYPE_tag2member_t *best = 0;
294 asn1_TYPE_tag2member_t *t2m_f, *t2m_l;
295 int edx_max = edx + elements[edx].optional;
Lev Walkin4c36e302004-06-06 07:20:02 +0000296 /*
297 * Rewind to the first element with that tag,
298 * `cause bsearch() does not guarantee order.
299 */
Lev Walkin924fa032004-06-14 13:42:23 +0000300 t2m_f = t2m + t2m->toff_first;
301 t2m_l = t2m + t2m->toff_last;
302 for(t2m = t2m_f; t2m <= t2m_l; t2m++) {
303 if(t2m->el_no > edx_max) break;
304 if(t2m->el_no < edx) continue;
305 best = t2m;
306 }
307 if(best) {
308 edx = best->el_no;
309 ctx->step = 1 + 2 * edx;
310 goto microphase2;
311 }
Lev Walkin4c36e302004-06-06 07:20:02 +0000312 }
313 n = opt_edx_end;
314 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000315 if(n == opt_edx_end) {
316 /*
317 * If tag is unknown, it may be either
318 * an unknown (thus, incorrect) tag,
319 * or an extension (...),
320 * or an end of the indefinite-length structure.
321 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000322 if(!IN_EXTENSION_GROUP(specs, edx)) {
323 ASN_DEBUG("Unexpected tag %s",
324 ber_tlv_tag_string(tlv_tag));
Lev Walkin4c36e302004-06-06 07:20:02 +0000325 ASN_DEBUG("Expected tag %s (%s)%s",
Lev Walkinf15320b2004-06-03 03:38:44 +0000326 ber_tlv_tag_string(elements[edx].tag),
Lev Walkin4c36e302004-06-06 07:20:02 +0000327 elements[edx].name,
Lev Walkinf15320b2004-06-03 03:38:44 +0000328 elements[edx].optional
329 ?" or alternatives":"");
330 RETURN(RC_FAIL);
331 }
332
333 if(ctx->left < 0
334 && ((uint8_t *)ptr)[0] == 0) {
335 if(LEFT < 2) {
336 if(SIZE_VIOLATION)
337 RETURN(RC_FAIL);
338 else
339 RETURN(RC_WMORE);
340 } else if(((uint8_t *)ptr)[1] == 0) {
341 /*
342 * Yeah, baby! Found the terminator
343 * of the indefinite length structure.
344 */
345 /*
346 * Proceed to the canonical
347 * finalization function.
348 * No advancing is necessary.
349 */
350 goto phase3;
351 }
352 } else {
353 /* Skip this tag */
354 ssize_t skip;
355
356 skip = ber_skip_length(
357 BER_TLV_CONSTRUCTED(ptr),
Lev Walkin4d9528c2004-08-11 08:10:13 +0000358 (char *)ptr + tag_len, LEFT - tag_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000359 ASN_DEBUG("Skip length %d in %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000360 (int)skip, td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000361 switch(skip) {
362 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
363 /* Fall through */
364 case -1: RETURN(RC_FAIL);
365 }
366
367 ADVANCE(skip + tag_len);
368 ctx->step -= 2;
369 edx--;
370 continue; /* Try again with the next tag */
371 }
372 }
373
374 /*
375 * MICROPHASE 2: Invoke the member-specific decoder.
376 */
377 ctx->step |= 1; /* Confirm entering next microphase */
378 microphase2:
Lev Walkin449f8322004-08-20 13:23:42 +0000379 ASN_DEBUG("Inside SEQUENCE %s MF2", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000380
381 /*
382 * Compute the position of the member inside a structure,
383 * and also a type of containment (it may be contained
384 * as pointer or using inline inclusion).
385 */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000386 if(elements[edx].flags & ATF_POINTER) {
387 /* Member is a pointer to another structure */
Lev Walkinc2346572004-08-11 09:07:36 +0000388 memb_ptr2 = (void **)((char *)st + elements[edx].memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000389 } else {
390 /*
391 * A pointer to a pointer
392 * holding the start of the structure
393 */
394 memb_ptr = (char *)st + elements[edx].memb_offset;
395 memb_ptr2 = &memb_ptr;
396 }
397 /*
398 * Invoke the member fetch routine according to member's type
399 */
400 rval = elements[edx].type->ber_decoder(
Lev Walkinc2346572004-08-11 09:07:36 +0000401 elements[edx].type,
Lev Walkinf15320b2004-06-03 03:38:44 +0000402 memb_ptr2, ptr, LEFT,
403 elements[edx].tag_mode);
Lev Walkin60b7cff2004-09-04 04:44:11 +0000404 ASN_DEBUG("In %s SEQUENCE decoded %d %s of %d "
Lev Walkin5beb7de2004-09-24 20:59:27 +0000405 "in %d bytes rval.code %d, size=%d",
Lev Walkin449f8322004-08-20 13:23:42 +0000406 td->name, edx, elements[edx].type->name,
Lev Walkin5beb7de2004-09-24 20:59:27 +0000407 (int)LEFT, (int)rval.consumed, rval.code, (int)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000408 switch(rval.code) {
409 case RC_OK:
410 break;
411 case RC_WMORE: /* More data expected */
412 if(!SIZE_VIOLATION) {
413 ADVANCE(rval.consumed);
414 RETURN(RC_WMORE);
415 }
Lev Walkin5beb7de2004-09-24 20:59:27 +0000416 ASN_DEBUG("Size violation (c->l=%ld <= s=%ld)",
417 (long)ctx->left, (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000418 /* Fall through */
419 case RC_FAIL: /* Fatal error */
420 RETURN(RC_FAIL);
421 } /* switch(rval) */
422
423 ADVANCE(rval.consumed);
424 } /* for(all structure members) */
425
426 phase3:
427 ctx->phase = 3;
428 case 3: /* 00 and other tags expected */
429 case 4: /* only 00's expected */
430
431 ASN_DEBUG("SEQUENCE %s Leftover: %ld, size = %ld",
Lev Walkin449f8322004-08-20 13:23:42 +0000432 td->name, (long)ctx->left, (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000433
434 /*
435 * Skip everything until the end of the SEQUENCE.
436 */
437 while(ctx->left) {
438 ssize_t tl, ll;
439
440 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
441 switch(tl) {
442 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
443 /* Fall through */
444 case -1: RETURN(RC_FAIL);
445 }
446
447 /*
448 * If expected <0><0>...
449 */
450 if(ctx->left < 0
451 && ((uint8_t *)ptr)[0] == 0) {
452 if(LEFT < 2) {
453 if(SIZE_VIOLATION)
454 RETURN(RC_FAIL);
455 else
456 RETURN(RC_WMORE);
457 } else if(((uint8_t *)ptr)[1] == 0) {
458 /*
459 * Correctly finished with <0><0>.
460 */
461 ADVANCE(2);
462 ctx->left++;
463 ctx->phase = 4;
464 continue;
465 }
466 }
467
Lev Walkin449f8322004-08-20 13:23:42 +0000468 if(!IN_EXTENSION_GROUP(specs, td->elements_count)
Lev Walkinf15320b2004-06-03 03:38:44 +0000469 || ctx->phase == 4) {
470 ASN_DEBUG("Unexpected continuation "
471 "of a non-extensible type "
472 "%s (SEQUENCE): %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000473 td->name,
Lev Walkinf15320b2004-06-03 03:38:44 +0000474 ber_tlv_tag_string(tlv_tag));
475 RETURN(RC_FAIL);
476 }
477
478 ll = ber_skip_length(
479 BER_TLV_CONSTRUCTED(ptr),
Lev Walkin4d9528c2004-08-11 08:10:13 +0000480 (char *)ptr + tl, LEFT - tl);
Lev Walkinf15320b2004-06-03 03:38:44 +0000481 switch(ll) {
482 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
483 /* Fall through */
484 case -1: RETURN(RC_FAIL);
485 }
486
487 ADVANCE(tl + ll);
488 }
489
490 PHASE_OUT(ctx);
491 }
492
493 RETURN(RC_OK);
494}
495
Lev Walkin4c36e302004-06-06 07:20:02 +0000496
Lev Walkinf15320b2004-06-03 03:38:44 +0000497/*
498 * The DER encoder of the SEQUENCE type.
499 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000500asn_enc_rval_t
Lev Walkin449f8322004-08-20 13:23:42 +0000501SEQUENCE_encode_der(asn1_TYPE_descriptor_t *td,
Lev Walkinf15320b2004-06-03 03:38:44 +0000502 void *ptr, int tag_mode, ber_tlv_tag_t tag,
503 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000504 size_t computed_size = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000505 asn_enc_rval_t erval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000506 ssize_t ret;
507 int edx;
508
509 ASN_DEBUG("%s %s as SEQUENCE",
Lev Walkin449f8322004-08-20 13:23:42 +0000510 cb?"Encoding":"Estimating", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000511
512 /*
513 * Gather the length of the underlying members sequence.
514 */
Lev Walkin449f8322004-08-20 13:23:42 +0000515 for(edx = 0; edx < td->elements_count; edx++) {
516 asn1_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000517 void *memb_ptr;
Lev Walkincc93b0f2004-09-10 09:18:20 +0000518 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000519 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
520 if(!memb_ptr) continue;
521 } else {
522 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
523 }
524 erval = elm->type->der_encoder(elm->type, memb_ptr,
525 elm->tag_mode, elm->tag,
526 0, 0);
527 if(erval.encoded == -1)
528 return erval;
529 computed_size += erval.encoded;
530 ASN_DEBUG("Member %d %s estimated %ld bytes",
531 edx, elm->name, (long)erval.encoded);
532 }
533
534 /*
535 * Encode the TLV for the sequence itself.
536 */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000537 ret = der_write_tags(td, computed_size, tag_mode, 1, tag, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +0000538 ASN_DEBUG("Wrote tags: %ld (+%ld)", (long)ret, (long)computed_size);
539 if(ret == -1) {
540 erval.encoded = -1;
Lev Walkin449f8322004-08-20 13:23:42 +0000541 erval.failed_type = td;
Lev Walkinf15320b2004-06-03 03:38:44 +0000542 erval.structure_ptr = ptr;
543 return erval;
544 }
545 erval.encoded = computed_size + ret;
546
547 if(!cb) return erval;
548
549 /*
550 * Encode all members.
551 */
Lev Walkin449f8322004-08-20 13:23:42 +0000552 for(edx = 0; edx < td->elements_count; edx++) {
553 asn1_TYPE_member_t *elm = &td->elements[edx];
Lev Walkina9cc46e2004-09-22 16:06:28 +0000554 asn_enc_rval_t tmperval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000555 void *memb_ptr;
556
Lev Walkincc93b0f2004-09-10 09:18:20 +0000557 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000558 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
559 if(!memb_ptr) continue;
560 } else {
561 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
562 }
563 tmperval = elm->type->der_encoder(elm->type, memb_ptr,
564 elm->tag_mode, elm->tag,
565 cb, app_key);
566 if(tmperval.encoded == -1)
567 return tmperval;
568 computed_size -= tmperval.encoded;
569 ASN_DEBUG("Member %d %s of SEQUENCE %s encoded in %d bytes",
Lev Walkin449f8322004-08-20 13:23:42 +0000570 edx, elm->name, td->name, tmperval.encoded);
Lev Walkinf15320b2004-06-03 03:38:44 +0000571 }
572
573 if(computed_size != 0) {
574 /*
575 * Encoded size is not equal to the computed size.
576 */
577 erval.encoded = -1;
Lev Walkin449f8322004-08-20 13:23:42 +0000578 erval.failed_type = td;
Lev Walkinf15320b2004-06-03 03:38:44 +0000579 erval.structure_ptr = ptr;
580 }
581
582 return erval;
583}
584
Lev Walkina9cc46e2004-09-22 16:06:28 +0000585asn_enc_rval_t
586SEQUENCE_encode_xer(asn1_TYPE_descriptor_t *td, void *sptr,
587 int ilevel, enum xer_encoder_flags_e flags,
588 asn_app_consume_bytes_f *cb, void *app_key) {
589 asn_enc_rval_t er;
590 int xcan = (flags & XER_F_CANONICAL);
591 int edx;
592
593 if(!sptr)
594 _ASN_ENCODE_FAILED;
595
596 er.encoded = 0;
597
598 for(edx = 0; edx < td->elements_count; edx++) {
599 asn_enc_rval_t tmper;
600 asn1_TYPE_member_t *elm = &td->elements[edx];
601 void *memb_ptr;
602 const char *mname = elm->name;
603 unsigned int mlen = strlen(mname);
604
605 if(elm->flags & ATF_POINTER) {
606 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
607 if(!memb_ptr) continue; /* OPTIONAL element? */
608 } else {
609 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
610 }
611
612 if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel);
613 _ASN_CALLBACK3("<", 1, mname, mlen, ">", 1);
614
615 /* Print the member itself */
616 tmper = elm->type->xer_encoder(elm->type, memb_ptr,
617 ilevel + 1, flags, cb, app_key);
618 if(tmper.encoded == -1) return tmper;
619
620 _ASN_CALLBACK3("</", 2, mname, mlen, ">", 1);
621 er.encoded += 5 + (2 * mlen) + tmper.encoded;
622 }
623
624 if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel - 1);
625
626 return er;
627}
628
Lev Walkinf15320b2004-06-03 03:38:44 +0000629int
630SEQUENCE_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
631 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000632 int edx;
633 int ret;
634
Lev Walkin8e8078a2004-09-26 13:10:40 +0000635 if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000636
637 /* Dump preamble */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000638 if(cb(td->name, strlen(td->name), app_key) < 0
639 || cb(" ::= {", 6, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000640 return -1;
641
Lev Walkin449f8322004-08-20 13:23:42 +0000642 for(edx = 0; edx < td->elements_count; edx++) {
643 asn1_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000644 const void *memb_ptr;
645
Lev Walkincc93b0f2004-09-10 09:18:20 +0000646 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000647 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
648 if(!memb_ptr) continue;
649 } else {
650 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
651 }
652
653 /* Indentation */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000654 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000655
656 /* Print the member's name and stuff */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000657 if(cb(elm->name, strlen(elm->name), app_key) < 0
658 || cb(": ", 2, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000659 return -1;
660
661 /* Print the member itself */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000662 ret = elm->type->print_struct(elm->type, memb_ptr, ilevel + 1,
Lev Walkinf15320b2004-06-03 03:38:44 +0000663 cb, app_key);
664 if(ret) return ret;
Lev Walkinf15320b2004-06-03 03:38:44 +0000665 }
666
Lev Walkin8e8078a2004-09-26 13:10:40 +0000667 ilevel--;
668 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000669
Lev Walkin8e8078a2004-09-26 13:10:40 +0000670 return (cb("}", 1, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000671}
672
673void
674SEQUENCE_free(asn1_TYPE_descriptor_t *td, void *sptr, int contents_only) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000675 int edx;
676
677 if(!td || !sptr)
678 return;
679
680 ASN_DEBUG("Freeing %s as SEQUENCE", td->name);
681
Lev Walkin449f8322004-08-20 13:23:42 +0000682 for(edx = 0; edx < td->elements_count; edx++) {
683 asn1_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000684 void *memb_ptr;
Lev Walkincc93b0f2004-09-10 09:18:20 +0000685 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000686 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
687 if(memb_ptr)
688 elm->type->free_struct(elm->type, memb_ptr, 0);
689 } else {
690 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
691 elm->type->free_struct(elm->type, memb_ptr, 1);
692 }
693 }
694
695 if(!contents_only) {
696 FREEMEM(sptr);
697 }
698}
699
700int
701SEQUENCE_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
702 asn_app_consume_bytes_f *app_errlog, void *app_key) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000703 int edx;
704
705 if(!sptr) {
Lev Walkinba4e5182004-08-11 09:44:13 +0000706 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkin16835b62004-08-22 13:47:59 +0000707 "%s: value not given (%s:%d)",
708 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000709 return -1;
710 }
711
712 /*
713 * Iterate over structure members and check their validity.
714 */
Lev Walkin449f8322004-08-20 13:23:42 +0000715 for(edx = 0; edx < td->elements_count; edx++) {
716 asn1_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000717 const void *memb_ptr;
718
Lev Walkincc93b0f2004-09-10 09:18:20 +0000719 if(elm->flags & ATF_POINTER) {
Lev Walkin4c36e302004-06-06 07:20:02 +0000720 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000721 if(!memb_ptr) continue;
722 } else {
723 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
724 }
725
Lev Walkin449f8322004-08-20 13:23:42 +0000726 if(elm->memb_constraints) {
727 int ret = elm->memb_constraints(elm->type, memb_ptr,
728 app_errlog, app_key);
729 if(ret) return ret;
730 } else {
731 int ret = elm->type->check_constraints(elm->type,
732 memb_ptr, app_errlog, app_key);
733 if(ret) return ret;
734 /*
735 * Cannot inherit it earlier:
736 * need to make sure we get the updated version.
737 */
738 elm->memb_constraints = elm->type->check_constraints;
739 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000740 }
741
742 return 0;
743}