blob: 53a9e611b810f69d70ea254885d24ada0caf9c18 [file] [log] [blame]
vlmfa67ddc2004-06-03 03:38:44 +00001/*-
2 * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
4 */
vlm39ba4c42004-09-22 16:06:28 +00005#include <asn_internal.h>
vlmfa67ddc2004-06-03 03:38:44 +00006#include <constr_SET.h>
vlmfa67ddc2004-06-03 03:38:44 +00007#include <assert.h> /* for assert() */
8
vlm97ed74f2004-08-11 05:58:52 +00009#ifndef WIN32
10#include <netinet/in.h> /* for ntohl() */
11#else
12#include <winsock2.h> /* for ntohl() */
13#endif
14
vlmc14611c2005-02-14 19:03:17 +000015/* Check that all the mandatory members are present */
16static int _SET_is_populated(asn_TYPE_descriptor_t *td, void *st);
17
vlmfa67ddc2004-06-03 03:38:44 +000018/*
19 * Number of bytes left for this structure.
20 * (ctx->left) indicates the number of bytes _transferred_ for the structure.
21 * (size) contains the number of bytes in the buffer passed.
22 */
vlmc5190612004-08-18 04:53:32 +000023#define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left)
vlmfa67ddc2004-06-03 03:38:44 +000024
25/*
26 * If the subprocessor function returns with an indication that it wants
27 * more data, it may well be a fatal decoding problem, because the
28 * size is constrained by the <TLV>'s L, even if the buffer size allows
29 * reading more data.
30 * For example, consider the buffer containing the following TLVs:
31 * <T:5><L:1><V> <T:6>...
32 * The TLV length clearly indicates that one byte is expected in V, but
33 * if the V processor returns with "want more data" even if the buffer
34 * contains way more data than the V processor have seen.
35 */
vlmb42843a2004-06-05 08:17:50 +000036#define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size)
vlmfa67ddc2004-06-03 03:38:44 +000037
38/*
39 * This macro "eats" the part of the buffer which is definitely "consumed",
40 * i.e. was correctly converted into local representation or rightfully skipped.
41 */
vlmef1b4c02004-09-23 22:06:26 +000042#undef ADVANCE
vlmfa67ddc2004-06-03 03:38:44 +000043#define ADVANCE(num_bytes) do { \
44 size_t num = num_bytes; \
vlmd86c9252004-08-25 01:34:11 +000045 ptr = ((char *)ptr) + num; \
vlmfa67ddc2004-06-03 03:38:44 +000046 size -= num; \
47 if(ctx->left >= 0) \
48 ctx->left -= num; \
49 consumed_myself += num; \
50 } while(0)
51
52/*
53 * Switch to the next phase of parsing.
54 */
vlmef1b4c02004-09-23 22:06:26 +000055#undef NEXT_PHASE
vlmfa67ddc2004-06-03 03:38:44 +000056#define NEXT_PHASE(ctx) do { \
57 ctx->phase++; \
58 ctx->step = 0; \
59 } while(0)
60
61/*
62 * Return a standardized complex structure.
63 */
vlmef1b4c02004-09-23 22:06:26 +000064#undef RETURN
vlmfa67ddc2004-06-03 03:38:44 +000065#define RETURN(_code) do { \
66 rval.code = _code; \
67 rval.consumed = consumed_myself;\
68 return rval; \
69 } while(0)
70
71/*
72 * Tags are canonically sorted in the tag2element map.
73 */
74static int
75_t2e_cmp(const void *ap, const void *bp) {
vlmef6355b2004-09-29 13:26:15 +000076 const asn_TYPE_tag2member_t *a = (const asn_TYPE_tag2member_t *)ap;
77 const asn_TYPE_tag2member_t *b = (const asn_TYPE_tag2member_t *)bp;
vlmda674682004-08-11 09:07:36 +000078
vlmfa67ddc2004-06-03 03:38:44 +000079 int a_class = BER_TAG_CLASS(a->el_tag);
80 int b_class = BER_TAG_CLASS(b->el_tag);
81
82 if(a_class == b_class) {
83 ber_tlv_tag_t a_value = BER_TAG_VALUE(a->el_tag);
84 ber_tlv_tag_t b_value = BER_TAG_VALUE(b->el_tag);
85
86 if(a_value == b_value)
87 return 0;
88 else if(a_value < b_value)
89 return -1;
90 else
91 return 1;
92 } else if(a_class < b_class) {
93 return -1;
94 } else {
95 return 1;
96 }
97}
98
99/*
100 * The decoder of the SET type.
101 */
vlm9de248e2004-10-20 15:50:55 +0000102asn_dec_rval_t
vlmef6355b2004-09-29 13:26:15 +0000103SET_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
vlmfa67ddc2004-06-03 03:38:44 +0000104 void **struct_ptr, void *ptr, size_t size, int tag_mode) {
105 /*
106 * Bring closer parts of structure description.
107 */
vlmef6355b2004-09-29 13:26:15 +0000108 asn_SET_specifics_t *specs = (asn_SET_specifics_t *)td->specifics;
109 asn_TYPE_member_t *elements = td->elements;
vlmfa67ddc2004-06-03 03:38:44 +0000110
111 /*
112 * Parts of the structure being constructed.
113 */
114 void *st = *struct_ptr; /* Target structure. */
vlmef6355b2004-09-29 13:26:15 +0000115 asn_struct_ctx_t *ctx; /* Decoder context */
vlmfa67ddc2004-06-03 03:38:44 +0000116
117 ber_tlv_tag_t tlv_tag; /* T from TLV */
vlm9de248e2004-10-20 15:50:55 +0000118 asn_dec_rval_t rval; /* Return code from subparsers */
vlmfa67ddc2004-06-03 03:38:44 +0000119
120 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
121 int edx; /* SET element's index */
122
vlme413c122004-08-20 13:23:42 +0000123 ASN_DEBUG("Decoding %s as SET", td->name);
vlmfa67ddc2004-06-03 03:38:44 +0000124
125 /*
126 * Create the target structure if it is not present already.
127 */
128 if(st == 0) {
129 st = *struct_ptr = CALLOC(1, specs->struct_size);
130 if(st == 0) {
131 RETURN(RC_FAIL);
132 }
133 }
134
135 /*
136 * Restore parsing context.
137 */
vlmef6355b2004-09-29 13:26:15 +0000138 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
vlmfa67ddc2004-06-03 03:38:44 +0000139
140 /*
141 * Start to parse where left previously
142 */
143 switch(ctx->phase) {
144 case 0:
145 /*
146 * PHASE 0.
147 * Check that the set of tags associated with given structure
148 * perfectly fits our expectations.
149 */
150
vlmef6355b2004-09-29 13:26:15 +0000151 rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size,
vlm6678cb12004-09-26 13:10:40 +0000152 tag_mode, 1, &ctx->left, 0);
vlmfa67ddc2004-06-03 03:38:44 +0000153 if(rval.code != RC_OK) {
154 ASN_DEBUG("%s tagging check failed: %d",
vlme413c122004-08-20 13:23:42 +0000155 td->name, rval.code);
vlm2ebb08c2004-10-05 06:36:44 +0000156 return rval;
vlmfa67ddc2004-06-03 03:38:44 +0000157 }
158
159 if(ctx->left >= 0)
160 ctx->left += rval.consumed; /* ?Substracted below! */
161 ADVANCE(rval.consumed);
162
163 NEXT_PHASE(ctx);
164
165 ASN_DEBUG("Structure advertised %ld bytes, "
166 "buffer contains %ld", (long)ctx->left, (long)size);
167
168 /* Fall through */
169 case 1:
170 /*
171 * PHASE 1.
172 * From the place where we've left it previously,
173 * try to decode the next member from the list of
174 * this structure's elements.
vlm7e79b8c2004-10-28 13:04:02 +0000175 * Note that elements in BER may arrive out of
vlmfa67ddc2004-06-03 03:38:44 +0000176 * order, yet DER mandates that they shall arive in the
177 * canonical order of their tags. So, there is a room
178 * for optimization.
179 */
vlm7e79b8c2004-10-28 13:04:02 +0000180 for(;; ctx->step = 0) {
181 asn_TYPE_tag2member_t *t2m;
182 asn_TYPE_tag2member_t key;
vlmfa67ddc2004-06-03 03:38:44 +0000183 void *memb_ptr; /* Pointer to the member */
vlmda674682004-08-11 09:07:36 +0000184 void **memb_ptr2; /* Pointer to that pointer */
vlmfa67ddc2004-06-03 03:38:44 +0000185 ssize_t tag_len; /* Length of TLV's T */
186
vlm7e79b8c2004-10-28 13:04:02 +0000187 if(ctx->step & 1) {
188 edx = ctx->step >> 1;
vlmfa67ddc2004-06-03 03:38:44 +0000189 goto microphase2;
vlm7e79b8c2004-10-28 13:04:02 +0000190 }
vlmfa67ddc2004-06-03 03:38:44 +0000191
192 /*
193 * MICROPHASE 1: Synchronize decoding.
194 */
195
196 if(ctx->left == 0)
197 /*
198 * No more things to decode.
199 * Exit out of here and check whether all mandatory
200 * elements have been received (in the next phase).
201 */
202 break;
203
204 /*
205 * Fetch the T from TLV.
206 */
207 tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
208 switch(tag_len) {
209 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
210 /* Fall through */
211 case -1: RETURN(RC_FAIL);
212 }
213
214 if(ctx->left < 0 && ((uint8_t *)ptr)[0] == 0) {
215 if(LEFT < 2) {
216 if(SIZE_VIOLATION)
217 RETURN(RC_FAIL);
218 else
219 RETURN(RC_WMORE);
220 } else if(((uint8_t *)ptr)[1] == 0) {
221 /*
222 * Found the terminator of the
223 * indefinite length structure.
224 * Invoke the generic finalization function.
225 */
226 goto phase3;
227 }
228 }
229
vlm7e79b8c2004-10-28 13:04:02 +0000230 key.el_tag = tlv_tag;
vlma8f4a4c2005-01-17 14:32:45 +0000231 t2m = (asn_TYPE_tag2member_t *)bsearch(&key,
vlm7e79b8c2004-10-28 13:04:02 +0000232 specs->tag2el, specs->tag2el_count,
233 sizeof(specs->tag2el[0]), _t2e_cmp);
234 if(t2m) {
vlmfa67ddc2004-06-03 03:38:44 +0000235 /*
vlm7e79b8c2004-10-28 13:04:02 +0000236 * Found the element corresponding to the tag.
vlmfa67ddc2004-06-03 03:38:44 +0000237 */
vlm7e79b8c2004-10-28 13:04:02 +0000238 edx = t2m->el_no;
239 ctx->step = (edx << 1) + 1;
240 ASN_DEBUG("Got tag %s (%s), edx %d",
241 ber_tlv_tag_string(tlv_tag), td->name, edx);
242 } else if(specs->extensible == 0) {
243 ASN_DEBUG("Unexpected tag %s "
244 "in non-extensible SET %s",
245 ber_tlv_tag_string(tlv_tag), td->name);
246 RETURN(RC_FAIL);
vlmfa67ddc2004-06-03 03:38:44 +0000247 } else {
vlm7e79b8c2004-10-28 13:04:02 +0000248 /* Skip this tag */
249 ssize_t skip;
vlmfa67ddc2004-06-03 03:38:44 +0000250
vlm7e79b8c2004-10-28 13:04:02 +0000251 ASN_DEBUG("Skipping unknown tag %s",
252 ber_tlv_tag_string(tlv_tag));
vlmfa67ddc2004-06-03 03:38:44 +0000253
vlm7e79b8c2004-10-28 13:04:02 +0000254 skip = ber_skip_length(opt_codec_ctx,
255 BER_TLV_CONSTRUCTED(ptr),
256 (char *)ptr + tag_len, LEFT - tag_len);
vlmfa67ddc2004-06-03 03:38:44 +0000257
vlm7e79b8c2004-10-28 13:04:02 +0000258 switch(skip) {
259 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
260 /* Fall through */
261 case -1: RETURN(RC_FAIL);
vlmfa67ddc2004-06-03 03:38:44 +0000262 }
vlm7e79b8c2004-10-28 13:04:02 +0000263
264 ADVANCE(skip + tag_len);
265 continue; /* Try again with the next tag */
vlmfa67ddc2004-06-03 03:38:44 +0000266 }
267
268 /*
269 * MICROPHASE 2: Invoke the member-specific decoder.
270 */
vlmfa67ddc2004-06-03 03:38:44 +0000271 microphase2:
272
273 /*
274 * Check for duplications: must not overwrite
275 * already decoded elements.
276 */
vlm1ff928d2004-08-11 08:10:13 +0000277 if(ASN_SET_ISPRESENT2((char *)st + specs->pres_offset, edx)) {
vlme26690f2004-07-01 00:47:16 +0000278 ASN_DEBUG("SET %s: Duplicate element %s (%d)",
vlme413c122004-08-20 13:23:42 +0000279 td->name, elements[edx].name, edx);
vlmfa67ddc2004-06-03 03:38:44 +0000280 RETURN(RC_FAIL);
281 }
282
283 /*
284 * Compute the position of the member inside a structure,
285 * and also a type of containment (it may be contained
286 * as pointer or using inline inclusion).
287 */
vlmddd5a7d2004-09-10 09:18:20 +0000288 if(elements[edx].flags & ATF_POINTER) {
289 /* Member is a pointer to another structure */
vlmda674682004-08-11 09:07:36 +0000290 memb_ptr2 = (void **)((char *)st + elements[edx].memb_offset);
vlmfa67ddc2004-06-03 03:38:44 +0000291 } else {
292 /*
293 * A pointer to a pointer
294 * holding the start of the structure
295 */
296 memb_ptr = (char *)st + elements[edx].memb_offset;
297 memb_ptr2 = &memb_ptr;
298 }
299 /*
300 * Invoke the member fetch routine according to member's type
301 */
vlmef6355b2004-09-29 13:26:15 +0000302 rval = elements[edx].type->ber_decoder(opt_codec_ctx,
vlmda674682004-08-11 09:07:36 +0000303 elements[edx].type,
vlmfa67ddc2004-06-03 03:38:44 +0000304 memb_ptr2, ptr, LEFT,
305 elements[edx].tag_mode);
306 switch(rval.code) {
307 case RC_OK:
vlm1ff928d2004-08-11 08:10:13 +0000308 ASN_SET_MKPRESENT((char *)st + specs->pres_offset, edx);
vlmfa67ddc2004-06-03 03:38:44 +0000309 break;
310 case RC_WMORE: /* More data expected */
311 if(!SIZE_VIOLATION) {
312 ADVANCE(rval.consumed);
313 RETURN(RC_WMORE);
314 }
315 /* Fail through */
316 case RC_FAIL: /* Fatal error */
317 RETURN(RC_FAIL);
318 } /* switch(rval) */
319
320 ADVANCE(rval.consumed);
321 } /* for(all structure members) */
322
323 phase3:
324 ctx->phase = 3;
325 /* Fall through */
326 case 3:
327 case 4: /* Only 00 is expected */
328 ASN_DEBUG("SET %s Leftover: %ld, size = %ld",
vlme413c122004-08-20 13:23:42 +0000329 td->name, (long)ctx->left, (long)size);
vlmfa67ddc2004-06-03 03:38:44 +0000330
331 /*
332 * Skip everything until the end of the SET.
333 */
334 while(ctx->left) {
335 ssize_t tl, ll;
336
337 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
338 switch(tl) {
339 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
340 /* Fall through */
341 case -1: RETURN(RC_FAIL);
342 }
343
344 /*
345 * If expected <0><0>...
346 */
347 if(ctx->left < 0
348 && ((uint8_t *)ptr)[0] == 0) {
349 if(LEFT < 2) {
350 if(SIZE_VIOLATION)
351 RETURN(RC_FAIL);
352 else
353 RETURN(RC_WMORE);
354 } else if(((uint8_t *)ptr)[1] == 0) {
355 /*
356 * Correctly finished with <0><0>.
357 */
358 ADVANCE(2);
359 ctx->left++;
360 ctx->phase = 4;
361 continue;
362 }
363 }
364
365 if(specs->extensible == 0 || ctx->phase == 4) {
366 ASN_DEBUG("Unexpected continuation "
vlm7e79b8c2004-10-28 13:04:02 +0000367 "of a non-extensible type %s "
368 "(ptr=%02x)",
369 td->name, *(uint8_t *)ptr);
vlmfa67ddc2004-06-03 03:38:44 +0000370 RETURN(RC_FAIL);
371 }
372
vlm6130a2f2004-09-29 14:19:14 +0000373 ll = ber_skip_length(opt_codec_ctx,
vlmfa67ddc2004-06-03 03:38:44 +0000374 BER_TLV_CONSTRUCTED(ptr),
vlm1ff928d2004-08-11 08:10:13 +0000375 (char *)ptr + tl, LEFT - tl);
vlmfa67ddc2004-06-03 03:38:44 +0000376 switch(ll) {
377 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
378 /* Fall through */
379 case -1: RETURN(RC_FAIL);
380 }
381
382 ADVANCE(tl + ll);
383 }
384
385 ctx->phase = 5;
386 case 5:
vlmc14611c2005-02-14 19:03:17 +0000387 /* Check that all mandatory elements are present. */
388 if(!_SET_is_populated(td, st))
389 RETURN(RC_FAIL);
vlmfa67ddc2004-06-03 03:38:44 +0000390
391 NEXT_PHASE(ctx);
392 }
393
394 RETURN(RC_OK);
395}
396
vlmc14611c2005-02-14 19:03:17 +0000397static int
398_SET_is_populated(asn_TYPE_descriptor_t *td, void *st) {
399 asn_SET_specifics_t *specs = (asn_SET_specifics_t *)td->specifics;
400 int edx;
401
402 /*
403 * Check that all mandatory elements are present.
404 */
405 for(edx = 0; edx < td->elements_count;
406 edx += (8 * sizeof(specs->_mandatory_elements[0]))) {
407 unsigned int midx, pres, must;
408
409 midx = edx/(8 * sizeof(specs->_mandatory_elements[0]));
410 pres = ((unsigned int *)((char *)st+specs->pres_offset))[midx];
411 must = ntohl(specs->_mandatory_elements[midx]);
412
413 if((pres & must) == must) {
414 /*
415 * Yes, everything seems to be in place.
416 */
417 } else {
418 ASN_DEBUG("One or more mandatory elements "
419 "of a SET %s %d (%08x.%08x)=%08x "
420 "are not present",
421 td->name,
422 midx,
423 pres,
424 must,
425 (~(pres & must) & must)
426 );
427 return 0;
428 }
429 }
430
431 return 1;
432}
433
vlmfa67ddc2004-06-03 03:38:44 +0000434/*
435 * The DER encoder of the SET type.
436 */
vlm39ba4c42004-09-22 16:06:28 +0000437asn_enc_rval_t
vlmef6355b2004-09-29 13:26:15 +0000438SET_encode_der(asn_TYPE_descriptor_t *td,
vlma3296662004-10-03 09:12:07 +0000439 void *sptr, int tag_mode, ber_tlv_tag_t tag,
vlmfa67ddc2004-06-03 03:38:44 +0000440 asn_app_consume_bytes_f *cb, void *app_key) {
vlmef6355b2004-09-29 13:26:15 +0000441 asn_SET_specifics_t *specs = (asn_SET_specifics_t *)td->specifics;
vlmfa67ddc2004-06-03 03:38:44 +0000442 size_t computed_size = 0;
vlma3296662004-10-03 09:12:07 +0000443 asn_enc_rval_t er;
vlme413c122004-08-20 13:23:42 +0000444 int t2m_build_own = (specs->tag2el_count != td->elements_count);
vlmef6355b2004-09-29 13:26:15 +0000445 asn_TYPE_tag2member_t *t2m;
vlmfa67ddc2004-06-03 03:38:44 +0000446 int t2m_count;
447 ssize_t ret;
448 int edx;
449
450 /*
451 * Use existing, or build our own tags map.
452 */
453 if(t2m_build_own) {
vlma8f4a4c2005-01-17 14:32:45 +0000454 t2m = (asn_TYPE_tag2member_t *)alloca(
455 td->elements_count * sizeof(t2m[0]));
vlma3296662004-10-03 09:12:07 +0000456 if(!t2m) _ASN_ENCODE_FAILED; /* There are such platforms */
vlmda674682004-08-11 09:07:36 +0000457 t2m_count = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000458 } else {
459 /*
460 * There is no untagged CHOICE in this SET.
461 * Employ existing table.
462 */
463 t2m = specs->tag2el;
464 t2m_count = specs->tag2el_count;
465 }
466
467 /*
468 * Gather the length of the underlying members sequence.
469 */
vlme413c122004-08-20 13:23:42 +0000470 for(edx = 0; edx < td->elements_count; edx++) {
vlmef6355b2004-09-29 13:26:15 +0000471 asn_TYPE_member_t *elm = &td->elements[edx];
vlma3296662004-10-03 09:12:07 +0000472 asn_enc_rval_t tmper;
vlmfa67ddc2004-06-03 03:38:44 +0000473 void *memb_ptr;
474
475 /*
476 * Compute the length of the encoding of this member.
477 */
vlmddd5a7d2004-09-10 09:18:20 +0000478 if(elm->flags & ATF_POINTER) {
vlma3296662004-10-03 09:12:07 +0000479 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
vlmfa67ddc2004-06-03 03:38:44 +0000480 if(!memb_ptr) {
481 if(t2m_build_own) {
482 t2m[t2m_count].el_no = edx;
483 t2m[t2m_count].el_tag = 0;
484 t2m_count++;
485 }
486 continue;
487 }
488 } else {
vlma3296662004-10-03 09:12:07 +0000489 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
vlmfa67ddc2004-06-03 03:38:44 +0000490 }
vlma3296662004-10-03 09:12:07 +0000491 tmper = elm->type->der_encoder(elm->type, memb_ptr,
vlmfa67ddc2004-06-03 03:38:44 +0000492 elm->tag_mode, elm->tag,
493 0, 0);
vlma3296662004-10-03 09:12:07 +0000494 if(tmper.encoded == -1)
495 return tmper;
496 computed_size += tmper.encoded;
vlmfa67ddc2004-06-03 03:38:44 +0000497
498 /*
499 * Remember the outmost tag of this member.
500 */
501 if(t2m_build_own) {
502 t2m[t2m_count].el_no = edx;
vlmef6355b2004-09-29 13:26:15 +0000503 t2m[t2m_count].el_tag = asn_TYPE_outmost_tag(
vlmfa67ddc2004-06-03 03:38:44 +0000504 elm->type, memb_ptr, elm->tag_mode, elm->tag);
505 t2m_count++;
506 } else {
507 /*
508 * No dynamic sorting is necessary.
509 */
510 }
511 }
512
513 /*
514 * Finalize order of the components.
515 */
vlme413c122004-08-20 13:23:42 +0000516 assert(t2m_count == td->elements_count);
vlmfa67ddc2004-06-03 03:38:44 +0000517 if(t2m_build_own) {
518 /*
519 * Sort the underlying members according to their
520 * canonical tags order. DER encoding mandates it.
521 */
522 qsort(t2m, t2m_count, sizeof(specs->tag2el[0]), _t2e_cmp);
523 } else {
524 /*
525 * Tags are already sorted by the compiler.
526 */
527 }
528
529 /*
530 * Encode the TLV for the sequence itself.
531 */
vlm6678cb12004-09-26 13:10:40 +0000532 ret = der_write_tags(td, computed_size, tag_mode, 1, tag, cb, app_key);
vlma3296662004-10-03 09:12:07 +0000533 if(ret == -1) _ASN_ENCODE_FAILED;
534 er.encoded = computed_size + ret;
vlmfa67ddc2004-06-03 03:38:44 +0000535
vlma3296662004-10-03 09:12:07 +0000536 if(!cb) return er;
vlmfa67ddc2004-06-03 03:38:44 +0000537
538 /*
539 * Encode all members.
540 */
vlme413c122004-08-20 13:23:42 +0000541 for(edx = 0; edx < td->elements_count; edx++) {
vlmef6355b2004-09-29 13:26:15 +0000542 asn_TYPE_member_t *elm;
vlma3296662004-10-03 09:12:07 +0000543 asn_enc_rval_t tmper;
vlmfa67ddc2004-06-03 03:38:44 +0000544 void *memb_ptr;
545
546 /* Encode according to the tag order */
vlme413c122004-08-20 13:23:42 +0000547 elm = &td->elements[t2m[edx].el_no];
vlmfa67ddc2004-06-03 03:38:44 +0000548
vlmddd5a7d2004-09-10 09:18:20 +0000549 if(elm->flags & ATF_POINTER) {
vlma3296662004-10-03 09:12:07 +0000550 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
vlmfa67ddc2004-06-03 03:38:44 +0000551 if(!memb_ptr) continue;
552 } else {
vlma3296662004-10-03 09:12:07 +0000553 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
vlmfa67ddc2004-06-03 03:38:44 +0000554 }
vlma3296662004-10-03 09:12:07 +0000555 tmper = elm->type->der_encoder(elm->type, memb_ptr,
vlmfa67ddc2004-06-03 03:38:44 +0000556 elm->tag_mode, elm->tag,
557 cb, app_key);
vlma3296662004-10-03 09:12:07 +0000558 if(tmper.encoded == -1)
559 return tmper;
560 computed_size -= tmper.encoded;
vlmfa67ddc2004-06-03 03:38:44 +0000561 }
562
563 if(computed_size != 0) {
564 /*
565 * Encoded size is not equal to the computed size.
566 */
vlma3296662004-10-03 09:12:07 +0000567 _ASN_ENCODE_FAILED;
vlmfa67ddc2004-06-03 03:38:44 +0000568 }
569
vlma3296662004-10-03 09:12:07 +0000570 return er;
vlmfa67ddc2004-06-03 03:38:44 +0000571}
572
vlmc14611c2005-02-14 19:03:17 +0000573#undef XER_ADVANCE
574#define XER_ADVANCE(num_bytes) do { \
575 size_t num = num_bytes; \
576 buf_ptr = ((char *)buf_ptr) + num; \
577 size -= num; \
578 consumed_myself += num; \
579 } while(0)
580
581/*
582 * Decode the XER (XML) data.
583 */
584asn_dec_rval_t
585SET_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
586 void **struct_ptr, const char *opt_mname,
587 void *buf_ptr, size_t size) {
588 /*
589 * Bring closer parts of structure description.
590 */
591 asn_SET_specifics_t *specs = (asn_SET_specifics_t *)td->specifics;
592 asn_TYPE_member_t *elements = td->elements;
593 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
594
595 /*
596 * ... and parts of the structure being constructed.
597 */
598 void *st = *struct_ptr; /* Target structure. */
599 asn_struct_ctx_t *ctx; /* Decoder context */
600
601 asn_dec_rval_t rval; /* Return value from a decoder */
602 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
603 int xer_state; /* XER low level parsing context */
604 int edx; /* Element index */
605
606 /*
607 * Create the target structure if it is not present already.
608 */
609 if(st == 0) {
610 st = *struct_ptr = CALLOC(1, specs->struct_size);
611 if(st == 0) RETURN(RC_FAIL);
612 }
613
614 /*
615 * Restore parsing context.
616 */
617 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
618
619 /*
620 * Phases of XER/XML processing:
621 * Phase 0: Check that the opening tag matches our expectations.
622 * Phase 1: Processing body and reacting on closing tag.
623 * Phase 2: Processing inner type.
624 */
625 for(xer_state = ctx->left, edx = ctx->step; ctx->phase <= 2;) {
626 pxer_chunk_type_e ch_type; /* XER chunk type */
627 ssize_t ch_size; /* Chunk size */
628 xer_check_tag_e tcv; /* Tag check value */
629 asn_TYPE_member_t *elm;
630
631 /*
632 * Go inside the inner member of a set.
633 */
634 if(ctx->phase == 2) {
635 asn_dec_rval_t tmprval;
636 void *memb_ptr; /* Pointer to the member */
637 void **memb_ptr2; /* Pointer to that pointer */
638
639 if(ASN_SET_ISPRESENT2((char *)st + specs->pres_offset,
640 edx)) {
641 ASN_DEBUG("SET %s: Duplicate element %s (%d)",
642 td->name, elements[edx].name, edx);
643 RETURN(RC_FAIL);
644 }
645
vlm5f4f4a82005-02-14 20:15:40 +0000646 elm = &elements[edx];
vlmc14611c2005-02-14 19:03:17 +0000647
648 if(elm->flags & ATF_POINTER) {
649 /* Member is a pointer to another structure */
650 memb_ptr2 = (void **)((char *)st
651 + elm->memb_offset);
652 } else {
653 memb_ptr = (char *)st + elm->memb_offset;
654 memb_ptr2 = &memb_ptr;
655 }
656
657 /* Invoke the inner type decoder, m.b. multiple times */
658 tmprval = elm->type->xer_decoder(opt_codec_ctx,
659 elm->type, memb_ptr2, elm->name,
660 buf_ptr, size);
661 XER_ADVANCE(tmprval.consumed);
662 if(tmprval.code != RC_OK)
663 RETURN(tmprval.code);
664 ctx->phase = 1; /* Back to body processing */
665 ctx->left = xer_state = 0; /* New, clean state */
666 ASN_SET_MKPRESENT((char *)st + specs->pres_offset, edx);
667 ASN_DEBUG("XER/SET phase => %d", ctx->phase);
668 /* Fall through */
669 }
670
671 /*
672 * Get the next part of the XML stream.
673 */
674 ch_size = xer_next_token(&xer_state, buf_ptr, size, &ch_type);
675 switch(ch_size) {
676 case -1: RETURN(RC_FAIL);
677 case 0:
678 ctx->left = xer_state;
679 RETURN(RC_WMORE);
680 default:
681 switch(ch_type) {
682 case PXER_COMMENT: /* Got XML comment */
683 case PXER_TEXT: /* Ignore free-standing text */
684 XER_ADVANCE(ch_size); /* Skip silently */
685 continue;
686 case PXER_TAG:
687 break; /* Check the rest down there */
688 }
689 }
690
691 tcv = xer_check_tag(buf_ptr, ch_size, xml_tag);
692 ASN_DEBUG("XER/SET: tcv = %d, ph=%d", tcv, ctx->phase);
693 switch(tcv) {
694 case XCT_CLOSING:
695 if(ctx->phase == 0) break;
696 ctx->phase = 0;
697 /* Fall through */
698 case XCT_BOTH:
699 if(ctx->phase == 0) {
700 if(_SET_is_populated(td, st)) {
701 XER_ADVANCE(ch_size);
702 ctx->phase = 3; /* Phase out */
703 RETURN(RC_OK);
704 } else {
705 ASN_DEBUG("Premature end of XER SET");
706 RETURN(RC_FAIL);
707 }
708 }
709 /* Fall through */
710 case XCT_OPENING:
711 if(ctx->phase == 0) {
712 XER_ADVANCE(ch_size);
713 ctx->phase = 1; /* Processing body phase */
714 continue;
715 }
716 /* Fall through */
717 case XCT_UNEXPECTED:
718
719 ASN_DEBUG("XER/SET: tcv=%d, ph=%d", tcv, ctx->phase);
720 if(ctx->phase != 1)
721 break; /* Really unexpected */
722
723 /*
724 * Search which member corresponds to this tag.
725 */
726 for(edx = 0; edx < td->elements_count; edx++) {
vlm5f4f4a82005-02-14 20:15:40 +0000727 elm = &elements[edx];
vlmc14611c2005-02-14 19:03:17 +0000728 tcv = xer_check_tag(buf_ptr,ch_size,elm->name);
729 switch(tcv) {
730 case XCT_BOTH:
731 case XCT_OPENING:
732 /*
733 * Process this member.
734 */
735 ctx->step = edx;
736 ctx->phase = 2;
737 break;
738 case XCT_UNEXPECTED:
739 continue;
740 case XCT_CLOSING:
741 default:
742 edx = td->elements_count;
743 break; /* Phase out */
744 }
745 break;
746 }
747 if(edx != td->elements_count)
748 continue;
749 /* Fall through */
750 default:
751 break;
752 }
753
754 ASN_DEBUG("Unexpected XML tag in SET");
755 break;
756 }
757
758 ctx->phase = 3; /* "Phase out" on hard failure */
759 RETURN(RC_FAIL);
760}
761
vlm39ba4c42004-09-22 16:06:28 +0000762asn_enc_rval_t
vlmef6355b2004-09-29 13:26:15 +0000763SET_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
vlm39ba4c42004-09-22 16:06:28 +0000764 int ilevel, enum xer_encoder_flags_e flags,
765 asn_app_consume_bytes_f *cb, void *app_key) {
vlma3296662004-10-03 09:12:07 +0000766 asn_SET_specifics_t *specs = (asn_SET_specifics_t *)td->specifics;
vlm39ba4c42004-09-22 16:06:28 +0000767 asn_enc_rval_t er;
768 int xcan = (flags & XER_F_CANONICAL);
vlma3296662004-10-03 09:12:07 +0000769 asn_TYPE_tag2member_t *t2m = specs->tag2el_cxer;
770 int t2m_count = specs->tag2el_cxer_count;
vlm39ba4c42004-09-22 16:06:28 +0000771 int edx;
772
773 if(!sptr)
774 _ASN_ENCODE_FAILED;
775
vlma3296662004-10-03 09:12:07 +0000776 assert(t2m_count == td->elements_count);
777
vlm39ba4c42004-09-22 16:06:28 +0000778 er.encoded = 0;
779
vlma3296662004-10-03 09:12:07 +0000780 for(edx = 0; edx < t2m_count; edx++) {
vlm39ba4c42004-09-22 16:06:28 +0000781 asn_enc_rval_t tmper;
vlma3296662004-10-03 09:12:07 +0000782 asn_TYPE_member_t *elm;
vlm39ba4c42004-09-22 16:06:28 +0000783 void *memb_ptr;
vlma3296662004-10-03 09:12:07 +0000784 const char *mname;
785 unsigned int mlen;
786
787 elm = &td->elements[t2m[edx].el_no];
788 mname = elm->name;
789 mlen = strlen(elm->name);
vlm39ba4c42004-09-22 16:06:28 +0000790
791 if(elm->flags & ATF_POINTER) {
792 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
793 if(!memb_ptr) continue; /* OPTIONAL element? */
794 } else {
795 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
796 }
797
798 if(!xcan)
799 _i_ASN_TEXT_INDENT(1, ilevel);
800 _ASN_CALLBACK3("<", 1, mname, mlen, ">", 1);
801
802 /* Print the member itself */
803 tmper = elm->type->xer_encoder(elm->type, memb_ptr,
804 ilevel + 1, flags, cb, app_key);
805 if(tmper.encoded == -1) return tmper;
806
807 _ASN_CALLBACK3("</", 2, mname, mlen, ">", 1);
808
809 er.encoded += 5 + (2 * mlen) + tmper.encoded;
810 }
811
812 if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel - 1);
813
814 return er;
vlma3296662004-10-03 09:12:07 +0000815cb_failed:
816 _ASN_ENCODE_FAILED;
vlm39ba4c42004-09-22 16:06:28 +0000817}
818
vlmfa67ddc2004-06-03 03:38:44 +0000819int
vlmef6355b2004-09-29 13:26:15 +0000820SET_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
vlmfa67ddc2004-06-03 03:38:44 +0000821 asn_app_consume_bytes_f *cb, void *app_key) {
vlmfa67ddc2004-06-03 03:38:44 +0000822 int edx;
823 int ret;
824
vlm6678cb12004-09-26 13:10:40 +0000825 if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +0000826
827 /* Dump preamble */
vlm6678cb12004-09-26 13:10:40 +0000828 if(cb(td->name, strlen(td->name), app_key) < 0
829 || cb(" ::= {", 6, app_key) < 0)
vlmfa67ddc2004-06-03 03:38:44 +0000830 return -1;
831
vlme413c122004-08-20 13:23:42 +0000832 for(edx = 0; edx < td->elements_count; edx++) {
vlmef6355b2004-09-29 13:26:15 +0000833 asn_TYPE_member_t *elm = &td->elements[edx];
vlmfa67ddc2004-06-03 03:38:44 +0000834 const void *memb_ptr;
835
vlmddd5a7d2004-09-10 09:18:20 +0000836 if(elm->flags & ATF_POINTER) {
vlmfa67ddc2004-06-03 03:38:44 +0000837 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
838 if(!memb_ptr) continue;
839 } else {
840 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
841 }
842
vlm6678cb12004-09-26 13:10:40 +0000843 _i_INDENT(1);
vlmfa67ddc2004-06-03 03:38:44 +0000844
845 /* Print the member's name and stuff */
vlm6678cb12004-09-26 13:10:40 +0000846 if(cb(elm->name, strlen(elm->name), app_key) < 0
847 || cb(": ", 2, app_key) < 0)
vlmfa67ddc2004-06-03 03:38:44 +0000848 return -1;
849
850 /* Print the member itself */
vlm6678cb12004-09-26 13:10:40 +0000851 ret = elm->type->print_struct(elm->type, memb_ptr, ilevel + 1,
vlmfa67ddc2004-06-03 03:38:44 +0000852 cb, app_key);
853 if(ret) return ret;
vlmfa67ddc2004-06-03 03:38:44 +0000854 }
855
vlm6678cb12004-09-26 13:10:40 +0000856 ilevel--;
857 _i_INDENT(1);
vlmfa67ddc2004-06-03 03:38:44 +0000858
vlm6678cb12004-09-26 13:10:40 +0000859 return (cb("}", 1, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +0000860}
861
862void
vlmef6355b2004-09-29 13:26:15 +0000863SET_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
vlmfa67ddc2004-06-03 03:38:44 +0000864 int edx;
865
866 if(!td || !ptr)
867 return;
868
869 ASN_DEBUG("Freeing %s as SET", td->name);
870
vlme413c122004-08-20 13:23:42 +0000871 for(edx = 0; edx < td->elements_count; edx++) {
vlmef6355b2004-09-29 13:26:15 +0000872 asn_TYPE_member_t *elm = &td->elements[edx];
vlmfa67ddc2004-06-03 03:38:44 +0000873 void *memb_ptr;
vlmddd5a7d2004-09-10 09:18:20 +0000874 if(elm->flags & ATF_POINTER) {
vlmfa67ddc2004-06-03 03:38:44 +0000875 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
876 if(memb_ptr)
877 elm->type->free_struct(elm->type, memb_ptr, 0);
878 } else {
879 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
880 elm->type->free_struct(elm->type, memb_ptr, 1);
881 }
882 }
883
884 if(!contents_only) {
885 FREEMEM(ptr);
886 }
887}
888
889int
vlmef6355b2004-09-29 13:26:15 +0000890SET_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
vlmfa67ddc2004-06-03 03:38:44 +0000891 asn_app_consume_bytes_f *app_errlog, void *app_key) {
vlmfa67ddc2004-06-03 03:38:44 +0000892 int edx;
893
894 if(!sptr) {
vlme3f0f282004-08-11 09:44:13 +0000895 _ASN_ERRLOG(app_errlog, app_key,
vlm758530a2004-08-22 13:47:59 +0000896 "%s: value not given (%s:%d)",
897 td->name, __FILE__, __LINE__);
vlmfa67ddc2004-06-03 03:38:44 +0000898 return -1;
899 }
900
901 /*
902 * Iterate over structure members and check their validity.
903 */
vlme413c122004-08-20 13:23:42 +0000904 for(edx = 0; edx < td->elements_count; edx++) {
vlmef6355b2004-09-29 13:26:15 +0000905 asn_TYPE_member_t *elm = &td->elements[edx];
vlmfa67ddc2004-06-03 03:38:44 +0000906 const void *memb_ptr;
907
vlmddd5a7d2004-09-10 09:18:20 +0000908 if(elm->flags & ATF_POINTER) {
vlmfa67ddc2004-06-03 03:38:44 +0000909 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
910 if(!memb_ptr) {
vlmddd5a7d2004-09-10 09:18:20 +0000911 if(elm->optional)
912 continue;
913 _ASN_ERRLOG(app_errlog, app_key,
914 "%s: mandatory element %s absent (%s:%d)",
915 td->name, elm->name, __FILE__, __LINE__);
916 return -1;
vlmfa67ddc2004-06-03 03:38:44 +0000917 }
918 } else {
919 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
920 }
921
vlme413c122004-08-20 13:23:42 +0000922 if(elm->memb_constraints) {
923 int ret = elm->memb_constraints(elm->type, memb_ptr,
924 app_errlog, app_key);
925 if(ret) return ret;
926 } else {
927 int ret = elm->type->check_constraints(elm->type,
928 memb_ptr, app_errlog, app_key);
929 if(ret) return ret;
930 /*
931 * Cannot inherit it earlier:
932 * need to make sure we get the updated version.
933 */
934 elm->memb_constraints = elm->type->check_constraints;
935 }
vlmfa67ddc2004-06-03 03:38:44 +0000936 }
937
938 return 0;
939}