constr_SEQUENCE.c 41.8 KB
Newer Older
Lev Walkin's avatar
Lev Walkin committed
1
/*-
2
 * Copyright (c) 2003-2017 Lev Walkin <vlm@lionet.info>.
Lev Walkin's avatar
Lev Walkin committed
3
 * All rights reserved.
Lev Walkin's avatar
Lev Walkin committed
4 5
 * Redistribution and modifications are permitted subject to BSD license.
 */
Lev Walkin's avatar
Lev Walkin committed
6
#include <asn_internal.h>
Lev Walkin's avatar
Lev Walkin committed
7
#include <constr_SEQUENCE.h>
8
#include <OPEN_TYPE.h>
9
#include <per_opentype.h>
Lev Walkin's avatar
Lev Walkin committed
10 11 12 13 14 15

/*
 * Number of bytes left for this structure.
 * (ctx->left) indicates the number of bytes _transferred_ for the structure.
 * (size) contains the number of bytes in the buffer passed.
 */
Lev Walkin's avatar
Lev Walkin committed
16
#define	LEFT	((size<(size_t)ctx->left)?size:(size_t)ctx->left)
Lev Walkin's avatar
Lev Walkin committed
17 18 19 20 21 22 23 24 25 26 27 28

/*
 * If the subprocessor function returns with an indication that it wants
 * more data, it may well be a fatal decoding problem, because the
 * size is constrained by the <TLV>'s L, even if the buffer size allows
 * reading more data.
 * For example, consider the buffer containing the following TLVs:
 * <T:5><L:1><V> <T:6>...
 * The TLV length clearly indicates that one byte is expected in V, but
 * if the V processor returns with "want more data" even if the buffer
 * contains way more data than the V processor have seen.
 */
29
#define	SIZE_VIOLATION	(ctx->left >= 0 && (size_t)ctx->left <= size)
Lev Walkin's avatar
Lev Walkin committed
30 31 32 33 34

/*
 * This macro "eats" the part of the buffer which is definitely "consumed",
 * i.e. was correctly converted into local representation or rightfully skipped.
 */
Lev Walkin's avatar
Lev Walkin committed
35
#undef	ADVANCE
Lev Walkin's avatar
Lev Walkin committed
36 37
#define	ADVANCE(num_bytes)	do {		\
		size_t num = num_bytes;		\
Lev Walkin's avatar
Lev Walkin committed
38
		ptr = ((const char *)ptr) + num; \
Lev Walkin's avatar
Lev Walkin committed
39 40 41 42 43 44 45 46 47
		size -= num;			\
		if(ctx->left >= 0)		\
			ctx->left -= num;	\
		consumed_myself += num;		\
	} while(0)

/*
 * Switch to the next phase of parsing.
 */
Lev Walkin's avatar
Lev Walkin committed
48 49
#undef	NEXT_PHASE
#undef	PHASE_OUT
Lev Walkin's avatar
Lev Walkin committed
50 51 52 53 54 55 56 57 58
#define	NEXT_PHASE(ctx)	do {			\
		ctx->phase++;			\
		ctx->step = 0;			\
	} while(0)
#define	PHASE_OUT(ctx)	do { ctx->phase = 10; } while(0)

/*
 * Return a standardized complex structure.
 */
Lev Walkin's avatar
Lev Walkin committed
59
#undef	RETURN
Lev Walkin's avatar
Lev Walkin committed
60 61 62 63 64 65 66 67 68 69
#define	RETURN(_code)	do {			\
		rval.code = _code;		\
		rval.consumed = consumed_myself;\
		return rval;			\
	} while(0)

/*
 * Check whether we are inside the extensions group.
 */
#define	IN_EXTENSION_GROUP(specs, memb_idx)	\
Lev Walkin's avatar
Lev Walkin committed
70 71
	( (((signed)(memb_idx)) > (specs)->ext_after)	\
	&&(((signed)(memb_idx)) < (specs)->ext_before))
Lev Walkin's avatar
Lev Walkin committed
72

Lev Walkin's avatar
Lev Walkin committed
73 74 75 76 77 78

/*
 * Tags are canonically sorted in the tag2element map.
 */
static int
_t2e_cmp(const void *ap, const void *bp) {
Lev Walkin's avatar
Lev Walkin committed
79 80
	const asn_TYPE_tag2member_t *a = (const asn_TYPE_tag2member_t *)ap;
	const asn_TYPE_tag2member_t *b = (const asn_TYPE_tag2member_t *)bp;
Lev Walkin's avatar
Lev Walkin committed
81

Lev Walkin's avatar
Lev Walkin committed
82 83 84 85 86 87 88
	int a_class = BER_TAG_CLASS(a->el_tag);
	int b_class = BER_TAG_CLASS(b->el_tag);

	if(a_class == b_class) {
		ber_tlv_tag_t a_value = BER_TAG_VALUE(a->el_tag);
		ber_tlv_tag_t b_value = BER_TAG_VALUE(b->el_tag);

89
		if(a_value == b_value) {
Lev Walkin's avatar
oops  
Lev Walkin committed
90 91
			if(a->el_no > b->el_no)
				return 1;
92 93
			/*
			 * Important: we do not check
Lev Walkin's avatar
oops  
Lev Walkin committed
94
			 * for a->el_no <= b->el_no!
95
			 */
Lev Walkin's avatar
Lev Walkin committed
96
			return 0;
97
		} else if(a_value < b_value)
Lev Walkin's avatar
Lev Walkin committed
98 99 100 101 102 103 104 105 106 107 108
			return -1;
		else
			return 1;
	} else if(a_class < b_class) {
		return -1;
	} else {
		return 1;
	}
}


Lev Walkin's avatar
Lev Walkin committed
109 110 111
/*
 * The decoder of the SEQUENCE type.
 */
