blob: bb6d2d391d55fe633d6903311edef5e6aa3ca333 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
Lev Walkin59b176e2005-11-26 11:25:14 +00002 * Copyright (c) 2003, 2004, 2005 Lev Walkin <vlm@lionet.info>.
3 * All rights reserved.
Lev Walkinf15320b2004-06-03 03:38:44 +00004 * Redistribution and modifications are permitted subject to BSD license.
5 */
Lev Walkina9cc46e2004-09-22 16:06:28 +00006#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00007#include <constr_SET_OF.h>
8#include <asn_SET_OF.h>
9
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 Walkin8c3b8542005-03-10 18:52:02 +000037 ptr = ((const 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 Walkin8c3b8542005-03-10 18:52:02 +000070 void **struct_ptr, const void *ptr, size_t size, int tag_mode) {
Lev Walkinf15320b2004-06-03 03:38:44 +000071 /*
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;
Lev Walkinda9a3b82005-08-16 17:00:21 +000075 asn_TYPE_member_t *elm = 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
Lev Walkin8c3b8542005-03-10 18:52:02 +0000169 if(ctx->left < 0 && ((const uint8_t *)ptr)[0] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000170 if(LEFT < 2) {
171 if(SIZE_VIOLATION)
172 RETURN(RC_FAIL);
173 else
174 RETURN(RC_WMORE);
Lev Walkin8c3b8542005-03-10 18:52:02 +0000175 } else if(((const uint8_t *)ptr)[1] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000176 /*
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 Walkinda9a3b82005-08-16 17:00:21 +0000185 if(elm->tag != (ber_tlv_tag_t)-1) {
186 if(BER_TAGS_EQUAL(tlv_tag, elm->tag)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000187 /*
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 Walkinda9a3b82005-08-16 17:00:21 +0000194 td->name, ber_tlv_tag_string(elm->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 Walkinda9a3b82005-08-16 17:00:21 +0000208 rval = elm->type->ber_decoder(opt_codec_ctx,
209 elm->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 Walkinda9a3b82005-08-16 17:00:21 +0000211 td->name, elm->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 Walkin7e033b52005-07-02 08:19:17 +0000216 asn_anonymous_set_ *list = _A_SET_FROM_VOID(st);
Lev Walkinf15320b2004-06-03 03:38:44 +0000217 if(ASN_SET_ADD(list, ctx->ptr) != 0)
218 RETURN(RC_FAIL);
219 else
220 ctx->ptr = 0;
221 }
222 break;
223 case RC_WMORE: /* More data expected */
224 if(!SIZE_VIOLATION) {
225 ADVANCE(rval.consumed);
226 RETURN(RC_WMORE);
227 }
228 /* Fall through */
229 case RC_FAIL: /* Fatal error */
230 RETURN(RC_FAIL);
231 } /* switch(rval) */
232
233 ADVANCE(rval.consumed);
234 } /* for(all list members) */
235
236 NEXT_PHASE(ctx);
237 case 2:
238 /*
239 * Read in all "end of content" TLVs.
240 */
241 while(ctx->left < 0) {
242 if(LEFT < 2) {
Lev Walkin8c3b8542005-03-10 18:52:02 +0000243 if(LEFT > 0 && ((const char *)ptr)[0] != 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000244 /* Unexpected tag */
245 RETURN(RC_FAIL);
246 } else {
247 RETURN(RC_WMORE);
248 }
249 }
Lev Walkin8c3b8542005-03-10 18:52:02 +0000250 if(((const char *)ptr)[0] == 0
251 && ((const char *)ptr)[1] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000252 ADVANCE(2);
253 ctx->left++;
254 } else {
255 RETURN(RC_FAIL);
256 }
257 }
258
259 PHASE_OUT(ctx);
260 }
261
262 RETURN(RC_OK);
263}
264
265/*
266 * Internally visible buffer holding a single encoded element.
267 */
268struct _el_buffer {
269 uint8_t *buf;
270 size_t length;
271 size_t size;
272};
273/* Append bytes to the above structure */
274static int _el_addbytes(const void *buffer, size_t size, void *el_buf_ptr) {
Lev Walkinc2346572004-08-11 09:07:36 +0000275 struct _el_buffer *el_buf = (struct _el_buffer *)el_buf_ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000276
277 if(el_buf->length + size > el_buf->size)
278 return -1;
279
280 memcpy(el_buf->buf + el_buf->length, buffer, size);
281
282 el_buf->length += size;
283 return 0;
284}
285static int _el_buf_cmp(const void *ap, const void *bp) {
Lev Walkinc2346572004-08-11 09:07:36 +0000286 const struct _el_buffer *a = (const struct _el_buffer *)ap;
287 const struct _el_buffer *b = (const struct _el_buffer *)bp;
Lev Walkinf15320b2004-06-03 03:38:44 +0000288 int ret;
289 size_t common_len;
290
291 if(a->length < b->length)
292 common_len = a->length;
293 else
294 common_len = b->length;
295
296 ret = memcmp(a->buf, b->buf, common_len);
297 if(ret == 0) {
298 if(a->length < b->length)
299 ret = -1;
300 else if(a->length > b->length)
301 ret = 1;
302 }
303
304 return ret;
305}
306
307/*
308 * The DER encoder of the SET OF type.
309 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000310asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000311SET_OF_encode_der(asn_TYPE_descriptor_t *td, void *ptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000312 int tag_mode, ber_tlv_tag_t tag,
313 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin5e033762004-09-29 13:26:15 +0000314 asn_TYPE_member_t *elm = td->elements;
315 asn_TYPE_descriptor_t *elm_type = elm->type;
Lev Walkinf15320b2004-06-03 03:38:44 +0000316 der_type_encoder_f *der_encoder = elm_type->der_encoder;
Lev Walkin7e033b52005-07-02 08:19:17 +0000317 asn_anonymous_set_ *list = _A_SET_FROM_VOID(ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +0000318 size_t computed_size = 0;
319 ssize_t encoding_size = 0;
320 struct _el_buffer *encoded_els;
Lev Walkinda9a3b82005-08-16 17:00:21 +0000321 ssize_t eels_count = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000322 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 */
332 for(edx = 0; edx < list->count; edx++) {
333 void *memb_ptr = list->array[edx];
Lev Walkinda9a3b82005-08-16 17:00:21 +0000334 if(!memb_ptr) continue;
Lev Walkinf15320b2004-06-03 03:38:44 +0000335 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
Lev Walkin3d231c02006-03-16 22:20:47 +0000358 if(!cb || list->count == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000359 erval.encoded = computed_size;
Lev Walkin59b176e2005-11-26 11:25:14 +0000360 _ASN_ENCODED_OK(erval);
Lev Walkinf15320b2004-06-03 03:38:44 +0000361 }
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];
Lev Walkinda9a3b82005-08-16 17:00:21 +0000384 struct _el_buffer *encoded_el = &encoded_els[eels_count];
385
386 if(!memb_ptr) continue;
Lev Walkinf15320b2004-06-03 03:38:44 +0000387
388 /*
389 * Prepare space for encoding.
390 */
Lev Walkinc2346572004-08-11 09:07:36 +0000391 encoded_el->buf = (uint8_t *)MALLOC(max_encoded_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000392 if(encoded_el->buf) {
393 encoded_el->length = 0;
394 encoded_el->size = max_encoded_len;
395 } else {
396 for(edx--; edx >= 0; edx--)
397 FREEMEM(encoded_els[edx].buf);
398 FREEMEM(encoded_els);
399 erval.encoded = -1;
Lev Walkin449f8322004-08-20 13:23:42 +0000400 erval.failed_type = td;
Lev Walkinf15320b2004-06-03 03:38:44 +0000401 erval.structure_ptr = ptr;
402 return erval;
403 }
404
405 /*
406 * Encode the member into the prepared space.
407 */
408 erval = der_encoder(elm_type, memb_ptr, 0, elm->tag,
409 _el_addbytes, encoded_el);
410 if(erval.encoded == -1) {
411 for(; edx >= 0; edx--)
412 FREEMEM(encoded_els[edx].buf);
413 FREEMEM(encoded_els);
414 return erval;
415 }
416 encoding_size += erval.encoded;
Lev Walkinda9a3b82005-08-16 17:00:21 +0000417 eels_count++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000418 }
419
420 /*
421 * Sort the encoded elements according to their encoding.
422 */
Lev Walkinda9a3b82005-08-16 17:00:21 +0000423 qsort(encoded_els, eels_count, sizeof(encoded_els[0]), _el_buf_cmp);
Lev Walkinf15320b2004-06-03 03:38:44 +0000424
425 /*
426 * Report encoded elements to the application.
427 * Dispose of temporary sorted members table.
428 */
429 ret = 0;
Lev Walkinda9a3b82005-08-16 17:00:21 +0000430 for(edx = 0; edx < eels_count; edx++) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000431 struct _el_buffer *encoded_el = &encoded_els[edx];
432 /* Report encoded chunks to the application */
433 if(ret == 0
Lev Walkin8e8078a2004-09-26 13:10:40 +0000434 && cb(encoded_el->buf, encoded_el->length, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000435 ret = -1;
436 FREEMEM(encoded_el->buf);
437 }
438 FREEMEM(encoded_els);
439
Lev Walkind9bd7752004-06-05 08:17:50 +0000440 if(ret || computed_size != (size_t)encoding_size) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000441 /*
442 * Standard callback failed, or
443 * encoded size is not equal to the computed size.
444 */
445 erval.encoded = -1;
Lev Walkin449f8322004-08-20 13:23:42 +0000446 erval.failed_type = td;
Lev Walkinf15320b2004-06-03 03:38:44 +0000447 erval.structure_ptr = ptr;
448 } else {
449 erval.encoded = computed_size;
450 }
451
Lev Walkin59b176e2005-11-26 11:25:14 +0000452 _ASN_ENCODED_OK(erval);
Lev Walkinf15320b2004-06-03 03:38:44 +0000453}
454
Lev Walkin8bb4a952005-02-14 20:15:40 +0000455#undef XER_ADVANCE
456#define XER_ADVANCE(num_bytes) do { \
457 size_t num = num_bytes; \
Lev Walkin8c3b8542005-03-10 18:52:02 +0000458 buf_ptr = ((const char *)buf_ptr) + num;\
Lev Walkin8bb4a952005-02-14 20:15:40 +0000459 size -= num; \
460 consumed_myself += num; \
461 } while(0)
462
463/*
464 * Decode the XER (XML) data.
465 */
466asn_dec_rval_t
467SET_OF_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
468 void **struct_ptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000469 const void *buf_ptr, size_t size) {
Lev Walkin8bb4a952005-02-14 20:15:40 +0000470 /*
471 * Bring closer parts of structure description.
472 */
473 asn_SET_OF_specifics_t *specs = (asn_SET_OF_specifics_t *)td->specifics;
474 asn_TYPE_member_t *element = td->elements;
Lev Walkind1bfea62005-11-08 03:06:16 +0000475 const char *elm_tag;
Lev Walkin8bb4a952005-02-14 20:15:40 +0000476 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
477
478 /*
479 * ... and parts of the structure being constructed.
480 */
481 void *st = *struct_ptr; /* Target structure. */
482 asn_struct_ctx_t *ctx; /* Decoder context */
483
484 asn_dec_rval_t rval; /* Return value from a decoder */
485 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
Lev Walkin8bb4a952005-02-14 20:15:40 +0000486
487 /*
488 * Create the target structure if it is not present already.
489 */
490 if(st == 0) {
491 st = *struct_ptr = CALLOC(1, specs->struct_size);
492 if(st == 0) RETURN(RC_FAIL);
493 }
494
Lev Walkind1bfea62005-11-08 03:06:16 +0000495 /* Which tag is expected for the downstream */
496 if(specs->as_XMLValueList) {
497 elm_tag = (specs->as_XMLValueList == 1) ? 0 : "";
498 } else {
499 elm_tag = (*element->name)
500 ? element->name : element->type->xml_tag;
501 }
502
Lev Walkin8bb4a952005-02-14 20:15:40 +0000503 /*
504 * Restore parsing context.
505 */
506 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
507
508 /*
509 * Phases of XER/XML processing:
510 * Phase 0: Check that the opening tag matches our expectations.
511 * Phase 1: Processing body and reacting on closing tag.
512 * Phase 2: Processing inner type.
513 */
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000514 for(; ctx->phase <= 2;) {
Lev Walkin8bb4a952005-02-14 20:15:40 +0000515 pxer_chunk_type_e ch_type; /* XER chunk type */
516 ssize_t ch_size; /* Chunk size */
517 xer_check_tag_e tcv; /* Tag check value */
518
519 /*
520 * Go inside the inner member of a set.
521 */
522 if(ctx->phase == 2) {
523 asn_dec_rval_t tmprval;
524
525 /* Invoke the inner type decoder, m.b. multiple times */
Lev Walkind1bfea62005-11-08 03:06:16 +0000526 ASN_DEBUG("XER/SET OF element [%s]", elm_tag);
Lev Walkin8bb4a952005-02-14 20:15:40 +0000527 tmprval = element->type->xer_decoder(opt_codec_ctx,
528 element->type, &ctx->ptr, elm_tag,
529 buf_ptr, size);
530 if(tmprval.code == RC_OK) {
Lev Walkin7e033b52005-07-02 08:19:17 +0000531 asn_anonymous_set_ *list = _A_SET_FROM_VOID(st);
Lev Walkin8bb4a952005-02-14 20:15:40 +0000532 if(ASN_SET_ADD(list, ctx->ptr) != 0)
533 RETURN(RC_FAIL);
534 ctx->ptr = 0;
535 XER_ADVANCE(tmprval.consumed);
536 } else {
537 XER_ADVANCE(tmprval.consumed);
538 RETURN(tmprval.code);
539 }
540 ctx->phase = 1; /* Back to body processing */
Lev Walkin8bb4a952005-02-14 20:15:40 +0000541 ASN_DEBUG("XER/SET OF phase => %d", ctx->phase);
542 /* Fall through */
543 }
544
545 /*
546 * Get the next part of the XML stream.
547 */
Lev Walkin1e443962005-02-18 18:06:36 +0000548 ch_size = xer_next_token(&ctx->context,
549 buf_ptr, size, &ch_type);
Lev Walkin8bb4a952005-02-14 20:15:40 +0000550 switch(ch_size) {
551 case -1: RETURN(RC_FAIL);
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000552 case 0: RETURN(RC_WMORE);
Lev Walkin8bb4a952005-02-14 20:15:40 +0000553 default:
554 switch(ch_type) {
555 case PXER_COMMENT: /* Got XML comment */
556 case PXER_TEXT: /* Ignore free-standing text */
557 XER_ADVANCE(ch_size); /* Skip silently */
558 continue;
559 case PXER_TAG:
560 break; /* Check the rest down there */
561 }
562 }
563
564 tcv = xer_check_tag(buf_ptr, ch_size, xml_tag);
Lev Walkind1bfea62005-11-08 03:06:16 +0000565 ASN_DEBUG("XER/SET OF: tcv = %d, ph=%d t=%s",
566 tcv, ctx->phase, xml_tag);
Lev Walkin8bb4a952005-02-14 20:15:40 +0000567 switch(tcv) {
568 case XCT_CLOSING:
569 if(ctx->phase == 0) break;
570 ctx->phase = 0;
571 /* Fall through */
572 case XCT_BOTH:
573 if(ctx->phase == 0) {
574 /* No more things to decode */
575 XER_ADVANCE(ch_size);
576 ctx->phase = 3; /* Phase out */
577 RETURN(RC_OK);
578 }
579 /* Fall through */
580 case XCT_OPENING:
581 if(ctx->phase == 0) {
582 XER_ADVANCE(ch_size);
583 ctx->phase = 1; /* Processing body phase */
584 continue;
585 }
586 /* Fall through */
Lev Walkin904e65b2005-02-18 14:23:48 +0000587 case XCT_UNKNOWN_OP:
588 case XCT_UNKNOWN_BO:
Lev Walkin8bb4a952005-02-14 20:15:40 +0000589
590 ASN_DEBUG("XER/SET OF: tcv=%d, ph=%d", tcv, ctx->phase);
Lev Walkin642962a2005-02-24 22:37:07 +0000591 if(ctx->phase == 1) {
Lev Walkin8bb4a952005-02-14 20:15:40 +0000592 /*
Lev Walkin642962a2005-02-24 22:37:07 +0000593 * Process a single possible member.
Lev Walkin8bb4a952005-02-14 20:15:40 +0000594 */
595 ctx->phase = 2;
596 continue;
Lev Walkin8bb4a952005-02-14 20:15:40 +0000597 }
598 /* Fall through */
599 default:
600 break;
601 }
602
603 ASN_DEBUG("Unexpected XML tag in SET OF");
604 break;
605 }
606
607 ctx->phase = 3; /* "Phase out" on hard failure */
608 RETURN(RC_FAIL);
609}
610
611
612
Lev Walkind5193802004-10-03 09:12:07 +0000613typedef struct xer_tmp_enc_s {
614 void *buffer;
615 size_t offset;
616 size_t size;
617} xer_tmp_enc_t;
618static int
619SET_OF_encode_xer_callback(const void *buffer, size_t size, void *key) {
620 xer_tmp_enc_t *t = (xer_tmp_enc_t *)key;
621 if(t->offset + size >= t->size) {
622 size_t newsize = (t->size << 2) + size;
623 void *p = REALLOC(t->buffer, newsize);
624 if(!p) return -1;
625 t->buffer = p;
626 t->size = newsize;
627 }
628 memcpy((char *)t->buffer + t->offset, buffer, size);
629 t->offset += size;
630 return 0;
631}
632static int
633SET_OF_xer_order(const void *aptr, const void *bptr) {
634 const xer_tmp_enc_t *a = (const xer_tmp_enc_t *)aptr;
635 const xer_tmp_enc_t *b = (const xer_tmp_enc_t *)bptr;
636 size_t minlen = a->offset;
637 int ret;
638 if(b->offset < minlen) minlen = b->offset;
639 /* Well-formed UTF-8 has this nice lexicographical property... */
640 ret = memcmp(a->buffer, b->buffer, minlen);
641 if(ret != 0) return ret;
642 if(a->offset == b->offset)
643 return 0;
644 if(a->offset == minlen)
645 return -1;
646 return 1;
647}
648
649
Lev Walkina9cc46e2004-09-22 16:06:28 +0000650asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000651SET_OF_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000652 int ilevel, enum xer_encoder_flags_e flags,
653 asn_app_consume_bytes_f *cb, void *app_key) {
654 asn_enc_rval_t er;
Lev Walkine0b56e02005-02-25 12:10:27 +0000655 asn_SET_OF_specifics_t *specs = (asn_SET_OF_specifics_t *)td->specifics;
Lev Walkinda9a3b82005-08-16 17:00:21 +0000656 asn_TYPE_member_t *elm = td->elements;
Lev Walkin7e033b52005-07-02 08:19:17 +0000657 asn_anonymous_set_ *list = _A_SET_FROM_VOID(sptr);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000658 const char *mname = specs->as_XMLValueList
Lev Walkinda9a3b82005-08-16 17:00:21 +0000659 ? 0 : ((*elm->name) ? elm->name : elm->type->xml_tag);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000660 size_t mlen = mname ? strlen(mname) : 0;
661 int xcan = (flags & XER_F_CANONICAL);
Lev Walkind5193802004-10-03 09:12:07 +0000662 xer_tmp_enc_t *encs = 0;
663 size_t encs_count = 0;
664 void *original_app_key = app_key;
665 asn_app_consume_bytes_f *original_cb = cb;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000666 int i;
667
668 if(!sptr) _ASN_ENCODE_FAILED;
669
Lev Walkind5193802004-10-03 09:12:07 +0000670 if(xcan) {
671 encs = (xer_tmp_enc_t *)MALLOC(list->count * sizeof(encs[0]));
672 if(!encs) _ASN_ENCODE_FAILED;
673 cb = SET_OF_encode_xer_callback;
674 }
675
Lev Walkina9cc46e2004-09-22 16:06:28 +0000676 er.encoded = 0;
677
Lev Walkina9cc46e2004-09-22 16:06:28 +0000678 for(i = 0; i < list->count; i++) {
679 asn_enc_rval_t tmper;
680
681 void *memb_ptr = list->array[i];
682 if(!memb_ptr) continue;
683
Lev Walkind5193802004-10-03 09:12:07 +0000684 if(encs) {
685 memset(&encs[encs_count], 0, sizeof(encs[0]));
686 app_key = &encs[encs_count];
687 encs_count++;
688 }
689
Lev Walkina9cc46e2004-09-22 16:06:28 +0000690 if(mname) {
691 if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel);
692 _ASN_CALLBACK3("<", 1, mname, mlen, ">", 1);
693 }
694
Lev Walkind1bfea62005-11-08 03:06:16 +0000695 if(!xcan && specs->as_XMLValueList == 1)
Lev Walkine0b56e02005-02-25 12:10:27 +0000696 _i_ASN_TEXT_INDENT(1, ilevel + 1);
Lev Walkinda9a3b82005-08-16 17:00:21 +0000697 tmper = elm->type->xer_encoder(elm->type, memb_ptr,
Lev Walkind1bfea62005-11-08 03:06:16 +0000698 ilevel + (specs->as_XMLValueList != 2),
699 flags, cb, app_key);
Lev Walkind5193802004-10-03 09:12:07 +0000700 if(tmper.encoded == -1) {
701 td = tmper.failed_type;
702 sptr = tmper.structure_ptr;
703 goto cb_failed;
704 }
Lev Walkin642962a2005-02-24 22:37:07 +0000705 if(tmper.encoded == 0 && specs->as_XMLValueList) {
Lev Walkind1bfea62005-11-08 03:06:16 +0000706 const char *name = elm->type->xml_tag;
Lev Walkin642962a2005-02-24 22:37:07 +0000707 size_t len = strlen(name);
Lev Walkin642962a2005-02-24 22:37:07 +0000708 _ASN_CALLBACK3("<", 1, name, len, "/>", 2);
709 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000710
711 if(mname) {
712 _ASN_CALLBACK3("</", 2, mname, mlen, ">", 1);
713 er.encoded += 5;
714 }
715
716 er.encoded += (2 * mlen) + tmper.encoded;
717 }
718
719 if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel - 1);
720
Lev Walkind5193802004-10-03 09:12:07 +0000721 if(encs) {
722 xer_tmp_enc_t *enc = encs;
723 xer_tmp_enc_t *end = encs + encs_count;
724 ssize_t control_size = 0;
725
726 cb = original_cb;
727 app_key = original_app_key;
728 qsort(encs, encs_count, sizeof(encs[0]), SET_OF_xer_order);
729
730 for(; enc < end; enc++) {
731 _ASN_CALLBACK(enc->buffer, enc->offset);
732 FREEMEM(enc->buffer);
733 enc->buffer = 0;
734 control_size += enc->offset;
735 }
736 assert(control_size == er.encoded);
737 }
738
739 goto cleanup;
740cb_failed:
741 er.encoded = -1;
742 er.failed_type = td;
743 er.structure_ptr = sptr;
744cleanup:
745 if(encs) {
746 while(encs_count-- > 0) {
747 if(encs[encs_count].buffer)
748 FREEMEM(encs[encs_count].buffer);
749 }
750 free(encs);
751 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000752 _ASN_ENCODED_OK(er);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000753}
754
Lev Walkinf15320b2004-06-03 03:38:44 +0000755int
Lev Walkin5e033762004-09-29 13:26:15 +0000756SET_OF_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000757 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinda9a3b82005-08-16 17:00:21 +0000758 asn_TYPE_member_t *elm = td->elements;
Lev Walkin7e033b52005-07-02 08:19:17 +0000759 const asn_anonymous_set_ *list = _A_CSET_FROM_VOID(sptr);
Lev Walkinf15320b2004-06-03 03:38:44 +0000760 int ret;
761 int i;
762
Lev Walkin8e8078a2004-09-26 13:10:40 +0000763 if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000764
765 /* Dump preamble */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000766 if(cb(td->name, strlen(td->name), app_key) < 0
767 || cb(" ::= {", 6, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000768 return -1;
769
770 for(i = 0; i < list->count; i++) {
771 const void *memb_ptr = list->array[i];
772 if(!memb_ptr) continue;
773
Lev Walkin8e8078a2004-09-26 13:10:40 +0000774 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000775
Lev Walkinda9a3b82005-08-16 17:00:21 +0000776 ret = elm->type->print_struct(elm->type, memb_ptr,
Lev Walkin8e8078a2004-09-26 13:10:40 +0000777 ilevel + 1, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +0000778 if(ret) return ret;
779 }
780
Lev Walkin8e8078a2004-09-26 13:10:40 +0000781 ilevel--;
782 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000783
Lev Walkin8e8078a2004-09-26 13:10:40 +0000784 return (cb("}", 1, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000785}
786
787void
Lev Walkin5e033762004-09-29 13:26:15 +0000788SET_OF_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000789 if(td && ptr) {
Lev Walkinda9a3b82005-08-16 17:00:21 +0000790 asn_TYPE_member_t *elm = td->elements;
Lev Walkin7e033b52005-07-02 08:19:17 +0000791 asn_anonymous_set_ *list = _A_SET_FROM_VOID(ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +0000792 int i;
793
794 /*
795 * Could not use set_of_empty() because of (*free)
796 * incompatibility.
797 */
798 for(i = 0; i < list->count; i++) {
799 void *memb_ptr = list->array[i];
800 if(memb_ptr)
Lev Walkinadcb5862006-03-17 02:11:12 +0000801 ASN_STRUCT_FREE(*elm->type, memb_ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +0000802 }
Lev Walkin246a2af2004-07-15 10:51:26 +0000803 list->count = 0; /* No meaningful elements left */
804
805 asn_set_empty(list); /* Remove (list->array) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000806
807 if(!contents_only) {
808 FREEMEM(ptr);
809 }
810 }
811}
812
813int
Lev Walkin5e033762004-09-29 13:26:15 +0000814SET_OF_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000815 asn_app_consume_bytes_f *app_errlog, void *app_key) {
Lev Walkinda9a3b82005-08-16 17:00:21 +0000816 asn_TYPE_member_t *elm = td->elements;
Lev Walkin449f8322004-08-20 13:23:42 +0000817 asn_constr_check_f *constr;
Lev Walkin7e033b52005-07-02 08:19:17 +0000818 const asn_anonymous_set_ *list = _A_CSET_FROM_VOID(sptr);
Lev Walkinf15320b2004-06-03 03:38:44 +0000819 int i;
820
821 if(!sptr) {
Lev Walkinba4e5182004-08-11 09:44:13 +0000822 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkin16835b62004-08-22 13:47:59 +0000823 "%s: value not given (%s:%d)",
824 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000825 return -1;
826 }
827
Lev Walkinda9a3b82005-08-16 17:00:21 +0000828 constr = elm->memb_constraints;
829 if(!constr) constr = elm->type->check_constraints;
Lev Walkin449f8322004-08-20 13:23:42 +0000830
831 /*
832 * Iterate over the members of an array.
833 * Validate each in turn, until one fails.
834 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000835 for(i = 0; i < list->count; i++) {
836 const void *memb_ptr = list->array[i];
Lev Walkin449f8322004-08-20 13:23:42 +0000837 int ret;
838
Lev Walkinf15320b2004-06-03 03:38:44 +0000839 if(!memb_ptr) continue;
Lev Walkin449f8322004-08-20 13:23:42 +0000840
Lev Walkinda9a3b82005-08-16 17:00:21 +0000841 ret = constr(elm->type, memb_ptr, app_errlog, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +0000842 if(ret) return ret;
Lev Walkinf15320b2004-06-03 03:38:44 +0000843 }
844
Lev Walkin449f8322004-08-20 13:23:42 +0000845 /*
846 * Cannot inherit it eralier:
847 * need to make sure we get the updated version.
848 */
Lev Walkinda9a3b82005-08-16 17:00:21 +0000849 if(!elm->memb_constraints)
850 elm->memb_constraints = elm->type->check_constraints;
Lev Walkin449f8322004-08-20 13:23:42 +0000851
Lev Walkinf15320b2004-06-03 03:38:44 +0000852 return 0;
853}
Lev Walkin59b176e2005-11-26 11:25:14 +0000854
855asn_dec_rval_t
856SET_OF_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
857 asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
858 asn_dec_rval_t rv;
859 asn_SET_OF_specifics_t *specs = (asn_SET_OF_specifics_t *)td->specifics;
860 asn_TYPE_member_t *elm = td->elements; /* Single one */
861 void *st = *sptr;
862 asn_anonymous_set_ *list;
863 asn_per_constraint_t *ct;
864 int repeat = 0;
865 ssize_t nelems;
866
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000867 if(_ASN_STACK_OVERFLOW_CHECK(opt_codec_ctx))
868 _ASN_DECODE_FAILED;
869
Lev Walkin59b176e2005-11-26 11:25:14 +0000870 /*
871 * Create the target structure if it is not present already.
872 */
873 if(!st) {
874 st = *sptr = CALLOC(1, specs->struct_size);
875 if(!st) _ASN_DECODE_FAILED;
876 }
877 list = _A_SET_FROM_VOID(st);
878
879 /* Figure out which constraints to use */
880 if(constraints) ct = &constraints->size;
881 else if(td->per_constraints) ct = &td->per_constraints->size;
882 else ct = 0;
883
884 if(ct && ct->flags & APC_EXTENSIBLE) {
885 int value = per_get_few_bits(pd, 1);
886 if(value < 0) _ASN_DECODE_FAILED;
887 if(value) ct = 0; /* Not restricted! */
888 }
889
890 if(ct && ct->effective_bits >= 0) {
891 /* X.691, #19.5: No length determinant */
892 nelems = per_get_few_bits(pd, ct->effective_bits);
893 ASN_DEBUG("Preparing to fetch %ld+%ld elements from %s",
894 (long)nelems, ct->lower_bound, td->name);
895 if(nelems < 0) _ASN_DECODE_FAILED;
896 nelems += ct->lower_bound;
897 } else {
898 nelems = -1;
899 }
900
901 do {
902 int i;
903 if(nelems < 0) {
904 nelems = uper_get_length(pd,
905 ct ? ct->effective_bits : -1, &repeat);
906 ASN_DEBUG("Got to decode %d elements (eff %d)",
Lev Walkin1b4d0592005-11-27 12:54:20 +0000907 (int)nelems, (int)ct ? ct->effective_bits : -1);
Lev Walkin59b176e2005-11-26 11:25:14 +0000908 if(nelems < 0) _ASN_DECODE_FAILED;
909 }
910
911 for(i = 0; i < nelems; i++) {
912 void *ptr = 0;
913 ASN_DEBUG("SET OF %s decoding", elm->type->name);
914 rv = elm->type->uper_decoder(opt_codec_ctx, elm->type,
915 elm->per_constraints, &ptr, pd);
916 ASN_DEBUG("%s SET OF %s decoded %d, %p",
917 td->name, elm->type->name, rv.code, ptr);
918 if(rv.code == RC_OK) {
919 if(ASN_SET_ADD(list, ptr) == 0)
920 continue;
921 ASN_DEBUG("Failed to add element into %s",
922 td->name);
923 /* Fall through */
924 } else {
925 ASN_DEBUG("Failed decoding %s of %s (SET OF)",
926 elm->type->name, td->name);
927 }
Lev Walkinadcb5862006-03-17 02:11:12 +0000928 if(ptr) ASN_STRUCT_FREE(*elm->type, ptr);
Lev Walkin59b176e2005-11-26 11:25:14 +0000929 _ASN_DECODE_FAILED;
930 }
931
932 nelems = -1; /* Allow uper_get_length() */
933 } while(repeat);
934
935 ASN_DEBUG("Decoded %s as SET OF", td->name);
936
937 rv.code = RC_OK;
938 rv.consumed = 0;
939 return rv;
940}
941