blob: ac2bbd68f305feeb0261c67d1c2ec552cd6f2c7f [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_CHOICE.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00007#include <assert.h>
8
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 */
33#define ADVANCE(num_bytes) do { \
34 size_t num = num_bytes; \
Lev Walkin4ce78ca2004-08-25 01:34:11 +000035 ptr = ((char *)ptr) + num; \
Lev Walkinf15320b2004-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
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 * See the definitions.
61 */
Lev Walkin91f5cd02004-08-11 09:10:59 +000062static int _fetch_present_idx(const void *struct_ptr, int off, int size);
63static void _set_present_idx(void *sptr, int offset, int size, int pres);
Lev Walkinf15320b2004-06-03 03:38:44 +000064
65/*
66 * Tags are canonically sorted in the tag to member table.
67 */
68static int
69_search4tag(const void *ap, const void *bp) {
Lev Walkinc2346572004-08-11 09:07:36 +000070 const asn1_TYPE_tag2member_t *a = (const asn1_TYPE_tag2member_t *)ap;
71 const asn1_TYPE_tag2member_t *b = (const asn1_TYPE_tag2member_t *)bp;
72
Lev Walkinf15320b2004-06-03 03:38:44 +000073 int a_class = BER_TAG_CLASS(a->el_tag);
74 int b_class = BER_TAG_CLASS(b->el_tag);
75
76 if(a_class == b_class) {
77 ber_tlv_tag_t a_value = BER_TAG_VALUE(a->el_tag);
78 ber_tlv_tag_t b_value = BER_TAG_VALUE(b->el_tag);
79
80 if(a_value == b_value)
81 return 0;
82 else if(a_value < b_value)
83 return -1;
84 else
85 return 1;
86 } else if(a_class < b_class) {
87 return -1;
88 } else {
89 return 1;
90 }
91}
92
93/*
94 * The decoder of the CHOICE type.
95 */
96ber_dec_rval_t
Lev Walkin449f8322004-08-20 13:23:42 +000097CHOICE_decode_ber(asn1_TYPE_descriptor_t *td,
Lev Walkinf15320b2004-06-03 03:38:44 +000098 void **struct_ptr, void *ptr, size_t size, int tag_mode) {
99 /*
100 * Bring closer parts of structure description.
101 */
Lev Walkin449f8322004-08-20 13:23:42 +0000102 asn1_CHOICE_specifics_t *specs = (asn1_CHOICE_specifics_t *)td->specifics;
103 asn1_TYPE_member_t *elements = td->elements;
Lev Walkinf15320b2004-06-03 03:38:44 +0000104
105 /*
106 * Parts of the structure being constructed.
107 */
108 void *st = *struct_ptr; /* Target structure. */
109 ber_dec_ctx_t *ctx; /* Decoder context */
110
111 ber_tlv_tag_t tlv_tag; /* T from TLV */
112 ssize_t tag_len; /* Length of TLV's T */
113 //ber_tlv_len_t tlv_len; /* L from TLV */
114 ber_dec_rval_t rval; /* Return code from subparsers */
115
116 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
117
Lev Walkin449f8322004-08-20 13:23:42 +0000118 ASN_DEBUG("Decoding %s as CHOICE", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000119
120 /*
121 * Create the target structure if it is not present already.
122 */
123 if(st == 0) {
124 st = *struct_ptr = CALLOC(1, specs->struct_size);
125 if(st == 0) {
126 RETURN(RC_FAIL);
127 }
128 }
129
130 /*
131 * Restore parsing context.
132 */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000133 ctx = (ber_dec_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000134
135 /*
136 * Start to parse where left previously
137 */
138 switch(ctx->phase) {
139 case 0:
140 /*
141 * PHASE 0.
142 * Check that the set of tags associated with given structure
143 * perfectly fits our expectations.
144 */
145
Lev Walkin449f8322004-08-20 13:23:42 +0000146 if(tag_mode || td->tags_count) {
147 rval = ber_check_tags(td, ctx, ptr, size,
Lev Walkinf15320b2004-06-03 03:38:44 +0000148 tag_mode, &ctx->left, 0);
149 if(rval.code != RC_OK) {
150 ASN_DEBUG("%s tagging check failed: %d",
Lev Walkin449f8322004-08-20 13:23:42 +0000151 td->name, rval.code);
Lev Walkinf15320b2004-06-03 03:38:44 +0000152 consumed_myself += rval.consumed;
153 RETURN(rval.code);
154 }
155
156 if(ctx->left >= 0) {
157 /* ?Substracted below! */
158 ctx->left += rval.consumed;
159 }
160 ADVANCE(rval.consumed);
161 } else {
162 ctx->left = -1;
163 }
164
165 NEXT_PHASE(ctx);
166
167 ASN_DEBUG("Structure consumes %ld bytes, buffer %ld",
168 (long)ctx->left, (long)size);
169
170 /* Fall through */
171 case 1:
172 /*
173 * Fetch the T from TLV.
174 */
175 tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
Lev Walkin449f8322004-08-20 13:23:42 +0000176 ASN_DEBUG("In %s CHOICE tag length %d", td->name, (int)tag_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000177 switch(tag_len) {
178 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
179 /* Fall through */
180 case -1: RETURN(RC_FAIL);
181 }
182
183 do {
Lev Walkin4c36e302004-06-06 07:20:02 +0000184 asn1_TYPE_tag2member_t *t2m;
185 asn1_TYPE_tag2member_t key;
Lev Walkinf15320b2004-06-03 03:38:44 +0000186
187 key.el_tag = tlv_tag;
Lev Walkinc2346572004-08-11 09:07:36 +0000188 (void *)t2m = bsearch(&key,
189 specs->tag2el, specs->tag2el_count,
Lev Walkinf15320b2004-06-03 03:38:44 +0000190 sizeof(specs->tag2el[0]), _search4tag);
191 if(t2m) {
192 /*
193 * Found the element corresponding to the tag.
194 */
195 NEXT_PHASE(ctx);
196 ctx->step = t2m->el_no;
197 break;
198 } else if(specs->extensible == 0) {
199 ASN_DEBUG("Unexpected tag %s "
200 "in non-extensible CHOICE %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000201 ber_tlv_tag_string(tlv_tag), td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000202 RETURN(RC_FAIL);
203 } else {
204 /* Skip this tag */
205 ssize_t skip;
206
207 ASN_DEBUG("Skipping unknown tag %s",
208 ber_tlv_tag_string(tlv_tag));
209
210 skip = ber_skip_length(
211 BER_TLV_CONSTRUCTED(ptr),
Lev Walkin4d9528c2004-08-11 08:10:13 +0000212 (char *)ptr + tag_len, LEFT - tag_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000213
214 switch(skip) {
215 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
216 /* Fall through */
217 case -1: RETURN(RC_FAIL);
218 }
219
220 ADVANCE(skip + tag_len);
221 RETURN(RC_OK);
222 }
223 } while(0);
224
225 case 2:
226 /*
227 * PHASE 2.
228 * Read in the element.
229 */
230 do {
Lev Walkin449f8322004-08-20 13:23:42 +0000231 asn1_TYPE_member_t *elm;/* CHOICE's element */
Lev Walkinf15320b2004-06-03 03:38:44 +0000232 void *memb_ptr; /* Pointer to the member */
Lev Walkinc2346572004-08-11 09:07:36 +0000233 void **memb_ptr2; /* Pointer to that pointer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000234
235 elm = &elements[ctx->step];
236
237 /*
238 * Compute the position of the member inside a structure,
239 * and also a type of containment (it may be contained
240 * as pointer or using inline inclusion).
241 */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000242 if(elm->flags & ATF_POINTER) {
243 /* Member is a pointer to another structure */
Lev Walkinc2346572004-08-11 09:07:36 +0000244 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000245 } else {
246 /*
247 * A pointer to a pointer
248 * holding the start of the structure
249 */
250 memb_ptr = (char *)st + elm->memb_offset;
251 memb_ptr2 = &memb_ptr;
252 }
253 /*
254 * Invoke the member fetch routine according to member's type
255 */
Lev Walkinc2346572004-08-11 09:07:36 +0000256 rval = elm->type->ber_decoder(elm->type,
Lev Walkinf15320b2004-06-03 03:38:44 +0000257 memb_ptr2, ptr, LEFT,
258 elm->tag_mode);
259 switch(rval.code) {
260 case RC_OK:
261 _set_present_idx(st, specs->pres_offset,
262 specs->pres_size, ctx->step + 1);
263 break;
264 case RC_WMORE: /* More data expected */
265 if(!SIZE_VIOLATION) {
266 ADVANCE(rval.consumed);
267 RETURN(RC_WMORE);
268 }
269 RETURN(RC_FAIL);
270 case RC_FAIL: /* Fatal error */
271 RETURN(rval.code);
272 } /* switch(rval) */
273
274 ADVANCE(rval.consumed);
275 } while(0);
276
277 NEXT_PHASE(ctx);
278
279 /* Fall through */
280 case 3:
281 ASN_DEBUG("CHOICE %s Leftover: %ld, size = %ld, tm=%d, tc=%d",
Lev Walkin449f8322004-08-20 13:23:42 +0000282 td->name, (long)ctx->left, (long)size,
283 tag_mode, td->tags_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000284
285 if(ctx->left > 0) {
286 /*
287 * The type must be fully decoded
288 * by the CHOICE member-specific decoder.
289 */
290 RETURN(RC_FAIL);
291 }
292
293 if(ctx->left == -1
Lev Walkin449f8322004-08-20 13:23:42 +0000294 && !(tag_mode || td->tags_count)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000295 /*
296 * This is an untagged CHOICE.
297 * It doesn't contain nothing
298 * except for the member itself, including all its tags.
299 * The decoding is completed.
300 */
301 NEXT_PHASE(ctx);
302 break;
303 }
304
305 /*
306 * Read in the "end of data chunks"'s.
307 */
308 while(ctx->left < 0) {
309 ssize_t tl;
310
311 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
312 switch(tl) {
313 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
314 /* Fall through */
315 case -1: RETURN(RC_FAIL);
316 }
317
318 /*
319 * Expected <0><0>...
320 */
321 if(((uint8_t *)ptr)[0] == 0) {
322 if(LEFT < 2) {
323 if(SIZE_VIOLATION)
324 RETURN(RC_FAIL);
325 else
326 RETURN(RC_WMORE);
327 } else if(((uint8_t *)ptr)[1] == 0) {
328 /*
329 * Correctly finished with <0><0>.
330 */
331 continue;
332 }
333 } else {
334 ASN_DEBUG("Unexpected continuation in %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000335 td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000336 RETURN(RC_FAIL);
337 }
338
339 ADVANCE(2);
340 ctx->left++;
341 }
342
343 NEXT_PHASE(ctx);
344 case 4:
345 /* No meaningful work here */
346 break;
347 }
348
349 RETURN(RC_OK);
350}
351
Lev Walkina9cc46e2004-09-22 16:06:28 +0000352asn_enc_rval_t
Lev Walkin449f8322004-08-20 13:23:42 +0000353CHOICE_encode_der(asn1_TYPE_descriptor_t *td,
Lev Walkinf15320b2004-06-03 03:38:44 +0000354 void *struct_ptr,
355 int tag_mode, ber_tlv_tag_t tag,
356 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin449f8322004-08-20 13:23:42 +0000357 asn1_CHOICE_specifics_t *specs = (asn1_CHOICE_specifics_t *)td->specifics;
358 asn1_TYPE_member_t *elm; /* CHOICE element */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000359 asn_enc_rval_t erval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000360 void *memb_ptr;
361 size_t computed_size = 0;
362 int present;
363
364 ASN_DEBUG("%s %s as CHOICE",
Lev Walkin449f8322004-08-20 13:23:42 +0000365 cb?"Encoding":"Estimating", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000366
367 present = _fetch_present_idx(struct_ptr,
368 specs->pres_offset, specs->pres_size);
369
370 /*
371 * If the structure was not initialized, it cannot be encoded:
372 * can't deduce what to encode in the choice type.
373 */
Lev Walkin449f8322004-08-20 13:23:42 +0000374 if(present <= 0 || present > td->elements_count) {
375 if(present == 0 && td->elements_count == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000376 /* The CHOICE is empty?! */
377 erval.encoded = 0;
378 return erval;
379 }
380 erval.encoded = -1;
Lev Walkin449f8322004-08-20 13:23:42 +0000381 erval.failed_type = td;
Lev Walkinf15320b2004-06-03 03:38:44 +0000382 erval.structure_ptr = struct_ptr;
383 return erval;
384 }
385
386 /*
387 * Seek over the present member of the structure.
388 */
Lev Walkin449f8322004-08-20 13:23:42 +0000389 elm = &td->elements[present-1];
Lev Walkincc93b0f2004-09-10 09:18:20 +0000390 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000391 memb_ptr = *(void **)((char *)struct_ptr + elm->memb_offset);
392 if(memb_ptr == 0) {
Lev Walkincc93b0f2004-09-10 09:18:20 +0000393 if(elm->optional) {
394 erval.encoded = 0;
395 } else {
396 /* Mandatory element absent */
397 erval.encoded = -1;
398 erval.failed_type = td;
399 erval.structure_ptr = struct_ptr;
400 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000401 return erval;
402 }
403 } else {
404 memb_ptr = (void *)((char *)struct_ptr + elm->memb_offset);
405 }
406
407 /*
408 * If the CHOICE itself is tagged EXPLICIT:
409 * T ::= [2] EXPLICIT CHOICE { ... }
410 * Then emit the appropriate tags.
411 */
Lev Walkin449f8322004-08-20 13:23:42 +0000412 if(tag_mode == 1 || td->tags_count) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000413 /*
414 * For this, we need to pre-compute the member.
415 */
416 ssize_t ret;
417
418 /* Encode member with its tag */
419 erval = elm->type->der_encoder(elm->type, memb_ptr,
420 elm->tag_mode, elm->tag, 0, 0);
421 if(erval.encoded == -1)
422 return erval;
423
424 /* Encode CHOICE with parent or my own tag */
Lev Walkin449f8322004-08-20 13:23:42 +0000425 ret = der_write_tags(td, erval.encoded, tag_mode, tag,
Lev Walkinf15320b2004-06-03 03:38:44 +0000426 cb, app_key);
427 if(ret == -1) {
428 erval.encoded = -1;
Lev Walkin449f8322004-08-20 13:23:42 +0000429 erval.failed_type = td;
Lev Walkinf15320b2004-06-03 03:38:44 +0000430 erval.structure_ptr = struct_ptr;
431 return erval;
432 }
433 computed_size += ret;
434 }
435
436 /*
437 * Encode the single underlying member.
438 */
439 erval = elm->type->der_encoder(elm->type, memb_ptr,
440 elm->tag_mode, elm->tag, cb, app_key);
441 if(erval.encoded == -1)
442 return erval;
443
444 ASN_DEBUG("Encoded CHOICE member in %ld bytes (+%ld)",
445 (long)erval.encoded, (long)computed_size);
446
447 erval.encoded += computed_size;
448
449 return erval;
450}
451
452ber_tlv_tag_t
453CHOICE_outmost_tag(asn1_TYPE_descriptor_t *td, const void *ptr, int tag_mode, ber_tlv_tag_t tag) {
Lev Walkinc2346572004-08-11 09:07:36 +0000454 asn1_CHOICE_specifics_t *specs = (asn1_CHOICE_specifics_t *)td->specifics;
Lev Walkinf15320b2004-06-03 03:38:44 +0000455 int present;
456
457 assert(tag_mode == 0);
458 assert(tag == 0);
459
460 /*
461 * Figure out which CHOICE element is encoded.
462 */
463 present = _fetch_present_idx(ptr, specs->pres_offset, specs->pres_size);
464
Lev Walkin449f8322004-08-20 13:23:42 +0000465 if(present > 0 || present <= td->elements_count) {
466 asn1_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkin70e70282004-06-05 01:45:48 +0000467 const void *memb_ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000468
Lev Walkincc93b0f2004-09-10 09:18:20 +0000469 if(elm->flags & ATF_POINTER) {
Lev Walkin70e70282004-06-05 01:45:48 +0000470 memb_ptr = *(const void * const *)
471 ((const char *)ptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000472 } else {
Lev Walkin70e70282004-06-05 01:45:48 +0000473 memb_ptr = (const void *)
474 ((const char *)ptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000475 }
476
477 return asn1_TYPE_outmost_tag(elm->type, memb_ptr,
478 elm->tag_mode, elm->tag);
479 } else {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000480 return (ber_tlv_tag_t)-1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000481 }
482}
483
484int
485CHOICE_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
486 asn_app_consume_bytes_f *app_errlog, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000487 asn1_CHOICE_specifics_t *specs = (asn1_CHOICE_specifics_t *)td->specifics;
Lev Walkinf15320b2004-06-03 03:38:44 +0000488 int present;
489
490 if(!sptr) {
Lev Walkinba4e5182004-08-11 09:44:13 +0000491 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkin16835b62004-08-22 13:47:59 +0000492 "%s: value not given (%s:%d)",
493 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000494 return -1;
495 }
496
497 /*
498 * Figure out which CHOICE element is encoded.
499 */
500 present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
Lev Walkin449f8322004-08-20 13:23:42 +0000501 if(present > 0 && present <= td->elements_count) {
502 asn1_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkinf15320b2004-06-03 03:38:44 +0000503 const void *memb_ptr;
504
Lev Walkincc93b0f2004-09-10 09:18:20 +0000505 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000506 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkincc93b0f2004-09-10 09:18:20 +0000507 if(!memb_ptr) {
508 if(elm->optional)
509 return 0;
510 _ASN_ERRLOG(app_errlog, app_key,
511 "%s: mandatory CHOICE element %s absent (%s:%d)",
512 td->name, elm->name, __FILE__, __LINE__);
513 return -1;
514 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000515 } else {
516 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
517 }
518
Lev Walkin449f8322004-08-20 13:23:42 +0000519 if(elm->memb_constraints) {
520 return elm->memb_constraints(elm->type, memb_ptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000521 app_errlog, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +0000522 } else {
523 int ret = elm->type->check_constraints(elm->type,
524 memb_ptr, app_errlog, app_key);
525 /*
526 * Cannot inherit it eralier:
527 * need to make sure we get the updated version.
528 */
529 elm->memb_constraints = elm->type->check_constraints;
530 return ret;
531 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000532 } else {
Lev Walkinba4e5182004-08-11 09:44:13 +0000533 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkind34a8512004-08-22 13:55:49 +0000534 "%s: no CHOICE element given (%s:%d)",
Lev Walkin16835b62004-08-22 13:47:59 +0000535 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000536 return -1;
537 }
538}
539
Lev Walkina9cc46e2004-09-22 16:06:28 +0000540asn_enc_rval_t
541CHOICE_encode_xer(asn1_TYPE_descriptor_t *td, void *sptr,
542 int ilevel, enum xer_encoder_flags_e flags,
543 asn_app_consume_bytes_f *cb, void *app_key) {
544 asn1_CHOICE_specifics_t *specs=(asn1_CHOICE_specifics_t *)td->specifics;
545 asn_enc_rval_t er;
546 int present;
547
548 if(!sptr)
549 _ASN_ENCODE_FAILED;
550
551 /*
552 * Figure out which CHOICE element is encoded.
553 */
554 present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
555
556 if(present <= 0 || present > td->elements_count) {
557 _ASN_ENCODE_FAILED;
558 } else {
559 asn_enc_rval_t tmper;
560 asn1_TYPE_member_t *elm = &td->elements[present-1];
561 void *memb_ptr;
562 const char *mname = elm->name;
563 unsigned int mlen = strlen(mname);
564
565 if(elm->flags & ATF_POINTER) {
566 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
567 if(!memb_ptr) _ASN_ENCODE_FAILED;
568 } else {
569 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
570 }
571
572 er.encoded = 0;
573
574 if(!(flags & XER_F_CANONICAL)) _i_ASN_TEXT_INDENT(1, ilevel);
575 _ASN_CALLBACK3("<", 1, mname, mlen, ">", 1);
576
577 tmper = elm->type->xer_encoder(elm->type, memb_ptr,
578 ilevel + 1, flags, cb, app_key);
579 if(tmper.encoded == -1) return tmper;
580
581 _ASN_CALLBACK3("</", 2, mname, mlen, ">", 1);
582
583 er.encoded += 5 + (2 * mlen) + tmper.encoded;
584 }
585
586 if(!(flags & XER_F_CANONICAL)) _i_ASN_TEXT_INDENT(1, ilevel - 1);
587
588 return er;
589}
590
Lev Walkinf15320b2004-06-03 03:38:44 +0000591int
592CHOICE_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
593 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000594 asn1_CHOICE_specifics_t *specs = (asn1_CHOICE_specifics_t *)td->specifics;
Lev Walkinf15320b2004-06-03 03:38:44 +0000595 int present;
596
597 if(!sptr) return cb("<absent>", 8, app_key);
598
599 /*
600 * Figure out which CHOICE element is encoded.
601 */
602 present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
603
604 /*
Lev Walkina9cc46e2004-09-22 16:06:28 +0000605 * Print that element.
Lev Walkinf15320b2004-06-03 03:38:44 +0000606 */
Lev Walkin449f8322004-08-20 13:23:42 +0000607 if(present > 0 && present <= td->elements_count) {
608 asn1_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkinf15320b2004-06-03 03:38:44 +0000609 const void *memb_ptr;
610
Lev Walkincc93b0f2004-09-10 09:18:20 +0000611 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000612 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
613 if(!memb_ptr) return cb("<absent>", 8, app_key);
614 } else {
615 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
616 }
617
618 /* Print member's name and stuff */
619 if(cb(elm->name, strlen(elm->name), app_key)
620 || cb(": ", 2, app_key))
621 return -1;
622
623 return elm->type->print_struct(elm->type, memb_ptr, ilevel,
624 cb, app_key);
625 } else {
626 return cb("<absent>", 8, app_key);
627 }
628}
629
630void
631CHOICE_free(asn1_TYPE_descriptor_t *td, void *ptr, int contents_only) {
Lev Walkinc2346572004-08-11 09:07:36 +0000632 asn1_CHOICE_specifics_t *specs = (asn1_CHOICE_specifics_t *)td->specifics;
Lev Walkinf15320b2004-06-03 03:38:44 +0000633 int present;
634
635 if(!td || !ptr)
636 return;
637
638 ASN_DEBUG("Freeing %s as CHOICE", td->name);
639
640 /*
641 * Figure out which CHOICE element is encoded.
642 */
643 present = _fetch_present_idx(ptr, specs->pres_offset, specs->pres_size);
644
645 /*
646 * Free that element.
647 */
Lev Walkin449f8322004-08-20 13:23:42 +0000648 if(present > 0 && present <= td->elements_count) {
649 asn1_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkinf15320b2004-06-03 03:38:44 +0000650 void *memb_ptr;
651
Lev Walkincc93b0f2004-09-10 09:18:20 +0000652 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000653 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
654 if(memb_ptr)
655 elm->type->free_struct(elm->type, memb_ptr, 0);
656 } else {
657 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
658 elm->type->free_struct(elm->type, memb_ptr, 1);
659 }
660 }
661
662 if(!contents_only) {
663 FREEMEM(ptr);
664 }
665}
666
667
668/*
669 * The following functions functions offer protection against -fshort-enums,
670 * compatible with little- and big-endian machines.
671 * If assertion is triggered, either disable -fshort-enums, or add an entry
672 * here with the ->pres_size of your target stracture.
673 * Unless the target structure is packed, the ".present" member
674 * is guaranteed to be aligned properly. ASN.1 compiler itself does not
675 * produce packed code.
676 */
Lev Walkin91f5cd02004-08-11 09:10:59 +0000677static int
Lev Walkinf15320b2004-06-03 03:38:44 +0000678_fetch_present_idx(const void *struct_ptr, int pres_offset, int pres_size) {
679 const void *present_ptr;
680 int present;
681
682 present_ptr = ((const char *)struct_ptr) + pres_offset;
683
684 switch(pres_size) {
685 case sizeof(int): present = *(const int *)present_ptr; break;
686 case sizeof(short): present = *(const short *)present_ptr; break;
687 case sizeof(char): present = *(const char *)present_ptr; break;
688 default:
689 /* ANSI C mandates enum to be equivalent to integer */
690 assert(pres_size != sizeof(int));
691 return 0; /* If not aborted, pass back safe value */
692 }
693
694 return present;
695}
696
Lev Walkin91f5cd02004-08-11 09:10:59 +0000697static void
Lev Walkinf15320b2004-06-03 03:38:44 +0000698_set_present_idx(void *struct_ptr, int pres_offset, int pres_size, int present) {
699 void *present_ptr;
700 present_ptr = ((char *)struct_ptr) + pres_offset;
701
702 switch(pres_size) {
703 case sizeof(int): *(int *)present_ptr = present; break;
704 case sizeof(short): *(short *)present_ptr = present; break;
705 case sizeof(char): *(char *)present_ptr = present; break;
706 default:
707 /* ANSI C mandates enum to be equivalent to integer */
708 assert(pres_size != sizeof(int));
709 }
710}