Lev Walkin's avatar
Lev Walkin committed
112
asn_dec_rval_t
Lev Walkin's avatar
Lev Walkin committed
113
SEQUENCE_decode_ber(const asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkin's avatar
Lev Walkin committed
114
	void **struct_ptr, const void *ptr, size_t size, int tag_mode) {
Lev Walkin's avatar
Lev Walkin committed
115 116 117
	/*
	 * Bring closer parts of structure description.
	 */
Lev Walkin's avatar
Lev Walkin committed
118 119
	asn_SEQUENCE_specifics_t *specs = (asn_SEQUENCE_specifics_t *)td->specifics;
	asn_TYPE_member_t *elements = td->elements;
Lev Walkin's avatar
Lev Walkin committed
120 121 122 123 124

	/*
	 * Parts of the structure being constructed.
	 */
	void *st = *struct_ptr;	/* Target structure. */
Lev Walkin's avatar
Lev Walkin committed
125
	asn_struct_ctx_t *ctx;	/* Decoder context */
Lev Walkin's avatar
Lev Walkin committed
126 127

	ber_tlv_tag_t tlv_tag;	/* T from TLV */
Lev Walkin's avatar
Lev Walkin committed
128
	asn_dec_rval_t rval;	/* Return code from subparsers */
Lev Walkin's avatar
Lev Walkin committed
129 130

	ssize_t consumed_myself = 0;	/* Consumed bytes from ptr */
Lev Walkin's avatar
Lev Walkin committed
131
	size_t edx;			/* SEQUENCE element's index */
Lev Walkin's avatar
Lev Walkin committed
132

Lev Walkin's avatar
Lev Walkin committed
133
	ASN_DEBUG("Decoding %s as SEQUENCE", td->name);
Lev Walkin's avatar
Lev Walkin committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147
	
	/*
	 * Create the target structure if it is not present already.
	 */
	if(st == 0) {
		st = *struct_ptr = CALLOC(1, specs->struct_size);
		if(st == 0) {
			RETURN(RC_FAIL);
		}
	}

	/*
	 * Restore parsing context.
	 */
Lev Walkin's avatar
Lev Walkin committed
148
	ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkin's avatar
Lev Walkin committed
149 150 151 152 153 154 155 156 157 158 159 160
	
	/*
	 * Start to parse where left previously
	 */
	switch(ctx->phase) {
	case 0:
		/*
		 * PHASE 0.
		 * Check that the set of tags associated with given structure
		 * perfectly fits our expectations.
		 */

Lev Walkin's avatar
Lev Walkin committed
161
		rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size,
162
			tag_mode, 1, &ctx->left, 0);
Lev Walkin's avatar
Lev Walkin committed
163 164
		if(rval.code != RC_OK) {
			ASN_DEBUG("%s tagging check failed: %d",
Lev Walkin's avatar
Lev Walkin committed
165
				td->name, rval.code);
166
			return rval;
Lev Walkin's avatar
Lev Walkin committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
		}

		if(ctx->left >= 0)
			ctx->left += rval.consumed; /* ?Substracted below! */
		ADVANCE(rval.consumed);

		NEXT_PHASE(ctx);

		ASN_DEBUG("Structure consumes %ld bytes, buffer %ld",
			(long)ctx->left, (long)size);

		/* Fall through */
	case 1:
		/*
		 * PHASE 1.
		 * From the place where we've left it previously,
		 * try to decode the next member from the list of
		 * this structure's elements.
		 * (ctx->step) stores the member being processed
		 * between invocations and the microphase {0,1} of parsing
		 * that member:
		 * 	step = (<member_number> * 2 + <microphase>).
		 */
Lev Walkin's avatar
Lev Walkin committed
190
	  for(edx = ((size_t)ctx->step >> 1); edx < td->elements_count;
Lev Walkin's avatar
Lev Walkin committed
191 192
			edx++, ctx->step = (ctx->step & ~1) + 2) {
		void *memb_ptr;		/* Pointer to the member */
Lev Walkin's avatar
Lev Walkin committed
193
		void **memb_ptr2;	/* Pointer to that pointer */
Lev Walkin's avatar
Lev Walkin committed
194
		ssize_t tag_len;	/* Length of TLV's T */
Lev Walkin's avatar
Lev Walkin committed
195 196
		size_t opt_edx_end;	/* Next non-optional element */
		size_t n;
Lev Walkin's avatar
Lev Walkin committed
197
		int use_bsearch;
Lev Walkin's avatar
Lev Walkin committed
198 199 200 201 202 203 204

		if(ctx->step & 1)
			goto microphase2;

		/*
		 * MICROPHASE 1: Synchronize decoding.
		 */
Lev Walkin's avatar
Lev Walkin committed
205
		ASN_DEBUG("In %s SEQUENCE left %d, edx=%zu flags=%d"
206 207 208 209
				" opt=%d ec=%d",
			td->name, (int)ctx->left, edx,
			elements[edx].flags, elements[edx].optional,
			td->elements_count);
Lev Walkin's avatar
Lev Walkin committed
210 211 212 213

		if(ctx->left == 0	/* No more stuff is expected */
		&& (
			/* Explicit OPTIONAL specification reaches the end */
214 215
			(edx + elements[edx].optional
					== td->elements_count)
Lev Walkin's avatar
Lev Walkin committed
216 217 218
			||
			/* All extensions are optional */
			(IN_EXTENSION_GROUP(specs, edx)
Lev Walkin's avatar
Lev Walkin committed
219
				&& specs->ext_before > (signed)td->elements_count)
Lev Walkin's avatar
Lev Walkin committed
220 221
		   )
		) {
Lev Walkin's avatar
Lev Walkin committed
222
			ASN_DEBUG("End of SEQUENCE %s", td->name);
Lev Walkin's avatar
Lev Walkin committed
223 224 225 226 227 228 229 230 231 232 233
			/*
			 * Found the legitimate end of the structure.
			 */
			PHASE_OUT(ctx);
			RETURN(RC_OK);
		}

		/*
		 * Fetch the T from TLV.
		 */
		tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
Lev Walkin's avatar
Lev Walkin committed
234
		ASN_DEBUG("Current tag in %s SEQUENCE for element %zu "
Lev Walkin's avatar
Lev Walkin committed
235
			"(%s) is %s encoded in %d bytes, of frame %ld",
Lev Walkin's avatar
Lev Walkin committed
236 237
			td->name, edx, elements[edx].name,
			ber_tlv_tag_string(tlv_tag), (int)tag_len, (long)LEFT);
Lev Walkin's avatar
Lev Walkin committed
238 239 240 241 242 243
		switch(tag_len) {
		case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
			/* Fall through */
		case -1: RETURN(RC_FAIL);
		}

Lev Walkin's avatar
Lev Walkin committed
244
		if(ctx->left < 0 && ((const uint8_t *)ptr)[0] == 0) {
245 246 247 248 249
			if(LEFT < 2) {
				if(SIZE_VIOLATION)
					RETURN(RC_FAIL);
				else
					RETURN(RC_WMORE);
Lev Walkin's avatar
Lev Walkin committed
250
			} else if(((const uint8_t *)ptr)[1] == 0) {
Lev Walkin's avatar
Lev Walkin committed
251
			ASN_DEBUG("edx = %zu, opt = %d, ec=%d",
252 253 254 255 256 257
				edx, elements[edx].optional,
				td->elements_count);
				if((edx + elements[edx].optional
					== td->elements_count)
				|| (IN_EXTENSION_GROUP(specs, edx)
					&& specs->ext_before
Lev Walkin's avatar
Lev Walkin committed
258
						> (signed)td->elements_count)) {
259 260 261 262 263 264 265 266 267 268 269 270 271 272
					/*
					 * Yeah, baby! Found the terminator
					 * of the indefinite length structure.
					 */
					/*
					 * Proceed to the canonical
					 * finalization function.
					 * No advancing is necessary.
					 */
					goto phase3;
				}
			}
		}

Lev Walkin's avatar
Lev Walkin committed
273 274 275
		/*
		 * Find the next available type with this tag.
		 */
Lev Walkin's avatar
Lev Walkin committed
276
		use_bsearch = 0;
Lev Walkin's avatar
Lev Walkin committed
277
		opt_edx_end = edx + elements[edx].optional + 1;
Lev Walkin's avatar
Lev Walkin committed
278 279
		if(opt_edx_end > td->elements_count)
			opt_edx_end = td->elements_count;	/* Cap */
Lev Walkin's avatar
Lev Walkin committed
280
		else if(opt_edx_end - edx > 8) {
281
			/* Limit the scope of linear search... */
Lev Walkin's avatar
Lev Walkin committed
282
			opt_edx_end = edx + 8;
Lev Walkin's avatar
Lev Walkin committed
283
			use_bsearch = 1;
284
			/* ... and resort to bsearch() */
Lev Walkin's avatar
Lev Walkin committed
285
		}
Lev Walkin's avatar
Lev Walkin committed
286 287 288 289 290 291 292 293
		for(n = edx; n < opt_edx_end; n++) {
			if(BER_TAGS_EQUAL(tlv_tag, elements[n].tag)) {
				/*
				 * Found element corresponding to the tag
				 * being looked at.
				 * Reposition over the right element.
				 */
				edx = n;
Lev Walkin's avatar
Lev Walkin committed
294 295
				ctx->step = 1 + 2 * edx;	/* Remember! */
				goto microphase2;
296
			} else if(elements[n].flags & ATF_ANY_TYPE) {
Lev Walkin's avatar
Lev Walkin committed
297 298 299 300 301 302 303
				/*
				 * This is the ANY type, which may bear
				 * any flag whatsoever.
				 */
				edx = n;
				ctx->step = 1 + 2 * edx;	/* Remember! */
				goto microphase2;
Lev Walkin's avatar
Lev Walkin committed
304 305
			} else if(elements[n].tag == (ber_tlv_tag_t)-1) {
				use_bsearch = 1;
Lev Walkin's avatar
Lev Walkin committed
306 307 308
				break;
			}
		}
Lev Walkin's avatar
Lev Walkin committed
309 310
		if(use_bsearch) {
			/*
Lev Walkin's avatar
typo  
Lev Walkin committed
311
			 * Resort to a binary search over
Lev Walkin's avatar
Lev Walkin committed
312 313
			 * sorted array of tags.
			 */
314
			const asn_TYPE_tag2member_t *t2m;
Lev Walkin's avatar
Lev Walkin committed
315 316 317
			asn_TYPE_tag2member_t key = {0, 0, 0, 0};
			key.el_tag = tlv_tag;
			key.el_no = edx;
318
			t2m = (const asn_TYPE_tag2member_t *)bsearch(&key,
Lev Walkin's avatar
Lev Walkin committed
319
				specs->tag2el, specs->tag2el_count,
Lev Walkin's avatar
Lev Walkin committed
320
				sizeof(specs->tag2el[0]), _t2e_cmp);
321
			if(t2m) {
322 323
				const asn_TYPE_tag2member_t *best = 0;
				const asn_TYPE_tag2member_t *t2m_f, *t2m_l;
Lev Walkin's avatar
Lev Walkin committed
324
				size_t edx_max = edx + elements[edx].optional;
Lev Walkin's avatar
Lev Walkin committed
325 326 327 328
				/*
				 * Rewind to the first element with that tag,
				 * `cause bsearch() does not guarantee order.
				 */
329 330 331 332 333 334 335 336 337 338 339 340
				t2m_f = t2m + t2m->toff_first;
				t2m_l = t2m + t2m->toff_last;
				for(t2m = t2m_f; t2m <= t2m_l; t2m++) {
					if(t2m->el_no > edx_max) break;
					if(t2m->el_no < edx) continue;
					best = t2m;
				}
				if(best) {
					edx = best->el_no;
					ctx->step = 1 + 2 * edx;
					goto microphase2;
				}
Lev Walkin's avatar
Lev Walkin committed
341 342 343
			}
			n = opt_edx_end;
		}
Lev Walkin's avatar
Lev Walkin committed
344 345 346 347 348 349 350
		if(n == opt_edx_end) {
			/*
			 * If tag is unknown, it may be either
			 * an unknown (thus, incorrect) tag,
			 * or an extension (...),
			 * or an end of the indefinite-length structure.
			 */
351 352
			if(!IN_EXTENSION_GROUP(specs,
				edx + elements[edx].optional)) {
Lev Walkin's avatar
Lev Walkin committed
353
				ASN_DEBUG("Unexpected tag %s (at %zu)",
354
					ber_tlv_tag_string(tlv_tag), edx);
Lev Walkin's avatar
Lev Walkin committed
355
				ASN_DEBUG("Expected tag %s (%s)%s",
Lev Walkin's avatar
Lev Walkin committed
356
					ber_tlv_tag_string(elements[edx].tag),
Lev Walkin's avatar
Lev Walkin committed
357
					elements[edx].name,
Lev Walkin's avatar
Lev Walkin committed
358 359 360 361 362 363
					elements[edx].optional
						?" or alternatives":"");
				RETURN(RC_FAIL);
			} else {
				/* Skip this tag */
				ssize_t skip;
364
				edx += elements[edx].optional;
Lev Walkin's avatar
Lev Walkin committed
365

Lev Walkin's avatar
Lev Walkin committed
366
				ASN_DEBUG("Skipping unexpected %s (at %zu)",
367
					ber_tlv_tag_string(tlv_tag), edx);
368
				skip = ber_skip_length(opt_codec_ctx,
Lev Walkin's avatar
Lev Walkin committed
369
					BER_TLV_CONSTRUCTED(ptr),
Lev Walkin's avatar
Lev Walkin committed
370
					(const char *)ptr + tag_len,
Lev Walkin's avatar
Lev Walkin committed
371
					LEFT - tag_len);
Lev Walkin's avatar
Lev Walkin committed
372
				ASN_DEBUG("Skip length %d in %s",
Lev Walkin's avatar
Lev Walkin committed
373
					(int)skip, td->name);
Lev Walkin's avatar
Lev Walkin committed
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
				switch(skip) {
				case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
					/* Fall through */
				case -1: RETURN(RC_FAIL);
				}

				ADVANCE(skip + tag_len);
				ctx->step -= 2;
				edx--;
				continue;  /* Try again with the next tag */
			}
		}

		/*
		 * MICROPHASE 2: Invoke the member-specific decoder.
		 */
		ctx->step |= 1;		/* Confirm entering next microphase */
	microphase2:
Lev Walkin's avatar
Lev Walkin committed
392
		ASN_DEBUG("Inside SEQUENCE %s MF2", td->name);
Lev Walkin's avatar
Lev Walkin committed
393 394 395 396 397 398
		
		/*
		 * Compute the position of the member inside a structure,
		 * and also a type of containment (it may be contained
		 * as pointer or using inline inclusion).
		 */
399 400
		if(elements[edx].flags & ATF_POINTER) {
			/* Member is a pointer to another structure */
Lev Walkin's avatar
Lev Walkin committed
401
			memb_ptr2 = (void **)((char *)st + elements[edx].memb_offset);
Lev Walkin's avatar
Lev Walkin committed
402 403 404 405 406
		} else {
			/*
			 * A pointer to a pointer
			 * holding the start of the structure
			 */
Lev Walkin's avatar
Lev Walkin committed
407
			memb_ptr = (char *)st + elements[edx].memb_offset;
Lev Walkin's avatar
Lev Walkin committed
408 409 410 411 412
			memb_ptr2 = &memb_ptr;
		}
		/*
		 * Invoke the member fetch routine according to member's type
		 */
Lev Walkin's avatar
Lev Walkin committed
413
		if(elements[edx].flags & ATF_OPEN_TYPE) {
Lev Walkin's avatar
Lev Walkin committed
414 415 416 417 418 419 420
			rval = OPEN_TYPE_ber_get(opt_codec_ctx, td, st, &elements[edx], ptr, LEFT);
        } else {
			rval = elements[edx].type->op->ber_decoder(opt_codec_ctx,
					elements[edx].type,
					memb_ptr2, ptr, LEFT,
					elements[edx].tag_mode);
		}
Lev Walkin's avatar
Lev Walkin committed
421
		ASN_DEBUG("In %s SEQUENCE decoded %zu %s of %d "
Lev Walkin's avatar
Lev Walkin committed
422
			"in %d bytes rval.code %d, size=%d",
Lev Walkin's avatar
Lev Walkin committed
423
			td->name, edx, elements[edx].type->name,
Lev Walkin's avatar
Lev Walkin committed
424
			(int)LEFT, (int)rval.consumed, rval.code, (int)size);
Lev Walkin's avatar
Lev Walkin committed
425 426 427 428 429 430 431 432
		switch(rval.code) {
		case RC_OK:
			break;
		case RC_WMORE: /* More data expected */
			if(!SIZE_VIOLATION) {
				ADVANCE(rval.consumed);
				RETURN(RC_WMORE);
			}
Lev Walkin's avatar
Lev Walkin committed
433 434
			ASN_DEBUG("Size violation (c->l=%ld <= s=%ld)",
				(long)ctx->left, (long)size);
Lev Walkin's avatar
Lev Walkin committed
435 436 437 438 439 440 441 442 443 444 445 446 447 448
			/* Fall through */
		case RC_FAIL: /* Fatal error */
			RETURN(RC_FAIL);
		} /* switch(rval) */
		
		ADVANCE(rval.consumed);
	  }	/* for(all structure members) */

	phase3:
		ctx->phase = 3;
	case 3:	/* 00 and other tags expected */
	case 4:	/* only 00's expected */

		ASN_DEBUG("SEQUENCE %s Leftover: %ld, size = %ld",
Lev Walkin's avatar
Lev Walkin committed
449
			td->name, (long)ctx->left, (long)size);
Lev Walkin's avatar
Lev Walkin committed
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467

		/*
		 * Skip everything until the end of the SEQUENCE.
		 */
		while(ctx->left) {
			ssize_t tl, ll;

			tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
			switch(tl) {
			case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
				/* Fall through */
			case -1: RETURN(RC_FAIL);
			}

			/*
			 * If expected <0><0>...
			 */
			if(ctx->left < 0
Lev Walkin's avatar
Lev Walkin committed
468
				&& ((const uint8_t *)ptr)[0] == 0) {
Lev Walkin's avatar
Lev Walkin committed
469 470 471 472 473
				if(LEFT < 2) {
					if(SIZE_VIOLATION)
						RETURN(RC_FAIL);
					else
						RETURN(RC_WMORE);
Lev Walkin's avatar
Lev Walkin committed
474
				} else if(((const uint8_t *)ptr)[1] == 0) {
Lev Walkin's avatar
Lev Walkin committed
475 476 477 478 479 480 481 482 483 484
					/*
					 * Correctly finished with <0><0>.
					 */
					ADVANCE(2);
					ctx->left++;
					ctx->phase = 4;
					continue;
				}
			}

Lev Walkin's avatar
Lev Walkin committed
485
			if(!IN_EXTENSION_GROUP(specs, td->elements_count)
Lev Walkin's avatar
Lev Walkin committed
486 487 488 489
			|| ctx->phase == 4) {
				ASN_DEBUG("Unexpected continuation "
					"of a non-extensible type "
					"%s (SEQUENCE): %s",
Lev Walkin's avatar
Lev Walkin committed
490
					td->name,
Lev Walkin's avatar
Lev Walkin committed
491 492 493 494
					ber_tlv_tag_string(tlv_tag));
				RETURN(RC_FAIL);
			}

495
			ll = ber_skip_length(opt_codec_ctx,
Lev Walkin's avatar
Lev Walkin committed
496
				BER_TLV_CONSTRUCTED(ptr),
Lev Walkin's avatar
Lev Walkin committed
497
				(const char *)ptr + tl, LEFT - tl);
Lev Walkin's avatar
Lev Walkin committed
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
			switch(ll) {
			case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
				/* Fall through */
			case -1: RETURN(RC_FAIL);
			}

			ADVANCE(tl + ll);
		}

		PHASE_OUT(ctx);
	}
	
	RETURN(RC_OK);
}

