blob: 224e56a90dad9023951a966aff8d0999b9a3bc27 [file] [log] [blame]
Lev Walkin523de9e2006-08-18 01:34:18 +00001/*
2 * Copyright (c) 2003, 2004, 2005, 2006 Lev Walkin <vlm@lionet.info>.
Lev Walkinc61f3862005-02-14 17:21:22 +00003 * All rights reserved.
Lev Walkinf15320b2004-06-03 03:38:44 +00004 * Redistribution and modifications are permitted subject to BSD license.
5 */
Lev Walkina9cc46e2004-09-22 16:06:28 +00006#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00007#include <constr_CHOICE.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00008
9/*
10 * Number of bytes left for this structure.
11 * (ctx->left) indicates the number of bytes _transferred_ for the structure.
12 * (size) contains the number of bytes in the buffer passed.
13 */
Lev Walkinec1ffd42004-08-18 04:53:32 +000014#define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left)
Lev Walkinf15320b2004-06-03 03:38:44 +000015
16/*
17 * If the subprocessor function returns with an indication that it wants
18 * more data, it may well be a fatal decoding problem, because the
19 * size is constrained by the <TLV>'s L, even if the buffer size allows
20 * reading more data.
21 * For example, consider the buffer containing the following TLVs:
22 * <T:5><L:1><V> <T:6>...
23 * The TLV length clearly indicates that one byte is expected in V, but
24 * if the V processor returns with "want more data" even if the buffer
25 * contains way more data than the V processor have seen.
26 */
Lev Walkind9bd7752004-06-05 08:17:50 +000027#define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size)
Lev Walkinf15320b2004-06-03 03:38:44 +000028
29/*
30 * This macro "eats" the part of the buffer which is definitely "consumed",
31 * i.e. was correctly converted into local representation or rightfully skipped.
32 */
Lev Walkincc6a9102004-09-23 22:06:26 +000033#undef ADVANCE
Lev Walkinf15320b2004-06-03 03:38:44 +000034#define ADVANCE(num_bytes) do { \
35 size_t num = num_bytes; \
Lev Walkin8c3b8542005-03-10 18:52:02 +000036 ptr = ((const 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 */
Lev Walkindc06f6b2004-10-20 15:50:55 +000099asn_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 Walkin8c3b8542005-03-10 18:52:02 +0000101 void **struct_ptr, const void *ptr, size_t size, int tag_mode) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000102 /*
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 Walkindc06f6b2004-10-20 15:50:55 +0000116 asn_dec_rval_t rval; /* Return code from subparsers */
Lev Walkinf15320b2004-06-03 03:38:44 +0000117
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 Walkin59b176e2005-11-26 11:25:14 +0000121
Lev Walkinf15320b2004-06-03 03:38:44 +0000122 /*
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 Walkinc17d90f2005-01-17 14:32:45 +0000189 t2m = (asn_TYPE_tag2member_t *)bsearch(&key,
Lev Walkinc2346572004-08-11 09:07:36 +0000190 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;
Lev Walkin59b176e2005-11-26 11:25:14 +0000199 } else if(specs->ext_start == -1) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000200 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 Walkin8c3b8542005-03-10 18:52:02 +0000213 (const char *)ptr + tag_len,
214 LEFT - tag_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000215
216 switch(skip) {
217 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
218 /* Fall through */
219 case -1: RETURN(RC_FAIL);
220 }
221
222 ADVANCE(skip + tag_len);
223 RETURN(RC_OK);
224 }
225 } while(0);
226
227 case 2:
228 /*
229 * PHASE 2.
230 * Read in the element.
231 */
232 do {
Lev Walkin5e033762004-09-29 13:26:15 +0000233 asn_TYPE_member_t *elm;/* CHOICE's element */
Lev Walkinf15320b2004-06-03 03:38:44 +0000234 void *memb_ptr; /* Pointer to the member */
Lev Walkinc2346572004-08-11 09:07:36 +0000235 void **memb_ptr2; /* Pointer to that pointer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000236
237 elm = &elements[ctx->step];
238
239 /*
240 * Compute the position of the member inside a structure,
241 * and also a type of containment (it may be contained
242 * as pointer or using inline inclusion).
243 */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000244 if(elm->flags & ATF_POINTER) {
245 /* Member is a pointer to another structure */
Lev Walkinc2346572004-08-11 09:07:36 +0000246 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000247 } else {
248 /*
249 * A pointer to a pointer
250 * holding the start of the structure
251 */
252 memb_ptr = (char *)st + elm->memb_offset;
253 memb_ptr2 = &memb_ptr;
254 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000255 /* Set presence to be able to free it properly at any time */
256 _set_present_idx(st, specs->pres_offset,
257 specs->pres_size, ctx->step + 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000258 /*
259 * Invoke the member fetch routine according to member's type
260 */
Lev Walkin5e033762004-09-29 13:26:15 +0000261 rval = elm->type->ber_decoder(opt_codec_ctx, elm->type,
Lev Walkin59b176e2005-11-26 11:25:14 +0000262 memb_ptr2, ptr, LEFT, elm->tag_mode);
Lev Walkinf15320b2004-06-03 03:38:44 +0000263 switch(rval.code) {
264 case RC_OK:
Lev Walkinf15320b2004-06-03 03:38:44 +0000265 break;
266 case RC_WMORE: /* More data expected */
267 if(!SIZE_VIOLATION) {
268 ADVANCE(rval.consumed);
269 RETURN(RC_WMORE);
270 }
271 RETURN(RC_FAIL);
272 case RC_FAIL: /* Fatal error */
273 RETURN(rval.code);
274 } /* switch(rval) */
275
276 ADVANCE(rval.consumed);
277 } while(0);
278
279 NEXT_PHASE(ctx);
280
281 /* Fall through */
282 case 3:
283 ASN_DEBUG("CHOICE %s Leftover: %ld, size = %ld, tm=%d, tc=%d",
Lev Walkin449f8322004-08-20 13:23:42 +0000284 td->name, (long)ctx->left, (long)size,
285 tag_mode, td->tags_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000286
287 if(ctx->left > 0) {
288 /*
289 * The type must be fully decoded
290 * by the CHOICE member-specific decoder.
291 */
292 RETURN(RC_FAIL);
293 }
294
295 if(ctx->left == -1
Lev Walkin449f8322004-08-20 13:23:42 +0000296 && !(tag_mode || td->tags_count)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000297 /*
298 * This is an untagged CHOICE.
299 * It doesn't contain nothing
300 * except for the member itself, including all its tags.
301 * The decoding is completed.
302 */
303 NEXT_PHASE(ctx);
304 break;
305 }
306
307 /*
308 * Read in the "end of data chunks"'s.
309 */
310 while(ctx->left < 0) {
311 ssize_t tl;
312
313 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
314 switch(tl) {
315 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
316 /* Fall through */
317 case -1: RETURN(RC_FAIL);
318 }
319
320 /*
321 * Expected <0><0>...
322 */
Lev Walkin8c3b8542005-03-10 18:52:02 +0000323 if(((const uint8_t *)ptr)[0] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000324 if(LEFT < 2) {
325 if(SIZE_VIOLATION)
326 RETURN(RC_FAIL);
327 else
328 RETURN(RC_WMORE);
Lev Walkin8c3b8542005-03-10 18:52:02 +0000329 } else if(((const uint8_t *)ptr)[1] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000330 /*
331 * Correctly finished with <0><0>.
332 */
Lev Walkinf0f04d12004-10-05 06:36:02 +0000333 ADVANCE(2);
334 ctx->left++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000335 continue;
336 }
337 } else {
338 ASN_DEBUG("Unexpected continuation in %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000339 td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000340 RETURN(RC_FAIL);
341 }
342
Lev Walkinf0f04d12004-10-05 06:36:02 +0000343 /* UNREACHABLE */
Lev Walkinf15320b2004-06-03 03:38:44 +0000344 }
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 Walkinac589332005-08-22 14:19:28 +0000356CHOICE_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000357 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
Lev Walkin523de9e2006-08-18 01:34:18 +0000366 if(!sptr) _ASN_ENCODE_FAILED;
367
Lev Walkinf15320b2004-06-03 03:38:44 +0000368 ASN_DEBUG("%s %s as CHOICE",
Lev Walkin449f8322004-08-20 13:23:42 +0000369 cb?"Encoding":"Estimating", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000370
Lev Walkinac589332005-08-22 14:19:28 +0000371 present = _fetch_present_idx(sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000372 specs->pres_offset, specs->pres_size);
373
374 /*
375 * If the structure was not initialized, it cannot be encoded:
376 * can't deduce what to encode in the choice type.
377 */
Lev Walkin449f8322004-08-20 13:23:42 +0000378 if(present <= 0 || present > td->elements_count) {
379 if(present == 0 && td->elements_count == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000380 /* The CHOICE is empty?! */
381 erval.encoded = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000382 _ASN_ENCODED_OK(erval);
Lev Walkinf15320b2004-06-03 03:38:44 +0000383 }
Lev Walkinac589332005-08-22 14:19:28 +0000384 _ASN_ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000385 }
386
387 /*
388 * Seek over the present member of the structure.
389 */
Lev Walkin449f8322004-08-20 13:23:42 +0000390 elm = &td->elements[present-1];
Lev Walkincc93b0f2004-09-10 09:18:20 +0000391 if(elm->flags & ATF_POINTER) {
Lev Walkinac589332005-08-22 14:19:28 +0000392 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000393 if(memb_ptr == 0) {
Lev Walkincc93b0f2004-09-10 09:18:20 +0000394 if(elm->optional) {
395 erval.encoded = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000396 _ASN_ENCODED_OK(erval);
Lev Walkincc93b0f2004-09-10 09:18:20 +0000397 }
Lev Walkinac589332005-08-22 14:19:28 +0000398 /* Mandatory element absent */
399 _ASN_ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000400 }
401 } else {
Lev Walkinac589332005-08-22 14:19:28 +0000402 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000403 }
404
405 /*
406 * If the CHOICE itself is tagged EXPLICIT:
407 * T ::= [2] EXPLICIT CHOICE { ... }
408 * Then emit the appropriate tags.
409 */
Lev Walkin449f8322004-08-20 13:23:42 +0000410 if(tag_mode == 1 || td->tags_count) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000411 /*
412 * For this, we need to pre-compute the member.
413 */
414 ssize_t ret;
415
416 /* Encode member with its tag */
417 erval = elm->type->der_encoder(elm->type, memb_ptr,
418 elm->tag_mode, elm->tag, 0, 0);
419 if(erval.encoded == -1)
420 return erval;
421
422 /* Encode CHOICE with parent or my own tag */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000423 ret = der_write_tags(td, erval.encoded, tag_mode, 1, tag,
Lev Walkinf15320b2004-06-03 03:38:44 +0000424 cb, app_key);
Lev Walkinac589332005-08-22 14:19:28 +0000425 if(ret == -1)
426 _ASN_ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000427 computed_size += ret;
428 }
429
430 /*
431 * Encode the single underlying member.
432 */
433 erval = elm->type->der_encoder(elm->type, memb_ptr,
434 elm->tag_mode, elm->tag, cb, app_key);
435 if(erval.encoded == -1)
436 return erval;
437
438 ASN_DEBUG("Encoded CHOICE member in %ld bytes (+%ld)",
439 (long)erval.encoded, (long)computed_size);
440
441 erval.encoded += computed_size;
442
443 return erval;
444}
445
446ber_tlv_tag_t
Lev Walkin5e033762004-09-29 13:26:15 +0000447CHOICE_outmost_tag(asn_TYPE_descriptor_t *td, const void *ptr, int tag_mode, ber_tlv_tag_t tag) {
448 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
Lev Walkinf15320b2004-06-03 03:38:44 +0000449 int present;
450
Lev Walkin32788732005-11-13 09:12:23 +0000451 assert(tag_mode == 0); (void)tag_mode;
452 assert(tag == 0); (void)tag;
Lev Walkinf15320b2004-06-03 03:38:44 +0000453
454 /*
455 * Figure out which CHOICE element is encoded.
456 */
457 present = _fetch_present_idx(ptr, specs->pres_offset, specs->pres_size);
458
Lev Walkin449f8322004-08-20 13:23:42 +0000459 if(present > 0 || present <= td->elements_count) {
Lev Walkin5e033762004-09-29 13:26:15 +0000460 asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkin70e70282004-06-05 01:45:48 +0000461 const void *memb_ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000462
Lev Walkincc93b0f2004-09-10 09:18:20 +0000463 if(elm->flags & ATF_POINTER) {
Lev Walkin70e70282004-06-05 01:45:48 +0000464 memb_ptr = *(const void * const *)
465 ((const char *)ptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000466 } else {
Lev Walkin70e70282004-06-05 01:45:48 +0000467 memb_ptr = (const void *)
468 ((const char *)ptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000469 }
470
Lev Walkin5e033762004-09-29 13:26:15 +0000471 return asn_TYPE_outmost_tag(elm->type, memb_ptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000472 elm->tag_mode, elm->tag);
473 } else {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000474 return (ber_tlv_tag_t)-1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000475 }
476}
477
478int
Lev Walkin5e033762004-09-29 13:26:15 +0000479CHOICE_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +0000480 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkin5e033762004-09-29 13:26:15 +0000481 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
Lev Walkinf15320b2004-06-03 03:38:44 +0000482 int present;
483
484 if(!sptr) {
Lev Walkin1eded352006-07-13 11:19:01 +0000485 _ASN_CTFAIL(app_key, td,
Lev Walkin16835b62004-08-22 13:47:59 +0000486 "%s: value not given (%s:%d)",
487 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000488 return -1;
489 }
490
491 /*
492 * Figure out which CHOICE element is encoded.
493 */
494 present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
Lev Walkin449f8322004-08-20 13:23:42 +0000495 if(present > 0 && present <= td->elements_count) {
Lev Walkin5e033762004-09-29 13:26:15 +0000496 asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkinf15320b2004-06-03 03:38:44 +0000497 const void *memb_ptr;
498
Lev Walkincc93b0f2004-09-10 09:18:20 +0000499 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000500 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkincc93b0f2004-09-10 09:18:20 +0000501 if(!memb_ptr) {
502 if(elm->optional)
503 return 0;
Lev Walkin1eded352006-07-13 11:19:01 +0000504 _ASN_CTFAIL(app_key, td,
Lev Walkincc93b0f2004-09-10 09:18:20 +0000505 "%s: mandatory CHOICE element %s absent (%s:%d)",
506 td->name, elm->name, __FILE__, __LINE__);
507 return -1;
508 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000509 } else {
510 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
511 }
512
Lev Walkin449f8322004-08-20 13:23:42 +0000513 if(elm->memb_constraints) {
514 return elm->memb_constraints(elm->type, memb_ptr,
Lev Walkin1eded352006-07-13 11:19:01 +0000515 ctfailcb, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +0000516 } else {
517 int ret = elm->type->check_constraints(elm->type,
Lev Walkin1eded352006-07-13 11:19:01 +0000518 memb_ptr, ctfailcb, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +0000519 /*
520 * Cannot inherit it eralier:
521 * need to make sure we get the updated version.
522 */
523 elm->memb_constraints = elm->type->check_constraints;
524 return ret;
525 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000526 } else {
Lev Walkin1eded352006-07-13 11:19:01 +0000527 _ASN_CTFAIL(app_key, td,
Lev Walkind34a8512004-08-22 13:55:49 +0000528 "%s: no CHOICE element given (%s:%d)",
Lev Walkin16835b62004-08-22 13:47:59 +0000529 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000530 return -1;
531 }
532}
533
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000534#undef XER_ADVANCE
535#define XER_ADVANCE(num_bytes) do { \
536 size_t num = num_bytes; \
Lev Walkin8c3b8542005-03-10 18:52:02 +0000537 buf_ptr = ((const char *)buf_ptr) + num;\
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000538 size -= num; \
539 consumed_myself += num; \
540 } while(0)
541
Lev Walkinc61f3862005-02-14 17:21:22 +0000542/*
543 * Decode the XER (XML) data.
544 */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000545asn_dec_rval_t
546CHOICE_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
547 void **struct_ptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000548 const void *buf_ptr, size_t size) {
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000549 /*
550 * Bring closer parts of structure description.
551 */
552 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
553 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
554
555 /*
556 * Parts of the structure being constructed.
557 */
558 void *st = *struct_ptr; /* Target structure. */
559 asn_struct_ctx_t *ctx; /* Decoder context */
560
Lev Walkinc61f3862005-02-14 17:21:22 +0000561 asn_dec_rval_t rval; /* Return value of a decoder */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000562 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000563 int edx; /* Element index */
564
565 /*
566 * Create the target structure if it is not present already.
567 */
568 if(st == 0) {
569 st = *struct_ptr = CALLOC(1, specs->struct_size);
570 if(st == 0) RETURN(RC_FAIL);
571 }
572
573 /*
574 * Restore parsing context.
575 */
576 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkind1bfea62005-11-08 03:06:16 +0000577 if(ctx->phase == 0 && !*xml_tag)
578 ctx->phase = 1; /* Skip the outer tag checking phase */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000579
580 /*
581 * Phases of XER/XML processing:
582 * Phase 0: Check that the opening tag matches our expectations.
583 * Phase 1: Processing body and reacting on closing tag.
584 * Phase 2: Processing inner type.
Lev Walkinc34dc462005-02-18 16:23:48 +0000585 * Phase 3: Only waiting for closing tag.
586 * Phase 4: Skipping unknown extensions.
587 * Phase 5: PHASED OUT
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000588 */
Lev Walkinc34dc462005-02-18 16:23:48 +0000589 for(edx = ctx->step; ctx->phase <= 4;) {
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000590 pxer_chunk_type_e ch_type; /* XER chunk type */
591 ssize_t ch_size; /* Chunk size */
592 xer_check_tag_e tcv; /* Tag check value */
593 asn_TYPE_member_t *elm;
594
595 /*
596 * Go inside the member.
597 */
598 if(ctx->phase == 2) {
599 asn_dec_rval_t tmprval;
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000600 void *memb_ptr; /* Pointer to the member */
601 void **memb_ptr2; /* Pointer to that pointer */
602
Lev Walkinabf68892004-10-26 10:12:14 +0000603 elm = &td->elements[edx];
604
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000605 if(elm->flags & ATF_POINTER) {
606 /* Member is a pointer to another structure */
607 memb_ptr2 = (void **)((char *)st
608 + elm->memb_offset);
609 } else {
610 memb_ptr = (char *)st + elm->memb_offset;
611 memb_ptr2 = &memb_ptr;
612 }
613
Lev Walkinc61f3862005-02-14 17:21:22 +0000614 /* Start/Continue decoding the inner member */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000615 tmprval = elm->type->xer_decoder(opt_codec_ctx,
616 elm->type, memb_ptr2, elm->name,
617 buf_ptr, size);
618 XER_ADVANCE(tmprval.consumed);
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000619 ASN_DEBUG("XER/CHOICE: itdf: code=%d", tmprval.code);
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000620 if(tmprval.code != RC_OK)
621 RETURN(tmprval.code);
Lev Walkinc61f3862005-02-14 17:21:22 +0000622 assert(_fetch_present_idx(st,
623 specs->pres_offset, specs->pres_size) == 0);
624 /* Record what we've got */
625 _set_present_idx(st,
626 specs->pres_offset, specs->pres_size, edx + 1);
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000627 ctx->phase = 3;
628 /* Fall through */
629 }
630
Lev Walkind1bfea62005-11-08 03:06:16 +0000631 /* No need to wait for closing tag; special mode. */
632 if(ctx->phase == 3 && !*xml_tag) {
633 ctx->phase = 5; /* Phase out */
634 RETURN(RC_OK);
635 }
636
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000637 /*
638 * Get the next part of the XML stream.
639 */
Lev Walkin1e443962005-02-18 18:06:36 +0000640 ch_size = xer_next_token(&ctx->context, buf_ptr, size, &ch_type);
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000641 switch(ch_size) {
642 case -1: RETURN(RC_FAIL);
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000643 case 0: RETURN(RC_WMORE);
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000644 default:
645 switch(ch_type) {
646 case PXER_COMMENT: /* Got XML comment */
647 case PXER_TEXT: /* Ignore free-standing text */
648 XER_ADVANCE(ch_size); /* Skip silently */
649 continue;
650 case PXER_TAG:
651 break; /* Check the rest down there */
652 }
653 }
654
Lev Walkinc61f3862005-02-14 17:21:22 +0000655 tcv = xer_check_tag(buf_ptr, ch_size, xml_tag);
Lev Walkind1bfea62005-11-08 03:06:16 +0000656 ASN_DEBUG("XER/CHOICE checked [%c%c%c%c] vs [%s], tcv=%d",
Lev Walkin32788732005-11-13 09:12:23 +0000657 ch_size>0?((const uint8_t *)buf_ptr)[0]:'?',
658 ch_size>1?((const uint8_t *)buf_ptr)[1]:'?',
659 ch_size>2?((const uint8_t *)buf_ptr)[2]:'?',
660 ch_size>3?((const uint8_t *)buf_ptr)[3]:'?',
Lev Walkind1bfea62005-11-08 03:06:16 +0000661 xml_tag, tcv);
Lev Walkinc34dc462005-02-18 16:23:48 +0000662
663 /* Skip the extensions section */
664 if(ctx->phase == 4) {
665 ASN_DEBUG("skip_unknown(%d, %ld)",
666 tcv, (long)ctx->left);
667 switch(xer_skip_unknown(tcv, &ctx->left)) {
668 case -1:
669 ctx->phase = 5;
670 RETURN(RC_FAIL);
671 continue;
672 case 1:
673 ctx->phase = 3;
Lev Walkin1e443962005-02-18 18:06:36 +0000674 /* Fall through */
Lev Walkinc34dc462005-02-18 16:23:48 +0000675 case 0:
676 XER_ADVANCE(ch_size);
677 continue;
678 case 2:
679 ctx->phase = 3;
680 break;
681 }
682 }
683
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000684 switch(tcv) {
685 case XCT_BOTH:
Lev Walkinc61f3862005-02-14 17:21:22 +0000686 break; /* No CHOICE? */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000687 case XCT_CLOSING:
Lev Walkinc61f3862005-02-14 17:21:22 +0000688 if(ctx->phase != 3)
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000689 break;
690 XER_ADVANCE(ch_size);
Lev Walkinc34dc462005-02-18 16:23:48 +0000691 ctx->phase = 5; /* Phase out */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000692 RETURN(RC_OK);
693 case XCT_OPENING:
694 if(ctx->phase == 0) {
695 XER_ADVANCE(ch_size);
696 ctx->phase = 1; /* Processing body phase */
697 continue;
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000698 }
699 /* Fall through */
Lev Walkin904e65b2005-02-18 14:23:48 +0000700 case XCT_UNKNOWN_OP:
701 case XCT_UNKNOWN_BO:
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000702
703 if(ctx->phase != 1)
704 break; /* Really unexpected */
705
706 /*
Lev Walkinc61f3862005-02-14 17:21:22 +0000707 * Search which inner member corresponds to this tag.
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000708 */
709 for(edx = 0; edx < td->elements_count; edx++) {
710 elm = &td->elements[edx];
Lev Walkinc61f3862005-02-14 17:21:22 +0000711 tcv = xer_check_tag(buf_ptr,ch_size,elm->name);
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000712 switch(tcv) {
713 case XCT_BOTH:
714 case XCT_OPENING:
715 /*
716 * Process this member.
717 */
718 ctx->step = edx;
719 ctx->phase = 2;
720 break;
Lev Walkin904e65b2005-02-18 14:23:48 +0000721 case XCT_UNKNOWN_OP:
722 case XCT_UNKNOWN_BO:
Lev Walkin6c0df202005-02-14 19:03:17 +0000723 continue;
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000724 default:
725 edx = td->elements_count;
726 break; /* Phase out */
727 }
728 break;
729 }
Lev Walkin6c0df202005-02-14 19:03:17 +0000730 if(edx != td->elements_count)
731 continue;
Lev Walkin904e65b2005-02-18 14:23:48 +0000732
733 /* It is expected extension */
Lev Walkin59b176e2005-11-26 11:25:14 +0000734 if(specs->ext_start != -1) {
Lev Walkinc34dc462005-02-18 16:23:48 +0000735 ASN_DEBUG("Got anticipated extension");
Lev Walkin904e65b2005-02-18 14:23:48 +0000736 /*
Lev Walkinc34dc462005-02-18 16:23:48 +0000737 * Check for (XCT_BOTH or XCT_UNKNOWN_BO)
738 * By using a mask. Only record a pure
739 * <opening> tags.
Lev Walkin904e65b2005-02-18 14:23:48 +0000740 */
Lev Walkinc34dc462005-02-18 16:23:48 +0000741 if(tcv & XCT_CLOSING) {
742 /* Found </extension> without body */
743 ctx->phase = 3; /* Terminating */
744 } else {
745 ctx->left = 1;
746 ctx->phase = 4; /* Skip ...'s */
747 }
748 XER_ADVANCE(ch_size);
749 continue;
Lev Walkin904e65b2005-02-18 14:23:48 +0000750 }
751
Lev Walkin6c0df202005-02-14 19:03:17 +0000752 /* Fall through */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000753 default:
754 break;
755 }
756
Lev Walkind1bfea62005-11-08 03:06:16 +0000757 ASN_DEBUG("Unexpected XML tag in CHOICE (ph=%d, tag=%s)",
758 ctx->phase, xml_tag);
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000759 break;
760 }
761
Lev Walkinc34dc462005-02-18 16:23:48 +0000762 ctx->phase = 5; /* Phase out, just in case */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000763 RETURN(RC_FAIL);
764}
765
766
Lev Walkina9cc46e2004-09-22 16:06:28 +0000767asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000768CHOICE_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000769 int ilevel, enum xer_encoder_flags_e flags,
770 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin5e033762004-09-29 13:26:15 +0000771 asn_CHOICE_specifics_t *specs=(asn_CHOICE_specifics_t *)td->specifics;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000772 asn_enc_rval_t er;
773 int present;
774
775 if(!sptr)
776 _ASN_ENCODE_FAILED;
777
778 /*
779 * Figure out which CHOICE element is encoded.
780 */
781 present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
782
783 if(present <= 0 || present > td->elements_count) {
784 _ASN_ENCODE_FAILED;
785 } else {
786 asn_enc_rval_t tmper;
Lev Walkin5e033762004-09-29 13:26:15 +0000787 asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkina9cc46e2004-09-22 16:06:28 +0000788 void *memb_ptr;
789 const char *mname = elm->name;
790 unsigned int mlen = strlen(mname);
791
792 if(elm->flags & ATF_POINTER) {
793 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
794 if(!memb_ptr) _ASN_ENCODE_FAILED;
795 } else {
796 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
797 }
798
799 er.encoded = 0;
800
801 if(!(flags & XER_F_CANONICAL)) _i_ASN_TEXT_INDENT(1, ilevel);
802 _ASN_CALLBACK3("<", 1, mname, mlen, ">", 1);
803
804 tmper = elm->type->xer_encoder(elm->type, memb_ptr,
805 ilevel + 1, flags, cb, app_key);
806 if(tmper.encoded == -1) return tmper;
807
808 _ASN_CALLBACK3("</", 2, mname, mlen, ">", 1);
809
810 er.encoded += 5 + (2 * mlen) + tmper.encoded;
811 }
812
813 if(!(flags & XER_F_CANONICAL)) _i_ASN_TEXT_INDENT(1, ilevel - 1);
814
Lev Walkin59b176e2005-11-26 11:25:14 +0000815 _ASN_ENCODED_OK(er);
Lev Walkin942fd082004-10-03 09:13:02 +0000816cb_failed:
817 _ASN_ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000818}
819
Lev Walkin59b176e2005-11-26 11:25:14 +0000820asn_dec_rval_t
821CHOICE_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
822 asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
823 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
824 asn_dec_rval_t rv;
825 asn_per_constraint_t *ct;
826 asn_TYPE_member_t *elm; /* CHOICE's element */
827 void *memb_ptr;
828 void **memb_ptr2;
829 void *st = *sptr;
830 int value;
831
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000832 if(_ASN_STACK_OVERFLOW_CHECK(opt_codec_ctx))
833 _ASN_DECODE_FAILED;
834
Lev Walkin59b176e2005-11-26 11:25:14 +0000835 /*
836 * Create the target structure if it is not present already.
837 */
838 if(!st) {
839 st = *sptr = CALLOC(1, specs->struct_size);
840 if(!st) _ASN_DECODE_FAILED;
841 }
842
843 if(constraints) ct = &constraints->value;
844 else if(td->per_constraints) ct = &td->per_constraints->value;
845 else ct = 0;
846
847 if(ct && ct->flags & APC_EXTENSIBLE) {
848 value = per_get_few_bits(pd, 1);
849 if(value < 0) _ASN_DECODE_FAILED;
850 if(value) ct = 0; /* Not restricted */
851 }
852
853 if(ct && ct->range_bits >= 0) {
854 value = per_get_few_bits(pd, ct->range_bits);
855 if(value < 0) _ASN_DECODE_FAILED;
856 if(value > ct->upper_bound)
857 _ASN_DECODE_FAILED;
858 ASN_DEBUG("CHOICE %s got index %d in range %d",
859 td->name, value, ct->range_bits);
860 } else {
861 if(specs->ext_start == -1)
862 _ASN_DECODE_FAILED;
863 value = uper_get_nsnnwn(pd);
864 if(value < 0) _ASN_DECODE_FAILED;
865 value += specs->ext_start;
866 if(value >= td->elements_count)
867 _ASN_DECODE_FAILED;
868 ASN_DEBUG("NOT IMPLEMENTED YET");
869 _ASN_DECODE_FAILED;
870 }
871
872 /* Adjust if canonical order is different from natural order */
873 if(specs->canonical_order)
874 value = specs->canonical_order[value];
875
876 /* Set presence to be able to free it later */
877 _set_present_idx(st, specs->pres_offset, specs->pres_size, value + 1);
878
879 elm = &td->elements[value];
880 if(elm->flags & ATF_POINTER) {
881 /* Member is a pointer to another structure */
882 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
883 } else {
884 memb_ptr = (char *)st + elm->memb_offset;
885 memb_ptr2 = &memb_ptr;
886 }
887 ASN_DEBUG("Discovered CHOICE %s encodes %s", td->name, elm->name);
888
889 rv = elm->type->uper_decoder(opt_codec_ctx, elm->type,
890 elm->per_constraints, memb_ptr2, pd);
891 if(rv.code != RC_OK)
892 ASN_DEBUG("Failed to decode %s in %s (CHOICE)",
893 elm->name, td->name);
894 return rv;
895}
896
Lev Walkin523de9e2006-08-18 01:34:18 +0000897asn_enc_rval_t
898CHOICE_encode_uper(asn_TYPE_descriptor_t *td,
899 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
900 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
901 asn_TYPE_member_t *elm; /* CHOICE's element */
902 asn_per_constraint_t *ct;
903 void *memb_ptr;
904 int present;
905
906 if(!sptr) _ASN_ENCODE_FAILED;
907
908 ASN_DEBUG("Encoding %s as CHOICE", td->name);
909
910 if(constraints) ct = &constraints->value;
911 else if(td->per_constraints) ct = &td->per_constraints->value;
912 else ct = 0;
913
914 present = _fetch_present_idx(sptr,
915 specs->pres_offset, specs->pres_size);
916
917 /*
918 * If the structure was not initialized properly, it cannot be encoded:
919 * can't deduce what to encode in the choice type.
920 */
921 if(present <= 0 || present > td->elements_count)
922 _ASN_ENCODE_FAILED;
923 else
924 present--;
925
926 /* Adjust if canonical order is different from natural order */
927 if(specs->canonical_order)
928 present = specs->canonical_order[present];
929
930 ASN_DEBUG("Encoding %s CHOICE element %d", td->name, present);
931
932 if(ct && ct->range_bits >= 0) {
933 if(present < ct->lower_bound
934 || present > ct->upper_bound) {
935 if(ct->flags & APC_EXTENSIBLE) {
936 if(per_put_few_bits(po, 1, 1))
937 _ASN_ENCODE_FAILED;
938 } else {
939 _ASN_ENCODE_FAILED;
940 }
941 ct = 0;
942 }
943 }
944 if(ct && ct->flags & APC_EXTENSIBLE)
945 if(per_put_few_bits(po, 0, 1))
946 _ASN_ENCODE_FAILED;
947
948 if(ct && ct->range_bits >= 0) {
949 if(per_put_few_bits(po, present, ct->range_bits))
950 _ASN_ENCODE_FAILED;
951 } else {
952 if(specs->ext_start == -1)
953 _ASN_ENCODE_FAILED;
954 if(uper_put_nsnnwn(po, present - specs->ext_start))
955 _ASN_ENCODE_FAILED;
956 ASN_DEBUG("NOT IMPLEMENTED YET");
957 _ASN_ENCODE_FAILED;
958 }
959
960 elm = &td->elements[present];
961 if(elm->flags & ATF_POINTER) {
962 /* Member is a pointer to another structure */
963 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
964 if(!memb_ptr) _ASN_ENCODE_FAILED;
965 } else {
966 memb_ptr = (char *)sptr + elm->memb_offset;
967 }
968
969 return elm->type->uper_encoder(elm->type, elm->per_constraints,
970 memb_ptr, po);
971}
972
Lev Walkin59b176e2005-11-26 11:25:14 +0000973
Lev Walkinf15320b2004-06-03 03:38:44 +0000974int
Lev Walkin5e033762004-09-29 13:26:15 +0000975CHOICE_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000976 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin5e033762004-09-29 13:26:15 +0000977 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
Lev Walkinf15320b2004-06-03 03:38:44 +0000978 int present;
979
Lev Walkin8e8078a2004-09-26 13:10:40 +0000980 if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000981
982 /*
983 * Figure out which CHOICE element is encoded.
984 */
985 present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
986
987 /*
Lev Walkina9cc46e2004-09-22 16:06:28 +0000988 * Print that element.
Lev Walkinf15320b2004-06-03 03:38:44 +0000989 */
Lev Walkin449f8322004-08-20 13:23:42 +0000990 if(present > 0 && present <= td->elements_count) {
Lev Walkin5e033762004-09-29 13:26:15 +0000991 asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkinf15320b2004-06-03 03:38:44 +0000992 const void *memb_ptr;
993
Lev Walkincc93b0f2004-09-10 09:18:20 +0000994 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000995 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkin8e8078a2004-09-26 13:10:40 +0000996 if(!memb_ptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000997 } else {
998 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
999 }
1000
1001 /* Print member's name and stuff */
Lev Walkincc6a9102004-09-23 22:06:26 +00001002 if(0) {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001003 if(cb(elm->name, strlen(elm->name), app_key) < 0
1004 || cb(": ", 2, app_key) < 0)
Lev Walkincc6a9102004-09-23 22:06:26 +00001005 return -1;
1006 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001007
1008 return elm->type->print_struct(elm->type, memb_ptr, ilevel,
1009 cb, app_key);
1010 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001011 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001012 }
1013}
1014
1015void
Lev Walkin5e033762004-09-29 13:26:15 +00001016CHOICE_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
1017 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
Lev Walkinf15320b2004-06-03 03:38:44 +00001018 int present;
1019
1020 if(!td || !ptr)
1021 return;
1022
1023 ASN_DEBUG("Freeing %s as CHOICE", td->name);
1024
1025 /*
1026 * Figure out which CHOICE element is encoded.
1027 */
1028 present = _fetch_present_idx(ptr, specs->pres_offset, specs->pres_size);
1029
1030 /*
1031 * Free that element.
1032 */
Lev Walkin449f8322004-08-20 13:23:42 +00001033 if(present > 0 && present <= td->elements_count) {
Lev Walkin5e033762004-09-29 13:26:15 +00001034 asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkinf15320b2004-06-03 03:38:44 +00001035 void *memb_ptr;
1036
Lev Walkincc93b0f2004-09-10 09:18:20 +00001037 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +00001038 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
1039 if(memb_ptr)
Lev Walkinadcb5862006-03-17 02:11:12 +00001040 ASN_STRUCT_FREE(*elm->type, memb_ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +00001041 } else {
1042 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
Lev Walkinadcb5862006-03-17 02:11:12 +00001043 ASN_STRUCT_FREE_CONTENTS_ONLY(*elm->type, memb_ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +00001044 }
1045 }
1046
1047 if(!contents_only) {
1048 FREEMEM(ptr);
1049 }
1050}
1051
1052
1053/*
1054 * The following functions functions offer protection against -fshort-enums,
1055 * compatible with little- and big-endian machines.
1056 * If assertion is triggered, either disable -fshort-enums, or add an entry
1057 * here with the ->pres_size of your target stracture.
1058 * Unless the target structure is packed, the ".present" member
1059 * is guaranteed to be aligned properly. ASN.1 compiler itself does not
1060 * produce packed code.
1061 */
Lev Walkin91f5cd02004-08-11 09:10:59 +00001062static int
Lev Walkinf15320b2004-06-03 03:38:44 +00001063_fetch_present_idx(const void *struct_ptr, int pres_offset, int pres_size) {
1064 const void *present_ptr;
1065 int present;
1066
1067 present_ptr = ((const char *)struct_ptr) + pres_offset;
1068
1069 switch(pres_size) {
1070 case sizeof(int): present = *(const int *)present_ptr; break;
1071 case sizeof(short): present = *(const short *)present_ptr; break;
1072 case sizeof(char): present = *(const char *)present_ptr; break;
1073 default:
1074 /* ANSI C mandates enum to be equivalent to integer */
1075 assert(pres_size != sizeof(int));
1076 return 0; /* If not aborted, pass back safe value */
1077 }
1078
1079 return present;
1080}
1081
Lev Walkin91f5cd02004-08-11 09:10:59 +00001082static void
Lev Walkinf15320b2004-06-03 03:38:44 +00001083_set_present_idx(void *struct_ptr, int pres_offset, int pres_size, int present) {
1084 void *present_ptr;
1085 present_ptr = ((char *)struct_ptr) + pres_offset;
1086
1087 switch(pres_size) {
1088 case sizeof(int): *(int *)present_ptr = present; break;
1089 case sizeof(short): *(short *)present_ptr = present; break;
1090 case sizeof(char): *(char *)present_ptr = present; break;
1091 default:
1092 /* ANSI C mandates enum to be equivalent to integer */
1093 assert(pres_size != sizeof(int));
1094 }
1095}