| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/sdkutils/resource_name.h> |
| 7 |
|
| 8 |
#define ARN_SPLIT_COUNT ((size_t)5) |
| 9 |
#define ARN_PARTS_COUNT ((size_t)6) |
| 10 |
|
| 11 |
static const char ARN_DELIMETER[] = ":"; |
| 12 |
static const char ARN_DELIMETER_CHAR = ':'; |
| 13 |
|
| 14 |
static const size_t DELIMETER_LEN = 8; |
| 15 |
|
| 16 |
int aws_resource_name_init_from_cur(struct aws_resource_name *arn, const struct aws_byte_cursor *input) { |
| 17 |
struct aws_byte_cursor arn_parts[ARN_PARTS_COUNT]; |
| 18 |
struct aws_array_list arn_part_list; |
| 19 |
aws_array_list_init_static(&arn_part_list, arn_parts, ARN_PARTS_COUNT, sizeof(struct aws_byte_cursor)); |
| 20 |
if (aws_byte_cursor_split_on_char_n(input, ARN_DELIMETER_CHAR, ARN_SPLIT_COUNT, &arn_part_list)) { |
| 21 |
return aws_raise_error(AWS_ERROR_MALFORMED_INPUT_STRING); |
| 22 |
} |
| 23 |
|
| 24 |
struct aws_byte_cursor *arn_prefix; |
| 25 |
if (aws_array_list_get_at_ptr(&arn_part_list, (void **)&arn_prefix, 0) || |
| 26 |
!aws_byte_cursor_eq_c_str(arn_prefix, "arn")) { |
| 27 |
return aws_raise_error(AWS_ERROR_MALFORMED_INPUT_STRING); |
| 28 |
} |
| 29 |
if (aws_array_list_get_at(&arn_part_list, &arn->partition, 1)) { |
| 30 |
return aws_raise_error(AWS_ERROR_MALFORMED_INPUT_STRING); |
| 31 |
} |
| 32 |
if (aws_array_list_get_at(&arn_part_list, &arn->service, 2)) { |
| 33 |
return aws_raise_error(AWS_ERROR_MALFORMED_INPUT_STRING); |
| 34 |
} |
| 35 |
if (aws_array_list_get_at(&arn_part_list, &arn->region, 3)) { |
| 36 |
return aws_raise_error(AWS_ERROR_MALFORMED_INPUT_STRING); |
| 37 |
} |
| 38 |
if (aws_array_list_get_at(&arn_part_list, &arn->account_id, 4)) { |
| 39 |
return aws_raise_error(AWS_ERROR_MALFORMED_INPUT_STRING); |
| 40 |
} |
| 41 |
if (aws_array_list_get_at(&arn_part_list, &arn->resource_id, 5)) { |
| 42 |
return aws_raise_error(AWS_ERROR_MALFORMED_INPUT_STRING); |
| 43 |
} |
| 44 |
return AWS_OP_SUCCESS; |
| 45 |
} |
| 46 |
|
| 47 |
int aws_resource_name_length(const struct aws_resource_name *arn, size_t *size) { |
| 48 |
AWS_PRECONDITION(aws_byte_cursor_is_valid(&arn->partition)); |
| 49 |
AWS_PRECONDITION(aws_byte_cursor_is_valid(&arn->service)); |
| 50 |
AWS_PRECONDITION(aws_byte_cursor_is_valid(&arn->region)); |
| 51 |
AWS_PRECONDITION(aws_byte_cursor_is_valid(&arn->account_id)); |
| 52 |
AWS_PRECONDITION(aws_byte_cursor_is_valid(&arn->resource_id)); |
| 53 |
|
| 54 |
*size = arn->partition.len + arn->region.len + arn->service.len + arn->account_id.len + arn->resource_id.len + |
| 55 |
DELIMETER_LEN; |
| 56 |
|
| 57 |
return AWS_OP_SUCCESS; |
| 58 |
} |
| 59 |
|
| 60 |
int aws_byte_buf_append_resource_name(struct aws_byte_buf *buf, const struct aws_resource_name *arn) { |
| 61 |
AWS_PRECONDITION(aws_byte_buf_is_valid(buf)); |
| 62 |
AWS_PRECONDITION(aws_byte_cursor_is_valid(&arn->partition)); |
| 63 |
AWS_PRECONDITION(aws_byte_cursor_is_valid(&arn->service)); |
| 64 |
AWS_PRECONDITION(aws_byte_cursor_is_valid(&arn->region)); |
| 65 |
AWS_PRECONDITION(aws_byte_cursor_is_valid(&arn->account_id)); |
| 66 |
AWS_PRECONDITION(aws_byte_cursor_is_valid(&arn->resource_id)); |
| 67 |
|
| 68 |
const struct aws_byte_cursor prefix = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("arn:"); |
| 69 |
const struct aws_byte_cursor colon_cur = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(ARN_DELIMETER); |
| 70 |
|
| 71 |
if (aws_byte_buf_append(buf, &prefix)) { |
| 72 |
return aws_raise_error(aws_last_error()); |
| 73 |
} |
| 74 |
if (aws_byte_buf_append(buf, &arn->partition)) { |
| 75 |
return aws_raise_error(aws_last_error()); |
| 76 |
} |
| 77 |
if (aws_byte_buf_append(buf, &colon_cur)) { |
| 78 |
return aws_raise_error(aws_last_error()); |
| 79 |
} |
| 80 |
|
| 81 |
if (aws_byte_buf_append(buf, &arn->service)) { |
| 82 |
return aws_raise_error(aws_last_error()); |
| 83 |
} |
| 84 |
if (aws_byte_buf_append(buf, &colon_cur)) { |
| 85 |
return aws_raise_error(aws_last_error()); |
| 86 |
} |
| 87 |
|
| 88 |
if (aws_byte_buf_append(buf, &arn->region)) { |
| 89 |
return aws_raise_error(aws_last_error()); |
| 90 |
} |
| 91 |
if (aws_byte_buf_append(buf, &colon_cur)) { |
| 92 |
return aws_raise_error(aws_last_error()); |
| 93 |
} |
| 94 |
|
| 95 |
if (aws_byte_buf_append(buf, &arn->account_id)) { |
| 96 |
return aws_raise_error(aws_last_error()); |
| 97 |
} |
| 98 |
if (aws_byte_buf_append(buf, &colon_cur)) { |
| 99 |
return aws_raise_error(aws_last_error()); |
| 100 |
} |
| 101 |
|
| 102 |
if (aws_byte_buf_append(buf, &arn->resource_id)) { |
| 103 |
return aws_raise_error(aws_last_error()); |
| 104 |
} |
| 105 |
|
| 106 |
AWS_POSTCONDITION(aws_byte_buf_is_valid(buf)); |
| 107 |
return AWS_OP_SUCCESS; |
| 108 |
} |