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 | |
vlm | da31348 | 2006-09-21 02:21:53 +0000 | [diff] [blame] | 5 | static void _remove_extensions(arg_t *arg, asn1p_constraint_t *ct, int flast); |
vlm | 53832f1 | 2005-03-20 12:57:21 +0000 | [diff] [blame] | 6 | static int constraint_type_resolve(arg_t *arg, asn1p_constraint_t *ct); |
| 7 | static int constraint_value_resolve(arg_t *arg, asn1p_value_t **value, enum asn1p_constraint_type_e real_ctype); |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 8 | |
| 9 | int |
| 10 | asn1constraint_pullup(arg_t *arg) { |
| 11 | asn1p_expr_t *expr = arg->expr; |
vlm | 53832f1 | 2005-03-20 12:57:21 +0000 | [diff] [blame] | 12 | asn1p_expr_t *top_parent; |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 13 | asn1p_constraint_t *ct_parent; |
| 14 | asn1p_constraint_t *ct_expr; |
| 15 | int ret; |
| 16 | |
| 17 | if(expr->combined_constraints) |
| 18 | return 0; /* Operation already performed earlier */ |
| 19 | |
| 20 | switch(expr->meta_type) { |
| 21 | case AMT_TYPE: |
| 22 | case AMT_TYPEREF: |
| 23 | break; |
| 24 | default: |
| 25 | return 0; /* Nothing to do */ |
| 26 | } |
| 27 | |
| 28 | if(expr->expr_type == A1TC_REFERENCE) { |
| 29 | asn1p_ref_t *ref = expr->reference; |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 30 | asn1p_expr_t *parent_expr; |
| 31 | |
| 32 | assert(ref); |
vlm | 0c6d381 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 33 | parent_expr = asn1f_lookup_symbol(arg, expr->module, expr->rhs_pspecs, ref); |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 34 | if(!parent_expr) { |
| 35 | if(errno != EEXIST) { |
| 36 | DEBUG("\tWhile fetching parent constraints: " |
| 37 | "type \"%s\" not found: %s", |
| 38 | asn1f_printable_reference(ref), |
| 39 | strerror(errno)); |
| 40 | return -1; |
| 41 | } else { |
| 42 | /* |
| 43 | * -fknown-extern-type is given. |
| 44 | * Assume there are no constraints there. |
| 45 | */ |
| 46 | WARNING("External type \"%s\": " |
| 47 | "assuming no constraints", |
| 48 | asn1f_printable_reference(ref)); |
| 49 | ct_parent = 0; |
| 50 | } |
| 51 | } else { |
| 52 | arg->expr = parent_expr; |
| 53 | ret = asn1constraint_pullup(arg); |
| 54 | arg->expr = expr; |
| 55 | if(ret) return ret; |
| 56 | |
| 57 | ct_parent = parent_expr->combined_constraints; |
| 58 | } |
| 59 | } else { |
| 60 | ct_parent = 0; |
| 61 | } |
| 62 | |
| 63 | ct_expr = expr->constraints; |
| 64 | |
| 65 | if(!ct_parent && !ct_expr) |
| 66 | return 0; /* No constraints to consider */ |
| 67 | |
vlm | 53832f1 | 2005-03-20 12:57:21 +0000 | [diff] [blame] | 68 | /* |
| 69 | * Resolve constraints, if not already resolved. |
| 70 | */ |
| 71 | top_parent = asn1f_find_terminal_type(arg, arg->expr); |
| 72 | ret = asn1constraint_resolve(arg, ct_expr, |
| 73 | top_parent ? top_parent->expr_type : A1TC_INVALID, 0); |
| 74 | if(ret) return ret; |
| 75 | |
| 76 | /* |
| 77 | * Copy parent type constraints. |
| 78 | */ |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 79 | if(ct_parent) { |
| 80 | ct_parent = asn1p_constraint_clone(ct_parent); |
| 81 | assert(ct_parent); |
| 82 | } |
| 83 | |
| 84 | /* |
| 85 | * If the current type does not have constraints, it inherits |
| 86 | * the constraints of a parent. |
| 87 | */ |
| 88 | if(ct_parent && !ct_expr) { |
| 89 | expr->combined_constraints = ct_parent; |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | ct_expr = asn1p_constraint_clone(ct_expr); |
| 94 | assert(ct_expr); |
| 95 | |
| 96 | /* |
| 97 | * Now we have a set of current expression's constraints, |
| 98 | * and an optional set of the parent expression's constraints. |
| 99 | */ |
| 100 | |
| 101 | if(ct_parent) { |
| 102 | /* |
| 103 | * If we have a parent, remove all the extensions (46.4). |
| 104 | */ |
vlm | da31348 | 2006-09-21 02:21:53 +0000 | [diff] [blame] | 105 | _remove_extensions(arg, ct_parent, 0); |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 106 | |
| 107 | expr->combined_constraints = ct_parent; |
| 108 | if(ct_expr->type == ACT_CA_SET) { |
vlm | 7614245 | 2004-09-05 10:36:22 +0000 | [diff] [blame] | 109 | unsigned int i; |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 110 | for(i = 0; i < ct_expr->el_count; i++) { |
| 111 | if(asn1p_constraint_insert( |
| 112 | expr->combined_constraints, |
| 113 | ct_expr->elements[i])) { |
| 114 | expr->combined_constraints = 0; |
| 115 | asn1p_constraint_free(ct_expr); |
| 116 | asn1p_constraint_free(ct_parent); |
| 117 | return -1; |
| 118 | } else { |
| 119 | ct_expr->elements[i] = 0; |
| 120 | } |
| 121 | } |
| 122 | asn1p_constraint_free(ct_expr); |
| 123 | } else { |
| 124 | asn1p_constraint_insert(expr->combined_constraints, |
| 125 | ct_expr); |
| 126 | } |
| 127 | } else { |
vlm | da31348 | 2006-09-21 02:21:53 +0000 | [diff] [blame] | 128 | _remove_extensions(arg, ct_expr, 1); |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 129 | expr->combined_constraints = ct_expr; |
| 130 | } |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | int |
vlm | 53832f1 | 2005-03-20 12:57:21 +0000 | [diff] [blame] | 136 | asn1constraint_resolve(arg_t *arg, asn1p_constraint_t *ct, asn1p_expr_type_e etype, enum asn1p_constraint_type_e effective_type) { |
vlm | 8fba257 | 2005-03-10 11:27:13 +0000 | [diff] [blame] | 137 | enum asn1p_constraint_type_e real_constraint_type; |
vlm | 7614245 | 2004-09-05 10:36:22 +0000 | [diff] [blame] | 138 | unsigned int el; |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 139 | int rvalue = 0; |
| 140 | int ret; |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 141 | |
vlm | 8fba257 | 2005-03-10 11:27:13 +0000 | [diff] [blame] | 142 | DEBUG("(\"%s\")", arg->expr->Identifier); |
vlm | fd24593 | 2005-03-10 10:02:50 +0000 | [diff] [blame] | 143 | |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 144 | if(!ct) return 0; |
| 145 | |
| 146 | /* Don't touch information object classes */ |
| 147 | switch(ct->type) { |
vlm | d8d9035 | 2004-08-20 13:25:56 +0000 | [diff] [blame] | 148 | case ACT_CT_SIZE: |
| 149 | case ACT_CT_FROM: |
| 150 | if(effective_type && effective_type != ct->type) { |
| 151 | FATAL("%s at line %d: " |
| 152 | "Incompatible nested %s within %s", |
| 153 | arg->expr->Identifier, ct->_lineno, |
| 154 | asn1p_constraint_type2str(ct->type), |
| 155 | asn1p_constraint_type2str(effective_type) |
| 156 | ); |
| 157 | } |
| 158 | effective_type = ct->type; |
| 159 | break; |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 160 | case ACT_CT_WCOMP: |
| 161 | case ACT_CT_WCOMPS: |
| 162 | case ACT_CA_CRC: |
| 163 | return 0; |
| 164 | default: |
| 165 | break; |
| 166 | } |
| 167 | |
vlm | 8fba257 | 2005-03-10 11:27:13 +0000 | [diff] [blame] | 168 | real_constraint_type = effective_type ? effective_type : ct->type; |
| 169 | |
vlm | d8d9035 | 2004-08-20 13:25:56 +0000 | [diff] [blame] | 170 | if(etype != A1TC_INVALID) { |
vlm | d8d9035 | 2004-08-20 13:25:56 +0000 | [diff] [blame] | 171 | |
vlm | 2702858 | 2005-08-14 14:45:44 +0000 | [diff] [blame] | 172 | ret = asn1constraint_compatible(etype, real_constraint_type, |
| 173 | arg->flags & A1F_EXTENDED_SizeConstraint); |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 174 | switch(ret) { |
| 175 | case -1: /* If unknown, assume OK. */ |
| 176 | case 1: |
| 177 | break; |
| 178 | case 0: |
| 179 | default: |
| 180 | FATAL("%s at line %d: " |
| 181 | "Constraint type %s is not applicable to %s", |
| 182 | arg->expr->Identifier, ct->_lineno, |
vlm | 8fba257 | 2005-03-10 11:27:13 +0000 | [diff] [blame] | 183 | asn1p_constraint_type2str(real_constraint_type), |
vlm | d8d9035 | 2004-08-20 13:25:56 +0000 | [diff] [blame] | 184 | ASN_EXPR_TYPE2STR(etype) |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 185 | ); |
| 186 | rvalue = -1; |
| 187 | break; |
| 188 | } |
| 189 | } else { |
| 190 | WARNING("%s at line %d: " |
| 191 | "Constraints ignored: Unresolved parent type", |
| 192 | arg->expr->Identifier, arg->expr->_lineno); |
| 193 | } |
| 194 | |
vlm | d8d9035 | 2004-08-20 13:25:56 +0000 | [diff] [blame] | 195 | /* |
| 196 | * Resolve all possible references, wherever they occur. |
| 197 | */ |
vlm | 53832f1 | 2005-03-20 12:57:21 +0000 | [diff] [blame] | 198 | if(ct->containedSubtype) { |
vlm | 53832f1 | 2005-03-20 12:57:21 +0000 | [diff] [blame] | 199 | ret = constraint_type_resolve(arg, ct); |
| 200 | RET2RVAL(ret, rvalue); |
| 201 | } |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 202 | if(ct->value && ct->value->type == ATV_REFERENCED) { |
vlm | 53832f1 | 2005-03-20 12:57:21 +0000 | [diff] [blame] | 203 | ret = constraint_value_resolve(arg, |
vlm | 8fba257 | 2005-03-10 11:27:13 +0000 | [diff] [blame] | 204 | &ct->value, real_constraint_type); |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 205 | RET2RVAL(ret, rvalue); |
| 206 | } |
| 207 | if(ct->range_start && ct->range_start->type == ATV_REFERENCED) { |
vlm | 53832f1 | 2005-03-20 12:57:21 +0000 | [diff] [blame] | 208 | ret = constraint_value_resolve(arg, |
vlm | 8fba257 | 2005-03-10 11:27:13 +0000 | [diff] [blame] | 209 | &ct->range_start, real_constraint_type); |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 210 | RET2RVAL(ret, rvalue); |
| 211 | } |
| 212 | if(ct->range_stop && ct->range_stop->type == ATV_REFERENCED) { |
vlm | 53832f1 | 2005-03-20 12:57:21 +0000 | [diff] [blame] | 213 | ret = constraint_value_resolve(arg, |
vlm | 8fba257 | 2005-03-10 11:27:13 +0000 | [diff] [blame] | 214 | &ct->range_stop, real_constraint_type); |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 215 | RET2RVAL(ret, rvalue); |
| 216 | } |
| 217 | |
vlm | d8d9035 | 2004-08-20 13:25:56 +0000 | [diff] [blame] | 218 | /* |
| 219 | * Proceed recursively. |
| 220 | */ |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 221 | for(el = 0; el < ct->el_count; el++) { |
vlm | 53832f1 | 2005-03-20 12:57:21 +0000 | [diff] [blame] | 222 | ret = asn1constraint_resolve(arg, ct->elements[el], |
vlm | d8d9035 | 2004-08-20 13:25:56 +0000 | [diff] [blame] | 223 | etype, effective_type); |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 224 | RET2RVAL(ret, rvalue); |
| 225 | } |
| 226 | |
| 227 | return rvalue; |
| 228 | } |
| 229 | |
| 230 | static void |
vlm | da31348 | 2006-09-21 02:21:53 +0000 | [diff] [blame] | 231 | _remove_extensions(arg_t *arg, asn1p_constraint_t *ct, int forgive_last) { |
vlm | 7614245 | 2004-09-05 10:36:22 +0000 | [diff] [blame] | 232 | unsigned int i; |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 233 | |
vlm | da31348 | 2006-09-21 02:21:53 +0000 | [diff] [blame] | 234 | if(!ct) return; |
| 235 | |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 236 | for(i = 0; i < ct->el_count; i++) { |
| 237 | if(ct->elements[i]->type == ACT_EL_EXT) |
| 238 | break; |
vlm | da31348 | 2006-09-21 02:21:53 +0000 | [diff] [blame] | 239 | if(forgive_last && ct->type == ACT_CA_SET |
| 240 | && i + 1 == ct->el_count) |
| 241 | return; |
| 242 | _remove_extensions(arg, ct->elements[i], 0); |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | /* Remove the elements at and after the extensibility mark */ |
| 246 | for(; i < ct->el_count; ct->el_count--) { |
| 247 | asn1p_constraint_t *rm; |
| 248 | rm = ct->elements[ct->el_count-1]; |
| 249 | asn1p_constraint_free(rm); |
| 250 | } |
| 251 | |
| 252 | if(i < ct->el_size) |
| 253 | ct->elements[i] = 0; |
| 254 | } |
| 255 | |
vlm | 53832f1 | 2005-03-20 12:57:21 +0000 | [diff] [blame] | 256 | static int |
| 257 | constraint_type_resolve(arg_t *arg, asn1p_constraint_t *ct) { |
vlm | dfbff8c | 2006-03-21 09:41:28 +0000 | [diff] [blame] | 258 | asn1p_constraint_t *ct_expr; |
vlm | 53832f1 | 2005-03-20 12:57:21 +0000 | [diff] [blame] | 259 | int ret; |
| 260 | |
| 261 | DEBUG("(\"%s\")", asn1f_printable_value(ct->containedSubtype)); |
| 262 | |
vlm | dfbff8c | 2006-03-21 09:41:28 +0000 | [diff] [blame] | 263 | if(ct->containedSubtype->type == ATV_VALUESET) { |
| 264 | ct_expr = ct->containedSubtype->value.constraint; |
| 265 | DEBUG("Found %s in constraints", "ValueSet"); |
| 266 | } else if(ct->containedSubtype->type == ATV_REFERENCED) { |
| 267 | asn1p_expr_t *rtype; |
| 268 | arg_t tmparg; |
vlm | 53832f1 | 2005-03-20 12:57:21 +0000 | [diff] [blame] | 269 | |
vlm | dfbff8c | 2006-03-21 09:41:28 +0000 | [diff] [blame] | 270 | rtype = asn1f_lookup_symbol(arg, arg->expr->module, |
| 271 | arg->expr->rhs_pspecs, |
| 272 | ct->containedSubtype->value.reference); |
| 273 | if(!rtype) { |
| 274 | FATAL("Cannot find type \"%s\" in constraints " |
| 275 | "at line %d", |
| 276 | asn1f_printable_value(ct->containedSubtype), |
| 277 | ct->_lineno); |
| 278 | return -1; |
| 279 | } |
| 280 | |
| 281 | tmparg = *arg; |
| 282 | tmparg.expr = rtype; |
| 283 | tmparg.mod = rtype->module; |
| 284 | ret = asn1constraint_pullup(&tmparg); |
| 285 | if(ret) return ret; |
| 286 | |
| 287 | ct_expr = rtype->combined_constraints; |
| 288 | if(!ct_expr) return 0; |
| 289 | } else { |
| 290 | FATAL("Unsupported feature at line %d", ct->_lineno); |
vlm | 53832f1 | 2005-03-20 12:57:21 +0000 | [diff] [blame] | 291 | return -1; |
| 292 | } |
| 293 | |
vlm | dfbff8c | 2006-03-21 09:41:28 +0000 | [diff] [blame] | 294 | ct_expr = asn1p_constraint_clone(ct_expr); |
| 295 | assert(ct_expr); |
vlm | 53832f1 | 2005-03-20 12:57:21 +0000 | [diff] [blame] | 296 | |
vlm | da31348 | 2006-09-21 02:21:53 +0000 | [diff] [blame] | 297 | _remove_extensions(arg, ct_expr, 0); |
vlm | 53832f1 | 2005-03-20 12:57:21 +0000 | [diff] [blame] | 298 | |
vlm | dfbff8c | 2006-03-21 09:41:28 +0000 | [diff] [blame] | 299 | if(ct_expr->type == ACT_CA_SET) { |
| 300 | unsigned int i; |
| 301 | for(i = 0; i < ct_expr->el_count; i++) { |
| 302 | if(asn1p_constraint_insert( |
| 303 | ct, ct_expr->elements[i])) { |
| 304 | asn1p_constraint_free(ct_expr); |
| 305 | return -1; |
| 306 | } else { |
| 307 | ct_expr->elements[i] = 0; |
vlm | 53832f1 | 2005-03-20 12:57:21 +0000 | [diff] [blame] | 308 | } |
vlm | 53832f1 | 2005-03-20 12:57:21 +0000 | [diff] [blame] | 309 | } |
vlm | dfbff8c | 2006-03-21 09:41:28 +0000 | [diff] [blame] | 310 | asn1p_constraint_free(ct_expr); |
| 311 | } else { |
| 312 | ret = asn1p_constraint_insert(ct, ct_expr); |
| 313 | assert(ret == 0); |
vlm | 53832f1 | 2005-03-20 12:57:21 +0000 | [diff] [blame] | 314 | } |
| 315 | |
vlm | dfbff8c | 2006-03-21 09:41:28 +0000 | [diff] [blame] | 316 | ct->type = ACT_CA_SET; |
| 317 | asn1p_value_free(ct->containedSubtype); |
| 318 | ct->containedSubtype = NULL; |
| 319 | |
vlm | 53832f1 | 2005-03-20 12:57:21 +0000 | [diff] [blame] | 320 | return 0; |
| 321 | } |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 322 | |
| 323 | static int |
vlm | 53832f1 | 2005-03-20 12:57:21 +0000 | [diff] [blame] | 324 | constraint_value_resolve(arg_t *arg, |
vlm | 8fba257 | 2005-03-10 11:27:13 +0000 | [diff] [blame] | 325 | asn1p_value_t **value, enum asn1p_constraint_type_e real_ctype) { |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 326 | asn1p_expr_t static_expr; |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 327 | arg_t tmp_arg; |
| 328 | int rvalue = 0; |
| 329 | int ret; |
| 330 | |
vlm | 8fba257 | 2005-03-10 11:27:13 +0000 | [diff] [blame] | 331 | DEBUG("(\"%s\", within <%s>)", |
| 332 | asn1f_printable_value(*value), |
| 333 | asn1p_constraint_type2str(real_ctype)); |
| 334 | |
| 335 | static_expr = *arg->expr; |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 336 | static_expr.value = *value; |
vlm | 8fba257 | 2005-03-10 11:27:13 +0000 | [diff] [blame] | 337 | static_expr.meta_type = AMT_VALUE; |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 338 | tmp_arg = *arg; |
vlm | 8fba257 | 2005-03-10 11:27:13 +0000 | [diff] [blame] | 339 | tmp_arg.mod = arg->expr->module; |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 340 | tmp_arg.expr = &static_expr; |
vlm | 8fba257 | 2005-03-10 11:27:13 +0000 | [diff] [blame] | 341 | ret = asn1f_value_resolve(&tmp_arg, &static_expr, &real_ctype); |
vlm | b5be8c3 | 2004-08-18 05:42:05 +0000 | [diff] [blame] | 342 | RET2RVAL(ret, rvalue); |
| 343 | assert(static_expr.value); |
| 344 | *value = static_expr.value; |
| 345 | |
| 346 | return rvalue; |
| 347 | } |
vlm | 53832f1 | 2005-03-20 12:57:21 +0000 | [diff] [blame] | 348 | |