blob: 5ad33c83087c21d65b9bbd9b1e2670301ec74dcc [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 */
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
Lev Walkinf15320b2004-06-03 03:38:44 +000047#define NEXT_PHASE(ctx) do { \
48 ctx->phase++; \
49 ctx->step = 0; \
50 } while(0)
51
52/*
53 * Return a standardized complex structure.
54 */
Lev Walkincc6a9102004-09-23 22:06:26 +000055#undef RETURN
Lev Walkinf15320b2004-06-03 03:38:44 +000056#define RETURN(_code) do { \
57 rval.code = _code; \
58 rval.consumed = consumed_myself;\
59 return rval; \
60 } while(0)
61
62/*
63 * See the definitions.
64 */
Lev Walkin91f5cd02004-08-11 09:10:59 +000065static int _fetch_present_idx(const void *struct_ptr, int off, int size);
66static void _set_present_idx(void *sptr, int offset, int size, int pres);
Lev Walkinf15320b2004-06-03 03:38:44 +000067
68/*
69 * Tags are canonically sorted in the tag to member table.
70 */
71static int
72_search4tag(const void *ap, const void *bp) {
Lev Walkinc2346572004-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
Lev Walkinf15320b2004-06-03 03:38:44 +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
83 if(a_value == b_value)
84 return 0;
85 else if(a_value < b_value)
86 return -1;
87 else
88 return 1;
89 } else if(a_class < b_class) {
90 return -1;
91 } else {
92 return 1;
93 }
94}
95
96/*
97 * The decoder of the CHOICE type.
98 */
99ber_dec_rval_t
Lev Walkin449f8322004-08-20 13:23:42 +0000100CHOICE_decode_ber(asn1_TYPE_descriptor_t *td,
Lev Walkinf15320b2004-06-03 03:38:44 +0000101 void **struct_ptr, void *ptr, size_t size, int tag_mode) {
102 /*
103 * Bring closer parts of structure description.
104 */
Lev Walkin449f8322004-08-20 13:23:42 +0000105 asn1_CHOICE_specifics_t *specs = (asn1_CHOICE_specifics_t *)td->specifics;
106 asn1_TYPE_member_t *elements = td->elements;
Lev Walkinf15320b2004-06-03 03:38:44 +0000107
108 /*
109 * Parts of the structure being constructed.
110 */
111 void *st = *struct_ptr; /* Target structure. */
112 ber_dec_ctx_t *ctx; /* Decoder context */
113
114 ber_tlv_tag_t tlv_tag; /* T from TLV */
115 ssize_t tag_len; /* Length of TLV's T */
116 //ber_tlv_len_t tlv_len; /* L from TLV */
117 ber_dec_rval_t rval; /* Return code from subparsers */
118
119 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
120
Lev Walkin449f8322004-08-20 13:23:42 +0000121 ASN_DEBUG("Decoding %s as CHOICE", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000122
123 /*
124 * Create the target structure if it is not present already.
125 */
126 if(st == 0) {
127 st = *struct_ptr = CALLOC(1, specs->struct_size);
128 if(st == 0) {
129 RETURN(RC_FAIL);
130 }
131 }
132
133 /*
134 * Restore parsing context.
135 */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000136 ctx = (ber_dec_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000137
138 /*
139 * Start to parse where left previously
140 */
141 switch(ctx->phase) {
142 case 0:
143 /*
144 * PHASE 0.
145 * Check that the set of tags associated with given structure
146 * perfectly fits our expectations.
147 */
148
Lev Walkin449f8322004-08-20 13:23:42 +0000149 if(tag_mode || td->tags_count) {
150 rval = ber_check_tags(td, ctx, ptr, size,
Lev Walkin8e8078a2004-09-26 13:10:40 +0000151 tag_mode, -1, &ctx->left, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000152 if(rval.code != RC_OK) {
153 ASN_DEBUG("%s tagging check failed: %d",
Lev Walkin449f8322004-08-20 13:23:42 +0000154 td->name, rval.code);
Lev Walkinf15320b2004-06-03 03:38:44 +0000155 consumed_myself += rval.consumed;
156 RETURN(rval.code);
157 }
158
159 if(ctx->left >= 0) {
160 /* ?Substracted below! */
161 ctx->left += rval.consumed;
162 }
163 ADVANCE(rval.consumed);
164 } else {
165 ctx->left = -1;
166 }
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 * Fetch the T from TLV.
177 */
178 tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
Lev Walkin449f8322004-08-20 13:23:42 +0000179 ASN_DEBUG("In %s CHOICE tag length %d", td->name, (int)tag_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000180 switch(tag_len) {
181 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
182 /* Fall through */
183 case -1: RETURN(RC_FAIL);
184 }
185
186 do {
Lev Walkin4c36e302004-06-06 07:20:02 +0000187 asn1_TYPE_tag2member_t *t2m;
188 asn1_TYPE_tag2member_t key;
Lev Walkinf15320b2004-06-03 03:38:44 +0000189
190 key.el_tag = tlv_tag;
Lev Walkinc2346572004-08-11 09:07:36 +0000191 (void *)t2m = bsearch(&key,
192 specs->tag2el, specs->tag2el_count,
Lev Walkinf15320b2004-06-03 03:38:44 +0000193 sizeof(specs->tag2el[0]), _search4tag);
194 if(t2m) {
195 /*
196 * Found the element corresponding to the tag.
197 */
198 NEXT_PHASE(ctx);
199 ctx->step = t2m->el_no;
200 break;
201 } else if(specs->extensible == 0) {
202 ASN_DEBUG("Unexpected tag %s "
203 "in non-extensible CHOICE %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000204 ber_tlv_tag_string(tlv_tag), td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000205 RETURN(RC_FAIL);
206 } else {
207 /* Skip this tag */
208 ssize_t skip;
209
210 ASN_DEBUG("Skipping unknown tag %s",
211 ber_tlv_tag_string(tlv_tag));
212
213 skip = ber_skip_length(
214 BER_TLV_CONSTRUCTED(ptr),
Lev Walkin4d9528c2004-08-11 08:10:13 +0000215 (char *)ptr + tag_len, LEFT - tag_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000216
217 switch(skip) {
218 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
219 /* Fall through */
220 case -1: RETURN(RC_FAIL);
221 }
222
223 ADVANCE(skip + tag_len);
224 RETURN(RC_OK);
225 }
226 } while(0);
227
228 case 2:
229 /*
230 * PHASE 2.
231 * Read in the element.
232 */
233 do {
Lev Walkin449f8322004-08-20 13:23:42 +0000234 asn1_TYPE_member_t *elm;/* CHOICE's element */
Lev Walkinf15320b2004-06-03 03:38:44 +0000235 void *memb_ptr; /* Pointer to the member */
Lev Walkinc2346572004-08-11 09:07:36 +0000236 void **memb_ptr2; /* Pointer to that pointer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000237
238 elm = &elements[ctx->step];
239
240 /*
241 * Compute the position of the member inside a structure,
242 * and also a type of containment (it may be contained
243 * as pointer or using inline inclusion).
244 */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000245 if(elm->flags & ATF_POINTER) {
246 /* Member is a pointer to another structure */
Lev Walkinc2346572004-08-11 09:07:36 +0000247 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000248 } else {
249 /*
250 * A pointer to a pointer
251 * holding the start of the structure
252 */
253 memb_ptr = (char *)st + elm->memb_offset;
254 memb_ptr2 = &memb_ptr;
255 }
256 /*
257 * Invoke the member fetch routine according to member's type
258 */
Lev Walkinc2346572004-08-11 09:07:36 +0000259 rval = elm->type->ber_decoder(elm->type,
Lev Walkinf15320b2004-06-03 03:38:44 +0000260 memb_ptr2, ptr, LEFT,
261 elm->tag_mode);
262 switch(rval.code) {
263 case RC_OK:
264 _set_present_idx(st, specs->pres_offset,
265 specs->pres_size, ctx->step + 1);
266 break;
267 case RC_WMORE: /* More data expected */
268 if(!SIZE_VIOLATION) {
269 ADVANCE(rval.consumed);
270 RETURN(RC_WMORE);
271 }
272 RETURN(RC_FAIL);
273 case RC_FAIL: /* Fatal error */
274 RETURN(rval.code);
275 } /* switch(rval) */
276
277 ADVANCE(rval.consumed);
278 } while(0);
279
280 NEXT_PHASE(ctx);
281
282 /* Fall through */
283 case 3:
284 ASN_DEBUG("CHOICE %s Leftover: %ld, size = %ld, tm=%d, tc=%d",
Lev Walkin449f8322004-08-20 13:23:42 +0000285 td->name, (long)ctx->left, (long)size,
286 tag_mode, td->tags_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000287
288 if(ctx->left > 0) {
289 /*
290 * The type must be fully decoded
291 * by the CHOICE member-specific decoder.
292 */
293 RETURN(RC_FAIL);
294 }
295
296 if(ctx->left == -1
Lev Walkin449f8322004-08-20 13:23:42 +0000297 && !(tag_mode || td->tags_count)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000298 /*
299 * This is an untagged CHOICE.
300 * It doesn't contain nothing
301 * except for the member itself, including all its tags.
302 * The decoding is completed.
303 */
304 NEXT_PHASE(ctx);
305 break;
306 }
307
308 /*
309 * Read in the "end of data chunks"'s.
310 */
311 while(ctx->left < 0) {
312 ssize_t tl;
313
314 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
315 switch(tl) {
316 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
317 /* Fall through */
318 case -1: RETURN(RC_FAIL);
319 }
320
321 /*
322 * Expected <0><0>...
323 */
324 if(((uint8_t *)ptr)[0] == 0) {
325 if(LEFT < 2) {
326 if(SIZE_VIOLATION)
327 RETURN(RC_FAIL);
328 else
329 RETURN(RC_WMORE);
330 } else if(((uint8_t *)ptr)[1] == 0) {
331 /*
332 * Correctly finished with <0><0>.
333 */
334 continue;
335 }
336 } else {
337 ASN_DEBUG("Unexpected continuation in %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000338 td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000339 RETURN(RC_FAIL);
340 }
341
342 ADVANCE(2);
343 ctx->left++;
344 }
345
346 NEXT_PHASE(ctx);
347 case 4:
348 /* No meaningful work here */
349 break;
350 }
351
352 RETURN(RC_OK);
353}
354
Lev Walkina9cc46e2004-09-22 16:06:28 +0000355asn_enc_rval_t
Lev Walkin449f8322004-08-20 13:23:42 +0000356CHOICE_encode_der(asn1_TYPE_descriptor_t *td,
Lev Walkinf15320b2004-06-03 03:38:44 +0000357 void *struct_ptr,
358 int tag_mode, ber_tlv_tag_t tag,
359 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin449f8322004-08-20 13:23:42 +0000360 asn1_CHOICE_specifics_t *specs = (asn1_CHOICE_specifics_t *)td->specifics;
361 asn1_TYPE_member_t *elm; /* CHOICE element */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000362 asn_enc_rval_t erval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000363 void *memb_ptr;
364 size_t computed_size = 0;
365 int present;
366
367 ASN_DEBUG("%s %s as CHOICE",
Lev Walkin449f8322004-08-20 13:23:42 +0000368 cb?"Encoding":"Estimating", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000369
370 present = _fetch_present_idx(struct_ptr,
371 specs->pres_offset, specs->pres_size);
372
373 /*
374 * If the structure was not initialized, it cannot be encoded:
375 * can't deduce what to encode in the choice type.
376 */
Lev Walkin449f8322004-08-20 13:23:42 +0000377 if(present <= 0 || present > td->elements_count) {
378 if(present == 0 && td->elements_count == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000379 /* The CHOICE is empty?! */
380 erval.encoded = 0;
381 return erval;
382 }
383 erval.encoded = -1;
Lev Walkin449f8322004-08-20 13:23:42 +0000384 erval.failed_type = td;
Lev Walkinf15320b2004-06-03 03:38:44 +0000385 erval.structure_ptr = struct_ptr;
386 return erval;
387 }
388
389 /*
390 * Seek over the present member of the structure.
391 */
Lev Walkin449f8322004-08-20 13:23:42 +0000392 elm = &td->elements[present-1];
Lev Walkincc93b0f2004-09-10 09:18:20 +0000393 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000394 memb_ptr = *(void **)((char *)struct_ptr + elm->memb_offset);
395 if(memb_ptr == 0) {
Lev Walkincc93b0f2004-09-10 09:18:20 +0000396 if(elm->optional) {
397 erval.encoded = 0;
398 } else {
399 /* Mandatory element absent */
400 erval.encoded = -1;
401 erval.failed_type = td;
402 erval.structure_ptr = struct_ptr;
403 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000404 return erval;
405 }
406 } else {
407 memb_ptr = (void *)((char *)struct_ptr + elm->memb_offset);
408 }
409
410 /*
411 * If the CHOICE itself is tagged EXPLICIT:
412 * T ::= [2] EXPLICIT CHOICE { ... }
413 * Then emit the appropriate tags.
414 */
Lev Walkin449f8322004-08-20 13:23:42 +0000415 if(tag_mode == 1 || td->tags_count) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000416 /*
417 * For this, we need to pre-compute the member.
418 */
419 ssize_t ret;
420
421 /* Encode member with its tag */
422 erval = elm->type->der_encoder(elm->type, memb_ptr,
423 elm->tag_mode, elm->tag, 0, 0);
424 if(erval.encoded == -1)
425 return erval;
426
427 /* Encode CHOICE with parent or my own tag */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000428 ret = der_write_tags(td, erval.encoded, tag_mode, 1, tag,
Lev Walkinf15320b2004-06-03 03:38:44 +0000429 cb, app_key);
430 if(ret == -1) {
431 erval.encoded = -1;
Lev Walkin449f8322004-08-20 13:23:42 +0000432 erval.failed_type = td;
Lev Walkinf15320b2004-06-03 03:38:44 +0000433 erval.structure_ptr = struct_ptr;
434 return erval;
435 }
436 computed_size += ret;
437 }
438
439 /*
440 * Encode the single underlying member.
441 */
442 erval = elm->type->der_encoder(elm->type, memb_ptr,
443 elm->tag_mode, elm->tag, cb, app_key);
444 if(erval.encoded == -1)
445 return erval;
446
447 ASN_DEBUG("Encoded CHOICE member in %ld bytes (+%ld)",
448 (long)erval.encoded, (long)computed_size);
449
450 erval.encoded += computed_size;
451
452 return erval;
453}
454
455ber_tlv_tag_t
456CHOICE_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 +0000457 asn1_CHOICE_specifics_t *specs = (asn1_CHOICE_specifics_t *)td->specifics;
Lev Walkinf15320b2004-06-03 03:38:44 +0000458 int present;
459
460 assert(tag_mode == 0);
461 assert(tag == 0);
462
463 /*
464 * Figure out which CHOICE element is encoded.
465 */
466 present = _fetch_present_idx(ptr, specs->pres_offset, specs->pres_size);
467
Lev Walkin449f8322004-08-20 13:23:42 +0000468 if(present > 0 || present <= td->elements_count) {
469 asn1_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkin70e70282004-06-05 01:45:48 +0000470 const void *memb_ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000471
Lev Walkincc93b0f2004-09-10 09:18:20 +0000472 if(elm->flags & ATF_POINTER) {
Lev Walkin70e70282004-06-05 01:45:48 +0000473 memb_ptr = *(const void * const *)
474 ((const char *)ptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000475 } else {
Lev Walkin70e70282004-06-05 01:45:48 +0000476 memb_ptr = (const void *)
477 ((const char *)ptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000478 }
479
480 return asn1_TYPE_outmost_tag(elm->type, memb_ptr,
481 elm->tag_mode, elm->tag);
482 } else {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000483 return (ber_tlv_tag_t)-1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000484 }
485}
486
487int
488CHOICE_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
489 asn_app_consume_bytes_f *app_errlog, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000490 asn1_CHOICE_specifics_t *specs = (asn1_CHOICE_specifics_t *)td->specifics;
Lev Walkinf15320b2004-06-03 03:38:44 +0000491 int present;
492
493 if(!sptr) {
Lev Walkinba4e5182004-08-11 09:44:13 +0000494 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkin16835b62004-08-22 13:47:59 +0000495 "%s: value not given (%s:%d)",
496 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000497 return -1;
498 }
499
500 /*
501 * Figure out which CHOICE element is encoded.
502 */
503 present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
Lev Walkin449f8322004-08-20 13:23:42 +0000504 if(present > 0 && present <= td->elements_count) {
505 asn1_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkinf15320b2004-06-03 03:38:44 +0000506 const void *memb_ptr;
507
Lev Walkincc93b0f2004-09-10 09:18:20 +0000508 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000509 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkincc93b0f2004-09-10 09:18:20 +0000510 if(!memb_ptr) {
511 if(elm->optional)
512 return 0;
513 _ASN_ERRLOG(app_errlog, app_key,
514 "%s: mandatory CHOICE element %s absent (%s:%d)",
515 td->name, elm->name, __FILE__, __LINE__);
516 return -1;
517 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000518 } else {
519 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
520 }
521
Lev Walkin449f8322004-08-20 13:23:42 +0000522 if(elm->memb_constraints) {
523 return elm->memb_constraints(elm->type, memb_ptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000524 app_errlog, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +0000525 } else {
526 int ret = elm->type->check_constraints(elm->type,
527 memb_ptr, app_errlog, app_key);
528 /*
529 * Cannot inherit it eralier:
530 * need to make sure we get the updated version.
531 */
532 elm->memb_constraints = elm->type->check_constraints;
533 return ret;
534 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000535 } else {
Lev Walkinba4e5182004-08-11 09:44:13 +0000536 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkind34a8512004-08-22 13:55:49 +0000537 "%s: no CHOICE element given (%s:%d)",
Lev Walkin16835b62004-08-22 13:47:59 +0000538 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000539 return -1;
540 }
541}
542
Lev Walkina9cc46e2004-09-22 16:06:28 +0000543asn_enc_rval_t
544CHOICE_encode_xer(asn1_TYPE_descriptor_t *td, void *sptr,
545 int ilevel, enum xer_encoder_flags_e flags,
546 asn_app_consume_bytes_f *cb, void *app_key) {
547 asn1_CHOICE_specifics_t *specs=(asn1_CHOICE_specifics_t *)td->specifics;
548 asn_enc_rval_t er;
549 int present;
550
551 if(!sptr)
552 _ASN_ENCODE_FAILED;
553
554 /*
555 * Figure out which CHOICE element is encoded.
556 */
557 present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
558
559 if(present <= 0 || present > td->elements_count) {
560 _ASN_ENCODE_FAILED;
561 } else {
562 asn_enc_rval_t tmper;
563 asn1_TYPE_member_t *elm = &td->elements[present-1];
564 void *memb_ptr;
565 const char *mname = elm->name;
566 unsigned int mlen = strlen(mname);
567
568 if(elm->flags & ATF_POINTER) {
569 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
570 if(!memb_ptr) _ASN_ENCODE_FAILED;
571 } else {
572 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
573 }
574
575 er.encoded = 0;
576
577 if(!(flags & XER_F_CANONICAL)) _i_ASN_TEXT_INDENT(1, ilevel);
578 _ASN_CALLBACK3("<", 1, mname, mlen, ">", 1);
579
580 tmper = elm->type->xer_encoder(elm->type, memb_ptr,
581 ilevel + 1, flags, cb, app_key);
582 if(tmper.encoded == -1) return tmper;
583
584 _ASN_CALLBACK3("</", 2, mname, mlen, ">", 1);
585
586 er.encoded += 5 + (2 * mlen) + tmper.encoded;
587 }
588
589 if(!(flags & XER_F_CANONICAL)) _i_ASN_TEXT_INDENT(1, ilevel - 1);
590
591 return er;
592}
593
Lev Walkinf15320b2004-06-03 03:38:44 +0000594int
595CHOICE_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
596 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000597 asn1_CHOICE_specifics_t *specs = (asn1_CHOICE_specifics_t *)td->specifics;
Lev Walkinf15320b2004-06-03 03:38:44 +0000598 int present;
599
Lev Walkin8e8078a2004-09-26 13:10:40 +0000600 if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000601
602 /*
603 * Figure out which CHOICE element is encoded.
604 */
605 present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
606
607 /*
Lev Walkina9cc46e2004-09-22 16:06:28 +0000608 * Print that element.
Lev Walkinf15320b2004-06-03 03:38:44 +0000609 */
Lev Walkin449f8322004-08-20 13:23:42 +0000610 if(present > 0 && present <= td->elements_count) {
611 asn1_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkinf15320b2004-06-03 03:38:44 +0000612 const void *memb_ptr;
613
Lev Walkincc93b0f2004-09-10 09:18:20 +0000614 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000615 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkin8e8078a2004-09-26 13:10:40 +0000616 if(!memb_ptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000617 } else {
618 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
619 }
620
621 /* Print member's name and stuff */
Lev Walkincc6a9102004-09-23 22:06:26 +0000622 if(0) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000623 if(cb(elm->name, strlen(elm->name), app_key) < 0
624 || cb(": ", 2, app_key) < 0)
Lev Walkincc6a9102004-09-23 22:06:26 +0000625 return -1;
626 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000627
628 return elm->type->print_struct(elm->type, memb_ptr, ilevel,
629 cb, app_key);
630 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000631 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000632 }
633}
634
635void
636CHOICE_free(asn1_TYPE_descriptor_t *td, void *ptr, int contents_only) {
Lev Walkinc2346572004-08-11 09:07:36 +0000637 asn1_CHOICE_specifics_t *specs = (asn1_CHOICE_specifics_t *)td->specifics;
Lev Walkinf15320b2004-06-03 03:38:44 +0000638 int present;
639
640 if(!td || !ptr)
641 return;
642
643 ASN_DEBUG("Freeing %s as CHOICE", td->name);
644
645 /*
646 * Figure out which CHOICE element is encoded.
647 */
648 present = _fetch_present_idx(ptr, specs->pres_offset, specs->pres_size);
649
650 /*
651 * Free that element.
652 */
Lev Walkin449f8322004-08-20 13:23:42 +0000653 if(present > 0 && present <= td->elements_count) {
654 asn1_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkinf15320b2004-06-03 03:38:44 +0000655 void *memb_ptr;
656
Lev Walkincc93b0f2004-09-10 09:18:20 +0000657 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000658 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
659 if(memb_ptr)
660 elm->type->free_struct(elm->type, memb_ptr, 0);
661 } else {
662 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
663 elm->type->free_struct(elm->type, memb_ptr, 1);
664 }
665 }
666
667 if(!contents_only) {
668 FREEMEM(ptr);
669 }
670}
671
672
673/*
674 * The following functions functions offer protection against -fshort-enums,
675 * compatible with little- and big-endian machines.
676 * If assertion is triggered, either disable -fshort-enums, or add an entry
677 * here with the ->pres_size of your target stracture.
678 * Unless the target structure is packed, the ".present" member
679 * is guaranteed to be aligned properly. ASN.1 compiler itself does not
680 * produce packed code.
681 */
Lev Walkin91f5cd02004-08-11 09:10:59 +0000682static int
Lev Walkinf15320b2004-06-03 03:38:44 +0000683_fetch_present_idx(const void *struct_ptr, int pres_offset, int pres_size) {
684 const void *present_ptr;
685 int present;
686
687 present_ptr = ((const char *)struct_ptr) + pres_offset;
688
689 switch(pres_size) {
690 case sizeof(int): present = *(const int *)present_ptr; break;
691 case sizeof(short): present = *(const short *)present_ptr; break;
692 case sizeof(char): present = *(const char *)present_ptr; break;
693 default:
694 /* ANSI C mandates enum to be equivalent to integer */
695 assert(pres_size != sizeof(int));
696 return 0; /* If not aborted, pass back safe value */
697 }
698
699 return present;
700}
701
Lev Walkin91f5cd02004-08-11 09:10:59 +0000702static void
Lev Walkinf15320b2004-06-03 03:38:44 +0000703_set_present_idx(void *struct_ptr, int pres_offset, int pres_size, int present) {
704 void *present_ptr;
705 present_ptr = ((char *)struct_ptr) + pres_offset;
706
707 switch(pres_size) {
708 case sizeof(int): *(int *)present_ptr = present; break;
709 case sizeof(short): *(short *)present_ptr = present; break;
710 case sizeof(char): *(char *)present_ptr = present; break;
711 default:
712 /* ANSI C mandates enum to be equivalent to integer */
713 assert(pres_size != sizeof(int));
714 }
715}