blob: 422e66398af86db3761b5de8322a4e160da07824 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
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 */
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 Walkind84f6032017-10-03 16:33:59 -0700102 const asn_SET_specifics_t *specs = (const asn_SET_specifics_t *)td->specifics;
Lev Walkin5e033762004-09-29 13:26:15 +0000103 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) {
Lev Walkind84f6032017-10-03 16:33:59 -0700396 const asn_SET_specifics_t *specs = (const 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 Walkind84f6032017-10-03 16:33:59 -0700438 const asn_SET_specifics_t *specs = (const 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) {
Lev Walkine2bbbf82017-10-08 18:52:37 -0700452 t2m_build = (asn_TYPE_tag2member_t *)CALLOC(td->elements_count,
453 sizeof(t2m_build[0]));
454 if(!t2m_build) ASN__ENCODE_FAILED;
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 Walkine2bbbf82017-10-08 18:52:37 -0700479 if(!elm->optional) {
Lev Walkinac589332005-08-22 14:19:28 +0000480 /* Mandatory elements missing */
Lev Walkine2bbbf82017-10-08 18:52:37 -0700481 FREEMEM(t2m_build);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700482 ASN__ENCODE_FAILED;
Lev Walkine2bbbf82017-10-08 18:52:37 -0700483 }
484 if(t2m_build) {
Wim Lewisfb6344e2014-07-28 12:16:01 -0700485 t2m_build[t2m_count].el_no = edx;
486 t2m_build[t2m_count].el_tag = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000487 t2m_count++;
488 }
489 continue;
490 }
491 } else {
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400492 memb_ptr_dontuse = (void *)((char *)sptr + elm->memb_offset);
493 memb_ptr2 = &memb_ptr_dontuse; /* Only use of memb_ptr_dontuse */
Lev Walkinf15320b2004-06-03 03:38:44 +0000494 }
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400495
496 /* Eliminate default values */
497 if(elm->default_value && elm->default_value(0, memb_ptr2) == 1) {
Lev Walkine2bbbf82017-10-08 18:52:37 -0700498 if(t2m_build) {
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400499 t2m_build[t2m_count].el_no = edx;
500 t2m_build[t2m_count].el_tag = 0;
501 t2m_count++;
502 }
503 continue;
504 }
505
506 tmper = elm->type->op->der_encoder(elm->type, *memb_ptr2,
Lev Walkinf15320b2004-06-03 03:38:44 +0000507 elm->tag_mode, elm->tag,
508 0, 0);
Lev Walkind5193802004-10-03 09:12:07 +0000509 if(tmper.encoded == -1)
510 return tmper;
511 computed_size += tmper.encoded;
Lev Walkinf15320b2004-06-03 03:38:44 +0000512
513 /*
514 * Remember the outmost tag of this member.
515 */
Lev Walkine2bbbf82017-10-08 18:52:37 -0700516 if(t2m_build) {
Wim Lewisfb6344e2014-07-28 12:16:01 -0700517 t2m_build[t2m_count].el_no = edx;
518 t2m_build[t2m_count].el_tag = asn_TYPE_outmost_tag(
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400519 elm->type, *memb_ptr2, elm->tag_mode, elm->tag);
Lev Walkinf15320b2004-06-03 03:38:44 +0000520 t2m_count++;
521 } else {
522 /*
523 * No dynamic sorting is necessary.
524 */
525 }
526 }
527
528 /*
529 * Finalize order of the components.
530 */
Lev Walkine2bbbf82017-10-08 18:52:37 -0700531 if(t2m_build) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000532 /*
533 * Sort the underlying members according to their
534 * canonical tags order. DER encoding mandates it.
535 */
Wim Lewisfb6344e2014-07-28 12:16:01 -0700536 qsort(t2m_build, t2m_count, sizeof(specs->tag2el[0]), _t2e_cmp);
537 t2m = t2m_build;
Lev Walkinf15320b2004-06-03 03:38:44 +0000538 } else {
539 /*
540 * Tags are already sorted by the compiler.
541 */
Wim Lewisfb6344e2014-07-28 12:16:01 -0700542 t2m = specs->tag2el;
543 t2m_count = specs->tag2el_count;
Lev Walkinf15320b2004-06-03 03:38:44 +0000544 }
Wim Lewisfb6344e2014-07-28 12:16:01 -0700545 assert(t2m_count == td->elements_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000546
547 /*
548 * Encode the TLV for the sequence itself.
549 */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000550 ret = der_write_tags(td, computed_size, tag_mode, 1, tag, cb, app_key);
Lev Walkine2bbbf82017-10-08 18:52:37 -0700551 if(ret == -1) {
552 FREEMEM(t2m_build);
553 ASN__ENCODE_FAILED;
554 }
Lev Walkind5193802004-10-03 09:12:07 +0000555 er.encoded = computed_size + ret;
Lev Walkinf15320b2004-06-03 03:38:44 +0000556
Lev Walkine2bbbf82017-10-08 18:52:37 -0700557 if(!cb) {
558 FREEMEM(t2m_build);
559 ASN__ENCODED_OK(er);
560 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000561
562 /*
563 * Encode all members.
564 */
Lev Walkin449f8322004-08-20 13:23:42 +0000565 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000566 asn_TYPE_member_t *elm;
Lev Walkind5193802004-10-03 09:12:07 +0000567 asn_enc_rval_t tmper;
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400568
569 void *memb_ptr_dontuse; /* Pointer to the member */
570 void **memb_ptr2; /* Pointer to that pointer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000571
572 /* Encode according to the tag order */
Lev Walkin449f8322004-08-20 13:23:42 +0000573 elm = &td->elements[t2m[edx].el_no];
Lev Walkinf15320b2004-06-03 03:38:44 +0000574
Lev Walkincc93b0f2004-09-10 09:18:20 +0000575 if(elm->flags & ATF_POINTER) {
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400576 memb_ptr2 = (void **)((char *)sptr + elm->memb_offset);
577 if(!*memb_ptr2) continue;
Lev Walkinf15320b2004-06-03 03:38:44 +0000578 } else {
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400579 memb_ptr_dontuse = (void *)((char *)sptr + elm->memb_offset);
580 memb_ptr2 = &memb_ptr_dontuse; /* Only use of memb_ptr_dontuse */
Lev Walkinf15320b2004-06-03 03:38:44 +0000581 }
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400582
583 /* Eliminate default values */
584 if(elm->default_value && elm->default_value(0, memb_ptr2) == 1)
585 continue;
586
587 tmper = elm->type->op->der_encoder(elm->type, *memb_ptr2,
588 elm->tag_mode, elm->tag, cb, app_key);
Lev Walkind5193802004-10-03 09:12:07 +0000589 if(tmper.encoded == -1)
590 return tmper;
591 computed_size -= tmper.encoded;
Lev Walkinf15320b2004-06-03 03:38:44 +0000592 }
593
594 if(computed_size != 0) {
595 /*
596 * Encoded size is not equal to the computed size.
597 */
Lev Walkine2bbbf82017-10-08 18:52:37 -0700598 FREEMEM(t2m_build);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700599 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000600 }
601
Lev Walkine2bbbf82017-10-08 18:52:37 -0700602 FREEMEM(t2m_build);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700603 ASN__ENCODED_OK(er);
Lev Walkinf15320b2004-06-03 03:38:44 +0000604}
605
Lev Walkin6c0df202005-02-14 19:03:17 +0000606#undef XER_ADVANCE
607#define XER_ADVANCE(num_bytes) do { \
608 size_t num = num_bytes; \
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800609 buf_ptr = ((const char *)buf_ptr) + num;\
Lev Walkin6c0df202005-02-14 19:03:17 +0000610 size -= num; \
611 consumed_myself += num; \
612 } while(0)
613
614/*
615 * Decode the XER (XML) data.
616 */
617asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -0700618SET_decode_xer(const asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkin6c0df202005-02-14 19:03:17 +0000619 void **struct_ptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000620 const void *buf_ptr, size_t size) {
Lev Walkin6c0df202005-02-14 19:03:17 +0000621 /*
622 * Bring closer parts of structure description.
623 */
Lev Walkind84f6032017-10-03 16:33:59 -0700624 const asn_SET_specifics_t *specs = (const asn_SET_specifics_t *)td->specifics;
Lev Walkin6c0df202005-02-14 19:03:17 +0000625 asn_TYPE_member_t *elements = td->elements;
626 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
627
628 /*
629 * ... and parts of the structure being constructed.
630 */
631 void *st = *struct_ptr; /* Target structure. */
632 asn_struct_ctx_t *ctx; /* Decoder context */
633
634 asn_dec_rval_t rval; /* Return value from a decoder */
635 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
Lev Walkin494fb702017-08-07 20:07:00 -0700636 size_t edx; /* Element index */
Lev Walkin6c0df202005-02-14 19:03:17 +0000637
638 /*
639 * Create the target structure if it is not present already.
640 */
641 if(st == 0) {
642 st = *struct_ptr = CALLOC(1, specs->struct_size);
643 if(st == 0) RETURN(RC_FAIL);
644 }
645
646 /*
647 * Restore parsing context.
648 */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800649 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkin6c0df202005-02-14 19:03:17 +0000650
651 /*
652 * Phases of XER/XML processing:
653 * Phase 0: Check that the opening tag matches our expectations.
654 * Phase 1: Processing body and reacting on closing tag.
655 * Phase 2: Processing inner type.
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000656 * Phase 3: Skipping unknown extensions.
657 * Phase 4: PHASED OUT
Lev Walkin6c0df202005-02-14 19:03:17 +0000658 */
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000659 for(edx = ctx->step; ctx->phase <= 3;) {
Lev Walkin6c0df202005-02-14 19:03:17 +0000660 pxer_chunk_type_e ch_type; /* XER chunk type */
661 ssize_t ch_size; /* Chunk size */
662 xer_check_tag_e tcv; /* Tag check value */
663 asn_TYPE_member_t *elm;
664
665 /*
666 * Go inside the inner member of a set.
667 */
668 if(ctx->phase == 2) {
669 asn_dec_rval_t tmprval;
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400670 void *memb_ptr_dontuse; /* Pointer to the member */
Lev Walkin6c0df202005-02-14 19:03:17 +0000671 void **memb_ptr2; /* Pointer to that pointer */
672
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800673 if(ASN_SET_ISPRESENT2((char *)st + specs->pres_offset,
Lev Walkin6c0df202005-02-14 19:03:17 +0000674 edx)) {
Lev Walkinf3f7f942017-09-26 22:45:46 -0700675 ASN_DEBUG("SET %s: Duplicate element %s (%zd)",
Lev Walkin6c0df202005-02-14 19:03:17 +0000676 td->name, elements[edx].name, edx);
677 RETURN(RC_FAIL);
678 }
679
Lev Walkin8bb4a952005-02-14 20:15:40 +0000680 elm = &elements[edx];
Lev Walkin6c0df202005-02-14 19:03:17 +0000681
682 if(elm->flags & ATF_POINTER) {
683 /* Member is a pointer to another structure */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800684 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
Lev Walkin6c0df202005-02-14 19:03:17 +0000685 } else {
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400686 memb_ptr_dontuse = (char *)st + elm->memb_offset;
687 memb_ptr2 = &memb_ptr_dontuse; /* Only use of memb_ptr_dontuse */
Lev Walkin6c0df202005-02-14 19:03:17 +0000688 }
689
690 /* Invoke the inner type decoder, m.b. multiple times */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800691 tmprval = elm->type->op->xer_decoder(opt_codec_ctx,
Lev Walkin6c0df202005-02-14 19:03:17 +0000692 elm->type, memb_ptr2, elm->name,
693 buf_ptr, size);
694 XER_ADVANCE(tmprval.consumed);
695 if(tmprval.code != RC_OK)
696 RETURN(tmprval.code);
697 ctx->phase = 1; /* Back to body processing */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800698 ASN_SET_MKPRESENT((char *)st + specs->pres_offset, edx);
Lev Walkin6c0df202005-02-14 19:03:17 +0000699 ASN_DEBUG("XER/SET phase => %d", ctx->phase);
700 /* Fall through */
701 }
702
703 /*
704 * Get the next part of the XML stream.
705 */
Lev Walkin1e443962005-02-18 18:06:36 +0000706 ch_size = xer_next_token(&ctx->context,
707 buf_ptr, size, &ch_type);
Lev Walkin97363482016-01-24 19:23:02 -0800708 if(ch_size == -1) {
Ryan Sleeviae8a6e42017-08-16 15:28:58 -0400709 RETURN(RC_FAIL);
710 } else {
Lev Walkin6c0df202005-02-14 19:03:17 +0000711 switch(ch_type) {
Lev Walkin97363482016-01-24 19:23:02 -0800712 case PXER_WMORE:
713 RETURN(RC_WMORE);
Lev Walkin6c0df202005-02-14 19:03:17 +0000714 case PXER_COMMENT: /* Got XML comment */
715 case PXER_TEXT: /* Ignore free-standing text */
716 XER_ADVANCE(ch_size); /* Skip silently */
717 continue;
718 case PXER_TAG:
719 break; /* Check the rest down there */
720 }
721 }
722
723 tcv = xer_check_tag(buf_ptr, ch_size, xml_tag);
724 ASN_DEBUG("XER/SET: tcv = %d, ph=%d", tcv, ctx->phase);
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000725
726 /* Skip the extensions section */
727 if(ctx->phase == 3) {
728 switch(xer_skip_unknown(tcv, &ctx->left)) {
729 case -1:
730 ctx->phase = 4;
731 RETURN(RC_FAIL);
Lev Walkin1e443962005-02-18 18:06:36 +0000732 case 1:
733 ctx->phase = 1;
734 /* Fall through */
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000735 case 0:
736 XER_ADVANCE(ch_size);
737 continue;
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000738 case 2:
739 ctx->phase = 1;
740 break;
741 }
742 }
743
Lev Walkin6c0df202005-02-14 19:03:17 +0000744 switch(tcv) {
745 case XCT_CLOSING:
746 if(ctx->phase == 0) break;
747 ctx->phase = 0;
748 /* Fall through */
749 case XCT_BOTH:
750 if(ctx->phase == 0) {
751 if(_SET_is_populated(td, st)) {
752 XER_ADVANCE(ch_size);
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000753 ctx->phase = 4; /* Phase out */
Lev Walkin6c0df202005-02-14 19:03:17 +0000754 RETURN(RC_OK);
755 } else {
756 ASN_DEBUG("Premature end of XER SET");
757 RETURN(RC_FAIL);
758 }
759 }
760 /* Fall through */
761 case XCT_OPENING:
762 if(ctx->phase == 0) {
763 XER_ADVANCE(ch_size);
764 ctx->phase = 1; /* Processing body phase */
765 continue;
766 }
767 /* Fall through */
Lev Walkin904e65b2005-02-18 14:23:48 +0000768 case XCT_UNKNOWN_OP:
769 case XCT_UNKNOWN_BO:
Lev Walkin6c0df202005-02-14 19:03:17 +0000770
771 ASN_DEBUG("XER/SET: tcv=%d, ph=%d", tcv, ctx->phase);
772 if(ctx->phase != 1)
773 break; /* Really unexpected */
774
775 /*
776 * Search which member corresponds to this tag.
777 */
778 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000779 switch(xer_check_tag(buf_ptr, ch_size,
780 elements[edx].name)) {
Lev Walkin6c0df202005-02-14 19:03:17 +0000781 case XCT_BOTH:
782 case XCT_OPENING:
783 /*
784 * Process this member.
785 */
786 ctx->step = edx;
787 ctx->phase = 2;
788 break;
Lev Walkin904e65b2005-02-18 14:23:48 +0000789 case XCT_UNKNOWN_OP:
790 case XCT_UNKNOWN_BO:
Lev Walkin6c0df202005-02-14 19:03:17 +0000791 continue;
Lev Walkin6c0df202005-02-14 19:03:17 +0000792 default:
793 edx = td->elements_count;
794 break; /* Phase out */
795 }
796 break;
797 }
798 if(edx != td->elements_count)
799 continue;
Lev Walkin904e65b2005-02-18 14:23:48 +0000800
801 /* It is expected extension */
802 if(specs->extensible) {
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000803 ASN_DEBUG("Got anticipated extension");
Lev Walkin904e65b2005-02-18 14:23:48 +0000804 /*
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000805 * Check for (XCT_BOTH or XCT_UNKNOWN_BO)
806 * By using a mask. Only record a pure
807 * <opening> tags.
Lev Walkin904e65b2005-02-18 14:23:48 +0000808 */
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000809 if(tcv & XCT_CLOSING) {
810 /* Found </extension> without body */
811 } else {
812 ctx->left = 1;
813 ctx->phase = 3; /* Skip ...'s */
814 }
815 XER_ADVANCE(ch_size);
816 continue;
Lev Walkin904e65b2005-02-18 14:23:48 +0000817 }
818
Lev Walkin6c0df202005-02-14 19:03:17 +0000819 /* Fall through */
820 default:
821 break;
822 }
823
Lev Walkine0b56e02005-02-25 12:10:27 +0000824 ASN_DEBUG("Unexpected XML tag in SET, expected \"%s\"",
825 xml_tag);
Lev Walkin6c0df202005-02-14 19:03:17 +0000826 break;
827 }
828
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000829 ctx->phase = 4; /* "Phase out" on hard failure */
Lev Walkin6c0df202005-02-14 19:03:17 +0000830 RETURN(RC_FAIL);
831}
832
Lev Walkina9cc46e2004-09-22 16:06:28 +0000833asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000834SET_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000835 int ilevel, enum xer_encoder_flags_e flags,
836 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkind84f6032017-10-03 16:33:59 -0700837 const asn_SET_specifics_t *specs = (const asn_SET_specifics_t *)td->specifics;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000838 asn_enc_rval_t er;
839 int xcan = (flags & XER_F_CANONICAL);
Wim Lewisfb6344e2014-07-28 12:16:01 -0700840 const asn_TYPE_tag2member_t *t2m = specs->tag2el_cxer;
Lev Walkin494fb702017-08-07 20:07:00 -0700841 size_t t2m_count = specs->tag2el_cxer_count;
842 size_t edx;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000843
844 if(!sptr)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700845 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000846
Lev Walkind5193802004-10-03 09:12:07 +0000847 assert(t2m_count == td->elements_count);
848
Lev Walkina9cc46e2004-09-22 16:06:28 +0000849 er.encoded = 0;
850
Lev Walkind5193802004-10-03 09:12:07 +0000851 for(edx = 0; edx < t2m_count; edx++) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000852 asn_enc_rval_t tmper;
Lev Walkind5193802004-10-03 09:12:07 +0000853 asn_TYPE_member_t *elm;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000854 void *memb_ptr;
Lev Walkind5193802004-10-03 09:12:07 +0000855 const char *mname;
856 unsigned int mlen;
857
858 elm = &td->elements[t2m[edx].el_no];
859 mname = elm->name;
860 mlen = strlen(elm->name);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000861
862 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800863 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
Lev Walkinac589332005-08-22 14:19:28 +0000864 if(!memb_ptr) {
865 if(elm->optional)
866 continue;
867 /* Mandatory element missing */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700868 ASN__ENCODE_FAILED;
Lev Walkinac589332005-08-22 14:19:28 +0000869 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000870 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800871 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000872 }
873
874 if(!xcan)
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700875 ASN__TEXT_INDENT(1, ilevel);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700876 ASN__CALLBACK3("<", 1, mname, mlen, ">", 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000877
878 /* Print the member itself */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800879 tmper = elm->type->op->xer_encoder(elm->type, memb_ptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000880 ilevel + 1, flags, cb, app_key);
881 if(tmper.encoded == -1) return tmper;
Lev Walkin4fe28822017-09-18 02:57:34 -0700882 er.encoded += tmper.encoded;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000883
Lev Walkin7c1dc052016-03-14 03:08:15 -0700884 ASN__CALLBACK3("</", 2, mname, mlen, ">", 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000885 }
886
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700887 if(!xcan) ASN__TEXT_INDENT(1, ilevel - 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000888
Lev Walkin7c1dc052016-03-14 03:08:15 -0700889 ASN__ENCODED_OK(er);
Lev Walkind5193802004-10-03 09:12:07 +0000890cb_failed:
Lev Walkin7c1dc052016-03-14 03:08:15 -0700891 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000892}
893
Lev Walkinf15320b2004-06-03 03:38:44 +0000894int
Lev Walkin5e033762004-09-29 13:26:15 +0000895SET_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000896 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin494fb702017-08-07 20:07:00 -0700897 size_t edx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000898 int ret;
899
Lev Walkin8e8078a2004-09-26 13:10:40 +0000900 if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000901
902 /* Dump preamble */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000903 if(cb(td->name, strlen(td->name), app_key) < 0
904 || cb(" ::= {", 6, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000905 return -1;
906
Lev Walkin449f8322004-08-20 13:23:42 +0000907 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000908 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000909 const void *memb_ptr;
910
Lev Walkincc93b0f2004-09-10 09:18:20 +0000911 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800912 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkinac589332005-08-22 14:19:28 +0000913 if(!memb_ptr) {
914 if(elm->optional) continue;
915 /* Print <absent> line */
916 /* Fall through */
917 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000918 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800919 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000920 }
921
Lev Walkin8e8078a2004-09-26 13:10:40 +0000922 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000923
924 /* Print the member's name and stuff */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000925 if(cb(elm->name, strlen(elm->name), app_key) < 0
926 || cb(": ", 2, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000927 return -1;
928
929 /* Print the member itself */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800930 ret = elm->type->op->print_struct(elm->type, memb_ptr, ilevel + 1,
Lev Walkinf15320b2004-06-03 03:38:44 +0000931 cb, app_key);
932 if(ret) return ret;
Lev Walkinf15320b2004-06-03 03:38:44 +0000933 }
934
Lev Walkin8e8078a2004-09-26 13:10:40 +0000935 ilevel--;
936 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000937
Lev Walkin8e8078a2004-09-26 13:10:40 +0000938 return (cb("}", 1, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000939}
940
941void
Lev Walkin8d99d7b2017-08-25 01:06:00 -0700942SET_free(const asn_TYPE_descriptor_t *td, void *ptr,
943 enum asn_struct_free_method method) {
944 size_t edx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000945
946 if(!td || !ptr)
947 return;
948
949 ASN_DEBUG("Freeing %s as SET", td->name);
950
Lev Walkin449f8322004-08-20 13:23:42 +0000951 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000952 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000953 void *memb_ptr;
Lev Walkincc93b0f2004-09-10 09:18:20 +0000954 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800955 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000956 if(memb_ptr)
Lev Walkinadcb5862006-03-17 02:11:12 +0000957 ASN_STRUCT_FREE(*elm->type, memb_ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +0000958 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800959 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
Lev Walkinadcb5862006-03-17 02:11:12 +0000960 ASN_STRUCT_FREE_CONTENTS_ONLY(*elm->type, memb_ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +0000961 }
962 }
963
Lev Walkin8d99d7b2017-08-25 01:06:00 -0700964 switch(method) {
965 case ASFM_FREE_EVERYTHING:
966 FREEMEM(ptr);
967 break;
968 case ASFM_FREE_UNDERLYING:
969 break;
970 case ASFM_FREE_UNDERLYING_AND_RESET:
Lev Walkind84f6032017-10-03 16:33:59 -0700971 memset(ptr, 0,
972 ((const asn_SET_specifics_t *)(td->specifics))->struct_size);
Lev Walkin8d99d7b2017-08-25 01:06:00 -0700973 break;
974 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000975}
976
977int
Lev Walkin5e033762004-09-29 13:26:15 +0000978SET_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +0000979 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkin494fb702017-08-07 20:07:00 -0700980 size_t edx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000981
982 if(!sptr) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700983 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +0000984 "%s: value not given (%s:%d)",
985 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000986 return -1;
987 }
988
989 /*
990 * Iterate over structure members and check their validity.
991 */
Lev Walkin449f8322004-08-20 13:23:42 +0000992 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000993 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000994 const void *memb_ptr;
995
Lev Walkincc93b0f2004-09-10 09:18:20 +0000996 if(elm->flags & ATF_POINTER) {
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800997 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000998 if(!memb_ptr) {
Lev Walkincc93b0f2004-09-10 09:18:20 +0000999 if(elm->optional)
1000 continue;
Lev Walkin7c1dc052016-03-14 03:08:15 -07001001 ASN__CTFAIL(app_key, td, sptr,
Lev Walkincc93b0f2004-09-10 09:18:20 +00001002 "%s: mandatory element %s absent (%s:%d)",
1003 td->name, elm->name, __FILE__, __LINE__);
1004 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +00001005 }
1006 } else {
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001007 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +00001008 }
1009
Lev Walkina5972be2017-09-29 23:15:58 -07001010 if(elm->encoding_constraints.general_constraints) {
1011 return elm->encoding_constraints.general_constraints(
1012 elm->type, memb_ptr, ctfailcb, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +00001013 } else {
Lev Walkina5972be2017-09-29 23:15:58 -07001014 return elm->type->encoding_constraints.general_constraints(
1015 elm->type, memb_ptr, ctfailcb, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +00001016 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001017 }
1018
1019 return 0;
1020}
Lev Walkincd2f48e2017-08-10 02:14:59 -07001021
1022int
1023SET_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
1024 const void *bptr) {
1025 size_t edx;
1026
1027 for(edx = 0; edx < td->elements_count; edx++) {
1028 asn_TYPE_member_t *elm = &td->elements[edx];
1029 const void *amemb;
1030 const void *bmemb;
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001031 int ret;
Lev Walkincd2f48e2017-08-10 02:14:59 -07001032
1033 if(elm->flags & ATF_POINTER) {
1034 amemb =
1035 *(const void *const *)((const char *)aptr + elm->memb_offset);
1036 bmemb =
1037 *(const void *const *)((const char *)bptr + elm->memb_offset);
1038 if(!amemb) {
1039 if(!bmemb) continue;
1040 return -1;
1041 } else if(!bmemb) {
1042 return 1;
1043 }
1044 } else {
1045 amemb = (const void *)((const char *)aptr + elm->memb_offset);
1046 bmemb = (const void *)((const char *)bptr + elm->memb_offset);
1047 }
1048
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001049 ret = elm->type->op->compare_struct(elm->type, amemb, bmemb);
Lev Walkincd2f48e2017-08-10 02:14:59 -07001050 if(ret != 0) return ret;
1051 }
1052
1053 return 0;
1054}
1055
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001056
1057asn_TYPE_operation_t asn_OP_SET = {
1058 SET_free,
1059 SET_print,
1060 SET_compare,
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001061 SET_decode_ber,
1062 SET_encode_der,
1063 SET_decode_xer,
1064 SET_encode_xer,
Lev Walkinca0d40b2017-08-24 13:38:05 -07001065 0, /* SET_decode_oer */
1066 0, /* SET_encode_oer */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001067 0, /* SET_decode_uper */
1068 0, /* SET_encode_uper */
Lev Walkina5972be2017-09-29 23:15:58 -07001069 SET_random_fill,
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +08001070 0 /* Use generic outmost tag fetcher */
1071};
1072
Lev Walkina5972be2017-09-29 23:15:58 -07001073
1074asn_random_fill_result_t
1075SET_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
1076 const asn_encoding_constraints_t *constr,
1077 size_t max_length) {
1078 const asn_SET_specifics_t *specs =
1079 (const asn_SET_specifics_t *)td->specifics;
1080 asn_random_fill_result_t result_ok = {ARFILL_OK, 0};
1081 asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
1082 asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
1083 void *st = *sptr;
1084 size_t edx;
1085
1086 if(max_length == 0) return result_skipped;
1087
1088 (void)constr;
1089
1090 if(st == NULL) {
1091 st = CALLOC(1, specs->struct_size);
1092 if(st == NULL) {
1093 return result_failed;
1094 }
1095 }
1096
1097 for(edx = 0; edx < td->elements_count; edx++) {
1098 const asn_TYPE_member_t *elm = &td->elements[edx];
1099 void *memb_ptr; /* Pointer to the member */
1100 void **memb_ptr2; /* Pointer to that pointer */
1101 asn_random_fill_result_t tmpres;
1102
1103 if(elm->optional && asn_random_between(0, 4) == 2) {
1104 /* Sometimes decide not to fill the optional value */
1105 continue;
1106 }
1107
1108 if(elm->flags & ATF_POINTER) {
1109 /* Member is a pointer to another structure */
1110 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
1111 } else {
1112 memb_ptr = (char *)st + elm->memb_offset;
1113 memb_ptr2 = &memb_ptr;
1114 }
1115
1116 tmpres = elm->type->op->random_fill(
1117 elm->type, memb_ptr2, &elm->encoding_constraints,
1118 max_length > result_ok.length ? max_length - result_ok.length : 0);
1119 switch(tmpres.code) {
1120 case ARFILL_OK:
1121 result_ok.length += tmpres.length;
1122 continue;
1123 case ARFILL_SKIPPED:
1124 assert(!(elm->flags & ATF_POINTER) || *memb_ptr2 == NULL);
1125 continue;
1126 case ARFILL_FAILED:
1127 if(st == *sptr) {
1128 ASN_STRUCT_RESET(*td, st);
1129 } else {
1130 ASN_STRUCT_FREE(*td, st);
1131 }
1132 return tmpres;
1133 }
1134 }
1135
1136 *sptr = st;
1137
1138 return result_ok;
1139}
1140