blob: 628979ec22b417f9c5570b4afc720eae944ac076 [file] [log] [blame]
Lev Walkin523de9e2006-08-18 01:34:18 +00001/*
Lev Walkin20696a42017-10-17 21:27:33 -07002 * Copyright (c) 2003-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
Lev Walkinf15320b2004-06-03 03:38:44 +00003 * Redistribution and modifications are permitted subject to BSD license.
4 */
Lev Walkina9cc46e2004-09-22 16:06:28 +00005#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00006#include <constr_CHOICE.h>
Lev Walkin6f9060f2007-06-29 01:45:03 +00007#include <per_opentype.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00008
9/*
10 * Number of bytes left for this structure.
11 * (ctx->left) indicates the number of bytes _transferred_ for the structure.
12 * (size) contains the number of bytes in the buffer passed.
13 */
Lev Walkinec1ffd42004-08-18 04:53:32 +000014#define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left)
Lev Walkinf15320b2004-06-03 03:38:44 +000015
16/*
17 * If the subprocessor function returns with an indication that it wants
18 * more data, it may well be a fatal decoding problem, because the
19 * size is constrained by the <TLV>'s L, even if the buffer size allows
20 * reading more data.
21 * For example, consider the buffer containing the following TLVs:
22 * <T:5><L:1><V> <T:6>...
23 * The TLV length clearly indicates that one byte is expected in V, but
24 * if the V processor returns with "want more data" even if the buffer
25 * contains way more data than the V processor have seen.
26 */
Lev Walkind9bd7752004-06-05 08:17:50 +000027#define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size)
Lev Walkinf15320b2004-06-03 03:38:44 +000028
29/*
30 * This macro "eats" the part of the buffer which is definitely "consumed",
31 * i.e. was correctly converted into local representation or rightfully skipped.
32 */
Lev Walkincc6a9102004-09-23 22:06:26 +000033#undef ADVANCE
Lev Walkinf15320b2004-06-03 03:38:44 +000034#define ADVANCE(num_bytes) do { \
35 size_t num = num_bytes; \
Lev Walkinaa61a0f2014-01-13 23:08:47 -080036 ptr = ((const char *)ptr) + num;\
Lev Walkinf15320b2004-06-03 03:38:44 +000037 size -= num; \
38 if(ctx->left >= 0) \
39 ctx->left -= num; \
40 consumed_myself += num; \
41 } while(0)
42
43/*
44 * Switch to the next phase of parsing.
45 */
Lev Walkincc6a9102004-09-23 22:06:26 +000046#undef NEXT_PHASE
Lev Walkinf15320b2004-06-03 03:38:44 +000047#define NEXT_PHASE(ctx) do { \
48 ctx->phase++; \
49 ctx->step = 0; \
50 } while(0)
51
52/*
53 * Return a standardized complex structure.
54 */
Lev Walkincc6a9102004-09-23 22:06:26 +000055#undef RETURN
Lev Walkinf15320b2004-06-03 03:38:44 +000056#define RETURN(_code) do { \
57 rval.code = _code; \
58 rval.consumed = consumed_myself;\
59 return rval; \
60 } while(0)
61
62/*
63 * See the definitions.
64 */
Lev Walkinf6853ce2017-08-11 00:50:27 -070065static unsigned _fetch_present_idx(const void *struct_ptr, unsigned off,
66 unsigned size);
67static void _set_present_idx(void *sptr, unsigned offset, unsigned size,
68 unsigned pres);
Lev Walkincd2f48e2017-08-10 02:14:59 -070069static const void *_get_member_ptr(const asn_TYPE_descriptor_t *,
70 const void *sptr, asn_TYPE_member_t **elm,
Lev Walkin63a35232017-08-10 19:59:47 -070071 unsigned *present);
Lev Walkinf15320b2004-06-03 03:38:44 +000072
73/*
74 * Tags are canonically sorted in the tag to member table.
75 */
76static int
77_search4tag(const void *ap, const void *bp) {
Lev Walkin5e033762004-09-29 13:26:15 +000078 const asn_TYPE_tag2member_t *a = (const asn_TYPE_tag2member_t *)ap;
79 const asn_TYPE_tag2member_t *b = (const asn_TYPE_tag2member_t *)bp;
Lev Walkinc2346572004-08-11 09:07:36 +000080
Lev Walkinf15320b2004-06-03 03:38:44 +000081 int a_class = BER_TAG_CLASS(a->el_tag);
82 int b_class = BER_TAG_CLASS(b->el_tag);
83
84 if(a_class == b_class) {
85 ber_tlv_tag_t a_value = BER_TAG_VALUE(a->el_tag);
86 ber_tlv_tag_t b_value = BER_TAG_VALUE(b->el_tag);
87
88 if(a_value == b_value)
89 return 0;
90 else if(a_value < b_value)
91 return -1;
92 else
93 return 1;
94 } else if(a_class < b_class) {
95 return -1;
96 } else {
97 return 1;
98 }
99}
100
101/*
102 * The decoder of the CHOICE type.
103 */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000104asn_dec_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -0700105CHOICE_decode_ber(const asn_codec_ctx_t *opt_codec_ctx,
106 const asn_TYPE_descriptor_t *td, void **struct_ptr,
107 const void *ptr, size_t size, int tag_mode) {
108 /*
Lev Walkinf15320b2004-06-03 03:38:44 +0000109 * 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 Walkin20696a42017-10-17 21:27:33 -0700363CHOICE_encode_der(const asn_TYPE_descriptor_t *td, const void *sptr,
364 int tag_mode, ber_tlv_tag_t tag, asn_app_consume_bytes_f *cb,
365 void *app_key) {
366 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 Walkin20696a42017-10-17 21:27:33 -0700369 const void *memb_ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000370 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 Walkin20696a42017-10-17 21:27:33 -0700399 memb_ptr =
400 *(const void *const *)((const char *)sptr + elm->memb_offset);
401 if(memb_ptr == 0) {
Lev Walkincc93b0f2004-09-10 09:18:20 +0000402 if(elm->optional) {
403 erval.encoded = 0;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700404 ASN__ENCODED_OK(erval);
Lev Walkincc93b0f2004-09-10 09:18:20 +0000405 }
Lev Walkinac589332005-08-22 14:19:28 +0000406 /* Mandatory element absent */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700407 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000408 }
409 } else {
Lev Walkin20696a42017-10-17 21:27:33 -0700410 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
411 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000412
413 /*
414 * If the CHOICE itself is tagged EXPLICIT:
415 * T ::= [2] EXPLICIT CHOICE { ... }
416 * Then emit the appropriate tags.
417 */
Lev Walkin449f8322004-08-20 13:23:42 +0000418 if(tag_mode == 1 || td->tags_count) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000419 /*
420 * For this, we need to pre-compute the member.
421 */
422 ssize_t ret;
423
424 /* Encode member with its tag */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800425 erval = elm->type->op->der_encoder(elm->type, memb_ptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000426 elm->tag_mode, elm->tag, 0, 0);
427 if(erval.encoded == -1)
428 return erval;
429
430 /* Encode CHOICE with parent or my own tag */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000431 ret = der_write_tags(td, erval.encoded, tag_mode, 1, tag,
Lev Walkinf15320b2004-06-03 03:38:44 +0000432 cb, app_key);
Lev Walkinac589332005-08-22 14:19:28 +0000433 if(ret == -1)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700434 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000435 computed_size += ret;
436 }
437
438 /*
439 * Encode the single underlying member.
440 */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800441 erval = elm->type->op->der_encoder(elm->type, memb_ptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000442 elm->tag_mode, elm->tag, cb, app_key);
443 if(erval.encoded == -1)
444 return erval;
445
446 ASN_DEBUG("Encoded CHOICE member in %ld bytes (+%ld)",
447 (long)erval.encoded, (long)computed_size);
448
449 erval.encoded += computed_size;
450
451 return erval;
452}
453
454ber_tlv_tag_t
Wim Lewis14e6b162014-07-23 16:06:01 -0700455CHOICE_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 -0700456 const asn_CHOICE_specifics_t *specs = (const asn_CHOICE_specifics_t *)td->specifics;
Lev Walkin63a35232017-08-10 19:59:47 -0700457 unsigned present;
Lev Walkinf15320b2004-06-03 03:38:44 +0000458
Lev Walkin32788732005-11-13 09:12:23 +0000459 assert(tag_mode == 0); (void)tag_mode;
460 assert(tag == 0); (void)tag;
Lev Walkinf15320b2004-06-03 03:38:44 +0000461
462 /*
463 * Figure out which CHOICE element is encoded.
464 */
465 present = _fetch_present_idx(ptr, specs->pres_offset, specs->pres_size);
466
Lev Walkin63a35232017-08-10 19:59:47 -0700467 if(present > 0 && present <= td->elements_count) {
Wim Lewis14e6b162014-07-23 16:06:01 -0700468 const asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkin70e70282004-06-05 01:45:48 +0000469 const void *memb_ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000470
Lev Walkincc93b0f2004-09-10 09:18:20 +0000471 if(elm->flags & ATF_POINTER) {
Lev Walkin70e70282004-06-05 01:45:48 +0000472 memb_ptr = *(const void * const *)
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800473 ((const char *)ptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000474 } else {
Lev Walkin70e70282004-06-05 01:45:48 +0000475 memb_ptr = (const void *)
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800476 ((const char *)ptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000477 }
478
Lev Walkin5e033762004-09-29 13:26:15 +0000479 return asn_TYPE_outmost_tag(elm->type, memb_ptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000480 elm->tag_mode, elm->tag);
481 } else {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000482 return (ber_tlv_tag_t)-1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000483 }
484}
485
486int
Lev Walkin20696a42017-10-17 21:27:33 -0700487CHOICE_constraint(const asn_TYPE_descriptor_t *td, const void *sptr,
488 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
489 const asn_CHOICE_specifics_t *specs =
490 (const asn_CHOICE_specifics_t *)td->specifics;
491 unsigned present;
Lev Walkinf15320b2004-06-03 03:38:44 +0000492
493 if(!sptr) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700494 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +0000495 "%s: value not given (%s:%d)",
496 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000497 return -1;
498 }
499
500 /*
501 * Figure out which CHOICE element is encoded.
502 */
503 present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
Lev Walkin63a35232017-08-10 19:59:47 -0700504 if(present > 0 && present <= td->elements_count) {
Lev Walkin5e033762004-09-29 13:26:15 +0000505 asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkinf15320b2004-06-03 03:38:44 +0000506 const void *memb_ptr;
507
Lev Walkincc93b0f2004-09-10 09:18:20 +0000508 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800509 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkincc93b0f2004-09-10 09:18:20 +0000510 if(!memb_ptr) {
511 if(elm->optional)
512 return 0;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700513 ASN__CTFAIL(app_key, td, sptr,
Lev Walkincc93b0f2004-09-10 09:18:20 +0000514 "%s: mandatory CHOICE element %s absent (%s:%d)",
515 td->name, elm->name, __FILE__, __LINE__);
516 return -1;
517 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000518 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800519 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000520 }
521
Lev Walkina5972be2017-09-29 23:15:58 -0700522 if(elm->encoding_constraints.general_constraints) {
523 return elm->encoding_constraints.general_constraints(elm->type, memb_ptr,
Lev Walkin1eded352006-07-13 11:19:01 +0000524 ctfailcb, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +0000525 } else {
Lev Walkina5972be2017-09-29 23:15:58 -0700526 return elm->type->encoding_constraints.general_constraints(elm->type,
Lev Walkin1eded352006-07-13 11:19:01 +0000527 memb_ptr, ctfailcb, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +0000528 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000529 } else {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700530 ASN__CTFAIL(app_key, td, sptr,
Lev Walkind34a8512004-08-22 13:55:49 +0000531 "%s: no CHOICE element given (%s:%d)",
Lev Walkin16835b62004-08-22 13:47:59 +0000532 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000533 return -1;
534 }
535}
536
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000537#undef XER_ADVANCE
538#define XER_ADVANCE(num_bytes) do { \
539 size_t num = num_bytes; \
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800540 buf_ptr = (const void *)(((const char *)buf_ptr) + num); \
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000541 size -= num; \
542 consumed_myself += num; \
543 } while(0)
544
Lev Walkinc61f3862005-02-14 17:21:22 +0000545/*
546 * Decode the XER (XML) data.
547 */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000548asn_dec_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -0700549CHOICE_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
550 const asn_TYPE_descriptor_t *td, void **struct_ptr,
551 const char *opt_mname, const void *buf_ptr, size_t size) {
552 /*
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000553 * Bring closer parts of structure description.
554 */
Lev Walkind84f6032017-10-03 16:33:59 -0700555 const asn_CHOICE_specifics_t *specs = (const asn_CHOICE_specifics_t *)td->specifics;
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000556 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
557
558 /*
559 * Parts of the structure being constructed.
560 */
561 void *st = *struct_ptr; /* Target structure. */
562 asn_struct_ctx_t *ctx; /* Decoder context */
563
Lev Walkinc61f3862005-02-14 17:21:22 +0000564 asn_dec_rval_t rval; /* Return value of a decoder */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000565 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
Lev Walkin494fb702017-08-07 20:07:00 -0700566 size_t edx; /* Element index */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000567
568 /*
569 * Create the target structure if it is not present already.
570 */
571 if(st == 0) {
572 st = *struct_ptr = CALLOC(1, specs->struct_size);
573 if(st == 0) RETURN(RC_FAIL);
574 }
575
576 /*
577 * Restore parsing context.
578 */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800579 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkind1bfea62005-11-08 03:06:16 +0000580 if(ctx->phase == 0 && !*xml_tag)
581 ctx->phase = 1; /* Skip the outer tag checking phase */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000582
583 /*
584 * Phases of XER/XML processing:
585 * Phase 0: Check that the opening tag matches our expectations.
586 * Phase 1: Processing body and reacting on closing tag.
587 * Phase 2: Processing inner type.
Lev Walkinc34dc462005-02-18 16:23:48 +0000588 * Phase 3: Only waiting for closing tag.
589 * Phase 4: Skipping unknown extensions.
590 * Phase 5: PHASED OUT
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000591 */
Lev Walkinc34dc462005-02-18 16:23:48 +0000592 for(edx = ctx->step; ctx->phase <= 4;) {
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000593 pxer_chunk_type_e ch_type; /* XER chunk type */
594 ssize_t ch_size; /* Chunk size */
595 xer_check_tag_e tcv; /* Tag check value */
596 asn_TYPE_member_t *elm;
597
598 /*
599 * Go inside the member.
600 */
601 if(ctx->phase == 2) {
602 asn_dec_rval_t tmprval;
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000603 void *memb_ptr; /* Pointer to the member */
604 void **memb_ptr2; /* Pointer to that pointer */
Lev Walkindcae9ce2017-09-18 20:13:36 -0700605 unsigned old_present;
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000606
Lev Walkinabf68892004-10-26 10:12:14 +0000607 elm = &td->elements[edx];
608
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000609 if(elm->flags & ATF_POINTER) {
610 /* Member is a pointer to another structure */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800611 memb_ptr2 = (void **)((char *)st
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000612 + elm->memb_offset);
613 } else {
614 memb_ptr = (char *)st + elm->memb_offset;
615 memb_ptr2 = &memb_ptr;
616 }
617
Lev Walkinc61f3862005-02-14 17:21:22 +0000618 /* Start/Continue decoding the inner member */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800619 tmprval = elm->type->op->xer_decoder(opt_codec_ctx,
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000620 elm->type, memb_ptr2, elm->name,
621 buf_ptr, size);
622 XER_ADVANCE(tmprval.consumed);
Lev Walkin866c8802006-09-17 08:23:35 +0000623 ASN_DEBUG("XER/CHOICE: itdf: [%s] code=%d",
624 elm->type->name, tmprval.code);
Lev Walkindcae9ce2017-09-18 20:13:36 -0700625 old_present = _fetch_present_idx(st,
626 specs->pres_offset, specs->pres_size);
627 assert(old_present == 0 || old_present == edx + 1);
Lev Walkinc61f3862005-02-14 17:21:22 +0000628 /* Record what we've got */
629 _set_present_idx(st,
630 specs->pres_offset, specs->pres_size, edx + 1);
Lev Walkindcae9ce2017-09-18 20:13:36 -0700631 if(tmprval.code != RC_OK)
632 RETURN(tmprval.code);
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000633 ctx->phase = 3;
634 /* Fall through */
635 }
636
Lev Walkind1bfea62005-11-08 03:06:16 +0000637 /* No need to wait for closing tag; special mode. */
638 if(ctx->phase == 3 && !*xml_tag) {
639 ctx->phase = 5; /* Phase out */
640 RETURN(RC_OK);
641 }
642
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000643 /*
644 * Get the next part of the XML stream.
645 */
Lev Walkin1e443962005-02-18 18:06:36 +0000646 ch_size = xer_next_token(&ctx->context, buf_ptr, size, &ch_type);
Lev Walkin97363482016-01-24 19:23:02 -0800647 if(ch_size == -1) {
648 RETURN(RC_FAIL);
649 } else {
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000650 switch(ch_type) {
Lev Walkin97363482016-01-24 19:23:02 -0800651 case PXER_WMORE:
652 RETURN(RC_WMORE);
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000653 case PXER_COMMENT: /* Got XML comment */
654 case PXER_TEXT: /* Ignore free-standing text */
655 XER_ADVANCE(ch_size); /* Skip silently */
656 continue;
657 case PXER_TAG:
658 break; /* Check the rest down there */
659 }
660 }
661
Lev Walkinc61f3862005-02-14 17:21:22 +0000662 tcv = xer_check_tag(buf_ptr, ch_size, xml_tag);
Lev Walkind1bfea62005-11-08 03:06:16 +0000663 ASN_DEBUG("XER/CHOICE checked [%c%c%c%c] vs [%s], tcv=%d",
Lev Walkin32788732005-11-13 09:12:23 +0000664 ch_size>0?((const uint8_t *)buf_ptr)[0]:'?',
665 ch_size>1?((const uint8_t *)buf_ptr)[1]:'?',
666 ch_size>2?((const uint8_t *)buf_ptr)[2]:'?',
667 ch_size>3?((const uint8_t *)buf_ptr)[3]:'?',
Lev Walkind1bfea62005-11-08 03:06:16 +0000668 xml_tag, tcv);
Lev Walkinc34dc462005-02-18 16:23:48 +0000669
670 /* Skip the extensions section */
671 if(ctx->phase == 4) {
672 ASN_DEBUG("skip_unknown(%d, %ld)",
673 tcv, (long)ctx->left);
674 switch(xer_skip_unknown(tcv, &ctx->left)) {
675 case -1:
676 ctx->phase = 5;
677 RETURN(RC_FAIL);
Lev Walkinc34dc462005-02-18 16:23:48 +0000678 case 1:
679 ctx->phase = 3;
Lev Walkin1e443962005-02-18 18:06:36 +0000680 /* Fall through */
Lev Walkinc34dc462005-02-18 16:23:48 +0000681 case 0:
682 XER_ADVANCE(ch_size);
683 continue;
684 case 2:
685 ctx->phase = 3;
686 break;
687 }
688 }
689
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000690 switch(tcv) {
691 case XCT_BOTH:
Lev Walkinc61f3862005-02-14 17:21:22 +0000692 break; /* No CHOICE? */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000693 case XCT_CLOSING:
Lev Walkinc61f3862005-02-14 17:21:22 +0000694 if(ctx->phase != 3)
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000695 break;
696 XER_ADVANCE(ch_size);
Lev Walkinc34dc462005-02-18 16:23:48 +0000697 ctx->phase = 5; /* Phase out */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000698 RETURN(RC_OK);
699 case XCT_OPENING:
700 if(ctx->phase == 0) {
701 XER_ADVANCE(ch_size);
702 ctx->phase = 1; /* Processing body phase */
703 continue;
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000704 }
705 /* Fall through */
Lev Walkin904e65b2005-02-18 14:23:48 +0000706 case XCT_UNKNOWN_OP:
707 case XCT_UNKNOWN_BO:
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000708
709 if(ctx->phase != 1)
710 break; /* Really unexpected */
711
712 /*
Lev Walkinc61f3862005-02-14 17:21:22 +0000713 * Search which inner member corresponds to this tag.
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000714 */
715 for(edx = 0; edx < td->elements_count; edx++) {
716 elm = &td->elements[edx];
Lev Walkinc61f3862005-02-14 17:21:22 +0000717 tcv = xer_check_tag(buf_ptr,ch_size,elm->name);
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000718 switch(tcv) {
719 case XCT_BOTH:
720 case XCT_OPENING:
721 /*
722 * Process this member.
723 */
724 ctx->step = edx;
725 ctx->phase = 2;
726 break;
Lev Walkin904e65b2005-02-18 14:23:48 +0000727 case XCT_UNKNOWN_OP:
728 case XCT_UNKNOWN_BO:
Lev Walkin6c0df202005-02-14 19:03:17 +0000729 continue;
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000730 default:
731 edx = td->elements_count;
732 break; /* Phase out */
733 }
734 break;
735 }
Lev Walkin6c0df202005-02-14 19:03:17 +0000736 if(edx != td->elements_count)
737 continue;
Lev Walkin904e65b2005-02-18 14:23:48 +0000738
739 /* It is expected extension */
Lev Walkin59b176e2005-11-26 11:25:14 +0000740 if(specs->ext_start != -1) {
Lev Walkinc34dc462005-02-18 16:23:48 +0000741 ASN_DEBUG("Got anticipated extension");
Lev Walkin904e65b2005-02-18 14:23:48 +0000742 /*
Lev Walkinc34dc462005-02-18 16:23:48 +0000743 * Check for (XCT_BOTH or XCT_UNKNOWN_BO)
744 * By using a mask. Only record a pure
745 * <opening> tags.
Lev Walkin904e65b2005-02-18 14:23:48 +0000746 */
Lev Walkinc34dc462005-02-18 16:23:48 +0000747 if(tcv & XCT_CLOSING) {
748 /* Found </extension> without body */
749 ctx->phase = 3; /* Terminating */
750 } else {
751 ctx->left = 1;
752 ctx->phase = 4; /* Skip ...'s */
753 }
754 XER_ADVANCE(ch_size);
755 continue;
Lev Walkin904e65b2005-02-18 14:23:48 +0000756 }
757
Lev Walkin6c0df202005-02-14 19:03:17 +0000758 /* Fall through */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000759 default:
760 break;
761 }
762
Lev Walkin866c8802006-09-17 08:23:35 +0000763 ASN_DEBUG("Unexpected XML tag [%c%c%c%c] in CHOICE [%s]"
764 " (ph=%d, tag=%s)",
765 ch_size>0?((const uint8_t *)buf_ptr)[0]:'?',
766 ch_size>1?((const uint8_t *)buf_ptr)[1]:'?',
767 ch_size>2?((const uint8_t *)buf_ptr)[2]:'?',
768 ch_size>3?((const uint8_t *)buf_ptr)[3]:'?',
769 td->name, ctx->phase, xml_tag);
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000770 break;
771 }
772
Lev Walkinc34dc462005-02-18 16:23:48 +0000773 ctx->phase = 5; /* Phase out, just in case */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000774 RETURN(RC_FAIL);
775}
776
777
Lev Walkina9cc46e2004-09-22 16:06:28 +0000778asn_enc_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -0700779CHOICE_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
780 enum xer_encoder_flags_e flags, asn_app_consume_bytes_f *cb,
781 void *app_key) {
782 const asn_CHOICE_specifics_t *specs =
783 (const asn_CHOICE_specifics_t *)td->specifics;
784 asn_enc_rval_t er;
Lev Walkin63a35232017-08-10 19:59:47 -0700785 unsigned present;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000786
787 if(!sptr)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700788 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000789
790 /*
791 * Figure out which CHOICE element is encoded.
792 */
793 present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
794
Lev Walkin63a35232017-08-10 19:59:47 -0700795 if(present == 0 || present > td->elements_count) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700796 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000797 } else {
798 asn_enc_rval_t tmper;
Lev Walkin5e033762004-09-29 13:26:15 +0000799 asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkin20696a42017-10-17 21:27:33 -0700800 const void *memb_ptr;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000801 const char *mname = elm->name;
802 unsigned int mlen = strlen(mname);
803
804 if(elm->flags & ATF_POINTER) {
Lev Walkin20696a42017-10-17 21:27:33 -0700805 memb_ptr =
806 *(const void *const *)((const char *)sptr + elm->memb_offset);
807 if(!memb_ptr) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000808 } else {
Lev Walkin20696a42017-10-17 21:27:33 -0700809 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
810 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000811
Lev Walkin20696a42017-10-17 21:27:33 -0700812 er.encoded = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000813
Lev Walkin4fe28822017-09-18 02:57:34 -0700814 if(!(flags & XER_F_CANONICAL)) ASN__TEXT_INDENT(1, ilevel);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700815 ASN__CALLBACK3("<", 1, mname, mlen, ">", 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000816
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800817 tmper = elm->type->op->xer_encoder(elm->type, memb_ptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000818 ilevel + 1, flags, cb, app_key);
819 if(tmper.encoded == -1) return tmper;
Lev Walkin4fe28822017-09-18 02:57:34 -0700820 er.encoded += tmper.encoded;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000821
Lev Walkin7c1dc052016-03-14 03:08:15 -0700822 ASN__CALLBACK3("</", 2, mname, mlen, ">", 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000823 }
824
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700825 if(!(flags & XER_F_CANONICAL)) ASN__TEXT_INDENT(1, ilevel - 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000826
Lev Walkin7c1dc052016-03-14 03:08:15 -0700827 ASN__ENCODED_OK(er);
Lev Walkin942fd082004-10-03 09:13:02 +0000828cb_failed:
Lev Walkin7c1dc052016-03-14 03:08:15 -0700829 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000830}
831
Lev Walkin59b176e2005-11-26 11:25:14 +0000832asn_dec_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -0700833CHOICE_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
834 const asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -0700835 const asn_per_constraints_t *constraints, void **sptr,
836 asn_per_data_t *pd) {
Lev Walkind84f6032017-10-03 16:33:59 -0700837 const asn_CHOICE_specifics_t *specs =
838 (const asn_CHOICE_specifics_t *)td->specifics;
839 asn_dec_rval_t rv;
Lev Walkin494fb702017-08-07 20:07:00 -0700840 const asn_per_constraint_t *ct;
Lev Walkin59b176e2005-11-26 11:25:14 +0000841 asn_TYPE_member_t *elm; /* CHOICE's element */
842 void *memb_ptr;
843 void **memb_ptr2;
844 void *st = *sptr;
845 int value;
846
Lev Walkin7c1dc052016-03-14 03:08:15 -0700847 if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx))
848 ASN__DECODE_FAILED;
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000849
Lev Walkin59b176e2005-11-26 11:25:14 +0000850 /*
851 * Create the target structure if it is not present already.
852 */
853 if(!st) {
854 st = *sptr = CALLOC(1, specs->struct_size);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700855 if(!st) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000856 }
857
858 if(constraints) ct = &constraints->value;
Lev Walkina5972be2017-09-29 23:15:58 -0700859 else if(td->encoding_constraints.per_constraints) ct = &td->encoding_constraints.per_constraints->value;
Lev Walkin59b176e2005-11-26 11:25:14 +0000860 else ct = 0;
861
862 if(ct && ct->flags & APC_EXTENSIBLE) {
863 value = per_get_few_bits(pd, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700864 if(value < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000865 if(value) ct = 0; /* Not restricted */
866 }
867
868 if(ct && ct->range_bits >= 0) {
869 value = per_get_few_bits(pd, ct->range_bits);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700870 if(value < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000871 ASN_DEBUG("CHOICE %s got index %d in range %d",
872 td->name, value, ct->range_bits);
Lev Walkin0a8aa602006-09-18 20:05:55 +0000873 if(value > ct->upper_bound)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700874 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000875 } else {
876 if(specs->ext_start == -1)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700877 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000878 value = uper_get_nsnnwn(pd);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700879 if(value < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000880 value += specs->ext_start;
Lev Walkin494fb702017-08-07 20:07:00 -0700881 if((unsigned)value >= td->elements_count)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700882 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000883 }
884
885 /* Adjust if canonical order is different from natural order */
Lev Walkin20696a42017-10-17 21:27:33 -0700886 if(specs->from_canonical_order) {
887 ASN_DEBUG("CHOICE presence from wire %d", value);
888 value = specs->from_canonical_order[value];
889 ASN_DEBUG("CHOICE presence index effective %d", value);
890 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000891
892 /* Set presence to be able to free it later */
893 _set_present_idx(st, specs->pres_offset, specs->pres_size, value + 1);
894
895 elm = &td->elements[value];
896 if(elm->flags & ATF_POINTER) {
897 /* Member is a pointer to another structure */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800898 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
Lev Walkin59b176e2005-11-26 11:25:14 +0000899 } else {
900 memb_ptr = (char *)st + elm->memb_offset;
901 memb_ptr2 = &memb_ptr;
902 }
903 ASN_DEBUG("Discovered CHOICE %s encodes %s", td->name, elm->name);
904
Lev Walkin6f9060f2007-06-29 01:45:03 +0000905 if(ct && ct->range_bits >= 0) {
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800906 rv = elm->type->op->uper_decoder(opt_codec_ctx, elm->type,
Lev Walkina5972be2017-09-29 23:15:58 -0700907 elm->encoding_constraints.per_constraints, memb_ptr2, pd);
Lev Walkin6f9060f2007-06-29 01:45:03 +0000908 } else {
909 rv = uper_open_type_get(opt_codec_ctx, elm->type,
Lev Walkina5972be2017-09-29 23:15:58 -0700910 elm->encoding_constraints.per_constraints, memb_ptr2, pd);
Lev Walkin6f9060f2007-06-29 01:45:03 +0000911 }
912
Lev Walkin59b176e2005-11-26 11:25:14 +0000913 if(rv.code != RC_OK)
Lev Walkin6f9060f2007-06-29 01:45:03 +0000914 ASN_DEBUG("Failed to decode %s in %s (CHOICE) %d",
915 elm->name, td->name, rv.code);
Lev Walkin59b176e2005-11-26 11:25:14 +0000916 return rv;
917}
Lev Walkin494fb702017-08-07 20:07:00 -0700918
Lev Walkin523de9e2006-08-18 01:34:18 +0000919asn_enc_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -0700920CHOICE_encode_uper(const asn_TYPE_descriptor_t *td,
921 const asn_per_constraints_t *constraints, const void *sptr,
Lev Walkin494fb702017-08-07 20:07:00 -0700922 asn_per_outp_t *po) {
Lev Walkin20696a42017-10-17 21:27:33 -0700923 const asn_CHOICE_specifics_t *specs = (const asn_CHOICE_specifics_t *)td->specifics;
Lev Walkin523de9e2006-08-18 01:34:18 +0000924 asn_TYPE_member_t *elm; /* CHOICE's element */
Lev Walkin494fb702017-08-07 20:07:00 -0700925 const asn_per_constraint_t *ct;
Lev Walkin20696a42017-10-17 21:27:33 -0700926 const void *memb_ptr;
Lev Walkin63a35232017-08-10 19:59:47 -0700927 unsigned present;
Lev Walkine14480f2013-03-24 03:28:00 -0700928 int present_enc;
Lev Walkin523de9e2006-08-18 01:34:18 +0000929
Lev Walkin7c1dc052016-03-14 03:08:15 -0700930 if(!sptr) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000931
932 ASN_DEBUG("Encoding %s as CHOICE", td->name);
933
934 if(constraints) ct = &constraints->value;
Lev Walkina5972be2017-09-29 23:15:58 -0700935 else if(td->encoding_constraints.per_constraints)
936 ct = &td->encoding_constraints.per_constraints->value;
Lev Walkin523de9e2006-08-18 01:34:18 +0000937 else ct = 0;
938
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800939 present = _fetch_present_idx(sptr, specs->pres_offset, specs->pres_size);
Lev Walkin523de9e2006-08-18 01:34:18 +0000940
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800941 /*
Lev Walkin523de9e2006-08-18 01:34:18 +0000942 * If the structure was not initialized properly, it cannot be encoded:
943 * can't deduce what to encode in the choice type.
944 */
Lev Walkin63a35232017-08-10 19:59:47 -0700945 if(present == 0 || present > td->elements_count)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700946 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000947 else
948 present--;
949
Lev Walkin523de9e2006-08-18 01:34:18 +0000950 ASN_DEBUG("Encoding %s CHOICE element %d", td->name, present);
951
Lev Walkin20696a42017-10-17 21:27:33 -0700952 /* Adjust if canonical order is different from natural order */
953 if(specs->to_canonical_order)
954 present_enc = specs->to_canonical_order[present];
955 else
956 present_enc = present;
Lev Walkine14480f2013-03-24 03:28:00 -0700957
Lev Walkin20696a42017-10-17 21:27:33 -0700958 if(ct && ct->range_bits >= 0) {
Lev Walkine14480f2013-03-24 03:28:00 -0700959 if(present_enc < ct->lower_bound
960 || present_enc > ct->upper_bound) {
Lev Walkin523de9e2006-08-18 01:34:18 +0000961 if(ct->flags & APC_EXTENSIBLE) {
Lev Walkin20696a42017-10-17 21:27:33 -0700962 ASN_DEBUG(
963 "CHOICE member %d (enc %d) is an extension (%ld..%ld)",
964 present, present_enc, ct->lower_bound, ct->upper_bound);
965 if(per_put_few_bits(po, 1, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700966 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000967 } else {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700968 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000969 }
970 ct = 0;
971 }
972 }
Lev Walkin20696a42017-10-17 21:27:33 -0700973 if(ct && ct->flags & APC_EXTENSIBLE) {
974 ASN_DEBUG("CHOICE member %d (enc %d) is not an extension (%ld..%ld)",
975 present, present_enc, ct->lower_bound, ct->upper_bound);
976 if(per_put_few_bits(po, 0, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700977 ASN__ENCODE_FAILED;
Lev Walkin20696a42017-10-17 21:27:33 -0700978 }
979
Lev Walkin523de9e2006-08-18 01:34:18 +0000980
Lev Walkin523de9e2006-08-18 01:34:18 +0000981 elm = &td->elements[present];
Lev Walkin20696a42017-10-17 21:27:33 -0700982 ASN_DEBUG("CHOICE member \"%s\" %d (as %d)", elm->name, present,
983 present_enc);
984 if(elm->flags & ATF_POINTER) {
Lev Walkin523de9e2006-08-18 01:34:18 +0000985 /* Member is a pointer to another structure */
Lev Walkin20696a42017-10-17 21:27:33 -0700986 memb_ptr =
987 *(const void *const *)((const char *)sptr + elm->memb_offset);
988 if(!memb_ptr) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000989 } else {
Lev Walkin20696a42017-10-17 21:27:33 -0700990 memb_ptr = (const char *)sptr + elm->memb_offset;
991 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000992
Lev Walkin20696a42017-10-17 21:27:33 -0700993 if(ct && ct->range_bits >= 0) {
994 if(per_put_few_bits(po, present_enc, ct->range_bits))
995 ASN__ENCODE_FAILED;
Lev Walkin6f9060f2007-06-29 01:45:03 +0000996
Lev Walkin20696a42017-10-17 21:27:33 -0700997 return elm->type->op->uper_encoder(
998 elm->type, elm->encoding_constraints.per_constraints, memb_ptr, po);
999 } else {
1000 asn_enc_rval_t rval;
1001 if(specs->ext_start == -1) ASN__ENCODE_FAILED;
1002 if(uper_put_nsnnwn(po, present_enc - specs->ext_start))
1003 ASN__ENCODE_FAILED;
1004 if(uper_open_type_put(elm->type,
1005 elm->encoding_constraints.per_constraints,
1006 memb_ptr, po))
1007 ASN__ENCODE_FAILED;
1008 rval.encoded = 0;
1009 ASN__ENCODED_OK(rval);
1010 }
Lev Walkin523de9e2006-08-18 01:34:18 +00001011}
Lev Walkin20696a42017-10-17 21:27:33 -07001012
Lev Walkin59b176e2005-11-26 11:25:14 +00001013
Lev Walkinf15320b2004-06-03 03:38:44 +00001014int
Lev Walkin20696a42017-10-17 21:27:33 -07001015CHOICE_print(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
1016 asn_app_consume_bytes_f *cb, void *app_key) {
1017 const asn_CHOICE_specifics_t *specs = (const asn_CHOICE_specifics_t *)td->specifics;
Lev Walkin63a35232017-08-10 19:59:47 -07001018 unsigned present;
Lev Walkinf15320b2004-06-03 03:38:44 +00001019
Lev Walkin8e8078a2004-09-26 13:10:40 +00001020 if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001021
1022 /*
1023 * Figure out which CHOICE element is encoded.
1024 */
1025 present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
1026
1027 /*
Lev Walkina9cc46e2004-09-22 16:06:28 +00001028 * Print that element.
Lev Walkinf15320b2004-06-03 03:38:44 +00001029 */
Lev Walkin63a35232017-08-10 19:59:47 -07001030 if(present > 0 && present <= td->elements_count) {
Lev Walkin5e033762004-09-29 13:26:15 +00001031 asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkinf15320b2004-06-03 03:38:44 +00001032 const void *memb_ptr;
1033
Lev Walkincc93b0f2004-09-10 09:18:20 +00001034 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001035 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkin8e8078a2004-09-26 13:10:40 +00001036 if(!memb_ptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001037 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001038 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +00001039 }
1040
1041 /* Print member's name and stuff */
Lev Walkincc6a9102004-09-23 22:06:26 +00001042 if(0) {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001043 if(cb(elm->name, strlen(elm->name), app_key) < 0
1044 || cb(": ", 2, app_key) < 0)
Lev Walkincc6a9102004-09-23 22:06:26 +00001045 return -1;
1046 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001047
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001048 return elm->type->op->print_struct(elm->type, memb_ptr, ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +00001049 cb, app_key);
1050 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001051 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001052 }
1053}
1054
1055void
Lev Walkin8d99d7b2017-08-25 01:06:00 -07001056CHOICE_free(const asn_TYPE_descriptor_t *td, void *ptr,
1057 enum asn_struct_free_method method) {
Lev Walkind84f6032017-10-03 16:33:59 -07001058 const asn_CHOICE_specifics_t *specs =
1059 (const asn_CHOICE_specifics_t *)td->specifics;
1060 unsigned present;
Lev Walkinf15320b2004-06-03 03:38:44 +00001061
1062 if(!td || !ptr)
1063 return;
1064
1065 ASN_DEBUG("Freeing %s as CHOICE", td->name);
1066
1067 /*
1068 * Figure out which CHOICE element is encoded.
1069 */
1070 present = _fetch_present_idx(ptr, specs->pres_offset, specs->pres_size);
1071
1072 /*
1073 * Free that element.
1074 */
Lev Walkin63a35232017-08-10 19:59:47 -07001075 if(present > 0 && present <= td->elements_count) {
Lev Walkin5e033762004-09-29 13:26:15 +00001076 asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkinf15320b2004-06-03 03:38:44 +00001077 void *memb_ptr;
1078
Lev Walkincc93b0f2004-09-10 09:18:20 +00001079 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001080 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +00001081 if(memb_ptr)
Lev Walkinadcb5862006-03-17 02:11:12 +00001082 ASN_STRUCT_FREE(*elm->type, memb_ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +00001083 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001084 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
Lev Walkinadcb5862006-03-17 02:11:12 +00001085 ASN_STRUCT_FREE_CONTENTS_ONLY(*elm->type, memb_ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +00001086 }
1087 }
1088
Lev Walkin8d99d7b2017-08-25 01:06:00 -07001089 switch(method) {
1090 case ASFM_FREE_EVERYTHING:
1091 FREEMEM(ptr);
1092 break;
1093 case ASFM_FREE_UNDERLYING:
1094 break;
1095 case ASFM_FREE_UNDERLYING_AND_RESET:
1096 memset(ptr, 0, specs->struct_size);
1097 break;
1098 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001099}
1100
1101
1102/*
1103 * The following functions functions offer protection against -fshort-enums,
1104 * compatible with little- and big-endian machines.
1105 * If assertion is triggered, either disable -fshort-enums, or add an entry
1106 * here with the ->pres_size of your target stracture.
1107 * Unless the target structure is packed, the ".present" member
1108 * is guaranteed to be aligned properly. ASN.1 compiler itself does not
1109 * produce packed code.
1110 */
Lev Walkinf6853ce2017-08-11 00:50:27 -07001111static unsigned
1112_fetch_present_idx(const void *struct_ptr, unsigned pres_offset,
1113 unsigned pres_size) {
1114 const void *present_ptr;
Lev Walkin63a35232017-08-10 19:59:47 -07001115 unsigned present;
Lev Walkinf15320b2004-06-03 03:38:44 +00001116
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001117 present_ptr = ((const char *)struct_ptr) + pres_offset;
Lev Walkinf15320b2004-06-03 03:38:44 +00001118
1119 switch(pres_size) {
Lev Walkinf6853ce2017-08-11 00:50:27 -07001120 case sizeof(int): present = *(const unsigned int *)present_ptr; break;
1121 case sizeof(short): present = *(const unsigned short *)present_ptr; break;
1122 case sizeof(char): present = *(const unsigned char *)present_ptr; break;
Lev Walkinf15320b2004-06-03 03:38:44 +00001123 default:
1124 /* ANSI C mandates enum to be equivalent to integer */
1125 assert(pres_size != sizeof(int));
1126 return 0; /* If not aborted, pass back safe value */
1127 }
1128
1129 return present;
1130}
1131
Lev Walkin91f5cd02004-08-11 09:10:59 +00001132static void
Lev Walkinf6853ce2017-08-11 00:50:27 -07001133_set_present_idx(void *struct_ptr, unsigned pres_offset, unsigned pres_size,
1134 unsigned present) {
1135 void *present_ptr;
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001136 present_ptr = ((char *)struct_ptr) + pres_offset;
Lev Walkinf15320b2004-06-03 03:38:44 +00001137
1138 switch(pres_size) {
Lev Walkinf6853ce2017-08-11 00:50:27 -07001139 case sizeof(int): *(unsigned int *)present_ptr = present; break;
1140 case sizeof(short): *(unsigned short *)present_ptr = present; break;
1141 case sizeof(char): *(unsigned char *)present_ptr = present; break;
Lev Walkinf15320b2004-06-03 03:38:44 +00001142 default:
1143 /* ANSI C mandates enum to be equivalent to integer */
1144 assert(pres_size != sizeof(int));
1145 }
1146}
Lev Walkincd2f48e2017-08-10 02:14:59 -07001147
1148static const void *
1149_get_member_ptr(const asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin63a35232017-08-10 19:59:47 -07001150 asn_TYPE_member_t **elm_ptr, unsigned *present_out) {
Lev Walkind84f6032017-10-03 16:33:59 -07001151 const asn_CHOICE_specifics_t *specs =
1152 (const asn_CHOICE_specifics_t *)td->specifics;
Lev Walkin63a35232017-08-10 19:59:47 -07001153 unsigned present;
Lev Walkincd2f48e2017-08-10 02:14:59 -07001154
1155 if(!sptr) {
1156 *elm_ptr = NULL;
1157 *present_out = 0;
1158 return NULL;
1159 }
1160
1161 /*
1162 * Figure out which CHOICE element is encoded.
1163 */
1164 present = _fetch_present_idx(sptr, specs->pres_offset, specs->pres_size);
1165 *present_out = present;
1166
1167 /*
1168 * The presence index is intentionally 1-based to avoid
1169 * treating zeroed structure as a valid one.
1170 */
Lev Walkin63a35232017-08-10 19:59:47 -07001171 if(present > 0 && present <= td->elements_count) {
Lev Walkincd2f48e2017-08-10 02:14:59 -07001172 asn_TYPE_member_t *const elm = &td->elements[present - 1];
1173 const void *memb_ptr;
1174
1175 if(elm->flags & ATF_POINTER) {
1176 memb_ptr =
1177 *(const void *const *)((const char *)sptr + elm->memb_offset);
1178 } else {
1179 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
1180 }
1181 *elm_ptr = elm;
1182 return memb_ptr;
1183 } else {
1184 *elm_ptr = NULL;
1185 return NULL;
1186 }
1187
1188}
1189
1190int
1191CHOICE_compare(const asn_TYPE_descriptor_t *td, const void *aptr, const void *bptr) {
1192 asn_TYPE_member_t *aelm;
1193 asn_TYPE_member_t *belm;
Lev Walkin63a35232017-08-10 19:59:47 -07001194 unsigned apresent = 0;
1195 unsigned bpresent = 0;
Lev Walkincd2f48e2017-08-10 02:14:59 -07001196 const void *amember = _get_member_ptr(td, aptr, &aelm, &apresent);
Lev Walkin7193cf02017-10-17 14:10:35 -07001197 const void *bmember = _get_member_ptr(td, bptr, &belm, &bpresent);
Lev Walkincd2f48e2017-08-10 02:14:59 -07001198
1199 if(amember && bmember) {
1200 if(apresent == bpresent) {
1201 assert(aelm == belm);
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001202 return aelm->type->op->compare_struct(aelm->type, amember, bmember);
Lev Walkincd2f48e2017-08-10 02:14:59 -07001203 } else if(apresent < bpresent) {
1204 return -1;
1205 } else {
1206 return 1;
1207 }
1208 } else if(!amember) {
1209 return -1;
1210 } else {
1211 return 1;
1212 }
1213}
Lev Walkinf6853ce2017-08-11 00:50:27 -07001214
1215/*
1216 * Return the 1-based choice variant presence index.
1217 * Returns 0 in case of error.
1218 */
1219unsigned
1220CHOICE_variant_get_presence(const asn_TYPE_descriptor_t *td, const void *sptr) {
Lev Walkind84f6032017-10-03 16:33:59 -07001221 const asn_CHOICE_specifics_t *specs =
1222 (const asn_CHOICE_specifics_t *)td->specifics;
Lev Walkinf6853ce2017-08-11 00:50:27 -07001223 return _fetch_present_idx(sptr, specs->pres_offset, specs->pres_size);
1224}
1225
1226/*
1227 * Sets or resets the 1-based choice variant presence index.
1228 * In case a previous index is not zero, the currently selected structure
1229 * member is freed and zeroed-out first.
1230 * Returns 0 on success and -1 on error.
1231 */
1232int
1233CHOICE_variant_set_presence(const asn_TYPE_descriptor_t *td, void *sptr,
1234 unsigned present) {
Lev Walkind84f6032017-10-03 16:33:59 -07001235 const asn_CHOICE_specifics_t *specs =
1236 (const asn_CHOICE_specifics_t *)td->specifics;
Lev Walkinf6853ce2017-08-11 00:50:27 -07001237 unsigned old_present;
1238
1239 if(!sptr) {
1240 return -1;
1241 }
1242
1243 if(present > td->elements_count)
1244 return -1;
1245
1246 old_present =
1247 _fetch_present_idx(sptr, specs->pres_offset, specs->pres_size);
1248 if(present == old_present)
1249 return 0;
1250
Lev Walkin91ca2732017-08-11 02:32:42 -07001251 if(old_present != 0) {
Lev Walkinf6853ce2017-08-11 00:50:27 -07001252 assert(old_present <= td->elements_count);
Lev Walkin8d99d7b2017-08-25 01:06:00 -07001253 ASN_STRUCT_RESET(*td, sptr);
Lev Walkinf6853ce2017-08-11 00:50:27 -07001254 }
1255
1256 _set_present_idx(sptr, specs->pres_offset, specs->pres_size, present);
1257
1258 return 0;
1259}
1260
Lev Walkina5972be2017-09-29 23:15:58 -07001261
1262asn_random_fill_result_t
1263CHOICE_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
1264 const asn_encoding_constraints_t *constr,
1265 size_t max_length) {
1266 const asn_CHOICE_specifics_t *specs =
1267 (const asn_CHOICE_specifics_t *)td->specifics;
1268 asn_random_fill_result_t res;
1269 asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
1270 asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
1271 const asn_TYPE_member_t *elm;
1272 unsigned present;
1273 void *memb_ptr; /* Pointer to the member */
1274 void **memb_ptr2; /* Pointer to that pointer */
1275 void *st = *sptr;
1276
1277 if(max_length == 0) return result_skipped;
1278
1279 (void)constr;
1280
1281 if(st == NULL) {
1282 st = CALLOC(1, specs->struct_size);
1283 if(st == NULL) {
1284 return result_failed;
1285 }
1286 }
1287
1288 present = asn_random_between(1, td->elements_count);
1289 elm = &td->elements[present - 1];
1290
1291 if(elm->flags & ATF_POINTER) {
1292 /* Member is a pointer to another structure */
1293 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
1294 } else {
1295 memb_ptr = (char *)st + elm->memb_offset;
1296 memb_ptr2 = &memb_ptr;
1297 }
1298
1299 res = elm->type->op->random_fill(elm->type, memb_ptr2,
1300 &elm->encoding_constraints, max_length);
1301 _set_present_idx(st, specs->pres_offset, specs->pres_size, present);
1302 if(res.code == ARFILL_OK) {
1303 *sptr = st;
1304 } else {
1305 if(st == *sptr) {
1306 ASN_STRUCT_RESET(*td, st);
1307 } else {
1308 ASN_STRUCT_FREE(*td, st);
1309 }
1310 }
1311
1312 return res;
1313}
1314
1315
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001316asn_TYPE_operation_t asn_OP_CHOICE = {
1317 CHOICE_free,
1318 CHOICE_print,
1319 CHOICE_compare,
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001320 CHOICE_decode_ber,
1321 CHOICE_encode_der,
1322 CHOICE_decode_xer,
1323 CHOICE_encode_xer,
1324#ifdef ASN_DISABLE_OER_SUPPORT
1325 0,
1326 0,
1327#else
Lev Walkin9a1736d2017-08-27 23:46:34 -07001328 CHOICE_decode_oer,
1329 CHOICE_encode_oer,
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001330#endif /* ASN_DISABLE_OER_SUPPORT */
1331#ifdef ASN_DISABLE_PER_SUPPORT
1332 0,
1333 0,
1334#else
1335 CHOICE_decode_uper,
1336 CHOICE_encode_uper,
1337#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkina5972be2017-09-29 23:15:58 -07001338 CHOICE_random_fill,
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001339 CHOICE_outmost_tag
1340};