blob: f2736811935efa2a0501b21e9bc554bc15cb7730 [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 */
5#include <constr_SEQUENCE.h>
Lev Walkin4c36e302004-06-06 07:20:02 +00006#include <assert.h>
Lev Walkinf15320b2004-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 */
Lev Walkind9bd7752004-06-05 08:17:50 +000013#define LEFT ((size<(size_t)ctx->left)?size:ctx->left)
Lev Walkinf15320b2004-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 */
Lev Walkind9bd7752004-06-05 08:17:50 +000026#define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size)
Lev Walkinf15320b2004-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; \
Lev Walkin4d9528c2004-08-11 08:10:13 +000034 (char *)ptr += num; \
Lev Walkinf15320b2004-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
Lev Walkin4c36e302004-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) {
72 const asn1_TYPE_tag2member_t *a = ap;
73 const asn1_TYPE_tag2member_t *b = bp;
74 int a_class = BER_TAG_CLASS(a->el_tag);
75 int b_class = BER_TAG_CLASS(b->el_tag);
76
77 if(a_class == b_class) {
78 ber_tlv_tag_t a_value = BER_TAG_VALUE(a->el_tag);
79 ber_tlv_tag_t b_value = BER_TAG_VALUE(b->el_tag);
80
Lev Walkin924fa032004-06-14 13:42:23 +000081 if(a_value == b_value) {
Lev Walkin50299c12004-06-14 14:11:14 +000082 if(a->el_no > b->el_no)
83 return 1;
Lev Walkin924fa032004-06-14 13:42:23 +000084 /*
85 * Important: we do not check
Lev Walkin50299c12004-06-14 14:11:14 +000086 * for a->el_no <= b->el_no!
Lev Walkin924fa032004-06-14 13:42:23 +000087 */
Lev Walkin4c36e302004-06-06 07:20:02 +000088 return 0;
Lev Walkin924fa032004-06-14 13:42:23 +000089 } else if(a_value < b_value)
Lev Walkin4c36e302004-06-06 07:20:02 +000090 return -1;
91 else
92 return 1;
93 } else if(a_class < b_class) {
94 return -1;
95 } else {
96 return 1;
97 }
98}
99
100
Lev Walkinf15320b2004-06-03 03:38:44 +0000101/*
102 * The decoder of the SEQUENCE type.
103 */
104ber_dec_rval_t
105SEQUENCE_decode_ber(asn1_TYPE_descriptor_t *sd,
106 void **struct_ptr, void *ptr, size_t size, int tag_mode) {
107 /*
108 * Bring closer parts of structure description.
109 */
110 asn1_SEQUENCE_specifics_t *specs = sd->specifics;
111 asn1_SEQUENCE_element_t *elements = specs->elements;
112
113 /*
114 * Parts of the structure being constructed.
115 */
116 void *st = *struct_ptr; /* Target structure. */
117 ber_dec_ctx_t *ctx; /* Decoder context */
118
119 ber_tlv_tag_t tlv_tag; /* T from TLV */
120 //ber_tlv_len_t tlv_len; /* L from TLV */
121 ber_dec_rval_t rval; /* Return code from subparsers */
122
123 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
124 int edx; /* SEQUENCE element's index */
125
126 ASN_DEBUG("Decoding %s as SEQUENCE", sd->name);
127
128 /*
129 * Create the target structure if it is not present already.
130 */
131 if(st == 0) {
132 st = *struct_ptr = CALLOC(1, specs->struct_size);
133 if(st == 0) {
134 RETURN(RC_FAIL);
135 }
136 }
137
138 /*
139 * Restore parsing context.
140 */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000141 ctx = (ber_dec_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000142
143 /*
144 * Start to parse where left previously
145 */
146 switch(ctx->phase) {
147 case 0:
148 /*
149 * PHASE 0.
150 * Check that the set of tags associated with given structure
151 * perfectly fits our expectations.
152 */
153
154 rval = ber_check_tags(sd, ctx, ptr, size,
155 tag_mode, &ctx->left, 0);
156 if(rval.code != RC_OK) {
157 ASN_DEBUG("%s tagging check failed: %d",
158 sd->name, rval.code);
159 consumed_myself += rval.consumed;
160 RETURN(rval.code);
161 }
162
163 if(ctx->left >= 0)
164 ctx->left += rval.consumed; /* ?Substracted below! */
165 ADVANCE(rval.consumed);
166
167 NEXT_PHASE(ctx);
168
169 ASN_DEBUG("Structure consumes %ld bytes, buffer %ld",
170 (long)ctx->left, (long)size);
171
172 /* Fall through */
173 case 1:
174 /*
175 * PHASE 1.
176 * From the place where we've left it previously,
177 * try to decode the next member from the list of
178 * this structure's elements.
179 * (ctx->step) stores the member being processed
180 * between invocations and the microphase {0,1} of parsing
181 * that member:
182 * step = (<member_number> * 2 + <microphase>).
183 */
184 for(edx = (ctx->step >> 1); edx < specs->elements_count;
185 edx++, ctx->step = (ctx->step & ~1) + 2) {
186 void *memb_ptr; /* Pointer to the member */
187 void *memb_ptr2; /* Pointer to that pointer */
188 ssize_t tag_len; /* Length of TLV's T */
189 int opt_edx_end; /* Next non-optional element */
Lev Walkin4c36e302004-06-06 07:20:02 +0000190 int use_bsearch;
Lev Walkinf15320b2004-06-03 03:38:44 +0000191 int n;
192
193 if(ctx->step & 1)
194 goto microphase2;
195
196 /*
197 * MICROPHASE 1: Synchronize decoding.
198 */
199 ASN_DEBUG("In %s SEQUENCE left %d, edx=%d opt=%d ec=%d",
200 sd->name, (int)ctx->left,
201 edx, elements[edx].optional, specs->elements_count);
202
203 if(ctx->left == 0 /* No more stuff is expected */
204 && (
205 /* Explicit OPTIONAL specification reaches the end */
206 (edx + elements[edx].optional == specs->elements_count)
207 ||
208 /* All extensions are optional */
209 (IN_EXTENSION_GROUP(specs, edx)
210 && specs->ext_before > specs->elements_count)
211 )
212 ) {
213 ASN_DEBUG("End of SEQUENCE %s", sd->name);
214 /*
215 * Found the legitimate end of the structure.
216 */
217 PHASE_OUT(ctx);
218 RETURN(RC_OK);
219 }
220
221 /*
222 * Fetch the T from TLV.
223 */
224 tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
225 ASN_DEBUG("In %s SEQUENCE for %d %s next tag length %d",
226 sd->name, edx, elements[edx].name, (int)tag_len);
227 switch(tag_len) {
228 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
229 /* Fall through */
230 case -1: RETURN(RC_FAIL);
231 }
232
233 /*
234 * Find the next available type with this tag.
235 */
Lev Walkin4c36e302004-06-06 07:20:02 +0000236 use_bsearch = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000237 opt_edx_end = edx + elements[edx].optional + 1;
238 if(opt_edx_end > specs->elements_count)
239 opt_edx_end = specs->elements_count; /* Cap */
Lev Walkinf9dbd9a2004-07-01 00:48:04 +0000240 else if(opt_edx_end - edx > 8) {
Lev Walkin924fa032004-06-14 13:42:23 +0000241 /* Limit the scope of linear search... */
Lev Walkinf9dbd9a2004-07-01 00:48:04 +0000242 opt_edx_end = edx + 8;
Lev Walkin4c36e302004-06-06 07:20:02 +0000243 use_bsearch = 1;
Lev Walkin924fa032004-06-14 13:42:23 +0000244 /* ... and resort to bsearch() */
Lev Walkin4c36e302004-06-06 07:20:02 +0000245 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000246 for(n = edx; n < opt_edx_end; n++) {
247 if(BER_TAGS_EQUAL(tlv_tag, elements[n].tag)) {
248 /*
249 * Found element corresponding to the tag
250 * being looked at.
251 * Reposition over the right element.
252 */
253 edx = n;
Lev Walkin4c36e302004-06-06 07:20:02 +0000254 ctx->step = 1 + 2 * edx; /* Remember! */
255 goto microphase2;
256 } else if(elements[n].tag == (ber_tlv_tag_t)-1) {
257 use_bsearch = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000258 break;
259 }
260 }
Lev Walkin4c36e302004-06-06 07:20:02 +0000261 if(use_bsearch) {
262 /*
263 * Resorch to a binary search over
264 * sorted array of tags.
265 */
266 asn1_TYPE_tag2member_t *t2m;
267 asn1_TYPE_tag2member_t key;
268 key.el_tag = tlv_tag;
Lev Walkin924fa032004-06-14 13:42:23 +0000269 key.el_no = edx;
Lev Walkin4c36e302004-06-06 07:20:02 +0000270 t2m = bsearch(&key, specs->tag2el, specs->tag2el_count,
271 sizeof(specs->tag2el[0]), _t2e_cmp);
Lev Walkin924fa032004-06-14 13:42:23 +0000272 if(t2m) {
273 asn1_TYPE_tag2member_t *best = 0;
274 asn1_TYPE_tag2member_t *t2m_f, *t2m_l;
275 int edx_max = edx + elements[edx].optional;
Lev Walkin4c36e302004-06-06 07:20:02 +0000276 /*
277 * Rewind to the first element with that tag,
278 * `cause bsearch() does not guarantee order.
279 */
Lev Walkin924fa032004-06-14 13:42:23 +0000280 t2m_f = t2m + t2m->toff_first;
281 t2m_l = t2m + t2m->toff_last;
282 for(t2m = t2m_f; t2m <= t2m_l; t2m++) {
283 if(t2m->el_no > edx_max) break;
284 if(t2m->el_no < edx) continue;
285 best = t2m;
286 }
287 if(best) {
288 edx = best->el_no;
289 ctx->step = 1 + 2 * edx;
290 goto microphase2;
291 }
Lev Walkin4c36e302004-06-06 07:20:02 +0000292 }
293 n = opt_edx_end;
294 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000295 if(n == opt_edx_end) {
296 /*
297 * If tag is unknown, it may be either
298 * an unknown (thus, incorrect) tag,
299 * or an extension (...),
300 * or an end of the indefinite-length structure.
301 */
302
303 if(!IN_EXTENSION_GROUP(specs, edx)) {
304 ASN_DEBUG("Unexpected tag %s",
305 ber_tlv_tag_string(tlv_tag));
Lev Walkin4c36e302004-06-06 07:20:02 +0000306 ASN_DEBUG("Expected tag %s (%s)%s",
Lev Walkinf15320b2004-06-03 03:38:44 +0000307 ber_tlv_tag_string(elements[edx].tag),
Lev Walkin4c36e302004-06-06 07:20:02 +0000308 elements[edx].name,
Lev Walkinf15320b2004-06-03 03:38:44 +0000309 elements[edx].optional
310 ?" or alternatives":"");
311 RETURN(RC_FAIL);
312 }
313
314 if(ctx->left < 0
315 && ((uint8_t *)ptr)[0] == 0) {
316 if(LEFT < 2) {
317 if(SIZE_VIOLATION)
318 RETURN(RC_FAIL);
319 else
320 RETURN(RC_WMORE);
321 } else if(((uint8_t *)ptr)[1] == 0) {
322 /*
323 * Yeah, baby! Found the terminator
324 * of the indefinite length structure.
325 */
326 /*
327 * Proceed to the canonical
328 * finalization function.
329 * No advancing is necessary.
330 */
331 goto phase3;
332 }
333 } else {
334 /* Skip this tag */
335 ssize_t skip;
336
337 skip = ber_skip_length(
338 BER_TLV_CONSTRUCTED(ptr),
Lev Walkin4d9528c2004-08-11 08:10:13 +0000339 (char *)ptr + tag_len, LEFT - tag_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000340 ASN_DEBUG("Skip length %d in %s",
341 (int)skip, sd->name);
342 switch(skip) {
343 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
344 /* Fall through */
345 case -1: RETURN(RC_FAIL);
346 }
347
348 ADVANCE(skip + tag_len);
349 ctx->step -= 2;
350 edx--;
351 continue; /* Try again with the next tag */
352 }
353 }
354
355 /*
356 * MICROPHASE 2: Invoke the member-specific decoder.
357 */
358 ctx->step |= 1; /* Confirm entering next microphase */
359 microphase2:
360 ASN_DEBUG("Inside SEQUENCE %s MF2", sd->name);
361
362 /*
363 * Compute the position of the member inside a structure,
364 * and also a type of containment (it may be contained
365 * as pointer or using inline inclusion).
366 */
367 if(elements[edx].optional) {
368 /* Optional member, hereby, a simple pointer */
369 memb_ptr2 = (char *)st + elements[edx].memb_offset;
370 } else {
371 /*
372 * A pointer to a pointer
373 * holding the start of the structure
374 */
375 memb_ptr = (char *)st + elements[edx].memb_offset;
376 memb_ptr2 = &memb_ptr;
377 }
378 /*
379 * Invoke the member fetch routine according to member's type
380 */
381 rval = elements[edx].type->ber_decoder(
382 (void *)elements[edx].type,
383 memb_ptr2, ptr, LEFT,
384 elements[edx].tag_mode);
385 ASN_DEBUG("In %s SEQUENCE decoded %d %s in %d bytes code %d",
386 sd->name, edx, elements[edx].type->name,
387 (int)rval.consumed, rval.code);
388 switch(rval.code) {
389 case RC_OK:
390 break;
391 case RC_WMORE: /* More data expected */
392 if(!SIZE_VIOLATION) {
393 ADVANCE(rval.consumed);
394 RETURN(RC_WMORE);
395 }
396 /* Fall through */
397 case RC_FAIL: /* Fatal error */
398 RETURN(RC_FAIL);
399 } /* switch(rval) */
400
401 ADVANCE(rval.consumed);
402 } /* for(all structure members) */
403
404 phase3:
405 ctx->phase = 3;
406 case 3: /* 00 and other tags expected */
407 case 4: /* only 00's expected */
408
409 ASN_DEBUG("SEQUENCE %s Leftover: %ld, size = %ld",
410 sd->name, (long)ctx->left, (long)size);
411
412 /*
413 * Skip everything until the end of the SEQUENCE.
414 */
415 while(ctx->left) {
416 ssize_t tl, ll;
417
418 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
419 switch(tl) {
420 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
421 /* Fall through */
422 case -1: RETURN(RC_FAIL);
423 }
424
425 /*
426 * If expected <0><0>...
427 */
428 if(ctx->left < 0
429 && ((uint8_t *)ptr)[0] == 0) {
430 if(LEFT < 2) {
431 if(SIZE_VIOLATION)
432 RETURN(RC_FAIL);
433 else
434 RETURN(RC_WMORE);
435 } else if(((uint8_t *)ptr)[1] == 0) {
436 /*
437 * Correctly finished with <0><0>.
438 */
439 ADVANCE(2);
440 ctx->left++;
441 ctx->phase = 4;
442 continue;
443 }
444 }
445
446 if(!IN_EXTENSION_GROUP(specs, specs->elements_count)
447 || ctx->phase == 4) {
448 ASN_DEBUG("Unexpected continuation "
449 "of a non-extensible type "
450 "%s (SEQUENCE): %s",
451 sd->name,
452 ber_tlv_tag_string(tlv_tag));
453 RETURN(RC_FAIL);
454 }
455
456 ll = ber_skip_length(
457 BER_TLV_CONSTRUCTED(ptr),
Lev Walkin4d9528c2004-08-11 08:10:13 +0000458 (char *)ptr + tl, LEFT - tl);
Lev Walkinf15320b2004-06-03 03:38:44 +0000459 switch(ll) {
460 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
461 /* Fall through */
462 case -1: RETURN(RC_FAIL);
463 }
464
465 ADVANCE(tl + ll);
466 }
467
468 PHASE_OUT(ctx);
469 }
470
471 RETURN(RC_OK);
472}
473
Lev Walkin4c36e302004-06-06 07:20:02 +0000474
Lev Walkinf15320b2004-06-03 03:38:44 +0000475/*
476 * The DER encoder of the SEQUENCE type.
477 */
478der_enc_rval_t
479SEQUENCE_encode_der(asn1_TYPE_descriptor_t *sd,
480 void *ptr, int tag_mode, ber_tlv_tag_t tag,
481 asn_app_consume_bytes_f *cb, void *app_key) {
482 asn1_SEQUENCE_specifics_t *specs = sd->specifics;
483 size_t computed_size = 0;
484 der_enc_rval_t erval;
485 ssize_t ret;
486 int edx;
487
488 ASN_DEBUG("%s %s as SEQUENCE",
489 cb?"Encoding":"Estimating", sd->name);
490
491 /*
492 * Gather the length of the underlying members sequence.
493 */
494 for(edx = 0; edx < specs->elements_count; edx++) {
495 asn1_SEQUENCE_element_t *elm = &specs->elements[edx];
496 void *memb_ptr;
497 if(elm->optional) {
498 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
499 if(!memb_ptr) continue;
500 } else {
501 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
502 }
503 erval = elm->type->der_encoder(elm->type, memb_ptr,
504 elm->tag_mode, elm->tag,
505 0, 0);
506 if(erval.encoded == -1)
507 return erval;
508 computed_size += erval.encoded;
509 ASN_DEBUG("Member %d %s estimated %ld bytes",
510 edx, elm->name, (long)erval.encoded);
511 }
512
513 /*
514 * Encode the TLV for the sequence itself.
515 */
516 ret = der_write_tags(sd, computed_size, tag_mode, tag, cb, app_key);
517 ASN_DEBUG("Wrote tags: %ld (+%ld)", (long)ret, (long)computed_size);
518 if(ret == -1) {
519 erval.encoded = -1;
520 erval.failed_type = sd;
521 erval.structure_ptr = ptr;
522 return erval;
523 }
524 erval.encoded = computed_size + ret;
525
526 if(!cb) return erval;
527
528 /*
529 * Encode all members.
530 */
531 for(edx = 0; edx < specs->elements_count; edx++) {
532 asn1_SEQUENCE_element_t *elm = &specs->elements[edx];
533 der_enc_rval_t tmperval;
534 void *memb_ptr;
535
536 if(elm->optional) {
537 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
538 if(!memb_ptr) continue;
539 } else {
540 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
541 }
542 tmperval = elm->type->der_encoder(elm->type, memb_ptr,
543 elm->tag_mode, elm->tag,
544 cb, app_key);
545 if(tmperval.encoded == -1)
546 return tmperval;
547 computed_size -= tmperval.encoded;
548 ASN_DEBUG("Member %d %s of SEQUENCE %s encoded in %d bytes",
549 edx, elm->name, sd->name, tmperval.encoded);
550 }
551
552 if(computed_size != 0) {
553 /*
554 * Encoded size is not equal to the computed size.
555 */
556 erval.encoded = -1;
557 erval.failed_type = sd;
558 erval.structure_ptr = ptr;
559 }
560
561 return erval;
562}
563
564int
565SEQUENCE_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
566 asn_app_consume_bytes_f *cb, void *app_key) {
567 asn1_SEQUENCE_specifics_t *specs = td->specifics;
568 int edx;
569 int ret;
570
571 if(!sptr) return cb("<absent>", 8, app_key);
572
573 /* Dump preamble */
574 if(cb(td->name, strlen(td->name), app_key)
575 || cb(" ::= {\n", 7, app_key))
576 return -1;
577
578 for(edx = 0; edx < specs->elements_count; edx++) {
579 asn1_SEQUENCE_element_t *elm = &specs->elements[edx];
580 const void *memb_ptr;
581
582 if(elm->optional) {
583 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
584 if(!memb_ptr) continue;
585 } else {
586 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
587 }
588
589 /* Indentation */
590 for(ret = 0; ret < ilevel; ret++) cb(" ", 1, app_key);
591
592 /* Print the member's name and stuff */
593 if(cb(elm->name, strlen(elm->name), app_key)
594 || cb(": ", 2, app_key))
595 return -1;
596
597 /* Print the member itself */
598 ret = elm->type->print_struct(elm->type, memb_ptr, ilevel + 4,
599 cb, app_key);
600 if(ret) return ret;
601
602 /* Print out the terminator */
603 ret = cb("\n", 1, app_key);
604 if(ret) return ret;
605 }
606
607 /* Indentation */
608 for(ret = 0; ret < ilevel - 4; ret++) cb(" ", 1, app_key);
609
610 return cb("}", 1, app_key);
611}
612
613void
614SEQUENCE_free(asn1_TYPE_descriptor_t *td, void *sptr, int contents_only) {
615 asn1_SEQUENCE_specifics_t *specs = td->specifics;
616 int edx;
617
618 if(!td || !sptr)
619 return;
620
621 ASN_DEBUG("Freeing %s as SEQUENCE", td->name);
622
623 for(edx = 0; edx < specs->elements_count; edx++) {
624 asn1_SEQUENCE_element_t *elm = &specs->elements[edx];
625 void *memb_ptr;
626 if(elm->optional) {
627 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
628 if(memb_ptr)
629 elm->type->free_struct(elm->type, memb_ptr, 0);
630 } else {
631 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
632 elm->type->free_struct(elm->type, memb_ptr, 1);
633 }
634 }
635
636 if(!contents_only) {
637 FREEMEM(sptr);
638 }
639}
640
641int
642SEQUENCE_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
643 asn_app_consume_bytes_f *app_errlog, void *app_key) {
644 asn1_SEQUENCE_specifics_t *specs = td->specifics;
645 int edx;
646
647 if(!sptr) {
648 _ASN_ERRLOG("%s: value not given", td->name);
649 return -1;
650 }
651
652 /*
653 * Iterate over structure members and check their validity.
654 */
655 for(edx = 0; edx < specs->elements_count; edx++) {
656 asn1_SEQUENCE_element_t *elm = &specs->elements[edx];
657 const void *memb_ptr;
658
659 if(elm->optional) {
Lev Walkin4c36e302004-06-06 07:20:02 +0000660 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000661 if(!memb_ptr) continue;
662 } else {
663 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
664 }
665
666 return elm->type->check_constraints(elm->type, memb_ptr,
667 app_errlog, app_key);
668 }
669
670 return 0;
671}