blob: 8a124c09f1dbf754b6893b9e17e89b8f558291a6 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
Lev Walkinadf863f2006-09-05 16:18:34 +00002 * Copyright (c) 2003, 2004, 2005, 2006 Lev Walkin <vlm@lionet.info>.
Lev Walkinac589332005-08-22 14:19:28 +00003 * 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.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00008
Lev Walkin6c0df202005-02-14 19:03:17 +00009/* Check that all the mandatory members are present */
10static int _SET_is_populated(asn_TYPE_descriptor_t *td, void *st);
11
Lev Walkinf15320b2004-06-03 03:38:44 +000012/*
13 * Number of bytes left for this structure.
14 * (ctx->left) indicates the number of bytes _transferred_ for the structure.
15 * (size) contains the number of bytes in the buffer passed.
16 */
Lev Walkinec1ffd42004-08-18 04:53:32 +000017#define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left)
Lev Walkinf15320b2004-06-03 03:38:44 +000018
19/*
20 * If the subprocessor function returns with an indication that it wants
21 * more data, it may well be a fatal decoding problem, because the
22 * size is constrained by the <TLV>'s L, even if the buffer size allows
23 * reading more data.
24 * For example, consider the buffer containing the following TLVs:
25 * <T:5><L:1><V> <T:6>...
26 * The TLV length clearly indicates that one byte is expected in V, but
27 * if the V processor returns with "want more data" even if the buffer
28 * contains way more data than the V processor have seen.
29 */
Lev Walkind9bd7752004-06-05 08:17:50 +000030#define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size)
Lev Walkinf15320b2004-06-03 03:38:44 +000031
32/*
33 * This macro "eats" the part of the buffer which is definitely "consumed",
34 * i.e. was correctly converted into local representation or rightfully skipped.
35 */
Lev Walkincc6a9102004-09-23 22:06:26 +000036#undef ADVANCE
Lev Walkinf15320b2004-06-03 03:38:44 +000037#define ADVANCE(num_bytes) do { \
38 size_t num = num_bytes; \
Lev Walkinaa61a0f2014-01-13 23:08:47 -080039 ptr = ((const char *)ptr) + num;\
Lev Walkinf15320b2004-06-03 03:38:44 +000040 size -= num; \
41 if(ctx->left >= 0) \
42 ctx->left -= num; \
43 consumed_myself += num; \
44 } while(0)
45
46/*
47 * Switch to the next phase of parsing.
48 */
Lev Walkincc6a9102004-09-23 22:06:26 +000049#undef NEXT_PHASE
Lev Walkinf15320b2004-06-03 03:38:44 +000050#define NEXT_PHASE(ctx) do { \
51 ctx->phase++; \
52 ctx->step = 0; \
53 } 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 * Tags are canonically sorted in the tag2element map.
67 */
68static int
69_t2e_cmp(const void *ap, const void *bp) {
Lev Walkin5e033762004-09-29 13:26:15 +000070 const asn_TYPE_tag2member_t *a = (const asn_TYPE_tag2member_t *)ap;
71 const asn_TYPE_tag2member_t *b = (const asn_TYPE_tag2member_t *)bp;
Lev Walkinc2346572004-08-11 09:07:36 +000072
Lev Walkinf15320b2004-06-03 03:38:44 +000073 int a_class = BER_TAG_CLASS(a->el_tag);
74 int b_class = BER_TAG_CLASS(b->el_tag);
75
76 if(a_class == b_class) {
77 ber_tlv_tag_t a_value = BER_TAG_VALUE(a->el_tag);
78 ber_tlv_tag_t b_value = BER_TAG_VALUE(b->el_tag);
79
80 if(a_value == b_value)
81 return 0;
82 else if(a_value < b_value)
83 return -1;
84 else
85 return 1;
86 } else if(a_class < b_class) {
87 return -1;
88 } else {
89 return 1;
90 }
91}
92
93/*
94 * The decoder of the SET type.
95 */
Lev Walkindc06f6b2004-10-20 15:50:55 +000096asn_dec_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +000097SET_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkin8c3b8542005-03-10 18:52:02 +000098 void **struct_ptr, const void *ptr, size_t size, int tag_mode) {
Lev Walkinf15320b2004-06-03 03:38:44 +000099 /*
100 * Bring closer parts of structure description.
101 */
Lev Walkin5e033762004-09-29 13:26:15 +0000102 asn_SET_specifics_t *specs = (asn_SET_specifics_t *)td->specifics;
103 asn_TYPE_member_t *elements = td->elements;
Lev Walkinf15320b2004-06-03 03:38:44 +0000104
105 /*
106 * Parts of the structure being constructed.
107 */
108 void *st = *struct_ptr; /* Target structure. */
Lev Walkin5e033762004-09-29 13:26:15 +0000109 asn_struct_ctx_t *ctx; /* Decoder context */
Lev Walkinf15320b2004-06-03 03:38:44 +0000110
111 ber_tlv_tag_t tlv_tag; /* T from TLV */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000112 asn_dec_rval_t rval; /* Return code from subparsers */
Lev Walkinf15320b2004-06-03 03:38:44 +0000113
114 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
115 int edx; /* SET element's index */
116
Lev Walkin449f8322004-08-20 13:23:42 +0000117 ASN_DEBUG("Decoding %s as SET", td->name);
Lev Walkinadf863f2006-09-05 16:18:34 +0000118
119 if(_ASN_STACK_OVERFLOW_CHECK(opt_codec_ctx))
120 _ASN_DECODE_FAILED;
121
Lev Walkinf15320b2004-06-03 03:38:44 +0000122 /*
123 * Create the target structure if it is not present already.
124 */
125 if(st == 0) {
126 st = *struct_ptr = CALLOC(1, specs->struct_size);
127 if(st == 0) {
128 RETURN(RC_FAIL);
129 }
130 }
131
132 /*
133 * Restore parsing context.
134 */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800135 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000136
137 /*
138 * Start to parse where left previously
139 */
140 switch(ctx->phase) {
141 case 0:
142 /*
143 * PHASE 0.
144 * Check that the set of tags associated with given structure
145 * perfectly fits our expectations.
146 */
147
Lev Walkin5e033762004-09-29 13:26:15 +0000148 rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size,
Lev Walkin8e8078a2004-09-26 13:10:40 +0000149 tag_mode, 1, &ctx->left, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000150 if(rval.code != RC_OK) {
151 ASN_DEBUG("%s tagging check failed: %d",
Lev Walkin449f8322004-08-20 13:23:42 +0000152 td->name, rval.code);
Lev Walkin4ab42cd2004-10-05 06:36:44 +0000153 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000154 }
155
156 if(ctx->left >= 0)
157 ctx->left += rval.consumed; /* ?Substracted below! */
158 ADVANCE(rval.consumed);
159
160 NEXT_PHASE(ctx);
161
162 ASN_DEBUG("Structure advertised %ld bytes, "
163 "buffer contains %ld", (long)ctx->left, (long)size);
164
165 /* Fall through */
166 case 1:
167 /*
168 * PHASE 1.
169 * From the place where we've left it previously,
170 * try to decode the next member from the list of
171 * this structure's elements.
Lev Walkin3e478ed2004-10-28 13:04:02 +0000172 * Note that elements in BER may arrive out of
Lev Walkinf15320b2004-06-03 03:38:44 +0000173 * order, yet DER mandates that they shall arive in the
174 * canonical order of their tags. So, there is a room
175 * for optimization.
176 */
Lev Walkin3e478ed2004-10-28 13:04:02 +0000177 for(;; ctx->step = 0) {
Wim Lewisfb6344e2014-07-28 12:16:01 -0700178 const asn_TYPE_tag2member_t *t2m;
Lev Walkin3e478ed2004-10-28 13:04:02 +0000179 asn_TYPE_tag2member_t key;
Lev Walkinf15320b2004-06-03 03:38:44 +0000180 void *memb_ptr; /* Pointer to the member */
Lev Walkinc2346572004-08-11 09:07:36 +0000181 void **memb_ptr2; /* Pointer to that pointer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000182 ssize_t tag_len; /* Length of TLV's T */
183
Lev Walkin3e478ed2004-10-28 13:04:02 +0000184 if(ctx->step & 1) {
185 edx = ctx->step >> 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000186 goto microphase2;
Lev Walkin3e478ed2004-10-28 13:04:02 +0000187 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000188
189 /*
190 * MICROPHASE 1: Synchronize decoding.
191 */
192
193 if(ctx->left == 0)
194 /*
195 * No more things to decode.
196 * Exit out of here and check whether all mandatory
197 * elements have been received (in the next phase).
198 */
199 break;
200
201 /*
202 * Fetch the T from TLV.
203 */
204 tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
205 switch(tag_len) {
206 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
207 /* Fall through */
208 case -1: RETURN(RC_FAIL);
209 }
210
Lev Walkin8c3b8542005-03-10 18:52:02 +0000211 if(ctx->left < 0 && ((const uint8_t *)ptr)[0] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000212 if(LEFT < 2) {
213 if(SIZE_VIOLATION)
214 RETURN(RC_FAIL);
215 else
216 RETURN(RC_WMORE);
Lev Walkin8c3b8542005-03-10 18:52:02 +0000217 } else if(((const uint8_t *)ptr)[1] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000218 /*
219 * Found the terminator of the
220 * indefinite length structure.
221 * Invoke the generic finalization function.
222 */
223 goto phase3;
224 }
225 }
226
Lev Walkin3e478ed2004-10-28 13:04:02 +0000227 key.el_tag = tlv_tag;
Wim Lewisfb6344e2014-07-28 12:16:01 -0700228 t2m = (const asn_TYPE_tag2member_t *)bsearch(&key,
Lev Walkin3e478ed2004-10-28 13:04:02 +0000229 specs->tag2el, specs->tag2el_count,
230 sizeof(specs->tag2el[0]), _t2e_cmp);
231 if(t2m) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000232 /*
Lev Walkin3e478ed2004-10-28 13:04:02 +0000233 * Found the element corresponding to the tag.
Lev Walkinf15320b2004-06-03 03:38:44 +0000234 */
Lev Walkin3e478ed2004-10-28 13:04:02 +0000235 edx = t2m->el_no;
236 ctx->step = (edx << 1) + 1;
237 ASN_DEBUG("Got tag %s (%s), edx %d",
238 ber_tlv_tag_string(tlv_tag), td->name, edx);
239 } else if(specs->extensible == 0) {
240 ASN_DEBUG("Unexpected tag %s "
241 "in non-extensible SET %s",
242 ber_tlv_tag_string(tlv_tag), td->name);
243 RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000244 } else {
Lev Walkin3e478ed2004-10-28 13:04:02 +0000245 /* Skip this tag */
246 ssize_t skip;
Lev Walkinf15320b2004-06-03 03:38:44 +0000247
Lev Walkin3e478ed2004-10-28 13:04:02 +0000248 ASN_DEBUG("Skipping unknown tag %s",
249 ber_tlv_tag_string(tlv_tag));
Lev Walkinf15320b2004-06-03 03:38:44 +0000250
Lev Walkin3e478ed2004-10-28 13:04:02 +0000251 skip = ber_skip_length(opt_codec_ctx,
252 BER_TLV_CONSTRUCTED(ptr),
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800253 (const char *)ptr + tag_len, LEFT - tag_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000254
Lev Walkin3e478ed2004-10-28 13:04:02 +0000255 switch(skip) {
256 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
257 /* Fall through */
258 case -1: RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000259 }
Lev Walkin3e478ed2004-10-28 13:04:02 +0000260
261 ADVANCE(skip + tag_len);
262 continue; /* Try again with the next tag */
Lev Walkinf15320b2004-06-03 03:38:44 +0000263 }
264
265 /*
266 * MICROPHASE 2: Invoke the member-specific decoder.
267 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000268 microphase2:
269
270 /*
271 * Check for duplications: must not overwrite
272 * already decoded elements.
273 */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800274 if(ASN_SET_ISPRESENT2((char *)st + specs->pres_offset, edx)) {
Lev Walkinbbe04752004-07-01 00:47:16 +0000275 ASN_DEBUG("SET %s: Duplicate element %s (%d)",
Lev Walkin449f8322004-08-20 13:23:42 +0000276 td->name, elements[edx].name, edx);
Lev Walkinf15320b2004-06-03 03:38:44 +0000277 RETURN(RC_FAIL);
278 }
279
280 /*
281 * Compute the position of the member inside a structure,
282 * and also a type of containment (it may be contained
283 * as pointer or using inline inclusion).
284 */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000285 if(elements[edx].flags & ATF_POINTER) {
286 /* Member is a pointer to another structure */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800287 memb_ptr2 = (void **)((char *)st + elements[edx].memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000288 } else {
289 /*
290 * A pointer to a pointer
291 * holding the start of the structure
292 */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800293 memb_ptr = (char *)st + elements[edx].memb_offset;
Lev Walkinf15320b2004-06-03 03:38:44 +0000294 memb_ptr2 = &memb_ptr;
295 }
296 /*
297 * Invoke the member fetch routine according to member's type
298 */
Lev Walkin5e033762004-09-29 13:26:15 +0000299 rval = elements[edx].type->ber_decoder(opt_codec_ctx,
Lev Walkinc2346572004-08-11 09:07:36 +0000300 elements[edx].type,
Lev Walkinf15320b2004-06-03 03:38:44 +0000301 memb_ptr2, ptr, LEFT,
302 elements[edx].tag_mode);
303 switch(rval.code) {
304 case RC_OK:
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800305 ASN_SET_MKPRESENT((char *)st + specs->pres_offset, edx);
Lev Walkinf15320b2004-06-03 03:38:44 +0000306 break;
307 case RC_WMORE: /* More data expected */
308 if(!SIZE_VIOLATION) {
309 ADVANCE(rval.consumed);
310 RETURN(RC_WMORE);
311 }
312 /* Fail through */
313 case RC_FAIL: /* Fatal error */
314 RETURN(RC_FAIL);
315 } /* switch(rval) */
316
317 ADVANCE(rval.consumed);
318 } /* for(all structure members) */
319
320 phase3:
321 ctx->phase = 3;
322 /* Fall through */
323 case 3:
324 case 4: /* Only 00 is expected */
325 ASN_DEBUG("SET %s Leftover: %ld, size = %ld",
Lev Walkin449f8322004-08-20 13:23:42 +0000326 td->name, (long)ctx->left, (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000327
328 /*
329 * Skip everything until the end of the SET.
330 */
331 while(ctx->left) {
332 ssize_t tl, ll;
333
334 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
335 switch(tl) {
336 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
337 /* Fall through */
338 case -1: RETURN(RC_FAIL);
339 }
340
341 /*
342 * If expected <0><0>...
343 */
344 if(ctx->left < 0
Lev Walkin8c3b8542005-03-10 18:52:02 +0000345 && ((const uint8_t *)ptr)[0] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000346 if(LEFT < 2) {
347 if(SIZE_VIOLATION)
348 RETURN(RC_FAIL);
349 else
350 RETURN(RC_WMORE);
Lev Walkin8c3b8542005-03-10 18:52:02 +0000351 } else if(((const uint8_t *)ptr)[1] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000352 /*
353 * Correctly finished with <0><0>.
354 */
355 ADVANCE(2);
356 ctx->left++;
357 ctx->phase = 4;
358 continue;
359 }
360 }
361
362 if(specs->extensible == 0 || ctx->phase == 4) {
363 ASN_DEBUG("Unexpected continuation "
Lev Walkin3e478ed2004-10-28 13:04:02 +0000364 "of a non-extensible type %s "
365 "(ptr=%02x)",
Lev Walkin8c3b8542005-03-10 18:52:02 +0000366 td->name, *(const uint8_t *)ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +0000367 RETURN(RC_FAIL);
368 }
369
Lev Walkinbaaa24f2004-09-29 14:19:14 +0000370 ll = ber_skip_length(opt_codec_ctx,
Lev Walkinf15320b2004-06-03 03:38:44 +0000371 BER_TLV_CONSTRUCTED(ptr),
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800372 (const char *)ptr + tl, LEFT - tl);
Lev Walkinf15320b2004-06-03 03:38:44 +0000373 switch(ll) {
374 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
375 /* Fall through */
376 case -1: RETURN(RC_FAIL);
377 }
378
379 ADVANCE(tl + ll);
380 }
381
382 ctx->phase = 5;
383 case 5:
Lev Walkin6c0df202005-02-14 19:03:17 +0000384 /* Check that all mandatory elements are present. */
385 if(!_SET_is_populated(td, st))
386 RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000387
388 NEXT_PHASE(ctx);
389 }
390
391 RETURN(RC_OK);
392}
393
Lev Walkin6c0df202005-02-14 19:03:17 +0000394static int
395_SET_is_populated(asn_TYPE_descriptor_t *td, void *st) {
396 asn_SET_specifics_t *specs = (asn_SET_specifics_t *)td->specifics;
397 int edx;
398
399 /*
400 * Check that all mandatory elements are present.
401 */
402 for(edx = 0; edx < td->elements_count;
403 edx += (8 * sizeof(specs->_mandatory_elements[0]))) {
404 unsigned int midx, pres, must;
405
406 midx = edx/(8 * sizeof(specs->_mandatory_elements[0]));
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800407 pres = ((unsigned int *)((char *)st + specs->pres_offset))[midx];
Lev Walkin217f2402006-11-21 10:38:50 +0000408 must = sys_ntohl(specs->_mandatory_elements[midx]);
Lev Walkin6c0df202005-02-14 19:03:17 +0000409
410 if((pres & must) == must) {
411 /*
412 * Yes, everything seems to be in place.
413 */
414 } else {
415 ASN_DEBUG("One or more mandatory elements "
416 "of a SET %s %d (%08x.%08x)=%08x "
417 "are not present",
418 td->name,
419 midx,
420 pres,
421 must,
422 (~(pres & must) & must)
423 );
424 return 0;
425 }
426 }
427
428 return 1;
429}
430
Lev Walkinf15320b2004-06-03 03:38:44 +0000431/*
432 * The DER encoder of the SET type.
433 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000434asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000435SET_encode_der(asn_TYPE_descriptor_t *td,
Lev Walkind5193802004-10-03 09:12:07 +0000436 void *sptr, int tag_mode, ber_tlv_tag_t tag,
Lev Walkinf15320b2004-06-03 03:38:44 +0000437 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin5e033762004-09-29 13:26:15 +0000438 asn_SET_specifics_t *specs = (asn_SET_specifics_t *)td->specifics;
Lev Walkinf15320b2004-06-03 03:38:44 +0000439 size_t computed_size = 0;
Lev Walkind5193802004-10-03 09:12:07 +0000440 asn_enc_rval_t er;
Lev Walkin449f8322004-08-20 13:23:42 +0000441 int t2m_build_own = (specs->tag2el_count != td->elements_count);
Wim Lewisfb6344e2014-07-28 12:16:01 -0700442 const asn_TYPE_tag2member_t *t2m;
443 asn_TYPE_tag2member_t *t2m_build;
Lev Walkinf15320b2004-06-03 03:38:44 +0000444 int t2m_count;
445 ssize_t ret;
446 int edx;
447
448 /*
449 * Use existing, or build our own tags map.
450 */
451 if(t2m_build_own) {
Wim Lewisfb6344e2014-07-28 12:16:01 -0700452 t2m_build = (asn_TYPE_tag2member_t *)alloca(
453 td->elements_count * sizeof(t2m_build[0]));
454 if(!t2m_build) _ASN_ENCODE_FAILED; /* There are such platforms */
Lev Walkinc2346572004-08-11 09:07:36 +0000455 t2m_count = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000456 } else {
Wim Lewisfb6344e2014-07-28 12:16:01 -0700457 t2m_build = NULL;
Lev Walkinf15320b2004-06-03 03:38:44 +0000458 /*
459 * There is no untagged CHOICE in this SET.
460 * Employ existing table.
461 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000462 }
463
464 /*
465 * Gather the length of the underlying members sequence.
466 */
Lev Walkin449f8322004-08-20 13:23:42 +0000467 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000468 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkind5193802004-10-03 09:12:07 +0000469 asn_enc_rval_t tmper;
Lev Walkinf15320b2004-06-03 03:38:44 +0000470 void *memb_ptr;
471
472 /*
473 * Compute the length of the encoding of this member.
474 */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000475 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800476 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000477 if(!memb_ptr) {
Lev Walkinac589332005-08-22 14:19:28 +0000478 if(!elm->optional)
479 /* Mandatory elements missing */
480 _ASN_ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000481 if(t2m_build_own) {
Wim Lewisfb6344e2014-07-28 12:16:01 -0700482 t2m_build[t2m_count].el_no = edx;
483 t2m_build[t2m_count].el_tag = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000484 t2m_count++;
485 }
486 continue;
487 }
488 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800489 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000490 }
Lev Walkind5193802004-10-03 09:12:07 +0000491 tmper = elm->type->der_encoder(elm->type, memb_ptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000492 elm->tag_mode, elm->tag,
493 0, 0);
Lev Walkind5193802004-10-03 09:12:07 +0000494 if(tmper.encoded == -1)
495 return tmper;
496 computed_size += tmper.encoded;
Lev Walkinf15320b2004-06-03 03:38:44 +0000497
498 /*
499 * Remember the outmost tag of this member.
500 */
501 if(t2m_build_own) {
Wim Lewisfb6344e2014-07-28 12:16:01 -0700502 t2m_build[t2m_count].el_no = edx;
503 t2m_build[t2m_count].el_tag = asn_TYPE_outmost_tag(
Lev Walkinf15320b2004-06-03 03:38:44 +0000504 elm->type, memb_ptr, elm->tag_mode, elm->tag);
505 t2m_count++;
506 } else {
507 /*
508 * No dynamic sorting is necessary.
509 */
510 }
511 }
512
513 /*
514 * Finalize order of the components.
515 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000516 if(t2m_build_own) {
517 /*
518 * Sort the underlying members according to their
519 * canonical tags order. DER encoding mandates it.
520 */
Wim Lewisfb6344e2014-07-28 12:16:01 -0700521 qsort(t2m_build, t2m_count, sizeof(specs->tag2el[0]), _t2e_cmp);
522 t2m = t2m_build;
Lev Walkinf15320b2004-06-03 03:38:44 +0000523 } else {
524 /*
525 * Tags are already sorted by the compiler.
526 */
Wim Lewisfb6344e2014-07-28 12:16:01 -0700527 t2m = specs->tag2el;
528 t2m_count = specs->tag2el_count;
Lev Walkinf15320b2004-06-03 03:38:44 +0000529 }
Wim Lewisfb6344e2014-07-28 12:16:01 -0700530 assert(t2m_count == td->elements_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000531
532 /*
533 * Encode the TLV for the sequence itself.
534 */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000535 ret = der_write_tags(td, computed_size, tag_mode, 1, tag, cb, app_key);
Lev Walkind5193802004-10-03 09:12:07 +0000536 if(ret == -1) _ASN_ENCODE_FAILED;
537 er.encoded = computed_size + ret;
Lev Walkinf15320b2004-06-03 03:38:44 +0000538
Lev Walkin59b176e2005-11-26 11:25:14 +0000539 if(!cb) _ASN_ENCODED_OK(er);
Lev Walkinf15320b2004-06-03 03:38:44 +0000540
541 /*
542 * Encode all members.
543 */
Lev Walkin449f8322004-08-20 13:23:42 +0000544 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000545 asn_TYPE_member_t *elm;
Lev Walkind5193802004-10-03 09:12:07 +0000546 asn_enc_rval_t tmper;
Lev Walkinf15320b2004-06-03 03:38:44 +0000547 void *memb_ptr;
548
549 /* Encode according to the tag order */
Lev Walkin449f8322004-08-20 13:23:42 +0000550 elm = &td->elements[t2m[edx].el_no];
Lev Walkinf15320b2004-06-03 03:38:44 +0000551
Lev Walkincc93b0f2004-09-10 09:18:20 +0000552 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800553 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000554 if(!memb_ptr) continue;
555 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800556 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000557 }
Lev Walkind5193802004-10-03 09:12:07 +0000558 tmper = elm->type->der_encoder(elm->type, memb_ptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000559 elm->tag_mode, elm->tag,
560 cb, app_key);
Lev Walkind5193802004-10-03 09:12:07 +0000561 if(tmper.encoded == -1)
562 return tmper;
563 computed_size -= tmper.encoded;
Lev Walkinf15320b2004-06-03 03:38:44 +0000564 }
565
566 if(computed_size != 0) {
567 /*
568 * Encoded size is not equal to the computed size.
569 */
Lev Walkind5193802004-10-03 09:12:07 +0000570 _ASN_ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000571 }
572
Lev Walkin59b176e2005-11-26 11:25:14 +0000573 _ASN_ENCODED_OK(er);
Lev Walkinf15320b2004-06-03 03:38:44 +0000574}
575
Harald Welte498c9712015-08-30 16:33:07 +0200576asn_enc_rval_t
577SET_encode_uper(asn_TYPE_descriptor_t *td,
578 asn_per_constraints_t *constraints,
579 void *sptr,
580 asn_per_outp_t *po) {
Harald Welte6e00cbc2015-08-31 09:24:42 +0200581 _ASN_ENCODE_FAILED;
Harald Welte498c9712015-08-30 16:33:07 +0200582}
583
584asn_dec_rval_t
585SET_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
586 asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
Harald Welte6e00cbc2015-08-31 09:24:42 +0200587 _ASN_DECODE_FAILED;
Harald Welte498c9712015-08-30 16:33:07 +0200588}
589
590asn_enc_rval_t
591SET_encode_aper(asn_TYPE_descriptor_t *td,
592 asn_per_constraints_t *constraints,
593 void *sptr,
594 asn_per_outp_t *po) {
595 asn_SET_specifics_t *specs = (asn_SET_specifics_t *)td->specifics;
596 asn_enc_rval_t er;
Harald Welte6e00cbc2015-08-31 09:24:42 +0200597 int edx;
Harald Welte498c9712015-08-30 16:33:07 +0200598 int t2m_build_own = (specs->tag2el_count != td->elements_count);
Harald Welte6e00cbc2015-08-31 09:24:42 +0200599 const asn_TYPE_tag2member_t *t2m;
600 asn_TYPE_tag2member_t *t2m_build;
Harald Welte498c9712015-08-30 16:33:07 +0200601 int t2m_count;
602
603 (void)constraints;
604
605 if(!sptr)
606 _ASN_ENCODE_FAILED;
607
608 er.encoded = 0;
609
610 ASN_DEBUG("Encoding %s as SET (APER) map %d", td->name, specs->_mandatory_elements[0]);
611
612 /*
613 * Use existing, or build our own tags map.
614 */
615 if(t2m_build_own) {
Harald Welte6e00cbc2015-08-31 09:24:42 +0200616 t2m_build = (asn_TYPE_tag2member_t *)alloca(
Harald Welte498c9712015-08-30 16:33:07 +0200617 td->elements_count * sizeof(t2m[0]));
Harald Welte6e00cbc2015-08-31 09:24:42 +0200618 if(!t2m_build) _ASN_ENCODE_FAILED; /* There are such platforms */
Harald Welte498c9712015-08-30 16:33:07 +0200619 t2m_count = 0;
620 } else {
Harald Welte6e00cbc2015-08-31 09:24:42 +0200621 t2m_build = NULL;
Harald Welte498c9712015-08-30 16:33:07 +0200622 /*
623 * There is no untagged CHOICE in this SET.
624 * Employ existing table.
625 */
Harald Welte498c9712015-08-30 16:33:07 +0200626 }
627
628 /*
629 * Gather the length of the underlying members sequence.
630 */
631 for(edx = 0; edx < td->elements_count; edx++) {
632 asn_TYPE_member_t *elm = &td->elements[edx];
Harald Welte498c9712015-08-30 16:33:07 +0200633 void *memb_ptr;
634
635 /*
636 * Compute the length of the encoding of this member.
637 */
638 if(elm->flags & ATF_POINTER) {
639 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
640 if(!memb_ptr) {
641 if(!elm->optional)
642 /* Mandatory elements missing */
643 _ASN_ENCODE_FAILED;
644 if(t2m_build_own) {
Harald Welte6e00cbc2015-08-31 09:24:42 +0200645 t2m_build[t2m_count].el_no = edx;
646 t2m_build[t2m_count].el_tag = 0;
Harald Welte498c9712015-08-30 16:33:07 +0200647 t2m_count++;
648 }
649 continue;
650 }
651 } else {
652 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
653 }
654
655 /*
656 * Remember the outmost tag of this member.
657 */
658 if(t2m_build_own) {
Harald Welte6e00cbc2015-08-31 09:24:42 +0200659 t2m_build[t2m_count].el_no = edx;
660 t2m_build[t2m_count].el_tag = asn_TYPE_outmost_tag(
Harald Welte498c9712015-08-30 16:33:07 +0200661 elm->type, memb_ptr, elm->tag_mode, elm->tag);
662 t2m_count++;
663 } else {
664 /*
665 * No dynamic sorting is necessary.
666 */
667 }
668 }
669
670 /*
671 * Finalize order of the components.
672 */
673 assert(t2m_count == td->elements_count);
674 if(t2m_build_own) {
675 /*
676 * Sort the underlying members according to their
677 * canonical tags order. DER encoding mandates it.
678 */
Harald Welte6e00cbc2015-08-31 09:24:42 +0200679 qsort(t2m_build, t2m_count, sizeof(specs->tag2el[0]), _t2e_cmp);
680 t2m = t2m_build;
Harald Welte498c9712015-08-30 16:33:07 +0200681 } else {
682 /*
683 * Tags are already sorted by the compiler.
684 */
Harald Welte6e00cbc2015-08-31 09:24:42 +0200685 t2m = specs->tag2el;
686 t2m_count = specs->tag2el_count;
Harald Welte498c9712015-08-30 16:33:07 +0200687 }
Harald Welte6e00cbc2015-08-31 09:24:42 +0200688 assert(t2m_count == td->elements_count);
Harald Welte498c9712015-08-30 16:33:07 +0200689
690 for(edx = 0; edx < td->elements_count; edx++) {
691 asn_TYPE_member_t *elm = &td->elements[t2m[edx].el_no];
Harald Welte6e00cbc2015-08-31 09:24:42 +0200692// asn_enc_rval_t tmper;
693// void *memb_ptr; /* Pointer to the member */
Harald Welte498c9712015-08-30 16:33:07 +0200694 void **memb_ptr2; /* Pointer to that pointer */
695 int present;
696
697 /* Fetch the pointer to this member */
698 if(elm->flags & ATF_POINTER) {
699 memb_ptr2 = (void **)((char *)sptr + elm->memb_offset);
700 present = (*memb_ptr2 != 0);
701 } else {
702// memb_ptr = (void *)((char *)sptr + elm->memb_offset);
703// memb_ptr2 = &memb_ptr;
704 present = 1;
705 continue;
706 }
707
708// /* Eliminate default values */
709// if(present && elm->default_value
710// && elm->default_value(0, memb_ptr2) == 1)
711// present = 0;
712
713 ASN_DEBUG("Element %s %s %s->%s is %s",
714 elm->flags & ATF_POINTER ? "ptr" : "inline",
715 elm->default_value ? "def" : "wtv",
716 td->name, elm->name, present ? "present" : "absent");
717 if(per_put_few_bits(po, present << 7, 8))
718 _ASN_ENCODE_FAILED;
719 }
720
721 /*
722 * Encode all members.
723 */
724 for(edx = 0; edx < td->elements_count; edx++) {
725 asn_TYPE_member_t *elm = &td->elements[edx];
726 asn_enc_rval_t tmper;
727 void *memb_ptr; /* Pointer to the member */
728 void **memb_ptr2; /* Pointer to that pointer */
729
730 /* Encode according to the tag order */
731// elm = &td->elements[t2m[edx].el_no];
732
733 if(elm->flags & ATF_POINTER) {
734 memb_ptr2 = (void **)((char *)sptr + elm->memb_offset);
735 if(!*memb_ptr2) {
736 ASN_DEBUG("Element %s %d not present",
737 elm->name, edx);
738 if(elm->optional)
739 continue;
740 /* Mandatory element is missing */
741 _ASN_ENCODE_FAILED;
742 }
743 } else {
744 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
745 memb_ptr2 = &memb_ptr;
746 }
747 tmper = elm->type->aper_encoder(elm->type, elm->per_constraints,
748 *memb_ptr2, po);
749 if(tmper.encoded == -1)
750 return tmper;
751 }
752
753 _ASN_ENCODED_OK(er);
754}
755
Lev Walkin6c0df202005-02-14 19:03:17 +0000756#undef XER_ADVANCE
757#define XER_ADVANCE(num_bytes) do { \
758 size_t num = num_bytes; \
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800759 buf_ptr = ((const char *)buf_ptr) + num;\
Lev Walkin6c0df202005-02-14 19:03:17 +0000760 size -= num; \
761 consumed_myself += num; \
762 } while(0)
763
764/*
765 * Decode the XER (XML) data.
766 */
767asn_dec_rval_t
768SET_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
769 void **struct_ptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000770 const void *buf_ptr, size_t size) {
Lev Walkin6c0df202005-02-14 19:03:17 +0000771 /*
772 * Bring closer parts of structure description.
773 */
774 asn_SET_specifics_t *specs = (asn_SET_specifics_t *)td->specifics;
775 asn_TYPE_member_t *elements = td->elements;
776 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
777
778 /*
779 * ... and parts of the structure being constructed.
780 */
781 void *st = *struct_ptr; /* Target structure. */
782 asn_struct_ctx_t *ctx; /* Decoder context */
783
784 asn_dec_rval_t rval; /* Return value from a decoder */
785 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
Lev Walkin6c0df202005-02-14 19:03:17 +0000786 int edx; /* Element index */
787
788 /*
789 * Create the target structure if it is not present already.
790 */
791 if(st == 0) {
792 st = *struct_ptr = CALLOC(1, specs->struct_size);
793 if(st == 0) RETURN(RC_FAIL);
794 }
795
796 /*
797 * Restore parsing context.
798 */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800799 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkin6c0df202005-02-14 19:03:17 +0000800
801 /*
802 * Phases of XER/XML processing:
803 * Phase 0: Check that the opening tag matches our expectations.
804 * Phase 1: Processing body and reacting on closing tag.
805 * Phase 2: Processing inner type.
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000806 * Phase 3: Skipping unknown extensions.
807 * Phase 4: PHASED OUT
Lev Walkin6c0df202005-02-14 19:03:17 +0000808 */
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000809 for(edx = ctx->step; ctx->phase <= 3;) {
Lev Walkin6c0df202005-02-14 19:03:17 +0000810 pxer_chunk_type_e ch_type; /* XER chunk type */
811 ssize_t ch_size; /* Chunk size */
812 xer_check_tag_e tcv; /* Tag check value */
813 asn_TYPE_member_t *elm;
814
815 /*
816 * Go inside the inner member of a set.
817 */
818 if(ctx->phase == 2) {
819 asn_dec_rval_t tmprval;
820 void *memb_ptr; /* Pointer to the member */
821 void **memb_ptr2; /* Pointer to that pointer */
822
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800823 if(ASN_SET_ISPRESENT2((char *)st + specs->pres_offset,
Lev Walkin6c0df202005-02-14 19:03:17 +0000824 edx)) {
825 ASN_DEBUG("SET %s: Duplicate element %s (%d)",
826 td->name, elements[edx].name, edx);
827 RETURN(RC_FAIL);
828 }
829
Lev Walkin8bb4a952005-02-14 20:15:40 +0000830 elm = &elements[edx];
Lev Walkin6c0df202005-02-14 19:03:17 +0000831
832 if(elm->flags & ATF_POINTER) {
833 /* Member is a pointer to another structure */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800834 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
Lev Walkin6c0df202005-02-14 19:03:17 +0000835 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800836 memb_ptr = (char *)st + elm->memb_offset;
Lev Walkin6c0df202005-02-14 19:03:17 +0000837 memb_ptr2 = &memb_ptr;
838 }
839
840 /* Invoke the inner type decoder, m.b. multiple times */
841 tmprval = elm->type->xer_decoder(opt_codec_ctx,
842 elm->type, memb_ptr2, elm->name,
843 buf_ptr, size);
844 XER_ADVANCE(tmprval.consumed);
845 if(tmprval.code != RC_OK)
846 RETURN(tmprval.code);
847 ctx->phase = 1; /* Back to body processing */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800848 ASN_SET_MKPRESENT((char *)st + specs->pres_offset, edx);
Lev Walkin6c0df202005-02-14 19:03:17 +0000849 ASN_DEBUG("XER/SET phase => %d", ctx->phase);
850 /* Fall through */
851 }
852
853 /*
854 * Get the next part of the XML stream.
855 */
Lev Walkin1e443962005-02-18 18:06:36 +0000856 ch_size = xer_next_token(&ctx->context,
857 buf_ptr, size, &ch_type);
Lev Walkin6c0df202005-02-14 19:03:17 +0000858 switch(ch_size) {
859 case -1: RETURN(RC_FAIL);
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000860 case 0: RETURN(RC_WMORE);
Lev Walkin6c0df202005-02-14 19:03:17 +0000861 default:
862 switch(ch_type) {
863 case PXER_COMMENT: /* Got XML comment */
864 case PXER_TEXT: /* Ignore free-standing text */
865 XER_ADVANCE(ch_size); /* Skip silently */
866 continue;
867 case PXER_TAG:
868 break; /* Check the rest down there */
869 }
870 }
871
872 tcv = xer_check_tag(buf_ptr, ch_size, xml_tag);
873 ASN_DEBUG("XER/SET: tcv = %d, ph=%d", tcv, ctx->phase);
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000874
875 /* Skip the extensions section */
876 if(ctx->phase == 3) {
877 switch(xer_skip_unknown(tcv, &ctx->left)) {
878 case -1:
879 ctx->phase = 4;
880 RETURN(RC_FAIL);
Lev Walkin1e443962005-02-18 18:06:36 +0000881 case 1:
882 ctx->phase = 1;
883 /* Fall through */
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000884 case 0:
885 XER_ADVANCE(ch_size);
886 continue;
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000887 case 2:
888 ctx->phase = 1;
889 break;
890 }
891 }
892
Lev Walkin6c0df202005-02-14 19:03:17 +0000893 switch(tcv) {
894 case XCT_CLOSING:
895 if(ctx->phase == 0) break;
896 ctx->phase = 0;
897 /* Fall through */
898 case XCT_BOTH:
899 if(ctx->phase == 0) {
900 if(_SET_is_populated(td, st)) {
901 XER_ADVANCE(ch_size);
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000902 ctx->phase = 4; /* Phase out */
Lev Walkin6c0df202005-02-14 19:03:17 +0000903 RETURN(RC_OK);
904 } else {
905 ASN_DEBUG("Premature end of XER SET");
906 RETURN(RC_FAIL);
907 }
908 }
909 /* Fall through */
910 case XCT_OPENING:
911 if(ctx->phase == 0) {
912 XER_ADVANCE(ch_size);
913 ctx->phase = 1; /* Processing body phase */
914 continue;
915 }
916 /* Fall through */
Lev Walkin904e65b2005-02-18 14:23:48 +0000917 case XCT_UNKNOWN_OP:
918 case XCT_UNKNOWN_BO:
Lev Walkin6c0df202005-02-14 19:03:17 +0000919
920 ASN_DEBUG("XER/SET: tcv=%d, ph=%d", tcv, ctx->phase);
921 if(ctx->phase != 1)
922 break; /* Really unexpected */
923
924 /*
925 * Search which member corresponds to this tag.
926 */
927 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000928 switch(xer_check_tag(buf_ptr, ch_size,
929 elements[edx].name)) {
Lev Walkin6c0df202005-02-14 19:03:17 +0000930 case XCT_BOTH:
931 case XCT_OPENING:
932 /*
933 * Process this member.
934 */
935 ctx->step = edx;
936 ctx->phase = 2;
937 break;
Lev Walkin904e65b2005-02-18 14:23:48 +0000938 case XCT_UNKNOWN_OP:
939 case XCT_UNKNOWN_BO:
Lev Walkin6c0df202005-02-14 19:03:17 +0000940 continue;
Lev Walkin6c0df202005-02-14 19:03:17 +0000941 default:
942 edx = td->elements_count;
943 break; /* Phase out */
944 }
945 break;
946 }
947 if(edx != td->elements_count)
948 continue;
Lev Walkin904e65b2005-02-18 14:23:48 +0000949
950 /* It is expected extension */
951 if(specs->extensible) {
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000952 ASN_DEBUG("Got anticipated extension");
Lev Walkin904e65b2005-02-18 14:23:48 +0000953 /*
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000954 * Check for (XCT_BOTH or XCT_UNKNOWN_BO)
955 * By using a mask. Only record a pure
956 * <opening> tags.
Lev Walkin904e65b2005-02-18 14:23:48 +0000957 */
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000958 if(tcv & XCT_CLOSING) {
959 /* Found </extension> without body */
960 } else {
961 ctx->left = 1;
962 ctx->phase = 3; /* Skip ...'s */
963 }
964 XER_ADVANCE(ch_size);
965 continue;
Lev Walkin904e65b2005-02-18 14:23:48 +0000966 }
967
Lev Walkin6c0df202005-02-14 19:03:17 +0000968 /* Fall through */
969 default:
970 break;
971 }
972
Lev Walkine0b56e02005-02-25 12:10:27 +0000973 ASN_DEBUG("Unexpected XML tag in SET, expected \"%s\"",
974 xml_tag);
Lev Walkin6c0df202005-02-14 19:03:17 +0000975 break;
976 }
977
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000978 ctx->phase = 4; /* "Phase out" on hard failure */
Lev Walkin6c0df202005-02-14 19:03:17 +0000979 RETURN(RC_FAIL);
980}
981
Lev Walkina9cc46e2004-09-22 16:06:28 +0000982asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000983SET_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000984 int ilevel, enum xer_encoder_flags_e flags,
985 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkind5193802004-10-03 09:12:07 +0000986 asn_SET_specifics_t *specs = (asn_SET_specifics_t *)td->specifics;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000987 asn_enc_rval_t er;
988 int xcan = (flags & XER_F_CANONICAL);
Wim Lewisfb6344e2014-07-28 12:16:01 -0700989 const asn_TYPE_tag2member_t *t2m = specs->tag2el_cxer;
Lev Walkind5193802004-10-03 09:12:07 +0000990 int t2m_count = specs->tag2el_cxer_count;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000991 int edx;
992
993 if(!sptr)
994 _ASN_ENCODE_FAILED;
995
Lev Walkind5193802004-10-03 09:12:07 +0000996 assert(t2m_count == td->elements_count);
997
Lev Walkina9cc46e2004-09-22 16:06:28 +0000998 er.encoded = 0;
999
Lev Walkind5193802004-10-03 09:12:07 +00001000 for(edx = 0; edx < t2m_count; edx++) {
Lev Walkina9cc46e2004-09-22 16:06:28 +00001001 asn_enc_rval_t tmper;
Lev Walkind5193802004-10-03 09:12:07 +00001002 asn_TYPE_member_t *elm;
Lev Walkina9cc46e2004-09-22 16:06:28 +00001003 void *memb_ptr;
Lev Walkind5193802004-10-03 09:12:07 +00001004 const char *mname;
1005 unsigned int mlen;
1006
1007 elm = &td->elements[t2m[edx].el_no];
1008 mname = elm->name;
1009 mlen = strlen(elm->name);
Lev Walkina9cc46e2004-09-22 16:06:28 +00001010
1011 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001012 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
Lev Walkinac589332005-08-22 14:19:28 +00001013 if(!memb_ptr) {
1014 if(elm->optional)
1015 continue;
1016 /* Mandatory element missing */
1017 _ASN_ENCODE_FAILED;
1018 }
Lev Walkina9cc46e2004-09-22 16:06:28 +00001019 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001020 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkina9cc46e2004-09-22 16:06:28 +00001021 }
1022
1023 if(!xcan)
1024 _i_ASN_TEXT_INDENT(1, ilevel);
1025 _ASN_CALLBACK3("<", 1, mname, mlen, ">", 1);
1026
1027 /* Print the member itself */
1028 tmper = elm->type->xer_encoder(elm->type, memb_ptr,
1029 ilevel + 1, flags, cb, app_key);
1030 if(tmper.encoded == -1) return tmper;
1031
1032 _ASN_CALLBACK3("</", 2, mname, mlen, ">", 1);
1033
1034 er.encoded += 5 + (2 * mlen) + tmper.encoded;
1035 }
1036
1037 if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel - 1);
1038
Lev Walkin59b176e2005-11-26 11:25:14 +00001039 _ASN_ENCODED_OK(er);
Lev Walkind5193802004-10-03 09:12:07 +00001040cb_failed:
1041 _ASN_ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +00001042}
1043
Lev Walkinf15320b2004-06-03 03:38:44 +00001044int
Lev Walkin5e033762004-09-29 13:26:15 +00001045SET_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +00001046 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinf15320b2004-06-03 03:38:44 +00001047 int edx;
1048 int ret;
1049
Lev Walkin8e8078a2004-09-26 13:10:40 +00001050 if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001051
1052 /* Dump preamble */
Lev Walkin8e8078a2004-09-26 13:10:40 +00001053 if(cb(td->name, strlen(td->name), app_key) < 0
1054 || cb(" ::= {", 6, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +00001055 return -1;
1056
Lev Walkin449f8322004-08-20 13:23:42 +00001057 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +00001058 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +00001059 const void *memb_ptr;
1060
Lev Walkincc93b0f2004-09-10 09:18:20 +00001061 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001062 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkinac589332005-08-22 14:19:28 +00001063 if(!memb_ptr) {
1064 if(elm->optional) continue;
1065 /* Print <absent> line */
1066 /* Fall through */
1067 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001068 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001069 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +00001070 }
1071
Lev Walkin8e8078a2004-09-26 13:10:40 +00001072 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +00001073
1074 /* Print the member's name and stuff */
Lev Walkin8e8078a2004-09-26 13:10:40 +00001075 if(cb(elm->name, strlen(elm->name), app_key) < 0
1076 || cb(": ", 2, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +00001077 return -1;
1078
1079 /* Print the member itself */
Lev Walkin8e8078a2004-09-26 13:10:40 +00001080 ret = elm->type->print_struct(elm->type, memb_ptr, ilevel + 1,
Lev Walkinf15320b2004-06-03 03:38:44 +00001081 cb, app_key);
1082 if(ret) return ret;
Lev Walkinf15320b2004-06-03 03:38:44 +00001083 }
1084
Lev Walkin8e8078a2004-09-26 13:10:40 +00001085 ilevel--;
1086 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +00001087
Lev Walkin8e8078a2004-09-26 13:10:40 +00001088 return (cb("}", 1, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001089}
1090
1091void
Lev Walkin5e033762004-09-29 13:26:15 +00001092SET_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
Lev Walkinf15320b2004-06-03 03:38:44 +00001093 int edx;
1094
1095 if(!td || !ptr)
1096 return;
1097
1098 ASN_DEBUG("Freeing %s as SET", td->name);
1099
Lev Walkin449f8322004-08-20 13:23:42 +00001100 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +00001101 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +00001102 void *memb_ptr;
Lev Walkincc93b0f2004-09-10 09:18:20 +00001103 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001104 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +00001105 if(memb_ptr)
Lev Walkinadcb5862006-03-17 02:11:12 +00001106 ASN_STRUCT_FREE(*elm->type, memb_ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +00001107 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001108 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
Lev Walkinadcb5862006-03-17 02:11:12 +00001109 ASN_STRUCT_FREE_CONTENTS_ONLY(*elm->type, memb_ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +00001110 }
1111 }
1112
1113 if(!contents_only) {
1114 FREEMEM(ptr);
1115 }
1116}
1117
1118int
Lev Walkin5e033762004-09-29 13:26:15 +00001119SET_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +00001120 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkinf15320b2004-06-03 03:38:44 +00001121 int edx;
1122
1123 if(!sptr) {
Lev Walkined9019a2006-10-16 12:18:41 +00001124 _ASN_CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +00001125 "%s: value not given (%s:%d)",
1126 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +00001127 return -1;
1128 }
1129
1130 /*
1131 * Iterate over structure members and check their validity.
1132 */
Lev Walkin449f8322004-08-20 13:23:42 +00001133 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +00001134 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +00001135 const void *memb_ptr;
1136
Lev Walkincc93b0f2004-09-10 09:18:20 +00001137 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001138 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +00001139 if(!memb_ptr) {
Lev Walkincc93b0f2004-09-10 09:18:20 +00001140 if(elm->optional)
1141 continue;
Lev Walkined9019a2006-10-16 12:18:41 +00001142 _ASN_CTFAIL(app_key, td, sptr,
Lev Walkincc93b0f2004-09-10 09:18:20 +00001143 "%s: mandatory element %s absent (%s:%d)",
1144 td->name, elm->name, __FILE__, __LINE__);
1145 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +00001146 }
1147 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001148 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +00001149 }
1150
Lev Walkin449f8322004-08-20 13:23:42 +00001151 if(elm->memb_constraints) {
1152 int ret = elm->memb_constraints(elm->type, memb_ptr,
Lev Walkin1eded352006-07-13 11:19:01 +00001153 ctfailcb, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +00001154 if(ret) return ret;
1155 } else {
1156 int ret = elm->type->check_constraints(elm->type,
Lev Walkin1eded352006-07-13 11:19:01 +00001157 memb_ptr, ctfailcb, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +00001158 if(ret) return ret;
1159 /*
1160 * Cannot inherit it earlier:
1161 * need to make sure we get the updated version.
1162 */
1163 elm->memb_constraints = elm->type->check_constraints;
1164 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001165 }
1166
1167 return 0;
1168}