blob: bcf4c1628cf081fa84a17d7f4b2c3ecf21b62fec [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
Lev Walkinc61f3862005-02-14 17:21:22 +00002 * Copyright (c) 2003, 2004, 2005 Lev Walkin <vlm@lionet.info>.
3 * 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 Walkinf15320b2004-06-03 03:38:44 +00008#include <assert.h>
9
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 Walkin8c3b8542005-03-10 18:52:02 +000037 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 Walkin91f5cd02004-08-11 09:10:59 +000066static int _fetch_present_idx(const void *struct_ptr, int off, int size);
67static void _set_present_idx(void *sptr, int offset, int size, int pres);
Lev Walkinf15320b2004-06-03 03:38:44 +000068
69/*
70 * Tags are canonically sorted in the tag to member table.
71 */
72static int
73_search4tag(const void *ap, const void *bp) {
Lev Walkin5e033762004-09-29 13:26:15 +000074 const asn_TYPE_tag2member_t *a = (const asn_TYPE_tag2member_t *)ap;
75 const asn_TYPE_tag2member_t *b = (const asn_TYPE_tag2member_t *)bp;
Lev Walkinc2346572004-08-11 09:07:36 +000076
Lev Walkinf15320b2004-06-03 03:38:44 +000077 int a_class = BER_TAG_CLASS(a->el_tag);
78 int b_class = BER_TAG_CLASS(b->el_tag);
79
80 if(a_class == b_class) {
81 ber_tlv_tag_t a_value = BER_TAG_VALUE(a->el_tag);
82 ber_tlv_tag_t b_value = BER_TAG_VALUE(b->el_tag);
83
84 if(a_value == b_value)
85 return 0;
86 else if(a_value < b_value)
87 return -1;
88 else
89 return 1;
90 } else if(a_class < b_class) {
91 return -1;
92 } else {
93 return 1;
94 }
95}
96
97/*
98 * The decoder of the CHOICE type.
99 */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000100asn_dec_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000101CHOICE_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000102 void **struct_ptr, const void *ptr, size_t size, int tag_mode) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000103 /*
104 * Bring closer parts of structure description.
105 */
Lev Walkin5e033762004-09-29 13:26:15 +0000106 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
107 asn_TYPE_member_t *elements = td->elements;
Lev Walkinf15320b2004-06-03 03:38:44 +0000108
109 /*
110 * Parts of the structure being constructed.
111 */
112 void *st = *struct_ptr; /* Target structure. */
Lev Walkin5e033762004-09-29 13:26:15 +0000113 asn_struct_ctx_t *ctx; /* Decoder context */
Lev Walkinf15320b2004-06-03 03:38:44 +0000114
115 ber_tlv_tag_t tlv_tag; /* T from TLV */
116 ssize_t tag_len; /* Length of TLV's T */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000117 asn_dec_rval_t rval; /* Return code from subparsers */
Lev Walkinf15320b2004-06-03 03:38:44 +0000118
119 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
120
Lev Walkin449f8322004-08-20 13:23:42 +0000121 ASN_DEBUG("Decoding %s as CHOICE", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000122
123 /*
124 * Create the target structure if it is not present already.
125 */
126 if(st == 0) {
127 st = *struct_ptr = CALLOC(1, specs->struct_size);
128 if(st == 0) {
129 RETURN(RC_FAIL);
130 }
131 }
132
133 /*
134 * Restore parsing context.
135 */
Lev Walkin5e033762004-09-29 13:26:15 +0000136 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000137
138 /*
139 * Start to parse where left previously
140 */
141 switch(ctx->phase) {
142 case 0:
143 /*
144 * PHASE 0.
145 * Check that the set of tags associated with given structure
146 * perfectly fits our expectations.
147 */
148
Lev Walkin449f8322004-08-20 13:23:42 +0000149 if(tag_mode || td->tags_count) {
Lev Walkin5e033762004-09-29 13:26:15 +0000150 rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size,
Lev Walkin8e8078a2004-09-26 13:10:40 +0000151 tag_mode, -1, &ctx->left, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000152 if(rval.code != RC_OK) {
153 ASN_DEBUG("%s tagging check failed: %d",
Lev Walkin449f8322004-08-20 13:23:42 +0000154 td->name, rval.code);
Lev Walkinf0f04d12004-10-05 06:36:02 +0000155 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000156 }
157
158 if(ctx->left >= 0) {
159 /* ?Substracted below! */
160 ctx->left += rval.consumed;
161 }
162 ADVANCE(rval.consumed);
163 } else {
164 ctx->left = -1;
165 }
166
167 NEXT_PHASE(ctx);
168
169 ASN_DEBUG("Structure consumes %ld bytes, buffer %ld",
170 (long)ctx->left, (long)size);
171
172 /* Fall through */
173 case 1:
174 /*
175 * Fetch the T from TLV.
176 */
177 tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
Lev Walkin449f8322004-08-20 13:23:42 +0000178 ASN_DEBUG("In %s CHOICE tag length %d", td->name, (int)tag_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000179 switch(tag_len) {
180 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
181 /* Fall through */
182 case -1: RETURN(RC_FAIL);
183 }
184
185 do {
Lev Walkin5e033762004-09-29 13:26:15 +0000186 asn_TYPE_tag2member_t *t2m;
187 asn_TYPE_tag2member_t key;
Lev Walkinf15320b2004-06-03 03:38:44 +0000188
189 key.el_tag = tlv_tag;
Lev Walkinc17d90f2005-01-17 14:32:45 +0000190 t2m = (asn_TYPE_tag2member_t *)bsearch(&key,
Lev Walkinc2346572004-08-11 09:07:36 +0000191 specs->tag2el, specs->tag2el_count,
Lev Walkinf15320b2004-06-03 03:38:44 +0000192 sizeof(specs->tag2el[0]), _search4tag);
193 if(t2m) {
194 /*
195 * Found the element corresponding to the tag.
196 */
197 NEXT_PHASE(ctx);
198 ctx->step = t2m->el_no;
199 break;
200 } else if(specs->extensible == 0) {
201 ASN_DEBUG("Unexpected tag %s "
202 "in non-extensible CHOICE %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000203 ber_tlv_tag_string(tlv_tag), td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000204 RETURN(RC_FAIL);
205 } else {
206 /* Skip this tag */
207 ssize_t skip;
208
209 ASN_DEBUG("Skipping unknown tag %s",
210 ber_tlv_tag_string(tlv_tag));
211
Lev Walkinbaaa24f2004-09-29 14:19:14 +0000212 skip = ber_skip_length(opt_codec_ctx,
Lev Walkinf15320b2004-06-03 03:38:44 +0000213 BER_TLV_CONSTRUCTED(ptr),
Lev Walkin8c3b8542005-03-10 18:52:02 +0000214 (const char *)ptr + tag_len,
215 LEFT - tag_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000216
217 switch(skip) {
218 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
219 /* Fall through */
220 case -1: RETURN(RC_FAIL);
221 }
222
223 ADVANCE(skip + tag_len);
224 RETURN(RC_OK);
225 }
226 } while(0);
227
228 case 2:
229 /*
230 * PHASE 2.
231 * Read in the element.
232 */
233 do {
Lev Walkin5e033762004-09-29 13:26:15 +0000234 asn_TYPE_member_t *elm;/* CHOICE's element */
Lev Walkinf15320b2004-06-03 03:38:44 +0000235 void *memb_ptr; /* Pointer to the member */
Lev Walkinc2346572004-08-11 09:07:36 +0000236 void **memb_ptr2; /* Pointer to that pointer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000237
238 elm = &elements[ctx->step];
239
240 /*
241 * Compute the position of the member inside a structure,
242 * and also a type of containment (it may be contained
243 * as pointer or using inline inclusion).
244 */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000245 if(elm->flags & ATF_POINTER) {
246 /* Member is a pointer to another structure */
Lev Walkinc2346572004-08-11 09:07:36 +0000247 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000248 } else {
249 /*
250 * A pointer to a pointer
251 * holding the start of the structure
252 */
253 memb_ptr = (char *)st + elm->memb_offset;
254 memb_ptr2 = &memb_ptr;
255 }
256 /*
257 * Invoke the member fetch routine according to member's type
258 */
Lev Walkin5e033762004-09-29 13:26:15 +0000259 rval = elm->type->ber_decoder(opt_codec_ctx, elm->type,
Lev Walkinf15320b2004-06-03 03:38:44 +0000260 memb_ptr2, ptr, LEFT,
261 elm->tag_mode);
262 switch(rval.code) {
263 case RC_OK:
264 _set_present_idx(st, specs->pres_offset,
265 specs->pres_size, ctx->step + 1);
266 break;
267 case RC_WMORE: /* More data expected */
268 if(!SIZE_VIOLATION) {
269 ADVANCE(rval.consumed);
270 RETURN(RC_WMORE);
271 }
272 RETURN(RC_FAIL);
273 case RC_FAIL: /* Fatal error */
274 RETURN(rval.code);
275 } /* switch(rval) */
276
277 ADVANCE(rval.consumed);
278 } while(0);
279
280 NEXT_PHASE(ctx);
281
282 /* Fall through */
283 case 3:
284 ASN_DEBUG("CHOICE %s Leftover: %ld, size = %ld, tm=%d, tc=%d",
Lev Walkin449f8322004-08-20 13:23:42 +0000285 td->name, (long)ctx->left, (long)size,
286 tag_mode, td->tags_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000287
288 if(ctx->left > 0) {
289 /*
290 * The type must be fully decoded
291 * by the CHOICE member-specific decoder.
292 */
293 RETURN(RC_FAIL);
294 }
295
296 if(ctx->left == -1
Lev Walkin449f8322004-08-20 13:23:42 +0000297 && !(tag_mode || td->tags_count)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000298 /*
299 * This is an untagged CHOICE.
300 * It doesn't contain nothing
301 * except for the member itself, including all its tags.
302 * The decoding is completed.
303 */
304 NEXT_PHASE(ctx);
305 break;
306 }
307
308 /*
309 * Read in the "end of data chunks"'s.
310 */
311 while(ctx->left < 0) {
312 ssize_t tl;
313
314 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
315 switch(tl) {
316 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
317 /* Fall through */
318 case -1: RETURN(RC_FAIL);
319 }
320
321 /*
322 * Expected <0><0>...
323 */
Lev Walkin8c3b8542005-03-10 18:52:02 +0000324 if(((const uint8_t *)ptr)[0] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000325 if(LEFT < 2) {
326 if(SIZE_VIOLATION)
327 RETURN(RC_FAIL);
328 else
329 RETURN(RC_WMORE);
Lev Walkin8c3b8542005-03-10 18:52:02 +0000330 } else if(((const uint8_t *)ptr)[1] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000331 /*
332 * Correctly finished with <0><0>.
333 */
Lev Walkinf0f04d12004-10-05 06:36:02 +0000334 ADVANCE(2);
335 ctx->left++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000336 continue;
337 }
338 } else {
339 ASN_DEBUG("Unexpected continuation in %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000340 td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000341 RETURN(RC_FAIL);
342 }
343
Lev Walkinf0f04d12004-10-05 06:36:02 +0000344 /* UNREACHABLE */
Lev Walkinf15320b2004-06-03 03:38:44 +0000345 }
346
347 NEXT_PHASE(ctx);
348 case 4:
349 /* No meaningful work here */
350 break;
351 }
352
353 RETURN(RC_OK);
354}
355
Lev Walkina9cc46e2004-09-22 16:06:28 +0000356asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000357CHOICE_encode_der(asn_TYPE_descriptor_t *td,
Lev Walkinf15320b2004-06-03 03:38:44 +0000358 void *struct_ptr,
359 int tag_mode, ber_tlv_tag_t tag,
360 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin5e033762004-09-29 13:26:15 +0000361 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
362 asn_TYPE_member_t *elm; /* CHOICE element */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000363 asn_enc_rval_t erval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000364 void *memb_ptr;
365 size_t computed_size = 0;
366 int present;
367
368 ASN_DEBUG("%s %s as CHOICE",
Lev Walkin449f8322004-08-20 13:23:42 +0000369 cb?"Encoding":"Estimating", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000370
371 present = _fetch_present_idx(struct_ptr,
372 specs->pres_offset, specs->pres_size);
373
374 /*
375 * If the structure was not initialized, it cannot be encoded:
376 * can't deduce what to encode in the choice type.
377 */
Lev Walkin449f8322004-08-20 13:23:42 +0000378 if(present <= 0 || present > td->elements_count) {
379 if(present == 0 && td->elements_count == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000380 /* The CHOICE is empty?! */
381 erval.encoded = 0;
382 return erval;
383 }
384 erval.encoded = -1;
Lev Walkin449f8322004-08-20 13:23:42 +0000385 erval.failed_type = td;
Lev Walkinf15320b2004-06-03 03:38:44 +0000386 erval.structure_ptr = struct_ptr;
387 return erval;
388 }
389
390 /*
391 * Seek over the present member of the structure.
392 */
Lev Walkin449f8322004-08-20 13:23:42 +0000393 elm = &td->elements[present-1];
Lev Walkincc93b0f2004-09-10 09:18:20 +0000394 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000395 memb_ptr = *(void **)((char *)struct_ptr + elm->memb_offset);
396 if(memb_ptr == 0) {
Lev Walkincc93b0f2004-09-10 09:18:20 +0000397 if(elm->optional) {
398 erval.encoded = 0;
399 } else {
400 /* Mandatory element absent */
401 erval.encoded = -1;
402 erval.failed_type = td;
403 erval.structure_ptr = struct_ptr;
404 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000405 return erval;
406 }
407 } else {
408 memb_ptr = (void *)((char *)struct_ptr + elm->memb_offset);
409 }
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);
431 if(ret == -1) {
432 erval.encoded = -1;
Lev Walkin449f8322004-08-20 13:23:42 +0000433 erval.failed_type = td;
Lev Walkinf15320b2004-06-03 03:38:44 +0000434 erval.structure_ptr = struct_ptr;
435 return erval;
436 }
437 computed_size += ret;
438 }
439
440 /*
441 * Encode the single underlying member.
442 */
443 erval = elm->type->der_encoder(elm->type, memb_ptr,
444 elm->tag_mode, elm->tag, cb, app_key);
445 if(erval.encoded == -1)
446 return erval;
447
448 ASN_DEBUG("Encoded CHOICE member in %ld bytes (+%ld)",
449 (long)erval.encoded, (long)computed_size);
450
451 erval.encoded += computed_size;
452
453 return erval;
454}
455
456ber_tlv_tag_t
Lev Walkin5e033762004-09-29 13:26:15 +0000457CHOICE_outmost_tag(asn_TYPE_descriptor_t *td, const void *ptr, int tag_mode, ber_tlv_tag_t tag) {
458 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
Lev Walkinf15320b2004-06-03 03:38:44 +0000459 int present;
460
461 assert(tag_mode == 0);
462 assert(tag == 0);
463
464 /*
465 * Figure out which CHOICE element is encoded.
466 */
467 present = _fetch_present_idx(ptr, specs->pres_offset, specs->pres_size);
468
Lev Walkin449f8322004-08-20 13:23:42 +0000469 if(present > 0 || present <= td->elements_count) {
Lev Walkin5e033762004-09-29 13:26:15 +0000470 asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkin70e70282004-06-05 01:45:48 +0000471 const void *memb_ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000472
Lev Walkincc93b0f2004-09-10 09:18:20 +0000473 if(elm->flags & ATF_POINTER) {
Lev Walkin70e70282004-06-05 01:45:48 +0000474 memb_ptr = *(const void * const *)
475 ((const char *)ptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000476 } else {
Lev Walkin70e70282004-06-05 01:45:48 +0000477 memb_ptr = (const void *)
478 ((const char *)ptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000479 }
480
Lev Walkin5e033762004-09-29 13:26:15 +0000481 return asn_TYPE_outmost_tag(elm->type, memb_ptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000482 elm->tag_mode, elm->tag);
483 } else {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000484 return (ber_tlv_tag_t)-1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000485 }
486}
487
488int
Lev Walkin5e033762004-09-29 13:26:15 +0000489CHOICE_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000490 asn_app_consume_bytes_f *app_errlog, void *app_key) {
Lev Walkin5e033762004-09-29 13:26:15 +0000491 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
Lev Walkinf15320b2004-06-03 03:38:44 +0000492 int present;
493
494 if(!sptr) {
Lev Walkinba4e5182004-08-11 09:44:13 +0000495 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkin16835b62004-08-22 13:47:59 +0000496 "%s: value not given (%s:%d)",
497 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000498 return -1;
499 }
500
501 /*
502 * Figure out which CHOICE element is encoded.
503 */
504 present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
Lev Walkin449f8322004-08-20 13:23:42 +0000505 if(present > 0 && present <= td->elements_count) {
Lev Walkin5e033762004-09-29 13:26:15 +0000506 asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkinf15320b2004-06-03 03:38:44 +0000507 const void *memb_ptr;
508
Lev Walkincc93b0f2004-09-10 09:18:20 +0000509 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000510 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkincc93b0f2004-09-10 09:18:20 +0000511 if(!memb_ptr) {
512 if(elm->optional)
513 return 0;
514 _ASN_ERRLOG(app_errlog, app_key,
515 "%s: mandatory CHOICE element %s absent (%s:%d)",
516 td->name, elm->name, __FILE__, __LINE__);
517 return -1;
518 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000519 } else {
520 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
521 }
522
Lev Walkin449f8322004-08-20 13:23:42 +0000523 if(elm->memb_constraints) {
524 return elm->memb_constraints(elm->type, memb_ptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000525 app_errlog, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +0000526 } else {
527 int ret = elm->type->check_constraints(elm->type,
528 memb_ptr, app_errlog, app_key);
529 /*
530 * Cannot inherit it eralier:
531 * need to make sure we get the updated version.
532 */
533 elm->memb_constraints = elm->type->check_constraints;
534 return ret;
535 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000536 } else {
Lev Walkinba4e5182004-08-11 09:44:13 +0000537 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkind34a8512004-08-22 13:55:49 +0000538 "%s: no CHOICE element given (%s:%d)",
Lev Walkin16835b62004-08-22 13:47:59 +0000539 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000540 return -1;
541 }
542}
543
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000544#undef XER_ADVANCE
545#define XER_ADVANCE(num_bytes) do { \
546 size_t num = num_bytes; \
Lev Walkin8c3b8542005-03-10 18:52:02 +0000547 buf_ptr = ((const char *)buf_ptr) + num;\
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000548 size -= num; \
549 consumed_myself += num; \
550 } while(0)
551
Lev Walkinc61f3862005-02-14 17:21:22 +0000552/*
553 * Decode the XER (XML) data.
554 */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000555asn_dec_rval_t
556CHOICE_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
557 void **struct_ptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000558 const void *buf_ptr, size_t size) {
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000559 /*
560 * Bring closer parts of structure description.
561 */
562 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
563 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
564
565 /*
566 * Parts of the structure being constructed.
567 */
568 void *st = *struct_ptr; /* Target structure. */
569 asn_struct_ctx_t *ctx; /* Decoder context */
570
Lev Walkinc61f3862005-02-14 17:21:22 +0000571 asn_dec_rval_t rval; /* Return value of a decoder */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000572 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000573 int edx; /* Element index */
574
575 /*
576 * Create the target structure if it is not present already.
577 */
578 if(st == 0) {
579 st = *struct_ptr = CALLOC(1, specs->struct_size);
580 if(st == 0) RETURN(RC_FAIL);
581 }
582
583 /*
584 * Restore parsing context.
585 */
586 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
587
588
589 /*
590 * Phases of XER/XML processing:
591 * Phase 0: Check that the opening tag matches our expectations.
592 * Phase 1: Processing body and reacting on closing tag.
593 * Phase 2: Processing inner type.
Lev Walkinc34dc462005-02-18 16:23:48 +0000594 * Phase 3: Only waiting for closing tag.
595 * Phase 4: Skipping unknown extensions.
596 * Phase 5: PHASED OUT
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000597 */
Lev Walkinc34dc462005-02-18 16:23:48 +0000598 for(edx = ctx->step; ctx->phase <= 4;) {
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000599 pxer_chunk_type_e ch_type; /* XER chunk type */
600 ssize_t ch_size; /* Chunk size */
601 xer_check_tag_e tcv; /* Tag check value */
602 asn_TYPE_member_t *elm;
603
604 /*
605 * Go inside the member.
606 */
607 if(ctx->phase == 2) {
608 asn_dec_rval_t tmprval;
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000609 void *memb_ptr; /* Pointer to the member */
610 void **memb_ptr2; /* Pointer to that pointer */
611
Lev Walkinabf68892004-10-26 10:12:14 +0000612 elm = &td->elements[edx];
613
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000614 if(elm->flags & ATF_POINTER) {
615 /* Member is a pointer to another structure */
616 memb_ptr2 = (void **)((char *)st
617 + elm->memb_offset);
618 } else {
619 memb_ptr = (char *)st + elm->memb_offset;
620 memb_ptr2 = &memb_ptr;
621 }
622
Lev Walkinc61f3862005-02-14 17:21:22 +0000623 /* Start/Continue decoding the inner member */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000624 tmprval = elm->type->xer_decoder(opt_codec_ctx,
625 elm->type, memb_ptr2, elm->name,
626 buf_ptr, size);
627 XER_ADVANCE(tmprval.consumed);
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000628 ASN_DEBUG("XER/CHOICE: itdf: code=%d", tmprval.code);
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000629 if(tmprval.code != RC_OK)
630 RETURN(tmprval.code);
Lev Walkinc61f3862005-02-14 17:21:22 +0000631 assert(_fetch_present_idx(st,
632 specs->pres_offset, specs->pres_size) == 0);
633 /* Record what we've got */
634 _set_present_idx(st,
635 specs->pres_offset, specs->pres_size, edx + 1);
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000636 ctx->phase = 3;
637 /* Fall through */
638 }
639
640 /*
641 * Get the next part of the XML stream.
642 */
Lev Walkin1e443962005-02-18 18:06:36 +0000643 ch_size = xer_next_token(&ctx->context, buf_ptr, size, &ch_type);
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000644 switch(ch_size) {
645 case -1: RETURN(RC_FAIL);
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000646 case 0: RETURN(RC_WMORE);
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000647 default:
648 switch(ch_type) {
649 case PXER_COMMENT: /* Got XML comment */
650 case PXER_TEXT: /* Ignore free-standing text */
651 XER_ADVANCE(ch_size); /* Skip silently */
652 continue;
653 case PXER_TAG:
654 break; /* Check the rest down there */
655 }
656 }
657
Lev Walkinc61f3862005-02-14 17:21:22 +0000658 tcv = xer_check_tag(buf_ptr, ch_size, xml_tag);
Lev Walkinc34dc462005-02-18 16:23:48 +0000659
660 /* Skip the extensions section */
661 if(ctx->phase == 4) {
662 ASN_DEBUG("skip_unknown(%d, %ld)",
663 tcv, (long)ctx->left);
664 switch(xer_skip_unknown(tcv, &ctx->left)) {
665 case -1:
666 ctx->phase = 5;
667 RETURN(RC_FAIL);
668 continue;
669 case 1:
670 ctx->phase = 3;
Lev Walkin1e443962005-02-18 18:06:36 +0000671 /* Fall through */
Lev Walkinc34dc462005-02-18 16:23:48 +0000672 case 0:
673 XER_ADVANCE(ch_size);
674 continue;
675 case 2:
676 ctx->phase = 3;
677 break;
678 }
679 }
680
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000681 switch(tcv) {
682 case XCT_BOTH:
Lev Walkinc61f3862005-02-14 17:21:22 +0000683 break; /* No CHOICE? */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000684 case XCT_CLOSING:
Lev Walkinc61f3862005-02-14 17:21:22 +0000685 if(ctx->phase != 3)
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000686 break;
687 XER_ADVANCE(ch_size);
Lev Walkinc34dc462005-02-18 16:23:48 +0000688 ctx->phase = 5; /* Phase out */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000689 RETURN(RC_OK);
690 case XCT_OPENING:
691 if(ctx->phase == 0) {
692 XER_ADVANCE(ch_size);
693 ctx->phase = 1; /* Processing body phase */
694 continue;
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000695 }
696 /* Fall through */
Lev Walkin904e65b2005-02-18 14:23:48 +0000697 case XCT_UNKNOWN_OP:
698 case XCT_UNKNOWN_BO:
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000699
700 if(ctx->phase != 1)
701 break; /* Really unexpected */
702
703 /*
Lev Walkinc61f3862005-02-14 17:21:22 +0000704 * Search which inner member corresponds to this tag.
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000705 */
706 for(edx = 0; edx < td->elements_count; edx++) {
707 elm = &td->elements[edx];
Lev Walkinc61f3862005-02-14 17:21:22 +0000708 tcv = xer_check_tag(buf_ptr,ch_size,elm->name);
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000709 switch(tcv) {
710 case XCT_BOTH:
711 case XCT_OPENING:
712 /*
713 * Process this member.
714 */
715 ctx->step = edx;
716 ctx->phase = 2;
717 break;
Lev Walkin904e65b2005-02-18 14:23:48 +0000718 case XCT_UNKNOWN_OP:
719 case XCT_UNKNOWN_BO:
Lev Walkin6c0df202005-02-14 19:03:17 +0000720 continue;
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000721 default:
722 edx = td->elements_count;
723 break; /* Phase out */
724 }
725 break;
726 }
Lev Walkin6c0df202005-02-14 19:03:17 +0000727 if(edx != td->elements_count)
728 continue;
Lev Walkin904e65b2005-02-18 14:23:48 +0000729
730 /* It is expected extension */
731 if(specs->extensible) {
Lev Walkinc34dc462005-02-18 16:23:48 +0000732 ASN_DEBUG("Got anticipated extension");
Lev Walkin904e65b2005-02-18 14:23:48 +0000733 /*
Lev Walkinc34dc462005-02-18 16:23:48 +0000734 * Check for (XCT_BOTH or XCT_UNKNOWN_BO)
735 * By using a mask. Only record a pure
736 * <opening> tags.
Lev Walkin904e65b2005-02-18 14:23:48 +0000737 */
Lev Walkinc34dc462005-02-18 16:23:48 +0000738 if(tcv & XCT_CLOSING) {
739 /* Found </extension> without body */
740 ctx->phase = 3; /* Terminating */
741 } else {
742 ctx->left = 1;
743 ctx->phase = 4; /* Skip ...'s */
744 }
745 XER_ADVANCE(ch_size);
746 continue;
Lev Walkin904e65b2005-02-18 14:23:48 +0000747 }
748
Lev Walkin6c0df202005-02-14 19:03:17 +0000749 /* Fall through */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000750 default:
751 break;
752 }
753
Lev Walkinc61f3862005-02-14 17:21:22 +0000754 ASN_DEBUG("Unexpected XML tag in CHOICE");
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000755 break;
756 }
757
Lev Walkinc34dc462005-02-18 16:23:48 +0000758 ctx->phase = 5; /* Phase out, just in case */
Lev Walkin8d01b4c2004-10-23 14:57:50 +0000759 RETURN(RC_FAIL);
760}
761
762
Lev Walkina9cc46e2004-09-22 16:06:28 +0000763asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000764CHOICE_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000765 int ilevel, enum xer_encoder_flags_e flags,
766 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin5e033762004-09-29 13:26:15 +0000767 asn_CHOICE_specifics_t *specs=(asn_CHOICE_specifics_t *)td->specifics;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000768 asn_enc_rval_t er;
769 int present;
770
771 if(!sptr)
772 _ASN_ENCODE_FAILED;
773
774 /*
775 * Figure out which CHOICE element is encoded.
776 */
777 present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
778
779 if(present <= 0 || present > td->elements_count) {
780 _ASN_ENCODE_FAILED;
781 } else {
782 asn_enc_rval_t tmper;
Lev Walkin5e033762004-09-29 13:26:15 +0000783 asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkina9cc46e2004-09-22 16:06:28 +0000784 void *memb_ptr;
785 const char *mname = elm->name;
786 unsigned int mlen = strlen(mname);
787
788 if(elm->flags & ATF_POINTER) {
789 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
790 if(!memb_ptr) _ASN_ENCODE_FAILED;
791 } else {
792 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
793 }
794
795 er.encoded = 0;
796
797 if(!(flags & XER_F_CANONICAL)) _i_ASN_TEXT_INDENT(1, ilevel);
798 _ASN_CALLBACK3("<", 1, mname, mlen, ">", 1);
799
800 tmper = elm->type->xer_encoder(elm->type, memb_ptr,
801 ilevel + 1, flags, cb, app_key);
802 if(tmper.encoded == -1) return tmper;
803
804 _ASN_CALLBACK3("</", 2, mname, mlen, ">", 1);
805
806 er.encoded += 5 + (2 * mlen) + tmper.encoded;
807 }
808
809 if(!(flags & XER_F_CANONICAL)) _i_ASN_TEXT_INDENT(1, ilevel - 1);
810
811 return er;
Lev Walkin942fd082004-10-03 09:13:02 +0000812cb_failed:
813 _ASN_ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000814}
815
Lev Walkinf15320b2004-06-03 03:38:44 +0000816int
Lev Walkin5e033762004-09-29 13:26:15 +0000817CHOICE_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000818 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin5e033762004-09-29 13:26:15 +0000819 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
Lev Walkinf15320b2004-06-03 03:38:44 +0000820 int present;
821
Lev Walkin8e8078a2004-09-26 13:10:40 +0000822 if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000823
824 /*
825 * Figure out which CHOICE element is encoded.
826 */
827 present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
828
829 /*
Lev Walkina9cc46e2004-09-22 16:06:28 +0000830 * Print that element.
Lev Walkinf15320b2004-06-03 03:38:44 +0000831 */
Lev Walkin449f8322004-08-20 13:23:42 +0000832 if(present > 0 && present <= td->elements_count) {
Lev Walkin5e033762004-09-29 13:26:15 +0000833 asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkinf15320b2004-06-03 03:38:44 +0000834 const void *memb_ptr;
835
Lev Walkincc93b0f2004-09-10 09:18:20 +0000836 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000837 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkin8e8078a2004-09-26 13:10:40 +0000838 if(!memb_ptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000839 } else {
840 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
841 }
842
843 /* Print member's name and stuff */
Lev Walkincc6a9102004-09-23 22:06:26 +0000844 if(0) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000845 if(cb(elm->name, strlen(elm->name), app_key) < 0
846 || cb(": ", 2, app_key) < 0)
Lev Walkincc6a9102004-09-23 22:06:26 +0000847 return -1;
848 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000849
850 return elm->type->print_struct(elm->type, memb_ptr, ilevel,
851 cb, app_key);
852 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000853 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000854 }
855}
856
857void
Lev Walkin5e033762004-09-29 13:26:15 +0000858CHOICE_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
859 asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
Lev Walkinf15320b2004-06-03 03:38:44 +0000860 int present;
861
862 if(!td || !ptr)
863 return;
864
865 ASN_DEBUG("Freeing %s as CHOICE", td->name);
866
867 /*
868 * Figure out which CHOICE element is encoded.
869 */
870 present = _fetch_present_idx(ptr, specs->pres_offset, specs->pres_size);
871
872 /*
873 * Free that element.
874 */
Lev Walkin449f8322004-08-20 13:23:42 +0000875 if(present > 0 && present <= td->elements_count) {
Lev Walkin5e033762004-09-29 13:26:15 +0000876 asn_TYPE_member_t *elm = &td->elements[present-1];
Lev Walkinf15320b2004-06-03 03:38:44 +0000877 void *memb_ptr;
878
Lev Walkincc93b0f2004-09-10 09:18:20 +0000879 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000880 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
881 if(memb_ptr)
882 elm->type->free_struct(elm->type, memb_ptr, 0);
883 } else {
884 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
885 elm->type->free_struct(elm->type, memb_ptr, 1);
886 }
887 }
888
889 if(!contents_only) {
890 FREEMEM(ptr);
891 }
892}
893
894
895/*
896 * The following functions functions offer protection against -fshort-enums,
897 * compatible with little- and big-endian machines.
898 * If assertion is triggered, either disable -fshort-enums, or add an entry
899 * here with the ->pres_size of your target stracture.
900 * Unless the target structure is packed, the ".present" member
901 * is guaranteed to be aligned properly. ASN.1 compiler itself does not
902 * produce packed code.
903 */
Lev Walkin91f5cd02004-08-11 09:10:59 +0000904static int
Lev Walkinf15320b2004-06-03 03:38:44 +0000905_fetch_present_idx(const void *struct_ptr, int pres_offset, int pres_size) {
906 const void *present_ptr;
907 int present;
908
909 present_ptr = ((const char *)struct_ptr) + pres_offset;
910
911 switch(pres_size) {
912 case sizeof(int): present = *(const int *)present_ptr; break;
913 case sizeof(short): present = *(const short *)present_ptr; break;
914 case sizeof(char): present = *(const char *)present_ptr; break;
915 default:
916 /* ANSI C mandates enum to be equivalent to integer */
917 assert(pres_size != sizeof(int));
918 return 0; /* If not aborted, pass back safe value */
919 }
920
921 return present;
922}
923
Lev Walkin91f5cd02004-08-11 09:10:59 +0000924static void
Lev Walkinf15320b2004-06-03 03:38:44 +0000925_set_present_idx(void *struct_ptr, int pres_offset, int pres_size, int present) {
926 void *present_ptr;
927 present_ptr = ((char *)struct_ptr) + pres_offset;
928
929 switch(pres_size) {
930 case sizeof(int): *(int *)present_ptr = present; break;
931 case sizeof(short): *(short *)present_ptr = present; break;
932 case sizeof(char): *(char *)present_ptr = present; break;
933 default:
934 /* ANSI C mandates enum to be equivalent to integer */
935 assert(pres_size != sizeof(int));
936 }
937}