blob: be36016219be637d03201b8836929fb523df3706 [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
vlmfa67ddc2004-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 */
vlmc5190612004-08-18 04:53:32 +000020#define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left)
vlmfa67ddc2004-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 */
vlmb42843a2004-06-05 08:17:50 +000033#define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size)
vlmfa67ddc2004-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 */
vlmef1b4c02004-09-23 22:06:26 +000039#undef ADVANCE
vlmfa67ddc2004-06-03 03:38:44 +000040#define ADVANCE(num_bytes) do { \
41 size_t num = num_bytes; \
vlmd86c9252004-08-25 01:34:11 +000042 ptr = ((char *)ptr) + num; \
vlmfa67ddc2004-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 */
vlmef1b4c02004-09-23 22:06:26 +000052#undef NEXT_PHASE
vlmfa67ddc2004-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 */
vlmef1b4c02004-09-23 22:06:26 +000061#undef RETURN
vlmfa67ddc2004-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) {
vlmef6355b2004-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;
vlmda674682004-08-11 09:07:36 +000075
vlmfa67ddc2004-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 */
vlm9de248e2004-10-20 15:50:55 +000099asn_dec_rval_t
vlmef6355b2004-09-29 13:26:15 +0000100SET_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
vlmfa67ddc2004-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 */
vlmef6355b2004-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;
vlmfa67ddc2004-06-03 03:38:44 +0000107
108 /*
109 * Parts of the structure being constructed.
110 */
111 void *st = *struct_ptr; /* Target structure. */
vlmef6355b2004-09-29 13:26:15 +0000112 asn_struct_ctx_t *ctx; /* Decoder context */
vlmfa67ddc2004-06-03 03:38:44 +0000113
114 ber_tlv_tag_t tlv_tag; /* T from TLV */
vlm9de248e2004-10-20 15:50:55 +0000115 asn_dec_rval_t rval; /* Return code from subparsers */
vlmfa67ddc2004-06-03 03:38:44 +0000116
117 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
118 int edx; /* SET element's index */
119
vlme413c122004-08-20 13:23:42 +0000120 ASN_DEBUG("Decoding %s as SET", td->name);
vlmfa67ddc2004-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 */
vlmef6355b2004-09-29 13:26:15 +0000135 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
vlmfa67ddc2004-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
vlmef6355b2004-09-29 13:26:15 +0000148 rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size,
vlm6678cb12004-09-26 13:10:40 +0000149 tag_mode, 1, &ctx->left, 0);
vlmfa67ddc2004-06-03 03:38:44 +0000150 if(rval.code != RC_OK) {
151 ASN_DEBUG("%s tagging check failed: %d",
vlme413c122004-08-20 13:23:42 +0000152 td->name, rval.code);
vlm2ebb08c2004-10-05 06:36:44 +0000153 return rval;
vlmfa67ddc2004-06-03 03:38:44 +0000154 }
155
156 if(ctx->left >= 0)
157 ctx->left += rval.consumed; /* ?Substracted below! */
158 ADVANCE(rval.consumed);
159
160 NEXT_PHASE(ctx);
161
162 ASN_DEBUG("Structure advertised %ld bytes, "
163 "buffer contains %ld", (long)ctx->left, (long)size);
164
165 /* Fall through */
166 case 1:
167 /*
168 * PHASE 1.
169 * From the place where we've left it previously,
170 * try to decode the next member from the list of
171 * this structure's elements.
172 * (ctx->step) stores the member being processed
173 * between invocations and the microphase {0,1} of parsing
174 * that member:
175 * step = (2 * <member_number> + <microphase>).
176 * Note, however, that the elements in BER may arrive out of
177 * order, yet DER mandates that they shall arive in the
178 * canonical order of their tags. So, there is a room
179 * for optimization.
180 */
vlme413c122004-08-20 13:23:42 +0000181 for(edx = (ctx->step >> 1); edx < td->elements_count;
vlmfa67ddc2004-06-03 03:38:44 +0000182 ctx->step = (ctx->step & ~1) + 2,
183 edx = (ctx->step >> 1)) {
184 void *memb_ptr; /* Pointer to the member */
vlmda674682004-08-11 09:07:36 +0000185 void **memb_ptr2; /* Pointer to that pointer */
vlmfa67ddc2004-06-03 03:38:44 +0000186 ssize_t tag_len; /* Length of TLV's T */
187
188 if(ctx->step & 1)
189 goto microphase2;
190
191 /*
192 * MICROPHASE 1: Synchronize decoding.
193 */
194
195 if(ctx->left == 0)
196 /*
197 * No more things to decode.
198 * Exit out of here and check whether all mandatory
199 * elements have been received (in the next phase).
200 */
201 break;
202
203 /*
204 * Fetch the T from TLV.
205 */
206 tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
207 switch(tag_len) {
208 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
209 /* Fall through */
210 case -1: RETURN(RC_FAIL);
211 }
212
213 if(ctx->left < 0 && ((uint8_t *)ptr)[0] == 0) {
214 if(LEFT < 2) {
215 if(SIZE_VIOLATION)
216 RETURN(RC_FAIL);
217 else
218 RETURN(RC_WMORE);
219 } else if(((uint8_t *)ptr)[1] == 0) {
220 /*
221 * Found the terminator of the
222 * indefinite length structure.
223 * Invoke the generic finalization function.
224 */
225 goto phase3;
226 }
227 }
228
229 if(BER_TAGS_EQUAL(tlv_tag, elements[edx].tag)) {
230 /*
231 * The elements seem to go in order.
232 * This is not particularly strange,
233 * but is not strongly anticipated either.
234 */
235 } else {
vlmef6355b2004-09-29 13:26:15 +0000236 asn_TYPE_tag2member_t *t2m;
237 asn_TYPE_tag2member_t key;
vlmfa67ddc2004-06-03 03:38:44 +0000238
239 key.el_tag = tlv_tag;
vlmda674682004-08-11 09:07:36 +0000240 (void *)t2m = bsearch(&key,
241 specs->tag2el, specs->tag2el_count,
vlmfa67ddc2004-06-03 03:38:44 +0000242 sizeof(specs->tag2el[0]), _t2e_cmp);
243 if(t2m) {
244 /*
245 * Found the element corresponding to the tag.
246 */
247 edx = t2m->el_no;
248 ctx->step = 2 * edx;
249 } else if(specs->extensible == 0) {
250 ASN_DEBUG("Unexpected tag %s "
251 "in non-extensible SET %s",
vlme413c122004-08-20 13:23:42 +0000252 ber_tlv_tag_string(tlv_tag), td->name);
vlmfa67ddc2004-06-03 03:38:44 +0000253 RETURN(RC_FAIL);
254 } else {
255 /* Skip this tag */
256 ssize_t skip;
257
258 ASN_DEBUG("Skipping unknown tag %s",
259 ber_tlv_tag_string(tlv_tag));
260
vlm6130a2f2004-09-29 14:19:14 +0000261 skip = ber_skip_length(opt_codec_ctx,
vlmfa67ddc2004-06-03 03:38:44 +0000262 BER_TLV_CONSTRUCTED(ptr),
vlm1ff928d2004-08-11 08:10:13 +0000263 (char *)ptr + tag_len, LEFT - tag_len);
vlmfa67ddc2004-06-03 03:38:44 +0000264
265 switch(skip) {
266 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
267 /* Fall through */
268 case -1: RETURN(RC_FAIL);
269 }
270
271 ADVANCE(skip + tag_len);
272 ctx->step -= 2;
273 edx--;
274 continue; /* Try again with the next tag */
275 }
276 }
277
278 /*
279 * MICROPHASE 2: Invoke the member-specific decoder.
280 */
281 ctx->step |= 1; /* Confirm entering next microphase */
282 microphase2:
283
284 /*
285 * Check for duplications: must not overwrite
286 * already decoded elements.
287 */
vlm1ff928d2004-08-11 08:10:13 +0000288 if(ASN_SET_ISPRESENT2((char *)st + specs->pres_offset, edx)) {
vlme26690f2004-07-01 00:47:16 +0000289 ASN_DEBUG("SET %s: Duplicate element %s (%d)",
vlme413c122004-08-20 13:23:42 +0000290 td->name, elements[edx].name, edx);
vlmfa67ddc2004-06-03 03:38:44 +0000291 RETURN(RC_FAIL);
292 }
293
294 /*
295 * Compute the position of the member inside a structure,
296 * and also a type of containment (it may be contained
297 * as pointer or using inline inclusion).
298 */
vlmddd5a7d2004-09-10 09:18:20 +0000299 if(elements[edx].flags & ATF_POINTER) {
300 /* Member is a pointer to another structure */
vlmda674682004-08-11 09:07:36 +0000301 memb_ptr2 = (void **)((char *)st + elements[edx].memb_offset);
vlmfa67ddc2004-06-03 03:38:44 +0000302 } else {
303 /*
304 * A pointer to a pointer
305 * holding the start of the structure
306 */
307 memb_ptr = (char *)st + elements[edx].memb_offset;
308 memb_ptr2 = &memb_ptr;
309 }
310 /*
311 * Invoke the member fetch routine according to member's type
312 */
vlmef6355b2004-09-29 13:26:15 +0000313 rval = elements[edx].type->ber_decoder(opt_codec_ctx,
vlmda674682004-08-11 09:07:36 +0000314 elements[edx].type,
vlmfa67ddc2004-06-03 03:38:44 +0000315 memb_ptr2, ptr, LEFT,
316 elements[edx].tag_mode);
317 switch(rval.code) {
318 case RC_OK:
vlm1ff928d2004-08-11 08:10:13 +0000319 ASN_SET_MKPRESENT((char *)st + specs->pres_offset, edx);
vlmfa67ddc2004-06-03 03:38:44 +0000320 break;
321 case RC_WMORE: /* More data expected */
322 if(!SIZE_VIOLATION) {
323 ADVANCE(rval.consumed);
324 RETURN(RC_WMORE);
325 }
326 /* Fail through */
327 case RC_FAIL: /* Fatal error */
328 RETURN(RC_FAIL);
329 } /* switch(rval) */
330
331 ADVANCE(rval.consumed);
332 } /* for(all structure members) */
333
334 phase3:
335 ctx->phase = 3;
336 /* Fall through */
337 case 3:
338 case 4: /* Only 00 is expected */
339 ASN_DEBUG("SET %s Leftover: %ld, size = %ld",
vlme413c122004-08-20 13:23:42 +0000340 td->name, (long)ctx->left, (long)size);
vlmfa67ddc2004-06-03 03:38:44 +0000341
342 /*
343 * Skip everything until the end of the SET.
344 */
345 while(ctx->left) {
346 ssize_t tl, ll;
347
348 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
349 switch(tl) {
350 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
351 /* Fall through */
352 case -1: RETURN(RC_FAIL);
353 }
354
355 /*
356 * If expected <0><0>...
357 */
358 if(ctx->left < 0
359 && ((uint8_t *)ptr)[0] == 0) {
360 if(LEFT < 2) {
361 if(SIZE_VIOLATION)
362 RETURN(RC_FAIL);
363 else
364 RETURN(RC_WMORE);
365 } else if(((uint8_t *)ptr)[1] == 0) {
366 /*
367 * Correctly finished with <0><0>.
368 */
369 ADVANCE(2);
370 ctx->left++;
371 ctx->phase = 4;
372 continue;
373 }
374 }
375
376 if(specs->extensible == 0 || ctx->phase == 4) {
377 ASN_DEBUG("Unexpected continuation "
378 "of a non-extensible type %s",
vlme413c122004-08-20 13:23:42 +0000379 td->name);
vlmfa67ddc2004-06-03 03:38:44 +0000380 RETURN(RC_FAIL);
381 }
382
vlm6130a2f2004-09-29 14:19:14 +0000383 ll = ber_skip_length(opt_codec_ctx,
vlmfa67ddc2004-06-03 03:38:44 +0000384 BER_TLV_CONSTRUCTED(ptr),
vlm1ff928d2004-08-11 08:10:13 +0000385 (char *)ptr + tl, LEFT - tl);
vlmfa67ddc2004-06-03 03:38:44 +0000386 switch(ll) {
387 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
388 /* Fall through */
389 case -1: RETURN(RC_FAIL);
390 }
391
392 ADVANCE(tl + ll);
393 }
394
395 ctx->phase = 5;
396 case 5:
397 /*
398 * Check that all mandatory elements are present.
399 */
vlme413c122004-08-20 13:23:42 +0000400 for(edx = 0; edx < td->elements_count;
vlmfa67ddc2004-06-03 03:38:44 +0000401 edx += (8 * sizeof(specs->_mandatory_elements[0]))) {
402 unsigned int midx, pres, must;
403
404 midx = edx/(8 * sizeof(specs->_mandatory_elements[0]));
vlm1ff928d2004-08-11 08:10:13 +0000405 pres = ((unsigned int *)((char *)st+specs->pres_offset))[midx];
vlmfa67ddc2004-06-03 03:38:44 +0000406 must = ntohl(specs->_mandatory_elements[midx]);
407
408 if((pres & must) == must) {
409 /*
410 * Yes, everything seems to be in place.
411 */
412 } else {
413 ASN_DEBUG("One or more mandatory elements "
414 "of a SET %s %d (%08x.%08x)=%08x "
415 "are not present",
vlme413c122004-08-20 13:23:42 +0000416 td->name,
vlmfa67ddc2004-06-03 03:38:44 +0000417 midx,
418 pres,
419 must,
420 (~(pres & must) & must)
421 );
422 RETURN(RC_FAIL);
423 }
424 }
425
426 NEXT_PHASE(ctx);
427 }
428
429 RETURN(RC_OK);
430}
431
432/*
433 * The DER encoder of the SET type.
434 */
vlm39ba4c42004-09-22 16:06:28 +0000435asn_enc_rval_t
vlmef6355b2004-09-29 13:26:15 +0000436SET_encode_der(asn_TYPE_descriptor_t *td,
vlma3296662004-10-03 09:12:07 +0000437 void *sptr, int tag_mode, ber_tlv_tag_t tag,
vlmfa67ddc2004-06-03 03:38:44 +0000438 asn_app_consume_bytes_f *cb, void *app_key) {
vlmef6355b2004-09-29 13:26:15 +0000439 asn_SET_specifics_t *specs = (asn_SET_specifics_t *)td->specifics;
vlmfa67ddc2004-06-03 03:38:44 +0000440 size_t computed_size = 0;
vlma3296662004-10-03 09:12:07 +0000441 asn_enc_rval_t er;
vlme413c122004-08-20 13:23:42 +0000442 int t2m_build_own = (specs->tag2el_count != td->elements_count);
vlmef6355b2004-09-29 13:26:15 +0000443 asn_TYPE_tag2member_t *t2m;
vlmfa67ddc2004-06-03 03:38:44 +0000444 int t2m_count;
445 ssize_t ret;
446 int edx;
447
448 /*
449 * Use existing, or build our own tags map.
450 */
451 if(t2m_build_own) {
vlme413c122004-08-20 13:23:42 +0000452 (void *)t2m = alloca(td->elements_count * sizeof(t2m[0]));
vlma3296662004-10-03 09:12:07 +0000453 if(!t2m) _ASN_ENCODE_FAILED; /* There are such platforms */
vlmda674682004-08-11 09:07:36 +0000454 t2m_count = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000455 } else {
456 /*
457 * There is no untagged CHOICE in this SET.
458 * Employ existing table.
459 */
460 t2m = specs->tag2el;
461 t2m_count = specs->tag2el_count;
462 }
463
464 /*
465 * Gather the length of the underlying members sequence.
466 */
vlme413c122004-08-20 13:23:42 +0000467 for(edx = 0; edx < td->elements_count; edx++) {
vlmef6355b2004-09-29 13:26:15 +0000468 asn_TYPE_member_t *elm = &td->elements[edx];
vlma3296662004-10-03 09:12:07 +0000469 asn_enc_rval_t tmper;
vlmfa67ddc2004-06-03 03:38:44 +0000470 void *memb_ptr;
471
472 /*
473 * Compute the length of the encoding of this member.
474 */
vlmddd5a7d2004-09-10 09:18:20 +0000475 if(elm->flags & ATF_POINTER) {
vlma3296662004-10-03 09:12:07 +0000476 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
vlmfa67ddc2004-06-03 03:38:44 +0000477 if(!memb_ptr) {
478 if(t2m_build_own) {
479 t2m[t2m_count].el_no = edx;
480 t2m[t2m_count].el_tag = 0;
481 t2m_count++;
482 }
483 continue;
484 }
485 } else {
vlma3296662004-10-03 09:12:07 +0000486 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
vlmfa67ddc2004-06-03 03:38:44 +0000487 }
vlma3296662004-10-03 09:12:07 +0000488 tmper = elm->type->der_encoder(elm->type, memb_ptr,
vlmfa67ddc2004-06-03 03:38:44 +0000489 elm->tag_mode, elm->tag,
490 0, 0);
vlma3296662004-10-03 09:12:07 +0000491 if(tmper.encoded == -1)
492 return tmper;
493 computed_size += tmper.encoded;
vlmfa67ddc2004-06-03 03:38:44 +0000494
495 /*
496 * Remember the outmost tag of this member.
497 */
498 if(t2m_build_own) {
499 t2m[t2m_count].el_no = edx;
vlmef6355b2004-09-29 13:26:15 +0000500 t2m[t2m_count].el_tag = asn_TYPE_outmost_tag(
vlmfa67ddc2004-06-03 03:38:44 +0000501 elm->type, memb_ptr, elm->tag_mode, elm->tag);
502 t2m_count++;
503 } else {
504 /*
505 * No dynamic sorting is necessary.
506 */
507 }
508 }
509
510 /*
511 * Finalize order of the components.
512 */
vlme413c122004-08-20 13:23:42 +0000513 assert(t2m_count == td->elements_count);
vlmfa67ddc2004-06-03 03:38:44 +0000514 if(t2m_build_own) {
515 /*
516 * Sort the underlying members according to their
517 * canonical tags order. DER encoding mandates it.
518 */
519 qsort(t2m, t2m_count, sizeof(specs->tag2el[0]), _t2e_cmp);
520 } else {
521 /*
522 * Tags are already sorted by the compiler.
523 */
524 }
525
526 /*
527 * Encode the TLV for the sequence itself.
528 */
vlm6678cb12004-09-26 13:10:40 +0000529 ret = der_write_tags(td, computed_size, tag_mode, 1, tag, cb, app_key);
vlma3296662004-10-03 09:12:07 +0000530 if(ret == -1) _ASN_ENCODE_FAILED;
531 er.encoded = computed_size + ret;
vlmfa67ddc2004-06-03 03:38:44 +0000532
vlma3296662004-10-03 09:12:07 +0000533 if(!cb) return er;
vlmfa67ddc2004-06-03 03:38:44 +0000534
535 /*
536 * Encode all members.
537 */
vlme413c122004-08-20 13:23:42 +0000538 for(edx = 0; edx < td->elements_count; edx++) {
vlmef6355b2004-09-29 13:26:15 +0000539 asn_TYPE_member_t *elm;
vlma3296662004-10-03 09:12:07 +0000540 asn_enc_rval_t tmper;
vlmfa67ddc2004-06-03 03:38:44 +0000541 void *memb_ptr;
542
543 /* Encode according to the tag order */
vlme413c122004-08-20 13:23:42 +0000544 elm = &td->elements[t2m[edx].el_no];
vlmfa67ddc2004-06-03 03:38:44 +0000545
vlmddd5a7d2004-09-10 09:18:20 +0000546 if(elm->flags & ATF_POINTER) {
vlma3296662004-10-03 09:12:07 +0000547 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
vlmfa67ddc2004-06-03 03:38:44 +0000548 if(!memb_ptr) continue;
549 } else {
vlma3296662004-10-03 09:12:07 +0000550 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
vlmfa67ddc2004-06-03 03:38:44 +0000551 }
vlma3296662004-10-03 09:12:07 +0000552 tmper = elm->type->der_encoder(elm->type, memb_ptr,
vlmfa67ddc2004-06-03 03:38:44 +0000553 elm->tag_mode, elm->tag,
554 cb, app_key);
vlma3296662004-10-03 09:12:07 +0000555 if(tmper.encoded == -1)
556 return tmper;
557 computed_size -= tmper.encoded;
vlmfa67ddc2004-06-03 03:38:44 +0000558 }
559
560 if(computed_size != 0) {
561 /*
562 * Encoded size is not equal to the computed size.
563 */
vlma3296662004-10-03 09:12:07 +0000564 _ASN_ENCODE_FAILED;
vlmfa67ddc2004-06-03 03:38:44 +0000565 }
566
vlma3296662004-10-03 09:12:07 +0000567 return er;
vlmfa67ddc2004-06-03 03:38:44 +0000568}
569
vlm39ba4c42004-09-22 16:06:28 +0000570asn_enc_rval_t
vlmef6355b2004-09-29 13:26:15 +0000571SET_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
vlm39ba4c42004-09-22 16:06:28 +0000572 int ilevel, enum xer_encoder_flags_e flags,
573 asn_app_consume_bytes_f *cb, void *app_key) {
vlma3296662004-10-03 09:12:07 +0000574 asn_SET_specifics_t *specs = (asn_SET_specifics_t *)td->specifics;
vlm39ba4c42004-09-22 16:06:28 +0000575 asn_enc_rval_t er;
576 int xcan = (flags & XER_F_CANONICAL);
vlma3296662004-10-03 09:12:07 +0000577 asn_TYPE_tag2member_t *t2m = specs->tag2el_cxer;
578 int t2m_count = specs->tag2el_cxer_count;
vlm39ba4c42004-09-22 16:06:28 +0000579 int edx;
580
581 if(!sptr)
582 _ASN_ENCODE_FAILED;
583
vlma3296662004-10-03 09:12:07 +0000584 assert(t2m_count == td->elements_count);
585
vlm39ba4c42004-09-22 16:06:28 +0000586 er.encoded = 0;
587
vlma3296662004-10-03 09:12:07 +0000588 for(edx = 0; edx < t2m_count; edx++) {
vlm39ba4c42004-09-22 16:06:28 +0000589 asn_enc_rval_t tmper;
vlma3296662004-10-03 09:12:07 +0000590 asn_TYPE_member_t *elm;
vlm39ba4c42004-09-22 16:06:28 +0000591 void *memb_ptr;
vlma3296662004-10-03 09:12:07 +0000592 const char *mname;
593 unsigned int mlen;
594
595 elm = &td->elements[t2m[edx].el_no];
596 mname = elm->name;
597 mlen = strlen(elm->name);
vlm39ba4c42004-09-22 16:06:28 +0000598
599 if(elm->flags & ATF_POINTER) {
600 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
601 if(!memb_ptr) continue; /* OPTIONAL element? */
602 } else {
603 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
604 }
605
606 if(!xcan)
607 _i_ASN_TEXT_INDENT(1, ilevel);
608 _ASN_CALLBACK3("<", 1, mname, mlen, ">", 1);
609
610 /* Print the member itself */
611 tmper = elm->type->xer_encoder(elm->type, memb_ptr,
612 ilevel + 1, flags, cb, app_key);
613 if(tmper.encoded == -1) return tmper;
614
615 _ASN_CALLBACK3("</", 2, mname, mlen, ">", 1);
616
617 er.encoded += 5 + (2 * mlen) + tmper.encoded;
618 }
619
620 if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel - 1);
621
622 return er;
vlma3296662004-10-03 09:12:07 +0000623cb_failed:
624 _ASN_ENCODE_FAILED;
vlm39ba4c42004-09-22 16:06:28 +0000625}
626
vlmfa67ddc2004-06-03 03:38:44 +0000627int
vlmef6355b2004-09-29 13:26:15 +0000628SET_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
vlmfa67ddc2004-06-03 03:38:44 +0000629 asn_app_consume_bytes_f *cb, void *app_key) {
vlmfa67ddc2004-06-03 03:38:44 +0000630 int edx;
631 int ret;
632
vlm6678cb12004-09-26 13:10:40 +0000633 if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +0000634
635 /* Dump preamble */
vlm6678cb12004-09-26 13:10:40 +0000636 if(cb(td->name, strlen(td->name), app_key) < 0
637 || cb(" ::= {", 6, app_key) < 0)
vlmfa67ddc2004-06-03 03:38:44 +0000638 return -1;
639
vlme413c122004-08-20 13:23:42 +0000640 for(edx = 0; edx < td->elements_count; edx++) {
vlmef6355b2004-09-29 13:26:15 +0000641 asn_TYPE_member_t *elm = &td->elements[edx];
vlmfa67ddc2004-06-03 03:38:44 +0000642 const void *memb_ptr;
643
vlmddd5a7d2004-09-10 09:18:20 +0000644 if(elm->flags & ATF_POINTER) {
vlmfa67ddc2004-06-03 03:38:44 +0000645 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
646 if(!memb_ptr) continue;
647 } else {
648 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
649 }
650
vlm6678cb12004-09-26 13:10:40 +0000651 _i_INDENT(1);
vlmfa67ddc2004-06-03 03:38:44 +0000652
653 /* Print the member's name and stuff */
vlm6678cb12004-09-26 13:10:40 +0000654 if(cb(elm->name, strlen(elm->name), app_key) < 0
655 || cb(": ", 2, app_key) < 0)
vlmfa67ddc2004-06-03 03:38:44 +0000656 return -1;
657
658 /* Print the member itself */
vlm6678cb12004-09-26 13:10:40 +0000659 ret = elm->type->print_struct(elm->type, memb_ptr, ilevel + 1,
vlmfa67ddc2004-06-03 03:38:44 +0000660 cb, app_key);
661 if(ret) return ret;
vlmfa67ddc2004-06-03 03:38:44 +0000662 }
663
vlm6678cb12004-09-26 13:10:40 +0000664 ilevel--;
665 _i_INDENT(1);
vlmfa67ddc2004-06-03 03:38:44 +0000666
vlm6678cb12004-09-26 13:10:40 +0000667 return (cb("}", 1, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +0000668}
669
670void
vlmef6355b2004-09-29 13:26:15 +0000671SET_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
vlmfa67ddc2004-06-03 03:38:44 +0000672 int edx;
673
674 if(!td || !ptr)
675 return;
676
677 ASN_DEBUG("Freeing %s as SET", td->name);
678
vlme413c122004-08-20 13:23:42 +0000679 for(edx = 0; edx < td->elements_count; edx++) {
vlmef6355b2004-09-29 13:26:15 +0000680 asn_TYPE_member_t *elm = &td->elements[edx];
vlmfa67ddc2004-06-03 03:38:44 +0000681 void *memb_ptr;
vlmddd5a7d2004-09-10 09:18:20 +0000682 if(elm->flags & ATF_POINTER) {
vlmfa67ddc2004-06-03 03:38:44 +0000683 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
684 if(memb_ptr)
685 elm->type->free_struct(elm->type, memb_ptr, 0);
686 } else {
687 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
688 elm->type->free_struct(elm->type, memb_ptr, 1);
689 }
690 }
691
692 if(!contents_only) {
693 FREEMEM(ptr);
694 }
695}
696
697int
vlmef6355b2004-09-29 13:26:15 +0000698SET_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
vlmfa67ddc2004-06-03 03:38:44 +0000699 asn_app_consume_bytes_f *app_errlog, void *app_key) {
vlmfa67ddc2004-06-03 03:38:44 +0000700 int edx;
701
702 if(!sptr) {
vlme3f0f282004-08-11 09:44:13 +0000703 _ASN_ERRLOG(app_errlog, app_key,
vlm758530a2004-08-22 13:47:59 +0000704 "%s: value not given (%s:%d)",
705 td->name, __FILE__, __LINE__);
vlmfa67ddc2004-06-03 03:38:44 +0000706 return -1;
707 }
708
709 /*
710 * Iterate over structure members and check their validity.
711 */
vlme413c122004-08-20 13:23:42 +0000712 for(edx = 0; edx < td->elements_count; edx++) {
vlmef6355b2004-09-29 13:26:15 +0000713 asn_TYPE_member_t *elm = &td->elements[edx];
vlmfa67ddc2004-06-03 03:38:44 +0000714 const void *memb_ptr;
715
vlmddd5a7d2004-09-10 09:18:20 +0000716 if(elm->flags & ATF_POINTER) {
vlmfa67ddc2004-06-03 03:38:44 +0000717 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
718 if(!memb_ptr) {
vlmddd5a7d2004-09-10 09:18:20 +0000719 if(elm->optional)
720 continue;
721 _ASN_ERRLOG(app_errlog, app_key,
722 "%s: mandatory element %s absent (%s:%d)",
723 td->name, elm->name, __FILE__, __LINE__);
724 return -1;
vlmfa67ddc2004-06-03 03:38:44 +0000725 }
726 } else {
727 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
728 }
729
vlme413c122004-08-20 13:23:42 +0000730 if(elm->memb_constraints) {
731 int ret = elm->memb_constraints(elm->type, memb_ptr,
732 app_errlog, app_key);
733 if(ret) return ret;
734 } else {
735 int ret = elm->type->check_constraints(elm->type,
736 memb_ptr, app_errlog, app_key);
737 if(ret) return ret;
738 /*
739 * Cannot inherit it earlier:
740 * need to make sure we get the updated version.
741 */
742 elm->memb_constraints = elm->type->check_constraints;
743 }
vlmfa67ddc2004-06-03 03:38:44 +0000744 }
745
746 return 0;
747}