blob: e7adbf75e8f6c7f9f501b013122612e33f2982d6 [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 Walkincd2f48e2017-08-10 02:14:59 -070066static signed _fetch_present_idx(const void *struct_ptr, int off, int size);
Lev Walkin91f5cd02004-08-11 09:10:59 +000067static void _set_present_idx(void *sptr, int offset, int size, int pres);
Lev Walkincd2f48e2017-08-10 02:14:59 -070068static const void *_get_member_ptr(const asn_TYPE_descriptor_t *,
69 const void *sptr, asn_TYPE_member_t **elm,
70 signed *present);
Lev Walkinf15320b2004-06-03 03:38:44 +000071
72/*
73 * Tags are canonically sorted in the tag to member table.
74 */
75static int
76_search4tag(const void *ap, const void *bp) {
Lev Walkin5e033762004-09-29 13:26:15 +000077 const asn_TYPE_tag2member_t *a = (const asn_TYPE_tag2member_t *)ap;
78 const asn_TYPE_tag2member_t *b = (const asn_TYPE_tag2member_t *)bp;
Lev Walkinc2346572004-08-11 09:07:36 +000079
Lev Walkinf15320b2004-06-03 03:38:44 +000080 int a_class = BER_TAG_CLASS(a->el_tag);
81 int b_class = BER_TAG_CLASS(b->el_tag);
82
83 if(a_class == b_class) {
84 ber_tlv_tag_t a_value = BER_TAG_VALUE(a->el_tag);
85 ber_tlv_tag_t b_value = BER_TAG_VALUE(b->el_tag);
86
87 if(a_value == b_value)
88 return 0;
89 else if(a_value < b_value)
90 return -1;
91 else
92 return 1;
93 } else if(a_class < b_class) {
94 return -1;
95 } else {
96 return 1;
97 }
98}
99
100/*
101 * The decoder of the CHOICE type.
102 */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000103asn_dec_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000104CHOICE_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000105 void **struct_ptr, const void *ptr, size_t size, int tag_mode) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000106 /*
107 * Bring closer parts of structure description.
108 */
Lev Walkin5e033762004-09-29 13:26:15 +0000109 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
110 asn_TYPE_member_t *elements = td->elements;
Lev Walkinf15320b2004-06-03 03:38:44 +0000111
112 /*
113 * Parts of the structure being constructed.
114 */
115 void *st = *struct_ptr; /* Target structure. */
Lev Walkin5e033762004-09-29 13:26:15 +0000116 asn_struct_ctx_t *ctx; /* Decoder context */
Lev Walkinf15320b2004-06-03 03:38:44 +0000117
118 ber_tlv_tag_t tlv_tag; /* T from TLV */
119 ssize_t tag_len; /* Length of TLV's T */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000120 asn_dec_rval_t rval; /* Return code from subparsers */
Lev Walkinf15320b2004-06-03 03:38:44 +0000121
122 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
123
Lev Walkin449f8322004-08-20 13:23:42 +0000124 ASN_DEBUG("Decoding %s as CHOICE", td->name);
Lev Walkin59b176e2005-11-26 11:25:14 +0000125
Lev Walkinf15320b2004-06-03 03:38:44 +0000126 /*
127 * Create the target structure if it is not present already.
128 */
129 if(st == 0) {
130 st = *struct_ptr = CALLOC(1, specs->struct_size);
131 if(st == 0) {
132 RETURN(RC_FAIL);
133 }
134 }
135
136 /*
137 * Restore parsing context.
138 */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800139 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000140
141 /*
142 * Start to parse where left previously
143 */
144 switch(ctx->phase) {
145 case 0:
146 /*
147 * PHASE 0.
148 * Check that the set of tags associated with given structure
149 * perfectly fits our expectations.
150 */
151
Lev Walkin449f8322004-08-20 13:23:42 +0000152 if(tag_mode || td->tags_count) {
Lev Walkin5e033762004-09-29 13:26:15 +0000153 rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size,
Lev Walkin8e8078a2004-09-26 13:10:40 +0000154 tag_mode, -1, &ctx->left, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000155 if(rval.code != RC_OK) {
156 ASN_DEBUG("%s tagging check failed: %d",
Lev Walkin449f8322004-08-20 13:23:42 +0000157 td->name, rval.code);
Lev Walkinf0f04d12004-10-05 06:36:02 +0000158 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000159 }
160
161 if(ctx->left >= 0) {
162 /* ?Substracted below! */
163 ctx->left += rval.consumed;
164 }
165 ADVANCE(rval.consumed);
166 } else {
167 ctx->left = -1;
168 }
169
170 NEXT_PHASE(ctx);
171
172 ASN_DEBUG("Structure consumes %ld bytes, buffer %ld",
173 (long)ctx->left, (long)size);
174
175 /* Fall through */
176 case 1:
177 /*
178 * Fetch the T from TLV.
179 */
180 tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
Lev Walkin449f8322004-08-20 13:23:42 +0000181 ASN_DEBUG("In %s CHOICE tag length %d", td->name, (int)tag_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000182 switch(tag_len) {
183 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
184 /* Fall through */
185 case -1: RETURN(RC_FAIL);
186 }
187
188 do {
Wim Lewisfb6344e2014-07-28 12:16:01 -0700189 const asn_TYPE_tag2member_t *t2m;
Lev Walkin5e033762004-09-29 13:26:15 +0000190 asn_TYPE_tag2member_t key;
Lev Walkinf15320b2004-06-03 03:38:44 +0000191
192 key.el_tag = tlv_tag;
Wim Lewisfb6344e2014-07-28 12:16:01 -0700193 t2m = (const asn_TYPE_tag2member_t *)bsearch(&key,
Lev Walkinc2346572004-08-11 09:07:36 +0000194 specs->tag2el, specs->tag2el_count,
Lev Walkinf15320b2004-06-03 03:38:44 +0000195 sizeof(specs->tag2el[0]), _search4tag);
196 if(t2m) {
197 /*
198 * Found the element corresponding to the tag.
199 */
200 NEXT_PHASE(ctx);
201 ctx->step = t2m->el_no;
202 break;
Lev Walkin59b176e2005-11-26 11:25:14 +0000203 } else if(specs->ext_start == -1) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000204 ASN_DEBUG("Unexpected tag %s "
205 "in non-extensible CHOICE %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000206 ber_tlv_tag_string(tlv_tag), td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000207 RETURN(RC_FAIL);
208 } else {
209 /* Skip this tag */
210 ssize_t skip;
211
212 ASN_DEBUG("Skipping unknown tag %s",
213 ber_tlv_tag_string(tlv_tag));
214
Lev Walkinbaaa24f2004-09-29 14:19:14 +0000215 skip = ber_skip_length(opt_codec_ctx,
Lev Walkinf15320b2004-06-03 03:38:44 +0000216 BER_TLV_CONSTRUCTED(ptr),
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800217 (const char *)ptr + tag_len,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000218 LEFT - tag_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000219
220 switch(skip) {
221 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
222 /* Fall through */
223 case -1: RETURN(RC_FAIL);
224 }
225
226 ADVANCE(skip + tag_len);
227 RETURN(RC_OK);
228 }
229 } while(0);
230
231 case 2:
232 /*
233 * PHASE 2.
234 * Read in the element.
235 */
236 do {
Lev Walkin5e033762004-09-29 13:26:15 +0000237 asn_TYPE_member_t *elm;/* CHOICE's element */
Lev Walkinf15320b2004-06-03 03:38:44 +0000238 void *memb_ptr; /* Pointer to the member */
Lev Walkinc2346572004-08-11 09:07:36 +0000239 void **memb_ptr2; /* Pointer to that pointer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000240
241 elm = &elements[ctx->step];
242
243 /*
244 * Compute the position of the member inside a structure,
245 * and also a type of containment (it may be contained
246 * as pointer or using inline inclusion).
247 */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000248 if(elm->flags & ATF_POINTER) {
249 /* Member is a pointer to another structure */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800250 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000251 } else {
252 /*
253 * A pointer to a pointer
254 * holding the start of the structure
255 */
256 memb_ptr = (char *)st + elm->memb_offset;
257 memb_ptr2 = &memb_ptr;
258 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000259 /* Set presence to be able to free it properly at any time */
260 _set_present_idx(st, specs->pres_offset,
261 specs->pres_size, ctx->step + 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000262 /*
263 * Invoke the member fetch routine according to member's type
264 */
Lev Walkin5e033762004-09-29 13:26:15 +0000265 rval = elm->type->ber_decoder(opt_codec_ctx, elm->type,
Lev Walkin59b176e2005-11-26 11:25:14 +0000266 memb_ptr2, ptr, LEFT, elm->tag_mode);
Lev Walkinf15320b2004-06-03 03:38:44 +0000267 switch(rval.code) {
268 case RC_OK:
Lev Walkinf15320b2004-06-03 03:38:44 +0000269 break;
270 case RC_WMORE: /* More data expected */
271 if(!SIZE_VIOLATION) {
272 ADVANCE(rval.consumed);
273 RETURN(RC_WMORE);
274 }
275 RETURN(RC_FAIL);
276 case RC_FAIL: /* Fatal error */
277 RETURN(rval.code);
278 } /* switch(rval) */
279
280 ADVANCE(rval.consumed);
281 } while(0);
282
283 NEXT_PHASE(ctx);
284
285 /* Fall through */
286 case 3:
287 ASN_DEBUG("CHOICE %s Leftover: %ld, size = %ld, tm=%d, tc=%d",
Lev Walkin449f8322004-08-20 13:23:42 +0000288 td->name, (long)ctx->left, (long)size,
289 tag_mode, td->tags_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000290
291 if(ctx->left > 0) {
292 /*
293 * The type must be fully decoded
294 * by the CHOICE member-specific decoder.
295 */
296 RETURN(RC_FAIL);
297 }
298
299 if(ctx->left == -1
Lev Walkin449f8322004-08-20 13:23:42 +0000300 && !(tag_mode || td->tags_count)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000301 /*
302 * This is an untagged CHOICE.
303 * It doesn't contain nothing
304 * except for the member itself, including all its tags.
305 * The decoding is completed.
306 */
307 NEXT_PHASE(ctx);
308 break;
309 }
310
311 /*
312 * Read in the "end of data chunks"'s.
313 */
314 while(ctx->left < 0) {
315 ssize_t tl;
316
317 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
318 switch(tl) {
319 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
320 /* Fall through */
321 case -1: RETURN(RC_FAIL);
322 }
323
324 /*
325 * Expected <0><0>...
326 */
Lev Walkin8c3b8542005-03-10 18:52:02 +0000327 if(((const uint8_t *)ptr)[0] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000328 if(LEFT < 2) {
329 if(SIZE_VIOLATION)
330 RETURN(RC_FAIL);
331 else
332 RETURN(RC_WMORE);
Lev Walkin8c3b8542005-03-10 18:52:02 +0000333 } else if(((const uint8_t *)ptr)[1] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000334 /*
335 * Correctly finished with <0><0>.
336 */
Lev Walkinf0f04d12004-10-05 06:36:02 +0000337 ADVANCE(2);
338 ctx->left++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000339 continue;
340 }
341 } else {
342 ASN_DEBUG("Unexpected continuation in %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000343 td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000344 RETURN(RC_FAIL);
345 }
346
Lev Walkinf0f04d12004-10-05 06:36:02 +0000347 /* UNREACHABLE */
Lev Walkinf15320b2004-06-03 03:38:44 +0000348 }
349
350 NEXT_PHASE(ctx);
351 case 4:
352 /* No meaningful work here */
353 break;
354 }
355
356 RETURN(RC_OK);
357}
358
Lev Walkina9cc46e2004-09-22 16:06:28 +0000359asn_enc_rval_t
Lev Walkinac589332005-08-22 14:19:28 +0000360CHOICE_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000361 int tag_mode, ber_tlv_tag_t tag,
362 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin5e033762004-09-29 13:26:15 +0000363 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
364 asn_TYPE_member_t *elm; /* CHOICE element */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000365 asn_enc_rval_t erval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000366 void *memb_ptr;
367 size_t computed_size = 0;
Lev Walkincd2f48e2017-08-10 02:14:59 -0700368 signed present;
Lev Walkinf15320b2004-06-03 03:38:44 +0000369
Lev Walkin7c1dc052016-03-14 03:08:15 -0700370 if(!sptr) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000371
Lev Walkinf15320b2004-06-03 03:38:44 +0000372 ASN_DEBUG("%s %s as CHOICE",
Lev Walkin449f8322004-08-20 13:23:42 +0000373 cb?"Encoding":"Estimating", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000374
Lev Walkinac589332005-08-22 14:19:28 +0000375 present = _fetch_present_idx(sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000376 specs->pres_offset, specs->pres_size);
377
378 /*
379 * If the structure was not initialized, it cannot be encoded:
380 * can't deduce what to encode in the choice type.
381 */
Lev Walkin494fb702017-08-07 20:07:00 -0700382 if(present <= 0 || (unsigned)present > td->elements_count) {
Lev Walkin449f8322004-08-20 13:23:42 +0000383 if(present == 0 && td->elements_count == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000384 /* The CHOICE is empty?! */
385 erval.encoded = 0;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700386 ASN__ENCODED_OK(erval);
Lev Walkinf15320b2004-06-03 03:38:44 +0000387 }
Lev Walkin7c1dc052016-03-14 03:08:15 -0700388 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000389 }
390
391 /*
392 * Seek over the present member of the structure.
393 */
Lev Walkin449f8322004-08-20 13:23:42 +0000394 elm = &td->elements[present-1];
Lev Walkincc93b0f2004-09-10 09:18:20 +0000395 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800396 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000397 if(memb_ptr == 0) {
Lev Walkincc93b0f2004-09-10 09:18:20 +0000398 if(elm->optional) {
399 erval.encoded = 0;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700400 ASN__ENCODED_OK(erval);
Lev Walkincc93b0f2004-09-10 09:18:20 +0000401 }
Lev Walkinac589332005-08-22 14:19:28 +0000402 /* Mandatory element absent */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700403 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000404 }
405 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800406 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000407 }
408
409 /*
410 * If the CHOICE itself is tagged EXPLICIT:
411 * T ::= [2] EXPLICIT CHOICE { ... }
412 * Then emit the appropriate tags.
413 */
Lev Walkin449f8322004-08-20 13:23:42 +0000414 if(tag_mode == 1 || td->tags_count) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000415 /*
416 * For this, we need to pre-compute the member.
417 */
418 ssize_t ret;
419
420 /* Encode member with its tag */
421 erval = elm->type->der_encoder(elm->type, memb_ptr,
422 elm->tag_mode, elm->tag, 0, 0);
423 if(erval.encoded == -1)
424 return erval;
425
426 /* Encode CHOICE with parent or my own tag */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000427 ret = der_write_tags(td, erval.encoded, tag_mode, 1, tag,
Lev Walkinf15320b2004-06-03 03:38:44 +0000428 cb, app_key);
Lev Walkinac589332005-08-22 14:19:28 +0000429 if(ret == -1)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700430 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000431 computed_size += ret;
432 }
433
434 /*
435 * Encode the single underlying member.
436 */
437 erval = elm->type->der_encoder(elm->type, memb_ptr,
438 elm->tag_mode, elm->tag, cb, app_key);
439 if(erval.encoded == -1)
440 return erval;
441
442 ASN_DEBUG("Encoded CHOICE member in %ld bytes (+%ld)",
443 (long)erval.encoded, (long)computed_size);
444
445 erval.encoded += computed_size;
446
447 return erval;
448}
449
450ber_tlv_tag_t
Wim Lewis14e6b162014-07-23 16:06:01 -0700451CHOICE_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 +0000452 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
Lev Walkincd2f48e2017-08-10 02:14:59 -0700453 signed present;
Lev Walkinf15320b2004-06-03 03:38:44 +0000454
Lev Walkin32788732005-11-13 09:12:23 +0000455 assert(tag_mode == 0); (void)tag_mode;
456 assert(tag == 0); (void)tag;
Lev Walkinf15320b2004-06-03 03:38:44 +0000457
458 /*
459 * Figure out which CHOICE element is encoded.
460 */
461 present = _fetch_present_idx(ptr, specs->pres_offset, specs->pres_size);
462
Lev Walkin494fb702017-08-07 20:07:00 -0700463 if(present > 0 && (unsigned)present <= td->elements_count) {
Wim Lewis14e6b162014-07-23 16:06:01 -0700464 const asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkin70e70282004-06-05 01:45:48 +0000465 const void *memb_ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000466
Lev Walkincc93b0f2004-09-10 09:18:20 +0000467 if(elm->flags & ATF_POINTER) {
Lev Walkin70e70282004-06-05 01:45:48 +0000468 memb_ptr = *(const void * const *)
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800469 ((const char *)ptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000470 } else {
Lev Walkin70e70282004-06-05 01:45:48 +0000471 memb_ptr = (const void *)
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800472 ((const char *)ptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000473 }
474
Lev Walkin5e033762004-09-29 13:26:15 +0000475 return asn_TYPE_outmost_tag(elm->type, memb_ptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000476 elm->tag_mode, elm->tag);
477 } else {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000478 return (ber_tlv_tag_t)-1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000479 }
480}
481
482int
Lev Walkin5e033762004-09-29 13:26:15 +0000483CHOICE_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +0000484 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkin5e033762004-09-29 13:26:15 +0000485 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
Lev Walkincd2f48e2017-08-10 02:14:59 -0700486 signed present;
Lev Walkinf15320b2004-06-03 03:38:44 +0000487
488 if(!sptr) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700489 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +0000490 "%s: value not given (%s:%d)",
491 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000492 return -1;
493 }
494
495 /*
496 * Figure out which CHOICE element is encoded.
497 */
498 present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
Lev Walkin494fb702017-08-07 20:07:00 -0700499 if(present > 0 && (unsigned)present <= td->elements_count) {
Lev Walkin5e033762004-09-29 13:26:15 +0000500 asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkinf15320b2004-06-03 03:38:44 +0000501 const void *memb_ptr;
502
Lev Walkincc93b0f2004-09-10 09:18:20 +0000503 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800504 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkincc93b0f2004-09-10 09:18:20 +0000505 if(!memb_ptr) {
506 if(elm->optional)
507 return 0;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700508 ASN__CTFAIL(app_key, td, sptr,
Lev Walkincc93b0f2004-09-10 09:18:20 +0000509 "%s: mandatory CHOICE element %s absent (%s:%d)",
510 td->name, elm->name, __FILE__, __LINE__);
511 return -1;
512 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000513 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800514 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000515 }
516
Lev Walkin449f8322004-08-20 13:23:42 +0000517 if(elm->memb_constraints) {
518 return elm->memb_constraints(elm->type, memb_ptr,
Lev Walkin1eded352006-07-13 11:19:01 +0000519 ctfailcb, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +0000520 } else {
521 int ret = elm->type->check_constraints(elm->type,
Lev Walkin1eded352006-07-13 11:19:01 +0000522 memb_ptr, ctfailcb, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +0000523 /*
524 * Cannot inherit it eralier:
525 * need to make sure we get the updated version.
526 */
527 elm->memb_constraints = elm->type->check_constraints;
528 return ret;
529 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000530 } else {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700531 ASN__CTFAIL(app_key, td, sptr,
Lev Walkind34a8512004-08-22 13:55:49 +0000532 "%s: no CHOICE element given (%s:%d)",
Lev Walkin16835b62004-08-22 13:47:59 +0000533 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000534 return -1;
535 }
536}
537
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000538#undef XER_ADVANCE
539#define XER_ADVANCE(num_bytes) do { \
540 size_t num = num_bytes; \
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800541 buf_ptr = (const void *)(((const char *)buf_ptr) + num); \
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000542 size -= num; \
543 consumed_myself += num; \
544 } while(0)
545
Lev Walkinc61f3862005-02-14 17:21:22 +0000546/*
547 * Decode the XER (XML) data.
548 */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000549asn_dec_rval_t
550CHOICE_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
551 void **struct_ptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000552 const void *buf_ptr, size_t size) {
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000553 /*
554 * Bring closer parts of structure description.
555 */
556 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
557 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
558
559 /*
560 * Parts of the structure being constructed.
561 */
562 void *st = *struct_ptr; /* Target structure. */
563 asn_struct_ctx_t *ctx; /* Decoder context */
564
Lev Walkinc61f3862005-02-14 17:21:22 +0000565 asn_dec_rval_t rval; /* Return value of a decoder */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000566 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
Lev Walkin494fb702017-08-07 20:07:00 -0700567 size_t edx; /* Element index */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000568
569 /*
570 * Create the target structure if it is not present already.
571 */
572 if(st == 0) {
573 st = *struct_ptr = CALLOC(1, specs->struct_size);
574 if(st == 0) RETURN(RC_FAIL);
575 }
576
577 /*
578 * Restore parsing context.
579 */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800580 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkind1bfea62005-11-08 03:06:16 +0000581 if(ctx->phase == 0 && !*xml_tag)
582 ctx->phase = 1; /* Skip the outer tag checking phase */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000583
584 /*
585 * Phases of XER/XML processing:
586 * Phase 0: Check that the opening tag matches our expectations.
587 * Phase 1: Processing body and reacting on closing tag.
588 * Phase 2: Processing inner type.
Lev Walkinc34dc462005-02-18 16:23:48 +0000589 * Phase 3: Only waiting for closing tag.
590 * Phase 4: Skipping unknown extensions.
591 * Phase 5: PHASED OUT
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000592 */
Lev Walkinc34dc462005-02-18 16:23:48 +0000593 for(edx = ctx->step; ctx->phase <= 4;) {
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000594 pxer_chunk_type_e ch_type; /* XER chunk type */
595 ssize_t ch_size; /* Chunk size */
596 xer_check_tag_e tcv; /* Tag check value */
597 asn_TYPE_member_t *elm;
598
599 /*
600 * Go inside the member.
601 */
602 if(ctx->phase == 2) {
603 asn_dec_rval_t tmprval;
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000604 void *memb_ptr; /* Pointer to the member */
605 void **memb_ptr2; /* Pointer to that pointer */
606
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 */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000619 tmprval = elm->type->xer_decoder(opt_codec_ctx,
620 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 Walkin8d01b4c2004-10-23 14:57:50 +0000625 if(tmprval.code != RC_OK)
626 RETURN(tmprval.code);
Lev Walkinc61f3862005-02-14 17:21:22 +0000627 assert(_fetch_present_idx(st,
628 specs->pres_offset, specs->pres_size) == 0);
629 /* Record what we've got */
630 _set_present_idx(st,
631 specs->pres_offset, specs->pres_size, edx + 1);
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000632 ctx->phase = 3;
633 /* Fall through */
634 }
635
Lev Walkind1bfea62005-11-08 03:06:16 +0000636 /* No need to wait for closing tag; special mode. */
637 if(ctx->phase == 3 && !*xml_tag) {
638 ctx->phase = 5; /* Phase out */
639 RETURN(RC_OK);
640 }
641
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000642 /*
643 * Get the next part of the XML stream.
644 */
Lev Walkin1e443962005-02-18 18:06:36 +0000645 ch_size = xer_next_token(&ctx->context, buf_ptr, size, &ch_type);
Lev Walkin97363482016-01-24 19:23:02 -0800646 if(ch_size == -1) {
647 RETURN(RC_FAIL);
648 } else {
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000649 switch(ch_type) {
Lev Walkin97363482016-01-24 19:23:02 -0800650 case PXER_WMORE:
651 RETURN(RC_WMORE);
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000652 case PXER_COMMENT: /* Got XML comment */
653 case PXER_TEXT: /* Ignore free-standing text */
654 XER_ADVANCE(ch_size); /* Skip silently */
655 continue;
656 case PXER_TAG:
657 break; /* Check the rest down there */
658 }
659 }
660
Lev Walkinc61f3862005-02-14 17:21:22 +0000661 tcv = xer_check_tag(buf_ptr, ch_size, xml_tag);
Lev Walkind1bfea62005-11-08 03:06:16 +0000662 ASN_DEBUG("XER/CHOICE checked [%c%c%c%c] vs [%s], tcv=%d",
Lev Walkin32788732005-11-13 09:12:23 +0000663 ch_size>0?((const uint8_t *)buf_ptr)[0]:'?',
664 ch_size>1?((const uint8_t *)buf_ptr)[1]:'?',
665 ch_size>2?((const uint8_t *)buf_ptr)[2]:'?',
666 ch_size>3?((const uint8_t *)buf_ptr)[3]:'?',
Lev Walkind1bfea62005-11-08 03:06:16 +0000667 xml_tag, tcv);
Lev Walkinc34dc462005-02-18 16:23:48 +0000668
669 /* Skip the extensions section */
670 if(ctx->phase == 4) {
671 ASN_DEBUG("skip_unknown(%d, %ld)",
672 tcv, (long)ctx->left);
673 switch(xer_skip_unknown(tcv, &ctx->left)) {
674 case -1:
675 ctx->phase = 5;
676 RETURN(RC_FAIL);
Lev Walkinc34dc462005-02-18 16:23:48 +0000677 case 1:
678 ctx->phase = 3;
Lev Walkin1e443962005-02-18 18:06:36 +0000679 /* Fall through */
Lev Walkinc34dc462005-02-18 16:23:48 +0000680 case 0:
681 XER_ADVANCE(ch_size);
682 continue;
683 case 2:
684 ctx->phase = 3;
685 break;
686 }
687 }
688
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000689 switch(tcv) {
690 case XCT_BOTH:
Lev Walkinc61f3862005-02-14 17:21:22 +0000691 break; /* No CHOICE? */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000692 case XCT_CLOSING:
Lev Walkinc61f3862005-02-14 17:21:22 +0000693 if(ctx->phase != 3)
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000694 break;
695 XER_ADVANCE(ch_size);
Lev Walkinc34dc462005-02-18 16:23:48 +0000696 ctx->phase = 5; /* Phase out */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000697 RETURN(RC_OK);
698 case XCT_OPENING:
699 if(ctx->phase == 0) {
700 XER_ADVANCE(ch_size);
701 ctx->phase = 1; /* Processing body phase */
702 continue;
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000703 }
704 /* Fall through */
Lev Walkin904e65b2005-02-18 14:23:48 +0000705 case XCT_UNKNOWN_OP:
706 case XCT_UNKNOWN_BO:
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000707
708 if(ctx->phase != 1)
709 break; /* Really unexpected */
710
711 /*
Lev Walkinc61f3862005-02-14 17:21:22 +0000712 * Search which inner member corresponds to this tag.
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000713 */
714 for(edx = 0; edx < td->elements_count; edx++) {
715 elm = &td->elements[edx];
Lev Walkinc61f3862005-02-14 17:21:22 +0000716 tcv = xer_check_tag(buf_ptr,ch_size,elm->name);
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000717 switch(tcv) {
718 case XCT_BOTH:
719 case XCT_OPENING:
720 /*
721 * Process this member.
722 */
723 ctx->step = edx;
724 ctx->phase = 2;
725 break;
Lev Walkin904e65b2005-02-18 14:23:48 +0000726 case XCT_UNKNOWN_OP:
727 case XCT_UNKNOWN_BO:
Lev Walkin6c0df202005-02-14 19:03:17 +0000728 continue;
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000729 default:
730 edx = td->elements_count;
731 break; /* Phase out */
732 }
733 break;
734 }
Lev Walkin6c0df202005-02-14 19:03:17 +0000735 if(edx != td->elements_count)
736 continue;
Lev Walkin904e65b2005-02-18 14:23:48 +0000737
738 /* It is expected extension */
Lev Walkin59b176e2005-11-26 11:25:14 +0000739 if(specs->ext_start != -1) {
Lev Walkinc34dc462005-02-18 16:23:48 +0000740 ASN_DEBUG("Got anticipated extension");
Lev Walkin904e65b2005-02-18 14:23:48 +0000741 /*
Lev Walkinc34dc462005-02-18 16:23:48 +0000742 * Check for (XCT_BOTH or XCT_UNKNOWN_BO)
743 * By using a mask. Only record a pure
744 * <opening> tags.
Lev Walkin904e65b2005-02-18 14:23:48 +0000745 */
Lev Walkinc34dc462005-02-18 16:23:48 +0000746 if(tcv & XCT_CLOSING) {
747 /* Found </extension> without body */
748 ctx->phase = 3; /* Terminating */
749 } else {
750 ctx->left = 1;
751 ctx->phase = 4; /* Skip ...'s */
752 }
753 XER_ADVANCE(ch_size);
754 continue;
Lev Walkin904e65b2005-02-18 14:23:48 +0000755 }
756
Lev Walkin6c0df202005-02-14 19:03:17 +0000757 /* Fall through */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000758 default:
759 break;
760 }
761
Lev Walkin866c8802006-09-17 08:23:35 +0000762 ASN_DEBUG("Unexpected XML tag [%c%c%c%c] in CHOICE [%s]"
763 " (ph=%d, tag=%s)",
764 ch_size>0?((const uint8_t *)buf_ptr)[0]:'?',
765 ch_size>1?((const uint8_t *)buf_ptr)[1]:'?',
766 ch_size>2?((const uint8_t *)buf_ptr)[2]:'?',
767 ch_size>3?((const uint8_t *)buf_ptr)[3]:'?',
768 td->name, ctx->phase, xml_tag);
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000769 break;
770 }
771
Lev Walkinc34dc462005-02-18 16:23:48 +0000772 ctx->phase = 5; /* Phase out, just in case */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000773 RETURN(RC_FAIL);
774}
775
776
Lev Walkina9cc46e2004-09-22 16:06:28 +0000777asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000778CHOICE_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000779 int ilevel, enum xer_encoder_flags_e flags,
780 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin5e033762004-09-29 13:26:15 +0000781 asn_CHOICE_specifics_t *specs=(asn_CHOICE_specifics_t *)td->specifics;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000782 asn_enc_rval_t er;
Lev Walkincd2f48e2017-08-10 02:14:59 -0700783 signed present;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000784
785 if(!sptr)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700786 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000787
788 /*
789 * Figure out which CHOICE element is encoded.
790 */
791 present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
792
Lev Walkin494fb702017-08-07 20:07:00 -0700793 if(present <= 0 || (unsigned)present > td->elements_count) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700794 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000795 } else {
796 asn_enc_rval_t tmper;
Lev Walkin5e033762004-09-29 13:26:15 +0000797 asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkina9cc46e2004-09-22 16:06:28 +0000798 void *memb_ptr;
799 const char *mname = elm->name;
800 unsigned int mlen = strlen(mname);
801
802 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800803 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700804 if(!memb_ptr) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000805 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800806 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000807 }
808
809 er.encoded = 0;
810
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700811 if(!(flags & XER_F_CANONICAL)) ASN__TEXT_INDENT(1, ilevel);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700812 ASN__CALLBACK3("<", 1, mname, mlen, ">", 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000813
814 tmper = elm->type->xer_encoder(elm->type, memb_ptr,
815 ilevel + 1, flags, cb, app_key);
816 if(tmper.encoded == -1) return tmper;
817
Lev Walkin7c1dc052016-03-14 03:08:15 -0700818 ASN__CALLBACK3("</", 2, mname, mlen, ">", 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000819
820 er.encoded += 5 + (2 * mlen) + tmper.encoded;
821 }
822
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700823 if(!(flags & XER_F_CANONICAL)) ASN__TEXT_INDENT(1, ilevel - 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000824
Lev Walkin7c1dc052016-03-14 03:08:15 -0700825 ASN__ENCODED_OK(er);
Lev Walkin942fd082004-10-03 09:13:02 +0000826cb_failed:
Lev Walkin7c1dc052016-03-14 03:08:15 -0700827 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000828}
829
Lev Walkin59b176e2005-11-26 11:25:14 +0000830asn_dec_rval_t
831CHOICE_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -0700832 const asn_per_constraints_t *constraints, void **sptr,
833 asn_per_data_t *pd) {
834 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
Lev Walkin59b176e2005-11-26 11:25:14 +0000835 asn_dec_rval_t rv;
Lev Walkin494fb702017-08-07 20:07:00 -0700836 const asn_per_constraint_t *ct;
Lev Walkin59b176e2005-11-26 11:25:14 +0000837 asn_TYPE_member_t *elm; /* CHOICE's element */
838 void *memb_ptr;
839 void **memb_ptr2;
840 void *st = *sptr;
841 int value;
842
Lev Walkin7c1dc052016-03-14 03:08:15 -0700843 if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx))
844 ASN__DECODE_FAILED;
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000845
Lev Walkin59b176e2005-11-26 11:25:14 +0000846 /*
847 * Create the target structure if it is not present already.
848 */
849 if(!st) {
850 st = *sptr = CALLOC(1, specs->struct_size);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700851 if(!st) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000852 }
853
854 if(constraints) ct = &constraints->value;
855 else if(td->per_constraints) ct = &td->per_constraints->value;
856 else ct = 0;
857
858 if(ct && ct->flags & APC_EXTENSIBLE) {
859 value = per_get_few_bits(pd, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700860 if(value < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000861 if(value) ct = 0; /* Not restricted */
862 }
863
864 if(ct && ct->range_bits >= 0) {
865 value = per_get_few_bits(pd, ct->range_bits);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700866 if(value < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000867 ASN_DEBUG("CHOICE %s got index %d in range %d",
868 td->name, value, ct->range_bits);
Lev Walkin0a8aa602006-09-18 20:05:55 +0000869 if(value > ct->upper_bound)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700870 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000871 } else {
872 if(specs->ext_start == -1)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700873 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000874 value = uper_get_nsnnwn(pd);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700875 if(value < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000876 value += specs->ext_start;
Lev Walkin494fb702017-08-07 20:07:00 -0700877 if((unsigned)value >= td->elements_count)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700878 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000879 }
880
881 /* Adjust if canonical order is different from natural order */
882 if(specs->canonical_order)
883 value = specs->canonical_order[value];
884
885 /* Set presence to be able to free it later */
886 _set_present_idx(st, specs->pres_offset, specs->pres_size, value + 1);
887
888 elm = &td->elements[value];
889 if(elm->flags & ATF_POINTER) {
890 /* Member is a pointer to another structure */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800891 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
Lev Walkin59b176e2005-11-26 11:25:14 +0000892 } else {
893 memb_ptr = (char *)st + elm->memb_offset;
894 memb_ptr2 = &memb_ptr;
895 }
896 ASN_DEBUG("Discovered CHOICE %s encodes %s", td->name, elm->name);
897
Lev Walkin6f9060f2007-06-29 01:45:03 +0000898 if(ct && ct->range_bits >= 0) {
899 rv = elm->type->uper_decoder(opt_codec_ctx, elm->type,
Lev Walkin59b176e2005-11-26 11:25:14 +0000900 elm->per_constraints, memb_ptr2, pd);
Lev Walkin6f9060f2007-06-29 01:45:03 +0000901 } else {
902 rv = uper_open_type_get(opt_codec_ctx, elm->type,
903 elm->per_constraints, memb_ptr2, pd);
904 }
905
Lev Walkin59b176e2005-11-26 11:25:14 +0000906 if(rv.code != RC_OK)
Lev Walkin6f9060f2007-06-29 01:45:03 +0000907 ASN_DEBUG("Failed to decode %s in %s (CHOICE) %d",
908 elm->name, td->name, rv.code);
Lev Walkin59b176e2005-11-26 11:25:14 +0000909 return rv;
910}
Lev Walkin494fb702017-08-07 20:07:00 -0700911
Lev Walkin523de9e2006-08-18 01:34:18 +0000912asn_enc_rval_t
913CHOICE_encode_uper(asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -0700914 const asn_per_constraints_t *constraints, void *sptr,
915 asn_per_outp_t *po) {
916 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
Lev Walkin523de9e2006-08-18 01:34:18 +0000917 asn_TYPE_member_t *elm; /* CHOICE's element */
Lev Walkin494fb702017-08-07 20:07:00 -0700918 const asn_per_constraint_t *ct;
Lev Walkin523de9e2006-08-18 01:34:18 +0000919 void *memb_ptr;
Lev Walkincd2f48e2017-08-10 02:14:59 -0700920 signed present;
Lev Walkine14480f2013-03-24 03:28:00 -0700921 int present_enc;
Lev Walkin523de9e2006-08-18 01:34:18 +0000922
Lev Walkin7c1dc052016-03-14 03:08:15 -0700923 if(!sptr) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000924
925 ASN_DEBUG("Encoding %s as CHOICE", td->name);
926
927 if(constraints) ct = &constraints->value;
928 else if(td->per_constraints) ct = &td->per_constraints->value;
929 else ct = 0;
930
931 present = _fetch_present_idx(sptr,
932 specs->pres_offset, specs->pres_size);
933
934 /*
935 * If the structure was not initialized properly, it cannot be encoded:
936 * can't deduce what to encode in the choice type.
937 */
Lev Walkin494fb702017-08-07 20:07:00 -0700938 if(present <= 0 || (unsigned)present > td->elements_count)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700939 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000940 else
941 present--;
942
Lev Walkin523de9e2006-08-18 01:34:18 +0000943 ASN_DEBUG("Encoding %s CHOICE element %d", td->name, present);
944
Lev Walkine14480f2013-03-24 03:28:00 -0700945 /* Adjust if canonical order is different from natural order */
946 if(specs->canonical_order)
947 present_enc = specs->canonical_order[present];
948 else
949 present_enc = present;
950
Lev Walkin523de9e2006-08-18 01:34:18 +0000951 if(ct && ct->range_bits >= 0) {
Lev Walkine14480f2013-03-24 03:28:00 -0700952 if(present_enc < ct->lower_bound
953 || present_enc > ct->upper_bound) {
Lev Walkin523de9e2006-08-18 01:34:18 +0000954 if(ct->flags & APC_EXTENSIBLE) {
955 if(per_put_few_bits(po, 1, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700956 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000957 } else {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700958 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000959 }
960 ct = 0;
961 }
962 }
963 if(ct && ct->flags & APC_EXTENSIBLE)
964 if(per_put_few_bits(po, 0, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700965 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000966
Lev Walkin523de9e2006-08-18 01:34:18 +0000967 elm = &td->elements[present];
968 if(elm->flags & ATF_POINTER) {
969 /* Member is a pointer to another structure */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800970 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700971 if(!memb_ptr) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000972 } else {
973 memb_ptr = (char *)sptr + elm->memb_offset;
974 }
975
Lev Walkin6f9060f2007-06-29 01:45:03 +0000976 if(ct && ct->range_bits >= 0) {
Lev Walkine14480f2013-03-24 03:28:00 -0700977 if(per_put_few_bits(po, present_enc, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700978 ASN__ENCODE_FAILED;
Lev Walkin6f9060f2007-06-29 01:45:03 +0000979
980 return elm->type->uper_encoder(elm->type, elm->per_constraints,
Lev Walkin523de9e2006-08-18 01:34:18 +0000981 memb_ptr, po);
Lev Walkin6f9060f2007-06-29 01:45:03 +0000982 } else {
983 asn_enc_rval_t rval;
984 if(specs->ext_start == -1)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700985 ASN__ENCODE_FAILED;
Lev Walkine14480f2013-03-24 03:28:00 -0700986 if(uper_put_nsnnwn(po, present_enc - specs->ext_start))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700987 ASN__ENCODE_FAILED;
Lev Walkin6f9060f2007-06-29 01:45:03 +0000988 if(uper_open_type_put(elm->type, elm->per_constraints,
989 memb_ptr, po))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700990 ASN__ENCODE_FAILED;
Lev Walkin6f9060f2007-06-29 01:45:03 +0000991 rval.encoded = 0;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700992 ASN__ENCODED_OK(rval);
Lev Walkin6f9060f2007-06-29 01:45:03 +0000993 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000994}
995
Lev Walkin59b176e2005-11-26 11:25:14 +0000996
Lev Walkinf15320b2004-06-03 03:38:44 +0000997int
Lev Walkin5e033762004-09-29 13:26:15 +0000998CHOICE_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000999 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin5e033762004-09-29 13:26:15 +00001000 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
Lev Walkincd2f48e2017-08-10 02:14:59 -07001001 signed present;
Lev Walkinf15320b2004-06-03 03:38:44 +00001002
Lev Walkin8e8078a2004-09-26 13:10:40 +00001003 if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001004
1005 /*
1006 * Figure out which CHOICE element is encoded.
1007 */
1008 present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
1009
1010 /*
Lev Walkina9cc46e2004-09-22 16:06:28 +00001011 * Print that element.
Lev Walkinf15320b2004-06-03 03:38:44 +00001012 */
Lev Walkin494fb702017-08-07 20:07:00 -07001013 if(present > 0 && (unsigned)present <= td->elements_count) {
Lev Walkin5e033762004-09-29 13:26:15 +00001014 asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkinf15320b2004-06-03 03:38:44 +00001015 const void *memb_ptr;
1016
Lev Walkincc93b0f2004-09-10 09:18:20 +00001017 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001018 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkin8e8078a2004-09-26 13:10:40 +00001019 if(!memb_ptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001020 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001021 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +00001022 }
1023
1024 /* Print member's name and stuff */
Lev Walkincc6a9102004-09-23 22:06:26 +00001025 if(0) {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001026 if(cb(elm->name, strlen(elm->name), app_key) < 0
1027 || cb(": ", 2, app_key) < 0)
Lev Walkincc6a9102004-09-23 22:06:26 +00001028 return -1;
1029 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001030
1031 return elm->type->print_struct(elm->type, memb_ptr, ilevel,
1032 cb, app_key);
1033 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001034 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001035 }
1036}
1037
1038void
Lev Walkin5e033762004-09-29 13:26:15 +00001039CHOICE_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
1040 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
Lev Walkincd2f48e2017-08-10 02:14:59 -07001041 signed present;
Lev Walkinf15320b2004-06-03 03:38:44 +00001042
1043 if(!td || !ptr)
1044 return;
1045
1046 ASN_DEBUG("Freeing %s as CHOICE", td->name);
1047
1048 /*
1049 * Figure out which CHOICE element is encoded.
1050 */
1051 present = _fetch_present_idx(ptr, specs->pres_offset, specs->pres_size);
1052
1053 /*
1054 * Free that element.
1055 */
Lev Walkin494fb702017-08-07 20:07:00 -07001056 if(present > 0 && (unsigned)present <= td->elements_count) {
Lev Walkin5e033762004-09-29 13:26:15 +00001057 asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkinf15320b2004-06-03 03:38:44 +00001058 void *memb_ptr;
1059
Lev Walkincc93b0f2004-09-10 09:18:20 +00001060 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001061 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +00001062 if(memb_ptr)
Lev Walkinadcb5862006-03-17 02:11:12 +00001063 ASN_STRUCT_FREE(*elm->type, memb_ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +00001064 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001065 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
Lev Walkinadcb5862006-03-17 02:11:12 +00001066 ASN_STRUCT_FREE_CONTENTS_ONLY(*elm->type, memb_ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +00001067 }
1068 }
1069
1070 if(!contents_only) {
1071 FREEMEM(ptr);
1072 }
1073}
1074
1075
1076/*
1077 * The following functions functions offer protection against -fshort-enums,
1078 * compatible with little- and big-endian machines.
1079 * If assertion is triggered, either disable -fshort-enums, or add an entry
1080 * here with the ->pres_size of your target stracture.
1081 * Unless the target structure is packed, the ".present" member
1082 * is guaranteed to be aligned properly. ASN.1 compiler itself does not
1083 * produce packed code.
1084 */
Lev Walkincd2f48e2017-08-10 02:14:59 -07001085static signed
Lev Walkinf15320b2004-06-03 03:38:44 +00001086_fetch_present_idx(const void *struct_ptr, int pres_offset, int pres_size) {
1087 const void *present_ptr;
Lev Walkincd2f48e2017-08-10 02:14:59 -07001088 signed present;
Lev Walkinf15320b2004-06-03 03:38:44 +00001089
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001090 present_ptr = ((const char *)struct_ptr) + pres_offset;
Lev Walkinf15320b2004-06-03 03:38:44 +00001091
1092 switch(pres_size) {
1093 case sizeof(int): present = *(const int *)present_ptr; break;
1094 case sizeof(short): present = *(const short *)present_ptr; break;
1095 case sizeof(char): present = *(const char *)present_ptr; break;
1096 default:
1097 /* ANSI C mandates enum to be equivalent to integer */
1098 assert(pres_size != sizeof(int));
1099 return 0; /* If not aborted, pass back safe value */
1100 }
1101
1102 return present;
1103}
1104
Lev Walkin91f5cd02004-08-11 09:10:59 +00001105static void
Lev Walkinf15320b2004-06-03 03:38:44 +00001106_set_present_idx(void *struct_ptr, int pres_offset, int pres_size, int present) {
1107 void *present_ptr;
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001108 present_ptr = ((char *)struct_ptr) + pres_offset;
Lev Walkinf15320b2004-06-03 03:38:44 +00001109
1110 switch(pres_size) {
1111 case sizeof(int): *(int *)present_ptr = present; break;
1112 case sizeof(short): *(short *)present_ptr = present; break;
1113 case sizeof(char): *(char *)present_ptr = present; break;
1114 default:
1115 /* ANSI C mandates enum to be equivalent to integer */
1116 assert(pres_size != sizeof(int));
1117 }
1118}
Lev Walkincd2f48e2017-08-10 02:14:59 -07001119
1120static const void *
1121_get_member_ptr(const asn_TYPE_descriptor_t *td, const void *sptr,
1122 asn_TYPE_member_t **elm_ptr, signed *present_out) {
1123 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
1124 signed present;
1125
1126 if(!sptr) {
1127 *elm_ptr = NULL;
1128 *present_out = 0;
1129 return NULL;
1130 }
1131
1132 /*
1133 * Figure out which CHOICE element is encoded.
1134 */
1135 present = _fetch_present_idx(sptr, specs->pres_offset, specs->pres_size);
1136 *present_out = present;
1137
1138 /*
1139 * The presence index is intentionally 1-based to avoid
1140 * treating zeroed structure as a valid one.
1141 */
1142 if(present > 0 && (unsigned)present <= td->elements_count) {
1143 asn_TYPE_member_t *const elm = &td->elements[present - 1];
1144 const void *memb_ptr;
1145
1146 if(elm->flags & ATF_POINTER) {
1147 memb_ptr =
1148 *(const void *const *)((const char *)sptr + elm->memb_offset);
1149 } else {
1150 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
1151 }
1152 *elm_ptr = elm;
1153 return memb_ptr;
1154 } else {
1155 *elm_ptr = NULL;
1156 return NULL;
1157 }
1158
1159}
1160
1161int
1162CHOICE_compare(const asn_TYPE_descriptor_t *td, const void *aptr, const void *bptr) {
1163 asn_TYPE_member_t *aelm;
1164 asn_TYPE_member_t *belm;
1165 signed apresent = 0;
1166 signed bpresent = 0;
1167 const void *amember = _get_member_ptr(td, aptr, &aelm, &apresent);
1168 const void *bmember = _get_member_ptr(td, bptr, &belm, &apresent);
1169
1170 if(amember && bmember) {
1171 if(apresent == bpresent) {
1172 assert(aelm == belm);
1173 return aelm->type->compare_struct(aelm->type, amember, bmember);
1174 } else if(apresent < bpresent) {
1175 return -1;
1176 } else {
1177 return 1;
1178 }
1179 } else if(!amember) {
1180 return -1;
1181 } else {
1182 return 1;
1183 }
1184}