asn1fix_crange.c 35.2 KB
Newer Older
Lev Walkin's avatar
Lev Walkin committed
1 2 3
#include "asn1fix_internal.h"
#include "asn1fix_constraint.h"
#include "asn1fix_crange.h"
Lev Walkin's avatar
Lev Walkin committed
4 5 6 7 8 9 10 11

#undef	FATAL
#define	FATAL(fmt, args...)	do {			\
		fprintf(stderr, "FATAL: ");		\
		fprintf(stderr, fmt, ##args);		\
		fprintf(stderr, "\n");			\
	} while(0)

12 13
static void
asn1constraint_range_free_outer(asn1cnst_range_t *cr) {
Lev Walkin's avatar
Lev Walkin committed
14 15 16 17 18 19 20
	if(cr) {
		int i;
		if(cr->elements) {
			for(i = 0; i < cr->el_count; i++)
				asn1constraint_range_free(cr->elements[i]);
			free(cr->elements);
		}
21 22 23 24 25 26 27 28
        memset(cr, 0, sizeof(*cr));
    }
}

void
asn1constraint_range_free(asn1cnst_range_t *cr) {
    asn1constraint_range_free_outer(cr);
    free(cr);
Lev Walkin's avatar
Lev Walkin committed
29
}
30

Lev Walkin's avatar
Lev Walkin committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
#define	_range_free(foo)	asn1constraint_range_free(foo)

static asn1cnst_range_t *_range_new() {
	asn1cnst_range_t *r;
	r = calloc(1, sizeof(*r));
	if(r) {
		r->left.type = ARE_MIN;
		r->right.type = ARE_MAX;
	}
	return r;
}

static void _range_remove_element(asn1cnst_range_t *range, int idx) {
	assert(idx >= 0 && idx < range->el_count);

	assert(!range->elements[idx]->elements);

	_range_free(range->elements[idx]);

	memmove(&range->elements[idx],
		&range->elements[idx + 1],
		(range->el_count - idx - 1)
			* sizeof(range->elements[0])
	);
	range->el_count--;
	range->elements[range->el_count] = 0;	/* JIC */

	if(range->el_count == 0) {
		range->el_size = 0;
		free(range->elements);
		range->elements = 0;
	}
}

static int _range_insert(asn1cnst_range_t *into, asn1cnst_range_t *cr) {

	assert(!cr->elements);

	if(into->el_count == into->el_size) {
		void *p;
		int n = into->el_size?(into->el_size << 1):4;
		p = realloc(into->elements, n * sizeof(into->elements[0]));
		if(p) {
			into->el_size = n;
			into->elements = p;
		} else {
			assert(p);
			return -1;
		}
	}

	into->elements[into->el_count++] = cr;
	return 0;
}

static asn1cnst_range_t *_range_clone(const asn1cnst_range_t *range) {
	asn1cnst_range_t *clone;
	int i;

	clone = _range_new();
	if(!clone) return NULL;

	*clone = *range;
	clone->elements = 0;
	clone->el_count = 0;
	clone->el_size = 0;

	for(i = 0; i < range->el_count; i++) {
		asn1cnst_range_t *r = _range_clone(range->elements[i]);
		if(!r || _range_insert(clone, r)) {
			_range_free(clone);
			_range_free(r);
			return NULL;
		}
	}

	return clone;
}

static int
_edge_compare(const asn1cnst_edge_t *el, const asn1cnst_edge_t *er) {

	switch(el->type) {
	case ARE_MIN:
		switch(er->type) {
		case ARE_MIN: return 0;
		case ARE_MAX: return -1;
		case ARE_VALUE: return -1;
		}
		break;
	case ARE_MAX:
		switch(er->type) {
		case ARE_MIN: return 1;
		case ARE_MAX: return 0;
		case ARE_VALUE: return 1;
		}
		break;
	case ARE_VALUE:
		switch(er->type) {
		case ARE_MIN: return 1;
		case ARE_MAX: return -1;
		case ARE_VALUE:
			if(el->value < er->value)
				return -1;
			if(el->value > er->value)
				return 1;
			return 0;
		}
		break;
	}

	return 0;
}

static int
_range_compare(const void *a, const void *b) {
	const asn1cnst_range_t *ra = *(const asn1cnst_range_t * const *)a;
	const asn1cnst_range_t *rb = *(const asn1cnst_range_t * const *)b;
	int ret;

	ret = _edge_compare(&ra->left, &rb->left);
	if(!ret) {
		ret = _edge_compare(&ra->right, &rb->right);
	}

	return ret;
}

159 160
static const char *
_edge_print(const asn1cnst_edge_t *edge, char *buf, size_t size) {
Lev Walkin's avatar
Lev Walkin committed
161
	*buf = '\0';
162
    assert(size > 4);
Lev Walkin's avatar
Lev Walkin committed
163 164 165 166
	switch(edge->type) {
	case ARE_MIN:	strcpy(buf, "MIN"); break;
	case ARE_MAX:	strcpy(buf, "MAX"); break;
	case ARE_VALUE:
167
		snprintf(buf, size, "%s", asn1p_itoa(edge->value));
168 169 170
        break;
    default:
        assert(!"edge->type");
Lev Walkin's avatar
Lev Walkin committed
171 172 173 174
	}
	return buf;
}

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
static const char *
_edge_string(const asn1cnst_edge_t *edge) {
	static char buf[128];
    return _edge_print(edge, buf, sizeof(buf));
}

static const char *
_range_string(const asn1cnst_range_t *range) {
    static char buf[128];
    char buf0[128];
    char buf1[128];
    snprintf(buf, sizeof(buf), "(%s..%s)",
             _edge_print(&range->left, buf0, sizeof(buf0)),
             _edge_print(&range->right, buf1, sizeof(buf1)));
    return buf;
}

Lev Walkin's avatar
Lev Walkin committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
static int
_edge_is_within(const asn1cnst_range_t *range, const asn1cnst_edge_t *edge) {
	int i;

	for(i = -1; i < range->el_count; i++) {
		const asn1cnst_range_t *r;
		if(i == -1) {
			if(range->el_count) continue;
			r = range;
		} else {
			r = range->elements[i];
		}
		if(_edge_compare(&r->left, edge) <= 0
		&& _edge_compare(&r->right, edge) >= 0)
			return 1;
	}

	return 0;
}

static int
_check_edges_within(const asn1cnst_range_t *range, const asn1cnst_range_t *r) {

	if(!_edge_is_within(range, &r->left)) {
		FATAL("Constraint value %s at line %d "
			"is not within "
218 219 220 221
			"a parent constraint range %s",
			_edge_string(&r->left),
			r->left.lineno,
			_range_string(range)
Lev Walkin's avatar
Lev Walkin committed
222 223 224 225 226 227 228
		);
		return -1;
	}

	if(!_edge_is_within(range, &r->right)) {
		FATAL("Constraint value %s at line %d "
			"is not within "
229 230 231 232
			"a parent constraint range %s",
			_edge_string(&r->right),
			r->left.lineno,
			_range_string(range)
Lev Walkin's avatar
Lev Walkin committed
233 234 235 236 237 238 239
		);
		return -1;
	}

	return 0;
}

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
/*
 * Combine narrowings to get the strongest (most specific).
 */
