blob: 9b6012ff3995697638c43e6065707096fce3ad96 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
Lev Walkinadf863f2006-09-05 16:18:34 +00002 * Copyright (c) 2003, 2004, 2005, 2006 Lev Walkin <vlm@lionet.info>.
Lev Walkinac589332005-08-22 14:19:28 +00003 * All rights reserved.
Lev Walkinf15320b2004-06-03 03:38:44 +00004 * Redistribution and modifications are permitted subject to BSD license.
5 */
Lev Walkina9cc46e2004-09-22 16:06:28 +00006#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00007#include <constr_SET.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00008
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 Walkin6c0df202005-02-14 19:03:17 +000015/* Check that all the mandatory members are present */
16static int _SET_is_populated(asn_TYPE_descriptor_t *td, void *st);
17
Lev Walkinf15320b2004-06-03 03:38:44 +000018/*
19 * Number of bytes left for this structure.
20 * (ctx->left) indicates the number of bytes _transferred_ for the structure.
21 * (size) contains the number of bytes in the buffer passed.
22 */
Lev Walkinec1ffd42004-08-18 04:53:32 +000023#define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left)
Lev Walkinf15320b2004-06-03 03:38:44 +000024
25/*
26 * If the subprocessor function returns with an indication that it wants
27 * more data, it may well be a fatal decoding problem, because the
28 * size is constrained by the <TLV>'s L, even if the buffer size allows
29 * reading more data.
30 * For example, consider the buffer containing the following TLVs:
31 * <T:5><L:1><V> <T:6>...
32 * The TLV length clearly indicates that one byte is expected in V, but
33 * if the V processor returns with "want more data" even if the buffer
34 * contains way more data than the V processor have seen.
35 */
Lev Walkind9bd7752004-06-05 08:17:50 +000036#define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size)
Lev Walkinf15320b2004-06-03 03:38:44 +000037
38/*
39 * This macro "eats" the part of the buffer which is definitely "consumed",
40 * i.e. was correctly converted into local representation or rightfully skipped.
41 */
Lev Walkincc6a9102004-09-23 22:06:26 +000042#undef ADVANCE
Lev Walkinf15320b2004-06-03 03:38:44 +000043#define ADVANCE(num_bytes) do { \
44 size_t num = num_bytes; \
Lev Walkin8c3b8542005-03-10 18:52:02 +000045 ptr = ((const char *)ptr) + num;\
Lev Walkinf15320b2004-06-03 03:38:44 +000046 size -= num; \
47 if(ctx->left >= 0) \
48 ctx->left -= num; \
49 consumed_myself += num; \
50 } while(0)
51
52/*
53 * Switch to the next phase of parsing.
54 */
Lev Walkincc6a9102004-09-23 22:06:26 +000055#undef NEXT_PHASE
Lev Walkinf15320b2004-06-03 03:38:44 +000056#define NEXT_PHASE(ctx) do { \
57 ctx->phase++; \
58 ctx->step = 0; \
59 } while(0)
60
61/*
62 * Return a standardized complex structure.
63 */
Lev Walkincc6a9102004-09-23 22:06:26 +000064#undef RETURN
Lev Walkinf15320b2004-06-03 03:38:44 +000065#define RETURN(_code) do { \
66 rval.code = _code; \
67 rval.consumed = consumed_myself;\
68 return rval; \
69 } while(0)
70
71/*
72 * Tags are canonically sorted in the tag2element map.
73 */
74static int
75_t2e_cmp(const void *ap, const void *bp) {
Lev Walkin5e033762004-09-29 13:26:15 +000076 const asn_TYPE_tag2member_t *a = (const asn_TYPE_tag2member_t *)ap;
77 const asn_TYPE_tag2member_t *b = (const asn_TYPE_tag2member_t *)bp;
Lev Walkinc2346572004-08-11 09:07:36 +000078
Lev Walkinf15320b2004-06-03 03:38:44 +000079 int a_class = BER_TAG_CLASS(a->el_tag);
80 int b_class = BER_TAG_CLASS(b->el_tag);
81
82 if(a_class == b_class) {
83 ber_tlv_tag_t a_value = BER_TAG_VALUE(a->el_tag);
84 ber_tlv_tag_t b_value = BER_TAG_VALUE(b->el_tag);
85
86 if(a_value == b_value)
87 return 0;
88 else if(a_value < b_value)
89 return -1;
90 else
91 return 1;
92 } else if(a_class < b_class) {
93 return -1;
94 } else {
95 return 1;
96 }
97}
98
99/*
100 * The decoder of the SET type.
101 */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000102asn_dec_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000103SET_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000104 void **struct_ptr, const void *ptr, size_t size, int tag_mode) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000105 /*
106 * Bring closer parts of structure description.
107 */
Lev Walkin5e033762004-09-29 13:26:15 +0000108 asn_SET_specifics_t *specs = (asn_SET_specifics_t *)td->specifics;
109 asn_TYPE_member_t *elements = td->elements;
Lev Walkinf15320b2004-06-03 03:38:44 +0000110
111 /*
112 * Parts of the structure being constructed.
113 */
114 void *st = *struct_ptr; /* Target structure. */
Lev Walkin5e033762004-09-29 13:26:15 +0000115 asn_struct_ctx_t *ctx; /* Decoder context */
Lev Walkinf15320b2004-06-03 03:38:44 +0000116
117 ber_tlv_tag_t tlv_tag; /* T from TLV */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000118 asn_dec_rval_t rval; /* Return code from subparsers */
Lev Walkinf15320b2004-06-03 03:38:44 +0000119
120 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
121 int edx; /* SET element's index */
122
Lev Walkin449f8322004-08-20 13:23:42 +0000123 ASN_DEBUG("Decoding %s as SET", td->name);
Lev Walkinadf863f2006-09-05 16:18:34 +0000124
125 if(_ASN_STACK_OVERFLOW_CHECK(opt_codec_ctx))
126 _ASN_DECODE_FAILED;
127
Lev Walkinf15320b2004-06-03 03:38:44 +0000128 /*
129 * Create the target structure if it is not present already.
130 */
131 if(st == 0) {
132 st = *struct_ptr = CALLOC(1, specs->struct_size);
133 if(st == 0) {
134 RETURN(RC_FAIL);
135 }
136 }
137
138 /*
139 * Restore parsing context.
140 */
Lev Walkin5e033762004-09-29 13:26:15 +0000141 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000142
143 /*
144 * Start to parse where left previously
145 */
146 switch(ctx->phase) {
147 case 0:
148 /*
149 * PHASE 0.
150 * Check that the set of tags associated with given structure
151 * perfectly fits our expectations.
152 */
153
Lev Walkin5e033762004-09-29 13:26:15 +0000154 rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size,
Lev Walkin8e8078a2004-09-26 13:10:40 +0000155 tag_mode, 1, &ctx->left, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000156 if(rval.code != RC_OK) {
157 ASN_DEBUG("%s tagging check failed: %d",
Lev Walkin449f8322004-08-20 13:23:42 +0000158 td->name, rval.code);
Lev Walkin4ab42cd2004-10-05 06:36:44 +0000159 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000160 }
161
162 if(ctx->left >= 0)
163 ctx->left += rval.consumed; /* ?Substracted below! */
164 ADVANCE(rval.consumed);
165
166 NEXT_PHASE(ctx);
167
168 ASN_DEBUG("Structure advertised %ld bytes, "
169 "buffer contains %ld", (long)ctx->left, (long)size);
170
171 /* Fall through */
172 case 1:
173 /*
174 * PHASE 1.
175 * From the place where we've left it previously,
176 * try to decode the next member from the list of
177 * this structure's elements.
Lev Walkin3e478ed2004-10-28 13:04:02 +0000178 * Note that elements in BER may arrive out of
Lev Walkinf15320b2004-06-03 03:38:44 +0000179 * 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 Walkin3e478ed2004-10-28 13:04:02 +0000183 for(;; ctx->step = 0) {
184 asn_TYPE_tag2member_t *t2m;
185 asn_TYPE_tag2member_t key;
Lev Walkinf15320b2004-06-03 03:38:44 +0000186 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
Lev Walkin3e478ed2004-10-28 13:04:02 +0000190 if(ctx->step & 1) {
191 edx = ctx->step >> 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000192 goto microphase2;
Lev Walkin3e478ed2004-10-28 13:04:02 +0000193 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000194
195 /*
196 * MICROPHASE 1: Synchronize decoding.
197 */
198
199 if(ctx->left == 0)
200 /*
201 * No more things to decode.
202 * Exit out of here and check whether all mandatory
203 * elements have been received (in the next phase).
204 */
205 break;
206
207 /*
208 * Fetch the T from TLV.
209 */
210 tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
211 switch(tag_len) {
212 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
213 /* Fall through */
214 case -1: RETURN(RC_FAIL);
215 }
216
Lev Walkin8c3b8542005-03-10 18:52:02 +0000217 if(ctx->left < 0 && ((const uint8_t *)ptr)[0] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000218 if(LEFT < 2) {
219 if(SIZE_VIOLATION)
220 RETURN(RC_FAIL);
221 else
222 RETURN(RC_WMORE);
Lev Walkin8c3b8542005-03-10 18:52:02 +0000223 } else if(((const uint8_t *)ptr)[1] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000224 /*
225 * Found the terminator of the
226 * indefinite length structure.
227 * Invoke the generic finalization function.
228 */
229 goto phase3;
230 }
231 }
232
Lev Walkin3e478ed2004-10-28 13:04:02 +0000233 key.el_tag = tlv_tag;
Lev Walkinc17d90f2005-01-17 14:32:45 +0000234 t2m = (asn_TYPE_tag2member_t *)bsearch(&key,
Lev Walkin3e478ed2004-10-28 13:04:02 +0000235 specs->tag2el, specs->tag2el_count,
236 sizeof(specs->tag2el[0]), _t2e_cmp);
237 if(t2m) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000238 /*
Lev Walkin3e478ed2004-10-28 13:04:02 +0000239 * Found the element corresponding to the tag.
Lev Walkinf15320b2004-06-03 03:38:44 +0000240 */
Lev Walkin3e478ed2004-10-28 13:04:02 +0000241 edx = t2m->el_no;
242 ctx->step = (edx << 1) + 1;
243 ASN_DEBUG("Got tag %s (%s), edx %d",
244 ber_tlv_tag_string(tlv_tag), td->name, edx);
245 } else if(specs->extensible == 0) {
246 ASN_DEBUG("Unexpected tag %s "
247 "in non-extensible SET %s",
248 ber_tlv_tag_string(tlv_tag), td->name);
249 RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000250 } else {
Lev Walkin3e478ed2004-10-28 13:04:02 +0000251 /* Skip this tag */
252 ssize_t skip;
Lev Walkinf15320b2004-06-03 03:38:44 +0000253
Lev Walkin3e478ed2004-10-28 13:04:02 +0000254 ASN_DEBUG("Skipping unknown tag %s",
255 ber_tlv_tag_string(tlv_tag));
Lev Walkinf15320b2004-06-03 03:38:44 +0000256
Lev Walkin3e478ed2004-10-28 13:04:02 +0000257 skip = ber_skip_length(opt_codec_ctx,
258 BER_TLV_CONSTRUCTED(ptr),
Lev Walkin8c3b8542005-03-10 18:52:02 +0000259 (const char *)ptr + tag_len, LEFT - tag_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000260
Lev Walkin3e478ed2004-10-28 13:04:02 +0000261 switch(skip) {
262 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
263 /* Fall through */
264 case -1: RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000265 }
Lev Walkin3e478ed2004-10-28 13:04:02 +0000266
267 ADVANCE(skip + tag_len);
268 continue; /* Try again with the next tag */
Lev Walkinf15320b2004-06-03 03:38:44 +0000269 }
270
271 /*
272 * MICROPHASE 2: Invoke the member-specific decoder.
273 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000274 microphase2:
275
276 /*
277 * Check for duplications: must not overwrite
278 * already decoded elements.
279 */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000280 if(ASN_SET_ISPRESENT2((char *)st + specs->pres_offset, edx)) {
Lev Walkinbbe04752004-07-01 00:47:16 +0000281 ASN_DEBUG("SET %s: Duplicate element %s (%d)",
Lev Walkin449f8322004-08-20 13:23:42 +0000282 td->name, elements[edx].name, edx);
Lev Walkinf15320b2004-06-03 03:38:44 +0000283 RETURN(RC_FAIL);
284 }
285
286 /*
287 * Compute the position of the member inside a structure,
288 * and also a type of containment (it may be contained
289 * as pointer or using inline inclusion).
290 */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000291 if(elements[edx].flags & ATF_POINTER) {
292 /* Member is a pointer to another structure */
Lev Walkinc2346572004-08-11 09:07:36 +0000293 memb_ptr2 = (void **)((char *)st + elements[edx].memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000294 } else {
295 /*
296 * A pointer to a pointer
297 * holding the start of the structure
298 */
299 memb_ptr = (char *)st + elements[edx].memb_offset;
300 memb_ptr2 = &memb_ptr;
301 }
302 /*
303 * Invoke the member fetch routine according to member's type
304 */
Lev Walkin5e033762004-09-29 13:26:15 +0000305 rval = elements[edx].type->ber_decoder(opt_codec_ctx,
Lev Walkinc2346572004-08-11 09:07:36 +0000306 elements[edx].type,
Lev Walkinf15320b2004-06-03 03:38:44 +0000307 memb_ptr2, ptr, LEFT,
308 elements[edx].tag_mode);
309 switch(rval.code) {
310 case RC_OK:
Lev Walkin4d9528c2004-08-11 08:10:13 +0000311 ASN_SET_MKPRESENT((char *)st + specs->pres_offset, edx);
Lev Walkinf15320b2004-06-03 03:38:44 +0000312 break;
313 case RC_WMORE: /* More data expected */
314 if(!SIZE_VIOLATION) {
315 ADVANCE(rval.consumed);
316 RETURN(RC_WMORE);
317 }
318 /* Fail through */
319 case RC_FAIL: /* Fatal error */
320 RETURN(RC_FAIL);
321 } /* switch(rval) */
322
323 ADVANCE(rval.consumed);
324 } /* for(all structure members) */
325
326 phase3:
327 ctx->phase = 3;
328 /* Fall through */
329 case 3:
330 case 4: /* Only 00 is expected */
331 ASN_DEBUG("SET %s Leftover: %ld, size = %ld",
Lev Walkin449f8322004-08-20 13:23:42 +0000332 td->name, (long)ctx->left, (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000333
334 /*
335 * Skip everything until the end of the SET.
336 */
337 while(ctx->left) {
338 ssize_t tl, ll;
339
340 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
341 switch(tl) {
342 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
343 /* Fall through */
344 case -1: RETURN(RC_FAIL);
345 }
346
347 /*
348 * If expected <0><0>...
349 */
350 if(ctx->left < 0
Lev Walkin8c3b8542005-03-10 18:52:02 +0000351 && ((const uint8_t *)ptr)[0] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000352 if(LEFT < 2) {
353 if(SIZE_VIOLATION)
354 RETURN(RC_FAIL);
355 else
356 RETURN(RC_WMORE);
Lev Walkin8c3b8542005-03-10 18:52:02 +0000357 } else if(((const uint8_t *)ptr)[1] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000358 /*
359 * Correctly finished with <0><0>.
360 */
361 ADVANCE(2);
362 ctx->left++;
363 ctx->phase = 4;
364 continue;
365 }
366 }
367
368 if(specs->extensible == 0 || ctx->phase == 4) {
369 ASN_DEBUG("Unexpected continuation "
Lev Walkin3e478ed2004-10-28 13:04:02 +0000370 "of a non-extensible type %s "
371 "(ptr=%02x)",
Lev Walkin8c3b8542005-03-10 18:52:02 +0000372 td->name, *(const uint8_t *)ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +0000373 RETURN(RC_FAIL);
374 }
375
Lev Walkinbaaa24f2004-09-29 14:19:14 +0000376 ll = ber_skip_length(opt_codec_ctx,
Lev Walkinf15320b2004-06-03 03:38:44 +0000377 BER_TLV_CONSTRUCTED(ptr),
Lev Walkin8c3b8542005-03-10 18:52:02 +0000378 (const char *)ptr + tl, LEFT - tl);
Lev Walkinf15320b2004-06-03 03:38:44 +0000379 switch(ll) {
380 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
381 /* Fall through */
382 case -1: RETURN(RC_FAIL);
383 }
384
385 ADVANCE(tl + ll);
386 }
387
388 ctx->phase = 5;
389 case 5:
Lev Walkin6c0df202005-02-14 19:03:17 +0000390 /* Check that all mandatory elements are present. */
391 if(!_SET_is_populated(td, st))
392 RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000393
394 NEXT_PHASE(ctx);
395 }
396
397 RETURN(RC_OK);
398}
399
Lev Walkin6c0df202005-02-14 19:03:17 +0000400static int
401_SET_is_populated(asn_TYPE_descriptor_t *td, void *st) {
402 asn_SET_specifics_t *specs = (asn_SET_specifics_t *)td->specifics;
403 int edx;
404
405 /*
406 * Check that all mandatory elements are present.
407 */
408 for(edx = 0; edx < td->elements_count;
409 edx += (8 * sizeof(specs->_mandatory_elements[0]))) {
410 unsigned int midx, pres, must;
411
412 midx = edx/(8 * sizeof(specs->_mandatory_elements[0]));
413 pres = ((unsigned int *)((char *)st+specs->pres_offset))[midx];
414 must = ntohl(specs->_mandatory_elements[midx]);
415
416 if((pres & must) == must) {
417 /*
418 * Yes, everything seems to be in place.
419 */
420 } else {
421 ASN_DEBUG("One or more mandatory elements "
422 "of a SET %s %d (%08x.%08x)=%08x "
423 "are not present",
424 td->name,
425 midx,
426 pres,
427 must,
428 (~(pres & must) & must)
429 );
430 return 0;
431 }
432 }
433
434 return 1;
435}
436
Lev Walkinf15320b2004-06-03 03:38:44 +0000437/*
438 * The DER encoder of the SET type.
439 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000440asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000441SET_encode_der(asn_TYPE_descriptor_t *td,
Lev Walkind5193802004-10-03 09:12:07 +0000442 void *sptr, int tag_mode, ber_tlv_tag_t tag,
Lev Walkinf15320b2004-06-03 03:38:44 +0000443 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin5e033762004-09-29 13:26:15 +0000444 asn_SET_specifics_t *specs = (asn_SET_specifics_t *)td->specifics;
Lev Walkinf15320b2004-06-03 03:38:44 +0000445 size_t computed_size = 0;
Lev Walkind5193802004-10-03 09:12:07 +0000446 asn_enc_rval_t er;
Lev Walkin449f8322004-08-20 13:23:42 +0000447 int t2m_build_own = (specs->tag2el_count != td->elements_count);
Lev Walkin5e033762004-09-29 13:26:15 +0000448 asn_TYPE_tag2member_t *t2m;
Lev Walkinf15320b2004-06-03 03:38:44 +0000449 int t2m_count;
450 ssize_t ret;
451 int edx;
452
453 /*
454 * Use existing, or build our own tags map.
455 */
456 if(t2m_build_own) {
Lev Walkinc17d90f2005-01-17 14:32:45 +0000457 t2m = (asn_TYPE_tag2member_t *)alloca(
458 td->elements_count * sizeof(t2m[0]));
Lev Walkind5193802004-10-03 09:12:07 +0000459 if(!t2m) _ASN_ENCODE_FAILED; /* There are such platforms */
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 Walkind5193802004-10-03 09:12:07 +0000475 asn_enc_rval_t tmper;
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 Walkind5193802004-10-03 09:12:07 +0000482 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000483 if(!memb_ptr) {
Lev Walkinac589332005-08-22 14:19:28 +0000484 if(!elm->optional)
485 /* Mandatory elements missing */
486 _ASN_ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000487 if(t2m_build_own) {
488 t2m[t2m_count].el_no = edx;
489 t2m[t2m_count].el_tag = 0;
490 t2m_count++;
491 }
492 continue;
493 }
494 } else {
Lev Walkind5193802004-10-03 09:12:07 +0000495 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000496 }
Lev Walkind5193802004-10-03 09:12:07 +0000497 tmper = elm->type->der_encoder(elm->type, memb_ptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000498 elm->tag_mode, elm->tag,
499 0, 0);
Lev Walkind5193802004-10-03 09:12:07 +0000500 if(tmper.encoded == -1)
501 return tmper;
502 computed_size += tmper.encoded;
Lev Walkinf15320b2004-06-03 03:38:44 +0000503
504 /*
505 * Remember the outmost tag of this member.
506 */
507 if(t2m_build_own) {
508 t2m[t2m_count].el_no = edx;
Lev Walkin5e033762004-09-29 13:26:15 +0000509 t2m[t2m_count].el_tag = asn_TYPE_outmost_tag(
Lev Walkinf15320b2004-06-03 03:38:44 +0000510 elm->type, memb_ptr, elm->tag_mode, elm->tag);
511 t2m_count++;
512 } else {
513 /*
514 * No dynamic sorting is necessary.
515 */
516 }
517 }
518
519 /*
520 * Finalize order of the components.
521 */
Lev Walkin449f8322004-08-20 13:23:42 +0000522 assert(t2m_count == td->elements_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000523 if(t2m_build_own) {
524 /*
525 * Sort the underlying members according to their
526 * canonical tags order. DER encoding mandates it.
527 */
528 qsort(t2m, t2m_count, sizeof(specs->tag2el[0]), _t2e_cmp);
529 } else {
530 /*
531 * Tags are already sorted by the compiler.
532 */
533 }
534
535 /*
536 * Encode the TLV for the sequence itself.
537 */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000538 ret = der_write_tags(td, computed_size, tag_mode, 1, tag, cb, app_key);
Lev Walkind5193802004-10-03 09:12:07 +0000539 if(ret == -1) _ASN_ENCODE_FAILED;
540 er.encoded = computed_size + ret;
Lev Walkinf15320b2004-06-03 03:38:44 +0000541
Lev Walkin59b176e2005-11-26 11:25:14 +0000542 if(!cb) _ASN_ENCODED_OK(er);
Lev Walkinf15320b2004-06-03 03:38:44 +0000543
544 /*
545 * Encode all members.
546 */
Lev Walkin449f8322004-08-20 13:23:42 +0000547 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000548 asn_TYPE_member_t *elm;
Lev Walkind5193802004-10-03 09:12:07 +0000549 asn_enc_rval_t tmper;
Lev Walkinf15320b2004-06-03 03:38:44 +0000550 void *memb_ptr;
551
552 /* Encode according to the tag order */
Lev Walkin449f8322004-08-20 13:23:42 +0000553 elm = &td->elements[t2m[edx].el_no];
Lev Walkinf15320b2004-06-03 03:38:44 +0000554
Lev Walkincc93b0f2004-09-10 09:18:20 +0000555 if(elm->flags & ATF_POINTER) {
Lev Walkind5193802004-10-03 09:12:07 +0000556 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000557 if(!memb_ptr) continue;
558 } else {
Lev Walkind5193802004-10-03 09:12:07 +0000559 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000560 }
Lev Walkind5193802004-10-03 09:12:07 +0000561 tmper = elm->type->der_encoder(elm->type, memb_ptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000562 elm->tag_mode, elm->tag,
563 cb, app_key);
Lev Walkind5193802004-10-03 09:12:07 +0000564 if(tmper.encoded == -1)
565 return tmper;
566 computed_size -= tmper.encoded;
Lev Walkinf15320b2004-06-03 03:38:44 +0000567 }
568
569 if(computed_size != 0) {
570 /*
571 * Encoded size is not equal to the computed size.
572 */
Lev Walkind5193802004-10-03 09:12:07 +0000573 _ASN_ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000574 }
575
Lev Walkin59b176e2005-11-26 11:25:14 +0000576 _ASN_ENCODED_OK(er);
Lev Walkinf15320b2004-06-03 03:38:44 +0000577}
578
Lev Walkin6c0df202005-02-14 19:03:17 +0000579#undef XER_ADVANCE
580#define XER_ADVANCE(num_bytes) do { \
581 size_t num = num_bytes; \
Lev Walkin8c3b8542005-03-10 18:52:02 +0000582 buf_ptr = ((const char *)buf_ptr) + num;\
Lev Walkin6c0df202005-02-14 19:03:17 +0000583 size -= num; \
584 consumed_myself += num; \
585 } while(0)
586
587/*
588 * Decode the XER (XML) data.
589 */
590asn_dec_rval_t
591SET_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
592 void **struct_ptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000593 const void *buf_ptr, size_t size) {
Lev Walkin6c0df202005-02-14 19:03:17 +0000594 /*
595 * Bring closer parts of structure description.
596 */
597 asn_SET_specifics_t *specs = (asn_SET_specifics_t *)td->specifics;
598 asn_TYPE_member_t *elements = td->elements;
599 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
600
601 /*
602 * ... and parts of the structure being constructed.
603 */
604 void *st = *struct_ptr; /* Target structure. */
605 asn_struct_ctx_t *ctx; /* Decoder context */
606
607 asn_dec_rval_t rval; /* Return value from a decoder */
608 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
Lev Walkin6c0df202005-02-14 19:03:17 +0000609 int edx; /* Element index */
610
611 /*
612 * Create the target structure if it is not present already.
613 */
614 if(st == 0) {
615 st = *struct_ptr = CALLOC(1, specs->struct_size);
616 if(st == 0) RETURN(RC_FAIL);
617 }
618
619 /*
620 * Restore parsing context.
621 */
622 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
623
624 /*
625 * Phases of XER/XML processing:
626 * Phase 0: Check that the opening tag matches our expectations.
627 * Phase 1: Processing body and reacting on closing tag.
628 * Phase 2: Processing inner type.
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000629 * Phase 3: Skipping unknown extensions.
630 * Phase 4: PHASED OUT
Lev Walkin6c0df202005-02-14 19:03:17 +0000631 */
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000632 for(edx = ctx->step; ctx->phase <= 3;) {
Lev Walkin6c0df202005-02-14 19:03:17 +0000633 pxer_chunk_type_e ch_type; /* XER chunk type */
634 ssize_t ch_size; /* Chunk size */
635 xer_check_tag_e tcv; /* Tag check value */
636 asn_TYPE_member_t *elm;
637
638 /*
639 * Go inside the inner member of a set.
640 */
641 if(ctx->phase == 2) {
642 asn_dec_rval_t tmprval;
643 void *memb_ptr; /* Pointer to the member */
644 void **memb_ptr2; /* Pointer to that pointer */
645
646 if(ASN_SET_ISPRESENT2((char *)st + specs->pres_offset,
647 edx)) {
648 ASN_DEBUG("SET %s: Duplicate element %s (%d)",
649 td->name, elements[edx].name, edx);
650 RETURN(RC_FAIL);
651 }
652
Lev Walkin8bb4a952005-02-14 20:15:40 +0000653 elm = &elements[edx];
Lev Walkin6c0df202005-02-14 19:03:17 +0000654
655 if(elm->flags & ATF_POINTER) {
656 /* Member is a pointer to another structure */
657 memb_ptr2 = (void **)((char *)st
658 + elm->memb_offset);
659 } else {
660 memb_ptr = (char *)st + elm->memb_offset;
661 memb_ptr2 = &memb_ptr;
662 }
663
664 /* Invoke the inner type decoder, m.b. multiple times */
665 tmprval = elm->type->xer_decoder(opt_codec_ctx,
666 elm->type, memb_ptr2, elm->name,
667 buf_ptr, size);
668 XER_ADVANCE(tmprval.consumed);
669 if(tmprval.code != RC_OK)
670 RETURN(tmprval.code);
671 ctx->phase = 1; /* Back to body processing */
Lev Walkin6c0df202005-02-14 19:03:17 +0000672 ASN_SET_MKPRESENT((char *)st + specs->pres_offset, edx);
673 ASN_DEBUG("XER/SET phase => %d", ctx->phase);
674 /* Fall through */
675 }
676
677 /*
678 * Get the next part of the XML stream.
679 */
Lev Walkin1e443962005-02-18 18:06:36 +0000680 ch_size = xer_next_token(&ctx->context,
681 buf_ptr, size, &ch_type);
Lev Walkin6c0df202005-02-14 19:03:17 +0000682 switch(ch_size) {
683 case -1: RETURN(RC_FAIL);
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000684 case 0: RETURN(RC_WMORE);
Lev Walkin6c0df202005-02-14 19:03:17 +0000685 default:
686 switch(ch_type) {
687 case PXER_COMMENT: /* Got XML comment */
688 case PXER_TEXT: /* Ignore free-standing text */
689 XER_ADVANCE(ch_size); /* Skip silently */
690 continue;
691 case PXER_TAG:
692 break; /* Check the rest down there */
693 }
694 }
695
696 tcv = xer_check_tag(buf_ptr, ch_size, xml_tag);
697 ASN_DEBUG("XER/SET: tcv = %d, ph=%d", tcv, ctx->phase);
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000698
699 /* Skip the extensions section */
700 if(ctx->phase == 3) {
701 switch(xer_skip_unknown(tcv, &ctx->left)) {
702 case -1:
703 ctx->phase = 4;
704 RETURN(RC_FAIL);
Lev Walkin1e443962005-02-18 18:06:36 +0000705 case 1:
706 ctx->phase = 1;
707 /* Fall through */
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000708 case 0:
709 XER_ADVANCE(ch_size);
710 continue;
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000711 case 2:
712 ctx->phase = 1;
713 break;
714 }
715 }
716
Lev Walkin6c0df202005-02-14 19:03:17 +0000717 switch(tcv) {
718 case XCT_CLOSING:
719 if(ctx->phase == 0) break;
720 ctx->phase = 0;
721 /* Fall through */
722 case XCT_BOTH:
723 if(ctx->phase == 0) {
724 if(_SET_is_populated(td, st)) {
725 XER_ADVANCE(ch_size);
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000726 ctx->phase = 4; /* Phase out */
Lev Walkin6c0df202005-02-14 19:03:17 +0000727 RETURN(RC_OK);
728 } else {
729 ASN_DEBUG("Premature end of XER SET");
730 RETURN(RC_FAIL);
731 }
732 }
733 /* Fall through */
734 case XCT_OPENING:
735 if(ctx->phase == 0) {
736 XER_ADVANCE(ch_size);
737 ctx->phase = 1; /* Processing body phase */
738 continue;
739 }
740 /* Fall through */
Lev Walkin904e65b2005-02-18 14:23:48 +0000741 case XCT_UNKNOWN_OP:
742 case XCT_UNKNOWN_BO:
Lev Walkin6c0df202005-02-14 19:03:17 +0000743
744 ASN_DEBUG("XER/SET: tcv=%d, ph=%d", tcv, ctx->phase);
745 if(ctx->phase != 1)
746 break; /* Really unexpected */
747
748 /*
749 * Search which member corresponds to this tag.
750 */
751 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000752 switch(xer_check_tag(buf_ptr, ch_size,
753 elements[edx].name)) {
Lev Walkin6c0df202005-02-14 19:03:17 +0000754 case XCT_BOTH:
755 case XCT_OPENING:
756 /*
757 * Process this member.
758 */
759 ctx->step = edx;
760 ctx->phase = 2;
761 break;
Lev Walkin904e65b2005-02-18 14:23:48 +0000762 case XCT_UNKNOWN_OP:
763 case XCT_UNKNOWN_BO:
Lev Walkin6c0df202005-02-14 19:03:17 +0000764 continue;
Lev Walkin6c0df202005-02-14 19:03:17 +0000765 default:
766 edx = td->elements_count;
767 break; /* Phase out */
768 }
769 break;
770 }
771 if(edx != td->elements_count)
772 continue;
Lev Walkin904e65b2005-02-18 14:23:48 +0000773
774 /* It is expected extension */
775 if(specs->extensible) {
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000776 ASN_DEBUG("Got anticipated extension");
Lev Walkin904e65b2005-02-18 14:23:48 +0000777 /*
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000778 * Check for (XCT_BOTH or XCT_UNKNOWN_BO)
779 * By using a mask. Only record a pure
780 * <opening> tags.
Lev Walkin904e65b2005-02-18 14:23:48 +0000781 */
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000782 if(tcv & XCT_CLOSING) {
783 /* Found </extension> without body */
784 } else {
785 ctx->left = 1;
786 ctx->phase = 3; /* Skip ...'s */
787 }
788 XER_ADVANCE(ch_size);
789 continue;
Lev Walkin904e65b2005-02-18 14:23:48 +0000790 }
791
Lev Walkin6c0df202005-02-14 19:03:17 +0000792 /* Fall through */
793 default:
794 break;
795 }
796
Lev Walkine0b56e02005-02-25 12:10:27 +0000797 ASN_DEBUG("Unexpected XML tag in SET, expected \"%s\"",
798 xml_tag);
Lev Walkin6c0df202005-02-14 19:03:17 +0000799 break;
800 }
801
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000802 ctx->phase = 4; /* "Phase out" on hard failure */
Lev Walkin6c0df202005-02-14 19:03:17 +0000803 RETURN(RC_FAIL);
804}
805
Lev Walkina9cc46e2004-09-22 16:06:28 +0000806asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000807SET_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000808 int ilevel, enum xer_encoder_flags_e flags,
809 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkind5193802004-10-03 09:12:07 +0000810 asn_SET_specifics_t *specs = (asn_SET_specifics_t *)td->specifics;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000811 asn_enc_rval_t er;
812 int xcan = (flags & XER_F_CANONICAL);
Lev Walkind5193802004-10-03 09:12:07 +0000813 asn_TYPE_tag2member_t *t2m = specs->tag2el_cxer;
814 int t2m_count = specs->tag2el_cxer_count;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000815 int edx;
816
817 if(!sptr)
818 _ASN_ENCODE_FAILED;
819
Lev Walkind5193802004-10-03 09:12:07 +0000820 assert(t2m_count == td->elements_count);
821
Lev Walkina9cc46e2004-09-22 16:06:28 +0000822 er.encoded = 0;
823
Lev Walkind5193802004-10-03 09:12:07 +0000824 for(edx = 0; edx < t2m_count; edx++) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000825 asn_enc_rval_t tmper;
Lev Walkind5193802004-10-03 09:12:07 +0000826 asn_TYPE_member_t *elm;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000827 void *memb_ptr;
Lev Walkind5193802004-10-03 09:12:07 +0000828 const char *mname;
829 unsigned int mlen;
830
831 elm = &td->elements[t2m[edx].el_no];
832 mname = elm->name;
833 mlen = strlen(elm->name);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000834
835 if(elm->flags & ATF_POINTER) {
836 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
Lev Walkinac589332005-08-22 14:19:28 +0000837 if(!memb_ptr) {
838 if(elm->optional)
839 continue;
840 /* Mandatory element missing */
841 _ASN_ENCODE_FAILED;
842 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000843 } else {
844 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
845 }
846
847 if(!xcan)
848 _i_ASN_TEXT_INDENT(1, ilevel);
849 _ASN_CALLBACK3("<", 1, mname, mlen, ">", 1);
850
851 /* Print the member itself */
852 tmper = elm->type->xer_encoder(elm->type, memb_ptr,
853 ilevel + 1, flags, cb, app_key);
854 if(tmper.encoded == -1) return tmper;
855
856 _ASN_CALLBACK3("</", 2, mname, mlen, ">", 1);
857
858 er.encoded += 5 + (2 * mlen) + tmper.encoded;
859 }
860
861 if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel - 1);
862
Lev Walkin59b176e2005-11-26 11:25:14 +0000863 _ASN_ENCODED_OK(er);
Lev Walkind5193802004-10-03 09:12:07 +0000864cb_failed:
865 _ASN_ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000866}
867
Lev Walkinf15320b2004-06-03 03:38:44 +0000868int
Lev Walkin5e033762004-09-29 13:26:15 +0000869SET_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000870 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000871 int edx;
872 int ret;
873
Lev Walkin8e8078a2004-09-26 13:10:40 +0000874 if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000875
876 /* Dump preamble */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000877 if(cb(td->name, strlen(td->name), app_key) < 0
878 || cb(" ::= {", 6, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000879 return -1;
880
Lev Walkin449f8322004-08-20 13:23:42 +0000881 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000882 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000883 const void *memb_ptr;
884
Lev Walkincc93b0f2004-09-10 09:18:20 +0000885 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000886 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkinac589332005-08-22 14:19:28 +0000887 if(!memb_ptr) {
888 if(elm->optional) continue;
889 /* Print <absent> line */
890 /* Fall through */
891 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000892 } else {
893 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
894 }
895
Lev Walkin8e8078a2004-09-26 13:10:40 +0000896 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000897
898 /* Print the member's name and stuff */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000899 if(cb(elm->name, strlen(elm->name), app_key) < 0
900 || cb(": ", 2, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000901 return -1;
902
903 /* Print the member itself */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000904 ret = elm->type->print_struct(elm->type, memb_ptr, ilevel + 1,
Lev Walkinf15320b2004-06-03 03:38:44 +0000905 cb, app_key);
906 if(ret) return ret;
Lev Walkinf15320b2004-06-03 03:38:44 +0000907 }
908
Lev Walkin8e8078a2004-09-26 13:10:40 +0000909 ilevel--;
910 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000911
Lev Walkin8e8078a2004-09-26 13:10:40 +0000912 return (cb("}", 1, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000913}
914
915void
Lev Walkin5e033762004-09-29 13:26:15 +0000916SET_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000917 int edx;
918
919 if(!td || !ptr)
920 return;
921
922 ASN_DEBUG("Freeing %s as SET", td->name);
923
Lev Walkin449f8322004-08-20 13:23:42 +0000924 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000925 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000926 void *memb_ptr;
Lev Walkincc93b0f2004-09-10 09:18:20 +0000927 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000928 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
929 if(memb_ptr)
Lev Walkinadcb5862006-03-17 02:11:12 +0000930 ASN_STRUCT_FREE(*elm->type, memb_ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +0000931 } else {
932 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
Lev Walkinadcb5862006-03-17 02:11:12 +0000933 ASN_STRUCT_FREE_CONTENTS_ONLY(*elm->type, memb_ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +0000934 }
935 }
936
937 if(!contents_only) {
938 FREEMEM(ptr);
939 }
940}
941
942int
Lev Walkin5e033762004-09-29 13:26:15 +0000943SET_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +0000944 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000945 int edx;
946
947 if(!sptr) {
Lev Walkin1eded352006-07-13 11:19:01 +0000948 _ASN_CTFAIL(app_key, td,
Lev Walkin16835b62004-08-22 13:47:59 +0000949 "%s: value not given (%s:%d)",
950 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000951 return -1;
952 }
953
954 /*
955 * Iterate over structure members and check their validity.
956 */
Lev Walkin449f8322004-08-20 13:23:42 +0000957 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000958 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000959 const void *memb_ptr;
960
Lev Walkincc93b0f2004-09-10 09:18:20 +0000961 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000962 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
963 if(!memb_ptr) {
Lev Walkincc93b0f2004-09-10 09:18:20 +0000964 if(elm->optional)
965 continue;
Lev Walkin1eded352006-07-13 11:19:01 +0000966 _ASN_CTFAIL(app_key, td,
Lev Walkincc93b0f2004-09-10 09:18:20 +0000967 "%s: mandatory element %s absent (%s:%d)",
968 td->name, elm->name, __FILE__, __LINE__);
969 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000970 }
971 } else {
972 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
973 }
974
Lev Walkin449f8322004-08-20 13:23:42 +0000975 if(elm->memb_constraints) {
976 int ret = elm->memb_constraints(elm->type, memb_ptr,
Lev Walkin1eded352006-07-13 11:19:01 +0000977 ctfailcb, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +0000978 if(ret) return ret;
979 } else {
980 int ret = elm->type->check_constraints(elm->type,
Lev Walkin1eded352006-07-13 11:19:01 +0000981 memb_ptr, ctfailcb, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +0000982 if(ret) return ret;
983 /*
984 * Cannot inherit it earlier:
985 * need to make sure we get the updated version.
986 */
987 elm->memb_constraints = elm->type->check_constraints;
988 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000989 }
990
991 return 0;
992}