blob: 647bb83a234d1bdf5fb5c3c07fd27aa7ba7a15dc [file] [log] [blame]
Lev Walkinf15320b2004-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 */
Lev Walkina9cc46e2004-09-22 16:06:28 +00005#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00006#include <constr_SET.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00007#include <assert.h> /* for assert() */
8
Lev Walkin2c962732004-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
Lev Walkinf15320b2004-06-03 03:38:44 +000015/*
16 * Number of bytes left for this structure.
17 * (ctx->left) indicates the number of bytes _transferred_ for the structure.
18 * (size) contains the number of bytes in the buffer passed.
19 */
Lev Walkinec1ffd42004-08-18 04:53:32 +000020#define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left)
Lev Walkinf15320b2004-06-03 03:38:44 +000021
22/*
23 * If the subprocessor function returns with an indication that it wants
24 * more data, it may well be a fatal decoding problem, because the
25 * size is constrained by the <TLV>'s L, even if the buffer size allows
26 * reading more data.
27 * For example, consider the buffer containing the following TLVs:
28 * <T:5><L:1><V> <T:6>...
29 * The TLV length clearly indicates that one byte is expected in V, but
30 * if the V processor returns with "want more data" even if the buffer
31 * contains way more data than the V processor have seen.
32 */
Lev Walkind9bd7752004-06-05 08:17:50 +000033#define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size)
Lev Walkinf15320b2004-06-03 03:38:44 +000034
35/*
36 * This macro "eats" the part of the buffer which is definitely "consumed",
37 * i.e. was correctly converted into local representation or rightfully skipped.
38 */
Lev Walkincc6a9102004-09-23 22:06:26 +000039#undef ADVANCE
Lev Walkinf15320b2004-06-03 03:38:44 +000040#define ADVANCE(num_bytes) do { \
41 size_t num = num_bytes; \
Lev Walkin4ce78ca2004-08-25 01:34:11 +000042 ptr = ((char *)ptr) + num; \
Lev Walkinf15320b2004-06-03 03:38:44 +000043 size -= num; \
44 if(ctx->left >= 0) \
45 ctx->left -= num; \
46 consumed_myself += num; \
47 } while(0)
48
49/*
50 * Switch to the next phase of parsing.
51 */
Lev Walkincc6a9102004-09-23 22:06:26 +000052#undef NEXT_PHASE
Lev Walkinf15320b2004-06-03 03:38:44 +000053#define NEXT_PHASE(ctx) do { \
54 ctx->phase++; \
55 ctx->step = 0; \
56 } while(0)
57
58/*
59 * Return a standardized complex structure.
60 */
Lev Walkincc6a9102004-09-23 22:06:26 +000061#undef RETURN
Lev Walkinf15320b2004-06-03 03:38:44 +000062#define RETURN(_code) do { \
63 rval.code = _code; \
64 rval.consumed = consumed_myself;\
65 return rval; \
66 } while(0)
67
68/*
69 * Tags are canonically sorted in the tag2element map.
70 */
71static int
72_t2e_cmp(const void *ap, const void *bp) {
Lev Walkin5e033762004-09-29 13:26:15 +000073 const asn_TYPE_tag2member_t *a = (const asn_TYPE_tag2member_t *)ap;
74 const asn_TYPE_tag2member_t *b = (const asn_TYPE_tag2member_t *)bp;
Lev Walkinc2346572004-08-11 09:07:36 +000075
Lev Walkinf15320b2004-06-03 03:38:44 +000076 int a_class = BER_TAG_CLASS(a->el_tag);
77 int b_class = BER_TAG_CLASS(b->el_tag);
78
79 if(a_class == b_class) {
80 ber_tlv_tag_t a_value = BER_TAG_VALUE(a->el_tag);
81 ber_tlv_tag_t b_value = BER_TAG_VALUE(b->el_tag);
82
83 if(a_value == b_value)
84 return 0;
85 else if(a_value < b_value)
86 return -1;
87 else
88 return 1;
89 } else if(a_class < b_class) {
90 return -1;
91 } else {
92 return 1;
93 }
94}
95
96/*
97 * The decoder of the SET type.
98 */
99ber_dec_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000100SET_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkinf15320b2004-06-03 03:38:44 +0000101 void **struct_ptr, void *ptr, size_t size, int tag_mode) {
102 /*
103 * Bring closer parts of structure description.
104 */
Lev Walkin5e033762004-09-29 13:26:15 +0000105 asn_SET_specifics_t *specs = (asn_SET_specifics_t *)td->specifics;
106 asn_TYPE_member_t *elements = td->elements;
Lev Walkinf15320b2004-06-03 03:38:44 +0000107
108 /*
109 * Parts of the structure being constructed.
110 */
111 void *st = *struct_ptr; /* Target structure. */
Lev Walkin5e033762004-09-29 13:26:15 +0000112 asn_struct_ctx_t *ctx; /* Decoder context */
Lev Walkinf15320b2004-06-03 03:38:44 +0000113
114 ber_tlv_tag_t tlv_tag; /* T from TLV */
Lev Walkinf15320b2004-06-03 03:38:44 +0000115 ber_dec_rval_t rval; /* Return code from subparsers */
116
117 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
118 int edx; /* SET element's index */
119
Lev Walkin449f8322004-08-20 13:23:42 +0000120 ASN_DEBUG("Decoding %s as SET", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000121
122 /*
123 * Create the target structure if it is not present already.
124 */
125 if(st == 0) {
126 st = *struct_ptr = CALLOC(1, specs->struct_size);
127 if(st == 0) {
128 RETURN(RC_FAIL);
129 }
130 }
131
132 /*
133 * Restore parsing context.
134 */
Lev Walkin5e033762004-09-29 13:26:15 +0000135 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000136
137 /*
138 * Start to parse where left previously
139 */
140 switch(ctx->phase) {
141 case 0:
142 /*
143 * PHASE 0.
144 * Check that the set of tags associated with given structure
145 * perfectly fits our expectations.
146 */
147
Lev Walkin5e033762004-09-29 13:26:15 +0000148 rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size,
Lev Walkin8e8078a2004-09-26 13:10:40 +0000149 tag_mode, 1, &ctx->left, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000150 if(rval.code != RC_OK) {
151 ASN_DEBUG("%s tagging check failed: %d",
Lev Walkin449f8322004-08-20 13:23:42 +0000152 td->name, rval.code);
Lev Walkinf15320b2004-06-03 03:38:44 +0000153 consumed_myself += rval.consumed;
154 RETURN(rval.code);
155 }
156
157 if(ctx->left >= 0)
158 ctx->left += rval.consumed; /* ?Substracted below! */
159 ADVANCE(rval.consumed);
160
161 NEXT_PHASE(ctx);
162
163 ASN_DEBUG("Structure advertised %ld bytes, "
164 "buffer contains %ld", (long)ctx->left, (long)size);
165
166 /* Fall through */
167 case 1:
168 /*
169 * PHASE 1.
170 * From the place where we've left it previously,
171 * try to decode the next member from the list of
172 * this structure's elements.
173 * (ctx->step) stores the member being processed
174 * between invocations and the microphase {0,1} of parsing
175 * that member:
176 * step = (2 * <member_number> + <microphase>).
177 * Note, however, that the elements in BER may arrive out of
178 * order, yet DER mandates that they shall arive in the
179 * canonical order of their tags. So, there is a room
180 * for optimization.
181 */
Lev Walkin449f8322004-08-20 13:23:42 +0000182 for(edx = (ctx->step >> 1); edx < td->elements_count;
Lev Walkinf15320b2004-06-03 03:38:44 +0000183 ctx->step = (ctx->step & ~1) + 2,
184 edx = (ctx->step >> 1)) {
185 void *memb_ptr; /* Pointer to the member */
Lev Walkinc2346572004-08-11 09:07:36 +0000186 void **memb_ptr2; /* Pointer to that pointer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000187 ssize_t tag_len; /* Length of TLV's T */
188
189 if(ctx->step & 1)
190 goto microphase2;
191
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
230 if(BER_TAGS_EQUAL(tlv_tag, elements[edx].tag)) {
231 /*
232 * The elements seem to go in order.
233 * This is not particularly strange,
234 * but is not strongly anticipated either.
235 */
236 } else {
Lev Walkin5e033762004-09-29 13:26:15 +0000237 asn_TYPE_tag2member_t *t2m;
238 asn_TYPE_tag2member_t key;
Lev Walkinf15320b2004-06-03 03:38:44 +0000239
240 key.el_tag = tlv_tag;
Lev Walkinc2346572004-08-11 09:07:36 +0000241 (void *)t2m = bsearch(&key,
242 specs->tag2el, specs->tag2el_count,
Lev Walkinf15320b2004-06-03 03:38:44 +0000243 sizeof(specs->tag2el[0]), _t2e_cmp);
244 if(t2m) {
245 /*
246 * Found the element corresponding to the tag.
247 */
248 edx = t2m->el_no;
249 ctx->step = 2 * edx;
250 } else if(specs->extensible == 0) {
251 ASN_DEBUG("Unexpected tag %s "
252 "in non-extensible SET %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000253 ber_tlv_tag_string(tlv_tag), td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000254 RETURN(RC_FAIL);
255 } else {
256 /* Skip this tag */
257 ssize_t skip;
258
259 ASN_DEBUG("Skipping unknown tag %s",
260 ber_tlv_tag_string(tlv_tag));
261
262 skip = ber_skip_length(
263 BER_TLV_CONSTRUCTED(ptr),
Lev Walkin4d9528c2004-08-11 08:10:13 +0000264 (char *)ptr + tag_len, LEFT - tag_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000265
266 switch(skip) {
267 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
268 /* Fall through */
269 case -1: RETURN(RC_FAIL);
270 }
271
272 ADVANCE(skip + tag_len);
273 ctx->step -= 2;
274 edx--;
275 continue; /* Try again with the next tag */
276 }
277 }
278
279 /*
280 * MICROPHASE 2: Invoke the member-specific decoder.
281 */
282 ctx->step |= 1; /* Confirm entering next microphase */
283 microphase2:
284
285 /*
286 * Check for duplications: must not overwrite
287 * already decoded elements.
288 */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000289 if(ASN_SET_ISPRESENT2((char *)st + specs->pres_offset, edx)) {
Lev Walkinbbe04752004-07-01 00:47:16 +0000290 ASN_DEBUG("SET %s: Duplicate element %s (%d)",
Lev Walkin449f8322004-08-20 13:23:42 +0000291 td->name, elements[edx].name, edx);
Lev Walkinf15320b2004-06-03 03:38:44 +0000292 RETURN(RC_FAIL);
293 }
294
295 /*
296 * Compute the position of the member inside a structure,
297 * and also a type of containment (it may be contained
298 * as pointer or using inline inclusion).
299 */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000300 if(elements[edx].flags & ATF_POINTER) {
301 /* Member is a pointer to another structure */
Lev Walkinc2346572004-08-11 09:07:36 +0000302 memb_ptr2 = (void **)((char *)st + elements[edx].memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000303 } else {
304 /*
305 * A pointer to a pointer
306 * holding the start of the structure
307 */
308 memb_ptr = (char *)st + elements[edx].memb_offset;
309 memb_ptr2 = &memb_ptr;
310 }
311 /*
312 * Invoke the member fetch routine according to member's type
313 */
Lev Walkin5e033762004-09-29 13:26:15 +0000314 rval = elements[edx].type->ber_decoder(opt_codec_ctx,
Lev Walkinc2346572004-08-11 09:07:36 +0000315 elements[edx].type,
Lev Walkinf15320b2004-06-03 03:38:44 +0000316 memb_ptr2, ptr, LEFT,
317 elements[edx].tag_mode);
318 switch(rval.code) {
319 case RC_OK:
Lev Walkin4d9528c2004-08-11 08:10:13 +0000320 ASN_SET_MKPRESENT((char *)st + specs->pres_offset, edx);
Lev Walkinf15320b2004-06-03 03:38:44 +0000321 break;
322 case RC_WMORE: /* More data expected */
323 if(!SIZE_VIOLATION) {
324 ADVANCE(rval.consumed);
325 RETURN(RC_WMORE);
326 }
327 /* Fail through */
328 case RC_FAIL: /* Fatal error */
329 RETURN(RC_FAIL);
330 } /* switch(rval) */
331
332 ADVANCE(rval.consumed);
333 } /* for(all structure members) */
334
335 phase3:
336 ctx->phase = 3;
337 /* Fall through */
338 case 3:
339 case 4: /* Only 00 is expected */
340 ASN_DEBUG("SET %s Leftover: %ld, size = %ld",
Lev Walkin449f8322004-08-20 13:23:42 +0000341 td->name, (long)ctx->left, (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000342
343 /*
344 * Skip everything until the end of the SET.
345 */
346 while(ctx->left) {
347 ssize_t tl, ll;
348
349 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
350 switch(tl) {
351 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
352 /* Fall through */
353 case -1: RETURN(RC_FAIL);
354 }
355
356 /*
357 * If expected <0><0>...
358 */
359 if(ctx->left < 0
360 && ((uint8_t *)ptr)[0] == 0) {
361 if(LEFT < 2) {
362 if(SIZE_VIOLATION)
363 RETURN(RC_FAIL);
364 else
365 RETURN(RC_WMORE);
366 } else if(((uint8_t *)ptr)[1] == 0) {
367 /*
368 * Correctly finished with <0><0>.
369 */
370 ADVANCE(2);
371 ctx->left++;
372 ctx->phase = 4;
373 continue;
374 }
375 }
376
377 if(specs->extensible == 0 || ctx->phase == 4) {
378 ASN_DEBUG("Unexpected continuation "
379 "of a non-extensible type %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000380 td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000381 RETURN(RC_FAIL);
382 }
383
384 ll = ber_skip_length(
385 BER_TLV_CONSTRUCTED(ptr),
Lev Walkin4d9528c2004-08-11 08:10:13 +0000386 (char *)ptr + tl, LEFT - tl);
Lev Walkinf15320b2004-06-03 03:38:44 +0000387 switch(ll) {
388 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
389 /* Fall through */
390 case -1: RETURN(RC_FAIL);
391 }
392
393 ADVANCE(tl + ll);
394 }
395
396 ctx->phase = 5;
397 case 5:
398 /*
399 * Check that all mandatory elements are present.
400 */
Lev Walkin449f8322004-08-20 13:23:42 +0000401 for(edx = 0; edx < td->elements_count;
Lev Walkinf15320b2004-06-03 03:38:44 +0000402 edx += (8 * sizeof(specs->_mandatory_elements[0]))) {
403 unsigned int midx, pres, must;
404
405 midx = edx/(8 * sizeof(specs->_mandatory_elements[0]));
Lev Walkin4d9528c2004-08-11 08:10:13 +0000406 pres = ((unsigned int *)((char *)st+specs->pres_offset))[midx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000407 must = ntohl(specs->_mandatory_elements[midx]);
408
409 if((pres & must) == must) {
410 /*
411 * Yes, everything seems to be in place.
412 */
413 } else {
414 ASN_DEBUG("One or more mandatory elements "
415 "of a SET %s %d (%08x.%08x)=%08x "
416 "are not present",
Lev Walkin449f8322004-08-20 13:23:42 +0000417 td->name,
Lev Walkinf15320b2004-06-03 03:38:44 +0000418 midx,
419 pres,
420 must,
421 (~(pres & must) & must)
422 );
423 RETURN(RC_FAIL);
424 }
425 }
426
427 NEXT_PHASE(ctx);
428 }
429
430 RETURN(RC_OK);
431}
432
433/*
434 * The DER encoder of the SET type.
435 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000436asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000437SET_encode_der(asn_TYPE_descriptor_t *td,
Lev Walkinf15320b2004-06-03 03:38:44 +0000438 void *ptr, int tag_mode, ber_tlv_tag_t tag,
439 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin5e033762004-09-29 13:26:15 +0000440 asn_SET_specifics_t *specs = (asn_SET_specifics_t *)td->specifics;
Lev Walkinf15320b2004-06-03 03:38:44 +0000441 size_t computed_size = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000442 asn_enc_rval_t my_erval;
Lev Walkin449f8322004-08-20 13:23:42 +0000443 int t2m_build_own = (specs->tag2el_count != td->elements_count);
Lev Walkin5e033762004-09-29 13:26:15 +0000444 asn_TYPE_tag2member_t *t2m;
Lev Walkinf15320b2004-06-03 03:38:44 +0000445 int t2m_count;
446 ssize_t ret;
447 int edx;
448
449 /*
450 * Use existing, or build our own tags map.
451 */
452 if(t2m_build_own) {
Lev Walkin449f8322004-08-20 13:23:42 +0000453 (void *)t2m = alloca(td->elements_count * sizeof(t2m[0]));
Lev Walkin7e0d2cb2004-08-11 07:41:45 +0000454 if(!t2m) { /* There are such platforms */
455 my_erval.encoded = -1;
Lev Walkin449f8322004-08-20 13:23:42 +0000456 my_erval.failed_type = td;
Lev Walkin7e0d2cb2004-08-11 07:41:45 +0000457 my_erval.structure_ptr = ptr;
458 return my_erval;
459 }
Lev Walkinc2346572004-08-11 09:07:36 +0000460 t2m_count = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000461 } else {
462 /*
463 * There is no untagged CHOICE in this SET.
464 * Employ existing table.
465 */
466 t2m = specs->tag2el;
467 t2m_count = specs->tag2el_count;
468 }
469
470 /*
471 * Gather the length of the underlying members sequence.
472 */
Lev Walkin449f8322004-08-20 13:23:42 +0000473 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000474 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkina9cc46e2004-09-22 16:06:28 +0000475 asn_enc_rval_t erval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000476 void *memb_ptr;
477
478 /*
479 * Compute the length of the encoding of this member.
480 */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000481 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000482 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
483 if(!memb_ptr) {
484 if(t2m_build_own) {
485 t2m[t2m_count].el_no = edx;
486 t2m[t2m_count].el_tag = 0;
487 t2m_count++;
488 }
489 continue;
490 }
491 } else {
492 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
493 }
494 erval = elm->type->der_encoder(elm->type, memb_ptr,
495 elm->tag_mode, elm->tag,
496 0, 0);
497 if(erval.encoded == -1)
498 return erval;
499 computed_size += erval.encoded;
500
501 /*
502 * Remember the outmost tag of this member.
503 */
504 if(t2m_build_own) {
505 t2m[t2m_count].el_no = edx;
Lev Walkin5e033762004-09-29 13:26:15 +0000506 t2m[t2m_count].el_tag = asn_TYPE_outmost_tag(
Lev Walkinf15320b2004-06-03 03:38:44 +0000507 elm->type, memb_ptr, elm->tag_mode, elm->tag);
508 t2m_count++;
509 } else {
510 /*
511 * No dynamic sorting is necessary.
512 */
513 }
514 }
515
516 /*
517 * Finalize order of the components.
518 */
Lev Walkin449f8322004-08-20 13:23:42 +0000519 assert(t2m_count == td->elements_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000520 if(t2m_build_own) {
521 /*
522 * Sort the underlying members according to their
523 * canonical tags order. DER encoding mandates it.
524 */
525 qsort(t2m, t2m_count, sizeof(specs->tag2el[0]), _t2e_cmp);
526 } else {
527 /*
528 * Tags are already sorted by the compiler.
529 */
530 }
531
532 /*
533 * Encode the TLV for the sequence itself.
534 */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000535 ret = der_write_tags(td, computed_size, tag_mode, 1, tag, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +0000536 if(ret == -1) {
537 my_erval.encoded = -1;
Lev Walkin449f8322004-08-20 13:23:42 +0000538 my_erval.failed_type = td;
Lev Walkinf15320b2004-06-03 03:38:44 +0000539 my_erval.structure_ptr = ptr;
540 return my_erval;
541 }
542 my_erval.encoded = computed_size + ret;
543
544 if(!cb) return my_erval;
545
546 /*
547 * Encode all members.
548 */
Lev Walkin449f8322004-08-20 13:23:42 +0000549 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000550 asn_TYPE_member_t *elm;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000551 asn_enc_rval_t erval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000552 void *memb_ptr;
553
554 /* Encode according to the tag order */
Lev Walkin449f8322004-08-20 13:23:42 +0000555 elm = &td->elements[t2m[edx].el_no];
Lev Walkinf15320b2004-06-03 03:38:44 +0000556
Lev Walkincc93b0f2004-09-10 09:18:20 +0000557 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000558 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
559 if(!memb_ptr) continue;
560 } else {
561 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
562 }
563 erval = elm->type->der_encoder(elm->type, memb_ptr,
564 elm->tag_mode, elm->tag,
565 cb, app_key);
566 if(erval.encoded == -1)
567 return erval;
568 computed_size -= erval.encoded;
569 }
570
571 if(computed_size != 0) {
572 /*
573 * Encoded size is not equal to the computed size.
574 */
575 my_erval.encoded = -1;
Lev Walkin449f8322004-08-20 13:23:42 +0000576 my_erval.failed_type = td;
Lev Walkinf15320b2004-06-03 03:38:44 +0000577 my_erval.structure_ptr = ptr;
578 }
579
580 return my_erval;
581}
582
Lev Walkina9cc46e2004-09-22 16:06:28 +0000583asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000584SET_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000585 int ilevel, enum xer_encoder_flags_e flags,
586 asn_app_consume_bytes_f *cb, void *app_key) {
587 asn_enc_rval_t er;
588 int xcan = (flags & XER_F_CANONICAL);
589 int edx;
590
591 if(!sptr)
592 _ASN_ENCODE_FAILED;
593
594 er.encoded = 0;
595
596 for(edx = 0; edx < td->elements_count; edx++) {
597 asn_enc_rval_t tmper;
Lev Walkin5e033762004-09-29 13:26:15 +0000598 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkina9cc46e2004-09-22 16:06:28 +0000599 void *memb_ptr;
600 const char *mname = elm->name;
601 unsigned int mlen = strlen(elm->name);
602
603 if(elm->flags & ATF_POINTER) {
604 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
605 if(!memb_ptr) continue; /* OPTIONAL element? */
606 } else {
607 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
608 }
609
610 if(!xcan)
611 _i_ASN_TEXT_INDENT(1, ilevel);
612 _ASN_CALLBACK3("<", 1, mname, mlen, ">", 1);
613
614 /* Print the member itself */
615 tmper = elm->type->xer_encoder(elm->type, memb_ptr,
616 ilevel + 1, flags, cb, app_key);
617 if(tmper.encoded == -1) return tmper;
618
619 _ASN_CALLBACK3("</", 2, mname, mlen, ">", 1);
620
621 er.encoded += 5 + (2 * mlen) + tmper.encoded;
622 }
623
624 if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel - 1);
625
626 return er;
627}
628
Lev Walkinf15320b2004-06-03 03:38:44 +0000629int
Lev Walkin5e033762004-09-29 13:26:15 +0000630SET_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000631 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000632 int edx;
633 int ret;
634
Lev Walkin8e8078a2004-09-26 13:10:40 +0000635 if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000636
637 /* Dump preamble */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000638 if(cb(td->name, strlen(td->name), app_key) < 0
639 || cb(" ::= {", 6, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000640 return -1;
641
Lev Walkin449f8322004-08-20 13:23:42 +0000642 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000643 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000644 const void *memb_ptr;
645
Lev Walkincc93b0f2004-09-10 09:18:20 +0000646 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000647 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
648 if(!memb_ptr) continue;
649 } else {
650 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
651 }
652
Lev Walkin8e8078a2004-09-26 13:10:40 +0000653 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000654
655 /* Print the member's name and stuff */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000656 if(cb(elm->name, strlen(elm->name), app_key) < 0
657 || cb(": ", 2, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000658 return -1;
659
660 /* Print the member itself */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000661 ret = elm->type->print_struct(elm->type, memb_ptr, ilevel + 1,
Lev Walkinf15320b2004-06-03 03:38:44 +0000662 cb, app_key);
663 if(ret) return ret;
Lev Walkinf15320b2004-06-03 03:38:44 +0000664 }
665
Lev Walkin8e8078a2004-09-26 13:10:40 +0000666 ilevel--;
667 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000668
Lev Walkin8e8078a2004-09-26 13:10:40 +0000669 return (cb("}", 1, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000670}
671
672void
Lev Walkin5e033762004-09-29 13:26:15 +0000673SET_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000674 int edx;
675
676 if(!td || !ptr)
677 return;
678
679 ASN_DEBUG("Freeing %s as SET", td->name);
680
Lev Walkin449f8322004-08-20 13:23:42 +0000681 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000682 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000683 void *memb_ptr;
Lev Walkincc93b0f2004-09-10 09:18:20 +0000684 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000685 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
686 if(memb_ptr)
687 elm->type->free_struct(elm->type, memb_ptr, 0);
688 } else {
689 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
690 elm->type->free_struct(elm->type, memb_ptr, 1);
691 }
692 }
693
694 if(!contents_only) {
695 FREEMEM(ptr);
696 }
697}
698
699int
Lev Walkin5e033762004-09-29 13:26:15 +0000700SET_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000701 asn_app_consume_bytes_f *app_errlog, void *app_key) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000702 int edx;
703
704 if(!sptr) {
Lev Walkinba4e5182004-08-11 09:44:13 +0000705 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkin16835b62004-08-22 13:47:59 +0000706 "%s: value not given (%s:%d)",
707 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000708 return -1;
709 }
710
711 /*
712 * Iterate over structure members and check their validity.
713 */
Lev Walkin449f8322004-08-20 13:23:42 +0000714 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000715 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000716 const void *memb_ptr;
717
Lev Walkincc93b0f2004-09-10 09:18:20 +0000718 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000719 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
720 if(!memb_ptr) {
Lev Walkincc93b0f2004-09-10 09:18:20 +0000721 if(elm->optional)
722 continue;
723 _ASN_ERRLOG(app_errlog, app_key,
724 "%s: mandatory element %s absent (%s:%d)",
725 td->name, elm->name, __FILE__, __LINE__);
726 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000727 }
728 } else {
729 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
730 }
731
Lev Walkin449f8322004-08-20 13:23:42 +0000732 if(elm->memb_constraints) {
733 int ret = elm->memb_constraints(elm->type, memb_ptr,
734 app_errlog, app_key);
735 if(ret) return ret;
736 } else {
737 int ret = elm->type->check_constraints(elm->type,
738 memb_ptr, app_errlog, app_key);
739 if(ret) return ret;
740 /*
741 * Cannot inherit it earlier:
742 * need to make sure we get the updated version.
743 */
744 elm->memb_constraints = elm->type->check_constraints;
745 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000746 }
747
748 return 0;
749}