blob: 055676050bcca596f3d7b12802ba588897b381da [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_OF.h>
7#include <asn_SET_OF.h>
8
9/*
10 * Number of bytes left for this structure.
11 * (ctx->left) indicates the number of bytes _transferred_ for the structure.
12 * (size) contains the number of bytes in the buffer passed.
13 */
Lev Walkinec1ffd42004-08-18 04:53:32 +000014#define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left)
Lev Walkinf15320b2004-06-03 03:38:44 +000015
16/*
17 * If the subprocessor function returns with an indication that it wants
18 * more data, it may well be a fatal decoding problem, because the
19 * size is constrained by the <TLV>'s L, even if the buffer size allows
20 * reading more data.
21 * For example, consider the buffer containing the following TLVs:
22 * <T:5><L:1><V> <T:6>...
23 * The TLV length clearly indicates that one byte is expected in V, but
24 * if the V processor returns with "want more data" even if the buffer
25 * contains way more data than the V processor have seen.
26 */
Lev Walkincc6a9102004-09-23 22:06:26 +000027#define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size)
Lev Walkinf15320b2004-06-03 03:38:44 +000028
29/*
30 * This macro "eats" the part of the buffer which is definitely "consumed",
31 * i.e. was correctly converted into local representation or rightfully skipped.
32 */
Lev Walkincc6a9102004-09-23 22:06:26 +000033#undef ADVANCE
Lev Walkinf15320b2004-06-03 03:38:44 +000034#define ADVANCE(num_bytes) do { \
35 size_t num = num_bytes; \
Lev Walkin4ce78ca2004-08-25 01:34:11 +000036 ptr = ((char *)ptr) + num; \
Lev Walkinf15320b2004-06-03 03:38:44 +000037 size -= num; \
38 if(ctx->left >= 0) \
39 ctx->left -= num; \
40 consumed_myself += num; \
41 } while(0)
42
43/*
44 * Switch to the next phase of parsing.
45 */
Lev Walkincc6a9102004-09-23 22:06:26 +000046#undef NEXT_PHASE
47#undef PHASE_OUT
Lev Walkinf15320b2004-06-03 03:38:44 +000048#define NEXT_PHASE(ctx) do { \
49 ctx->phase++; \
50 ctx->step = 0; \
51 } while(0)
52#define PHASE_OUT(ctx) do { ctx->phase = 10; } while(0)
53
54/*
55 * Return a standardized complex structure.
56 */
Lev Walkincc6a9102004-09-23 22:06:26 +000057#undef RETURN
Lev Walkinf15320b2004-06-03 03:38:44 +000058#define RETURN(_code) do { \
59 rval.code = _code; \
60 rval.consumed = consumed_myself;\
61 return rval; \
62 } while(0)
63
64/*
65 * The decoder of the SET OF type.
66 */
67ber_dec_rval_t
Lev Walkin449f8322004-08-20 13:23:42 +000068SET_OF_decode_ber(asn1_TYPE_descriptor_t *td,
Lev Walkinf15320b2004-06-03 03:38:44 +000069 void **struct_ptr, void *ptr, size_t size, int tag_mode) {
70 /*
71 * Bring closer parts of structure description.
72 */
Lev Walkin449f8322004-08-20 13:23:42 +000073 asn1_SET_OF_specifics_t *specs = (asn1_SET_OF_specifics_t *)td->specifics;
74 asn1_TYPE_member_t *element = td->elements; /* Single one */
Lev Walkinf15320b2004-06-03 03:38:44 +000075
76 /*
77 * Parts of the structure being constructed.
78 */
79 void *st = *struct_ptr; /* Target structure. */
80 ber_dec_ctx_t *ctx; /* Decoder context */
81
82 ber_tlv_tag_t tlv_tag; /* T from TLV */
83 //ber_tlv_len_t tlv_len; /* L from TLV */
84 ber_dec_rval_t rval; /* Return code from subparsers */
85
86 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
87
Lev Walkin449f8322004-08-20 13:23:42 +000088 ASN_DEBUG("Decoding %s as SET OF", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +000089
90 /*
91 * Create the target structure if it is not present already.
92 */
93 if(st == 0) {
94 st = *struct_ptr = CALLOC(1, specs->struct_size);
95 if(st == 0) {
96 RETURN(RC_FAIL);
97 }
98 }
99
100 /*
101 * Restore parsing context.
102 */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000103 ctx = (ber_dec_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000104
105 /*
106 * Start to parse where left previously
107 */
108 switch(ctx->phase) {
109 case 0:
110 /*
111 * PHASE 0.
112 * Check that the set of tags associated with given structure
113 * perfectly fits our expectations.
114 */
115
Lev Walkin449f8322004-08-20 13:23:42 +0000116 rval = ber_check_tags(td, ctx, ptr, size,
Lev Walkinf15320b2004-06-03 03:38:44 +0000117 tag_mode, &ctx->left, 0);
118 if(rval.code != RC_OK) {
119 ASN_DEBUG("%s tagging check failed: %d",
Lev Walkin449f8322004-08-20 13:23:42 +0000120 td->name, rval.code);
Lev Walkinf15320b2004-06-03 03:38:44 +0000121 consumed_myself += rval.consumed;
122 RETURN(rval.code);
123 }
124
125 if(ctx->left >= 0)
126 ctx->left += rval.consumed; /* ?Substracted below! */
127 ADVANCE(rval.consumed);
128
129 ASN_DEBUG("Structure consumes %ld bytes, "
130 "buffer %ld", (long)ctx->left, (long)size);
131
132 NEXT_PHASE(ctx);
133 /* Fall through */
134 case 1:
135 /*
136 * PHASE 1.
137 * From the place where we've left it previously,
138 * try to decode the next item.
139 */
140 for(;; ctx->step = 0) {
141 ssize_t tag_len; /* Length of TLV's T */
142
143 if(ctx->step & 1)
144 goto microphase2;
145
146 /*
147 * MICROPHASE 1: Synchronize decoding.
148 */
149
150 if(ctx->left == 0) {
Lev Walkin449f8322004-08-20 13:23:42 +0000151 ASN_DEBUG("End of SET OF %s", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000152 /*
153 * No more things to decode.
154 * Exit out of here.
155 */
156 PHASE_OUT(ctx);
157 RETURN(RC_OK);
158 }
159
160 /*
161 * Fetch the T from TLV.
162 */
163 tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
164 switch(tag_len) {
165 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
166 /* Fall through */
167 case -1: RETURN(RC_FAIL);
168 }
169
170 if(ctx->left < 0 && ((uint8_t *)ptr)[0] == 0) {
171 if(LEFT < 2) {
172 if(SIZE_VIOLATION)
173 RETURN(RC_FAIL);
174 else
175 RETURN(RC_WMORE);
176 } else if(((uint8_t *)ptr)[1] == 0) {
177 /*
178 * Found the terminator of the
179 * indefinite length structure.
180 */
181 break;
182 }
183 }
184
185 /* Outmost tag may be unknown and cannot be fetched/compared */
Lev Walkind9bd7752004-06-05 08:17:50 +0000186 if(element->tag != (ber_tlv_tag_t)-1) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000187 if(BER_TAGS_EQUAL(tlv_tag, element->tag)) {
188 /*
189 * The new list member of expected type has arrived.
190 */
191 } else {
192 ASN_DEBUG("Unexpected tag %s fixed SET OF %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000193 ber_tlv_tag_string(tlv_tag), td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000194 ASN_DEBUG("%s SET OF has tag %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000195 td->name, ber_tlv_tag_string(element->tag));
Lev Walkinf15320b2004-06-03 03:38:44 +0000196 RETURN(RC_FAIL);
197 }
198 }
199
200 /*
201 * MICROPHASE 2: Invoke the member-specific decoder.
202 */
203 ctx->step |= 1; /* Confirm entering next microphase */
204 microphase2:
205
206 /*
207 * Invoke the member fetch routine according to member's type
208 */
Lev Walkinc2346572004-08-11 09:07:36 +0000209 rval = element->type->ber_decoder(element->type,
Lev Walkinf15320b2004-06-03 03:38:44 +0000210 &ctx->ptr, ptr, LEFT, 0);
211 ASN_DEBUG("In %s SET OF %s code %d consumed %d",
Lev Walkin449f8322004-08-20 13:23:42 +0000212 td->name, element->type->name,
Lev Walkinf15320b2004-06-03 03:38:44 +0000213 rval.code, (int)rval.consumed);
214 switch(rval.code) {
215 case RC_OK:
216 {
Lev Walkinc2346572004-08-11 09:07:36 +0000217 A_SET_OF(void) *list;
218 (void *)list = st;
Lev Walkinf15320b2004-06-03 03:38:44 +0000219 if(ASN_SET_ADD(list, ctx->ptr) != 0)
220 RETURN(RC_FAIL);
221 else
222 ctx->ptr = 0;
223 }
224 break;
225 case RC_WMORE: /* More data expected */
226 if(!SIZE_VIOLATION) {
227 ADVANCE(rval.consumed);
228 RETURN(RC_WMORE);
229 }
230 /* Fall through */
231 case RC_FAIL: /* Fatal error */
232 RETURN(RC_FAIL);
233 } /* switch(rval) */
234
235 ADVANCE(rval.consumed);
236 } /* for(all list members) */
237
238 NEXT_PHASE(ctx);
239 case 2:
240 /*
241 * Read in all "end of content" TLVs.
242 */
243 while(ctx->left < 0) {
244 if(LEFT < 2) {
245 if(LEFT > 0 && ((char *)ptr)[0] != 0) {
246 /* Unexpected tag */
247 RETURN(RC_FAIL);
248 } else {
249 RETURN(RC_WMORE);
250 }
251 }
252 if(((char *)ptr)[0] == 0
253 && ((char *)ptr)[1] == 0) {
254 ADVANCE(2);
255 ctx->left++;
256 } else {
257 RETURN(RC_FAIL);
258 }
259 }
260
261 PHASE_OUT(ctx);
262 }
263
264 RETURN(RC_OK);
265}
266
267/*
268 * Internally visible buffer holding a single encoded element.
269 */
270struct _el_buffer {
271 uint8_t *buf;
272 size_t length;
273 size_t size;
274};
275/* Append bytes to the above structure */
276static int _el_addbytes(const void *buffer, size_t size, void *el_buf_ptr) {
Lev Walkinc2346572004-08-11 09:07:36 +0000277 struct _el_buffer *el_buf = (struct _el_buffer *)el_buf_ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000278
279 if(el_buf->length + size > el_buf->size)
280 return -1;
281
282 memcpy(el_buf->buf + el_buf->length, buffer, size);
283
284 el_buf->length += size;
285 return 0;
286}
287static int _el_buf_cmp(const void *ap, const void *bp) {
Lev Walkinc2346572004-08-11 09:07:36 +0000288 const struct _el_buffer *a = (const struct _el_buffer *)ap;
289 const struct _el_buffer *b = (const struct _el_buffer *)bp;
Lev Walkinf15320b2004-06-03 03:38:44 +0000290 int ret;
291 size_t common_len;
292
293 if(a->length < b->length)
294 common_len = a->length;
295 else
296 common_len = b->length;
297
298 ret = memcmp(a->buf, b->buf, common_len);
299 if(ret == 0) {
300 if(a->length < b->length)
301 ret = -1;
302 else if(a->length > b->length)
303 ret = 1;
304 }
305
306 return ret;
307}
308
309/*
310 * The DER encoder of the SET OF type.
311 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000312asn_enc_rval_t
Lev Walkin449f8322004-08-20 13:23:42 +0000313SET_OF_encode_der(asn1_TYPE_descriptor_t *td, void *ptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000314 int tag_mode, ber_tlv_tag_t tag,
315 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin449f8322004-08-20 13:23:42 +0000316 asn1_TYPE_member_t *elm = td->elements;
Lev Walkinf15320b2004-06-03 03:38:44 +0000317 asn1_TYPE_descriptor_t *elm_type = elm->type;
318 der_type_encoder_f *der_encoder = elm_type->der_encoder;
Lev Walkinc2346572004-08-11 09:07:36 +0000319 A_SET_OF(void) *list;
Lev Walkinf15320b2004-06-03 03:38:44 +0000320 size_t computed_size = 0;
321 ssize_t encoding_size = 0;
322 struct _el_buffer *encoded_els;
323 size_t max_encoded_len = 1;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000324 asn_enc_rval_t erval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000325 int ret;
326 int edx;
327
Lev Walkin449f8322004-08-20 13:23:42 +0000328 ASN_DEBUG("Estimating size for SET OF %s", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000329
330 /*
331 * Gather the length of the underlying members sequence.
332 */
Lev Walkinc2346572004-08-11 09:07:36 +0000333 (void *)list = ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000334 for(edx = 0; edx < list->count; edx++) {
335 void *memb_ptr = list->array[edx];
336 erval = der_encoder(elm_type, memb_ptr, 0, elm->tag, 0, 0);
337 if(erval.encoded == -1)
338 return erval;
339 computed_size += erval.encoded;
340
341 /* Compute maximum encoding's size */
Lev Walkind9bd7752004-06-05 08:17:50 +0000342 if(max_encoded_len < (size_t)erval.encoded)
Lev Walkinf15320b2004-06-03 03:38:44 +0000343 max_encoded_len = erval.encoded;
344 }
345
346 /*
347 * Encode the TLV for the sequence itself.
348 */
Lev Walkin449f8322004-08-20 13:23:42 +0000349 encoding_size = der_write_tags(td, computed_size, tag_mode, tag,
Lev Walkinf15320b2004-06-03 03:38:44 +0000350 cb, app_key);
351 if(encoding_size == -1) {
352 erval.encoded = -1;
Lev Walkin449f8322004-08-20 13:23:42 +0000353 erval.failed_type = td;
Lev Walkinf15320b2004-06-03 03:38:44 +0000354 erval.structure_ptr = ptr;
355 return erval;
356 }
357 computed_size += encoding_size;
358
359 if(!cb) {
360 erval.encoded = computed_size;
361 return erval;
362 }
363
364 /*
365 * DER mandates dynamic sorting of the SET OF elements
366 * according to their encodings. Build an array of the
367 * encoded elements.
368 */
Lev Walkinc2346572004-08-11 09:07:36 +0000369 (void *)encoded_els = MALLOC(list->count * sizeof(encoded_els[0]));
Lev Walkinf15320b2004-06-03 03:38:44 +0000370 if(encoded_els == NULL) {
371 erval.encoded = -1;
Lev Walkin449f8322004-08-20 13:23:42 +0000372 erval.failed_type = td;
Lev Walkinf15320b2004-06-03 03:38:44 +0000373 erval.structure_ptr = ptr;
374 return erval;
375 }
376
Lev Walkin449f8322004-08-20 13:23:42 +0000377 ASN_DEBUG("Encoding members of %s SET OF", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000378
379 /*
380 * Encode all members.
381 */
382 for(edx = 0; edx < list->count; edx++) {
383 void *memb_ptr = list->array[edx];
384 struct _el_buffer *encoded_el = &encoded_els[edx];
385
386 /*
387 * Prepare space for encoding.
388 */
Lev Walkinc2346572004-08-11 09:07:36 +0000389 encoded_el->buf = (uint8_t *)MALLOC(max_encoded_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000390 if(encoded_el->buf) {
391 encoded_el->length = 0;
392 encoded_el->size = max_encoded_len;
393 } else {
394 for(edx--; edx >= 0; edx--)
395 FREEMEM(encoded_els[edx].buf);
396 FREEMEM(encoded_els);
397 erval.encoded = -1;
Lev Walkin449f8322004-08-20 13:23:42 +0000398 erval.failed_type = td;
Lev Walkinf15320b2004-06-03 03:38:44 +0000399 erval.structure_ptr = ptr;
400 return erval;
401 }
402
403 /*
404 * Encode the member into the prepared space.
405 */
406 erval = der_encoder(elm_type, memb_ptr, 0, elm->tag,
407 _el_addbytes, encoded_el);
408 if(erval.encoded == -1) {
409 for(; edx >= 0; edx--)
410 FREEMEM(encoded_els[edx].buf);
411 FREEMEM(encoded_els);
412 return erval;
413 }
414 encoding_size += erval.encoded;
415 }
416
417 /*
418 * Sort the encoded elements according to their encoding.
419 */
420 qsort(encoded_els, list->count, sizeof(encoded_els[0]), _el_buf_cmp);
421
422 /*
423 * Report encoded elements to the application.
424 * Dispose of temporary sorted members table.
425 */
426 ret = 0;
427 for(edx = 0; edx < list->count; edx++) {
428 struct _el_buffer *encoded_el = &encoded_els[edx];
429 /* Report encoded chunks to the application */
430 if(ret == 0
431 && cb(encoded_el->buf, encoded_el->length, app_key) == -1)
432 ret = -1;
433 FREEMEM(encoded_el->buf);
434 }
435 FREEMEM(encoded_els);
436
Lev Walkind9bd7752004-06-05 08:17:50 +0000437 if(ret || computed_size != (size_t)encoding_size) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000438 /*
439 * Standard callback failed, or
440 * encoded size is not equal to the computed size.
441 */
442 erval.encoded = -1;
Lev Walkin449f8322004-08-20 13:23:42 +0000443 erval.failed_type = td;
Lev Walkinf15320b2004-06-03 03:38:44 +0000444 erval.structure_ptr = ptr;
445 } else {
446 erval.encoded = computed_size;
447 }
448
449 return erval;
450}
451
Lev Walkina9cc46e2004-09-22 16:06:28 +0000452asn_enc_rval_t
453SET_OF_encode_xer(asn1_TYPE_descriptor_t *td, void *sptr,
454 int ilevel, enum xer_encoder_flags_e flags,
455 asn_app_consume_bytes_f *cb, void *app_key) {
456 asn_enc_rval_t er;
457 asn1_SET_OF_specifics_t *specs=(asn1_SET_OF_specifics_t *)td->specifics;
458 asn1_TYPE_member_t *element = td->elements;
459 A_SET_OF(void) *list;
460 const char *mname = specs->as_XMLValueList
461 ? 0 : ((*element->name) ? element->name : element->type->name);
462 size_t mlen = mname ? strlen(mname) : 0;
463 int xcan = (flags & XER_F_CANONICAL);
464 int i;
465
466 if(!sptr) _ASN_ENCODE_FAILED;
467
468 er.encoded = 0;
469
470 (void *)list = sptr;
471 for(i = 0; i < list->count; i++) {
472 asn_enc_rval_t tmper;
473
474 void *memb_ptr = list->array[i];
475 if(!memb_ptr) continue;
476
477 if(mname) {
478 if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel);
479 _ASN_CALLBACK3("<", 1, mname, mlen, ">", 1);
480 }
481
482 tmper = element->type->xer_encoder(element->type, memb_ptr,
483 ilevel + 1, flags, cb, app_key);
484 if(tmper.encoded == -1) return tmper;
485
486 if(mname) {
487 _ASN_CALLBACK3("</", 2, mname, mlen, ">", 1);
488 er.encoded += 5;
489 }
490
491 er.encoded += (2 * mlen) + tmper.encoded;
492 }
493
494 if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel - 1);
495
496 return er;
497}
498
Lev Walkinf15320b2004-06-03 03:38:44 +0000499int
500SET_OF_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
501 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin449f8322004-08-20 13:23:42 +0000502 asn1_TYPE_member_t *element = td->elements;
Lev Walkinc2346572004-08-11 09:07:36 +0000503 const A_SET_OF(void) *list;
Lev Walkinf15320b2004-06-03 03:38:44 +0000504 int ret;
505 int i;
506
507 if(!sptr) return cb("<absent>", 8, app_key);
508
509 /* Dump preamble */
510 if(cb(td->name, strlen(td->name), app_key)
511 || cb(" ::= {\n", 7, app_key))
512 return -1;
513
Lev Walkin8ee15932004-08-11 09:34:42 +0000514 (const void *)list = sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000515 for(i = 0; i < list->count; i++) {
516 const void *memb_ptr = list->array[i];
517 if(!memb_ptr) continue;
518
519 /* Indentation */
520 for(ret = 0; ret < ilevel; ret++) cb(" ", 1, app_key);
521
522 ret = element->type->print_struct(element->type, memb_ptr,
523 ilevel + 4, cb, app_key);
524 if(ret) return ret;
525
526 ret = cb("\n", 1, app_key);
527 if(ret) return ret;
528 }
529
530 /* Indentation */
531 for(ret = 0; ret < ilevel - 4; ret++) cb(" ", 1, app_key);
532
533 return cb("}", 1, app_key);
534}
535
536void
537SET_OF_free(asn1_TYPE_descriptor_t *td, void *ptr, int contents_only) {
538 if(td && ptr) {
Lev Walkin449f8322004-08-20 13:23:42 +0000539 asn1_TYPE_member_t *element = td->elements;
Lev Walkinc2346572004-08-11 09:07:36 +0000540 A_SET_OF(void) *list;
Lev Walkinf15320b2004-06-03 03:38:44 +0000541 int i;
542
543 /*
544 * Could not use set_of_empty() because of (*free)
545 * incompatibility.
546 */
Lev Walkinc2346572004-08-11 09:07:36 +0000547 (void *)list = ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000548 for(i = 0; i < list->count; i++) {
549 void *memb_ptr = list->array[i];
550 if(memb_ptr)
551 element->type->free_struct(element->type, memb_ptr, 0);
552 }
Lev Walkin246a2af2004-07-15 10:51:26 +0000553 list->count = 0; /* No meaningful elements left */
554
555 asn_set_empty(list); /* Remove (list->array) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000556
557 if(!contents_only) {
558 FREEMEM(ptr);
559 }
560 }
561}
562
563int
564SET_OF_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
565 asn_app_consume_bytes_f *app_errlog, void *app_key) {
Lev Walkin449f8322004-08-20 13:23:42 +0000566 asn1_TYPE_member_t *element = td->elements;
567 asn_constr_check_f *constr;
Lev Walkinc2346572004-08-11 09:07:36 +0000568 const A_SET_OF(void) *list;
Lev Walkinf15320b2004-06-03 03:38:44 +0000569 int i;
570
571 if(!sptr) {
Lev Walkinba4e5182004-08-11 09:44:13 +0000572 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkin16835b62004-08-22 13:47:59 +0000573 "%s: value not given (%s:%d)",
574 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000575 return -1;
576 }
577
Lev Walkin8ee15932004-08-11 09:34:42 +0000578 (const void *)list = sptr;
Lev Walkin449f8322004-08-20 13:23:42 +0000579
580 constr = element->memb_constraints;
581 if(!constr) constr = element->type->check_constraints;
582
583 /*
584 * Iterate over the members of an array.
585 * Validate each in turn, until one fails.
586 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000587 for(i = 0; i < list->count; i++) {
588 const void *memb_ptr = list->array[i];
Lev Walkin449f8322004-08-20 13:23:42 +0000589 int ret;
590
Lev Walkinf15320b2004-06-03 03:38:44 +0000591 if(!memb_ptr) continue;
Lev Walkin449f8322004-08-20 13:23:42 +0000592
593 ret = constr(element->type, memb_ptr, app_errlog, app_key);
594 if(ret) return ret;
Lev Walkinf15320b2004-06-03 03:38:44 +0000595 }
596
Lev Walkin449f8322004-08-20 13:23:42 +0000597 /*
598 * Cannot inherit it eralier:
599 * need to make sure we get the updated version.
600 */
601 if(!element->memb_constraints)
602 element->memb_constraints = element->type->check_constraints;
603
Lev Walkinf15320b2004-06-03 03:38:44 +0000604 return 0;
605}