| 1 | #ifndef AWS_COMMON_STRING_H |
| 2 | #define AWS_COMMON_STRING_H |
| 3 | /** |
| 4 | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 5 | * SPDX-License-Identifier: Apache-2.0. |
| 6 | */ |
| 7 | #include <aws/common/byte_buf.h> |
| 8 | #include <aws/common/common.h> |
| 9 | |
| 10 | AWS_PUSH_SANE_WARNING_LEVEL |
| 11 | |
| 12 | /** |
| 13 | * Represents an immutable string holding either text or binary data. If the |
| 14 | * string is in constant memory or memory that should otherwise not be freed by |
| 15 | * this struct, set allocator to NULL and destroy function will be a no-op. |
| 16 | * |
| 17 | * This is for use cases where the entire struct and the data bytes themselves |
| 18 | * need to be held in dynamic memory, such as when held by a struct |
| 19 | * aws_hash_table. The data bytes themselves are always held in contiguous |
| 20 | * memory immediately after the end of the struct aws_string, and the memory for |
| 21 | * both the header and the data bytes is allocated together. |
| 22 | * |
| 23 | * Use the aws_string_bytes function to access the data bytes. A null byte is |
| 24 | * always included immediately after the data but not counted in the length, so |
| 25 | * that the output of aws_string_bytes can be treated as a C-string in cases |
| 26 | * where none of the the data bytes are null. |
| 27 | * |
| 28 | * Note that the fields of this structure are const; this ensures not only that |
| 29 | * they cannot be modified, but also that you can't assign the structure using |
| 30 | * the = operator accidentally. |
| 31 | */ |
| 32 | |
| 33 | /* Using a flexible array member is the C99 compliant way to have the bytes of |
| 34 | * the string immediately follow the header. |
| 35 | * |
| 36 | * MSVC doesn't know this for some reason so we need to use a pragma to make |
| 37 | * it happy. |
| 38 | */ |
| 39 | #ifdef _MSC_VER |
| 40 | # pragma warning(push) |
| 41 | # pragma warning(disable : 4623) /* default constructor was implicitly defined as deleted */ |
| 42 | # pragma warning(disable : 4626) /* assignment operator was implicitly defined as deleted */ |
| 43 | # pragma warning(disable : 5027) /* move assignment operator was implicitly defined as deleted */ |
| 44 | #endif |
| 45 | struct aws_string { |
| 46 | struct aws_allocator *const allocator; |
| 47 | /* size in bytes of `bytes` minus any null terminator. |
| 48 | * NOTE: This is not the number of characters in the string. */ |
| 49 | const size_t len; |
| 50 | /* give this a storage specifier for C++ purposes. It will likely be larger after init. */ |
| 51 | const uint8_t bytes[1]; |
| 52 | }; |
| 53 | |
| 54 | #ifdef AWS_OS_WINDOWS |
| 55 | struct aws_wstring { |
| 56 | struct aws_allocator *const allocator; |
| 57 | /* number of characters in the string not including the null terminator. */ |
| 58 | const size_t len; |
| 59 | /* give this a storage specifier for C++ purposes. It will likely be larger after init. */ |
| 60 | const wchar_t bytes[1]; |
| 61 | }; |
| 62 | #endif /* AWS_OS_WINDOWS */ |
| 63 | |
| 64 | #ifdef _MSC_VER |
| 65 | # pragma warning(pop) |
| 66 | #endif |
| 67 | |
| 68 | AWS_EXTERN_C_BEGIN |
| 69 | |
| 70 | #ifdef AWS_OS_WINDOWS |
| 71 | /** |
| 72 | * For windows only. Converts `to_convert` to a windows whcar format (UTF-16) for use with windows OS interop. |
| 73 | * |
| 74 | * Note: `to_convert` is assumed to be UTF-8 or ASCII. |
| 75 | * |
| 76 | * returns NULL on failure. |
| 77 | */ |
| 78 | AWS_COMMON_API struct aws_wstring *aws_string_convert_to_wstring( |
| 79 | struct aws_allocator *allocator, |
| 80 | const struct aws_string *to_convert); |
| 81 | |
| 82 | /** |
| 83 | * For windows only. Converts `to_convert` to a windows whcar format (UTF-16) for use with windows OS interop. |
| 84 | * |
| 85 | * Note: `to_convert` is assumed to be UTF-8 or ASCII. |
| 86 | * |
| 87 | * returns NULL on failure. |
| 88 | */ |
| 89 | AWS_COMMON_API struct aws_wstring *aws_string_convert_to_wchar_from_byte_cursor( |
| 90 | struct aws_allocator *allocator, |
| 91 | const struct aws_byte_cursor *to_convert); |
| 92 | |
| 93 | /** |
| 94 | * clean up str. |
| 95 | */ |
| 96 | AWS_COMMON_API |
| 97 | void aws_wstring_destroy(struct aws_wstring *str); |
| 98 | |
| 99 | /** |
| 100 | * For windows only. Converts `to_convert` from a windows whcar format (UTF-16) to UTF-8. |
| 101 | * |
| 102 | * Note: `to_convert` is assumed to be wchar already. |
| 103 | * |
| 104 | * returns NULL on failure. |
| 105 | */ |
| 106 | AWS_COMMON_API struct aws_string *aws_string_convert_from_wchar_str( |
| 107 | struct aws_allocator *allocator, |
| 108 | const struct aws_wstring *to_convert); |
| 109 | |
| 110 | /** |
| 111 | * For windows only. Converts `to_convert` from a windows whcar format (UTF-16) to UTF-8. |
| 112 | * |
| 113 | * Note: `to_convert` is assumed to be wchar already. |
| 114 | * |
| 115 | * returns NULL on failure. |
| 116 | */ |
| 117 | AWS_COMMON_API struct aws_string *aws_string_convert_from_wchar_byte_cursor( |
| 118 | struct aws_allocator *allocator, |
| 119 | const struct aws_byte_cursor *to_convert); |
| 120 | |
| 121 | /** |
| 122 | * For windows only. Converts `to_convert` from a windows whcar format (UTF-16) to UTF-8. |
| 123 | * |
| 124 | * Note: `to_convert` is assumed to be wchar already. |
| 125 | * |
| 126 | * returns NULL on failure. |
| 127 | */ |
| 128 | AWS_COMMON_API struct aws_string *aws_string_convert_from_wchar_c_str( |
| 129 | struct aws_allocator *allocator, |
| 130 | const wchar_t *to_convert); |
| 131 | |
| 132 | /** |
| 133 | * Create a new wide string from a byte cursor. This assumes that w_str_cur is already in utf-16. |
| 134 | * |
| 135 | * returns NULL on failure. |
| 136 | */ |
| 137 | AWS_COMMON_API struct aws_wstring *aws_wstring_new_from_cursor( |
| 138 | struct aws_allocator *allocator, |
| 139 | const struct aws_byte_cursor *w_str_cur); |
| 140 | |
| 141 | /** |
| 142 | * Create a new wide string from a utf-16 string enclosing array. The length field is in number of characters not |
| 143 | * counting the null terminator. |
| 144 | * |
| 145 | * returns NULL on failure. |
| 146 | */ |
| 147 | AWS_COMMON_API struct aws_wstring *aws_wstring_new_from_array( |
| 148 | struct aws_allocator *allocator, |
| 149 | const wchar_t *w_str, |
| 150 | size_t length); |
| 151 | |
| 152 | /** |
| 153 | * Returns a wchar_t * pointer for use with windows OS interop. |
| 154 | */ |
| 155 | AWS_COMMON_API const wchar_t *aws_wstring_c_str(const struct aws_wstring *str); |
| 156 | |
| 157 | /** |
| 158 | * Returns the number of characters in the wchar string. NOTE: This is not the length in bytes or the buffer size. |
| 159 | */ |
| 160 | AWS_COMMON_API size_t aws_wstring_num_chars(const struct aws_wstring *str); |
| 161 | |
| 162 | /** |
| 163 | * Returns the length in bytes for the buffer. |
| 164 | */ |
| 165 | AWS_COMMON_API size_t aws_wstring_size_bytes(const struct aws_wstring *str); |
| 166 | |
| 167 | /** |
| 168 | * Verifies that str is a valid string. Returns true if it's valid and false otherwise. |
| 169 | */ |
| 170 | AWS_COMMON_API bool aws_wstring_is_valid(const struct aws_wstring *str); |
| 171 | |
| 172 | #endif /* AWS_OS_WINDOWS */ |
| 173 | |
| 174 | /** |
| 175 | * Returns true if bytes of string are the same, false otherwise. |
| 176 | */ |
| 177 | AWS_COMMON_API |
| 178 | bool aws_string_eq(const struct aws_string *a, const struct aws_string *b); |
| 179 | |
| 180 | /** |
| 181 | * Returns true if bytes of string are equivalent, using a case-insensitive comparison. |
| 182 | */ |
| 183 | AWS_COMMON_API |
| 184 | bool aws_string_eq_ignore_case(const struct aws_string *a, const struct aws_string *b); |
| 185 | |
| 186 | /** |
| 187 | * Returns true if bytes of string and cursor are the same, false otherwise. |
| 188 | */ |
| 189 | AWS_COMMON_API |
| 190 | bool aws_string_eq_byte_cursor(const struct aws_string *str, const struct aws_byte_cursor *cur); |
| 191 | |
| 192 | /** |
| 193 | * Returns true if bytes of string and cursor are equivalent, using a case-insensitive comparison. |
| 194 | */ |
| 195 | AWS_COMMON_API |
| 196 | bool aws_string_eq_byte_cursor_ignore_case(const struct aws_string *str, const struct aws_byte_cursor *cur); |
| 197 | |
| 198 | /** |
| 199 | * Returns true if bytes of string and buffer are the same, false otherwise. |
| 200 | */ |
| 201 | AWS_COMMON_API |
| 202 | bool aws_string_eq_byte_buf(const struct aws_string *str, const struct aws_byte_buf *buf); |
| 203 | |
| 204 | /** |
| 205 | * Returns true if bytes of string and buffer are equivalent, using a case-insensitive comparison. |
| 206 | */ |
| 207 | AWS_COMMON_API |
| 208 | bool aws_string_eq_byte_buf_ignore_case(const struct aws_string *str, const struct aws_byte_buf *buf); |
| 209 | |
| 210 | AWS_COMMON_API |
| 211 | bool aws_string_eq_c_str(const struct aws_string *str, const char *c_str); |
| 212 | |
| 213 | /** |
| 214 | * Returns true if bytes of strings are equivalent, using a case-insensitive comparison. |
| 215 | */ |
| 216 | AWS_COMMON_API |
| 217 | bool aws_string_eq_c_str_ignore_case(const struct aws_string *str, const char *c_str); |
| 218 | |
| 219 | /** |
| 220 | * Constructor functions which copy data from null-terminated C-string or array of bytes. |
| 221 | */ |
| 222 | AWS_COMMON_API |
| 223 | struct aws_string *aws_string_new_from_c_str(struct aws_allocator *allocator, const char *c_str); |
| 224 | |
| 225 | /** |
| 226 | * Allocate a new string with the same contents as array. |
| 227 | */ |
| 228 | AWS_COMMON_API |
| 229 | struct aws_string *aws_string_new_from_array(struct aws_allocator *allocator, const uint8_t *bytes, size_t len); |
| 230 | |
| 231 | /** |
| 232 | * Allocate a new string with the same contents as another string. |
| 233 | */ |
| 234 | AWS_COMMON_API |
| 235 | struct aws_string *aws_string_new_from_string(struct aws_allocator *allocator, const struct aws_string *str); |
| 236 | |
| 237 | /** |
| 238 | * Allocate a new string with the same contents as cursor. |
| 239 | */ |
| 240 | AWS_COMMON_API |
| 241 | struct aws_string *aws_string_new_from_cursor(struct aws_allocator *allocator, const struct aws_byte_cursor *cursor); |
| 242 | |
| 243 | /** |
| 244 | * Allocate a new string with the same contents as buf. |
| 245 | */ |
| 246 | AWS_COMMON_API |
| 247 | struct aws_string *aws_string_new_from_buf(struct aws_allocator *allocator, const struct aws_byte_buf *buf); |
| 248 | |
| 249 | /** |
| 250 | * Deallocate string. |
| 251 | */ |
| 252 | AWS_COMMON_API |
| 253 | void aws_string_destroy(struct aws_string *str); |
| 254 | |
| 255 | /** |
| 256 | * Zeroes out the data bytes of string and then deallocates the memory. |
| 257 | * Not safe to run on a string created with AWS_STATIC_STRING_FROM_LITERAL. |
| 258 | */ |
| 259 | AWS_COMMON_API |
| 260 | void aws_string_destroy_secure(struct aws_string *str); |
| 261 | |
| 262 | /** |
| 263 | * Compares lexicographical ordering of two strings. This is a binary |
| 264 | * byte-by-byte comparison, treating bytes as unsigned integers. It is suitable |
| 265 | * for either textual or binary data and is unaware of unicode or any other byte |
| 266 | * encoding. If both strings are identical in the bytes of the shorter string, |
| 267 | * then the longer string is lexicographically after the shorter. |
| 268 | * |
| 269 | * Returns a positive number if string a > string b. (i.e., string a is |
| 270 | * lexicographically after string b.) Returns zero if string a = string b. |
| 271 | * Returns negative number if string a < string b. |
| 272 | */ |
| 273 | AWS_COMMON_API |
| 274 | int aws_string_compare(const struct aws_string *a, const struct aws_string *b); |
| 275 | |
| 276 | /** |
| 277 | * A convenience function for sorting lists of (const struct aws_string *) elements. This can be used as a |
| 278 | * comparator for aws_array_list_sort. It is just a simple wrapper around aws_string_compare. |
| 279 | */ |
| 280 | AWS_COMMON_API |
| 281 | int aws_array_list_comparator_string(const void *a, const void *b); |
| 282 | |
| 283 | /** |
| 284 | * Defines a (static const struct aws_string *) with name specified in first |
| 285 | * argument that points to constant memory and has data bytes containing the |
| 286 | * string literal in the second argument. |
| 287 | * |
| 288 | * GCC allows direct initilization of structs with variable length final fields |
| 289 | * However, this might not be portable, so we can do this instead |
| 290 | * This will have to be updated whenever the aws_string structure changes |
| 291 | */ |
| 292 | #define AWS_STATIC_STRING_FROM_LITERAL(name, literal) \ |
| 293 | static const struct { \ |
| 294 | struct aws_allocator *const allocator; \ |
| 295 | const size_t len; \ |
| 296 | const uint8_t bytes[sizeof(literal)]; \ |
| 297 | } name##_s = {NULL, sizeof(literal) - 1, literal}; \ |
| 298 | static const struct aws_string *name = (struct aws_string *)(&name##_s) /* NOLINT(bugprone-macro-parentheses) */ |
| 299 | |
| 300 | /* NOLINT above is because clang-tidy complains that (name) isn't in parentheses, |
| 301 | * but gcc8-c++ complains that the parentheses are unnecessary */ |
| 302 | |
| 303 | /* |
| 304 | * A related macro that declares the string pointer without static, allowing it to be externed as a global constant |
| 305 | */ |
| 306 | #define AWS_STRING_FROM_LITERAL(name, literal) \ |
| 307 | static const struct { \ |
| 308 | struct aws_allocator *const allocator; \ |
| 309 | const size_t len; \ |
| 310 | const uint8_t bytes[sizeof(literal)]; \ |
| 311 | } name##_s = {NULL, sizeof(literal) - 1, literal}; \ |
| 312 | const struct aws_string *(name) = (struct aws_string *)(&name##_s) |
| 313 | |
| 314 | /** |
| 315 | * Copies all bytes from string to buf. |
| 316 | * |
| 317 | * On success, returns true and updates the buf pointer/length |
| 318 | * accordingly. If there is insufficient space in the buf, returns |
| 319 | * false, leaving the buf unchanged. |
| 320 | */ |
| 321 | AWS_COMMON_API |
| 322 | bool aws_byte_buf_write_from_whole_string( |
| 323 | struct aws_byte_buf *AWS_RESTRICT buf, |
| 324 | const struct aws_string *AWS_RESTRICT src); |
| 325 | |
| 326 | /** |
| 327 | * Creates an aws_byte_cursor from an existing string. |
| 328 | * If the src is NULL, it returns an empty cursor |
| 329 | */ |
| 330 | AWS_COMMON_API |
| 331 | struct aws_byte_cursor aws_byte_cursor_from_string(const struct aws_string *src); |
| 332 | |
| 333 | /** |
| 334 | * If the string was dynamically allocated, clones it. If the string was statically allocated (i.e. has no allocator), |
| 335 | * returns the original string. |
| 336 | */ |
| 337 | AWS_COMMON_API |
| 338 | struct aws_string *aws_string_clone_or_reuse(struct aws_allocator *allocator, const struct aws_string *str); |
| 339 | |
| 340 | /** Computes the length of a c string in bytes assuming the character set is either ASCII or UTF-8. If no NULL character |
| 341 | * is found within max_read_len of str, AWS_ERROR_C_STRING_BUFFER_NOT_NULL_TERMINATED is raised. Otherwise, str_len |
| 342 | * will contain the string length minus the NULL character, and AWS_OP_SUCCESS will be returned. */ |
| 343 | AWS_COMMON_API |
| 344 | int aws_secure_strlen(const char *str, size_t max_read_len, size_t *str_len); |
| 345 | |
| 346 | /** |
| 347 | * Equivalent to str->bytes. |
| 348 | */ |
| 349 | AWS_STATIC_IMPL |
| 350 | const uint8_t *aws_string_bytes(const struct aws_string *str); |
| 351 | |
| 352 | /** |
| 353 | * Equivalent to `(const char *)str->bytes`. |
| 354 | */ |
| 355 | AWS_STATIC_IMPL |
| 356 | const char *aws_string_c_str(const struct aws_string *str); |
| 357 | |
| 358 | /** |
| 359 | * Evaluates the set of properties that define the shape of all valid aws_string structures. |
| 360 | * It is also a cheap check, in the sense it run in constant time (i.e., no loops or recursion). |
| 361 | */ |
| 362 | AWS_STATIC_IMPL |
| 363 | bool aws_string_is_valid(const struct aws_string *str); |
| 364 | |
| 365 | /** |
| 366 | * Best-effort checks aws_string invariants, when the str->len is unknown |
| 367 | */ |
| 368 | AWS_STATIC_IMPL |
| 369 | bool aws_c_string_is_valid(const char *str); |
| 370 | |
| 371 | /** |
| 372 | * Evaluates if a char is a white character. |
| 373 | */ |
| 374 | AWS_STATIC_IMPL |
| 375 | bool aws_char_is_space(uint8_t c); |
| 376 | |
| 377 | AWS_EXTERN_C_END |
| 378 | |
| 379 | #ifndef AWS_NO_STATIC_IMPL |
| 380 | # include <aws/common/string.inl> |
| 381 | #endif /* AWS_NO_STATIC_IMPL */ |
| 382 | |
| 383 | AWS_POP_SANE_WARNING_LEVEL |
| 384 | |
| 385 | #endif /* AWS_COMMON_STRING_H */ |