vlm | 8a09e0f | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 1 | #include "asn1fix_internal.h" |
| 2 | #include "asn1fix_constraint.h" |
| 3 | #include "asn1fix_crange.h" |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 4 | |
| 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 | |
| 12 | void |
| 13 | asn1constraint_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 | |
| 26 | static 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 | |
| 36 | static 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 | |
| 58 | static 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 | |
| 79 | static 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 | |
| 103 | static 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 | |
| 138 | static 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 | |
| 152 | static 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: |
vlm | 47ae158 | 2004-09-24 21:01:43 +0000 | [diff] [blame] | 160 | snprintf(buf, sizeof(buf), "%" PRIdASN, edge->value); |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 161 | } |
| 162 | return buf; |
| 163 | } |
| 164 | |
| 165 | static 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)); |
vlm | ee8b06f | 2004-08-25 02:05:28 +0000 | [diff] [blame] | 170 | printf(".%s", _edge_value(&range->right)); |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 171 | } else { |
vlm | ee8b06f | 2004-08-25 02:05:28 +0000 | [diff] [blame] | 172 | printf("(%s", _edge_value(&range->left)); |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 173 | } |
| 174 | |
vlm | ee8b06f | 2004-08-25 02:05:28 +0000 | [diff] [blame] | 175 | 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 | |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 184 | 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 | |
| 193 | static 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 | |
| 213 | static 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 | |
| 239 | static 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 | |
vlm | ee8b06f | 2004-08-25 02:05:28 +0000 | [diff] [blame] | 244 | into->not_PER_visible |= cr->not_PER_visible; |
| 245 | into->extensible |= cr->extensible; |
| 246 | |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 247 | /* |
| 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 | |
| 275 | static 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) { |
vlm | 47ae158 | 2004-09-24 21:01:43 +0000 | [diff] [blame] | 283 | FATAL("Integer %" PRIdASN " value invalid " |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 284 | "for %s constraint at line %d", |
vlm | 47ae158 | 2004-09-24 21:01:43 +0000 | [diff] [blame] | 285 | val->value.v_integer, |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 286 | 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; |
vlm | e1e6ed8 | 2005-03-24 14:26:38 +0000 | [diff] [blame] | 324 | case ATV_TUPLE: |
| 325 | case ATV_QUADRUPLE: |
| 326 | edge->type = ARE_VALUE; |
| 327 | edge->value = val->value.v_integer; |
| 328 | return 0; |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 329 | case ATV_STRING: |
| 330 | if(type != ACT_CT_FROM) |
| 331 | return 0; |
| 332 | break; |
vlm | b6fd3b2 | 2004-08-25 00:42:25 +0000 | [diff] [blame] | 333 | case ATV_REFERENCED: |
vlm | 5746178 | 2004-09-15 11:45:44 +0000 | [diff] [blame] | 334 | FATAL("Unresolved constraint element \"%s\" at line %d", |
vlm | b6fd3b2 | 2004-08-25 00:42:25 +0000 | [diff] [blame] | 335 | asn1f_printable_reference(val->value.reference), |
| 336 | lineno); |
| 337 | return -1; |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 338 | 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 | */ |
vlm | d5b3bf3 | 2004-09-29 13:17:17 +0000 | [diff] [blame] | 360 | asn1c_integer_t vmin, vmax; |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 361 | 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 | */ |
| 387 | static 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 | */ |
| 419 | static 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 | */ |
vlm | e6c8973 | 2005-05-17 21:46:18 +0000 | [diff] [blame] | 456 | while(ll < 0) { |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 457 | nr->left = ra->left; |
| 458 | nr->right = rb->left; |
vlm | e6c8973 | 2005-05-17 21:46:18 +0000 | [diff] [blame] | 459 | 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 | } |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 464 | nr->right.value--; |
vlm | e6c8973 | 2005-05-17 21:46:18 +0000 | [diff] [blame] | 465 | } |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 466 | _range_insert(range, nr); |
| 467 | nr = _range_new(); |
| 468 | assert(nr); |
vlm | e6c8973 | 2005-05-17 21:46:18 +0000 | [diff] [blame] | 469 | break; |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | /* |
| 473 | * L: ...---| |
| 474 | * R: ..--| |
| 475 | */ |
vlm | e6c8973 | 2005-05-17 21:46:18 +0000 | [diff] [blame] | 476 | while(rr > 0) { |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 477 | nr->left = rb->right; |
| 478 | nr->right = ra->right; |
vlm | e6c8973 | 2005-05-17 21:46:18 +0000 | [diff] [blame] | 479 | 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 | } |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 484 | nr->left.value++; |
vlm | e6c8973 | 2005-05-17 21:46:18 +0000 | [diff] [blame] | 485 | } |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 486 | _range_insert(range, nr); |
| 487 | nr = _range_new(); |
| 488 | assert(nr); |
vlm | e6c8973 | 2005-05-17 21:46:18 +0000 | [diff] [blame] | 489 | break; |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 490 | } |
| 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 | |
| 508 | static 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 | |
vlm | ee8b06f | 2004-08-25 02:05:28 +0000 | [diff] [blame] | 513 | 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 */ |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 522 | 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]; |
vlm | e6c8973 | 2005-05-17 21:46:18 +0000 | [diff] [blame] | 571 | assert(!wel->el_count); /* non-compound item! */ |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 572 | } |
| 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 | |
| 624 | static 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 |
vlm | ee8b06f | 2004-08-25 02:05:28 +0000 | [diff] [blame] | 649 | * are not PER-visible (X.691, #9.3.12). |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 650 | */ |
| 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 | |
| 672 | static 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 | |
| 713 | asn1cnst_range_t * |
vlm | ee8b06f | 2004-08-25 02:05:28 +0000 | [diff] [blame] | 714 | asn1constraint_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) { |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 715 | asn1cnst_range_t *range; |
| 716 | asn1cnst_range_t *tmp; |
| 717 | asn1p_value_t *vmin; |
| 718 | asn1p_value_t *vmax; |
| 719 | int expectation_met; |
vlm | 7614245 | 2004-09-05 10:36:22 +0000 | [diff] [blame] | 720 | unsigned int i; |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 721 | int ret; |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 722 | |
| 723 | if(!exmet) { |
| 724 | exmet = &expectation_met; |
| 725 | *exmet = 0; |
| 726 | } |
| 727 | |
| 728 | /* |
vlm | ee8b06f | 2004-08-25 02:05:28 +0000 | [diff] [blame] | 729 | * Check if the requested constraint is theoretically compatible |
| 730 | * with the given expression type. |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 731 | */ |
| 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 | |
vlm | ee8b06f | 2004-08-25 02:05:28 +0000 | [diff] [blame] | 771 | /* |
| 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)) |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 780 | 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) { |
vlm | ee8b06f | 2004-08-25 02:05:28 +0000 | [diff] [blame] | 795 | range->incompatible = 1; |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 796 | } else { |
vlm | ee8b06f | 2004-08-25 02:05:28 +0000 | [diff] [blame] | 797 | _range_free(range); |
| 798 | errno = ERANGE; |
| 799 | range = 0; |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 800 | } |
| 801 | return range; |
| 802 | case ACT_CT_SIZE: |
| 803 | case ACT_CT_FROM: |
| 804 | if(type == ct->type) { |
| 805 | *exmet = 1; |
| 806 | } else { |
vlm | ee8b06f | 2004-08-25 02:05:28 +0000 | [diff] [blame] | 807 | range->incompatible = 1; |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 808 | return range; |
| 809 | } |
| 810 | assert(ct->el_count == 1); |
vlm | ee8b06f | 2004-08-25 02:05:28 +0000 | [diff] [blame] | 811 | 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; |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 825 | 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, |
vlm | ee8b06f | 2004-08-25 02:05:28 +0000 | [diff] [blame] | 832 | ct->type==ACT_CA_SET?range:minmax, exmet, |
| 833 | strict_PV); |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 834 | if(!tmp) { |
vlm | ee8b06f | 2004-08-25 02:05:28 +0000 | [diff] [blame] | 835 | if(errno == ERANGE) { |
| 836 | continue; |
| 837 | } else { |
| 838 | _range_free(range); |
| 839 | return NULL; |
| 840 | } |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 841 | } |
| 842 | |
vlm | ee8b06f | 2004-08-25 02:05:28 +0000 | [diff] [blame] | 843 | 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) { |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 855 | 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 | |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 870 | 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 | |
vlm | ee8b06f | 2004-08-25 02:05:28 +0000 | [diff] [blame] | 885 | /* |
| 886 | * Grab the first valid constraint. |
| 887 | */ |
| 888 | tmp = 0; |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 889 | for(i = 0; i < ct->el_count; i++) { |
| 890 | tmp = asn1constraint_compute_PER_range(expr_type, |
vlm | ee8b06f | 2004-08-25 02:05:28 +0000 | [diff] [blame] | 891 | ct->elements[i], type, minmax, exmet, |
| 892 | strict_PV); |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 893 | if(!tmp) { |
vlm | ee8b06f | 2004-08-25 02:05:28 +0000 | [diff] [blame] | 894 | 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; |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | if(tmp->empty_constraint) { |
vlm | ee8b06f | 2004-08-25 02:05:28 +0000 | [diff] [blame] | 944 | /* |
| 945 | * Ignore empty constraints in OR logic. |
| 946 | */ |
| 947 | range->extensible |= tmp->extensible; |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 948 | _range_free(tmp); |
| 949 | continue; |
| 950 | } |
| 951 | |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 952 | _range_merge_in(range, tmp); |
| 953 | } |
| 954 | |
| 955 | _range_canonicalize(range); |
| 956 | |
vlm | ee8b06f | 2004-08-25 02:05:28 +0000 | [diff] [blame] | 957 | 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) { |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 967 | /* |
| 968 | * X.691, #9.3.19: |
| 969 | * If not PER-visible constraint is part of UNION, |
vlm | ee8b06f | 2004-08-25 02:05:28 +0000 | [diff] [blame] | 970 | * the whole resulting constraint is not PER-visible. |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 971 | */ |
| 972 | _range_free(range); |
| 973 | if(minmax) |
| 974 | range = _range_clone(minmax); |
| 975 | else |
| 976 | range = _range_new(); |
vlm | ee8b06f | 2004-08-25 02:05:28 +0000 | [diff] [blame] | 977 | range->not_PER_visible = 1; |
| 978 | range->incompatible = 1; |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 979 | } |
| 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, |
vlm | ee8b06f | 2004-08-25 02:05:28 +0000 | [diff] [blame] | 990 | ct->elements[0], type, minmax, exmet, strict_PV); |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 991 | return range; |
| 992 | default: |
vlm | ee8b06f | 2004-08-25 02:05:28 +0000 | [diff] [blame] | 993 | range->incompatible = 1; |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 994 | return range; |
| 995 | } |
| 996 | |
| 997 | |
| 998 | if(!*exmet) { |
| 999 | /* |
| 1000 | * Expectation is not met. Return the default range. |
| 1001 | */ |
vlm | ee8b06f | 2004-08-25 02:05:28 +0000 | [diff] [blame] | 1002 | range->incompatible = 1; |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 1003 | 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); |
vlm | b6fd3b2 | 2004-08-25 00:42:25 +0000 | [diff] [blame] | 1011 | if(!ret) |
| 1012 | ret = _range_fill(vmax, minmax, &range->right, |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 1013 | 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 | |