Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | #include "asn1fix_internal.h" |
| 2 | |
Lev Walkin | ef4a32b | 2006-08-28 02:12:49 +0000 | [diff] [blame] | 3 | static int asn1f_fix_bit_string_type(arg_t *arg); |
| 4 | static int asn1f_fix_bit_string_value(arg_t *arg, asn1p_expr_t *ttype); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 5 | static void asn1f_BS_remove_trailing_zero_bits(asn1p_value_t *value); |
| 6 | static int asn1f_BS_unparsed_convert(arg_t *arg, asn1p_value_t *value, asn1p_expr_t *ttype); |
| 7 | |
| 8 | int |
| 9 | asn1f_fix_bit_string(arg_t *arg) { |
| 10 | asn1p_expr_t *expr = arg->expr; |
| 11 | int r_value = 0; |
| 12 | int ret; |
| 13 | |
| 14 | if(expr->meta_type == AMT_VALUE) { |
| 15 | asn1p_expr_t *ttype; |
| 16 | |
Lev Walkin | 0385018 | 2005-03-10 10:02:50 +0000 | [diff] [blame] | 17 | DEBUG("(%s) for line %d", expr->Identifier, expr->_lineno); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 18 | |
Lev Walkin | 6fec44d | 2004-08-22 03:10:23 +0000 | [diff] [blame] | 19 | ttype = asn1f_find_terminal_type(arg, expr); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 20 | if(ttype && ttype->expr_type == ASN_BASIC_BIT_STRING) { |
| 21 | ret = asn1f_fix_bit_string_value(arg, ttype); |
| 22 | RET2RVAL(ret, r_value); |
| 23 | } |
| 24 | } |
| 25 | |
Lev Walkin | ef4a32b | 2006-08-28 02:12:49 +0000 | [diff] [blame] | 26 | if(expr->meta_type == AMT_TYPE |
| 27 | && expr->expr_type == ASN_BASIC_BIT_STRING) { |
| 28 | ret = asn1f_fix_bit_string_type(arg); |
| 29 | RET2RVAL(ret, r_value); |
| 30 | } |
| 31 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 32 | return r_value; |
| 33 | } |
| 34 | |
Lev Walkin | fbfc7bc | 2006-08-28 02:45:44 +0000 | [diff] [blame] | 35 | static int _compare_value(asn1p_expr_t *expr1, asn1p_expr_t *expr2) { |
| 36 | return expr2->value->value.v_integer - expr1->value->value.v_integer; |
| 37 | } |
| 38 | |
Lev Walkin | ef4a32b | 2006-08-28 02:12:49 +0000 | [diff] [blame] | 39 | static int |
| 40 | asn1f_fix_bit_string_type(arg_t *arg) { |
| 41 | asn1p_expr_t *expr = arg->expr; |
| 42 | asn1p_expr_t *v; |
Lev Walkin | a0c9290 | 2006-08-28 02:24:24 +0000 | [diff] [blame] | 43 | int r_value = 0; |
| 44 | int ret; |
Lev Walkin | ef4a32b | 2006-08-28 02:12:49 +0000 | [diff] [blame] | 45 | |
| 46 | TQ_FOR(v, &(expr->members), next) { |
Lev Walkin | a0c9290 | 2006-08-28 02:24:24 +0000 | [diff] [blame] | 47 | if(v->expr_type == A1TC_EXTENSIBLE) { |
| 48 | FATAL("Extension marker (...) is not allowed " |
| 49 | "as a BIT STRING NamedBit at line %d ", |
| 50 | v->_lineno); |
| 51 | return -1; |
| 52 | } |
Lev Walkin | ef4a32b | 2006-08-28 02:12:49 +0000 | [diff] [blame] | 53 | if(v->expr_type != A1TC_UNIVERVAL) { |
| 54 | FATAL("BIT STRING value at line %d " |
Lev Walkin | a0c9290 | 2006-08-28 02:24:24 +0000 | [diff] [blame] | 55 | "is not an identifier", v->_lineno); |
Lev Walkin | ef4a32b | 2006-08-28 02:12:49 +0000 | [diff] [blame] | 56 | return -1; |
| 57 | } |
Lev Walkin | ceac154 | 2006-08-28 02:30:41 +0000 | [diff] [blame] | 58 | |
| 59 | /* 21.1 */ |
| 60 | if(v->value == NULL) { |
| 61 | FATAL("BIT STRING NamedBit value at line %d " |
| 62 | "must be explicitly specified in braces", |
| 63 | v->_lineno); |
| 64 | return -1; |
| 65 | } else if(v->value->type == ATV_REFERENCED) { |
| 66 | /* Resolve the value */ |
| 67 | if(asn1f_value_resolve(arg, v, 0)) |
| 68 | return -1; |
| 69 | } |
| 70 | if(v->value->type != ATV_INTEGER |
| 71 | || v->value->value.v_integer < 0) { |
| 72 | FATAL("BIT STRING NamedBit value at line %d: " |
| 73 | "non-negative integer value expected", |
| 74 | v->_lineno); |
| 75 | return -1; |
| 76 | } |
Lev Walkin | fbfc7bc | 2006-08-28 02:45:44 +0000 | [diff] [blame] | 77 | |
| 78 | /* Check value uniqueness as per 21.4 */ |
| 79 | ret = asn1f_check_unique_expr_child(arg, v, |
| 80 | _compare_value, "value"); |
| 81 | RET2RVAL(ret, r_value); |
| 82 | /* Check identifier uniqueness as per 21.5 */ |
| 83 | ret = asn1f_check_unique_expr_child(arg, v, 0, "identifier"); |
| 84 | RET2RVAL(ret, r_value); |
Lev Walkin | ef4a32b | 2006-08-28 02:12:49 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Lev Walkin | a0c9290 | 2006-08-28 02:24:24 +0000 | [diff] [blame] | 87 | return r_value; |
Lev Walkin | ef4a32b | 2006-08-28 02:12:49 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | static int |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 91 | asn1f_fix_bit_string_value(arg_t *arg, asn1p_expr_t *ttype) { |
| 92 | asn1p_expr_t *expr = arg->expr; |
| 93 | int r_value = 0; |
| 94 | |
Lev Walkin | 0385018 | 2005-03-10 10:02:50 +0000 | [diff] [blame] | 95 | DEBUG("(%s) for line %d", |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 96 | expr->Identifier, expr->_lineno); |
| 97 | |
| 98 | switch(expr->value->type) { |
| 99 | case ATV_UNPARSED: |
| 100 | /* |
| 101 | * Most definitely we have something like |
| 102 | * value BitStringType1 ::= { a, b, c } |
| 103 | * which could not be parsed by the LALR parser, mostly |
| 104 | * because it requires knowledge about BitStringType1 |
| 105 | * during the parsing. So, here's a little hack: we create |
| 106 | * a buffer containing the full specification of a module, |
| 107 | * which contains some pre-defined INTEGER type with the |
| 108 | * opaque definition "{ a, b, c }" from the bit string. |
| 109 | */ |
| 110 | if(asn1f_BS_unparsed_convert(arg, expr->value, ttype)) { |
| 111 | r_value = -1; |
| 112 | break; |
| 113 | } |
| 114 | /* Fall through: remove trailing zero bits */ |
| 115 | case ATV_BITVECTOR: |
| 116 | asn1f_BS_remove_trailing_zero_bits(expr->value); |
| 117 | break; |
| 118 | default: |
| 119 | break; |
| 120 | } |
| 121 | |
| 122 | return r_value; |
| 123 | } |
| 124 | |
| 125 | static void |
| 126 | asn1f_BS_remove_trailing_zero_bits(asn1p_value_t *value) { |
| 127 | int lmfb = -1; /* Last meaningful byte position */ |
| 128 | int bits; /* Number of bits in the BIT STRING value */ |
| 129 | int b; |
| 130 | |
| 131 | assert(value->type == ATV_BITVECTOR); |
| 132 | |
| 133 | bits = value->value.binary_vector.size_in_bits; |
| 134 | /* |
| 135 | * Figure out the rightmost meaningful byte. |
| 136 | */ |
| 137 | for(b = 0; b < ((bits + 7) >> 3); b++) { |
| 138 | uint8_t uc = value->value.binary_vector.bits[b]; |
| 139 | if(uc && b > lmfb) |
| 140 | lmfb = b; |
| 141 | } |
| 142 | if(lmfb == -1) { |
| 143 | bits = 0; |
| 144 | } else { |
| 145 | uint8_t uc; |
| 146 | uc = value->value.binary_vector.bits[lmfb]; |
| 147 | bits = (lmfb+1) * 8; |
| 148 | /* |
| 149 | * Squeeze the bit string width until the rightmost |
| 150 | * bit is set. |
| 151 | */ |
| 152 | for(; uc && (uc & 1) == 0; uc >>= 1) |
| 153 | bits--; |
| 154 | if(uc == 0) { |
| 155 | bits = lmfb * 8; |
| 156 | } |
| 157 | } |
| 158 | value->value.binary_vector.size_in_bits = bits; |
| 159 | } |
| 160 | |
| 161 | static int |
| 162 | asn1f_BS_unparsed_convert(arg_t *arg, asn1p_value_t *value, asn1p_expr_t *ttype) { |
| 163 | asn1p_t *asn; |
| 164 | asn1p_module_t *mod; |
| 165 | asn1p_expr_t *V; |
| 166 | asn1p_expr_t *bit; |
Lev Walkin | b8108ec | 2004-09-29 13:17:17 +0000 | [diff] [blame] | 167 | asn1c_integer_t aI; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 168 | uint8_t *bitbuf; |
| 169 | int bits; |
| 170 | int psize; |
| 171 | char *p; |
| 172 | int ret; |
| 173 | int r_value = 0; |
| 174 | |
| 175 | assert(value->type == ATV_UNPARSED); |
| 176 | |
| 177 | psize = value->value.string.size + 64; |
| 178 | p = malloc(psize); |
| 179 | if(p == NULL) |
| 180 | return -1; |
| 181 | |
| 182 | ret = snprintf(p, psize, |
| 183 | "M DEFINITIONS ::=\nBEGIN\n" |
| 184 | "V ::= INTEGER %s\n" |
| 185 | "END\n", |
| 186 | value->value.string.buf |
| 187 | ); |
| 188 | assert(ret < psize); |
| 189 | psize = ret; |
| 190 | |
| 191 | asn = asn1p_parse_buffer(p, psize, A1P_NOFLAGS); |
| 192 | free(p); |
| 193 | if(asn == NULL) { |
| 194 | FATAL("Cannot parse BIT STRING value %s " |
| 195 | "defined as %s at line %d", |
| 196 | arg->expr->Identifier, |
| 197 | value->value.string.buf, |
| 198 | arg->expr->_lineno |
| 199 | ); |
| 200 | return -1; |
| 201 | } |
| 202 | |
| 203 | mod = TQ_FIRST(&(asn->modules)); |
| 204 | assert(mod); |
| 205 | V = TQ_FIRST(&(mod->members)); |
| 206 | assert(V); |
| 207 | assert(strcmp(V->Identifier, "V") == 0); |
| 208 | assert(TQ_FIRST(&(V->members))); |
| 209 | |
| 210 | /* |
| 211 | * Simple loop just to fetch the maximal bit position |
| 212 | * out of the BIT STRING value defined as NamedBitList. |
| 213 | */ |
| 214 | aI = -1; |
| 215 | TQ_FOR(bit, &(V->members), next) { |
| 216 | asn1p_expr_t *bitdef; |
| 217 | bitdef = asn1f_lookup_child(ttype, bit->Identifier); |
| 218 | if(bitdef && bitdef->value |
| 219 | && bitdef->value->type == ATV_INTEGER) { |
| 220 | if(bitdef->value->value.v_integer > aI) |
| 221 | aI = bitdef->value->value.v_integer; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | if(aI > 1024 * 1024 * 8) { /* One megabyte */ |
| 226 | FATAL("Unsupportedly large BIT STRING value \"%s\" " |
| 227 | "defined at line %d " |
| 228 | "(larger than 1MByte)", |
| 229 | arg->expr->Identifier, |
| 230 | arg->expr->_lineno |
| 231 | ); |
Lev Walkin | f4b1d7f | 2006-09-12 05:19:28 +0000 | [diff] [blame] | 232 | asn1p_delete(asn); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 233 | return -1; |
| 234 | } |
| 235 | |
| 236 | bits = aI + 1; /* Number of bits is more than a last bit position */ |
| 237 | bitbuf = calloc(1, 1 + ((bits + 7) / 8)); |
| 238 | if(bitbuf == NULL) { |
Lev Walkin | f4b1d7f | 2006-09-12 05:19:28 +0000 | [diff] [blame] | 239 | asn1p_delete(asn); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 240 | return -1; |
| 241 | } |
| 242 | |
| 243 | TQ_FOR(bit, &(V->members), next) { |
| 244 | asn1p_expr_t *bitdef; |
| 245 | int set_bit_pos; |
| 246 | |
| 247 | if(bit->value) { |
| 248 | WARNING("Identifier \"%s\" at line %d " |
| 249 | "must not have a value", |
| 250 | bit->Identifier, bit->_lineno); |
| 251 | RET2RVAL(1, r_value); |
| 252 | } |
| 253 | bitdef = asn1f_lookup_child(ttype, bit->Identifier); |
| 254 | if(bitdef == NULL) { |
| 255 | FATAL("Identifier \"%s\" at line %d is not defined " |
| 256 | "in the \"%s\" type definition at line %d", |
| 257 | bit->Identifier, |
| 258 | bit->_lineno, |
| 259 | ttype->Identifier, |
| 260 | ttype->_lineno |
| 261 | ); |
| 262 | RET2RVAL(-1, r_value); |
| 263 | continue; |
| 264 | } |
| 265 | if(bitdef->value == NULL |
| 266 | || bitdef->value->type != ATV_INTEGER) { |
| 267 | FATAL("Broken identifier " |
| 268 | "\"%s\" at line %d " |
| 269 | "referenced by \"%s\" at line %d", |
| 270 | bitdef->Identifier, |
| 271 | bitdef->_lineno, |
| 272 | arg->expr->Identifier, |
| 273 | arg->expr->_lineno |
| 274 | ); |
| 275 | RET2RVAL(-1, r_value); |
| 276 | continue; |
| 277 | } |
| 278 | |
| 279 | assert(bitdef->value->value.v_integer < bits); |
| 280 | set_bit_pos = bitdef->value->value.v_integer; |
| 281 | bitbuf[set_bit_pos>>3] |= 1 << (7-(set_bit_pos % 8)); |
| 282 | } |
| 283 | |
Lev Walkin | f4b1d7f | 2006-09-12 05:19:28 +0000 | [diff] [blame] | 284 | asn1p_delete(asn); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 285 | free(value->value.string.buf); |
| 286 | value->type = ATV_BITVECTOR; |
| 287 | value->value.binary_vector.bits = bitbuf; |
| 288 | value->value.binary_vector.size_in_bits = bits; |
| 289 | |
| 290 | return r_value; |
| 291 | } |