1#ifndef AWS_COMMON_ENCODING_H
2#define AWS_COMMON_ENCODING_H
3
4/*
5 * Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License").
8 * You may not use this file except in compliance with the License.
9 * A copy of the License is located at
10 *
11 * http://aws.amazon.com/apache2.0
12 *
13 * or in the "license" file accompanying this file. This file is distributed
14 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
15 * express or implied. See the License for the specific language governing
16 * permissions and limitations under the License.
17 */
18
19#include <aws/common/byte_buf.h>
20#include <aws/common/byte_order.h>
21#include <aws/common/common.h>
22
23#include <memory.h>
24
25AWS_EXTERN_C_BEGIN
26
27/*
28 * computes the length necessary to store the result of aws_hex_encode().
29 * returns -1 on failure, and 0 on success. encoded_length will be set on
30 * success.
31 */
32AWS_COMMON_API
33int aws_hex_compute_encoded_len(size_t to_encode_len, size_t *encoded_length);
34
35/*
36 * Base 16 (hex) encodes the contents of to_encode and stores the result in
37 * output. 0 terminates the result. Assumes the buffer is empty and does not resize on
38 * insufficient capacity.
39 */
40AWS_COMMON_API
41int aws_hex_encode(const struct aws_byte_cursor *AWS_RESTRICT to_encode, struct aws_byte_buf *AWS_RESTRICT output);
42
43/*
44 * Base 16 (hex) encodes the contents of to_encode and appends the result in
45 * output. Does not 0-terminate. Grows the destination buffer dynamically if necessary.
46 */
47AWS_COMMON_API
48int aws_hex_encode_append_dynamic(
49 const struct aws_byte_cursor *AWS_RESTRICT to_encode,
50 struct aws_byte_buf *AWS_RESTRICT output);
51
52/*
53 * computes the length necessary to store the result of aws_hex_decode().
54 * returns -1 on failure, and 0 on success. decoded_len will be set on success.
55 */
56AWS_COMMON_API
57int aws_hex_compute_decoded_len(size_t to_decode_len, size_t *decoded_len);
58
59/*
60 * Base 16 (hex) decodes the contents of to_decode and stores the result in
61 * output. If output is NULL, output_size will be set to what the output_size
62 * should be.
63 */
64AWS_COMMON_API
65int aws_hex_decode(const struct aws_byte_cursor *AWS_RESTRICT to_decode, struct aws_byte_buf *AWS_RESTRICT output);
66
67/*
68 * Computes the length necessary to store the output of aws_base64_encode call.
69 * returns -1 on failure, and 0 on success. encoded_length will be set on
70 * success.
71 */
72AWS_COMMON_API
73int aws_base64_compute_encoded_len(size_t to_encode_len, size_t *encoded_len);
74
75/*
76 * Base 64 encodes the contents of to_encode and stores the result in output.
77 */
78AWS_COMMON_API
79int aws_base64_encode(const struct aws_byte_cursor *AWS_RESTRICT to_encode, struct aws_byte_buf *AWS_RESTRICT output);
80
81/*
82 * Computes the length necessary to store the output of aws_base64_decode call.
83 * returns -1 on failure, and 0 on success. decoded_len will be set on success.
84 */
85AWS_COMMON_API
86int aws_base64_compute_decoded_len(const struct aws_byte_cursor *AWS_RESTRICT to_decode, size_t *decoded_len);
87
88/*
89 * Base 64 decodes the contents of to_decode and stores the result in output.
90 */
91AWS_COMMON_API
92int aws_base64_decode(const struct aws_byte_cursor *AWS_RESTRICT to_decode, struct aws_byte_buf *AWS_RESTRICT output);
93
94/* Add a 64 bit unsigned integer to the buffer, ensuring network - byte order
95 * Assumes the buffer size is at least 8 bytes.
96 */
97AWS_STATIC_IMPL void aws_write_u64(uint64_t value, uint8_t *buffer);
98
99/*
100 * Extracts a 64 bit unsigned integer from buffer. Ensures conversion from
101 * network byte order to host byte order. Assumes buffer size is at least 8
102 * bytes.
103 */
104AWS_STATIC_IMPL uint64_t aws_read_u64(const uint8_t *buffer);
105
106/* Add a 32 bit unsigned integer to the buffer, ensuring network - byte order
107 * Assumes the buffer size is at least 4 bytes.
108 */
109AWS_STATIC_IMPL void aws_write_u32(uint32_t value, uint8_t *buffer);
110
111/*
112 * Extracts a 32 bit unsigned integer from buffer. Ensures conversion from
113 * network byte order to host byte order. Assumes the buffer size is at least 4
114 * bytes.
115 */
116AWS_STATIC_IMPL uint32_t aws_read_u32(const uint8_t *buffer);
117
118/* Add a 24 bit unsigned integer to the buffer, ensuring network - byte order
119 * return the new position in the buffer for the next operation.
120 * Note, since this uses uint32_t for storage, the 3 least significant bytes
121 * will be used. Assumes buffer is at least 3 bytes long.
122 */
123AWS_STATIC_IMPL void aws_write_u24(uint32_t value, uint8_t *buffer);
124/*
125 * Extracts a 24 bit unsigned integer from buffer. Ensures conversion from
126 * network byte order to host byte order. Assumes buffer is at least 3 bytes
127 * long.
128 */
129AWS_STATIC_IMPL uint32_t aws_read_u24(const uint8_t *buffer);
130
131/* Add a 16 bit unsigned integer to the buffer, ensuring network-byte order
132 * return the new position in the buffer for the next operation.
133 * Assumes buffer is at least 2 bytes long.
134 */
135AWS_STATIC_IMPL void aws_write_u16(uint16_t value, uint8_t *buffer);
136/*
137 * Extracts a 16 bit unsigned integer from buffer. Ensures conversion from
138 * network byte order to host byte order. Assumes buffer is at least 2 bytes
139 * long.
140 */
141AWS_STATIC_IMPL uint16_t aws_read_u16(const uint8_t *buffer);
142
143#ifndef AWS_NO_STATIC_IMPL
144# include <aws/common/encoding.inl>
145#endif /* AWS_NO_STATIC_IMPL */
146
147AWS_EXTERN_C_END
148
149#endif /* AWS_COMMON_ENCODING_H */
150