blob: 6116e6a6b0b47d989300d409c63a4241c0d10ff4 [file] [log] [blame]
Lev Walkin523de9e2006-08-18 01:34:18 +00001/*
Lev Walkinac6db372007-06-26 10:14:11 +00002 * Copyright (c) 2003, 2004, 2005, 2006, 2007 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 Walkin6f9060f2007-06-29 01:45:03 +00008#include <per_opentype.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00009
10/*
11 * Number of bytes left for this structure.
12 * (ctx->left) indicates the number of bytes _transferred_ for the structure.
13 * (size) contains the number of bytes in the buffer passed.
14 */
Lev Walkinec1ffd42004-08-18 04:53:32 +000015#define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left)
Lev Walkinf15320b2004-06-03 03:38:44 +000016
17/*
18 * If the subprocessor function returns with an indication that it wants
19 * more data, it may well be a fatal decoding problem, because the
20 * size is constrained by the <TLV>'s L, even if the buffer size allows
21 * reading more data.
22 * For example, consider the buffer containing the following TLVs:
23 * <T:5><L:1><V> <T:6>...
24 * The TLV length clearly indicates that one byte is expected in V, but
25 * if the V processor returns with "want more data" even if the buffer
26 * contains way more data than the V processor have seen.
27 */
Lev Walkind9bd7752004-06-05 08:17:50 +000028#define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size)
Lev Walkinf15320b2004-06-03 03:38:44 +000029
30/*
31 * This macro "eats" the part of the buffer which is definitely "consumed",
32 * i.e. was correctly converted into local representation or rightfully skipped.
33 */
Lev Walkincc6a9102004-09-23 22:06:26 +000034#undef ADVANCE
Lev Walkinf15320b2004-06-03 03:38:44 +000035#define ADVANCE(num_bytes) do { \
36 size_t num = num_bytes; \
Lev Walkinaa61a0f2014-01-13 23:08:47 -080037 ptr = ((const char *)ptr) + num;\
Lev Walkinf15320b2004-06-03 03:38:44 +000038 size -= num; \
39 if(ctx->left >= 0) \
40 ctx->left -= num; \
41 consumed_myself += num; \
42 } while(0)
43
44/*
45 * Switch to the next phase of parsing.
46 */
Lev Walkincc6a9102004-09-23 22:06:26 +000047#undef NEXT_PHASE
Lev Walkinf15320b2004-06-03 03:38:44 +000048#define NEXT_PHASE(ctx) do { \
49 ctx->phase++; \
50 ctx->step = 0; \
51 } while(0)
52
53/*
54 * Return a standardized complex structure.
55 */
Lev Walkincc6a9102004-09-23 22:06:26 +000056#undef RETURN
Lev Walkinf15320b2004-06-03 03:38:44 +000057#define RETURN(_code) do { \
58 rval.code = _code; \
59 rval.consumed = consumed_myself;\
60 return rval; \
61 } while(0)
62
63/*
64 * See the definitions.
65 */
Lev Walkin91f5cd02004-08-11 09:10:59 +000066static int _fetch_present_idx(const void *struct_ptr, int off, int size);
67static void _set_present_idx(void *sptr, int offset, int size, int pres);
Lev Walkinf15320b2004-06-03 03:38:44 +000068
69/*
70 * Tags are canonically sorted in the tag to member table.
71 */
72static int
73_search4tag(const void *ap, const void *bp) {
Lev Walkin5e033762004-09-29 13:26:15 +000074 const asn_TYPE_tag2member_t *a = (const asn_TYPE_tag2member_t *)ap;
75 const asn_TYPE_tag2member_t *b = (const asn_TYPE_tag2member_t *)bp;
Lev Walkinc2346572004-08-11 09:07:36 +000076
Lev Walkinf15320b2004-06-03 03:38:44 +000077 int a_class = BER_TAG_CLASS(a->el_tag);
78 int b_class = BER_TAG_CLASS(b->el_tag);
79
80 if(a_class == b_class) {
81 ber_tlv_tag_t a_value = BER_TAG_VALUE(a->el_tag);
82 ber_tlv_tag_t b_value = BER_TAG_VALUE(b->el_tag);
83
84 if(a_value == b_value)
85 return 0;
86 else if(a_value < b_value)
87 return -1;
88 else
89 return 1;
90 } else if(a_class < b_class) {
91 return -1;
92 } else {
93 return 1;
94 }
95}
96
97/*
98 * The decoder of the CHOICE type.
99 */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000100asn_dec_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000101CHOICE_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000102 void **struct_ptr, const void *ptr, size_t size, int tag_mode) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000103 /*
104 * Bring closer parts of structure description.
105 */
Lev Walkin5e033762004-09-29 13:26:15 +0000106 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
107 asn_TYPE_member_t *elements = td->elements;
Lev Walkinf15320b2004-06-03 03:38:44 +0000108
109 /*
110 * Parts of the structure being constructed.
111 */
112 void *st = *struct_ptr; /* Target structure. */
Lev Walkin5e033762004-09-29 13:26:15 +0000113 asn_struct_ctx_t *ctx; /* Decoder context */
Lev Walkinf15320b2004-06-03 03:38:44 +0000114
115 ber_tlv_tag_t tlv_tag; /* T from TLV */
116 ssize_t tag_len; /* Length of TLV's T */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000117 asn_dec_rval_t rval; /* Return code from subparsers */
Lev Walkinf15320b2004-06-03 03:38:44 +0000118
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 Walkin59b176e2005-11-26 11:25:14 +0000122
Lev Walkinf15320b2004-06-03 03:38:44 +0000123 /*
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 Walkinaa61a0f2014-01-13 23:08:47 -0800136 ctx = (asn_struct_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) {
Lev Walkin5e033762004-09-29 13:26:15 +0000150 rval = ber_check_tags(opt_codec_ctx, 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 Walkinf0f04d12004-10-05 06:36:02 +0000155 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000156 }
157
158 if(ctx->left >= 0) {
159 /* ?Substracted below! */
160 ctx->left += rval.consumed;
161 }
162 ADVANCE(rval.consumed);
163 } else {
164 ctx->left = -1;
165 }
166
167 NEXT_PHASE(ctx);
168
169 ASN_DEBUG("Structure consumes %ld bytes, buffer %ld",
170 (long)ctx->left, (long)size);
171
172 /* Fall through */
173 case 1:
174 /*
175 * Fetch the T from TLV.
176 */
177 tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
Lev Walkin449f8322004-08-20 13:23:42 +0000178 ASN_DEBUG("In %s CHOICE tag length %d", td->name, (int)tag_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000179 switch(tag_len) {
180 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
181 /* Fall through */
182 case -1: RETURN(RC_FAIL);
183 }
184
185 do {
Wim Lewisfb6344e2014-07-28 12:16:01 -0700186 const asn_TYPE_tag2member_t *t2m;
Lev Walkin5e033762004-09-29 13:26:15 +0000187 asn_TYPE_tag2member_t key;
Lev Walkinf15320b2004-06-03 03:38:44 +0000188
189 key.el_tag = tlv_tag;
Wim Lewisfb6344e2014-07-28 12:16:01 -0700190 t2m = (const asn_TYPE_tag2member_t *)bsearch(&key,
Lev Walkinc2346572004-08-11 09:07:36 +0000191 specs->tag2el, specs->tag2el_count,
Lev Walkinf15320b2004-06-03 03:38:44 +0000192 sizeof(specs->tag2el[0]), _search4tag);
193 if(t2m) {
194 /*
195 * Found the element corresponding to the tag.
196 */
197 NEXT_PHASE(ctx);
198 ctx->step = t2m->el_no;
199 break;
Lev Walkin59b176e2005-11-26 11:25:14 +0000200 } else if(specs->ext_start == -1) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000201 ASN_DEBUG("Unexpected tag %s "
202 "in non-extensible CHOICE %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000203 ber_tlv_tag_string(tlv_tag), td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000204 RETURN(RC_FAIL);
205 } else {
206 /* Skip this tag */
207 ssize_t skip;
208
209 ASN_DEBUG("Skipping unknown tag %s",
210 ber_tlv_tag_string(tlv_tag));
211
Lev Walkinbaaa24f2004-09-29 14:19:14 +0000212 skip = ber_skip_length(opt_codec_ctx,
Lev Walkinf15320b2004-06-03 03:38:44 +0000213 BER_TLV_CONSTRUCTED(ptr),
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800214 (const char *)ptr + tag_len,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000215 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 Walkin5e033762004-09-29 13:26:15 +0000234 asn_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 Walkinaa61a0f2014-01-13 23:08:47 -0800247 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 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000256 /* Set presence to be able to free it properly at any time */
257 _set_present_idx(st, specs->pres_offset,
258 specs->pres_size, ctx->step + 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000259 /*
260 * Invoke the member fetch routine according to member's type
261 */
Lev Walkin5e033762004-09-29 13:26:15 +0000262 rval = elm->type->ber_decoder(opt_codec_ctx, elm->type,
Lev Walkin59b176e2005-11-26 11:25:14 +0000263 memb_ptr2, ptr, LEFT, elm->tag_mode);
Lev Walkinf15320b2004-06-03 03:38:44 +0000264 switch(rval.code) {
265 case RC_OK:
Lev Walkinf15320b2004-06-03 03:38:44 +0000266 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 */
Lev Walkin8c3b8542005-03-10 18:52:02 +0000324 if(((const uint8_t *)ptr)[0] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000325 if(LEFT < 2) {
326 if(SIZE_VIOLATION)
327 RETURN(RC_FAIL);
328 else
329 RETURN(RC_WMORE);
Lev Walkin8c3b8542005-03-10 18:52:02 +0000330 } else if(((const uint8_t *)ptr)[1] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000331 /*
332 * Correctly finished with <0><0>.
333 */
Lev Walkinf0f04d12004-10-05 06:36:02 +0000334 ADVANCE(2);
335 ctx->left++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000336 continue;
337 }
338 } else {
339 ASN_DEBUG("Unexpected continuation in %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000340 td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000341 RETURN(RC_FAIL);
342 }
343
Lev Walkinf0f04d12004-10-05 06:36:02 +0000344 /* UNREACHABLE */
Lev Walkinf15320b2004-06-03 03:38:44 +0000345 }
346
347 NEXT_PHASE(ctx);
348 case 4:
349 /* No meaningful work here */
350 break;
351 }
352
353 RETURN(RC_OK);
354}
355
Lev Walkina9cc46e2004-09-22 16:06:28 +0000356asn_enc_rval_t
Lev Walkinac589332005-08-22 14:19:28 +0000357CHOICE_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000358 int tag_mode, ber_tlv_tag_t tag,
359 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin5e033762004-09-29 13:26:15 +0000360 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
361 asn_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
Lev Walkin7c1dc052016-03-14 03:08:15 -0700367 if(!sptr) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000368
Lev Walkinf15320b2004-06-03 03:38:44 +0000369 ASN_DEBUG("%s %s as CHOICE",
Lev Walkin449f8322004-08-20 13:23:42 +0000370 cb?"Encoding":"Estimating", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000371
Lev Walkinac589332005-08-22 14:19:28 +0000372 present = _fetch_present_idx(sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000373 specs->pres_offset, specs->pres_size);
374
375 /*
376 * If the structure was not initialized, it cannot be encoded:
377 * can't deduce what to encode in the choice type.
378 */
Lev Walkin449f8322004-08-20 13:23:42 +0000379 if(present <= 0 || present > td->elements_count) {
380 if(present == 0 && td->elements_count == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000381 /* The CHOICE is empty?! */
382 erval.encoded = 0;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700383 ASN__ENCODED_OK(erval);
Lev Walkinf15320b2004-06-03 03:38:44 +0000384 }
Lev Walkin7c1dc052016-03-14 03:08:15 -0700385 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000386 }
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 Walkinaa61a0f2014-01-13 23:08:47 -0800393 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000394 if(memb_ptr == 0) {
Lev Walkincc93b0f2004-09-10 09:18:20 +0000395 if(elm->optional) {
396 erval.encoded = 0;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700397 ASN__ENCODED_OK(erval);
Lev Walkincc93b0f2004-09-10 09:18:20 +0000398 }
Lev Walkinac589332005-08-22 14:19:28 +0000399 /* Mandatory element absent */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700400 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000401 }
402 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800403 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000404 }
405
406 /*
407 * If the CHOICE itself is tagged EXPLICIT:
408 * T ::= [2] EXPLICIT CHOICE { ... }
409 * Then emit the appropriate tags.
410 */
Lev Walkin449f8322004-08-20 13:23:42 +0000411 if(tag_mode == 1 || td->tags_count) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000412 /*
413 * For this, we need to pre-compute the member.
414 */
415 ssize_t ret;
416
417 /* Encode member with its tag */
418 erval = elm->type->der_encoder(elm->type, memb_ptr,
419 elm->tag_mode, elm->tag, 0, 0);
420 if(erval.encoded == -1)
421 return erval;
422
423 /* Encode CHOICE with parent or my own tag */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000424 ret = der_write_tags(td, erval.encoded, tag_mode, 1, tag,
Lev Walkinf15320b2004-06-03 03:38:44 +0000425 cb, app_key);
Lev Walkinac589332005-08-22 14:19:28 +0000426 if(ret == -1)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700427 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000428 computed_size += ret;
429 }
430
431 /*
432 * Encode the single underlying member.
433 */
434 erval = elm->type->der_encoder(elm->type, memb_ptr,
435 elm->tag_mode, elm->tag, cb, app_key);
436 if(erval.encoded == -1)
437 return erval;
438
439 ASN_DEBUG("Encoded CHOICE member in %ld bytes (+%ld)",
440 (long)erval.encoded, (long)computed_size);
441
442 erval.encoded += computed_size;
443
444 return erval;
445}
446
447ber_tlv_tag_t
Wim Lewis14e6b162014-07-23 16:06:01 -0700448CHOICE_outmost_tag(const asn_TYPE_descriptor_t *td, const void *ptr, int tag_mode, ber_tlv_tag_t tag) {
Lev Walkin5e033762004-09-29 13:26:15 +0000449 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
Lev Walkinf15320b2004-06-03 03:38:44 +0000450 int present;
451
Lev Walkin32788732005-11-13 09:12:23 +0000452 assert(tag_mode == 0); (void)tag_mode;
453 assert(tag == 0); (void)tag;
Lev Walkinf15320b2004-06-03 03:38:44 +0000454
455 /*
456 * Figure out which CHOICE element is encoded.
457 */
458 present = _fetch_present_idx(ptr, specs->pres_offset, specs->pres_size);
459
Lev Walkin449f8322004-08-20 13:23:42 +0000460 if(present > 0 || present <= td->elements_count) {
Wim Lewis14e6b162014-07-23 16:06:01 -0700461 const asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkin70e70282004-06-05 01:45:48 +0000462 const void *memb_ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000463
Lev Walkincc93b0f2004-09-10 09:18:20 +0000464 if(elm->flags & ATF_POINTER) {
Lev Walkin70e70282004-06-05 01:45:48 +0000465 memb_ptr = *(const void * const *)
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800466 ((const char *)ptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000467 } else {
Lev Walkin70e70282004-06-05 01:45:48 +0000468 memb_ptr = (const void *)
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800469 ((const char *)ptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000470 }
471
Lev Walkin5e033762004-09-29 13:26:15 +0000472 return asn_TYPE_outmost_tag(elm->type, memb_ptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000473 elm->tag_mode, elm->tag);
474 } else {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000475 return (ber_tlv_tag_t)-1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000476 }
477}
478
479int
Lev Walkin5e033762004-09-29 13:26:15 +0000480CHOICE_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +0000481 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkin5e033762004-09-29 13:26:15 +0000482 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
Lev Walkinf15320b2004-06-03 03:38:44 +0000483 int present;
484
485 if(!sptr) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700486 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +0000487 "%s: value not given (%s:%d)",
488 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000489 return -1;
490 }
491
492 /*
493 * Figure out which CHOICE element is encoded.
494 */
495 present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
Lev Walkin449f8322004-08-20 13:23:42 +0000496 if(present > 0 && present <= td->elements_count) {
Lev Walkin5e033762004-09-29 13:26:15 +0000497 asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkinf15320b2004-06-03 03:38:44 +0000498 const void *memb_ptr;
499
Lev Walkincc93b0f2004-09-10 09:18:20 +0000500 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800501 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkincc93b0f2004-09-10 09:18:20 +0000502 if(!memb_ptr) {
503 if(elm->optional)
504 return 0;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700505 ASN__CTFAIL(app_key, td, sptr,
Lev Walkincc93b0f2004-09-10 09:18:20 +0000506 "%s: mandatory CHOICE element %s absent (%s:%d)",
507 td->name, elm->name, __FILE__, __LINE__);
508 return -1;
509 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000510 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800511 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000512 }
513
Lev Walkin449f8322004-08-20 13:23:42 +0000514 if(elm->memb_constraints) {
515 return elm->memb_constraints(elm->type, memb_ptr,
Lev Walkin1eded352006-07-13 11:19:01 +0000516 ctfailcb, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +0000517 } else {
518 int ret = elm->type->check_constraints(elm->type,
Lev Walkin1eded352006-07-13 11:19:01 +0000519 memb_ptr, ctfailcb, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +0000520 /*
521 * Cannot inherit it eralier:
522 * need to make sure we get the updated version.
523 */
524 elm->memb_constraints = elm->type->check_constraints;
525 return ret;
526 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000527 } else {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700528 ASN__CTFAIL(app_key, td, sptr,
Lev Walkind34a8512004-08-22 13:55:49 +0000529 "%s: no CHOICE element given (%s:%d)",
Lev Walkin16835b62004-08-22 13:47:59 +0000530 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000531 return -1;
532 }
533}
534
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000535#undef XER_ADVANCE
536#define XER_ADVANCE(num_bytes) do { \
537 size_t num = num_bytes; \
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800538 buf_ptr = (const void *)(((const char *)buf_ptr) + num); \
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000539 size -= num; \
540 consumed_myself += num; \
541 } while(0)
542
Lev Walkinc61f3862005-02-14 17:21:22 +0000543/*
544 * Decode the XER (XML) data.
545 */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000546asn_dec_rval_t
547CHOICE_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
548 void **struct_ptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000549 const void *buf_ptr, size_t size) {
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000550 /*
551 * Bring closer parts of structure description.
552 */
553 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
554 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
555
556 /*
557 * Parts of the structure being constructed.
558 */
559 void *st = *struct_ptr; /* Target structure. */
560 asn_struct_ctx_t *ctx; /* Decoder context */
561
Lev Walkinc61f3862005-02-14 17:21:22 +0000562 asn_dec_rval_t rval; /* Return value of a decoder */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000563 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000564 int edx; /* Element index */
565
566 /*
567 * Create the target structure if it is not present already.
568 */
569 if(st == 0) {
570 st = *struct_ptr = CALLOC(1, specs->struct_size);
571 if(st == 0) RETURN(RC_FAIL);
572 }
573
574 /*
575 * Restore parsing context.
576 */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800577 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkind1bfea62005-11-08 03:06:16 +0000578 if(ctx->phase == 0 && !*xml_tag)
579 ctx->phase = 1; /* Skip the outer tag checking phase */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000580
581 /*
582 * Phases of XER/XML processing:
583 * Phase 0: Check that the opening tag matches our expectations.
584 * Phase 1: Processing body and reacting on closing tag.
585 * Phase 2: Processing inner type.
Lev Walkinc34dc462005-02-18 16:23:48 +0000586 * Phase 3: Only waiting for closing tag.
587 * Phase 4: Skipping unknown extensions.
588 * Phase 5: PHASED OUT
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000589 */
Lev Walkinc34dc462005-02-18 16:23:48 +0000590 for(edx = ctx->step; ctx->phase <= 4;) {
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000591 pxer_chunk_type_e ch_type; /* XER chunk type */
592 ssize_t ch_size; /* Chunk size */
593 xer_check_tag_e tcv; /* Tag check value */
594 asn_TYPE_member_t *elm;
595
596 /*
597 * Go inside the member.
598 */
599 if(ctx->phase == 2) {
600 asn_dec_rval_t tmprval;
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000601 void *memb_ptr; /* Pointer to the member */
602 void **memb_ptr2; /* Pointer to that pointer */
603
Lev Walkinabf68892004-10-26 10:12:14 +0000604 elm = &td->elements[edx];
605
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000606 if(elm->flags & ATF_POINTER) {
607 /* Member is a pointer to another structure */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800608 memb_ptr2 = (void **)((char *)st
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000609 + elm->memb_offset);
610 } else {
611 memb_ptr = (char *)st + elm->memb_offset;
612 memb_ptr2 = &memb_ptr;
613 }
614
Lev Walkinc61f3862005-02-14 17:21:22 +0000615 /* Start/Continue decoding the inner member */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000616 tmprval = elm->type->xer_decoder(opt_codec_ctx,
617 elm->type, memb_ptr2, elm->name,
618 buf_ptr, size);
619 XER_ADVANCE(tmprval.consumed);
Lev Walkin866c8802006-09-17 08:23:35 +0000620 ASN_DEBUG("XER/CHOICE: itdf: [%s] code=%d",
621 elm->type->name, tmprval.code);
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000622 if(tmprval.code != RC_OK)
623 RETURN(tmprval.code);
Lev Walkinc61f3862005-02-14 17:21:22 +0000624 assert(_fetch_present_idx(st,
625 specs->pres_offset, specs->pres_size) == 0);
626 /* Record what we've got */
627 _set_present_idx(st,
628 specs->pres_offset, specs->pres_size, edx + 1);
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000629 ctx->phase = 3;
630 /* Fall through */
631 }
632
Lev Walkind1bfea62005-11-08 03:06:16 +0000633 /* No need to wait for closing tag; special mode. */
634 if(ctx->phase == 3 && !*xml_tag) {
635 ctx->phase = 5; /* Phase out */
636 RETURN(RC_OK);
637 }
638
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000639 /*
640 * Get the next part of the XML stream.
641 */
Lev Walkin1e443962005-02-18 18:06:36 +0000642 ch_size = xer_next_token(&ctx->context, buf_ptr, size, &ch_type);
Lev Walkin97363482016-01-24 19:23:02 -0800643 if(ch_size == -1) {
644 RETURN(RC_FAIL);
645 } else {
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000646 switch(ch_type) {
Lev Walkin97363482016-01-24 19:23:02 -0800647 case PXER_WMORE:
648 RETURN(RC_WMORE);
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000649 case PXER_COMMENT: /* Got XML comment */
650 case PXER_TEXT: /* Ignore free-standing text */
651 XER_ADVANCE(ch_size); /* Skip silently */
652 continue;
653 case PXER_TAG:
654 break; /* Check the rest down there */
655 }
656 }
657
Lev Walkinc61f3862005-02-14 17:21:22 +0000658 tcv = xer_check_tag(buf_ptr, ch_size, xml_tag);
Lev Walkind1bfea62005-11-08 03:06:16 +0000659 ASN_DEBUG("XER/CHOICE checked [%c%c%c%c] vs [%s], tcv=%d",
Lev Walkin32788732005-11-13 09:12:23 +0000660 ch_size>0?((const uint8_t *)buf_ptr)[0]:'?',
661 ch_size>1?((const uint8_t *)buf_ptr)[1]:'?',
662 ch_size>2?((const uint8_t *)buf_ptr)[2]:'?',
663 ch_size>3?((const uint8_t *)buf_ptr)[3]:'?',
Lev Walkind1bfea62005-11-08 03:06:16 +0000664 xml_tag, tcv);
Lev Walkinc34dc462005-02-18 16:23:48 +0000665
666 /* Skip the extensions section */
667 if(ctx->phase == 4) {
668 ASN_DEBUG("skip_unknown(%d, %ld)",
669 tcv, (long)ctx->left);
670 switch(xer_skip_unknown(tcv, &ctx->left)) {
671 case -1:
672 ctx->phase = 5;
673 RETURN(RC_FAIL);
674 continue;
675 case 1:
676 ctx->phase = 3;
Lev Walkin1e443962005-02-18 18:06:36 +0000677 /* Fall through */
Lev Walkinc34dc462005-02-18 16:23:48 +0000678 case 0:
679 XER_ADVANCE(ch_size);
680 continue;
681 case 2:
682 ctx->phase = 3;
683 break;
684 }
685 }
686
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000687 switch(tcv) {
688 case XCT_BOTH:
Lev Walkinc61f3862005-02-14 17:21:22 +0000689 break; /* No CHOICE? */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000690 case XCT_CLOSING:
Lev Walkinc61f3862005-02-14 17:21:22 +0000691 if(ctx->phase != 3)
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000692 break;
693 XER_ADVANCE(ch_size);
Lev Walkinc34dc462005-02-18 16:23:48 +0000694 ctx->phase = 5; /* Phase out */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000695 RETURN(RC_OK);
696 case XCT_OPENING:
697 if(ctx->phase == 0) {
698 XER_ADVANCE(ch_size);
699 ctx->phase = 1; /* Processing body phase */
700 continue;
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000701 }
702 /* Fall through */
Lev Walkin904e65b2005-02-18 14:23:48 +0000703 case XCT_UNKNOWN_OP:
704 case XCT_UNKNOWN_BO:
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000705
706 if(ctx->phase != 1)
707 break; /* Really unexpected */
708
709 /*
Lev Walkinc61f3862005-02-14 17:21:22 +0000710 * Search which inner member corresponds to this tag.
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000711 */
712 for(edx = 0; edx < td->elements_count; edx++) {
713 elm = &td->elements[edx];
Lev Walkinc61f3862005-02-14 17:21:22 +0000714 tcv = xer_check_tag(buf_ptr,ch_size,elm->name);
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000715 switch(tcv) {
716 case XCT_BOTH:
717 case XCT_OPENING:
718 /*
719 * Process this member.
720 */
721 ctx->step = edx;
722 ctx->phase = 2;
723 break;
Lev Walkin904e65b2005-02-18 14:23:48 +0000724 case XCT_UNKNOWN_OP:
725 case XCT_UNKNOWN_BO:
Lev Walkin6c0df202005-02-14 19:03:17 +0000726 continue;
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000727 default:
728 edx = td->elements_count;
729 break; /* Phase out */
730 }
731 break;
732 }
Lev Walkin6c0df202005-02-14 19:03:17 +0000733 if(edx != td->elements_count)
734 continue;
Lev Walkin904e65b2005-02-18 14:23:48 +0000735
736 /* It is expected extension */
Lev Walkin59b176e2005-11-26 11:25:14 +0000737 if(specs->ext_start != -1) {
Lev Walkinc34dc462005-02-18 16:23:48 +0000738 ASN_DEBUG("Got anticipated extension");
Lev Walkin904e65b2005-02-18 14:23:48 +0000739 /*
Lev Walkinc34dc462005-02-18 16:23:48 +0000740 * Check for (XCT_BOTH or XCT_UNKNOWN_BO)
741 * By using a mask. Only record a pure
742 * <opening> tags.
Lev Walkin904e65b2005-02-18 14:23:48 +0000743 */
Lev Walkinc34dc462005-02-18 16:23:48 +0000744 if(tcv & XCT_CLOSING) {
745 /* Found </extension> without body */
746 ctx->phase = 3; /* Terminating */
747 } else {
748 ctx->left = 1;
749 ctx->phase = 4; /* Skip ...'s */
750 }
751 XER_ADVANCE(ch_size);
752 continue;
Lev Walkin904e65b2005-02-18 14:23:48 +0000753 }
754
Lev Walkin6c0df202005-02-14 19:03:17 +0000755 /* Fall through */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000756 default:
757 break;
758 }
759
Lev Walkin866c8802006-09-17 08:23:35 +0000760 ASN_DEBUG("Unexpected XML tag [%c%c%c%c] in CHOICE [%s]"
761 " (ph=%d, tag=%s)",
762 ch_size>0?((const uint8_t *)buf_ptr)[0]:'?',
763 ch_size>1?((const uint8_t *)buf_ptr)[1]:'?',
764 ch_size>2?((const uint8_t *)buf_ptr)[2]:'?',
765 ch_size>3?((const uint8_t *)buf_ptr)[3]:'?',
766 td->name, ctx->phase, xml_tag);
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000767 break;
768 }
769
Lev Walkinc34dc462005-02-18 16:23:48 +0000770 ctx->phase = 5; /* Phase out, just in case */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000771 RETURN(RC_FAIL);
772}
773
774
Lev Walkina9cc46e2004-09-22 16:06:28 +0000775asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000776CHOICE_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000777 int ilevel, enum xer_encoder_flags_e flags,
778 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin5e033762004-09-29 13:26:15 +0000779 asn_CHOICE_specifics_t *specs=(asn_CHOICE_specifics_t *)td->specifics;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000780 asn_enc_rval_t er;
781 int present;
782
783 if(!sptr)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700784 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000785
786 /*
787 * Figure out which CHOICE element is encoded.
788 */
789 present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
790
791 if(present <= 0 || present > td->elements_count) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700792 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000793 } else {
794 asn_enc_rval_t tmper;
Lev Walkin5e033762004-09-29 13:26:15 +0000795 asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkina9cc46e2004-09-22 16:06:28 +0000796 void *memb_ptr;
797 const char *mname = elm->name;
798 unsigned int mlen = strlen(mname);
799
800 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800801 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700802 if(!memb_ptr) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000803 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800804 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000805 }
806
807 er.encoded = 0;
808
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700809 if(!(flags & XER_F_CANONICAL)) ASN__TEXT_INDENT(1, ilevel);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700810 ASN__CALLBACK3("<", 1, mname, mlen, ">", 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000811
812 tmper = elm->type->xer_encoder(elm->type, memb_ptr,
813 ilevel + 1, flags, cb, app_key);
814 if(tmper.encoded == -1) return tmper;
815
Lev Walkin7c1dc052016-03-14 03:08:15 -0700816 ASN__CALLBACK3("</", 2, mname, mlen, ">", 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000817
818 er.encoded += 5 + (2 * mlen) + tmper.encoded;
819 }
820
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700821 if(!(flags & XER_F_CANONICAL)) ASN__TEXT_INDENT(1, ilevel - 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000822
Lev Walkin7c1dc052016-03-14 03:08:15 -0700823 ASN__ENCODED_OK(er);
Lev Walkin942fd082004-10-03 09:13:02 +0000824cb_failed:
Lev Walkin7c1dc052016-03-14 03:08:15 -0700825 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000826}
827
Lev Walkin59b176e2005-11-26 11:25:14 +0000828asn_dec_rval_t
829CHOICE_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
830 asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
831 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
832 asn_dec_rval_t rv;
833 asn_per_constraint_t *ct;
834 asn_TYPE_member_t *elm; /* CHOICE's element */
835 void *memb_ptr;
836 void **memb_ptr2;
837 void *st = *sptr;
838 int value;
839
Lev Walkin7c1dc052016-03-14 03:08:15 -0700840 if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx))
841 ASN__DECODE_FAILED;
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000842
Lev Walkin59b176e2005-11-26 11:25:14 +0000843 /*
844 * Create the target structure if it is not present already.
845 */
846 if(!st) {
847 st = *sptr = CALLOC(1, specs->struct_size);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700848 if(!st) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000849 }
850
851 if(constraints) ct = &constraints->value;
852 else if(td->per_constraints) ct = &td->per_constraints->value;
853 else ct = 0;
854
855 if(ct && ct->flags & APC_EXTENSIBLE) {
856 value = per_get_few_bits(pd, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700857 if(value < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000858 if(value) ct = 0; /* Not restricted */
859 }
860
861 if(ct && ct->range_bits >= 0) {
862 value = per_get_few_bits(pd, ct->range_bits);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700863 if(value < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000864 ASN_DEBUG("CHOICE %s got index %d in range %d",
865 td->name, value, ct->range_bits);
Lev Walkin0a8aa602006-09-18 20:05:55 +0000866 if(value > ct->upper_bound)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700867 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000868 } else {
869 if(specs->ext_start == -1)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700870 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000871 value = uper_get_nsnnwn(pd);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700872 if(value < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000873 value += specs->ext_start;
874 if(value >= td->elements_count)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700875 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000876 }
877
878 /* Adjust if canonical order is different from natural order */
879 if(specs->canonical_order)
880 value = specs->canonical_order[value];
881
882 /* Set presence to be able to free it later */
883 _set_present_idx(st, specs->pres_offset, specs->pres_size, value + 1);
884
885 elm = &td->elements[value];
886 if(elm->flags & ATF_POINTER) {
887 /* Member is a pointer to another structure */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800888 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
Lev Walkin59b176e2005-11-26 11:25:14 +0000889 } else {
890 memb_ptr = (char *)st + elm->memb_offset;
891 memb_ptr2 = &memb_ptr;
892 }
893 ASN_DEBUG("Discovered CHOICE %s encodes %s", td->name, elm->name);
894
Lev Walkin6f9060f2007-06-29 01:45:03 +0000895 if(ct && ct->range_bits >= 0) {
896 rv = elm->type->uper_decoder(opt_codec_ctx, elm->type,
Lev Walkin59b176e2005-11-26 11:25:14 +0000897 elm->per_constraints, memb_ptr2, pd);
Lev Walkin6f9060f2007-06-29 01:45:03 +0000898 } else {
899 rv = uper_open_type_get(opt_codec_ctx, elm->type,
900 elm->per_constraints, memb_ptr2, pd);
901 }
902
Lev Walkin59b176e2005-11-26 11:25:14 +0000903 if(rv.code != RC_OK)
Lev Walkin6f9060f2007-06-29 01:45:03 +0000904 ASN_DEBUG("Failed to decode %s in %s (CHOICE) %d",
905 elm->name, td->name, rv.code);
Lev Walkin59b176e2005-11-26 11:25:14 +0000906 return rv;
907}
908
Lev Walkin523de9e2006-08-18 01:34:18 +0000909asn_enc_rval_t
910CHOICE_encode_uper(asn_TYPE_descriptor_t *td,
911 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
912 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
913 asn_TYPE_member_t *elm; /* CHOICE's element */
914 asn_per_constraint_t *ct;
915 void *memb_ptr;
916 int present;
Lev Walkine14480f2013-03-24 03:28:00 -0700917 int present_enc;
Lev Walkin523de9e2006-08-18 01:34:18 +0000918
Lev Walkin7c1dc052016-03-14 03:08:15 -0700919 if(!sptr) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000920
921 ASN_DEBUG("Encoding %s as CHOICE", td->name);
922
923 if(constraints) ct = &constraints->value;
924 else if(td->per_constraints) ct = &td->per_constraints->value;
925 else ct = 0;
926
927 present = _fetch_present_idx(sptr,
928 specs->pres_offset, specs->pres_size);
929
930 /*
931 * If the structure was not initialized properly, it cannot be encoded:
932 * can't deduce what to encode in the choice type.
933 */
934 if(present <= 0 || present > td->elements_count)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700935 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000936 else
937 present--;
938
Lev Walkin523de9e2006-08-18 01:34:18 +0000939 ASN_DEBUG("Encoding %s CHOICE element %d", td->name, present);
940
Lev Walkine14480f2013-03-24 03:28:00 -0700941 /* Adjust if canonical order is different from natural order */
942 if(specs->canonical_order)
943 present_enc = specs->canonical_order[present];
944 else
945 present_enc = present;
946
Lev Walkin523de9e2006-08-18 01:34:18 +0000947 if(ct && ct->range_bits >= 0) {
Lev Walkine14480f2013-03-24 03:28:00 -0700948 if(present_enc < ct->lower_bound
949 || present_enc > ct->upper_bound) {
Lev Walkin523de9e2006-08-18 01:34:18 +0000950 if(ct->flags & APC_EXTENSIBLE) {
951 if(per_put_few_bits(po, 1, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700952 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000953 } else {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700954 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000955 }
956 ct = 0;
957 }
958 }
959 if(ct && ct->flags & APC_EXTENSIBLE)
960 if(per_put_few_bits(po, 0, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700961 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000962
Lev Walkin523de9e2006-08-18 01:34:18 +0000963 elm = &td->elements[present];
964 if(elm->flags & ATF_POINTER) {
965 /* Member is a pointer to another structure */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800966 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700967 if(!memb_ptr) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000968 } else {
969 memb_ptr = (char *)sptr + elm->memb_offset;
970 }
971
Lev Walkin6f9060f2007-06-29 01:45:03 +0000972 if(ct && ct->range_bits >= 0) {
Lev Walkine14480f2013-03-24 03:28:00 -0700973 if(per_put_few_bits(po, present_enc, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700974 ASN__ENCODE_FAILED;
Lev Walkin6f9060f2007-06-29 01:45:03 +0000975
976 return elm->type->uper_encoder(elm->type, elm->per_constraints,
Lev Walkin523de9e2006-08-18 01:34:18 +0000977 memb_ptr, po);
Lev Walkin6f9060f2007-06-29 01:45:03 +0000978 } else {
979 asn_enc_rval_t rval;
980 if(specs->ext_start == -1)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700981 ASN__ENCODE_FAILED;
Lev Walkine14480f2013-03-24 03:28:00 -0700982 if(uper_put_nsnnwn(po, present_enc - specs->ext_start))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700983 ASN__ENCODE_FAILED;
Lev Walkin6f9060f2007-06-29 01:45:03 +0000984 if(uper_open_type_put(elm->type, elm->per_constraints,
985 memb_ptr, po))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700986 ASN__ENCODE_FAILED;
Lev Walkin6f9060f2007-06-29 01:45:03 +0000987 rval.encoded = 0;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700988 ASN__ENCODED_OK(rval);
Lev Walkin6f9060f2007-06-29 01:45:03 +0000989 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000990}
991
Lev Walkin59b176e2005-11-26 11:25:14 +0000992
Lev Walkinf15320b2004-06-03 03:38:44 +0000993int
Lev Walkin5e033762004-09-29 13:26:15 +0000994CHOICE_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000995 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin5e033762004-09-29 13:26:15 +0000996 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
Lev Walkinf15320b2004-06-03 03:38:44 +0000997 int present;
998
Lev Walkin8e8078a2004-09-26 13:10:40 +0000999 if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001000
1001 /*
1002 * Figure out which CHOICE element is encoded.
1003 */
1004 present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
1005
1006 /*
Lev Walkina9cc46e2004-09-22 16:06:28 +00001007 * Print that element.
Lev Walkinf15320b2004-06-03 03:38:44 +00001008 */
Lev Walkin449f8322004-08-20 13:23:42 +00001009 if(present > 0 && present <= td->elements_count) {
Lev Walkin5e033762004-09-29 13:26:15 +00001010 asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkinf15320b2004-06-03 03:38:44 +00001011 const void *memb_ptr;
1012
Lev Walkincc93b0f2004-09-10 09:18:20 +00001013 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001014 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkin8e8078a2004-09-26 13:10:40 +00001015 if(!memb_ptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001016 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001017 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +00001018 }
1019
1020 /* Print member's name and stuff */
Lev Walkincc6a9102004-09-23 22:06:26 +00001021 if(0) {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001022 if(cb(elm->name, strlen(elm->name), app_key) < 0
1023 || cb(": ", 2, app_key) < 0)
Lev Walkincc6a9102004-09-23 22:06:26 +00001024 return -1;
1025 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001026
1027 return elm->type->print_struct(elm->type, memb_ptr, ilevel,
1028 cb, app_key);
1029 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001030 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001031 }
1032}
1033
1034void
Lev Walkin5e033762004-09-29 13:26:15 +00001035CHOICE_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
1036 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
Lev Walkinf15320b2004-06-03 03:38:44 +00001037 int present;
1038
1039 if(!td || !ptr)
1040 return;
1041
1042 ASN_DEBUG("Freeing %s as CHOICE", td->name);
1043
1044 /*
1045 * Figure out which CHOICE element is encoded.
1046 */
1047 present = _fetch_present_idx(ptr, specs->pres_offset, specs->pres_size);
1048
1049 /*
1050 * Free that element.
1051 */
Lev Walkin449f8322004-08-20 13:23:42 +00001052 if(present > 0 && present <= td->elements_count) {
Lev Walkin5e033762004-09-29 13:26:15 +00001053 asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkinf15320b2004-06-03 03:38:44 +00001054 void *memb_ptr;
1055
Lev Walkincc93b0f2004-09-10 09:18:20 +00001056 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001057 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +00001058 if(memb_ptr)
Lev Walkinadcb5862006-03-17 02:11:12 +00001059 ASN_STRUCT_FREE(*elm->type, memb_ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +00001060 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001061 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
Lev Walkinadcb5862006-03-17 02:11:12 +00001062 ASN_STRUCT_FREE_CONTENTS_ONLY(*elm->type, memb_ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +00001063 }
1064 }
1065
1066 if(!contents_only) {
1067 FREEMEM(ptr);
1068 }
1069}
1070
1071
1072/*
1073 * The following functions functions offer protection against -fshort-enums,
1074 * compatible with little- and big-endian machines.
1075 * If assertion is triggered, either disable -fshort-enums, or add an entry
1076 * here with the ->pres_size of your target stracture.
1077 * Unless the target structure is packed, the ".present" member
1078 * is guaranteed to be aligned properly. ASN.1 compiler itself does not
1079 * produce packed code.
1080 */
Lev Walkin91f5cd02004-08-11 09:10:59 +00001081static int
Lev Walkinf15320b2004-06-03 03:38:44 +00001082_fetch_present_idx(const void *struct_ptr, int pres_offset, int pres_size) {
1083 const void *present_ptr;
1084 int present;
1085
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001086 present_ptr = ((const char *)struct_ptr) + pres_offset;
Lev Walkinf15320b2004-06-03 03:38:44 +00001087
1088 switch(pres_size) {
1089 case sizeof(int): present = *(const int *)present_ptr; break;
1090 case sizeof(short): present = *(const short *)present_ptr; break;
1091 case sizeof(char): present = *(const char *)present_ptr; break;
1092 default:
1093 /* ANSI C mandates enum to be equivalent to integer */
1094 assert(pres_size != sizeof(int));
1095 return 0; /* If not aborted, pass back safe value */
1096 }
1097
1098 return present;
1099}
1100
Lev Walkin91f5cd02004-08-11 09:10:59 +00001101static void
Lev Walkinf15320b2004-06-03 03:38:44 +00001102_set_present_idx(void *struct_ptr, int pres_offset, int pres_size, int present) {
1103 void *present_ptr;
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001104 present_ptr = ((char *)struct_ptr) + pres_offset;
Lev Walkinf15320b2004-06-03 03:38:44 +00001105
1106 switch(pres_size) {
1107 case sizeof(int): *(int *)present_ptr = present; break;
1108 case sizeof(short): *(short *)present_ptr = present; break;
1109 case sizeof(char): *(char *)present_ptr = present; break;
1110 default:
1111 /* ANSI C mandates enum to be equivalent to integer */
1112 assert(pres_size != sizeof(int));
1113 }
1114}