blob: 00c4cb3dd256a672559ab18410bad0bd12939b06 [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 Walkin5e033762004-09-29 13:26:15 +000068SET_OF_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_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 Walkin5e033762004-09-29 13:26:15 +000073 asn_SET_OF_specifics_t *specs = (asn_SET_OF_specifics_t *)td->specifics;
74 asn_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. */
Lev Walkin5e033762004-09-29 13:26:15 +000080 asn_struct_ctx_t *ctx; /* Decoder context */
Lev Walkinf15320b2004-06-03 03:38:44 +000081
82 ber_tlv_tag_t tlv_tag; /* T from TLV */
Lev Walkinf15320b2004-06-03 03:38:44 +000083 ber_dec_rval_t rval; /* Return code from subparsers */
84
85 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
86
Lev Walkin449f8322004-08-20 13:23:42 +000087 ASN_DEBUG("Decoding %s as SET OF", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +000088
89 /*
90 * Create the target structure if it is not present already.
91 */
92 if(st == 0) {
93 st = *struct_ptr = CALLOC(1, specs->struct_size);
94 if(st == 0) {
95 RETURN(RC_FAIL);
96 }
97 }
98
99 /*
100 * Restore parsing context.
101 */
Lev Walkin5e033762004-09-29 13:26:15 +0000102 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000103
104 /*
105 * Start to parse where left previously
106 */
107 switch(ctx->phase) {
108 case 0:
109 /*
110 * PHASE 0.
111 * Check that the set of tags associated with given structure
112 * perfectly fits our expectations.
113 */
114
Lev Walkin5e033762004-09-29 13:26:15 +0000115 rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size,
Lev Walkin8e8078a2004-09-26 13:10:40 +0000116 tag_mode, 1, &ctx->left, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000117 if(rval.code != RC_OK) {
118 ASN_DEBUG("%s tagging check failed: %d",
Lev Walkin449f8322004-08-20 13:23:42 +0000119 td->name, rval.code);
Lev Walkinf15320b2004-06-03 03:38:44 +0000120 consumed_myself += rval.consumed;
121 RETURN(rval.code);
122 }
123
124 if(ctx->left >= 0)
125 ctx->left += rval.consumed; /* ?Substracted below! */
126 ADVANCE(rval.consumed);
127
128 ASN_DEBUG("Structure consumes %ld bytes, "
129 "buffer %ld", (long)ctx->left, (long)size);
130
131 NEXT_PHASE(ctx);
132 /* Fall through */
133 case 1:
134 /*
135 * PHASE 1.
136 * From the place where we've left it previously,
137 * try to decode the next item.
138 */
139 for(;; ctx->step = 0) {
140 ssize_t tag_len; /* Length of TLV's T */
141
142 if(ctx->step & 1)
143 goto microphase2;
144
145 /*
146 * MICROPHASE 1: Synchronize decoding.
147 */
148
149 if(ctx->left == 0) {
Lev Walkin449f8322004-08-20 13:23:42 +0000150 ASN_DEBUG("End of SET OF %s", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000151 /*
152 * No more things to decode.
153 * Exit out of here.
154 */
155 PHASE_OUT(ctx);
156 RETURN(RC_OK);
157 }
158
159 /*
160 * Fetch the T from TLV.
161 */
162 tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
163 switch(tag_len) {
164 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
165 /* Fall through */
166 case -1: RETURN(RC_FAIL);
167 }
168
169 if(ctx->left < 0 && ((uint8_t *)ptr)[0] == 0) {
170 if(LEFT < 2) {
171 if(SIZE_VIOLATION)
172 RETURN(RC_FAIL);
173 else
174 RETURN(RC_WMORE);
175 } else if(((uint8_t *)ptr)[1] == 0) {
176 /*
177 * Found the terminator of the
178 * indefinite length structure.
179 */
180 break;
181 }
182 }
183
184 /* Outmost tag may be unknown and cannot be fetched/compared */
Lev Walkind9bd7752004-06-05 08:17:50 +0000185 if(element->tag != (ber_tlv_tag_t)-1) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000186 if(BER_TAGS_EQUAL(tlv_tag, element->tag)) {
187 /*
188 * The new list member of expected type has arrived.
189 */
190 } else {
191 ASN_DEBUG("Unexpected tag %s fixed SET OF %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000192 ber_tlv_tag_string(tlv_tag), td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000193 ASN_DEBUG("%s SET OF has tag %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000194 td->name, ber_tlv_tag_string(element->tag));
Lev Walkinf15320b2004-06-03 03:38:44 +0000195 RETURN(RC_FAIL);
196 }
197 }
198
199 /*
200 * MICROPHASE 2: Invoke the member-specific decoder.
201 */
202 ctx->step |= 1; /* Confirm entering next microphase */
203 microphase2:
204
205 /*
206 * Invoke the member fetch routine according to member's type
207 */
Lev Walkin5e033762004-09-29 13:26:15 +0000208 rval = element->type->ber_decoder(opt_codec_ctx,
209 element->type, &ctx->ptr, ptr, LEFT, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000210 ASN_DEBUG("In %s SET OF %s code %d consumed %d",
Lev Walkin449f8322004-08-20 13:23:42 +0000211 td->name, element->type->name,
Lev Walkinf15320b2004-06-03 03:38:44 +0000212 rval.code, (int)rval.consumed);
213 switch(rval.code) {
214 case RC_OK:
215 {
Lev Walkinc2346572004-08-11 09:07:36 +0000216 A_SET_OF(void) *list;
217 (void *)list = st;
Lev Walkinf15320b2004-06-03 03:38:44 +0000218 if(ASN_SET_ADD(list, ctx->ptr) != 0)
219 RETURN(RC_FAIL);
220 else
221 ctx->ptr = 0;
222 }
223 break;
224 case RC_WMORE: /* More data expected */
225 if(!SIZE_VIOLATION) {
226 ADVANCE(rval.consumed);
227 RETURN(RC_WMORE);
228 }
229 /* Fall through */
230 case RC_FAIL: /* Fatal error */
231 RETURN(RC_FAIL);
232 } /* switch(rval) */
233
234 ADVANCE(rval.consumed);
235 } /* for(all list members) */
236
237 NEXT_PHASE(ctx);
238 case 2:
239 /*
240 * Read in all "end of content" TLVs.
241 */
242 while(ctx->left < 0) {
243 if(LEFT < 2) {
244 if(LEFT > 0 && ((char *)ptr)[0] != 0) {
245 /* Unexpected tag */
246 RETURN(RC_FAIL);
247 } else {
248 RETURN(RC_WMORE);
249 }
250 }
251 if(((char *)ptr)[0] == 0
252 && ((char *)ptr)[1] == 0) {
253 ADVANCE(2);
254 ctx->left++;
255 } else {
256 RETURN(RC_FAIL);
257 }
258 }
259
260 PHASE_OUT(ctx);
261 }
262
263 RETURN(RC_OK);
264}
265
266/*
267 * Internally visible buffer holding a single encoded element.
268 */
269struct _el_buffer {
270 uint8_t *buf;
271 size_t length;
272 size_t size;
273};
274/* Append bytes to the above structure */
275static int _el_addbytes(const void *buffer, size_t size, void *el_buf_ptr) {
Lev Walkinc2346572004-08-11 09:07:36 +0000276 struct _el_buffer *el_buf = (struct _el_buffer *)el_buf_ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000277
278 if(el_buf->length + size > el_buf->size)
279 return -1;
280
281 memcpy(el_buf->buf + el_buf->length, buffer, size);
282
283 el_buf->length += size;
284 return 0;
285}
286static int _el_buf_cmp(const void *ap, const void *bp) {
Lev Walkinc2346572004-08-11 09:07:36 +0000287 const struct _el_buffer *a = (const struct _el_buffer *)ap;
288 const struct _el_buffer *b = (const struct _el_buffer *)bp;
Lev Walkinf15320b2004-06-03 03:38:44 +0000289 int ret;
290 size_t common_len;
291
292 if(a->length < b->length)
293 common_len = a->length;
294 else
295 common_len = b->length;
296
297 ret = memcmp(a->buf, b->buf, common_len);
298 if(ret == 0) {
299 if(a->length < b->length)
300 ret = -1;
301 else if(a->length > b->length)
302 ret = 1;
303 }
304
305 return ret;
306}
307
308/*
309 * The DER encoder of the SET OF type.
310 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000311asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000312SET_OF_encode_der(asn_TYPE_descriptor_t *td, void *ptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000313 int tag_mode, ber_tlv_tag_t tag,
314 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin5e033762004-09-29 13:26:15 +0000315 asn_TYPE_member_t *elm = td->elements;
316 asn_TYPE_descriptor_t *elm_type = elm->type;
Lev Walkinf15320b2004-06-03 03:38:44 +0000317 der_type_encoder_f *der_encoder = elm_type->der_encoder;
Lev Walkinc2346572004-08-11 09:07:36 +0000318 A_SET_OF(void) *list;
Lev Walkinf15320b2004-06-03 03:38:44 +0000319 size_t computed_size = 0;
320 ssize_t encoding_size = 0;
321 struct _el_buffer *encoded_els;
322 size_t max_encoded_len = 1;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000323 asn_enc_rval_t erval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000324 int ret;
325 int edx;
326
Lev Walkin449f8322004-08-20 13:23:42 +0000327 ASN_DEBUG("Estimating size for SET OF %s", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000328
329 /*
330 * Gather the length of the underlying members sequence.
331 */
Lev Walkinc2346572004-08-11 09:07:36 +0000332 (void *)list = ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000333 for(edx = 0; edx < list->count; edx++) {
334 void *memb_ptr = list->array[edx];
335 erval = der_encoder(elm_type, memb_ptr, 0, elm->tag, 0, 0);
336 if(erval.encoded == -1)
337 return erval;
338 computed_size += erval.encoded;
339
340 /* Compute maximum encoding's size */
Lev Walkind9bd7752004-06-05 08:17:50 +0000341 if(max_encoded_len < (size_t)erval.encoded)
Lev Walkinf15320b2004-06-03 03:38:44 +0000342 max_encoded_len = erval.encoded;
343 }
344
345 /*
346 * Encode the TLV for the sequence itself.
347 */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000348 encoding_size = der_write_tags(td, computed_size, tag_mode, 1, tag,
Lev Walkinf15320b2004-06-03 03:38:44 +0000349 cb, app_key);
350 if(encoding_size == -1) {
351 erval.encoded = -1;
Lev Walkin449f8322004-08-20 13:23:42 +0000352 erval.failed_type = td;
Lev Walkinf15320b2004-06-03 03:38:44 +0000353 erval.structure_ptr = ptr;
354 return erval;
355 }
356 computed_size += encoding_size;
357
358 if(!cb) {
359 erval.encoded = computed_size;
360 return erval;
361 }
362
363 /*
364 * DER mandates dynamic sorting of the SET OF elements
365 * according to their encodings. Build an array of the
366 * encoded elements.
367 */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000368 encoded_els = (struct _el_buffer *)MALLOC(
369 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
Lev Walkin8e8078a2004-09-26 13:10:40 +0000431 && cb(encoded_el->buf, encoded_el->length, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000432 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
Lev Walkin5e033762004-09-29 13:26:15 +0000453SET_OF_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000454 int ilevel, enum xer_encoder_flags_e flags,
455 asn_app_consume_bytes_f *cb, void *app_key) {
456 asn_enc_rval_t er;
Lev Walkin5e033762004-09-29 13:26:15 +0000457 asn_SET_OF_specifics_t *specs=(asn_SET_OF_specifics_t *)td->specifics;
458 asn_TYPE_member_t *element = td->elements;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000459 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
Lev Walkin5e033762004-09-29 13:26:15 +0000500SET_OF_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000501 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin5e033762004-09-29 13:26:15 +0000502 asn_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
Lev Walkin8e8078a2004-09-26 13:10:40 +0000507 if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000508
509 /* Dump preamble */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000510 if(cb(td->name, strlen(td->name), app_key) < 0
511 || cb(" ::= {", 6, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000512 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
Lev Walkin8e8078a2004-09-26 13:10:40 +0000519 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000520
521 ret = element->type->print_struct(element->type, memb_ptr,
Lev Walkin8e8078a2004-09-26 13:10:40 +0000522 ilevel + 1, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +0000523 if(ret) return ret;
524 }
525
Lev Walkin8e8078a2004-09-26 13:10:40 +0000526 ilevel--;
527 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000528
Lev Walkin8e8078a2004-09-26 13:10:40 +0000529 return (cb("}", 1, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000530}
531
532void
Lev Walkin5e033762004-09-29 13:26:15 +0000533SET_OF_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000534 if(td && ptr) {
Lev Walkin5e033762004-09-29 13:26:15 +0000535 asn_TYPE_member_t *element = td->elements;
Lev Walkinc2346572004-08-11 09:07:36 +0000536 A_SET_OF(void) *list;
Lev Walkinf15320b2004-06-03 03:38:44 +0000537 int i;
538
539 /*
540 * Could not use set_of_empty() because of (*free)
541 * incompatibility.
542 */
Lev Walkinc2346572004-08-11 09:07:36 +0000543 (void *)list = ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000544 for(i = 0; i < list->count; i++) {
545 void *memb_ptr = list->array[i];
546 if(memb_ptr)
547 element->type->free_struct(element->type, memb_ptr, 0);
548 }
Lev Walkin246a2af2004-07-15 10:51:26 +0000549 list->count = 0; /* No meaningful elements left */
550
551 asn_set_empty(list); /* Remove (list->array) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000552
553 if(!contents_only) {
554 FREEMEM(ptr);
555 }
556 }
557}
558
559int
Lev Walkin5e033762004-09-29 13:26:15 +0000560SET_OF_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000561 asn_app_consume_bytes_f *app_errlog, void *app_key) {
Lev Walkin5e033762004-09-29 13:26:15 +0000562 asn_TYPE_member_t *element = td->elements;
Lev Walkin449f8322004-08-20 13:23:42 +0000563 asn_constr_check_f *constr;
Lev Walkinc2346572004-08-11 09:07:36 +0000564 const A_SET_OF(void) *list;
Lev Walkinf15320b2004-06-03 03:38:44 +0000565 int i;
566
567 if(!sptr) {
Lev Walkinba4e5182004-08-11 09:44:13 +0000568 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkin16835b62004-08-22 13:47:59 +0000569 "%s: value not given (%s:%d)",
570 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000571 return -1;
572 }
573
Lev Walkin8ee15932004-08-11 09:34:42 +0000574 (const void *)list = sptr;
Lev Walkin449f8322004-08-20 13:23:42 +0000575
576 constr = element->memb_constraints;
577 if(!constr) constr = element->type->check_constraints;
578
579 /*
580 * Iterate over the members of an array.
581 * Validate each in turn, until one fails.
582 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000583 for(i = 0; i < list->count; i++) {
584 const void *memb_ptr = list->array[i];
Lev Walkin449f8322004-08-20 13:23:42 +0000585 int ret;
586
Lev Walkinf15320b2004-06-03 03:38:44 +0000587 if(!memb_ptr) continue;
Lev Walkin449f8322004-08-20 13:23:42 +0000588
589 ret = constr(element->type, memb_ptr, app_errlog, app_key);
590 if(ret) return ret;
Lev Walkinf15320b2004-06-03 03:38:44 +0000591 }
592
Lev Walkin449f8322004-08-20 13:23:42 +0000593 /*
594 * Cannot inherit it eralier:
595 * need to make sure we get the updated version.
596 */
597 if(!element->memb_constraints)
598 element->memb_constraints = element->type->check_constraints;
599
Lev Walkinf15320b2004-06-03 03:38:44 +0000600 return 0;
601}