Lev Walkin's avatar
Lev Walkin committed
513

Lev Walkin's avatar
Lev Walkin committed
514 515 516
/*
 * The DER encoder of the SEQUENCE type.
 */
Lev Walkin's avatar
Lev Walkin committed
517
asn_enc_rval_t
Lev Walkin's avatar
Lev Walkin committed
518
SEQUENCE_encode_der(asn_TYPE_descriptor_t *td,
519
	void *sptr, int tag_mode, ber_tlv_tag_t tag,
Lev Walkin's avatar
Lev Walkin committed
520 521
	asn_app_consume_bytes_f *cb, void *app_key) {
	size_t computed_size = 0;
Lev Walkin's avatar
Lev Walkin committed
522
	asn_enc_rval_t erval;
Lev Walkin's avatar
Lev Walkin committed
523
	ssize_t ret;
Lev Walkin's avatar
Lev Walkin committed
524
	size_t edx;
Lev Walkin's avatar
Lev Walkin committed
525 526

	ASN_DEBUG("%s %s as SEQUENCE",
Lev Walkin's avatar
Lev Walkin committed
527
		cb?"Encoding":"Estimating", td->name);
Lev Walkin's avatar
Lev Walkin committed
528 529 530 531

	/*
	 * Gather the length of the underlying members sequence.
	 */
Lev Walkin's avatar
Lev Walkin committed
532
	for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin's avatar
Lev Walkin committed
533
		asn_TYPE_member_t *elm = &td->elements[edx];
534 535 536 537

		void *memb_ptr;		/* Pointer to the member */
		void **memb_ptr2;	/* Pointer to that pointer */

538
		if(elm->flags & ATF_POINTER) {
539 540
			memb_ptr2 = (void **)((char *)sptr + elm->memb_offset);
			if(!*memb_ptr2) {
541
				ASN_DEBUG("Element %s %zu not present",
542 543 544
					elm->name, edx);
				if(elm->optional)
					continue;
545
				/* Mandatory element is missing */
546
				ASN__ENCODE_FAILED;
547
			}
Lev Walkin's avatar
Lev Walkin committed
548
		} else {
Lev Walkin's avatar
Lev Walkin committed
549
			memb_ptr = (void *)((char *)sptr + elm->memb_offset);
550
			memb_ptr2 = &memb_ptr;
Lev Walkin's avatar
Lev Walkin committed
551
		}
552 553 554 555 556 557

		/* Eliminate default values */
		if(elm->default_value && elm->default_value(0, memb_ptr2) == 1)
			continue;

		erval = elm->type->op->der_encoder(elm->type, *memb_ptr2,
Lev Walkin's avatar
Lev Walkin committed
558 559 560 561 562
			elm->tag_mode, elm->tag,
			0, 0);
		if(erval.encoded == -1)
			return erval;
		computed_size += erval.encoded;
Lev Walkin's avatar
Lev Walkin committed
563
		ASN_DEBUG("Member %zu %s estimated %ld bytes",
Lev Walkin's avatar
Lev Walkin committed
564 565 566 567 568 569
			edx, elm->name, (long)erval.encoded);
	}

	/*
	 * Encode the TLV for the sequence itself.
	 */
570
	ret = der_write_tags(td, computed_size, tag_mode, 1, tag, cb, app_key);
Lev Walkin's avatar
Lev Walkin committed
571
	ASN_DEBUG("Wrote tags: %ld (+%ld)", (long)ret, (long)computed_size);
572
	if(ret == -1)
573
		ASN__ENCODE_FAILED;
Lev Walkin's avatar
Lev Walkin committed
574 575
	erval.encoded = computed_size + ret;

576
	if(!cb) ASN__ENCODED_OK(erval);
Lev Walkin's avatar
Lev Walkin committed
577 578 579 580

	/*
	 * Encode all members.
	 */
Lev Walkin's avatar
Lev Walkin committed
581
	for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin's avatar
Lev Walkin committed
582
		asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkin's avatar
Lev Walkin committed
583
		asn_enc_rval_t tmperval;
584 585
		void *memb_ptr;		/* Pointer to the member */
		void **memb_ptr2;	/* Pointer to that pointer */
