blob: f82ebc698abab47407ffdade62f9ad6143f12713 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
Lev Walkin62258e22007-06-23 23:50:25 +00002 * Copyright (c) 2003, 2004, 2005, 2006, 2007 Lev Walkin <vlm@lionet.info>.
Lev Walkinc61f3862005-02-14 17:21:22 +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_SEQUENCE.h>
8
9/*
10 * Number of bytes left for this structure.
11 * (ctx->left) indicates the number of bytes _transferred_ for the structure.
12 * (size) contains the number of bytes in the buffer passed.
13 */
Lev Walkinec1ffd42004-08-18 04:53:32 +000014#define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left)
Lev Walkinf15320b2004-06-03 03:38:44 +000015
16/*
17 * If the subprocessor function returns with an indication that it wants
18 * more data, it may well be a fatal decoding problem, because the
19 * size is constrained by the <TLV>'s L, even if the buffer size allows
20 * reading more data.
21 * For example, consider the buffer containing the following TLVs:
22 * <T:5><L:1><V> <T:6>...
23 * The TLV length clearly indicates that one byte is expected in V, but
24 * if the V processor returns with "want more data" even if the buffer
25 * contains way more data than the V processor have seen.
26 */
Lev Walkind9bd7752004-06-05 08:17:50 +000027#define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size)
Lev Walkinf15320b2004-06-03 03:38:44 +000028
29/*
30 * This macro "eats" the part of the buffer which is definitely "consumed",
31 * i.e. was correctly converted into local representation or rightfully skipped.
32 */
Lev Walkincc6a9102004-09-23 22:06:26 +000033#undef ADVANCE
Lev Walkinf15320b2004-06-03 03:38:44 +000034#define ADVANCE(num_bytes) do { \
35 size_t num = num_bytes; \
Lev Walkin8c3b8542005-03-10 18:52:02 +000036 ptr = ((const char *)ptr) + num;\
Lev Walkinf15320b2004-06-03 03:38:44 +000037 size -= num; \
38 if(ctx->left >= 0) \
39 ctx->left -= num; \
40 consumed_myself += num; \
41 } while(0)
42
43/*
44 * Switch to the next phase of parsing.
45 */
Lev Walkincc6a9102004-09-23 22:06:26 +000046#undef NEXT_PHASE
47#undef PHASE_OUT
Lev Walkinf15320b2004-06-03 03:38:44 +000048#define NEXT_PHASE(ctx) do { \
49 ctx->phase++; \
50 ctx->step = 0; \
51 } while(0)
52#define PHASE_OUT(ctx) do { ctx->phase = 10; } while(0)
53
54/*
55 * Return a standardized complex structure.
56 */
Lev Walkincc6a9102004-09-23 22:06:26 +000057#undef RETURN
Lev Walkinf15320b2004-06-03 03:38:44 +000058#define RETURN(_code) do { \
59 rval.code = _code; \
60 rval.consumed = consumed_myself;\
61 return rval; \
62 } while(0)
63
64/*
65 * Check whether we are inside the extensions group.
66 */
67#define IN_EXTENSION_GROUP(specs, memb_idx) \
68 ( ((memb_idx) > (specs)->ext_after) \
69 &&((memb_idx) < (specs)->ext_before))
70
Lev Walkin4c36e302004-06-06 07:20:02 +000071
72/*
73 * Tags are canonically sorted in the tag2element map.
74 */
75static int
76_t2e_cmp(const void *ap, const void *bp) {
Lev Walkin5e033762004-09-29 13:26:15 +000077 const asn_TYPE_tag2member_t *a = (const asn_TYPE_tag2member_t *)ap;
78 const asn_TYPE_tag2member_t *b = (const asn_TYPE_tag2member_t *)bp;
Lev Walkinc2346572004-08-11 09:07:36 +000079
Lev Walkin4c36e302004-06-06 07:20:02 +000080 int a_class = BER_TAG_CLASS(a->el_tag);
81 int b_class = BER_TAG_CLASS(b->el_tag);
82
83 if(a_class == b_class) {
84 ber_tlv_tag_t a_value = BER_TAG_VALUE(a->el_tag);
85 ber_tlv_tag_t b_value = BER_TAG_VALUE(b->el_tag);
86
Lev Walkin924fa032004-06-14 13:42:23 +000087 if(a_value == b_value) {
Lev Walkin50299c12004-06-14 14:11:14 +000088 if(a->el_no > b->el_no)
89 return 1;
Lev Walkin924fa032004-06-14 13:42:23 +000090 /*
91 * Important: we do not check
Lev Walkin50299c12004-06-14 14:11:14 +000092 * for a->el_no <= b->el_no!
Lev Walkin924fa032004-06-14 13:42:23 +000093 */
Lev Walkin4c36e302004-06-06 07:20:02 +000094 return 0;
Lev Walkin924fa032004-06-14 13:42:23 +000095 } else if(a_value < b_value)
Lev Walkin4c36e302004-06-06 07:20:02 +000096 return -1;
97 else
98 return 1;
99 } else if(a_class < b_class) {
100 return -1;
101 } else {
102 return 1;
103 }
104}
105
106
Lev Walkinf15320b2004-06-03 03:38:44 +0000107/*
108 * The decoder of the SEQUENCE type.
109 */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000110asn_dec_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000111SEQUENCE_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000112 void **struct_ptr, const void *ptr, size_t size, int tag_mode) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000113 /*
114 * Bring closer parts of structure description.
115 */
Lev Walkin5e033762004-09-29 13:26:15 +0000116 asn_SEQUENCE_specifics_t *specs = (asn_SEQUENCE_specifics_t *)td->specifics;
117 asn_TYPE_member_t *elements = td->elements;
Lev Walkinf15320b2004-06-03 03:38:44 +0000118
119 /*
120 * Parts of the structure being constructed.
121 */
122 void *st = *struct_ptr; /* Target structure. */
Lev Walkin5e033762004-09-29 13:26:15 +0000123 asn_struct_ctx_t *ctx; /* Decoder context */
Lev Walkinf15320b2004-06-03 03:38:44 +0000124
125 ber_tlv_tag_t tlv_tag; /* T from TLV */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000126 asn_dec_rval_t rval; /* Return code from subparsers */
Lev Walkinf15320b2004-06-03 03:38:44 +0000127
128 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
129 int edx; /* SEQUENCE element's index */
130
Lev Walkin449f8322004-08-20 13:23:42 +0000131 ASN_DEBUG("Decoding %s as SEQUENCE", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000132
133 /*
134 * Create the target structure if it is not present already.
135 */
136 if(st == 0) {
137 st = *struct_ptr = CALLOC(1, specs->struct_size);
138 if(st == 0) {
139 RETURN(RC_FAIL);
140 }
141 }
142
143 /*
144 * Restore parsing context.
145 */
Lev Walkin5e033762004-09-29 13:26:15 +0000146 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000147
148 /*
149 * Start to parse where left previously
150 */
151 switch(ctx->phase) {
152 case 0:
153 /*
154 * PHASE 0.
155 * Check that the set of tags associated with given structure
156 * perfectly fits our expectations.
157 */
158
Lev Walkin5e033762004-09-29 13:26:15 +0000159 rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size,
Lev Walkin8e8078a2004-09-26 13:10:40 +0000160 tag_mode, 1, &ctx->left, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000161 if(rval.code != RC_OK) {
162 ASN_DEBUG("%s tagging check failed: %d",
Lev Walkin449f8322004-08-20 13:23:42 +0000163 td->name, rval.code);
Lev Walkin566a5672004-10-05 06:36:57 +0000164 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000165 }
166
167 if(ctx->left >= 0)
168 ctx->left += rval.consumed; /* ?Substracted below! */
169 ADVANCE(rval.consumed);
170
171 NEXT_PHASE(ctx);
172
173 ASN_DEBUG("Structure consumes %ld bytes, buffer %ld",
174 (long)ctx->left, (long)size);
175
176 /* Fall through */
177 case 1:
178 /*
179 * PHASE 1.
180 * From the place where we've left it previously,
181 * try to decode the next member from the list of
182 * this structure's elements.
183 * (ctx->step) stores the member being processed
184 * between invocations and the microphase {0,1} of parsing
185 * that member:
186 * step = (<member_number> * 2 + <microphase>).
187 */
Lev Walkin449f8322004-08-20 13:23:42 +0000188 for(edx = (ctx->step >> 1); edx < td->elements_count;
Lev Walkinf15320b2004-06-03 03:38:44 +0000189 edx++, ctx->step = (ctx->step & ~1) + 2) {
190 void *memb_ptr; /* Pointer to the member */
Lev Walkinc2346572004-08-11 09:07:36 +0000191 void **memb_ptr2; /* Pointer to that pointer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000192 ssize_t tag_len; /* Length of TLV's T */
193 int opt_edx_end; /* Next non-optional element */
Lev Walkin4c36e302004-06-06 07:20:02 +0000194 int use_bsearch;
Lev Walkinf15320b2004-06-03 03:38:44 +0000195 int n;
196
197 if(ctx->step & 1)
198 goto microphase2;
199
200 /*
201 * MICROPHASE 1: Synchronize decoding.
202 */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000203 ASN_DEBUG("In %s SEQUENCE left %d, edx=%d flags=%d"
204 " opt=%d ec=%d",
205 td->name, (int)ctx->left, edx,
206 elements[edx].flags, elements[edx].optional,
207 td->elements_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000208
209 if(ctx->left == 0 /* No more stuff is expected */
210 && (
211 /* Explicit OPTIONAL specification reaches the end */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000212 (edx + elements[edx].optional
213 == td->elements_count)
Lev Walkinf15320b2004-06-03 03:38:44 +0000214 ||
215 /* All extensions are optional */
216 (IN_EXTENSION_GROUP(specs, edx)
Lev Walkin449f8322004-08-20 13:23:42 +0000217 && specs->ext_before > td->elements_count)
Lev Walkinf15320b2004-06-03 03:38:44 +0000218 )
219 ) {
Lev Walkin449f8322004-08-20 13:23:42 +0000220 ASN_DEBUG("End of SEQUENCE %s", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000221 /*
222 * Found the legitimate end of the structure.
223 */
224 PHASE_OUT(ctx);
225 RETURN(RC_OK);
226 }
227
228 /*
229 * Fetch the T from TLV.
230 */
231 tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
Lev Walkin906654e2004-09-10 15:49:15 +0000232 ASN_DEBUG("Current tag in %s SEQUENCE for element %d "
Lev Walkincc6a9102004-09-23 22:06:26 +0000233 "(%s) is %s encoded in %d bytes, of frame %ld",
Lev Walkin906654e2004-09-10 15:49:15 +0000234 td->name, edx, elements[edx].name,
235 ber_tlv_tag_string(tlv_tag), (int)tag_len, (long)LEFT);
Lev Walkinf15320b2004-06-03 03:38:44 +0000236 switch(tag_len) {
237 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
238 /* Fall through */
239 case -1: RETURN(RC_FAIL);
240 }
241
Lev Walkin8c3b8542005-03-10 18:52:02 +0000242 if(ctx->left < 0 && ((const uint8_t *)ptr)[0] == 0) {
Lev Walkin566a5672004-10-05 06:36:57 +0000243 if(LEFT < 2) {
244 if(SIZE_VIOLATION)
245 RETURN(RC_FAIL);
246 else
247 RETURN(RC_WMORE);
Lev Walkin8c3b8542005-03-10 18:52:02 +0000248 } else if(((const uint8_t *)ptr)[1] == 0) {
Lev Walkin566a5672004-10-05 06:36:57 +0000249 ASN_DEBUG("edx = %d, opt = %d, ec=%d",
250 edx, elements[edx].optional,
251 td->elements_count);
252 if((edx + elements[edx].optional
253 == td->elements_count)
254 || (IN_EXTENSION_GROUP(specs, edx)
255 && specs->ext_before
256 > td->elements_count)) {
257 /*
258 * Yeah, baby! Found the terminator
259 * of the indefinite length structure.
260 */
261 /*
262 * Proceed to the canonical
263 * finalization function.
264 * No advancing is necessary.
265 */
266 goto phase3;
267 }
268 }
269 }
270
Lev Walkinf15320b2004-06-03 03:38:44 +0000271 /*
272 * Find the next available type with this tag.
273 */
Lev Walkin4c36e302004-06-06 07:20:02 +0000274 use_bsearch = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000275 opt_edx_end = edx + elements[edx].optional + 1;
Lev Walkin449f8322004-08-20 13:23:42 +0000276 if(opt_edx_end > td->elements_count)
277 opt_edx_end = td->elements_count; /* Cap */
Lev Walkinf9dbd9a2004-07-01 00:48:04 +0000278 else if(opt_edx_end - edx > 8) {
Lev Walkin924fa032004-06-14 13:42:23 +0000279 /* Limit the scope of linear search... */
Lev Walkinf9dbd9a2004-07-01 00:48:04 +0000280 opt_edx_end = edx + 8;
Lev Walkin4c36e302004-06-06 07:20:02 +0000281 use_bsearch = 1;
Lev Walkin924fa032004-06-14 13:42:23 +0000282 /* ... and resort to bsearch() */
Lev Walkin4c36e302004-06-06 07:20:02 +0000283 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000284 for(n = edx; n < opt_edx_end; n++) {
285 if(BER_TAGS_EQUAL(tlv_tag, elements[n].tag)) {
286 /*
287 * Found element corresponding to the tag
288 * being looked at.
289 * Reposition over the right element.
290 */
291 edx = n;
Lev Walkin4c36e302004-06-06 07:20:02 +0000292 ctx->step = 1 + 2 * edx; /* Remember! */
293 goto microphase2;
Lev Walkinb9189732004-09-10 09:37:12 +0000294 } else if(elements[n].flags & ATF_OPEN_TYPE) {
295 /*
296 * This is the ANY type, which may bear
297 * any flag whatsoever.
298 */
299 edx = n;
300 ctx->step = 1 + 2 * edx; /* Remember! */
301 goto microphase2;
Lev Walkin4c36e302004-06-06 07:20:02 +0000302 } else if(elements[n].tag == (ber_tlv_tag_t)-1) {
303 use_bsearch = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000304 break;
305 }
306 }
Lev Walkin4c36e302004-06-06 07:20:02 +0000307 if(use_bsearch) {
308 /*
Lev Walkin4e0704f2004-09-04 06:17:35 +0000309 * Resort to a binary search over
Lev Walkin4c36e302004-06-06 07:20:02 +0000310 * sorted array of tags.
311 */
Lev Walkin5e033762004-09-29 13:26:15 +0000312 asn_TYPE_tag2member_t *t2m;
313 asn_TYPE_tag2member_t key;
Lev Walkin4c36e302004-06-06 07:20:02 +0000314 key.el_tag = tlv_tag;
Lev Walkin924fa032004-06-14 13:42:23 +0000315 key.el_no = edx;
Lev Walkinc17d90f2005-01-17 14:32:45 +0000316 t2m = (asn_TYPE_tag2member_t *)bsearch(&key,
Lev Walkinc2346572004-08-11 09:07:36 +0000317 specs->tag2el, specs->tag2el_count,
Lev Walkin4c36e302004-06-06 07:20:02 +0000318 sizeof(specs->tag2el[0]), _t2e_cmp);
Lev Walkin924fa032004-06-14 13:42:23 +0000319 if(t2m) {
Lev Walkin5e033762004-09-29 13:26:15 +0000320 asn_TYPE_tag2member_t *best = 0;
321 asn_TYPE_tag2member_t *t2m_f, *t2m_l;
Lev Walkin924fa032004-06-14 13:42:23 +0000322 int edx_max = edx + elements[edx].optional;
Lev Walkin4c36e302004-06-06 07:20:02 +0000323 /*
324 * Rewind to the first element with that tag,
325 * `cause bsearch() does not guarantee order.
326 */
Lev Walkin924fa032004-06-14 13:42:23 +0000327 t2m_f = t2m + t2m->toff_first;
328 t2m_l = t2m + t2m->toff_last;
329 for(t2m = t2m_f; t2m <= t2m_l; t2m++) {
330 if(t2m->el_no > edx_max) break;
331 if(t2m->el_no < edx) continue;
332 best = t2m;
333 }
334 if(best) {
335 edx = best->el_no;
336 ctx->step = 1 + 2 * edx;
337 goto microphase2;
338 }
Lev Walkin4c36e302004-06-06 07:20:02 +0000339 }
340 n = opt_edx_end;
341 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000342 if(n == opt_edx_end) {
343 /*
344 * If tag is unknown, it may be either
345 * an unknown (thus, incorrect) tag,
346 * or an extension (...),
347 * or an end of the indefinite-length structure.
348 */
Lev Walkin7cbbc902006-10-03 06:39:36 +0000349 if(!IN_EXTENSION_GROUP(specs,
350 edx + elements[edx].optional)) {
Lev Walkin566a5672004-10-05 06:36:57 +0000351 ASN_DEBUG("Unexpected tag %s (at %d)",
352 ber_tlv_tag_string(tlv_tag), edx);
Lev Walkin4c36e302004-06-06 07:20:02 +0000353 ASN_DEBUG("Expected tag %s (%s)%s",
Lev Walkinf15320b2004-06-03 03:38:44 +0000354 ber_tlv_tag_string(elements[edx].tag),
Lev Walkin4c36e302004-06-06 07:20:02 +0000355 elements[edx].name,
Lev Walkinf15320b2004-06-03 03:38:44 +0000356 elements[edx].optional
357 ?" or alternatives":"");
358 RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000359 } else {
360 /* Skip this tag */
361 ssize_t skip;
Lev Walkin7cbbc902006-10-03 06:39:36 +0000362 edx += elements[edx].optional;
Lev Walkinf15320b2004-06-03 03:38:44 +0000363
Lev Walkin7cbbc902006-10-03 06:39:36 +0000364 ASN_DEBUG("Skipping unexpected %s (at %d)",
365 ber_tlv_tag_string(tlv_tag), edx);
Lev Walkinbaaa24f2004-09-29 14:19:14 +0000366 skip = ber_skip_length(opt_codec_ctx,
Lev Walkinf15320b2004-06-03 03:38:44 +0000367 BER_TLV_CONSTRUCTED(ptr),
Lev Walkin8c3b8542005-03-10 18:52:02 +0000368 (const char *)ptr + tag_len,
369 LEFT - tag_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000370 ASN_DEBUG("Skip length %d in %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000371 (int)skip, td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000372 switch(skip) {
373 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
374 /* Fall through */
375 case -1: RETURN(RC_FAIL);
376 }
377
378 ADVANCE(skip + tag_len);
379 ctx->step -= 2;
380 edx--;
381 continue; /* Try again with the next tag */
382 }
383 }
384
385 /*
386 * MICROPHASE 2: Invoke the member-specific decoder.
387 */
388 ctx->step |= 1; /* Confirm entering next microphase */
389 microphase2:
Lev Walkin449f8322004-08-20 13:23:42 +0000390 ASN_DEBUG("Inside SEQUENCE %s MF2", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000391
392 /*
393 * Compute the position of the member inside a structure,
394 * and also a type of containment (it may be contained
395 * as pointer or using inline inclusion).
396 */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000397 if(elements[edx].flags & ATF_POINTER) {
398 /* Member is a pointer to another structure */
Lev Walkinc2346572004-08-11 09:07:36 +0000399 memb_ptr2 = (void **)((char *)st + elements[edx].memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000400 } else {
401 /*
402 * A pointer to a pointer
403 * holding the start of the structure
404 */
405 memb_ptr = (char *)st + elements[edx].memb_offset;
406 memb_ptr2 = &memb_ptr;
407 }
408 /*
409 * Invoke the member fetch routine according to member's type
410 */
Lev Walkin5e033762004-09-29 13:26:15 +0000411 rval = elements[edx].type->ber_decoder(opt_codec_ctx,
Lev Walkinc2346572004-08-11 09:07:36 +0000412 elements[edx].type,
Lev Walkinf15320b2004-06-03 03:38:44 +0000413 memb_ptr2, ptr, LEFT,
414 elements[edx].tag_mode);
Lev Walkin60b7cff2004-09-04 04:44:11 +0000415 ASN_DEBUG("In %s SEQUENCE decoded %d %s of %d "
Lev Walkin5beb7de2004-09-24 20:59:27 +0000416 "in %d bytes rval.code %d, size=%d",
Lev Walkin449f8322004-08-20 13:23:42 +0000417 td->name, edx, elements[edx].type->name,
Lev Walkin5beb7de2004-09-24 20:59:27 +0000418 (int)LEFT, (int)rval.consumed, rval.code, (int)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000419 switch(rval.code) {
420 case RC_OK:
421 break;
422 case RC_WMORE: /* More data expected */
423 if(!SIZE_VIOLATION) {
424 ADVANCE(rval.consumed);
425 RETURN(RC_WMORE);
426 }
Lev Walkin5beb7de2004-09-24 20:59:27 +0000427 ASN_DEBUG("Size violation (c->l=%ld <= s=%ld)",
428 (long)ctx->left, (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000429 /* Fall through */
430 case RC_FAIL: /* Fatal error */
431 RETURN(RC_FAIL);
432 } /* switch(rval) */
433
434 ADVANCE(rval.consumed);
435 } /* for(all structure members) */
436
437 phase3:
438 ctx->phase = 3;
439 case 3: /* 00 and other tags expected */
440 case 4: /* only 00's expected */
441
442 ASN_DEBUG("SEQUENCE %s Leftover: %ld, size = %ld",
Lev Walkin449f8322004-08-20 13:23:42 +0000443 td->name, (long)ctx->left, (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000444
445 /*
446 * Skip everything until the end of the SEQUENCE.
447 */
448 while(ctx->left) {
449 ssize_t tl, ll;
450
451 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
452 switch(tl) {
453 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
454 /* Fall through */
455 case -1: RETURN(RC_FAIL);
456 }
457
458 /*
459 * If expected <0><0>...
460 */
461 if(ctx->left < 0
Lev Walkin8c3b8542005-03-10 18:52:02 +0000462 && ((const uint8_t *)ptr)[0] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000463 if(LEFT < 2) {
464 if(SIZE_VIOLATION)
465 RETURN(RC_FAIL);
466 else
467 RETURN(RC_WMORE);
Lev Walkin8c3b8542005-03-10 18:52:02 +0000468 } else if(((const uint8_t *)ptr)[1] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000469 /*
470 * Correctly finished with <0><0>.
471 */
472 ADVANCE(2);
473 ctx->left++;
474 ctx->phase = 4;
475 continue;
476 }
477 }
478
Lev Walkin449f8322004-08-20 13:23:42 +0000479 if(!IN_EXTENSION_GROUP(specs, td->elements_count)
Lev Walkinf15320b2004-06-03 03:38:44 +0000480 || ctx->phase == 4) {
481 ASN_DEBUG("Unexpected continuation "
482 "of a non-extensible type "
483 "%s (SEQUENCE): %s",
Lev Walkin449f8322004-08-20 13:23:42 +0000484 td->name,
Lev Walkinf15320b2004-06-03 03:38:44 +0000485 ber_tlv_tag_string(tlv_tag));
486 RETURN(RC_FAIL);
487 }
488
Lev Walkinbaaa24f2004-09-29 14:19:14 +0000489 ll = ber_skip_length(opt_codec_ctx,
Lev Walkinf15320b2004-06-03 03:38:44 +0000490 BER_TLV_CONSTRUCTED(ptr),
Lev Walkin8c3b8542005-03-10 18:52:02 +0000491 (const char *)ptr + tl, LEFT - tl);
Lev Walkinf15320b2004-06-03 03:38:44 +0000492 switch(ll) {
493 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
494 /* Fall through */
495 case -1: RETURN(RC_FAIL);
496 }
497
498 ADVANCE(tl + ll);
499 }
500
501 PHASE_OUT(ctx);
502 }
503
504 RETURN(RC_OK);
505}
506
Lev Walkin4c36e302004-06-06 07:20:02 +0000507
Lev Walkinf15320b2004-06-03 03:38:44 +0000508/*
509 * The DER encoder of the SEQUENCE type.
510 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000511asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000512SEQUENCE_encode_der(asn_TYPE_descriptor_t *td,
Lev Walkinac589332005-08-22 14:19:28 +0000513 void *sptr, int tag_mode, ber_tlv_tag_t tag,
Lev Walkinf15320b2004-06-03 03:38:44 +0000514 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000515 size_t computed_size = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000516 asn_enc_rval_t erval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000517 ssize_t ret;
518 int edx;
519
520 ASN_DEBUG("%s %s as SEQUENCE",
Lev Walkin449f8322004-08-20 13:23:42 +0000521 cb?"Encoding":"Estimating", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000522
523 /*
524 * Gather the length of the underlying members sequence.
525 */
Lev Walkin449f8322004-08-20 13:23:42 +0000526 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000527 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000528 void *memb_ptr;
Lev Walkincc93b0f2004-09-10 09:18:20 +0000529 if(elm->flags & ATF_POINTER) {
Lev Walkinac589332005-08-22 14:19:28 +0000530 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
531 if(!memb_ptr) {
532 if(elm->optional) continue;
533 /* Mandatory element is missing */
534 _ASN_ENCODE_FAILED;
535 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000536 } else {
Lev Walkinac589332005-08-22 14:19:28 +0000537 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000538 }
539 erval = elm->type->der_encoder(elm->type, memb_ptr,
540 elm->tag_mode, elm->tag,
541 0, 0);
542 if(erval.encoded == -1)
543 return erval;
544 computed_size += erval.encoded;
545 ASN_DEBUG("Member %d %s estimated %ld bytes",
546 edx, elm->name, (long)erval.encoded);
547 }
548
549 /*
550 * Encode the TLV for the sequence itself.
551 */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000552 ret = der_write_tags(td, computed_size, tag_mode, 1, tag, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +0000553 ASN_DEBUG("Wrote tags: %ld (+%ld)", (long)ret, (long)computed_size);
Lev Walkinac589332005-08-22 14:19:28 +0000554 if(ret == -1)
555 _ASN_ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000556 erval.encoded = computed_size + ret;
557
Lev Walkin59b176e2005-11-26 11:25:14 +0000558 if(!cb) _ASN_ENCODED_OK(erval);
Lev Walkinf15320b2004-06-03 03:38:44 +0000559
560 /*
561 * Encode all members.
562 */
Lev Walkin449f8322004-08-20 13:23:42 +0000563 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000564 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkina9cc46e2004-09-22 16:06:28 +0000565 asn_enc_rval_t tmperval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000566 void *memb_ptr;
567
Lev Walkincc93b0f2004-09-10 09:18:20 +0000568 if(elm->flags & ATF_POINTER) {
Lev Walkinac589332005-08-22 14:19:28 +0000569 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000570 if(!memb_ptr) continue;
571 } else {
Lev Walkinac589332005-08-22 14:19:28 +0000572 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000573 }
574 tmperval = elm->type->der_encoder(elm->type, memb_ptr,
575 elm->tag_mode, elm->tag,
576 cb, app_key);
577 if(tmperval.encoded == -1)
578 return tmperval;
579 computed_size -= tmperval.encoded;
Lev Walkina6a926a2005-03-02 01:54:28 +0000580 ASN_DEBUG("Member %d %s of SEQUENCE %s encoded in %ld bytes",
581 edx, elm->name, td->name, (long)tmperval.encoded);
Lev Walkinf15320b2004-06-03 03:38:44 +0000582 }
583
Lev Walkinac589332005-08-22 14:19:28 +0000584 if(computed_size != 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000585 /*
586 * Encoded size is not equal to the computed size.
587 */
Lev Walkinac589332005-08-22 14:19:28 +0000588 _ASN_ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000589
Lev Walkin59b176e2005-11-26 11:25:14 +0000590 _ASN_ENCODED_OK(erval);
Lev Walkinf15320b2004-06-03 03:38:44 +0000591}
592
Lev Walkinc61f3862005-02-14 17:21:22 +0000593
Lev Walkin1bbc2002004-10-23 13:27:30 +0000594#undef XER_ADVANCE
595#define XER_ADVANCE(num_bytes) do { \
596 size_t num = num_bytes; \
Lev Walkin8c3b8542005-03-10 18:52:02 +0000597 buf_ptr = ((const char *)buf_ptr) + num;\
Lev Walkin1bbc2002004-10-23 13:27:30 +0000598 size -= num; \
599 consumed_myself += num; \
600 } while(0)
601
Lev Walkinc61f3862005-02-14 17:21:22 +0000602/*
603 * Decode the XER (XML) data.
604 */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000605asn_dec_rval_t
606SEQUENCE_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
607 void **struct_ptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000608 const void *buf_ptr, size_t size) {
Lev Walkin1bbc2002004-10-23 13:27:30 +0000609 /*
610 * Bring closer parts of structure description.
611 */
Lev Walkinc61f3862005-02-14 17:21:22 +0000612 asn_SEQUENCE_specifics_t *specs
613 = (asn_SEQUENCE_specifics_t *)td->specifics;
Lev Walkin1bbc2002004-10-23 13:27:30 +0000614 asn_TYPE_member_t *elements = td->elements;
615 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
616
617 /*
Lev Walkinc61f3862005-02-14 17:21:22 +0000618 * ... and parts of the structure being constructed.
Lev Walkin1bbc2002004-10-23 13:27:30 +0000619 */
620 void *st = *struct_ptr; /* Target structure. */
621 asn_struct_ctx_t *ctx; /* Decoder context */
622
Lev Walkinc61f3862005-02-14 17:21:22 +0000623 asn_dec_rval_t rval; /* Return value from a decoder */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000624 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000625 int edx; /* Element index */
Lev Walkin904e65b2005-02-18 14:23:48 +0000626 int edx_end;
Lev Walkin1bbc2002004-10-23 13:27:30 +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 */
639 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
640
641
642 /*
643 * Phases of XER/XML processing:
644 * Phase 0: Check that the opening tag matches our expectations.
645 * Phase 1: Processing body and reacting on closing tag.
646 * Phase 2: Processing inner type.
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000647 * Phase 3: Skipping unknown extensions.
648 * Phase 4: PHASED OUT
Lev Walkin1bbc2002004-10-23 13:27:30 +0000649 */
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000650 for(edx = ctx->step; ctx->phase <= 3;) {
Lev Walkin1bbc2002004-10-23 13:27:30 +0000651 pxer_chunk_type_e ch_type; /* XER chunk type */
652 ssize_t ch_size; /* Chunk size */
653 xer_check_tag_e tcv; /* Tag check value */
654 asn_TYPE_member_t *elm;
Lev Walkin904e65b2005-02-18 14:23:48 +0000655 int n;
Lev Walkin1bbc2002004-10-23 13:27:30 +0000656
657 /*
Lev Walkinc61f3862005-02-14 17:21:22 +0000658 * Go inside the inner member of a sequence.
Lev Walkin1bbc2002004-10-23 13:27:30 +0000659 */
660 if(ctx->phase == 2) {
661 asn_dec_rval_t tmprval;
Lev Walkin1bbc2002004-10-23 13:27:30 +0000662 void *memb_ptr; /* Pointer to the member */
663 void **memb_ptr2; /* Pointer to that pointer */
664
Lev Walkinabf68892004-10-26 10:12:14 +0000665 elm = &td->elements[edx];
666
Lev Walkin1bbc2002004-10-23 13:27:30 +0000667 if(elm->flags & ATF_POINTER) {
668 /* Member is a pointer to another structure */
669 memb_ptr2 = (void **)((char *)st
670 + elm->memb_offset);
671 } else {
672 memb_ptr = (char *)st + elm->memb_offset;
673 memb_ptr2 = &memb_ptr;
674 }
675
Lev Walkinc61f3862005-02-14 17:21:22 +0000676 /* Invoke the inner type decoder, m.b. multiple times */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000677 tmprval = elm->type->xer_decoder(opt_codec_ctx,
678 elm->type, memb_ptr2, elm->name,
679 buf_ptr, size);
680 XER_ADVANCE(tmprval.consumed);
681 if(tmprval.code != RC_OK)
682 RETURN(tmprval.code);
Lev Walkinc61f3862005-02-14 17:21:22 +0000683 ctx->phase = 1; /* Back to body processing */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000684 ctx->step = ++edx;
Lev Walkinc61f3862005-02-14 17:21:22 +0000685 ASN_DEBUG("XER/SEQUENCE phase => %d, step => %d",
686 ctx->phase, ctx->step);
Lev Walkin1bbc2002004-10-23 13:27:30 +0000687 /* Fall through */
688 }
689
690 /*
691 * Get the next part of the XML stream.
692 */
Lev Walkin1e443962005-02-18 18:06:36 +0000693 ch_size = xer_next_token(&ctx->context, buf_ptr, size,
694 &ch_type);
Lev Walkin1bbc2002004-10-23 13:27:30 +0000695 switch(ch_size) {
696 case -1: RETURN(RC_FAIL);
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000697 case 0: RETURN(RC_WMORE);
Lev Walkin1bbc2002004-10-23 13:27:30 +0000698 default:
699 switch(ch_type) {
700 case PXER_COMMENT: /* Got XML comment */
701 case PXER_TEXT: /* Ignore free-standing text */
702 XER_ADVANCE(ch_size); /* Skip silently */
703 continue;
704 case PXER_TAG:
705 break; /* Check the rest down there */
706 }
707 }
708
Lev Walkinc61f3862005-02-14 17:21:22 +0000709 tcv = xer_check_tag(buf_ptr, ch_size, xml_tag);
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000710 ASN_DEBUG("XER/SEQUENCE: tcv = %d, ph=%d [%s]",
711 tcv, ctx->phase, xml_tag);
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000712
713 /* Skip the extensions section */
714 if(ctx->phase == 3) {
715 switch(xer_skip_unknown(tcv, &ctx->left)) {
716 case -1:
717 ctx->phase = 4;
718 RETURN(RC_FAIL);
719 case 0:
720 XER_ADVANCE(ch_size);
721 continue;
722 case 1:
723 XER_ADVANCE(ch_size);
724 ctx->phase = 1;
725 continue;
726 case 2:
727 ctx->phase = 1;
728 break;
729 }
730 }
731
Lev Walkin1bbc2002004-10-23 13:27:30 +0000732 switch(tcv) {
733 case XCT_CLOSING:
734 if(ctx->phase == 0) break;
Lev Walkin1bbc2002004-10-23 13:27:30 +0000735 ctx->phase = 0;
736 /* Fall through */
737 case XCT_BOTH:
738 if(ctx->phase == 0) {
739 if(edx >= td->elements_count
740 ||
741 /* Explicit OPTIONAL specs reaches the end */
742 (edx + elements[edx].optional
743 == td->elements_count)
744 ||
745 /* All extensions are optional */
746 (IN_EXTENSION_GROUP(specs, edx)
747 && specs->ext_before
748 > td->elements_count)
749 ) {
750 XER_ADVANCE(ch_size);
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000751 ctx->phase = 4; /* Phase out */
Lev Walkinc61f3862005-02-14 17:21:22 +0000752 RETURN(RC_OK);
Lev Walkin1bbc2002004-10-23 13:27:30 +0000753 } else {
754 ASN_DEBUG("Premature end of XER SEQUENCE");
755 RETURN(RC_FAIL);
756 }
757 }
758 /* Fall through */
759 case XCT_OPENING:
760 if(ctx->phase == 0) {
761 XER_ADVANCE(ch_size);
762 ctx->phase = 1; /* Processing body phase */
763 continue;
764 }
Lev Walkinda3ca412004-10-23 14:58:15 +0000765 /* Fall through */
Lev Walkin904e65b2005-02-18 14:23:48 +0000766 case XCT_UNKNOWN_OP:
767 case XCT_UNKNOWN_BO:
Lev Walkin1bbc2002004-10-23 13:27:30 +0000768
Lev Walkin02666192005-07-03 05:31:02 +0000769 ASN_DEBUG("XER/SEQUENCE: tcv=%d, ph=%d, edx=%d",
770 tcv, ctx->phase, edx);
771 if(ctx->phase != 1) {
Lev Walkin1bbc2002004-10-23 13:27:30 +0000772 break; /* Really unexpected */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000773 }
Lev Walkin02666192005-07-03 05:31:02 +0000774
775 if(edx < td->elements_count) {
776 /*
777 * Search which member corresponds to this tag.
778 */
779 edx_end = edx + elements[edx].optional + 1;
780 if(edx_end > td->elements_count)
781 edx_end = td->elements_count;
782 for(n = edx; n < edx_end; n++) {
783 elm = &td->elements[n];
784 tcv = xer_check_tag(buf_ptr,
785 ch_size, elm->name);
786 switch(tcv) {
787 case XCT_BOTH:
788 case XCT_OPENING:
789 /*
790 * Process this member.
791 */
792 ctx->step = edx = n;
793 ctx->phase = 2;
794 break;
795 case XCT_UNKNOWN_OP:
796 case XCT_UNKNOWN_BO:
797 continue;
798 default:
799 n = edx_end;
800 break; /* Phase out */
801 }
802 break;
803 }
804 if(n != edx_end)
805 continue;
806 } else {
807 ASN_DEBUG("Out of defined members: %d/%d",
808 edx, td->elements_count);
809 }
Lev Walkin904e65b2005-02-18 14:23:48 +0000810
811 /* It is expected extension */
812 if(IN_EXTENSION_GROUP(specs,
Lev Walkin02666192005-07-03 05:31:02 +0000813 edx + (edx < td->elements_count
814 ? elements[edx].optional : 0))) {
815 ASN_DEBUG("Got anticipated extension at %d",
816 edx);
Lev Walkin904e65b2005-02-18 14:23:48 +0000817 /*
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000818 * Check for (XCT_BOTH or XCT_UNKNOWN_BO)
819 * By using a mask. Only record a pure
820 * <opening> tags.
Lev Walkin904e65b2005-02-18 14:23:48 +0000821 */
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000822 if(tcv & XCT_CLOSING) {
823 /* Found </extension> without body */
824 } else {
825 ctx->left = 1;
826 ctx->phase = 3; /* Skip ...'s */
827 }
828 XER_ADVANCE(ch_size);
829 continue;
Lev Walkin904e65b2005-02-18 14:23:48 +0000830 }
831
832 /* Fall through */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000833 default:
834 break;
835 }
836
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000837 ASN_DEBUG("Unexpected XML tag in SEQUENCE [%c%c%c%c%c%c]",
838 size>0?((const char *)buf_ptr)[0]:'.',
839 size>1?((const char *)buf_ptr)[1]:'.',
840 size>2?((const char *)buf_ptr)[2]:'.',
841 size>3?((const char *)buf_ptr)[3]:'.',
842 size>4?((const char *)buf_ptr)[4]:'.',
843 size>5?((const char *)buf_ptr)[5]:'.');
Lev Walkin1bbc2002004-10-23 13:27:30 +0000844 break;
845 }
846
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000847 ctx->phase = 4; /* "Phase out" on hard failure */
Lev Walkin1bbc2002004-10-23 13:27:30 +0000848 RETURN(RC_FAIL);
849}
850
Lev Walkina9cc46e2004-09-22 16:06:28 +0000851asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000852SEQUENCE_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000853 int ilevel, enum xer_encoder_flags_e flags,
854 asn_app_consume_bytes_f *cb, void *app_key) {
855 asn_enc_rval_t er;
856 int xcan = (flags & XER_F_CANONICAL);
857 int edx;
858
859 if(!sptr)
860 _ASN_ENCODE_FAILED;
861
862 er.encoded = 0;
863
864 for(edx = 0; edx < td->elements_count; edx++) {
865 asn_enc_rval_t tmper;
Lev Walkin5e033762004-09-29 13:26:15 +0000866 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkina9cc46e2004-09-22 16:06:28 +0000867 void *memb_ptr;
868 const char *mname = elm->name;
869 unsigned int mlen = strlen(mname);
870
871 if(elm->flags & ATF_POINTER) {
872 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
Lev Walkinac589332005-08-22 14:19:28 +0000873 if(!memb_ptr) {
874 if(elm->optional)
875 continue;
876 /* Mandatory element is missing */
877 _ASN_ENCODE_FAILED;
878 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000879 } else {
880 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
881 }
882
883 if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel);
884 _ASN_CALLBACK3("<", 1, mname, mlen, ">", 1);
885
886 /* Print the member itself */
887 tmper = elm->type->xer_encoder(elm->type, memb_ptr,
888 ilevel + 1, flags, cb, app_key);
889 if(tmper.encoded == -1) return tmper;
890
891 _ASN_CALLBACK3("</", 2, mname, mlen, ">", 1);
892 er.encoded += 5 + (2 * mlen) + tmper.encoded;
893 }
894
895 if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel - 1);
896
Lev Walkin59b176e2005-11-26 11:25:14 +0000897 _ASN_ENCODED_OK(er);
Lev Walkin942fd082004-10-03 09:13:02 +0000898cb_failed:
899 _ASN_ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000900}
901
Lev Walkinf15320b2004-06-03 03:38:44 +0000902int
Lev Walkin5e033762004-09-29 13:26:15 +0000903SEQUENCE_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000904 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000905 int edx;
906 int ret;
907
Lev Walkin8e8078a2004-09-26 13:10:40 +0000908 if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000909
910 /* Dump preamble */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000911 if(cb(td->name, strlen(td->name), app_key) < 0
912 || cb(" ::= {", 6, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000913 return -1;
914
Lev Walkin449f8322004-08-20 13:23:42 +0000915 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000916 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000917 const void *memb_ptr;
918
Lev Walkincc93b0f2004-09-10 09:18:20 +0000919 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000920 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkinac589332005-08-22 14:19:28 +0000921 if(!memb_ptr) {
922 if(elm->optional) continue;
923 /* Print <absent> line */
924 /* Fall through */
925 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000926 } else {
927 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
928 }
929
930 /* Indentation */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000931 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000932
933 /* Print the member's name and stuff */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000934 if(cb(elm->name, strlen(elm->name), app_key) < 0
935 || cb(": ", 2, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000936 return -1;
937
938 /* Print the member itself */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000939 ret = elm->type->print_struct(elm->type, memb_ptr, ilevel + 1,
Lev Walkinf15320b2004-06-03 03:38:44 +0000940 cb, app_key);
941 if(ret) return ret;
Lev Walkinf15320b2004-06-03 03:38:44 +0000942 }
943
Lev Walkin8e8078a2004-09-26 13:10:40 +0000944 ilevel--;
945 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000946
Lev Walkin8e8078a2004-09-26 13:10:40 +0000947 return (cb("}", 1, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000948}
949
950void
Lev Walkin5e033762004-09-29 13:26:15 +0000951SEQUENCE_free(asn_TYPE_descriptor_t *td, void *sptr, int contents_only) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000952 int edx;
953
954 if(!td || !sptr)
955 return;
956
957 ASN_DEBUG("Freeing %s as SEQUENCE", td->name);
958
Lev Walkin449f8322004-08-20 13:23:42 +0000959 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000960 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000961 void *memb_ptr;
Lev Walkincc93b0f2004-09-10 09:18:20 +0000962 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000963 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
964 if(memb_ptr)
Lev Walkinadcb5862006-03-17 02:11:12 +0000965 ASN_STRUCT_FREE(*elm->type, memb_ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +0000966 } else {
967 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkinadcb5862006-03-17 02:11:12 +0000968 ASN_STRUCT_FREE_CONTENTS_ONLY(*elm->type, memb_ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +0000969 }
970 }
971
972 if(!contents_only) {
973 FREEMEM(sptr);
974 }
975}
976
977int
Lev Walkin5e033762004-09-29 13:26:15 +0000978SEQUENCE_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 Walkinf15320b2004-06-03 03:38:44 +0000980 int edx;
981
982 if(!sptr) {
Lev Walkined9019a2006-10-16 12:18:41 +0000983 _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 Walkin4c36e302004-06-06 07:20:02 +0000997 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
Lev Walkinac589332005-08-22 14:19:28 +0000998 if(!memb_ptr) {
999 if(elm->optional)
1000 continue;
Lev Walkined9019a2006-10-16 12:18:41 +00001001 _ASN_CTFAIL(app_key, td, sptr,
Lev Walkinac589332005-08-22 14:19:28 +00001002 "%s: mandatory element %s absent (%s:%d)",
1003 td->name, elm->name, __FILE__, __LINE__);
1004 return -1;
1005 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001006 } else {
1007 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
1008 }
1009
Lev Walkin449f8322004-08-20 13:23:42 +00001010 if(elm->memb_constraints) {
1011 int ret = elm->memb_constraints(elm->type, memb_ptr,
Lev Walkin1eded352006-07-13 11:19:01 +00001012 ctfailcb, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +00001013 if(ret) return ret;
1014 } else {
1015 int ret = elm->type->check_constraints(elm->type,
Lev Walkin1eded352006-07-13 11:19:01 +00001016 memb_ptr, ctfailcb, app_key);
Lev Walkin449f8322004-08-20 13:23:42 +00001017 if(ret) return ret;
1018 /*
1019 * Cannot inherit it earlier:
1020 * need to make sure we get the updated version.
1021 */
1022 elm->memb_constraints = elm->type->check_constraints;
1023 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001024 }
1025
1026 return 0;
1027}
Lev Walkin59b176e2005-11-26 11:25:14 +00001028
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001029/*
1030 * #10.1, #10.2
1031 */
1032static int
1033uper_put_open_type(asn_TYPE_descriptor_t *td, asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
1034 void *buf;
1035 ssize_t size;
1036
1037 ASN_DEBUG("Encoding as open type %s", td->name);
1038 size = uper_encode_to_new_buffer(td, constraints, sptr, &buf);
1039 if(size <= 0) return -1;
1040
1041 ASN_DEBUG("Putting %s of length %d", td->name, size);
1042 while(size) {
1043 ssize_t maySave = uper_put_length(po, size);
1044 if(maySave < 0) break;
1045 if(per_put_many_bits(po, buf, maySave * 8)) break;
1046 buf = (char *)buf + maySave;
1047 size -= maySave;
1048 }
1049
1050 if(size) {
1051 FREEMEM(buf);
1052 return -1;
1053 }
1054
1055 return 0;
1056}
1057
1058typedef struct uper_ugot_key {
1059 asn_per_data_t oldpd; /* Old per data source */
1060 size_t unclaimed;
Lev Walkin5ce11fd2007-06-26 03:16:35 +00001061 size_t ot_moved; /* Number of bits moved by OT processing */
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001062 int repeat;
1063} uper_ugot_key;
1064static int
1065uper_ugot_refill(asn_per_data_t *pd) {
1066 uper_ugot_key *arg = pd->refill_key;
1067 ssize_t next_chunk_bytes, next_chunk_bits;
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001068 ssize_t avail;
1069
1070 asn_per_data_t *oldpd = &arg->oldpd;
1071
Lev Walkin31c46d52007-06-26 06:46:04 +00001072 ASN_DEBUG("REFILLING (%+db) [from %d (%d->%d)_%d] now [%d (%d->%d)_%d] uncl %d",
1073 pd->buffer - oldpd->buffer,
1074 oldpd->nbits - oldpd->nboff, oldpd->nboff, oldpd->nbits, oldpd->moved,
1075 pd->nbits - pd->nboff, pd->nboff, pd->nbits, pd->moved, arg->unclaimed);
Lev Walkin5ce11fd2007-06-26 03:16:35 +00001076
1077 /* Advance our position to where pd is */
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001078 oldpd->buffer = pd->buffer;
Lev Walkin5ce11fd2007-06-26 03:16:35 +00001079 oldpd->nboff = pd->nboff;
1080 oldpd->nbits -= pd->moved - arg->ot_moved;
1081 oldpd->moved += pd->moved - arg->ot_moved;
1082 arg->ot_moved = pd->moved;
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001083
1084 if(arg->unclaimed) {
1085 /* Refill the container */
Lev Walkin7ed25982007-06-24 08:38:34 +00001086 if(per_get_few_bits(oldpd, 1))
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001087 return -1;
Lev Walkin7ed25982007-06-24 08:38:34 +00001088 if(oldpd->nboff == 0) {
1089 assert(0);
1090 return -1;
1091 }
1092 pd->buffer = oldpd->buffer;
1093 pd->nboff = oldpd->nboff - 1;
1094 pd->nbits = oldpd->nbits;
Lev Walkina2987ea2007-06-26 23:56:54 +00001095 ASN_DEBUG("Return from UNCLAIMED");
Lev Walkin7ed25982007-06-24 08:38:34 +00001096 return 0;
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001097 }
1098
1099 if(!arg->repeat) {
1100 ASN_DEBUG("Want more but refill doesn't have it");
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001101 return -1;
1102 }
1103
1104 next_chunk_bytes = uper_get_length(oldpd, -1, &arg->repeat);
Lev Walkin31c46d52007-06-26 06:46:04 +00001105 ASN_DEBUG("Open type LENGTH %d bytes at off %d, repeat %d",
1106 next_chunk_bytes, oldpd->moved, arg->repeat);
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001107 if(next_chunk_bytes < 0) return -1;
Lev Walkin5ce11fd2007-06-26 03:16:35 +00001108 if(next_chunk_bytes == 0) {
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001109 pd->refill = 0; /* No more refills, naturally */
Lev Walkin5ce11fd2007-06-26 03:16:35 +00001110 assert(!arg->repeat); /* Implementation guarantee */
1111 }
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001112 next_chunk_bits = next_chunk_bytes << 3;
Lev Walkin31c46d52007-06-26 06:46:04 +00001113 avail = oldpd->nbits - oldpd->nboff;
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001114 if(avail >= next_chunk_bits) {
Lev Walkin31c46d52007-06-26 06:46:04 +00001115 pd->nbits = oldpd->nboff + next_chunk_bits;
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001116 arg->unclaimed = 0;
1117 } else {
Lev Walkin31c46d52007-06-26 06:46:04 +00001118 pd->nbits = oldpd->nbits;
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001119 arg->unclaimed = next_chunk_bits - avail;
Lev Walkin7ed25982007-06-24 08:38:34 +00001120 ASN_DEBUG("Parent has %d, require %d, will claim %d", avail, next_chunk_bits, arg->unclaimed);
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001121 }
Lev Walkin31c46d52007-06-26 06:46:04 +00001122 pd->buffer = oldpd->buffer;
1123 pd->nboff = oldpd->nboff;
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001124 return 0;
1125}
1126
1127asn_dec_rval_t
1128uper_get_open_type(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
1129 asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
1130 uper_ugot_key arg;
1131 asn_dec_rval_t rv;
1132 ssize_t padding;
1133
1134 _ASN_STACK_OVERFLOW_CHECK(opt_codec_ctx);
1135
Lev Walkina2987ea2007-06-26 23:56:54 +00001136 ASN_DEBUG("Getting open type off %d (%d+%d), %p", pd->moved, pd->nboff, pd->nbits, pd->buffer);
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001137 arg.oldpd = *pd;
Lev Walkin5ce11fd2007-06-26 03:16:35 +00001138 arg.unclaimed = 0;
1139 arg.ot_moved = 0;
1140 arg.repeat = 1;
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001141 pd->refill = uper_ugot_refill;
1142 pd->refill_key = &arg;
Lev Walkin31c46d52007-06-26 06:46:04 +00001143 pd->nbits = pd->nboff; /* 0 good bits at this point, will refill */
1144 pd->moved = 0; /* This now counts the open type size in bits */
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001145
1146 rv = td->uper_decoder(opt_codec_ctx, td, constraints, sptr, pd);
1147
Lev Walkin00e43912007-06-26 07:18:54 +00001148 ASN_DEBUG("Open type %s consumed %d off of %d unclaimed=%d, repeat=%d",
1149 td->name, pd->moved, arg.oldpd.moved,
1150 arg.unclaimed, arg.repeat);
Lev Walkin7ed25982007-06-24 08:38:34 +00001151
Lev Walkina2987ea2007-06-26 23:56:54 +00001152 ASN_DEBUG("OT1 moved %d, estimated %d uncl=%d",
1153 arg.oldpd.moved,
1154 arg.oldpd.nboff + ((((int)arg.oldpd.buffer) & 0x7) << 3),
1155 arg.unclaimed
1156 );
1157
Lev Walkin31c46d52007-06-26 06:46:04 +00001158 padding = pd->moved % 8;
1159 if(padding) {
Lev Walkina2987ea2007-06-26 23:56:54 +00001160 int32_t pvalue;
Lev Walkin31c46d52007-06-26 06:46:04 +00001161 if(padding > 7) {
1162 ASN_DEBUG("Too large padding %d in open type",
1163 padding);
1164 rv.code = RC_FAIL;
1165 return rv;
1166 }
Lev Walkina2987ea2007-06-26 23:56:54 +00001167 padding = 8 - padding;
Lev Walkin31c46d52007-06-26 06:46:04 +00001168 ASN_DEBUG("Getting padding of %d bits", padding);
Lev Walkina2987ea2007-06-26 23:56:54 +00001169 pvalue = per_get_few_bits(pd, padding);
1170 switch(pvalue) {
Lev Walkin31c46d52007-06-26 06:46:04 +00001171 case -1:
1172 ASN_DEBUG("Padding skip failed");
Lev Walkina2987ea2007-06-26 23:56:54 +00001173 _ASN_DECODE_STARVED;
Lev Walkin31c46d52007-06-26 06:46:04 +00001174 case 0: break;
1175 default:
Lev Walkina2987ea2007-06-26 23:56:54 +00001176 ASN_DEBUG("Non-blank padding (%d bits 0x%02x)",
1177 padding, pvalue);
Lev Walkin31c46d52007-06-26 06:46:04 +00001178 _ASN_DECODE_FAILED;
1179 }
Lev Walkin7ed25982007-06-24 08:38:34 +00001180 }
Lev Walkin31c46d52007-06-26 06:46:04 +00001181 if(pd->nbits != pd->nboff) {
Lev Walkin00e43912007-06-26 07:18:54 +00001182 ASN_DEBUG("Open type container overhead of %d bits!", pd->nbits - pd->nboff);
1183 if(1) _ASN_DECODE_FAILED;
1184 arg.unclaimed += pd->nbits - pd->nboff;
Lev Walkin7ed25982007-06-24 08:38:34 +00001185 }
Lev Walkina2987ea2007-06-26 23:56:54 +00001186
1187 /* Adjust pd back so it points to original data */
1188 pd->nbits = arg.oldpd.nbits - (pd->moved - arg.ot_moved);
1189 pd->moved = arg.oldpd.moved + (pd->moved - arg.ot_moved);
Lev Walkin7ed25982007-06-24 08:38:34 +00001190 pd->refill = arg.oldpd.refill;
1191 pd->refill_key = arg.oldpd.refill_key;
1192
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001193 /* Skip data not consumed by the decoder */
Lev Walkina2987ea2007-06-26 23:56:54 +00001194 if(arg.unclaimed) ASN_DEBUG("Getting unclaimed %d", arg.unclaimed);
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001195 while(arg.unclaimed) {
Lev Walkin5ce11fd2007-06-26 03:16:35 +00001196 size_t toget = 24;
Lev Walkin7ed25982007-06-24 08:38:34 +00001197 if(arg.unclaimed < toget)
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001198 toget = arg.unclaimed;
Lev Walkin7ed25982007-06-24 08:38:34 +00001199 arg.unclaimed -= toget;
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001200 switch(per_get_few_bits(pd, toget)) {
Lev Walkin7ed25982007-06-24 08:38:34 +00001201 case -1:
1202 ASN_DEBUG("Claim of %d failed", toget);
1203 _ASN_DECODE_STARVED;
1204 case 0:
1205 ASN_DEBUG("Got claim of %d", toget);
1206 continue;
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001207 default:
1208 /* Padding must be blank */
1209 ASN_DEBUG("Non-blank unconsumed padding");
1210 _ASN_DECODE_FAILED;
1211 }
1212 }
1213
Lev Walkina2987ea2007-06-26 23:56:54 +00001214 assert(pd->moved == pd->nboff + ((((int)pd->buffer) & 0x7) << 3));
1215
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001216 if(arg.repeat) {
1217 ASN_DEBUG("Not consumed the whole thing");
1218 rv.code = RC_FAIL;
1219 return rv;
1220 }
1221
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001222 return rv;
1223}
1224
Lev Walkin06230f72007-06-26 09:52:44 +00001225asn_dec_rval_t
1226uper_sot_suck(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
1227 asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
1228 asn_dec_rval_t rv;
1229
1230 (void)opt_codec_ctx;
1231 (void)td;
1232 (void)constraints;
1233 (void)sptr;
1234
1235 while(per_get_few_bits(pd, 24) >= 0);
1236
1237 rv.code = RC_OK;
1238 rv.consumed = pd->moved;
1239
1240 return rv;
1241}
1242
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001243static int
1244uper_skip_open_type(asn_codec_ctx_t *opt_codec_ctx, asn_per_data_t *pd) {
Lev Walkin06230f72007-06-26 09:52:44 +00001245 asn_TYPE_descriptor_t s_td;
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001246 asn_dec_rval_t rv;
Lev Walkin06230f72007-06-26 09:52:44 +00001247
1248 s_td.name = "<unknown extension>";
1249 s_td.uper_decoder = uper_sot_suck;
1250
1251 rv = uper_get_open_type(opt_codec_ctx, &s_td, 0, 0, pd);
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001252 if(rv.code != RC_OK)
1253 return -1;
1254 else
1255 return 0;
1256}
1257
Lev Walkin59b176e2005-11-26 11:25:14 +00001258asn_dec_rval_t
1259SEQUENCE_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
1260 asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
1261 asn_SEQUENCE_specifics_t *specs = (asn_SEQUENCE_specifics_t *)td->specifics;
1262 void *st = *sptr; /* Target structure. */
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001263 int extpresent; /* Extension additions are present */
Lev Walkin59b176e2005-11-26 11:25:14 +00001264 uint8_t *opres; /* Presence of optional root members */
1265 asn_per_data_t opmd;
1266 asn_dec_rval_t rv;
1267 int edx;
1268
1269 (void)constraints;
1270
Lev Walkin1d9e8dd2005-12-07 05:46:03 +00001271 if(_ASN_STACK_OVERFLOW_CHECK(opt_codec_ctx))
1272 _ASN_DECODE_FAILED;
1273
Lev Walkin59b176e2005-11-26 11:25:14 +00001274 if(!st) {
1275 st = *sptr = CALLOC(1, specs->struct_size);
1276 if(!st) _ASN_DECODE_FAILED;
1277 }
1278
1279 ASN_DEBUG("Decoding %s as SEQUENCE (UPER)", td->name);
1280
1281 /* Handle extensions */
1282 if(specs->ext_before >= 0) {
1283 extpresent = per_get_few_bits(pd, 1);
Lev Walkin0a8aa602006-09-18 20:05:55 +00001284 if(extpresent < 0) _ASN_DECODE_STARVED;
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001285 } else {
1286 extpresent = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +00001287 }
1288
1289 /* Prepare a place and read-in the presence bitmap */
Lev Walkin73a5cc62007-06-24 08:45:31 +00001290 memset(&opmd, 0, sizeof(opmd));
Lev Walkin59b176e2005-11-26 11:25:14 +00001291 if(specs->roms_count) {
1292 opres = (uint8_t *)MALLOC(((specs->roms_count + 7) >> 3) + 1);
1293 if(!opres) _ASN_DECODE_FAILED;
1294 /* Get the presence map */
1295 if(per_get_many_bits(pd, opres, 0, specs->roms_count)) {
1296 FREEMEM(opres);
Lev Walkin0a8aa602006-09-18 20:05:55 +00001297 _ASN_DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +00001298 }
1299 opmd.buffer = opres;
Lev Walkin59b176e2005-11-26 11:25:14 +00001300 opmd.nbits = specs->roms_count;
1301 ASN_DEBUG("Read in presence bitmap for %s of %d bits (%x..)",
1302 td->name, specs->roms_count, *opres);
1303 } else {
1304 opres = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +00001305 }
1306
1307 /*
1308 * Get the sequence ROOT elements.
1309 */
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001310 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001311 asn_TYPE_member_t *elm = &td->elements[edx];
1312 void *memb_ptr; /* Pointer to the member */
1313 void **memb_ptr2; /* Pointer to that pointer */
1314
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001315 if(IN_EXTENSION_GROUP(specs, edx))
1316 continue;
1317
Lev Walkin59b176e2005-11-26 11:25:14 +00001318 /* Fetch the pointer to this member */
1319 if(elm->flags & ATF_POINTER) {
1320 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
1321 } else {
1322 memb_ptr = (char *)st + elm->memb_offset;
1323 memb_ptr2 = &memb_ptr;
1324 }
1325
1326 /* Deal with optionality */
1327 if(elm->optional) {
1328 int present = per_get_few_bits(&opmd, 1);
1329 ASN_DEBUG("Member %s->%s is optional, p=%d (%d->%d)",
1330 td->name, elm->name, present,
1331 (int)opmd.nboff, (int)opmd.nbits);
1332 if(present == 0) {
1333 /* This element is not present */
1334 if(elm->default_value) {
1335 /* Fill-in DEFAULT */
Lev Walkin523de9e2006-08-18 01:34:18 +00001336 if(elm->default_value(1, memb_ptr2)) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001337 FREEMEM(opres);
1338 _ASN_DECODE_FAILED;
1339 }
Lev Walkin8032f7a2007-06-27 01:54:57 +00001340 ASN_DEBUG("Filled-in default");
Lev Walkin59b176e2005-11-26 11:25:14 +00001341 }
1342 /* The member is just not present */
1343 continue;
1344 }
1345 /* Fall through */
1346 }
1347
1348 /* Fetch the member from the stream */
1349 ASN_DEBUG("Decoding member %s in %s", elm->name, td->name);
1350 rv = elm->type->uper_decoder(opt_codec_ctx, elm->type,
1351 elm->per_constraints, memb_ptr2, pd);
1352 if(rv.code != RC_OK) {
1353 ASN_DEBUG("Failed decode %s in %s",
1354 elm->name, td->name);
1355 FREEMEM(opres);
1356 return rv;
1357 }
1358 }
1359
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001360 /* Optionality map is not needed anymore */
1361 FREEMEM(opres);
1362
Lev Walkin59b176e2005-11-26 11:25:14 +00001363 /*
1364 * Deal with extensions.
1365 */
1366 if(extpresent) {
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001367 ssize_t bmlength;
1368 uint8_t *epres; /* Presence of extension members */
1369 asn_per_data_t epmd;
Lev Walkin59b176e2005-11-26 11:25:14 +00001370
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001371 bmlength = uper_get_nslength(pd);
1372 if(bmlength < 0) _ASN_DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +00001373
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001374 ASN_DEBUG("Extensions %d present in %s", bmlength, td->name);
1375
1376 epres = (uint8_t *)MALLOC((bmlength + 15) >> 3);
1377 if(!epres) _ASN_DECODE_STARVED;
1378
1379 /* Get the extensions map */
1380 if(per_get_many_bits(pd, epres, 0, bmlength))
1381 _ASN_DECODE_STARVED;
1382
Lev Walkin73a5cc62007-06-24 08:45:31 +00001383 memset(&epmd, 0, sizeof(epmd));
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001384 epmd.buffer = epres;
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001385 epmd.nbits = bmlength;
1386 ASN_DEBUG("Read in extensions bitmap for %s of %d bits (%x..)",
1387 td->name, bmlength, *epres);
1388
1389 /* Go over extensions and read them in */
1390 for(edx = specs->ext_after + 1; edx < td->elements_count; edx++) {
1391 asn_TYPE_member_t *elm = &td->elements[edx];
1392 void *memb_ptr; /* Pointer to the member */
1393 void **memb_ptr2; /* Pointer to that pointer */
1394 int present;
1395
1396 if(!IN_EXTENSION_GROUP(specs, edx)) {
1397 ASN_DEBUG("%d is not extension", edx);
1398 continue;
1399 }
1400
1401 /* Fetch the pointer to this member */
1402 if(elm->flags & ATF_POINTER) {
1403 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
1404 } else {
1405 memb_ptr = (void *)((char *)st + elm->memb_offset);
1406 memb_ptr2 = &memb_ptr;
1407 }
1408
1409 present = per_get_few_bits(&epmd, 1);
1410 if(present <= 0) {
1411 if(present < 0) break; /* No more extensions */
1412 continue;
1413 }
1414
1415 ASN_DEBUG("Decoding member %s in %s %p", elm->name, td->name, *memb_ptr2);
1416 rv = uper_get_open_type(opt_codec_ctx, elm->type,
1417 elm->per_constraints, memb_ptr2, pd);
1418 if(rv.code != RC_OK) {
1419 FREEMEM(epres);
1420 return rv;
1421 }
1422 }
1423
1424 /* Skip over overflow extensions which aren't present
1425 * in this system's version of the protocol */
Lev Walkin06230f72007-06-26 09:52:44 +00001426 for(;;) {
Lev Walkina2987ea2007-06-26 23:56:54 +00001427 ASN_DEBUG("Getting overflow extensions");
Lev Walkin06230f72007-06-26 09:52:44 +00001428 switch(per_get_few_bits(&epmd, 1)) {
1429 case -1: break;
1430 case 0: continue;
1431 default:
1432 if(uper_skip_open_type(opt_codec_ctx, pd)) {
1433 FREEMEM(epres);
1434 _ASN_DECODE_STARVED;
1435 }
Lev Walkin59b176e2005-11-26 11:25:14 +00001436 }
Lev Walkin06230f72007-06-26 09:52:44 +00001437 break;
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001438 }
Lev Walkin59b176e2005-11-26 11:25:14 +00001439
Lev Walkin5b78e1c2007-06-24 06:26:47 +00001440 FREEMEM(epres);
1441 }
1442
1443 /* Fill DEFAULT members in extensions */
1444 for(edx = specs->roms_count; edx < specs->roms_count
1445 + specs->aoms_count; edx++) {
1446 asn_TYPE_member_t *elm = &td->elements[edx];
1447 void **memb_ptr2; /* Pointer to member pointer */
1448
1449 if(!elm->default_value) continue;
1450
1451 /* Fetch the pointer to this member */
1452 if(elm->flags & ATF_POINTER) {
1453 memb_ptr2 = (void **)((char *)st
1454 + elm->memb_offset);
1455 if(*memb_ptr2) continue;
1456 } else {
1457 continue; /* Extensions are all optionals */
1458 }
1459
1460 /* Set default value */
1461 if(elm->default_value(1, memb_ptr2)) {
1462 _ASN_DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +00001463 }
1464 }
1465
1466 rv.consumed = 0;
1467 rv.code = RC_OK;
1468 return rv;
1469}
1470
Lev Walkin62258e22007-06-23 23:50:25 +00001471static int
1472SEQUENCE_handle_extensions(asn_TYPE_descriptor_t *td, void *sptr,
1473 asn_per_outp_t *po1, asn_per_outp_t *po2) {
1474 asn_SEQUENCE_specifics_t *specs
1475 = (asn_SEQUENCE_specifics_t *)td->specifics;
Lev Walkinf55a6dd2007-06-24 00:35:51 +00001476 int exts_present = 0;
1477 int exts_count = 0;
Lev Walkin62258e22007-06-23 23:50:25 +00001478 int edx;
1479
1480 if(specs->ext_before < 0)
1481 return 0;
1482
1483 /* Find out which extensions are present */
1484 for(edx = specs->ext_after + 1; edx < td->elements_count; edx++) {
1485 asn_TYPE_member_t *elm = &td->elements[edx];
1486 void *memb_ptr; /* Pointer to the member */
1487 void **memb_ptr2; /* Pointer to that pointer */
1488 int present;
1489
Lev Walkinf55a6dd2007-06-24 00:35:51 +00001490 if(!IN_EXTENSION_GROUP(specs, edx)) {
Lev Walkin06230f72007-06-26 09:52:44 +00001491 ASN_DEBUG("%s (@%d) is not extension", elm->type->name, edx);
Lev Walkin62258e22007-06-23 23:50:25 +00001492 continue;
Lev Walkinf55a6dd2007-06-24 00:35:51 +00001493 }
Lev Walkin62258e22007-06-23 23:50:25 +00001494
1495 /* Fetch the pointer to this member */
1496 if(elm->flags & ATF_POINTER) {
1497 memb_ptr2 = (void **)((char *)sptr + elm->memb_offset);
1498 present = (*memb_ptr2 != 0);
1499 } else {
1500 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
1501 memb_ptr2 = &memb_ptr;
1502 present = 1;
1503 }
1504
Lev Walkin06230f72007-06-26 09:52:44 +00001505 ASN_DEBUG("checking %s (@%d) present => %d",
1506 elm->type->name, edx, present);
Lev Walkinf55a6dd2007-06-24 00:35:51 +00001507 exts_count++;
1508 exts_present += present;
Lev Walkin62258e22007-06-23 23:50:25 +00001509
1510 /* Encode as presence marker */
1511 if(po1 && per_put_few_bits(po1, present, 1))
1512 return -1;
1513 /* Encode as open type field */
1514 if(po2 && present && uper_put_open_type(elm->type,
1515 elm->per_constraints, *memb_ptr2, po2))
1516 return -1;
1517
1518 }
1519
Lev Walkinf55a6dd2007-06-24 00:35:51 +00001520 return exts_present ? exts_count : 0;
Lev Walkin62258e22007-06-23 23:50:25 +00001521}
1522
Lev Walkin523de9e2006-08-18 01:34:18 +00001523asn_enc_rval_t
1524SEQUENCE_encode_uper(asn_TYPE_descriptor_t *td,
1525 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
1526 asn_SEQUENCE_specifics_t *specs
1527 = (asn_SEQUENCE_specifics_t *)td->specifics;
1528 asn_enc_rval_t er;
Lev Walkin62258e22007-06-23 23:50:25 +00001529 int n_extensions;
Lev Walkin523de9e2006-08-18 01:34:18 +00001530 int edx;
1531 int i;
1532
1533 (void)constraints;
1534
1535 if(!sptr)
1536 _ASN_ENCODE_FAILED;
1537
1538 er.encoded = 0;
1539
1540 ASN_DEBUG("Encoding %s as SEQUENCE (UPER)", td->name);
Lev Walkin62258e22007-06-23 23:50:25 +00001541
1542
1543 /*
1544 * X.691#18.1 Whether structure is extensible
1545 * and whether to encode extensions
1546 */
1547 if(specs->ext_before >= 0) {
1548 n_extensions = SEQUENCE_handle_extensions(td, sptr, 0, 0);
1549 per_put_few_bits(po, n_extensions ? 1 : 0, 1);
1550 } else {
1551 n_extensions = 0; /* There are no extensions to encode */
1552 }
Lev Walkin523de9e2006-08-18 01:34:18 +00001553
1554 /* Encode a presence bitmap */
1555 for(i = 0; i < specs->roms_count; i++) {
Lev Walkinc46b7cb2006-08-18 02:27:55 +00001556 asn_TYPE_member_t *elm;
Lev Walkin523de9e2006-08-18 01:34:18 +00001557 void *memb_ptr; /* Pointer to the member */
1558 void **memb_ptr2; /* Pointer to that pointer */
Lev Walkin523de9e2006-08-18 01:34:18 +00001559 int present;
1560
Lev Walkinc46b7cb2006-08-18 02:27:55 +00001561 edx = specs->oms[i];
1562 elm = &td->elements[edx];
1563
Lev Walkin523de9e2006-08-18 01:34:18 +00001564 /* Fetch the pointer to this member */
1565 if(elm->flags & ATF_POINTER) {
1566 memb_ptr2 = (void **)((char *)sptr + elm->memb_offset);
1567 present = (*memb_ptr2 != 0);
1568 } else {
1569 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
1570 memb_ptr2 = &memb_ptr;
1571 present = 1;
1572 }
1573
1574 /* Eliminate default values */
1575 if(present && elm->default_value
1576 && elm->default_value(0, memb_ptr2) == 1)
1577 present = 0;
1578
1579 ASN_DEBUG("Element %s %s %s->%s is %s",
1580 elm->flags & ATF_POINTER ? "ptr" : "inline",
1581 elm->default_value ? "def" : "wtv",
1582 td->name, elm->name, present ? "present" : "absent");
1583 if(per_put_few_bits(po, present, 1))
1584 _ASN_ENCODE_FAILED;
1585 }
1586
1587 /*
Lev Walkin62258e22007-06-23 23:50:25 +00001588 * Encode the sequence ROOT elements.
Lev Walkin523de9e2006-08-18 01:34:18 +00001589 */
Lev Walkin62258e22007-06-23 23:50:25 +00001590 ASN_DEBUG("ext_after = %d, ec = %d, eb = %d", specs->ext_after, td->elements_count, specs->ext_before);
Lev Walkin523de9e2006-08-18 01:34:18 +00001591 for(edx = 0; edx < ((specs->ext_before < 0)
Lev Walkin62258e22007-06-23 23:50:25 +00001592 ? td->elements_count : specs->ext_after); edx++) {
Lev Walkin523de9e2006-08-18 01:34:18 +00001593 asn_TYPE_member_t *elm = &td->elements[edx];
1594 void *memb_ptr; /* Pointer to the member */
1595 void **memb_ptr2; /* Pointer to that pointer */
1596
Lev Walkina2987ea2007-06-26 23:56:54 +00001597 ASN_DEBUG("About to encode %s", elm->type->name);
1598
Lev Walkin523de9e2006-08-18 01:34:18 +00001599 /* Fetch the pointer to this member */
1600 if(elm->flags & ATF_POINTER) {
1601 memb_ptr2 = (void **)((char *)sptr + elm->memb_offset);
1602 if(!*memb_ptr2) {
1603 ASN_DEBUG("Element %s %d not present",
1604 elm->name, edx);
1605 if(elm->optional)
1606 continue;
1607 /* Mandatory element is missing */
1608 _ASN_ENCODE_FAILED;
1609 }
1610 } else {
1611 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
1612 memb_ptr2 = &memb_ptr;
1613 }
1614
1615 /* Eliminate default values */
Lev Walkin8032f7a2007-06-27 01:54:57 +00001616 ASN_DEBUG("Defv %p mptr %p\n", elm->default_value, memb_ptr2);
1617 ASN_DEBUG("Do not encode default: %s\n", (*(char **)(*memb_ptr2)));
1618 if(elm->default_value && elm->default_value(0, memb_ptr2) == 1) {
Lev Walkin523de9e2006-08-18 01:34:18 +00001619 continue;
Lev Walkin8032f7a2007-06-27 01:54:57 +00001620 }
Lev Walkin523de9e2006-08-18 01:34:18 +00001621
1622 er = elm->type->uper_encoder(elm->type, elm->per_constraints,
1623 *memb_ptr2, po);
1624 if(er.encoded == -1)
1625 return er;
1626 }
1627
Lev Walkin62258e22007-06-23 23:50:25 +00001628 /* No extensions to encode */
1629 if(!n_extensions) _ASN_ENCODED_OK(er);
1630
1631 ASN_DEBUG("Length of %d bit-map", n_extensions);
1632 /* #18.8. Write down the presence bit-map length. */
1633 if(uper_put_nslength(po, n_extensions))
1634 _ASN_ENCODE_FAILED;
1635
1636 ASN_DEBUG("Bit-map of %d elements", n_extensions);
1637 /* #18.7. Encoding the extensions presence bit-map. */
1638 /* TODO: act upon NOTE in #18.7 for canonical PER */
1639 if(SEQUENCE_handle_extensions(td, sptr, po, 0) != n_extensions)
1640 _ASN_ENCODE_FAILED;
1641
1642 ASN_DEBUG("Writing %d extensions", n_extensions);
1643 /* #18.9. Encode extensions as open type fields. */
1644 if(SEQUENCE_handle_extensions(td, sptr, 0, po) != n_extensions)
1645 _ASN_ENCODE_FAILED;
1646
Lev Walkin523de9e2006-08-18 01:34:18 +00001647 _ASN_ENCODED_OK(er);
1648}
1649