blob: fa9324000ee4559536ecd28917483e9df79bf525 [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>
Lev Walkind5193802004-10-03 09:12:07 +00008#include <assert.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00009
10/*
11 * Number of bytes left for this structure.
12 * (ctx->left) indicates the number of bytes _transferred_ for the structure.
13 * (size) contains the number of bytes in the buffer passed.
14 */
Lev Walkinec1ffd42004-08-18 04:53:32 +000015#define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left)
Lev Walkinf15320b2004-06-03 03:38:44 +000016
17/*
18 * If the subprocessor function returns with an indication that it wants
19 * more data, it may well be a fatal decoding problem, because the
20 * size is constrained by the <TLV>'s L, even if the buffer size allows
21 * reading more data.
22 * For example, consider the buffer containing the following TLVs:
23 * <T:5><L:1><V> <T:6>...
24 * The TLV length clearly indicates that one byte is expected in V, but
25 * if the V processor returns with "want more data" even if the buffer
26 * contains way more data than the V processor have seen.
27 */
Lev Walkincc6a9102004-09-23 22:06:26 +000028#define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size)
Lev Walkinf15320b2004-06-03 03:38:44 +000029
30/*
31 * This macro "eats" the part of the buffer which is definitely "consumed",
32 * i.e. was correctly converted into local representation or rightfully skipped.
33 */
Lev Walkincc6a9102004-09-23 22:06:26 +000034#undef ADVANCE
Lev Walkinf15320b2004-06-03 03:38:44 +000035#define ADVANCE(num_bytes) do { \
36 size_t num = num_bytes; \
Lev Walkin4ce78ca2004-08-25 01:34:11 +000037 ptr = ((char *)ptr) + num; \
Lev Walkinf15320b2004-06-03 03:38:44 +000038 size -= num; \
39 if(ctx->left >= 0) \
40 ctx->left -= num; \
41 consumed_myself += num; \
42 } while(0)
43
44/*
45 * Switch to the next phase of parsing.
46 */
Lev Walkincc6a9102004-09-23 22:06:26 +000047#undef NEXT_PHASE
48#undef PHASE_OUT
Lev Walkinf15320b2004-06-03 03:38:44 +000049#define NEXT_PHASE(ctx) do { \
50 ctx->phase++; \
51 ctx->step = 0; \
52 } while(0)
53#define PHASE_OUT(ctx) do { ctx->phase = 10; } while(0)
54
55/*
56 * Return a standardized complex structure.
57 */
Lev Walkincc6a9102004-09-23 22:06:26 +000058#undef RETURN
Lev Walkinf15320b2004-06-03 03:38:44 +000059#define RETURN(_code) do { \
60 rval.code = _code; \
61 rval.consumed = consumed_myself;\
62 return rval; \
63 } while(0)
64
65/*
66 * The decoder of the SET OF type.
67 */
Lev Walkindc06f6b2004-10-20 15:50:55 +000068asn_dec_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +000069SET_OF_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkinf15320b2004-06-03 03:38:44 +000070 void **struct_ptr, void *ptr, size_t size, int tag_mode) {
71 /*
72 * Bring closer parts of structure description.
73 */
Lev Walkin5e033762004-09-29 13:26:15 +000074 asn_SET_OF_specifics_t *specs = (asn_SET_OF_specifics_t *)td->specifics;
75 asn_TYPE_member_t *element = td->elements; /* Single one */
Lev Walkinf15320b2004-06-03 03:38:44 +000076
77 /*
78 * Parts of the structure being constructed.
79 */
80 void *st = *struct_ptr; /* Target structure. */
Lev Walkin5e033762004-09-29 13:26:15 +000081 asn_struct_ctx_t *ctx; /* Decoder context */
Lev Walkinf15320b2004-06-03 03:38:44 +000082
83 ber_tlv_tag_t tlv_tag; /* T from TLV */
Lev Walkindc06f6b2004-10-20 15:50:55 +000084 asn_dec_rval_t rval; /* Return code from subparsers */
Lev Walkinf15320b2004-06-03 03:38:44 +000085
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 Walkin5e033762004-09-29 13:26:15 +0000103 ctx = (asn_struct_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 Walkin5e033762004-09-29 13:26:15 +0000116 rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size,
Lev Walkin8e8078a2004-09-26 13:10:40 +0000117 tag_mode, 1, &ctx->left, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000118 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 Walkin4ab42cd2004-10-05 06:36:44 +0000121 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000122 }
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;
Lev Walkinabc76b02005-01-27 17:55:42 +0000217 (void *)list = (void *)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 Walkinabc76b02005-01-27 17:55:42 +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 Walkind5193802004-10-03 09:12:07 +0000452typedef struct xer_tmp_enc_s {
453 void *buffer;
454 size_t offset;
455 size_t size;
456} xer_tmp_enc_t;
457static int
458SET_OF_encode_xer_callback(const void *buffer, size_t size, void *key) {
459 xer_tmp_enc_t *t = (xer_tmp_enc_t *)key;
460 if(t->offset + size >= t->size) {
461 size_t newsize = (t->size << 2) + size;
462 void *p = REALLOC(t->buffer, newsize);
463 if(!p) return -1;
464 t->buffer = p;
465 t->size = newsize;
466 }
467 memcpy((char *)t->buffer + t->offset, buffer, size);
468 t->offset += size;
469 return 0;
470}
471static int
472SET_OF_xer_order(const void *aptr, const void *bptr) {
473 const xer_tmp_enc_t *a = (const xer_tmp_enc_t *)aptr;
474 const xer_tmp_enc_t *b = (const xer_tmp_enc_t *)bptr;
475 size_t minlen = a->offset;
476 int ret;
477 if(b->offset < minlen) minlen = b->offset;
478 /* Well-formed UTF-8 has this nice lexicographical property... */
479 ret = memcmp(a->buffer, b->buffer, minlen);
480 if(ret != 0) return ret;
481 if(a->offset == b->offset)
482 return 0;
483 if(a->offset == minlen)
484 return -1;
485 return 1;
486}
487
488
Lev Walkina9cc46e2004-09-22 16:06:28 +0000489asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000490SET_OF_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000491 int ilevel, enum xer_encoder_flags_e flags,
492 asn_app_consume_bytes_f *cb, void *app_key) {
493 asn_enc_rval_t er;
Lev Walkin5e033762004-09-29 13:26:15 +0000494 asn_SET_OF_specifics_t *specs=(asn_SET_OF_specifics_t *)td->specifics;
495 asn_TYPE_member_t *element = td->elements;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000496 A_SET_OF(void) *list;
497 const char *mname = specs->as_XMLValueList
Lev Walkindc06f6b2004-10-20 15:50:55 +0000498 ? 0 : ((*element->name)
499 ? element->name : element->type->xml_tag);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000500 size_t mlen = mname ? strlen(mname) : 0;
501 int xcan = (flags & XER_F_CANONICAL);
Lev Walkind5193802004-10-03 09:12:07 +0000502 xer_tmp_enc_t *encs = 0;
503 size_t encs_count = 0;
504 void *original_app_key = app_key;
505 asn_app_consume_bytes_f *original_cb = cb;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000506 int i;
507
508 if(!sptr) _ASN_ENCODE_FAILED;
509
Lev Walkinabc76b02005-01-27 17:55:42 +0000510 (void *)list = sptr;
Lev Walkind5193802004-10-03 09:12:07 +0000511
512 if(xcan) {
513 encs = (xer_tmp_enc_t *)MALLOC(list->count * sizeof(encs[0]));
514 if(!encs) _ASN_ENCODE_FAILED;
515 cb = SET_OF_encode_xer_callback;
516 }
517
Lev Walkina9cc46e2004-09-22 16:06:28 +0000518 er.encoded = 0;
519
Lev Walkina9cc46e2004-09-22 16:06:28 +0000520 for(i = 0; i < list->count; i++) {
521 asn_enc_rval_t tmper;
522
523 void *memb_ptr = list->array[i];
524 if(!memb_ptr) continue;
525
Lev Walkind5193802004-10-03 09:12:07 +0000526 if(encs) {
527 memset(&encs[encs_count], 0, sizeof(encs[0]));
528 app_key = &encs[encs_count];
529 encs_count++;
530 }
531
Lev Walkina9cc46e2004-09-22 16:06:28 +0000532 if(mname) {
533 if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel);
534 _ASN_CALLBACK3("<", 1, mname, mlen, ">", 1);
535 }
536
537 tmper = element->type->xer_encoder(element->type, memb_ptr,
538 ilevel + 1, flags, cb, app_key);
Lev Walkind5193802004-10-03 09:12:07 +0000539 if(tmper.encoded == -1) {
540 td = tmper.failed_type;
541 sptr = tmper.structure_ptr;
542 goto cb_failed;
543 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000544
545 if(mname) {
546 _ASN_CALLBACK3("</", 2, mname, mlen, ">", 1);
547 er.encoded += 5;
548 }
549
550 er.encoded += (2 * mlen) + tmper.encoded;
551 }
552
553 if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel - 1);
554
Lev Walkind5193802004-10-03 09:12:07 +0000555 if(encs) {
556 xer_tmp_enc_t *enc = encs;
557 xer_tmp_enc_t *end = encs + encs_count;
558 ssize_t control_size = 0;
559
560 cb = original_cb;
561 app_key = original_app_key;
562 qsort(encs, encs_count, sizeof(encs[0]), SET_OF_xer_order);
563
564 for(; enc < end; enc++) {
565 _ASN_CALLBACK(enc->buffer, enc->offset);
566 FREEMEM(enc->buffer);
567 enc->buffer = 0;
568 control_size += enc->offset;
569 }
570 assert(control_size == er.encoded);
571 }
572
573 goto cleanup;
574cb_failed:
575 er.encoded = -1;
576 er.failed_type = td;
577 er.structure_ptr = sptr;
578cleanup:
579 if(encs) {
580 while(encs_count-- > 0) {
581 if(encs[encs_count].buffer)
582 FREEMEM(encs[encs_count].buffer);
583 }
584 free(encs);
585 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000586 return er;
587}
588
Lev Walkinf15320b2004-06-03 03:38:44 +0000589int
Lev Walkin5e033762004-09-29 13:26:15 +0000590SET_OF_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000591 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin5e033762004-09-29 13:26:15 +0000592 asn_TYPE_member_t *element = td->elements;
Lev Walkinc2346572004-08-11 09:07:36 +0000593 const A_SET_OF(void) *list;
Lev Walkinf15320b2004-06-03 03:38:44 +0000594 int ret;
595 int i;
596
Lev Walkin8e8078a2004-09-26 13:10:40 +0000597 if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000598
599 /* Dump preamble */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000600 if(cb(td->name, strlen(td->name), app_key) < 0
601 || cb(" ::= {", 6, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000602 return -1;
603
Lev Walkinabc76b02005-01-27 17:55:42 +0000604 (const void *)list = sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000605 for(i = 0; i < list->count; i++) {
606 const void *memb_ptr = list->array[i];
607 if(!memb_ptr) continue;
608
Lev Walkin8e8078a2004-09-26 13:10:40 +0000609 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000610
611 ret = element->type->print_struct(element->type, memb_ptr,
Lev Walkin8e8078a2004-09-26 13:10:40 +0000612 ilevel + 1, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +0000613 if(ret) return ret;
614 }
615
Lev Walkin8e8078a2004-09-26 13:10:40 +0000616 ilevel--;
617 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000618
Lev Walkin8e8078a2004-09-26 13:10:40 +0000619 return (cb("}", 1, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000620}
621
622void
Lev Walkin5e033762004-09-29 13:26:15 +0000623SET_OF_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000624 if(td && ptr) {
Lev Walkin5e033762004-09-29 13:26:15 +0000625 asn_TYPE_member_t *element = td->elements;
Lev Walkinc2346572004-08-11 09:07:36 +0000626 A_SET_OF(void) *list;
Lev Walkinf15320b2004-06-03 03:38:44 +0000627 int i;
628
629 /*
630 * Could not use set_of_empty() because of (*free)
631 * incompatibility.
632 */
Lev Walkinabc76b02005-01-27 17:55:42 +0000633 (void *)list = ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000634 for(i = 0; i < list->count; i++) {
635 void *memb_ptr = list->array[i];
636 if(memb_ptr)
637 element->type->free_struct(element->type, memb_ptr, 0);
638 }
Lev Walkin246a2af2004-07-15 10:51:26 +0000639 list->count = 0; /* No meaningful elements left */
640
641 asn_set_empty(list); /* Remove (list->array) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000642
643 if(!contents_only) {
644 FREEMEM(ptr);
645 }
646 }
647}
648
649int
Lev Walkin5e033762004-09-29 13:26:15 +0000650SET_OF_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000651 asn_app_consume_bytes_f *app_errlog, void *app_key) {
Lev Walkin5e033762004-09-29 13:26:15 +0000652 asn_TYPE_member_t *element = td->elements;
Lev Walkin449f8322004-08-20 13:23:42 +0000653 asn_constr_check_f *constr;
Lev Walkinc2346572004-08-11 09:07:36 +0000654 const A_SET_OF(void) *list;
Lev Walkinf15320b2004-06-03 03:38:44 +0000655 int i;
656
657 if(!sptr) {
Lev Walkinba4e5182004-08-11 09:44:13 +0000658 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkin16835b62004-08-22 13:47:59 +0000659 "%s: value not given (%s:%d)",
660 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000661 return -1;
662 }
663
Lev Walkinabc76b02005-01-27 17:55:42 +0000664 (const void *)list = sptr;
Lev Walkin449f8322004-08-20 13:23:42 +0000665
666 constr = element->memb_constraints;
667 if(!constr) constr = element->type->check_constraints;
668
669 /*
670 * Iterate over the members of an array.
671 * Validate each in turn, until one fails.
672 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000673 for(i = 0; i < list->count; i++) {
674 const void *memb_ptr = list->array[i];
Lev Walkin449f8322004-08-20 13:23:42 +0000675 int ret;
676
Lev Walkinf15320b2004-06-03 03:38:44 +0000677 if(!memb_ptr) continue;
Lev Walkin449f8322004-08-20 13:23:42 +0000678
679 ret = constr(element->type, memb_ptr, app_errlog, app_key);
680 if(ret) return ret;
Lev Walkinf15320b2004-06-03 03:38:44 +0000681 }
682
Lev Walkin449f8322004-08-20 13:23:42 +0000683 /*
684 * Cannot inherit it eralier:
685 * need to make sure we get the updated version.
686 */
687 if(!element->memb_constraints)
688 element->memb_constraints = element->type->check_constraints;
689
Lev Walkinf15320b2004-06-03 03:38:44 +0000690 return 0;
691}