static enum asn1cnst_range_narrowing
_strongest_narrowing(const asn1cnst_range_t *ar, const asn1cnst_range_t *br) {
    enum asn1cnst_range_narrowing an = ar->narrowing;
    enum asn1cnst_range_narrowing bn = br->narrowing;

    if(!an) {
        return bn;
    } else if(!bn) {
        return an;
    } else {
        return an > bn ? an : bn;
    }
}

/*
 * Combine narrowings to get the softest one.
 */
static enum asn1cnst_range_narrowing
_softest_narrowing(const asn1cnst_range_t *ar, const asn1cnst_range_t *br) {
    enum asn1cnst_range_narrowing an = ar->narrowing;
    enum asn1cnst_range_narrowing bn = br->narrowing;

    if(an) {
        if(bn) {
            return an < bn ? an : bn;
        } else {
            assert(bn == NOT_NARROW);
            if(br->left.type == ARE_VALUE && br->right.type == ARE_VALUE) {
                return an;
            } else {
                return NOT_NARROW;
            }
        }
    } else if(bn) {
        if(ar->left.type == ARE_VALUE && ar->right.type == ARE_VALUE) {
            return bn;
        } else {
            return NOT_NARROW;
        }
    } else {
        return NOT_NARROW;
    }
}

Lev Walkin's avatar
Lev Walkin committed
287 288 289 290 291
static int _range_merge_in(asn1cnst_range_t *into, asn1cnst_range_t *cr) {
	asn1cnst_range_t *r;
	int prev_count = into->el_count;
	int i;

Lev Walkin's avatar
Lev Walkin committed
292
	into->not_OER_visible |= cr->not_OER_visible;
293 294
	into->not_PER_visible |= cr->not_PER_visible;
	into->extensible |= cr->extensible;
Lev Walkin's avatar
Lev Walkin committed
295 296
	if(into->extensible)
		into->not_OER_visible = 1;
297

298 299
    into->narrowing = _softest_narrowing(into, cr);

Lev Walkin's avatar
Lev Walkin committed
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
	/*
	 * Add the element OR all its children "into".
	 */
	for(i = -1; i < cr->el_count; i++) {

		if(i == -1) {
			if(cr->el_count) continue;
			r = cr;
		} else {
			r = cr->elements[i];
		}

		if(_range_insert(into, r)) {
			into->el_count = prev_count;	/* Undo */
			return -1;
		}
	}

	if(cr->el_count) {
		cr->el_count = 0;
		_range_free(cr);
	} else {
		/* This range is linked into "into". */
	}

	return 0;
}

328 329 330 331 332 333
enum range_fill_result {
    RFR_OK,
    RFR_FAIL,
    RFR_INCOMPATIBLE
};
static enum range_fill_result _range_fill(asn1p_value_t *val, const asn1cnst_range_t *minmax, asn1cnst_edge_t *edge, asn1cnst_range_t *range, enum asn1p_constraint_type_e type, int lineno) {
Lev Walkin's avatar
Lev Walkin committed
334 335 336 337 338 339 340
	unsigned char *p, *pend;

	edge->lineno = lineno;

	switch(val->type) {
	case ATV_INTEGER:
		if(type != ACT_EL_RANGE && type != ACT_CT_SIZE) {
341
			FATAL("Integer %s value invalid "
Lev Walkin's avatar
Lev Walkin committed
342
				"for %s constraint at line %d",
343
				asn1p_itoa(val->value.v_integer),
Lev Walkin's avatar
Lev Walkin committed
344
				asn1p_constraint_type2str(type), lineno);
345
			return RFR_FAIL;
Lev Walkin's avatar
Lev Walkin committed
346 347 348
		}
		edge->type = ARE_VALUE;
		edge->value = val->value.v_integer;
349
		return RFR_OK;
Lev Walkin's avatar
Lev Walkin committed
350 351 352 353
	case ATV_MIN:
		if(type != ACT_EL_RANGE && type != ACT_CT_SIZE) {
			FATAL("MIN invalid for %s constraint at line %d",
				asn1p_constraint_type2str(type), lineno);
354
			return RFR_FAIL;
Lev Walkin's avatar
Lev Walkin committed
355 356 357 358
		}
		edge->type = ARE_MIN;
		if(minmax) *edge = minmax->left;
		edge->lineno = lineno;	/* Restore lineno */
359
		return RFR_OK;
Lev Walkin's avatar
Lev Walkin committed
360 361 362 363
	case ATV_MAX:
		if(type != ACT_EL_RANGE && type != ACT_CT_SIZE) {
			FATAL("MAX invalid for %s constraint at line %d",
				asn1p_constraint_type2str(type), lineno);
364
			return RFR_FAIL;
Lev Walkin's avatar
Lev Walkin committed
365 366 367 368
		}
		edge->type = ARE_MAX;
		if(minmax) *edge = minmax->right;
		edge->lineno = lineno;	/* Restore lineno */
369
		return RFR_OK;
Lev Walkin's avatar
Lev Walkin committed
370 371 372 373 374 375 376
	case ATV_FALSE:
	case ATV_TRUE:
		if(type != ACT_EL_RANGE) {
			FATAL("%s is invalid for %s constraint at line %d",
				val->type==ATV_TRUE?"TRUE":"FALSE",
				asn1p_constraint_type2str(type),
				lineno);
377
			return RFR_FAIL;
Lev Walkin's avatar
Lev Walkin committed
378 379 380
		}
		edge->type = ARE_VALUE;
		edge->value = (val->type==ATV_TRUE);
381
		return RFR_OK;
Lev Walkin's avatar
Lev Walkin committed
382 383 384 385
	case ATV_TUPLE:
	case ATV_QUADRUPLE:
		edge->type = ARE_VALUE;
		edge->value = val->value.v_integer;
386
		return RFR_OK;
Lev Walkin's avatar
Lev Walkin committed
387 388
	case ATV_STRING:
		if(type != ACT_CT_FROM)
389
			return RFR_OK;
Lev Walkin's avatar
Lev Walkin committed
390
		break;
Lev Walkin's avatar
Lev Walkin committed
391
	case ATV_REFERENCED:
Lev Walkin's avatar
Lev Walkin committed
392
		FATAL("Unresolved constraint element \"%s\" at line %d",
Lev Walkin's avatar
Lev Walkin committed
393 394
			asn1f_printable_reference(val->value.reference),
			lineno);
395 396 397 398 399
		return RFR_FAIL;
	case ATV_BITVECTOR:
        /* Value constraint... not supported yet. */
        /* OER: X.696 (08/2015) #8.2.2i */
        return RFR_INCOMPATIBLE;
Lev Walkin's avatar
Lev Walkin committed
400
	default:
401 402 403
		FATAL("Unrecognized constraint range element type %d at line %d",
			val->type, lineno);
		return RFR_FAIL;
Lev Walkin's avatar
Lev Walkin committed
404 405 406 407 408 409
	}

