blob: 79b3fa768248d38c4537db7a4f148ac975482913 [file] [log] [blame]
Lev Walkin62155df2017-10-21 01:16:17 -07001/*
Lev Walkine2bbbf82017-10-08 18:52:37 -07002 * Copyright (c) 2003-2017 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 */
Lev Walkin20696a42017-10-17 21:27:33 -070010static int _SET_is_populated(const asn_TYPE_descriptor_t *td, const void *st);
Lev Walkin6c0df202005-02-14 19:03:17 +000011
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 Walkin20696a42017-10-17 21:27:33 -070097SET_decode_ber(const asn_codec_ctx_t *opt_codec_ctx,
98 const asn_TYPE_descriptor_t *td, void **struct_ptr,
99 const void *ptr, size_t size, int tag_mode) {
100 /*
Lev Walkinf15320b2004-06-03 03:38:44 +0000101 * Bring closer parts of structure description.
102 */
Lev Walkind84f6032017-10-03 16:33:59 -0700103 const asn_SET_specifics_t *specs = (const asn_SET_specifics_t *)td->specifics;
Lev Walkin20696a42017-10-17 21:27:33 -0700104 const asn_TYPE_member_t *elements = td->elements;
Lev Walkinf15320b2004-06-03 03:38:44 +0000105
106 /*
107 * Parts of the structure being constructed.
108 */
109 void *st = *struct_ptr; /* Target structure. */
Lev Walkin5e033762004-09-29 13:26:15 +0000110 asn_struct_ctx_t *ctx; /* Decoder context */
Lev Walkinf15320b2004-06-03 03:38:44 +0000111
112 ber_tlv_tag_t tlv_tag; /* T from TLV */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000113 asn_dec_rval_t rval; /* Return code from subparsers */
Lev Walkinf15320b2004-06-03 03:38:44 +0000114
115 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
Lev Walkin494fb702017-08-07 20:07:00 -0700116 size_t edx; /* SET element's index */
Lev Walkinf15320b2004-06-03 03:38:44 +0000117
Lev Walkin449f8322004-08-20 13:23:42 +0000118 ASN_DEBUG("Decoding %s as SET", td->name);
Lev Walkinadf863f2006-09-05 16:18:34 +0000119
Lev Walkin7c1dc052016-03-14 03:08:15 -0700120 if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx))
121 ASN__DECODE_FAILED;
Lev Walkinadf863f2006-09-05 16:18:34 +0000122
Lev Walkinf15320b2004-06-03 03:38:44 +0000123 /*
124 * Create the target structure if it is not present already.
125 */
126 if(st == 0) {
127 st = *struct_ptr = CALLOC(1, specs->struct_size);
128 if(st == 0) {
129 RETURN(RC_FAIL);
130 }
131 }
132
133 /*
134 * Restore parsing context.
135 */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800136 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000137
138 /*
139 * Start to parse where left previously
140 */
141 switch(ctx->phase) {
142 case 0:
143 /*
144 * PHASE 0.
145 * Check that the set of tags associated with given structure
146 * perfectly fits our expectations.
147 */
148
Lev Walkin5e033762004-09-29 13:26:15 +0000149 rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size,
Lev Walkin8e8078a2004-09-26 13:10:40 +0000150 tag_mode, 1, &ctx->left, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000151 if(rval.code != RC_OK) {
152 ASN_DEBUG("%s tagging check failed: %d",
Lev Walkin449f8322004-08-20 13:23:42 +0000153 td->name, rval.code);
Lev Walkin4ab42cd2004-10-05 06:36:44 +0000154 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000155 }
156
157 if(ctx->left >= 0)
158 ctx->left += rval.consumed; /* ?Substracted below! */
159 ADVANCE(rval.consumed);
160
161 NEXT_PHASE(ctx);
162
163 ASN_DEBUG("Structure advertised %ld bytes, "
164 "buffer contains %ld", (long)ctx->left, (long)size);
165
166 /* Fall through */
167 case 1:
168 /*
169 * PHASE 1.
170 * From the place where we've left it previously,
171 * try to decode the next member from the list of
172 * this structure's elements.
Lev Walkin3e478ed2004-10-28 13:04:02 +0000173 * Note that elements in BER may arrive out of
Lev Walkinf15320b2004-06-03 03:38:44 +0000174 * order, yet DER mandates that they shall arive in the
175 * canonical order of their tags. So, there is a room
176 * for optimization.
177 */
Lev Walkin3e478ed2004-10-28 13:04:02 +0000178 for(;; ctx->step = 0) {
Wim Lewisfb6344e2014-07-28 12:16:01 -0700179 const asn_TYPE_tag2member_t *t2m;
Lev Walkin3e478ed2004-10-28 13:04:02 +0000180 asn_TYPE_tag2member_t key;
Lev Walkinf15320b2004-06-03 03:38:44 +0000181 void *memb_ptr; /* Pointer to the member */
Lev Walkinc2346572004-08-11 09:07:36 +0000182 void **memb_ptr2; /* Pointer to that pointer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000183 ssize_t tag_len; /* Length of TLV's T */
184
Lev Walkin3e478ed2004-10-28 13:04:02 +0000185 if(ctx->step & 1) {
186 edx = ctx->step >> 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000187 goto microphase2;
Lev Walkin3e478ed2004-10-28 13:04:02 +0000188 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000189
190 /*
191 * MICROPHASE 1: Synchronize decoding.
192 */
193
194 if(ctx->left == 0)
195 /*
196 * No more things to decode.
197 * Exit out of here and check whether all mandatory
198 * elements have been received (in the next phase).
199 */
200 break;
201
202 /*
203 * Fetch the T from TLV.
204 */
205 tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
206 switch(tag_len) {
207 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
208 /* Fall through */
209 case -1: RETURN(RC_FAIL);
210 }
211
Lev Walkin8c3b8542005-03-10 18:52:02 +0000212 if(ctx->left < 0 && ((const uint8_t *)ptr)[0] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000213 if(LEFT < 2) {
214 if(SIZE_VIOLATION)
215 RETURN(RC_FAIL);
216 else
217 RETURN(RC_WMORE);
Lev Walkin8c3b8542005-03-10 18:52:02 +0000218 } else if(((const uint8_t *)ptr)[1] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000219 /*
220 * Found the terminator of the
221 * indefinite length structure.
222 * Invoke the generic finalization function.
223 */
224 goto phase3;
225 }
226 }
227
Lev Walkin3e478ed2004-10-28 13:04:02 +0000228 key.el_tag = tlv_tag;
Wim Lewisfb6344e2014-07-28 12:16:01 -0700229 t2m = (const asn_TYPE_tag2member_t *)bsearch(&key,
Lev Walkin3e478ed2004-10-28 13:04:02 +0000230 specs->tag2el, specs->tag2el_count,
231 sizeof(specs->tag2el[0]), _t2e_cmp);
232 if(t2m) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000233 /*
Lev Walkin3e478ed2004-10-28 13:04:02 +0000234 * Found the element corresponding to the tag.
Lev Walkinf15320b2004-06-03 03:38:44 +0000235 */
Lev Walkin3e478ed2004-10-28 13:04:02 +0000236 edx = t2m->el_no;
237 ctx->step = (edx << 1) + 1;
Lev Walkin9f470d62017-10-21 16:27:08 -0700238 ASN_DEBUG("Got tag %s (%s), edx %" ASN_PRI_SSIZE "",
Lev Walkin3e478ed2004-10-28 13:04:02 +0000239 ber_tlv_tag_string(tlv_tag), td->name, edx);
240 } else if(specs->extensible == 0) {
241 ASN_DEBUG("Unexpected tag %s "
242 "in non-extensible SET %s",
243 ber_tlv_tag_string(tlv_tag), td->name);
244 RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000245 } else {
Lev Walkin3e478ed2004-10-28 13:04:02 +0000246 /* Skip this tag */
247 ssize_t skip;
Lev Walkinf15320b2004-06-03 03:38:44 +0000248
Lev Walkin3e478ed2004-10-28 13:04:02 +0000249 ASN_DEBUG("Skipping unknown tag %s",
250 ber_tlv_tag_string(tlv_tag));
Lev Walkinf15320b2004-06-03 03:38:44 +0000251
Lev Walkin3e478ed2004-10-28 13:04:02 +0000252 skip = ber_skip_length(opt_codec_ctx,
253 BER_TLV_CONSTRUCTED(ptr),
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800254 (const char *)ptr + tag_len, LEFT - tag_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000255
Lev Walkin3e478ed2004-10-28 13:04:02 +0000256 switch(skip) {
257 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
258 /* Fall through */
259 case -1: RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000260 }
Lev Walkin3e478ed2004-10-28 13:04:02 +0000261
262 ADVANCE(skip + tag_len);
263 continue; /* Try again with the next tag */
Lev Walkinf15320b2004-06-03 03:38:44 +0000264 }
265
266 /*
267 * MICROPHASE 2: Invoke the member-specific decoder.
268 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000269 microphase2:
270
271 /*
272 * Check for duplications: must not overwrite
273 * already decoded elements.
274 */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800275 if(ASN_SET_ISPRESENT2((char *)st + specs->pres_offset, edx)) {
Lev Walkin9f470d62017-10-21 16:27:08 -0700276 ASN_DEBUG("SET %s: Duplicate element %s (%" ASN_PRI_SSIZE ")",
Lev Walkin449f8322004-08-20 13:23:42 +0000277 td->name, elements[edx].name, edx);
Lev Walkinf15320b2004-06-03 03:38:44 +0000278 RETURN(RC_FAIL);
279 }
280
281 /*
282 * Compute the position of the member inside a structure,
283 * and also a type of containment (it may be contained
284 * as pointer or using inline inclusion).
285 */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000286 if(elements[edx].flags & ATF_POINTER) {
287 /* Member is a pointer to another structure */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800288 memb_ptr2 = (void **)((char *)st + elements[edx].memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000289 } else {
290 /*
291 * A pointer to a pointer
292 * holding the start of the structure
293 */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800294 memb_ptr = (char *)st + elements[edx].memb_offset;
Lev Walkinf15320b2004-06-03 03:38:44 +0000295 memb_ptr2 = &memb_ptr;
296 }
297 /*
298 * Invoke the member fetch routine according to member's type
299 */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800300 rval = elements[edx].type->op->ber_decoder(opt_codec_ctx,
Lev Walkinc2346572004-08-11 09:07:36 +0000301 elements[edx].type,
Lev Walkinf15320b2004-06-03 03:38:44 +0000302 memb_ptr2, ptr, LEFT,
303 elements[edx].tag_mode);
304 switch(rval.code) {
305 case RC_OK:
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800306 ASN_SET_MKPRESENT((char *)st + specs->pres_offset, edx);
Lev Walkinf15320b2004-06-03 03:38:44 +0000307 break;
308 case RC_WMORE: /* More data expected */
309 if(!SIZE_VIOLATION) {
310 ADVANCE(rval.consumed);
311 RETURN(RC_WMORE);
312 }
Lev Walkin48e82d12017-10-19 03:06:35 -0700313 /* Fall through */
Lev Walkinf15320b2004-06-03 03:38:44 +0000314 case RC_FAIL: /* Fatal error */
315 RETURN(RC_FAIL);
316 } /* switch(rval) */
317
318 ADVANCE(rval.consumed);
319 } /* for(all structure members) */
320
321 phase3:
322 ctx->phase = 3;
323 /* Fall through */
324 case 3:
325 case 4: /* Only 00 is expected */
326 ASN_DEBUG("SET %s Leftover: %ld, size = %ld",
Lev Walkin449f8322004-08-20 13:23:42 +0000327 td->name, (long)ctx->left, (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000328
329 /*
330 * Skip everything until the end of the SET.
331 */
332 while(ctx->left) {
333 ssize_t tl, ll;
334
335 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
336 switch(tl) {
337 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
338 /* Fall through */
339 case -1: RETURN(RC_FAIL);
340 }
341
342 /*
343 * If expected <0><0>...
344 */
345 if(ctx->left < 0
Lev Walkin8c3b8542005-03-10 18:52:02 +0000346 && ((const uint8_t *)ptr)[0] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000347 if(LEFT < 2) {
348 if(SIZE_VIOLATION)
349 RETURN(RC_FAIL);
350 else
351 RETURN(RC_WMORE);
Lev Walkin8c3b8542005-03-10 18:52:02 +0000352 } else if(((const uint8_t *)ptr)[1] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000353 /*
354 * Correctly finished with <0><0>.
355 */
356 ADVANCE(2);
357 ctx->left++;
358 ctx->phase = 4;
359 continue;
360 }
361 }
362
363 if(specs->extensible == 0 || ctx->phase == 4) {
364 ASN_DEBUG("Unexpected continuation "
Lev Walkin3e478ed2004-10-28 13:04:02 +0000365 "of a non-extensible type %s "
366 "(ptr=%02x)",
Lev Walkin8c3b8542005-03-10 18:52:02 +0000367 td->name, *(const uint8_t *)ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +0000368 RETURN(RC_FAIL);
369 }
370
Lev Walkinbaaa24f2004-09-29 14:19:14 +0000371 ll = ber_skip_length(opt_codec_ctx,
Lev Walkinf15320b2004-06-03 03:38:44 +0000372 BER_TLV_CONSTRUCTED(ptr),
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800373 (const char *)ptr + tl, LEFT - tl);
Lev Walkinf15320b2004-06-03 03:38:44 +0000374 switch(ll) {
375 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
376 /* Fall through */
377 case -1: RETURN(RC_FAIL);
378 }
379
380 ADVANCE(tl + ll);
381 }
382
383 ctx->phase = 5;
Lev Walkin48e82d12017-10-19 03:06:35 -0700384 /* Fall through */
Lev Walkinf15320b2004-06-03 03:38:44 +0000385 case 5:
Lev Walkin6c0df202005-02-14 19:03:17 +0000386 /* Check that all mandatory elements are present. */
387 if(!_SET_is_populated(td, st))
388 RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000389
390 NEXT_PHASE(ctx);
391 }
392
393 RETURN(RC_OK);
394}
395
Lev Walkin6c0df202005-02-14 19:03:17 +0000396static int
Lev Walkin20696a42017-10-17 21:27:33 -0700397_SET_is_populated(const asn_TYPE_descriptor_t *td, const void *st) {
398 const asn_SET_specifics_t *specs = (const asn_SET_specifics_t *)td->specifics;
Lev Walkin494fb702017-08-07 20:07:00 -0700399 size_t edx;
Lev Walkin6c0df202005-02-14 19:03:17 +0000400
401 /*
402 * Check that all mandatory elements are present.
403 */
404 for(edx = 0; edx < td->elements_count;
405 edx += (8 * sizeof(specs->_mandatory_elements[0]))) {
406 unsigned int midx, pres, must;
407
408 midx = edx/(8 * sizeof(specs->_mandatory_elements[0]));
Lev Walkin20696a42017-10-17 21:27:33 -0700409 pres = ((const unsigned int *)((const char *)st
410 + specs->pres_offset))[midx];
411 must = sys_ntohl(specs->_mandatory_elements[midx]);
Lev Walkin6c0df202005-02-14 19:03:17 +0000412
413 if((pres & must) == must) {
414 /*
415 * Yes, everything seems to be in place.
416 */
417 } else {
418 ASN_DEBUG("One or more mandatory elements "
419 "of a SET %s %d (%08x.%08x)=%08x "
420 "are not present",
421 td->name,
422 midx,
423 pres,
424 must,
425 (~(pres & must) & must)
426 );
427 return 0;
428 }
429 }
430
431 return 1;
432}
433
Lev Walkinf15320b2004-06-03 03:38:44 +0000434/*
435 * The DER encoder of the SET type.
436 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000437asn_enc_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -0700438SET_encode_der(const asn_TYPE_descriptor_t *td, const void *sptr, int tag_mode,
439 ber_tlv_tag_t tag, asn_app_consume_bytes_f *cb, void *app_key) {
440 const asn_SET_specifics_t *specs = (const asn_SET_specifics_t *)td->specifics;
Lev Walkinf15320b2004-06-03 03:38:44 +0000441 size_t computed_size = 0;
Lev Walkind5193802004-10-03 09:12:07 +0000442 asn_enc_rval_t er;
Lev Walkin449f8322004-08-20 13:23:42 +0000443 int t2m_build_own = (specs->tag2el_count != td->elements_count);
Wim Lewisfb6344e2014-07-28 12:16:01 -0700444 const asn_TYPE_tag2member_t *t2m;
445 asn_TYPE_tag2member_t *t2m_build;
Lev Walkin494fb702017-08-07 20:07:00 -0700446 size_t t2m_count;
Lev Walkinf15320b2004-06-03 03:38:44 +0000447 ssize_t ret;
Lev Walkin494fb702017-08-07 20:07:00 -0700448 size_t edx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000449
450 /*
451 * Use existing, or build our own tags map.
452 */
453 if(t2m_build_own) {
Lev Walkine2bbbf82017-10-08 18:52:37 -0700454 t2m_build = (asn_TYPE_tag2member_t *)CALLOC(td->elements_count,
455 sizeof(t2m_build[0]));
456 if(!t2m_build) ASN__ENCODE_FAILED;
Lev Walkinc2346572004-08-11 09:07:36 +0000457 t2m_count = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000458 } else {
Wim Lewisfb6344e2014-07-28 12:16:01 -0700459 t2m_build = NULL;
Lev Walkinf15320b2004-06-03 03:38:44 +0000460 /*
461 * There is no untagged CHOICE in this SET.
462 * Employ existing table.
463 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000464 }
465
466 /*
467 * Gather the length of the underlying members sequence.
468 */
Lev Walkin449f8322004-08-20 13:23:42 +0000469 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000470 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkind5193802004-10-03 09:12:07 +0000471 asn_enc_rval_t tmper;
Lev Walkin20696a42017-10-17 21:27:33 -0700472 const void *memb_ptr_dontuse; /* Pointer to the member */
473 const void *const *memb_ptr2; /* Pointer to that pointer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000474
Lev Walkin20696a42017-10-17 21:27:33 -0700475 /*
Lev Walkinf15320b2004-06-03 03:38:44 +0000476 * Compute the length of the encoding of this member.
477 */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000478 if(elm->flags & ATF_POINTER) {
Lev Walkin20696a42017-10-17 21:27:33 -0700479 memb_ptr2 =
480 (const void *const *)((const char *)sptr + elm->memb_offset);
481 if(!*memb_ptr2) {
Lev Walkine2bbbf82017-10-08 18:52:37 -0700482 if(!elm->optional) {
Lev Walkinac589332005-08-22 14:19:28 +0000483 /* Mandatory elements missing */
Lev Walkine2bbbf82017-10-08 18:52:37 -0700484 FREEMEM(t2m_build);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700485 ASN__ENCODE_FAILED;
Lev Walkine2bbbf82017-10-08 18:52:37 -0700486 }
487 if(t2m_build) {
Wim Lewisfb6344e2014-07-28 12:16:01 -0700488 t2m_build[t2m_count].el_no = edx;
489 t2m_build[t2m_count].el_tag = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000490 t2m_count++;
491 }
492 continue;
493 }
494 } else {
Lev Walkin20696a42017-10-17 21:27:33 -0700495 memb_ptr_dontuse =
496 (const void *)((const char *)sptr + elm->memb_offset);
497 memb_ptr2 = &memb_ptr_dontuse; /* Only use of memb_ptr_dontuse */
Lev Walkinf15320b2004-06-03 03:38:44 +0000498 }
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400499
500 /* Eliminate default values */
Lev Walkin20696a42017-10-17 21:27:33 -0700501 if(elm->default_value_cmp && elm->default_value_cmp(*memb_ptr2) == 0) {
502 if(t2m_build) {
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400503 t2m_build[t2m_count].el_no = edx;
504 t2m_build[t2m_count].el_tag = 0;
505 t2m_count++;
506 }
507 continue;
508 }
509
510 tmper = elm->type->op->der_encoder(elm->type, *memb_ptr2,
Lev Walkinf15320b2004-06-03 03:38:44 +0000511 elm->tag_mode, elm->tag,
512 0, 0);
Lev Walkind5193802004-10-03 09:12:07 +0000513 if(tmper.encoded == -1)
514 return tmper;
515 computed_size += tmper.encoded;
Lev Walkinf15320b2004-06-03 03:38:44 +0000516
517 /*
518 * Remember the outmost tag of this member.
519 */
Lev Walkine2bbbf82017-10-08 18:52:37 -0700520 if(t2m_build) {
Wim Lewisfb6344e2014-07-28 12:16:01 -0700521 t2m_build[t2m_count].el_no = edx;
522 t2m_build[t2m_count].el_tag = asn_TYPE_outmost_tag(
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400523 elm->type, *memb_ptr2, elm->tag_mode, elm->tag);
Lev Walkinf15320b2004-06-03 03:38:44 +0000524 t2m_count++;
525 } else {
526 /*
527 * No dynamic sorting is necessary.
528 */
529 }
530 }
531
532 /*
533 * Finalize order of the components.
534 */
Lev Walkine2bbbf82017-10-08 18:52:37 -0700535 if(t2m_build) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000536 /*
537 * Sort the underlying members according to their
538 * canonical tags order. DER encoding mandates it.
539 */
Wim Lewisfb6344e2014-07-28 12:16:01 -0700540 qsort(t2m_build, t2m_count, sizeof(specs->tag2el[0]), _t2e_cmp);
541 t2m = t2m_build;
Lev Walkinf15320b2004-06-03 03:38:44 +0000542 } else {
543 /*
544 * Tags are already sorted by the compiler.
545 */
Wim Lewisfb6344e2014-07-28 12:16:01 -0700546 t2m = specs->tag2el;
547 t2m_count = specs->tag2el_count;
Lev Walkinf15320b2004-06-03 03:38:44 +0000548 }
Wim Lewisfb6344e2014-07-28 12:16:01 -0700549 assert(t2m_count == td->elements_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000550
551 /*
552 * Encode the TLV for the sequence itself.
553 */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000554 ret = der_write_tags(td, computed_size, tag_mode, 1, tag, cb, app_key);
Lev Walkine2bbbf82017-10-08 18:52:37 -0700555 if(ret == -1) {
556 FREEMEM(t2m_build);
557 ASN__ENCODE_FAILED;
558 }
Lev Walkind5193802004-10-03 09:12:07 +0000559 er.encoded = computed_size + ret;
Lev Walkinf15320b2004-06-03 03:38:44 +0000560
Lev Walkine2bbbf82017-10-08 18:52:37 -0700561 if(!cb) {
562 FREEMEM(t2m_build);
563 ASN__ENCODED_OK(er);
564 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000565
566 /*
567 * Encode all members.
568 */
Lev Walkin449f8322004-08-20 13:23:42 +0000569 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000570 asn_TYPE_member_t *elm;
Lev Walkind5193802004-10-03 09:12:07 +0000571 asn_enc_rval_t tmper;
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400572
Lev Walkin20696a42017-10-17 21:27:33 -0700573 const void *memb_ptr_dontuse; /* Pointer to the member */
574 const void *const *memb_ptr2; /* Pointer to that pointer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000575
Lev Walkin20696a42017-10-17 21:27:33 -0700576 /* Encode according to the tag order */
Lev Walkin449f8322004-08-20 13:23:42 +0000577 elm = &td->elements[t2m[edx].el_no];
Lev Walkinf15320b2004-06-03 03:38:44 +0000578
Lev Walkincc93b0f2004-09-10 09:18:20 +0000579 if(elm->flags & ATF_POINTER) {
Lev Walkin20696a42017-10-17 21:27:33 -0700580 memb_ptr2 =
581 (const void *const *)((const char *)sptr + elm->memb_offset);
582 if(!*memb_ptr2) continue;
Lev Walkinf15320b2004-06-03 03:38:44 +0000583 } else {
Lev Walkin20696a42017-10-17 21:27:33 -0700584 memb_ptr_dontuse =
585 (const void *)((const char *)sptr + elm->memb_offset);
586 memb_ptr2 = &memb_ptr_dontuse; /* Only use of memb_ptr_dontuse */
Lev Walkinf15320b2004-06-03 03:38:44 +0000587 }
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400588
589 /* Eliminate default values */
Lev Walkin20696a42017-10-17 21:27:33 -0700590 if(elm->default_value_cmp && elm->default_value_cmp(*memb_ptr2) == 0)
591 continue;
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400592
593 tmper = elm->type->op->der_encoder(elm->type, *memb_ptr2,
594 elm->tag_mode, elm->tag, cb, app_key);
Lev Walkind5193802004-10-03 09:12:07 +0000595 if(tmper.encoded == -1)
596 return tmper;
597 computed_size -= tmper.encoded;
Lev Walkinf15320b2004-06-03 03:38:44 +0000598 }
599
600 if(computed_size != 0) {
601 /*
602 * Encoded size is not equal to the computed size.
603 */
Lev Walkine2bbbf82017-10-08 18:52:37 -0700604 FREEMEM(t2m_build);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700605 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000606 }
607
Lev Walkine2bbbf82017-10-08 18:52:37 -0700608 FREEMEM(t2m_build);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700609 ASN__ENCODED_OK(er);
Lev Walkinf15320b2004-06-03 03:38:44 +0000610}
611
Lev Walkin6c0df202005-02-14 19:03:17 +0000612#undef XER_ADVANCE
613#define XER_ADVANCE(num_bytes) do { \
614 size_t num = num_bytes; \
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800615 buf_ptr = ((const char *)buf_ptr) + num;\
Lev Walkin6c0df202005-02-14 19:03:17 +0000616 size -= num; \
617 consumed_myself += num; \
618 } while(0)
619
620/*
621 * Decode the XER (XML) data.
622 */
623asn_dec_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -0700624SET_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
625 const asn_TYPE_descriptor_t *td, void **struct_ptr,
626 const char *opt_mname, const void *buf_ptr, size_t size) {
627 /*
Lev Walkin6c0df202005-02-14 19:03:17 +0000628 * Bring closer parts of structure description.
629 */
Lev Walkind84f6032017-10-03 16:33:59 -0700630 const asn_SET_specifics_t *specs = (const asn_SET_specifics_t *)td->specifics;
Lev Walkin20696a42017-10-17 21:27:33 -0700631 const asn_TYPE_member_t *elements = td->elements;
Lev Walkin6c0df202005-02-14 19:03:17 +0000632 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
633
634 /*
635 * ... and parts of the structure being constructed.
636 */
637 void *st = *struct_ptr; /* Target structure. */
638 asn_struct_ctx_t *ctx; /* Decoder context */
639
640 asn_dec_rval_t rval; /* Return value from a decoder */
641 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
Lev Walkin494fb702017-08-07 20:07:00 -0700642 size_t edx; /* Element index */
Lev Walkin6c0df202005-02-14 19:03:17 +0000643
644 /*
645 * Create the target structure if it is not present already.
646 */
647 if(st == 0) {
648 st = *struct_ptr = CALLOC(1, specs->struct_size);
649 if(st == 0) RETURN(RC_FAIL);
650 }
651
652 /*
653 * Restore parsing context.
654 */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800655 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkin6c0df202005-02-14 19:03:17 +0000656
657 /*
658 * Phases of XER/XML processing:
659 * Phase 0: Check that the opening tag matches our expectations.
660 * Phase 1: Processing body and reacting on closing tag.
661 * Phase 2: Processing inner type.
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000662 * Phase 3: Skipping unknown extensions.
663 * Phase 4: PHASED OUT
Lev Walkin6c0df202005-02-14 19:03:17 +0000664 */
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000665 for(edx = ctx->step; ctx->phase <= 3;) {
Lev Walkin6c0df202005-02-14 19:03:17 +0000666 pxer_chunk_type_e ch_type; /* XER chunk type */
667 ssize_t ch_size; /* Chunk size */
668 xer_check_tag_e tcv; /* Tag check value */
Lev Walkin20696a42017-10-17 21:27:33 -0700669 const asn_TYPE_member_t *elm;
Lev Walkin6c0df202005-02-14 19:03:17 +0000670
671 /*
672 * Go inside the inner member of a set.
673 */
674 if(ctx->phase == 2) {
675 asn_dec_rval_t tmprval;
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400676 void *memb_ptr_dontuse; /* Pointer to the member */
Lev Walkin6c0df202005-02-14 19:03:17 +0000677 void **memb_ptr2; /* Pointer to that pointer */
678
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800679 if(ASN_SET_ISPRESENT2((char *)st + specs->pres_offset,
Lev Walkin6c0df202005-02-14 19:03:17 +0000680 edx)) {
Lev Walkin9f470d62017-10-21 16:27:08 -0700681 ASN_DEBUG("SET %s: Duplicate element %s (%" ASN_PRI_SSIZE ")",
Lev Walkin6c0df202005-02-14 19:03:17 +0000682 td->name, elements[edx].name, edx);
683 RETURN(RC_FAIL);
684 }
685
Lev Walkin8bb4a952005-02-14 20:15:40 +0000686 elm = &elements[edx];
Lev Walkin6c0df202005-02-14 19:03:17 +0000687
688 if(elm->flags & ATF_POINTER) {
689 /* Member is a pointer to another structure */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800690 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
Lev Walkin6c0df202005-02-14 19:03:17 +0000691 } else {
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400692 memb_ptr_dontuse = (char *)st + elm->memb_offset;
693 memb_ptr2 = &memb_ptr_dontuse; /* Only use of memb_ptr_dontuse */
Lev Walkin6c0df202005-02-14 19:03:17 +0000694 }
695
696 /* Invoke the inner type decoder, m.b. multiple times */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800697 tmprval = elm->type->op->xer_decoder(opt_codec_ctx,
Lev Walkin6c0df202005-02-14 19:03:17 +0000698 elm->type, memb_ptr2, elm->name,
699 buf_ptr, size);
700 XER_ADVANCE(tmprval.consumed);
701 if(tmprval.code != RC_OK)
702 RETURN(tmprval.code);
703 ctx->phase = 1; /* Back to body processing */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800704 ASN_SET_MKPRESENT((char *)st + specs->pres_offset, edx);
Lev Walkin6c0df202005-02-14 19:03:17 +0000705 ASN_DEBUG("XER/SET phase => %d", ctx->phase);
706 /* Fall through */
707 }
708
709 /*
710 * Get the next part of the XML stream.
711 */
Lev Walkin1e443962005-02-18 18:06:36 +0000712 ch_size = xer_next_token(&ctx->context,
713 buf_ptr, size, &ch_type);
Lev Walkin97363482016-01-24 19:23:02 -0800714 if(ch_size == -1) {
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400715 RETURN(RC_FAIL);
716 } else {
Lev Walkin6c0df202005-02-14 19:03:17 +0000717 switch(ch_type) {
Lev Walkin97363482016-01-24 19:23:02 -0800718 case PXER_WMORE:
719 RETURN(RC_WMORE);
Lev Walkin6c0df202005-02-14 19:03:17 +0000720 case PXER_COMMENT: /* Got XML comment */
721 case PXER_TEXT: /* Ignore free-standing text */
722 XER_ADVANCE(ch_size); /* Skip silently */
723 continue;
724 case PXER_TAG:
725 break; /* Check the rest down there */
726 }
727 }
728
729 tcv = xer_check_tag(buf_ptr, ch_size, xml_tag);
730 ASN_DEBUG("XER/SET: tcv = %d, ph=%d", tcv, ctx->phase);
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000731
732 /* Skip the extensions section */
733 if(ctx->phase == 3) {
734 switch(xer_skip_unknown(tcv, &ctx->left)) {
735 case -1:
736 ctx->phase = 4;
737 RETURN(RC_FAIL);
Lev Walkin1e443962005-02-18 18:06:36 +0000738 case 1:
739 ctx->phase = 1;
740 /* Fall through */
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000741 case 0:
742 XER_ADVANCE(ch_size);
743 continue;
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000744 case 2:
745 ctx->phase = 1;
746 break;
747 }
748 }
749
Lev Walkin6c0df202005-02-14 19:03:17 +0000750 switch(tcv) {
751 case XCT_CLOSING:
752 if(ctx->phase == 0) break;
753 ctx->phase = 0;
754 /* Fall through */
755 case XCT_BOTH:
756 if(ctx->phase == 0) {
757 if(_SET_is_populated(td, st)) {
758 XER_ADVANCE(ch_size);
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000759 ctx->phase = 4; /* Phase out */
Lev Walkin6c0df202005-02-14 19:03:17 +0000760 RETURN(RC_OK);
761 } else {
762 ASN_DEBUG("Premature end of XER SET");
763 RETURN(RC_FAIL);
764 }
765 }
766 /* Fall through */
767 case XCT_OPENING:
768 if(ctx->phase == 0) {
769 XER_ADVANCE(ch_size);
770 ctx->phase = 1; /* Processing body phase */
771 continue;
772 }
773 /* Fall through */
Lev Walkin904e65b2005-02-18 14:23:48 +0000774 case XCT_UNKNOWN_OP:
775 case XCT_UNKNOWN_BO:
Lev Walkin6c0df202005-02-14 19:03:17 +0000776
777 ASN_DEBUG("XER/SET: tcv=%d, ph=%d", tcv, ctx->phase);
778 if(ctx->phase != 1)
779 break; /* Really unexpected */
780
781 /*
782 * Search which member corresponds to this tag.
783 */
784 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000785 switch(xer_check_tag(buf_ptr, ch_size,
786 elements[edx].name)) {
Lev Walkin6c0df202005-02-14 19:03:17 +0000787 case XCT_BOTH:
788 case XCT_OPENING:
789 /*
790 * Process this member.
791 */
792 ctx->step = edx;
793 ctx->phase = 2;
794 break;
Lev Walkin904e65b2005-02-18 14:23:48 +0000795 case XCT_UNKNOWN_OP:
796 case XCT_UNKNOWN_BO:
Lev Walkin6c0df202005-02-14 19:03:17 +0000797 continue;
Lev Walkin6c0df202005-02-14 19:03:17 +0000798 default:
799 edx = td->elements_count;
800 break; /* Phase out */
801 }
802 break;
803 }
804 if(edx != td->elements_count)
805 continue;
Lev Walkin904e65b2005-02-18 14:23:48 +0000806
807 /* It is expected extension */
808 if(specs->extensible) {
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000809 ASN_DEBUG("Got anticipated extension");
Lev Walkin904e65b2005-02-18 14:23:48 +0000810 /*
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000811 * Check for (XCT_BOTH or XCT_UNKNOWN_BO)
812 * By using a mask. Only record a pure
813 * <opening> tags.
Lev Walkin904e65b2005-02-18 14:23:48 +0000814 */
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000815 if(tcv & XCT_CLOSING) {
816 /* Found </extension> without body */
817 } else {
818 ctx->left = 1;
819 ctx->phase = 3; /* Skip ...'s */
820 }
821 XER_ADVANCE(ch_size);
822 continue;
Lev Walkin904e65b2005-02-18 14:23:48 +0000823 }
824
Lev Walkin6c0df202005-02-14 19:03:17 +0000825 /* Fall through */
826 default:
827 break;
828 }
829
Lev Walkine0b56e02005-02-25 12:10:27 +0000830 ASN_DEBUG("Unexpected XML tag in SET, expected \"%s\"",
831 xml_tag);
Lev Walkin6c0df202005-02-14 19:03:17 +0000832 break;
833 }
834
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000835 ctx->phase = 4; /* "Phase out" on hard failure */
Lev Walkin6c0df202005-02-14 19:03:17 +0000836 RETURN(RC_FAIL);
837}
838
Lev Walkina9cc46e2004-09-22 16:06:28 +0000839asn_enc_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -0700840SET_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
841 enum xer_encoder_flags_e flags, asn_app_consume_bytes_f *cb,
842 void *app_key) {
843 const asn_SET_specifics_t *specs = (const asn_SET_specifics_t *)td->specifics;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000844 asn_enc_rval_t er;
845 int xcan = (flags & XER_F_CANONICAL);
Wim Lewisfb6344e2014-07-28 12:16:01 -0700846 const asn_TYPE_tag2member_t *t2m = specs->tag2el_cxer;
Lev Walkin494fb702017-08-07 20:07:00 -0700847 size_t t2m_count = specs->tag2el_cxer_count;
848 size_t edx;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000849
850 if(!sptr)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700851 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000852
Lev Walkind5193802004-10-03 09:12:07 +0000853 assert(t2m_count == td->elements_count);
854
Lev Walkina9cc46e2004-09-22 16:06:28 +0000855 er.encoded = 0;
856
Lev Walkind5193802004-10-03 09:12:07 +0000857 for(edx = 0; edx < t2m_count; edx++) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000858 asn_enc_rval_t tmper;
Lev Walkind5193802004-10-03 09:12:07 +0000859 asn_TYPE_member_t *elm;
Lev Walkin20696a42017-10-17 21:27:33 -0700860 const void *memb_ptr;
861 const char *mname;
862 size_t mlen;
Lev Walkind5193802004-10-03 09:12:07 +0000863
864 elm = &td->elements[t2m[edx].el_no];
865 mname = elm->name;
866 mlen = strlen(elm->name);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000867
868 if(elm->flags & ATF_POINTER) {
Lev Walkin20696a42017-10-17 21:27:33 -0700869 memb_ptr =
870 *(const void *const *)((const char *)sptr + elm->memb_offset);
871 if(!memb_ptr) {
Lev Walkinac589332005-08-22 14:19:28 +0000872 if(elm->optional)
873 continue;
874 /* Mandatory element missing */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700875 ASN__ENCODE_FAILED;
Lev Walkinac589332005-08-22 14:19:28 +0000876 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000877 } else {
Lev Walkin20696a42017-10-17 21:27:33 -0700878 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
879 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000880
881 if(!xcan)
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700882 ASN__TEXT_INDENT(1, ilevel);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700883 ASN__CALLBACK3("<", 1, mname, mlen, ">", 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000884
885 /* Print the member itself */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800886 tmper = elm->type->op->xer_encoder(elm->type, memb_ptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000887 ilevel + 1, flags, cb, app_key);
888 if(tmper.encoded == -1) return tmper;
Lev Walkin4fe28822017-09-18 02:57:34 -0700889 er.encoded += tmper.encoded;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000890
Lev Walkin7c1dc052016-03-14 03:08:15 -0700891 ASN__CALLBACK3("</", 2, mname, mlen, ">", 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000892 }
893
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700894 if(!xcan) ASN__TEXT_INDENT(1, ilevel - 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000895
Lev Walkin7c1dc052016-03-14 03:08:15 -0700896 ASN__ENCODED_OK(er);
Lev Walkind5193802004-10-03 09:12:07 +0000897cb_failed:
Lev Walkin7c1dc052016-03-14 03:08:15 -0700898 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000899}
900
Lev Walkinf15320b2004-06-03 03:38:44 +0000901int
Lev Walkin20696a42017-10-17 21:27:33 -0700902SET_print(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
903 asn_app_consume_bytes_f *cb, void *app_key) {
904 size_t edx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000905 int ret;
906
Lev Walkin8e8078a2004-09-26 13:10:40 +0000907 if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000908
909 /* Dump preamble */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000910 if(cb(td->name, strlen(td->name), app_key) < 0
911 || cb(" ::= {", 6, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000912 return -1;
913
Lev Walkin449f8322004-08-20 13:23:42 +0000914 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000915 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000916 const void *memb_ptr;
917
Lev Walkincc93b0f2004-09-10 09:18:20 +0000918 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800919 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkinac589332005-08-22 14:19:28 +0000920 if(!memb_ptr) {
921 if(elm->optional) continue;
922 /* Print <absent> line */
923 /* Fall through */
924 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000925 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800926 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000927 }
928
Lev Walkin8e8078a2004-09-26 13:10:40 +0000929 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000930
931 /* Print the member's name and stuff */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000932 if(cb(elm->name, strlen(elm->name), app_key) < 0
933 || cb(": ", 2, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000934 return -1;
935
936 /* Print the member itself */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800937 ret = elm->type->op->print_struct(elm->type, memb_ptr, ilevel + 1,
Lev Walkinf15320b2004-06-03 03:38:44 +0000938 cb, app_key);
939 if(ret) return ret;
Lev Walkinf15320b2004-06-03 03:38:44 +0000940 }
941
Lev Walkin8e8078a2004-09-26 13:10:40 +0000942 ilevel--;
943 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000944
Lev Walkin8e8078a2004-09-26 13:10:40 +0000945 return (cb("}", 1, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000946}
947
948void
Lev Walkin8d99d7b2017-08-25 01:06:00 -0700949SET_free(const asn_TYPE_descriptor_t *td, void *ptr,
950 enum asn_struct_free_method method) {
951 size_t edx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000952
953 if(!td || !ptr)
954 return;
955
956 ASN_DEBUG("Freeing %s as SET", td->name);
957
Lev Walkin449f8322004-08-20 13:23:42 +0000958 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000959 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000960 void *memb_ptr;
Lev Walkincc93b0f2004-09-10 09:18:20 +0000961 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800962 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000963 if(memb_ptr)
Lev Walkinadcb5862006-03-17 02:11:12 +0000964 ASN_STRUCT_FREE(*elm->type, memb_ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +0000965 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800966 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
Lev Walkinadcb5862006-03-17 02:11:12 +0000967 ASN_STRUCT_FREE_CONTENTS_ONLY(*elm->type, memb_ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +0000968 }
969 }
970
Lev Walkin8d99d7b2017-08-25 01:06:00 -0700971 switch(method) {
972 case ASFM_FREE_EVERYTHING:
973 FREEMEM(ptr);
974 break;
975 case ASFM_FREE_UNDERLYING:
976 break;
977 case ASFM_FREE_UNDERLYING_AND_RESET:
Lev Walkind84f6032017-10-03 16:33:59 -0700978 memset(ptr, 0,
979 ((const asn_SET_specifics_t *)(td->specifics))->struct_size);
Lev Walkin8d99d7b2017-08-25 01:06:00 -0700980 break;
981 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000982}
983
984int
Lev Walkin20696a42017-10-17 21:27:33 -0700985SET_constraint(const asn_TYPE_descriptor_t *td, const void *sptr,
986 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
987 size_t edx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000988
989 if(!sptr) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700990 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +0000991 "%s: value not given (%s:%d)",
992 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000993 return -1;
994 }
995
996 /*
997 * Iterate over structure members and check their validity.
998 */
Lev Walkin449f8322004-08-20 13:23:42 +0000999 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +00001000 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +00001001 const void *memb_ptr;
1002
Lev Walkincc93b0f2004-09-10 09:18:20 +00001003 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001004 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +00001005 if(!memb_ptr) {
Lev Walkincc93b0f2004-09-10 09:18:20 +00001006 if(elm->optional)
1007 continue;
Lev Walkin7c1dc052016-03-14 03:08:15 -07001008 ASN__CTFAIL(app_key, td, sptr,
Lev Walkincc93b0f2004-09-10 09:18:20 +00001009 "%s: mandatory element %s absent (%s:%d)",
1010 td->name, elm->name, __FILE__, __LINE__);
1011 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +00001012 }
1013 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001014 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +00001015 }
1016
Lev Walkina5972be2017-09-29 23:15:58 -07001017 if(elm->encoding_constraints.general_constraints) {
1018 return elm->encoding_constraints.general_constraints(
1019 elm->type, memb_ptr, ctfailcb, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +00001020 } else {
Lev Walkina5972be2017-09-29 23:15:58 -07001021 return elm->type->encoding_constraints.general_constraints(
1022 elm->type, memb_ptr, ctfailcb, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +00001023 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001024 }
1025
1026 return 0;
1027}
Lev Walkincd2f48e2017-08-10 02:14:59 -07001028
1029int
1030SET_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
1031 const void *bptr) {
1032 size_t edx;
1033
1034 for(edx = 0; edx < td->elements_count; edx++) {
1035 asn_TYPE_member_t *elm = &td->elements[edx];
1036 const void *amemb;
1037 const void *bmemb;
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001038 int ret;
Lev Walkincd2f48e2017-08-10 02:14:59 -07001039
1040 if(elm->flags & ATF_POINTER) {
1041 amemb =
1042 *(const void *const *)((const char *)aptr + elm->memb_offset);
1043 bmemb =
1044 *(const void *const *)((const char *)bptr + elm->memb_offset);
1045 if(!amemb) {
1046 if(!bmemb) continue;
1047 return -1;
1048 } else if(!bmemb) {
1049 return 1;
1050 }
1051 } else {
1052 amemb = (const void *)((const char *)aptr + elm->memb_offset);
1053 bmemb = (const void *)((const char *)bptr + elm->memb_offset);
1054 }
1055
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001056 ret = elm->type->op->compare_struct(elm->type, amemb, bmemb);
Lev Walkincd2f48e2017-08-10 02:14:59 -07001057 if(ret != 0) return ret;
1058 }
1059
1060 return 0;
1061}
1062
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001063
1064asn_TYPE_operation_t asn_OP_SET = {
1065 SET_free,
1066 SET_print,
1067 SET_compare,
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001068 SET_decode_ber,
1069 SET_encode_der,
1070 SET_decode_xer,
1071 SET_encode_xer,
Lev Walkinca0d40b2017-08-24 13:38:05 -07001072 0, /* SET_decode_oer */
1073 0, /* SET_encode_oer */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001074 0, /* SET_decode_uper */
1075 0, /* SET_encode_uper */
Lev Walkina5972be2017-09-29 23:15:58 -07001076 SET_random_fill,
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001077 0 /* Use generic outmost tag fetcher */
1078};
1079
Lev Walkina5972be2017-09-29 23:15:58 -07001080
1081asn_random_fill_result_t
1082SET_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
1083 const asn_encoding_constraints_t *constr,
1084 size_t max_length) {
1085 const asn_SET_specifics_t *specs =
1086 (const asn_SET_specifics_t *)td->specifics;
1087 asn_random_fill_result_t result_ok = {ARFILL_OK, 0};
1088 asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
1089 asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
1090 void *st = *sptr;
1091 size_t edx;
1092
1093 if(max_length == 0) return result_skipped;
1094
1095 (void)constr;
1096
1097 if(st == NULL) {
1098 st = CALLOC(1, specs->struct_size);
1099 if(st == NULL) {
1100 return result_failed;
1101 }
1102 }
1103
1104 for(edx = 0; edx < td->elements_count; edx++) {
1105 const asn_TYPE_member_t *elm = &td->elements[edx];
1106 void *memb_ptr; /* Pointer to the member */
1107 void **memb_ptr2; /* Pointer to that pointer */
1108 asn_random_fill_result_t tmpres;
1109
1110 if(elm->optional && asn_random_between(0, 4) == 2) {
1111 /* Sometimes decide not to fill the optional value */
1112 continue;
1113 }
1114
1115 if(elm->flags & ATF_POINTER) {
1116 /* Member is a pointer to another structure */
1117 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
1118 } else {
1119 memb_ptr = (char *)st + elm->memb_offset;
1120 memb_ptr2 = &memb_ptr;
1121 }
1122
1123 tmpres = elm->type->op->random_fill(
1124 elm->type, memb_ptr2, &elm->encoding_constraints,
1125 max_length > result_ok.length ? max_length - result_ok.length : 0);
1126 switch(tmpres.code) {
1127 case ARFILL_OK:
1128 result_ok.length += tmpres.length;
1129 continue;
1130 case ARFILL_SKIPPED:
1131 assert(!(elm->flags & ATF_POINTER) || *memb_ptr2 == NULL);
1132 continue;
1133 case ARFILL_FAILED:
1134 if(st == *sptr) {
1135 ASN_STRUCT_RESET(*td, st);
1136 } else {
1137 ASN_STRUCT_FREE(*td, st);
1138 }
1139 return tmpres;
1140 }
1141 }
1142
1143 *sptr = st;
1144
1145 return result_ok;
1146}
1147