blob: 86b3601cd2e0f457d8957d1bbbc175c39062ce9c [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 Walkin5e033762004-09-29 13:26:15 +0000106CHOICE_decode_ber(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 */
Lev Walkin5e033762004-09-29 13:26:15 +0000267 rval = elm->type->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 */
423 erval = elm->type->der_encoder(elm->type, memb_ptr,
424 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 */
439 erval = elm->type->der_encoder(elm->type, memb_ptr,
440 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
552CHOICE_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
553 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 */
608
Lev Walkinabf68892004-10-26 10:12:14 +0000609 elm = &td->elements[edx];
610
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000611 if(elm->flags & ATF_POINTER) {
612 /* Member is a pointer to another structure */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800613 memb_ptr2 = (void **)((char *)st
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000614 + elm->memb_offset);
615 } else {
616 memb_ptr = (char *)st + elm->memb_offset;
617 memb_ptr2 = &memb_ptr;
618 }
619
Lev Walkinc61f3862005-02-14 17:21:22 +0000620 /* Start/Continue decoding the inner member */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000621 tmprval = elm->type->xer_decoder(opt_codec_ctx,
622 elm->type, memb_ptr2, elm->name,
623 buf_ptr, size);
624 XER_ADVANCE(tmprval.consumed);
Lev Walkin866c8802006-09-17 08:23:35 +0000625 ASN_DEBUG("XER/CHOICE: itdf: [%s] code=%d",
626 elm->type->name, tmprval.code);
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000627 if(tmprval.code != RC_OK)
628 RETURN(tmprval.code);
Lev Walkinc61f3862005-02-14 17:21:22 +0000629 assert(_fetch_present_idx(st,
630 specs->pres_offset, specs->pres_size) == 0);
631 /* Record what we've got */
632 _set_present_idx(st,
633 specs->pres_offset, specs->pres_size, edx + 1);
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000634 ctx->phase = 3;
635 /* Fall through */
636 }
637
Lev Walkind1bfea62005-11-08 03:06:16 +0000638 /* No need to wait for closing tag; special mode. */
639 if(ctx->phase == 3 && !*xml_tag) {
640 ctx->phase = 5; /* Phase out */
641 RETURN(RC_OK);
642 }
643
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000644 /*
645 * Get the next part of the XML stream.
646 */
Lev Walkin1e443962005-02-18 18:06:36 +0000647 ch_size = xer_next_token(&ctx->context, buf_ptr, size, &ch_type);
Lev Walkin97363482016-01-24 19:23:02 -0800648 if(ch_size == -1) {
649 RETURN(RC_FAIL);
650 } else {
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000651 switch(ch_type) {
Lev Walkin97363482016-01-24 19:23:02 -0800652 case PXER_WMORE:
653 RETURN(RC_WMORE);
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000654 case PXER_COMMENT: /* Got XML comment */
655 case PXER_TEXT: /* Ignore free-standing text */
656 XER_ADVANCE(ch_size); /* Skip silently */
657 continue;
658 case PXER_TAG:
659 break; /* Check the rest down there */
660 }
661 }
662
Lev Walkinc61f3862005-02-14 17:21:22 +0000663 tcv = xer_check_tag(buf_ptr, ch_size, xml_tag);
Lev Walkind1bfea62005-11-08 03:06:16 +0000664 ASN_DEBUG("XER/CHOICE checked [%c%c%c%c] vs [%s], tcv=%d",
Lev Walkin32788732005-11-13 09:12:23 +0000665 ch_size>0?((const uint8_t *)buf_ptr)[0]:'?',
666 ch_size>1?((const uint8_t *)buf_ptr)[1]:'?',
667 ch_size>2?((const uint8_t *)buf_ptr)[2]:'?',
668 ch_size>3?((const uint8_t *)buf_ptr)[3]:'?',
Lev Walkind1bfea62005-11-08 03:06:16 +0000669 xml_tag, tcv);
Lev Walkinc34dc462005-02-18 16:23:48 +0000670
671 /* Skip the extensions section */
672 if(ctx->phase == 4) {
673 ASN_DEBUG("skip_unknown(%d, %ld)",
674 tcv, (long)ctx->left);
675 switch(xer_skip_unknown(tcv, &ctx->left)) {
676 case -1:
677 ctx->phase = 5;
678 RETURN(RC_FAIL);
Lev Walkinc34dc462005-02-18 16:23:48 +0000679 case 1:
680 ctx->phase = 3;
Lev Walkin1e443962005-02-18 18:06:36 +0000681 /* Fall through */
Lev Walkinc34dc462005-02-18 16:23:48 +0000682 case 0:
683 XER_ADVANCE(ch_size);
684 continue;
685 case 2:
686 ctx->phase = 3;
687 break;
688 }
689 }
690
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000691 switch(tcv) {
692 case XCT_BOTH:
Lev Walkinc61f3862005-02-14 17:21:22 +0000693 break; /* No CHOICE? */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000694 case XCT_CLOSING:
Lev Walkinc61f3862005-02-14 17:21:22 +0000695 if(ctx->phase != 3)
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000696 break;
697 XER_ADVANCE(ch_size);
Lev Walkinc34dc462005-02-18 16:23:48 +0000698 ctx->phase = 5; /* Phase out */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000699 RETURN(RC_OK);
700 case XCT_OPENING:
701 if(ctx->phase == 0) {
702 XER_ADVANCE(ch_size);
703 ctx->phase = 1; /* Processing body phase */
704 continue;
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000705 }
706 /* Fall through */
Lev Walkin904e65b2005-02-18 14:23:48 +0000707 case XCT_UNKNOWN_OP:
708 case XCT_UNKNOWN_BO:
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000709
710 if(ctx->phase != 1)
711 break; /* Really unexpected */
712
713 /*
Lev Walkinc61f3862005-02-14 17:21:22 +0000714 * Search which inner member corresponds to this tag.
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000715 */
716 for(edx = 0; edx < td->elements_count; edx++) {
717 elm = &td->elements[edx];
Lev Walkinc61f3862005-02-14 17:21:22 +0000718 tcv = xer_check_tag(buf_ptr,ch_size,elm->name);
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000719 switch(tcv) {
720 case XCT_BOTH:
721 case XCT_OPENING:
722 /*
723 * Process this member.
724 */
725 ctx->step = edx;
726 ctx->phase = 2;
727 break;
Lev Walkin904e65b2005-02-18 14:23:48 +0000728 case XCT_UNKNOWN_OP:
729 case XCT_UNKNOWN_BO:
Lev Walkin6c0df202005-02-14 19:03:17 +0000730 continue;
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000731 default:
732 edx = td->elements_count;
733 break; /* Phase out */
734 }
735 break;
736 }
Lev Walkin6c0df202005-02-14 19:03:17 +0000737 if(edx != td->elements_count)
738 continue;
Lev Walkin904e65b2005-02-18 14:23:48 +0000739
740 /* It is expected extension */
Lev Walkin59b176e2005-11-26 11:25:14 +0000741 if(specs->ext_start != -1) {
Lev Walkinc34dc462005-02-18 16:23:48 +0000742 ASN_DEBUG("Got anticipated extension");
Lev Walkin904e65b2005-02-18 14:23:48 +0000743 /*
Lev Walkinc34dc462005-02-18 16:23:48 +0000744 * Check for (XCT_BOTH or XCT_UNKNOWN_BO)
745 * By using a mask. Only record a pure
746 * <opening> tags.
Lev Walkin904e65b2005-02-18 14:23:48 +0000747 */
Lev Walkinc34dc462005-02-18 16:23:48 +0000748 if(tcv & XCT_CLOSING) {
749 /* Found </extension> without body */
750 ctx->phase = 3; /* Terminating */
751 } else {
752 ctx->left = 1;
753 ctx->phase = 4; /* Skip ...'s */
754 }
755 XER_ADVANCE(ch_size);
756 continue;
Lev Walkin904e65b2005-02-18 14:23:48 +0000757 }
758
Lev Walkin6c0df202005-02-14 19:03:17 +0000759 /* Fall through */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000760 default:
761 break;
762 }
763
Lev Walkin866c8802006-09-17 08:23:35 +0000764 ASN_DEBUG("Unexpected XML tag [%c%c%c%c] in CHOICE [%s]"
765 " (ph=%d, tag=%s)",
766 ch_size>0?((const uint8_t *)buf_ptr)[0]:'?',
767 ch_size>1?((const uint8_t *)buf_ptr)[1]:'?',
768 ch_size>2?((const uint8_t *)buf_ptr)[2]:'?',
769 ch_size>3?((const uint8_t *)buf_ptr)[3]:'?',
770 td->name, ctx->phase, xml_tag);
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000771 break;
772 }
773
Lev Walkinc34dc462005-02-18 16:23:48 +0000774 ctx->phase = 5; /* Phase out, just in case */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000775 RETURN(RC_FAIL);
776}
777
778
Lev Walkina9cc46e2004-09-22 16:06:28 +0000779asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000780CHOICE_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000781 int ilevel, enum xer_encoder_flags_e flags,
782 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin5e033762004-09-29 13:26:15 +0000783 asn_CHOICE_specifics_t *specs=(asn_CHOICE_specifics_t *)td->specifics;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000784 asn_enc_rval_t er;
Lev Walkin63a35232017-08-10 19:59:47 -0700785 unsigned present;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000786
787 if(!sptr)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700788 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000789
790 /*
791 * Figure out which CHOICE element is encoded.
792 */
793 present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
794
Lev Walkin63a35232017-08-10 19:59:47 -0700795 if(present == 0 || present > td->elements_count) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700796 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000797 } else {
798 asn_enc_rval_t tmper;
Lev Walkin5e033762004-09-29 13:26:15 +0000799 asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkina9cc46e2004-09-22 16:06:28 +0000800 void *memb_ptr;
801 const char *mname = elm->name;
802 unsigned int mlen = strlen(mname);
803
804 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800805 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700806 if(!memb_ptr) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000807 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800808 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000809 }
810
811 er.encoded = 0;
812
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700813 if(!(flags & XER_F_CANONICAL)) ASN__TEXT_INDENT(1, ilevel);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700814 ASN__CALLBACK3("<", 1, mname, mlen, ">", 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000815
816 tmper = elm->type->xer_encoder(elm->type, memb_ptr,
817 ilevel + 1, flags, cb, app_key);
818 if(tmper.encoded == -1) return tmper;
819
Lev Walkin7c1dc052016-03-14 03:08:15 -0700820 ASN__CALLBACK3("</", 2, mname, mlen, ">", 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000821
822 er.encoded += 5 + (2 * mlen) + tmper.encoded;
823 }
824
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700825 if(!(flags & XER_F_CANONICAL)) ASN__TEXT_INDENT(1, ilevel - 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000826
Lev Walkin7c1dc052016-03-14 03:08:15 -0700827 ASN__ENCODED_OK(er);
Lev Walkin942fd082004-10-03 09:13:02 +0000828cb_failed:
Lev Walkin7c1dc052016-03-14 03:08:15 -0700829 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000830}
831
Lev Walkin59b176e2005-11-26 11:25:14 +0000832asn_dec_rval_t
833CHOICE_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -0700834 const asn_per_constraints_t *constraints, void **sptr,
835 asn_per_data_t *pd) {
836 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
Lev Walkin59b176e2005-11-26 11:25:14 +0000837 asn_dec_rval_t rv;
Lev Walkin494fb702017-08-07 20:07:00 -0700838 const asn_per_constraint_t *ct;
Lev Walkin59b176e2005-11-26 11:25:14 +0000839 asn_TYPE_member_t *elm; /* CHOICE's element */
840 void *memb_ptr;
841 void **memb_ptr2;
842 void *st = *sptr;
843 int value;
844
Lev Walkin7c1dc052016-03-14 03:08:15 -0700845 if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx))
846 ASN__DECODE_FAILED;
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000847
Lev Walkin59b176e2005-11-26 11:25:14 +0000848 /*
849 * Create the target structure if it is not present already.
850 */
851 if(!st) {
852 st = *sptr = CALLOC(1, specs->struct_size);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700853 if(!st) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000854 }
855
856 if(constraints) ct = &constraints->value;
857 else if(td->per_constraints) ct = &td->per_constraints->value;
858 else ct = 0;
859
860 if(ct && ct->flags & APC_EXTENSIBLE) {
861 value = per_get_few_bits(pd, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700862 if(value < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000863 if(value) ct = 0; /* Not restricted */
864 }
865
866 if(ct && ct->range_bits >= 0) {
867 value = per_get_few_bits(pd, ct->range_bits);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700868 if(value < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000869 ASN_DEBUG("CHOICE %s got index %d in range %d",
870 td->name, value, ct->range_bits);
Lev Walkin0a8aa602006-09-18 20:05:55 +0000871 if(value > ct->upper_bound)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700872 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000873 } else {
874 if(specs->ext_start == -1)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700875 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000876 value = uper_get_nsnnwn(pd);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700877 if(value < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000878 value += specs->ext_start;
Lev Walkin494fb702017-08-07 20:07:00 -0700879 if((unsigned)value >= td->elements_count)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700880 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000881 }
882
883 /* Adjust if canonical order is different from natural order */
884 if(specs->canonical_order)
885 value = specs->canonical_order[value];
886
887 /* Set presence to be able to free it later */
888 _set_present_idx(st, specs->pres_offset, specs->pres_size, value + 1);
889
890 elm = &td->elements[value];
891 if(elm->flags & ATF_POINTER) {
892 /* Member is a pointer to another structure */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800893 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
Lev Walkin59b176e2005-11-26 11:25:14 +0000894 } else {
895 memb_ptr = (char *)st + elm->memb_offset;
896 memb_ptr2 = &memb_ptr;
897 }
898 ASN_DEBUG("Discovered CHOICE %s encodes %s", td->name, elm->name);
899
Lev Walkin6f9060f2007-06-29 01:45:03 +0000900 if(ct && ct->range_bits >= 0) {
901 rv = elm->type->uper_decoder(opt_codec_ctx, elm->type,
Lev Walkin59b176e2005-11-26 11:25:14 +0000902 elm->per_constraints, memb_ptr2, pd);
Lev Walkin6f9060f2007-06-29 01:45:03 +0000903 } else {
904 rv = uper_open_type_get(opt_codec_ctx, elm->type,
905 elm->per_constraints, memb_ptr2, pd);
906 }
907
Lev Walkin59b176e2005-11-26 11:25:14 +0000908 if(rv.code != RC_OK)
Lev Walkin6f9060f2007-06-29 01:45:03 +0000909 ASN_DEBUG("Failed to decode %s in %s (CHOICE) %d",
910 elm->name, td->name, rv.code);
Lev Walkin59b176e2005-11-26 11:25:14 +0000911 return rv;
912}
Lev Walkin494fb702017-08-07 20:07:00 -0700913
Lev Walkin523de9e2006-08-18 01:34:18 +0000914asn_enc_rval_t
915CHOICE_encode_uper(asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -0700916 const asn_per_constraints_t *constraints, void *sptr,
917 asn_per_outp_t *po) {
918 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
Lev Walkin523de9e2006-08-18 01:34:18 +0000919 asn_TYPE_member_t *elm; /* CHOICE's element */
Lev Walkin494fb702017-08-07 20:07:00 -0700920 const asn_per_constraint_t *ct;
Lev Walkin523de9e2006-08-18 01:34:18 +0000921 void *memb_ptr;
Lev Walkin63a35232017-08-10 19:59:47 -0700922 unsigned present;
Lev Walkine14480f2013-03-24 03:28:00 -0700923 int present_enc;
Lev Walkin523de9e2006-08-18 01:34:18 +0000924
Lev Walkin7c1dc052016-03-14 03:08:15 -0700925 if(!sptr) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000926
927 ASN_DEBUG("Encoding %s as CHOICE", td->name);
928
929 if(constraints) ct = &constraints->value;
930 else if(td->per_constraints) ct = &td->per_constraints->value;
931 else ct = 0;
932
Lev Walkin63a35232017-08-10 19:59:47 -0700933 present = _fetch_present_idx(sptr, specs->pres_offset, specs->pres_size);
Lev Walkin523de9e2006-08-18 01:34:18 +0000934
Lev Walkin63a35232017-08-10 19:59:47 -0700935 /*
Lev Walkin523de9e2006-08-18 01:34:18 +0000936 * If the structure was not initialized properly, it cannot be encoded:
937 * can't deduce what to encode in the choice type.
938 */
Lev Walkin63a35232017-08-10 19:59:47 -0700939 if(present == 0 || present > td->elements_count)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700940 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000941 else
942 present--;
943
Lev Walkin523de9e2006-08-18 01:34:18 +0000944 ASN_DEBUG("Encoding %s CHOICE element %d", td->name, present);
945
Lev Walkine14480f2013-03-24 03:28:00 -0700946 /* Adjust if canonical order is different from natural order */
947 if(specs->canonical_order)
948 present_enc = specs->canonical_order[present];
949 else
950 present_enc = present;
951
Lev Walkin523de9e2006-08-18 01:34:18 +0000952 if(ct && ct->range_bits >= 0) {
Lev Walkine14480f2013-03-24 03:28:00 -0700953 if(present_enc < ct->lower_bound
954 || present_enc > ct->upper_bound) {
Lev Walkin523de9e2006-08-18 01:34:18 +0000955 if(ct->flags & APC_EXTENSIBLE) {
956 if(per_put_few_bits(po, 1, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700957 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000958 } else {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700959 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000960 }
961 ct = 0;
962 }
963 }
964 if(ct && ct->flags & APC_EXTENSIBLE)
965 if(per_put_few_bits(po, 0, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700966 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000967
Lev Walkin523de9e2006-08-18 01:34:18 +0000968 elm = &td->elements[present];
969 if(elm->flags & ATF_POINTER) {
970 /* Member is a pointer to another structure */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800971 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700972 if(!memb_ptr) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000973 } else {
974 memb_ptr = (char *)sptr + elm->memb_offset;
975 }
976
Lev Walkin6f9060f2007-06-29 01:45:03 +0000977 if(ct && ct->range_bits >= 0) {
Lev Walkine14480f2013-03-24 03:28:00 -0700978 if(per_put_few_bits(po, present_enc, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700979 ASN__ENCODE_FAILED;
Lev Walkin6f9060f2007-06-29 01:45:03 +0000980
981 return elm->type->uper_encoder(elm->type, elm->per_constraints,
Lev Walkin523de9e2006-08-18 01:34:18 +0000982 memb_ptr, po);
Lev Walkin6f9060f2007-06-29 01:45:03 +0000983 } else {
984 asn_enc_rval_t rval;
985 if(specs->ext_start == -1)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700986 ASN__ENCODE_FAILED;
Lev Walkine14480f2013-03-24 03:28:00 -0700987 if(uper_put_nsnnwn(po, present_enc - specs->ext_start))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700988 ASN__ENCODE_FAILED;
Lev Walkin6f9060f2007-06-29 01:45:03 +0000989 if(uper_open_type_put(elm->type, elm->per_constraints,
990 memb_ptr, po))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700991 ASN__ENCODE_FAILED;
Lev Walkin6f9060f2007-06-29 01:45:03 +0000992 rval.encoded = 0;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700993 ASN__ENCODED_OK(rval);
Lev Walkin6f9060f2007-06-29 01:45:03 +0000994 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000995}
996
Lev Walkin59b176e2005-11-26 11:25:14 +0000997
Lev Walkinf15320b2004-06-03 03:38:44 +0000998int
Lev Walkin5e033762004-09-29 13:26:15 +0000999CHOICE_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +00001000 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin5e033762004-09-29 13:26:15 +00001001 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
Lev Walkin63a35232017-08-10 19:59:47 -07001002 unsigned present;
Lev Walkinf15320b2004-06-03 03:38:44 +00001003
Lev Walkin8e8078a2004-09-26 13:10:40 +00001004 if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001005
1006 /*
1007 * Figure out which CHOICE element is encoded.
1008 */
1009 present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
1010
1011 /*
Lev Walkina9cc46e2004-09-22 16:06:28 +00001012 * Print that element.
Lev Walkinf15320b2004-06-03 03:38:44 +00001013 */
Lev Walkin63a35232017-08-10 19:59:47 -07001014 if(present > 0 && present <= td->elements_count) {
Lev Walkin5e033762004-09-29 13:26:15 +00001015 asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkinf15320b2004-06-03 03:38:44 +00001016 const void *memb_ptr;
1017
Lev Walkincc93b0f2004-09-10 09:18:20 +00001018 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001019 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkin8e8078a2004-09-26 13:10:40 +00001020 if(!memb_ptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001021 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001022 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +00001023 }
1024
1025 /* Print member's name and stuff */
Lev Walkincc6a9102004-09-23 22:06:26 +00001026 if(0) {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001027 if(cb(elm->name, strlen(elm->name), app_key) < 0
1028 || cb(": ", 2, app_key) < 0)
Lev Walkincc6a9102004-09-23 22:06:26 +00001029 return -1;
1030 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001031
1032 return elm->type->print_struct(elm->type, memb_ptr, ilevel,
1033 cb, app_key);
1034 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001035 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001036 }
1037}
1038
1039void
Lev Walkinf6853ce2017-08-11 00:50:27 -07001040CHOICE_free(const asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
1041 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
Lev Walkin63a35232017-08-10 19:59:47 -07001042 unsigned present;
Lev Walkinf15320b2004-06-03 03:38:44 +00001043
1044 if(!td || !ptr)
1045 return;
1046
1047 ASN_DEBUG("Freeing %s as CHOICE", td->name);
1048
1049 /*
1050 * Figure out which CHOICE element is encoded.
1051 */
1052 present = _fetch_present_idx(ptr, specs->pres_offset, specs->pres_size);
1053
1054 /*
1055 * Free that element.
1056 */
Lev Walkin63a35232017-08-10 19:59:47 -07001057 if(present > 0 && present <= td->elements_count) {
Lev Walkin5e033762004-09-29 13:26:15 +00001058 asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkinf15320b2004-06-03 03:38:44 +00001059 void *memb_ptr;
1060
Lev Walkincc93b0f2004-09-10 09:18:20 +00001061 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001062 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +00001063 if(memb_ptr)
Lev Walkinadcb5862006-03-17 02:11:12 +00001064 ASN_STRUCT_FREE(*elm->type, memb_ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +00001065 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001066 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
Lev Walkinadcb5862006-03-17 02:11:12 +00001067 ASN_STRUCT_FREE_CONTENTS_ONLY(*elm->type, memb_ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +00001068 }
1069 }
1070
1071 if(!contents_only) {
1072 FREEMEM(ptr);
1073 }
1074}
1075
1076
1077/*
1078 * The following functions functions offer protection against -fshort-enums,
1079 * compatible with little- and big-endian machines.
1080 * If assertion is triggered, either disable -fshort-enums, or add an entry
1081 * here with the ->pres_size of your target stracture.
1082 * Unless the target structure is packed, the ".present" member
1083 * is guaranteed to be aligned properly. ASN.1 compiler itself does not
1084 * produce packed code.
1085 */
Lev Walkinf6853ce2017-08-11 00:50:27 -07001086static unsigned
1087_fetch_present_idx(const void *struct_ptr, unsigned pres_offset,
1088 unsigned pres_size) {
1089 const void *present_ptr;
Lev Walkin63a35232017-08-10 19:59:47 -07001090 unsigned present;
Lev Walkinf15320b2004-06-03 03:38:44 +00001091
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001092 present_ptr = ((const char *)struct_ptr) + pres_offset;
Lev Walkinf15320b2004-06-03 03:38:44 +00001093
1094 switch(pres_size) {
Lev Walkinf6853ce2017-08-11 00:50:27 -07001095 case sizeof(int): present = *(const unsigned int *)present_ptr; break;
1096 case sizeof(short): present = *(const unsigned short *)present_ptr; break;
1097 case sizeof(char): present = *(const unsigned char *)present_ptr; break;
Lev Walkinf15320b2004-06-03 03:38:44 +00001098 default:
1099 /* ANSI C mandates enum to be equivalent to integer */
1100 assert(pres_size != sizeof(int));
1101 return 0; /* If not aborted, pass back safe value */
1102 }
1103
1104 return present;
1105}
1106
Lev Walkin91f5cd02004-08-11 09:10:59 +00001107static void
Lev Walkinf6853ce2017-08-11 00:50:27 -07001108_set_present_idx(void *struct_ptr, unsigned pres_offset, unsigned pres_size,
1109 unsigned present) {
1110 void *present_ptr;
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001111 present_ptr = ((char *)struct_ptr) + pres_offset;
Lev Walkinf15320b2004-06-03 03:38:44 +00001112
1113 switch(pres_size) {
Lev Walkinf6853ce2017-08-11 00:50:27 -07001114 case sizeof(int): *(unsigned int *)present_ptr = present; break;
1115 case sizeof(short): *(unsigned short *)present_ptr = present; break;
1116 case sizeof(char): *(unsigned char *)present_ptr = present; break;
Lev Walkinf15320b2004-06-03 03:38:44 +00001117 default:
1118 /* ANSI C mandates enum to be equivalent to integer */
1119 assert(pres_size != sizeof(int));
1120 }
1121}
Lev Walkincd2f48e2017-08-10 02:14:59 -07001122
1123static const void *
1124_get_member_ptr(const asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin63a35232017-08-10 19:59:47 -07001125 asn_TYPE_member_t **elm_ptr, unsigned *present_out) {
Lev Walkincd2f48e2017-08-10 02:14:59 -07001126 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
Lev Walkin63a35232017-08-10 19:59:47 -07001127 unsigned present;
Lev Walkincd2f48e2017-08-10 02:14:59 -07001128
1129 if(!sptr) {
1130 *elm_ptr = NULL;
1131 *present_out = 0;
1132 return NULL;
1133 }
1134
1135 /*
1136 * Figure out which CHOICE element is encoded.
1137 */
1138 present = _fetch_present_idx(sptr, specs->pres_offset, specs->pres_size);
1139 *present_out = present;
1140
1141 /*
1142 * The presence index is intentionally 1-based to avoid
1143 * treating zeroed structure as a valid one.
1144 */
Lev Walkin63a35232017-08-10 19:59:47 -07001145 if(present > 0 && present <= td->elements_count) {
Lev Walkincd2f48e2017-08-10 02:14:59 -07001146 asn_TYPE_member_t *const elm = &td->elements[present - 1];
1147 const void *memb_ptr;
1148
1149 if(elm->flags & ATF_POINTER) {
1150 memb_ptr =
1151 *(const void *const *)((const char *)sptr + elm->memb_offset);
1152 } else {
1153 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
1154 }
1155 *elm_ptr = elm;
1156 return memb_ptr;
1157 } else {
1158 *elm_ptr = NULL;
1159 return NULL;
1160 }
1161
1162}
1163
1164int
1165CHOICE_compare(const asn_TYPE_descriptor_t *td, const void *aptr, const void *bptr) {
1166 asn_TYPE_member_t *aelm;
1167 asn_TYPE_member_t *belm;
Lev Walkin63a35232017-08-10 19:59:47 -07001168 unsigned apresent = 0;
1169 unsigned bpresent = 0;
Lev Walkincd2f48e2017-08-10 02:14:59 -07001170 const void *amember = _get_member_ptr(td, aptr, &aelm, &apresent);
1171 const void *bmember = _get_member_ptr(td, bptr, &belm, &apresent);
1172
1173 if(amember && bmember) {
1174 if(apresent == bpresent) {
1175 assert(aelm == belm);
1176 return aelm->type->compare_struct(aelm->type, amember, bmember);
1177 } else if(apresent < bpresent) {
1178 return -1;
1179 } else {
1180 return 1;
1181 }
1182 } else if(!amember) {
1183 return -1;
1184 } else {
1185 return 1;
1186 }
1187}
Lev Walkinf6853ce2017-08-11 00:50:27 -07001188
1189/*
1190 * Return the 1-based choice variant presence index.
1191 * Returns 0 in case of error.
1192 */
1193unsigned
1194CHOICE_variant_get_presence(const asn_TYPE_descriptor_t *td, const void *sptr) {
1195 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
1196 return _fetch_present_idx(sptr, specs->pres_offset, specs->pres_size);
1197}
1198
1199/*
1200 * Sets or resets the 1-based choice variant presence index.
1201 * In case a previous index is not zero, the currently selected structure
1202 * member is freed and zeroed-out first.
1203 * Returns 0 on success and -1 on error.
1204 */
1205int
1206CHOICE_variant_set_presence(const asn_TYPE_descriptor_t *td, void *sptr,
1207 unsigned present) {
1208 extern asn_CHOICE_specifics_t asn_SPC_value_specs_3;
1209 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
1210 unsigned old_present;
1211
1212 if(!sptr) {
1213 return -1;
1214 }
1215
1216 if(present > td->elements_count)
1217 return -1;
1218
1219 old_present =
1220 _fetch_present_idx(sptr, specs->pres_offset, specs->pres_size);
1221 if(present == old_present)
1222 return 0;
1223
Lev Walkin91ca2732017-08-11 02:32:42 -07001224 if(old_present != 0) {
Lev Walkinf6853ce2017-08-11 00:50:27 -07001225 assert(old_present <= td->elements_count);
1226 ASN_STRUCT_FREE_CONTENTS_ONLY(*td, sptr);
1227 memset(sptr, 0, specs->struct_size);
1228 }
1229
1230 _set_present_idx(sptr, specs->pres_offset, specs->pres_size, present);
1231
1232 return 0;
1233}
1234