blob: 5975bc1a3aa9745c17413bce972f040ef67fffc3 [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 */
vlme6c89732005-05-17 21:46:18 +0000456 while(ll < 0) {
vlmb5be8c32004-08-18 05:42:05 +0000457 nr->left = ra->left;
458 nr->right = rb->left;
vlme6c89732005-05-17 21:46:18 +0000459 if(nr->right.type == ARE_VALUE) {
460 if(nr->right.value - 1 >= nr->right.value) {
461 /* We've hit the limit here. */
462 break;
463 }
vlmb5be8c32004-08-18 05:42:05 +0000464 nr->right.value--;
vlme6c89732005-05-17 21:46:18 +0000465 }
vlmb5be8c32004-08-18 05:42:05 +0000466 _range_insert(range, nr);
467 nr = _range_new();
468 assert(nr);
vlme6c89732005-05-17 21:46:18 +0000469 break;
vlmb5be8c32004-08-18 05:42:05 +0000470 }
471
472 /*
473 * L: ...---|
474 * R: ..--|
475 */
vlme6c89732005-05-17 21:46:18 +0000476 while(rr > 0) {
vlmb5be8c32004-08-18 05:42:05 +0000477 nr->left = rb->right;
478 nr->right = ra->right;
vlme6c89732005-05-17 21:46:18 +0000479 if(nr->left.type == ARE_VALUE) {
480 if(nr->left.value + 1 <= nr->left.value) {
481 /* We've hit the limit here. */
482 break;
483 }
vlmb5be8c32004-08-18 05:42:05 +0000484 nr->left.value++;
vlme6c89732005-05-17 21:46:18 +0000485 }
vlmb5be8c32004-08-18 05:42:05 +0000486 _range_insert(range, nr);
487 nr = _range_new();
488 assert(nr);
vlme6c89732005-05-17 21:46:18 +0000489 break;
vlmb5be8c32004-08-18 05:42:05 +0000490 }
491
492 /*
493 * L: |---|
494 * R: |-----|
495 */
496 nr->left = ra->left;
497 nr->right = ra->right;
498 if(_edge_compare(&ra->left, &rb->left) < 0)
499 nr->left = rb->left;
500 if(_edge_compare(&ra->right, &rb->right) > 0)
501 nr->right = rb->right;
502
503 _range_insert(range, nr);
504
505 return range;
506}
507
508static int
509_range_intersection(asn1cnst_range_t *range, const asn1cnst_range_t *with, int strict_edge_check) {
510 int ret;
511 int i, j;
512
vlmee8b06f2004-08-25 02:05:28 +0000513 assert(!range->incompatible);
514
515 /* Propagate errors */
516 range->extensible |= with->extensible;
517 range->not_PER_visible |= with->not_PER_visible;
518 range->empty_constraint |= with->empty_constraint;
519
520 if(range->empty_constraint) {
521 /* No use in intersecting empty constraints */
vlmb5be8c32004-08-18 05:42:05 +0000522 return 0;
523 }
524
525 /*
526 * This is an AND operation.
527 */
528
529 /* If this is the only element, insert it into itself as a child */
530 if(range->el_count == 0) {
531 asn1cnst_range_t *r = _range_new();
532 r->left = range->left;
533 r->right = range->right;
534 _range_insert(range, r);
535 assert(range->el_count == 1);
536 }
537
538 /*
539 * Make sure we're dealing with sane data.
540 * G.4.2.3
541 */
542 if(strict_edge_check) {
543 for(j = -1; j < with->el_count; j++) {
544
545 if(j == -1) {
546 if(with->el_count) continue;
547 if(_check_edges_within(range, with))
548 return -1;
549 } else {
550 if(_check_edges_within(range,
551 with->elements[j]))
552 return -1;
553 }
554 }
555 }
556
557 /*
558 * Split range in pieces.
559 */
560
561 for(i = 0; i < range->el_count; i++) {
562 for(j = -1; j < with->el_count; j++) {
563 const asn1cnst_range_t *wel;
564 asn1cnst_range_t *r;
565
566 if(j == -1) {
567 if(with->el_count) continue;
568 wel = with;
569 } else {
570 wel = with->elements[j];
vlme6c89732005-05-17 21:46:18 +0000571 assert(!wel->el_count); /* non-compound item! */
vlmb5be8c32004-08-18 05:42:05 +0000572 }
573
574 r = _range_split(range->elements[i], wel);
575 if(r) {
576 int ec;
577 /* Substitute the current element with a split */
578 _range_remove_element(range, i);
579 assert(r->el_count);
580 for(ec = 0; ec < r->el_count; ec++) {
581 ret = _range_insert(range, r->elements[ec]);
582 assert(ret == 0);
583 }
584 r->el_count = 0;
585 _range_free(r);
586 i--;
587 break; /* Try again from this point */
588 }
589 }
590 }
591
592 assert(range->el_count);
593
594 /*
595 * Remove pieces which aren't AND-compatible "with" range.
596 */
597
598 for(i = 0; i < range->el_count; i++) {
599 for(j = -1; j < with->el_count; j++) {
600 const asn1cnst_range_t *wel;
601
602 if(j == -1) {
603 if(with->el_count) continue;
604 wel = with;
605 } else {
606 wel = with->elements[j];
607 }
608
609 if(_range_overlap(range->elements[i], wel))
610 break;
611 }
612 if(j == with->el_count) {
613 _range_remove_element(range, i);
614 i--;
615 }
616 }
617
618 if(range->el_count == 0)
619 range->empty_constraint = 1;
620
621 return 0;
622}
623
624static int
625_range_union(asn1cnst_range_t *range) {
626 int i;
627
628 qsort(range->elements, range->el_count, sizeof(range->elements[0]),
629 _range_compare);
630
631 /*
632 * The range is sorted by the start values.
633 */
634 for(i = 1; i < range->el_count; i++) {
635 asn1cnst_range_t *ra = range->elements[i - 1];
636 asn1cnst_range_t *rb = range->elements[i];
637
638 if(_range_overlap(ra, rb)) {
639 if(_edge_compare(&ra->left, &rb->left) < 0)
640 rb->left = ra->left;
641
642 if(_edge_compare(&ra->right, &rb->right) > 0)
643 rb->right = ra->right;
644 } else {
645 /*
646 * Still, range may be joined: (1..4)(5..10).
647 * This logic is valid only for whole numbers
648 * (i.e., not REAL type, but REAL constraints
vlmee8b06f2004-08-25 02:05:28 +0000649 * are not PER-visible (X.691, #9.3.12).
vlmb5be8c32004-08-18 05:42:05 +0000650 */
651 if(ra->right.type == ARE_VALUE
652 && rb->left.type == ARE_VALUE
653 && (rb->left.value - ra->right.value) == 1) {
654 /* (1..10) */
655 rb->left = ra->left;
656 } else {
657 continue;
658 }
659 }
660
661 /*
662 * Squeeze the array by removing the ra.
663 */
664 _range_remove_element(range, i - 1);
665
666 i--; /* Retry from the current point */
667 }
668
669 return 0;
670}
671
672static int
673_range_canonicalize(asn1cnst_range_t *range) {
674
675 if(range->el_count == 0) {
676 /*
677 * Switch left and right edges, make them sorted.
678 * It might be a mild warning though.
679 */
680 if(_edge_compare(&range->left, &range->right) > 0) {
681 asn1cnst_edge_t tmp = range->left;
682 range->left = range->right;
683 range->right = tmp;
684 }
685
686 if(range->elements) {
687 free(range->elements);
688 range->elements = 0;
689 }
690 range->el_size = 0;
691 return 0;
692 }
693
694 /*
695 * Remove duplicates and overlaps by merging them in.
696 */
697 _range_union(range);
698
699 /* Refine the left edge of a parent */
700 range->left = range->elements[0]->left;
701
702 /* Refine the right edge of a parent */
703 range->right = range->elements[range->el_count - 1]->right;
704
705 /* Remove the child, if it's a single one */
706 if(range->el_count == 1) {
707 _range_remove_element(range, 0);
708 }
709
710 return 0;
711}
712
713asn1cnst_range_t *
vlmee8b06f2004-08-25 02:05:28 +0000714asn1constraint_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 +0000715 asn1cnst_range_t *range;
716 asn1cnst_range_t *tmp;
717 asn1p_value_t *vmin;
718 asn1p_value_t *vmax;
719 int expectation_met;
vlm76142452004-09-05 10:36:22 +0000720 unsigned int i;
vlmb5be8c32004-08-18 05:42:05 +0000721 int ret;
vlmb5be8c32004-08-18 05:42:05 +0000722
723 if(!exmet) {
724 exmet = &expectation_met;
725 *exmet = 0;
726 }
727
728 /*
vlmee8b06f2004-08-25 02:05:28 +0000729 * Check if the requested constraint is theoretically compatible
730 * with the given expression type.
vlmb5be8c32004-08-18 05:42:05 +0000731 */
732 if(asn1constraint_compatible(expr_type, type) != 1) {
733 errno = EINVAL;
734 return 0;
735 }
736
737 /* Check arguments' validity. */
738 switch(type) {
739 case ACT_EL_RANGE:
740 if(exmet == &expectation_met)
741 *exmet = 1;
742 break;
743 case ACT_CT_FROM:
744 if(!minmax) {
745 minmax = asn1constraint_default_alphabet(expr_type);
746 if(minmax) {
747 break;
748 }
749 }
750 /* Fall through */
751 case ACT_CT_SIZE:
752 if(!minmax) {
753 static asn1cnst_range_t mm;
754 mm.left.type = ARE_VALUE;
755 mm.left.value = 0;
756 mm.right.type = ARE_MAX;
757 minmax = &mm;
758 }
759 break;
760 default:
761 errno = EINVAL;
762 return 0;
763 }
764
765 if(minmax) {
766 range = _range_clone(minmax);
767 } else {
768 range = _range_new();
769 }
770
vlmee8b06f2004-08-25 02:05:28 +0000771 /*
772 * X.691, #9.3.6
773 * Constraints on restricter character string types
774 * which are not known-multiplier are not PER-visible.
775 */
776 if((expr_type & ASN_STRING_NKM_MASK))
777 range->not_PER_visible = 1;
778
779 if(!ct || (strict_PV && range->not_PER_visible))
vlmb5be8c32004-08-18 05:42:05 +0000780 return range;
781
782 switch(ct->type) {
783 case ACT_EL_VALUE:
784 vmin = vmax = ct->value;
785 break;
786 case ACT_EL_RANGE:
787 case ACT_EL_LLRANGE:
788 case ACT_EL_RLRANGE:
789 case ACT_EL_ULRANGE:
790 vmin = ct->range_start;
791 vmax = ct->range_stop;
792 break;
793 case ACT_EL_EXT:
794 if(!*exmet) {
vlmee8b06f2004-08-25 02:05:28 +0000795 range->incompatible = 1;
vlmb5be8c32004-08-18 05:42:05 +0000796 } else {
vlmee8b06f2004-08-25 02:05:28 +0000797 _range_free(range);
798 errno = ERANGE;
799 range = 0;
vlmb5be8c32004-08-18 05:42:05 +0000800 }
801 return range;
802 case ACT_CT_SIZE:
803 case ACT_CT_FROM:
804 if(type == ct->type) {
805 *exmet = 1;
806 } else {
vlmee8b06f2004-08-25 02:05:28 +0000807 range->incompatible = 1;
vlmb5be8c32004-08-18 05:42:05 +0000808 return range;
809 }
810 assert(ct->el_count == 1);
vlmee8b06f2004-08-25 02:05:28 +0000811 tmp = asn1constraint_compute_PER_range(expr_type,
812 ct->elements[0], type, minmax, exmet, strict_PV);
813 if(tmp) {
814 _range_free(range);
815 } else {
816 if(errno == ERANGE) {
817 range->empty_constraint = 1;
818 range->extensible = 1;
819 tmp = range;
820 } else {
821 _range_free(range);
822 }
823 }
824 return tmp;
vlmb5be8c32004-08-18 05:42:05 +0000825 case ACT_CA_SET: /* (10..20)(15..17) */
826 case ACT_CA_INT: /* SIZE(1..2) ^ FROM("ABCD") */
827
828 /* AND constraints, one after another. */
829 for(i = 0; i < ct->el_count; i++) {
830 tmp = asn1constraint_compute_PER_range(expr_type,
831 ct->elements[i], type,
vlmee8b06f2004-08-25 02:05:28 +0000832 ct->type==ACT_CA_SET?range:minmax, exmet,
833 strict_PV);
vlmb5be8c32004-08-18 05:42:05 +0000834 if(!tmp) {
vlmee8b06f2004-08-25 02:05:28 +0000835 if(errno == ERANGE) {
836 continue;
837 } else {
838 _range_free(range);
839 return NULL;
840 }
vlmb5be8c32004-08-18 05:42:05 +0000841 }
842
vlmee8b06f2004-08-25 02:05:28 +0000843 if(tmp->incompatible) {
844 /*
845 * Ignore constraints
846 * incompatible with arguments:
847 * SIZE(1..2) ^ FROM("ABCD")
848 * either SIZE or FROM will be ignored.
849 */
850 _range_free(tmp);
851 continue;
852 }
853
854 if(strict_PV && tmp->not_PER_visible) {
vlmb5be8c32004-08-18 05:42:05 +0000855 if(ct->type == ACT_CA_SET) {
856 /*
857 * X.691, #9.3.18:
858 * Ignore this separate component.
859 */
860 } else {
861 /*
862 * X.691, #9.3.19:
863 * Ignore not PER-visible INTERSECTION
864 */
865 }
866 _range_free(tmp);
867 continue;
868 }
869
vlmb5be8c32004-08-18 05:42:05 +0000870 ret = _range_intersection(range, tmp,
871 ct->type == ACT_CA_SET);
872 _range_free(tmp);
873 if(ret) {
874 _range_free(range);
875 errno = EPERM;
876 return NULL;
877 }
878 _range_canonicalize(range);
879 }
880
881 return range;
882 case ACT_CA_CSV: /* SIZE(1..2, 3..4) */
883 case ACT_CA_UNI: /* SIZE(1..2) | FROM("ABCD") */
884
vlmee8b06f2004-08-25 02:05:28 +0000885 /*
886 * Grab the first valid constraint.
887 */
888 tmp = 0;
vlmb5be8c32004-08-18 05:42:05 +0000889 for(i = 0; i < ct->el_count; i++) {
890 tmp = asn1constraint_compute_PER_range(expr_type,
vlmee8b06f2004-08-25 02:05:28 +0000891 ct->elements[i], type, minmax, exmet,
892 strict_PV);
vlmb5be8c32004-08-18 05:42:05 +0000893 if(!tmp) {
vlmee8b06f2004-08-25 02:05:28 +0000894 if(errno == ERANGE) {
895 range->extensible = 1;
896 continue;
897 } else {
898 _range_free(range);
899 return NULL;
900 }
901 }
902 if(tmp->incompatible) {
903 _range_free(tmp);
904 tmp = 0;
905 }
906 break;
907 }
908 if(tmp) {
909 tmp->extensible |= range->extensible;
910 tmp->empty_constraint |= range->empty_constraint;
911 _range_free(range);
912 range = tmp;
913 } else {
914 range->incompatible = 1;
915 return range;
916 }
917
918 /*
919 * Merge with the rest of them.
920 * Canonicalizator will do the union magic.
921 */
922 for(; i < ct->el_count; i++) {
923 tmp = asn1constraint_compute_PER_range(expr_type,
924 ct->elements[i], type, minmax, exmet,
925 strict_PV);
926 if(!tmp) {
927 if(errno == ERANGE) {
928 range->extensible = 1;
929 continue;
930 } else {
931 _range_free(range);
932 return NULL;
933 }
934 }
935
936 if(tmp->incompatible) {
937 _range_free(tmp);
938 _range_canonicalize(range);
939 range->incompatible = 1;
940 return range;
vlmb5be8c32004-08-18 05:42:05 +0000941 }
942
943 if(tmp->empty_constraint) {
vlmee8b06f2004-08-25 02:05:28 +0000944 /*
945 * Ignore empty constraints in OR logic.
946 */
947 range->extensible |= tmp->extensible;
vlmb5be8c32004-08-18 05:42:05 +0000948 _range_free(tmp);
949 continue;
950 }
951
vlmb5be8c32004-08-18 05:42:05 +0000952 _range_merge_in(range, tmp);
953 }
954
955 _range_canonicalize(range);
956
vlmee8b06f2004-08-25 02:05:28 +0000957 if(range->extensible && type == ACT_CT_FROM) {
958 /*
959 * X.691, #9.3.10:
960 * Extensible permitted alphabet constraints
961 * are not PER-visible.
962 */
963 range->not_PER_visible = 1;
964 }
965
966 if(strict_PV && range->not_PER_visible) {
vlmb5be8c32004-08-18 05:42:05 +0000967 /*
968 * X.691, #9.3.19:
969 * If not PER-visible constraint is part of UNION,
vlmee8b06f2004-08-25 02:05:28 +0000970 * the whole resulting constraint is not PER-visible.
vlmb5be8c32004-08-18 05:42:05 +0000971 */
972 _range_free(range);
973 if(minmax)
974 range = _range_clone(minmax);
975 else
976 range = _range_new();
vlmee8b06f2004-08-25 02:05:28 +0000977 range->not_PER_visible = 1;
978 range->incompatible = 1;
vlmb5be8c32004-08-18 05:42:05 +0000979 }
980
981 return range;
982 case ACT_CA_EXC: /* FROM("ABCD") EXCEPT FROM("AB") */
983 /*
984 * X.691, #9.3.19:
985 * EXCEPT and the following value set is completely ignored.
986 */
987 assert(ct->el_count >= 1);
988 _range_free(range);
989 range = asn1constraint_compute_PER_range(expr_type,
vlmee8b06f2004-08-25 02:05:28 +0000990 ct->elements[0], type, minmax, exmet, strict_PV);
vlmb5be8c32004-08-18 05:42:05 +0000991 return range;
992 default:
vlmee8b06f2004-08-25 02:05:28 +0000993 range->incompatible = 1;
vlmb5be8c32004-08-18 05:42:05 +0000994 return range;
995 }
996
997
998 if(!*exmet) {
999 /*
1000 * Expectation is not met. Return the default range.
1001 */
vlmee8b06f2004-08-25 02:05:28 +00001002 range->incompatible = 1;
vlmb5be8c32004-08-18 05:42:05 +00001003 return range;
1004 }
1005
1006 _range_free(range);
1007 range = _range_new();
1008
1009 ret = _range_fill(vmin, minmax, &range->left,
1010 range, type, ct->_lineno);
vlmb6fd3b22004-08-25 00:42:25 +00001011 if(!ret)
1012 ret = _range_fill(vmax, minmax, &range->right,
vlmb5be8c32004-08-18 05:42:05 +00001013 range, type, ct->_lineno);
1014 if(ret) {
1015 _range_free(range);
1016 errno = EPERM;
1017 return NULL;
1018 }
1019
1020 if(minmax) {
1021 asn1cnst_range_t *clone;
1022
1023 clone = _range_clone(minmax);
1024
1025 /* Constrain parent type with given data. */
1026 ret = _range_intersection(clone, range, 1);
1027 _range_free(range);
1028 if(ret) {
1029 _range_free(clone);
1030 errno = EPERM;
1031 return NULL;
1032 }
1033 range = clone;
1034 }
1035
1036 /*
1037 * Recompute elements's min/max, remove duplicates, etc.
1038 */
1039 _range_canonicalize(range);
1040
1041 return range;
1042}
1043