blob: 36412a657c084ac458412da1ac1ed54db06c1b51 [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);
vlm1308d2b2004-09-10 15:49:15 +0000229 ASN_DEBUG("Current tag in %s SEQUENCE for element %d "
230 "(%s) is %s encoded in %d bytes, left %ld",
231 td->name, edx, elements[edx].name,
232 ber_tlv_tag_string(tlv_tag), (int)tag_len, (long)LEFT);
vlmfa67ddc2004-06-03 03:38:44 +0000233 switch(tag_len) {
234 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
235 /* Fall through */
236 case -1: RETURN(RC_FAIL);
237 }
238
239 /*
240 * Find the next available type with this tag.
241 */
vlm31473082004-06-06 07:20:02 +0000242 use_bsearch = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000243 opt_edx_end = edx + elements[edx].optional + 1;
vlme413c122004-08-20 13:23:42 +0000244 if(opt_edx_end > td->elements_count)
245 opt_edx_end = td->elements_count; /* Cap */
vlm5d219bf2004-07-01 00:48:04 +0000246 else if(opt_edx_end - edx > 8) {
vlm1692da02004-06-14 13:42:23 +0000247 /* Limit the scope of linear search... */
vlm5d219bf2004-07-01 00:48:04 +0000248 opt_edx_end = edx + 8;
vlm31473082004-06-06 07:20:02 +0000249 use_bsearch = 1;
vlm1692da02004-06-14 13:42:23 +0000250 /* ... and resort to bsearch() */
vlm31473082004-06-06 07:20:02 +0000251 }
vlmfa67ddc2004-06-03 03:38:44 +0000252 for(n = edx; n < opt_edx_end; n++) {
253 if(BER_TAGS_EQUAL(tlv_tag, elements[n].tag)) {
254 /*
255 * Found element corresponding to the tag
256 * being looked at.
257 * Reposition over the right element.
258 */
259 edx = n;
vlm31473082004-06-06 07:20:02 +0000260 ctx->step = 1 + 2 * edx; /* Remember! */
261 goto microphase2;
vlm060fe2a2004-09-10 09:37:12 +0000262 } else if(elements[n].flags & ATF_OPEN_TYPE) {
263 /*
264 * This is the ANY type, which may bear
265 * any flag whatsoever.
266 */
267 edx = n;
268 ctx->step = 1 + 2 * edx; /* Remember! */
269 goto microphase2;
vlm31473082004-06-06 07:20:02 +0000270 } else if(elements[n].tag == (ber_tlv_tag_t)-1) {
271 use_bsearch = 1;
vlmfa67ddc2004-06-03 03:38:44 +0000272 break;
273 }
274 }
vlm31473082004-06-06 07:20:02 +0000275 if(use_bsearch) {
276 /*
vlm141874a2004-09-04 06:17:35 +0000277 * Resort to a binary search over
vlm31473082004-06-06 07:20:02 +0000278 * sorted array of tags.
279 */
280 asn1_TYPE_tag2member_t *t2m;
281 asn1_TYPE_tag2member_t key;
282 key.el_tag = tlv_tag;
vlm1692da02004-06-14 13:42:23 +0000283 key.el_no = edx;
vlmda674682004-08-11 09:07:36 +0000284 (void *)t2m = bsearch(&key,
285 specs->tag2el, specs->tag2el_count,
vlm31473082004-06-06 07:20:02 +0000286 sizeof(specs->tag2el[0]), _t2e_cmp);
vlm1692da02004-06-14 13:42:23 +0000287 if(t2m) {
288 asn1_TYPE_tag2member_t *best = 0;
289 asn1_TYPE_tag2member_t *t2m_f, *t2m_l;
290 int edx_max = edx + elements[edx].optional;
vlm31473082004-06-06 07:20:02 +0000291 /*
292 * Rewind to the first element with that tag,
293 * `cause bsearch() does not guarantee order.
294 */
vlm1692da02004-06-14 13:42:23 +0000295 t2m_f = t2m + t2m->toff_first;
296 t2m_l = t2m + t2m->toff_last;
297 for(t2m = t2m_f; t2m <= t2m_l; t2m++) {
298 if(t2m->el_no > edx_max) break;
299 if(t2m->el_no < edx) continue;
300 best = t2m;
301 }
302 if(best) {
303 edx = best->el_no;
304 ctx->step = 1 + 2 * edx;
305 goto microphase2;
306 }
vlm31473082004-06-06 07:20:02 +0000307 }
308 n = opt_edx_end;
309 }
vlmfa67ddc2004-06-03 03:38:44 +0000310 if(n == opt_edx_end) {
311 /*
312 * If tag is unknown, it may be either
313 * an unknown (thus, incorrect) tag,
314 * or an extension (...),
315 * or an end of the indefinite-length structure.
316 */
vlmfa67ddc2004-06-03 03:38:44 +0000317 if(!IN_EXTENSION_GROUP(specs, edx)) {
318 ASN_DEBUG("Unexpected tag %s",
319 ber_tlv_tag_string(tlv_tag));
vlm31473082004-06-06 07:20:02 +0000320 ASN_DEBUG("Expected tag %s (%s)%s",
vlmfa67ddc2004-06-03 03:38:44 +0000321 ber_tlv_tag_string(elements[edx].tag),
vlm31473082004-06-06 07:20:02 +0000322 elements[edx].name,
vlmfa67ddc2004-06-03 03:38:44 +0000323 elements[edx].optional
324 ?" or alternatives":"");
325 RETURN(RC_FAIL);
326 }
327
328 if(ctx->left < 0
329 && ((uint8_t *)ptr)[0] == 0) {
330 if(LEFT < 2) {
331 if(SIZE_VIOLATION)
332 RETURN(RC_FAIL);
333 else
334 RETURN(RC_WMORE);
335 } else if(((uint8_t *)ptr)[1] == 0) {
336 /*
337 * Yeah, baby! Found the terminator
338 * of the indefinite length structure.
339 */
340 /*
341 * Proceed to the canonical
342 * finalization function.
343 * No advancing is necessary.
344 */
345 goto phase3;
346 }
347 } else {
348 /* Skip this tag */
349 ssize_t skip;
350
351 skip = ber_skip_length(
352 BER_TLV_CONSTRUCTED(ptr),
vlm1ff928d2004-08-11 08:10:13 +0000353 (char *)ptr + tag_len, LEFT - tag_len);
vlmfa67ddc2004-06-03 03:38:44 +0000354 ASN_DEBUG("Skip length %d in %s",
vlme413c122004-08-20 13:23:42 +0000355 (int)skip, td->name);
vlmfa67ddc2004-06-03 03:38:44 +0000356 switch(skip) {
357 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
358 /* Fall through */
359 case -1: RETURN(RC_FAIL);
360 }
361
362 ADVANCE(skip + tag_len);
363 ctx->step -= 2;
364 edx--;
365 continue; /* Try again with the next tag */
366 }
367 }
368
369 /*
370 * MICROPHASE 2: Invoke the member-specific decoder.
371 */
372 ctx->step |= 1; /* Confirm entering next microphase */
373 microphase2:
vlme413c122004-08-20 13:23:42 +0000374 ASN_DEBUG("Inside SEQUENCE %s MF2", td->name);
vlmfa67ddc2004-06-03 03:38:44 +0000375
376 /*
377 * Compute the position of the member inside a structure,
378 * and also a type of containment (it may be contained
379 * as pointer or using inline inclusion).
380 */
vlmddd5a7d2004-09-10 09:18:20 +0000381 if(elements[edx].flags & ATF_POINTER) {
382 /* Member is a pointer to another structure */
vlmda674682004-08-11 09:07:36 +0000383 memb_ptr2 = (void **)((char *)st + elements[edx].memb_offset);
vlmfa67ddc2004-06-03 03:38:44 +0000384 } else {
385 /*
386 * A pointer to a pointer
387 * holding the start of the structure
388 */
389 memb_ptr = (char *)st + elements[edx].memb_offset;
390 memb_ptr2 = &memb_ptr;
391 }
392 /*
393 * Invoke the member fetch routine according to member's type
394 */
395 rval = elements[edx].type->ber_decoder(
vlmda674682004-08-11 09:07:36 +0000396 elements[edx].type,
vlmfa67ddc2004-06-03 03:38:44 +0000397 memb_ptr2, ptr, LEFT,
398 elements[edx].tag_mode);
vlmef16e8c2004-09-04 04:44:11 +0000399 ASN_DEBUG("In %s SEQUENCE decoded %d %s of %d "
400 "in %d bytes rval.code %d",
vlme413c122004-08-20 13:23:42 +0000401 td->name, edx, elements[edx].type->name,
vlmef16e8c2004-09-04 04:44:11 +0000402 (int)LEFT, (int)rval.consumed, rval.code);
vlmfa67ddc2004-06-03 03:38:44 +0000403 switch(rval.code) {
404 case RC_OK:
405 break;
406 case RC_WMORE: /* More data expected */
407 if(!SIZE_VIOLATION) {
408 ADVANCE(rval.consumed);
409 RETURN(RC_WMORE);
410 }
411 /* Fall through */
412 case RC_FAIL: /* Fatal error */
413 RETURN(RC_FAIL);
414 } /* switch(rval) */
415
416 ADVANCE(rval.consumed);
417 } /* for(all structure members) */
418
419 phase3:
420 ctx->phase = 3;
421 case 3: /* 00 and other tags expected */
422 case 4: /* only 00's expected */
423
424 ASN_DEBUG("SEQUENCE %s Leftover: %ld, size = %ld",
vlme413c122004-08-20 13:23:42 +0000425 td->name, (long)ctx->left, (long)size);
vlmfa67ddc2004-06-03 03:38:44 +0000426
427 /*
428 * Skip everything until the end of the SEQUENCE.
429 */
430 while(ctx->left) {
431 ssize_t tl, ll;
432
433 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
434 switch(tl) {
435 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
436 /* Fall through */
437 case -1: RETURN(RC_FAIL);
438 }
439
440 /*
441 * If expected <0><0>...
442 */
443 if(ctx->left < 0
444 && ((uint8_t *)ptr)[0] == 0) {
445 if(LEFT < 2) {
446 if(SIZE_VIOLATION)
447 RETURN(RC_FAIL);
448 else
449 RETURN(RC_WMORE);
450 } else if(((uint8_t *)ptr)[1] == 0) {
451 /*
452 * Correctly finished with <0><0>.
453 */
454 ADVANCE(2);
455 ctx->left++;
456 ctx->phase = 4;
457 continue;
458 }
459 }
460
vlme413c122004-08-20 13:23:42 +0000461 if(!IN_EXTENSION_GROUP(specs, td->elements_count)
vlmfa67ddc2004-06-03 03:38:44 +0000462 || ctx->phase == 4) {
463 ASN_DEBUG("Unexpected continuation "
464 "of a non-extensible type "
465 "%s (SEQUENCE): %s",
vlme413c122004-08-20 13:23:42 +0000466 td->name,
vlmfa67ddc2004-06-03 03:38:44 +0000467 ber_tlv_tag_string(tlv_tag));
468 RETURN(RC_FAIL);
469 }
470
471 ll = ber_skip_length(
472 BER_TLV_CONSTRUCTED(ptr),
vlm1ff928d2004-08-11 08:10:13 +0000473 (char *)ptr + tl, LEFT - tl);
vlmfa67ddc2004-06-03 03:38:44 +0000474 switch(ll) {
475 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
476 /* Fall through */
477 case -1: RETURN(RC_FAIL);
478 }
479
480 ADVANCE(tl + ll);
481 }
482
483 PHASE_OUT(ctx);
484 }
485
486 RETURN(RC_OK);
487}
488
vlm31473082004-06-06 07:20:02 +0000489
vlmfa67ddc2004-06-03 03:38:44 +0000490/*
491 * The DER encoder of the SEQUENCE type.
492 */
493der_enc_rval_t
vlme413c122004-08-20 13:23:42 +0000494SEQUENCE_encode_der(asn1_TYPE_descriptor_t *td,
vlmfa67ddc2004-06-03 03:38:44 +0000495 void *ptr, int tag_mode, ber_tlv_tag_t tag,
496 asn_app_consume_bytes_f *cb, void *app_key) {
vlmfa67ddc2004-06-03 03:38:44 +0000497 size_t computed_size = 0;
498 der_enc_rval_t erval;
499 ssize_t ret;
500 int edx;
501
502 ASN_DEBUG("%s %s as SEQUENCE",
vlme413c122004-08-20 13:23:42 +0000503 cb?"Encoding":"Estimating", td->name);
vlmfa67ddc2004-06-03 03:38:44 +0000504
505 /*
506 * Gather the length of the underlying members sequence.
507 */
vlme413c122004-08-20 13:23:42 +0000508 for(edx = 0; edx < td->elements_count; edx++) {
509 asn1_TYPE_member_t *elm = &td->elements[edx];
vlmfa67ddc2004-06-03 03:38:44 +0000510 void *memb_ptr;
vlmddd5a7d2004-09-10 09:18:20 +0000511 if(elm->flags & ATF_POINTER) {
vlmfa67ddc2004-06-03 03:38:44 +0000512 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
513 if(!memb_ptr) continue;
514 } else {
515 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
516 }
517 erval = elm->type->der_encoder(elm->type, memb_ptr,
518 elm->tag_mode, elm->tag,
519 0, 0);
520 if(erval.encoded == -1)
521 return erval;
522 computed_size += erval.encoded;
523 ASN_DEBUG("Member %d %s estimated %ld bytes",
524 edx, elm->name, (long)erval.encoded);
525 }
526
527 /*
528 * Encode the TLV for the sequence itself.
529 */
vlme413c122004-08-20 13:23:42 +0000530 ret = der_write_tags(td, computed_size, tag_mode, tag, cb, app_key);
vlmfa67ddc2004-06-03 03:38:44 +0000531 ASN_DEBUG("Wrote tags: %ld (+%ld)", (long)ret, (long)computed_size);
532 if(ret == -1) {
533 erval.encoded = -1;
vlme413c122004-08-20 13:23:42 +0000534 erval.failed_type = td;
vlmfa67ddc2004-06-03 03:38:44 +0000535 erval.structure_ptr = ptr;
536 return erval;
537 }
538 erval.encoded = computed_size + ret;
539
540 if(!cb) return erval;
541
542 /*
543 * Encode all members.
544 */
vlme413c122004-08-20 13:23:42 +0000545 for(edx = 0; edx < td->elements_count; edx++) {
546 asn1_TYPE_member_t *elm = &td->elements[edx];
vlmfa67ddc2004-06-03 03:38:44 +0000547 der_enc_rval_t tmperval;
548 void *memb_ptr;
549
vlmddd5a7d2004-09-10 09:18:20 +0000550 if(elm->flags & ATF_POINTER) {
vlmfa67ddc2004-06-03 03:38:44 +0000551 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
552 if(!memb_ptr) continue;
553 } else {
554 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
555 }
556 tmperval = elm->type->der_encoder(elm->type, memb_ptr,
557 elm->tag_mode, elm->tag,
558 cb, app_key);
559 if(tmperval.encoded == -1)
560 return tmperval;
561 computed_size -= tmperval.encoded;
562 ASN_DEBUG("Member %d %s of SEQUENCE %s encoded in %d bytes",
vlme413c122004-08-20 13:23:42 +0000563 edx, elm->name, td->name, tmperval.encoded);
vlmfa67ddc2004-06-03 03:38:44 +0000564 }
565
566 if(computed_size != 0) {
567 /*
568 * Encoded size is not equal to the computed size.
569 */
570 erval.encoded = -1;
vlme413c122004-08-20 13:23:42 +0000571 erval.failed_type = td;
vlmfa67ddc2004-06-03 03:38:44 +0000572 erval.structure_ptr = ptr;
573 }
574
575 return erval;
576}
577
578int
579SEQUENCE_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
580 asn_app_consume_bytes_f *cb, void *app_key) {
vlmfa67ddc2004-06-03 03:38:44 +0000581 int edx;
582 int ret;
583
584 if(!sptr) return cb("<absent>", 8, app_key);
585
586 /* Dump preamble */
587 if(cb(td->name, strlen(td->name), app_key)
588 || cb(" ::= {\n", 7, app_key))
589 return -1;
590
vlme413c122004-08-20 13:23:42 +0000591 for(edx = 0; edx < td->elements_count; edx++) {
592 asn1_TYPE_member_t *elm = &td->elements[edx];
vlmfa67ddc2004-06-03 03:38:44 +0000593 const void *memb_ptr;
594
vlmddd5a7d2004-09-10 09:18:20 +0000595 if(elm->flags & ATF_POINTER) {
vlmfa67ddc2004-06-03 03:38:44 +0000596 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
597 if(!memb_ptr) continue;
598 } else {
599 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
600 }
601
602 /* Indentation */
603 for(ret = 0; ret < ilevel; ret++) cb(" ", 1, app_key);
604
605 /* Print the member's name and stuff */
606 if(cb(elm->name, strlen(elm->name), app_key)
607 || cb(": ", 2, app_key))
608 return -1;
609
610 /* Print the member itself */
611 ret = elm->type->print_struct(elm->type, memb_ptr, ilevel + 4,
612 cb, app_key);
613 if(ret) return ret;
614
615 /* Print out the terminator */
616 ret = cb("\n", 1, app_key);
617 if(ret) return ret;
618 }
619
620 /* Indentation */
621 for(ret = 0; ret < ilevel - 4; ret++) cb(" ", 1, app_key);
622
623 return cb("}", 1, app_key);
624}
625
626void
627SEQUENCE_free(asn1_TYPE_descriptor_t *td, void *sptr, int contents_only) {
vlmfa67ddc2004-06-03 03:38:44 +0000628 int edx;
629
630 if(!td || !sptr)
631 return;
632
633 ASN_DEBUG("Freeing %s as SEQUENCE", td->name);
634
vlme413c122004-08-20 13:23:42 +0000635 for(edx = 0; edx < td->elements_count; edx++) {
636 asn1_TYPE_member_t *elm = &td->elements[edx];
vlmfa67ddc2004-06-03 03:38:44 +0000637 void *memb_ptr;
vlmddd5a7d2004-09-10 09:18:20 +0000638 if(elm->flags & ATF_POINTER) {
vlmfa67ddc2004-06-03 03:38:44 +0000639 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
640 if(memb_ptr)
641 elm->type->free_struct(elm->type, memb_ptr, 0);
642 } else {
643 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
644 elm->type->free_struct(elm->type, memb_ptr, 1);
645 }
646 }
647
648 if(!contents_only) {
649 FREEMEM(sptr);
650 }
651}
652
653int
654SEQUENCE_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
655 asn_app_consume_bytes_f *app_errlog, void *app_key) {
vlmfa67ddc2004-06-03 03:38:44 +0000656 int edx;
657
658 if(!sptr) {
vlme3f0f282004-08-11 09:44:13 +0000659 _ASN_ERRLOG(app_errlog, app_key,
vlm758530a2004-08-22 13:47:59 +0000660 "%s: value not given (%s:%d)",
661 td->name, __FILE__, __LINE__);
vlmfa67ddc2004-06-03 03:38:44 +0000662 return -1;
663 }
664
665 /*
666 * Iterate over structure members and check their validity.
667 */
vlme413c122004-08-20 13:23:42 +0000668 for(edx = 0; edx < td->elements_count; edx++) {
669 asn1_TYPE_member_t *elm = &td->elements[edx];
vlmfa67ddc2004-06-03 03:38:44 +0000670 const void *memb_ptr;
671
vlmddd5a7d2004-09-10 09:18:20 +0000672 if(elm->flags & ATF_POINTER) {
vlm31473082004-06-06 07:20:02 +0000673 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
vlmfa67ddc2004-06-03 03:38:44 +0000674 if(!memb_ptr) continue;
675 } else {
676 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
677 }
678
vlme413c122004-08-20 13:23:42 +0000679 if(elm->memb_constraints) {
680 int ret = elm->memb_constraints(elm->type, memb_ptr,
681 app_errlog, app_key);
682 if(ret) return ret;
683 } else {
684 int ret = elm->type->check_constraints(elm->type,
685 memb_ptr, app_errlog, app_key);
686 if(ret) return ret;
687 /*
688 * Cannot inherit it earlier:
689 * need to make sure we get the updated version.
690 */
691 elm->memb_constraints = elm->type->check_constraints;
692 }
vlmfa67ddc2004-06-03 03:38:44 +0000693 }
694
695 return 0;
696}