Lev Walkin's avatar
Lev Walkin committed
586

587
		if(elm->flags & ATF_POINTER) {
588 589
			memb_ptr2 = (void **)((char *)sptr + elm->memb_offset);
			if(!*memb_ptr2) continue;
Lev Walkin's avatar
Lev Walkin committed
590
		} else {
Lev Walkin's avatar
Lev Walkin committed
591
			memb_ptr = (void *)((char *)sptr + elm->memb_offset);
592
			memb_ptr2 = &memb_ptr;
Lev Walkin's avatar
Lev Walkin committed
593
		}
594 595 596 597 598 599 600

		/* Eliminate default values */
		if(elm->default_value && elm->default_value(0, memb_ptr2) == 1)
			continue;

		tmperval = elm->type->op->der_encoder(elm->type, *memb_ptr2,
			elm->tag_mode, elm->tag, cb, app_key);
Lev Walkin's avatar
Lev Walkin committed
601 602 603
		if(tmperval.encoded == -1)
			return tmperval;
		computed_size -= tmperval.encoded;
Lev Walkin's avatar
Lev Walkin committed
604
		ASN_DEBUG("Member %zu %s of SEQUENCE %s encoded in %ld bytes",
Lev Walkin's avatar
Lev Walkin committed
605
			edx, elm->name, td->name, (long)tmperval.encoded);
Lev Walkin's avatar
Lev Walkin committed
606 607
	}

608
	if(computed_size != 0)
Lev Walkin's avatar
Lev Walkin committed
609 610 611
		/*
		 * Encoded size is not equal to the computed size.
		 */
612
		ASN__ENCODE_FAILED;
Lev Walkin's avatar
Lev Walkin committed
613

614
	ASN__ENCODED_OK(erval);
Lev Walkin's avatar
Lev Walkin committed
615 616
}

Lev Walkin's avatar
Lev Walkin committed
617

618
#undef	XER_ADVANCE
Lev Walkin's avatar
Lev Walkin committed
619 620 621 622 623 624 625
#define XER_ADVANCE(num_bytes)           \
    do {                                 \
        size_t num = (num_bytes);        \
        ptr = ((const char *)ptr) + num; \
        size -= num;                     \
        consumed_myself += num;          \
    } while(0)
626

Lev Walkin's avatar
Lev Walkin committed
627 628 629
/*
 * Decode the XER (XML) data.
 */
630
asn_dec_rval_t
Lev Walkin's avatar
Lev Walkin committed
631
SEQUENCE_decode_xer(const asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
632
	void **struct_ptr, const char *opt_mname,
Lev Walkin's avatar
Lev Walkin committed
633
		const void *ptr, size_t size) {
634 635 636
	/*
	 * Bring closer parts of structure description.
	 */
Lev Walkin's avatar
Lev Walkin committed
637 638
	asn_SEQUENCE_specifics_t *specs
		= (asn_SEQUENCE_specifics_t *)td->specifics;
639 640 641 642
	asn_TYPE_member_t *elements = td->elements;
	const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;

	/*
Lev Walkin's avatar
Lev Walkin committed
643
	 * ... and parts of the structure being constructed.
644 645 646 647
	 */
	void *st = *struct_ptr;	/* Target structure. */
	asn_struct_ctx_t *ctx;	/* Decoder context */

Lev Walkin's avatar
Lev Walkin committed
648
	asn_dec_rval_t rval;		/* Return value from a decoder */
649
	ssize_t consumed_myself = 0;	/* Consumed bytes from ptr */
Lev Walkin's avatar
Lev Walkin committed
650
	size_t edx;			/* Element index */
651 652 653 654 655 656 657 658 659 660 661 662

	/*
	 * Create the target structure if it is not present already.
	 */
	if(st == 0) {
		st = *struct_ptr = CALLOC(1, specs->struct_size);
		if(st == 0) RETURN(RC_FAIL);
	}

	/*
	 * Restore parsing context.
	 */
Lev Walkin's avatar
Lev Walkin committed
663
	ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
664 665 666 667 668 669 670


	/*
	 * Phases of XER/XML processing:
	 * Phase 0: Check that the opening tag matches our expectations.
	 * Phase 1: Processing body and reacting on closing tag.
	 * Phase 2: Processing inner type.
Lev Walkin's avatar
Lev Walkin committed
671 672
	 * Phase 3: Skipping unknown extensions.
	 * Phase 4: PHASED OUT
673
	 */
Lev Walkin's avatar
Lev Walkin committed
674
	for(edx = ctx->step; ctx->phase <= 3;) {
675 676 677 678 679 680
		pxer_chunk_type_e ch_type;	/* XER chunk type */
		ssize_t ch_size;		/* Chunk size */
		xer_check_tag_e tcv;		/* Tag check value */
		asn_TYPE_member_t *elm;

		/*
Lev Walkin's avatar
Lev Walkin committed
681
		 * Go inside the inner member of a sequence.
682 683 684
		 */
		if(ctx->phase == 2) {
			asn_dec_rval_t tmprval;
685
			void *memb_ptr_dontuse;		/* Pointer to the member */
686 687
			void **memb_ptr2;	/* Pointer to that pointer */

Lev Walkin's avatar
Lev Walkin committed
688 689
			elm = &td->elements[edx];

690 691
			if(elm->flags & ATF_POINTER) {
				/* Member is a pointer to another structure */
Lev Walkin's avatar
Lev Walkin committed
692
				memb_ptr2 = (void **)((char *)st + elm->memb_offset);
693
			} else {
694 695
				memb_ptr_dontuse = (char *)st + elm->memb_offset;
				memb_ptr2 = &memb_ptr_dontuse;  /* Only use of memb_ptr_dontuse */
696 697
			}

Lev Walkin's avatar
Lev Walkin committed
698
			if(elm->flags & ATF_OPEN_TYPE) {
Lev Walkin's avatar
Lev Walkin committed
699 700 701 702 703 704 705
				tmprval = OPEN_TYPE_xer_get(opt_codec_ctx, td, st, elm, ptr, size);
			} else {
				/* Invoke the inner type decoder, m.b. multiple times */
				tmprval = elm->type->op->xer_decoder(opt_codec_ctx,
						elm->type, memb_ptr2, elm->name,
						ptr, size);
			}
706 707 708
			XER_ADVANCE(tmprval.consumed);
			if(tmprval.code != RC_OK)
				RETURN(tmprval.code);
Lev Walkin's avatar
Lev Walkin committed
709
			ctx->phase = 1;	/* Back to body processing */
710
			ctx->step = ++edx;
Lev Walkin's avatar
Lev Walkin committed
711 712
			ASN_DEBUG("XER/SEQUENCE phase => %d, step => %d",
				ctx->phase, ctx->step);
713 714 715 716 717 718
			/* Fall through */
		}

		/*
		 * Get the next part of the XML stream.
		 */
Lev Walkin's avatar
Lev Walkin committed
719
		ch_size = xer_next_token(&ctx->context, ptr, size,
Lev Walkin's avatar
Lev Walkin committed
720
			&ch_type);
Lev Walkin's avatar
Lev Walkin committed
721 722
		if(ch_size == -1) {
		    RETURN(RC_FAIL);
Lev Walkin's avatar
Lev Walkin committed
723
		} else {
724
			switch(ch_type) {
Lev Walkin's avatar
Lev Walkin committed
725 726
			case PXER_WMORE:
				RETURN(RC_WMORE);
727 728 729 730 731 732 733 734 735
			case PXER_COMMENT:	/* Got XML comment */
			case PXER_TEXT:		/* Ignore free-standing text */
				XER_ADVANCE(ch_size);	/* Skip silently */
				continue;
			case PXER_TAG:
				break;	/* Check the rest down there */
			}
		}

Lev Walkin's avatar
Lev Walkin committed
736
		tcv = xer_check_tag(ptr, ch_size, xml_tag);
Lev Walkin's avatar
Lev Walkin committed
737 738
		ASN_DEBUG("XER/SEQUENCE: tcv = %d, ph=%d [%s]",
			tcv, ctx->phase, xml_tag);
Lev Walkin's avatar
Lev Walkin committed
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758

		/* Skip the extensions section */
		if(ctx->phase == 3) {
			switch(xer_skip_unknown(tcv, &ctx->left)) {
			case -1:
				ctx->phase = 4;
				RETURN(RC_FAIL);
			case 0:
				XER_ADVANCE(ch_size);
				continue;
			case 1:
				XER_ADVANCE(ch_size);
				ctx->phase = 1;
				continue;
			case 2:
				ctx->phase = 1;
				break;
			}
		}

759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
		switch(tcv) {
		case XCT_CLOSING:
			if(ctx->phase == 0) break;
			ctx->phase = 0;
			/* Fall through */
		case XCT_BOTH:
			if(ctx->phase == 0) {
				if(edx >= td->elements_count
				   ||
				   /* Explicit OPTIONAL specs reaches the end */
				    (edx + elements[edx].optional
					== td->elements_count)
				   ||
				   /* All extensions are optional */
				   (IN_EXTENSION_GROUP(specs, edx)
					&& specs->ext_before
Lev Walkin's avatar
Lev Walkin committed
775
						> (signed)td->elements_count)
776 777
				) {
					XER_ADVANCE(ch_size);
Lev Walkin's avatar
Lev Walkin committed
778
					ctx->phase = 4;	/* Phase out */
Lev Walkin's avatar
Lev Walkin committed
779
					RETURN(RC_OK);
780 781 782 783 784 785 786 787 788 789 790 791
				} else {
					ASN_DEBUG("Premature end of XER SEQUENCE");
					RETURN(RC_FAIL);
				}
			}
			/* Fall through */
		case XCT_OPENING:
			if(ctx->phase == 0) {
				XER_ADVANCE(ch_size);
				ctx->phase = 1;	/* Processing body phase */
				continue;
			}
792
			/* Fall through */
Lev Walkin's avatar
Lev Walkin committed
793 794
		case XCT_UNKNOWN_OP:
		case XCT_UNKNOWN_BO:
795

Lev Walkin's avatar
Lev Walkin committed
796
			ASN_DEBUG("XER/SEQUENCE: tcv=%d, ph=%d, edx=%zu",
797 798
				tcv, ctx->phase, edx);
			if(ctx->phase != 1) {
799
				break;	/* Really unexpected */
800
			}
801

802 803 804 805
			if(edx < td->elements_count) {
				/*
				 * Search which member corresponds to this tag.
				 */
806 807
				size_t n;
				size_t edx_end = edx + elements[edx].optional + 1;
808 809 810 811
				if(edx_end > td->elements_count)
					edx_end = td->elements_count;
				for(n = edx; n < edx_end; n++) {
					elm = &td->elements[n];
Lev Walkin's avatar
Lev Walkin committed
812
					tcv = xer_check_tag(ptr, ch_size, elm->name);
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
					switch(tcv) {
					case XCT_BOTH:
					case XCT_OPENING:
						/*
						 * Process this member.
						 */
						ctx->step = edx = n;
						ctx->phase = 2;
						break;
					case XCT_UNKNOWN_OP:
					case XCT_UNKNOWN_BO:
						continue;
					default:
						n = edx_end;
						break;	/* Phase out */
					}
829 830
					break;
				}
831 832 833
				if(n != edx_end)
					continue;
			} else {
Lev Walkin's avatar
Lev Walkin committed
834
				ASN_DEBUG("Out of defined members: %zu/%u",
835
					edx, td->elements_count);
836
			}
Lev Walkin's avatar
Lev Walkin committed
837 838 839

			/* It is expected extension */
			if(IN_EXTENSION_GROUP(specs,
840 841
				edx + (edx < td->elements_count
					? elements[edx].optional : 0))) {
Lev Walkin's avatar
Lev Walkin committed
842
				ASN_DEBUG("Got anticipated extension at %zu",
843
					edx);
Lev Walkin's avatar
Lev Walkin committed
844
				/*
Lev Walkin's avatar
Lev Walkin committed
845 846 847
				 * Check for (XCT_BOTH or XCT_UNKNOWN_BO)
				 * By using a mask. Only record a pure
				 * <opening> tags.
Lev Walkin's avatar
Lev Walkin committed
848
				 */
Lev Walkin's avatar
Lev Walkin committed
849 850 851 852 853 854 855 856
				if(tcv & XCT_CLOSING) {
					/* Found </extension> without body */
				} else {
					ctx->left = 1;
					ctx->phase = 3;	/* Skip ...'s */
				}
				XER_ADVANCE(ch_size);
				continue;
Lev Walkin's avatar
Lev Walkin committed
857 858 859
			}

			/* Fall through */
860 861 862 863
		default:
			break;
		}

Lev Walkin's avatar
Lev Walkin committed
864
		ASN_DEBUG("Unexpected XML tag in SEQUENCE [%c%c%c%c%c%c]",
Lev Walkin's avatar
Lev Walkin committed
865 866 867 868 869 870
			size>0?((const char *)ptr)[0]:'.',
			size>1?((const char *)ptr)[1]:'.',
			size>2?((const char *)ptr)[2]:'.',
			size>3?((const char *)ptr)[3]:'.',
			size>4?((const char *)ptr)[4]:'.',
			size>5?((const char *)ptr)[5]:'.');
871 872 873
		break;
	}

Lev Walkin's avatar
Lev Walkin committed
874
	ctx->phase = 4;	/* "Phase out" on hard failure */
875 876 877
	RETURN(RC_FAIL);
}

