| 1 | #ifndef AWS_COMMON_BYTE_BUF_H |
| 2 | #define AWS_COMMON_BYTE_BUF_H |
| 3 | /** |
| 4 | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 5 | * SPDX-License-Identifier: Apache-2.0. |
| 6 | */ |
| 7 | |
| 8 | #include <aws/common/array_list.h> |
| 9 | #include <aws/common/byte_order.h> |
| 10 | #include <aws/common/common.h> |
| 11 | |
| 12 | #include <string.h> |
| 13 | |
| 14 | AWS_PUSH_SANE_WARNING_LEVEL |
| 15 | |
| 16 | /** |
| 17 | * Represents a length-delimited binary string or buffer. If byte buffer points |
| 18 | * to constant memory or memory that should otherwise not be freed by this |
| 19 | * struct, set allocator to NULL and free function will be a no-op. |
| 20 | * |
| 21 | * This structure used to define the output for all functions that write to a buffer. |
| 22 | * |
| 23 | * Note that this structure allocates memory at the buffer pointer only. The |
| 24 | * struct itself does not get dynamically allocated and must be either |
| 25 | * maintained or copied to avoid losing access to the memory. |
| 26 | * |
| 27 | * GROWABILITY CONTRACT: |
| 28 | * - When `allocator != NULL`: the buffer owns its memory and can be grown |
| 29 | * via aws_byte_buf_reserve / aws_byte_buf_append_dynamic / etc. |
| 30 | * - When `allocator == NULL`: the buffer's memory is externally owned |
| 31 | * (e.g. by a buffer pool, by static storage, or by a stack frame) and |
| 32 | * MUST NOT be reallocated. Functions that would grow the buffer |
| 33 | * (aws_byte_buf_reserve, aws_byte_buf_append_dynamic, etc.) fail with |
| 34 | * AWS_ERROR_INVALID_ARGUMENT when called on such a buffer. |
| 35 | * |
| 36 | * Callers that may receive a buffer of either kind (e.g. from a buffer |
| 37 | * pool ticket) should use aws_byte_buf_append_auto, which selects |
| 38 | * the right append path automatically. |
| 39 | */ |
| 40 | struct aws_byte_buf { |
| 41 | /* do not reorder this, this struct lines up nicely with windows buffer structures--saving us allocations.*/ |
| 42 | size_t len; |
| 43 | uint8_t *buffer; |
| 44 | size_t capacity; |
| 45 | struct aws_allocator *allocator; |
| 46 | }; |
| 47 | |
| 48 | /** |
| 49 | * Represents a movable pointer within a larger binary string or buffer. |
| 50 | * |
| 51 | * This structure is used to define buffers for reading. |
| 52 | */ |
| 53 | struct aws_byte_cursor { |
| 54 | /* do not reorder this, this struct lines up nicely with windows buffer structures--saving us allocations */ |
| 55 | size_t len; |
| 56 | uint8_t *ptr; |
| 57 | }; |
| 58 | |
| 59 | /** |
| 60 | * Helper macro for passing aws_byte_cursor to the printf family of functions. |
| 61 | * Intended for use with the PRInSTR format macro. |
| 62 | * Ex: printf(PRInSTR "\n", AWS_BYTE_CURSOR_PRI(my_cursor)); |
| 63 | */ |
| 64 | #define AWS_BYTE_CURSOR_PRI(C) ((int)(C).len < 0 ? 0 : (int)(C).len), (const char *)(C).ptr |
| 65 | |
| 66 | /** |
| 67 | * Helper macro for passing aws_byte_buf to the printf family of functions. |
| 68 | * Intended for use with the PRInSTR format macro. |
| 69 | * Ex: printf(PRInSTR "\n", AWS_BYTE_BUF_PRI(my_buf)); |
| 70 | */ |
| 71 | #define AWS_BYTE_BUF_PRI(B) ((int)(B).len < 0 ? 0 : (int)(B).len), (const char *)(B).buffer |
| 72 | |
| 73 | /** |
| 74 | * Helper Macro for initializing a byte cursor from a string literal |
| 75 | */ |
| 76 | #define AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(literal) \ |
| 77 | {.ptr = (uint8_t *)(const char *)(literal), .len = sizeof(literal) - 1} |
| 78 | |
| 79 | /** |
| 80 | * Signature for function argument to trim APIs |
| 81 | */ |
| 82 | typedef bool(aws_byte_predicate_fn)(uint8_t value); |
| 83 | |
| 84 | AWS_EXTERN_C_BEGIN |
| 85 | |
| 86 | /** |
| 87 | * Compare two arrays. |
| 88 | * Return whether their contents are equivalent. |
| 89 | * NULL may be passed as the array pointer if its length is declared to be 0. |
| 90 | */ |
| 91 | AWS_COMMON_API |
| 92 | bool aws_array_eq(const void *const array_a, const size_t len_a, const void *array_b, const size_t len_b); |
| 93 | |
| 94 | /** |
| 95 | * Perform a case-insensitive string comparison of two arrays. |
| 96 | * Return whether their contents are equivalent. |
| 97 | * NULL may be passed as the array pointer if its length is declared to be 0. |
| 98 | * The "C" locale is used for comparing upper and lowercase letters. |
| 99 | * Data is assumed to be ASCII text, UTF-8 will work fine too. |
| 100 | */ |
| 101 | AWS_COMMON_API |
| 102 | bool aws_array_eq_ignore_case( |
| 103 | const void *const array_a, |
| 104 | const size_t len_a, |
| 105 | const void *const array_b, |
| 106 | const size_t len_b); |
| 107 | |
| 108 | /** |
| 109 | * Compare an array and a null-terminated string. |
| 110 | * Returns true if their contents are equivalent. |
| 111 | * The array should NOT contain a null-terminator, or the comparison will always return false. |
| 112 | * NULL may be passed as the array pointer if its length is declared to be 0. |
| 113 | */ |
| 114 | AWS_COMMON_API |
| 115 | bool aws_array_eq_c_str(const void *const array, const size_t array_len, const char *const c_str); |
| 116 | |
| 117 | /** |
| 118 | * Perform a case-insensitive string comparison of an array and a null-terminated string. |
| 119 | * Return whether their contents are equivalent. |
| 120 | * The array should NOT contain a null-terminator, or the comparison will always return false. |
| 121 | * NULL may be passed as the array pointer if its length is declared to be 0. |
| 122 | * The "C" locale is used for comparing upper and lowercase letters. |
| 123 | * Data is assumed to be ASCII text, UTF-8 will work fine too. |
| 124 | */ |
| 125 | AWS_COMMON_API |
| 126 | bool aws_array_eq_c_str_ignore_case(const void *const array, const size_t array_len, const char *const c_str); |
| 127 | |
| 128 | AWS_COMMON_API |
| 129 | int aws_byte_buf_init(struct aws_byte_buf *buf, struct aws_allocator *allocator, size_t capacity); |
| 130 | |
| 131 | /** |
| 132 | * Initializes an aws_byte_buf structure base on another valid one. |
| 133 | * Requires: *src and *allocator are valid objects. |
| 134 | * Ensures: *dest is a valid aws_byte_buf with a new backing array dest->buffer |
| 135 | * which is a copy of the elements from src->buffer. |
| 136 | */ |
| 137 | AWS_COMMON_API int aws_byte_buf_init_copy( |
| 138 | struct aws_byte_buf *dest, |
| 139 | struct aws_allocator *allocator, |
| 140 | const struct aws_byte_buf *src); |
| 141 | |
| 142 | /** |
| 143 | * Reads 'filename' into 'out_buf'. If successful, 'out_buf' is allocated and filled with the data; |
| 144 | * It is your responsibility to call 'aws_byte_buf_clean_up()' on it. Otherwise, 'out_buf' remains |
| 145 | * unused. In the very unfortunate case where some API needs to treat out_buf as a c_string, a null terminator |
| 146 | * is appended, but is not included as part of the length field. |
| 147 | */ |
| 148 | AWS_COMMON_API |
| 149 | int aws_byte_buf_init_from_file(struct aws_byte_buf *out_buf, struct aws_allocator *alloc, const char *filename); |
| 150 | |
| 151 | /** |
| 152 | * Same as aws_byte_buf_init_from_file(), but for reading "special files" like /proc/cpuinfo. |
| 153 | * These files don't accurately report their size, so size_hint is used as initial buffer size, |
| 154 | * and the buffer grows until the while file is read. |
| 155 | */ |
| 156 | AWS_COMMON_API |
| 157 | int aws_byte_buf_init_from_file_with_size_hint( |
| 158 | struct aws_byte_buf *out_buf, |
| 159 | struct aws_allocator *alloc, |
| 160 | const char *filename, |
| 161 | size_t size_hint); |
| 162 | |
| 163 | /** |
| 164 | * Evaluates the set of properties that define the shape of all valid aws_byte_buf structures. |
| 165 | * It is also a cheap check, in the sense it run in constant time (i.e., no loops or recursion). |
| 166 | */ |
| 167 | AWS_COMMON_API |
| 168 | bool aws_byte_buf_is_valid(const struct aws_byte_buf *const buf); |
| 169 | |
| 170 | /** |
| 171 | * Evaluates the set of properties that define the shape of all valid aws_byte_cursor structures. |
| 172 | * It is also a cheap check, in the sense it runs in constant time (i.e., no loops or recursion). |
| 173 | */ |
| 174 | AWS_COMMON_API |
| 175 | bool aws_byte_cursor_is_valid(const struct aws_byte_cursor *cursor); |
| 176 | |
| 177 | /** |
| 178 | * Copies src buffer into dest and sets the correct len and capacity. |
| 179 | * A new memory zone is allocated for dest->buffer. When dest is no longer needed it will have to be cleaned-up using |
| 180 | * aws_byte_buf_clean_up(dest). |
| 181 | * Dest capacity and len will be equal to the src len. Allocator of the dest will be identical with parameter allocator. |
| 182 | * If src buffer is null the dest will have a null buffer with a len and a capacity of 0 |
| 183 | * Returns AWS_OP_SUCCESS in case of success or AWS_OP_ERR when memory can't be allocated. |
| 184 | */ |
| 185 | AWS_COMMON_API |
| 186 | int aws_byte_buf_init_copy_from_cursor( |
| 187 | struct aws_byte_buf *dest, |
| 188 | struct aws_allocator *allocator, |
| 189 | struct aws_byte_cursor src); |
| 190 | |
| 191 | /** |
| 192 | * Init buffer with contents of multiple cursors, and update cursors to reference the memory stored in the buffer. |
| 193 | * Each cursor arg must be an `struct aws_byte_cursor *`. NULL must be passed as the final arg. |
| 194 | * NOTE: Do not append/grow/resize buffers initialized this way, or the cursors will end up referencing invalid memory. |
| 195 | * Returns AWS_OP_SUCCESS in case of success. |
| 196 | * AWS_OP_ERR is returned if memory can't be allocated or the total cursor length exceeds SIZE_MAX. |
| 197 | */ |
| 198 | AWS_COMMON_API |
| 199 | int aws_byte_buf_init_cache_and_update_cursors(struct aws_byte_buf *dest, struct aws_allocator *allocator, ...); |
| 200 | |
| 201 | AWS_COMMON_API |
| 202 | void aws_byte_buf_clean_up(struct aws_byte_buf *buf); |
| 203 | |
| 204 | /** |
| 205 | * Equivalent to calling aws_byte_buf_secure_zero and then aws_byte_buf_clean_up |
| 206 | * on the buffer. |
| 207 | */ |
| 208 | AWS_COMMON_API |
| 209 | void aws_byte_buf_clean_up_secure(struct aws_byte_buf *buf); |
| 210 | |
| 211 | /** |
| 212 | * Resets the len of the buffer to 0, but does not free the memory. The buffer can then be reused. |
| 213 | * Optionally zeroes the contents, if the "zero_contents" flag is true. |
| 214 | */ |
| 215 | AWS_COMMON_API |
| 216 | void aws_byte_buf_reset(struct aws_byte_buf *buf, bool zero_contents); |
| 217 | |
| 218 | /** |
| 219 | * Sets all bytes of buffer to zero and resets len to zero. |
| 220 | */ |
| 221 | AWS_COMMON_API |
| 222 | void aws_byte_buf_secure_zero(struct aws_byte_buf *buf); |
| 223 | |
| 224 | /** |
| 225 | * Compare two aws_byte_buf structures. |
| 226 | * Return whether their contents are equivalent. |
| 227 | */ |
| 228 | AWS_COMMON_API |
| 229 | bool aws_byte_buf_eq(const struct aws_byte_buf *const a, const struct aws_byte_buf *const b); |
| 230 | |
| 231 | /** |
| 232 | * Perform a case-insensitive string comparison of two aws_byte_buf structures. |
| 233 | * Return whether their contents are equivalent. |
| 234 | * The "C" locale is used for comparing upper and lowercase letters. |
| 235 | * Data is assumed to be ASCII text, UTF-8 will work fine too. |
| 236 | */ |
| 237 | AWS_COMMON_API |
| 238 | bool aws_byte_buf_eq_ignore_case(const struct aws_byte_buf *const a, const struct aws_byte_buf *const b); |
| 239 | |
| 240 | /** |
| 241 | * Compare an aws_byte_buf and a null-terminated string. |
| 242 | * Returns true if their contents are equivalent. |
| 243 | * The buffer should NOT contain a null-terminator, or the comparison will always return false. |
| 244 | */ |
| 245 | AWS_COMMON_API |
| 246 | bool aws_byte_buf_eq_c_str(const struct aws_byte_buf *const buf, const char *const c_str); |
| 247 | |
| 248 | /** |
| 249 | * Perform a case-insensitive string comparison of an aws_byte_buf and a null-terminated string. |
| 250 | * Return whether their contents are equivalent. |
| 251 | * The buffer should NOT contain a null-terminator, or the comparison will always return false. |
| 252 | * The "C" locale is used for comparing upper and lowercase letters. |
| 253 | * Data is assumed to be ASCII text, UTF-8 will work fine too. |
| 254 | */ |
| 255 | AWS_COMMON_API |
| 256 | bool aws_byte_buf_eq_c_str_ignore_case(const struct aws_byte_buf *const buf, const char *const c_str); |
| 257 | |
| 258 | /** |
| 259 | * No copies, no buffer allocations. Iterates over input_str, and returns the |
| 260 | * next substring between split_on instances relative to previous substr. |
| 261 | * Behaves similar to strtok with substr being used as state for next split. |
| 262 | * |
| 263 | * Returns true each time substr is set and false when there is no more splits |
| 264 | * (substr is set to empty in that case). |
| 265 | * |
| 266 | * Example usage. |
| 267 | * struct aws_byte_cursor substr = {0}; |
| 268 | * while (aws_byte_cursor_next_split(&input_str, ';', &substr)) { |
| 269 | * // ...use substr... |
| 270 | * } |
| 271 | * |
| 272 | * Note: It is the user's responsibility zero-initialize substr before the first call. |
| 273 | * |
| 274 | * Edge case rules are as follows: |
| 275 | * empty input will have single empty split. ex. "" splits into "" |
| 276 | * if input starts with split_on then first split is empty. ex ";A" splits into "", "A" |
| 277 | * adjacent split tokens result in empty split. ex "A;;B" splits into "A", "", "B" |
| 278 | * If the input ends with split_on, last split is empty. ex. "A;" splits into "A", "" |
| 279 | * |
| 280 | * It is the user's responsibility to make sure the input buffer stays in memory |
| 281 | * long enough to use the results. |
| 282 | */ |
| 283 | AWS_COMMON_API |
| 284 | bool aws_byte_cursor_next_split( |
| 285 | const struct aws_byte_cursor *AWS_RESTRICT input_str, |
| 286 | char split_on, |
| 287 | struct aws_byte_cursor *AWS_RESTRICT substr); |
| 288 | |
| 289 | /** |
| 290 | * same as aws_byte_cursor_next_split, but splits on a cursor instead of a single char. |
| 291 | * ex. can split on delims like "--" or "<<". |
| 292 | * Note: splits on the whole split_on cursor, it does not treat split_on as array of different chars to split on |
| 293 | */ |
| 294 | AWS_COMMON_API |
| 295 | bool aws_byte_cursor_next_split_on_cursor( |
| 296 | const struct aws_byte_cursor *AWS_RESTRICT input_str, |
| 297 | struct aws_byte_cursor split_on, |
| 298 | struct aws_byte_cursor *AWS_RESTRICT substr); |
| 299 | |
| 300 | /** |
| 301 | * No copies, no buffer allocations. Fills in output with a list of |
| 302 | * aws_byte_cursor instances where buffer is an offset into the input_str and |
| 303 | * len is the length of that string in the original buffer. |
| 304 | * |
| 305 | * Edge case rules are as follows: |
| 306 | * if the input begins with split_on, an empty cursor will be the first entry in |
| 307 | * output. if the input has two adjacent split_on tokens, an empty cursor will |
| 308 | * be inserted into the output. if the input ends with split_on, an empty cursor |
| 309 | * will be appended to the output. |
| 310 | * |
| 311 | * It is the user's responsibility to properly initialize output. Recommended number of preallocated elements from |
| 312 | * output is your most likely guess for the upper bound of the number of elements resulting from the split. |
| 313 | * |
| 314 | * The type that will be stored in output is struct aws_byte_cursor (you'll need |
| 315 | * this for the item size param). |
| 316 | * |
| 317 | * It is the user's responsibility to make sure the input buffer stays in memory |
| 318 | * long enough to use the results. |
| 319 | */ |
| 320 | AWS_COMMON_API |
| 321 | int aws_byte_cursor_split_on_char( |
| 322 | const struct aws_byte_cursor *AWS_RESTRICT input_str, |
| 323 | char split_on, |
| 324 | struct aws_array_list *AWS_RESTRICT output); |
| 325 | |
| 326 | /** |
| 327 | * No copies, no buffer allocations. Fills in output with a list of aws_byte_cursor instances where buffer is |
| 328 | * an offset into the input_str and len is the length of that string in the original buffer. N is the max number of |
| 329 | * splits, if this value is zero, it will add all splits to the output. |
| 330 | * |
| 331 | * Edge case rules are as follows: |
| 332 | * if the input begins with split_on, an empty cursor will be the first entry in output |
| 333 | * if the input has two adjacent split_on tokens, an empty cursor will be inserted into the output. |
| 334 | * if the input ends with split_on, an empty cursor will be appended to the output. |
| 335 | * |
| 336 | * It is the user's responsibility to properly initialize output. Recommended number of preallocated elements from |
| 337 | * output is your most likely guess for the upper bound of the number of elements resulting from the split. |
| 338 | * |
| 339 | * If the output array is not large enough, input_str will be updated to point to the first character after the last |
| 340 | * processed split_on instance. |
| 341 | * |
| 342 | * The type that will be stored in output is struct aws_byte_cursor (you'll need this for the item size param). |
| 343 | * |
| 344 | * It is the user's responsibility to make sure the input buffer stays in memory long enough to use the results. |
| 345 | */ |
| 346 | AWS_COMMON_API |
| 347 | int aws_byte_cursor_split_on_char_n( |
| 348 | const struct aws_byte_cursor *AWS_RESTRICT input_str, |
| 349 | char split_on, |
| 350 | size_t n, |
| 351 | struct aws_array_list *AWS_RESTRICT output); |
| 352 | |
| 353 | /** |
| 354 | * Search for an exact byte match inside a cursor. The first match will be returned. Returns AWS_OP_SUCCESS |
| 355 | * on successful match and first_find will be set to the offset in input_str, and length will be the remaining length |
| 356 | * from input_str past the returned offset. If the match was not found, AWS_OP_ERR will be returned and |
| 357 | * AWS_ERROR_STRING_MATCH_NOT_FOUND will be raised. |
| 358 | */ |
| 359 | AWS_COMMON_API |
| 360 | int aws_byte_cursor_find_exact( |
| 361 | const struct aws_byte_cursor *AWS_RESTRICT input_str, |
| 362 | const struct aws_byte_cursor *AWS_RESTRICT to_find, |
| 363 | struct aws_byte_cursor *first_find); |
| 364 | |
| 365 | /** |
| 366 | * |
| 367 | * Shrinks a byte cursor from the right for as long as the supplied predicate is true |
| 368 | */ |
| 369 | AWS_COMMON_API |
| 370 | struct aws_byte_cursor aws_byte_cursor_right_trim_pred( |
| 371 | const struct aws_byte_cursor *source, |
| 372 | aws_byte_predicate_fn *predicate); |
| 373 | |
| 374 | /** |
| 375 | * Shrinks a byte cursor from the left for as long as the supplied predicate is true |
| 376 | */ |
| 377 | AWS_COMMON_API |
| 378 | struct aws_byte_cursor aws_byte_cursor_left_trim_pred( |
| 379 | const struct aws_byte_cursor *source, |
| 380 | aws_byte_predicate_fn *predicate); |
| 381 | |
| 382 | /** |
| 383 | * Shrinks a byte cursor from both sides for as long as the supplied predicate is true |
| 384 | */ |
| 385 | AWS_COMMON_API |
| 386 | struct aws_byte_cursor aws_byte_cursor_trim_pred( |
| 387 | const struct aws_byte_cursor *source, |
| 388 | aws_byte_predicate_fn *predicate); |
| 389 | |
| 390 | /** |
| 391 | * Returns true if the byte cursor's range of bytes all satisfy the predicate |
| 392 | */ |
| 393 | AWS_COMMON_API |
| 394 | bool aws_byte_cursor_satisfies_pred(const struct aws_byte_cursor *source, aws_byte_predicate_fn *predicate); |
| 395 | |
| 396 | /** |
| 397 | * Copies from to to. If to is too small, AWS_ERROR_DEST_COPY_TOO_SMALL will be |
| 398 | * returned. dest->len will contain the amount of data actually copied to dest. |
| 399 | * |
| 400 | * from and to may be the same buffer, permitting copying a buffer into itself. |
| 401 | */ |
| 402 | AWS_COMMON_API |
| 403 | int aws_byte_buf_append(struct aws_byte_buf *to, const struct aws_byte_cursor *from); |
| 404 | |
| 405 | /** |
| 406 | * Copies from to to while converting bytes via the passed in lookup table. |
| 407 | * If to is too small, AWS_ERROR_DEST_COPY_TOO_SMALL will be |
| 408 | * returned. to->len will contain its original size plus the amount of data actually copied to to. |
| 409 | * |
| 410 | * from and to should not be the same buffer (overlap is not handled) |
| 411 | * lookup_table must be at least 256 bytes |
| 412 | */ |
| 413 | AWS_COMMON_API |
| 414 | int aws_byte_buf_append_with_lookup( |
| 415 | struct aws_byte_buf *AWS_RESTRICT to, |
| 416 | const struct aws_byte_cursor *AWS_RESTRICT from, |
| 417 | const uint8_t *lookup_table); |
| 418 | |
| 419 | /** |
| 420 | * Copies from to to. If to is too small, the buffer will be grown appropriately and |
| 421 | * the old contents copied to, before the new contents are appended. |
| 422 | * |
| 423 | * If the grow fails (overflow or OOM), then an error will be returned. |
| 424 | * |
| 425 | * from and to may be the same buffer, permitting copying a buffer into itself. |
| 426 | */ |
| 427 | AWS_COMMON_API |
| 428 | int aws_byte_buf_append_dynamic(struct aws_byte_buf *to, const struct aws_byte_cursor *from); |
| 429 | |
| 430 | /** |
| 431 | * Copies `from` to `to`, selecting between static and dynamic append based |
| 432 | * on whether `to` has an allocator. |
| 433 | * |
| 434 | * - If `to->allocator != NULL`: uses aws_byte_buf_append_dynamic (grows |
| 435 | * the buffer as needed; same behavior as calling that function directly). |
| 436 | * - If `to->allocator == NULL`: uses aws_byte_buf_append (no grow; returns |
| 437 | * AWS_ERROR_DEST_COPY_TOO_SMALL if `from` does not fit in remaining |
| 438 | * capacity). |
| 439 | * |
| 440 | * This is intended for callers that receive a buffer from a source whose |
| 441 | * growability is not known at the call site — most commonly a buffer |
| 442 | * obtained from an aws_s3_buffer_pool ticket, where pool-backed tickets |
| 443 | * return a fixed-size buffer (allocator == NULL) and the default pool may |
| 444 | * return a growable one. |
| 445 | * |
| 446 | * `from` and `to` may be the same buffer, permitting copying a buffer |
| 447 | * into itself. |
| 448 | */ |
| 449 | AWS_COMMON_API |
| 450 | int aws_byte_buf_append_auto(struct aws_byte_buf *to, const struct aws_byte_cursor *from); |
| 451 | |
| 452 | /** |
| 453 | * Copies `from` to `to`. If `to` is too small, the buffer will be grown appropriately and |
| 454 | * the old contents copied over, before the new contents are appended. |
| 455 | * |
| 456 | * If the grow fails (overflow or OOM), then an error will be returned. |
| 457 | * |
| 458 | * If the buffer is grown, the old buffer will be securely cleared before getting freed. |
| 459 | * |
| 460 | * `from` and `to` may be the same buffer, permitting copying a buffer into itself. |
| 461 | */ |
| 462 | AWS_COMMON_API |
| 463 | int aws_byte_buf_append_dynamic_secure(struct aws_byte_buf *to, const struct aws_byte_cursor *from); |
| 464 | |
| 465 | /** |
| 466 | * Copies a single byte into `to`. If `to` is too small, the buffer will be grown appropriately and |
| 467 | * the old contents copied over, before the byte is appended. |
| 468 | * |
| 469 | * If the grow fails (overflow or OOM), then an error will be returned. |
| 470 | */ |
| 471 | AWS_COMMON_API |
| 472 | int aws_byte_buf_append_byte_dynamic(struct aws_byte_buf *buffer, uint8_t value); |
| 473 | |
| 474 | /** |
| 475 | * Copies a single byte into `to`. If `to` is too small, the buffer will be grown appropriately and |
| 476 | * the old contents copied over, before the byte is appended. |
| 477 | * |
| 478 | * If the grow fails (overflow or OOM), then an error will be returned. |
| 479 | * |
| 480 | * If the buffer is grown, the old buffer will be securely cleared before getting freed. |
| 481 | */ |
| 482 | AWS_COMMON_API |
| 483 | int aws_byte_buf_append_byte_dynamic_secure(struct aws_byte_buf *buffer, uint8_t value); |
| 484 | |
| 485 | /** |
| 486 | * Copy contents of cursor to buffer, then update cursor to reference the memory stored in the buffer. |
| 487 | * If buffer is too small, AWS_ERROR_DEST_COPY_TOO_SMALL will be returned. |
| 488 | * |
| 489 | * The cursor is permitted to reference memory from earlier in the buffer. |
| 490 | */ |
| 491 | AWS_COMMON_API |
| 492 | int aws_byte_buf_append_and_update(struct aws_byte_buf *to, struct aws_byte_cursor *from_and_update); |
| 493 | |
| 494 | /** |
| 495 | * Appends '\0' at the end of the buffer. |
| 496 | */ |
| 497 | AWS_COMMON_API |
| 498 | int aws_byte_buf_append_null_terminator(struct aws_byte_buf *buf); |
| 499 | |
| 500 | /** |
| 501 | * Attempts to increase the capacity of a buffer to the requested capacity |
| 502 | * |
| 503 | * If the the buffer's capacity is currently larger than the request capacity, the |
| 504 | * function does nothing (no shrink is performed). |
| 505 | */ |
| 506 | AWS_COMMON_API |
| 507 | int aws_byte_buf_reserve(struct aws_byte_buf *buffer, size_t requested_capacity); |
| 508 | |
| 509 | /** |
| 510 | * Convenience function that attempts to increase the capacity of a buffer relative to the current |
| 511 | * length. |
| 512 | * |
| 513 | * aws_byte_buf_reserve_relative(buf, x) ~~ aws_byte_buf_reserve(buf, buf->len + x) |
| 514 | * |
| 515 | */ |
| 516 | AWS_COMMON_API |
| 517 | int aws_byte_buf_reserve_relative(struct aws_byte_buf *buffer, size_t additional_length); |
| 518 | |
| 519 | /** |
| 520 | * Concatenates a variable number of struct aws_byte_buf * into destination. |
| 521 | * Number of args must be greater than 1. If dest is too small, |
| 522 | * AWS_ERROR_DEST_COPY_TOO_SMALL will be returned. dest->len will contain the |
| 523 | * amount of data actually copied to dest. |
| 524 | */ |
| 525 | AWS_COMMON_API |
| 526 | int aws_byte_buf_cat(struct aws_byte_buf *dest, size_t number_of_args, ...); |
| 527 | |
| 528 | /** |
| 529 | * Compare two aws_byte_cursor structures. |
| 530 | * Return whether their contents are equivalent. |
| 531 | */ |
| 532 | AWS_COMMON_API |
| 533 | bool aws_byte_cursor_eq(const struct aws_byte_cursor *a, const struct aws_byte_cursor *b); |
| 534 | |
| 535 | /** |
| 536 | * Perform a case-insensitive string comparison of two aws_byte_cursor structures. |
| 537 | * Return whether their contents are equivalent. |
| 538 | * The "C" locale is used for comparing upper and lowercase letters. |
| 539 | * Data is assumed to be ASCII text, UTF-8 will work fine too. |
| 540 | */ |
| 541 | AWS_COMMON_API |
| 542 | bool aws_byte_cursor_eq_ignore_case(const struct aws_byte_cursor *a, const struct aws_byte_cursor *b); |
| 543 | |
| 544 | /** |
| 545 | * Compare an aws_byte_cursor and an aws_byte_buf. |
| 546 | * Return whether their contents are equivalent. |
| 547 | */ |
| 548 | AWS_COMMON_API |
| 549 | bool aws_byte_cursor_eq_byte_buf(const struct aws_byte_cursor *const a, const struct aws_byte_buf *const b); |
| 550 | |
| 551 | /** |
| 552 | * Perform a case-insensitive string comparison of an aws_byte_cursor and an aws_byte_buf. |
| 553 | * Return whether their contents are equivalent. |
| 554 | * The "C" locale is used for comparing upper and lowercase letters. |
| 555 | * Data is assumed to be ASCII text, UTF-8 will work fine too. |
| 556 | */ |
| 557 | AWS_COMMON_API |
| 558 | bool aws_byte_cursor_eq_byte_buf_ignore_case(const struct aws_byte_cursor *const a, const struct aws_byte_buf *const b); |
| 559 | |
| 560 | /** |
| 561 | * Compare an aws_byte_cursor and a null-terminated string. |
| 562 | * Returns true if their contents are equivalent. |
| 563 | * The cursor should NOT contain a null-terminator, or the comparison will always return false. |
| 564 | */ |
| 565 | AWS_COMMON_API |
| 566 | bool aws_byte_cursor_eq_c_str(const struct aws_byte_cursor *const cursor, const char *const c_str); |
| 567 | |
| 568 | /** |
| 569 | * Perform a case-insensitive string comparison of an aws_byte_cursor and a null-terminated string. |
| 570 | * Return whether their contents are equivalent. |
| 571 | * The cursor should NOT contain a null-terminator, or the comparison will always return false. |
| 572 | * The "C" locale is used for comparing upper and lowercase letters. |
| 573 | * Data is assumed to be ASCII text, UTF-8 will work fine too. |
| 574 | */ |
| 575 | AWS_COMMON_API |
| 576 | bool aws_byte_cursor_eq_c_str_ignore_case(const struct aws_byte_cursor *const cursor, const char *const c_str); |
| 577 | |
| 578 | /** |
| 579 | * Return true if the input starts with the prefix (exact byte comparison). |
| 580 | */ |
| 581 | AWS_COMMON_API |
| 582 | bool aws_byte_cursor_starts_with(const struct aws_byte_cursor *input, const struct aws_byte_cursor *prefix); |
| 583 | |
| 584 | /** |
| 585 | * Return true if the input starts with the prefix (case-insensitive). |
| 586 | * The "C" locale is used for comparing upper and lowercase letters. |
| 587 | * Data is assumed to be ASCII text, UTF-8 will work fine too. |
| 588 | */ |
| 589 | AWS_COMMON_API |
| 590 | bool aws_byte_cursor_starts_with_ignore_case(const struct aws_byte_cursor *input, const struct aws_byte_cursor *prefix); |
| 591 | |
| 592 | /** |
| 593 | * Case-insensitive hash function for array containing ASCII or UTF-8 text. |
| 594 | */ |
| 595 | AWS_COMMON_API |
| 596 | uint64_t aws_hash_array_ignore_case(const void *array, const size_t len); |
| 597 | |
| 598 | /** |
| 599 | * Case-insensitive hash function for aws_byte_cursors stored in an aws_hash_table. |
| 600 | * For case-sensitive hashing, use aws_hash_byte_cursor_ptr(). |
| 601 | */ |
| 602 | AWS_COMMON_API |
| 603 | uint64_t aws_hash_byte_cursor_ptr_ignore_case(const void *item); |
| 604 | |
| 605 | /** |
| 606 | * Returns a lookup table for bytes that is the identity transformation with the exception |
| 607 | * of uppercase ascii characters getting replaced with lowercase characters. Used in |
| 608 | * caseless comparisons. |
| 609 | */ |
| 610 | AWS_COMMON_API |
| 611 | const uint8_t *aws_lookup_table_to_lower_get(void); |
| 612 | |
| 613 | /** |
| 614 | * Returns lookup table to go from ASCII/UTF-8 hex character to a number (0-15). |
| 615 | * Non-hex characters map to 255. |
| 616 | * Valid examples: |
| 617 | * '0' -> 0 |
| 618 | * 'F' -> 15 |
| 619 | * 'f' -> 15 |
| 620 | * Invalid examples: |
| 621 | * ' ' -> 255 |
| 622 | * 'Z' -> 255 |
| 623 | * '\0' -> 255 |
| 624 | */ |
| 625 | AWS_COMMON_API |
| 626 | const uint8_t *aws_lookup_table_hex_to_num_get(void); |
| 627 | |
| 628 | /** |
| 629 | * Lexical (byte value) comparison of two byte cursors |
| 630 | */ |
| 631 | AWS_COMMON_API |
| 632 | int aws_byte_cursor_compare_lexical(const struct aws_byte_cursor *lhs, const struct aws_byte_cursor *rhs); |
| 633 | |
| 634 | /** |
| 635 | * Lexical (byte value) comparison of two byte cursors where the raw values are sent through a lookup table first |
| 636 | */ |
| 637 | AWS_COMMON_API |
| 638 | int aws_byte_cursor_compare_lookup( |
| 639 | const struct aws_byte_cursor *lhs, |
| 640 | const struct aws_byte_cursor *rhs, |
| 641 | const uint8_t *lookup_table); |
| 642 | |
| 643 | /** |
| 644 | * For creating a byte buffer from a null-terminated string literal. |
| 645 | */ |
| 646 | AWS_COMMON_API struct aws_byte_buf aws_byte_buf_from_c_str(const char *c_str); |
| 647 | |
| 648 | AWS_COMMON_API struct aws_byte_buf aws_byte_buf_from_array(const void *bytes, size_t len); |
| 649 | |
| 650 | AWS_COMMON_API struct aws_byte_buf aws_byte_buf_from_empty_array(const void *bytes, size_t capacity); |
| 651 | |
| 652 | AWS_COMMON_API struct aws_byte_cursor aws_byte_cursor_from_buf(const struct aws_byte_buf *const buf); |
| 653 | |
| 654 | AWS_COMMON_API struct aws_byte_cursor aws_byte_cursor_from_c_str(const char *c_str); |
| 655 | |
| 656 | AWS_COMMON_API struct aws_byte_cursor aws_byte_cursor_from_array(const void *const bytes, const size_t len); |
| 657 | |
| 658 | /** |
| 659 | * Tests if the given aws_byte_cursor has at least len bytes remaining. If so, |
| 660 | * *buf is advanced by len bytes (incrementing ->ptr and decrementing ->len), |
| 661 | * and an aws_byte_cursor referring to the first len bytes of the original *buf |
| 662 | * is returned. Otherwise, an aws_byte_cursor with ->ptr = NULL, ->len = 0 is |
| 663 | * returned. |
| 664 | * |
| 665 | * Note that if len is above (SIZE_MAX / 2), this function will also treat it as |
| 666 | * a buffer overflow, and return NULL without changing *buf. |
| 667 | */ |
| 668 | AWS_COMMON_API struct aws_byte_cursor aws_byte_cursor_advance(struct aws_byte_cursor *const cursor, const size_t len); |
| 669 | |
| 670 | /** |
| 671 | * Behaves identically to aws_byte_cursor_advance, but avoids speculative |
| 672 | * execution potentially reading out-of-bounds pointers (by returning an |
| 673 | * empty ptr in such speculated paths). |
| 674 | * |
| 675 | * This should generally be done when using an untrusted or |
| 676 | * data-dependent value for 'len', to avoid speculating into a path where |
| 677 | * cursor->ptr points outside the true ptr length. |
| 678 | */ |
| 679 | |
| 680 | AWS_COMMON_API struct aws_byte_cursor aws_byte_cursor_advance_nospec(struct aws_byte_cursor *const cursor, size_t len); |
| 681 | |
| 682 | /** |
| 683 | * Reads specified length of data from byte cursor and copies it to the |
| 684 | * destination array. |
| 685 | * |
| 686 | * On success, returns true and updates the cursor pointer/length accordingly. |
| 687 | * If there is insufficient space in the cursor, returns false, leaving the |
| 688 | * cursor unchanged. |
| 689 | */ |
| 690 | AWS_COMMON_API bool aws_byte_cursor_read( |
| 691 | struct aws_byte_cursor *AWS_RESTRICT cur, |
| 692 | void *AWS_RESTRICT dest, |
| 693 | const size_t len); |
| 694 | |
| 695 | /** |
| 696 | * Reads as many bytes from cursor as size of buffer, and copies them to buffer. |
| 697 | * |
| 698 | * On success, returns true and updates the cursor pointer/length accordingly. |
| 699 | * If there is insufficient space in the cursor, returns false, leaving the |
| 700 | * cursor unchanged. |
| 701 | */ |
| 702 | AWS_COMMON_API bool aws_byte_cursor_read_and_fill_buffer( |
| 703 | struct aws_byte_cursor *AWS_RESTRICT cur, |
| 704 | struct aws_byte_buf *AWS_RESTRICT dest); |
| 705 | |
| 706 | /** |
| 707 | * Reads a single byte from cursor, placing it in *var. |
| 708 | * |
| 709 | * On success, returns true and updates the cursor pointer/length accordingly. |
| 710 | * If there is insufficient space in the cursor, returns false, leaving the |
| 711 | * cursor unchanged. |
| 712 | */ |
| 713 | AWS_COMMON_API bool aws_byte_cursor_read_u8(struct aws_byte_cursor *AWS_RESTRICT cur, uint8_t *AWS_RESTRICT var); |
| 714 | |
| 715 | /** |
| 716 | * Reads a 16-bit value in network byte order from cur, and places it in host |
| 717 | * byte order into var. |
| 718 | * |
| 719 | * On success, returns true and updates the cursor pointer/length accordingly. |
| 720 | * If there is insufficient space in the cursor, returns false, leaving the |
| 721 | * cursor unchanged. |
| 722 | */ |
| 723 | AWS_COMMON_API bool aws_byte_cursor_read_be16(struct aws_byte_cursor *cur, uint16_t *var); |
| 724 | |
| 725 | /** |
| 726 | * Reads a 16-bit value in little-endian byte order from cur, and places it in host |
| 727 | * byte order into var. |
| 728 | * |
| 729 | * On success, returns true and updates the cursor pointer/length accordingly. |
| 730 | * If there is insufficient space in the cursor, returns false, leaving the |
| 731 | * cursor unchanged. |
| 732 | */ |
| 733 | AWS_COMMON_API bool aws_byte_cursor_read_le16(struct aws_byte_cursor *cur, uint16_t *var); |
| 734 | |
| 735 | /** |
| 736 | * Reads an unsigned 24-bit value (3 bytes) in network byte order from cur, |
| 737 | * and places it in host byte order into 32-bit var. |
| 738 | * Ex: if cur's next 3 bytes are {0xAA, 0xBB, 0xCC}, then var becomes 0x00AABBCC. |
| 739 | * |
| 740 | * On success, returns true and updates the cursor pointer/length accordingly. |
| 741 | * If there is insufficient space in the cursor, returns false, leaving the |
| 742 | * cursor unchanged. |
| 743 | */ |
| 744 | AWS_COMMON_API bool aws_byte_cursor_read_be24(struct aws_byte_cursor *cur, uint32_t *var); |
| 745 | |
| 746 | /** |
| 747 | * Reads a 32-bit value in network byte order from cur, and places it in host |
| 748 | * byte order into var. |
| 749 | * |
| 750 | * On success, returns true and updates the cursor pointer/length accordingly. |
| 751 | * If there is insufficient space in the cursor, returns false, leaving the |
| 752 | * cursor unchanged. |
| 753 | */ |
| 754 | AWS_COMMON_API bool aws_byte_cursor_read_be32(struct aws_byte_cursor *cur, uint32_t *var); |
| 755 | |
| 756 | /** |
| 757 | * Reads a 32-bit value in little endian byte order from cur, and places it in host |
| 758 | * byte order into var. |
| 759 | * |
| 760 | * On success, returns true and updates the cursor pointer/length accordingly. |
| 761 | * If there is insufficient space in the cursor, returns false, leaving the |
| 762 | * cursor unchanged. |
| 763 | */ |
| 764 | AWS_COMMON_API bool aws_byte_cursor_read_le32(struct aws_byte_cursor *cur, uint32_t *var); |
| 765 | |
| 766 | /** |
| 767 | * Reads a signed 32-bit value in network byte order from cur, and places it in host |
| 768 | * byte order into var. |
| 769 | * |
| 770 | * On success, returns true and updates the cursor pointer/length accordingly. |
| 771 | * If there is insufficient space in the cursor, returns false, leaving the |
| 772 | * cursor unchanged. |
| 773 | */ |
| 774 | AWS_COMMON_API bool aws_byte_cursor_read_be_i32(struct aws_byte_cursor *cur, int32_t *var); |
| 775 | |
| 776 | /** |
| 777 | * Reads a signed 32-bit value in little-endian order from cur, and places it in host |
| 778 | * byte order into var. |
| 779 | * |
| 780 | * On success, returns true and updates the cursor pointer/length accordingly. |
| 781 | * If there is insufficient space in the cursor, returns false, leaving the |
| 782 | * cursor unchanged. |
| 783 | */ |
| 784 | AWS_COMMON_API bool aws_byte_cursor_read_le_i32(struct aws_byte_cursor *cur, int32_t *var); |
| 785 | |
| 786 | /** |
| 787 | * Reads a 64-bit value in network byte order from cur, and places it in host |
| 788 | * byte order into var. |
| 789 | * |
| 790 | * On success, returns true and updates the cursor pointer/length accordingly. |
| 791 | * If there is insufficient space in the cursor, returns false, leaving the |
| 792 | * cursor unchanged. |
| 793 | */ |
| 794 | AWS_COMMON_API bool aws_byte_cursor_read_be64(struct aws_byte_cursor *cur, uint64_t *var); |
| 795 | |
| 796 | /** |
| 797 | * Reads a 32-bit value in network byte order from cur, and places it in host |
| 798 | * byte order into var. |
| 799 | * |
| 800 | * On success, returns true and updates the cursor pointer/length accordingly. |
| 801 | * If there is insufficient space in the cursor, returns false, leaving the |
| 802 | * cursor unchanged. |
| 803 | */ |
| 804 | AWS_COMMON_API bool aws_byte_cursor_read_float_be32(struct aws_byte_cursor *cur, float *var); |
| 805 | |
| 806 | /** |
| 807 | * Reads a 64-bit value in network byte order from cur, and places it in host |
| 808 | * byte order into var. |
| 809 | * |
| 810 | * On success, returns true and updates the cursor pointer/length accordingly. |
| 811 | * If there is insufficient space in the cursor, returns false, leaving the |
| 812 | * cursor unchanged. |
| 813 | */ |
| 814 | AWS_COMMON_API bool aws_byte_cursor_read_float_be64(struct aws_byte_cursor *cur, double *var); |
| 815 | |
| 816 | /** |
| 817 | * Reads 2 hex characters from ASCII/UTF-8 text to produce an 8-bit number. |
| 818 | * Accepts both lowercase 'a'-'f' and uppercase 'A'-'F'. |
| 819 | * For example: "0F" produces 15. |
| 820 | * |
| 821 | * On success, returns true and advances the cursor by 2. |
| 822 | * If there is insufficient space in the cursor or an invalid character |
| 823 | * is encountered, returns false, leaving the cursor unchanged. |
| 824 | */ |
| 825 | AWS_COMMON_API bool aws_byte_cursor_read_hex_u8(struct aws_byte_cursor *cur, uint8_t *var); |
| 826 | |
| 827 | /** |
| 828 | * Appends a sub-buffer to the specified buffer. |
| 829 | * |
| 830 | * If the buffer has at least `len' bytes remaining (buffer->capacity - buffer->len >= len), |
| 831 | * then buffer->len is incremented by len, and an aws_byte_buf is assigned to *output corresponding |
| 832 | * to the last len bytes of the input buffer. The aws_byte_buf at *output will have a null |
| 833 | * allocator, a zero initial length, and a capacity of 'len'. The function then returns true. |
| 834 | * |
| 835 | * If there is insufficient space, then this function nulls all fields in *output and returns |
| 836 | * false. |
| 837 | */ |
| 838 | AWS_COMMON_API bool aws_byte_buf_advance( |
| 839 | struct aws_byte_buf *const AWS_RESTRICT buffer, |
| 840 | struct aws_byte_buf *const AWS_RESTRICT output, |
| 841 | const size_t len); |
| 842 | |
| 843 | /** |
| 844 | * Write specified number of bytes from array to byte buffer. |
| 845 | * |
| 846 | * On success, returns true and updates the buffer length accordingly. |
| 847 | * If there is insufficient space in the buffer, returns false, leaving the |
| 848 | * buffer unchanged. |
| 849 | */ |
| 850 | AWS_COMMON_API bool aws_byte_buf_write( |
| 851 | struct aws_byte_buf *AWS_RESTRICT buf, |
| 852 | const uint8_t *AWS_RESTRICT src, |
| 853 | size_t len); |
| 854 | |
| 855 | /** |
| 856 | * Copies all bytes from buffer to buffer. |
| 857 | * |
| 858 | * On success, returns true and updates the buffer /length accordingly. |
| 859 | * If there is insufficient space in the buffer, returns false, leaving the |
| 860 | * buffer unchanged. |
| 861 | */ |
| 862 | AWS_COMMON_API bool aws_byte_buf_write_from_whole_buffer( |
| 863 | struct aws_byte_buf *AWS_RESTRICT buf, |
| 864 | struct aws_byte_buf src); |
| 865 | |
| 866 | /** |
| 867 | * Copies all bytes from buffer to buffer. |
| 868 | * |
| 869 | * On success, returns true and updates the buffer /length accordingly. |
| 870 | * If there is insufficient space in the buffer, returns false, leaving the |
| 871 | * buffer unchanged. |
| 872 | */ |
| 873 | AWS_COMMON_API bool aws_byte_buf_write_from_whole_cursor( |
| 874 | struct aws_byte_buf *AWS_RESTRICT buf, |
| 875 | struct aws_byte_cursor src); |
| 876 | |
| 877 | /** |
| 878 | * Without increasing buf's capacity, write as much as possible from advancing_cursor into buf. |
| 879 | * |
| 880 | * buf's len is updated accordingly. |
| 881 | * advancing_cursor is advanced so it contains the remaining unwritten parts. |
| 882 | * Returns the section of advancing_cursor which was written. |
| 883 | * |
| 884 | * This function cannot fail. If buf is full (len == capacity) or advancing_len has 0 length, |
| 885 | * then buf and advancing_cursor are not altered and a cursor with 0 length is returned. |
| 886 | * |
| 887 | * Example: Given a buf with 2 bytes of space available and advancing_cursor with contents "abc". |
| 888 | * "ab" will be written to buf and buf->len will increase 2 and become equal to buf->capacity. |
| 889 | * advancing_cursor will advance so its contents become the unwritten "c". |
| 890 | * The returned cursor's contents will be the "ab" from the original advancing_cursor. |
| 891 | */ |
| 892 | AWS_COMMON_API struct aws_byte_cursor aws_byte_buf_write_to_capacity( |
| 893 | struct aws_byte_buf *buf, |
| 894 | struct aws_byte_cursor *advancing_cursor); |
| 895 | |
| 896 | /** |
| 897 | * Copies one byte to buffer. |
| 898 | * |
| 899 | * On success, returns true and updates the cursor /length |
| 900 | accordingly. |
| 901 | * |
| 902 | * If there is insufficient space in the buffer, returns false, leaving the |
| 903 | * buffer unchanged. |
| 904 | */ |
| 905 | AWS_COMMON_API bool aws_byte_buf_write_u8(struct aws_byte_buf *AWS_RESTRICT buf, uint8_t c); |
| 906 | |
| 907 | /** |
| 908 | * Writes one byte repeatedly to buffer (like memset) |
| 909 | * |
| 910 | * If there is insufficient space in the buffer, returns false, leaving the |
| 911 | * buffer unchanged. |
| 912 | */ |
| 913 | AWS_COMMON_API bool aws_byte_buf_write_u8_n(struct aws_byte_buf *buf, uint8_t c, size_t count); |
| 914 | |
| 915 | /** |
| 916 | * Writes a 16-bit integer in network byte order (big endian) to buffer. |
| 917 | * |
| 918 | * On success, returns true and updates the buffer /length accordingly. |
| 919 | * If there is insufficient space in the buffer, returns false, leaving the |
| 920 | * buffer unchanged. |
| 921 | */ |
| 922 | AWS_COMMON_API bool aws_byte_buf_write_be16(struct aws_byte_buf *buf, uint16_t x); |
| 923 | |
| 924 | /** |
| 925 | * Writes low 24-bits (3 bytes) of an unsigned integer in network byte order (big endian) to buffer. |
| 926 | * Ex: If x is 0x00AABBCC then {0xAA, 0xBB, 0xCC} is written to buffer. |
| 927 | * |
| 928 | * On success, returns true and updates the buffer /length accordingly. |
| 929 | * If there is insufficient space in the buffer, or x's value cannot fit in 3 bytes, |
| 930 | * returns false, leaving the buffer unchanged. |
| 931 | */ |
| 932 | AWS_COMMON_API bool aws_byte_buf_write_be24(struct aws_byte_buf *buf, uint32_t x); |
| 933 | |
| 934 | /** |
| 935 | * Writes a 32-bit integer in network byte order (big endian) to buffer. |
| 936 | * |
| 937 | * On success, returns true and updates the buffer /length accordingly. |
| 938 | * If there is insufficient space in the buffer, returns false, leaving the |
| 939 | * buffer unchanged. |
| 940 | */ |
| 941 | AWS_COMMON_API bool aws_byte_buf_write_be32(struct aws_byte_buf *buf, uint32_t x); |
| 942 | |
| 943 | /** |
| 944 | * Writes a 32-bit float in network byte order (big endian) to buffer. |
| 945 | * |
| 946 | * On success, returns true and updates the buffer /length accordingly. |
| 947 | * If there is insufficient space in the buffer, returns false, leaving the |
| 948 | * buffer unchanged. |
| 949 | */ |
| 950 | AWS_COMMON_API bool aws_byte_buf_write_float_be32(struct aws_byte_buf *buf, float x); |
| 951 | |
| 952 | /** |
| 953 | * Writes a 64-bit integer in network byte order (big endian) to buffer. |
| 954 | * |
| 955 | * On success, returns true and updates the buffer /length accordingly. |
| 956 | * If there is insufficient space in the buffer, returns false, leaving the |
| 957 | * buffer unchanged. |
| 958 | */ |
| 959 | AWS_COMMON_API bool aws_byte_buf_write_be64(struct aws_byte_buf *buf, uint64_t x); |
| 960 | |
| 961 | /** |
| 962 | * Writes a 64-bit float in network byte order (big endian) to buffer. |
| 963 | * |
| 964 | * On success, returns true and updates the buffer /length accordingly. |
| 965 | * If there is insufficient space in the buffer, returns false, leaving the |
| 966 | * buffer unchanged. |
| 967 | */ |
| 968 | AWS_COMMON_API bool aws_byte_buf_write_float_be64(struct aws_byte_buf *buf, double x); |
| 969 | |
| 970 | /** |
| 971 | * Like isalnum(), but ignores C locale. |
| 972 | * Returns true if ch has the value of ASCII/UTF-8: 'a'-'z', 'A'-'Z', or '0'-'9'. |
| 973 | */ |
| 974 | AWS_COMMON_API bool aws_isalnum(uint8_t ch); |
| 975 | |
| 976 | /** |
| 977 | * Like isalpha(), but ignores C locale. |
| 978 | * Returns true if ch has the value of ASCII/UTF-8: 'a'-'z' or 'A'-'Z'. |
| 979 | */ |
| 980 | AWS_COMMON_API bool aws_isalpha(uint8_t ch); |
| 981 | |
| 982 | /** |
| 983 | * Like isdigit(). |
| 984 | * Returns true if ch has the value of ASCII/UTF-8: '0'-'9'. |
| 985 | * |
| 986 | * Note: C's built-in isdigit() is also supposed to ignore the C locale, |
| 987 | * but cppreference.com claims "some implementations (e.g. Microsoft in 1252 codepage) |
| 988 | * may classify additional single-byte characters as digits" |
| 989 | */ |
| 990 | AWS_COMMON_API bool aws_isdigit(uint8_t ch); |
| 991 | |
| 992 | /** |
| 993 | * Like isxdigit(). |
| 994 | * Returns true if ch has the value of ASCII/UTF-8: '0'-'9', 'a'-'f', or 'A'-'F'. |
| 995 | * |
| 996 | * Note: C's built-in isxdigit() is also supposed to ignore the C locale, |
| 997 | * but cppreference.com claims "some implementations (e.g. Microsoft in 1252 codepage) |
| 998 | * may classify additional single-byte characters as digits" |
| 999 | */ |
| 1000 | |
| 1001 | AWS_COMMON_API bool aws_isxdigit(uint8_t ch); |
| 1002 | |
| 1003 | /** |
| 1004 | * Like isspace(), but ignores C locale. |
| 1005 | * Return true if ch has the value of ASCII/UTF-8: space (0x20), form feed (0x0C), |
| 1006 | * line feed (0x0A), carriage return (0x0D), horizontal tab (0x09), or vertical tab (0x0B). |
| 1007 | */ |
| 1008 | AWS_COMMON_API bool aws_isspace(uint8_t ch); |
| 1009 | |
| 1010 | /** |
| 1011 | * Read entire cursor as ASCII/UTF-8 unsigned base-10 number. |
| 1012 | * Stricter than strtoull(), which allows whitespace and inputs that start with "0x" |
| 1013 | * |
| 1014 | * Examples: |
| 1015 | * "0" -> 0 |
| 1016 | * "123" -> 123 |
| 1017 | * "00004" -> 4 // leading zeros ok |
| 1018 | * |
| 1019 | * Rejects things like: |
| 1020 | * "-1" // negative numbers not allowed |
| 1021 | * "1,000" // only characters 0-9 allowed |
| 1022 | * "" // blank string not allowed |
| 1023 | * " 0 " // whitespace not allowed |
| 1024 | * "0x0" // hex not allowed |
| 1025 | * "FF" // hex not allowed |
| 1026 | * "999999999999999999999999999999999999999999" // larger than max u64 |
| 1027 | */ |
| 1028 | AWS_COMMON_API |
| 1029 | int aws_byte_cursor_utf8_parse_u64(struct aws_byte_cursor cursor, uint64_t *dst); |
| 1030 | |
| 1031 | /** |
| 1032 | * Read entire cursor as ASCII/UTF-8 signed base-10 number. |
| 1033 | * Stricter than strtoll(), which allows whitespace and inputs that start with "0x" |
| 1034 | * |
| 1035 | * Examples: |
| 1036 | * "0" -> 0 |
| 1037 | * "123" -> 123 |
| 1038 | * "00004" -> 4 // leading zeros ok |
| 1039 | * "-1" -> -1 |
| 1040 | * |
| 1041 | * Rejects things like: |
| 1042 | * "1,000" // only characters 0-9 allowed |
| 1043 | * "" // blank string not allowed |
| 1044 | * " 0 " // whitespace not allowed |
| 1045 | * "0x0" // hex not allowed |
| 1046 | * "FF" // hex not allowed |
| 1047 | * "999999999999999999999999999999999999999999" // larger than max i64 |
| 1048 | */ |
| 1049 | AWS_COMMON_API |
| 1050 | int aws_byte_cursor_utf8_parse_i64(struct aws_byte_cursor cursor, int64_t *dst); |
| 1051 | |
| 1052 | /** |
| 1053 | * Read entire cursor as ASCII/UTF-8 unsigned base-16 number with NO "0x" prefix. |
| 1054 | * |
| 1055 | * Examples: |
| 1056 | * "F" -> 15 |
| 1057 | * "000000ff" -> 255 // leading zeros ok |
| 1058 | * "Ff" -> 255 // mixed case ok |
| 1059 | * "123" -> 291 |
| 1060 | * "FFFFFFFFFFFFFFFF" -> 18446744073709551616 // max u64 |
| 1061 | * |
| 1062 | * Rejects things like: |
| 1063 | * "0x0" // 0x prefix not allowed |
| 1064 | * "" // blank string not allowed |
| 1065 | * " F " // whitespace not allowed |
| 1066 | * "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" // larger than max u64 |
| 1067 | */ |
| 1068 | AWS_COMMON_API |
| 1069 | int aws_byte_cursor_utf8_parse_u64_hex(struct aws_byte_cursor cursor, uint64_t *dst); |
| 1070 | |
| 1071 | AWS_EXTERN_C_END |
| 1072 | AWS_POP_SANE_WARNING_LEVEL |
| 1073 | |
| 1074 | #endif /* AWS_COMMON_BYTE_BUF_H */ |