blob: 93da7828fbcec6826477528ba7b4e521416e76bc [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 Walkinc2346572004-08-11 09:07:36 +000073 const asn1_TYPE_tag2member_t *a = (const asn1_TYPE_tag2member_t *)ap;
74 const asn1_TYPE_tag2member_t *b = (const asn1_TYPE_tag2member_t *)bp;
75
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 Walkin449f8322004-08-20 13:23:42 +0000100SET_decode_ber(asn1_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 Walkin449f8322004-08-20 13:23:42 +0000105 asn1_SET_specifics_t *specs = (asn1_SET_specifics_t *)td->specifics;
106 asn1_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. */
112 ber_dec_ctx_t *ctx; /* Decoder context */
113
114 ber_tlv_tag_t tlv_tag; /* T from TLV */
115 //ber_tlv_len_t tlv_len; /* L from TLV */
116 ber_dec_rval_t rval; /* Return code from subparsers */
117
118 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
119 int edx; /* SET element's index */
120
Lev Walkin449f8322004-08-20 13:23:42 +0000121 ASN_DEBUG("Decoding %s as SET", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000122
123 /*
124 * Create the target structure if it is not present already.
125 */
126 if(st == 0) {
127 st = *struct_ptr = CALLOC(1, specs->struct_size);
128 if(st == 0) {
129 RETURN(RC_FAIL);
130 }
131 }
132
133 /*
134 * Restore parsing context.
135 */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000136 ctx = (ber_dec_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000137
138 /*
139 * Start to parse where left previously
140 */
141 switch(ctx->phase) {
142 case 0:
143 /*
144 * PHASE 0.
145 * Check that the set of tags associated with given structure
146 * perfectly fits our expectations.
147 */
148
Lev Walkin449f8322004-08-20 13:23:42 +0000149 rval = ber_check_tags(td, ctx, ptr, size,
Lev Walkinf15320b2004-06-03 03:38:44 +0000150 tag_mode, &ctx->left, 0);
151 if(rval.code != RC_OK) {
152 ASN_DEBUG("%s tagging check failed: %d",
Lev Walkin449f8322004-08-20 13:23:42 +0000153 td->name, rval.code);
Lev Walkinf15320b2004-06-03 03:38:44 +0000154 consumed_myself += rval.consumed;
155 RETURN(rval.code);
156 }
157
158 if(ctx->left >= 0)
159 ctx->left += rval.consumed; /* ?Substracted below! */
160 ADVANCE(rval.consumed);
161
162 NEXT_PHASE(ctx);
163
164 ASN_DEBUG("Structure advertised %ld bytes, "
165 "buffer contains %ld", (long)ctx->left, (long)size);
166
167 /* Fall through */
168 case 1:
169 /*
170 * PHASE 1.
171 * From the place where we've left it previously,
172 * try to decode the next member from the list of
173 * this structure's elements.
174 * (ctx->step) stores the member being processed
175 * between invocations and the microphase {0,1} of parsing
176 * that member:
177 * step = (2 * <member_number> + <microphase>).
178 * Note, however, that the elements in BER may arrive out of
179 * order, yet DER mandates that they shall arive in the
180 * canonical order of their tags. So, there is a room
181 * for optimization.
182 */
Lev Walkin449f8322004-08-20 13:23:42 +0000183 for(edx = (ctx->step >> 1); edx < td->elements_count;
Lev Walkinf15320b2004-06-03 03:38:44 +0000184 ctx->step = (ctx->step & ~1) + 2,
185 edx = (ctx->step >> 1)) {
186 void *memb_ptr; /* Pointer to the member */
Lev Walkinc2346572004-08-11 09:07:36 +0000187 void **memb_ptr2; /* Pointer to that pointer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000188 ssize_t tag_len; /* Length of TLV's T */
189
190 if(ctx->step & 1)
191 goto microphase2;
192
193 /*
194 * MICROPHASE 1: Synchronize decoding.
195 */
196
197 if(ctx->left == 0)
198 /*
199 * No more things to decode.
200 * Exit out of here and check whether all mandatory
201 * elements have been received (in the next phase).
202 */
203 break;
204
205 /*
206 * Fetch the T from TLV.
207 */
208 tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
209 switch(tag_len) {
210 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
211 /* Fall through */
212 case -1: RETURN(RC_FAIL);
213 }
214
215 if(ctx->left < 0 && ((uint8_t *)ptr)[0] == 0) {
216 if(LEFT < 2) {
217 if(SIZE_VIOLATION)
218 RETURN(RC_FAIL);
219 else
220 RETURN(RC_WMORE);
221 } else if(((uint8_t *)ptr)[1] == 0) {
222 /*
223 * Found the terminator of the
224 * indefinite length structure.
225 * Invoke the generic finalization function.
226 */
227 goto phase3;
228 }
229 }
230
231 if(BER_TAGS_EQUAL(tlv_tag, elements[edx].tag)) {
232 /*
233 * The elements seem to go in order.
234 * This is not particularly strange,
235 * but is not strongly anticipated either.
236 */
237 } else {
Lev Walkin4c36e302004-06-06 07:20:02 +0000238 asn1_TYPE_tag2member_t *t2m;
239 asn1_TYPE_tag2member_t key;
Lev Walkinf15320b2004-06-03 03:38:44 +0000240
241 key.el_tag = tlv_tag;
Lev Walkinc2346572004-08-11 09:07:36 +0000242 (void *)t2m = bsearch(&key,
243 specs->tag2el, specs->tag2el_count,
Lev Walkinf15320b2004-06-03 03:38:44 +0000244 sizeof(specs->tag2el[0]), _t2e_cmp);
245 if(t2m) {
246 /*
247 * Found the element corresponding to the tag.
248 */
249 edx = t2m->el_no;
250 ctx->step = 2 * edx;
251 } else if(specs->extensible == 0) {
252 ASN_DEBUG("Unexpected tag %s "
253 "in non-extensible SET %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000254 ber_tlv_tag_string(tlv_tag), td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000255 RETURN(RC_FAIL);
256 } else {
257 /* Skip this tag */
258 ssize_t skip;
259
260 ASN_DEBUG("Skipping unknown tag %s",
261 ber_tlv_tag_string(tlv_tag));
262
263 skip = ber_skip_length(
264 BER_TLV_CONSTRUCTED(ptr),
Lev Walkin4d9528c2004-08-11 08:10:13 +0000265 (char *)ptr + tag_len, LEFT - tag_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000266
267 switch(skip) {
268 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
269 /* Fall through */
270 case -1: RETURN(RC_FAIL);
271 }
272
273 ADVANCE(skip + tag_len);
274 ctx->step -= 2;
275 edx--;
276 continue; /* Try again with the next tag */
277 }
278 }
279
280 /*
281 * MICROPHASE 2: Invoke the member-specific decoder.
282 */
283 ctx->step |= 1; /* Confirm entering next microphase */
284 microphase2:
285
286 /*
287 * Check for duplications: must not overwrite
288 * already decoded elements.
289 */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000290 if(ASN_SET_ISPRESENT2((char *)st + specs->pres_offset, edx)) {
Lev Walkinbbe04752004-07-01 00:47:16 +0000291 ASN_DEBUG("SET %s: Duplicate element %s (%d)",
Lev Walkin449f8322004-08-20 13:23:42 +0000292 td->name, elements[edx].name, edx);
Lev Walkinf15320b2004-06-03 03:38:44 +0000293 RETURN(RC_FAIL);
294 }
295
296 /*
297 * Compute the position of the member inside a structure,
298 * and also a type of containment (it may be contained
299 * as pointer or using inline inclusion).
300 */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000301 if(elements[edx].flags & ATF_POINTER) {
302 /* Member is a pointer to another structure */
Lev Walkinc2346572004-08-11 09:07:36 +0000303 memb_ptr2 = (void **)((char *)st + elements[edx].memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000304 } else {
305 /*
306 * A pointer to a pointer
307 * holding the start of the structure
308 */
309 memb_ptr = (char *)st + elements[edx].memb_offset;
310 memb_ptr2 = &memb_ptr;
311 }
312 /*
313 * Invoke the member fetch routine according to member's type
314 */
315 rval = elements[edx].type->ber_decoder(
Lev Walkinc2346572004-08-11 09:07:36 +0000316 elements[edx].type,
Lev Walkinf15320b2004-06-03 03:38:44 +0000317 memb_ptr2, ptr, LEFT,
318 elements[edx].tag_mode);
319 switch(rval.code) {
320 case RC_OK:
Lev Walkin4d9528c2004-08-11 08:10:13 +0000321 ASN_SET_MKPRESENT((char *)st + specs->pres_offset, edx);
Lev Walkinf15320b2004-06-03 03:38:44 +0000322 break;
323 case RC_WMORE: /* More data expected */
324 if(!SIZE_VIOLATION) {
325 ADVANCE(rval.consumed);
326 RETURN(RC_WMORE);
327 }
328 /* Fail through */
329 case RC_FAIL: /* Fatal error */
330 RETURN(RC_FAIL);
331 } /* switch(rval) */
332
333 ADVANCE(rval.consumed);
334 } /* for(all structure members) */
335
336 phase3:
337 ctx->phase = 3;
338 /* Fall through */
339 case 3:
340 case 4: /* Only 00 is expected */
341 ASN_DEBUG("SET %s Leftover: %ld, size = %ld",
Lev Walkin449f8322004-08-20 13:23:42 +0000342 td->name, (long)ctx->left, (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000343
344 /*
345 * Skip everything until the end of the SET.
346 */
347 while(ctx->left) {
348 ssize_t tl, ll;
349
350 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
351 switch(tl) {
352 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
353 /* Fall through */
354 case -1: RETURN(RC_FAIL);
355 }
356
357 /*
358 * If expected <0><0>...
359 */
360 if(ctx->left < 0
361 && ((uint8_t *)ptr)[0] == 0) {
362 if(LEFT < 2) {
363 if(SIZE_VIOLATION)
364 RETURN(RC_FAIL);
365 else
366 RETURN(RC_WMORE);
367 } else if(((uint8_t *)ptr)[1] == 0) {
368 /*
369 * Correctly finished with <0><0>.
370 */
371 ADVANCE(2);
372 ctx->left++;
373 ctx->phase = 4;
374 continue;
375 }
376 }
377
378 if(specs->extensible == 0 || ctx->phase == 4) {
379 ASN_DEBUG("Unexpected continuation "
380 "of a non-extensible type %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000381 td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000382 RETURN(RC_FAIL);
383 }
384
385 ll = ber_skip_length(
386 BER_TLV_CONSTRUCTED(ptr),
Lev Walkin4d9528c2004-08-11 08:10:13 +0000387 (char *)ptr + tl, LEFT - tl);
Lev Walkinf15320b2004-06-03 03:38:44 +0000388 switch(ll) {
389 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
390 /* Fall through */
391 case -1: RETURN(RC_FAIL);
392 }
393
394 ADVANCE(tl + ll);
395 }
396
397 ctx->phase = 5;
398 case 5:
399 /*
400 * Check that all mandatory elements are present.
401 */
Lev Walkin449f8322004-08-20 13:23:42 +0000402 for(edx = 0; edx < td->elements_count;
Lev Walkinf15320b2004-06-03 03:38:44 +0000403 edx += (8 * sizeof(specs->_mandatory_elements[0]))) {
404 unsigned int midx, pres, must;
405
406 midx = edx/(8 * sizeof(specs->_mandatory_elements[0]));
Lev Walkin4d9528c2004-08-11 08:10:13 +0000407 pres = ((unsigned int *)((char *)st+specs->pres_offset))[midx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000408 must = ntohl(specs->_mandatory_elements[midx]);
409
410 if((pres & must) == must) {
411 /*
412 * Yes, everything seems to be in place.
413 */
414 } else {
415 ASN_DEBUG("One or more mandatory elements "
416 "of a SET %s %d (%08x.%08x)=%08x "
417 "are not present",
Lev Walkin449f8322004-08-20 13:23:42 +0000418 td->name,
Lev Walkinf15320b2004-06-03 03:38:44 +0000419 midx,
420 pres,
421 must,
422 (~(pres & must) & must)
423 );
424 RETURN(RC_FAIL);
425 }
426 }
427
428 NEXT_PHASE(ctx);
429 }
430
431 RETURN(RC_OK);
432}
433
434/*
435 * The DER encoder of the SET type.
436 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000437asn_enc_rval_t
Lev Walkin449f8322004-08-20 13:23:42 +0000438SET_encode_der(asn1_TYPE_descriptor_t *td,
Lev Walkinf15320b2004-06-03 03:38:44 +0000439 void *ptr, int tag_mode, ber_tlv_tag_t tag,
440 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin449f8322004-08-20 13:23:42 +0000441 asn1_SET_specifics_t *specs = (asn1_SET_specifics_t *)td->specifics;
Lev Walkinf15320b2004-06-03 03:38:44 +0000442 size_t computed_size = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000443 asn_enc_rval_t my_erval;
Lev Walkin449f8322004-08-20 13:23:42 +0000444 int t2m_build_own = (specs->tag2el_count != td->elements_count);
Lev Walkin4c36e302004-06-06 07:20:02 +0000445 asn1_TYPE_tag2member_t *t2m;
Lev Walkinf15320b2004-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) {
Lev Walkin449f8322004-08-20 13:23:42 +0000454 (void *)t2m = alloca(td->elements_count * sizeof(t2m[0]));
Lev Walkin7e0d2cb2004-08-11 07:41:45 +0000455 if(!t2m) { /* There are such platforms */
456 my_erval.encoded = -1;
Lev Walkin449f8322004-08-20 13:23:42 +0000457 my_erval.failed_type = td;
Lev Walkin7e0d2cb2004-08-11 07:41:45 +0000458 my_erval.structure_ptr = ptr;
459 return my_erval;
460 }
Lev Walkinc2346572004-08-11 09:07:36 +0000461 t2m_count = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000462 } else {
463 /*
464 * There is no untagged CHOICE in this SET.
465 * Employ existing table.
466 */
467 t2m = specs->tag2el;
468 t2m_count = specs->tag2el_count;
469 }
470
471 /*
472 * Gather the length of the underlying members sequence.
473 */
Lev Walkin449f8322004-08-20 13:23:42 +0000474 for(edx = 0; edx < td->elements_count; edx++) {
475 asn1_TYPE_member_t *elm = &td->elements[edx];
Lev Walkina9cc46e2004-09-22 16:06:28 +0000476 asn_enc_rval_t erval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000477 void *memb_ptr;
478
479 /*
480 * Compute the length of the encoding of this member.
481 */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000482 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000483 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
484 if(!memb_ptr) {
485 if(t2m_build_own) {
486 t2m[t2m_count].el_no = edx;
487 t2m[t2m_count].el_tag = 0;
488 t2m_count++;
489 }
490 continue;
491 }
492 } else {
493 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
494 }
495 erval = elm->type->der_encoder(elm->type, memb_ptr,
496 elm->tag_mode, elm->tag,
497 0, 0);
498 if(erval.encoded == -1)
499 return erval;
500 computed_size += erval.encoded;
501
502 /*
503 * Remember the outmost tag of this member.
504 */
505 if(t2m_build_own) {
506 t2m[t2m_count].el_no = edx;
507 t2m[t2m_count].el_tag = asn1_TYPE_outmost_tag(
508 elm->type, memb_ptr, elm->tag_mode, elm->tag);
509 t2m_count++;
510 } else {
511 /*
512 * No dynamic sorting is necessary.
513 */
514 }
515 }
516
517 /*
518 * Finalize order of the components.
519 */
Lev Walkin449f8322004-08-20 13:23:42 +0000520 assert(t2m_count == td->elements_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000521 if(t2m_build_own) {
522 /*
523 * Sort the underlying members according to their
524 * canonical tags order. DER encoding mandates it.
525 */
526 qsort(t2m, t2m_count, sizeof(specs->tag2el[0]), _t2e_cmp);
527 } else {
528 /*
529 * Tags are already sorted by the compiler.
530 */
531 }
532
533 /*
534 * Encode the TLV for the sequence itself.
535 */
Lev Walkin449f8322004-08-20 13:23:42 +0000536 ret = der_write_tags(td, computed_size, tag_mode, tag, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +0000537 if(ret == -1) {
538 my_erval.encoded = -1;
Lev Walkin449f8322004-08-20 13:23:42 +0000539 my_erval.failed_type = td;
Lev Walkinf15320b2004-06-03 03:38:44 +0000540 my_erval.structure_ptr = ptr;
541 return my_erval;
542 }
543 my_erval.encoded = computed_size + ret;
544
545 if(!cb) return my_erval;
546
547 /*
548 * Encode all members.
549 */
Lev Walkin449f8322004-08-20 13:23:42 +0000550 for(edx = 0; edx < td->elements_count; edx++) {
551 asn1_TYPE_member_t *elm;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000552 asn_enc_rval_t erval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000553 void *memb_ptr;
554
555 /* Encode according to the tag order */
Lev Walkin449f8322004-08-20 13:23:42 +0000556 elm = &td->elements[t2m[edx].el_no];
Lev Walkinf15320b2004-06-03 03:38:44 +0000557
Lev Walkincc93b0f2004-09-10 09:18:20 +0000558 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000559 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
560 if(!memb_ptr) continue;
561 } else {
562 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
563 }
564 erval = elm->type->der_encoder(elm->type, memb_ptr,
565 elm->tag_mode, elm->tag,
566 cb, app_key);
567 if(erval.encoded == -1)
568 return erval;
569 computed_size -= erval.encoded;
570 }
571
572 if(computed_size != 0) {
573 /*
574 * Encoded size is not equal to the computed size.
575 */
576 my_erval.encoded = -1;
Lev Walkin449f8322004-08-20 13:23:42 +0000577 my_erval.failed_type = td;
Lev Walkinf15320b2004-06-03 03:38:44 +0000578 my_erval.structure_ptr = ptr;
579 }
580
581 return my_erval;
582}
583
Lev Walkina9cc46e2004-09-22 16:06:28 +0000584asn_enc_rval_t
585SET_encode_xer(asn1_TYPE_descriptor_t *td, void *sptr,
586 int ilevel, enum xer_encoder_flags_e flags,
587 asn_app_consume_bytes_f *cb, void *app_key) {
588 asn_enc_rval_t er;
589 int xcan = (flags & XER_F_CANONICAL);
590 int edx;
591
592 if(!sptr)
593 _ASN_ENCODE_FAILED;
594
595 er.encoded = 0;
596
597 for(edx = 0; edx < td->elements_count; edx++) {
598 asn_enc_rval_t tmper;
599 asn1_TYPE_member_t *elm = &td->elements[edx];
600 void *memb_ptr;
601 const char *mname = elm->name;
602 unsigned int mlen = strlen(elm->name);
603
604 if(elm->flags & ATF_POINTER) {
605 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
606 if(!memb_ptr) continue; /* OPTIONAL element? */
607 } else {
608 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
609 }
610
611 if(!xcan)
612 _i_ASN_TEXT_INDENT(1, ilevel);
613 _ASN_CALLBACK3("<", 1, mname, mlen, ">", 1);
614
615 /* Print the member itself */
616 tmper = elm->type->xer_encoder(elm->type, memb_ptr,
617 ilevel + 1, flags, cb, app_key);
618 if(tmper.encoded == -1) return tmper;
619
620 _ASN_CALLBACK3("</", 2, mname, mlen, ">", 1);
621
622 er.encoded += 5 + (2 * mlen) + tmper.encoded;
623 }
624
625 if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel - 1);
626
627 return er;
628}
629
Lev Walkinf15320b2004-06-03 03:38:44 +0000630int
631SET_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
632 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000633 int edx;
634 int ret;
635
636 if(!sptr) return cb("<absent>", 8, app_key);
637
638 /* Dump preamble */
639 if(cb(td->name, strlen(td->name), app_key)
640 || cb(" ::= {\n", 7, app_key))
641 return -1;
642
Lev Walkin449f8322004-08-20 13:23:42 +0000643 for(edx = 0; edx < td->elements_count; edx++) {
644 asn1_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000645 const void *memb_ptr;
646
Lev Walkincc93b0f2004-09-10 09:18:20 +0000647 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000648 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
649 if(!memb_ptr) continue;
650 } else {
651 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
652 }
653
654 /* Indentation */
655 for(ret = 0; ret < ilevel; ret++) cb(" ", 1, app_key);
656
657 /* Print the member's name and stuff */
658 if(cb(elm->name, strlen(elm->name), app_key)
659 || cb(": ", 2, app_key))
660 return -1;
661
662 /* Print the member itself */
663 ret = elm->type->print_struct(elm->type, memb_ptr, ilevel + 4,
664 cb, app_key);
665 if(ret) return ret;
666
667 ret = cb("\n", 1, app_key);
668 if(ret) return ret;
669 }
670
671 /* Indentation */
672 for(ret = 0; ret < ilevel - 4; ret++) cb(" ", 1, app_key);
673
674 return cb("}", 1, app_key);
675}
676
677void
678SET_free(asn1_TYPE_descriptor_t *td, void *ptr, int contents_only) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000679 int edx;
680
681 if(!td || !ptr)
682 return;
683
684 ASN_DEBUG("Freeing %s as SET", td->name);
685
Lev Walkin449f8322004-08-20 13:23:42 +0000686 for(edx = 0; edx < td->elements_count; edx++) {
687 asn1_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000688 void *memb_ptr;
Lev Walkincc93b0f2004-09-10 09:18:20 +0000689 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000690 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
691 if(memb_ptr)
692 elm->type->free_struct(elm->type, memb_ptr, 0);
693 } else {
694 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
695 elm->type->free_struct(elm->type, memb_ptr, 1);
696 }
697 }
698
699 if(!contents_only) {
700 FREEMEM(ptr);
701 }
702}
703
704int
705SET_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
706 asn_app_consume_bytes_f *app_errlog, void *app_key) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000707 int edx;
708
709 if(!sptr) {
Lev Walkinba4e5182004-08-11 09:44:13 +0000710 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkin16835b62004-08-22 13:47:59 +0000711 "%s: value not given (%s:%d)",
712 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000713 return -1;
714 }
715
716 /*
717 * Iterate over structure members and check their validity.
718 */
Lev Walkin449f8322004-08-20 13:23:42 +0000719 for(edx = 0; edx < td->elements_count; edx++) {
720 asn1_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000721 const void *memb_ptr;
722
Lev Walkincc93b0f2004-09-10 09:18:20 +0000723 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000724 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
725 if(!memb_ptr) {
Lev Walkincc93b0f2004-09-10 09:18:20 +0000726 if(elm->optional)
727 continue;
728 _ASN_ERRLOG(app_errlog, app_key,
729 "%s: mandatory element %s absent (%s:%d)",
730 td->name, elm->name, __FILE__, __LINE__);
731 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000732 }
733 } else {
734 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
735 }
736
Lev Walkin449f8322004-08-20 13:23:42 +0000737 if(elm->memb_constraints) {
738 int ret = elm->memb_constraints(elm->type, memb_ptr,
739 app_errlog, app_key);
740 if(ret) return ret;
741 } else {
742 int ret = elm->type->check_constraints(elm->type,
743 memb_ptr, app_errlog, app_key);
744 if(ret) return ret;
745 /*
746 * Cannot inherit it earlier:
747 * need to make sure we get the updated version.
748 */
749 elm->memb_constraints = elm->type->check_constraints;
750 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000751 }
752
753 return 0;
754}