Lev Walkin's avatar
Lev Walkin committed
878
asn_enc_rval_t
879 880 881 882 883 884 885
SEQUENCE_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, int ilevel,
                    enum xer_encoder_flags_e flags, asn_app_consume_bytes_f *cb,
                    void *app_key) {
    asn_enc_rval_t er;
    int xcan = (flags & XER_F_CANONICAL);
    asn_TYPE_descriptor_t *tmp_def_val_td = 0;
    void *tmp_def_val = 0;
Lev Walkin's avatar
Lev Walkin committed
886
	size_t edx;
Lev Walkin's avatar
Lev Walkin committed
887

888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
    if(!sptr) ASN__ENCODE_FAILED;

    er.encoded = 0;

    for(edx = 0; edx < td->elements_count; edx++) {
        asn_enc_rval_t tmper;
        asn_TYPE_member_t *elm = &td->elements[edx];
        void *memb_ptr;
        const char *mname = elm->name;
        unsigned int mlen = strlen(mname);

        if(elm->flags & ATF_POINTER) {
            memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
            if(!memb_ptr) {
                assert(tmp_def_val == 0);
                if(elm->default_value) {
                    if(elm->default_value(1, &tmp_def_val)) {
                        ASN__ENCODE_FAILED;
                    } else {
                        memb_ptr = tmp_def_val;
                        tmp_def_val_td = elm->type;
                    }
                } else if(elm->optional) {
                    continue;
                } else {
                    /* Mandatory element is missing */
                    ASN__ENCODE_FAILED;
                }
            }
        } else {
            memb_ptr = (void *)((char *)sptr + elm->memb_offset);
        }
920

921 922
        if(!xcan) ASN__TEXT_INDENT(1, ilevel);
        ASN__CALLBACK3("<", 1, mname, mlen, ">", 1);
Lev Walkin's avatar
Lev Walkin committed
923

924 925 926 927 928 929 930 931
        /* Print the member itself */
        tmper = elm->type->op->xer_encoder(elm->type, memb_ptr, ilevel + 1,
                                           flags, cb, app_key);
        if(tmp_def_val) {
            ASN_STRUCT_FREE(*tmp_def_val_td, tmp_def_val);
            tmp_def_val = 0;
        }
        if(tmper.encoded == -1) return tmper;
932
        er.encoded += tmper.encoded;
Lev Walkin's avatar
Lev Walkin committed
933

934 935
        ASN__CALLBACK3("</", 2, mname, mlen, ">", 1);
    }
Lev Walkin's avatar
Lev Walkin committed
936

937
    if(!xcan) ASN__TEXT_INDENT(1, ilevel - 1);
Lev Walkin's avatar
Lev Walkin committed
938

939
    ASN__ENCODED_OK(er);
Lev Walkin's avatar
Lev Walkin committed
940
cb_failed:
941 942
    if(tmp_def_val) ASN_STRUCT_FREE(*tmp_def_val_td, tmp_def_val);
    ASN__ENCODE_FAILED;
Lev Walkin's avatar
Lev Walkin committed
943 944
}

Lev Walkin's avatar
Lev Walkin committed
945
int
Lev Walkin's avatar
Lev Walkin committed
946
SEQUENCE_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkin's avatar
Lev Walkin committed
947
		asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin's avatar
Lev Walkin committed
948
	size_t edx;
Lev Walkin's avatar
Lev Walkin committed
949 950
	int ret;

951
	if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkin's avatar
Lev Walkin committed
952 953

	/* Dump preamble */
954 955
	if(cb(td->name, strlen(td->name), app_key) < 0
	|| cb(" ::= {", 6, app_key) < 0)
Lev Walkin's avatar
Lev Walkin committed
956 957
		return -1;

Lev Walkin's avatar
Lev Walkin committed
958
	for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin's avatar
Lev Walkin committed
959
		asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkin's avatar
Lev Walkin committed
960 961
		const void *memb_ptr;

962
		if(elm->flags & ATF_POINTER) {
Lev Walkin's avatar
Lev Walkin committed
963
			memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
964 965 966 967 968
			if(!memb_ptr) {
				if(elm->optional) continue;
				/* Print <absent> line */
				/* Fall through */
			}
Lev Walkin's avatar
Lev Walkin committed
969
		} else {
Lev Walkin's avatar
Lev Walkin committed
970
			memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
Lev Walkin's avatar
Lev Walkin committed
971 972 973
		}

		/* Indentation */
974
		_i_INDENT(1);
Lev Walkin's avatar
Lev Walkin committed
975 976

		/* Print the member's name and stuff */
977 978
		if(cb(elm->name, strlen(elm->name), app_key) < 0
		|| cb(": ", 2, app_key) < 0)
Lev Walkin's avatar
Lev Walkin committed
979 980 981
			return -1;

		/* Print the member itself */
982
		ret = elm->type->op->print_struct(elm->type, memb_ptr, ilevel + 1,
Lev Walkin's avatar
Lev Walkin committed
983 984 985 986
			cb, app_key);
		if(ret) return ret;
	}

987 988
	ilevel--;
	_i_INDENT(1);
Lev Walkin's avatar
Lev Walkin committed
989

990
	return (cb("}", 1, app_key) < 0) ? -1 : 0;
Lev Walkin's avatar
Lev Walkin committed
991 992 993
}

void
994 995
SEQUENCE_free(const asn_TYPE_descriptor_t *td, void *sptr,
              enum asn_struct_free_method method) {
996
    size_t edx;
Lev Walkin's avatar
Lev Walkin committed
997 998 999 1000 1001 1002

	if(!td || !sptr)
		return;

	ASN_DEBUG("Freeing %s as SEQUENCE", td->name);

Lev Walkin's avatar
Lev Walkin committed
1003
	for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin's avatar
Lev Walkin committed
1004
		asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkin's avatar
Lev Walkin committed
1005
		void *memb_ptr;
1006
		if(elm->flags & ATF_POINTER) {
Lev Walkin's avatar
Lev Walkin committed
1007
			memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
Lev Walkin's avatar
Lev Walkin committed
1008
			if(memb_ptr)
Lev Walkin's avatar
Lev Walkin committed
1009
				ASN_STRUCT_FREE(*elm->type, memb_ptr);
Lev Walkin's avatar
Lev Walkin committed
1010
		} else {
Lev Walkin's avatar
Lev Walkin committed
1011
			memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkin's avatar
Lev Walkin committed
1012
			ASN_STRUCT_FREE_CONTENTS_ONLY(*elm->type, memb_ptr);
Lev Walkin's avatar
Lev Walkin committed
1013 1014 1015
		}
	}

