blob: 67c916e686641e40ed66437dafbcba55bfa9a138 [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 Walkinec1ffd42004-08-18 04:53:32 +000013#define LEFT ((size<(size_t)ctx->left)?size:(size_t)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 Walkin4ce78ca2004-08-25 01:34:11 +000034 ptr = ((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) {
Lev Walkinc2346572004-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
Lev Walkin4c36e302004-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
Lev Walkin924fa032004-06-14 13:42:23 +000082 if(a_value == b_value) {
Lev Walkin50299c12004-06-14 14:11:14 +000083 if(a->el_no > b->el_no)
84 return 1;
Lev Walkin924fa032004-06-14 13:42:23 +000085 /*
86 * Important: we do not check
Lev Walkin50299c12004-06-14 14:11:14 +000087 * for a->el_no <= b->el_no!
Lev Walkin924fa032004-06-14 13:42:23 +000088 */
Lev Walkin4c36e302004-06-06 07:20:02 +000089 return 0;
Lev Walkin924fa032004-06-14 13:42:23 +000090 } else if(a_value < b_value)
Lev Walkin4c36e302004-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
Lev Walkinf15320b2004-06-03 03:38:44 +0000102/*
103 * The decoder of the SEQUENCE type.
104 */
105ber_dec_rval_t
Lev Walkin449f8322004-08-20 13:23:42 +0000106SEQUENCE_decode_ber(asn1_TYPE_descriptor_t *td,
Lev Walkinf15320b2004-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 */
Lev Walkin449f8322004-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;
Lev Walkinf15320b2004-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
Lev Walkin449f8322004-08-20 13:23:42 +0000127 ASN_DEBUG("Decoding %s as SEQUENCE", td->name);
Lev Walkinf15320b2004-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 */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000142 ctx = (ber_dec_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkinf15320b2004-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
Lev Walkin449f8322004-08-20 13:23:42 +0000155 rval = ber_check_tags(td, ctx, ptr, size,
Lev Walkinf15320b2004-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",
Lev Walkin449f8322004-08-20 13:23:42 +0000159 td->name, rval.code);
Lev Walkinf15320b2004-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 */
Lev Walkin449f8322004-08-20 13:23:42 +0000185 for(edx = (ctx->step >> 1); edx < td->elements_count;
Lev Walkinf15320b2004-06-03 03:38:44 +0000186 edx++, ctx->step = (ctx->step & ~1) + 2) {
187 void *memb_ptr; /* Pointer to the member */
Lev Walkinc2346572004-08-11 09:07:36 +0000188 void **memb_ptr2; /* Pointer to that pointer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000189 ssize_t tag_len; /* Length of TLV's T */
190 int opt_edx_end; /* Next non-optional element */
Lev Walkin4c36e302004-06-06 07:20:02 +0000191 int use_bsearch;
Lev Walkinf15320b2004-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",
Lev Walkin449f8322004-08-20 13:23:42 +0000201 td->name, (int)ctx->left,
202 edx, elements[edx].optional, td->elements_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000203
204 if(ctx->left == 0 /* No more stuff is expected */
205 && (
206 /* Explicit OPTIONAL specification reaches the end */
Lev Walkin449f8322004-08-20 13:23:42 +0000207 (edx + elements[edx].optional == td->elements_count)
Lev Walkinf15320b2004-06-03 03:38:44 +0000208 ||
209 /* All extensions are optional */
210 (IN_EXTENSION_GROUP(specs, edx)
Lev Walkin449f8322004-08-20 13:23:42 +0000211 && specs->ext_before > td->elements_count)
Lev Walkinf15320b2004-06-03 03:38:44 +0000212 )
213 ) {
Lev Walkin449f8322004-08-20 13:23:42 +0000214 ASN_DEBUG("End of SEQUENCE %s", td->name);
Lev Walkinf15320b2004-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",
Lev Walkin449f8322004-08-20 13:23:42 +0000227 td->name, edx, elements[edx].name, (int)tag_len);
Lev Walkinf15320b2004-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 */
Lev Walkin4c36e302004-06-06 07:20:02 +0000237 use_bsearch = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000238 opt_edx_end = edx + elements[edx].optional + 1;
Lev Walkin449f8322004-08-20 13:23:42 +0000239 if(opt_edx_end > td->elements_count)
240 opt_edx_end = td->elements_count; /* Cap */
Lev Walkinf9dbd9a2004-07-01 00:48:04 +0000241 else if(opt_edx_end - edx > 8) {
Lev Walkin924fa032004-06-14 13:42:23 +0000242 /* Limit the scope of linear search... */
Lev Walkinf9dbd9a2004-07-01 00:48:04 +0000243 opt_edx_end = edx + 8;
Lev Walkin4c36e302004-06-06 07:20:02 +0000244 use_bsearch = 1;
Lev Walkin924fa032004-06-14 13:42:23 +0000245 /* ... and resort to bsearch() */
Lev Walkin4c36e302004-06-06 07:20:02 +0000246 }
Lev Walkinf15320b2004-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;
Lev Walkin4c36e302004-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;
Lev Walkinf15320b2004-06-03 03:38:44 +0000259 break;
260 }
261 }
Lev Walkin4c36e302004-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;
Lev Walkin924fa032004-06-14 13:42:23 +0000270 key.el_no = edx;
Lev Walkinc2346572004-08-11 09:07:36 +0000271 (void *)t2m = bsearch(&key,
272 specs->tag2el, specs->tag2el_count,
Lev Walkin4c36e302004-06-06 07:20:02 +0000273 sizeof(specs->tag2el[0]), _t2e_cmp);
Lev Walkin924fa032004-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;
Lev Walkin4c36e302004-06-06 07:20:02 +0000278 /*
279 * Rewind to the first element with that tag,
280 * `cause bsearch() does not guarantee order.
281 */
Lev Walkin924fa032004-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 }
Lev Walkin4c36e302004-06-06 07:20:02 +0000294 }
295 n = opt_edx_end;
296 }
Lev Walkinf15320b2004-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)) {
Lev Walkin60b7cff2004-09-04 04:44:11 +0000306 if(elements[edx].tag == (ber_tlv_tag_t)-1
307 && elements[edx].optional == 0) {
308 /*
309 * This must be the ANY type.
310 */
311 ctx->step |= 1;
312 goto microphase2;
313 }
314
Lev Walkinf15320b2004-06-03 03:38:44 +0000315 ASN_DEBUG("Unexpected tag %s",
316 ber_tlv_tag_string(tlv_tag));
Lev Walkin4c36e302004-06-06 07:20:02 +0000317 ASN_DEBUG("Expected tag %s (%s)%s",
Lev Walkinf15320b2004-06-03 03:38:44 +0000318 ber_tlv_tag_string(elements[edx].tag),
Lev Walkin4c36e302004-06-06 07:20:02 +0000319 elements[edx].name,
Lev Walkinf15320b2004-06-03 03:38:44 +0000320 elements[edx].optional
321 ?" or alternatives":"");
322 RETURN(RC_FAIL);
323 }
324
325 if(ctx->left < 0
326 && ((uint8_t *)ptr)[0] == 0) {
327 if(LEFT < 2) {
328 if(SIZE_VIOLATION)
329 RETURN(RC_FAIL);
330 else
331 RETURN(RC_WMORE);
332 } else if(((uint8_t *)ptr)[1] == 0) {
333 /*
334 * Yeah, baby! Found the terminator
335 * of the indefinite length structure.
336 */
337 /*
338 * Proceed to the canonical
339 * finalization function.
340 * No advancing is necessary.
341 */
342 goto phase3;
343 }
344 } else {
345 /* Skip this tag */
346 ssize_t skip;
347
348 skip = ber_skip_length(
349 BER_TLV_CONSTRUCTED(ptr),
Lev Walkin4d9528c2004-08-11 08:10:13 +0000350 (char *)ptr + tag_len, LEFT - tag_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000351 ASN_DEBUG("Skip length %d in %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000352 (int)skip, td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000353 switch(skip) {
354 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
355 /* Fall through */
356 case -1: RETURN(RC_FAIL);
357 }
358
359 ADVANCE(skip + tag_len);
360 ctx->step -= 2;
361 edx--;
362 continue; /* Try again with the next tag */
363 }
364 }
365
366 /*
367 * MICROPHASE 2: Invoke the member-specific decoder.
368 */
369 ctx->step |= 1; /* Confirm entering next microphase */
370 microphase2:
Lev Walkin449f8322004-08-20 13:23:42 +0000371 ASN_DEBUG("Inside SEQUENCE %s MF2", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000372
373 /*
374 * Compute the position of the member inside a structure,
375 * and also a type of containment (it may be contained
376 * as pointer or using inline inclusion).
377 */
378 if(elements[edx].optional) {
379 /* Optional member, hereby, a simple pointer */
Lev Walkinc2346572004-08-11 09:07:36 +0000380 memb_ptr2 = (void **)((char *)st + elements[edx].memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000381 } else {
382 /*
383 * A pointer to a pointer
384 * holding the start of the structure
385 */
386 memb_ptr = (char *)st + elements[edx].memb_offset;
387 memb_ptr2 = &memb_ptr;
388 }
389 /*
390 * Invoke the member fetch routine according to member's type
391 */
392 rval = elements[edx].type->ber_decoder(
Lev Walkinc2346572004-08-11 09:07:36 +0000393 elements[edx].type,
Lev Walkinf15320b2004-06-03 03:38:44 +0000394 memb_ptr2, ptr, LEFT,
395 elements[edx].tag_mode);
Lev Walkin60b7cff2004-09-04 04:44:11 +0000396 ASN_DEBUG("In %s SEQUENCE decoded %d %s of %d "
397 "in %d bytes rval.code %d",
Lev Walkin449f8322004-08-20 13:23:42 +0000398 td->name, edx, elements[edx].type->name,
Lev Walkin60b7cff2004-09-04 04:44:11 +0000399 (int)LEFT, (int)rval.consumed, rval.code);
Lev Walkinf15320b2004-06-03 03:38:44 +0000400 switch(rval.code) {
401 case RC_OK:
402 break;
403 case RC_WMORE: /* More data expected */
404 if(!SIZE_VIOLATION) {
405 ADVANCE(rval.consumed);
406 RETURN(RC_WMORE);
407 }
408 /* Fall through */
409 case RC_FAIL: /* Fatal error */
410 RETURN(RC_FAIL);
411 } /* switch(rval) */
412
413 ADVANCE(rval.consumed);
414 } /* for(all structure members) */
415
416 phase3:
417 ctx->phase = 3;
418 case 3: /* 00 and other tags expected */
419 case 4: /* only 00's expected */
420
421 ASN_DEBUG("SEQUENCE %s Leftover: %ld, size = %ld",
Lev Walkin449f8322004-08-20 13:23:42 +0000422 td->name, (long)ctx->left, (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000423
424 /*
425 * Skip everything until the end of the SEQUENCE.
426 */
427 while(ctx->left) {
428 ssize_t tl, ll;
429
430 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
431 switch(tl) {
432 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
433 /* Fall through */
434 case -1: RETURN(RC_FAIL);
435 }
436
437 /*
438 * If expected <0><0>...
439 */
440 if(ctx->left < 0
441 && ((uint8_t *)ptr)[0] == 0) {
442 if(LEFT < 2) {
443 if(SIZE_VIOLATION)
444 RETURN(RC_FAIL);
445 else
446 RETURN(RC_WMORE);
447 } else if(((uint8_t *)ptr)[1] == 0) {
448 /*
449 * Correctly finished with <0><0>.
450 */
451 ADVANCE(2);
452 ctx->left++;
453 ctx->phase = 4;
454 continue;
455 }
456 }
457
Lev Walkin449f8322004-08-20 13:23:42 +0000458 if(!IN_EXTENSION_GROUP(specs, td->elements_count)
Lev Walkinf15320b2004-06-03 03:38:44 +0000459 || ctx->phase == 4) {
460 ASN_DEBUG("Unexpected continuation "
461 "of a non-extensible type "
462 "%s (SEQUENCE): %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000463 td->name,
Lev Walkinf15320b2004-06-03 03:38:44 +0000464 ber_tlv_tag_string(tlv_tag));
465 RETURN(RC_FAIL);
466 }
467
468 ll = ber_skip_length(
469 BER_TLV_CONSTRUCTED(ptr),
Lev Walkin4d9528c2004-08-11 08:10:13 +0000470 (char *)ptr + tl, LEFT - tl);
Lev Walkinf15320b2004-06-03 03:38:44 +0000471 switch(ll) {
472 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
473 /* Fall through */
474 case -1: RETURN(RC_FAIL);
475 }
476
477 ADVANCE(tl + ll);
478 }
479
480 PHASE_OUT(ctx);
481 }
482
483 RETURN(RC_OK);
484}
485
Lev Walkin4c36e302004-06-06 07:20:02 +0000486
Lev Walkinf15320b2004-06-03 03:38:44 +0000487/*
488 * The DER encoder of the SEQUENCE type.
489 */
490der_enc_rval_t
Lev Walkin449f8322004-08-20 13:23:42 +0000491SEQUENCE_encode_der(asn1_TYPE_descriptor_t *td,
Lev Walkinf15320b2004-06-03 03:38:44 +0000492 void *ptr, int tag_mode, ber_tlv_tag_t tag,
493 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000494 size_t computed_size = 0;
495 der_enc_rval_t erval;
496 ssize_t ret;
497 int edx;
498
499 ASN_DEBUG("%s %s as SEQUENCE",
Lev Walkin449f8322004-08-20 13:23:42 +0000500 cb?"Encoding":"Estimating", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000501
502 /*
503 * Gather the length of the underlying members sequence.
504 */
Lev Walkin449f8322004-08-20 13:23:42 +0000505 for(edx = 0; edx < td->elements_count; edx++) {
506 asn1_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000507 void *memb_ptr;
508 if(elm->optional) {
509 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
510 if(!memb_ptr) continue;
511 } else {
512 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
513 }
514 erval = elm->type->der_encoder(elm->type, memb_ptr,
515 elm->tag_mode, elm->tag,
516 0, 0);
517 if(erval.encoded == -1)
518 return erval;
519 computed_size += erval.encoded;
520 ASN_DEBUG("Member %d %s estimated %ld bytes",
521 edx, elm->name, (long)erval.encoded);
522 }
523
524 /*
525 * Encode the TLV for the sequence itself.
526 */
Lev Walkin449f8322004-08-20 13:23:42 +0000527 ret = der_write_tags(td, computed_size, tag_mode, tag, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +0000528 ASN_DEBUG("Wrote tags: %ld (+%ld)", (long)ret, (long)computed_size);
529 if(ret == -1) {
530 erval.encoded = -1;
Lev Walkin449f8322004-08-20 13:23:42 +0000531 erval.failed_type = td;
Lev Walkinf15320b2004-06-03 03:38:44 +0000532 erval.structure_ptr = ptr;
533 return erval;
534 }
535 erval.encoded = computed_size + ret;
536
537 if(!cb) return erval;
538
539 /*
540 * Encode all members.
541 */
Lev Walkin449f8322004-08-20 13:23:42 +0000542 for(edx = 0; edx < td->elements_count; edx++) {
543 asn1_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000544 der_enc_rval_t tmperval;
545 void *memb_ptr;
546
547 if(elm->optional) {
548 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
549 if(!memb_ptr) continue;
550 } else {
551 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
552 }
553 tmperval = elm->type->der_encoder(elm->type, memb_ptr,
554 elm->tag_mode, elm->tag,
555 cb, app_key);
556 if(tmperval.encoded == -1)
557 return tmperval;
558 computed_size -= tmperval.encoded;
559 ASN_DEBUG("Member %d %s of SEQUENCE %s encoded in %d bytes",
Lev Walkin449f8322004-08-20 13:23:42 +0000560 edx, elm->name, td->name, tmperval.encoded);
Lev Walkinf15320b2004-06-03 03:38:44 +0000561 }
562
563 if(computed_size != 0) {
564 /*
565 * Encoded size is not equal to the computed size.
566 */
567 erval.encoded = -1;
Lev Walkin449f8322004-08-20 13:23:42 +0000568 erval.failed_type = td;
Lev Walkinf15320b2004-06-03 03:38:44 +0000569 erval.structure_ptr = ptr;
570 }
571
572 return erval;
573}
574
575int
576SEQUENCE_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
577 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000578 int edx;
579 int ret;
580
581 if(!sptr) return cb("<absent>", 8, app_key);
582
583 /* Dump preamble */
584 if(cb(td->name, strlen(td->name), app_key)
585 || cb(" ::= {\n", 7, app_key))
586 return -1;
587
Lev Walkin449f8322004-08-20 13:23:42 +0000588 for(edx = 0; edx < td->elements_count; edx++) {
589 asn1_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000590 const void *memb_ptr;
591
592 if(elm->optional) {
593 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
594 if(!memb_ptr) continue;
595 } else {
596 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
597 }
598
599 /* Indentation */
600 for(ret = 0; ret < ilevel; ret++) cb(" ", 1, app_key);
601
602 /* Print the member's name and stuff */
603 if(cb(elm->name, strlen(elm->name), app_key)
604 || cb(": ", 2, app_key))
605 return -1;
606
607 /* Print the member itself */
608 ret = elm->type->print_struct(elm->type, memb_ptr, ilevel + 4,
609 cb, app_key);
610 if(ret) return ret;
611
612 /* Print out the terminator */
613 ret = cb("\n", 1, app_key);
614 if(ret) return ret;
615 }
616
617 /* Indentation */
618 for(ret = 0; ret < ilevel - 4; ret++) cb(" ", 1, app_key);
619
620 return cb("}", 1, app_key);
621}
622
623void
624SEQUENCE_free(asn1_TYPE_descriptor_t *td, void *sptr, int contents_only) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000625 int edx;
626
627 if(!td || !sptr)
628 return;
629
630 ASN_DEBUG("Freeing %s as SEQUENCE", td->name);
631
Lev Walkin449f8322004-08-20 13:23:42 +0000632 for(edx = 0; edx < td->elements_count; edx++) {
633 asn1_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000634 void *memb_ptr;
635 if(elm->optional) {
636 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
637 if(memb_ptr)
638 elm->type->free_struct(elm->type, memb_ptr, 0);
639 } else {
640 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
641 elm->type->free_struct(elm->type, memb_ptr, 1);
642 }
643 }
644
645 if(!contents_only) {
646 FREEMEM(sptr);
647 }
648}
649
650int
651SEQUENCE_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
652 asn_app_consume_bytes_f *app_errlog, void *app_key) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000653 int edx;
654
655 if(!sptr) {
Lev Walkinba4e5182004-08-11 09:44:13 +0000656 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkin16835b62004-08-22 13:47:59 +0000657 "%s: value not given (%s:%d)",
658 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000659 return -1;
660 }
661
662 /*
663 * Iterate over structure members and check their validity.
664 */
Lev Walkin449f8322004-08-20 13:23:42 +0000665 for(edx = 0; edx < td->elements_count; edx++) {
666 asn1_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000667 const void *memb_ptr;
668
669 if(elm->optional) {
Lev Walkin4c36e302004-06-06 07:20:02 +0000670 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000671 if(!memb_ptr) continue;
672 } else {
673 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
674 }
675
Lev Walkin449f8322004-08-20 13:23:42 +0000676 if(elm->memb_constraints) {
677 int ret = elm->memb_constraints(elm->type, memb_ptr,
678 app_errlog, app_key);
679 if(ret) return ret;
680 } else {
681 int ret = elm->type->check_constraints(elm->type,
682 memb_ptr, app_errlog, app_key);
683 if(ret) return ret;
684 /*
685 * Cannot inherit it earlier:
686 * need to make sure we get the updated version.
687 */
688 elm->memb_constraints = elm->type->check_constraints;
689 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000690 }
691
692 return 0;
693}