	assert(val->type == ATV_STRING);

	p = val->value.string.buf;
	pend = p + val->value.string.size;
410
	if(p == pend) return RFR_OK;
Lev Walkin's avatar
Lev Walkin committed
411 412 413 414 415 416 417 418 419 420 421

	edge->type = ARE_VALUE;
	if(val->value.string.size == 1) {
		edge->value = *p;
	} else {
		/*
		 * Else this is a set:
		 * (FROM("abcdef"))
		 * However, (FROM("abc".."def")) is forbidden.
		 * See also 47.4.4.
		 */
422
		asn1c_integer_t vmin, vmax;
Lev Walkin's avatar
Lev Walkin committed
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
		vmin = vmax = *p;
		for(; p < pend; p++) {
			asn1cnst_range_t *nr = _range_new();
			int ret;
			assert(nr);

			if(*p < vmin) vmin = *p;
			if(*p > vmax) vmax = *p;

			ret = _range_insert(range, nr);
			assert(ret == 0);

			nr->left.type = ARE_VALUE;
			nr->left.value = *p;
			nr->left.lineno = lineno;
			nr->right = nr->left;
		}
		edge->value = (edge == &range->right) ? vmin : vmax;
	}

443
	return RFR_OK;
Lev Walkin's avatar
Lev Walkin committed
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
}

/*
 * Check if ranges contain common elements.
 */
static int
_range_overlap(const asn1cnst_range_t *ra, const asn1cnst_range_t *rb) {
	int lr, rl;
	const asn1cnst_edge_t *ra_l = &ra->left;
	const asn1cnst_edge_t *ra_r = &ra->right;
	const asn1cnst_edge_t *rb_l = &rb->left;
	const asn1cnst_edge_t *rb_r = &rb->right;

	assert(_edge_compare(ra_l, ra_r) <= 0);
	assert(_edge_compare(rb_l, rb_r) <= 0);

	lr = _edge_compare(ra_l, rb_r);
	rl = _edge_compare(ra_r, rb_l);

	/*
	 * L:       |---|
	 * R: |---|
	 */
	if(lr > 0) return 0;

	/*
	 * L: |---|
	 * R:       |---|
	 */
	if(rl < 0) return 0;

	return 1;
}

478 479 480 481 482 483 484 485 486 487 488 489 490

static int _range_partial_compare(const void *pa, const void *pb) {
    const asn1cnst_range_t *ra = *(const void * const *)pa;
    const asn1cnst_range_t *rb = *(const void * const *)pb;

	return _edge_compare(&ra->left, &rb->left);
}

static void _range_partial_sort_elements(asn1cnst_range_t *r) {
    qsort(r->elements, r->el_count, sizeof(r->elements[0]),
          _range_partial_compare);
}

Lev Walkin's avatar
Lev Walkin committed
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
/*
 * (MIN..20) x (10..15) = (MIN..9,10..15,16..20)
 */
static asn1cnst_range_t *
_range_split(asn1cnst_range_t *ra, const asn1cnst_range_t *rb) {
	asn1cnst_range_t *range, *nr;
	int ll, rr;

	assert(ra);
	assert(rb);
	assert(!ra->el_count);
	assert(!rb->el_count);

	if(!_range_overlap(ra, rb)) {
		errno = 0;
		return 0;
	}

	ll = _edge_compare(&ra->left, &rb->left);
	rr = _edge_compare(&ra->right, &rb->right);

	/*
	 * L:   |---|
	 * R: |-------|
	 */
	if(ll >= 0 && rr <= 0) {
		errno = 0;
		return 0;
	}

	range = _range_new();
	assert(range);

	nr = _range_new();
	assert(nr);

	/*
	 * L: |---...
	 * R:   |--..
	 */
Lev Walkin's avatar
Lev Walkin committed
531
	while(ll < 0) {
Lev Walkin's avatar
Lev Walkin committed
532 533
		nr->left = ra->left;
		nr->right = rb->left;
Lev Walkin's avatar
Lev Walkin committed
534
		if(nr->right.type == ARE_VALUE) {
535
			if(nr->right.value == INTMAX_MIN) {
Lev Walkin's avatar
Lev Walkin committed
536 537 538
				/* We've hit the limit here. */
				break;
			}
Lev Walkin's avatar
Lev Walkin committed
539
			nr->right.value--;
Lev Walkin's avatar
Lev Walkin committed
540
		}
Lev Walkin's avatar
Lev Walkin committed
541 542 543
		_range_insert(range, nr);
		nr = _range_new();
		assert(nr);
Lev Walkin's avatar
Lev Walkin committed
544
		break;
Lev Walkin's avatar
Lev Walkin committed
545 546 547 548 549 550
	}

	/*
	 * L: ...---|
	 * R: ..--|
	 */
Lev Walkin's avatar
Lev Walkin committed
551
	while(rr > 0) {
Lev Walkin's avatar
Lev Walkin committed
552 553
		nr->left = rb->right;
		nr->right = ra->right;
Lev Walkin's avatar
Lev Walkin committed
554
		if(nr->left.type == ARE_VALUE) {
555
			if(nr->left.value == INTMAX_MAX) {
Lev Walkin's avatar
Lev Walkin committed
556 557 558
				/* We've hit the limit here. */
				break;
			}
Lev Walkin's avatar
Lev Walkin committed
559
			nr->left.value++;
Lev Walkin's avatar
Lev Walkin committed
560
		}
Lev Walkin's avatar
Lev Walkin committed
561 562 563
		_range_insert(range, nr);
		nr = _range_new();
		assert(nr);
Lev Walkin's avatar
Lev Walkin committed
564
		break;
Lev Walkin's avatar
Lev Walkin committed
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
	}

	/*
	 * L:  |---|
	 * R: |-----|
	 */
	nr->left = ra->left;
	nr->right = ra->right;
	if(_edge_compare(&ra->left, &rb->left) < 0)
		nr->left = rb->left;
	if(_edge_compare(&ra->right, &rb->right) > 0)
		nr->right = rb->right;

	_range_insert(range, nr);

580 581
    _range_partial_sort_elements(range);

Lev Walkin's avatar
Lev Walkin committed
582 583 584 585
	return range;
}