1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
    switch(method) {
    case ASFM_FREE_EVERYTHING:
        FREEMEM(sptr);
        break;
    case ASFM_FREE_UNDERLYING:
        break;
    case ASFM_FREE_UNDERLYING_AND_RESET:
        memset(sptr, 0,
               ((asn_SEQUENCE_specifics_t *)(td->specifics))->struct_size);
        break;
    }
Lev Walkin's avatar
Lev Walkin committed
1027 1028 1029
}

int
Lev Walkin's avatar
Lev Walkin committed
1030
SEQUENCE_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin's avatar
Lev Walkin committed
1031
		asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkin's avatar
Lev Walkin committed
1032
	size_t edx;
Lev Walkin's avatar
Lev Walkin committed
1033 1034

	if(!sptr) {
1035
		ASN__CTFAIL(app_key, td, sptr,
1036 1037
			"%s: value not given (%s:%d)",
			td->name, __FILE__, __LINE__);
Lev Walkin's avatar
Lev Walkin committed
1038 1039 1040 1041 1042 1043
		return -1;
	}

	/*
	 * Iterate over structure members and check their validity.
	 */
Lev Walkin's avatar
Lev Walkin committed
1044
	for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin's avatar
Lev Walkin committed
1045
		asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkin's avatar
Lev Walkin committed
1046 1047
		const void *memb_ptr;

1048
		if(elm->flags & ATF_POINTER) {
Lev Walkin's avatar
Lev Walkin committed
1049
			memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
1050 1051 1052
			if(!memb_ptr) {
				if(elm->optional)
					continue;
1053
				ASN__CTFAIL(app_key, td, sptr,
1054 1055 1056 1057
				"%s: mandatory element %s absent (%s:%d)",
				td->name, elm->name, __FILE__, __LINE__);
				return -1;
			}
Lev Walkin's avatar
Lev Walkin committed
1058
		} else {
Lev Walkin's avatar
Lev Walkin committed
1059
			memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
Lev Walkin's avatar
Lev Walkin committed
1060 1061
		}

1062 1063
		if(elm->encoding_constraints.general_constraints) {
			int ret = elm->encoding_constraints.general_constraints(elm->type, memb_ptr,
Lev Walkin's avatar
Lev Walkin committed
1064
				ctfailcb, app_key);
Lev Walkin's avatar
Lev Walkin committed
1065 1066
			if(ret) return ret;
		} else {
1067
			return elm->type->encoding_constraints.general_constraints(elm->type,
Lev Walkin's avatar
Lev Walkin committed
1068
				memb_ptr, ctfailcb, app_key);
Lev Walkin's avatar
Lev Walkin committed
1069
		}
Lev Walkin's avatar
Lev Walkin committed
1070 1071 1072 1073
	}

	return 0;
}
Lev Walkin's avatar
Lev Walkin committed
1074

Lev Walkin's avatar
Lev Walkin committed
1075 1076
#ifndef ASN_DISABLE_PER_SUPPORT

Lev Walkin's avatar
Lev Walkin committed
1077
asn_dec_rval_t
Lev Walkin's avatar
Lev Walkin committed
1078
SEQUENCE_decode_uper(const asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkin's avatar
Lev Walkin committed
1079 1080
                     const asn_per_constraints_t *constraints, void **sptr,
                     asn_per_data_t *pd) {
1081
	asn_SEQUENCE_specifics_t *specs = (asn_SEQUENCE_specifics_t *)td->specifics;
Lev Walkin's avatar
Lev Walkin committed
1082
	void *st = *sptr;	/* Target structure. */
Lev Walkin's avatar
Lev Walkin committed
1083
	int extpresent;		/* Extension additions are present */
Lev Walkin's avatar
Lev Walkin committed
1084 1085 1086
	uint8_t *opres;		/* Presence of optional root members */
	asn_per_data_t opmd;
	asn_dec_rval_t rv;
Lev Walkin's avatar
Lev Walkin committed
1087
	size_t edx;
Lev Walkin's avatar
Lev Walkin committed
1088 1089 1090

	(void)constraints;

1091 1092
	if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx))
		ASN__DECODE_FAILED;
Lev Walkin's avatar
Lev Walkin committed
1093

Lev Walkin's avatar
Lev Walkin committed
1094 1095
	if(!st) {
		st = *sptr = CALLOC(1, specs->struct_size);
1096
		if(!st) ASN__DECODE_FAILED;
Lev Walkin's avatar
Lev Walkin committed
1097 1098 1099 1100 1101 1102 1103
	}

	ASN_DEBUG("Decoding %s as SEQUENCE (UPER)", td->name);

	/* Handle extensions */
	if(specs->ext_before >= 0) {
		extpresent = per_get_few_bits(pd, 1);
1104
		if(extpresent < 0) ASN__DECODE_STARVED;
Lev Walkin's avatar
Lev Walkin committed
1105 1106
	} else {
		extpresent = 0;
Lev Walkin's avatar
Lev Walkin committed
1107 1108 1109
	}

	/* Prepare a place and read-in the presence bitmap */
Lev Walkin's avatar
Lev Walkin committed
1110
	memset(&opmd, 0, sizeof(opmd));
Lev Walkin's avatar
Lev Walkin committed
1111 1112
	if(specs->roms_count) {
		opres = (uint8_t *)MALLOC(((specs->roms_count + 7) >> 3) + 1);
1113
		if(!opres) ASN__DECODE_FAILED;
Lev Walkin's avatar
Lev Walkin committed
1114 1115 1116
		/* Get the presence map */
		if(per_get_many_bits(pd, opres, 0, specs->roms_count)) {
			FREEMEM(opres);
1117
			ASN__DECODE_STARVED;
Lev Walkin's avatar
Lev Walkin committed
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
		}
		opmd.buffer = opres;
		opmd.nbits = specs->roms_count;
		ASN_DEBUG("Read in presence bitmap for %s of %d bits (%x..)",
			td->name, specs->roms_count, *opres);
	} else {
		opres = 0;
	}

	/*
	 * Get the sequence ROOT elements.
	 */
Lev Walkin's avatar
Lev Walkin committed
1130
	for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin's avatar
Lev Walkin committed
1131
		asn_TYPE_member_t *elm = &td->elements[edx];
1132
		void *memb_ptr;		/* Pointer to the member */
Lev Walkin's avatar
Lev Walkin committed
1133 1134
		void **memb_ptr2;	/* Pointer to that pointer */

Lev Walkin's avatar
Lev Walkin committed
1135 1136 1137
		if(IN_EXTENSION_GROUP(specs, edx))
			continue;

Lev Walkin's avatar
Lev Walkin committed
1138 1139
		/* Fetch the pointer to this member */
		if(elm->flags & ATF_POINTER) {
Lev Walkin's avatar
Lev Walkin committed
1140
			memb_ptr2 = (void **)((char *)st + elm->memb_offset);
Lev Walkin's avatar
Lev Walkin committed
1141
		} else {
Lev Walkin's avatar
Lev Walkin committed
1142
			memb_ptr = (char *)st + elm->memb_offset;
Lev Walkin's avatar
Lev Walkin committed
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
			memb_ptr2 = &memb_ptr;
		}

		/* Deal with optionality */
		if(elm->optional) {
			int present = per_get_few_bits(&opmd, 1);
			ASN_DEBUG("Member %s->%s is optional, p=%d (%d->%d)",
				td->name, elm->name, present,
				(int)opmd.nboff, (int)opmd.nbits);
			if(present == 0) {
				/* This element is not present */
				if(elm->default_value) {
					/* Fill-in DEFAULT */
Lev Walkin's avatar
Lev Walkin committed
1156
					if(elm->default_value(1, memb_ptr2)) {
Lev Walkin's avatar
Lev Walkin committed
1157
						FREEMEM(opres);
1158
						ASN__DECODE_FAILED;
Lev Walkin's avatar
Lev Walkin committed
1159
					}
Lev Walkin's avatar
Lev Walkin committed
1160
					ASN_DEBUG("Filled-in default");
Lev Walkin's avatar
Lev Walkin committed
1161 1162 1163 1164 1165 1166 1167 1168
				}
				/* The member is just not present */
				continue;
			}
			/* Fall through */
		}

		/* Fetch the member from the stream */
1169
		ASN_DEBUG("Decoding member \"%s\" in %s", elm->name, td->name);
1170

Lev Walkin's avatar
Lev Walkin committed
1171
		if(elm->flags & ATF_OPEN_TYPE) {
1172 1173
			rv = OPEN_TYPE_uper_get(opt_codec_ctx, td, st, elm, pd);
		} else {
Lev Walkin's avatar
Lev Walkin committed
1174
			rv = elm->type->op->uper_decoder(opt_codec_ctx, elm->type,
1175
					elm->encoding_constraints.per_constraints, memb_ptr2, pd);
1176 1177
		}
		if(rv.code != RC_OK) {
Lev Walkin's avatar
Lev Walkin committed
1178 1179 1180 1181 1182 1183 1184
			ASN_DEBUG("Failed decode %s in %s",
				elm->name, td->name);
			FREEMEM(opres);
			return rv;
		}
	}

Lev Walkin's avatar
Lev Walkin committed
1185 1186 1187
	/* Optionality map is not needed anymore */
	FREEMEM(opres);

