| 1 | #ifndef AWS_COMMON_ENCODING_H |
| 2 | #define AWS_COMMON_ENCODING_H |
| 3 | |
| 4 | /** |
| 5 | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 6 | * SPDX-License-Identifier: Apache-2.0. |
| 7 | */ |
| 8 | |
| 9 | #include <aws/common/byte_buf.h> |
| 10 | #include <aws/common/byte_order.h> |
| 11 | #include <aws/common/common.h> |
| 12 | |
| 13 | #include <memory.h> |
| 14 | |
| 15 | AWS_PUSH_SANE_WARNING_LEVEL |
| 16 | AWS_EXTERN_C_BEGIN |
| 17 | |
| 18 | /* |
| 19 | * computes the length necessary to store the result of aws_hex_encode(). |
| 20 | * returns -1 on failure, and 0 on success. encoded_length will be set on |
| 21 | * success. |
| 22 | */ |
| 23 | AWS_COMMON_API |
| 24 | int aws_hex_compute_encoded_len(size_t to_encode_len, size_t *encoded_length); |
| 25 | |
| 26 | /* |
| 27 | * Base 16 (hex) encodes the contents of to_encode and stores the result in |
| 28 | * output. Assumes the buffer is empty and does not resize on |
| 29 | * insufficient capacity. |
| 30 | */ |
| 31 | AWS_COMMON_API |
| 32 | int aws_hex_encode(const struct aws_byte_cursor *AWS_RESTRICT to_encode, struct aws_byte_buf *AWS_RESTRICT output); |
| 33 | |
| 34 | /* |
| 35 | * Base 16 (hex) encodes the contents of to_encode and appends the result in |
| 36 | * output. Grows the destination buffer dynamically if necessary. |
| 37 | */ |
| 38 | AWS_COMMON_API |
| 39 | int aws_hex_encode_append_dynamic( |
| 40 | const struct aws_byte_cursor *AWS_RESTRICT to_encode, |
| 41 | struct aws_byte_buf *AWS_RESTRICT output); |
| 42 | |
| 43 | /* |
| 44 | * computes the length necessary to store the result of aws_hex_decode(). |
| 45 | * returns -1 on failure, and 0 on success. decoded_len will be set on success. |
| 46 | */ |
| 47 | AWS_COMMON_API |
| 48 | int aws_hex_compute_decoded_len(size_t to_decode_len, size_t *decoded_len); |
| 49 | |
| 50 | /* |
| 51 | * Base 16 (hex) decodes the contents of to_decode and stores the result in |
| 52 | * output. If output is NULL, output_size will be set to what the output_size |
| 53 | * should be. |
| 54 | */ |
| 55 | AWS_COMMON_API |
| 56 | int aws_hex_decode(const struct aws_byte_cursor *AWS_RESTRICT to_decode, struct aws_byte_buf *AWS_RESTRICT output); |
| 57 | |
| 58 | /* |
| 59 | * Computes the length necessary to store the output of aws_base64_encode call. |
| 60 | * returns -1 on failure, and 0 on success. encoded_length will be set on |
| 61 | * success. |
| 62 | */ |
| 63 | AWS_COMMON_API |
| 64 | int aws_base64_compute_encoded_len(size_t to_encode_len, size_t *encoded_len); |
| 65 | |
| 66 | /* |
| 67 | * Computes the length necessary to store the output of aws_base64_url_encode call. |
| 68 | * returns -1 on failure, and 0 on success. encoded_length will be set on |
| 69 | * success. |
| 70 | */ |
| 71 | AWS_COMMON_API |
| 72 | int aws_base64_url_compute_encoded_len(size_t to_encode_len, size_t *encoded_len); |
| 73 | |
| 74 | /* |
| 75 | * Base 64 encodes the contents of to_encode and stores the result in output. |
| 76 | */ |
| 77 | AWS_COMMON_API |
| 78 | int aws_base64_encode(const struct aws_byte_cursor *AWS_RESTRICT to_encode, struct aws_byte_buf *AWS_RESTRICT output); |
| 79 | |
| 80 | /* |
| 81 | * Base 64 URL encodes the contents of to_encode and stores the result in output. |
| 82 | */ |
| 83 | AWS_COMMON_API |
| 84 | int aws_base64_url_encode( |
| 85 | const struct aws_byte_cursor *AWS_RESTRICT to_encode, |
| 86 | struct aws_byte_buf *AWS_RESTRICT output); |
| 87 | |
| 88 | /* |
| 89 | * Computes the length necessary to store the output of aws_base64_decode call. |
| 90 | * Note: works on both regular and url base64. |
| 91 | * returns -1 on failure, and 0 on success. decoded_len will be set on success. |
| 92 | */ |
| 93 | AWS_COMMON_API |
| 94 | int aws_base64_compute_decoded_len(const struct aws_byte_cursor *AWS_RESTRICT to_decode, size_t *decoded_len); |
| 95 | |
| 96 | /* |
| 97 | * Base 64 decodes the contents of to_decode and stores the result in output. |
| 98 | * Note: works on both regular and url base64. |
| 99 | */ |
| 100 | AWS_COMMON_API |
| 101 | int aws_base64_decode(const struct aws_byte_cursor *AWS_RESTRICT to_decode, struct aws_byte_buf *AWS_RESTRICT output); |
| 102 | |
| 103 | /* Add a 64 bit unsigned integer to the buffer, ensuring network - byte order |
| 104 | * Assumes the buffer size is at least 8 bytes. |
| 105 | */ |
| 106 | AWS_STATIC_IMPL void aws_write_u64(uint64_t value, uint8_t *buffer); |
| 107 | |
| 108 | /* |
| 109 | * Extracts a 64 bit unsigned integer from buffer. Ensures conversion from |
| 110 | * network byte order to host byte order. Assumes buffer size is at least 8 |
| 111 | * bytes. |
| 112 | */ |
| 113 | AWS_STATIC_IMPL uint64_t aws_read_u64(const uint8_t *buffer); |
| 114 | |
| 115 | /* Add a 32 bit unsigned integer to the buffer, ensuring network - byte order |
| 116 | * Assumes the buffer size is at least 4 bytes. |
| 117 | */ |
| 118 | AWS_STATIC_IMPL void aws_write_u32(uint32_t value, uint8_t *buffer); |
| 119 | |
| 120 | /* |
| 121 | * Extracts a 32 bit unsigned integer from buffer. Ensures conversion from |
| 122 | * network byte order to host byte order. Assumes the buffer size is at least 4 |
| 123 | * bytes. |
| 124 | */ |
| 125 | AWS_STATIC_IMPL uint32_t aws_read_u32(const uint8_t *buffer); |
| 126 | |
| 127 | /* Add a 24 bit unsigned integer to the buffer, ensuring network - byte order |
| 128 | * return the new position in the buffer for the next operation. |
| 129 | * Note, since this uses uint32_t for storage, the 3 least significant bytes |
| 130 | * will be used. Assumes buffer is at least 3 bytes long. |
| 131 | */ |
| 132 | AWS_STATIC_IMPL void aws_write_u24(uint32_t value, uint8_t *buffer); |
| 133 | /* |
| 134 | * Extracts a 24 bit unsigned integer from buffer. Ensures conversion from |
| 135 | * network byte order to host byte order. Assumes buffer is at least 3 bytes |
| 136 | * long. |
| 137 | */ |
| 138 | AWS_STATIC_IMPL uint32_t aws_read_u24(const uint8_t *buffer); |
| 139 | |
| 140 | /* Add a 16 bit unsigned integer to the buffer, ensuring network-byte order |
| 141 | * return the new position in the buffer for the next operation. |
| 142 | * Assumes buffer is at least 2 bytes long. |
| 143 | */ |
| 144 | AWS_STATIC_IMPL void aws_write_u16(uint16_t value, uint8_t *buffer); |
| 145 | /* |
| 146 | * Extracts a 16 bit unsigned integer from buffer. Ensures conversion from |
| 147 | * network byte order to host byte order. Assumes buffer is at least 2 bytes |
| 148 | * long. |
| 149 | */ |
| 150 | AWS_STATIC_IMPL uint16_t aws_read_u16(const uint8_t *buffer); |
| 151 | |
| 152 | enum aws_text_encoding { |
| 153 | AWS_TEXT_UNKNOWN, |
| 154 | AWS_TEXT_UTF8, |
| 155 | AWS_TEXT_UTF16, |
| 156 | AWS_TEXT_UTF32, |
| 157 | AWS_TEXT_ASCII, |
| 158 | }; |
| 159 | |
| 160 | /* Checks the BOM in the buffer to see if encoding can be determined. If there is no BOM or |
| 161 | * it is unrecognizable, then AWS_TEXT_UNKNOWN will be returned. |
| 162 | */ |
| 163 | AWS_STATIC_IMPL enum aws_text_encoding aws_text_detect_encoding(const uint8_t *bytes, size_t size); |
| 164 | |
| 165 | /* |
| 166 | * Returns true if aws_text_detect_encoding() determines the text is UTF8 or ASCII. |
| 167 | * Note that this immediately returns true if the UTF8 BOM is seen. |
| 168 | * To fully validate every byte, use aws_decode_utf8(). |
| 169 | */ |
| 170 | AWS_STATIC_IMPL bool aws_text_is_utf8(const uint8_t *bytes, size_t size); |
| 171 | |
| 172 | struct aws_utf8_decoder_options { |
| 173 | /** |
| 174 | * Optional. |
| 175 | * Callback invoked for each Unicode codepoint. |
| 176 | * Use this callback to store codepoints as they're decoded, |
| 177 | * or to perform additional validation. RFC-3629 is already enforced, |
| 178 | * which forbids codepoints between U+D800 and U+DFFF, |
| 179 | * but you may whish to forbid codepoints like U+0000. |
| 180 | * |
| 181 | * @return AWS_OP_SUCCESS to continue processing the string, otherwise |
| 182 | * return AWS_OP_ERROR and raise an error (i.e. AWS_ERROR_INVALID_UTF8) |
| 183 | * to stop processing the string and report failure. |
| 184 | */ |
| 185 | int (*on_codepoint)(uint32_t codepoint, void *user_data); |
| 186 | |
| 187 | /* Optional. Pointer passed to on_codepoint callback. */ |
| 188 | void *user_data; |
| 189 | }; |
| 190 | |
| 191 | /** |
| 192 | * Decode a complete string of UTF8/ASCII text. |
| 193 | * Text is always validated according to RFC-3629 (you may perform additional |
| 194 | * validation in the on_codepoint callback). |
| 195 | * The text does not need to begin with a UTF8 BOM. |
| 196 | * If you need to decode text incrementally as you receive it, use aws_utf8_decoder_new() instead. |
| 197 | * |
| 198 | * @param bytes Text to decode. |
| 199 | * @param options Options for decoding. If NULL is passed, the text is simply validated. |
| 200 | * |
| 201 | * @return AWS_OP_SUCCESS if successful. |
| 202 | * An error is raised if the text is not valid, or the on_codepoint callback raises an error. |
| 203 | */ |
| 204 | AWS_COMMON_API int aws_decode_utf8(struct aws_byte_cursor bytes, const struct aws_utf8_decoder_options *options); |
| 205 | |
| 206 | struct aws_utf8_decoder; |
| 207 | |
| 208 | /** |
| 209 | * Create a UTF8/ASCII decoder, which can process text incrementally as you receive it. |
| 210 | * Text is always validated according to RFC-3629 (you may perform additional |
| 211 | * validation in the on_codepoint callback). |
| 212 | * The text does not need to begin with a UTF8 BOM. |
| 213 | * To decode text all at once, simply use aws_decode_utf8(). |
| 214 | * |
| 215 | * Feed bytes into the decoder with aws_utf8_decoder_update(), |
| 216 | * and call aws_utf8_decoder_finalize() when the text is complete. |
| 217 | * |
| 218 | * @param allocator Allocator |
| 219 | * @param options Options for decoder. If NULL is passed, the text is simply validated. |
| 220 | */ |
| 221 | AWS_COMMON_API struct aws_utf8_decoder *aws_utf8_decoder_new( |
| 222 | struct aws_allocator *allocator, |
| 223 | const struct aws_utf8_decoder_options *options); |
| 224 | |
| 225 | AWS_COMMON_API void aws_utf8_decoder_destroy(struct aws_utf8_decoder *decoder); |
| 226 | AWS_COMMON_API void aws_utf8_decoder_reset(struct aws_utf8_decoder *decoder); |
| 227 | |
| 228 | /** |
| 229 | * Update the decoder with more bytes of text. |
| 230 | * The on_codepoint callback will be invoked for each codepoint encountered. |
| 231 | * Raises an error if invalid UTF8 is encountered or the on_codepoint callback reports an error. |
| 232 | * |
| 233 | * Note: You must call aws_utf8_decoder_finalize() when the text is 100% complete, |
| 234 | * to ensure the input was completely valid. |
| 235 | */ |
| 236 | AWS_COMMON_API int aws_utf8_decoder_update(struct aws_utf8_decoder *decoder, struct aws_byte_cursor bytes); |
| 237 | |
| 238 | /** |
| 239 | * Tell the decoder that you've reached the end of your text. |
| 240 | * Raises AWS_ERROR_INVALID_UTF8 if the text did not end with a complete UTF8 codepoint. |
| 241 | * This also resets the decoder. |
| 242 | */ |
| 243 | AWS_COMMON_API int aws_utf8_decoder_finalize(struct aws_utf8_decoder *decoder); |
| 244 | |
| 245 | AWS_EXTERN_C_END |
| 246 | |
| 247 | #ifndef AWS_NO_STATIC_IMPL |
| 248 | # include <aws/common/encoding.inl> |
| 249 | #endif /* AWS_NO_STATIC_IMPL */ |
| 250 | |
| 251 | AWS_POP_SANE_WARNING_LEVEL |
| 252 | |
| 253 | #endif /* AWS_COMMON_ENCODING_H */ |