static int
Lev Walkin's avatar
Lev Walkin committed
586
_range_intersection(asn1cnst_range_t *range, const asn1cnst_range_t *with, int strict_edge_check, int is_oer) {
Lev Walkin's avatar
Lev Walkin committed
587 588 589
	int ret;
	int i, j;

590 591
	assert(!range->incompatible);

Lev Walkin's avatar
Lev Walkin committed
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
    if(is_oer) {
        assert(range->extensible == 0);
        assert(range->not_OER_visible == 0);
        assert(with->extensible == 0);
        assert(with->not_OER_visible == 0);
        if(range->extensible) {
            assert(range->not_OER_visible);
        }
        if(range->extensible || range->not_OER_visible) {
            /* X.696 #8.2.4 Completely ignore the extensible constraints */
            /* (XXX)(YYY,...) Retain the first one (XXX). */
            asn1cnst_range_t *tmp = _range_new();
            *tmp = *range;
            *range = *with;
            _range_free(tmp);
            return 0;
        }

        if(with->extensible) {
            assert(with->not_OER_visible);
        }
        if(with->extensible || with->not_OER_visible) {
            /* X.696 #8.2.4 Completely ignore the extensible constraints */
            return 0;
        }
    } else {
        /* Propagate errors */
        range->extensible |= with->extensible;
        if(with->extensible)
            range->not_OER_visible = 1;
        range->not_PER_visible |= with->not_PER_visible;
    }
624 625
	range->empty_constraint |= with->empty_constraint;

626 627
    range->narrowing = _strongest_narrowing(range, with);

628 629
	if(range->empty_constraint) {
		/* No use in intersecting empty constraints */
Lev Walkin's avatar
Lev Walkin committed
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
		return 0;
	}

	/*
	 * This is an AND operation.
	 */

	/* If this is the only element, insert it into itself as a child */
	if(range->el_count == 0) {
		asn1cnst_range_t *r = _range_new();
		r->left = range->left;
		r->right = range->right;
		_range_insert(range, r);
		assert(range->el_count == 1);
	}

	/*
	 * Make sure we're dealing with sane data.
	 * G.4.2.3
	 */
	if(strict_edge_check) {
		for(j = -1; j < with->el_count; j++) {

			if(j == -1) {
				if(with->el_count) continue;
				if(_check_edges_within(range, with))
					return -1;
			} else {
				if(_check_edges_within(range,
						with->elements[j]))
					return -1;
			}
		}
	}

	/*
	 * Split range in pieces.
	 */

	for(i = 0; i < range->el_count; i++) {
	  for(j = -1; j < with->el_count; j++) {
		const asn1cnst_range_t *wel;
		asn1cnst_range_t *r;

		if(j == -1) {
			if(with->el_count) continue;
			wel = with;
		} else {
			wel = with->elements[j];
Lev Walkin's avatar
Lev Walkin committed
679
			assert(!wel->el_count);	/* non-compound item! */
Lev Walkin's avatar
Lev Walkin committed
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
		}

		r = _range_split(range->elements[i], wel);
		if(r) {
			int ec;
			/* Substitute the current element with a split */
			_range_remove_element(range, i);
			assert(r->el_count);
			for(ec = 0; ec < r->el_count; ec++) {
				ret = _range_insert(range, r->elements[ec]);
				assert(ret == 0);
			}
			r->el_count = 0;
			_range_free(r);
			i--;
			break;	/* Try again from this point */
		}
	  }
	}

	assert(range->el_count);

	/*
	 * Remove pieces which aren't AND-compatible "with" range.
	 */

	for(i = 0; i < range->el_count; i++) {
		for(j = -1; j < with->el_count; j++) {
			const asn1cnst_range_t *wel;
	
			if(j == -1) {
				if(with->el_count) continue;
				wel = with;
			} else {
				wel = with->elements[j];
			}

			if(_range_overlap(range->elements[i], wel))
				break;
		}
		if(j == with->el_count) {
			_range_remove_element(range, i);
			i--;
		}
	}

	if(range->el_count == 0)
		range->empty_constraint = 1;

	return 0;
}

static int
_range_union(asn1cnst_range_t *range) {
	int i;

	qsort(range->elements, range->el_count, sizeof(range->elements[0]),
		_range_compare);

	/*
	 * The range is sorted by the start values.
	 */
	for(i = 1; i < range->el_count; i++) {
		asn1cnst_range_t *ra = range->elements[i - 1];
		asn1cnst_range_t *rb = range->elements[i];

		if(_range_overlap(ra, rb)) {
			if(_edge_compare(&ra->left, &rb->left) < 0)
				rb->left = ra->left;

			if(_edge_compare(&ra->right, &rb->right) > 0)
				rb->right = ra->right;
		} else {
			/*
			 * Still, range may be joined: (1..4)(5..10).
			 * This logic is valid only for whole numbers
			 * (i.e., not REAL type, but REAL constraints
757
			 * are not PER-visible (X.691, #9.3.12).
Lev Walkin's avatar
Lev Walkin committed
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
			 */
			if(ra->right.type == ARE_VALUE
			&& rb->left.type == ARE_VALUE
			&& (rb->left.value - ra->right.value) == 1) {
				/* (1..10) */
				rb->left = ra->left;
			} else {
				continue;
			}
		}

		/*
		 * Squeeze the array by removing the ra.
		 */
		_range_remove_element(range, i - 1);

		i--;	/* Retry from the current point */
	}

	return 0;
}

static int
_range_canonicalize(asn1cnst_range_t *range) {

	if(range->el_count == 0) {
		/*
		 * Switch left and right edges, make them sorted.
		 * It might be a mild warning though.
		 */
		if(_edge_compare(&range->left, &range->right) > 0) {
			asn1cnst_edge_t tmp = range->left;
			range->left = range->right;
			range->right = tmp;
		}

794 795
		free(range->elements);
		range->elements = 0;
Lev Walkin's avatar
Lev Walkin committed
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
		range->el_size = 0;
		return 0;
	}

	/*
	 * Remove duplicates and overlaps by merging them in.
	 */
	_range_union(range);

	/* Refine the left edge of a parent */
	range->left = range->elements[0]->left;

	/* Refine the right edge of a parent */
	range->right = range->elements[range->el_count - 1]->right;

	/* Remove the child, if it's a single one */
	if(range->el_count == 1) {
		_range_remove_element(range, 0);
	}

	return 0;
}

Lev Walkin's avatar
Lev Walkin committed
819
asn1cnst_range_t *
Lev Walkin's avatar
Lev Walkin committed
820
asn1constraint_compute_OER_range(const char *dbg_name, asn1p_expr_type_e expr_type, const asn1p_constraint_t *ct, enum asn1p_constraint_type_e requested_ct_type, const asn1cnst_range_t *minmax, int *exmet, enum cpr_flags cpr_flags) {
Lev Walkin's avatar
Lev Walkin committed
821
    return asn1constraint_compute_constraint_range(dbg_name, expr_type, ct, requested_ct_type, minmax, exmet, cpr_flags | CPR_strict_OER_visibility);
Lev Walkin's avatar
Lev Walkin committed
822 823
}