Lev Walkin's avatar
Lev Walkin committed
1188 1189 1190 1191
	/*
	 * Deal with extensions.
	 */
	if(extpresent) {
Lev Walkin's avatar
Lev Walkin committed
1192 1193 1194
		ssize_t bmlength;
		uint8_t *epres;		/* Presence of extension members */
		asn_per_data_t epmd;
Lev Walkin's avatar
Lev Walkin committed
1195

Lev Walkin's avatar
Lev Walkin committed
1196
		bmlength = uper_get_nslength(pd);
1197
		if(bmlength < 0) ASN__DECODE_STARVED;
Lev Walkin's avatar
Lev Walkin committed
1198

Lev Walkin's avatar
Lev Walkin committed
1199
		ASN_DEBUG("Extensions %ld present in %s", (long)bmlength, td->name);
Lev Walkin's avatar
Lev Walkin committed
1200

Lev Walkin's avatar
Lev Walkin committed
1201
		epres = (uint8_t *)MALLOC((bmlength + 15) >> 3);
1202
		if(!epres) ASN__DECODE_STARVED;
Lev Walkin's avatar
Lev Walkin committed
1203 1204

		/* Get the extensions map */
Simo Sorce's avatar
Simo Sorce committed
1205 1206
		if(per_get_many_bits(pd, epres, 0, bmlength)) {
			FREEMEM(epres);
1207
			ASN__DECODE_STARVED;
Simo Sorce's avatar
Simo Sorce committed
1208
		}
Lev Walkin's avatar
Lev Walkin committed
1209

Lev Walkin's avatar
Lev Walkin committed
1210
		memset(&epmd, 0, sizeof(epmd));
Lev Walkin's avatar
Lev Walkin committed
1211 1212
		epmd.buffer = epres;
		epmd.nbits = bmlength;
Lev Walkin's avatar
Lev Walkin committed
1213 1214
		ASN_DEBUG("Read in extensions bitmap for %s of %ld bits (%x..)",
			td->name, (long)bmlength, *epres);
Lev Walkin's avatar
Lev Walkin committed
1215 1216 1217 1218 1219 1220 1221 1222 1223

	    /* Go over extensions and read them in */
	    for(edx = specs->ext_after + 1; edx < td->elements_count; edx++) {
		asn_TYPE_member_t *elm = &td->elements[edx];
		void *memb_ptr;		/* Pointer to the member */
		void **memb_ptr2;	/* Pointer to that pointer */
		int present;

		if(!IN_EXTENSION_GROUP(specs, edx)) {
Lev Walkin's avatar
Lev Walkin committed
1224
			ASN_DEBUG("%zu is not an extension", edx);
Lev Walkin's avatar
Lev Walkin committed
1225
			continue;
Lev Walkin's avatar
Lev Walkin committed
1226 1227
		}

Lev Walkin's avatar
Lev Walkin committed
1228 1229
		/* Fetch the pointer to this member */
		if(elm->flags & ATF_POINTER) {
Lev Walkin's avatar
Lev Walkin committed
1230
			memb_ptr2 = (void **)((char *)st + elm->memb_offset);
Lev Walkin's avatar
Lev Walkin committed
1231
		} else {
Lev Walkin's avatar
Lev Walkin committed
1232
			memb_ptr = (void *)((char *)st + elm->memb_offset);
Lev Walkin's avatar
Lev Walkin committed
1233 1234
			memb_ptr2 = &memb_ptr;
		}
Lev Walkin's avatar
Lev Walkin committed
1235

Lev Walkin's avatar
Lev Walkin committed
1236 1237 1238 1239 1240
		present = per_get_few_bits(&epmd, 1);
		if(present <= 0) {
			if(present < 0) break;	/* No more extensions */
			continue;
		}
1241

Lev Walkin's avatar
Lev Walkin committed
1242
		ASN_DEBUG("Decoding member %s in %s %p", elm->name, td->name, *memb_ptr2);
1243
		rv = uper_open_type_get(opt_codec_ctx, elm->type,
1244
			elm->encoding_constraints.per_constraints, memb_ptr2, pd);
Lev Walkin's avatar
Lev Walkin committed
1245 1246 1247 1248 1249 1250 1251 1252
		if(rv.code != RC_OK) {
			FREEMEM(epres);
			return rv;
		}
	    }

		/* Skip over overflow extensions which aren't present
		 * in this system's version of the protocol */
Lev Walkin's avatar
Lev Walkin committed
1253
		for(;;) {
Lev Walkin's avatar
Lev Walkin committed
1254
			ASN_DEBUG("Getting overflow extensions");
Lev Walkin's avatar
Lev Walkin committed
1255 1256 1257 1258
			switch(per_get_few_bits(&epmd, 1)) {
			case -1: break;
			case 0: continue;
			default:
1259
				if(uper_open_type_skip(opt_codec_ctx, pd)) {
Lev Walkin's avatar
Lev Walkin committed
1260
					FREEMEM(epres);
1261
					ASN__DECODE_STARVED;
Lev Walkin's avatar
Lev Walkin committed
1262
				}
Lev Walkin's avatar
Lev Walkin committed
1263
			}
Lev Walkin's avatar
Lev Walkin committed
1264
			break;
Lev Walkin's avatar
Lev Walkin committed
1265
		}
1266

Lev Walkin's avatar
Lev Walkin committed
1267
		FREEMEM(epres);
1268 1269
	}

Lev Walkin's avatar
Lev Walkin committed
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
	/* Fill DEFAULT members in extensions */
	for(edx = specs->roms_count; edx < specs->roms_count
			+ specs->aoms_count; edx++) {
		asn_TYPE_member_t *elm = &td->elements[edx];
		void **memb_ptr2;	/* Pointer to member pointer */

		if(!elm->default_value) continue;

		/* Fetch the pointer to this member */
		if(elm->flags & ATF_POINTER) {
Lev Walkin's avatar
Lev Walkin committed
1280
			memb_ptr2 = (void **)((char *)st
Lev Walkin's avatar
Lev Walkin committed
1281 1282 1283 1284 1285 1286 1287 1288
					+ elm->memb_offset);
			if(*memb_ptr2) continue;
		} else {
			continue;	/* Extensions are all optionals */
		}

		/* Set default value */
		if(elm->default_value(1, memb_ptr2)) {
1289
			ASN__DECODE_FAILED;
Lev Walkin's avatar
Lev Walkin committed
1290
		}
1291 1292
	}

Lev Walkin's avatar
Lev Walkin committed
1293 1294 1295
	rv.consumed = 0;
	rv.code = RC_OK;
	return rv;
1296 1297 1298 1299 1300 1301 1302
}

static int
SEQUENCE_handle_extensions(asn_TYPE_descriptor_t *td, void *sptr,
		asn_per_outp_t *po1, asn_per_outp_t *po2) {
	asn_SEQUENCE_specifics_t *specs
		= (asn_SEQUENCE_specifics_t *)td->specifics;
Lev Walkin's avatar
Lev Walkin committed
1303 1304
	int exts_present = 0;
	int exts_count = 0;
Lev Walkin's avatar
Lev Walkin committed
1305
	size_t edx;
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316

	if(specs->ext_before < 0)
		return 0;

	/* Find out which extensions are present */
	for(edx = specs->ext_after + 1; edx < td->elements_count; edx++) {
		asn_TYPE_member_t *elm = &td->elements[edx];
		void *memb_ptr;		/* Pointer to the member */
		void **memb_ptr2;	/* Pointer to that pointer */
		int present;

Lev Walkin's avatar
Lev Walkin committed
1317
		if(!IN_EXTENSION_GROUP(specs, edx)) {
Lev Walkin's avatar
Lev Walkin committed
1318
			ASN_DEBUG("%s (@%zu) is not extension", elm->type->name, edx);
1319
			continue;
Lev Walkin's avatar
Lev Walkin committed
1320
		}
1321 1322 1323

		/* Fetch the pointer to this member */
		if(elm->flags & ATF_POINTER) {
Lev Walkin's avatar
Lev Walkin committed
1324
			memb_ptr2 = (void **)((char *)sptr + elm->memb_offset);
1325 1326
			present = (*memb_ptr2 != 0);
		} else {
Lev Walkin's avatar
Lev Walkin committed
1327
			memb_ptr = (void *)((char *)sptr + elm->memb_offset);
1328 1329 1330 1331
			memb_ptr2 = &memb_ptr;
			present = 1;
		}

Lev Walkin's avatar
Lev Walkin committed
1332
		ASN_DEBUG("checking %s (@%zu) present => %d",
Lev Walkin's avatar
Lev Walkin committed
1333
			elm->type->name, edx, present);
Lev Walkin's avatar
Lev Walkin committed
1334 1335
		exts_count++;
		exts_present += present;
1336 1337 1338 1339 1340

		/* Encode as presence marker */
		if(po1 && per_put_few_bits(po1, present, 1))
			return -1;
		/* Encode as open type field */
1341
		if(po2 && present && uper_open_type_put(elm->type,
1342
				elm->encoding_constraints.per_constraints, *memb_ptr2, po2))
1343 1344 1345 1346
			return -1;

	}

Lev Walkin's avatar
Lev Walkin committed
1347
	return exts_present ? exts_count : 0;
1348 1349
}

