blob: e9a25aefdb7695aed3d15d5f9dbe35881ec6a26d [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 Walkinafbf2a92017-09-12 23:30:27 -070097SET_decode_ber(const 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 */
Lev Walkin494fb702017-08-07 20:07:00 -0700115 size_t edx; /* SET element's index */
Lev Walkinf15320b2004-06-03 03:38:44 +0000116
Lev Walkin449f8322004-08-20 13:23:42 +0000117 ASN_DEBUG("Decoding %s as SET", td->name);
Lev Walkinadf863f2006-09-05 16:18:34 +0000118
Lev Walkin7c1dc052016-03-14 03:08:15 -0700119 if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx))
120 ASN__DECODE_FAILED;
Lev Walkinadf863f2006-09-05 16:18:34 +0000121
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;
Lev Walkinf3f7f942017-09-26 22:45:46 -0700237 ASN_DEBUG("Got tag %s (%s), edx %zd",
Lev Walkin3e478ed2004-10-28 13:04:02 +0000238 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 Walkinf3f7f942017-09-26 22:45:46 -0700275 ASN_DEBUG("SET %s: Duplicate element %s (%zd)",
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 */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800299 rval = elements[edx].type->op->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;
Lev Walkin494fb702017-08-07 20:07:00 -0700397 size_t edx;
Lev Walkin6c0df202005-02-14 19:03:17 +0000398
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 Walkin494fb702017-08-07 20:07:00 -0700444 size_t t2m_count;
Lev Walkinf15320b2004-06-03 03:38:44 +0000445 ssize_t ret;
Lev Walkin494fb702017-08-07 20:07:00 -0700446 size_t edx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000447
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]));
Lev Walkin7c1dc052016-03-14 03:08:15 -0700454 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;
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400470 void *memb_ptr_dontuse; /* Pointer to the member */
471 void **memb_ptr2; /* Pointer to that pointer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000472
473 /*
474 * Compute the length of the encoding of this member.
475 */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000476 if(elm->flags & ATF_POINTER) {
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400477 memb_ptr2 = (void **)((char *)sptr + elm->memb_offset);
478 if(!*memb_ptr2) {
Lev Walkinac589332005-08-22 14:19:28 +0000479 if(!elm->optional)
480 /* Mandatory elements missing */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700481 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000482 if(t2m_build_own) {
Wim Lewisfb6344e2014-07-28 12:16:01 -0700483 t2m_build[t2m_count].el_no = edx;
484 t2m_build[t2m_count].el_tag = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000485 t2m_count++;
486 }
487 continue;
488 }
489 } else {
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400490 memb_ptr_dontuse = (void *)((char *)sptr + elm->memb_offset);
491 memb_ptr2 = &memb_ptr_dontuse; /* Only use of memb_ptr_dontuse */
Lev Walkinf15320b2004-06-03 03:38:44 +0000492 }
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400493
494 /* Eliminate default values */
495 if(elm->default_value && elm->default_value(0, memb_ptr2) == 1) {
496 if(t2m_build_own) {
497 t2m_build[t2m_count].el_no = edx;
498 t2m_build[t2m_count].el_tag = 0;
499 t2m_count++;
500 }
501 continue;
502 }
503
504 tmper = elm->type->op->der_encoder(elm->type, *memb_ptr2,
Lev Walkinf15320b2004-06-03 03:38:44 +0000505 elm->tag_mode, elm->tag,
506 0, 0);
Lev Walkind5193802004-10-03 09:12:07 +0000507 if(tmper.encoded == -1)
508 return tmper;
509 computed_size += tmper.encoded;
Lev Walkinf15320b2004-06-03 03:38:44 +0000510
511 /*
512 * Remember the outmost tag of this member.
513 */
514 if(t2m_build_own) {
Wim Lewisfb6344e2014-07-28 12:16:01 -0700515 t2m_build[t2m_count].el_no = edx;
516 t2m_build[t2m_count].el_tag = asn_TYPE_outmost_tag(
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400517 elm->type, *memb_ptr2, elm->tag_mode, elm->tag);
Lev Walkinf15320b2004-06-03 03:38:44 +0000518 t2m_count++;
519 } else {
520 /*
521 * No dynamic sorting is necessary.
522 */
523 }
524 }
525
526 /*
527 * Finalize order of the components.
528 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000529 if(t2m_build_own) {
530 /*
531 * Sort the underlying members according to their
532 * canonical tags order. DER encoding mandates it.
533 */
Wim Lewisfb6344e2014-07-28 12:16:01 -0700534 qsort(t2m_build, t2m_count, sizeof(specs->tag2el[0]), _t2e_cmp);
535 t2m = t2m_build;
Lev Walkinf15320b2004-06-03 03:38:44 +0000536 } else {
537 /*
538 * Tags are already sorted by the compiler.
539 */
Wim Lewisfb6344e2014-07-28 12:16:01 -0700540 t2m = specs->tag2el;
541 t2m_count = specs->tag2el_count;
Lev Walkinf15320b2004-06-03 03:38:44 +0000542 }
Wim Lewisfb6344e2014-07-28 12:16:01 -0700543 assert(t2m_count == td->elements_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000544
545 /*
546 * Encode the TLV for the sequence itself.
547 */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000548 ret = der_write_tags(td, computed_size, tag_mode, 1, tag, cb, app_key);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700549 if(ret == -1) ASN__ENCODE_FAILED;
Lev Walkind5193802004-10-03 09:12:07 +0000550 er.encoded = computed_size + ret;
Lev Walkinf15320b2004-06-03 03:38:44 +0000551
Lev Walkin7c1dc052016-03-14 03:08:15 -0700552 if(!cb) ASN__ENCODED_OK(er);
Lev Walkinf15320b2004-06-03 03:38:44 +0000553
554 /*
555 * Encode all members.
556 */
Lev Walkin449f8322004-08-20 13:23:42 +0000557 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000558 asn_TYPE_member_t *elm;
Lev Walkind5193802004-10-03 09:12:07 +0000559 asn_enc_rval_t tmper;
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400560
561 void *memb_ptr_dontuse; /* Pointer to the member */
562 void **memb_ptr2; /* Pointer to that pointer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000563
564 /* Encode according to the tag order */
Lev Walkin449f8322004-08-20 13:23:42 +0000565 elm = &td->elements[t2m[edx].el_no];
Lev Walkinf15320b2004-06-03 03:38:44 +0000566
Lev Walkincc93b0f2004-09-10 09:18:20 +0000567 if(elm->flags & ATF_POINTER) {
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400568 memb_ptr2 = (void **)((char *)sptr + elm->memb_offset);
569 if(!*memb_ptr2) continue;
Lev Walkinf15320b2004-06-03 03:38:44 +0000570 } else {
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400571 memb_ptr_dontuse = (void *)((char *)sptr + elm->memb_offset);
572 memb_ptr2 = &memb_ptr_dontuse; /* Only use of memb_ptr_dontuse */
Lev Walkinf15320b2004-06-03 03:38:44 +0000573 }
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400574
575 /* Eliminate default values */
576 if(elm->default_value && elm->default_value(0, memb_ptr2) == 1)
577 continue;
578
579 tmper = elm->type->op->der_encoder(elm->type, *memb_ptr2,
580 elm->tag_mode, elm->tag, cb, app_key);
Lev Walkind5193802004-10-03 09:12:07 +0000581 if(tmper.encoded == -1)
582 return tmper;
583 computed_size -= tmper.encoded;
Lev Walkinf15320b2004-06-03 03:38:44 +0000584 }
585
586 if(computed_size != 0) {
587 /*
588 * Encoded size is not equal to the computed size.
589 */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700590 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000591 }
592
Lev Walkin7c1dc052016-03-14 03:08:15 -0700593 ASN__ENCODED_OK(er);
Lev Walkinf15320b2004-06-03 03:38:44 +0000594}
595
Lev Walkin6c0df202005-02-14 19:03:17 +0000596#undef XER_ADVANCE
597#define XER_ADVANCE(num_bytes) do { \
598 size_t num = num_bytes; \
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800599 buf_ptr = ((const char *)buf_ptr) + num;\
Lev Walkin6c0df202005-02-14 19:03:17 +0000600 size -= num; \
601 consumed_myself += num; \
602 } while(0)
603
604/*
605 * Decode the XER (XML) data.
606 */
607asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -0700608SET_decode_xer(const asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkin6c0df202005-02-14 19:03:17 +0000609 void **struct_ptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000610 const void *buf_ptr, size_t size) {
Lev Walkin6c0df202005-02-14 19:03:17 +0000611 /*
612 * Bring closer parts of structure description.
613 */
614 asn_SET_specifics_t *specs = (asn_SET_specifics_t *)td->specifics;
615 asn_TYPE_member_t *elements = td->elements;
616 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
617
618 /*
619 * ... and parts of the structure being constructed.
620 */
621 void *st = *struct_ptr; /* Target structure. */
622 asn_struct_ctx_t *ctx; /* Decoder context */
623
624 asn_dec_rval_t rval; /* Return value from a decoder */
625 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
Lev Walkin494fb702017-08-07 20:07:00 -0700626 size_t edx; /* Element index */
Lev Walkin6c0df202005-02-14 19:03:17 +0000627
628 /*
629 * Create the target structure if it is not present already.
630 */
631 if(st == 0) {
632 st = *struct_ptr = CALLOC(1, specs->struct_size);
633 if(st == 0) RETURN(RC_FAIL);
634 }
635
636 /*
637 * Restore parsing context.
638 */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800639 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkin6c0df202005-02-14 19:03:17 +0000640
641 /*
642 * Phases of XER/XML processing:
643 * Phase 0: Check that the opening tag matches our expectations.
644 * Phase 1: Processing body and reacting on closing tag.
645 * Phase 2: Processing inner type.
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000646 * Phase 3: Skipping unknown extensions.
647 * Phase 4: PHASED OUT
Lev Walkin6c0df202005-02-14 19:03:17 +0000648 */
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000649 for(edx = ctx->step; ctx->phase <= 3;) {
Lev Walkin6c0df202005-02-14 19:03:17 +0000650 pxer_chunk_type_e ch_type; /* XER chunk type */
651 ssize_t ch_size; /* Chunk size */
652 xer_check_tag_e tcv; /* Tag check value */
653 asn_TYPE_member_t *elm;
654
655 /*
656 * Go inside the inner member of a set.
657 */
658 if(ctx->phase == 2) {
659 asn_dec_rval_t tmprval;
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400660 void *memb_ptr_dontuse; /* Pointer to the member */
Lev Walkin6c0df202005-02-14 19:03:17 +0000661 void **memb_ptr2; /* Pointer to that pointer */
662
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800663 if(ASN_SET_ISPRESENT2((char *)st + specs->pres_offset,
Lev Walkin6c0df202005-02-14 19:03:17 +0000664 edx)) {
Lev Walkinf3f7f942017-09-26 22:45:46 -0700665 ASN_DEBUG("SET %s: Duplicate element %s (%zd)",
Lev Walkin6c0df202005-02-14 19:03:17 +0000666 td->name, elements[edx].name, edx);
667 RETURN(RC_FAIL);
668 }
669
Lev Walkin8bb4a952005-02-14 20:15:40 +0000670 elm = &elements[edx];
Lev Walkin6c0df202005-02-14 19:03:17 +0000671
672 if(elm->flags & ATF_POINTER) {
673 /* Member is a pointer to another structure */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800674 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
Lev Walkin6c0df202005-02-14 19:03:17 +0000675 } else {
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400676 memb_ptr_dontuse = (char *)st + elm->memb_offset;
677 memb_ptr2 = &memb_ptr_dontuse; /* Only use of memb_ptr_dontuse */
Lev Walkin6c0df202005-02-14 19:03:17 +0000678 }
679
680 /* Invoke the inner type decoder, m.b. multiple times */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800681 tmprval = elm->type->op->xer_decoder(opt_codec_ctx,
Lev Walkin6c0df202005-02-14 19:03:17 +0000682 elm->type, memb_ptr2, elm->name,
683 buf_ptr, size);
684 XER_ADVANCE(tmprval.consumed);
685 if(tmprval.code != RC_OK)
686 RETURN(tmprval.code);
687 ctx->phase = 1; /* Back to body processing */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800688 ASN_SET_MKPRESENT((char *)st + specs->pres_offset, edx);
Lev Walkin6c0df202005-02-14 19:03:17 +0000689 ASN_DEBUG("XER/SET phase => %d", ctx->phase);
690 /* Fall through */
691 }
692
693 /*
694 * Get the next part of the XML stream.
695 */
Lev Walkin1e443962005-02-18 18:06:36 +0000696 ch_size = xer_next_token(&ctx->context,
697 buf_ptr, size, &ch_type);
Lev Walkin97363482016-01-24 19:23:02 -0800698 if(ch_size == -1) {
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400699 RETURN(RC_FAIL);
700 } else {
Lev Walkin6c0df202005-02-14 19:03:17 +0000701 switch(ch_type) {
Lev Walkin97363482016-01-24 19:23:02 -0800702 case PXER_WMORE:
703 RETURN(RC_WMORE);
Lev Walkin6c0df202005-02-14 19:03:17 +0000704 case PXER_COMMENT: /* Got XML comment */
705 case PXER_TEXT: /* Ignore free-standing text */
706 XER_ADVANCE(ch_size); /* Skip silently */
707 continue;
708 case PXER_TAG:
709 break; /* Check the rest down there */
710 }
711 }
712
713 tcv = xer_check_tag(buf_ptr, ch_size, xml_tag);
714 ASN_DEBUG("XER/SET: tcv = %d, ph=%d", tcv, ctx->phase);
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000715
716 /* Skip the extensions section */
717 if(ctx->phase == 3) {
718 switch(xer_skip_unknown(tcv, &ctx->left)) {
719 case -1:
720 ctx->phase = 4;
721 RETURN(RC_FAIL);
Lev Walkin1e443962005-02-18 18:06:36 +0000722 case 1:
723 ctx->phase = 1;
724 /* Fall through */
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000725 case 0:
726 XER_ADVANCE(ch_size);
727 continue;
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000728 case 2:
729 ctx->phase = 1;
730 break;
731 }
732 }
733
Lev Walkin6c0df202005-02-14 19:03:17 +0000734 switch(tcv) {
735 case XCT_CLOSING:
736 if(ctx->phase == 0) break;
737 ctx->phase = 0;
738 /* Fall through */
739 case XCT_BOTH:
740 if(ctx->phase == 0) {
741 if(_SET_is_populated(td, st)) {
742 XER_ADVANCE(ch_size);
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000743 ctx->phase = 4; /* Phase out */
Lev Walkin6c0df202005-02-14 19:03:17 +0000744 RETURN(RC_OK);
745 } else {
746 ASN_DEBUG("Premature end of XER SET");
747 RETURN(RC_FAIL);
748 }
749 }
750 /* Fall through */
751 case XCT_OPENING:
752 if(ctx->phase == 0) {
753 XER_ADVANCE(ch_size);
754 ctx->phase = 1; /* Processing body phase */
755 continue;
756 }
757 /* Fall through */
Lev Walkin904e65b2005-02-18 14:23:48 +0000758 case XCT_UNKNOWN_OP:
759 case XCT_UNKNOWN_BO:
Lev Walkin6c0df202005-02-14 19:03:17 +0000760
761 ASN_DEBUG("XER/SET: tcv=%d, ph=%d", tcv, ctx->phase);
762 if(ctx->phase != 1)
763 break; /* Really unexpected */
764
765 /*
766 * Search which member corresponds to this tag.
767 */
768 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000769 switch(xer_check_tag(buf_ptr, ch_size,
770 elements[edx].name)) {
Lev Walkin6c0df202005-02-14 19:03:17 +0000771 case XCT_BOTH:
772 case XCT_OPENING:
773 /*
774 * Process this member.
775 */
776 ctx->step = edx;
777 ctx->phase = 2;
778 break;
Lev Walkin904e65b2005-02-18 14:23:48 +0000779 case XCT_UNKNOWN_OP:
780 case XCT_UNKNOWN_BO:
Lev Walkin6c0df202005-02-14 19:03:17 +0000781 continue;
Lev Walkin6c0df202005-02-14 19:03:17 +0000782 default:
783 edx = td->elements_count;
784 break; /* Phase out */
785 }
786 break;
787 }
788 if(edx != td->elements_count)
789 continue;
Lev Walkin904e65b2005-02-18 14:23:48 +0000790
791 /* It is expected extension */
792 if(specs->extensible) {
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000793 ASN_DEBUG("Got anticipated extension");
Lev Walkin904e65b2005-02-18 14:23:48 +0000794 /*
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000795 * Check for (XCT_BOTH or XCT_UNKNOWN_BO)
796 * By using a mask. Only record a pure
797 * <opening> tags.
Lev Walkin904e65b2005-02-18 14:23:48 +0000798 */
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000799 if(tcv & XCT_CLOSING) {
800 /* Found </extension> without body */
801 } else {
802 ctx->left = 1;
803 ctx->phase = 3; /* Skip ...'s */
804 }
805 XER_ADVANCE(ch_size);
806 continue;
Lev Walkin904e65b2005-02-18 14:23:48 +0000807 }
808
Lev Walkin6c0df202005-02-14 19:03:17 +0000809 /* Fall through */
810 default:
811 break;
812 }
813
Lev Walkine0b56e02005-02-25 12:10:27 +0000814 ASN_DEBUG("Unexpected XML tag in SET, expected \"%s\"",
815 xml_tag);
Lev Walkin6c0df202005-02-14 19:03:17 +0000816 break;
817 }
818
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000819 ctx->phase = 4; /* "Phase out" on hard failure */
Lev Walkin6c0df202005-02-14 19:03:17 +0000820 RETURN(RC_FAIL);
821}
822
Lev Walkina9cc46e2004-09-22 16:06:28 +0000823asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000824SET_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000825 int ilevel, enum xer_encoder_flags_e flags,
826 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkind5193802004-10-03 09:12:07 +0000827 asn_SET_specifics_t *specs = (asn_SET_specifics_t *)td->specifics;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000828 asn_enc_rval_t er;
829 int xcan = (flags & XER_F_CANONICAL);
Wim Lewisfb6344e2014-07-28 12:16:01 -0700830 const asn_TYPE_tag2member_t *t2m = specs->tag2el_cxer;
Lev Walkin494fb702017-08-07 20:07:00 -0700831 size_t t2m_count = specs->tag2el_cxer_count;
832 size_t edx;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000833
834 if(!sptr)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700835 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000836
Lev Walkind5193802004-10-03 09:12:07 +0000837 assert(t2m_count == td->elements_count);
838
Lev Walkina9cc46e2004-09-22 16:06:28 +0000839 er.encoded = 0;
840
Lev Walkind5193802004-10-03 09:12:07 +0000841 for(edx = 0; edx < t2m_count; edx++) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000842 asn_enc_rval_t tmper;
Lev Walkind5193802004-10-03 09:12:07 +0000843 asn_TYPE_member_t *elm;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000844 void *memb_ptr;
Lev Walkind5193802004-10-03 09:12:07 +0000845 const char *mname;
846 unsigned int mlen;
847
848 elm = &td->elements[t2m[edx].el_no];
849 mname = elm->name;
850 mlen = strlen(elm->name);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000851
852 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800853 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
Lev Walkinac589332005-08-22 14:19:28 +0000854 if(!memb_ptr) {
855 if(elm->optional)
856 continue;
857 /* Mandatory element missing */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700858 ASN__ENCODE_FAILED;
Lev Walkinac589332005-08-22 14:19:28 +0000859 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000860 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800861 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000862 }
863
864 if(!xcan)
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700865 ASN__TEXT_INDENT(1, ilevel);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700866 ASN__CALLBACK3("<", 1, mname, mlen, ">", 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000867
868 /* Print the member itself */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800869 tmper = elm->type->op->xer_encoder(elm->type, memb_ptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000870 ilevel + 1, flags, cb, app_key);
871 if(tmper.encoded == -1) return tmper;
Lev Walkin4fe28822017-09-18 02:57:34 -0700872 er.encoded += tmper.encoded;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000873
Lev Walkin7c1dc052016-03-14 03:08:15 -0700874 ASN__CALLBACK3("</", 2, mname, mlen, ">", 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000875 }
876
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700877 if(!xcan) ASN__TEXT_INDENT(1, ilevel - 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000878
Lev Walkin7c1dc052016-03-14 03:08:15 -0700879 ASN__ENCODED_OK(er);
Lev Walkind5193802004-10-03 09:12:07 +0000880cb_failed:
Lev Walkin7c1dc052016-03-14 03:08:15 -0700881 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000882}
883
Lev Walkinf15320b2004-06-03 03:38:44 +0000884int
Lev Walkin5e033762004-09-29 13:26:15 +0000885SET_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000886 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin494fb702017-08-07 20:07:00 -0700887 size_t edx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000888 int ret;
889
Lev Walkin8e8078a2004-09-26 13:10:40 +0000890 if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000891
892 /* Dump preamble */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000893 if(cb(td->name, strlen(td->name), app_key) < 0
894 || cb(" ::= {", 6, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000895 return -1;
896
Lev Walkin449f8322004-08-20 13:23:42 +0000897 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000898 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000899 const void *memb_ptr;
900
Lev Walkincc93b0f2004-09-10 09:18:20 +0000901 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800902 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkinac589332005-08-22 14:19:28 +0000903 if(!memb_ptr) {
904 if(elm->optional) continue;
905 /* Print <absent> line */
906 /* Fall through */
907 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000908 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800909 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000910 }
911
Lev Walkin8e8078a2004-09-26 13:10:40 +0000912 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000913
914 /* Print the member's name and stuff */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000915 if(cb(elm->name, strlen(elm->name), app_key) < 0
916 || cb(": ", 2, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000917 return -1;
918
919 /* Print the member itself */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800920 ret = elm->type->op->print_struct(elm->type, memb_ptr, ilevel + 1,
Lev Walkinf15320b2004-06-03 03:38:44 +0000921 cb, app_key);
922 if(ret) return ret;
Lev Walkinf15320b2004-06-03 03:38:44 +0000923 }
924
Lev Walkin8e8078a2004-09-26 13:10:40 +0000925 ilevel--;
926 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000927
Lev Walkin8e8078a2004-09-26 13:10:40 +0000928 return (cb("}", 1, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000929}
930
931void
Lev Walkin8d99d7b2017-08-25 01:06:00 -0700932SET_free(const asn_TYPE_descriptor_t *td, void *ptr,
933 enum asn_struct_free_method method) {
934 size_t edx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000935
936 if(!td || !ptr)
937 return;
938
939 ASN_DEBUG("Freeing %s as SET", td->name);
940
Lev Walkin449f8322004-08-20 13:23:42 +0000941 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000942 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000943 void *memb_ptr;
Lev Walkincc93b0f2004-09-10 09:18:20 +0000944 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800945 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000946 if(memb_ptr)
Lev Walkinadcb5862006-03-17 02:11:12 +0000947 ASN_STRUCT_FREE(*elm->type, memb_ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +0000948 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800949 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
Lev Walkinadcb5862006-03-17 02:11:12 +0000950 ASN_STRUCT_FREE_CONTENTS_ONLY(*elm->type, memb_ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +0000951 }
952 }
953
Lev Walkin8d99d7b2017-08-25 01:06:00 -0700954 switch(method) {
955 case ASFM_FREE_EVERYTHING:
956 FREEMEM(ptr);
957 break;
958 case ASFM_FREE_UNDERLYING:
959 break;
960 case ASFM_FREE_UNDERLYING_AND_RESET:
961 memset(ptr, 0, ((asn_SET_specifics_t *)(td->specifics))->struct_size);
962 break;
963 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000964}
965
966int
Lev Walkin5e033762004-09-29 13:26:15 +0000967SET_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +0000968 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkin494fb702017-08-07 20:07:00 -0700969 size_t edx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000970
971 if(!sptr) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700972 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +0000973 "%s: value not given (%s:%d)",
974 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000975 return -1;
976 }
977
978 /*
979 * Iterate over structure members and check their validity.
980 */
Lev Walkin449f8322004-08-20 13:23:42 +0000981 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000982 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000983 const void *memb_ptr;
984
Lev Walkincc93b0f2004-09-10 09:18:20 +0000985 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800986 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000987 if(!memb_ptr) {
Lev Walkincc93b0f2004-09-10 09:18:20 +0000988 if(elm->optional)
989 continue;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700990 ASN__CTFAIL(app_key, td, sptr,
Lev Walkincc93b0f2004-09-10 09:18:20 +0000991 "%s: mandatory element %s absent (%s:%d)",
992 td->name, elm->name, __FILE__, __LINE__);
993 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000994 }
995 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800996 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000997 }
998
Lev Walkina5972be2017-09-29 23:15:58 -0700999 if(elm->encoding_constraints.general_constraints) {
1000 return elm->encoding_constraints.general_constraints(
1001 elm->type, memb_ptr, ctfailcb, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +00001002 } else {
Lev Walkina5972be2017-09-29 23:15:58 -07001003 return elm->type->encoding_constraints.general_constraints(
1004 elm->type, memb_ptr, ctfailcb, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +00001005 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001006 }
1007
1008 return 0;
1009}
Lev Walkincd2f48e2017-08-10 02:14:59 -07001010
1011int
1012SET_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
1013 const void *bptr) {
1014 size_t edx;
1015
1016 for(edx = 0; edx < td->elements_count; edx++) {
1017 asn_TYPE_member_t *elm = &td->elements[edx];
1018 const void *amemb;
1019 const void *bmemb;
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001020 int ret;
Lev Walkincd2f48e2017-08-10 02:14:59 -07001021
1022 if(elm->flags & ATF_POINTER) {
1023 amemb =
1024 *(const void *const *)((const char *)aptr + elm->memb_offset);
1025 bmemb =
1026 *(const void *const *)((const char *)bptr + elm->memb_offset);
1027 if(!amemb) {
1028 if(!bmemb) continue;
1029 return -1;
1030 } else if(!bmemb) {
1031 return 1;
1032 }
1033 } else {
1034 amemb = (const void *)((const char *)aptr + elm->memb_offset);
1035 bmemb = (const void *)((const char *)bptr + elm->memb_offset);
1036 }
1037
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001038 ret = elm->type->op->compare_struct(elm->type, amemb, bmemb);
Lev Walkincd2f48e2017-08-10 02:14:59 -07001039 if(ret != 0) return ret;
1040 }
1041
1042 return 0;
1043}
1044
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001045
1046asn_TYPE_operation_t asn_OP_SET = {
1047 SET_free,
1048 SET_print,
1049 SET_compare,
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001050 SET_decode_ber,
1051 SET_encode_der,
1052 SET_decode_xer,
1053 SET_encode_xer,
Lev Walkinca0d40b2017-08-24 13:38:05 -07001054 0, /* SET_decode_oer */
1055 0, /* SET_encode_oer */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001056 0, /* SET_decode_uper */
1057 0, /* SET_encode_uper */
Lev Walkina5972be2017-09-29 23:15:58 -07001058 SET_random_fill,
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001059 0 /* Use generic outmost tag fetcher */
1060};
1061
Lev Walkina5972be2017-09-29 23:15:58 -07001062
1063asn_random_fill_result_t
1064SET_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
1065 const asn_encoding_constraints_t *constr,
1066 size_t max_length) {
1067 const asn_SET_specifics_t *specs =
1068 (const asn_SET_specifics_t *)td->specifics;
1069 asn_random_fill_result_t result_ok = {ARFILL_OK, 0};
1070 asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
1071 asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
1072 void *st = *sptr;
1073 size_t edx;
1074
1075 if(max_length == 0) return result_skipped;
1076
1077 (void)constr;
1078
1079 if(st == NULL) {
1080 st = CALLOC(1, specs->struct_size);
1081 if(st == NULL) {
1082 return result_failed;
1083 }
1084 }
1085
1086 for(edx = 0; edx < td->elements_count; edx++) {
1087 const asn_TYPE_member_t *elm = &td->elements[edx];
1088 void *memb_ptr; /* Pointer to the member */
1089 void **memb_ptr2; /* Pointer to that pointer */
1090 asn_random_fill_result_t tmpres;
1091
1092 if(elm->optional && asn_random_between(0, 4) == 2) {
1093 /* Sometimes decide not to fill the optional value */
1094 continue;
1095 }
1096
1097 if(elm->flags & ATF_POINTER) {
1098 /* Member is a pointer to another structure */
1099 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
1100 } else {
1101 memb_ptr = (char *)st + elm->memb_offset;
1102 memb_ptr2 = &memb_ptr;
1103 }
1104
1105 tmpres = elm->type->op->random_fill(
1106 elm->type, memb_ptr2, &elm->encoding_constraints,
1107 max_length > result_ok.length ? max_length - result_ok.length : 0);
1108 switch(tmpres.code) {
1109 case ARFILL_OK:
1110 result_ok.length += tmpres.length;
1111 continue;
1112 case ARFILL_SKIPPED:
1113 assert(!(elm->flags & ATF_POINTER) || *memb_ptr2 == NULL);
1114 continue;
1115 case ARFILL_FAILED:
1116 if(st == *sptr) {
1117 ASN_STRUCT_RESET(*td, st);
1118 } else {
1119 ASN_STRUCT_FREE(*td, st);
1120 }
1121 return tmpres;
1122 }
1123 }
1124
1125 *sptr = st;
1126
1127 return result_ok;
1128}
1129