blob: 4c0c3b770164d9f6ba250a15c474a45288690f2f [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 */
39#define ADVANCE(num_bytes) do { \
40 size_t num = num_bytes; \
vlmd86c9252004-08-25 01:34:11 +000041 ptr = ((char *)ptr) + num; \
vlmfa67ddc2004-06-03 03:38:44 +000042 size -= num; \
43 if(ctx->left >= 0) \
44 ctx->left -= num; \
45 consumed_myself += num; \
46 } while(0)
47
48/*
49 * Switch to the next phase of parsing.
50 */
51#define NEXT_PHASE(ctx) do { \
52 ctx->phase++; \
53 ctx->step = 0; \
54 } while(0)
55
56/*
57 * Return a standardized complex structure.
58 */
59#define RETURN(_code) do { \
60 rval.code = _code; \
61 rval.consumed = consumed_myself;\
62 return rval; \
63 } while(0)
64
65/*
66 * Tags are canonically sorted in the tag2element map.
67 */
68static int
69_t2e_cmp(const void *ap, const void *bp) {
vlmda674682004-08-11 09:07:36 +000070 const asn1_TYPE_tag2member_t *a = (const asn1_TYPE_tag2member_t *)ap;
71 const asn1_TYPE_tag2member_t *b = (const asn1_TYPE_tag2member_t *)bp;
72
vlmfa67ddc2004-06-03 03:38:44 +000073 int a_class = BER_TAG_CLASS(a->el_tag);
74 int b_class = BER_TAG_CLASS(b->el_tag);
75
76 if(a_class == b_class) {
77 ber_tlv_tag_t a_value = BER_TAG_VALUE(a->el_tag);
78 ber_tlv_tag_t b_value = BER_TAG_VALUE(b->el_tag);
79
80 if(a_value == b_value)
81 return 0;
82 else if(a_value < b_value)
83 return -1;
84 else
85 return 1;
86 } else if(a_class < b_class) {
87 return -1;
88 } else {
89 return 1;
90 }
91}
92
93/*
94 * The decoder of the SET type.
95 */
96ber_dec_rval_t
vlme413c122004-08-20 13:23:42 +000097SET_decode_ber(asn1_TYPE_descriptor_t *td,
vlmfa67ddc2004-06-03 03:38:44 +000098 void **struct_ptr, void *ptr, size_t size, int tag_mode) {
99 /*
100 * Bring closer parts of structure description.
101 */
vlme413c122004-08-20 13:23:42 +0000102 asn1_SET_specifics_t *specs = (asn1_SET_specifics_t *)td->specifics;
103 asn1_TYPE_member_t *elements = td->elements;
vlmfa67ddc2004-06-03 03:38:44 +0000104
105 /*
106 * Parts of the structure being constructed.
107 */
108 void *st = *struct_ptr; /* Target structure. */
109 ber_dec_ctx_t *ctx; /* Decoder context */
110
111 ber_tlv_tag_t tlv_tag; /* T from TLV */
112 //ber_tlv_len_t tlv_len; /* L from TLV */
113 ber_dec_rval_t rval; /* Return code from subparsers */
114
115 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
116 int edx; /* SET element's index */
117
vlme413c122004-08-20 13:23:42 +0000118 ASN_DEBUG("Decoding %s as SET", td->name);
vlmfa67ddc2004-06-03 03:38:44 +0000119
120 /*
121 * Create the target structure if it is not present already.
122 */
123 if(st == 0) {
124 st = *struct_ptr = CALLOC(1, specs->struct_size);
125 if(st == 0) {
126 RETURN(RC_FAIL);
127 }
128 }
129
130 /*
131 * Restore parsing context.
132 */
vlm1ff928d2004-08-11 08:10:13 +0000133 ctx = (ber_dec_ctx_t *)((char *)st + specs->ctx_offset);
vlmfa67ddc2004-06-03 03:38:44 +0000134
135 /*
136 * Start to parse where left previously
137 */
138 switch(ctx->phase) {
139 case 0:
140 /*
141 * PHASE 0.
142 * Check that the set of tags associated with given structure
143 * perfectly fits our expectations.
144 */
145
vlme413c122004-08-20 13:23:42 +0000146 rval = ber_check_tags(td, ctx, ptr, size,
vlmfa67ddc2004-06-03 03:38:44 +0000147 tag_mode, &ctx->left, 0);
148 if(rval.code != RC_OK) {
149 ASN_DEBUG("%s tagging check failed: %d",
vlme413c122004-08-20 13:23:42 +0000150 td->name, rval.code);
vlmfa67ddc2004-06-03 03:38:44 +0000151 consumed_myself += rval.consumed;
152 RETURN(rval.code);
153 }
154
155 if(ctx->left >= 0)
156 ctx->left += rval.consumed; /* ?Substracted below! */
157 ADVANCE(rval.consumed);
158
159 NEXT_PHASE(ctx);
160
161 ASN_DEBUG("Structure advertised %ld bytes, "
162 "buffer contains %ld", (long)ctx->left, (long)size);
163
164 /* Fall through */
165 case 1:
166 /*
167 * PHASE 1.
168 * From the place where we've left it previously,
169 * try to decode the next member from the list of
170 * this structure's elements.
171 * (ctx->step) stores the member being processed
172 * between invocations and the microphase {0,1} of parsing
173 * that member:
174 * step = (2 * <member_number> + <microphase>).
175 * Note, however, that the elements in BER may arrive out of
176 * 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 */
vlme413c122004-08-20 13:23:42 +0000180 for(edx = (ctx->step >> 1); edx < td->elements_count;
vlmfa67ddc2004-06-03 03:38:44 +0000181 ctx->step = (ctx->step & ~1) + 2,
182 edx = (ctx->step >> 1)) {
183 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
187 if(ctx->step & 1)
188 goto microphase2;
189
190 /*
191 * MICROPHASE 1: Synchronize decoding.
192 */
193
194 if(ctx->left == 0)
195 /*
196 * No more things to decode.
197 * Exit out of here and check whether all mandatory
198 * elements have been received (in the next phase).
199 */
200 break;
201
202 /*
203 * Fetch the T from TLV.
204 */
205 tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
206 switch(tag_len) {
207 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
208 /* Fall through */
209 case -1: RETURN(RC_FAIL);
210 }
211
212 if(ctx->left < 0 && ((uint8_t *)ptr)[0] == 0) {
213 if(LEFT < 2) {
214 if(SIZE_VIOLATION)
215 RETURN(RC_FAIL);
216 else
217 RETURN(RC_WMORE);
218 } else if(((uint8_t *)ptr)[1] == 0) {
219 /*
220 * Found the terminator of the
221 * indefinite length structure.
222 * Invoke the generic finalization function.
223 */
224 goto phase3;
225 }
226 }
227
228 if(BER_TAGS_EQUAL(tlv_tag, elements[edx].tag)) {
229 /*
230 * The elements seem to go in order.
231 * This is not particularly strange,
232 * but is not strongly anticipated either.
233 */
234 } else {
vlm31473082004-06-06 07:20:02 +0000235 asn1_TYPE_tag2member_t *t2m;
236 asn1_TYPE_tag2member_t key;
vlmfa67ddc2004-06-03 03:38:44 +0000237
238 key.el_tag = tlv_tag;
vlmda674682004-08-11 09:07:36 +0000239 (void *)t2m = bsearch(&key,
240 specs->tag2el, specs->tag2el_count,
vlmfa67ddc2004-06-03 03:38:44 +0000241 sizeof(specs->tag2el[0]), _t2e_cmp);
242 if(t2m) {
243 /*
244 * Found the element corresponding to the tag.
245 */
246 edx = t2m->el_no;
247 ctx->step = 2 * edx;
248 } else if(specs->extensible == 0) {
249 ASN_DEBUG("Unexpected tag %s "
250 "in non-extensible SET %s",
vlme413c122004-08-20 13:23:42 +0000251 ber_tlv_tag_string(tlv_tag), td->name);
vlmfa67ddc2004-06-03 03:38:44 +0000252 RETURN(RC_FAIL);
253 } else {
254 /* Skip this tag */
255 ssize_t skip;
256
257 ASN_DEBUG("Skipping unknown tag %s",
258 ber_tlv_tag_string(tlv_tag));
259
260 skip = ber_skip_length(
261 BER_TLV_CONSTRUCTED(ptr),
vlm1ff928d2004-08-11 08:10:13 +0000262 (char *)ptr + tag_len, LEFT - tag_len);
vlmfa67ddc2004-06-03 03:38:44 +0000263
264 switch(skip) {
265 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
266 /* Fall through */
267 case -1: RETURN(RC_FAIL);
268 }
269
270 ADVANCE(skip + tag_len);
271 ctx->step -= 2;
272 edx--;
273 continue; /* Try again with the next tag */
274 }
275 }
276
277 /*
278 * MICROPHASE 2: Invoke the member-specific decoder.
279 */
280 ctx->step |= 1; /* Confirm entering next microphase */
281 microphase2:
282
283 /*
284 * Check for duplications: must not overwrite
285 * already decoded elements.
286 */
vlm1ff928d2004-08-11 08:10:13 +0000287 if(ASN_SET_ISPRESENT2((char *)st + specs->pres_offset, edx)) {
vlme26690f2004-07-01 00:47:16 +0000288 ASN_DEBUG("SET %s: Duplicate element %s (%d)",
vlme413c122004-08-20 13:23:42 +0000289 td->name, elements[edx].name, edx);
vlmfa67ddc2004-06-03 03:38:44 +0000290 RETURN(RC_FAIL);
291 }
292
293 /*
294 * Compute the position of the member inside a structure,
295 * and also a type of containment (it may be contained
296 * as pointer or using inline inclusion).
297 */
vlmddd5a7d2004-09-10 09:18:20 +0000298 if(elements[edx].flags & ATF_POINTER) {
299 /* Member is a pointer to another structure */
vlmda674682004-08-11 09:07:36 +0000300 memb_ptr2 = (void **)((char *)st + elements[edx].memb_offset);
vlmfa67ddc2004-06-03 03:38:44 +0000301 } else {
302 /*
303 * A pointer to a pointer
304 * holding the start of the structure
305 */
306 memb_ptr = (char *)st + elements[edx].memb_offset;
307 memb_ptr2 = &memb_ptr;
308 }
309 /*
310 * Invoke the member fetch routine according to member's type
311 */
312 rval = elements[edx].type->ber_decoder(
vlmda674682004-08-11 09:07:36 +0000313 elements[edx].type,
vlmfa67ddc2004-06-03 03:38:44 +0000314 memb_ptr2, ptr, LEFT,
315 elements[edx].tag_mode);
316 switch(rval.code) {
317 case RC_OK:
vlm1ff928d2004-08-11 08:10:13 +0000318 ASN_SET_MKPRESENT((char *)st + specs->pres_offset, edx);
vlmfa67ddc2004-06-03 03:38:44 +0000319 break;
320 case RC_WMORE: /* More data expected */
321 if(!SIZE_VIOLATION) {
322 ADVANCE(rval.consumed);
323 RETURN(RC_WMORE);
324 }
325 /* Fail through */
326 case RC_FAIL: /* Fatal error */
327 RETURN(RC_FAIL);
328 } /* switch(rval) */
329
330 ADVANCE(rval.consumed);
331 } /* for(all structure members) */
332
333 phase3:
334 ctx->phase = 3;
335 /* Fall through */
336 case 3:
337 case 4: /* Only 00 is expected */
338 ASN_DEBUG("SET %s Leftover: %ld, size = %ld",
vlme413c122004-08-20 13:23:42 +0000339 td->name, (long)ctx->left, (long)size);
vlmfa67ddc2004-06-03 03:38:44 +0000340
341 /*
342 * Skip everything until the end of the SET.
343 */
344 while(ctx->left) {
345 ssize_t tl, ll;
346
347 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
348 switch(tl) {
349 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
350 /* Fall through */
351 case -1: RETURN(RC_FAIL);
352 }
353
354 /*
355 * If expected <0><0>...
356 */
357 if(ctx->left < 0
358 && ((uint8_t *)ptr)[0] == 0) {
359 if(LEFT < 2) {
360 if(SIZE_VIOLATION)
361 RETURN(RC_FAIL);
362 else
363 RETURN(RC_WMORE);
364 } else if(((uint8_t *)ptr)[1] == 0) {
365 /*
366 * Correctly finished with <0><0>.
367 */
368 ADVANCE(2);
369 ctx->left++;
370 ctx->phase = 4;
371 continue;
372 }
373 }
374
375 if(specs->extensible == 0 || ctx->phase == 4) {
376 ASN_DEBUG("Unexpected continuation "
377 "of a non-extensible type %s",
vlme413c122004-08-20 13:23:42 +0000378 td->name);
vlmfa67ddc2004-06-03 03:38:44 +0000379 RETURN(RC_FAIL);
380 }
381
382 ll = ber_skip_length(
383 BER_TLV_CONSTRUCTED(ptr),
vlm1ff928d2004-08-11 08:10:13 +0000384 (char *)ptr + tl, LEFT - tl);
vlmfa67ddc2004-06-03 03:38:44 +0000385 switch(ll) {
386 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
387 /* Fall through */
388 case -1: RETURN(RC_FAIL);
389 }
390
391 ADVANCE(tl + ll);
392 }
393
394 ctx->phase = 5;
395 case 5:
396 /*
397 * Check that all mandatory elements are present.
398 */
vlme413c122004-08-20 13:23:42 +0000399 for(edx = 0; edx < td->elements_count;
vlmfa67ddc2004-06-03 03:38:44 +0000400 edx += (8 * sizeof(specs->_mandatory_elements[0]))) {
401 unsigned int midx, pres, must;
402
403 midx = edx/(8 * sizeof(specs->_mandatory_elements[0]));
vlm1ff928d2004-08-11 08:10:13 +0000404 pres = ((unsigned int *)((char *)st+specs->pres_offset))[midx];
vlmfa67ddc2004-06-03 03:38:44 +0000405 must = ntohl(specs->_mandatory_elements[midx]);
406
407 if((pres & must) == must) {
408 /*
409 * Yes, everything seems to be in place.
410 */
411 } else {
412 ASN_DEBUG("One or more mandatory elements "
413 "of a SET %s %d (%08x.%08x)=%08x "
414 "are not present",
vlme413c122004-08-20 13:23:42 +0000415 td->name,
vlmfa67ddc2004-06-03 03:38:44 +0000416 midx,
417 pres,
418 must,
419 (~(pres & must) & must)
420 );
421 RETURN(RC_FAIL);
422 }
423 }
424
425 NEXT_PHASE(ctx);
426 }
427
428 RETURN(RC_OK);
429}
430
431/*
432 * The DER encoder of the SET type.
433 */
vlm39ba4c42004-09-22 16:06:28 +0000434asn_enc_rval_t
vlme413c122004-08-20 13:23:42 +0000435SET_encode_der(asn1_TYPE_descriptor_t *td,
vlmfa67ddc2004-06-03 03:38:44 +0000436 void *ptr, int tag_mode, ber_tlv_tag_t tag,
437 asn_app_consume_bytes_f *cb, void *app_key) {
vlme413c122004-08-20 13:23:42 +0000438 asn1_SET_specifics_t *specs = (asn1_SET_specifics_t *)td->specifics;
vlmfa67ddc2004-06-03 03:38:44 +0000439 size_t computed_size = 0;
vlm39ba4c42004-09-22 16:06:28 +0000440 asn_enc_rval_t my_erval;
vlme413c122004-08-20 13:23:42 +0000441 int t2m_build_own = (specs->tag2el_count != td->elements_count);
vlm31473082004-06-06 07:20:02 +0000442 asn1_TYPE_tag2member_t *t2m;
vlmfa67ddc2004-06-03 03:38:44 +0000443 int t2m_count;
444 ssize_t ret;
445 int edx;
446
447 /*
448 * Use existing, or build our own tags map.
449 */
450 if(t2m_build_own) {
vlme413c122004-08-20 13:23:42 +0000451 (void *)t2m = alloca(td->elements_count * sizeof(t2m[0]));
vlm7a6a60e2004-08-11 07:41:45 +0000452 if(!t2m) { /* There are such platforms */
453 my_erval.encoded = -1;
vlme413c122004-08-20 13:23:42 +0000454 my_erval.failed_type = td;
vlm7a6a60e2004-08-11 07:41:45 +0000455 my_erval.structure_ptr = ptr;
456 return my_erval;
457 }
vlmda674682004-08-11 09:07:36 +0000458 t2m_count = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000459 } else {
460 /*
461 * There is no untagged CHOICE in this SET.
462 * Employ existing table.
463 */
464 t2m = specs->tag2el;
465 t2m_count = specs->tag2el_count;
466 }
467
468 /*
469 * Gather the length of the underlying members sequence.
470 */
vlme413c122004-08-20 13:23:42 +0000471 for(edx = 0; edx < td->elements_count; edx++) {
472 asn1_TYPE_member_t *elm = &td->elements[edx];
vlm39ba4c42004-09-22 16:06:28 +0000473 asn_enc_rval_t erval;
vlmfa67ddc2004-06-03 03:38:44 +0000474 void *memb_ptr;
475
476 /*
477 * Compute the length of the encoding of this member.
478 */
vlmddd5a7d2004-09-10 09:18:20 +0000479 if(elm->flags & ATF_POINTER) {
vlmfa67ddc2004-06-03 03:38:44 +0000480 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
481 if(!memb_ptr) {
482 if(t2m_build_own) {
483 t2m[t2m_count].el_no = edx;
484 t2m[t2m_count].el_tag = 0;
485 t2m_count++;
486 }
487 continue;
488 }
489 } else {
490 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
491 }
492 erval = elm->type->der_encoder(elm->type, memb_ptr,
493 elm->tag_mode, elm->tag,
494 0, 0);
495 if(erval.encoded == -1)
496 return erval;
497 computed_size += erval.encoded;
498
499 /*
500 * Remember the outmost tag of this member.
501 */
502 if(t2m_build_own) {
503 t2m[t2m_count].el_no = edx;
504 t2m[t2m_count].el_tag = asn1_TYPE_outmost_tag(
505 elm->type, memb_ptr, elm->tag_mode, elm->tag);
506 t2m_count++;
507 } else {
508 /*
509 * No dynamic sorting is necessary.
510 */
511 }
512 }
513
514 /*
515 * Finalize order of the components.
516 */
vlme413c122004-08-20 13:23:42 +0000517 assert(t2m_count == td->elements_count);
vlmfa67ddc2004-06-03 03:38:44 +0000518 if(t2m_build_own) {
519 /*
520 * Sort the underlying members according to their
521 * canonical tags order. DER encoding mandates it.
522 */
523 qsort(t2m, t2m_count, sizeof(specs->tag2el[0]), _t2e_cmp);
524 } else {
525 /*
526 * Tags are already sorted by the compiler.
527 */
528 }
529
530 /*
531 * Encode the TLV for the sequence itself.
532 */
vlme413c122004-08-20 13:23:42 +0000533 ret = der_write_tags(td, computed_size, tag_mode, tag, cb, app_key);
vlmfa67ddc2004-06-03 03:38:44 +0000534 if(ret == -1) {
535 my_erval.encoded = -1;
vlme413c122004-08-20 13:23:42 +0000536 my_erval.failed_type = td;
vlmfa67ddc2004-06-03 03:38:44 +0000537 my_erval.structure_ptr = ptr;
538 return my_erval;
539 }
540 my_erval.encoded = computed_size + ret;
541
542 if(!cb) return my_erval;
543
544 /*
545 * Encode all members.
546 */
vlme413c122004-08-20 13:23:42 +0000547 for(edx = 0; edx < td->elements_count; edx++) {
548 asn1_TYPE_member_t *elm;
vlm39ba4c42004-09-22 16:06:28 +0000549 asn_enc_rval_t erval;
vlmfa67ddc2004-06-03 03:38:44 +0000550 void *memb_ptr;
551
552 /* Encode according to the tag order */
vlme413c122004-08-20 13:23:42 +0000553 elm = &td->elements[t2m[edx].el_no];
vlmfa67ddc2004-06-03 03:38:44 +0000554
vlmddd5a7d2004-09-10 09:18:20 +0000555 if(elm->flags & ATF_POINTER) {
vlmfa67ddc2004-06-03 03:38:44 +0000556 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
557 if(!memb_ptr) continue;
558 } else {
559 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
560 }
561 erval = elm->type->der_encoder(elm->type, memb_ptr,
562 elm->tag_mode, elm->tag,
563 cb, app_key);
564 if(erval.encoded == -1)
565 return erval;
566 computed_size -= erval.encoded;
567 }
568
569 if(computed_size != 0) {
570 /*
571 * Encoded size is not equal to the computed size.
572 */
573 my_erval.encoded = -1;
vlme413c122004-08-20 13:23:42 +0000574 my_erval.failed_type = td;
vlmfa67ddc2004-06-03 03:38:44 +0000575 my_erval.structure_ptr = ptr;
576 }
577
578 return my_erval;
579}
580
vlm39ba4c42004-09-22 16:06:28 +0000581asn_enc_rval_t
582SET_encode_xer(asn1_TYPE_descriptor_t *td, void *sptr,
583 int ilevel, enum xer_encoder_flags_e flags,
584 asn_app_consume_bytes_f *cb, void *app_key) {
585 asn_enc_rval_t er;
586 int xcan = (flags & XER_F_CANONICAL);
587 int edx;
588
589 if(!sptr)
590 _ASN_ENCODE_FAILED;
591
592 er.encoded = 0;
593
594 for(edx = 0; edx < td->elements_count; edx++) {
595 asn_enc_rval_t tmper;
596 asn1_TYPE_member_t *elm = &td->elements[edx];
597 void *memb_ptr;
598 const char *mname = elm->name;
599 unsigned int mlen = strlen(elm->name);
600
601 if(elm->flags & ATF_POINTER) {
602 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
603 if(!memb_ptr) continue; /* OPTIONAL element? */
604 } else {
605 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
606 }
607
608 if(!xcan)
609 _i_ASN_TEXT_INDENT(1, ilevel);
610 _ASN_CALLBACK3("<", 1, mname, mlen, ">", 1);
611
612 /* Print the member itself */
613 tmper = elm->type->xer_encoder(elm->type, memb_ptr,
614 ilevel + 1, flags, cb, app_key);
615 if(tmper.encoded == -1) return tmper;
616
617 _ASN_CALLBACK3("</", 2, mname, mlen, ">", 1);
618
619 er.encoded += 5 + (2 * mlen) + tmper.encoded;
620 }
621
622 if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel - 1);
623
624 return er;
625}
626
vlmfa67ddc2004-06-03 03:38:44 +0000627int
628SET_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
629 asn_app_consume_bytes_f *cb, void *app_key) {
vlmfa67ddc2004-06-03 03:38:44 +0000630 int edx;
631 int ret;
632
633 if(!sptr) return cb("<absent>", 8, app_key);
634
635 /* Dump preamble */
636 if(cb(td->name, strlen(td->name), app_key)
637 || cb(" ::= {\n", 7, app_key))
638 return -1;
639
vlme413c122004-08-20 13:23:42 +0000640 for(edx = 0; edx < td->elements_count; edx++) {
641 asn1_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
651 /* Indentation */
652 for(ret = 0; ret < ilevel; ret++) cb(" ", 1, app_key);
653
654 /* Print the member's name and stuff */
655 if(cb(elm->name, strlen(elm->name), app_key)
656 || cb(": ", 2, app_key))
657 return -1;
658
659 /* Print the member itself */
660 ret = elm->type->print_struct(elm->type, memb_ptr, ilevel + 4,
661 cb, app_key);
662 if(ret) return ret;
663
664 ret = cb("\n", 1, app_key);
665 if(ret) return ret;
666 }
667
668 /* Indentation */
669 for(ret = 0; ret < ilevel - 4; ret++) cb(" ", 1, app_key);
670
671 return cb("}", 1, app_key);
672}
673
674void
675SET_free(asn1_TYPE_descriptor_t *td, void *ptr, int contents_only) {
vlmfa67ddc2004-06-03 03:38:44 +0000676 int edx;
677
678 if(!td || !ptr)
679 return;
680
681 ASN_DEBUG("Freeing %s as SET", td->name);
682
vlme413c122004-08-20 13:23:42 +0000683 for(edx = 0; edx < td->elements_count; edx++) {
684 asn1_TYPE_member_t *elm = &td->elements[edx];
vlmfa67ddc2004-06-03 03:38:44 +0000685 void *memb_ptr;
vlmddd5a7d2004-09-10 09:18:20 +0000686 if(elm->flags & ATF_POINTER) {
vlmfa67ddc2004-06-03 03:38:44 +0000687 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
688 if(memb_ptr)
689 elm->type->free_struct(elm->type, memb_ptr, 0);
690 } else {
691 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
692 elm->type->free_struct(elm->type, memb_ptr, 1);
693 }
694 }
695
696 if(!contents_only) {
697 FREEMEM(ptr);
698 }
699}
700
701int
702SET_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
703 asn_app_consume_bytes_f *app_errlog, void *app_key) {
vlmfa67ddc2004-06-03 03:38:44 +0000704 int edx;
705
706 if(!sptr) {
vlme3f0f282004-08-11 09:44:13 +0000707 _ASN_ERRLOG(app_errlog, app_key,
vlm758530a2004-08-22 13:47:59 +0000708 "%s: value not given (%s:%d)",
709 td->name, __FILE__, __LINE__);
vlmfa67ddc2004-06-03 03:38:44 +0000710 return -1;
711 }
712
713 /*
714 * Iterate over structure members and check their validity.
715 */
vlme413c122004-08-20 13:23:42 +0000716 for(edx = 0; edx < td->elements_count; edx++) {
717 asn1_TYPE_member_t *elm = &td->elements[edx];
vlmfa67ddc2004-06-03 03:38:44 +0000718 const void *memb_ptr;
719
vlmddd5a7d2004-09-10 09:18:20 +0000720 if(elm->flags & ATF_POINTER) {
vlmfa67ddc2004-06-03 03:38:44 +0000721 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
722 if(!memb_ptr) {
vlmddd5a7d2004-09-10 09:18:20 +0000723 if(elm->optional)
724 continue;
725 _ASN_ERRLOG(app_errlog, app_key,
726 "%s: mandatory element %s absent (%s:%d)",
727 td->name, elm->name, __FILE__, __LINE__);
728 return -1;
vlmfa67ddc2004-06-03 03:38:44 +0000729 }
730 } else {
731 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
732 }
733
vlme413c122004-08-20 13:23:42 +0000734 if(elm->memb_constraints) {
735 int ret = elm->memb_constraints(elm->type, memb_ptr,
736 app_errlog, app_key);
737 if(ret) return ret;
738 } else {
739 int ret = elm->type->check_constraints(elm->type,
740 memb_ptr, app_errlog, app_key);
741 if(ret) return ret;
742 /*
743 * Cannot inherit it earlier:
744 * need to make sure we get the updated version.
745 */
746 elm->memb_constraints = elm->type->check_constraints;
747 }
vlmfa67ddc2004-06-03 03:38:44 +0000748 }
749
750 return 0;
751}