blob: 041f8c2d09dc6417836b043a9ab9a501c8d4c6f6 [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 */
Lev Walkina9cc46e2004-09-22 16:06:28 +00005#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00006#include <constr_SEQUENCE.h>
Lev Walkin4c36e302004-06-06 07:20:02 +00007#include <assert.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00008
9/*
10 * Number of bytes left for this structure.
11 * (ctx->left) indicates the number of bytes _transferred_ for the structure.
12 * (size) contains the number of bytes in the buffer passed.
13 */
Lev Walkinec1ffd42004-08-18 04:53:32 +000014#define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left)
Lev Walkinf15320b2004-06-03 03:38:44 +000015
16/*
17 * If the subprocessor function returns with an indication that it wants
18 * more data, it may well be a fatal decoding problem, because the
19 * size is constrained by the <TLV>'s L, even if the buffer size allows
20 * reading more data.
21 * For example, consider the buffer containing the following TLVs:
22 * <T:5><L:1><V> <T:6>...
23 * The TLV length clearly indicates that one byte is expected in V, but
24 * if the V processor returns with "want more data" even if the buffer
25 * contains way more data than the V processor have seen.
26 */
Lev Walkind9bd7752004-06-05 08:17:50 +000027#define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size)
Lev Walkinf15320b2004-06-03 03:38:44 +000028
29/*
30 * This macro "eats" the part of the buffer which is definitely "consumed",
31 * i.e. was correctly converted into local representation or rightfully skipped.
32 */
Lev Walkincc6a9102004-09-23 22:06:26 +000033#undef ADVANCE
Lev Walkinf15320b2004-06-03 03:38:44 +000034#define ADVANCE(num_bytes) do { \
35 size_t num = num_bytes; \
Lev Walkin4ce78ca2004-08-25 01:34:11 +000036 ptr = ((char *)ptr) + num; \
Lev Walkinf15320b2004-06-03 03:38:44 +000037 size -= num; \
38 if(ctx->left >= 0) \
39 ctx->left -= num; \
40 consumed_myself += num; \
41 } while(0)
42
43/*
44 * Switch to the next phase of parsing.
45 */
Lev Walkincc6a9102004-09-23 22:06:26 +000046#undef NEXT_PHASE
47#undef PHASE_OUT
Lev Walkinf15320b2004-06-03 03:38:44 +000048#define NEXT_PHASE(ctx) do { \
49 ctx->phase++; \
50 ctx->step = 0; \
51 } while(0)
52#define PHASE_OUT(ctx) do { ctx->phase = 10; } while(0)
53
54/*
55 * Return a standardized complex structure.
56 */
Lev Walkincc6a9102004-09-23 22:06:26 +000057#undef RETURN
Lev Walkinf15320b2004-06-03 03:38:44 +000058#define RETURN(_code) do { \
59 rval.code = _code; \
60 rval.consumed = consumed_myself;\
61 return rval; \
62 } while(0)
63
64/*
65 * Check whether we are inside the extensions group.
66 */
67#define IN_EXTENSION_GROUP(specs, memb_idx) \
68 ( ((memb_idx) > (specs)->ext_after) \
69 &&((memb_idx) < (specs)->ext_before))
70
Lev Walkin4c36e302004-06-06 07:20:02 +000071
72/*
73 * Tags are canonically sorted in the tag2element map.
74 */
75static int
76_t2e_cmp(const void *ap, const void *bp) {
Lev Walkinc2346572004-08-11 09:07:36 +000077 const asn1_TYPE_tag2member_t *a = (const asn1_TYPE_tag2member_t *)ap;
78 const asn1_TYPE_tag2member_t *b = (const asn1_TYPE_tag2member_t *)bp;
79
Lev Walkin4c36e302004-06-06 07:20:02 +000080 int a_class = BER_TAG_CLASS(a->el_tag);
81 int b_class = BER_TAG_CLASS(b->el_tag);
82
83 if(a_class == b_class) {
84 ber_tlv_tag_t a_value = BER_TAG_VALUE(a->el_tag);
85 ber_tlv_tag_t b_value = BER_TAG_VALUE(b->el_tag);
86
Lev Walkin924fa032004-06-14 13:42:23 +000087 if(a_value == b_value) {
Lev Walkin50299c12004-06-14 14:11:14 +000088 if(a->el_no > b->el_no)
89 return 1;
Lev Walkin924fa032004-06-14 13:42:23 +000090 /*
91 * Important: we do not check
Lev Walkin50299c12004-06-14 14:11:14 +000092 * for a->el_no <= b->el_no!
Lev Walkin924fa032004-06-14 13:42:23 +000093 */
Lev Walkin4c36e302004-06-06 07:20:02 +000094 return 0;
Lev Walkin924fa032004-06-14 13:42:23 +000095 } else if(a_value < b_value)
Lev Walkin4c36e302004-06-06 07:20:02 +000096 return -1;
97 else
98 return 1;
99 } else if(a_class < b_class) {
100 return -1;
101 } else {
102 return 1;
103 }
104}
105
106
Lev Walkinf15320b2004-06-03 03:38:44 +0000107/*
108 * The decoder of the SEQUENCE type.
109 */
110ber_dec_rval_t
Lev Walkin449f8322004-08-20 13:23:42 +0000111SEQUENCE_decode_ber(asn1_TYPE_descriptor_t *td,
Lev Walkinf15320b2004-06-03 03:38:44 +0000112 void **struct_ptr, void *ptr, size_t size, int tag_mode) {
113 /*
114 * Bring closer parts of structure description.
115 */
Lev Walkin449f8322004-08-20 13:23:42 +0000116 asn1_SEQUENCE_specifics_t *specs = (asn1_SEQUENCE_specifics_t *)td->specifics;
117 asn1_TYPE_member_t *elements = td->elements;
Lev Walkinf15320b2004-06-03 03:38:44 +0000118
119 /*
120 * Parts of the structure being constructed.
121 */
122 void *st = *struct_ptr; /* Target structure. */
123 ber_dec_ctx_t *ctx; /* Decoder context */
124
125 ber_tlv_tag_t tlv_tag; /* T from TLV */
126 //ber_tlv_len_t tlv_len; /* L from TLV */
127 ber_dec_rval_t rval; /* Return code from subparsers */
128
129 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
130 int edx; /* SEQUENCE element's index */
131
Lev Walkin449f8322004-08-20 13:23:42 +0000132 ASN_DEBUG("Decoding %s as SEQUENCE", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000133
134 /*
135 * Create the target structure if it is not present already.
136 */
137 if(st == 0) {
138 st = *struct_ptr = CALLOC(1, specs->struct_size);
139 if(st == 0) {
140 RETURN(RC_FAIL);
141 }
142 }
143
144 /*
145 * Restore parsing context.
146 */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000147 ctx = (ber_dec_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000148
149 /*
150 * Start to parse where left previously
151 */
152 switch(ctx->phase) {
153 case 0:
154 /*
155 * PHASE 0.
156 * Check that the set of tags associated with given structure
157 * perfectly fits our expectations.
158 */
159
Lev Walkin449f8322004-08-20 13:23:42 +0000160 rval = ber_check_tags(td, ctx, ptr, size,
Lev Walkinf15320b2004-06-03 03:38:44 +0000161 tag_mode, &ctx->left, 0);
162 if(rval.code != RC_OK) {
163 ASN_DEBUG("%s tagging check failed: %d",
Lev Walkin449f8322004-08-20 13:23:42 +0000164 td->name, rval.code);
Lev Walkinf15320b2004-06-03 03:38:44 +0000165 consumed_myself += rval.consumed;
166 RETURN(rval.code);
167 }
168
169 if(ctx->left >= 0)
170 ctx->left += rval.consumed; /* ?Substracted below! */
171 ADVANCE(rval.consumed);
172
173 NEXT_PHASE(ctx);
174
175 ASN_DEBUG("Structure consumes %ld bytes, buffer %ld",
176 (long)ctx->left, (long)size);
177
178 /* Fall through */
179 case 1:
180 /*
181 * PHASE 1.
182 * From the place where we've left it previously,
183 * try to decode the next member from the list of
184 * this structure's elements.
185 * (ctx->step) stores the member being processed
186 * between invocations and the microphase {0,1} of parsing
187 * that member:
188 * step = (<member_number> * 2 + <microphase>).
189 */
Lev Walkin449f8322004-08-20 13:23:42 +0000190 for(edx = (ctx->step >> 1); edx < td->elements_count;
Lev Walkinf15320b2004-06-03 03:38:44 +0000191 edx++, ctx->step = (ctx->step & ~1) + 2) {
192 void *memb_ptr; /* Pointer to the member */
Lev Walkinc2346572004-08-11 09:07:36 +0000193 void **memb_ptr2; /* Pointer to that pointer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000194 ssize_t tag_len; /* Length of TLV's T */
195 int opt_edx_end; /* Next non-optional element */
Lev Walkin4c36e302004-06-06 07:20:02 +0000196 int use_bsearch;
Lev Walkinf15320b2004-06-03 03:38:44 +0000197 int n;
198
199 if(ctx->step & 1)
200 goto microphase2;
201
202 /*
203 * MICROPHASE 1: Synchronize decoding.
204 */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000205 ASN_DEBUG("In %s SEQUENCE left %d, edx=%d flags=%d"
206 " opt=%d ec=%d",
207 td->name, (int)ctx->left, edx,
208 elements[edx].flags, elements[edx].optional,
209 td->elements_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000210
211 if(ctx->left == 0 /* No more stuff is expected */
212 && (
213 /* Explicit OPTIONAL specification reaches the end */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000214 (edx + elements[edx].optional
215 == td->elements_count)
Lev Walkinf15320b2004-06-03 03:38:44 +0000216 ||
217 /* All extensions are optional */
218 (IN_EXTENSION_GROUP(specs, edx)
Lev Walkin449f8322004-08-20 13:23:42 +0000219 && specs->ext_before > td->elements_count)
Lev Walkinf15320b2004-06-03 03:38:44 +0000220 )
221 ) {
Lev Walkin449f8322004-08-20 13:23:42 +0000222 ASN_DEBUG("End of SEQUENCE %s", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000223 /*
224 * Found the legitimate end of the structure.
225 */
226 PHASE_OUT(ctx);
227 RETURN(RC_OK);
228 }
229
230 /*
231 * Fetch the T from TLV.
232 */
233 tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
Lev Walkin906654e2004-09-10 15:49:15 +0000234 ASN_DEBUG("Current tag in %s SEQUENCE for element %d "
Lev Walkincc6a9102004-09-23 22:06:26 +0000235 "(%s) is %s encoded in %d bytes, of frame %ld",
Lev Walkin906654e2004-09-10 15:49:15 +0000236 td->name, edx, elements[edx].name,
237 ber_tlv_tag_string(tlv_tag), (int)tag_len, (long)LEFT);
Lev Walkinf15320b2004-06-03 03:38:44 +0000238 switch(tag_len) {
239 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
240 /* Fall through */
241 case -1: RETURN(RC_FAIL);
242 }
243
244 /*
245 * Find the next available type with this tag.
246 */
Lev Walkin4c36e302004-06-06 07:20:02 +0000247 use_bsearch = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000248 opt_edx_end = edx + elements[edx].optional + 1;
Lev Walkin449f8322004-08-20 13:23:42 +0000249 if(opt_edx_end > td->elements_count)
250 opt_edx_end = td->elements_count; /* Cap */
Lev Walkinf9dbd9a2004-07-01 00:48:04 +0000251 else if(opt_edx_end - edx > 8) {
Lev Walkin924fa032004-06-14 13:42:23 +0000252 /* Limit the scope of linear search... */
Lev Walkinf9dbd9a2004-07-01 00:48:04 +0000253 opt_edx_end = edx + 8;
Lev Walkin4c36e302004-06-06 07:20:02 +0000254 use_bsearch = 1;
Lev Walkin924fa032004-06-14 13:42:23 +0000255 /* ... and resort to bsearch() */
Lev Walkin4c36e302004-06-06 07:20:02 +0000256 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000257 for(n = edx; n < opt_edx_end; n++) {
258 if(BER_TAGS_EQUAL(tlv_tag, elements[n].tag)) {
259 /*
260 * Found element corresponding to the tag
261 * being looked at.
262 * Reposition over the right element.
263 */
264 edx = n;
Lev Walkin4c36e302004-06-06 07:20:02 +0000265 ctx->step = 1 + 2 * edx; /* Remember! */
266 goto microphase2;
Lev Walkinb9189732004-09-10 09:37:12 +0000267 } else if(elements[n].flags & ATF_OPEN_TYPE) {
268 /*
269 * This is the ANY type, which may bear
270 * any flag whatsoever.
271 */
272 edx = n;
273 ctx->step = 1 + 2 * edx; /* Remember! */
274 goto microphase2;
Lev Walkin4c36e302004-06-06 07:20:02 +0000275 } else if(elements[n].tag == (ber_tlv_tag_t)-1) {
276 use_bsearch = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000277 break;
278 }
279 }
Lev Walkin4c36e302004-06-06 07:20:02 +0000280 if(use_bsearch) {
281 /*
Lev Walkin4e0704f2004-09-04 06:17:35 +0000282 * Resort to a binary search over
Lev Walkin4c36e302004-06-06 07:20:02 +0000283 * sorted array of tags.
284 */
285 asn1_TYPE_tag2member_t *t2m;
286 asn1_TYPE_tag2member_t key;
287 key.el_tag = tlv_tag;
Lev Walkin924fa032004-06-14 13:42:23 +0000288 key.el_no = edx;
Lev Walkinc2346572004-08-11 09:07:36 +0000289 (void *)t2m = bsearch(&key,
290 specs->tag2el, specs->tag2el_count,
Lev Walkin4c36e302004-06-06 07:20:02 +0000291 sizeof(specs->tag2el[0]), _t2e_cmp);
Lev Walkin924fa032004-06-14 13:42:23 +0000292 if(t2m) {
293 asn1_TYPE_tag2member_t *best = 0;
294 asn1_TYPE_tag2member_t *t2m_f, *t2m_l;
295 int edx_max = edx + elements[edx].optional;
Lev Walkin4c36e302004-06-06 07:20:02 +0000296 /*
297 * Rewind to the first element with that tag,
298 * `cause bsearch() does not guarantee order.
299 */
Lev Walkin924fa032004-06-14 13:42:23 +0000300 t2m_f = t2m + t2m->toff_first;
301 t2m_l = t2m + t2m->toff_last;
302 for(t2m = t2m_f; t2m <= t2m_l; t2m++) {
303 if(t2m->el_no > edx_max) break;
304 if(t2m->el_no < edx) continue;
305 best = t2m;
306 }
307 if(best) {
308 edx = best->el_no;
309 ctx->step = 1 + 2 * edx;
310 goto microphase2;
311 }
Lev Walkin4c36e302004-06-06 07:20:02 +0000312 }
313 n = opt_edx_end;
314 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000315 if(n == opt_edx_end) {
316 /*
317 * If tag is unknown, it may be either
318 * an unknown (thus, incorrect) tag,
319 * or an extension (...),
320 * or an end of the indefinite-length structure.
321 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000322 if(!IN_EXTENSION_GROUP(specs, edx)) {
323 ASN_DEBUG("Unexpected tag %s",
324 ber_tlv_tag_string(tlv_tag));
Lev Walkin4c36e302004-06-06 07:20:02 +0000325 ASN_DEBUG("Expected tag %s (%s)%s",
Lev Walkinf15320b2004-06-03 03:38:44 +0000326 ber_tlv_tag_string(elements[edx].tag),
Lev Walkin4c36e302004-06-06 07:20:02 +0000327 elements[edx].name,
Lev Walkinf15320b2004-06-03 03:38:44 +0000328 elements[edx].optional
329 ?" or alternatives":"");
330 RETURN(RC_FAIL);
331 }
332
333 if(ctx->left < 0
334 && ((uint8_t *)ptr)[0] == 0) {
335 if(LEFT < 2) {
336 if(SIZE_VIOLATION)
337 RETURN(RC_FAIL);
338 else
339 RETURN(RC_WMORE);
340 } else if(((uint8_t *)ptr)[1] == 0) {
341 /*
342 * Yeah, baby! Found the terminator
343 * of the indefinite length structure.
344 */
345 /*
346 * Proceed to the canonical
347 * finalization function.
348 * No advancing is necessary.
349 */
350 goto phase3;
351 }
352 } else {
353 /* Skip this tag */
354 ssize_t skip;
355
356 skip = ber_skip_length(
357 BER_TLV_CONSTRUCTED(ptr),
Lev Walkin4d9528c2004-08-11 08:10:13 +0000358 (char *)ptr + tag_len, LEFT - tag_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000359 ASN_DEBUG("Skip length %d in %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000360 (int)skip, td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000361 switch(skip) {
362 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
363 /* Fall through */
364 case -1: RETURN(RC_FAIL);
365 }
366
367 ADVANCE(skip + tag_len);
368 ctx->step -= 2;
369 edx--;
370 continue; /* Try again with the next tag */
371 }
372 }
373
374 /*
375 * MICROPHASE 2: Invoke the member-specific decoder.
376 */
377 ctx->step |= 1; /* Confirm entering next microphase */
378 microphase2:
Lev Walkin449f8322004-08-20 13:23:42 +0000379 ASN_DEBUG("Inside SEQUENCE %s MF2", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000380
381 /*
382 * Compute the position of the member inside a structure,
383 * and also a type of containment (it may be contained
384 * as pointer or using inline inclusion).
385 */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000386 if(elements[edx].flags & ATF_POINTER) {
387 /* Member is a pointer to another structure */
Lev Walkinc2346572004-08-11 09:07:36 +0000388 memb_ptr2 = (void **)((char *)st + elements[edx].memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000389 } else {
390 /*
391 * A pointer to a pointer
392 * holding the start of the structure
393 */
394 memb_ptr = (char *)st + elements[edx].memb_offset;
395 memb_ptr2 = &memb_ptr;
396 }
397 /*
398 * Invoke the member fetch routine according to member's type
399 */
400 rval = elements[edx].type->ber_decoder(
Lev Walkinc2346572004-08-11 09:07:36 +0000401 elements[edx].type,
Lev Walkinf15320b2004-06-03 03:38:44 +0000402 memb_ptr2, ptr, LEFT,
403 elements[edx].tag_mode);
Lev Walkin60b7cff2004-09-04 04:44:11 +0000404 ASN_DEBUG("In %s SEQUENCE decoded %d %s of %d "
405 "in %d bytes rval.code %d",
Lev Walkin449f8322004-08-20 13:23:42 +0000406 td->name, edx, elements[edx].type->name,
Lev Walkin60b7cff2004-09-04 04:44:11 +0000407 (int)LEFT, (int)rval.consumed, rval.code);
Lev Walkinf15320b2004-06-03 03:38:44 +0000408 switch(rval.code) {
409 case RC_OK:
410 break;
411 case RC_WMORE: /* More data expected */
412 if(!SIZE_VIOLATION) {
413 ADVANCE(rval.consumed);
414 RETURN(RC_WMORE);
415 }
416 /* Fall through */
417 case RC_FAIL: /* Fatal error */
418 RETURN(RC_FAIL);
419 } /* switch(rval) */
420
421 ADVANCE(rval.consumed);
422 } /* for(all structure members) */
423
424 phase3:
425 ctx->phase = 3;
426 case 3: /* 00 and other tags expected */
427 case 4: /* only 00's expected */
428
429 ASN_DEBUG("SEQUENCE %s Leftover: %ld, size = %ld",
Lev Walkin449f8322004-08-20 13:23:42 +0000430 td->name, (long)ctx->left, (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000431
432 /*
433 * Skip everything until the end of the SEQUENCE.
434 */
435 while(ctx->left) {
436 ssize_t tl, ll;
437
438 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
439 switch(tl) {
440 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
441 /* Fall through */
442 case -1: RETURN(RC_FAIL);
443 }
444
445 /*
446 * If expected <0><0>...
447 */
448 if(ctx->left < 0
449 && ((uint8_t *)ptr)[0] == 0) {
450 if(LEFT < 2) {
451 if(SIZE_VIOLATION)
452 RETURN(RC_FAIL);
453 else
454 RETURN(RC_WMORE);
455 } else if(((uint8_t *)ptr)[1] == 0) {
456 /*
457 * Correctly finished with <0><0>.
458 */
459 ADVANCE(2);
460 ctx->left++;
461 ctx->phase = 4;
462 continue;
463 }
464 }
465
Lev Walkin449f8322004-08-20 13:23:42 +0000466 if(!IN_EXTENSION_GROUP(specs, td->elements_count)
Lev Walkinf15320b2004-06-03 03:38:44 +0000467 || ctx->phase == 4) {
468 ASN_DEBUG("Unexpected continuation "
469 "of a non-extensible type "
470 "%s (SEQUENCE): %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000471 td->name,
Lev Walkinf15320b2004-06-03 03:38:44 +0000472 ber_tlv_tag_string(tlv_tag));
473 RETURN(RC_FAIL);
474 }
475
476 ll = ber_skip_length(
477 BER_TLV_CONSTRUCTED(ptr),
Lev Walkin4d9528c2004-08-11 08:10:13 +0000478 (char *)ptr + tl, LEFT - tl);
Lev Walkinf15320b2004-06-03 03:38:44 +0000479 switch(ll) {
480 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
481 /* Fall through */
482 case -1: RETURN(RC_FAIL);
483 }
484
485 ADVANCE(tl + ll);
486 }
487
488 PHASE_OUT(ctx);
489 }
490
491 RETURN(RC_OK);
492}
493
Lev Walkin4c36e302004-06-06 07:20:02 +0000494
Lev Walkinf15320b2004-06-03 03:38:44 +0000495/*
496 * The DER encoder of the SEQUENCE type.
497 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000498asn_enc_rval_t
Lev Walkin449f8322004-08-20 13:23:42 +0000499SEQUENCE_encode_der(asn1_TYPE_descriptor_t *td,
Lev Walkinf15320b2004-06-03 03:38:44 +0000500 void *ptr, int tag_mode, ber_tlv_tag_t tag,
501 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000502 size_t computed_size = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000503 asn_enc_rval_t erval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000504 ssize_t ret;
505 int edx;
506
507 ASN_DEBUG("%s %s as SEQUENCE",
Lev Walkin449f8322004-08-20 13:23:42 +0000508 cb?"Encoding":"Estimating", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000509
510 /*
511 * Gather the length of the underlying members sequence.
512 */
Lev Walkin449f8322004-08-20 13:23:42 +0000513 for(edx = 0; edx < td->elements_count; edx++) {
514 asn1_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000515 void *memb_ptr;
Lev Walkincc93b0f2004-09-10 09:18:20 +0000516 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000517 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
518 if(!memb_ptr) continue;
519 } else {
520 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
521 }
522 erval = elm->type->der_encoder(elm->type, memb_ptr,
523 elm->tag_mode, elm->tag,
524 0, 0);
525 if(erval.encoded == -1)
526 return erval;
527 computed_size += erval.encoded;
528 ASN_DEBUG("Member %d %s estimated %ld bytes",
529 edx, elm->name, (long)erval.encoded);
530 }
531
532 /*
533 * Encode the TLV for the sequence itself.
534 */
Lev Walkin449f8322004-08-20 13:23:42 +0000535 ret = der_write_tags(td, computed_size, tag_mode, tag, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +0000536 ASN_DEBUG("Wrote tags: %ld (+%ld)", (long)ret, (long)computed_size);
537 if(ret == -1) {
538 erval.encoded = -1;
Lev Walkin449f8322004-08-20 13:23:42 +0000539 erval.failed_type = td;
Lev Walkinf15320b2004-06-03 03:38:44 +0000540 erval.structure_ptr = ptr;
541 return erval;
542 }
543 erval.encoded = computed_size + ret;
544
545 if(!cb) return erval;
546
547 /*
548 * Encode all members.
549 */
Lev Walkin449f8322004-08-20 13:23:42 +0000550 for(edx = 0; edx < td->elements_count; edx++) {
551 asn1_TYPE_member_t *elm = &td->elements[edx];
Lev Walkina9cc46e2004-09-22 16:06:28 +0000552 asn_enc_rval_t tmperval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000553 void *memb_ptr;
554
Lev Walkincc93b0f2004-09-10 09:18:20 +0000555 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000556 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
557 if(!memb_ptr) continue;
558 } else {
559 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
560 }
561 tmperval = elm->type->der_encoder(elm->type, memb_ptr,
562 elm->tag_mode, elm->tag,
563 cb, app_key);
564 if(tmperval.encoded == -1)
565 return tmperval;
566 computed_size -= tmperval.encoded;
567 ASN_DEBUG("Member %d %s of SEQUENCE %s encoded in %d bytes",
Lev Walkin449f8322004-08-20 13:23:42 +0000568 edx, elm->name, td->name, tmperval.encoded);
Lev Walkinf15320b2004-06-03 03:38:44 +0000569 }
570
571 if(computed_size != 0) {
572 /*
573 * Encoded size is not equal to the computed size.
574 */
575 erval.encoded = -1;
Lev Walkin449f8322004-08-20 13:23:42 +0000576 erval.failed_type = td;
Lev Walkinf15320b2004-06-03 03:38:44 +0000577 erval.structure_ptr = ptr;
578 }
579
580 return erval;
581}
582
Lev Walkina9cc46e2004-09-22 16:06:28 +0000583asn_enc_rval_t
584SEQUENCE_encode_xer(asn1_TYPE_descriptor_t *td, void *sptr,
585 int ilevel, enum xer_encoder_flags_e flags,
586 asn_app_consume_bytes_f *cb, void *app_key) {
587 asn_enc_rval_t er;
588 int xcan = (flags & XER_F_CANONICAL);
589 int edx;
590
591 if(!sptr)
592 _ASN_ENCODE_FAILED;
593
594 er.encoded = 0;
595
596 for(edx = 0; edx < td->elements_count; edx++) {
597 asn_enc_rval_t tmper;
598 asn1_TYPE_member_t *elm = &td->elements[edx];
599 void *memb_ptr;
600 const char *mname = elm->name;
601 unsigned int mlen = strlen(mname);
602
603 if(elm->flags & ATF_POINTER) {
604 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
605 if(!memb_ptr) continue; /* OPTIONAL element? */
606 } else {
607 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
608 }
609
610 if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel);
611 _ASN_CALLBACK3("<", 1, mname, mlen, ">", 1);
612
613 /* Print the member itself */
614 tmper = elm->type->xer_encoder(elm->type, memb_ptr,
615 ilevel + 1, flags, cb, app_key);
616 if(tmper.encoded == -1) return tmper;
617
618 _ASN_CALLBACK3("</", 2, mname, mlen, ">", 1);
619 er.encoded += 5 + (2 * mlen) + tmper.encoded;
620 }
621
622 if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel - 1);
623
624 return er;
625}
626
Lev Walkinf15320b2004-06-03 03:38:44 +0000627int
628SEQUENCE_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
629 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000630 int edx;
631 int ret;
632
633 if(!sptr) return cb("<absent>", 8, app_key);
634
635 /* Dump preamble */
636 if(cb(td->name, strlen(td->name), app_key)
637 || cb(" ::= {\n", 7, app_key))
638 return -1;
639
Lev Walkin449f8322004-08-20 13:23:42 +0000640 for(edx = 0; edx < td->elements_count; edx++) {
641 asn1_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000642 const void *memb_ptr;
643
Lev Walkincc93b0f2004-09-10 09:18:20 +0000644 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000645 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
646 if(!memb_ptr) continue;
647 } else {
648 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
649 }
650
651 /* Indentation */
652 for(ret = 0; ret < ilevel; ret++) cb(" ", 1, app_key);
653
654 /* Print the member's name and stuff */
655 if(cb(elm->name, strlen(elm->name), app_key)
656 || cb(": ", 2, app_key))
657 return -1;
658
659 /* Print the member itself */
660 ret = elm->type->print_struct(elm->type, memb_ptr, ilevel + 4,
661 cb, app_key);
662 if(ret) return ret;
663
664 /* Print out the terminator */
665 ret = cb("\n", 1, app_key);
666 if(ret) return ret;
667 }
668
669 /* Indentation */
670 for(ret = 0; ret < ilevel - 4; ret++) cb(" ", 1, app_key);
671
672 return cb("}", 1, app_key);
673}
674
675void
676SEQUENCE_free(asn1_TYPE_descriptor_t *td, void *sptr, int contents_only) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000677 int edx;
678
679 if(!td || !sptr)
680 return;
681
682 ASN_DEBUG("Freeing %s as SEQUENCE", td->name);
683
Lev Walkin449f8322004-08-20 13:23:42 +0000684 for(edx = 0; edx < td->elements_count; edx++) {
685 asn1_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000686 void *memb_ptr;
Lev Walkincc93b0f2004-09-10 09:18:20 +0000687 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000688 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
689 if(memb_ptr)
690 elm->type->free_struct(elm->type, memb_ptr, 0);
691 } else {
692 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
693 elm->type->free_struct(elm->type, memb_ptr, 1);
694 }
695 }
696
697 if(!contents_only) {
698 FREEMEM(sptr);
699 }
700}
701
702int
703SEQUENCE_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
704 asn_app_consume_bytes_f *app_errlog, void *app_key) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000705 int edx;
706
707 if(!sptr) {
Lev Walkinba4e5182004-08-11 09:44:13 +0000708 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkin16835b62004-08-22 13:47:59 +0000709 "%s: value not given (%s:%d)",
710 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000711 return -1;
712 }
713
714 /*
715 * Iterate over structure members and check their validity.
716 */
Lev Walkin449f8322004-08-20 13:23:42 +0000717 for(edx = 0; edx < td->elements_count; edx++) {
718 asn1_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000719 const void *memb_ptr;
720
Lev Walkincc93b0f2004-09-10 09:18:20 +0000721 if(elm->flags & ATF_POINTER) {
Lev Walkin4c36e302004-06-06 07:20:02 +0000722 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000723 if(!memb_ptr) continue;
724 } else {
725 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
726 }
727
Lev Walkin449f8322004-08-20 13:23:42 +0000728 if(elm->memb_constraints) {
729 int ret = elm->memb_constraints(elm->type, memb_ptr,
730 app_errlog, app_key);
731 if(ret) return ret;
732 } else {
733 int ret = elm->type->check_constraints(elm->type,
734 memb_ptr, app_errlog, app_key);
735 if(ret) return ret;
736 /*
737 * Cannot inherit it earlier:
738 * need to make sure we get the updated version.
739 */
740 elm->memb_constraints = elm->type->check_constraints;
741 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000742 }
743
744 return 0;
745}