Lev Walkin's avatar
Lev Walkin committed
824
asn1cnst_range_t *
Lev Walkin's avatar
Lev Walkin committed
825
asn1constraint_compute_PER_range(const char *dbg_name, asn1p_expr_type_e expr_type, const asn1p_constraint_t *ct, enum asn1p_constraint_type_e requested_ct_type, const asn1cnst_range_t *minmax, int *exmet, enum cpr_flags cpr_flags) {
Lev Walkin's avatar
Lev Walkin committed
826 827 828
    if(0) return asn1constraint_compute_constraint_range(dbg_name, expr_type, ct, requested_ct_type, minmax, exmet, cpr_flags | CPR_strict_PER_visibility);
    /* Due to pecularities of PER constraint handling, we don't enable strict PER visibility upfront here. */
    return asn1constraint_compute_constraint_range(dbg_name, expr_type, ct, requested_ct_type, minmax, exmet, cpr_flags);
Lev Walkin's avatar
Lev Walkin committed
829 830
}

831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 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 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
static asn1cnst_range_t *
asn1f_real_range_from_WCOMPS(const char *dbg_name,
                             const asn1p_constraint_t *ct) {
    asn1cnst_range_t two = {
        {ARE_VALUE, 0, 2}, {ARE_VALUE, 0, 2}, 0, NULL, 0, 0, 0, 0, 0, 0, 0};
    asn1cnst_range_t ten = {
        {ARE_VALUE, 0, 10}, {ARE_VALUE, 0, 10}, 0, NULL, 0, 0, 0, 0, 0, 0, 0};
    asn1cnst_range_t *two_ten[] = {&two, &ten};
    /* Interpretation of X.680 #21.5 */
    asn1cnst_range_t mantissa_default_range = {
        {ARE_MIN, 0, 0}, {ARE_MAX, 0, 0}, 0, NULL, 0, 0, 0, 0, 0, 0, 0};
    asn1cnst_range_t exponent_default_range = {
        {ARE_MIN, 0, 0}, {ARE_MAX, 0, 0}, 0, NULL, 0, 0, 0, 0, 0, 0, 0};
    asn1cnst_range_t base_default_range = {
        {ARE_VALUE, 0, 2}, {ARE_MAX, 0, 10}, 0, two_ten, 2, 0, 0, 0, 0, 0, 0};

    asn1cnst_range_t *mantissa = _range_clone(&mantissa_default_range);
    asn1cnst_range_t *exponent = _range_clone(&exponent_default_range);
    asn1cnst_range_t *base = _range_clone(&base_default_range);
    asn1cnst_range_t *range;

#define FREE_MEB()             \
    do {                       \
        _range_free(mantissa); \
        _range_free(exponent); \
        _range_free(base);     \
    } while(0)

    (void)dbg_name;

    assert(ct->type == ACT_CT_WCOMPS);

    range = _range_new();
    range->incompatible = 1;

    for(unsigned i = 0; i < ct->el_count; i++) {
        struct asn1p_constraint_s *el = ct->elements[i];
        asn1p_value_t *value = 0;
        asn1cnst_range_t **dst_range = 0;

        switch(el->type) {
        case ACT_EL_EXT:
            /* Some components might not be defined. */
            assert(range->incompatible);
            FREE_MEB();
            return range;
        default:
            assert(range->incompatible);
            FREE_MEB();
            return range;
        case ACT_EL_VALUE:
            value = el->value;
            if(value->type != ATV_REFERENCED) {
                FREE_MEB();
                return range;
            }
            break;
        }

        if(strcmp(asn1p_ref_string(value->value.reference), "mantissa") == 0) {
            dst_range = &mantissa;
        } else if(strcmp(asn1p_ref_string(value->value.reference), "exponent")
                  == 0) {
            dst_range = &exponent;
        } else if(strcmp(asn1p_ref_string(value->value.reference), "base")
                  == 0) {
            dst_range = &base;
        } else {
            FATAL("Invalid specification \"%s\" for REAL",
                  asn1p_ref_string(value->value.reference));
            FREE_MEB();
            return range;
        }

        switch(el->el_count) {
        case 0: continue;
        case 1: break;
        default:
            FATAL("Too many constraints (%u) for \"%s\" for REAL", el->el_count,
                  asn1p_ref_string(value->value.reference));
            FREE_MEB();
            return range;
        }

        asn1cnst_range_t *tmprange = asn1constraint_compute_constraint_range(
            dbg_name, ASN_BASIC_INTEGER, el->elements[0], ACT_EL_RANGE,
            *dst_range, 0, CPR_noflags);
        assert(tmprange);
        if(tmprange->incompatible) {
            _range_free(tmprange);
            FREE_MEB();
            return range;
        }
        asn1constraint_range_free(*dst_range);
        *dst_range = tmprange;
    }

    range->incompatible = 0;

    /* X.696 #12.2 */
    if(mantissa->left.type == ARE_VALUE && mantissa->right.type == ARE_VALUE
       && mantissa->left.value >= -16777215 && mantissa->right.value <= 16777215
       && base->left.type == ARE_VALUE && base->right.type == ARE_VALUE
       && base->left.value == 2 && base->right.value == 2
       && exponent->left.type == ARE_VALUE && exponent->right.type == ARE_VALUE
936
       && exponent->left.value >= -149 && exponent->right.value <= 104) {
937 938 939 940 941 942 943 944
        range->narrowing = NARROW_FLOAT32;
    } else /* X.696 #12.3 */
    if(mantissa->left.type == ARE_VALUE && mantissa->right.type == ARE_VALUE
       && mantissa->left.value >= -9007199254740991ll
       && mantissa->right.value <= 9007199254740991ull
       && base->left.type == ARE_VALUE && base->right.type == ARE_VALUE
       && base->left.value == 2 && base->right.value == 2
       && exponent->left.type == ARE_VALUE && exponent->right.type == ARE_VALUE
945
       && exponent->left.value >= -1074 && exponent->right.value <= 971) {
946
        range->narrowing = NARROW_DOUBLE64;
947 948 949 950 951 952
    }

    FREE_MEB();
    return range;
}

