blob: 23110a234bfaf55053cdf70c9efa067825e1171b [file] [log] [blame]
vlm8a09e0f2005-02-25 14:20:30 +00001#include "asn1fix_internal.h"
2#include "asn1fix_constraint.h"
3#include "asn1fix_crange.h"
vlmb5be8c32004-08-18 05:42:05 +00004
5#undef FATAL
6#define FATAL(fmt, args...) do { \
7 fprintf(stderr, "FATAL: "); \
8 fprintf(stderr, fmt, ##args); \
9 fprintf(stderr, "\n"); \
10 } while(0)
11
12void
13asn1constraint_range_free(asn1cnst_range_t *cr) {
14 if(cr) {
15 int i;
16 if(cr->elements) {
17 for(i = 0; i < cr->el_count; i++)
18 asn1constraint_range_free(cr->elements[i]);
19 free(cr->elements);
20 }
21 free(cr);
22 }
23}
24#define _range_free(foo) asn1constraint_range_free(foo)
25
26static asn1cnst_range_t *_range_new() {
27 asn1cnst_range_t *r;
28 r = calloc(1, sizeof(*r));
29 if(r) {
30 r->left.type = ARE_MIN;
31 r->right.type = ARE_MAX;
32 }
33 return r;
34}
35
36static void _range_remove_element(asn1cnst_range_t *range, int idx) {
37 assert(idx >= 0 && idx < range->el_count);
38
39 assert(!range->elements[idx]->elements);
40
41 _range_free(range->elements[idx]);
42
43 memmove(&range->elements[idx],
44 &range->elements[idx + 1],
45 (range->el_count - idx - 1)
46 * sizeof(range->elements[0])
47 );
48 range->el_count--;
49 range->elements[range->el_count] = 0; /* JIC */
50
51 if(range->el_count == 0) {
52 range->el_size = 0;
53 free(range->elements);
54 range->elements = 0;
55 }
56}
57
58static int _range_insert(asn1cnst_range_t *into, asn1cnst_range_t *cr) {
59
60 assert(!cr->elements);
61
62 if(into->el_count == into->el_size) {
63 void *p;
64 int n = into->el_size?(into->el_size << 1):4;
65 p = realloc(into->elements, n * sizeof(into->elements[0]));
66 if(p) {
67 into->el_size = n;
68 into->elements = p;
69 } else {
70 assert(p);
71 return -1;
72 }
73 }
74
75 into->elements[into->el_count++] = cr;
76 return 0;
77}
78
79static asn1cnst_range_t *_range_clone(const asn1cnst_range_t *range) {
80 asn1cnst_range_t *clone;
81 int i;
82
83 clone = _range_new();
84 if(!clone) return NULL;
85
86 *clone = *range;
87 clone->elements = 0;
88 clone->el_count = 0;
89 clone->el_size = 0;
90
91 for(i = 0; i < range->el_count; i++) {
92 asn1cnst_range_t *r = _range_clone(range->elements[i]);
93 if(!r || _range_insert(clone, r)) {
94 _range_free(clone);
95 _range_free(r);
96 return NULL;
97 }
98 }
99
100 return clone;
101}
102
103static int
104_edge_compare(const asn1cnst_edge_t *el, const asn1cnst_edge_t *er) {
105
106 switch(el->type) {
107 case ARE_MIN:
108 switch(er->type) {
109 case ARE_MIN: return 0;
110 case ARE_MAX: return -1;
111 case ARE_VALUE: return -1;
112 }
113 break;
114 case ARE_MAX:
115 switch(er->type) {
116 case ARE_MIN: return 1;
117 case ARE_MAX: return 0;
118 case ARE_VALUE: return 1;
119 }
120 break;
121 case ARE_VALUE:
122 switch(er->type) {
123 case ARE_MIN: return 1;
124 case ARE_MAX: return -1;
125 case ARE_VALUE:
126 if(el->value < er->value)
127 return -1;
128 if(el->value > er->value)
129 return 1;
130 return 0;
131 }
132 break;
133 }
134
135 return 0;
136}
137
138static int
139_range_compare(const void *a, const void *b) {
140 const asn1cnst_range_t *ra = *(const asn1cnst_range_t * const *)a;
141 const asn1cnst_range_t *rb = *(const asn1cnst_range_t * const *)b;
142 int ret;
143
144 ret = _edge_compare(&ra->left, &rb->left);
145 if(!ret) {
146 ret = _edge_compare(&ra->right, &rb->right);
147 }
148
149 return ret;
150}
151
152static char *
153_edge_value(const asn1cnst_edge_t *edge) {
154 static char buf[128];
155 *buf = '\0';
156 switch(edge->type) {
157 case ARE_MIN: strcpy(buf, "MIN"); break;
158 case ARE_MAX: strcpy(buf, "MAX"); break;
159 case ARE_VALUE:
vlm47ae1582004-09-24 21:01:43 +0000160 snprintf(buf, sizeof(buf), "%" PRIdASN, edge->value);
vlmb5be8c32004-08-18 05:42:05 +0000161 }
162 return buf;
163}
164
165static void
166_range_print(const asn1cnst_range_t *range) {
167
168 if(_edge_compare(&range->left, &range->right)) {
169 printf("(%s.", _edge_value(&range->left));
vlmee8b06f2004-08-25 02:05:28 +0000170 printf(".%s", _edge_value(&range->right));
vlmb5be8c32004-08-18 05:42:05 +0000171 } else {
vlmee8b06f2004-08-25 02:05:28 +0000172 printf("(%s", _edge_value(&range->left));
vlmb5be8c32004-08-18 05:42:05 +0000173 }
174
vlmee8b06f2004-08-25 02:05:28 +0000175 if(range->extensible) {
176 printf(",...)");
177 } else {
178 printf(")");
179 }
180
181 if(range->incompatible) printf("/I");
182 if(range->not_PER_visible) printf("/!V");
183
vlmb5be8c32004-08-18 05:42:05 +0000184 if(range->el_count) {
185 int i;
186 printf("-=>");
187 for(i = 0; i < range->el_count; i++)
188 _range_print(range->elements[i]);
189 }
190
191}
192
193static int
194_edge_is_within(const asn1cnst_range_t *range, const asn1cnst_edge_t *edge) {
195 int i;
196
197 for(i = -1; i < range->el_count; i++) {
198 const asn1cnst_range_t *r;
199 if(i == -1) {
200 if(range->el_count) continue;
201 r = range;
202 } else {
203 r = range->elements[i];
204 }
205 if(_edge_compare(&r->left, edge) <= 0
206 && _edge_compare(&r->right, edge) >= 0)
207 return 1;
208 }
209
210 return 0;
211}
212
213static int
214_check_edges_within(const asn1cnst_range_t *range, const asn1cnst_range_t *r) {
215
216 if(!_edge_is_within(range, &r->left)) {
217 FATAL("Constraint value %s at line %d "
218 "is not within "
219 "a parent constraint range",
220 _edge_value(&r->left),
221 r->left.lineno
222 );
223 return -1;
224 }
225
226 if(!_edge_is_within(range, &r->right)) {
227 FATAL("Constraint value %s at line %d "
228 "is not within "
229 "a parent constraint range",
230 _edge_value(&r->right),
231 r->right.lineno
232 );
233 return -1;
234 }
235
236 return 0;
237}
238
239static int _range_merge_in(asn1cnst_range_t *into, asn1cnst_range_t *cr) {
240 asn1cnst_range_t *r;
241 int prev_count = into->el_count;
242 int i;
243
vlmee8b06f2004-08-25 02:05:28 +0000244 into->not_PER_visible |= cr->not_PER_visible;
245 into->extensible |= cr->extensible;
246
vlmb5be8c32004-08-18 05:42:05 +0000247 /*
248 * Add the element OR all its children "into".
249 */
250 for(i = -1; i < cr->el_count; i++) {
251
252 if(i == -1) {
253 if(cr->el_count) continue;
254 r = cr;
255 } else {
256 r = cr->elements[i];
257 }
258
259 if(_range_insert(into, r)) {
260 into->el_count = prev_count; /* Undo */
261 return -1;
262 }
263 }
264
265 if(cr->el_count) {
266 cr->el_count = 0;
267 _range_free(cr);
268 } else {
269 /* This range is linked into "into". */
270 }
271
272 return 0;
273}
274
275static int _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) {
276 unsigned char *p, *pend;
277
278 edge->lineno = lineno;
279
280 switch(val->type) {
281 case ATV_INTEGER:
282 if(type != ACT_EL_RANGE && type != ACT_CT_SIZE) {
vlm47ae1582004-09-24 21:01:43 +0000283 FATAL("Integer %" PRIdASN " value invalid "
vlmb5be8c32004-08-18 05:42:05 +0000284 "for %s constraint at line %d",
vlm47ae1582004-09-24 21:01:43 +0000285 val->value.v_integer,
vlmb5be8c32004-08-18 05:42:05 +0000286 asn1p_constraint_type2str(type), lineno);
287 return -1;
288 }
289 edge->type = ARE_VALUE;
290 edge->value = val->value.v_integer;
291 return 0;
292 case ATV_MIN:
293 if(type != ACT_EL_RANGE && type != ACT_CT_SIZE) {
294 FATAL("MIN invalid for %s constraint at line %d",
295 asn1p_constraint_type2str(type), lineno);
296 return -1;
297 }
298 edge->type = ARE_MIN;
299 if(minmax) *edge = minmax->left;
300 edge->lineno = lineno; /* Restore lineno */
301 return 0;
302 case ATV_MAX:
303 if(type != ACT_EL_RANGE && type != ACT_CT_SIZE) {
304 FATAL("MAX invalid for %s constraint at line %d",
305 asn1p_constraint_type2str(type), lineno);
306 return -1;
307 }
308 edge->type = ARE_MAX;
309 if(minmax) *edge = minmax->right;
310 edge->lineno = lineno; /* Restore lineno */
311 return 0;
312 case ATV_FALSE:
313 case ATV_TRUE:
314 if(type != ACT_EL_RANGE) {
315 FATAL("%s is invalid for %s constraint at line %d",
316 val->type==ATV_TRUE?"TRUE":"FALSE",
317 asn1p_constraint_type2str(type),
318 lineno);
319 return -1;
320 }
321 edge->type = ARE_VALUE;
322 edge->value = (val->type==ATV_TRUE);
323 return 0;
vlme1e6ed82005-03-24 14:26:38 +0000324 case ATV_TUPLE:
325 case ATV_QUADRUPLE:
326 edge->type = ARE_VALUE;
327 edge->value = val->value.v_integer;
328 return 0;
vlmb5be8c32004-08-18 05:42:05 +0000329 case ATV_STRING:
330 if(type != ACT_CT_FROM)
331 return 0;
332 break;
vlmb6fd3b22004-08-25 00:42:25 +0000333 case ATV_REFERENCED:
vlm57461782004-09-15 11:45:44 +0000334 FATAL("Unresolved constraint element \"%s\" at line %d",
vlmb6fd3b22004-08-25 00:42:25 +0000335 asn1f_printable_reference(val->value.reference),
336 lineno);
337 return -1;
vlmb5be8c32004-08-18 05:42:05 +0000338 default:
339 FATAL("Unrecognized constraint element at line %d",
340 lineno);
341 return -1;
342 }
343
344 assert(val->type == ATV_STRING);
345
346 p = val->value.string.buf;
347 pend = p + val->value.string.size;
348 if(p == pend) return 0;
349
350 edge->type = ARE_VALUE;
351 if(val->value.string.size == 1) {
352 edge->value = *p;
353 } else {
354 /*
355 * Else this is a set:
356 * (FROM("abcdef"))
357 * However, (FROM("abc".."def")) is forbidden.
358 * See also 47.4.4.
359 */
vlmd5b3bf32004-09-29 13:17:17 +0000360 asn1c_integer_t vmin, vmax;
vlmb5be8c32004-08-18 05:42:05 +0000361 vmin = vmax = *p;
362 for(; p < pend; p++) {
363 asn1cnst_range_t *nr = _range_new();
364 int ret;
365 assert(nr);
366
367 if(*p < vmin) vmin = *p;
368 if(*p > vmax) vmax = *p;
369
370 ret = _range_insert(range, nr);
371 assert(ret == 0);
372
373 nr->left.type = ARE_VALUE;
374 nr->left.value = *p;
375 nr->left.lineno = lineno;
376 nr->right = nr->left;
377 }
378 edge->value = (edge == &range->right) ? vmin : vmax;
379 }
380
381 return 0;
382}
383
384/*
385 * Check if ranges contain common elements.
386 */
387static int
388_range_overlap(const asn1cnst_range_t *ra, const asn1cnst_range_t *rb) {
389 int lr, rl;
390 const asn1cnst_edge_t *ra_l = &ra->left;
391 const asn1cnst_edge_t *ra_r = &ra->right;
392 const asn1cnst_edge_t *rb_l = &rb->left;
393 const asn1cnst_edge_t *rb_r = &rb->right;
394
395 assert(_edge_compare(ra_l, ra_r) <= 0);
396 assert(_edge_compare(rb_l, rb_r) <= 0);
397
398 lr = _edge_compare(ra_l, rb_r);
399 rl = _edge_compare(ra_r, rb_l);
400
401 /*
402 * L: |---|
403 * R: |---|
404 */
405 if(lr > 0) return 0;
406
407 /*
408 * L: |---|
409 * R: |---|
410 */
411 if(rl < 0) return 0;
412
413 return 1;
414}
415
416/*
417 * (MIN..20) x (10..15) = (MIN..9,10..15,16..20)
418 */
419static asn1cnst_range_t *
420_range_split(asn1cnst_range_t *ra, const asn1cnst_range_t *rb) {
421 asn1cnst_range_t *range, *nr;
422 int ll, rr;
423
424 assert(ra);
425 assert(rb);
426 assert(!ra->el_count);
427 assert(!rb->el_count);
428
429 if(!_range_overlap(ra, rb)) {
430 errno = 0;
431 return 0;
432 }
433
434 ll = _edge_compare(&ra->left, &rb->left);
435 rr = _edge_compare(&ra->right, &rb->right);
436
437 /*
438 * L: |---|
439 * R: |-------|
440 */
441 if(ll >= 0 && rr <= 0) {
442 errno = 0;
443 return 0;
444 }
445
446 range = _range_new();
447 assert(range);
448
449 nr = _range_new();
450 assert(nr);
451
452 /*
453 * L: |---...
454 * R: |--..
455 */
456 if(ll < 0) {
457 nr->left = ra->left;
458 nr->right = rb->left;
459 if(nr->right.type == ARE_VALUE)
460 nr->right.value--;
461 _range_insert(range, nr);
462 nr = _range_new();
463 assert(nr);
464 }
465
466 /*
467 * L: ...---|
468 * R: ..--|
469 */
470 if(rr > 0) {
471 nr->left = rb->right;
472 nr->right = ra->right;
473 if(nr->left.type == ARE_VALUE)
474 nr->left.value++;
475 _range_insert(range, nr);
476 nr = _range_new();
477 assert(nr);
478 }
479
480 /*
481 * L: |---|
482 * R: |-----|
483 */
484 nr->left = ra->left;
485 nr->right = ra->right;
486 if(_edge_compare(&ra->left, &rb->left) < 0)
487 nr->left = rb->left;
488 if(_edge_compare(&ra->right, &rb->right) > 0)
489 nr->right = rb->right;
490
491 _range_insert(range, nr);
492
493 return range;
494}
495
496static int
497_range_intersection(asn1cnst_range_t *range, const asn1cnst_range_t *with, int strict_edge_check) {
498 int ret;
499 int i, j;
500
vlmee8b06f2004-08-25 02:05:28 +0000501 assert(!range->incompatible);
502
503 /* Propagate errors */
504 range->extensible |= with->extensible;
505 range->not_PER_visible |= with->not_PER_visible;
506 range->empty_constraint |= with->empty_constraint;
507
508 if(range->empty_constraint) {
509 /* No use in intersecting empty constraints */
vlmb5be8c32004-08-18 05:42:05 +0000510 return 0;
511 }
512
513 /*
514 * This is an AND operation.
515 */
516
517 /* If this is the only element, insert it into itself as a child */
518 if(range->el_count == 0) {
519 asn1cnst_range_t *r = _range_new();
520 r->left = range->left;
521 r->right = range->right;
522 _range_insert(range, r);
523 assert(range->el_count == 1);
524 }
525
526 /*
527 * Make sure we're dealing with sane data.
528 * G.4.2.3
529 */
530 if(strict_edge_check) {
531 for(j = -1; j < with->el_count; j++) {
532
533 if(j == -1) {
534 if(with->el_count) continue;
535 if(_check_edges_within(range, with))
536 return -1;
537 } else {
538 if(_check_edges_within(range,
539 with->elements[j]))
540 return -1;
541 }
542 }
543 }
544
545 /*
546 * Split range in pieces.
547 */
548
549 for(i = 0; i < range->el_count; i++) {
550 for(j = -1; j < with->el_count; j++) {
551 const asn1cnst_range_t *wel;
552 asn1cnst_range_t *r;
553
554 if(j == -1) {
555 if(with->el_count) continue;
556 wel = with;
557 } else {
558 wel = with->elements[j];
559 }
560
561 r = _range_split(range->elements[i], wel);
562 if(r) {
563 int ec;
564 /* Substitute the current element with a split */
565 _range_remove_element(range, i);
566 assert(r->el_count);
567 for(ec = 0; ec < r->el_count; ec++) {
568 ret = _range_insert(range, r->elements[ec]);
569 assert(ret == 0);
570 }
571 r->el_count = 0;
572 _range_free(r);
573 i--;
574 break; /* Try again from this point */
575 }
576 }
577 }
578
579 assert(range->el_count);
580
581 /*
582 * Remove pieces which aren't AND-compatible "with" range.
583 */
584
585 for(i = 0; i < range->el_count; i++) {
586 for(j = -1; j < with->el_count; j++) {
587 const asn1cnst_range_t *wel;
588
589 if(j == -1) {
590 if(with->el_count) continue;
591 wel = with;
592 } else {
593 wel = with->elements[j];
594 }
595
596 if(_range_overlap(range->elements[i], wel))
597 break;
598 }
599 if(j == with->el_count) {
600 _range_remove_element(range, i);
601 i--;
602 }
603 }
604
605 if(range->el_count == 0)
606 range->empty_constraint = 1;
607
608 return 0;
609}
610
611static int
612_range_union(asn1cnst_range_t *range) {
613 int i;
614
615 qsort(range->elements, range->el_count, sizeof(range->elements[0]),
616 _range_compare);
617
618 /*
619 * The range is sorted by the start values.
620 */
621 for(i = 1; i < range->el_count; i++) {
622 asn1cnst_range_t *ra = range->elements[i - 1];
623 asn1cnst_range_t *rb = range->elements[i];
624
625 if(_range_overlap(ra, rb)) {
626 if(_edge_compare(&ra->left, &rb->left) < 0)
627 rb->left = ra->left;
628
629 if(_edge_compare(&ra->right, &rb->right) > 0)
630 rb->right = ra->right;
631 } else {
632 /*
633 * Still, range may be joined: (1..4)(5..10).
634 * This logic is valid only for whole numbers
635 * (i.e., not REAL type, but REAL constraints
vlmee8b06f2004-08-25 02:05:28 +0000636 * are not PER-visible (X.691, #9.3.12).
vlmb5be8c32004-08-18 05:42:05 +0000637 */
638 if(ra->right.type == ARE_VALUE
639 && rb->left.type == ARE_VALUE
640 && (rb->left.value - ra->right.value) == 1) {
641 /* (1..10) */
642 rb->left = ra->left;
643 } else {
644 continue;
645 }
646 }
647
648 /*
649 * Squeeze the array by removing the ra.
650 */
651 _range_remove_element(range, i - 1);
652
653 i--; /* Retry from the current point */
654 }
655
656 return 0;
657}
658
659static int
660_range_canonicalize(asn1cnst_range_t *range) {
661
662 if(range->el_count == 0) {
663 /*
664 * Switch left and right edges, make them sorted.
665 * It might be a mild warning though.
666 */
667 if(_edge_compare(&range->left, &range->right) > 0) {
668 asn1cnst_edge_t tmp = range->left;
669 range->left = range->right;
670 range->right = tmp;
671 }
672
673 if(range->elements) {
674 free(range->elements);
675 range->elements = 0;
676 }
677 range->el_size = 0;
678 return 0;
679 }
680
681 /*
682 * Remove duplicates and overlaps by merging them in.
683 */
684 _range_union(range);
685
686 /* Refine the left edge of a parent */
687 range->left = range->elements[0]->left;
688
689 /* Refine the right edge of a parent */
690 range->right = range->elements[range->el_count - 1]->right;
691
692 /* Remove the child, if it's a single one */
693 if(range->el_count == 1) {
694 _range_remove_element(range, 0);
695 }
696
697 return 0;
698}
699
700asn1cnst_range_t *
vlmee8b06f2004-08-25 02:05:28 +0000701asn1constraint_compute_PER_range(asn1p_expr_type_e expr_type, const asn1p_constraint_t *ct, enum asn1p_constraint_type_e type, const asn1cnst_range_t *minmax, int *exmet, int strict_PV) {
vlmb5be8c32004-08-18 05:42:05 +0000702 asn1cnst_range_t *range;
703 asn1cnst_range_t *tmp;
704 asn1p_value_t *vmin;
705 asn1p_value_t *vmax;
706 int expectation_met;
vlm76142452004-09-05 10:36:22 +0000707 unsigned int i;
vlmb5be8c32004-08-18 05:42:05 +0000708 int ret;
vlmb5be8c32004-08-18 05:42:05 +0000709
710 if(!exmet) {
711 exmet = &expectation_met;
712 *exmet = 0;
713 }
714
715 /*
vlmee8b06f2004-08-25 02:05:28 +0000716 * Check if the requested constraint is theoretically compatible
717 * with the given expression type.
vlmb5be8c32004-08-18 05:42:05 +0000718 */
719 if(asn1constraint_compatible(expr_type, type) != 1) {
720 errno = EINVAL;
721 return 0;
722 }
723
724 /* Check arguments' validity. */
725 switch(type) {
726 case ACT_EL_RANGE:
727 if(exmet == &expectation_met)
728 *exmet = 1;
729 break;
730 case ACT_CT_FROM:
731 if(!minmax) {
732 minmax = asn1constraint_default_alphabet(expr_type);
733 if(minmax) {
734 break;
735 }
736 }
737 /* Fall through */
738 case ACT_CT_SIZE:
739 if(!minmax) {
740 static asn1cnst_range_t mm;
741 mm.left.type = ARE_VALUE;
742 mm.left.value = 0;
743 mm.right.type = ARE_MAX;
744 minmax = &mm;
745 }
746 break;
747 default:
748 errno = EINVAL;
749 return 0;
750 }
751
752 if(minmax) {
753 range = _range_clone(minmax);
754 } else {
755 range = _range_new();
756 }
757
vlmee8b06f2004-08-25 02:05:28 +0000758 /*
759 * X.691, #9.3.6
760 * Constraints on restricter character string types
761 * which are not known-multiplier are not PER-visible.
762 */
763 if((expr_type & ASN_STRING_NKM_MASK))
764 range->not_PER_visible = 1;
765
766 if(!ct || (strict_PV && range->not_PER_visible))
vlmb5be8c32004-08-18 05:42:05 +0000767 return range;
768
769 switch(ct->type) {
770 case ACT_EL_VALUE:
771 vmin = vmax = ct->value;
772 break;
773 case ACT_EL_RANGE:
774 case ACT_EL_LLRANGE:
775 case ACT_EL_RLRANGE:
776 case ACT_EL_ULRANGE:
777 vmin = ct->range_start;
778 vmax = ct->range_stop;
779 break;
780 case ACT_EL_EXT:
781 if(!*exmet) {
vlmee8b06f2004-08-25 02:05:28 +0000782 range->incompatible = 1;
vlmb5be8c32004-08-18 05:42:05 +0000783 } else {
vlmee8b06f2004-08-25 02:05:28 +0000784 _range_free(range);
785 errno = ERANGE;
786 range = 0;
vlmb5be8c32004-08-18 05:42:05 +0000787 }
788 return range;
789 case ACT_CT_SIZE:
790 case ACT_CT_FROM:
791 if(type == ct->type) {
792 *exmet = 1;
793 } else {
vlmee8b06f2004-08-25 02:05:28 +0000794 range->incompatible = 1;
vlmb5be8c32004-08-18 05:42:05 +0000795 return range;
796 }
797 assert(ct->el_count == 1);
vlmee8b06f2004-08-25 02:05:28 +0000798 tmp = asn1constraint_compute_PER_range(expr_type,
799 ct->elements[0], type, minmax, exmet, strict_PV);
800 if(tmp) {
801 _range_free(range);
802 } else {
803 if(errno == ERANGE) {
804 range->empty_constraint = 1;
805 range->extensible = 1;
806 tmp = range;
807 } else {
808 _range_free(range);
809 }
810 }
811 return tmp;
vlmb5be8c32004-08-18 05:42:05 +0000812 case ACT_CA_SET: /* (10..20)(15..17) */
813 case ACT_CA_INT: /* SIZE(1..2) ^ FROM("ABCD") */
814
815 /* AND constraints, one after another. */
816 for(i = 0; i < ct->el_count; i++) {
817 tmp = asn1constraint_compute_PER_range(expr_type,
818 ct->elements[i], type,
vlmee8b06f2004-08-25 02:05:28 +0000819 ct->type==ACT_CA_SET?range:minmax, exmet,
820 strict_PV);
vlmb5be8c32004-08-18 05:42:05 +0000821 if(!tmp) {
vlmee8b06f2004-08-25 02:05:28 +0000822 if(errno == ERANGE) {
823 continue;
824 } else {
825 _range_free(range);
826 return NULL;
827 }
vlmb5be8c32004-08-18 05:42:05 +0000828 }
829
vlmee8b06f2004-08-25 02:05:28 +0000830 if(tmp->incompatible) {
831 /*
832 * Ignore constraints
833 * incompatible with arguments:
834 * SIZE(1..2) ^ FROM("ABCD")
835 * either SIZE or FROM will be ignored.
836 */
837 _range_free(tmp);
838 continue;
839 }
840
841 if(strict_PV && tmp->not_PER_visible) {
vlmb5be8c32004-08-18 05:42:05 +0000842 if(ct->type == ACT_CA_SET) {
843 /*
844 * X.691, #9.3.18:
845 * Ignore this separate component.
846 */
847 } else {
848 /*
849 * X.691, #9.3.19:
850 * Ignore not PER-visible INTERSECTION
851 */
852 }
853 _range_free(tmp);
854 continue;
855 }
856
vlmb5be8c32004-08-18 05:42:05 +0000857 ret = _range_intersection(range, tmp,
858 ct->type == ACT_CA_SET);
859 _range_free(tmp);
860 if(ret) {
861 _range_free(range);
862 errno = EPERM;
863 return NULL;
864 }
865 _range_canonicalize(range);
866 }
867
868 return range;
869 case ACT_CA_CSV: /* SIZE(1..2, 3..4) */
870 case ACT_CA_UNI: /* SIZE(1..2) | FROM("ABCD") */
871
vlmee8b06f2004-08-25 02:05:28 +0000872 /*
873 * Grab the first valid constraint.
874 */
875 tmp = 0;
vlmb5be8c32004-08-18 05:42:05 +0000876 for(i = 0; i < ct->el_count; i++) {
877 tmp = asn1constraint_compute_PER_range(expr_type,
vlmee8b06f2004-08-25 02:05:28 +0000878 ct->elements[i], type, minmax, exmet,
879 strict_PV);
vlmb5be8c32004-08-18 05:42:05 +0000880 if(!tmp) {
vlmee8b06f2004-08-25 02:05:28 +0000881 if(errno == ERANGE) {
882 range->extensible = 1;
883 continue;
884 } else {
885 _range_free(range);
886 return NULL;
887 }
888 }
889 if(tmp->incompatible) {
890 _range_free(tmp);
891 tmp = 0;
892 }
893 break;
894 }
895 if(tmp) {
896 tmp->extensible |= range->extensible;
897 tmp->empty_constraint |= range->empty_constraint;
898 _range_free(range);
899 range = tmp;
900 } else {
901 range->incompatible = 1;
902 return range;
903 }
904
905 /*
906 * Merge with the rest of them.
907 * Canonicalizator will do the union magic.
908 */
909 for(; i < ct->el_count; i++) {
910 tmp = asn1constraint_compute_PER_range(expr_type,
911 ct->elements[i], type, minmax, exmet,
912 strict_PV);
913 if(!tmp) {
914 if(errno == ERANGE) {
915 range->extensible = 1;
916 continue;
917 } else {
918 _range_free(range);
919 return NULL;
920 }
921 }
922
923 if(tmp->incompatible) {
924 _range_free(tmp);
925 _range_canonicalize(range);
926 range->incompatible = 1;
927 return range;
vlmb5be8c32004-08-18 05:42:05 +0000928 }
929
930 if(tmp->empty_constraint) {
vlmee8b06f2004-08-25 02:05:28 +0000931 /*
932 * Ignore empty constraints in OR logic.
933 */
934 range->extensible |= tmp->extensible;
vlmb5be8c32004-08-18 05:42:05 +0000935 _range_free(tmp);
936 continue;
937 }
938
vlmb5be8c32004-08-18 05:42:05 +0000939 _range_merge_in(range, tmp);
940 }
941
942 _range_canonicalize(range);
943
vlmee8b06f2004-08-25 02:05:28 +0000944 if(range->extensible && type == ACT_CT_FROM) {
945 /*
946 * X.691, #9.3.10:
947 * Extensible permitted alphabet constraints
948 * are not PER-visible.
949 */
950 range->not_PER_visible = 1;
951 }
952
953 if(strict_PV && range->not_PER_visible) {
vlmb5be8c32004-08-18 05:42:05 +0000954 /*
955 * X.691, #9.3.19:
956 * If not PER-visible constraint is part of UNION,
vlmee8b06f2004-08-25 02:05:28 +0000957 * the whole resulting constraint is not PER-visible.
vlmb5be8c32004-08-18 05:42:05 +0000958 */
959 _range_free(range);
960 if(minmax)
961 range = _range_clone(minmax);
962 else
963 range = _range_new();
vlmee8b06f2004-08-25 02:05:28 +0000964 range->not_PER_visible = 1;
965 range->incompatible = 1;
vlmb5be8c32004-08-18 05:42:05 +0000966 }
967
968 return range;
969 case ACT_CA_EXC: /* FROM("ABCD") EXCEPT FROM("AB") */
970 /*
971 * X.691, #9.3.19:
972 * EXCEPT and the following value set is completely ignored.
973 */
974 assert(ct->el_count >= 1);
975 _range_free(range);
976 range = asn1constraint_compute_PER_range(expr_type,
vlmee8b06f2004-08-25 02:05:28 +0000977 ct->elements[0], type, minmax, exmet, strict_PV);
vlmb5be8c32004-08-18 05:42:05 +0000978 return range;
979 default:
vlmee8b06f2004-08-25 02:05:28 +0000980 range->incompatible = 1;
vlmb5be8c32004-08-18 05:42:05 +0000981 return range;
982 }
983
984
985 if(!*exmet) {
986 /*
987 * Expectation is not met. Return the default range.
988 */
vlmee8b06f2004-08-25 02:05:28 +0000989 range->incompatible = 1;
vlmb5be8c32004-08-18 05:42:05 +0000990 return range;
991 }
992
993 _range_free(range);
994 range = _range_new();
995
996 ret = _range_fill(vmin, minmax, &range->left,
997 range, type, ct->_lineno);
vlmb6fd3b22004-08-25 00:42:25 +0000998 if(!ret)
999 ret = _range_fill(vmax, minmax, &range->right,
vlmb5be8c32004-08-18 05:42:05 +00001000 range, type, ct->_lineno);
1001 if(ret) {
1002 _range_free(range);
1003 errno = EPERM;
1004 return NULL;
1005 }
1006
1007 if(minmax) {
1008 asn1cnst_range_t *clone;
1009
1010 clone = _range_clone(minmax);
1011
1012 /* Constrain parent type with given data. */
1013 ret = _range_intersection(clone, range, 1);
1014 _range_free(range);
1015 if(ret) {
1016 _range_free(clone);
1017 errno = EPERM;
1018 return NULL;
1019 }
1020 range = clone;
1021 }
1022
1023 /*
1024 * Recompute elements's min/max, remove duplicates, etc.
1025 */
1026 _range_canonicalize(range);
1027
1028 return range;
1029}
1030