aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/restricted/aws/aws-c-sdkutils/source/resource_name.c
blob: 0687c5ea7e5e74d3db29515ccb2e496fb274e4b2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

#include <aws/sdkutils/resource_name.h>

#define ARN_SPLIT_COUNT ((size_t)5)
#define ARN_PARTS_COUNT ((size_t)6)

static const char ARN_DELIMETER[] = ":";
static const char ARN_DELIMETER_CHAR = ':';

static const size_t DELIMETER_LEN = 8; /* strlen("arn:::::") */

int aws_resource_name_init_from_cur(struct aws_resource_name *arn, const struct aws_byte_cursor *input) {
    struct aws_byte_cursor arn_parts[ARN_PARTS_COUNT];
    struct aws_array_list arn_part_list;
    aws_array_list_init_static(&arn_part_list, arn_parts, ARN_PARTS_COUNT, sizeof(struct aws_byte_cursor));
    if (aws_byte_cursor_split_on_char_n(input, ARN_DELIMETER_CHAR, ARN_SPLIT_COUNT, &arn_part_list)) {
        return aws_raise_error(AWS_ERROR_MALFORMED_INPUT_STRING);
    }

    struct aws_byte_cursor *arn_prefix;
    if (aws_array_list_get_at_ptr(&arn_part_list, (void **)&arn_prefix, 0) ||
        !aws_byte_cursor_eq_c_str(arn_prefix, "arn")) {
        return aws_raise_error(AWS_ERROR_MALFORMED_INPUT_STRING);
    }
    if (aws_array_list_get_at(&arn_part_list, &arn->partition, 1)) {
        return aws_raise_error(AWS_ERROR_MALFORMED_INPUT_STRING);
    }
    if (aws_array_list_get_at(&arn_part_list, &arn->service, 2)) {
        return aws_raise_error(AWS_ERROR_MALFORMED_INPUT_STRING);
    }
    if (aws_array_list_get_at(&arn_part_list, &arn->region, 3)) {
        return aws_raise_error(AWS_ERROR_MALFORMED_INPUT_STRING);
    }
    if (aws_array_list_get_at(&arn_part_list, &arn->account_id, 4)) {
        return aws_raise_error(AWS_ERROR_MALFORMED_INPUT_STRING);
    }
    if (aws_array_list_get_at(&arn_part_list, &arn->resource_id, 5)) {
        return aws_raise_error(AWS_ERROR_MALFORMED_INPUT_STRING);
    }
    return AWS_OP_SUCCESS;
}

int aws_resource_name_length(const struct aws_resource_name *arn, size_t *size) {
    AWS_PRECONDITION(aws_byte_cursor_is_valid(&arn->partition));
    AWS_PRECONDITION(aws_byte_cursor_is_valid(&arn->service));
    AWS_PRECONDITION(aws_byte_cursor_is_valid(&arn->region));
    AWS_PRECONDITION(aws_byte_cursor_is_valid(&arn->account_id));
    AWS_PRECONDITION(aws_byte_cursor_is_valid(&arn->resource_id));

    *size = arn->partition.len + arn->region.len + arn->service.len + arn->account_id.len + arn->resource_id.len +
            DELIMETER_LEN;

    return AWS_OP_SUCCESS;
}

int aws_byte_buf_append_resource_name(struct aws_byte_buf *buf, const struct aws_resource_name *arn) {
    AWS_PRECONDITION(aws_byte_buf_is_valid(buf));
    AWS_PRECONDITION(aws_byte_cursor_is_valid(&arn->partition));
    AWS_PRECONDITION(aws_byte_cursor_is_valid(&arn->service));
    AWS_PRECONDITION(aws_byte_cursor_is_valid(&arn->region));
    AWS_PRECONDITION(aws_byte_cursor_is_valid(&arn->account_id));
    AWS_PRECONDITION(aws_byte_cursor_is_valid(&arn->resource_id));

    const struct aws_byte_cursor prefix = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("arn:");
    const struct aws_byte_cursor colon_cur = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(ARN_DELIMETER);

    if (aws_byte_buf_append(buf, &prefix)) {
        return aws_raise_error(aws_last_error());
    }
    if (aws_byte_buf_append(buf, &arn->partition)) {
        return aws_raise_error(aws_last_error());
    }
    if (aws_byte_buf_append(buf, &colon_cur)) {
        return aws_raise_error(aws_last_error());
    }

    if (aws_byte_buf_append(buf, &arn->service)) {
        return aws_raise_error(aws_last_error());
    }
    if (aws_byte_buf_append(buf, &colon_cur)) {
        return aws_raise_error(aws_last_error());
    }

    if (aws_byte_buf_append(buf, &arn->region)) {
        return aws_raise_error(aws_last_error());
    }
    if (aws_byte_buf_append(buf, &colon_cur)) {
        return aws_raise_error(aws_last_error());
    }

    if (aws_byte_buf_append(buf, &arn->account_id)) {
        return aws_raise_error(aws_last_error());
    }
    if (aws_byte_buf_append(buf, &colon_cur)) {
        return aws_raise_error(aws_last_error());
    }

    if (aws_byte_buf_append(buf, &arn->resource_id)) {
        return aws_raise_error(aws_last_error());
    }

    AWS_POSTCONDITION(aws_byte_buf_is_valid(buf));
    return AWS_OP_SUCCESS;
}