| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/common/byte_buf.h> |
| 7 |
#include <aws/common/encoding.h> |
| 8 |
#include <aws/common/hash_table.h> |
| 9 |
#include <aws/sdkutils/endpoints_bdd_engine.h> |
| 10 |
#include <aws/sdkutils/partitions.h> |
| 11 |
#include <aws/sdkutils/private/endpoints_types_impl.h> |
| 12 |
#include <aws/sdkutils/private/endpoints_util.h> |
| 13 |
|
| 14 |
#define BDD_MAGIC_NUMBER 0x45504452 |
| 15 |
|
| 16 |
enum bdd_opcode { |
| 17 |
BDD_OP_PARAM_STRING = 0x01, |
| 18 |
BDD_OP_PARAM_BOOL = 0x02, |
| 19 |
BDD_OP_PARAM_STRING_ARRAY = 0x03, |
| 20 |
BDD_OP_CONDITION = 0x10, |
| 21 |
BDD_OP_RESULT_ENDPOINT = 0x20, |
| 22 |
BDD_OP_RESULT_ERROR = 0x21, |
| 23 |
}; |
| 24 |
|
| 25 |
|
| 26 |
static void s_endpoints_bdd_engine_destroy(void *data); |
| 27 |
|
| 28 |
static void s_on_condition_array_element_clean_up(void *element) { |
| 29 |
struct aws_endpoints_condition *condition = element; |
| 30 |
aws_endpoints_condition_clean_up(condition); |
| 31 |
} |
| 32 |
|
| 33 |
static void s_on_resutls_array_element_clean_up(void *element) { |
| 34 |
struct aws_endpoints_bdd_result *result = element; |
| 35 |
switch (result->type) { |
| 36 |
case AWS_ENDPOINTS_RESOLVED_ENDPOINT: |
| 37 |
aws_endpoints_rule_data_endpoint_clean_up(&result->data.endpoint); |
| 38 |
break; |
| 39 |
case AWS_ENDPOINTS_RESOLVED_ERROR: |
| 40 |
aws_endpoints_rule_data_error_clean_up(&result->data.error); |
| 41 |
break; |
| 42 |
default: |
| 43 |
AWS_FATAL_ASSERT(false); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
static int s_validate_magic_number(struct aws_byte_cursor *cursor) { |
| 48 |
uint32_t magic; |
| 49 |
if (!aws_byte_cursor_read_le32(cursor, &magic)) { |
| 50 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 51 |
} |
| 52 |
if (magic != BDD_MAGIC_NUMBER) { |
| 53 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 54 |
} |
| 55 |
return AWS_OP_SUCCESS; |
| 56 |
} |
| 57 |
|
| 58 |
static int s_load_string_table(struct aws_byte_cursor *cursor, struct aws_byte_cursor *out_blob) { |
| 59 |
uint32_t blob_size; |
| 60 |
if (!aws_byte_cursor_read_le32(cursor, &blob_size)) { |
| 61 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 62 |
} |
| 63 |
if (cursor->len < blob_size) { |
| 64 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 65 |
} |
| 66 |
*out_blob = aws_byte_cursor_from_array(cursor->ptr, blob_size); |
| 67 |
aws_byte_cursor_advance(cursor, blob_size); |
| 68 |
return AWS_OP_SUCCESS; |
| 69 |
} |
| 70 |
|
| 71 |
static int s_read_string_ref(struct aws_byte_cursor *cursor, struct aws_byte_cursor blob, struct aws_byte_cursor *out) { |
| 72 |
uint16_t offset, length; |
| 73 |
if (!aws_byte_cursor_read_le16(cursor, &offset) || !aws_byte_cursor_read_le16(cursor, &length)) { |
| 74 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 75 |
} |
| 76 |
if ((size_t)offset + length > blob.len) { |
| 77 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 78 |
} |
| 79 |
*out = aws_byte_cursor_from_array(blob.ptr + offset, length); |
| 80 |
return AWS_OP_SUCCESS; |
| 81 |
} |
| 82 |
|
| 83 |
static void s_callback_endpoints_parameter_destroy(void *data) { |
| 84 |
struct aws_endpoints_parameter *parameter = data; |
| 85 |
aws_endpoints_parameter_destroy(parameter); |
| 86 |
} |
| 87 |
|
| 88 |
static int s_parse_one_parameter( |
| 89 |
struct aws_byte_cursor *cursor, |
| 90 |
struct aws_byte_cursor blob, |
| 91 |
struct aws_endpoints_parameter *param) { |
| 92 |
|
| 93 |
uint8_t opcode; |
| 94 |
if (!aws_byte_cursor_read_u8(cursor, &opcode)) { |
| 95 |
return AWS_OP_ERR; |
| 96 |
} |
| 97 |
|
| 98 |
switch (opcode) { |
| 99 |
case BDD_OP_PARAM_STRING: |
| 100 |
param->type = AWS_ENDPOINTS_PARAMETER_STRING; |
| 101 |
break; |
| 102 |
case BDD_OP_PARAM_BOOL: |
| 103 |
param->type = AWS_ENDPOINTS_PARAMETER_BOOLEAN; |
| 104 |
break; |
| 105 |
case BDD_OP_PARAM_STRING_ARRAY: |
| 106 |
param->type = AWS_ENDPOINTS_PARAMETER_STRING_ARRAY; |
| 107 |
break; |
| 108 |
default: |
| 109 |
return AWS_OP_ERR; |
| 110 |
} |
| 111 |
|
| 112 |
if (s_read_string_ref(cursor, blob, ¶m->name)) { |
| 113 |
return AWS_OP_ERR; |
| 114 |
} |
| 115 |
|
| 116 |
uint8_t has_default; |
| 117 |
if (!aws_byte_cursor_read_u8(cursor, &has_default)) { |
| 118 |
return AWS_OP_ERR; |
| 119 |
} |
| 120 |
param->has_default_value = (has_default != 0); |
| 121 |
|
| 122 |
if (has_default) { |
| 123 |
if (param->type == AWS_ENDPOINTS_PARAMETER_STRING || param->type == AWS_ENDPOINTS_PARAMETER_STRING_ARRAY) { |
| 124 |
struct aws_byte_cursor default_cur; |
| 125 |
if (s_read_string_ref(cursor, blob, &default_cur)) { |
| 126 |
return AWS_OP_ERR; |
| 127 |
} |
| 128 |
param->default_value.type = AWS_ENDPOINTS_VALUE_STRING; |
| 129 |
param->default_value.v.owning_cursor_string.cur = default_cur; |
| 130 |
param->default_value.is_ref = true; |
| 131 |
} else { |
| 132 |
uint8_t bool_val; |
| 133 |
if (!aws_byte_cursor_read_u8(cursor, &bool_val)) { |
| 134 |
return AWS_OP_ERR; |
| 135 |
} |
| 136 |
param->default_value.type = AWS_ENDPOINTS_VALUE_BOOLEAN; |
| 137 |
param->default_value.v.boolean = (bool_val != 0); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
uint8_t is_required; |
| 142 |
if (!aws_byte_cursor_read_u8(cursor, &is_required)) { |
| 143 |
return AWS_OP_ERR; |
| 144 |
} |
| 145 |
param->is_required = (is_required != 0); |
| 146 |
|
| 147 |
uint8_t has_builtin; |
| 148 |
if (!aws_byte_cursor_read_u8(cursor, &has_builtin)) { |
| 149 |
return AWS_OP_ERR; |
| 150 |
} |
| 151 |
if (has_builtin) { |
| 152 |
if (s_read_string_ref(cursor, blob, ¶m->built_in)) { |
| 153 |
return AWS_OP_ERR; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
return AWS_OP_SUCCESS; |
| 158 |
} |
| 159 |
|
| 160 |
static int s_load_parameters( |
| 161 |
struct aws_allocator *allocator, |
| 162 |
struct aws_byte_cursor *cursor, |
| 163 |
struct aws_byte_cursor blob, |
| 164 |
struct aws_hash_table *out_parameters) { |
| 165 |
|
| 166 |
uint16_t count; |
| 167 |
if (!aws_byte_cursor_read_le16(cursor, &count)) { |
| 168 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 169 |
} |
| 170 |
|
| 171 |
if (aws_hash_table_init( |
| 172 |
out_parameters, |
| 173 |
allocator, |
| 174 |
count, |
| 175 |
aws_hash_byte_cursor_ptr, |
| 176 |
aws_endpoints_byte_cursor_eq, |
| 177 |
NULL, |
| 178 |
s_callback_endpoints_parameter_destroy)) { |
| 179 |
return AWS_OP_ERR; |
| 180 |
} |
| 181 |
|
| 182 |
for (uint16_t i = 0; i < count; ++i) { |
| 183 |
struct aws_endpoints_parameter *param = aws_mem_calloc(allocator, 1, sizeof(struct aws_endpoints_parameter)); |
| 184 |
if (!param) { |
| 185 |
goto error; |
| 186 |
} |
| 187 |
param->allocator = allocator; |
| 188 |
|
| 189 |
if (s_parse_one_parameter(cursor, blob, param) || |
| 190 |
aws_hash_table_put(out_parameters, ¶m->name, param, NULL)) { |
| 191 |
aws_mem_release(allocator, param); |
| 192 |
goto error; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
return AWS_OP_SUCCESS; |
| 197 |
|
| 198 |
error: |
| 199 |
aws_hash_table_clean_up(out_parameters); |
| 200 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 201 |
} |
| 202 |
|
| 203 |
static int s_decode_value( |
| 204 |
struct aws_endpoints_bdd_engine *engine, |
| 205 |
struct aws_byte_cursor *cursor, |
| 206 |
struct aws_byte_cursor blob, |
| 207 |
struct aws_endpoints_expr *out_expr); |
| 208 |
|
| 209 |
static int s_decode_value_to_ref( |
| 210 |
struct aws_endpoints_bdd_engine *engine, |
| 211 |
struct aws_byte_cursor *cursor, |
| 212 |
struct aws_byte_cursor blob, |
| 213 |
uint16_t *out_ref) { |
| 214 |
|
| 215 |
struct aws_endpoints_expr *expr = &engine->expr_ptr[engine->expr_len]; |
| 216 |
*out_ref = engine->expr_len++; |
| 217 |
|
| 218 |
if (s_decode_value(engine, cursor, blob, expr)) { |
| 219 |
return AWS_OP_ERR; |
| 220 |
} |
| 221 |
|
| 222 |
return AWS_OP_SUCCESS; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
static bool s_is_template_string(struct aws_byte_cursor cur) { |
| 227 |
for (size_t i = 0; i < cur.len; ++i) { |
| 228 |
if (cur.ptr[i] == '{') { |
| 229 |
return true; |
| 230 |
} |
| 231 |
} |
| 232 |
return false; |
| 233 |
} |
| 234 |
|
| 235 |
static int s_decode_value( |
| 236 |
struct aws_endpoints_bdd_engine *engine, |
| 237 |
struct aws_byte_cursor *cursor, |
| 238 |
struct aws_byte_cursor blob, |
| 239 |
struct aws_endpoints_expr *out_expr) { |
| 240 |
|
| 241 |
uint8_t tag; |
| 242 |
if (!aws_byte_cursor_read_u8(cursor, &tag)) { |
| 243 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 244 |
} |
| 245 |
|
| 246 |
switch (tag) { |
| 247 |
case 0: |
| 248 |
out_expr->type = AWS_ENDPOINTS_EXPR_STRING; |
| 249 |
out_expr->e.string = aws_byte_cursor_from_c_str(""); |
| 250 |
break; |
| 251 |
|
| 252 |
case 1: { |
| 253 |
struct aws_byte_cursor str_cur; |
| 254 |
if (s_read_string_ref(cursor, blob, &str_cur)) { |
| 255 |
return AWS_OP_ERR; |
| 256 |
} |
| 257 |
out_expr->type = |
| 258 |
s_is_template_string(str_cur) ? AWS_ENDPOINTS_EXPR_TEMPLATE_STRING : AWS_ENDPOINTS_EXPR_STRING; |
| 259 |
out_expr->e.string = str_cur; |
| 260 |
break; |
| 261 |
} |
| 262 |
|
| 263 |
case 2: { |
| 264 |
uint8_t bool_val; |
| 265 |
if (!aws_byte_cursor_read_u8(cursor, &bool_val)) { |
| 266 |
return AWS_OP_ERR; |
| 267 |
} |
| 268 |
out_expr->type = AWS_ENDPOINTS_EXPR_BOOLEAN; |
| 269 |
out_expr->e.boolean = (bool_val != 0); |
| 270 |
break; |
| 271 |
} |
| 272 |
|
| 273 |
case 3: { |
| 274 |
int32_t int_val; |
| 275 |
if (!aws_byte_cursor_read_le_i32(cursor, &int_val)) { |
| 276 |
return AWS_OP_ERR; |
| 277 |
} |
| 278 |
out_expr->type = AWS_ENDPOINTS_EXPR_NUMBER; |
| 279 |
out_expr->e.number = (double)int_val; |
| 280 |
break; |
| 281 |
} |
| 282 |
|
| 283 |
case 4: { |
| 284 |
struct aws_byte_cursor ref_cur; |
| 285 |
if (s_read_string_ref(cursor, blob, &ref_cur)) { |
| 286 |
return AWS_OP_ERR; |
| 287 |
} |
| 288 |
out_expr->type = AWS_ENDPOINTS_EXPR_REFERENCE; |
| 289 |
out_expr->e.reference = ref_cur; |
| 290 |
break; |
| 291 |
} |
| 292 |
|
| 293 |
case 5: { |
| 294 |
uint8_t fn_type; |
| 295 |
if (!aws_byte_cursor_read_u8(cursor, &fn_type)) { |
| 296 |
return AWS_OP_ERR; |
| 297 |
} |
| 298 |
|
| 299 |
if (!aws_byte_cursor_read_le16(cursor, &out_expr->e.function.args.argc)) { |
| 300 |
return AWS_OP_ERR; |
| 301 |
} |
| 302 |
|
| 303 |
out_expr->type = AWS_ENDPOINTS_EXPR_FUNCTION; |
| 304 |
out_expr->e.function.fn = fn_type; |
| 305 |
|
| 306 |
for (uint16_t i = 0; i < out_expr->e.function.args.argc; ++i) { |
| 307 |
if (s_decode_value_to_ref(engine, cursor, blob, &out_expr->e.function.args.argv[i])) { |
| 308 |
return AWS_OP_ERR; |
| 309 |
} |
| 310 |
} |
| 311 |
break; |
| 312 |
} |
| 313 |
|
| 314 |
case 6: { |
| 315 |
uint16_t length; |
| 316 |
if (!aws_byte_cursor_read_le16(cursor, &length)) { |
| 317 |
return AWS_OP_ERR; |
| 318 |
} |
| 319 |
|
| 320 |
out_expr->type = AWS_ENDPOINTS_EXPR_ARRAY; |
| 321 |
out_expr->e.array.len = length; |
| 322 |
|
| 323 |
for (uint16_t i = 0; i < length; ++i) { |
| 324 |
if (s_decode_value_to_ref(engine, cursor, blob, &out_expr->e.array.ptr[i])) { |
| 325 |
return AWS_OP_ERR; |
| 326 |
} |
| 327 |
} |
| 328 |
break; |
| 329 |
} |
| 330 |
|
| 331 |
case 7: { |
| 332 |
uint16_t length; |
| 333 |
if (!aws_byte_cursor_read_le16(cursor, &length)) { |
| 334 |
return AWS_OP_ERR; |
| 335 |
} |
| 336 |
|
| 337 |
out_expr->type = AWS_ENDPOINTS_EXPR_OBJECT; |
| 338 |
if (aws_array_list_init_dynamic( |
| 339 |
&out_expr->e.object, engine->allocator, length, sizeof(struct aws_endpoints_kv_pair))) { |
| 340 |
return AWS_OP_ERR; |
| 341 |
} |
| 342 |
|
| 343 |
for (uint16_t i = 0; i < length; ++i) { |
| 344 |
struct aws_byte_cursor key_cur; |
| 345 |
if (s_read_string_ref(cursor, blob, &key_cur)) { |
| 346 |
return AWS_OP_ERR; |
| 347 |
} |
| 348 |
|
| 349 |
struct aws_endpoints_kv_pair pair; |
| 350 |
pair.allocator = engine->allocator; |
| 351 |
pair.key = key_cur; |
| 352 |
|
| 353 |
if (s_decode_value_to_ref(engine, cursor, blob, &pair.expr_ref)) { |
| 354 |
return AWS_OP_ERR; |
| 355 |
} |
| 356 |
} |
| 357 |
break; |
| 358 |
} |
| 359 |
|
| 360 |
default: |
| 361 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Unknown value tag: %d", (int)tag); |
| 362 |
return AWS_OP_ERR; |
| 363 |
} |
| 364 |
|
| 365 |
return AWS_OP_SUCCESS; |
| 366 |
} |
| 367 |
|
| 368 |
static int s_parse_one_condition( |
| 369 |
struct aws_endpoints_bdd_engine *engine, |
| 370 |
struct aws_byte_cursor *cursor, |
| 371 |
struct aws_byte_cursor blob, |
| 372 |
struct aws_endpoints_condition *cond) { |
| 373 |
|
| 374 |
uint8_t opcode; |
| 375 |
if (!aws_byte_cursor_read_u8(cursor, &opcode) || opcode != BDD_OP_CONDITION) { |
| 376 |
return AWS_OP_ERR; |
| 377 |
} |
| 378 |
|
| 379 |
struct aws_endpoints_expr *expr = &engine->expr_ptr[engine->expr_len]; |
| 380 |
expr->type = AWS_ENDPOINTS_EXPR_FUNCTION; |
| 381 |
expr->e.function.allocator = engine->allocator; |
| 382 |
uint8_t fn_type; |
| 383 |
if (!aws_byte_cursor_read_u8(cursor, &fn_type)) { |
| 384 |
return AWS_OP_ERR; |
| 385 |
} |
| 386 |
expr->e.function.fn = fn_type; |
| 387 |
|
| 388 |
if (!aws_byte_cursor_read_le16(cursor, &expr->e.function.args.argc)) { |
| 389 |
return AWS_OP_ERR; |
| 390 |
} |
| 391 |
|
| 392 |
cond->expr_ref = engine->expr_len++; |
| 393 |
|
| 394 |
for (uint16_t i = 0; i < expr->e.function.args.argc; ++i) { |
| 395 |
if (s_decode_value_to_ref(engine, cursor, blob, &expr->e.function.args.argv[i])) { |
| 396 |
goto on_error; |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
uint8_t has_assign; |
| 401 |
if (!aws_byte_cursor_read_u8(cursor, &has_assign)) { |
| 402 |
goto on_error; |
| 403 |
} |
| 404 |
|
| 405 |
if (has_assign) { |
| 406 |
if (s_read_string_ref(cursor, blob, &cond->assign)) { |
| 407 |
goto on_error; |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
return AWS_OP_SUCCESS; |
| 412 |
|
| 413 |
on_error: |
| 414 |
aws_endpoints_expr_clean_up(expr); |
| 415 |
return AWS_OP_ERR; |
| 416 |
} |
| 417 |
|
| 418 |
static int s_load_conditions( |
| 419 |
struct aws_endpoints_bdd_engine *engine, |
| 420 |
struct aws_byte_cursor *cursor, |
| 421 |
struct aws_byte_cursor blob, |
| 422 |
struct aws_array_list *out_conditions, |
| 423 |
struct aws_endpoints_condition **out_conditions_ptr) { |
| 424 |
|
| 425 |
uint16_t count; |
| 426 |
if (!aws_byte_cursor_read_le16(cursor, &count)) { |
| 427 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 428 |
} |
| 429 |
|
| 430 |
struct aws_endpoints_condition *conditions = |
| 431 |
aws_mem_calloc(engine->allocator, count, sizeof(struct aws_endpoints_condition)); |
| 432 |
|
| 433 |
for (uint16_t i = 0; i < count; ++i) { |
| 434 |
if (s_parse_one_condition(engine, cursor, blob, &conditions[i])) { |
| 435 |
goto error; |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
aws_array_list_init_static_from_initialized( |
| 440 |
out_conditions, conditions, count, sizeof(struct aws_endpoints_condition)); |
| 441 |
|
| 442 |
*out_conditions_ptr = conditions; |
| 443 |
|
| 444 |
return AWS_OP_SUCCESS; |
| 445 |
|
| 446 |
error: |
| 447 |
aws_mem_release(engine->allocator, conditions); |
| 448 |
aws_array_list_deep_clean_up(out_conditions, s_on_condition_array_element_clean_up); |
| 449 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 450 |
} |
| 451 |
|
| 452 |
static void s_callback_headers_destroy(void *data) { |
| 453 |
struct aws_array_list *array = data; |
| 454 |
struct aws_allocator *alloc = array->alloc; |
| 455 |
aws_array_list_clean_up(array); |
| 456 |
aws_mem_release(alloc, array); |
| 457 |
} |
| 458 |
|
| 459 |
static int s_load_results( |
| 460 |
struct aws_endpoints_bdd_engine *engine, |
| 461 |
struct aws_byte_cursor *cursor, |
| 462 |
struct aws_byte_cursor blob, |
| 463 |
struct aws_array_list *out_results) { |
| 464 |
|
| 465 |
uint16_t count; |
| 466 |
if (!aws_byte_cursor_read_le16(cursor, &count)) { |
| 467 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 468 |
} |
| 469 |
|
| 470 |
if (aws_array_list_init_dynamic( |
| 471 |
out_results, engine->allocator, count + 1, sizeof(struct aws_endpoints_bdd_result))) { |
| 472 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 473 |
} |
| 474 |
|
| 475 |
|
| 476 |
struct aws_endpoints_expr *expr = &engine->expr_ptr[engine->expr_len]; |
| 477 |
expr->type = AWS_ENDPOINTS_EXPR_STRING; |
| 478 |
expr->e.string = aws_byte_cursor_from_c_str("No matching rule"); |
| 479 |
|
| 480 |
struct aws_endpoints_bdd_result no_match; |
| 481 |
AWS_ZERO_STRUCT(no_match); |
| 482 |
no_match.type = AWS_ENDPOINTS_RESOLVED_ERROR; |
| 483 |
no_match.data.error.error_expr_ref = engine->expr_len++; |
| 484 |
|
| 485 |
if (aws_array_list_push_back(out_results, &no_match)) { |
| 486 |
goto error; |
| 487 |
} |
| 488 |
|
| 489 |
for (uint16_t i = 0; i < count; ++i) { |
| 490 |
uint8_t opcode; |
| 491 |
if (!aws_byte_cursor_read_u8(cursor, &opcode)) { |
| 492 |
goto error; |
| 493 |
} |
| 494 |
|
| 495 |
struct aws_endpoints_bdd_result result; |
| 496 |
AWS_ZERO_STRUCT(result); |
| 497 |
|
| 498 |
if (opcode == BDD_OP_RESULT_ENDPOINT) { |
| 499 |
result.type = AWS_ENDPOINTS_RESOLVED_ENDPOINT; |
| 500 |
|
| 501 |
if (s_decode_value_to_ref(engine, cursor, blob, &result.data.endpoint.url_expr_ref)) { |
| 502 |
goto error; |
| 503 |
} |
| 504 |
|
| 505 |
struct aws_byte_cursor props; |
| 506 |
if (s_read_string_ref(cursor, blob, &props)) { |
| 507 |
goto error; |
| 508 |
} |
| 509 |
|
| 510 |
result.data.endpoint.properties = aws_byte_buf_from_array(props.ptr, props.len); |
| 511 |
|
| 512 |
uint16_t headers_count; |
| 513 |
if (!aws_byte_cursor_read_le16(cursor, &headers_count)) { |
| 514 |
goto error; |
| 515 |
} |
| 516 |
|
| 517 |
aws_hash_table_init( |
| 518 |
&result.data.endpoint.headers, |
| 519 |
engine->allocator, |
| 520 |
headers_count, |
| 521 |
aws_hash_string, |
| 522 |
aws_hash_callback_string_eq, |
| 523 |
aws_hash_callback_string_destroy, |
| 524 |
s_callback_headers_destroy); |
| 525 |
|
| 526 |
for (uint16_t header_i = 0; header_i < headers_count; ++header_i) { |
| 527 |
struct aws_byte_cursor header_name; |
| 528 |
if (s_read_string_ref(cursor, blob, &header_name)) { |
| 529 |
aws_hash_table_clean_up(&result.data.endpoint.headers); |
| 530 |
goto error; |
| 531 |
} |
| 532 |
|
| 533 |
uint16_t value_count; |
| 534 |
if (!aws_byte_cursor_read_le16(cursor, &value_count)) { |
| 535 |
aws_hash_table_clean_up(&result.data.endpoint.headers); |
| 536 |
goto error; |
| 537 |
} |
| 538 |
|
| 539 |
struct aws_array_list *values = aws_mem_calloc(engine->allocator, 1, sizeof(struct aws_array_list)); |
| 540 |
aws_array_list_init_dynamic(values, engine->allocator, value_count, sizeof(uint16_t)); |
| 541 |
for (uint16_t value_i = 0; value_i < value_count; ++value_i) { |
| 542 |
uint16_t expr_ref; |
| 543 |
if (s_decode_value_to_ref(engine, cursor, blob, &expr_ref)) { |
| 544 |
aws_hash_table_clean_up(&result.data.endpoint.headers); |
| 545 |
aws_mem_release(engine->allocator, values); |
| 546 |
goto error; |
| 547 |
} |
| 548 |
aws_array_list_push_back(values, &expr_ref); |
| 549 |
} |
| 550 |
|
| 551 |
aws_hash_table_put( |
| 552 |
&result.data.endpoint.headers, |
| 553 |
aws_string_new_from_cursor(engine->allocator, &header_name), |
| 554 |
values, |
| 555 |
NULL); |
| 556 |
} |
| 557 |
|
| 558 |
} else if (opcode == BDD_OP_RESULT_ERROR) { |
| 559 |
result.type = AWS_ENDPOINTS_RESOLVED_ERROR; |
| 560 |
|
| 561 |
if (s_decode_value_to_ref(engine, cursor, blob, &result.data.error.error_expr_ref)) { |
| 562 |
goto error; |
| 563 |
} |
| 564 |
|
| 565 |
} else { |
| 566 |
goto error; |
| 567 |
} |
| 568 |
|
| 569 |
if (aws_array_list_push_back(out_results, &result)) { |
| 570 |
goto error; |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
return AWS_OP_SUCCESS; |
| 575 |
|
| 576 |
error: |
| 577 |
aws_array_list_clean_up(out_results); |
| 578 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 579 |
} |
| 580 |
|
| 581 |
static int s_load_nodes( |
| 582 |
struct aws_allocator *allocator, |
| 583 |
struct aws_byte_cursor *cursor, |
| 584 |
int32_t *out_root_ref, |
| 585 |
struct aws_array_list *out_nodes) { |
| 586 |
|
| 587 |
if (!aws_byte_cursor_read_le_i32(cursor, out_root_ref)) { |
| 588 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 589 |
} |
| 590 |
|
| 591 |
uint32_t node_count; |
| 592 |
if (!aws_byte_cursor_read_le32(cursor, &node_count)) { |
| 593 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 594 |
} |
| 595 |
|
| 596 |
uint16_t data_length; |
| 597 |
if (!aws_byte_cursor_read_le16(cursor, &data_length)) { |
| 598 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 599 |
} |
| 600 |
|
| 601 |
if (cursor->len < data_length) { |
| 602 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 603 |
} |
| 604 |
|
| 605 |
struct aws_byte_cursor data = aws_byte_cursor_advance(cursor, data_length); |
| 606 |
|
| 607 |
size_t expected_size = node_count * 12; |
| 608 |
if (data.len != expected_size) { |
| 609 |
return AWS_OP_ERR; |
| 610 |
} |
| 611 |
|
| 612 |
if (aws_is_big_endian()) { |
| 613 |
if (aws_array_list_init_dynamic(out_nodes, allocator, node_count, sizeof(struct aws_endpoints_bdd_node))) { |
| 614 |
return AWS_OP_ERR; |
| 615 |
} |
| 616 |
for (uint32_t i = 0; i < node_count; ++i) { |
| 617 |
struct aws_endpoints_bdd_node node; |
| 618 |
if (!aws_byte_cursor_read_le_i32(&data, &node.condition_index) || |
| 619 |
!aws_byte_cursor_read_le_i32(&data, &node.high_ref) || |
| 620 |
!aws_byte_cursor_read_le_i32(&data, &node.low_ref)) { |
| 621 |
aws_array_list_clean_up(out_nodes); |
| 622 |
return AWS_OP_ERR; |
| 623 |
} |
| 624 |
aws_array_list_push_back(out_nodes, &node); |
| 625 |
} |
| 626 |
|
| 627 |
} else { |
| 628 |
aws_array_list_init_static_from_initialized( |
| 629 |
out_nodes, data.ptr, node_count, sizeof(struct aws_endpoints_bdd_node)); |
| 630 |
|
| 631 |
aws_byte_cursor_advance(cursor, expected_size); |
| 632 |
} |
| 633 |
|
| 634 |
return AWS_OP_SUCCESS; |
| 635 |
} |
| 636 |
|
| 637 |
struct aws_endpoints_bdd_engine *aws_endpoints_bdd_engine_new_from_bytecode( |
| 638 |
struct aws_allocator *allocator, |
| 639 |
struct aws_byte_cursor bytecode, |
| 640 |
struct aws_partitions_config *partitions_config) { |
| 641 |
|
| 642 |
AWS_PRECONDITION(allocator); |
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
struct aws_endpoints_bdd_engine *engine = aws_mem_calloc(allocator, 1, sizeof(struct aws_endpoints_bdd_engine)); |
| 648 |
if (!engine) { |
| 649 |
return NULL; |
| 650 |
} |
| 651 |
|
| 652 |
engine->allocator = allocator; |
| 653 |
aws_ref_count_init(&engine->ref_count, engine, s_endpoints_bdd_engine_destroy); |
| 654 |
engine->partitions_config = aws_partitions_config_acquire(partitions_config); |
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
if (s_validate_magic_number(&bytecode)) { |
| 662 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to validate magic number"); |
| 663 |
goto error; |
| 664 |
} |
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
if (s_load_string_table(&bytecode, &engine->string_blob)) { |
| 671 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to load string table"); |
| 672 |
goto error; |
| 673 |
} |
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
struct aws_byte_cursor version_cur; |
| 680 |
if (s_read_string_ref(&bytecode, engine->string_blob, &version_cur)) { |
| 681 |
goto error; |
| 682 |
} |
| 683 |
engine->version = version_cur; |
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
if (s_load_parameters(allocator, &bytecode, engine->string_blob, &engine->parameters)) { |
| 691 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to load parameters"); |
| 692 |
goto error; |
| 693 |
} |
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
if (s_load_conditions(engine, &bytecode, engine->string_blob, &engine->conditions, &engine->conditions_ptr)) { |
| 700 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to load conditions"); |
| 701 |
goto error; |
| 702 |
} |
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
if (s_load_results(engine, &bytecode, engine->string_blob, &engine->results)) { |
| 709 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to load results"); |
| 710 |
goto error; |
| 711 |
} |
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
if (s_load_nodes(allocator, &bytecode, &engine->root_ref, &engine->nodes)) { |
| 718 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to load nodes"); |
| 719 |
goto error; |
| 720 |
} |
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
AWS_LOGF_DEBUG( |
| 726 |
AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, |
| 727 |
"BDD engine loaded: %d params, %d conditions, %d results, %d nodes", |
| 728 |
(int)aws_hash_table_get_entry_count(&engine->parameters), |
| 729 |
(int)aws_array_list_length(&engine->conditions), |
| 730 |
(int)aws_array_list_length(&engine->results), |
| 731 |
(int)aws_array_list_length(&engine->nodes)); |
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
return engine; |
| 738 |
|
| 739 |
error: |
| 740 |
s_endpoints_bdd_engine_destroy(engine); |
| 741 |
return NULL; |
| 742 |
} |
| 743 |
|
| 744 |
static void s_endpoints_bdd_engine_destroy(void *data) { |
| 745 |
if (data == NULL) { |
| 746 |
return; |
| 747 |
} |
| 748 |
|
| 749 |
struct aws_endpoints_bdd_engine *engine = data; |
| 750 |
|
| 751 |
if (engine->partitions_config) { |
| 752 |
aws_partitions_config_release(engine->partitions_config); |
| 753 |
} |
| 754 |
|
| 755 |
aws_hash_table_clean_up(&engine->parameters); |
| 756 |
aws_array_list_deep_clean_up(&engine->conditions, s_on_condition_array_element_clean_up); |
| 757 |
aws_mem_release(engine->allocator, engine->conditions_ptr); |
| 758 |
aws_array_list_deep_clean_up(&engine->results, s_on_resutls_array_element_clean_up); |
| 759 |
aws_array_list_clean_up(&engine->results); |
| 760 |
aws_array_list_clean_up(&engine->nodes); |
| 761 |
|
| 762 |
aws_mem_release(engine->allocator, engine); |
| 763 |
} |
| 764 |
|
| 765 |
struct aws_endpoints_bdd_engine *aws_endpoints_bdd_engine_acquire(struct aws_endpoints_bdd_engine *engine) { |
| 766 |
AWS_PRECONDITION(engine); |
| 767 |
if (engine) { |
| 768 |
aws_ref_count_acquire(&engine->ref_count); |
| 769 |
} |
| 770 |
return engine; |
| 771 |
} |
| 772 |
|
| 773 |
struct aws_endpoints_bdd_engine *aws_endpoints_bdd_engine_release(struct aws_endpoints_bdd_engine *engine) { |
| 774 |
if (engine) { |
| 775 |
aws_ref_count_release(&engine->ref_count); |
| 776 |
} |
| 777 |
return NULL; |
| 778 |
} |