blob: 649f53b723276e97525923a33483c4773c5c1ae4 [file] [log] [blame]
vlmfa67ddc2004-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 */
5#include <constr_SEQUENCE.h>
vlm31473082004-06-06 07:20:02 +00006#include <assert.h>
vlmfa67ddc2004-06-03 03:38:44 +00007
8/*
9 * Number of bytes left for this structure.
10 * (ctx->left) indicates the number of bytes _transferred_ for the structure.
11 * (size) contains the number of bytes in the buffer passed.
12 */
vlmc5190612004-08-18 04:53:32 +000013#define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left)
vlmfa67ddc2004-06-03 03:38:44 +000014
15/*
16 * If the subprocessor function returns with an indication that it wants
17 * more data, it may well be a fatal decoding problem, because the
18 * size is constrained by the <TLV>'s L, even if the buffer size allows
19 * reading more data.
20 * For example, consider the buffer containing the following TLVs:
21 * <T:5><L:1><V> <T:6>...
22 * The TLV length clearly indicates that one byte is expected in V, but
23 * if the V processor returns with "want more data" even if the buffer
24 * contains way more data than the V processor have seen.
25 */
vlmb42843a2004-06-05 08:17:50 +000026#define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size)
vlmfa67ddc2004-06-03 03:38:44 +000027
28/*
29 * This macro "eats" the part of the buffer which is definitely "consumed",
30 * i.e. was correctly converted into local representation or rightfully skipped.
31 */
32#define ADVANCE(num_bytes) do { \
33 size_t num = num_bytes; \
vlmd86c9252004-08-25 01:34:11 +000034 ptr = ((char *)ptr) + num; \
vlmfa67ddc2004-06-03 03:38:44 +000035 size -= num; \
36 if(ctx->left >= 0) \
37 ctx->left -= num; \
38 consumed_myself += num; \
39 } while(0)
40
41/*
42 * Switch to the next phase of parsing.
43 */
44#define NEXT_PHASE(ctx) do { \
45 ctx->phase++; \
46 ctx->step = 0; \
47 } while(0)
48#define PHASE_OUT(ctx) do { ctx->phase = 10; } while(0)
49
50/*
51 * Return a standardized complex structure.
52 */
53#define RETURN(_code) do { \
54 rval.code = _code; \
55 rval.consumed = consumed_myself;\
56 return rval; \
57 } while(0)
58
59/*
60 * Check whether we are inside the extensions group.
61 */
62#define IN_EXTENSION_GROUP(specs, memb_idx) \
63 ( ((memb_idx) > (specs)->ext_after) \
64 &&((memb_idx) < (specs)->ext_before))
65
vlm31473082004-06-06 07:20:02 +000066
67/*
68 * Tags are canonically sorted in the tag2element map.
69 */
70static int
71_t2e_cmp(const void *ap, const void *bp) {
vlmda674682004-08-11 09:07:36 +000072 const asn1_TYPE_tag2member_t *a = (const asn1_TYPE_tag2member_t *)ap;
73 const asn1_TYPE_tag2member_t *b = (const asn1_TYPE_tag2member_t *)bp;
74
vlm31473082004-06-06 07:20:02 +000075 int a_class = BER_TAG_CLASS(a->el_tag);
76 int b_class = BER_TAG_CLASS(b->el_tag);
77
78 if(a_class == b_class) {
79 ber_tlv_tag_t a_value = BER_TAG_VALUE(a->el_tag);
80 ber_tlv_tag_t b_value = BER_TAG_VALUE(b->el_tag);
81
vlm1692da02004-06-14 13:42:23 +000082 if(a_value == b_value) {
vlme4ee6ee2004-06-14 14:11:14 +000083 if(a->el_no > b->el_no)
84 return 1;
vlm1692da02004-06-14 13:42:23 +000085 /*
86 * Important: we do not check
vlme4ee6ee2004-06-14 14:11:14 +000087 * for a->el_no <= b->el_no!
vlm1692da02004-06-14 13:42:23 +000088 */
vlm31473082004-06-06 07:20:02 +000089 return 0;
vlm1692da02004-06-14 13:42:23 +000090 } else if(a_value < b_value)
vlm31473082004-06-06 07:20:02 +000091 return -1;
92 else
93 return 1;
94 } else if(a_class < b_class) {
95 return -1;
96 } else {
97 return 1;
98 }
99}
100
101
vlmfa67ddc2004-06-03 03:38:44 +0000102/*
103 * The decoder of the SEQUENCE type.
104 */
105ber_dec_rval_t
vlme413c122004-08-20 13:23:42 +0000106SEQUENCE_decode_ber(asn1_TYPE_descriptor_t *td,
vlmfa67ddc2004-06-03 03:38:44 +0000107 void **struct_ptr, void *ptr, size_t size, int tag_mode) {
108 /*
109 * Bring closer parts of structure description.
110 */
vlme413c122004-08-20 13:23:42 +0000111 asn1_SEQUENCE_specifics_t *specs = (asn1_SEQUENCE_specifics_t *)td->specifics;
112 asn1_TYPE_member_t *elements = td->elements;
vlmfa67ddc2004-06-03 03:38:44 +0000113
114 /*
115 * Parts of the structure being constructed.
116 */
117 void *st = *struct_ptr; /* Target structure. */
118 ber_dec_ctx_t *ctx; /* Decoder context */
119
120 ber_tlv_tag_t tlv_tag; /* T from TLV */
121 //ber_tlv_len_t tlv_len; /* L from TLV */
122 ber_dec_rval_t rval; /* Return code from subparsers */
123
124 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
125 int edx; /* SEQUENCE element's index */
126
vlme413c122004-08-20 13:23:42 +0000127 ASN_DEBUG("Decoding %s as SEQUENCE", td->name);
vlmfa67ddc2004-06-03 03:38:44 +0000128
129 /*
130 * Create the target structure if it is not present already.
131 */
132 if(st == 0) {
133 st = *struct_ptr = CALLOC(1, specs->struct_size);
134 if(st == 0) {
135 RETURN(RC_FAIL);
136 }
137 }
138
139 /*
140 * Restore parsing context.
141 */
vlm1ff928d2004-08-11 08:10:13 +0000142 ctx = (ber_dec_ctx_t *)((char *)st + specs->ctx_offset);
vlmfa67ddc2004-06-03 03:38:44 +0000143
144 /*
145 * Start to parse where left previously
146 */
147 switch(ctx->phase) {
148 case 0:
149 /*
150 * PHASE 0.
151 * Check that the set of tags associated with given structure
152 * perfectly fits our expectations.
153 */
154
vlme413c122004-08-20 13:23:42 +0000155 rval = ber_check_tags(td, ctx, ptr, size,
vlmfa67ddc2004-06-03 03:38:44 +0000156 tag_mode, &ctx->left, 0);
157 if(rval.code != RC_OK) {
158 ASN_DEBUG("%s tagging check failed: %d",
vlme413c122004-08-20 13:23:42 +0000159 td->name, rval.code);
vlmfa67ddc2004-06-03 03:38:44 +0000160 consumed_myself += rval.consumed;
161 RETURN(rval.code);
162 }
163
164 if(ctx->left >= 0)
165 ctx->left += rval.consumed; /* ?Substracted below! */
166 ADVANCE(rval.consumed);
167
168 NEXT_PHASE(ctx);
169
170 ASN_DEBUG("Structure consumes %ld bytes, buffer %ld",
171 (long)ctx->left, (long)size);
172
173 /* Fall through */
174 case 1:
175 /*
176 * PHASE 1.
177 * From the place where we've left it previously,
178 * try to decode the next member from the list of
179 * this structure's elements.
180 * (ctx->step) stores the member being processed
181 * between invocations and the microphase {0,1} of parsing
182 * that member:
183 * step = (<member_number> * 2 + <microphase>).
184 */
vlme413c122004-08-20 13:23:42 +0000185 for(edx = (ctx->step >> 1); edx < td->elements_count;
vlmfa67ddc2004-06-03 03:38:44 +0000186 edx++, ctx->step = (ctx->step & ~1) + 2) {
187 void *memb_ptr; /* Pointer to the member */
vlmda674682004-08-11 09:07:36 +0000188 void **memb_ptr2; /* Pointer to that pointer */
vlmfa67ddc2004-06-03 03:38:44 +0000189 ssize_t tag_len; /* Length of TLV's T */
190 int opt_edx_end; /* Next non-optional element */
vlm31473082004-06-06 07:20:02 +0000191 int use_bsearch;
vlmfa67ddc2004-06-03 03:38:44 +0000192 int n;
193
194 if(ctx->step & 1)
195 goto microphase2;
196
197 /*
198 * MICROPHASE 1: Synchronize decoding.
199 */
vlmddd5a7d2004-09-10 09:18:20 +0000200 ASN_DEBUG("In %s SEQUENCE left %d, edx=%d flags=%d"
201 " opt=%d ec=%d",
202 td->name, (int)ctx->left, edx,
203 elements[edx].flags, elements[edx].optional,
204 td->elements_count);
vlmfa67ddc2004-06-03 03:38:44 +0000205
206 if(ctx->left == 0 /* No more stuff is expected */
207 && (
208 /* Explicit OPTIONAL specification reaches the end */
vlmddd5a7d2004-09-10 09:18:20 +0000209 (edx + elements[edx].optional
210 == td->elements_count)
vlmfa67ddc2004-06-03 03:38:44 +0000211 ||
212 /* All extensions are optional */
213 (IN_EXTENSION_GROUP(specs, edx)
vlme413c122004-08-20 13:23:42 +0000214 && specs->ext_before > td->elements_count)
vlmfa67ddc2004-06-03 03:38:44 +0000215 )
216 ) {
vlme413c122004-08-20 13:23:42 +0000217 ASN_DEBUG("End of SEQUENCE %s", td->name);
vlmfa67ddc2004-06-03 03:38:44 +0000218 /*
219 * Found the legitimate end of the structure.
220 */
221 PHASE_OUT(ctx);
222 RETURN(RC_OK);
223 }
224
225 /*
226 * Fetch the T from TLV.
227 */
228 tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
229 ASN_DEBUG("In %s SEQUENCE for %d %s next tag length %d",
vlme413c122004-08-20 13:23:42 +0000230 td->name, edx, elements[edx].name, (int)tag_len);
vlmfa67ddc2004-06-03 03:38:44 +0000231 switch(tag_len) {
232 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
233 /* Fall through */
234 case -1: RETURN(RC_FAIL);
235 }
236
237 /*
238 * Find the next available type with this tag.
239 */
vlm31473082004-06-06 07:20:02 +0000240 use_bsearch = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000241 opt_edx_end = edx + elements[edx].optional + 1;
vlme413c122004-08-20 13:23:42 +0000242 if(opt_edx_end > td->elements_count)
243 opt_edx_end = td->elements_count; /* Cap */
vlm5d219bf2004-07-01 00:48:04 +0000244 else if(opt_edx_end - edx > 8) {
vlm1692da02004-06-14 13:42:23 +0000245 /* Limit the scope of linear search... */
vlm5d219bf2004-07-01 00:48:04 +0000246 opt_edx_end = edx + 8;
vlm31473082004-06-06 07:20:02 +0000247 use_bsearch = 1;
vlm1692da02004-06-14 13:42:23 +0000248 /* ... and resort to bsearch() */
vlm31473082004-06-06 07:20:02 +0000249 }
vlmfa67ddc2004-06-03 03:38:44 +0000250 for(n = edx; n < opt_edx_end; n++) {
251 if(BER_TAGS_EQUAL(tlv_tag, elements[n].tag)) {
252 /*
253 * Found element corresponding to the tag
254 * being looked at.
255 * Reposition over the right element.
256 */
257 edx = n;
vlm31473082004-06-06 07:20:02 +0000258 ctx->step = 1 + 2 * edx; /* Remember! */
259 goto microphase2;
vlm060fe2a2004-09-10 09:37:12 +0000260 } else if(elements[n].flags & ATF_OPEN_TYPE) {
261 /*
262 * This is the ANY type, which may bear
263 * any flag whatsoever.
264 */
265 edx = n;
266 ctx->step = 1 + 2 * edx; /* Remember! */
267 goto microphase2;
vlm31473082004-06-06 07:20:02 +0000268 } else if(elements[n].tag == (ber_tlv_tag_t)-1) {
269 use_bsearch = 1;
vlmfa67ddc2004-06-03 03:38:44 +0000270 break;
271 }
272 }
vlm31473082004-06-06 07:20:02 +0000273 if(use_bsearch) {
274 /*
vlm141874a2004-09-04 06:17:35 +0000275 * Resort to a binary search over
vlm31473082004-06-06 07:20:02 +0000276 * sorted array of tags.
277 */
278 asn1_TYPE_tag2member_t *t2m;
279 asn1_TYPE_tag2member_t key;
280 key.el_tag = tlv_tag;
vlm1692da02004-06-14 13:42:23 +0000281 key.el_no = edx;
vlmda674682004-08-11 09:07:36 +0000282 (void *)t2m = bsearch(&key,
283 specs->tag2el, specs->tag2el_count,
vlm31473082004-06-06 07:20:02 +0000284 sizeof(specs->tag2el[0]), _t2e_cmp);
vlm1692da02004-06-14 13:42:23 +0000285 if(t2m) {
286 asn1_TYPE_tag2member_t *best = 0;
287 asn1_TYPE_tag2member_t *t2m_f, *t2m_l;
288 int edx_max = edx + elements[edx].optional;
vlm31473082004-06-06 07:20:02 +0000289 /*
290 * Rewind to the first element with that tag,
291 * `cause bsearch() does not guarantee order.
292 */
vlm1692da02004-06-14 13:42:23 +0000293 t2m_f = t2m + t2m->toff_first;
294 t2m_l = t2m + t2m->toff_last;
295 for(t2m = t2m_f; t2m <= t2m_l; t2m++) {
296 if(t2m->el_no > edx_max) break;
297 if(t2m->el_no < edx) continue;
298 best = t2m;
299 }
300 if(best) {
301 edx = best->el_no;
302 ctx->step = 1 + 2 * edx;
303 goto microphase2;
304 }
vlm31473082004-06-06 07:20:02 +0000305 }
306 n = opt_edx_end;
307 }
vlmfa67ddc2004-06-03 03:38:44 +0000308 if(n == opt_edx_end) {
309 /*
310 * If tag is unknown, it may be either
311 * an unknown (thus, incorrect) tag,
312 * or an extension (...),
313 * or an end of the indefinite-length structure.
314 */
vlmfa67ddc2004-06-03 03:38:44 +0000315 if(!IN_EXTENSION_GROUP(specs, edx)) {
316 ASN_DEBUG("Unexpected tag %s",
317 ber_tlv_tag_string(tlv_tag));
vlm31473082004-06-06 07:20:02 +0000318 ASN_DEBUG("Expected tag %s (%s)%s",
vlmfa67ddc2004-06-03 03:38:44 +0000319 ber_tlv_tag_string(elements[edx].tag),
vlm31473082004-06-06 07:20:02 +0000320 elements[edx].name,
vlmfa67ddc2004-06-03 03:38:44 +0000321 elements[edx].optional
322 ?" or alternatives":"");
323 RETURN(RC_FAIL);
324 }
325
326 if(ctx->left < 0
327 && ((uint8_t *)ptr)[0] == 0) {
328 if(LEFT < 2) {
329 if(SIZE_VIOLATION)
330 RETURN(RC_FAIL);
331 else
332 RETURN(RC_WMORE);
333 } else if(((uint8_t *)ptr)[1] == 0) {
334 /*
335 * Yeah, baby! Found the terminator
336 * of the indefinite length structure.
337 */
338 /*
339 * Proceed to the canonical
340 * finalization function.
341 * No advancing is necessary.
342 */
343 goto phase3;
344 }
345 } else {
346 /* Skip this tag */
347 ssize_t skip;
348
349 skip = ber_skip_length(
350 BER_TLV_CONSTRUCTED(ptr),
vlm1ff928d2004-08-11 08:10:13 +0000351 (char *)ptr + tag_len, LEFT - tag_len);
vlmfa67ddc2004-06-03 03:38:44 +0000352 ASN_DEBUG("Skip length %d in %s",
vlme413c122004-08-20 13:23:42 +0000353 (int)skip, td->name);
vlmfa67ddc2004-06-03 03:38:44 +0000354 switch(skip) {
355 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
356 /* Fall through */
357 case -1: RETURN(RC_FAIL);
358 }
359
360 ADVANCE(skip + tag_len);
361 ctx->step -= 2;
362 edx--;
363 continue; /* Try again with the next tag */
364 }
365 }
366
367 /*
368 * MICROPHASE 2: Invoke the member-specific decoder.
369 */
370 ctx->step |= 1; /* Confirm entering next microphase */
371 microphase2:
vlme413c122004-08-20 13:23:42 +0000372 ASN_DEBUG("Inside SEQUENCE %s MF2", td->name);
vlmfa67ddc2004-06-03 03:38:44 +0000373
374 /*
375 * Compute the position of the member inside a structure,
376 * and also a type of containment (it may be contained
377 * as pointer or using inline inclusion).
378 */
vlmddd5a7d2004-09-10 09:18:20 +0000379 if(elements[edx].flags & ATF_POINTER) {
380 /* Member is a pointer to another structure */
vlmda674682004-08-11 09:07:36 +0000381 memb_ptr2 = (void **)((char *)st + elements[edx].memb_offset);
vlmfa67ddc2004-06-03 03:38:44 +0000382 } else {
383 /*
384 * A pointer to a pointer
385 * holding the start of the structure
386 */
387 memb_ptr = (char *)st + elements[edx].memb_offset;
388 memb_ptr2 = &memb_ptr;
389 }
390 /*
391 * Invoke the member fetch routine according to member's type
392 */
393 rval = elements[edx].type->ber_decoder(
vlmda674682004-08-11 09:07:36 +0000394 elements[edx].type,
vlmfa67ddc2004-06-03 03:38:44 +0000395 memb_ptr2, ptr, LEFT,
396 elements[edx].tag_mode);
vlmef16e8c2004-09-04 04:44:11 +0000397 ASN_DEBUG("In %s SEQUENCE decoded %d %s of %d "
398 "in %d bytes rval.code %d",
vlme413c122004-08-20 13:23:42 +0000399 td->name, edx, elements[edx].type->name,
vlmef16e8c2004-09-04 04:44:11 +0000400 (int)LEFT, (int)rval.consumed, rval.code);
vlmfa67ddc2004-06-03 03:38:44 +0000401 switch(rval.code) {
402 case RC_OK:
403 break;
404 case RC_WMORE: /* More data expected */
405 if(!SIZE_VIOLATION) {
406 ADVANCE(rval.consumed);
407 RETURN(RC_WMORE);
408 }
409 /* Fall through */
410 case RC_FAIL: /* Fatal error */
411 RETURN(RC_FAIL);
412 } /* switch(rval) */
413
414 ADVANCE(rval.consumed);
415 } /* for(all structure members) */
416
417 phase3:
418 ctx->phase = 3;
419 case 3: /* 00 and other tags expected */
420 case 4: /* only 00's expected */
421
422 ASN_DEBUG("SEQUENCE %s Leftover: %ld, size = %ld",
vlme413c122004-08-20 13:23:42 +0000423 td->name, (long)ctx->left, (long)size);
vlmfa67ddc2004-06-03 03:38:44 +0000424
425 /*
426 * Skip everything until the end of the SEQUENCE.
427 */
428 while(ctx->left) {
429 ssize_t tl, ll;
430
431 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
432 switch(tl) {
433 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
434 /* Fall through */
435 case -1: RETURN(RC_FAIL);
436 }
437
438 /*
439 * If expected <0><0>...
440 */
441 if(ctx->left < 0
442 && ((uint8_t *)ptr)[0] == 0) {
443 if(LEFT < 2) {
444 if(SIZE_VIOLATION)
445 RETURN(RC_FAIL);
446 else
447 RETURN(RC_WMORE);
448 } else if(((uint8_t *)ptr)[1] == 0) {
449 /*
450 * Correctly finished with <0><0>.
451 */
452 ADVANCE(2);
453 ctx->left++;
454 ctx->phase = 4;
455 continue;
456 }
457 }
458
vlme413c122004-08-20 13:23:42 +0000459 if(!IN_EXTENSION_GROUP(specs, td->elements_count)
vlmfa67ddc2004-06-03 03:38:44 +0000460 || ctx->phase == 4) {
461 ASN_DEBUG("Unexpected continuation "
462 "of a non-extensible type "
463 "%s (SEQUENCE): %s",
vlme413c122004-08-20 13:23:42 +0000464 td->name,
vlmfa67ddc2004-06-03 03:38:44 +0000465 ber_tlv_tag_string(tlv_tag));
466 RETURN(RC_FAIL);
467 }
468
469 ll = ber_skip_length(
470 BER_TLV_CONSTRUCTED(ptr),
vlm1ff928d2004-08-11 08:10:13 +0000471 (char *)ptr + tl, LEFT - tl);
vlmfa67ddc2004-06-03 03:38:44 +0000472 switch(ll) {
473 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
474 /* Fall through */
475 case -1: RETURN(RC_FAIL);
476 }
477
478 ADVANCE(tl + ll);
479 }
480
481 PHASE_OUT(ctx);
482 }
483
484 RETURN(RC_OK);
485}
486
vlm31473082004-06-06 07:20:02 +0000487
vlmfa67ddc2004-06-03 03:38:44 +0000488/*
489 * The DER encoder of the SEQUENCE type.
490 */
491der_enc_rval_t
vlme413c122004-08-20 13:23:42 +0000492SEQUENCE_encode_der(asn1_TYPE_descriptor_t *td,
vlmfa67ddc2004-06-03 03:38:44 +0000493 void *ptr, int tag_mode, ber_tlv_tag_t tag,
494 asn_app_consume_bytes_f *cb, void *app_key) {
vlmfa67ddc2004-06-03 03:38:44 +0000495 size_t computed_size = 0;
496 der_enc_rval_t erval;
497 ssize_t ret;
498 int edx;
499
500 ASN_DEBUG("%s %s as SEQUENCE",
vlme413c122004-08-20 13:23:42 +0000501 cb?"Encoding":"Estimating", td->name);
vlmfa67ddc2004-06-03 03:38:44 +0000502
503 /*
504 * Gather the length of the underlying members sequence.
505 */
vlme413c122004-08-20 13:23:42 +0000506 for(edx = 0; edx < td->elements_count; edx++) {
507 asn1_TYPE_member_t *elm = &td->elements[edx];
vlmfa67ddc2004-06-03 03:38:44 +0000508 void *memb_ptr;
vlmddd5a7d2004-09-10 09:18:20 +0000509 if(elm->flags & ATF_POINTER) {
vlmfa67ddc2004-06-03 03:38:44 +0000510 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
511 if(!memb_ptr) continue;
512 } else {
513 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
514 }
515 erval = elm->type->der_encoder(elm->type, memb_ptr,
516 elm->tag_mode, elm->tag,
517 0, 0);
518 if(erval.encoded == -1)
519 return erval;
520 computed_size += erval.encoded;
521 ASN_DEBUG("Member %d %s estimated %ld bytes",
522 edx, elm->name, (long)erval.encoded);
523 }
524
525 /*
526 * Encode the TLV for the sequence itself.
527 */
vlme413c122004-08-20 13:23:42 +0000528 ret = der_write_tags(td, computed_size, tag_mode, tag, cb, app_key);
vlmfa67ddc2004-06-03 03:38:44 +0000529 ASN_DEBUG("Wrote tags: %ld (+%ld)", (long)ret, (long)computed_size);
530 if(ret == -1) {
531 erval.encoded = -1;
vlme413c122004-08-20 13:23:42 +0000532 erval.failed_type = td;
vlmfa67ddc2004-06-03 03:38:44 +0000533 erval.structure_ptr = ptr;
534 return erval;
535 }
536 erval.encoded = computed_size + ret;
537
538 if(!cb) return erval;
539
540 /*
541 * Encode all members.
542 */
vlme413c122004-08-20 13:23:42 +0000543 for(edx = 0; edx < td->elements_count; edx++) {
544 asn1_TYPE_member_t *elm = &td->elements[edx];
vlmfa67ddc2004-06-03 03:38:44 +0000545 der_enc_rval_t tmperval;
546 void *memb_ptr;
547
vlmddd5a7d2004-09-10 09:18:20 +0000548 if(elm->flags & ATF_POINTER) {
vlmfa67ddc2004-06-03 03:38:44 +0000549 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
550 if(!memb_ptr) continue;
551 } else {
552 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
553 }
554 tmperval = elm->type->der_encoder(elm->type, memb_ptr,
555 elm->tag_mode, elm->tag,
556 cb, app_key);
557 if(tmperval.encoded == -1)
558 return tmperval;
559 computed_size -= tmperval.encoded;
560 ASN_DEBUG("Member %d %s of SEQUENCE %s encoded in %d bytes",
vlme413c122004-08-20 13:23:42 +0000561 edx, elm->name, td->name, tmperval.encoded);
vlmfa67ddc2004-06-03 03:38:44 +0000562 }
563
564 if(computed_size != 0) {
565 /*
566 * Encoded size is not equal to the computed size.
567 */
568 erval.encoded = -1;
vlme413c122004-08-20 13:23:42 +0000569 erval.failed_type = td;
vlmfa67ddc2004-06-03 03:38:44 +0000570 erval.structure_ptr = ptr;
571 }
572
573 return erval;
574}
575
576int
577SEQUENCE_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
578 asn_app_consume_bytes_f *cb, void *app_key) {
vlmfa67ddc2004-06-03 03:38:44 +0000579 int edx;
580 int ret;
581
582 if(!sptr) return cb("<absent>", 8, app_key);
583
584 /* Dump preamble */
585 if(cb(td->name, strlen(td->name), app_key)
586 || cb(" ::= {\n", 7, app_key))
587 return -1;
588
vlme413c122004-08-20 13:23:42 +0000589 for(edx = 0; edx < td->elements_count; edx++) {
590 asn1_TYPE_member_t *elm = &td->elements[edx];
vlmfa67ddc2004-06-03 03:38:44 +0000591 const void *memb_ptr;
592
vlmddd5a7d2004-09-10 09:18:20 +0000593 if(elm->flags & ATF_POINTER) {
vlmfa67ddc2004-06-03 03:38:44 +0000594 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
595 if(!memb_ptr) continue;
596 } else {
597 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
598 }
599
600 /* Indentation */
601 for(ret = 0; ret < ilevel; ret++) cb(" ", 1, app_key);
602
603 /* Print the member's name and stuff */
604 if(cb(elm->name, strlen(elm->name), app_key)
605 || cb(": ", 2, app_key))
606 return -1;
607
608 /* Print the member itself */
609 ret = elm->type->print_struct(elm->type, memb_ptr, ilevel + 4,
610 cb, app_key);
611 if(ret) return ret;
612
613 /* Print out the terminator */
614 ret = cb("\n", 1, app_key);
615 if(ret) return ret;
616 }
617
618 /* Indentation */
619 for(ret = 0; ret < ilevel - 4; ret++) cb(" ", 1, app_key);
620
621 return cb("}", 1, app_key);
622}
623
624void
625SEQUENCE_free(asn1_TYPE_descriptor_t *td, void *sptr, int contents_only) {
vlmfa67ddc2004-06-03 03:38:44 +0000626 int edx;
627
628 if(!td || !sptr)
629 return;
630
631 ASN_DEBUG("Freeing %s as SEQUENCE", td->name);
632
vlme413c122004-08-20 13:23:42 +0000633 for(edx = 0; edx < td->elements_count; edx++) {
634 asn1_TYPE_member_t *elm = &td->elements[edx];
vlmfa67ddc2004-06-03 03:38:44 +0000635 void *memb_ptr;
vlmddd5a7d2004-09-10 09:18:20 +0000636 if(elm->flags & ATF_POINTER) {
vlmfa67ddc2004-06-03 03:38:44 +0000637 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
638 if(memb_ptr)
639 elm->type->free_struct(elm->type, memb_ptr, 0);
640 } else {
641 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
642 elm->type->free_struct(elm->type, memb_ptr, 1);
643 }
644 }
645
646 if(!contents_only) {
647 FREEMEM(sptr);
648 }
649}
650
651int
652SEQUENCE_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
653 asn_app_consume_bytes_f *app_errlog, void *app_key) {
vlmfa67ddc2004-06-03 03:38:44 +0000654 int edx;
655
656 if(!sptr) {
vlme3f0f282004-08-11 09:44:13 +0000657 _ASN_ERRLOG(app_errlog, app_key,
vlm758530a2004-08-22 13:47:59 +0000658 "%s: value not given (%s:%d)",
659 td->name, __FILE__, __LINE__);
vlmfa67ddc2004-06-03 03:38:44 +0000660 return -1;
661 }
662
663 /*
664 * Iterate over structure members and check their validity.
665 */
vlme413c122004-08-20 13:23:42 +0000666 for(edx = 0; edx < td->elements_count; edx++) {
667 asn1_TYPE_member_t *elm = &td->elements[edx];
vlmfa67ddc2004-06-03 03:38:44 +0000668 const void *memb_ptr;
669
vlmddd5a7d2004-09-10 09:18:20 +0000670 if(elm->flags & ATF_POINTER) {
vlm31473082004-06-06 07:20:02 +0000671 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
vlmfa67ddc2004-06-03 03:38:44 +0000672 if(!memb_ptr) continue;
673 } else {
674 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
675 }
676
vlme413c122004-08-20 13:23:42 +0000677 if(elm->memb_constraints) {
678 int ret = elm->memb_constraints(elm->type, memb_ptr,
679 app_errlog, app_key);
680 if(ret) return ret;
681 } else {
682 int ret = elm->type->check_constraints(elm->type,
683 memb_ptr, app_errlog, app_key);
684 if(ret) return ret;
685 /*
686 * Cannot inherit it earlier:
687 * need to make sure we get the updated version.
688 */
689 elm->memb_constraints = elm->type->check_constraints;
690 }
vlmfa67ddc2004-06-03 03:38:44 +0000691 }
692
693 return 0;
694}