Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | #include "asn1fix_internal.h" |
| 2 | |
| 3 | /* |
| 4 | * Check the validity of an enumeration. |
| 5 | */ |
| 6 | int |
| 7 | asn1f_fix_enum(arg_t *arg) { |
| 8 | asn1p_expr_t *expr = arg->expr; |
| 9 | asn1p_expr_t *ev; |
Lev Walkin | b8108ec | 2004-09-29 13:17:17 +0000 | [diff] [blame] | 10 | asn1c_integer_t max_value = -1; |
Lev Walkin | ffe79f4 | 2010-10-25 00:10:34 -0700 | [diff] [blame] | 11 | asn1c_integer_t max_value_ext = -1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 12 | int rvalue = 0; |
| 13 | asn1p_expr_t *ext_marker = NULL; /* "..." position */ |
| 14 | int ret; |
| 15 | |
Lev Walkin | ffe79f4 | 2010-10-25 00:10:34 -0700 | [diff] [blame] | 16 | /* Keep track of value collisions */ |
| 17 | asn1c_integer_t *used_vals; |
| 18 | int used_vals_sz = 50; |
| 19 | int used_vals_next = 0; |
| 20 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 21 | if(expr->expr_type != ASN_BASIC_ENUMERATED) |
| 22 | return 0; /* Just ignore it */ |
| 23 | |
Lev Walkin | 0385018 | 2005-03-10 10:02:50 +0000 | [diff] [blame] | 24 | DEBUG("(%s)", expr->Identifier); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 25 | |
Lev Walkin | ffe79f4 | 2010-10-25 00:10:34 -0700 | [diff] [blame] | 26 | used_vals = (asn1c_integer_t *) malloc(sizeof(asn1c_integer_t) * used_vals_sz); |
| 27 | if (!used_vals) { |
| 28 | FATAL("Out of memory"); |
| 29 | return -1; |
| 30 | } |
| 31 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 32 | /* |
| 33 | * 1. Scan the enumeration values in search for inconsistencies. |
| 34 | */ |
| 35 | TQ_FOR(ev, &(expr->members), next) { |
Lev Walkin | b8108ec | 2004-09-29 13:17:17 +0000 | [diff] [blame] | 36 | asn1c_integer_t eval; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 37 | |
| 38 | if(ev->value) |
| 39 | DEBUG("\tItem %s(%s)", ev->Identifier, |
| 40 | asn1f_printable_value(ev->value)); |
| 41 | else |
| 42 | DEBUG("\tItem %s", ev->Identifier); |
| 43 | |
| 44 | /* |
| 45 | * 1.1 Found an extension mark "...", check correctness. |
| 46 | */ |
| 47 | if(ev->expr_type == A1TC_EXTENSIBLE) { |
| 48 | if(ext_marker) { |
Lev Walkin | a0c9290 | 2006-08-28 02:24:24 +0000 | [diff] [blame] | 49 | FATAL("Enumeration %s at line %d: " |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 50 | "Second extension marker is not allowed", |
| 51 | expr->Identifier, |
| 52 | ev->_lineno); |
| 53 | rvalue = -1; |
| 54 | } else { |
| 55 | /* |
| 56 | * Remember the marker's position. |
| 57 | */ |
| 58 | ext_marker = ev; |
| 59 | } |
| 60 | continue; |
| 61 | } else if(ev->Identifier == NULL |
| 62 | || ev->expr_type != A1TC_UNIVERVAL) { |
| 63 | FATAL( |
| 64 | "Enumeration %s at line %d: " |
| 65 | "Unsupported enumeration element %s", |
| 66 | expr->Identifier, |
| 67 | ev->_lineno, |
| 68 | ev->Identifier?ev->Identifier:"<anonymous>"); |
| 69 | rvalue = -1; |
| 70 | continue; |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | * 1.2 Compute the value of the enumeration element. |
| 75 | */ |
| 76 | if(ev->value) { |
| 77 | switch(ev->value->type) { |
| 78 | case ATV_INTEGER: |
| 79 | eval = ev->value->value.v_integer; |
| 80 | break; |
| 81 | case ATV_REFERENCED: |
| 82 | FATAL("HERE HERE HERE", 1); |
| 83 | rvalue = -1; |
| 84 | continue; |
| 85 | break; |
| 86 | default: |
| 87 | FATAL("ENUMERATED type %s at line %d " |
| 88 | "contain element %s(%s) at line %d", |
| 89 | expr->Identifier, expr->_lineno, |
| 90 | ev->Identifier, |
| 91 | asn1f_printable_value(ev->value), |
| 92 | ev->_lineno); |
| 93 | rvalue = -1; |
| 94 | continue; |
| 95 | } |
| 96 | } else { |
| 97 | eval = max_value + 1; |
| 98 | ev->value = asn1p_value_fromint(eval); |
| 99 | if(ev->value == NULL) { |
| 100 | rvalue = -1; |
| 101 | continue; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /* |
| 106 | * 1.3 Check the applicability of this value. |
| 107 | */ |
Lev Walkin | ffe79f4 | 2010-10-25 00:10:34 -0700 | [diff] [blame] | 108 | |
| 109 | /* |
| 110 | * Enumeration is allowed to be unordered |
| 111 | * before the first marker, but after the marker |
| 112 | * the values must be ordered. |
| 113 | */ |
| 114 | if (ext_marker) { |
| 115 | if (eval > max_value_ext) { |
| 116 | max_value_ext = eval; |
| 117 | } else { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 118 | FATAL( |
| 119 | "Enumeration %s at line %d: " |
Lev Walkin | 33c16ba | 2004-09-24 21:01:43 +0000 | [diff] [blame] | 120 | "Explicit value \"%s(%" PRIdASN ")\" " |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 121 | "is not greater " |
Lev Walkin | 33c16ba | 2004-09-24 21:01:43 +0000 | [diff] [blame] | 122 | "than previous values (max %" PRIdASN ")", |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 123 | expr->Identifier, |
| 124 | ev->_lineno, |
| 125 | ev->Identifier, |
| 126 | eval, |
Lev Walkin | ffe79f4 | 2010-10-25 00:10:34 -0700 | [diff] [blame] | 127 | max_value_ext); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 128 | rvalue = -1; |
| 129 | } |
Lev Walkin | ffe79f4 | 2010-10-25 00:10:34 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | if (eval > max_value) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 133 | max_value = eval; |
| 134 | } |
| 135 | |
Lev Walkin | ffe79f4 | 2010-10-25 00:10:34 -0700 | [diff] [blame] | 136 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 137 | /* |
Lev Walkin | ffe79f4 | 2010-10-25 00:10:34 -0700 | [diff] [blame] | 138 | * 1.4 Check that all identifiers are unique |
| 139 | */ |
| 140 | int unique = 1; |
| 141 | int uv_idx; |
| 142 | for (uv_idx = 0; uv_idx < used_vals_next; uv_idx++) { |
| 143 | if (used_vals[uv_idx] == eval) { |
| 144 | FATAL( |
| 145 | "Enumeration %s at line %d: " |
| 146 | "Explicit value \"%s(%" PRIdASN ")\" " |
| 147 | "collides with previous values", |
| 148 | expr->Identifier, |
| 149 | ev->_lineno, |
| 150 | ev->Identifier, |
| 151 | eval); |
| 152 | rvalue = -1; |
| 153 | unique = 0; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | if (unique) { |
| 158 | /* Grow the array if needed */ |
| 159 | if (used_vals_next >= used_vals_sz) { |
| 160 | asn1c_integer_t *temp; |
| 161 | int new_sz = used_vals_sz + 50; |
| 162 | temp = (asn1c_integer_t *) realloc(used_vals, |
| 163 | sizeof(asn1c_integer_t) * new_sz); |
| 164 | if (!temp) return -1; |
| 165 | used_vals = temp; |
| 166 | used_vals_sz = new_sz; |
| 167 | } |
| 168 | used_vals[used_vals_next++] = eval; |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | * 1.5 Check that all identifiers before the current one |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 173 | * differs from it. |
| 174 | */ |
Lev Walkin | fbfc7bc | 2006-08-28 02:45:44 +0000 | [diff] [blame] | 175 | ret = asn1f_check_unique_expr_child(arg, ev, 0, "identifier"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 176 | RET2RVAL(ret, rvalue); |
| 177 | } |
| 178 | |
Lev Walkin | ffe79f4 | 2010-10-25 00:10:34 -0700 | [diff] [blame] | 179 | free(used_vals); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 180 | |
| 181 | /* |
| 182 | * 2. Reorder the first half (before optional "...") of the |
| 183 | * identifiers alphabetically. |
| 184 | */ |
| 185 | // TODO |
| 186 | |
| 187 | return rvalue; |
| 188 | } |
| 189 | |