Lev Walkin | cb4cd5b | 2006-03-14 15:23:06 +0000 | [diff] [blame] | 1 | #include "asn1fix_internal.h" |
Lev Walkin | aa7f530 | 2006-03-14 15:53:59 +0000 | [diff] [blame] | 2 | #include "asn1fix_cws.h" |
Lev Walkin | 9d542d2 | 2006-03-14 16:31:37 +0000 | [diff] [blame] | 3 | |
Lev Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 4 | static int _asn1f_parse_class_object_data(arg_t *, asn1p_expr_t *eclass, |
| 5 | struct asn1p_ioc_row_s *row, asn1p_wsyntx_t *syntax, |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 6 | const uint8_t *buf, const uint8_t *bend, |
Lev Walkin | d357f3d | 2017-08-10 17:40:37 -0700 | [diff] [blame] | 7 | int optional_mode, const uint8_t **newpos, int counter); |
| 8 | static int _asn1f_assign_cell_value(arg_t *arg, struct asn1p_ioc_cell_s *cell, const uint8_t *buf, const uint8_t *bend, int counter); |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 9 | static asn1p_wsyntx_chunk_t *asn1f_next_literal_chunk(asn1p_wsyntx_t *syntax, asn1p_wsyntx_chunk_t *chunk, const uint8_t *buf); |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 10 | |
| 11 | int |
| 12 | asn1f_check_class_object(arg_t *arg) { |
| 13 | asn1p_expr_t *expr = arg->expr; |
| 14 | asn1p_expr_t *eclass; |
| 15 | asn1p_ioc_row_t *row; |
| 16 | int ret; |
| 17 | |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 18 | if(expr->meta_type != AMT_VALUESET |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 19 | || expr->expr_type != A1TC_REFERENCE |
| 20 | || !expr->value |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 21 | || expr->value->type != ATV_UNPARSED) { |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 22 | return 0; |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 23 | } |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 24 | |
| 25 | eclass = asn1f_find_terminal_type(arg, expr); |
| 26 | if(!eclass |
| 27 | || eclass->meta_type != AMT_OBJECTCLASS |
| 28 | || eclass->expr_type != A1TC_CLASSDEF) { |
| 29 | return 0; |
| 30 | } |
| 31 | |
| 32 | if(!eclass->with_syntax) { |
| 33 | DEBUG("Can't process classes without %s just yet", |
| 34 | "WITH SYNTAX"); |
| 35 | return 0; |
| 36 | } |
| 37 | |
| 38 | row = asn1p_ioc_row_new(eclass); |
| 39 | assert(row); |
| 40 | |
| 41 | ret = _asn1f_parse_class_object_data(arg, eclass, row, |
| 42 | eclass->with_syntax, |
| 43 | expr->value->value.string.buf + 1, |
| 44 | expr->value->value.string.buf |
| 45 | + expr->value->value.string.size - 1, |
Lev Walkin | d357f3d | 2017-08-10 17:40:37 -0700 | [diff] [blame] | 46 | 0, 0, 0); |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 47 | |
| 48 | asn1p_ioc_row_delete(row); |
| 49 | |
| 50 | return ret; |
| 51 | } |
Lev Walkin | 9d542d2 | 2006-03-14 16:31:37 +0000 | [diff] [blame] | 52 | |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 53 | static int |
Lev Walkin | 4dcf836 | 2017-08-07 20:10:05 -0700 | [diff] [blame] | 54 | _asn1f_is_ioc_row_duplicate(asn1p_ioc_table_t *it, asn1p_ioc_row_t *row) { |
| 55 | if(!it) { |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | for(size_t i = 0; i < it->rows; i++) { |
| 60 | switch(asn1p_ioc_row_match(it->row[i], row)) { |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 61 | default: |
| 62 | case -1: |
| 63 | return -1; |
| 64 | case 1: |
| 65 | continue; |
| 66 | case 0: |
| 67 | return 1; /* Duplicate! */ |
| 68 | } |
| 69 | } |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | struct parse_object_key { |
| 74 | arg_t *arg; |
| 75 | asn1p_expr_t *expr; /* InformationObjectSet */ |
| 76 | asn1p_expr_t *eclass; /* CLASS */ |
Lev Walkin | d357f3d | 2017-08-10 17:40:37 -0700 | [diff] [blame] | 77 | int sequence; /* Sequence counter */ |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
Lev Walkin | 4dcf836 | 2017-08-07 20:10:05 -0700 | [diff] [blame] | 80 | /* |
| 81 | * Add to the IoC table if the row is unique. |
| 82 | */ |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 83 | static int |
Lev Walkin | 4dcf836 | 2017-08-07 20:10:05 -0700 | [diff] [blame] | 84 | _asn1f_add_unique_row(arg_t *arg, asn1p_expr_t *expr, asn1p_ioc_row_t *row) { |
Lev Walkin | 53a28a2 | 2017-08-23 09:56:53 -0700 | [diff] [blame] | 85 | assert(expr->ioc_table); |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 86 | |
Lev Walkin | 53a28a2 | 2017-08-23 09:56:53 -0700 | [diff] [blame] | 87 | /* Look for duplicates */ |
| 88 | switch(_asn1f_is_ioc_row_duplicate(expr->ioc_table, row)) { |
| 89 | case -1: |
| 90 | DEBUG("Found Information Object Duplicate in %s", expr->Identifier, |
| 91 | expr->_lineno); |
| 92 | return -1; |
| 93 | case 0: |
| 94 | /* Not a duplicate */ |
| 95 | break; |
| 96 | case 1: |
| 97 | /* Proper duplicate detected; ignore */ |
| 98 | asn1p_ioc_row_delete(row); |
| 99 | return 0; |
Lev Walkin | 4dcf836 | 2017-08-07 20:10:05 -0700 | [diff] [blame] | 100 | } |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 101 | |
Lev Walkin | 4dcf836 | 2017-08-07 20:10:05 -0700 | [diff] [blame] | 102 | asn1p_ioc_table_add(expr->ioc_table, row); |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * Given a single blob of unparsed Information Object specification, parse it |
| 109 | * into a given InformationObjectSet. |
| 110 | */ |
| 111 | static int |
| 112 | _asn1f_parse_object_cb(const uint8_t *buf, size_t size, void *keyp) { |
| 113 | struct parse_object_key *key = keyp; |
| 114 | arg_t *arg = key->arg; |
| 115 | asn1p_expr_t *expr = key->expr; |
| 116 | asn1p_expr_t *eclass = key->eclass; |
| 117 | asn1p_ioc_row_t *row; |
| 118 | int ret; |
| 119 | |
Lev Walkin | d357f3d | 2017-08-10 17:40:37 -0700 | [diff] [blame] | 120 | key->sequence++; |
| 121 | |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 122 | row = asn1p_ioc_row_new(eclass); |
| 123 | assert(row); |
| 124 | |
| 125 | ret = _asn1f_parse_class_object_data(arg, eclass, row, eclass->with_syntax, |
Lev Walkin | d357f3d | 2017-08-10 17:40:37 -0700 | [diff] [blame] | 126 | buf, buf + size, 0, 0, key->sequence); |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 127 | if(ret) { |
Lev Walkin | 48e82d1 | 2017-10-19 03:06:35 -0700 | [diff] [blame] | 128 | LOG(ret, "Cannot parse %s of CLASS %s found at line %d", |
| 129 | expr->Identifier, eclass->Identifier, expr->_lineno); |
| 130 | asn1p_ioc_row_delete(row); |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 131 | return ret; |
| 132 | } |
| 133 | |
| 134 | /* Add object to a CLASS. */ |
Lev Walkin | 4dcf836 | 2017-08-07 20:10:05 -0700 | [diff] [blame] | 135 | if(_asn1f_add_unique_row(arg, eclass, row) != 0) |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 136 | return -1; |
| 137 | |
| 138 | /* |
| 139 | * Add a copy of the object to the Information Object Set. |
| 140 | */ |
| 141 | row = asn1p_ioc_row_new(eclass); |
| 142 | assert(row); |
| 143 | ret = _asn1f_parse_class_object_data(arg, eclass, row, eclass->with_syntax, |
Lev Walkin | d357f3d | 2017-08-10 17:40:37 -0700 | [diff] [blame] | 144 | buf, buf + size, 0, 0, key->sequence); |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 145 | assert(ret == 0); |
| 146 | |
Lev Walkin | 4dcf836 | 2017-08-07 20:10:05 -0700 | [diff] [blame] | 147 | if(_asn1f_add_unique_row(arg, expr, row) != 0) |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 148 | return -1; |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | static int |
| 154 | _asn1f_foreach_unparsed_union(const asn1p_constraint_t *ct_union, |
| 155 | int (*process)(const uint8_t *buf, size_t size, |
| 156 | void *key), |
| 157 | void *key) { |
| 158 | assert(ct_union->type == ACT_CA_UNI); |
| 159 | |
| 160 | for(size_t j = 0; j < ct_union->el_count; j++) { |
| 161 | const asn1p_constraint_t *ct2 = ct_union->elements[j]; |
| 162 | if(ct2->type == ACT_EL_VALUE && ct2->value->type == ATV_UNPARSED) { |
| 163 | if(process |
| 164 | && process(ct2->value->value.string.buf + 1, |
| 165 | ct2->value->value.string.size - 2, key) |
| 166 | != 0) { |
| 167 | return -1; |
| 168 | } |
| 169 | continue; |
| 170 | } |
| 171 | return -1; |
| 172 | } |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | static int |
Lev Walkin | 53a28a2 | 2017-08-23 09:56:53 -0700 | [diff] [blame] | 178 | _asn1f_foreach_unparsed(arg_t *arg, const asn1p_constraint_t *ct, |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 179 | int (*process)(const uint8_t *buf, size_t size, |
| 180 | void *key), |
Lev Walkin | ade508c | 2017-08-23 10:19:30 -0700 | [diff] [blame] | 181 | void *keyp) { |
| 182 | struct parse_object_key *key = keyp; |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 183 | if(!ct) return -1; |
Lev Walkin | 53a28a2 | 2017-08-23 09:56:53 -0700 | [diff] [blame] | 184 | |
| 185 | switch(ct->type) { |
| 186 | default: |
Lev Walkin | ade508c | 2017-08-23 10:19:30 -0700 | [diff] [blame] | 187 | DEBUG("Constraint is of unknown type %d for CWS", ct->type); |
Lev Walkin | 53a28a2 | 2017-08-23 09:56:53 -0700 | [diff] [blame] | 188 | return -1; |
| 189 | case ACT_EL_EXT: /* ... */ |
Lev Walkin | ade508c | 2017-08-23 10:19:30 -0700 | [diff] [blame] | 190 | if(key) { |
| 191 | key->expr->ioc_table->extensible = 1; |
| 192 | } |
Lev Walkin | 53a28a2 | 2017-08-23 09:56:53 -0700 | [diff] [blame] | 193 | return 0; |
| 194 | case ACT_CA_UNI: /* | */ |
Lev Walkin | ade508c | 2017-08-23 10:19:30 -0700 | [diff] [blame] | 195 | return _asn1f_foreach_unparsed_union(ct, process, keyp); |
Lev Walkin | 53a28a2 | 2017-08-23 09:56:53 -0700 | [diff] [blame] | 196 | case ACT_CA_CSV: /* , */ |
| 197 | break; |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 198 | } |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 199 | |
| 200 | for(size_t i = 0; i < ct->el_count; i++) { |
| 201 | const asn1p_constraint_t *ct1 = ct->elements[i]; |
Lev Walkin | ade508c | 2017-08-23 10:19:30 -0700 | [diff] [blame] | 202 | if(_asn1f_foreach_unparsed(arg, ct1, process, keyp) != 0) { |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 203 | return -1; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | static int |
Lev Walkin | 53a28a2 | 2017-08-23 09:56:53 -0700 | [diff] [blame] | 211 | _asn1f_constraint_looks_like_object_set(arg_t *arg, |
| 212 | const asn1p_constraint_t *ct) { |
| 213 | return 0 == _asn1f_foreach_unparsed(arg, ct, NULL, NULL); |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Lev Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 216 | int |
| 217 | asn1f_parse_class_object(arg_t *arg) { |
| 218 | asn1p_expr_t *expr = arg->expr; |
| 219 | asn1p_expr_t *eclass; |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 220 | enum { |
| 221 | FROM_VALUE, |
| 222 | FROM_CONSTRAINT, |
| 223 | } source = FROM_VALUE; |
Lev Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 224 | |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 225 | if(expr->meta_type == AMT_VALUE |
| 226 | && expr->expr_type == A1TC_REFERENCE |
| 227 | && expr->value && expr->value->type == ATV_UNPARSED) { |
| 228 | source = FROM_VALUE; |
| 229 | } else if(expr->meta_type != AMT_VALUESET |
| 230 | || expr->expr_type != A1TC_REFERENCE) { |
| 231 | return 0; |
| 232 | } else if(expr->value && expr->value->type == ATV_UNPARSED) { |
| 233 | source = FROM_VALUE; |
| 234 | } else if(!expr->value) { |
Lev Walkin | 53a28a2 | 2017-08-23 09:56:53 -0700 | [diff] [blame] | 235 | if(_asn1f_constraint_looks_like_object_set(arg, expr->constraints)) { |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 236 | source = FROM_CONSTRAINT; |
| 237 | } else { |
| 238 | return 0; |
| 239 | } |
| 240 | } else { |
| 241 | return 0; |
| 242 | } |
Lev Walkin | 9d542d2 | 2006-03-14 16:31:37 +0000 | [diff] [blame] | 243 | |
Lev Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 244 | /* |
| 245 | * Find the governing class. |
| 246 | */ |
| 247 | eclass = asn1f_find_terminal_type(arg, expr); |
| 248 | if(!eclass |
| 249 | || eclass->meta_type != AMT_OBJECTCLASS |
| 250 | || eclass->expr_type != A1TC_CLASSDEF) { |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | DEBUG("Value %s of CLASS %s found at line %d", |
| 255 | expr->Identifier, eclass->Identifier, expr->_lineno); |
| 256 | |
| 257 | if(!eclass->with_syntax) { |
Lev Walkin | faf35a8 | 2006-03-16 13:37:03 +0000 | [diff] [blame] | 258 | DEBUG("Can't process classes without %s just yet", |
| 259 | "WITH SYNTAX"); |
Lev Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 260 | return 0; |
| 261 | } |
| 262 | |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 263 | struct parse_object_key key = { |
| 264 | .arg = arg, |
| 265 | .expr = expr, |
| 266 | .eclass = eclass, |
Lev Walkin | d357f3d | 2017-08-10 17:40:37 -0700 | [diff] [blame] | 267 | .sequence = 0 |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 268 | }; |
Lev Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 269 | |
Lev Walkin | 53a28a2 | 2017-08-23 09:56:53 -0700 | [diff] [blame] | 270 | if(!expr->ioc_table) { |
| 271 | expr->ioc_table = asn1p_ioc_table_new(); |
| 272 | } |
| 273 | |
| 274 | if(!eclass->ioc_table) { |
| 275 | eclass->ioc_table = asn1p_ioc_table_new(); |
| 276 | } |
| 277 | |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 278 | switch(source) { |
| 279 | case FROM_VALUE: |
| 280 | if(_asn1f_parse_object_cb(expr->value->value.string.buf + 1, |
| 281 | expr->value->value.string.size - 2, &key) |
| 282 | != 0) { |
| 283 | return -1; |
| 284 | } |
| 285 | break; |
| 286 | case FROM_CONSTRAINT: |
Lev Walkin | 53a28a2 | 2017-08-23 09:56:53 -0700 | [diff] [blame] | 287 | if(_asn1f_foreach_unparsed(arg, expr->constraints, |
| 288 | _asn1f_parse_object_cb, &key) |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 289 | != 0) { |
| 290 | return -1; |
| 291 | } |
| 292 | } |
Lev Walkin | 9d542d2 | 2006-03-14 16:31:37 +0000 | [diff] [blame] | 293 | |
| 294 | return 0; |
| 295 | } |
Lev Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 296 | |
| 297 | #define SKIPSPACES for(; buf < bend && isspace(*buf); buf++) |
| 298 | |
| 299 | static int |
| 300 | _asn1f_parse_class_object_data(arg_t *arg, asn1p_expr_t *eclass, |
| 301 | struct asn1p_ioc_row_s *row, asn1p_wsyntx_t *syntax, |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 302 | const uint8_t *buf, const uint8_t *bend, |
Lev Walkin | d357f3d | 2017-08-10 17:40:37 -0700 | [diff] [blame] | 303 | int optional_mode, const uint8_t **newpos, int counter) { |
Lev Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 304 | struct asn1p_wsyntx_chunk_s *chunk; |
| 305 | int ret; |
| 306 | |
| 307 | TQ_FOR(chunk, (&syntax->chunks), next) { |
| 308 | switch(chunk->type) { |
| 309 | case WC_LITERAL: { |
| 310 | int token_len = strlen(chunk->content.token); |
| 311 | SKIPSPACES; |
| 312 | if(bend - buf < token_len |
| 313 | || memcmp(buf, chunk->content.token, token_len)) { |
| 314 | if(!optional_mode) { |
| 315 | FATAL("While parsing object class value %s at line %d: Expected: \"%s\", found: \"%s\"", |
| 316 | arg->expr->Identifier, arg->expr->_lineno, chunk->content.token, buf); |
| 317 | } |
| 318 | if(newpos) *newpos = buf; |
| 319 | return -1; |
| 320 | } |
| 321 | buf += token_len; |
| 322 | } break; |
| 323 | case WC_WHITESPACE: break; /* Ignore whitespace */ |
| 324 | case WC_FIELD: { |
| 325 | struct asn1p_ioc_cell_s *cell; |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 326 | asn1p_wsyntx_chunk_t *next_literal; |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 327 | const uint8_t *buf_old = buf; |
| 328 | const uint8_t *p = 0; |
Lev Walkin | faf35a8 | 2006-03-16 13:37:03 +0000 | [diff] [blame] | 329 | |
| 330 | SKIPSPACES; |
| 331 | |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 332 | next_literal = asn1f_next_literal_chunk(syntax, chunk, buf); |
| 333 | if(!next_literal) { |
| 334 | p += (bend - p); |
| 335 | } else { |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 336 | p = (uint8_t *)strstr((const char *)buf, (const char *)next_literal->content.token); |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 337 | if(!p) { |
| 338 | if (!optional_mode) |
| 339 | FATAL("Next literal \"%s\" not found !", next_literal->content.token); |
| 340 | |
| 341 | if(newpos) *newpos = buf_old; |
| 342 | return -1; |
Lev Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 343 | } |
| 344 | } |
Lev Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 345 | cell = asn1p_ioc_row_cell_fetch(row, |
| 346 | chunk->content.token); |
| 347 | if(cell == NULL) { |
| 348 | FATAL("Field reference %s found in WITH SYNAX {} clause does not match actual field in Object Class %s", |
| 349 | chunk->content.token, |
| 350 | eclass->Identifier, eclass->_lineno); |
| 351 | if(newpos) *newpos = buf; |
| 352 | return -1; |
| 353 | } |
| 354 | DEBUG("Reference %s satisfied by %s (%d)", |
| 355 | chunk->content.token, |
| 356 | buf, p - buf); |
Lev Walkin | d357f3d | 2017-08-10 17:40:37 -0700 | [diff] [blame] | 357 | ret = _asn1f_assign_cell_value(arg, cell, buf, p, counter); |
Lev Walkin | dc4376d | 2006-03-16 11:04:55 +0000 | [diff] [blame] | 358 | if(ret) return ret; |
Lev Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 359 | buf = p; |
Lev Walkin | dc4376d | 2006-03-16 11:04:55 +0000 | [diff] [blame] | 360 | if(newpos) *newpos = buf; |
Lev Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 361 | } break; |
| 362 | case WC_OPTIONALGROUP: { |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 363 | const uint8_t *np = 0; |
Lev Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 364 | SKIPSPACES; |
| 365 | ret = _asn1f_parse_class_object_data(arg, eclass, row, |
Lev Walkin | d357f3d | 2017-08-10 17:40:37 -0700 | [diff] [blame] | 366 | chunk->content.syntax, buf, bend, 1, &np, counter); |
Lev Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 367 | if(newpos) *newpos = np; |
| 368 | if(ret && np != buf) |
| 369 | return ret; |
Lev Walkin | dc4376d | 2006-03-16 11:04:55 +0000 | [diff] [blame] | 370 | buf = np; |
Lev Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 371 | } break; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | |
| 376 | if(newpos) *newpos = buf; |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | |
| 381 | static int |
Lev Walkin | 4dcf836 | 2017-08-07 20:10:05 -0700 | [diff] [blame] | 382 | _asn1f_assign_cell_value(arg_t *arg, struct asn1p_ioc_cell_s *cell, |
Lev Walkin | d357f3d | 2017-08-10 17:40:37 -0700 | [diff] [blame] | 383 | const uint8_t *buf, const uint8_t *bend, int counter) { |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 384 | asn1p_expr_t *expr = (asn1p_expr_t *)NULL; |
Lev Walkin | d357f3d | 2017-08-10 17:40:37 -0700 | [diff] [blame] | 385 | char *mivr; /* Most Immediate Value Representation */ |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 386 | int new_ref = 1; |
| 387 | asn1p_t *asn; |
| 388 | asn1p_module_t *mod; |
| 389 | asn1p_expr_t *type_expr = (asn1p_expr_t *)NULL; |
| 390 | int i, ret = 0, psize; |
| 391 | char *pp; |
Lev Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 392 | |
| 393 | if((bend - buf) <= 0) { |
| 394 | FATAL("Assignment warning: empty string is being assigned into %s for %s at line %d", |
| 395 | cell->field->Identifier, |
| 396 | arg->expr->Identifier, arg->expr->_lineno); |
| 397 | return -1; |
| 398 | } |
| 399 | |
Lev Walkin | d357f3d | 2017-08-10 17:40:37 -0700 | [diff] [blame] | 400 | mivr = malloc(bend - buf + 1); |
| 401 | assert(mivr); |
| 402 | memcpy(mivr, buf, bend - buf); |
| 403 | mivr[bend - buf] = '\0'; |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 404 | /* remove trailing space */ |
Lev Walkin | d357f3d | 2017-08-10 17:40:37 -0700 | [diff] [blame] | 405 | for (i = bend - buf - 1; (i > 0) && isspace(mivr[i]); i--) |
| 406 | mivr[i] = '\0'; |
Lev Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 407 | |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 408 | /* This value 100 should be larger than following formatting string */ |
| 409 | psize = bend - buf + 100; |
| 410 | pp = malloc(psize); |
| 411 | if(pp == NULL) { |
Lev Walkin | d357f3d | 2017-08-10 17:40:37 -0700 | [diff] [blame] | 412 | free(mivr); |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 413 | return -1; |
| 414 | } |
Lev Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 415 | |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 416 | if(cell->field->expr_type == A1TC_CLASSFIELD_TFS) { |
| 417 | ret = snprintf(pp, psize, |
| 418 | "M DEFINITIONS ::=\nBEGIN\n" |
| 419 | "V ::= %s\n" |
| 420 | "END\n", |
Lev Walkin | d357f3d | 2017-08-10 17:40:37 -0700 | [diff] [blame] | 421 | mivr |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 422 | ); |
| 423 | } else if(cell->field->expr_type == A1TC_CLASSFIELD_FTVFS) { |
| 424 | type_expr = TQ_FIRST(&(cell->field->members)); |
| 425 | ret = snprintf(pp, psize, |
| 426 | "M DEFINITIONS ::=\nBEGIN\n" |
| 427 | "v %s ::= %s\n" |
| 428 | "END\n", |
| 429 | type_expr->reference ? |
| 430 | type_expr->reference->components[0].name : |
| 431 | _asn1p_expr_type2string(type_expr->expr_type), |
Lev Walkin | d357f3d | 2017-08-10 17:40:37 -0700 | [diff] [blame] | 432 | mivr |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 433 | ); |
Lev Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 434 | } else { |
Lev Walkin | d357f3d | 2017-08-10 17:40:37 -0700 | [diff] [blame] | 435 | WARNING("asn1c only be able to parse TypeFieldSpec and FixedTypeValueFieldSpec. Failed when parsing %s at line %d\n", mivr, arg->expr->_lineno); |
| 436 | free(mivr); |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 437 | free(pp); |
| 438 | return -1; |
| 439 | } |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 440 | DEBUG("ASN.1:\n\n%s\n", pp); |
Bi-Ruei, Chiu | 3dcf05b | 2017-05-04 21:45:05 +0800 | [diff] [blame] | 441 | |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 442 | assert(ret < psize); |
| 443 | psize = ret; |
| 444 | |
Lev Walkin | be518fa | 2017-09-07 02:05:28 -0700 | [diff] [blame] | 445 | asn = asn1p_parse_buffer(pp, psize, |
| 446 | arg->expr->module->source_file_name, arg->expr->_lineno, A1P_NOFLAGS); |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 447 | free(pp); |
| 448 | if(asn == NULL) { |
| 449 | FATAL("Cannot parse Setting token %s " |
| 450 | "at line %d", |
Lev Walkin | d357f3d | 2017-08-10 17:40:37 -0700 | [diff] [blame] | 451 | mivr, |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 452 | arg->expr->_lineno |
| 453 | ); |
Lev Walkin | d357f3d | 2017-08-10 17:40:37 -0700 | [diff] [blame] | 454 | free(mivr); |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 455 | return -1; |
| 456 | } else { |
| 457 | mod = TQ_FIRST(&(asn->modules)); |
| 458 | assert(mod); |
| 459 | expr = TQ_REMOVE(&(mod->members), next); |
| 460 | assert(expr); |
| 461 | |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 462 | expr->parent_expr = NULL; |
| 463 | asn1p_expr_set_source(expr, arg->expr->module, arg->expr->_lineno); |
Lev Walkin | d357f3d | 2017-08-10 17:40:37 -0700 | [diff] [blame] | 464 | expr->_type_unique_index = counter; |
| 465 | DEBUG("Parsed identifier %s, mivr [%s], reference [%s] value [%s]", |
| 466 | expr->Identifier, mivr, asn1p_ref_string(expr->reference), |
| 467 | asn1f_printable_value(expr->value)); |
| 468 | free(expr->Identifier); |
| 469 | if(expr->value) { |
| 470 | expr->Identifier = strdup(asn1f_printable_value(expr->value)); |
| 471 | } else if (expr->reference) { |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 472 | expr->Identifier = strdup(expr->reference->components[expr->reference->comp_count - 1].name); |
| 473 | } else { |
Lev Walkin | d357f3d | 2017-08-10 17:40:37 -0700 | [diff] [blame] | 474 | expr->Identifier = mivr; |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 475 | } |
| 476 | asn1p_delete(asn); |
| 477 | } |
| 478 | |
| 479 | if(expr->reference && |
Lev Walkin | c0e03b9 | 2017-08-22 01:48:23 -0700 | [diff] [blame] | 480 | !asn1f_lookup_symbol(arg, expr->rhs_pspecs, expr->reference)) { |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 481 | |
| 482 | asn1p_ref_free(expr->reference); |
| 483 | new_ref = 0; |
| 484 | expr->reference = type_expr->reference; |
| 485 | if (asn1f_value_resolve(arg, expr, 0)) { |
| 486 | expr->reference = 0; |
| 487 | asn1p_expr_free(expr); |
Lev Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 488 | FATAL("Cannot find %s referenced by %s at line %d", |
Lev Walkin | d357f3d | 2017-08-10 17:40:37 -0700 | [diff] [blame] | 489 | mivr, arg->expr->Identifier, |
Lev Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 490 | arg->expr->_lineno); |
Lev Walkin | d357f3d | 2017-08-10 17:40:37 -0700 | [diff] [blame] | 491 | free(mivr); |
Lev Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 492 | return -1; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | DEBUG("Field %s assignment of %s got %s", |
Lev Walkin | d357f3d | 2017-08-10 17:40:37 -0700 | [diff] [blame] | 497 | cell->field->Identifier, mivr, expr->Identifier); |
Lev Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 498 | |
| 499 | cell->value = expr; |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 500 | cell->new_ref = new_ref; |
Lev Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 501 | |
Lev Walkin | d357f3d | 2017-08-10 17:40:37 -0700 | [diff] [blame] | 502 | if(expr->Identifier != mivr) { |
| 503 | free(mivr); |
Lev Walkin | 4dcf836 | 2017-08-07 20:10:05 -0700 | [diff] [blame] | 504 | } |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 505 | |
Lev Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 506 | return 0; |
| 507 | } |
| 508 | |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 509 | static asn1p_wsyntx_chunk_t * |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 510 | asn1f_next_literal_chunk(asn1p_wsyntx_t *syntax, asn1p_wsyntx_chunk_t *chunk, const uint8_t *buf) |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 511 | { |
| 512 | asn1p_wsyntx_chunk_t *next_chunk; |
| 513 | |
| 514 | next_chunk = TQ_NEXT(chunk, next); |
| 515 | do { |
| 516 | if(next_chunk == (struct asn1p_wsyntx_chunk_s *)0) { |
| 517 | if(!syntax->parent) |
| 518 | break; |
| 519 | next_chunk = TQ_NEXT(syntax->parent, next); |
| 520 | } else if(next_chunk->type == WC_LITERAL) { |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 521 | if(strstr((const char *)buf, (char *)next_chunk->content.token)) |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 522 | break; |
| 523 | if(!syntax->parent) |
| 524 | break; |
| 525 | next_chunk = TQ_NEXT(syntax->parent, next); |
| 526 | } else if(next_chunk->type == WC_WHITESPACE) { |
| 527 | next_chunk = TQ_NEXT(next_chunk, next); |
| 528 | } else if(next_chunk->type == WC_OPTIONALGROUP) { |
| 529 | syntax = next_chunk->content.syntax; |
| 530 | next_chunk = TQ_FIRST(((&next_chunk->content.syntax->chunks))); |
| 531 | } else |
| 532 | break; |
| 533 | } while (next_chunk); |
| 534 | |
| 535 | return next_chunk; |
| 536 | } |