Lev Walkin's avatar
Lev Walkin committed
1350 1351
asn_enc_rval_t
SEQUENCE_encode_uper(asn_TYPE_descriptor_t *td,
Lev Walkin's avatar
Lev Walkin committed
1352 1353
                     const asn_per_constraints_t *constraints, void *sptr,
                     asn_per_outp_t *po) {
1354
	asn_SEQUENCE_specifics_t *specs
Lev Walkin's avatar
Lev Walkin committed
1355 1356
		= (asn_SEQUENCE_specifics_t *)td->specifics;
	asn_enc_rval_t er;
1357
	int n_extensions;
Lev Walkin's avatar
Lev Walkin committed
1358 1359
	size_t edx;
	size_t i;
Lev Walkin's avatar
Lev Walkin committed
1360 1361 1362 1363

	(void)constraints;

	if(!sptr)
1364
		ASN__ENCODE_FAILED;
Lev Walkin's avatar
Lev Walkin committed
1365 1366 1367 1368

	er.encoded = 0;

	ASN_DEBUG("Encoding %s as SEQUENCE (UPER)", td->name);
1369 1370 1371 1372 1373 1374 1375 1376


	/*
	 * X.691#18.1 Whether structure is extensible
	 * and whether to encode extensions
	 */
	if(specs->ext_before >= 0) {
		n_extensions = SEQUENCE_handle_extensions(td, sptr, 0, 0);
1377
		if(n_extensions < 0)
Lev Walkin's avatar
Lev Walkin committed
1378 1379 1380
			ASN__ENCODE_FAILED;
		if(per_put_few_bits(po, n_extensions ? 1 : 0, 1))
			ASN__ENCODE_FAILED;
1381 1382 1383
	} else {
		n_extensions = 0;	/* There are no extensions to encode */
	}
Lev Walkin's avatar
Lev Walkin committed
1384 1385 1386

	/* Encode a presence bitmap */
	for(i = 0; i < specs->roms_count; i++) {
Lev Walkin's avatar
Lev Walkin committed
1387
		asn_TYPE_member_t *elm;
Lev Walkin's avatar
Lev Walkin committed
1388 1389 1390 1391
		void *memb_ptr;		/* Pointer to the member */
		void **memb_ptr2;	/* Pointer to that pointer */
		int present;

Lev Walkin's avatar
Lev Walkin committed
1392 1393 1394
		edx = specs->oms[i];
		elm = &td->elements[edx];

Lev Walkin's avatar
Lev Walkin committed
1395 1396
		/* Fetch the pointer to this member */
		if(elm->flags & ATF_POINTER) {
Lev Walkin's avatar
Lev Walkin committed
1397
			memb_ptr2 = (void **)((char *)sptr + elm->memb_offset);
Lev Walkin's avatar
Lev Walkin committed
1398 1399
			present = (*memb_ptr2 != 0);
		} else {
Lev Walkin's avatar
Lev Walkin committed
1400
			memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkin's avatar
Lev Walkin committed
1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
			memb_ptr2 = &memb_ptr;
			present = 1;
		}

		/* Eliminate default values */
		if(present && elm->default_value
		&& elm->default_value(0, memb_ptr2) == 1)
			present = 0;

		ASN_DEBUG("Element %s %s %s->%s is %s",
			elm->flags & ATF_POINTER ? "ptr" : "inline",
			elm->default_value ? "def" : "wtv",
			td->name, elm->name, present ? "present" : "absent");
		if(per_put_few_bits(po, present, 1))
1415
			ASN__ENCODE_FAILED;
Lev Walkin's avatar
Lev Walkin committed
1416 1417 1418
	}

	/*
1419
	 * Encode the sequence ROOT elements.
Lev Walkin's avatar
Lev Walkin committed
1420
	 */
1421
	ASN_DEBUG("ext_after = %d, ec = %d, eb = %d", specs->ext_after, td->elements_count, specs->ext_before);
Lev Walkin's avatar
Lev Walkin committed
1422 1423 1424
	for(edx = 0; edx < ((specs->ext_after < 0)
		? td->elements_count : specs->ext_before - 1); edx++) {

Lev Walkin's avatar
Lev Walkin committed
1425 1426 1427 1428
		asn_TYPE_member_t *elm = &td->elements[edx];
		void *memb_ptr;		/* Pointer to the member */
		void **memb_ptr2;	/* Pointer to that pointer */

Lev Walkin's avatar
Lev Walkin committed
1429 1430 1431
		if(IN_EXTENSION_GROUP(specs, edx))
			continue;

Lev Walkin's avatar
Lev Walkin committed
1432 1433
		ASN_DEBUG("About to encode %s", elm->type->name);

Lev Walkin's avatar
Lev Walkin committed
1434 1435
		/* Fetch the pointer to this member */
		if(elm->flags & ATF_POINTER) {
Lev Walkin's avatar
Lev Walkin committed
1436
			memb_ptr2 = (void **)((char *)sptr + elm->memb_offset);
Lev Walkin's avatar
Lev Walkin committed
1437
			if(!*memb_ptr2) {
Lev Walkin's avatar
Lev Walkin committed
1438
				ASN_DEBUG("Element %s %zu not present",
Lev Walkin's avatar
Lev Walkin committed
1439 1440 1441 1442
					elm->name, edx);
				if(elm->optional)
					continue;
				/* Mandatory element is missing */
1443
				ASN__ENCODE_FAILED;
Lev Walkin's avatar
Lev Walkin committed
1444 1445
			}
		} else {
Lev Walkin's avatar
Lev Walkin committed
1446
			memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkin's avatar
Lev Walkin committed
1447 1448 1449 1450
			memb_ptr2 = &memb_ptr;
		}

		/* Eliminate default values */
Lev Walkin's avatar
Lev Walkin committed
1451
		if(elm->default_value && elm->default_value(0, memb_ptr2) == 1)
Lev Walkin's avatar
Lev Walkin committed
1452 1453
			continue;

Lev Walkin's avatar
Lev Walkin committed
1454
		ASN_DEBUG("Encoding %s->%s", td->name, elm->name);
1455
		er = elm->type->op->uper_encoder(elm->type, elm->encoding_constraints.per_constraints,
Lev Walkin's avatar
Lev Walkin committed
1456 1457 1458 1459 1460
			*memb_ptr2, po);
		if(er.encoded == -1)
			return er;
	}

1461
	/* No extensions to encode */
1462
	if(!n_extensions) ASN__ENCODED_OK(er);
1463 1464 1465 1466

	ASN_DEBUG("Length of %d bit-map", n_extensions);
	/* #18.8. Write down the presence bit-map length. */
	if(uper_put_nslength(po, n_extensions))
1467
		ASN__ENCODE_FAILED;
1468 1469 1470 1471 1472

	ASN_DEBUG("Bit-map of %d elements", n_extensions);
	/* #18.7. Encoding the extensions presence bit-map. */
	/* TODO: act upon NOTE in #18.7 for canonical PER */
	if(SEQUENCE_handle_extensions(td, sptr, po, 0) != n_extensions)
1473
		ASN__ENCODE_FAILED;
1474 1475 1476 1477

	ASN_DEBUG("Writing %d extensions", n_extensions);
	/* #18.9. Encode extensions as open type fields. */
	if(SEQUENCE_handle_extensions(td, sptr, 0, po) != n_extensions)
1478
		ASN__ENCODE_FAILED;
1479

1480
	ASN__ENCODED_OK(er);
Lev Walkin's avatar
Lev Walkin committed
1481 1482
}

Lev Walkin's avatar
Lev Walkin committed
1483 1484
#endif  /* ASN_DISABLE_PER_SUPPORT */

1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
int
SEQUENCE_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
                 const void *bptr) {
    size_t edx;

	for(edx = 0; edx < td->elements_count; edx++) {
		asn_TYPE_member_t *elm = &td->elements[edx];
		const void *amemb;
		const void *bmemb;
        int ret;

		if(elm->flags & ATF_POINTER) {
            amemb =
                *(const void *const *)((const char *)aptr + elm->memb_offset);
            bmemb =
                *(const void *const *)((const char *)bptr + elm->memb_offset);
            if(!amemb) {
                if(!bmemb) continue;
                return -1;
            } else if(!bmemb) {
                return 1;
            }
		} else {
            amemb = (const void *)((const char *)aptr + elm->memb_offset);
            bmemb = (const void *)((const char *)bptr + elm->memb_offset);
		}

1512
        ret = elm->type->op->compare_struct(elm->type, amemb, bmemb);
1513 1514 1515 1516 1517 1518
        if(ret != 0) return ret;
    }

    return 0;
}

1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540
asn_TYPE_operation_t asn_OP_SEQUENCE = {
	SEQUENCE_free,
	SEQUENCE_print,
	SEQUENCE_compare,
	SEQUENCE_decode_ber,
	SEQUENCE_encode_der,
	SEQUENCE_decode_xer,
	SEQUENCE_encode_xer,
#ifdef	ASN_DISABLE_OER_SUPPORT
	0,
	0,
#else
	SEQUENCE_decode_oer,
	SEQUENCE_encode_oer,
#endif  /* ASN_DISABLE_OER_SUPPORT */
#ifdef ASN_DISABLE_PER_SUPPORT
	0,
	0,
#else
	SEQUENCE_decode_uper,
	SEQUENCE_encode_uper,
#endif /* ASN_DISABLE_PER_SUPPORT */
1541
	SEQUENCE_random_fill,
1542 1543 1544
	0	/* Use generic outmost tag fetcher */
};

1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612

asn_random_fill_result_t
SEQUENCE_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
                   const asn_encoding_constraints_t *constr,
                   size_t max_length) {
    const asn_SEQUENCE_specifics_t *specs =
        (const asn_SEQUENCE_specifics_t *)td->specifics;
    asn_random_fill_result_t result_ok = {ARFILL_OK, 0};
    asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
    asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
    void *st = *sptr;
    size_t edx;

    if(max_length == 0) return result_skipped;

    (void)constr;

    if(st == NULL) {
        st = CALLOC(1, specs->struct_size);
        if(st == NULL) {
            return result_failed;
        }
    }

    for(edx = 0; edx < td->elements_count; edx++) {
        const asn_TYPE_member_t *elm = &td->elements[edx];
        void *memb_ptr;   /* Pointer to the member */
        void **memb_ptr2; /* Pointer to that pointer */
        asn_random_fill_result_t tmpres;

        if(elm->optional && asn_random_between(0, 4) == 2) {
            /* Sometimes decide not to fill the optional value */
            continue;
        }

        if(elm->flags & ATF_POINTER) {
            /* Member is a pointer to another structure */
            memb_ptr2 = (void **)((char *)st + elm->memb_offset);
        } else {
            memb_ptr = (char *)st + elm->memb_offset;
            memb_ptr2 = &memb_ptr;
        }

        tmpres = elm->type->op->random_fill(
            elm->type, memb_ptr2, &elm->encoding_constraints,
            max_length > result_ok.length ? max_length - result_ok.length : 0);
        switch(tmpres.code) {
        case ARFILL_OK:
            result_ok.length += tmpres.length;
            continue;
        case ARFILL_SKIPPED:
            assert(!(elm->flags & ATF_POINTER) || *memb_ptr2 == NULL);
            continue;
        case ARFILL_FAILED:
            if(st == *sptr) {
                ASN_STRUCT_RESET(*td, st);
            } else {
                ASN_STRUCT_FREE(*td, st);
            }
            return tmpres;
        }
    }

    *sptr = st;

    return result_ok;
}