blob: 1e8f8eede7b610ff75893eb8f315f2a35d4fdeef [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 Walkinf6853ce2017-08-11 00:50:27 -070066static unsigned _fetch_present_idx(const void *struct_ptr, unsigned off,
67 unsigned size);
68static void _set_present_idx(void *sptr, unsigned offset, unsigned size,
69 unsigned pres);
Lev Walkincd2f48e2017-08-10 02:14:59 -070070static const void *_get_member_ptr(const asn_TYPE_descriptor_t *,
71 const void *sptr, asn_TYPE_member_t **elm,
Lev Walkin63a35232017-08-10 19:59:47 -070072 unsigned *present);
Lev Walkinf15320b2004-06-03 03:38:44 +000073
74/*
75 * Tags are canonically sorted in the tag to member table.
76 */
77static int
78_search4tag(const void *ap, const void *bp) {
Lev Walkin5e033762004-09-29 13:26:15 +000079 const asn_TYPE_tag2member_t *a = (const asn_TYPE_tag2member_t *)ap;
80 const asn_TYPE_tag2member_t *b = (const asn_TYPE_tag2member_t *)bp;
Lev Walkinc2346572004-08-11 09:07:36 +000081
Lev Walkinf15320b2004-06-03 03:38:44 +000082 int a_class = BER_TAG_CLASS(a->el_tag);
83 int b_class = BER_TAG_CLASS(b->el_tag);
84
85 if(a_class == b_class) {
86 ber_tlv_tag_t a_value = BER_TAG_VALUE(a->el_tag);
87 ber_tlv_tag_t b_value = BER_TAG_VALUE(b->el_tag);
88
89 if(a_value == b_value)
90 return 0;
91 else if(a_value < b_value)
92 return -1;
93 else
94 return 1;
95 } else if(a_class < b_class) {
96 return -1;
97 } else {
98 return 1;
99 }
100}
101
102/*
103 * The decoder of the CHOICE type.
104 */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000105asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -0700106CHOICE_decode_ber(const asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000107 void **struct_ptr, const void *ptr, size_t size, int tag_mode) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000108 /*
109 * Bring closer parts of structure description.
110 */
Lev Walkind84f6032017-10-03 16:33:59 -0700111 const asn_CHOICE_specifics_t *specs =
112 (const asn_CHOICE_specifics_t *)td->specifics;
Lev Walkin5e033762004-09-29 13:26:15 +0000113 asn_TYPE_member_t *elements = td->elements;
Lev Walkinf15320b2004-06-03 03:38:44 +0000114
115 /*
116 * Parts of the structure being constructed.
117 */
118 void *st = *struct_ptr; /* Target structure. */
Lev Walkin5e033762004-09-29 13:26:15 +0000119 asn_struct_ctx_t *ctx; /* Decoder context */
Lev Walkinf15320b2004-06-03 03:38:44 +0000120
121 ber_tlv_tag_t tlv_tag; /* T from TLV */
122 ssize_t tag_len; /* Length of TLV's T */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000123 asn_dec_rval_t rval; /* Return code from subparsers */
Lev Walkinf15320b2004-06-03 03:38:44 +0000124
125 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
126
Lev Walkin449f8322004-08-20 13:23:42 +0000127 ASN_DEBUG("Decoding %s as CHOICE", td->name);
Lev Walkin59b176e2005-11-26 11:25:14 +0000128
Lev Walkinf15320b2004-06-03 03:38:44 +0000129 /*
130 * Create the target structure if it is not present already.
131 */
132 if(st == 0) {
133 st = *struct_ptr = CALLOC(1, specs->struct_size);
134 if(st == 0) {
135 RETURN(RC_FAIL);
136 }
137 }
138
139 /*
140 * Restore parsing context.
141 */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800142 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000143
144 /*
145 * Start to parse where left previously
146 */
147 switch(ctx->phase) {
148 case 0:
149 /*
150 * PHASE 0.
151 * Check that the set of tags associated with given structure
152 * perfectly fits our expectations.
153 */
154
Lev Walkin449f8322004-08-20 13:23:42 +0000155 if(tag_mode || td->tags_count) {
Lev Walkin5e033762004-09-29 13:26:15 +0000156 rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size,
Lev Walkin8e8078a2004-09-26 13:10:40 +0000157 tag_mode, -1, &ctx->left, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000158 if(rval.code != RC_OK) {
159 ASN_DEBUG("%s tagging check failed: %d",
Lev Walkin449f8322004-08-20 13:23:42 +0000160 td->name, rval.code);
Lev Walkinf0f04d12004-10-05 06:36:02 +0000161 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000162 }
163
164 if(ctx->left >= 0) {
165 /* ?Substracted below! */
166 ctx->left += rval.consumed;
167 }
168 ADVANCE(rval.consumed);
169 } else {
170 ctx->left = -1;
171 }
172
173 NEXT_PHASE(ctx);
174
175 ASN_DEBUG("Structure consumes %ld bytes, buffer %ld",
176 (long)ctx->left, (long)size);
177
178 /* Fall through */
179 case 1:
180 /*
181 * Fetch the T from TLV.
182 */
183 tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
Lev Walkin449f8322004-08-20 13:23:42 +0000184 ASN_DEBUG("In %s CHOICE tag length %d", td->name, (int)tag_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000185 switch(tag_len) {
186 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
187 /* Fall through */
188 case -1: RETURN(RC_FAIL);
189 }
190
191 do {
Wim Lewisfb6344e2014-07-28 12:16:01 -0700192 const asn_TYPE_tag2member_t *t2m;
Lev Walkin5e033762004-09-29 13:26:15 +0000193 asn_TYPE_tag2member_t key;
Lev Walkinf15320b2004-06-03 03:38:44 +0000194
195 key.el_tag = tlv_tag;
Wim Lewisfb6344e2014-07-28 12:16:01 -0700196 t2m = (const asn_TYPE_tag2member_t *)bsearch(&key,
Lev Walkinc2346572004-08-11 09:07:36 +0000197 specs->tag2el, specs->tag2el_count,
Lev Walkinf15320b2004-06-03 03:38:44 +0000198 sizeof(specs->tag2el[0]), _search4tag);
199 if(t2m) {
200 /*
201 * Found the element corresponding to the tag.
202 */
203 NEXT_PHASE(ctx);
204 ctx->step = t2m->el_no;
205 break;
Lev Walkin59b176e2005-11-26 11:25:14 +0000206 } else if(specs->ext_start == -1) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000207 ASN_DEBUG("Unexpected tag %s "
208 "in non-extensible CHOICE %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000209 ber_tlv_tag_string(tlv_tag), td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000210 RETURN(RC_FAIL);
211 } else {
212 /* Skip this tag */
213 ssize_t skip;
214
215 ASN_DEBUG("Skipping unknown tag %s",
216 ber_tlv_tag_string(tlv_tag));
217
Lev Walkinbaaa24f2004-09-29 14:19:14 +0000218 skip = ber_skip_length(opt_codec_ctx,
Lev Walkinf15320b2004-06-03 03:38:44 +0000219 BER_TLV_CONSTRUCTED(ptr),
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800220 (const char *)ptr + tag_len,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000221 LEFT - tag_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000222
223 switch(skip) {
224 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
225 /* Fall through */
226 case -1: RETURN(RC_FAIL);
227 }
228
229 ADVANCE(skip + tag_len);
230 RETURN(RC_OK);
231 }
232 } while(0);
233
234 case 2:
235 /*
236 * PHASE 2.
237 * Read in the element.
238 */
239 do {
Lev Walkin5e033762004-09-29 13:26:15 +0000240 asn_TYPE_member_t *elm;/* CHOICE's element */
Lev Walkinf15320b2004-06-03 03:38:44 +0000241 void *memb_ptr; /* Pointer to the member */
Lev Walkinc2346572004-08-11 09:07:36 +0000242 void **memb_ptr2; /* Pointer to that pointer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000243
244 elm = &elements[ctx->step];
245
246 /*
247 * Compute the position of the member inside a structure,
248 * and also a type of containment (it may be contained
249 * as pointer or using inline inclusion).
250 */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000251 if(elm->flags & ATF_POINTER) {
252 /* Member is a pointer to another structure */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800253 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000254 } else {
255 /*
256 * A pointer to a pointer
257 * holding the start of the structure
258 */
259 memb_ptr = (char *)st + elm->memb_offset;
260 memb_ptr2 = &memb_ptr;
261 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000262 /* Set presence to be able to free it properly at any time */
263 _set_present_idx(st, specs->pres_offset,
264 specs->pres_size, ctx->step + 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000265 /*
266 * Invoke the member fetch routine according to member's type
267 */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800268 rval = elm->type->op->ber_decoder(opt_codec_ctx, elm->type,
Lev Walkin59b176e2005-11-26 11:25:14 +0000269 memb_ptr2, ptr, LEFT, elm->tag_mode);
Lev Walkinf15320b2004-06-03 03:38:44 +0000270 switch(rval.code) {
271 case RC_OK:
Lev Walkinf15320b2004-06-03 03:38:44 +0000272 break;
273 case RC_WMORE: /* More data expected */
274 if(!SIZE_VIOLATION) {
275 ADVANCE(rval.consumed);
276 RETURN(RC_WMORE);
277 }
278 RETURN(RC_FAIL);
279 case RC_FAIL: /* Fatal error */
280 RETURN(rval.code);
281 } /* switch(rval) */
282
283 ADVANCE(rval.consumed);
284 } while(0);
285
286 NEXT_PHASE(ctx);
287
288 /* Fall through */
289 case 3:
290 ASN_DEBUG("CHOICE %s Leftover: %ld, size = %ld, tm=%d, tc=%d",
Lev Walkin449f8322004-08-20 13:23:42 +0000291 td->name, (long)ctx->left, (long)size,
292 tag_mode, td->tags_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000293
294 if(ctx->left > 0) {
295 /*
296 * The type must be fully decoded
297 * by the CHOICE member-specific decoder.
298 */
299 RETURN(RC_FAIL);
300 }
301
302 if(ctx->left == -1
Lev Walkin449f8322004-08-20 13:23:42 +0000303 && !(tag_mode || td->tags_count)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000304 /*
305 * This is an untagged CHOICE.
306 * It doesn't contain nothing
307 * except for the member itself, including all its tags.
308 * The decoding is completed.
309 */
310 NEXT_PHASE(ctx);
311 break;
312 }
313
314 /*
315 * Read in the "end of data chunks"'s.
316 */
317 while(ctx->left < 0) {
318 ssize_t tl;
319
320 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
321 switch(tl) {
322 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
323 /* Fall through */
324 case -1: RETURN(RC_FAIL);
325 }
326
327 /*
328 * Expected <0><0>...
329 */
Lev Walkin8c3b8542005-03-10 18:52:02 +0000330 if(((const uint8_t *)ptr)[0] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000331 if(LEFT < 2) {
332 if(SIZE_VIOLATION)
333 RETURN(RC_FAIL);
334 else
335 RETURN(RC_WMORE);
Lev Walkin8c3b8542005-03-10 18:52:02 +0000336 } else if(((const uint8_t *)ptr)[1] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000337 /*
338 * Correctly finished with <0><0>.
339 */
Lev Walkinf0f04d12004-10-05 06:36:02 +0000340 ADVANCE(2);
341 ctx->left++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000342 continue;
343 }
344 } else {
345 ASN_DEBUG("Unexpected continuation in %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000346 td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000347 RETURN(RC_FAIL);
348 }
349
Lev Walkinf0f04d12004-10-05 06:36:02 +0000350 /* UNREACHABLE */
Lev Walkinf15320b2004-06-03 03:38:44 +0000351 }
352
353 NEXT_PHASE(ctx);
354 case 4:
355 /* No meaningful work here */
356 break;
357 }
358
359 RETURN(RC_OK);
360}
361
Lev Walkina9cc46e2004-09-22 16:06:28 +0000362asn_enc_rval_t
Lev Walkinac589332005-08-22 14:19:28 +0000363CHOICE_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000364 int tag_mode, ber_tlv_tag_t tag,
365 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkind84f6032017-10-03 16:33:59 -0700366 const asn_CHOICE_specifics_t *specs = (const asn_CHOICE_specifics_t *)td->specifics;
Lev Walkin5e033762004-09-29 13:26:15 +0000367 asn_TYPE_member_t *elm; /* CHOICE element */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000368 asn_enc_rval_t erval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000369 void *memb_ptr;
370 size_t computed_size = 0;
Lev Walkin63a35232017-08-10 19:59:47 -0700371 unsigned present;
Lev Walkinf15320b2004-06-03 03:38:44 +0000372
Lev Walkin7c1dc052016-03-14 03:08:15 -0700373 if(!sptr) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000374
Lev Walkinf15320b2004-06-03 03:38:44 +0000375 ASN_DEBUG("%s %s as CHOICE",
Lev Walkin449f8322004-08-20 13:23:42 +0000376 cb?"Encoding":"Estimating", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000377
Lev Walkinac589332005-08-22 14:19:28 +0000378 present = _fetch_present_idx(sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000379 specs->pres_offset, specs->pres_size);
380
381 /*
382 * If the structure was not initialized, it cannot be encoded:
383 * can't deduce what to encode in the choice type.
384 */
Lev Walkin63a35232017-08-10 19:59:47 -0700385 if(present == 0 || present > td->elements_count) {
Lev Walkin449f8322004-08-20 13:23:42 +0000386 if(present == 0 && td->elements_count == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000387 /* The CHOICE is empty?! */
388 erval.encoded = 0;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700389 ASN__ENCODED_OK(erval);
Lev Walkinf15320b2004-06-03 03:38:44 +0000390 }
Lev Walkin7c1dc052016-03-14 03:08:15 -0700391 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000392 }
393
394 /*
395 * Seek over the present member of the structure.
396 */
Lev Walkin449f8322004-08-20 13:23:42 +0000397 elm = &td->elements[present-1];
Lev Walkincc93b0f2004-09-10 09:18:20 +0000398 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800399 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000400 if(memb_ptr == 0) {
Lev Walkincc93b0f2004-09-10 09:18:20 +0000401 if(elm->optional) {
402 erval.encoded = 0;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700403 ASN__ENCODED_OK(erval);
Lev Walkincc93b0f2004-09-10 09:18:20 +0000404 }
Lev Walkinac589332005-08-22 14:19:28 +0000405 /* Mandatory element absent */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700406 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000407 }
408 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800409 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000410 }
411
412 /*
413 * If the CHOICE itself is tagged EXPLICIT:
414 * T ::= [2] EXPLICIT CHOICE { ... }
415 * Then emit the appropriate tags.
416 */
Lev Walkin449f8322004-08-20 13:23:42 +0000417 if(tag_mode == 1 || td->tags_count) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000418 /*
419 * For this, we need to pre-compute the member.
420 */
421 ssize_t ret;
422
423 /* Encode member with its tag */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800424 erval = elm->type->op->der_encoder(elm->type, memb_ptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000425 elm->tag_mode, elm->tag, 0, 0);
426 if(erval.encoded == -1)
427 return erval;
428
429 /* Encode CHOICE with parent or my own tag */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000430 ret = der_write_tags(td, erval.encoded, tag_mode, 1, tag,
Lev Walkinf15320b2004-06-03 03:38:44 +0000431 cb, app_key);
Lev Walkinac589332005-08-22 14:19:28 +0000432 if(ret == -1)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700433 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000434 computed_size += ret;
435 }
436
437 /*
438 * Encode the single underlying member.
439 */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800440 erval = elm->type->op->der_encoder(elm->type, memb_ptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000441 elm->tag_mode, elm->tag, cb, app_key);
442 if(erval.encoded == -1)
443 return erval;
444
445 ASN_DEBUG("Encoded CHOICE member in %ld bytes (+%ld)",
446 (long)erval.encoded, (long)computed_size);
447
448 erval.encoded += computed_size;
449
450 return erval;
451}
452
453ber_tlv_tag_t
Wim Lewis14e6b162014-07-23 16:06:01 -0700454CHOICE_outmost_tag(const asn_TYPE_descriptor_t *td, const void *ptr, int tag_mode, ber_tlv_tag_t tag) {
Lev Walkind84f6032017-10-03 16:33:59 -0700455 const asn_CHOICE_specifics_t *specs = (const asn_CHOICE_specifics_t *)td->specifics;
Lev Walkin63a35232017-08-10 19:59:47 -0700456 unsigned present;
Lev Walkinf15320b2004-06-03 03:38:44 +0000457
Lev Walkin32788732005-11-13 09:12:23 +0000458 assert(tag_mode == 0); (void)tag_mode;
459 assert(tag == 0); (void)tag;
Lev Walkinf15320b2004-06-03 03:38:44 +0000460
461 /*
462 * Figure out which CHOICE element is encoded.
463 */
464 present = _fetch_present_idx(ptr, specs->pres_offset, specs->pres_size);
465
Lev Walkin63a35232017-08-10 19:59:47 -0700466 if(present > 0 && present <= td->elements_count) {
Wim Lewis14e6b162014-07-23 16:06:01 -0700467 const asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkin70e70282004-06-05 01:45:48 +0000468 const void *memb_ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000469
Lev Walkincc93b0f2004-09-10 09:18:20 +0000470 if(elm->flags & ATF_POINTER) {
Lev Walkin70e70282004-06-05 01:45:48 +0000471 memb_ptr = *(const void * const *)
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800472 ((const char *)ptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000473 } else {
Lev Walkin70e70282004-06-05 01:45:48 +0000474 memb_ptr = (const void *)
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800475 ((const char *)ptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000476 }
477
Lev Walkin5e033762004-09-29 13:26:15 +0000478 return asn_TYPE_outmost_tag(elm->type, memb_ptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000479 elm->tag_mode, elm->tag);
480 } else {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000481 return (ber_tlv_tag_t)-1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000482 }
483}
484
485int
Lev Walkin5e033762004-09-29 13:26:15 +0000486CHOICE_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +0000487 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkind84f6032017-10-03 16:33:59 -0700488 const asn_CHOICE_specifics_t *specs = (const asn_CHOICE_specifics_t *)td->specifics;
Lev Walkin63a35232017-08-10 19:59:47 -0700489 unsigned present;
Lev Walkinf15320b2004-06-03 03:38:44 +0000490
491 if(!sptr) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700492 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +0000493 "%s: value not given (%s:%d)",
494 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000495 return -1;
496 }
497
498 /*
499 * Figure out which CHOICE element is encoded.
500 */
501 present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
Lev Walkin63a35232017-08-10 19:59:47 -0700502 if(present > 0 && present <= td->elements_count) {
Lev Walkin5e033762004-09-29 13:26:15 +0000503 asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkinf15320b2004-06-03 03:38:44 +0000504 const void *memb_ptr;
505
Lev Walkincc93b0f2004-09-10 09:18:20 +0000506 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800507 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkincc93b0f2004-09-10 09:18:20 +0000508 if(!memb_ptr) {
509 if(elm->optional)
510 return 0;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700511 ASN__CTFAIL(app_key, td, sptr,
Lev Walkincc93b0f2004-09-10 09:18:20 +0000512 "%s: mandatory CHOICE element %s absent (%s:%d)",
513 td->name, elm->name, __FILE__, __LINE__);
514 return -1;
515 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000516 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800517 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000518 }
519
Lev Walkina5972be2017-09-29 23:15:58 -0700520 if(elm->encoding_constraints.general_constraints) {
521 return elm->encoding_constraints.general_constraints(elm->type, memb_ptr,
Lev Walkin1eded352006-07-13 11:19:01 +0000522 ctfailcb, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +0000523 } else {
Lev Walkina5972be2017-09-29 23:15:58 -0700524 return elm->type->encoding_constraints.general_constraints(elm->type,
Lev Walkin1eded352006-07-13 11:19:01 +0000525 memb_ptr, ctfailcb, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +0000526 }
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
Lev Walkinafbf2a92017-09-12 23:30:27 -0700547CHOICE_decode_xer(const asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000548 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 */
Lev Walkind84f6032017-10-03 16:33:59 -0700553 const asn_CHOICE_specifics_t *specs = (const asn_CHOICE_specifics_t *)td->specifics;
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000554 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 Walkin494fb702017-08-07 20:07:00 -0700564 size_t edx; /* Element index */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000565
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 */
Lev Walkindcae9ce2017-09-18 20:13:36 -0700603 unsigned old_present;
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000604
Lev Walkinabf68892004-10-26 10:12:14 +0000605 elm = &td->elements[edx];
606
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000607 if(elm->flags & ATF_POINTER) {
608 /* Member is a pointer to another structure */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800609 memb_ptr2 = (void **)((char *)st
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000610 + elm->memb_offset);
611 } else {
612 memb_ptr = (char *)st + elm->memb_offset;
613 memb_ptr2 = &memb_ptr;
614 }
615
Lev Walkinc61f3862005-02-14 17:21:22 +0000616 /* Start/Continue decoding the inner member */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800617 tmprval = elm->type->op->xer_decoder(opt_codec_ctx,
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000618 elm->type, memb_ptr2, elm->name,
619 buf_ptr, size);
620 XER_ADVANCE(tmprval.consumed);
Lev Walkin866c8802006-09-17 08:23:35 +0000621 ASN_DEBUG("XER/CHOICE: itdf: [%s] code=%d",
622 elm->type->name, tmprval.code);
Lev Walkindcae9ce2017-09-18 20:13:36 -0700623 old_present = _fetch_present_idx(st,
624 specs->pres_offset, specs->pres_size);
625 assert(old_present == 0 || old_present == edx + 1);
Lev Walkinc61f3862005-02-14 17:21:22 +0000626 /* Record what we've got */
627 _set_present_idx(st,
628 specs->pres_offset, specs->pres_size, edx + 1);
Lev Walkindcae9ce2017-09-18 20:13:36 -0700629 if(tmprval.code != RC_OK)
630 RETURN(tmprval.code);
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000631 ctx->phase = 3;
632 /* Fall through */
633 }
634
Lev Walkind1bfea62005-11-08 03:06:16 +0000635 /* No need to wait for closing tag; special mode. */
636 if(ctx->phase == 3 && !*xml_tag) {
637 ctx->phase = 5; /* Phase out */
638 RETURN(RC_OK);
639 }
640
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000641 /*
642 * Get the next part of the XML stream.
643 */
Lev Walkin1e443962005-02-18 18:06:36 +0000644 ch_size = xer_next_token(&ctx->context, buf_ptr, size, &ch_type);
Lev Walkin97363482016-01-24 19:23:02 -0800645 if(ch_size == -1) {
646 RETURN(RC_FAIL);
647 } else {
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000648 switch(ch_type) {
Lev Walkin97363482016-01-24 19:23:02 -0800649 case PXER_WMORE:
650 RETURN(RC_WMORE);
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000651 case PXER_COMMENT: /* Got XML comment */
652 case PXER_TEXT: /* Ignore free-standing text */
653 XER_ADVANCE(ch_size); /* Skip silently */
654 continue;
655 case PXER_TAG:
656 break; /* Check the rest down there */
657 }
658 }
659
Lev Walkinc61f3862005-02-14 17:21:22 +0000660 tcv = xer_check_tag(buf_ptr, ch_size, xml_tag);
Lev Walkind1bfea62005-11-08 03:06:16 +0000661 ASN_DEBUG("XER/CHOICE checked [%c%c%c%c] vs [%s], tcv=%d",
Lev Walkin32788732005-11-13 09:12:23 +0000662 ch_size>0?((const uint8_t *)buf_ptr)[0]:'?',
663 ch_size>1?((const uint8_t *)buf_ptr)[1]:'?',
664 ch_size>2?((const uint8_t *)buf_ptr)[2]:'?',
665 ch_size>3?((const uint8_t *)buf_ptr)[3]:'?',
Lev Walkind1bfea62005-11-08 03:06:16 +0000666 xml_tag, tcv);
Lev Walkinc34dc462005-02-18 16:23:48 +0000667
668 /* Skip the extensions section */
669 if(ctx->phase == 4) {
670 ASN_DEBUG("skip_unknown(%d, %ld)",
671 tcv, (long)ctx->left);
672 switch(xer_skip_unknown(tcv, &ctx->left)) {
673 case -1:
674 ctx->phase = 5;
675 RETURN(RC_FAIL);
Lev Walkinc34dc462005-02-18 16:23:48 +0000676 case 1:
677 ctx->phase = 3;
Lev Walkin1e443962005-02-18 18:06:36 +0000678 /* Fall through */
Lev Walkinc34dc462005-02-18 16:23:48 +0000679 case 0:
680 XER_ADVANCE(ch_size);
681 continue;
682 case 2:
683 ctx->phase = 3;
684 break;
685 }
686 }
687
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000688 switch(tcv) {
689 case XCT_BOTH:
Lev Walkinc61f3862005-02-14 17:21:22 +0000690 break; /* No CHOICE? */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000691 case XCT_CLOSING:
Lev Walkinc61f3862005-02-14 17:21:22 +0000692 if(ctx->phase != 3)
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000693 break;
694 XER_ADVANCE(ch_size);
Lev Walkinc34dc462005-02-18 16:23:48 +0000695 ctx->phase = 5; /* Phase out */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000696 RETURN(RC_OK);
697 case XCT_OPENING:
698 if(ctx->phase == 0) {
699 XER_ADVANCE(ch_size);
700 ctx->phase = 1; /* Processing body phase */
701 continue;
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000702 }
703 /* Fall through */
Lev Walkin904e65b2005-02-18 14:23:48 +0000704 case XCT_UNKNOWN_OP:
705 case XCT_UNKNOWN_BO:
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000706
707 if(ctx->phase != 1)
708 break; /* Really unexpected */
709
710 /*
Lev Walkinc61f3862005-02-14 17:21:22 +0000711 * Search which inner member corresponds to this tag.
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000712 */
713 for(edx = 0; edx < td->elements_count; edx++) {
714 elm = &td->elements[edx];
Lev Walkinc61f3862005-02-14 17:21:22 +0000715 tcv = xer_check_tag(buf_ptr,ch_size,elm->name);
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000716 switch(tcv) {
717 case XCT_BOTH:
718 case XCT_OPENING:
719 /*
720 * Process this member.
721 */
722 ctx->step = edx;
723 ctx->phase = 2;
724 break;
Lev Walkin904e65b2005-02-18 14:23:48 +0000725 case XCT_UNKNOWN_OP:
726 case XCT_UNKNOWN_BO:
Lev Walkin6c0df202005-02-14 19:03:17 +0000727 continue;
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000728 default:
729 edx = td->elements_count;
730 break; /* Phase out */
731 }
732 break;
733 }
Lev Walkin6c0df202005-02-14 19:03:17 +0000734 if(edx != td->elements_count)
735 continue;
Lev Walkin904e65b2005-02-18 14:23:48 +0000736
737 /* It is expected extension */
Lev Walkin59b176e2005-11-26 11:25:14 +0000738 if(specs->ext_start != -1) {
Lev Walkinc34dc462005-02-18 16:23:48 +0000739 ASN_DEBUG("Got anticipated extension");
Lev Walkin904e65b2005-02-18 14:23:48 +0000740 /*
Lev Walkinc34dc462005-02-18 16:23:48 +0000741 * Check for (XCT_BOTH or XCT_UNKNOWN_BO)
742 * By using a mask. Only record a pure
743 * <opening> tags.
Lev Walkin904e65b2005-02-18 14:23:48 +0000744 */
Lev Walkinc34dc462005-02-18 16:23:48 +0000745 if(tcv & XCT_CLOSING) {
746 /* Found </extension> without body */
747 ctx->phase = 3; /* Terminating */
748 } else {
749 ctx->left = 1;
750 ctx->phase = 4; /* Skip ...'s */
751 }
752 XER_ADVANCE(ch_size);
753 continue;
Lev Walkin904e65b2005-02-18 14:23:48 +0000754 }
755
Lev Walkin6c0df202005-02-14 19:03:17 +0000756 /* Fall through */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000757 default:
758 break;
759 }
760
Lev Walkin866c8802006-09-17 08:23:35 +0000761 ASN_DEBUG("Unexpected XML tag [%c%c%c%c] in CHOICE [%s]"
762 " (ph=%d, tag=%s)",
763 ch_size>0?((const uint8_t *)buf_ptr)[0]:'?',
764 ch_size>1?((const uint8_t *)buf_ptr)[1]:'?',
765 ch_size>2?((const uint8_t *)buf_ptr)[2]:'?',
766 ch_size>3?((const uint8_t *)buf_ptr)[3]:'?',
767 td->name, ctx->phase, xml_tag);
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000768 break;
769 }
770
Lev Walkinc34dc462005-02-18 16:23:48 +0000771 ctx->phase = 5; /* Phase out, just in case */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000772 RETURN(RC_FAIL);
773}
774
775
Lev Walkina9cc46e2004-09-22 16:06:28 +0000776asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000777CHOICE_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000778 int ilevel, enum xer_encoder_flags_e flags,
779 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkind84f6032017-10-03 16:33:59 -0700780 const asn_CHOICE_specifics_t *specs=(const asn_CHOICE_specifics_t *)td->specifics;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000781 asn_enc_rval_t er;
Lev Walkin63a35232017-08-10 19:59:47 -0700782 unsigned present;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000783
784 if(!sptr)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700785 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000786
787 /*
788 * Figure out which CHOICE element is encoded.
789 */
790 present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
791
Lev Walkin63a35232017-08-10 19:59:47 -0700792 if(present == 0 || present > td->elements_count) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700793 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000794 } else {
795 asn_enc_rval_t tmper;
Lev Walkin5e033762004-09-29 13:26:15 +0000796 asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkina9cc46e2004-09-22 16:06:28 +0000797 void *memb_ptr;
798 const char *mname = elm->name;
799 unsigned int mlen = strlen(mname);
800
801 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800802 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700803 if(!memb_ptr) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000804 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800805 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000806 }
807
808 er.encoded = 0;
809
Lev Walkin4fe28822017-09-18 02:57:34 -0700810 if(!(flags & XER_F_CANONICAL)) ASN__TEXT_INDENT(1, ilevel);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700811 ASN__CALLBACK3("<", 1, mname, mlen, ">", 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000812
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800813 tmper = elm->type->op->xer_encoder(elm->type, memb_ptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000814 ilevel + 1, flags, cb, app_key);
815 if(tmper.encoded == -1) return tmper;
Lev Walkin4fe28822017-09-18 02:57:34 -0700816 er.encoded += tmper.encoded;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000817
Lev Walkin7c1dc052016-03-14 03:08:15 -0700818 ASN__CALLBACK3("</", 2, mname, mlen, ">", 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000819 }
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
Lev Walkinafbf2a92017-09-12 23:30:27 -0700829CHOICE_decode_uper(const asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -0700830 const asn_per_constraints_t *constraints, void **sptr,
831 asn_per_data_t *pd) {
Lev Walkind84f6032017-10-03 16:33:59 -0700832 const asn_CHOICE_specifics_t *specs =
833 (const asn_CHOICE_specifics_t *)td->specifics;
834 asn_dec_rval_t rv;
Lev Walkin494fb702017-08-07 20:07:00 -0700835 const asn_per_constraint_t *ct;
Lev Walkin59b176e2005-11-26 11:25:14 +0000836 asn_TYPE_member_t *elm; /* CHOICE's element */
837 void *memb_ptr;
838 void **memb_ptr2;
839 void *st = *sptr;
840 int value;
841
Lev Walkin7c1dc052016-03-14 03:08:15 -0700842 if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx))
843 ASN__DECODE_FAILED;
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000844
Lev Walkin59b176e2005-11-26 11:25:14 +0000845 /*
846 * Create the target structure if it is not present already.
847 */
848 if(!st) {
849 st = *sptr = CALLOC(1, specs->struct_size);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700850 if(!st) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000851 }
852
853 if(constraints) ct = &constraints->value;
Lev Walkina5972be2017-09-29 23:15:58 -0700854 else if(td->encoding_constraints.per_constraints) ct = &td->encoding_constraints.per_constraints->value;
Lev Walkin59b176e2005-11-26 11:25:14 +0000855 else ct = 0;
856
857 if(ct && ct->flags & APC_EXTENSIBLE) {
858 value = per_get_few_bits(pd, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700859 if(value < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000860 if(value) ct = 0; /* Not restricted */
861 }
862
863 if(ct && ct->range_bits >= 0) {
864 value = per_get_few_bits(pd, ct->range_bits);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700865 if(value < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000866 ASN_DEBUG("CHOICE %s got index %d in range %d",
867 td->name, value, ct->range_bits);
Lev Walkin0a8aa602006-09-18 20:05:55 +0000868 if(value > ct->upper_bound)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700869 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000870 } else {
871 if(specs->ext_start == -1)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700872 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000873 value = uper_get_nsnnwn(pd);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700874 if(value < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000875 value += specs->ext_start;
Lev Walkin494fb702017-08-07 20:07:00 -0700876 if((unsigned)value >= td->elements_count)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700877 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000878 }
879
880 /* Adjust if canonical order is different from natural order */
881 if(specs->canonical_order)
882 value = specs->canonical_order[value];
883
884 /* Set presence to be able to free it later */
885 _set_present_idx(st, specs->pres_offset, specs->pres_size, value + 1);
886
887 elm = &td->elements[value];
888 if(elm->flags & ATF_POINTER) {
889 /* Member is a pointer to another structure */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800890 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
Lev Walkin59b176e2005-11-26 11:25:14 +0000891 } else {
892 memb_ptr = (char *)st + elm->memb_offset;
893 memb_ptr2 = &memb_ptr;
894 }
895 ASN_DEBUG("Discovered CHOICE %s encodes %s", td->name, elm->name);
896
Lev Walkin6f9060f2007-06-29 01:45:03 +0000897 if(ct && ct->range_bits >= 0) {
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800898 rv = elm->type->op->uper_decoder(opt_codec_ctx, elm->type,
Lev Walkina5972be2017-09-29 23:15:58 -0700899 elm->encoding_constraints.per_constraints, memb_ptr2, pd);
Lev Walkin6f9060f2007-06-29 01:45:03 +0000900 } else {
901 rv = uper_open_type_get(opt_codec_ctx, elm->type,
Lev Walkina5972be2017-09-29 23:15:58 -0700902 elm->encoding_constraints.per_constraints, memb_ptr2, pd);
Lev Walkin6f9060f2007-06-29 01:45:03 +0000903 }
904
Lev Walkin59b176e2005-11-26 11:25:14 +0000905 if(rv.code != RC_OK)
Lev Walkin6f9060f2007-06-29 01:45:03 +0000906 ASN_DEBUG("Failed to decode %s in %s (CHOICE) %d",
907 elm->name, td->name, rv.code);
Lev Walkin59b176e2005-11-26 11:25:14 +0000908 return rv;
909}
Lev Walkin494fb702017-08-07 20:07:00 -0700910
Lev Walkin523de9e2006-08-18 01:34:18 +0000911asn_enc_rval_t
912CHOICE_encode_uper(asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -0700913 const asn_per_constraints_t *constraints, void *sptr,
914 asn_per_outp_t *po) {
Lev Walkind84f6032017-10-03 16:33:59 -0700915 const asn_CHOICE_specifics_t *specs = (const asn_CHOICE_specifics_t *)td->specifics;
Lev Walkin523de9e2006-08-18 01:34:18 +0000916 asn_TYPE_member_t *elm; /* CHOICE's element */
Lev Walkin494fb702017-08-07 20:07:00 -0700917 const asn_per_constraint_t *ct;
Lev Walkin523de9e2006-08-18 01:34:18 +0000918 void *memb_ptr;
Lev Walkin63a35232017-08-10 19:59:47 -0700919 unsigned present;
Lev Walkine14480f2013-03-24 03:28:00 -0700920 int present_enc;
Lev Walkin523de9e2006-08-18 01:34:18 +0000921
Lev Walkin7c1dc052016-03-14 03:08:15 -0700922 if(!sptr) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000923
924 ASN_DEBUG("Encoding %s as CHOICE", td->name);
925
926 if(constraints) ct = &constraints->value;
Lev Walkina5972be2017-09-29 23:15:58 -0700927 else if(td->encoding_constraints.per_constraints)
928 ct = &td->encoding_constraints.per_constraints->value;
Lev Walkin523de9e2006-08-18 01:34:18 +0000929 else ct = 0;
930
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800931 present = _fetch_present_idx(sptr, specs->pres_offset, specs->pres_size);
Lev Walkin523de9e2006-08-18 01:34:18 +0000932
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800933 /*
Lev Walkin523de9e2006-08-18 01:34:18 +0000934 * If the structure was not initialized properly, it cannot be encoded:
935 * can't deduce what to encode in the choice type.
936 */
Lev Walkin63a35232017-08-10 19:59:47 -0700937 if(present == 0 || present > td->elements_count)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700938 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000939 else
940 present--;
941
Lev Walkin523de9e2006-08-18 01:34:18 +0000942 ASN_DEBUG("Encoding %s CHOICE element %d", td->name, present);
943
Lev Walkine14480f2013-03-24 03:28:00 -0700944 /* Adjust if canonical order is different from natural order */
945 if(specs->canonical_order)
946 present_enc = specs->canonical_order[present];
947 else
948 present_enc = present;
949
Lev Walkin523de9e2006-08-18 01:34:18 +0000950 if(ct && ct->range_bits >= 0) {
Lev Walkine14480f2013-03-24 03:28:00 -0700951 if(present_enc < ct->lower_bound
952 || present_enc > ct->upper_bound) {
Lev Walkin523de9e2006-08-18 01:34:18 +0000953 if(ct->flags & APC_EXTENSIBLE) {
954 if(per_put_few_bits(po, 1, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700955 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000956 } else {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700957 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000958 }
959 ct = 0;
960 }
961 }
962 if(ct && ct->flags & APC_EXTENSIBLE)
963 if(per_put_few_bits(po, 0, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700964 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000965
Lev Walkin523de9e2006-08-18 01:34:18 +0000966 elm = &td->elements[present];
967 if(elm->flags & ATF_POINTER) {
968 /* Member is a pointer to another structure */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800969 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700970 if(!memb_ptr) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000971 } else {
972 memb_ptr = (char *)sptr + elm->memb_offset;
973 }
974
Lev Walkin6f9060f2007-06-29 01:45:03 +0000975 if(ct && ct->range_bits >= 0) {
Lev Walkine14480f2013-03-24 03:28:00 -0700976 if(per_put_few_bits(po, present_enc, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700977 ASN__ENCODE_FAILED;
Lev Walkin6f9060f2007-06-29 01:45:03 +0000978
Lev Walkina5972be2017-09-29 23:15:58 -0700979 return elm->type->op->uper_encoder(elm->type, elm->encoding_constraints.per_constraints,
Lev Walkin523de9e2006-08-18 01:34:18 +0000980 memb_ptr, po);
Lev Walkin6f9060f2007-06-29 01:45:03 +0000981 } else {
982 asn_enc_rval_t rval;
983 if(specs->ext_start == -1)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700984 ASN__ENCODE_FAILED;
Lev Walkine14480f2013-03-24 03:28:00 -0700985 if(uper_put_nsnnwn(po, present_enc - specs->ext_start))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700986 ASN__ENCODE_FAILED;
Lev Walkina5972be2017-09-29 23:15:58 -0700987 if(uper_open_type_put(elm->type, elm->encoding_constraints.per_constraints,
Lev Walkin6f9060f2007-06-29 01:45:03 +0000988 memb_ptr, po))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700989 ASN__ENCODE_FAILED;
Lev Walkin6f9060f2007-06-29 01:45:03 +0000990 rval.encoded = 0;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700991 ASN__ENCODED_OK(rval);
Lev Walkin6f9060f2007-06-29 01:45:03 +0000992 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000993}
994
Lev Walkin59b176e2005-11-26 11:25:14 +0000995
Lev Walkinf15320b2004-06-03 03:38:44 +0000996int
Lev Walkin5e033762004-09-29 13:26:15 +0000997CHOICE_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000998 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkind84f6032017-10-03 16:33:59 -0700999 const asn_CHOICE_specifics_t *specs = (const asn_CHOICE_specifics_t *)td->specifics;
Lev Walkin63a35232017-08-10 19:59:47 -07001000 unsigned present;
Lev Walkinf15320b2004-06-03 03:38:44 +00001001
Lev Walkin8e8078a2004-09-26 13:10:40 +00001002 if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001003
1004 /*
1005 * Figure out which CHOICE element is encoded.
1006 */
1007 present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
1008
1009 /*
Lev Walkina9cc46e2004-09-22 16:06:28 +00001010 * Print that element.
Lev Walkinf15320b2004-06-03 03:38:44 +00001011 */
Lev Walkin63a35232017-08-10 19:59:47 -07001012 if(present > 0 && present <= td->elements_count) {
Lev Walkin5e033762004-09-29 13:26:15 +00001013 asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkinf15320b2004-06-03 03:38:44 +00001014 const void *memb_ptr;
1015
Lev Walkincc93b0f2004-09-10 09:18:20 +00001016 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001017 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkin8e8078a2004-09-26 13:10:40 +00001018 if(!memb_ptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001019 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001020 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +00001021 }
1022
1023 /* Print member's name and stuff */
Lev Walkincc6a9102004-09-23 22:06:26 +00001024 if(0) {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001025 if(cb(elm->name, strlen(elm->name), app_key) < 0
1026 || cb(": ", 2, app_key) < 0)
Lev Walkincc6a9102004-09-23 22:06:26 +00001027 return -1;
1028 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001029
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001030 return elm->type->op->print_struct(elm->type, memb_ptr, ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +00001031 cb, app_key);
1032 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001033 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001034 }
1035}
1036
1037void
Lev Walkin8d99d7b2017-08-25 01:06:00 -07001038CHOICE_free(const asn_TYPE_descriptor_t *td, void *ptr,
1039 enum asn_struct_free_method method) {
Lev Walkind84f6032017-10-03 16:33:59 -07001040 const asn_CHOICE_specifics_t *specs =
1041 (const asn_CHOICE_specifics_t *)td->specifics;
1042 unsigned present;
Lev Walkinf15320b2004-06-03 03:38:44 +00001043
1044 if(!td || !ptr)
1045 return;
1046
1047 ASN_DEBUG("Freeing %s as CHOICE", td->name);
1048
1049 /*
1050 * Figure out which CHOICE element is encoded.
1051 */
1052 present = _fetch_present_idx(ptr, specs->pres_offset, specs->pres_size);
1053
1054 /*
1055 * Free that element.
1056 */
Lev Walkin63a35232017-08-10 19:59:47 -07001057 if(present > 0 && present <= td->elements_count) {
Lev Walkin5e033762004-09-29 13:26:15 +00001058 asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkinf15320b2004-06-03 03:38:44 +00001059 void *memb_ptr;
1060
Lev Walkincc93b0f2004-09-10 09:18:20 +00001061 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001062 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +00001063 if(memb_ptr)
Lev Walkinadcb5862006-03-17 02:11:12 +00001064 ASN_STRUCT_FREE(*elm->type, memb_ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +00001065 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001066 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
Lev Walkinadcb5862006-03-17 02:11:12 +00001067 ASN_STRUCT_FREE_CONTENTS_ONLY(*elm->type, memb_ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +00001068 }
1069 }
1070
Lev Walkin8d99d7b2017-08-25 01:06:00 -07001071 switch(method) {
1072 case ASFM_FREE_EVERYTHING:
1073 FREEMEM(ptr);
1074 break;
1075 case ASFM_FREE_UNDERLYING:
1076 break;
1077 case ASFM_FREE_UNDERLYING_AND_RESET:
1078 memset(ptr, 0, specs->struct_size);
1079 break;
1080 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001081}
1082
1083
1084/*
1085 * The following functions functions offer protection against -fshort-enums,
1086 * compatible with little- and big-endian machines.
1087 * If assertion is triggered, either disable -fshort-enums, or add an entry
1088 * here with the ->pres_size of your target stracture.
1089 * Unless the target structure is packed, the ".present" member
1090 * is guaranteed to be aligned properly. ASN.1 compiler itself does not
1091 * produce packed code.
1092 */
Lev Walkinf6853ce2017-08-11 00:50:27 -07001093static unsigned
1094_fetch_present_idx(const void *struct_ptr, unsigned pres_offset,
1095 unsigned pres_size) {
1096 const void *present_ptr;
Lev Walkin63a35232017-08-10 19:59:47 -07001097 unsigned present;
Lev Walkinf15320b2004-06-03 03:38:44 +00001098
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001099 present_ptr = ((const char *)struct_ptr) + pres_offset;
Lev Walkinf15320b2004-06-03 03:38:44 +00001100
1101 switch(pres_size) {
Lev Walkinf6853ce2017-08-11 00:50:27 -07001102 case sizeof(int): present = *(const unsigned int *)present_ptr; break;
1103 case sizeof(short): present = *(const unsigned short *)present_ptr; break;
1104 case sizeof(char): present = *(const unsigned char *)present_ptr; break;
Lev Walkinf15320b2004-06-03 03:38:44 +00001105 default:
1106 /* ANSI C mandates enum to be equivalent to integer */
1107 assert(pres_size != sizeof(int));
1108 return 0; /* If not aborted, pass back safe value */
1109 }
1110
1111 return present;
1112}
1113
Lev Walkin91f5cd02004-08-11 09:10:59 +00001114static void
Lev Walkinf6853ce2017-08-11 00:50:27 -07001115_set_present_idx(void *struct_ptr, unsigned pres_offset, unsigned pres_size,
1116 unsigned present) {
1117 void *present_ptr;
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001118 present_ptr = ((char *)struct_ptr) + pres_offset;
Lev Walkinf15320b2004-06-03 03:38:44 +00001119
1120 switch(pres_size) {
Lev Walkinf6853ce2017-08-11 00:50:27 -07001121 case sizeof(int): *(unsigned int *)present_ptr = present; break;
1122 case sizeof(short): *(unsigned short *)present_ptr = present; break;
1123 case sizeof(char): *(unsigned char *)present_ptr = present; break;
Lev Walkinf15320b2004-06-03 03:38:44 +00001124 default:
1125 /* ANSI C mandates enum to be equivalent to integer */
1126 assert(pres_size != sizeof(int));
1127 }
1128}
Lev Walkincd2f48e2017-08-10 02:14:59 -07001129
1130static const void *
1131_get_member_ptr(const asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin63a35232017-08-10 19:59:47 -07001132 asn_TYPE_member_t **elm_ptr, unsigned *present_out) {
Lev Walkind84f6032017-10-03 16:33:59 -07001133 const asn_CHOICE_specifics_t *specs =
1134 (const asn_CHOICE_specifics_t *)td->specifics;
Lev Walkin63a35232017-08-10 19:59:47 -07001135 unsigned present;
Lev Walkincd2f48e2017-08-10 02:14:59 -07001136
1137 if(!sptr) {
1138 *elm_ptr = NULL;
1139 *present_out = 0;
1140 return NULL;
1141 }
1142
1143 /*
1144 * Figure out which CHOICE element is encoded.
1145 */
1146 present = _fetch_present_idx(sptr, specs->pres_offset, specs->pres_size);
1147 *present_out = present;
1148
1149 /*
1150 * The presence index is intentionally 1-based to avoid
1151 * treating zeroed structure as a valid one.
1152 */
Lev Walkin63a35232017-08-10 19:59:47 -07001153 if(present > 0 && present <= td->elements_count) {
Lev Walkincd2f48e2017-08-10 02:14:59 -07001154 asn_TYPE_member_t *const elm = &td->elements[present - 1];
1155 const void *memb_ptr;
1156
1157 if(elm->flags & ATF_POINTER) {
1158 memb_ptr =
1159 *(const void *const *)((const char *)sptr + elm->memb_offset);
1160 } else {
1161 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
1162 }
1163 *elm_ptr = elm;
1164 return memb_ptr;
1165 } else {
1166 *elm_ptr = NULL;
1167 return NULL;
1168 }
1169
1170}
1171
1172int
1173CHOICE_compare(const asn_TYPE_descriptor_t *td, const void *aptr, const void *bptr) {
1174 asn_TYPE_member_t *aelm;
1175 asn_TYPE_member_t *belm;
Lev Walkin63a35232017-08-10 19:59:47 -07001176 unsigned apresent = 0;
1177 unsigned bpresent = 0;
Lev Walkincd2f48e2017-08-10 02:14:59 -07001178 const void *amember = _get_member_ptr(td, aptr, &aelm, &apresent);
1179 const void *bmember = _get_member_ptr(td, bptr, &belm, &apresent);
1180
1181 if(amember && bmember) {
1182 if(apresent == bpresent) {
1183 assert(aelm == belm);
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001184 return aelm->type->op->compare_struct(aelm->type, amember, bmember);
Lev Walkincd2f48e2017-08-10 02:14:59 -07001185 } else if(apresent < bpresent) {
1186 return -1;
1187 } else {
1188 return 1;
1189 }
1190 } else if(!amember) {
1191 return -1;
1192 } else {
1193 return 1;
1194 }
1195}
Lev Walkinf6853ce2017-08-11 00:50:27 -07001196
1197/*
1198 * Return the 1-based choice variant presence index.
1199 * Returns 0 in case of error.
1200 */
1201unsigned
1202CHOICE_variant_get_presence(const asn_TYPE_descriptor_t *td, const void *sptr) {
Lev Walkind84f6032017-10-03 16:33:59 -07001203 const asn_CHOICE_specifics_t *specs =
1204 (const asn_CHOICE_specifics_t *)td->specifics;
Lev Walkinf6853ce2017-08-11 00:50:27 -07001205 return _fetch_present_idx(sptr, specs->pres_offset, specs->pres_size);
1206}
1207
1208/*
1209 * Sets or resets the 1-based choice variant presence index.
1210 * In case a previous index is not zero, the currently selected structure
1211 * member is freed and zeroed-out first.
1212 * Returns 0 on success and -1 on error.
1213 */
1214int
1215CHOICE_variant_set_presence(const asn_TYPE_descriptor_t *td, void *sptr,
1216 unsigned present) {
Lev Walkind84f6032017-10-03 16:33:59 -07001217 const asn_CHOICE_specifics_t *specs =
1218 (const asn_CHOICE_specifics_t *)td->specifics;
Lev Walkinf6853ce2017-08-11 00:50:27 -07001219 unsigned old_present;
1220
1221 if(!sptr) {
1222 return -1;
1223 }
1224
1225 if(present > td->elements_count)
1226 return -1;
1227
1228 old_present =
1229 _fetch_present_idx(sptr, specs->pres_offset, specs->pres_size);
1230 if(present == old_present)
1231 return 0;
1232
Lev Walkin91ca2732017-08-11 02:32:42 -07001233 if(old_present != 0) {
Lev Walkinf6853ce2017-08-11 00:50:27 -07001234 assert(old_present <= td->elements_count);
Lev Walkin8d99d7b2017-08-25 01:06:00 -07001235 ASN_STRUCT_RESET(*td, sptr);
Lev Walkinf6853ce2017-08-11 00:50:27 -07001236 }
1237
1238 _set_present_idx(sptr, specs->pres_offset, specs->pres_size, present);
1239
1240 return 0;
1241}
1242
Lev Walkina5972be2017-09-29 23:15:58 -07001243
1244asn_random_fill_result_t
1245CHOICE_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
1246 const asn_encoding_constraints_t *constr,
1247 size_t max_length) {
1248 const asn_CHOICE_specifics_t *specs =
1249 (const asn_CHOICE_specifics_t *)td->specifics;
1250 asn_random_fill_result_t res;
1251 asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
1252 asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
1253 const asn_TYPE_member_t *elm;
1254 unsigned present;
1255 void *memb_ptr; /* Pointer to the member */
1256 void **memb_ptr2; /* Pointer to that pointer */
1257 void *st = *sptr;
1258
1259 if(max_length == 0) return result_skipped;
1260
1261 (void)constr;
1262
1263 if(st == NULL) {
1264 st = CALLOC(1, specs->struct_size);
1265 if(st == NULL) {
1266 return result_failed;
1267 }
1268 }
1269
1270 present = asn_random_between(1, td->elements_count);
1271 elm = &td->elements[present - 1];
1272
1273 if(elm->flags & ATF_POINTER) {
1274 /* Member is a pointer to another structure */
1275 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
1276 } else {
1277 memb_ptr = (char *)st + elm->memb_offset;
1278 memb_ptr2 = &memb_ptr;
1279 }
1280
1281 res = elm->type->op->random_fill(elm->type, memb_ptr2,
1282 &elm->encoding_constraints, max_length);
1283 _set_present_idx(st, specs->pres_offset, specs->pres_size, present);
1284 if(res.code == ARFILL_OK) {
1285 *sptr = st;
1286 } else {
1287 if(st == *sptr) {
1288 ASN_STRUCT_RESET(*td, st);
1289 } else {
1290 ASN_STRUCT_FREE(*td, st);
1291 }
1292 }
1293
1294 return res;
1295}
1296
1297
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001298asn_TYPE_operation_t asn_OP_CHOICE = {
1299 CHOICE_free,
1300 CHOICE_print,
1301 CHOICE_compare,
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001302 CHOICE_decode_ber,
1303 CHOICE_encode_der,
1304 CHOICE_decode_xer,
1305 CHOICE_encode_xer,
1306#ifdef ASN_DISABLE_OER_SUPPORT
1307 0,
1308 0,
1309#else
Lev Walkin9a1736d2017-08-27 23:46:34 -07001310 CHOICE_decode_oer,
1311 CHOICE_encode_oer,
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001312#endif /* ASN_DISABLE_OER_SUPPORT */
1313#ifdef ASN_DISABLE_PER_SUPPORT
1314 0,
1315 0,
1316#else
1317 CHOICE_decode_uper,
1318 CHOICE_encode_uper,
1319#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkina5972be2017-09-29 23:15:58 -07001320 CHOICE_random_fill,
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001321 CHOICE_outmost_tag
1322};