Lev Walkin's avatar
Lev Walkin committed
953
asn1cnst_range_t *
Lev Walkin's avatar
Lev Walkin committed
954 955 956 957 958 959
asn1constraint_compute_constraint_range(
    const char *dbg_name, asn1p_expr_type_e expr_type,
    const asn1p_constraint_t *ct,
    enum asn1p_constraint_type_e requested_ct_type,
    const asn1cnst_range_t *minmax, int *exmet, enum cpr_flags cpr_flags) {
    asn1cnst_range_t *range;
Lev Walkin's avatar
Lev Walkin committed
960 961 962 963
	asn1cnst_range_t *tmp;
	asn1p_value_t *vmin;
	asn1p_value_t *vmax;
	int expectation_met;
964
	unsigned int i;
Lev Walkin's avatar
Lev Walkin committed
965 966 967 968 969 970 971 972
	int ret;

	if(!exmet) {
		exmet = &expectation_met;
		*exmet = 0;
	}

	/*
973 974
	 * Check if the requested constraint is theoretically compatible
	 * with the given expression type.
Lev Walkin's avatar
Lev Walkin committed
975
	 */
Lev Walkin's avatar
Lev Walkin committed
976
	if(asn1constraint_compatible(expr_type, requested_ct_type,
977
			cpr_flags & CPR_simulate_fbless_SIZE) != 1) {
Lev Walkin's avatar
Lev Walkin committed
978 979 980 981
		errno = EINVAL;
		return 0;
	}

Lev Walkin's avatar
Lev Walkin committed
982 983 984 985
	/*
	 * Check arguments' validity.
	 */
	switch(requested_ct_type) {
Lev Walkin's avatar
Lev Walkin committed
986 987 988 989 990
	case ACT_EL_RANGE:
		if(exmet == &expectation_met)
			*exmet = 1;
		break;
	case ACT_CT_FROM:
Lev Walkin's avatar
Lev Walkin committed
991 992 993 994
        if(cpr_flags & CPR_strict_OER_visibility) {
            errno = EINVAL;
            return 0;
        }
Lev Walkin's avatar
Lev Walkin committed
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
		if(!minmax) {
			minmax = asn1constraint_default_alphabet(expr_type);
			if(minmax) {
				break;
			}
		}
		/* Fall through */
	case ACT_CT_SIZE:
		if(!minmax) {
			static asn1cnst_range_t mm;
			mm.left.type = ARE_VALUE;
			mm.left.value = 0;
			mm.right.type = ARE_MAX;
			minmax = &mm;
		}
		break;
	default:
		errno = EINVAL;
		return 0;
	}

	if(minmax) {
		range = _range_clone(minmax);
	} else {
		range = _range_new();
	}

1022 1023
	/*
	 * X.691, #9.3.6
Lev Walkin's avatar
Lev Walkin committed
1024
	 * Constraints on restricted character string types
1025 1026 1027 1028 1029
	 * which are not known-multiplier are not PER-visible.
	 */
	if((expr_type & ASN_STRING_NKM_MASK))
		range->not_PER_visible = 1;

Lev Walkin's avatar
Lev Walkin committed
1030 1031 1032 1033 1034 1035 1036 1037 1038
    if(!ct
       || (range->not_PER_visible && (cpr_flags & CPR_strict_PER_visibility)))
        return range;

    /*
     * X.696 (08/2015), #8.2.2
     * SIZE constraints on restricted character string types
     * which are not known-multiplier are not OER-visible.
     */
Lev Walkin's avatar
Lev Walkin committed
1039
	if(requested_ct_type == ACT_CT_SIZE && (expr_type & ASN_STRING_NKM_MASK))
Lev Walkin's avatar
Lev Walkin committed
1040 1041 1042
		range->not_OER_visible = 1;

    if(!ct
Lev Walkin's avatar
Lev Walkin committed
1043
       || (range->not_OER_visible && (cpr_flags & CPR_strict_OER_visibility))) {
Lev Walkin's avatar
Lev Walkin committed
1044
        return range;
Lev Walkin's avatar
Lev Walkin committed
1045
    }
Lev Walkin's avatar
Lev Walkin committed
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059

	switch(ct->type) {
	case ACT_EL_VALUE:
		vmin = vmax = ct->value;
		break;
	case ACT_EL_RANGE:
	case ACT_EL_LLRANGE:
	case ACT_EL_RLRANGE:
	case ACT_EL_ULRANGE:
		vmin = ct->range_start;
		vmax = ct->range_stop;
		break;
	case ACT_EL_EXT:
		if(!*exmet) {
Lev Walkin's avatar
Lev Walkin committed
1060 1061
			range->extensible = 1;
			range->not_OER_visible = 1;
Lev Walkin's avatar
Lev Walkin committed
1062
		} else {
1063 1064 1065
			_range_free(range);
			errno = ERANGE;
			range = 0;
Lev Walkin's avatar
Lev Walkin committed
1066 1067 1068 1069
		}
		return range;
	case ACT_CT_SIZE:
	case ACT_CT_FROM:
Lev Walkin's avatar
Lev Walkin committed
1070 1071 1072 1073
		if(requested_ct_type == ct->type) {
			/*
			 * Specifically requested to process SIZE() or FROM() constraint.
			 */
Lev Walkin's avatar
Lev Walkin committed
1074 1075
			*exmet = 1;
		} else {
1076
			range->incompatible = 1;
Lev Walkin's avatar
Lev Walkin committed
1077 1078 1079
			return range;
		}
		assert(ct->el_count == 1);
Lev Walkin's avatar
Lev Walkin committed
1080 1081 1082
		tmp = asn1constraint_compute_constraint_range(
			dbg_name, expr_type, ct->elements[0], requested_ct_type, minmax,
			exmet, cpr_flags);
1083 1084 1085 1086 1087 1088
		if(tmp) {
			_range_free(range);
		} else {
			if(errno == ERANGE) {
				range->empty_constraint = 1;
				range->extensible = 1;
Lev Walkin's avatar
Lev Walkin committed
1089 1090
				if(range->extensible)
					range->not_OER_visible = 1;
1091 1092 1093 1094 1095 1096
				tmp = range;
			} else {
				_range_free(range);
			}
		}
		return tmp;
Lev Walkin's avatar
Lev Walkin committed
1097 1098 1099 1100 1101
	case ACT_CA_SET:	/* (10..20)(15..17) */
	case ACT_CA_INT:	/* SIZE(1..2) ^ FROM("ABCD") */

		/* AND constraints, one after another. */
		for(i = 0; i < ct->el_count; i++) {
Lev Walkin's avatar
Lev Walkin committed
1102
			tmp = asn1constraint_compute_constraint_range(dbg_name, expr_type,
Lev Walkin's avatar
Lev Walkin committed
1103
				ct->elements[i], requested_ct_type,
1104
				ct->type==ACT_CA_SET?range:minmax, exmet,
1105
				cpr_flags);
Lev Walkin's avatar
Lev Walkin committed
1106
			if(!tmp) {
1107
				if(errno == ERANGE) {
Lev Walkin's avatar
Lev Walkin committed
1108 1109 1110
					range->extensible = 1;
					if(range->extensible)
						range->not_OER_visible = 1;
1111 1112 1113 1114 1115
					continue;
				} else {
					_range_free(range);
					return NULL;
				}
Lev Walkin's avatar
Lev Walkin committed
1116 1117
			}

1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
			if(tmp->incompatible) {
				/*
				 * Ignore constraints
				 * incompatible with arguments:
				 * 	SIZE(1..2) ^ FROM("ABCD")
				 * either SIZE or FROM will be ignored.
				 */
				_range_free(tmp);
				continue;
			}

Lev Walkin's avatar
Lev Walkin committed
1129 1130 1131 1132 1133 1134 1135 1136 1137
			if(tmp->not_OER_visible
			&& (cpr_flags & CPR_strict_OER_visibility)) {
                /*
                 * Ignore not OER-visible
                 */
                _range_free(tmp);
				continue;
			}

1138 1139
			if(tmp->not_PER_visible
			&& (cpr_flags & CPR_strict_PER_visibility)) {
Lev Walkin's avatar
Lev Walkin committed
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
				if(ct->type == ACT_CA_SET) {
					/*
					 * X.691, #9.3.18:
					 * Ignore this separate component.
					 */
				} else {
					/*
					 * X.691, #9.3.19:
					 * Ignore not PER-visible INTERSECTION
					 */
				}
				_range_free(tmp);
				continue;
			}

			ret = _range_intersection(range, tmp,
Lev Walkin's avatar
Lev Walkin committed
1156
				ct->type == ACT_CA_SET, cpr_flags & CPR_strict_OER_visibility);
Lev Walkin's avatar
Lev Walkin committed
1157 1158 1159 1160 1161 1162
			_range_free(tmp);
			if(ret) {
				_range_free(range);
				errno = EPERM;
				return NULL;
			}
Lev Walkin's avatar
Lev Walkin committed
1163

Lev Walkin's avatar
Lev Walkin committed
1164 1165 1166 1167 1168 1169 1170
			_range_canonicalize(range);
		}

		return range;
	case ACT_CA_CSV:	/* SIZE(1..2, 3..4) */
	case ACT_CA_UNI:	/* SIZE(1..2) | FROM("ABCD") */

1171 1172 1173 1174
		/*
		 * Grab the first valid constraint.
		 */
		tmp = 0;
Lev Walkin's avatar
Lev Walkin committed
1175
		for(i = 0; i < ct->el_count; i++) {
Lev Walkin's avatar
Lev Walkin committed
1176
			tmp = asn1constraint_compute_constraint_range(dbg_name, expr_type,
Lev Walkin's avatar
Lev Walkin committed
1177
				ct->elements[i], requested_ct_type, minmax, exmet,
1178
				cpr_flags);
Lev Walkin's avatar
Lev Walkin committed
1179
			if(!tmp) {
1180 1181
				if(errno == ERANGE) {
					range->extensible = 1;
Lev Walkin's avatar
Lev Walkin committed
1182
					range->not_OER_visible = 1;
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
					continue;
				} else {
					_range_free(range);
					return NULL;
				}
			}
			if(tmp->incompatible) {
				_range_free(tmp);
				tmp = 0;
			}
			break;
		}
		if(tmp) {
			tmp->extensible |= range->extensible;
Lev Walkin's avatar
Lev Walkin committed
1197
			tmp->not_OER_visible |= range->not_OER_visible;
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
			tmp->empty_constraint |= range->empty_constraint;
			_range_free(range);
			range = tmp;
		} else {
			range->incompatible = 1;
			return range;
		}

		/*
		 * Merge with the rest of them.
		 * Canonicalizator will do the union magic.
		 */
		for(; i < ct->el_count; i++) {
Lev Walkin's avatar
Lev Walkin committed
1211
			tmp = asn1constraint_compute_constraint_range(dbg_name, expr_type,
Lev Walkin's avatar
Lev Walkin committed
1212
				ct->elements[i], requested_ct_type, minmax, exmet,
1213
				cpr_flags);
1214 1215 1216
			if(!tmp) {
				if(errno == ERANGE) {
					range->extensible = 1;
Lev Walkin's avatar
Lev Walkin committed
1217
					range->not_OER_visible = 1;
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
					continue;
				} else {
					_range_free(range);
					return NULL;
				}
			}

			if(tmp->incompatible) {
				_range_free(tmp);
				_range_canonicalize(range);
				range->incompatible = 1;
				return range;
Lev Walkin's avatar
Lev Walkin committed
1230 1231 1232
			}

			if(tmp->empty_constraint) {
1233 1234 1235 1236
				/*
				 * Ignore empty constraints in OR logic.
				 */
				range->extensible |= tmp->extensible;
Lev Walkin's avatar
Lev Walkin committed
1237
				range->not_OER_visible |= tmp->not_OER_visible;
Lev Walkin's avatar
Lev Walkin committed
1238 1239 1240 1241 1242 1243 1244 1245 1246
				_range_free(tmp);
				continue;
			}

			_range_merge_in(range, tmp);
		}

		_range_canonicalize(range);

Lev Walkin's avatar
Lev Walkin committed
1247
        if(requested_ct_type == ACT_CT_FROM) {
Lev Walkin's avatar
Lev Walkin committed
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
            /*
             * X.696 permitted alphabet constraints are not OER-visible.
             */
            range->not_OER_visible = 1;
            if(range->extensible) {
                /*
                 * X.691, #9.3.10:
                 * Extensible permitted alphabet constraints
                 * are not PER-visible.
                 */
                range->not_PER_visible = 1;
            }
        }
1261

1262 1263
		if(range->not_PER_visible
		&& (cpr_flags & CPR_strict_PER_visibility)) {
Lev Walkin's avatar
Lev Walkin committed
1264 1265 1266
			/*
			 * X.691, #9.3.19:
			 * If not PER-visible constraint is part of UNION,
1267
			 * the whole resulting constraint is not PER-visible.
Lev Walkin's avatar
Lev Walkin committed
1268 1269 1270 1271 1272 1273
			 */
			_range_free(range);
			if(minmax)
				range = _range_clone(minmax);
			else
				range = _range_new();
1274 1275
			range->not_PER_visible = 1;
			range->incompatible = 1;
Lev Walkin's avatar
Lev Walkin committed
1276 1277 1278 1279 1280
		}

		return range;
	case ACT_CA_EXC:	/* FROM("ABCD") EXCEPT FROM("AB") */
		/*
1281
		 * X.691 (PER), #9.3.19:
Lev Walkin's avatar
Lev Walkin committed
1282
		 * EXCEPT and the following value set is completely ignored.
1283 1284
		 * X.696 (OER), #8.2.6:
		 * EXCEPT keyword and the following value set is completely ignored.
Lev Walkin's avatar
Lev Walkin committed
1285 1286 1287
		 */
		assert(ct->el_count >= 1);
		_range_free(range);
Lev Walkin's avatar
Lev Walkin committed
1288
		range = asn1constraint_compute_constraint_range(dbg_name, expr_type,
Lev Walkin's avatar
Lev Walkin committed
1289
			ct->elements[0], requested_ct_type, minmax, exmet, cpr_flags);
Lev Walkin's avatar
Lev Walkin committed
1290
		return range;
1291 1292 1293 1294 1295 1296
	case ACT_CT_WCOMPS:
        if(expr_type == ASN_BASIC_REAL) {
            return asn1f_real_range_from_WCOMPS(dbg_name, ct);
        }
		range->incompatible = 1;
		return range;
Lev Walkin's avatar
Lev Walkin committed
1297
	default:
1298
		range->incompatible = 1;
Lev Walkin's avatar
Lev Walkin committed
1299 1300 1301 1302 1303 1304 1305
		return range;
	}

	if(!*exmet) {
		/*
		 * Expectation is not met. Return the default range.
		 */
1306
		range->incompatible = 1;
Lev Walkin's avatar
Lev Walkin committed
1307 1308 1309
		return range;
	}

1310 1311 1312 1313 1314 1315 1316
    if(expr_type == ASN_BASIC_REAL
       && (vmin->type == ATV_REAL || vmax->type == ATV_REAL)) {
        range->incompatible = 1;
        return range;
    }

    _range_free(range);
Lev Walkin's avatar
Lev Walkin committed
1317 1318
	range = _range_new();

1319 1320
    enum range_fill_result rfr;
	rfr = _range_fill(vmin, minmax, &range->left,
Lev Walkin's avatar
Lev Walkin committed
1321
				range, requested_ct_type, ct->_lineno);
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
    if(rfr == RFR_OK) {
        rfr = _range_fill(vmax, minmax, &range->right,
                    range, requested_ct_type, ct->_lineno);
    }
    switch(rfr) {
    case RFR_OK:
        break;
    case RFR_FAIL:
        _range_free(range);
        errno = EPERM;
        return NULL;
    case RFR_INCOMPATIBLE:
		range->incompatible = 1;
		return range;
Lev Walkin's avatar
Lev Walkin committed
1336 1337 1338 1339 1340 1341 1342 1343
	}

	if(minmax) {
		asn1cnst_range_t *clone;

		clone = _range_clone(minmax);

		/* Constrain parent type with given data. */
Lev Walkin's avatar
Lev Walkin committed
1344 1345
		ret = _range_intersection(clone, range, 1,
		                          cpr_flags & CPR_strict_OER_visibility);
Lev Walkin's avatar
Lev Walkin committed
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362
		_range_free(range);
		if(ret) {
			_range_free(clone);
			errno = EPERM;
			return NULL;
		}
		range = clone;
	}

	/*
	 * Recompute elements's min/max, remove duplicates, etc.
	 */
	_range_canonicalize(range);

	return range;
}

