| 1 |
#ifndef AWS_COMMON_ENCODING_INL |
| 2 |
#define AWS_COMMON_ENCODING_INL |
| 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 |
#include <aws/common/encoding.h> |
| 13 |
|
| 14 |
AWS_EXTERN_C_BEGIN |
| 15 |
|
| 16 |
/* Add a 64 bit unsigned integer to the buffer, ensuring network - byte order |
| 17 |
* Assumes the buffer size is at least 8 bytes. |
| 18 |
*/ |
| 19 |
AWS_STATIC_IMPL void aws_write_u64(uint64_t value, uint8_t *buffer) { |
| 20 |
value = aws_hton64(value); |
| 21 |
|
| 22 |
memcpy((void *)buffer, &value, sizeof(value)); |
| 23 |
} |
| 24 |
|
| 25 |
/* |
| 26 |
* Extracts a 64 bit unsigned integer from buffer. Ensures conversion from |
| 27 |
* network byte order to host byte order. Assumes buffer size is at least 8 |
| 28 |
* bytes. |
| 29 |
*/ |
| 30 |
AWS_STATIC_IMPL uint64_t aws_read_u64(const uint8_t *buffer) { |
| 31 |
uint64_t value = 0; |
| 32 |
memcpy((void *)&value, (void *)buffer, sizeof(value)); |
| 33 |
|
| 34 |
return aws_ntoh64(value); |
| 35 |
} |
| 36 |
|
| 37 |
/* Add a 32 bit unsigned integer to the buffer, ensuring network - byte order |
| 38 |
* Assumes the buffer size is at least 4 bytes. |
| 39 |
*/ |
| 40 |
AWS_STATIC_IMPL void aws_write_u32(uint32_t value, uint8_t *buffer) { |
| 41 |
value = aws_hton32(value); |
| 42 |
|
| 43 |
memcpy((void *)buffer, (void *)&value, sizeof(value)); |
| 44 |
} |
| 45 |
|
| 46 |
/* |
| 47 |
* Extracts a 32 bit unsigned integer from buffer. Ensures conversion from |
| 48 |
* network byte order to host byte order. Assumes the buffer size is at least 4 |
| 49 |
* bytes. |
| 50 |
*/ |
| 51 |
AWS_STATIC_IMPL uint32_t aws_read_u32(const uint8_t *buffer) { |
| 52 |
uint32_t value = 0; |
| 53 |
memcpy((void *)&value, (void *)buffer, sizeof(value)); |
| 54 |
|
| 55 |
return aws_ntoh32(value); |
| 56 |
} |
| 57 |
|
| 58 |
/* Add a 24 bit unsigned integer to the buffer, ensuring network - byte order |
| 59 |
* return the new position in the buffer for the next operation. |
| 60 |
* Note, since this uses uint32_t for storage, the 3 least significant bytes |
| 61 |
* will be used. Assumes buffer is at least 3 bytes long. |
| 62 |
*/ |
| 63 |
AWS_STATIC_IMPL void aws_write_u24(uint32_t value, uint8_t *buffer) { |
| 64 |
value = aws_hton32(value); |
| 65 |
memcpy((void *)buffer, (void *)((uint8_t *)&value + 1), sizeof(value) - 1); |
| 66 |
} |
| 67 |
|
| 68 |
/* |
| 69 |
* Extracts a 24 bit unsigned integer from buffer. Ensures conversion from |
| 70 |
* network byte order to host byte order. Assumes buffer is at least 3 bytes |
| 71 |
* long. |
| 72 |
*/ |
| 73 |
AWS_STATIC_IMPL uint32_t aws_read_u24(const uint8_t *buffer) { |
| 74 |
uint32_t value = 0; |
| 75 |
memcpy((void *)((uint8_t *)&value + 1), (void *)buffer, sizeof(value) - 1); |
| 76 |
|
| 77 |
return aws_ntoh32(value); |
| 78 |
} |
| 79 |
|
| 80 |
/* Add a 16 bit unsigned integer to the buffer, ensuring network-byte order |
| 81 |
* return the new position in the buffer for the next operation. |
| 82 |
* Assumes buffer is at least 2 bytes long. |
| 83 |
*/ |
| 84 |
AWS_STATIC_IMPL void aws_write_u16(uint16_t value, uint8_t *buffer) { |
| 85 |
value = aws_hton16(value); |
| 86 |
|
| 87 |
memcpy((void *)buffer, (void *)&value, sizeof(value)); |
| 88 |
} |
| 89 |
|
| 90 |
/* |
| 91 |
* Extracts a 16 bit unsigned integer from buffer. Ensures conversion from |
| 92 |
* network byte order to host byte order. Assumes buffer is at least 2 bytes |
| 93 |
* long. |
| 94 |
*/ |
| 95 |
AWS_STATIC_IMPL uint16_t aws_read_u16(const uint8_t *buffer) { |
| 96 |
uint16_t value = 0; |
| 97 |
memcpy((void *)&value, (void *)buffer, sizeof(value)); |
| 98 |
|
| 99 |
return aws_ntoh16(value); |
| 100 |
} |
| 101 |
|
| 102 |
/* Reference: https://unicodebook.readthedocs.io/guess_encoding.html */ |
| 103 |
AWS_STATIC_IMPL enum aws_text_encoding aws_text_detect_encoding(const uint8_t *bytes, size_t size) { |
| 104 |
static const char *UTF_8_BOM = "\xEF\xBB\xBF"; |
| 105 |
static const char *UTF_16_BE_BOM = "\xFE\xFF"; |
| 106 |
static const char *UTF_16_LE_BOM = "\xFF\xFE"; |
| 107 |
static const char *UTF_32_BE_BOM = "\x00\x00\xFE\xFF"; |
| 108 |
static const char *UTF_32_LE_BOM = "\xFF\xFE\x00\x00"; |
| 109 |
|
| 110 |
if (size >= 3) { |
| 111 |
if (memcmp(bytes, UTF_8_BOM, 3) == 0) |
| 112 |
return AWS_TEXT_UTF8; |
| 113 |
} |
| 114 |
if (size >= 4) { |
| 115 |
if (memcmp(bytes, UTF_32_LE_BOM, 4) == 0) |
| 116 |
return AWS_TEXT_UTF32; |
| 117 |
if (memcmp(bytes, UTF_32_BE_BOM, 4) == 0) |
| 118 |
return AWS_TEXT_UTF32; |
| 119 |
} |
| 120 |
if (size >= 2) { |
| 121 |
if (memcmp(bytes, UTF_16_LE_BOM, 2) == 0) |
| 122 |
return AWS_TEXT_UTF16; |
| 123 |
if (memcmp(bytes, UTF_16_BE_BOM, 2) == 0) |
| 124 |
return AWS_TEXT_UTF16; |
| 125 |
} |
| 126 |
size_t idx = 0; |
| 127 |
for (; idx < size; ++idx) { |
| 128 |
if (bytes[idx] & 0x80) { |
| 129 |
return AWS_TEXT_UNKNOWN; |
| 130 |
} |
| 131 |
} |
| 132 |
return AWS_TEXT_ASCII; |
| 133 |
} |
| 134 |
|
| 135 |
AWS_STATIC_IMPL bool aws_text_is_utf8(const uint8_t *bytes, size_t size) { |
| 136 |
enum aws_text_encoding encoding = aws_text_detect_encoding(bytes, size); |
| 137 |
return encoding == AWS_TEXT_UTF8 || encoding == AWS_TEXT_ASCII; |
| 138 |
} |
| 139 |
|
| 140 |
AWS_EXTERN_C_END |
| 141 |
|
| 142 |
#endif /* AWS_COMMON_ENCODING_INL */ |