blob: 87d5ab660bc5d983a542ea7083bfd2513d61e0a1 [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; \
vlm1ff928d2004-08-11 08:10:13 +000034 (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 */
200 ASN_DEBUG("In %s SEQUENCE left %d, edx=%d opt=%d ec=%d",
vlme413c122004-08-20 13:23:42 +0000201 td->name, (int)ctx->left,
202 edx, elements[edx].optional, td->elements_count);
vlmfa67ddc2004-06-03 03:38:44 +0000203
204 if(ctx->left == 0 /* No more stuff is expected */
205 && (
206 /* Explicit OPTIONAL specification reaches the end */
vlme413c122004-08-20 13:23:42 +0000207 (edx + elements[edx].optional == td->elements_count)
vlmfa67ddc2004-06-03 03:38:44 +0000208 ||
209 /* All extensions are optional */
210 (IN_EXTENSION_GROUP(specs, edx)
vlme413c122004-08-20 13:23:42 +0000211 && specs->ext_before > td->elements_count)
vlmfa67ddc2004-06-03 03:38:44 +0000212 )
213 ) {
vlme413c122004-08-20 13:23:42 +0000214 ASN_DEBUG("End of SEQUENCE %s", td->name);
vlmfa67ddc2004-06-03 03:38:44 +0000215 /*
216 * Found the legitimate end of the structure.
217 */
218 PHASE_OUT(ctx);
219 RETURN(RC_OK);
220 }
221
222 /*
223 * Fetch the T from TLV.
224 */
225 tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
226 ASN_DEBUG("In %s SEQUENCE for %d %s next tag length %d",
vlme413c122004-08-20 13:23:42 +0000227 td->name, edx, elements[edx].name, (int)tag_len);
vlmfa67ddc2004-06-03 03:38:44 +0000228 switch(tag_len) {
229 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
230 /* Fall through */
231 case -1: RETURN(RC_FAIL);
232 }
233
234 /*
235 * Find the next available type with this tag.
236 */
vlm31473082004-06-06 07:20:02 +0000237 use_bsearch = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000238 opt_edx_end = edx + elements[edx].optional + 1;
vlme413c122004-08-20 13:23:42 +0000239 if(opt_edx_end > td->elements_count)
240 opt_edx_end = td->elements_count; /* Cap */
vlm5d219bf2004-07-01 00:48:04 +0000241 else if(opt_edx_end - edx > 8) {
vlm1692da02004-06-14 13:42:23 +0000242 /* Limit the scope of linear search... */
vlm5d219bf2004-07-01 00:48:04 +0000243 opt_edx_end = edx + 8;
vlm31473082004-06-06 07:20:02 +0000244 use_bsearch = 1;
vlm1692da02004-06-14 13:42:23 +0000245 /* ... and resort to bsearch() */
vlm31473082004-06-06 07:20:02 +0000246 }
vlmfa67ddc2004-06-03 03:38:44 +0000247 for(n = edx; n < opt_edx_end; n++) {
248 if(BER_TAGS_EQUAL(tlv_tag, elements[n].tag)) {
249 /*
250 * Found element corresponding to the tag
251 * being looked at.
252 * Reposition over the right element.
253 */
254 edx = n;
vlm31473082004-06-06 07:20:02 +0000255 ctx->step = 1 + 2 * edx; /* Remember! */
256 goto microphase2;
257 } else if(elements[n].tag == (ber_tlv_tag_t)-1) {
258 use_bsearch = 1;
vlmfa67ddc2004-06-03 03:38:44 +0000259 break;
260 }
261 }
vlm31473082004-06-06 07:20:02 +0000262 if(use_bsearch) {
263 /*
264 * Resorch to a binary search over
265 * sorted array of tags.
266 */
267 asn1_TYPE_tag2member_t *t2m;
268 asn1_TYPE_tag2member_t key;
269 key.el_tag = tlv_tag;
vlm1692da02004-06-14 13:42:23 +0000270 key.el_no = edx;
vlmda674682004-08-11 09:07:36 +0000271 (void *)t2m = bsearch(&key,
272 specs->tag2el, specs->tag2el_count,
vlm31473082004-06-06 07:20:02 +0000273 sizeof(specs->tag2el[0]), _t2e_cmp);
vlm1692da02004-06-14 13:42:23 +0000274 if(t2m) {
275 asn1_TYPE_tag2member_t *best = 0;
276 asn1_TYPE_tag2member_t *t2m_f, *t2m_l;
277 int edx_max = edx + elements[edx].optional;
vlm31473082004-06-06 07:20:02 +0000278 /*
279 * Rewind to the first element with that tag,
280 * `cause bsearch() does not guarantee order.
281 */
vlm1692da02004-06-14 13:42:23 +0000282 t2m_f = t2m + t2m->toff_first;
283 t2m_l = t2m + t2m->toff_last;
284 for(t2m = t2m_f; t2m <= t2m_l; t2m++) {
285 if(t2m->el_no > edx_max) break;
286 if(t2m->el_no < edx) continue;
287 best = t2m;
288 }
289 if(best) {
290 edx = best->el_no;
291 ctx->step = 1 + 2 * edx;
292 goto microphase2;
293 }
vlm31473082004-06-06 07:20:02 +0000294 }
295 n = opt_edx_end;
296 }
vlmfa67ddc2004-06-03 03:38:44 +0000297 if(n == opt_edx_end) {
298 /*
299 * If tag is unknown, it may be either
300 * an unknown (thus, incorrect) tag,
301 * or an extension (...),
302 * or an end of the indefinite-length structure.
303 */
304
305 if(!IN_EXTENSION_GROUP(specs, edx)) {
306 ASN_DEBUG("Unexpected tag %s",
307 ber_tlv_tag_string(tlv_tag));
vlm31473082004-06-06 07:20:02 +0000308 ASN_DEBUG("Expected tag %s (%s)%s",
vlmfa67ddc2004-06-03 03:38:44 +0000309 ber_tlv_tag_string(elements[edx].tag),
vlm31473082004-06-06 07:20:02 +0000310 elements[edx].name,
vlmfa67ddc2004-06-03 03:38:44 +0000311 elements[edx].optional
312 ?" or alternatives":"");
313 RETURN(RC_FAIL);
314 }
315
316 if(ctx->left < 0
317 && ((uint8_t *)ptr)[0] == 0) {
318 if(LEFT < 2) {
319 if(SIZE_VIOLATION)
320 RETURN(RC_FAIL);
321 else
322 RETURN(RC_WMORE);
323 } else if(((uint8_t *)ptr)[1] == 0) {
324 /*
325 * Yeah, baby! Found the terminator
326 * of the indefinite length structure.
327 */
328 /*
329 * Proceed to the canonical
330 * finalization function.
331 * No advancing is necessary.
332 */
333 goto phase3;
334 }
335 } else {
336 /* Skip this tag */
337 ssize_t skip;
338
339 skip = ber_skip_length(
340 BER_TLV_CONSTRUCTED(ptr),
vlm1ff928d2004-08-11 08:10:13 +0000341 (char *)ptr + tag_len, LEFT - tag_len);
vlmfa67ddc2004-06-03 03:38:44 +0000342 ASN_DEBUG("Skip length %d in %s",
vlme413c122004-08-20 13:23:42 +0000343 (int)skip, td->name);
vlmfa67ddc2004-06-03 03:38:44 +0000344 switch(skip) {
345 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
346 /* Fall through */
347 case -1: RETURN(RC_FAIL);
348 }
349
350 ADVANCE(skip + tag_len);
351 ctx->step -= 2;
352 edx--;
353 continue; /* Try again with the next tag */
354 }
355 }
356
357 /*
358 * MICROPHASE 2: Invoke the member-specific decoder.
359 */
360 ctx->step |= 1; /* Confirm entering next microphase */
361 microphase2:
vlme413c122004-08-20 13:23:42 +0000362 ASN_DEBUG("Inside SEQUENCE %s MF2", td->name);
vlmfa67ddc2004-06-03 03:38:44 +0000363
364 /*
365 * Compute the position of the member inside a structure,
366 * and also a type of containment (it may be contained
367 * as pointer or using inline inclusion).
368 */
369 if(elements[edx].optional) {
370 /* Optional member, hereby, a simple pointer */
vlmda674682004-08-11 09:07:36 +0000371 memb_ptr2 = (void **)((char *)st + elements[edx].memb_offset);
vlmfa67ddc2004-06-03 03:38:44 +0000372 } else {
373 /*
374 * A pointer to a pointer
375 * holding the start of the structure
376 */
377 memb_ptr = (char *)st + elements[edx].memb_offset;
378 memb_ptr2 = &memb_ptr;
379 }
380 /*
381 * Invoke the member fetch routine according to member's type
382 */
383 rval = elements[edx].type->ber_decoder(
vlmda674682004-08-11 09:07:36 +0000384 elements[edx].type,
vlmfa67ddc2004-06-03 03:38:44 +0000385 memb_ptr2, ptr, LEFT,
386 elements[edx].tag_mode);
387 ASN_DEBUG("In %s SEQUENCE decoded %d %s in %d bytes code %d",
vlme413c122004-08-20 13:23:42 +0000388 td->name, edx, elements[edx].type->name,
vlmfa67ddc2004-06-03 03:38:44 +0000389 (int)rval.consumed, rval.code);
390 switch(rval.code) {
391 case RC_OK:
392 break;
393 case RC_WMORE: /* More data expected */
394 if(!SIZE_VIOLATION) {
395 ADVANCE(rval.consumed);
396 RETURN(RC_WMORE);
397 }
398 /* Fall through */
399 case RC_FAIL: /* Fatal error */
400 RETURN(RC_FAIL);
401 } /* switch(rval) */
402
403 ADVANCE(rval.consumed);
404 } /* for(all structure members) */
405
406 phase3:
407 ctx->phase = 3;
408 case 3: /* 00 and other tags expected */
409 case 4: /* only 00's expected */
410
411 ASN_DEBUG("SEQUENCE %s Leftover: %ld, size = %ld",
vlme413c122004-08-20 13:23:42 +0000412 td->name, (long)ctx->left, (long)size);
vlmfa67ddc2004-06-03 03:38:44 +0000413
414 /*
415 * Skip everything until the end of the SEQUENCE.
416 */
417 while(ctx->left) {
418 ssize_t tl, ll;
419
420 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
421 switch(tl) {
422 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
423 /* Fall through */
424 case -1: RETURN(RC_FAIL);
425 }
426
427 /*
428 * If expected <0><0>...
429 */
430 if(ctx->left < 0
431 && ((uint8_t *)ptr)[0] == 0) {
432 if(LEFT < 2) {
433 if(SIZE_VIOLATION)
434 RETURN(RC_FAIL);
435 else
436 RETURN(RC_WMORE);
437 } else if(((uint8_t *)ptr)[1] == 0) {
438 /*
439 * Correctly finished with <0><0>.
440 */
441 ADVANCE(2);
442 ctx->left++;
443 ctx->phase = 4;
444 continue;
445 }
446 }
447
vlme413c122004-08-20 13:23:42 +0000448 if(!IN_EXTENSION_GROUP(specs, td->elements_count)
vlmfa67ddc2004-06-03 03:38:44 +0000449 || ctx->phase == 4) {
450 ASN_DEBUG("Unexpected continuation "
451 "of a non-extensible type "
452 "%s (SEQUENCE): %s",
vlme413c122004-08-20 13:23:42 +0000453 td->name,
vlmfa67ddc2004-06-03 03:38:44 +0000454 ber_tlv_tag_string(tlv_tag));
455 RETURN(RC_FAIL);
456 }
457
458 ll = ber_skip_length(
459 BER_TLV_CONSTRUCTED(ptr),
vlm1ff928d2004-08-11 08:10:13 +0000460 (char *)ptr + tl, LEFT - tl);
vlmfa67ddc2004-06-03 03:38:44 +0000461 switch(ll) {
462 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
463 /* Fall through */
464 case -1: RETURN(RC_FAIL);
465 }
466
467 ADVANCE(tl + ll);
468 }
469
470 PHASE_OUT(ctx);
471 }
472
473 RETURN(RC_OK);
474}
475
vlm31473082004-06-06 07:20:02 +0000476
vlmfa67ddc2004-06-03 03:38:44 +0000477/*
478 * The DER encoder of the SEQUENCE type.
479 */
480der_enc_rval_t
vlme413c122004-08-20 13:23:42 +0000481SEQUENCE_encode_der(asn1_TYPE_descriptor_t *td,
vlmfa67ddc2004-06-03 03:38:44 +0000482 void *ptr, int tag_mode, ber_tlv_tag_t tag,
483 asn_app_consume_bytes_f *cb, void *app_key) {
vlmfa67ddc2004-06-03 03:38:44 +0000484 size_t computed_size = 0;
485 der_enc_rval_t erval;
486 ssize_t ret;
487 int edx;
488
489 ASN_DEBUG("%s %s as SEQUENCE",
vlme413c122004-08-20 13:23:42 +0000490 cb?"Encoding":"Estimating", td->name);
vlmfa67ddc2004-06-03 03:38:44 +0000491
492 /*
493 * Gather the length of the underlying members sequence.
494 */
vlme413c122004-08-20 13:23:42 +0000495 for(edx = 0; edx < td->elements_count; edx++) {
496 asn1_TYPE_member_t *elm = &td->elements[edx];
vlmfa67ddc2004-06-03 03:38:44 +0000497 void *memb_ptr;
498 if(elm->optional) {
499 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
500 if(!memb_ptr) continue;
501 } else {
502 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
503 }
504 erval = elm->type->der_encoder(elm->type, memb_ptr,
505 elm->tag_mode, elm->tag,
506 0, 0);
507 if(erval.encoded == -1)
508 return erval;
509 computed_size += erval.encoded;
510 ASN_DEBUG("Member %d %s estimated %ld bytes",
511 edx, elm->name, (long)erval.encoded);
512 }
513
514 /*
515 * Encode the TLV for the sequence itself.
516 */
vlme413c122004-08-20 13:23:42 +0000517 ret = der_write_tags(td, computed_size, tag_mode, tag, cb, app_key);
vlmfa67ddc2004-06-03 03:38:44 +0000518 ASN_DEBUG("Wrote tags: %ld (+%ld)", (long)ret, (long)computed_size);
519 if(ret == -1) {
520 erval.encoded = -1;
vlme413c122004-08-20 13:23:42 +0000521 erval.failed_type = td;
vlmfa67ddc2004-06-03 03:38:44 +0000522 erval.structure_ptr = ptr;
523 return erval;
524 }
525 erval.encoded = computed_size + ret;
526
527 if(!cb) return erval;
528
529 /*
530 * Encode all members.
531 */
vlme413c122004-08-20 13:23:42 +0000532 for(edx = 0; edx < td->elements_count; edx++) {
533 asn1_TYPE_member_t *elm = &td->elements[edx];
vlmfa67ddc2004-06-03 03:38:44 +0000534 der_enc_rval_t tmperval;
535 void *memb_ptr;
536
537 if(elm->optional) {
538 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
539 if(!memb_ptr) continue;
540 } else {
541 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
542 }
543 tmperval = elm->type->der_encoder(elm->type, memb_ptr,
544 elm->tag_mode, elm->tag,
545 cb, app_key);
546 if(tmperval.encoded == -1)
547 return tmperval;
548 computed_size -= tmperval.encoded;
549 ASN_DEBUG("Member %d %s of SEQUENCE %s encoded in %d bytes",
vlme413c122004-08-20 13:23:42 +0000550 edx, elm->name, td->name, tmperval.encoded);
vlmfa67ddc2004-06-03 03:38:44 +0000551 }
552
553 if(computed_size != 0) {
554 /*
555 * Encoded size is not equal to the computed size.
556 */
557 erval.encoded = -1;
vlme413c122004-08-20 13:23:42 +0000558 erval.failed_type = td;
vlmfa67ddc2004-06-03 03:38:44 +0000559 erval.structure_ptr = ptr;
560 }
561
562 return erval;
563}
564
565int
566SEQUENCE_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
567 asn_app_consume_bytes_f *cb, void *app_key) {
vlmfa67ddc2004-06-03 03:38:44 +0000568 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
vlme413c122004-08-20 13:23:42 +0000578 for(edx = 0; edx < td->elements_count; edx++) {
579 asn1_TYPE_member_t *elm = &td->elements[edx];
vlmfa67ddc2004-06-03 03:38:44 +0000580 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) {
vlmfa67ddc2004-06-03 03:38:44 +0000615 int edx;
616
617 if(!td || !sptr)
618 return;
619
620 ASN_DEBUG("Freeing %s as SEQUENCE", td->name);
621
vlme413c122004-08-20 13:23:42 +0000622 for(edx = 0; edx < td->elements_count; edx++) {
623 asn1_TYPE_member_t *elm = &td->elements[edx];
vlmfa67ddc2004-06-03 03:38:44 +0000624 void *memb_ptr;
625 if(elm->optional) {
626 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
627 if(memb_ptr)
628 elm->type->free_struct(elm->type, memb_ptr, 0);
629 } else {
630 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
631 elm->type->free_struct(elm->type, memb_ptr, 1);
632 }
633 }
634
635 if(!contents_only) {
636 FREEMEM(sptr);
637 }
638}
639
640int
641SEQUENCE_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
642 asn_app_consume_bytes_f *app_errlog, void *app_key) {
vlmfa67ddc2004-06-03 03:38:44 +0000643 int edx;
644
645 if(!sptr) {
vlme3f0f282004-08-11 09:44:13 +0000646 _ASN_ERRLOG(app_errlog, app_key,
vlm758530a2004-08-22 13:47:59 +0000647 "%s: value not given (%s:%d)",
648 td->name, __FILE__, __LINE__);
vlmfa67ddc2004-06-03 03:38:44 +0000649 return -1;
650 }
651
652 /*
653 * Iterate over structure members and check their validity.
654 */
vlme413c122004-08-20 13:23:42 +0000655 for(edx = 0; edx < td->elements_count; edx++) {
656 asn1_TYPE_member_t *elm = &td->elements[edx];
vlmfa67ddc2004-06-03 03:38:44 +0000657 const void *memb_ptr;
658
659 if(elm->optional) {
vlm31473082004-06-06 07:20:02 +0000660 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
vlmfa67ddc2004-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
vlme413c122004-08-20 13:23:42 +0000666 if(elm->memb_constraints) {
667 int ret = elm->memb_constraints(elm->type, memb_ptr,
668 app_errlog, app_key);
669 if(ret) return ret;
670 } else {
671 int ret = elm->type->check_constraints(elm->type,
672 memb_ptr, app_errlog, app_key);
673 if(ret) return ret;
674 /*
675 * Cannot inherit it earlier:
676 * need to make sure we get the updated version.
677 */
678 elm->memb_constraints = elm->type->check_constraints;
679 }
vlmfa67ddc2004-06-03 03:38:44 +0000680 }
681
682 return 0;
683}