blob: 9057ac0368f6fa2c2f4e5f7c15d263f2d7fc1f25 [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 */
vlm39ba4c42004-09-22 16:06:28 +00005#include <asn_internal.h>
vlmfa67ddc2004-06-03 03:38:44 +00006#include <constr_SEQUENCE.h>
vlm31473082004-06-06 07:20:02 +00007#include <assert.h>
vlmfa67ddc2004-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 */
vlmc5190612004-08-18 04:53:32 +000014#define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left)
vlmfa67ddc2004-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 */
vlmb42843a2004-06-05 08:17:50 +000027#define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size)
vlmfa67ddc2004-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 */
33#define ADVANCE(num_bytes) do { \
34 size_t num = num_bytes; \
vlmd86c9252004-08-25 01:34:11 +000035 ptr = ((char *)ptr) + num; \
vlmfa67ddc2004-06-03 03:38:44 +000036 size -= num; \
37 if(ctx->left >= 0) \
38 ctx->left -= num; \
39 consumed_myself += num; \
40 } while(0)
41
42/*
43 * Switch to the next phase of parsing.
44 */
45#define NEXT_PHASE(ctx) do { \
46 ctx->phase++; \
47 ctx->step = 0; \
48 } while(0)
49#define PHASE_OUT(ctx) do { ctx->phase = 10; } while(0)
50
51/*
52 * Return a standardized complex structure.
53 */
54#define RETURN(_code) do { \
55 rval.code = _code; \
56 rval.consumed = consumed_myself;\
57 return rval; \
58 } while(0)
59
60/*
61 * Check whether we are inside the extensions group.
62 */
63#define IN_EXTENSION_GROUP(specs, memb_idx) \
64 ( ((memb_idx) > (specs)->ext_after) \
65 &&((memb_idx) < (specs)->ext_before))
66
vlm31473082004-06-06 07:20:02 +000067
68/*
69 * Tags are canonically sorted in the tag2element map.
70 */
71static int
72_t2e_cmp(const void *ap, const void *bp) {
vlmda674682004-08-11 09:07:36 +000073 const asn1_TYPE_tag2member_t *a = (const asn1_TYPE_tag2member_t *)ap;
74 const asn1_TYPE_tag2member_t *b = (const asn1_TYPE_tag2member_t *)bp;
75
vlm31473082004-06-06 07:20:02 +000076 int a_class = BER_TAG_CLASS(a->el_tag);
77 int b_class = BER_TAG_CLASS(b->el_tag);
78
79 if(a_class == b_class) {
80 ber_tlv_tag_t a_value = BER_TAG_VALUE(a->el_tag);
81 ber_tlv_tag_t b_value = BER_TAG_VALUE(b->el_tag);
82
vlm1692da02004-06-14 13:42:23 +000083 if(a_value == b_value) {
vlme4ee6ee2004-06-14 14:11:14 +000084 if(a->el_no > b->el_no)
85 return 1;
vlm1692da02004-06-14 13:42:23 +000086 /*
87 * Important: we do not check
vlme4ee6ee2004-06-14 14:11:14 +000088 * for a->el_no <= b->el_no!
vlm1692da02004-06-14 13:42:23 +000089 */
vlm31473082004-06-06 07:20:02 +000090 return 0;
vlm1692da02004-06-14 13:42:23 +000091 } else if(a_value < b_value)
vlm31473082004-06-06 07:20:02 +000092 return -1;
93 else
94 return 1;
95 } else if(a_class < b_class) {
96 return -1;
97 } else {
98 return 1;
99 }
100}
101
102
vlmfa67ddc2004-06-03 03:38:44 +0000103/*
104 * The decoder of the SEQUENCE type.
105 */
106ber_dec_rval_t
vlme413c122004-08-20 13:23:42 +0000107SEQUENCE_decode_ber(asn1_TYPE_descriptor_t *td,
vlmfa67ddc2004-06-03 03:38:44 +0000108 void **struct_ptr, void *ptr, size_t size, int tag_mode) {
109 /*
110 * Bring closer parts of structure description.
111 */
vlme413c122004-08-20 13:23:42 +0000112 asn1_SEQUENCE_specifics_t *specs = (asn1_SEQUENCE_specifics_t *)td->specifics;
113 asn1_TYPE_member_t *elements = td->elements;
vlmfa67ddc2004-06-03 03:38:44 +0000114
115 /*
116 * Parts of the structure being constructed.
117 */
118 void *st = *struct_ptr; /* Target structure. */
119 ber_dec_ctx_t *ctx; /* Decoder context */
120
121 ber_tlv_tag_t tlv_tag; /* T from TLV */
122 //ber_tlv_len_t tlv_len; /* L from TLV */
123 ber_dec_rval_t rval; /* Return code from subparsers */
124
125 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
126 int edx; /* SEQUENCE element's index */
127
vlme413c122004-08-20 13:23:42 +0000128 ASN_DEBUG("Decoding %s as SEQUENCE", td->name);
vlmfa67ddc2004-06-03 03:38:44 +0000129
130 /*
131 * Create the target structure if it is not present already.
132 */
133 if(st == 0) {
134 st = *struct_ptr = CALLOC(1, specs->struct_size);
135 if(st == 0) {
136 RETURN(RC_FAIL);
137 }
138 }
139
140 /*
141 * Restore parsing context.
142 */
vlm1ff928d2004-08-11 08:10:13 +0000143 ctx = (ber_dec_ctx_t *)((char *)st + specs->ctx_offset);
vlmfa67ddc2004-06-03 03:38:44 +0000144
145 /*
146 * Start to parse where left previously
147 */
148 switch(ctx->phase) {
149 case 0:
150 /*
151 * PHASE 0.
152 * Check that the set of tags associated with given structure
153 * perfectly fits our expectations.
154 */
155
vlme413c122004-08-20 13:23:42 +0000156 rval = ber_check_tags(td, ctx, ptr, size,
vlmfa67ddc2004-06-03 03:38:44 +0000157 tag_mode, &ctx->left, 0);
158 if(rval.code != RC_OK) {
159 ASN_DEBUG("%s tagging check failed: %d",
vlme413c122004-08-20 13:23:42 +0000160 td->name, rval.code);
vlmfa67ddc2004-06-03 03:38:44 +0000161 consumed_myself += rval.consumed;
162 RETURN(rval.code);
163 }
164
165 if(ctx->left >= 0)
166 ctx->left += rval.consumed; /* ?Substracted below! */
167 ADVANCE(rval.consumed);
168
169 NEXT_PHASE(ctx);
170
171 ASN_DEBUG("Structure consumes %ld bytes, buffer %ld",
172 (long)ctx->left, (long)size);
173
174 /* Fall through */
175 case 1:
176 /*
177 * PHASE 1.
178 * From the place where we've left it previously,
179 * try to decode the next member from the list of
180 * this structure's elements.
181 * (ctx->step) stores the member being processed
182 * between invocations and the microphase {0,1} of parsing
183 * that member:
184 * step = (<member_number> * 2 + <microphase>).
185 */
vlme413c122004-08-20 13:23:42 +0000186 for(edx = (ctx->step >> 1); edx < td->elements_count;
vlmfa67ddc2004-06-03 03:38:44 +0000187 edx++, ctx->step = (ctx->step & ~1) + 2) {
188 void *memb_ptr; /* Pointer to the member */
vlmda674682004-08-11 09:07:36 +0000189 void **memb_ptr2; /* Pointer to that pointer */
vlmfa67ddc2004-06-03 03:38:44 +0000190 ssize_t tag_len; /* Length of TLV's T */
191 int opt_edx_end; /* Next non-optional element */
vlm31473082004-06-06 07:20:02 +0000192 int use_bsearch;
vlmfa67ddc2004-06-03 03:38:44 +0000193 int n;
194
195 if(ctx->step & 1)
196 goto microphase2;
197
198 /*
199 * MICROPHASE 1: Synchronize decoding.
200 */
vlmddd5a7d2004-09-10 09:18:20 +0000201 ASN_DEBUG("In %s SEQUENCE left %d, edx=%d flags=%d"
202 " opt=%d ec=%d",
203 td->name, (int)ctx->left, edx,
204 elements[edx].flags, elements[edx].optional,
205 td->elements_count);
vlmfa67ddc2004-06-03 03:38:44 +0000206
207 if(ctx->left == 0 /* No more stuff is expected */
208 && (
209 /* Explicit OPTIONAL specification reaches the end */
vlmddd5a7d2004-09-10 09:18:20 +0000210 (edx + elements[edx].optional
211 == td->elements_count)
vlmfa67ddc2004-06-03 03:38:44 +0000212 ||
213 /* All extensions are optional */
214 (IN_EXTENSION_GROUP(specs, edx)
vlme413c122004-08-20 13:23:42 +0000215 && specs->ext_before > td->elements_count)
vlmfa67ddc2004-06-03 03:38:44 +0000216 )
217 ) {
vlme413c122004-08-20 13:23:42 +0000218 ASN_DEBUG("End of SEQUENCE %s", td->name);
vlmfa67ddc2004-06-03 03:38:44 +0000219 /*
220 * Found the legitimate end of the structure.
221 */
222 PHASE_OUT(ctx);
223 RETURN(RC_OK);
224 }
225
226 /*
227 * Fetch the T from TLV.
228 */
229 tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
vlm1308d2b2004-09-10 15:49:15 +0000230 ASN_DEBUG("Current tag in %s SEQUENCE for element %d "
231 "(%s) is %s encoded in %d bytes, left %ld",
232 td->name, edx, elements[edx].name,
233 ber_tlv_tag_string(tlv_tag), (int)tag_len, (long)LEFT);
vlmfa67ddc2004-06-03 03:38:44 +0000234 switch(tag_len) {
235 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
236 /* Fall through */
237 case -1: RETURN(RC_FAIL);
238 }
239
240 /*
241 * Find the next available type with this tag.
242 */
vlm31473082004-06-06 07:20:02 +0000243 use_bsearch = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000244 opt_edx_end = edx + elements[edx].optional + 1;
vlme413c122004-08-20 13:23:42 +0000245 if(opt_edx_end > td->elements_count)
246 opt_edx_end = td->elements_count; /* Cap */
vlm5d219bf2004-07-01 00:48:04 +0000247 else if(opt_edx_end - edx > 8) {
vlm1692da02004-06-14 13:42:23 +0000248 /* Limit the scope of linear search... */
vlm5d219bf2004-07-01 00:48:04 +0000249 opt_edx_end = edx + 8;
vlm31473082004-06-06 07:20:02 +0000250 use_bsearch = 1;
vlm1692da02004-06-14 13:42:23 +0000251 /* ... and resort to bsearch() */
vlm31473082004-06-06 07:20:02 +0000252 }
vlmfa67ddc2004-06-03 03:38:44 +0000253 for(n = edx; n < opt_edx_end; n++) {
254 if(BER_TAGS_EQUAL(tlv_tag, elements[n].tag)) {
255 /*
256 * Found element corresponding to the tag
257 * being looked at.
258 * Reposition over the right element.
259 */
260 edx = n;
vlm31473082004-06-06 07:20:02 +0000261 ctx->step = 1 + 2 * edx; /* Remember! */
262 goto microphase2;
vlm060fe2a2004-09-10 09:37:12 +0000263 } else if(elements[n].flags & ATF_OPEN_TYPE) {
264 /*
265 * This is the ANY type, which may bear
266 * any flag whatsoever.
267 */
268 edx = n;
269 ctx->step = 1 + 2 * edx; /* Remember! */
270 goto microphase2;
vlm31473082004-06-06 07:20:02 +0000271 } else if(elements[n].tag == (ber_tlv_tag_t)-1) {
272 use_bsearch = 1;
vlmfa67ddc2004-06-03 03:38:44 +0000273 break;
274 }
275 }
vlm31473082004-06-06 07:20:02 +0000276 if(use_bsearch) {
277 /*
vlm141874a2004-09-04 06:17:35 +0000278 * Resort to a binary search over
vlm31473082004-06-06 07:20:02 +0000279 * sorted array of tags.
280 */
281 asn1_TYPE_tag2member_t *t2m;
282 asn1_TYPE_tag2member_t key;
283 key.el_tag = tlv_tag;
vlm1692da02004-06-14 13:42:23 +0000284 key.el_no = edx;
vlmda674682004-08-11 09:07:36 +0000285 (void *)t2m = bsearch(&key,
286 specs->tag2el, specs->tag2el_count,
vlm31473082004-06-06 07:20:02 +0000287 sizeof(specs->tag2el[0]), _t2e_cmp);
vlm1692da02004-06-14 13:42:23 +0000288 if(t2m) {
289 asn1_TYPE_tag2member_t *best = 0;
290 asn1_TYPE_tag2member_t *t2m_f, *t2m_l;
291 int edx_max = edx + elements[edx].optional;
vlm31473082004-06-06 07:20:02 +0000292 /*
293 * Rewind to the first element with that tag,
294 * `cause bsearch() does not guarantee order.
295 */
vlm1692da02004-06-14 13:42:23 +0000296 t2m_f = t2m + t2m->toff_first;
297 t2m_l = t2m + t2m->toff_last;
298 for(t2m = t2m_f; t2m <= t2m_l; t2m++) {
299 if(t2m->el_no > edx_max) break;
300 if(t2m->el_no < edx) continue;
301 best = t2m;
302 }
303 if(best) {
304 edx = best->el_no;
305 ctx->step = 1 + 2 * edx;
306 goto microphase2;
307 }
vlm31473082004-06-06 07:20:02 +0000308 }
309 n = opt_edx_end;
310 }
vlmfa67ddc2004-06-03 03:38:44 +0000311 if(n == opt_edx_end) {
312 /*
313 * If tag is unknown, it may be either
314 * an unknown (thus, incorrect) tag,
315 * or an extension (...),
316 * or an end of the indefinite-length structure.
317 */
vlmfa67ddc2004-06-03 03:38:44 +0000318 if(!IN_EXTENSION_GROUP(specs, edx)) {
319 ASN_DEBUG("Unexpected tag %s",
320 ber_tlv_tag_string(tlv_tag));
vlm31473082004-06-06 07:20:02 +0000321 ASN_DEBUG("Expected tag %s (%s)%s",
vlmfa67ddc2004-06-03 03:38:44 +0000322 ber_tlv_tag_string(elements[edx].tag),
vlm31473082004-06-06 07:20:02 +0000323 elements[edx].name,
vlmfa67ddc2004-06-03 03:38:44 +0000324 elements[edx].optional
325 ?" or alternatives":"");
326 RETURN(RC_FAIL);
327 }
328
329 if(ctx->left < 0
330 && ((uint8_t *)ptr)[0] == 0) {
331 if(LEFT < 2) {
332 if(SIZE_VIOLATION)
333 RETURN(RC_FAIL);
334 else
335 RETURN(RC_WMORE);
336 } else if(((uint8_t *)ptr)[1] == 0) {
337 /*
338 * Yeah, baby! Found the terminator
339 * of the indefinite length structure.
340 */
341 /*
342 * Proceed to the canonical
343 * finalization function.
344 * No advancing is necessary.
345 */
346 goto phase3;
347 }
348 } else {
349 /* Skip this tag */
350 ssize_t skip;
351
352 skip = ber_skip_length(
353 BER_TLV_CONSTRUCTED(ptr),
vlm1ff928d2004-08-11 08:10:13 +0000354 (char *)ptr + tag_len, LEFT - tag_len);
vlmfa67ddc2004-06-03 03:38:44 +0000355 ASN_DEBUG("Skip length %d in %s",
vlme413c122004-08-20 13:23:42 +0000356 (int)skip, td->name);
vlmfa67ddc2004-06-03 03:38:44 +0000357 switch(skip) {
358 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
359 /* Fall through */
360 case -1: RETURN(RC_FAIL);
361 }
362
363 ADVANCE(skip + tag_len);
364 ctx->step -= 2;
365 edx--;
366 continue; /* Try again with the next tag */
367 }
368 }
369
370 /*
371 * MICROPHASE 2: Invoke the member-specific decoder.
372 */
373 ctx->step |= 1; /* Confirm entering next microphase */
374 microphase2:
vlme413c122004-08-20 13:23:42 +0000375 ASN_DEBUG("Inside SEQUENCE %s MF2", td->name);
vlmfa67ddc2004-06-03 03:38:44 +0000376
377 /*
378 * Compute the position of the member inside a structure,
379 * and also a type of containment (it may be contained
380 * as pointer or using inline inclusion).
381 */
vlmddd5a7d2004-09-10 09:18:20 +0000382 if(elements[edx].flags & ATF_POINTER) {
383 /* Member is a pointer to another structure */
vlmda674682004-08-11 09:07:36 +0000384 memb_ptr2 = (void **)((char *)st + elements[edx].memb_offset);
vlmfa67ddc2004-06-03 03:38:44 +0000385 } else {
386 /*
387 * A pointer to a pointer
388 * holding the start of the structure
389 */
390 memb_ptr = (char *)st + elements[edx].memb_offset;
391 memb_ptr2 = &memb_ptr;
392 }
393 /*
394 * Invoke the member fetch routine according to member's type
395 */
396 rval = elements[edx].type->ber_decoder(
vlmda674682004-08-11 09:07:36 +0000397 elements[edx].type,
vlmfa67ddc2004-06-03 03:38:44 +0000398 memb_ptr2, ptr, LEFT,
399 elements[edx].tag_mode);
vlmef16e8c2004-09-04 04:44:11 +0000400 ASN_DEBUG("In %s SEQUENCE decoded %d %s of %d "
401 "in %d bytes rval.code %d",
vlme413c122004-08-20 13:23:42 +0000402 td->name, edx, elements[edx].type->name,
vlmef16e8c2004-09-04 04:44:11 +0000403 (int)LEFT, (int)rval.consumed, rval.code);
vlmfa67ddc2004-06-03 03:38:44 +0000404 switch(rval.code) {
405 case RC_OK:
406 break;
407 case RC_WMORE: /* More data expected */
408 if(!SIZE_VIOLATION) {
409 ADVANCE(rval.consumed);
410 RETURN(RC_WMORE);
411 }
412 /* Fall through */
413 case RC_FAIL: /* Fatal error */
414 RETURN(RC_FAIL);
415 } /* switch(rval) */
416
417 ADVANCE(rval.consumed);
418 } /* for(all structure members) */
419
420 phase3:
421 ctx->phase = 3;
422 case 3: /* 00 and other tags expected */
423 case 4: /* only 00's expected */
424
425 ASN_DEBUG("SEQUENCE %s Leftover: %ld, size = %ld",
vlme413c122004-08-20 13:23:42 +0000426 td->name, (long)ctx->left, (long)size);
vlmfa67ddc2004-06-03 03:38:44 +0000427
428 /*
429 * Skip everything until the end of the SEQUENCE.
430 */
431 while(ctx->left) {
432 ssize_t tl, ll;
433
434 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
435 switch(tl) {
436 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
437 /* Fall through */
438 case -1: RETURN(RC_FAIL);
439 }
440
441 /*
442 * If expected <0><0>...
443 */
444 if(ctx->left < 0
445 && ((uint8_t *)ptr)[0] == 0) {
446 if(LEFT < 2) {
447 if(SIZE_VIOLATION)
448 RETURN(RC_FAIL);
449 else
450 RETURN(RC_WMORE);
451 } else if(((uint8_t *)ptr)[1] == 0) {
452 /*
453 * Correctly finished with <0><0>.
454 */
455 ADVANCE(2);
456 ctx->left++;
457 ctx->phase = 4;
458 continue;
459 }
460 }
461
vlme413c122004-08-20 13:23:42 +0000462 if(!IN_EXTENSION_GROUP(specs, td->elements_count)
vlmfa67ddc2004-06-03 03:38:44 +0000463 || ctx->phase == 4) {
464 ASN_DEBUG("Unexpected continuation "
465 "of a non-extensible type "
466 "%s (SEQUENCE): %s",
vlme413c122004-08-20 13:23:42 +0000467 td->name,
vlmfa67ddc2004-06-03 03:38:44 +0000468 ber_tlv_tag_string(tlv_tag));
469 RETURN(RC_FAIL);
470 }
471
472 ll = ber_skip_length(
473 BER_TLV_CONSTRUCTED(ptr),
vlm1ff928d2004-08-11 08:10:13 +0000474 (char *)ptr + tl, LEFT - tl);
vlmfa67ddc2004-06-03 03:38:44 +0000475 switch(ll) {
476 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
477 /* Fall through */
478 case -1: RETURN(RC_FAIL);
479 }
480
481 ADVANCE(tl + ll);
482 }
483
484 PHASE_OUT(ctx);
485 }
486
487 RETURN(RC_OK);
488}
489
vlm31473082004-06-06 07:20:02 +0000490
vlmfa67ddc2004-06-03 03:38:44 +0000491/*
492 * The DER encoder of the SEQUENCE type.
493 */
vlm39ba4c42004-09-22 16:06:28 +0000494asn_enc_rval_t
vlme413c122004-08-20 13:23:42 +0000495SEQUENCE_encode_der(asn1_TYPE_descriptor_t *td,
vlmfa67ddc2004-06-03 03:38:44 +0000496 void *ptr, int tag_mode, ber_tlv_tag_t tag,
497 asn_app_consume_bytes_f *cb, void *app_key) {
vlmfa67ddc2004-06-03 03:38:44 +0000498 size_t computed_size = 0;
vlm39ba4c42004-09-22 16:06:28 +0000499 asn_enc_rval_t erval;
vlmfa67ddc2004-06-03 03:38:44 +0000500 ssize_t ret;
501 int edx;
502
503 ASN_DEBUG("%s %s as SEQUENCE",
vlme413c122004-08-20 13:23:42 +0000504 cb?"Encoding":"Estimating", td->name);
vlmfa67ddc2004-06-03 03:38:44 +0000505
506 /*
507 * Gather the length of the underlying members sequence.
508 */
vlme413c122004-08-20 13:23:42 +0000509 for(edx = 0; edx < td->elements_count; edx++) {
510 asn1_TYPE_member_t *elm = &td->elements[edx];
vlmfa67ddc2004-06-03 03:38:44 +0000511 void *memb_ptr;
vlmddd5a7d2004-09-10 09:18:20 +0000512 if(elm->flags & ATF_POINTER) {
vlmfa67ddc2004-06-03 03:38:44 +0000513 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
514 if(!memb_ptr) continue;
515 } else {
516 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
517 }
518 erval = elm->type->der_encoder(elm->type, memb_ptr,
519 elm->tag_mode, elm->tag,
520 0, 0);
521 if(erval.encoded == -1)
522 return erval;
523 computed_size += erval.encoded;
524 ASN_DEBUG("Member %d %s estimated %ld bytes",
525 edx, elm->name, (long)erval.encoded);
526 }
527
528 /*
529 * Encode the TLV for the sequence itself.
530 */
vlme413c122004-08-20 13:23:42 +0000531 ret = der_write_tags(td, computed_size, tag_mode, tag, cb, app_key);
vlmfa67ddc2004-06-03 03:38:44 +0000532 ASN_DEBUG("Wrote tags: %ld (+%ld)", (long)ret, (long)computed_size);
533 if(ret == -1) {
534 erval.encoded = -1;
vlme413c122004-08-20 13:23:42 +0000535 erval.failed_type = td;
vlmfa67ddc2004-06-03 03:38:44 +0000536 erval.structure_ptr = ptr;
537 return erval;
538 }
539 erval.encoded = computed_size + ret;
540
541 if(!cb) return erval;
542
543 /*
544 * Encode all members.
545 */
vlme413c122004-08-20 13:23:42 +0000546 for(edx = 0; edx < td->elements_count; edx++) {
547 asn1_TYPE_member_t *elm = &td->elements[edx];
vlm39ba4c42004-09-22 16:06:28 +0000548 asn_enc_rval_t tmperval;
vlmfa67ddc2004-06-03 03:38:44 +0000549 void *memb_ptr;
550
vlmddd5a7d2004-09-10 09:18:20 +0000551 if(elm->flags & ATF_POINTER) {
vlmfa67ddc2004-06-03 03:38:44 +0000552 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
553 if(!memb_ptr) continue;
554 } else {
555 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
556 }
557 tmperval = elm->type->der_encoder(elm->type, memb_ptr,
558 elm->tag_mode, elm->tag,
559 cb, app_key);
560 if(tmperval.encoded == -1)
561 return tmperval;
562 computed_size -= tmperval.encoded;
563 ASN_DEBUG("Member %d %s of SEQUENCE %s encoded in %d bytes",
vlme413c122004-08-20 13:23:42 +0000564 edx, elm->name, td->name, tmperval.encoded);
vlmfa67ddc2004-06-03 03:38:44 +0000565 }
566
567 if(computed_size != 0) {
568 /*
569 * Encoded size is not equal to the computed size.
570 */
571 erval.encoded = -1;
vlme413c122004-08-20 13:23:42 +0000572 erval.failed_type = td;
vlmfa67ddc2004-06-03 03:38:44 +0000573 erval.structure_ptr = ptr;
574 }
575
576 return erval;
577}
578
vlm39ba4c42004-09-22 16:06:28 +0000579asn_enc_rval_t
580SEQUENCE_encode_xer(asn1_TYPE_descriptor_t *td, void *sptr,
581 int ilevel, enum xer_encoder_flags_e flags,
582 asn_app_consume_bytes_f *cb, void *app_key) {
583 asn_enc_rval_t er;
584 int xcan = (flags & XER_F_CANONICAL);
585 int edx;
586
587 if(!sptr)
588 _ASN_ENCODE_FAILED;
589
590 er.encoded = 0;
591
592 for(edx = 0; edx < td->elements_count; edx++) {
593 asn_enc_rval_t tmper;
594 asn1_TYPE_member_t *elm = &td->elements[edx];
595 void *memb_ptr;
596 const char *mname = elm->name;
597 unsigned int mlen = strlen(mname);
598
599 if(elm->flags & ATF_POINTER) {
600 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
601 if(!memb_ptr) continue; /* OPTIONAL element? */
602 } else {
603 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
604 }
605
606 if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel);
607 _ASN_CALLBACK3("<", 1, mname, mlen, ">", 1);
608
609 /* Print the member itself */
610 tmper = elm->type->xer_encoder(elm->type, memb_ptr,
611 ilevel + 1, flags, cb, app_key);
612 if(tmper.encoded == -1) return tmper;
613
614 _ASN_CALLBACK3("</", 2, mname, mlen, ">", 1);
615 er.encoded += 5 + (2 * mlen) + tmper.encoded;
616 }
617
618 if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel - 1);
619
620 return er;
621}
622
vlmfa67ddc2004-06-03 03:38:44 +0000623int
624SEQUENCE_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
625 asn_app_consume_bytes_f *cb, void *app_key) {
vlmfa67ddc2004-06-03 03:38:44 +0000626 int edx;
627 int ret;
628
629 if(!sptr) return cb("<absent>", 8, app_key);
630
631 /* Dump preamble */
632 if(cb(td->name, strlen(td->name), app_key)
633 || cb(" ::= {\n", 7, app_key))
634 return -1;
635
vlme413c122004-08-20 13:23:42 +0000636 for(edx = 0; edx < td->elements_count; edx++) {
637 asn1_TYPE_member_t *elm = &td->elements[edx];
vlmfa67ddc2004-06-03 03:38:44 +0000638 const void *memb_ptr;
639
vlmddd5a7d2004-09-10 09:18:20 +0000640 if(elm->flags & ATF_POINTER) {
vlmfa67ddc2004-06-03 03:38:44 +0000641 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
642 if(!memb_ptr) continue;
643 } else {
644 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
645 }
646
647 /* Indentation */
648 for(ret = 0; ret < ilevel; ret++) cb(" ", 1, app_key);
649
650 /* Print the member's name and stuff */
651 if(cb(elm->name, strlen(elm->name), app_key)
652 || cb(": ", 2, app_key))
653 return -1;
654
655 /* Print the member itself */
656 ret = elm->type->print_struct(elm->type, memb_ptr, ilevel + 4,
657 cb, app_key);
658 if(ret) return ret;
659
660 /* Print out the terminator */
661 ret = cb("\n", 1, app_key);
662 if(ret) return ret;
663 }
664
665 /* Indentation */
666 for(ret = 0; ret < ilevel - 4; ret++) cb(" ", 1, app_key);
667
668 return cb("}", 1, app_key);
669}
670
671void
672SEQUENCE_free(asn1_TYPE_descriptor_t *td, void *sptr, int contents_only) {
vlmfa67ddc2004-06-03 03:38:44 +0000673 int edx;
674
675 if(!td || !sptr)
676 return;
677
678 ASN_DEBUG("Freeing %s as SEQUENCE", td->name);
679
vlme413c122004-08-20 13:23:42 +0000680 for(edx = 0; edx < td->elements_count; edx++) {
681 asn1_TYPE_member_t *elm = &td->elements[edx];
vlmfa67ddc2004-06-03 03:38:44 +0000682 void *memb_ptr;
vlmddd5a7d2004-09-10 09:18:20 +0000683 if(elm->flags & ATF_POINTER) {
vlmfa67ddc2004-06-03 03:38:44 +0000684 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
685 if(memb_ptr)
686 elm->type->free_struct(elm->type, memb_ptr, 0);
687 } else {
688 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
689 elm->type->free_struct(elm->type, memb_ptr, 1);
690 }
691 }
692
693 if(!contents_only) {
694 FREEMEM(sptr);
695 }
696}
697
698int
699SEQUENCE_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
700 asn_app_consume_bytes_f *app_errlog, void *app_key) {
vlmfa67ddc2004-06-03 03:38:44 +0000701 int edx;
702
703 if(!sptr) {
vlme3f0f282004-08-11 09:44:13 +0000704 _ASN_ERRLOG(app_errlog, app_key,
vlm758530a2004-08-22 13:47:59 +0000705 "%s: value not given (%s:%d)",
706 td->name, __FILE__, __LINE__);
vlmfa67ddc2004-06-03 03:38:44 +0000707 return -1;
708 }
709
710 /*
711 * Iterate over structure members and check their validity.
712 */
vlme413c122004-08-20 13:23:42 +0000713 for(edx = 0; edx < td->elements_count; edx++) {
714 asn1_TYPE_member_t *elm = &td->elements[edx];
vlmfa67ddc2004-06-03 03:38:44 +0000715 const void *memb_ptr;
716
vlmddd5a7d2004-09-10 09:18:20 +0000717 if(elm->flags & ATF_POINTER) {
vlm31473082004-06-06 07:20:02 +0000718 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
vlmfa67ddc2004-06-03 03:38:44 +0000719 if(!memb_ptr) continue;
720 } else {
721 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
722 }
723
vlme413c122004-08-20 13:23:42 +0000724 if(elm->memb_constraints) {
725 int ret = elm->memb_constraints(elm->type, memb_ptr,
726 app_errlog, app_key);
727 if(ret) return ret;
728 } else {
729 int ret = elm->type->check_constraints(elm->type,
730 memb_ptr, app_errlog, app_key);
731 if(ret) return ret;
732 /*
733 * Cannot inherit it earlier:
734 * need to make sure we get the updated version.
735 */
736 elm->memb_constraints = elm->type->check_constraints;
737 }
vlmfa67ddc2004-06-03 03:38:44 +0000738 }
739
740 return 0;
741}