1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
#ifdef UNIT_TEST
int main() {
    asn1cnst_range_t *ra = _range_new();
    asn1cnst_range_t *rb = _range_new();

    fprintf(stderr, "Testing (MIN..20) x (10..15) => (MIN..9,10..15,16..20)\n");

    /* (MIN..20) */
    ra->left.type = ARE_MIN;
    ra->right.type = ARE_VALUE; ra->right.value = 20;

    /* (10..15) */
    rb->left.type = ARE_VALUE; rb->left.value = 10;
    rb->right.type = ARE_VALUE; rb->right.value = 15;

    /*
     * (MIN..20) x (10..15) = (MIN..9,10..15,16..20)
     */
    asn1cnst_range_t *r = _range_split(ra, rb);
    assert(r);
    assert(r->left.type == ARE_MIN);
    assert(r->right.type == ARE_MAX);

    assert(r->el_count == 3);
    assert(r->elements[0]->elements == NULL);
    assert(r->elements[1]->elements == NULL);
    assert(r->elements[2]->elements == NULL);

    /* (MIN..9) */
1392 1393
    fprintf(stderr, "[0].left = %s\n", _edge_string(&r->elements[0]->left));
    fprintf(stderr, "[0].right = %s\n", _edge_string(&r->elements[0]->right));
1394 1395 1396 1397 1398
    assert(r->elements[0]->left.type == ARE_MIN);
    assert(r->elements[0]->right.type == ARE_VALUE);
    assert(r->elements[0]->right.value == 9);

    /* (10..15) */
1399 1400
    fprintf(stderr, "[1].left = %s\n", _edge_string(&r->elements[1]->left));
    fprintf(stderr, "[1].right = %s\n", _edge_string(&r->elements[1]->right));
1401 1402 1403 1404 1405 1406
    assert(r->elements[1]->left.type == ARE_VALUE);
    assert(r->elements[1]->left.value == 10);
    assert(r->elements[1]->right.type == ARE_VALUE);
    assert(r->elements[1]->right.value == 15);

    /* (16..20) */
1407 1408
    fprintf(stderr, "[2].left = %s\n", _edge_string(&r->elements[2]->left));
    fprintf(stderr, "[2].right = %s\n", _edge_string(&r->elements[2]->right));
1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437
    assert(r->elements[2]->left.type == ARE_VALUE);
    assert(r->elements[2]->left.value == 16);
    assert(r->elements[2]->right.type == ARE_VALUE);
    assert(r->elements[2]->right.value == 20);

    _range_free(r);



    fprintf(stderr, "Testing (MIN..20) x (<min>..15) => (<min>..15,16..20)\n");

    /* (MIN..20) */
    ra->left.type = ARE_MIN;
    ra->right.type = ARE_VALUE; ra->right.value = 20;

    /* (<INTMAX_MIN>..15) */
    rb->left.type = ARE_VALUE; rb->left.value = INTMAX_MIN;
    rb->right.type = ARE_VALUE; rb->right.value = 15;

    r = _range_split(ra, rb);
    assert(r);
    assert(r->left.type == ARE_MIN);
    assert(r->right.type == ARE_MAX);

    assert(r->el_count == 2);
    assert(r->elements[0]->elements == NULL);
    assert(r->elements[1]->elements == NULL);

    /* (<min>..16) */
1438 1439
    fprintf(stderr, "[0].left = %s\n", _edge_string(&r->elements[0]->left));
    fprintf(stderr, "[0].right = %s\n", _edge_string(&r->elements[0]->right));
1440 1441 1442 1443 1444 1445
    assert(r->elements[0]->left.type == ARE_VALUE);
    assert(r->elements[0]->left.value == INTMAX_MIN);
    assert(r->elements[0]->right.type == ARE_VALUE);
    assert(r->elements[0]->right.value == 15);

    /* (16..20) */
1446 1447
    fprintf(stderr, "[1].left = %s\n", _edge_string(&r->elements[1]->left));
    fprintf(stderr, "[1].right = %s\n", _edge_string(&r->elements[1]->right));
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457
    assert(r->elements[1]->left.type == ARE_VALUE);
    assert(r->elements[1]->left.value == 16);
    assert(r->elements[1]->right.type == ARE_VALUE);
    assert(r->elements[1]->right.value == 20);

    _range_free(r);

    return 0;
}
#endif