blob: f3a53d36ae7421b941237b05111209eaceea275f [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 Walkin5e033762004-09-29 13:26:15 +0000111 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
112 asn_TYPE_member_t *elements = td->elements;
Lev Walkinf15320b2004-06-03 03:38:44 +0000113
114 /*
115 * Parts of the structure being constructed.
116 */
117 void *st = *struct_ptr; /* Target structure. */
Lev Walkin5e033762004-09-29 13:26:15 +0000118 asn_struct_ctx_t *ctx; /* Decoder context */
Lev Walkinf15320b2004-06-03 03:38:44 +0000119
120 ber_tlv_tag_t tlv_tag; /* T from TLV */
121 ssize_t tag_len; /* Length of TLV's T */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000122 asn_dec_rval_t rval; /* Return code from subparsers */
Lev Walkinf15320b2004-06-03 03:38:44 +0000123
124 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
125
Lev Walkin449f8322004-08-20 13:23:42 +0000126 ASN_DEBUG("Decoding %s as CHOICE", td->name);
Lev Walkin59b176e2005-11-26 11:25:14 +0000127
Lev Walkinf15320b2004-06-03 03:38:44 +0000128 /*
129 * Create the target structure if it is not present already.
130 */
131 if(st == 0) {
132 st = *struct_ptr = CALLOC(1, specs->struct_size);
133 if(st == 0) {
134 RETURN(RC_FAIL);
135 }
136 }
137
138 /*
139 * Restore parsing context.
140 */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800141 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000142
143 /*
144 * Start to parse where left previously
145 */
146 switch(ctx->phase) {
147 case 0:
148 /*
149 * PHASE 0.
150 * Check that the set of tags associated with given structure
151 * perfectly fits our expectations.
152 */
153
Lev Walkin449f8322004-08-20 13:23:42 +0000154 if(tag_mode || td->tags_count) {
Lev Walkin5e033762004-09-29 13:26:15 +0000155 rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size,
Lev Walkin8e8078a2004-09-26 13:10:40 +0000156 tag_mode, -1, &ctx->left, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000157 if(rval.code != RC_OK) {
158 ASN_DEBUG("%s tagging check failed: %d",
Lev Walkin449f8322004-08-20 13:23:42 +0000159 td->name, rval.code);
Lev Walkinf0f04d12004-10-05 06:36:02 +0000160 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000161 }
162
163 if(ctx->left >= 0) {
164 /* ?Substracted below! */
165 ctx->left += rval.consumed;
166 }
167 ADVANCE(rval.consumed);
168 } else {
169 ctx->left = -1;
170 }
171
172 NEXT_PHASE(ctx);
173
174 ASN_DEBUG("Structure consumes %ld bytes, buffer %ld",
175 (long)ctx->left, (long)size);
176
177 /* Fall through */
178 case 1:
179 /*
180 * Fetch the T from TLV.
181 */
182 tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
Lev Walkin449f8322004-08-20 13:23:42 +0000183 ASN_DEBUG("In %s CHOICE tag length %d", td->name, (int)tag_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000184 switch(tag_len) {
185 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
186 /* Fall through */
187 case -1: RETURN(RC_FAIL);
188 }
189
190 do {
Wim Lewisfb6344e2014-07-28 12:16:01 -0700191 const asn_TYPE_tag2member_t *t2m;
Lev Walkin5e033762004-09-29 13:26:15 +0000192 asn_TYPE_tag2member_t key;
Lev Walkinf15320b2004-06-03 03:38:44 +0000193
194 key.el_tag = tlv_tag;
Wim Lewisfb6344e2014-07-28 12:16:01 -0700195 t2m = (const asn_TYPE_tag2member_t *)bsearch(&key,
Lev Walkinc2346572004-08-11 09:07:36 +0000196 specs->tag2el, specs->tag2el_count,
Lev Walkinf15320b2004-06-03 03:38:44 +0000197 sizeof(specs->tag2el[0]), _search4tag);
198 if(t2m) {
199 /*
200 * Found the element corresponding to the tag.
201 */
202 NEXT_PHASE(ctx);
203 ctx->step = t2m->el_no;
204 break;
Lev Walkin59b176e2005-11-26 11:25:14 +0000205 } else if(specs->ext_start == -1) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000206 ASN_DEBUG("Unexpected tag %s "
207 "in non-extensible CHOICE %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000208 ber_tlv_tag_string(tlv_tag), td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000209 RETURN(RC_FAIL);
210 } else {
211 /* Skip this tag */
212 ssize_t skip;
213
214 ASN_DEBUG("Skipping unknown tag %s",
215 ber_tlv_tag_string(tlv_tag));
216
Lev Walkinbaaa24f2004-09-29 14:19:14 +0000217 skip = ber_skip_length(opt_codec_ctx,
Lev Walkinf15320b2004-06-03 03:38:44 +0000218 BER_TLV_CONSTRUCTED(ptr),
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800219 (const char *)ptr + tag_len,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000220 LEFT - tag_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000221
222 switch(skip) {
223 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
224 /* Fall through */
225 case -1: RETURN(RC_FAIL);
226 }
227
228 ADVANCE(skip + tag_len);
229 RETURN(RC_OK);
230 }
231 } while(0);
232
233 case 2:
234 /*
235 * PHASE 2.
236 * Read in the element.
237 */
238 do {
Lev Walkin5e033762004-09-29 13:26:15 +0000239 asn_TYPE_member_t *elm;/* CHOICE's element */
Lev Walkinf15320b2004-06-03 03:38:44 +0000240 void *memb_ptr; /* Pointer to the member */
Lev Walkinc2346572004-08-11 09:07:36 +0000241 void **memb_ptr2; /* Pointer to that pointer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000242
243 elm = &elements[ctx->step];
244
245 /*
246 * Compute the position of the member inside a structure,
247 * and also a type of containment (it may be contained
248 * as pointer or using inline inclusion).
249 */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000250 if(elm->flags & ATF_POINTER) {
251 /* Member is a pointer to another structure */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800252 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000253 } else {
254 /*
255 * A pointer to a pointer
256 * holding the start of the structure
257 */
258 memb_ptr = (char *)st + elm->memb_offset;
259 memb_ptr2 = &memb_ptr;
260 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000261 /* Set presence to be able to free it properly at any time */
262 _set_present_idx(st, specs->pres_offset,
263 specs->pres_size, ctx->step + 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000264 /*
265 * Invoke the member fetch routine according to member's type
266 */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800267 rval = elm->type->op->ber_decoder(opt_codec_ctx, elm->type,
Lev Walkin59b176e2005-11-26 11:25:14 +0000268 memb_ptr2, ptr, LEFT, elm->tag_mode);
Lev Walkinf15320b2004-06-03 03:38:44 +0000269 switch(rval.code) {
270 case RC_OK:
Lev Walkinf15320b2004-06-03 03:38:44 +0000271 break;
272 case RC_WMORE: /* More data expected */
273 if(!SIZE_VIOLATION) {
274 ADVANCE(rval.consumed);
275 RETURN(RC_WMORE);
276 }
277 RETURN(RC_FAIL);
278 case RC_FAIL: /* Fatal error */
279 RETURN(rval.code);
280 } /* switch(rval) */
281
282 ADVANCE(rval.consumed);
283 } while(0);
284
285 NEXT_PHASE(ctx);
286
287 /* Fall through */
288 case 3:
289 ASN_DEBUG("CHOICE %s Leftover: %ld, size = %ld, tm=%d, tc=%d",
Lev Walkin449f8322004-08-20 13:23:42 +0000290 td->name, (long)ctx->left, (long)size,
291 tag_mode, td->tags_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000292
293 if(ctx->left > 0) {
294 /*
295 * The type must be fully decoded
296 * by the CHOICE member-specific decoder.
297 */
298 RETURN(RC_FAIL);
299 }
300
301 if(ctx->left == -1
Lev Walkin449f8322004-08-20 13:23:42 +0000302 && !(tag_mode || td->tags_count)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000303 /*
304 * This is an untagged CHOICE.
305 * It doesn't contain nothing
306 * except for the member itself, including all its tags.
307 * The decoding is completed.
308 */
309 NEXT_PHASE(ctx);
310 break;
311 }
312
313 /*
314 * Read in the "end of data chunks"'s.
315 */
316 while(ctx->left < 0) {
317 ssize_t tl;
318
319 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
320 switch(tl) {
321 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
322 /* Fall through */
323 case -1: RETURN(RC_FAIL);
324 }
325
326 /*
327 * Expected <0><0>...
328 */
Lev Walkin8c3b8542005-03-10 18:52:02 +0000329 if(((const uint8_t *)ptr)[0] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000330 if(LEFT < 2) {
331 if(SIZE_VIOLATION)
332 RETURN(RC_FAIL);
333 else
334 RETURN(RC_WMORE);
Lev Walkin8c3b8542005-03-10 18:52:02 +0000335 } else if(((const uint8_t *)ptr)[1] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000336 /*
337 * Correctly finished with <0><0>.
338 */
Lev Walkinf0f04d12004-10-05 06:36:02 +0000339 ADVANCE(2);
340 ctx->left++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000341 continue;
342 }
343 } else {
344 ASN_DEBUG("Unexpected continuation in %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000345 td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000346 RETURN(RC_FAIL);
347 }
348
Lev Walkinf0f04d12004-10-05 06:36:02 +0000349 /* UNREACHABLE */
Lev Walkinf15320b2004-06-03 03:38:44 +0000350 }
351
352 NEXT_PHASE(ctx);
353 case 4:
354 /* No meaningful work here */
355 break;
356 }
357
358 RETURN(RC_OK);
359}
360
Lev Walkina9cc46e2004-09-22 16:06:28 +0000361asn_enc_rval_t
Lev Walkinac589332005-08-22 14:19:28 +0000362CHOICE_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000363 int tag_mode, ber_tlv_tag_t tag,
364 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin5e033762004-09-29 13:26:15 +0000365 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
366 asn_TYPE_member_t *elm; /* CHOICE element */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000367 asn_enc_rval_t erval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000368 void *memb_ptr;
369 size_t computed_size = 0;
Lev Walkin63a35232017-08-10 19:59:47 -0700370 unsigned present;
Lev Walkinf15320b2004-06-03 03:38:44 +0000371
Lev Walkin7c1dc052016-03-14 03:08:15 -0700372 if(!sptr) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000373
Lev Walkinf15320b2004-06-03 03:38:44 +0000374 ASN_DEBUG("%s %s as CHOICE",
Lev Walkin449f8322004-08-20 13:23:42 +0000375 cb?"Encoding":"Estimating", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000376
Lev Walkinac589332005-08-22 14:19:28 +0000377 present = _fetch_present_idx(sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000378 specs->pres_offset, specs->pres_size);
379
380 /*
381 * If the structure was not initialized, it cannot be encoded:
382 * can't deduce what to encode in the choice type.
383 */
Lev Walkin63a35232017-08-10 19:59:47 -0700384 if(present == 0 || present > td->elements_count) {
Lev Walkin449f8322004-08-20 13:23:42 +0000385 if(present == 0 && td->elements_count == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000386 /* The CHOICE is empty?! */
387 erval.encoded = 0;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700388 ASN__ENCODED_OK(erval);
Lev Walkinf15320b2004-06-03 03:38:44 +0000389 }
Lev Walkin7c1dc052016-03-14 03:08:15 -0700390 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000391 }
392
393 /*
394 * Seek over the present member of the structure.
395 */
Lev Walkin449f8322004-08-20 13:23:42 +0000396 elm = &td->elements[present-1];
Lev Walkincc93b0f2004-09-10 09:18:20 +0000397 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800398 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000399 if(memb_ptr == 0) {
Lev Walkincc93b0f2004-09-10 09:18:20 +0000400 if(elm->optional) {
401 erval.encoded = 0;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700402 ASN__ENCODED_OK(erval);
Lev Walkincc93b0f2004-09-10 09:18:20 +0000403 }
Lev Walkinac589332005-08-22 14:19:28 +0000404 /* Mandatory element absent */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700405 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000406 }
407 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800408 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000409 }
410
411 /*
412 * If the CHOICE itself is tagged EXPLICIT:
413 * T ::= [2] EXPLICIT CHOICE { ... }
414 * Then emit the appropriate tags.
415 */
Lev Walkin449f8322004-08-20 13:23:42 +0000416 if(tag_mode == 1 || td->tags_count) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000417 /*
418 * For this, we need to pre-compute the member.
419 */
420 ssize_t ret;
421
422 /* Encode member with its tag */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800423 erval = elm->type->op->der_encoder(elm->type, memb_ptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000424 elm->tag_mode, elm->tag, 0, 0);
425 if(erval.encoded == -1)
426 return erval;
427
428 /* Encode CHOICE with parent or my own tag */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000429 ret = der_write_tags(td, erval.encoded, tag_mode, 1, tag,
Lev Walkinf15320b2004-06-03 03:38:44 +0000430 cb, app_key);
Lev Walkinac589332005-08-22 14:19:28 +0000431 if(ret == -1)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700432 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000433 computed_size += ret;
434 }
435
436 /*
437 * Encode the single underlying member.
438 */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800439 erval = elm->type->op->der_encoder(elm->type, memb_ptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000440 elm->tag_mode, elm->tag, cb, app_key);
441 if(erval.encoded == -1)
442 return erval;
443
444 ASN_DEBUG("Encoded CHOICE member in %ld bytes (+%ld)",
445 (long)erval.encoded, (long)computed_size);
446
447 erval.encoded += computed_size;
448
449 return erval;
450}
451
452ber_tlv_tag_t
Wim Lewis14e6b162014-07-23 16:06:01 -0700453CHOICE_outmost_tag(const asn_TYPE_descriptor_t *td, const void *ptr, int tag_mode, ber_tlv_tag_t tag) {
Lev Walkin5e033762004-09-29 13:26:15 +0000454 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
Lev Walkin63a35232017-08-10 19:59:47 -0700455 unsigned present;
Lev Walkinf15320b2004-06-03 03:38:44 +0000456
Lev Walkin32788732005-11-13 09:12:23 +0000457 assert(tag_mode == 0); (void)tag_mode;
458 assert(tag == 0); (void)tag;
Lev Walkinf15320b2004-06-03 03:38:44 +0000459
460 /*
461 * Figure out which CHOICE element is encoded.
462 */
463 present = _fetch_present_idx(ptr, specs->pres_offset, specs->pres_size);
464
Lev Walkin63a35232017-08-10 19:59:47 -0700465 if(present > 0 && present <= td->elements_count) {
Wim Lewis14e6b162014-07-23 16:06:01 -0700466 const asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkin70e70282004-06-05 01:45:48 +0000467 const void *memb_ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000468
Lev Walkincc93b0f2004-09-10 09:18:20 +0000469 if(elm->flags & ATF_POINTER) {
Lev Walkin70e70282004-06-05 01:45:48 +0000470 memb_ptr = *(const void * const *)
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800471 ((const char *)ptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000472 } else {
Lev Walkin70e70282004-06-05 01:45:48 +0000473 memb_ptr = (const void *)
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800474 ((const char *)ptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000475 }
476
Lev Walkin5e033762004-09-29 13:26:15 +0000477 return asn_TYPE_outmost_tag(elm->type, memb_ptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000478 elm->tag_mode, elm->tag);
479 } else {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000480 return (ber_tlv_tag_t)-1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000481 }
482}
483
484int
Lev Walkin5e033762004-09-29 13:26:15 +0000485CHOICE_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +0000486 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkin5e033762004-09-29 13:26:15 +0000487 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
Lev Walkin63a35232017-08-10 19:59:47 -0700488 unsigned present;
Lev Walkinf15320b2004-06-03 03:38:44 +0000489
490 if(!sptr) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700491 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +0000492 "%s: value not given (%s:%d)",
493 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000494 return -1;
495 }
496
497 /*
498 * Figure out which CHOICE element is encoded.
499 */
500 present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
Lev Walkin63a35232017-08-10 19:59:47 -0700501 if(present > 0 && present <= td->elements_count) {
Lev Walkin5e033762004-09-29 13:26:15 +0000502 asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkinf15320b2004-06-03 03:38:44 +0000503 const void *memb_ptr;
504
Lev Walkincc93b0f2004-09-10 09:18:20 +0000505 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800506 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkincc93b0f2004-09-10 09:18:20 +0000507 if(!memb_ptr) {
508 if(elm->optional)
509 return 0;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700510 ASN__CTFAIL(app_key, td, sptr,
Lev Walkincc93b0f2004-09-10 09:18:20 +0000511 "%s: mandatory CHOICE element %s absent (%s:%d)",
512 td->name, elm->name, __FILE__, __LINE__);
513 return -1;
514 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000515 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800516 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000517 }
518
Lev Walkin449f8322004-08-20 13:23:42 +0000519 if(elm->memb_constraints) {
520 return elm->memb_constraints(elm->type, memb_ptr,
Lev Walkin1eded352006-07-13 11:19:01 +0000521 ctfailcb, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +0000522 } else {
523 int ret = elm->type->check_constraints(elm->type,
Lev Walkin1eded352006-07-13 11:19:01 +0000524 memb_ptr, ctfailcb, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +0000525 /*
526 * Cannot inherit it eralier:
527 * need to make sure we get the updated version.
528 */
529 elm->memb_constraints = elm->type->check_constraints;
530 return ret;
531 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000532 } else {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700533 ASN__CTFAIL(app_key, td, sptr,
Lev Walkind34a8512004-08-22 13:55:49 +0000534 "%s: no CHOICE element given (%s:%d)",
Lev Walkin16835b62004-08-22 13:47:59 +0000535 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000536 return -1;
537 }
538}
539
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000540#undef XER_ADVANCE
541#define XER_ADVANCE(num_bytes) do { \
542 size_t num = num_bytes; \
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800543 buf_ptr = (const void *)(((const char *)buf_ptr) + num); \
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000544 size -= num; \
545 consumed_myself += num; \
546 } while(0)
547
Lev Walkinc61f3862005-02-14 17:21:22 +0000548/*
549 * Decode the XER (XML) data.
550 */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000551asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -0700552CHOICE_decode_xer(const asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000553 void **struct_ptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000554 const void *buf_ptr, size_t size) {
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000555 /*
556 * Bring closer parts of structure description.
557 */
558 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
559 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
560
561 /*
562 * Parts of the structure being constructed.
563 */
564 void *st = *struct_ptr; /* Target structure. */
565 asn_struct_ctx_t *ctx; /* Decoder context */
566
Lev Walkinc61f3862005-02-14 17:21:22 +0000567 asn_dec_rval_t rval; /* Return value of a decoder */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000568 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
Lev Walkin494fb702017-08-07 20:07:00 -0700569 size_t edx; /* Element index */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000570
571 /*
572 * Create the target structure if it is not present already.
573 */
574 if(st == 0) {
575 st = *struct_ptr = CALLOC(1, specs->struct_size);
576 if(st == 0) RETURN(RC_FAIL);
577 }
578
579 /*
580 * Restore parsing context.
581 */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800582 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkind1bfea62005-11-08 03:06:16 +0000583 if(ctx->phase == 0 && !*xml_tag)
584 ctx->phase = 1; /* Skip the outer tag checking phase */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000585
586 /*
587 * Phases of XER/XML processing:
588 * Phase 0: Check that the opening tag matches our expectations.
589 * Phase 1: Processing body and reacting on closing tag.
590 * Phase 2: Processing inner type.
Lev Walkinc34dc462005-02-18 16:23:48 +0000591 * Phase 3: Only waiting for closing tag.
592 * Phase 4: Skipping unknown extensions.
593 * Phase 5: PHASED OUT
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000594 */
Lev Walkinc34dc462005-02-18 16:23:48 +0000595 for(edx = ctx->step; ctx->phase <= 4;) {
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000596 pxer_chunk_type_e ch_type; /* XER chunk type */
597 ssize_t ch_size; /* Chunk size */
598 xer_check_tag_e tcv; /* Tag check value */
599 asn_TYPE_member_t *elm;
600
601 /*
602 * Go inside the member.
603 */
604 if(ctx->phase == 2) {
605 asn_dec_rval_t tmprval;
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000606 void *memb_ptr; /* Pointer to the member */
607 void **memb_ptr2; /* Pointer to that pointer */
Lev Walkindcae9ce2017-09-18 20:13:36 -0700608 unsigned old_present;
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000609
Lev Walkinabf68892004-10-26 10:12:14 +0000610 elm = &td->elements[edx];
611
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000612 if(elm->flags & ATF_POINTER) {
613 /* Member is a pointer to another structure */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800614 memb_ptr2 = (void **)((char *)st
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000615 + elm->memb_offset);
616 } else {
617 memb_ptr = (char *)st + elm->memb_offset;
618 memb_ptr2 = &memb_ptr;
619 }
620
Lev Walkinc61f3862005-02-14 17:21:22 +0000621 /* Start/Continue decoding the inner member */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800622 tmprval = elm->type->op->xer_decoder(opt_codec_ctx,
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000623 elm->type, memb_ptr2, elm->name,
624 buf_ptr, size);
625 XER_ADVANCE(tmprval.consumed);
Lev Walkin866c8802006-09-17 08:23:35 +0000626 ASN_DEBUG("XER/CHOICE: itdf: [%s] code=%d",
627 elm->type->name, tmprval.code);
Lev Walkindcae9ce2017-09-18 20:13:36 -0700628 old_present = _fetch_present_idx(st,
629 specs->pres_offset, specs->pres_size);
630 assert(old_present == 0 || old_present == edx + 1);
Lev Walkinc61f3862005-02-14 17:21:22 +0000631 /* Record what we've got */
632 _set_present_idx(st,
633 specs->pres_offset, specs->pres_size, edx + 1);
Lev Walkindcae9ce2017-09-18 20:13:36 -0700634 if(tmprval.code != RC_OK)
635 RETURN(tmprval.code);
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000636 ctx->phase = 3;
637 /* Fall through */
638 }
639
Lev Walkind1bfea62005-11-08 03:06:16 +0000640 /* No need to wait for closing tag; special mode. */
641 if(ctx->phase == 3 && !*xml_tag) {
642 ctx->phase = 5; /* Phase out */
643 RETURN(RC_OK);
644 }
645
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000646 /*
647 * Get the next part of the XML stream.
648 */
Lev Walkin1e443962005-02-18 18:06:36 +0000649 ch_size = xer_next_token(&ctx->context, buf_ptr, size, &ch_type);
Lev Walkin97363482016-01-24 19:23:02 -0800650 if(ch_size == -1) {
651 RETURN(RC_FAIL);
652 } else {
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000653 switch(ch_type) {
Lev Walkin97363482016-01-24 19:23:02 -0800654 case PXER_WMORE:
655 RETURN(RC_WMORE);
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000656 case PXER_COMMENT: /* Got XML comment */
657 case PXER_TEXT: /* Ignore free-standing text */
658 XER_ADVANCE(ch_size); /* Skip silently */
659 continue;
660 case PXER_TAG:
661 break; /* Check the rest down there */
662 }
663 }
664
Lev Walkinc61f3862005-02-14 17:21:22 +0000665 tcv = xer_check_tag(buf_ptr, ch_size, xml_tag);
Lev Walkind1bfea62005-11-08 03:06:16 +0000666 ASN_DEBUG("XER/CHOICE checked [%c%c%c%c] vs [%s], tcv=%d",
Lev Walkin32788732005-11-13 09:12:23 +0000667 ch_size>0?((const uint8_t *)buf_ptr)[0]:'?',
668 ch_size>1?((const uint8_t *)buf_ptr)[1]:'?',
669 ch_size>2?((const uint8_t *)buf_ptr)[2]:'?',
670 ch_size>3?((const uint8_t *)buf_ptr)[3]:'?',
Lev Walkind1bfea62005-11-08 03:06:16 +0000671 xml_tag, tcv);
Lev Walkinc34dc462005-02-18 16:23:48 +0000672
673 /* Skip the extensions section */
674 if(ctx->phase == 4) {
675 ASN_DEBUG("skip_unknown(%d, %ld)",
676 tcv, (long)ctx->left);
677 switch(xer_skip_unknown(tcv, &ctx->left)) {
678 case -1:
679 ctx->phase = 5;
680 RETURN(RC_FAIL);
Lev Walkinc34dc462005-02-18 16:23:48 +0000681 case 1:
682 ctx->phase = 3;
Lev Walkin1e443962005-02-18 18:06:36 +0000683 /* Fall through */
Lev Walkinc34dc462005-02-18 16:23:48 +0000684 case 0:
685 XER_ADVANCE(ch_size);
686 continue;
687 case 2:
688 ctx->phase = 3;
689 break;
690 }
691 }
692
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000693 switch(tcv) {
694 case XCT_BOTH:
Lev Walkinc61f3862005-02-14 17:21:22 +0000695 break; /* No CHOICE? */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000696 case XCT_CLOSING:
Lev Walkinc61f3862005-02-14 17:21:22 +0000697 if(ctx->phase != 3)
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000698 break;
699 XER_ADVANCE(ch_size);
Lev Walkinc34dc462005-02-18 16:23:48 +0000700 ctx->phase = 5; /* Phase out */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000701 RETURN(RC_OK);
702 case XCT_OPENING:
703 if(ctx->phase == 0) {
704 XER_ADVANCE(ch_size);
705 ctx->phase = 1; /* Processing body phase */
706 continue;
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000707 }
708 /* Fall through */
Lev Walkin904e65b2005-02-18 14:23:48 +0000709 case XCT_UNKNOWN_OP:
710 case XCT_UNKNOWN_BO:
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000711
712 if(ctx->phase != 1)
713 break; /* Really unexpected */
714
715 /*
Lev Walkinc61f3862005-02-14 17:21:22 +0000716 * Search which inner member corresponds to this tag.
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000717 */
718 for(edx = 0; edx < td->elements_count; edx++) {
719 elm = &td->elements[edx];
Lev Walkinc61f3862005-02-14 17:21:22 +0000720 tcv = xer_check_tag(buf_ptr,ch_size,elm->name);
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000721 switch(tcv) {
722 case XCT_BOTH:
723 case XCT_OPENING:
724 /*
725 * Process this member.
726 */
727 ctx->step = edx;
728 ctx->phase = 2;
729 break;
Lev Walkin904e65b2005-02-18 14:23:48 +0000730 case XCT_UNKNOWN_OP:
731 case XCT_UNKNOWN_BO:
Lev Walkin6c0df202005-02-14 19:03:17 +0000732 continue;
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000733 default:
734 edx = td->elements_count;
735 break; /* Phase out */
736 }
737 break;
738 }
Lev Walkin6c0df202005-02-14 19:03:17 +0000739 if(edx != td->elements_count)
740 continue;
Lev Walkin904e65b2005-02-18 14:23:48 +0000741
742 /* It is expected extension */
Lev Walkin59b176e2005-11-26 11:25:14 +0000743 if(specs->ext_start != -1) {
Lev Walkinc34dc462005-02-18 16:23:48 +0000744 ASN_DEBUG("Got anticipated extension");
Lev Walkin904e65b2005-02-18 14:23:48 +0000745 /*
Lev Walkinc34dc462005-02-18 16:23:48 +0000746 * Check for (XCT_BOTH or XCT_UNKNOWN_BO)
747 * By using a mask. Only record a pure
748 * <opening> tags.
Lev Walkin904e65b2005-02-18 14:23:48 +0000749 */
Lev Walkinc34dc462005-02-18 16:23:48 +0000750 if(tcv & XCT_CLOSING) {
751 /* Found </extension> without body */
752 ctx->phase = 3; /* Terminating */
753 } else {
754 ctx->left = 1;
755 ctx->phase = 4; /* Skip ...'s */
756 }
757 XER_ADVANCE(ch_size);
758 continue;
Lev Walkin904e65b2005-02-18 14:23:48 +0000759 }
760
Lev Walkin6c0df202005-02-14 19:03:17 +0000761 /* Fall through */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000762 default:
763 break;
764 }
765
Lev Walkin866c8802006-09-17 08:23:35 +0000766 ASN_DEBUG("Unexpected XML tag [%c%c%c%c] in CHOICE [%s]"
767 " (ph=%d, tag=%s)",
768 ch_size>0?((const uint8_t *)buf_ptr)[0]:'?',
769 ch_size>1?((const uint8_t *)buf_ptr)[1]:'?',
770 ch_size>2?((const uint8_t *)buf_ptr)[2]:'?',
771 ch_size>3?((const uint8_t *)buf_ptr)[3]:'?',
772 td->name, ctx->phase, xml_tag);
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000773 break;
774 }
775
Lev Walkinc34dc462005-02-18 16:23:48 +0000776 ctx->phase = 5; /* Phase out, just in case */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000777 RETURN(RC_FAIL);
778}
779
780
Lev Walkina9cc46e2004-09-22 16:06:28 +0000781asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000782CHOICE_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000783 int ilevel, enum xer_encoder_flags_e flags,
784 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin5e033762004-09-29 13:26:15 +0000785 asn_CHOICE_specifics_t *specs=(asn_CHOICE_specifics_t *)td->specifics;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000786 asn_enc_rval_t er;
Lev Walkin63a35232017-08-10 19:59:47 -0700787 unsigned present;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000788
789 if(!sptr)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700790 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000791
792 /*
793 * Figure out which CHOICE element is encoded.
794 */
795 present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
796
Lev Walkin63a35232017-08-10 19:59:47 -0700797 if(present == 0 || present > td->elements_count) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700798 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000799 } else {
800 asn_enc_rval_t tmper;
Lev Walkin5e033762004-09-29 13:26:15 +0000801 asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkina9cc46e2004-09-22 16:06:28 +0000802 void *memb_ptr;
803 const char *mname = elm->name;
804 unsigned int mlen = strlen(mname);
805
806 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800807 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700808 if(!memb_ptr) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000809 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800810 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000811 }
812
813 er.encoded = 0;
814
Lev Walkin4fe28822017-09-18 02:57:34 -0700815 if(!(flags & XER_F_CANONICAL)) ASN__TEXT_INDENT(1, ilevel);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700816 ASN__CALLBACK3("<", 1, mname, mlen, ">", 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000817
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800818 tmper = elm->type->op->xer_encoder(elm->type, memb_ptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000819 ilevel + 1, flags, cb, app_key);
820 if(tmper.encoded == -1) return tmper;
Lev Walkin4fe28822017-09-18 02:57:34 -0700821 er.encoded += tmper.encoded;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000822
Lev Walkin7c1dc052016-03-14 03:08:15 -0700823 ASN__CALLBACK3("</", 2, mname, mlen, ">", 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000824 }
825
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700826 if(!(flags & XER_F_CANONICAL)) ASN__TEXT_INDENT(1, ilevel - 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000827
Lev Walkin7c1dc052016-03-14 03:08:15 -0700828 ASN__ENCODED_OK(er);
Lev Walkin942fd082004-10-03 09:13:02 +0000829cb_failed:
Lev Walkin7c1dc052016-03-14 03:08:15 -0700830 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000831}
832
Lev Walkin59b176e2005-11-26 11:25:14 +0000833asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -0700834CHOICE_decode_uper(const asn_codec_ctx_t *opt_codec_ctx, 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) {
837 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
Lev Walkin59b176e2005-11-26 11:25:14 +0000838 asn_dec_rval_t rv;
Lev Walkin494fb702017-08-07 20:07:00 -0700839 const asn_per_constraint_t *ct;
Lev Walkin59b176e2005-11-26 11:25:14 +0000840 asn_TYPE_member_t *elm; /* CHOICE's element */
841 void *memb_ptr;
842 void **memb_ptr2;
843 void *st = *sptr;
844 int value;
845
Lev Walkin7c1dc052016-03-14 03:08:15 -0700846 if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx))
847 ASN__DECODE_FAILED;
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000848
Lev Walkin59b176e2005-11-26 11:25:14 +0000849 /*
850 * Create the target structure if it is not present already.
851 */
852 if(!st) {
853 st = *sptr = CALLOC(1, specs->struct_size);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700854 if(!st) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000855 }
856
857 if(constraints) ct = &constraints->value;
858 else if(td->per_constraints) ct = &td->per_constraints->value;
859 else ct = 0;
860
861 if(ct && ct->flags & APC_EXTENSIBLE) {
862 value = per_get_few_bits(pd, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700863 if(value < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000864 if(value) ct = 0; /* Not restricted */
865 }
866
867 if(ct && ct->range_bits >= 0) {
868 value = per_get_few_bits(pd, ct->range_bits);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700869 if(value < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000870 ASN_DEBUG("CHOICE %s got index %d in range %d",
871 td->name, value, ct->range_bits);
Lev Walkin0a8aa602006-09-18 20:05:55 +0000872 if(value > ct->upper_bound)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700873 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000874 } else {
875 if(specs->ext_start == -1)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700876 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000877 value = uper_get_nsnnwn(pd);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700878 if(value < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000879 value += specs->ext_start;
Lev Walkin494fb702017-08-07 20:07:00 -0700880 if((unsigned)value >= td->elements_count)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700881 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000882 }
883
884 /* Adjust if canonical order is different from natural order */
885 if(specs->canonical_order)
886 value = specs->canonical_order[value];
887
888 /* Set presence to be able to free it later */
889 _set_present_idx(st, specs->pres_offset, specs->pres_size, value + 1);
890
891 elm = &td->elements[value];
892 if(elm->flags & ATF_POINTER) {
893 /* Member is a pointer to another structure */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800894 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
Lev Walkin59b176e2005-11-26 11:25:14 +0000895 } else {
896 memb_ptr = (char *)st + elm->memb_offset;
897 memb_ptr2 = &memb_ptr;
898 }
899 ASN_DEBUG("Discovered CHOICE %s encodes %s", td->name, elm->name);
900
Lev Walkin6f9060f2007-06-29 01:45:03 +0000901 if(ct && ct->range_bits >= 0) {
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800902 rv = elm->type->op->uper_decoder(opt_codec_ctx, elm->type,
Lev Walkin59b176e2005-11-26 11:25:14 +0000903 elm->per_constraints, memb_ptr2, pd);
Lev Walkin6f9060f2007-06-29 01:45:03 +0000904 } else {
905 rv = uper_open_type_get(opt_codec_ctx, elm->type,
906 elm->per_constraints, memb_ptr2, pd);
907 }
908
Lev Walkin59b176e2005-11-26 11:25:14 +0000909 if(rv.code != RC_OK)
Lev Walkin6f9060f2007-06-29 01:45:03 +0000910 ASN_DEBUG("Failed to decode %s in %s (CHOICE) %d",
911 elm->name, td->name, rv.code);
Lev Walkin59b176e2005-11-26 11:25:14 +0000912 return rv;
913}
Lev Walkin494fb702017-08-07 20:07:00 -0700914
Lev Walkin523de9e2006-08-18 01:34:18 +0000915asn_enc_rval_t
916CHOICE_encode_uper(asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -0700917 const asn_per_constraints_t *constraints, void *sptr,
918 asn_per_outp_t *po) {
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800919 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
Lev Walkin523de9e2006-08-18 01:34:18 +0000920 asn_TYPE_member_t *elm; /* CHOICE's element */
Lev Walkin494fb702017-08-07 20:07:00 -0700921 const asn_per_constraint_t *ct;
Lev Walkin523de9e2006-08-18 01:34:18 +0000922 void *memb_ptr;
Lev Walkin63a35232017-08-10 19:59:47 -0700923 unsigned present;
Lev Walkine14480f2013-03-24 03:28:00 -0700924 int present_enc;
Lev Walkin523de9e2006-08-18 01:34:18 +0000925
Lev Walkin7c1dc052016-03-14 03:08:15 -0700926 if(!sptr) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000927
928 ASN_DEBUG("Encoding %s as CHOICE", td->name);
929
930 if(constraints) ct = &constraints->value;
931 else if(td->per_constraints) ct = &td->per_constraints->value;
932 else ct = 0;
933
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800934 present = _fetch_present_idx(sptr, specs->pres_offset, specs->pres_size);
Lev Walkin523de9e2006-08-18 01:34:18 +0000935
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800936 /*
Lev Walkin523de9e2006-08-18 01:34:18 +0000937 * If the structure was not initialized properly, it cannot be encoded:
938 * can't deduce what to encode in the choice type.
939 */
Lev Walkin63a35232017-08-10 19:59:47 -0700940 if(present == 0 || present > td->elements_count)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700941 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000942 else
943 present--;
944
Lev Walkin523de9e2006-08-18 01:34:18 +0000945 ASN_DEBUG("Encoding %s CHOICE element %d", td->name, present);
946
Lev Walkine14480f2013-03-24 03:28:00 -0700947 /* Adjust if canonical order is different from natural order */
948 if(specs->canonical_order)
949 present_enc = specs->canonical_order[present];
950 else
951 present_enc = present;
952
Lev Walkin523de9e2006-08-18 01:34:18 +0000953 if(ct && ct->range_bits >= 0) {
Lev Walkine14480f2013-03-24 03:28:00 -0700954 if(present_enc < ct->lower_bound
955 || present_enc > ct->upper_bound) {
Lev Walkin523de9e2006-08-18 01:34:18 +0000956 if(ct->flags & APC_EXTENSIBLE) {
957 if(per_put_few_bits(po, 1, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700958 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000959 } else {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700960 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000961 }
962 ct = 0;
963 }
964 }
965 if(ct && ct->flags & APC_EXTENSIBLE)
966 if(per_put_few_bits(po, 0, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700967 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000968
Lev Walkin523de9e2006-08-18 01:34:18 +0000969 elm = &td->elements[present];
970 if(elm->flags & ATF_POINTER) {
971 /* Member is a pointer to another structure */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800972 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700973 if(!memb_ptr) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000974 } else {
975 memb_ptr = (char *)sptr + elm->memb_offset;
976 }
977
Lev Walkin6f9060f2007-06-29 01:45:03 +0000978 if(ct && ct->range_bits >= 0) {
Lev Walkine14480f2013-03-24 03:28:00 -0700979 if(per_put_few_bits(po, present_enc, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700980 ASN__ENCODE_FAILED;
Lev Walkin6f9060f2007-06-29 01:45:03 +0000981
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800982 return elm->type->op->uper_encoder(elm->type, elm->per_constraints,
Lev Walkin523de9e2006-08-18 01:34:18 +0000983 memb_ptr, po);
Lev Walkin6f9060f2007-06-29 01:45:03 +0000984 } else {
985 asn_enc_rval_t rval;
986 if(specs->ext_start == -1)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700987 ASN__ENCODE_FAILED;
Lev Walkine14480f2013-03-24 03:28:00 -0700988 if(uper_put_nsnnwn(po, present_enc - specs->ext_start))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700989 ASN__ENCODE_FAILED;
Lev Walkin6f9060f2007-06-29 01:45:03 +0000990 if(uper_open_type_put(elm->type, elm->per_constraints,
991 memb_ptr, po))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700992 ASN__ENCODE_FAILED;
Lev Walkin6f9060f2007-06-29 01:45:03 +0000993 rval.encoded = 0;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700994 ASN__ENCODED_OK(rval);
Lev Walkin6f9060f2007-06-29 01:45:03 +0000995 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000996}
997
Lev Walkin59b176e2005-11-26 11:25:14 +0000998
Lev Walkinf15320b2004-06-03 03:38:44 +0000999int
Lev Walkin5e033762004-09-29 13:26:15 +00001000CHOICE_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +00001001 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin5e033762004-09-29 13:26:15 +00001002 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
Lev Walkin63a35232017-08-10 19:59:47 -07001003 unsigned present;
Lev Walkinf15320b2004-06-03 03:38:44 +00001004
Lev Walkin8e8078a2004-09-26 13:10:40 +00001005 if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001006
1007 /*
1008 * Figure out which CHOICE element is encoded.
1009 */
1010 present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
1011
1012 /*
Lev Walkina9cc46e2004-09-22 16:06:28 +00001013 * Print that element.
Lev Walkinf15320b2004-06-03 03:38:44 +00001014 */
Lev Walkin63a35232017-08-10 19:59:47 -07001015 if(present > 0 && present <= td->elements_count) {
Lev Walkin5e033762004-09-29 13:26:15 +00001016 asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkinf15320b2004-06-03 03:38:44 +00001017 const void *memb_ptr;
1018
Lev Walkincc93b0f2004-09-10 09:18:20 +00001019 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001020 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkin8e8078a2004-09-26 13:10:40 +00001021 if(!memb_ptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001022 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001023 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +00001024 }
1025
1026 /* Print member's name and stuff */
Lev Walkincc6a9102004-09-23 22:06:26 +00001027 if(0) {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001028 if(cb(elm->name, strlen(elm->name), app_key) < 0
1029 || cb(": ", 2, app_key) < 0)
Lev Walkincc6a9102004-09-23 22:06:26 +00001030 return -1;
1031 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001032
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001033 return elm->type->op->print_struct(elm->type, memb_ptr, ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +00001034 cb, app_key);
1035 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001036 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001037 }
1038}
1039
1040void
Lev Walkin8d99d7b2017-08-25 01:06:00 -07001041CHOICE_free(const asn_TYPE_descriptor_t *td, void *ptr,
1042 enum asn_struct_free_method method) {
Lev Walkinf6853ce2017-08-11 00:50:27 -07001043 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
Lev Walkin63a35232017-08-10 19:59:47 -07001044 unsigned present;
Lev Walkinf15320b2004-06-03 03:38:44 +00001045
1046 if(!td || !ptr)
1047 return;
1048
1049 ASN_DEBUG("Freeing %s as CHOICE", td->name);
1050
1051 /*
1052 * Figure out which CHOICE element is encoded.
1053 */
1054 present = _fetch_present_idx(ptr, specs->pres_offset, specs->pres_size);
1055
1056 /*
1057 * Free that element.
1058 */
Lev Walkin63a35232017-08-10 19:59:47 -07001059 if(present > 0 && present <= td->elements_count) {
Lev Walkin5e033762004-09-29 13:26:15 +00001060 asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkinf15320b2004-06-03 03:38:44 +00001061 void *memb_ptr;
1062
Lev Walkincc93b0f2004-09-10 09:18:20 +00001063 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001064 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +00001065 if(memb_ptr)
Lev Walkinadcb5862006-03-17 02:11:12 +00001066 ASN_STRUCT_FREE(*elm->type, memb_ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +00001067 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001068 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
Lev Walkinadcb5862006-03-17 02:11:12 +00001069 ASN_STRUCT_FREE_CONTENTS_ONLY(*elm->type, memb_ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +00001070 }
1071 }
1072
Lev Walkin8d99d7b2017-08-25 01:06:00 -07001073 switch(method) {
1074 case ASFM_FREE_EVERYTHING:
1075 FREEMEM(ptr);
1076 break;
1077 case ASFM_FREE_UNDERLYING:
1078 break;
1079 case ASFM_FREE_UNDERLYING_AND_RESET:
1080 memset(ptr, 0, specs->struct_size);
1081 break;
1082 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001083}
1084
1085
1086/*
1087 * The following functions functions offer protection against -fshort-enums,
1088 * compatible with little- and big-endian machines.
1089 * If assertion is triggered, either disable -fshort-enums, or add an entry
1090 * here with the ->pres_size of your target stracture.
1091 * Unless the target structure is packed, the ".present" member
1092 * is guaranteed to be aligned properly. ASN.1 compiler itself does not
1093 * produce packed code.
1094 */
Lev Walkinf6853ce2017-08-11 00:50:27 -07001095static unsigned
1096_fetch_present_idx(const void *struct_ptr, unsigned pres_offset,
1097 unsigned pres_size) {
1098 const void *present_ptr;
Lev Walkin63a35232017-08-10 19:59:47 -07001099 unsigned present;
Lev Walkinf15320b2004-06-03 03:38:44 +00001100
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001101 present_ptr = ((const char *)struct_ptr) + pres_offset;
Lev Walkinf15320b2004-06-03 03:38:44 +00001102
1103 switch(pres_size) {
Lev Walkinf6853ce2017-08-11 00:50:27 -07001104 case sizeof(int): present = *(const unsigned int *)present_ptr; break;
1105 case sizeof(short): present = *(const unsigned short *)present_ptr; break;
1106 case sizeof(char): present = *(const unsigned char *)present_ptr; break;
Lev Walkinf15320b2004-06-03 03:38:44 +00001107 default:
1108 /* ANSI C mandates enum to be equivalent to integer */
1109 assert(pres_size != sizeof(int));
1110 return 0; /* If not aborted, pass back safe value */
1111 }
1112
1113 return present;
1114}
1115
Lev Walkin91f5cd02004-08-11 09:10:59 +00001116static void
Lev Walkinf6853ce2017-08-11 00:50:27 -07001117_set_present_idx(void *struct_ptr, unsigned pres_offset, unsigned pres_size,
1118 unsigned present) {
1119 void *present_ptr;
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001120 present_ptr = ((char *)struct_ptr) + pres_offset;
Lev Walkinf15320b2004-06-03 03:38:44 +00001121
1122 switch(pres_size) {
Lev Walkinf6853ce2017-08-11 00:50:27 -07001123 case sizeof(int): *(unsigned int *)present_ptr = present; break;
1124 case sizeof(short): *(unsigned short *)present_ptr = present; break;
1125 case sizeof(char): *(unsigned char *)present_ptr = present; break;
Lev Walkinf15320b2004-06-03 03:38:44 +00001126 default:
1127 /* ANSI C mandates enum to be equivalent to integer */
1128 assert(pres_size != sizeof(int));
1129 }
1130}
Lev Walkincd2f48e2017-08-10 02:14:59 -07001131
1132static const void *
1133_get_member_ptr(const asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin63a35232017-08-10 19:59:47 -07001134 asn_TYPE_member_t **elm_ptr, unsigned *present_out) {
Lev Walkincd2f48e2017-08-10 02:14:59 -07001135 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
Lev Walkin63a35232017-08-10 19:59:47 -07001136 unsigned present;
Lev Walkincd2f48e2017-08-10 02:14:59 -07001137
1138 if(!sptr) {
1139 *elm_ptr = NULL;
1140 *present_out = 0;
1141 return NULL;
1142 }
1143
1144 /*
1145 * Figure out which CHOICE element is encoded.
1146 */
1147 present = _fetch_present_idx(sptr, specs->pres_offset, specs->pres_size);
1148 *present_out = present;
1149
1150 /*
1151 * The presence index is intentionally 1-based to avoid
1152 * treating zeroed structure as a valid one.
1153 */
Lev Walkin63a35232017-08-10 19:59:47 -07001154 if(present > 0 && present <= td->elements_count) {
Lev Walkincd2f48e2017-08-10 02:14:59 -07001155 asn_TYPE_member_t *const elm = &td->elements[present - 1];
1156 const void *memb_ptr;
1157
1158 if(elm->flags & ATF_POINTER) {
1159 memb_ptr =
1160 *(const void *const *)((const char *)sptr + elm->memb_offset);
1161 } else {
1162 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
1163 }
1164 *elm_ptr = elm;
1165 return memb_ptr;
1166 } else {
1167 *elm_ptr = NULL;
1168 return NULL;
1169 }
1170
1171}
1172
1173int
1174CHOICE_compare(const asn_TYPE_descriptor_t *td, const void *aptr, const void *bptr) {
1175 asn_TYPE_member_t *aelm;
1176 asn_TYPE_member_t *belm;
Lev Walkin63a35232017-08-10 19:59:47 -07001177 unsigned apresent = 0;
1178 unsigned bpresent = 0;
Lev Walkincd2f48e2017-08-10 02:14:59 -07001179 const void *amember = _get_member_ptr(td, aptr, &aelm, &apresent);
1180 const void *bmember = _get_member_ptr(td, bptr, &belm, &apresent);
1181
1182 if(amember && bmember) {
1183 if(apresent == bpresent) {
1184 assert(aelm == belm);
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001185 return aelm->type->op->compare_struct(aelm->type, amember, bmember);
Lev Walkincd2f48e2017-08-10 02:14:59 -07001186 } else if(apresent < bpresent) {
1187 return -1;
1188 } else {
1189 return 1;
1190 }
1191 } else if(!amember) {
1192 return -1;
1193 } else {
1194 return 1;
1195 }
1196}
Lev Walkinf6853ce2017-08-11 00:50:27 -07001197
1198/*
1199 * Return the 1-based choice variant presence index.
1200 * Returns 0 in case of error.
1201 */
1202unsigned
1203CHOICE_variant_get_presence(const asn_TYPE_descriptor_t *td, const void *sptr) {
1204 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
1205 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) {
1217 extern asn_CHOICE_specifics_t asn_SPC_value_specs_3;
1218 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
1219 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
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001243asn_TYPE_operation_t asn_OP_CHOICE = {
1244 CHOICE_free,
1245 CHOICE_print,
1246 CHOICE_compare,
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001247 CHOICE_decode_ber,
1248 CHOICE_encode_der,
1249 CHOICE_decode_xer,
1250 CHOICE_encode_xer,
1251#ifdef ASN_DISABLE_OER_SUPPORT
1252 0,
1253 0,
1254#else
Lev Walkin9a1736d2017-08-27 23:46:34 -07001255 CHOICE_decode_oer,
1256 CHOICE_encode_oer,
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001257#endif /* ASN_DISABLE_OER_SUPPORT */
1258#ifdef ASN_DISABLE_PER_SUPPORT
1259 0,
1260 0,
1261#else
1262 CHOICE_decode_uper,
1263 CHOICE_encode_uper,
1264#endif /* ASN_DISABLE_PER_SUPPORT */
1265 CHOICE_outmost_tag
1266};