blob: 931aab27d5bc9cf78f5a63df302a363ba56bf4b7 [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 Walkin8bb4a952005-02-14 20:15:40 +0000452#undef XER_ADVANCE
453#define XER_ADVANCE(num_bytes) do { \
454 size_t num = num_bytes; \
455 buf_ptr = ((char *)buf_ptr) + num; \
456 size -= num; \
457 consumed_myself += num; \
458 } while(0)
459
460/*
461 * Decode the XER (XML) data.
462 */
463asn_dec_rval_t
464SET_OF_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
465 void **struct_ptr, const char *opt_mname,
466 void *buf_ptr, size_t size) {
467 /*
468 * Bring closer parts of structure description.
469 */
470 asn_SET_OF_specifics_t *specs = (asn_SET_OF_specifics_t *)td->specifics;
471 asn_TYPE_member_t *element = td->elements;
Lev Walkin642962a2005-02-24 22:37:07 +0000472 const char *elm_tag = ((*element->name)
473 ? element->name : element->type->xml_tag);
Lev Walkin8bb4a952005-02-14 20:15:40 +0000474 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
475
476 /*
477 * ... and parts of the structure being constructed.
478 */
479 void *st = *struct_ptr; /* Target structure. */
480 asn_struct_ctx_t *ctx; /* Decoder context */
481
482 asn_dec_rval_t rval; /* Return value from a decoder */
483 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
Lev Walkin8bb4a952005-02-14 20:15:40 +0000484
485 /*
486 * Create the target structure if it is not present already.
487 */
488 if(st == 0) {
489 st = *struct_ptr = CALLOC(1, specs->struct_size);
490 if(st == 0) RETURN(RC_FAIL);
491 }
492
493 /*
494 * Restore parsing context.
495 */
496 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
497
498 /*
499 * Phases of XER/XML processing:
500 * Phase 0: Check that the opening tag matches our expectations.
501 * Phase 1: Processing body and reacting on closing tag.
502 * Phase 2: Processing inner type.
503 */
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000504 for(; ctx->phase <= 2;) {
Lev Walkin8bb4a952005-02-14 20:15:40 +0000505 pxer_chunk_type_e ch_type; /* XER chunk type */
506 ssize_t ch_size; /* Chunk size */
507 xer_check_tag_e tcv; /* Tag check value */
508
509 /*
510 * Go inside the inner member of a set.
511 */
512 if(ctx->phase == 2) {
513 asn_dec_rval_t tmprval;
514
515 /* Invoke the inner type decoder, m.b. multiple times */
516 tmprval = element->type->xer_decoder(opt_codec_ctx,
517 element->type, &ctx->ptr, elm_tag,
518 buf_ptr, size);
519 if(tmprval.code == RC_OK) {
520 A_SET_OF(void) *list;
521 (void *)list = (void *)st;
522 if(ASN_SET_ADD(list, ctx->ptr) != 0)
523 RETURN(RC_FAIL);
524 ctx->ptr = 0;
525 XER_ADVANCE(tmprval.consumed);
526 } else {
527 XER_ADVANCE(tmprval.consumed);
528 RETURN(tmprval.code);
529 }
530 ctx->phase = 1; /* Back to body processing */
Lev Walkin8bb4a952005-02-14 20:15:40 +0000531 ASN_DEBUG("XER/SET OF phase => %d", ctx->phase);
532 /* Fall through */
533 }
534
535 /*
536 * Get the next part of the XML stream.
537 */
Lev Walkin1e443962005-02-18 18:06:36 +0000538 ch_size = xer_next_token(&ctx->context,
539 buf_ptr, size, &ch_type);
Lev Walkin8bb4a952005-02-14 20:15:40 +0000540 switch(ch_size) {
541 case -1: RETURN(RC_FAIL);
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000542 case 0: RETURN(RC_WMORE);
Lev Walkin8bb4a952005-02-14 20:15:40 +0000543 default:
544 switch(ch_type) {
545 case PXER_COMMENT: /* Got XML comment */
546 case PXER_TEXT: /* Ignore free-standing text */
547 XER_ADVANCE(ch_size); /* Skip silently */
548 continue;
549 case PXER_TAG:
550 break; /* Check the rest down there */
551 }
552 }
553
554 tcv = xer_check_tag(buf_ptr, ch_size, xml_tag);
555 ASN_DEBUG("XER/SET OF: tcv = %d, ph=%d", tcv, ctx->phase);
556 switch(tcv) {
557 case XCT_CLOSING:
558 if(ctx->phase == 0) break;
559 ctx->phase = 0;
560 /* Fall through */
561 case XCT_BOTH:
562 if(ctx->phase == 0) {
563 /* No more things to decode */
564 XER_ADVANCE(ch_size);
565 ctx->phase = 3; /* Phase out */
566 RETURN(RC_OK);
567 }
568 /* Fall through */
569 case XCT_OPENING:
570 if(ctx->phase == 0) {
571 XER_ADVANCE(ch_size);
572 ctx->phase = 1; /* Processing body phase */
573 continue;
574 }
575 /* Fall through */
Lev Walkin904e65b2005-02-18 14:23:48 +0000576 case XCT_UNKNOWN_OP:
577 case XCT_UNKNOWN_BO:
Lev Walkin8bb4a952005-02-14 20:15:40 +0000578
579 ASN_DEBUG("XER/SET OF: tcv=%d, ph=%d", tcv, ctx->phase);
Lev Walkin642962a2005-02-24 22:37:07 +0000580 if(ctx->phase == 1) {
Lev Walkin8bb4a952005-02-14 20:15:40 +0000581 /*
Lev Walkin642962a2005-02-24 22:37:07 +0000582 * Process a single possible member.
Lev Walkin8bb4a952005-02-14 20:15:40 +0000583 */
584 ctx->phase = 2;
585 continue;
Lev Walkin8bb4a952005-02-14 20:15:40 +0000586 }
587 /* Fall through */
588 default:
589 break;
590 }
591
592 ASN_DEBUG("Unexpected XML tag in SET OF");
593 break;
594 }
595
596 ctx->phase = 3; /* "Phase out" on hard failure */
597 RETURN(RC_FAIL);
598}
599
600
601
Lev Walkind5193802004-10-03 09:12:07 +0000602typedef struct xer_tmp_enc_s {
603 void *buffer;
604 size_t offset;
605 size_t size;
606} xer_tmp_enc_t;
607static int
608SET_OF_encode_xer_callback(const void *buffer, size_t size, void *key) {
609 xer_tmp_enc_t *t = (xer_tmp_enc_t *)key;
610 if(t->offset + size >= t->size) {
611 size_t newsize = (t->size << 2) + size;
612 void *p = REALLOC(t->buffer, newsize);
613 if(!p) return -1;
614 t->buffer = p;
615 t->size = newsize;
616 }
617 memcpy((char *)t->buffer + t->offset, buffer, size);
618 t->offset += size;
619 return 0;
620}
621static int
622SET_OF_xer_order(const void *aptr, const void *bptr) {
623 const xer_tmp_enc_t *a = (const xer_tmp_enc_t *)aptr;
624 const xer_tmp_enc_t *b = (const xer_tmp_enc_t *)bptr;
625 size_t minlen = a->offset;
626 int ret;
627 if(b->offset < minlen) minlen = b->offset;
628 /* Well-formed UTF-8 has this nice lexicographical property... */
629 ret = memcmp(a->buffer, b->buffer, minlen);
630 if(ret != 0) return ret;
631 if(a->offset == b->offset)
632 return 0;
633 if(a->offset == minlen)
634 return -1;
635 return 1;
636}
637
638
Lev Walkina9cc46e2004-09-22 16:06:28 +0000639asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000640SET_OF_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000641 int ilevel, enum xer_encoder_flags_e flags,
642 asn_app_consume_bytes_f *cb, void *app_key) {
643 asn_enc_rval_t er;
Lev Walkin5e033762004-09-29 13:26:15 +0000644 asn_SET_OF_specifics_t *specs=(asn_SET_OF_specifics_t *)td->specifics;
645 asn_TYPE_member_t *element = td->elements;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000646 A_SET_OF(void) *list;
647 const char *mname = specs->as_XMLValueList
Lev Walkindc06f6b2004-10-20 15:50:55 +0000648 ? 0 : ((*element->name)
649 ? element->name : element->type->xml_tag);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000650 size_t mlen = mname ? strlen(mname) : 0;
651 int xcan = (flags & XER_F_CANONICAL);
Lev Walkind5193802004-10-03 09:12:07 +0000652 xer_tmp_enc_t *encs = 0;
653 size_t encs_count = 0;
654 void *original_app_key = app_key;
655 asn_app_consume_bytes_f *original_cb = cb;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000656 int i;
657
658 if(!sptr) _ASN_ENCODE_FAILED;
659
Lev Walkinabc76b02005-01-27 17:55:42 +0000660 (void *)list = sptr;
Lev Walkind5193802004-10-03 09:12:07 +0000661
662 if(xcan) {
663 encs = (xer_tmp_enc_t *)MALLOC(list->count * sizeof(encs[0]));
664 if(!encs) _ASN_ENCODE_FAILED;
665 cb = SET_OF_encode_xer_callback;
666 }
667
Lev Walkina9cc46e2004-09-22 16:06:28 +0000668 er.encoded = 0;
669
Lev Walkina9cc46e2004-09-22 16:06:28 +0000670 for(i = 0; i < list->count; i++) {
671 asn_enc_rval_t tmper;
672
673 void *memb_ptr = list->array[i];
674 if(!memb_ptr) continue;
675
Lev Walkind5193802004-10-03 09:12:07 +0000676 if(encs) {
677 memset(&encs[encs_count], 0, sizeof(encs[0]));
678 app_key = &encs[encs_count];
679 encs_count++;
680 }
681
Lev Walkina9cc46e2004-09-22 16:06:28 +0000682 if(mname) {
683 if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel);
684 _ASN_CALLBACK3("<", 1, mname, mlen, ">", 1);
685 }
686
687 tmper = element->type->xer_encoder(element->type, memb_ptr,
688 ilevel + 1, flags, cb, app_key);
Lev Walkind5193802004-10-03 09:12:07 +0000689 if(tmper.encoded == -1) {
690 td = tmper.failed_type;
691 sptr = tmper.structure_ptr;
692 goto cb_failed;
693 }
Lev Walkin642962a2005-02-24 22:37:07 +0000694 if(tmper.encoded == 0 && specs->as_XMLValueList) {
695 const char *name = (*element->name)
696 ? element->name : element->type->xml_tag;
697 size_t len = strlen(name);
698 if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel + 1);
699 _ASN_CALLBACK3("<", 1, name, len, "/>", 2);
700 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000701
702 if(mname) {
703 _ASN_CALLBACK3("</", 2, mname, mlen, ">", 1);
704 er.encoded += 5;
705 }
706
707 er.encoded += (2 * mlen) + tmper.encoded;
708 }
709
710 if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel - 1);
711
Lev Walkind5193802004-10-03 09:12:07 +0000712 if(encs) {
713 xer_tmp_enc_t *enc = encs;
714 xer_tmp_enc_t *end = encs + encs_count;
715 ssize_t control_size = 0;
716
717 cb = original_cb;
718 app_key = original_app_key;
719 qsort(encs, encs_count, sizeof(encs[0]), SET_OF_xer_order);
720
721 for(; enc < end; enc++) {
722 _ASN_CALLBACK(enc->buffer, enc->offset);
723 FREEMEM(enc->buffer);
724 enc->buffer = 0;
725 control_size += enc->offset;
726 }
727 assert(control_size == er.encoded);
728 }
729
730 goto cleanup;
731cb_failed:
732 er.encoded = -1;
733 er.failed_type = td;
734 er.structure_ptr = sptr;
735cleanup:
736 if(encs) {
737 while(encs_count-- > 0) {
738 if(encs[encs_count].buffer)
739 FREEMEM(encs[encs_count].buffer);
740 }
741 free(encs);
742 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000743 return er;
744}
745
Lev Walkinf15320b2004-06-03 03:38:44 +0000746int
Lev Walkin5e033762004-09-29 13:26:15 +0000747SET_OF_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000748 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin5e033762004-09-29 13:26:15 +0000749 asn_TYPE_member_t *element = td->elements;
Lev Walkinc2346572004-08-11 09:07:36 +0000750 const A_SET_OF(void) *list;
Lev Walkinf15320b2004-06-03 03:38:44 +0000751 int ret;
752 int i;
753
Lev Walkin8e8078a2004-09-26 13:10:40 +0000754 if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000755
756 /* Dump preamble */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000757 if(cb(td->name, strlen(td->name), app_key) < 0
758 || cb(" ::= {", 6, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000759 return -1;
760
Lev Walkinabc76b02005-01-27 17:55:42 +0000761 (const void *)list = sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000762 for(i = 0; i < list->count; i++) {
763 const void *memb_ptr = list->array[i];
764 if(!memb_ptr) continue;
765
Lev Walkin8e8078a2004-09-26 13:10:40 +0000766 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000767
768 ret = element->type->print_struct(element->type, memb_ptr,
Lev Walkin8e8078a2004-09-26 13:10:40 +0000769 ilevel + 1, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +0000770 if(ret) return ret;
771 }
772
Lev Walkin8e8078a2004-09-26 13:10:40 +0000773 ilevel--;
774 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000775
Lev Walkin8e8078a2004-09-26 13:10:40 +0000776 return (cb("}", 1, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000777}
778
779void
Lev Walkin5e033762004-09-29 13:26:15 +0000780SET_OF_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000781 if(td && ptr) {
Lev Walkin5e033762004-09-29 13:26:15 +0000782 asn_TYPE_member_t *element = td->elements;
Lev Walkinc2346572004-08-11 09:07:36 +0000783 A_SET_OF(void) *list;
Lev Walkinf15320b2004-06-03 03:38:44 +0000784 int i;
785
786 /*
787 * Could not use set_of_empty() because of (*free)
788 * incompatibility.
789 */
Lev Walkinabc76b02005-01-27 17:55:42 +0000790 (void *)list = ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000791 for(i = 0; i < list->count; i++) {
792 void *memb_ptr = list->array[i];
793 if(memb_ptr)
794 element->type->free_struct(element->type, memb_ptr, 0);
795 }
Lev Walkin246a2af2004-07-15 10:51:26 +0000796 list->count = 0; /* No meaningful elements left */
797
798 asn_set_empty(list); /* Remove (list->array) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000799
800 if(!contents_only) {
801 FREEMEM(ptr);
802 }
803 }
804}
805
806int
Lev Walkin5e033762004-09-29 13:26:15 +0000807SET_OF_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000808 asn_app_consume_bytes_f *app_errlog, void *app_key) {
Lev Walkin5e033762004-09-29 13:26:15 +0000809 asn_TYPE_member_t *element = td->elements;
Lev Walkin449f8322004-08-20 13:23:42 +0000810 asn_constr_check_f *constr;
Lev Walkinc2346572004-08-11 09:07:36 +0000811 const A_SET_OF(void) *list;
Lev Walkinf15320b2004-06-03 03:38:44 +0000812 int i;
813
814 if(!sptr) {
Lev Walkinba4e5182004-08-11 09:44:13 +0000815 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkin16835b62004-08-22 13:47:59 +0000816 "%s: value not given (%s:%d)",
817 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000818 return -1;
819 }
820
Lev Walkinabc76b02005-01-27 17:55:42 +0000821 (const void *)list = sptr;
Lev Walkin449f8322004-08-20 13:23:42 +0000822
823 constr = element->memb_constraints;
824 if(!constr) constr = element->type->check_constraints;
825
826 /*
827 * Iterate over the members of an array.
828 * Validate each in turn, until one fails.
829 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000830 for(i = 0; i < list->count; i++) {
831 const void *memb_ptr = list->array[i];
Lev Walkin449f8322004-08-20 13:23:42 +0000832 int ret;
833
Lev Walkinf15320b2004-06-03 03:38:44 +0000834 if(!memb_ptr) continue;
Lev Walkin449f8322004-08-20 13:23:42 +0000835
836 ret = constr(element->type, memb_ptr, app_errlog, app_key);
837 if(ret) return ret;
Lev Walkinf15320b2004-06-03 03:38:44 +0000838 }
839
Lev Walkin449f8322004-08-20 13:23:42 +0000840 /*
841 * Cannot inherit it eralier:
842 * need to make sure we get the updated version.
843 */
844 if(!element->memb_constraints)
845 element->memb_constraints = element->type->check_constraints;
846
Lev Walkinf15320b2004-06-03 03:38:44 +0000847 return 0;
848}