blob: 236b9ad5a790d8bb68694211190c46df42162b54 [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 Walkin5e033762004-09-29 13:26:15 +000073 const asn_TYPE_tag2member_t *a = (const asn_TYPE_tag2member_t *)ap;
74 const asn_TYPE_tag2member_t *b = (const asn_TYPE_tag2member_t *)bp;
Lev Walkinc2346572004-08-11 09:07:36 +000075
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 Walkin5e033762004-09-29 13:26:15 +0000100CHOICE_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_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 Walkin5e033762004-09-29 13:26:15 +0000105 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
106 asn_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. */
Lev Walkin5e033762004-09-29 13:26:15 +0000112 asn_struct_ctx_t *ctx; /* Decoder context */
Lev Walkinf15320b2004-06-03 03:38:44 +0000113
114 ber_tlv_tag_t tlv_tag; /* T from TLV */
115 ssize_t tag_len; /* Length of TLV's T */
Lev Walkinf15320b2004-06-03 03:38:44 +0000116 ber_dec_rval_t rval; /* Return code from subparsers */
117
118 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
119
Lev Walkin449f8322004-08-20 13:23:42 +0000120 ASN_DEBUG("Decoding %s as CHOICE", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000121
122 /*
123 * Create the target structure if it is not present already.
124 */
125 if(st == 0) {
126 st = *struct_ptr = CALLOC(1, specs->struct_size);
127 if(st == 0) {
128 RETURN(RC_FAIL);
129 }
130 }
131
132 /*
133 * Restore parsing context.
134 */
Lev Walkin5e033762004-09-29 13:26:15 +0000135 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000136
137 /*
138 * Start to parse where left previously
139 */
140 switch(ctx->phase) {
141 case 0:
142 /*
143 * PHASE 0.
144 * Check that the set of tags associated with given structure
145 * perfectly fits our expectations.
146 */
147
Lev Walkin449f8322004-08-20 13:23:42 +0000148 if(tag_mode || td->tags_count) {
Lev Walkin5e033762004-09-29 13:26:15 +0000149 rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size,
Lev Walkin8e8078a2004-09-26 13:10:40 +0000150 tag_mode, -1, &ctx->left, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000151 if(rval.code != RC_OK) {
152 ASN_DEBUG("%s tagging check failed: %d",
Lev Walkin449f8322004-08-20 13:23:42 +0000153 td->name, rval.code);
Lev Walkinf0f04d12004-10-05 06:36:02 +0000154 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000155 }
156
157 if(ctx->left >= 0) {
158 /* ?Substracted below! */
159 ctx->left += rval.consumed;
160 }
161 ADVANCE(rval.consumed);
162 } else {
163 ctx->left = -1;
164 }
165
166 NEXT_PHASE(ctx);
167
168 ASN_DEBUG("Structure consumes %ld bytes, buffer %ld",
169 (long)ctx->left, (long)size);
170
171 /* Fall through */
172 case 1:
173 /*
174 * Fetch the T from TLV.
175 */
176 tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
Lev Walkin449f8322004-08-20 13:23:42 +0000177 ASN_DEBUG("In %s CHOICE tag length %d", td->name, (int)tag_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000178 switch(tag_len) {
179 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
180 /* Fall through */
181 case -1: RETURN(RC_FAIL);
182 }
183
184 do {
Lev Walkin5e033762004-09-29 13:26:15 +0000185 asn_TYPE_tag2member_t *t2m;
186 asn_TYPE_tag2member_t key;
Lev Walkinf15320b2004-06-03 03:38:44 +0000187
188 key.el_tag = tlv_tag;
Lev Walkinc2346572004-08-11 09:07:36 +0000189 (void *)t2m = bsearch(&key,
190 specs->tag2el, specs->tag2el_count,
Lev Walkinf15320b2004-06-03 03:38:44 +0000191 sizeof(specs->tag2el[0]), _search4tag);
192 if(t2m) {
193 /*
194 * Found the element corresponding to the tag.
195 */
196 NEXT_PHASE(ctx);
197 ctx->step = t2m->el_no;
198 break;
199 } else if(specs->extensible == 0) {
200 ASN_DEBUG("Unexpected tag %s "
201 "in non-extensible CHOICE %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000202 ber_tlv_tag_string(tlv_tag), td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000203 RETURN(RC_FAIL);
204 } else {
205 /* Skip this tag */
206 ssize_t skip;
207
208 ASN_DEBUG("Skipping unknown tag %s",
209 ber_tlv_tag_string(tlv_tag));
210
Lev Walkinbaaa24f2004-09-29 14:19:14 +0000211 skip = ber_skip_length(opt_codec_ctx,
Lev Walkinf15320b2004-06-03 03:38:44 +0000212 BER_TLV_CONSTRUCTED(ptr),
Lev Walkin4d9528c2004-08-11 08:10:13 +0000213 (char *)ptr + tag_len, LEFT - tag_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000214
215 switch(skip) {
216 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
217 /* Fall through */
218 case -1: RETURN(RC_FAIL);
219 }
220
221 ADVANCE(skip + tag_len);
222 RETURN(RC_OK);
223 }
224 } while(0);
225
226 case 2:
227 /*
228 * PHASE 2.
229 * Read in the element.
230 */
231 do {
Lev Walkin5e033762004-09-29 13:26:15 +0000232 asn_TYPE_member_t *elm;/* CHOICE's element */
Lev Walkinf15320b2004-06-03 03:38:44 +0000233 void *memb_ptr; /* Pointer to the member */
Lev Walkinc2346572004-08-11 09:07:36 +0000234 void **memb_ptr2; /* Pointer to that pointer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000235
236 elm = &elements[ctx->step];
237
238 /*
239 * Compute the position of the member inside a structure,
240 * and also a type of containment (it may be contained
241 * as pointer or using inline inclusion).
242 */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000243 if(elm->flags & ATF_POINTER) {
244 /* Member is a pointer to another structure */
Lev Walkinc2346572004-08-11 09:07:36 +0000245 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000246 } else {
247 /*
248 * A pointer to a pointer
249 * holding the start of the structure
250 */
251 memb_ptr = (char *)st + elm->memb_offset;
252 memb_ptr2 = &memb_ptr;
253 }
254 /*
255 * Invoke the member fetch routine according to member's type
256 */
Lev Walkin5e033762004-09-29 13:26:15 +0000257 rval = elm->type->ber_decoder(opt_codec_ctx, elm->type,
Lev Walkinf15320b2004-06-03 03:38:44 +0000258 memb_ptr2, ptr, LEFT,
259 elm->tag_mode);
260 switch(rval.code) {
261 case RC_OK:
262 _set_present_idx(st, specs->pres_offset,
263 specs->pres_size, ctx->step + 1);
264 break;
265 case RC_WMORE: /* More data expected */
266 if(!SIZE_VIOLATION) {
267 ADVANCE(rval.consumed);
268 RETURN(RC_WMORE);
269 }
270 RETURN(RC_FAIL);
271 case RC_FAIL: /* Fatal error */
272 RETURN(rval.code);
273 } /* switch(rval) */
274
275 ADVANCE(rval.consumed);
276 } while(0);
277
278 NEXT_PHASE(ctx);
279
280 /* Fall through */
281 case 3:
282 ASN_DEBUG("CHOICE %s Leftover: %ld, size = %ld, tm=%d, tc=%d",
Lev Walkin449f8322004-08-20 13:23:42 +0000283 td->name, (long)ctx->left, (long)size,
284 tag_mode, td->tags_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000285
286 if(ctx->left > 0) {
287 /*
288 * The type must be fully decoded
289 * by the CHOICE member-specific decoder.
290 */
291 RETURN(RC_FAIL);
292 }
293
294 if(ctx->left == -1
Lev Walkin449f8322004-08-20 13:23:42 +0000295 && !(tag_mode || td->tags_count)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000296 /*
297 * This is an untagged CHOICE.
298 * It doesn't contain nothing
299 * except for the member itself, including all its tags.
300 * The decoding is completed.
301 */
302 NEXT_PHASE(ctx);
303 break;
304 }
305
306 /*
307 * Read in the "end of data chunks"'s.
308 */
309 while(ctx->left < 0) {
310 ssize_t tl;
311
312 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
313 switch(tl) {
314 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
315 /* Fall through */
316 case -1: RETURN(RC_FAIL);
317 }
318
319 /*
320 * Expected <0><0>...
321 */
322 if(((uint8_t *)ptr)[0] == 0) {
323 if(LEFT < 2) {
324 if(SIZE_VIOLATION)
325 RETURN(RC_FAIL);
326 else
327 RETURN(RC_WMORE);
328 } else if(((uint8_t *)ptr)[1] == 0) {
329 /*
330 * Correctly finished with <0><0>.
331 */
Lev Walkinf0f04d12004-10-05 06:36:02 +0000332 ADVANCE(2);
333 ctx->left++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000334 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
Lev Walkinf0f04d12004-10-05 06:36:02 +0000342 /* UNREACHABLE */
Lev Walkinf15320b2004-06-03 03:38:44 +0000343 }
344
345 NEXT_PHASE(ctx);
346 case 4:
347 /* No meaningful work here */
348 break;
349 }
350
351 RETURN(RC_OK);
352}
353
Lev Walkina9cc46e2004-09-22 16:06:28 +0000354asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000355CHOICE_encode_der(asn_TYPE_descriptor_t *td,
Lev Walkinf15320b2004-06-03 03:38:44 +0000356 void *struct_ptr,
357 int tag_mode, ber_tlv_tag_t tag,
358 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin5e033762004-09-29 13:26:15 +0000359 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
360 asn_TYPE_member_t *elm; /* CHOICE element */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000361 asn_enc_rval_t erval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000362 void *memb_ptr;
363 size_t computed_size = 0;
364 int present;
365
366 ASN_DEBUG("%s %s as CHOICE",
Lev Walkin449f8322004-08-20 13:23:42 +0000367 cb?"Encoding":"Estimating", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000368
369 present = _fetch_present_idx(struct_ptr,
370 specs->pres_offset, specs->pres_size);
371
372 /*
373 * If the structure was not initialized, it cannot be encoded:
374 * can't deduce what to encode in the choice type.
375 */
Lev Walkin449f8322004-08-20 13:23:42 +0000376 if(present <= 0 || present > td->elements_count) {
377 if(present == 0 && td->elements_count == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000378 /* The CHOICE is empty?! */
379 erval.encoded = 0;
380 return erval;
381 }
382 erval.encoded = -1;
Lev Walkin449f8322004-08-20 13:23:42 +0000383 erval.failed_type = td;
Lev Walkinf15320b2004-06-03 03:38:44 +0000384 erval.structure_ptr = struct_ptr;
385 return erval;
386 }
387
388 /*
389 * Seek over the present member of the structure.
390 */
Lev Walkin449f8322004-08-20 13:23:42 +0000391 elm = &td->elements[present-1];
Lev Walkincc93b0f2004-09-10 09:18:20 +0000392 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000393 memb_ptr = *(void **)((char *)struct_ptr + elm->memb_offset);
394 if(memb_ptr == 0) {
Lev Walkincc93b0f2004-09-10 09:18:20 +0000395 if(elm->optional) {
396 erval.encoded = 0;
397 } else {
398 /* Mandatory element absent */
399 erval.encoded = -1;
400 erval.failed_type = td;
401 erval.structure_ptr = struct_ptr;
402 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000403 return erval;
404 }
405 } else {
406 memb_ptr = (void *)((char *)struct_ptr + elm->memb_offset);
407 }
408
409 /*
410 * If the CHOICE itself is tagged EXPLICIT:
411 * T ::= [2] EXPLICIT CHOICE { ... }
412 * Then emit the appropriate tags.
413 */
Lev Walkin449f8322004-08-20 13:23:42 +0000414 if(tag_mode == 1 || td->tags_count) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000415 /*
416 * For this, we need to pre-compute the member.
417 */
418 ssize_t ret;
419
420 /* Encode member with its tag */
421 erval = elm->type->der_encoder(elm->type, memb_ptr,
422 elm->tag_mode, elm->tag, 0, 0);
423 if(erval.encoded == -1)
424 return erval;
425
426 /* Encode CHOICE with parent or my own tag */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000427 ret = der_write_tags(td, erval.encoded, tag_mode, 1, tag,
Lev Walkinf15320b2004-06-03 03:38:44 +0000428 cb, app_key);
429 if(ret == -1) {
430 erval.encoded = -1;
Lev Walkin449f8322004-08-20 13:23:42 +0000431 erval.failed_type = td;
Lev Walkinf15320b2004-06-03 03:38:44 +0000432 erval.structure_ptr = struct_ptr;
433 return erval;
434 }
435 computed_size += ret;
436 }
437
438 /*
439 * Encode the single underlying member.
440 */
441 erval = elm->type->der_encoder(elm->type, memb_ptr,
442 elm->tag_mode, elm->tag, cb, app_key);
443 if(erval.encoded == -1)
444 return erval;
445
446 ASN_DEBUG("Encoded CHOICE member in %ld bytes (+%ld)",
447 (long)erval.encoded, (long)computed_size);
448
449 erval.encoded += computed_size;
450
451 return erval;
452}
453
454ber_tlv_tag_t
Lev Walkin5e033762004-09-29 13:26:15 +0000455CHOICE_outmost_tag(asn_TYPE_descriptor_t *td, const void *ptr, int tag_mode, ber_tlv_tag_t tag) {
456 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
Lev Walkinf15320b2004-06-03 03:38:44 +0000457 int present;
458
459 assert(tag_mode == 0);
460 assert(tag == 0);
461
462 /*
463 * Figure out which CHOICE element is encoded.
464 */
465 present = _fetch_present_idx(ptr, specs->pres_offset, specs->pres_size);
466
Lev Walkin449f8322004-08-20 13:23:42 +0000467 if(present > 0 || present <= td->elements_count) {
Lev Walkin5e033762004-09-29 13:26:15 +0000468 asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkin70e70282004-06-05 01:45:48 +0000469 const void *memb_ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000470
Lev Walkincc93b0f2004-09-10 09:18:20 +0000471 if(elm->flags & ATF_POINTER) {
Lev Walkin70e70282004-06-05 01:45:48 +0000472 memb_ptr = *(const void * const *)
473 ((const char *)ptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000474 } else {
Lev Walkin70e70282004-06-05 01:45:48 +0000475 memb_ptr = (const void *)
476 ((const char *)ptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000477 }
478
Lev Walkin5e033762004-09-29 13:26:15 +0000479 return asn_TYPE_outmost_tag(elm->type, memb_ptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000480 elm->tag_mode, elm->tag);
481 } else {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000482 return (ber_tlv_tag_t)-1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000483 }
484}
485
486int
Lev Walkin5e033762004-09-29 13:26:15 +0000487CHOICE_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000488 asn_app_consume_bytes_f *app_errlog, void *app_key) {
Lev Walkin5e033762004-09-29 13:26:15 +0000489 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
Lev Walkinf15320b2004-06-03 03:38:44 +0000490 int present;
491
492 if(!sptr) {
Lev Walkinba4e5182004-08-11 09:44:13 +0000493 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkin16835b62004-08-22 13:47:59 +0000494 "%s: value not given (%s:%d)",
495 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000496 return -1;
497 }
498
499 /*
500 * Figure out which CHOICE element is encoded.
501 */
502 present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
Lev Walkin449f8322004-08-20 13:23:42 +0000503 if(present > 0 && present <= td->elements_count) {
Lev Walkin5e033762004-09-29 13:26:15 +0000504 asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkinf15320b2004-06-03 03:38:44 +0000505 const void *memb_ptr;
506
Lev Walkincc93b0f2004-09-10 09:18:20 +0000507 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000508 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkincc93b0f2004-09-10 09:18:20 +0000509 if(!memb_ptr) {
510 if(elm->optional)
511 return 0;
512 _ASN_ERRLOG(app_errlog, app_key,
513 "%s: mandatory CHOICE element %s absent (%s:%d)",
514 td->name, elm->name, __FILE__, __LINE__);
515 return -1;
516 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000517 } else {
518 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
519 }
520
Lev Walkin449f8322004-08-20 13:23:42 +0000521 if(elm->memb_constraints) {
522 return elm->memb_constraints(elm->type, memb_ptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000523 app_errlog, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +0000524 } else {
525 int ret = elm->type->check_constraints(elm->type,
526 memb_ptr, app_errlog, app_key);
527 /*
528 * Cannot inherit it eralier:
529 * need to make sure we get the updated version.
530 */
531 elm->memb_constraints = elm->type->check_constraints;
532 return ret;
533 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000534 } else {
Lev Walkinba4e5182004-08-11 09:44:13 +0000535 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkind34a8512004-08-22 13:55:49 +0000536 "%s: no CHOICE element given (%s:%d)",
Lev Walkin16835b62004-08-22 13:47:59 +0000537 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000538 return -1;
539 }
540}
541
Lev Walkina9cc46e2004-09-22 16:06:28 +0000542asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000543CHOICE_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000544 int ilevel, enum xer_encoder_flags_e flags,
545 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin5e033762004-09-29 13:26:15 +0000546 asn_CHOICE_specifics_t *specs=(asn_CHOICE_specifics_t *)td->specifics;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000547 asn_enc_rval_t er;
548 int present;
549
550 if(!sptr)
551 _ASN_ENCODE_FAILED;
552
553 /*
554 * Figure out which CHOICE element is encoded.
555 */
556 present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
557
558 if(present <= 0 || present > td->elements_count) {
559 _ASN_ENCODE_FAILED;
560 } else {
561 asn_enc_rval_t tmper;
Lev Walkin5e033762004-09-29 13:26:15 +0000562 asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkina9cc46e2004-09-22 16:06:28 +0000563 void *memb_ptr;
564 const char *mname = elm->name;
565 unsigned int mlen = strlen(mname);
566
567 if(elm->flags & ATF_POINTER) {
568 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
569 if(!memb_ptr) _ASN_ENCODE_FAILED;
570 } else {
571 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
572 }
573
574 er.encoded = 0;
575
576 if(!(flags & XER_F_CANONICAL)) _i_ASN_TEXT_INDENT(1, ilevel);
577 _ASN_CALLBACK3("<", 1, mname, mlen, ">", 1);
578
579 tmper = elm->type->xer_encoder(elm->type, memb_ptr,
580 ilevel + 1, flags, cb, app_key);
581 if(tmper.encoded == -1) return tmper;
582
583 _ASN_CALLBACK3("</", 2, mname, mlen, ">", 1);
584
585 er.encoded += 5 + (2 * mlen) + tmper.encoded;
586 }
587
588 if(!(flags & XER_F_CANONICAL)) _i_ASN_TEXT_INDENT(1, ilevel - 1);
589
590 return er;
Lev Walkin942fd082004-10-03 09:13:02 +0000591cb_failed:
592 _ASN_ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000593}
594
Lev Walkinf15320b2004-06-03 03:38:44 +0000595int
Lev Walkin5e033762004-09-29 13:26:15 +0000596CHOICE_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000597 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin5e033762004-09-29 13:26:15 +0000598 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
Lev Walkinf15320b2004-06-03 03:38:44 +0000599 int present;
600
Lev Walkin8e8078a2004-09-26 13:10:40 +0000601 if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000602
603 /*
604 * Figure out which CHOICE element is encoded.
605 */
606 present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
607
608 /*
Lev Walkina9cc46e2004-09-22 16:06:28 +0000609 * Print that element.
Lev Walkinf15320b2004-06-03 03:38:44 +0000610 */
Lev Walkin449f8322004-08-20 13:23:42 +0000611 if(present > 0 && present <= td->elements_count) {
Lev Walkin5e033762004-09-29 13:26:15 +0000612 asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkinf15320b2004-06-03 03:38:44 +0000613 const void *memb_ptr;
614
Lev Walkincc93b0f2004-09-10 09:18:20 +0000615 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000616 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkin8e8078a2004-09-26 13:10:40 +0000617 if(!memb_ptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000618 } else {
619 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
620 }
621
622 /* Print member's name and stuff */
Lev Walkincc6a9102004-09-23 22:06:26 +0000623 if(0) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000624 if(cb(elm->name, strlen(elm->name), app_key) < 0
625 || cb(": ", 2, app_key) < 0)
Lev Walkincc6a9102004-09-23 22:06:26 +0000626 return -1;
627 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000628
629 return elm->type->print_struct(elm->type, memb_ptr, ilevel,
630 cb, app_key);
631 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000632 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000633 }
634}
635
636void
Lev Walkin5e033762004-09-29 13:26:15 +0000637CHOICE_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
638 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
Lev Walkinf15320b2004-06-03 03:38:44 +0000639 int present;
640
641 if(!td || !ptr)
642 return;
643
644 ASN_DEBUG("Freeing %s as CHOICE", td->name);
645
646 /*
647 * Figure out which CHOICE element is encoded.
648 */
649 present = _fetch_present_idx(ptr, specs->pres_offset, specs->pres_size);
650
651 /*
652 * Free that element.
653 */
Lev Walkin449f8322004-08-20 13:23:42 +0000654 if(present > 0 && present <= td->elements_count) {
Lev Walkin5e033762004-09-29 13:26:15 +0000655 asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkinf15320b2004-06-03 03:38:44 +0000656 void *memb_ptr;
657
Lev Walkincc93b0f2004-09-10 09:18:20 +0000658 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000659 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
660 if(memb_ptr)
661 elm->type->free_struct(elm->type, memb_ptr, 0);
662 } else {
663 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
664 elm->type->free_struct(elm->type, memb_ptr, 1);
665 }
666 }
667
668 if(!contents_only) {
669 FREEMEM(ptr);
670 }
671}
672
673
674/*
675 * The following functions functions offer protection against -fshort-enums,
676 * compatible with little- and big-endian machines.
677 * If assertion is triggered, either disable -fshort-enums, or add an entry
678 * here with the ->pres_size of your target stracture.
679 * Unless the target structure is packed, the ".present" member
680 * is guaranteed to be aligned properly. ASN.1 compiler itself does not
681 * produce packed code.
682 */
Lev Walkin91f5cd02004-08-11 09:10:59 +0000683static int
Lev Walkinf15320b2004-06-03 03:38:44 +0000684_fetch_present_idx(const void *struct_ptr, int pres_offset, int pres_size) {
685 const void *present_ptr;
686 int present;
687
688 present_ptr = ((const char *)struct_ptr) + pres_offset;
689
690 switch(pres_size) {
691 case sizeof(int): present = *(const int *)present_ptr; break;
692 case sizeof(short): present = *(const short *)present_ptr; break;
693 case sizeof(char): present = *(const char *)present_ptr; break;
694 default:
695 /* ANSI C mandates enum to be equivalent to integer */
696 assert(pres_size != sizeof(int));
697 return 0; /* If not aborted, pass back safe value */
698 }
699
700 return present;
701}
702
Lev Walkin91f5cd02004-08-11 09:10:59 +0000703static void
Lev Walkinf15320b2004-06-03 03:38:44 +0000704_set_present_idx(void *struct_ptr, int pres_offset, int pres_size, int present) {
705 void *present_ptr;
706 present_ptr = ((char *)struct_ptr) + pres_offset;
707
708 switch(pres_size) {
709 case sizeof(int): *(int *)present_ptr = present; break;
710 case sizeof(short): *(short *)present_ptr = present; break;
711 case sizeof(char): *(char *)present_ptr = present; break;
712 default:
713 /* ANSI C mandates enum to be equivalent to integer */
714 assert(pres_size != sizeof(int));
715 }
716}