diff options
author | unril <unril@yandex-team.ru> | 2022-02-10 16:46:05 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:46:05 +0300 |
commit | 11ae9eca250d0188b7962459cbc6706719e7dca9 (patch) | |
tree | 4b7d6755091980d33210de19b2eb35a401a761ea /contrib/restricted/aws/aws-c-common/source | |
parent | 9c914f41ba5e9f9365f404e892197553ac23809e (diff) | |
download | ydb-11ae9eca250d0188b7962459cbc6706719e7dca9.tar.gz |
Restoring authorship annotation for <unril@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'contrib/restricted/aws/aws-c-common/source')
26 files changed, 4549 insertions, 4549 deletions
diff --git a/contrib/restricted/aws/aws-c-common/source/array_list.c b/contrib/restricted/aws/aws-c-common/source/array_list.c index 7e05636a75..cfcd7d2db2 100644 --- a/contrib/restricted/aws/aws-c-common/source/array_list.c +++ b/contrib/restricted/aws/aws-c-common/source/array_list.c @@ -1,13 +1,13 @@ /** * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0. - */ - -#include <aws/common/array_list.h> + */ + +#include <aws/common/array_list.h> #include <aws/common/private/array_list.h> - -#include <stdlib.h> /* qsort */ - + +#include <stdlib.h> /* qsort */ + int aws_array_list_calc_necessary_size(struct aws_array_list *AWS_RESTRICT list, size_t index, size_t *necessary_size) { AWS_PRECONDITION(aws_array_list_is_valid(list)); size_t index_inc; @@ -24,184 +24,184 @@ int aws_array_list_calc_necessary_size(struct aws_array_list *AWS_RESTRICT list, return AWS_OP_SUCCESS; } -int aws_array_list_shrink_to_fit(struct aws_array_list *AWS_RESTRICT list) { - AWS_PRECONDITION(aws_array_list_is_valid(list)); - if (list->alloc) { - size_t ideal_size; - if (aws_mul_size_checked(list->length, list->item_size, &ideal_size)) { - AWS_POSTCONDITION(aws_array_list_is_valid(list)); - return AWS_OP_ERR; - } - - if (ideal_size < list->current_size) { - void *raw_data = NULL; - - if (ideal_size > 0) { - raw_data = aws_mem_acquire(list->alloc, ideal_size); - if (!raw_data) { - AWS_POSTCONDITION(aws_array_list_is_valid(list)); - return AWS_OP_ERR; - } - - memcpy(raw_data, list->data, ideal_size); - aws_mem_release(list->alloc, list->data); - } - list->data = raw_data; - list->current_size = ideal_size; - } - AWS_POSTCONDITION(aws_array_list_is_valid(list)); - return AWS_OP_SUCCESS; - } - - AWS_POSTCONDITION(aws_array_list_is_valid(list)); - return aws_raise_error(AWS_ERROR_LIST_STATIC_MODE_CANT_SHRINK); -} - -int aws_array_list_copy(const struct aws_array_list *AWS_RESTRICT from, struct aws_array_list *AWS_RESTRICT to) { +int aws_array_list_shrink_to_fit(struct aws_array_list *AWS_RESTRICT list) { + AWS_PRECONDITION(aws_array_list_is_valid(list)); + if (list->alloc) { + size_t ideal_size; + if (aws_mul_size_checked(list->length, list->item_size, &ideal_size)) { + AWS_POSTCONDITION(aws_array_list_is_valid(list)); + return AWS_OP_ERR; + } + + if (ideal_size < list->current_size) { + void *raw_data = NULL; + + if (ideal_size > 0) { + raw_data = aws_mem_acquire(list->alloc, ideal_size); + if (!raw_data) { + AWS_POSTCONDITION(aws_array_list_is_valid(list)); + return AWS_OP_ERR; + } + + memcpy(raw_data, list->data, ideal_size); + aws_mem_release(list->alloc, list->data); + } + list->data = raw_data; + list->current_size = ideal_size; + } + AWS_POSTCONDITION(aws_array_list_is_valid(list)); + return AWS_OP_SUCCESS; + } + + AWS_POSTCONDITION(aws_array_list_is_valid(list)); + return aws_raise_error(AWS_ERROR_LIST_STATIC_MODE_CANT_SHRINK); +} + +int aws_array_list_copy(const struct aws_array_list *AWS_RESTRICT from, struct aws_array_list *AWS_RESTRICT to) { AWS_FATAL_PRECONDITION(from->item_size == to->item_size); AWS_FATAL_PRECONDITION(from->data); - AWS_PRECONDITION(aws_array_list_is_valid(from)); - AWS_PRECONDITION(aws_array_list_is_valid(to)); - - size_t copy_size; - if (aws_mul_size_checked(from->length, from->item_size, ©_size)) { - AWS_POSTCONDITION(aws_array_list_is_valid(from)); - AWS_POSTCONDITION(aws_array_list_is_valid(to)); - return AWS_OP_ERR; - } - - if (to->current_size >= copy_size) { - if (copy_size > 0) { - memcpy(to->data, from->data, copy_size); - } - to->length = from->length; - AWS_POSTCONDITION(aws_array_list_is_valid(from)); - AWS_POSTCONDITION(aws_array_list_is_valid(to)); - return AWS_OP_SUCCESS; - } - /* if to is in dynamic mode, we can just reallocate it and copy */ - if (to->alloc != NULL) { - void *tmp = aws_mem_acquire(to->alloc, copy_size); - - if (!tmp) { - AWS_POSTCONDITION(aws_array_list_is_valid(from)); - AWS_POSTCONDITION(aws_array_list_is_valid(to)); - return AWS_OP_ERR; - } - - memcpy(tmp, from->data, copy_size); - if (to->data) { - aws_mem_release(to->alloc, to->data); - } - - to->data = tmp; - to->length = from->length; - to->current_size = copy_size; - AWS_POSTCONDITION(aws_array_list_is_valid(from)); - AWS_POSTCONDITION(aws_array_list_is_valid(to)); - return AWS_OP_SUCCESS; - } - - return aws_raise_error(AWS_ERROR_DEST_COPY_TOO_SMALL); -} - -int aws_array_list_ensure_capacity(struct aws_array_list *AWS_RESTRICT list, size_t index) { - AWS_PRECONDITION(aws_array_list_is_valid(list)); - size_t necessary_size; - if (aws_array_list_calc_necessary_size(list, index, &necessary_size)) { - AWS_POSTCONDITION(aws_array_list_is_valid(list)); - return AWS_OP_ERR; - } - - if (list->current_size < necessary_size) { - if (!list->alloc) { - AWS_POSTCONDITION(aws_array_list_is_valid(list)); - return aws_raise_error(AWS_ERROR_INVALID_INDEX); - } - - /* this will double capacity if the index isn't bigger than what the - * next allocation would be, but allocates the exact requested size if - * it is. This is largely because we don't have a good way to predict - * the usage pattern to make a smart decision about it. However, if the - * user - * is doing this in an iterative fashion, necessary_size will never be - * used.*/ - size_t next_allocation_size = list->current_size << 1; - size_t new_size = next_allocation_size > necessary_size ? next_allocation_size : necessary_size; - - if (new_size < list->current_size) { - /* this means new_size overflowed. The only way this happens is on a - * 32-bit system where size_t is 32 bits, in which case we're out of - * addressable memory anyways, or we're on a 64 bit system and we're - * most certainly out of addressable memory. But since we're simply - * going to fail fast and say, sorry can't do it, we'll just tell - * the user they can't grow the list anymore. */ - AWS_POSTCONDITION(aws_array_list_is_valid(list)); - return aws_raise_error(AWS_ERROR_LIST_EXCEEDS_MAX_SIZE); - } - - void *temp = aws_mem_acquire(list->alloc, new_size); - - if (!temp) { - AWS_POSTCONDITION(aws_array_list_is_valid(list)); - return AWS_OP_ERR; - } - - if (list->data) { - memcpy(temp, list->data, list->current_size); - -#ifdef DEBUG_BUILD - memset( - (void *)((uint8_t *)temp + list->current_size), - AWS_ARRAY_LIST_DEBUG_FILL, - new_size - list->current_size); -#endif - aws_mem_release(list->alloc, list->data); - } - list->data = temp; - list->current_size = new_size; - } - - AWS_POSTCONDITION(aws_array_list_is_valid(list)); - return AWS_OP_SUCCESS; -} - -static void aws_array_list_mem_swap(void *AWS_RESTRICT item1, void *AWS_RESTRICT item2, size_t item_size) { - enum { SLICE = 128 }; - + AWS_PRECONDITION(aws_array_list_is_valid(from)); + AWS_PRECONDITION(aws_array_list_is_valid(to)); + + size_t copy_size; + if (aws_mul_size_checked(from->length, from->item_size, ©_size)) { + AWS_POSTCONDITION(aws_array_list_is_valid(from)); + AWS_POSTCONDITION(aws_array_list_is_valid(to)); + return AWS_OP_ERR; + } + + if (to->current_size >= copy_size) { + if (copy_size > 0) { + memcpy(to->data, from->data, copy_size); + } + to->length = from->length; + AWS_POSTCONDITION(aws_array_list_is_valid(from)); + AWS_POSTCONDITION(aws_array_list_is_valid(to)); + return AWS_OP_SUCCESS; + } + /* if to is in dynamic mode, we can just reallocate it and copy */ + if (to->alloc != NULL) { + void *tmp = aws_mem_acquire(to->alloc, copy_size); + + if (!tmp) { + AWS_POSTCONDITION(aws_array_list_is_valid(from)); + AWS_POSTCONDITION(aws_array_list_is_valid(to)); + return AWS_OP_ERR; + } + + memcpy(tmp, from->data, copy_size); + if (to->data) { + aws_mem_release(to->alloc, to->data); + } + + to->data = tmp; + to->length = from->length; + to->current_size = copy_size; + AWS_POSTCONDITION(aws_array_list_is_valid(from)); + AWS_POSTCONDITION(aws_array_list_is_valid(to)); + return AWS_OP_SUCCESS; + } + + return aws_raise_error(AWS_ERROR_DEST_COPY_TOO_SMALL); +} + +int aws_array_list_ensure_capacity(struct aws_array_list *AWS_RESTRICT list, size_t index) { + AWS_PRECONDITION(aws_array_list_is_valid(list)); + size_t necessary_size; + if (aws_array_list_calc_necessary_size(list, index, &necessary_size)) { + AWS_POSTCONDITION(aws_array_list_is_valid(list)); + return AWS_OP_ERR; + } + + if (list->current_size < necessary_size) { + if (!list->alloc) { + AWS_POSTCONDITION(aws_array_list_is_valid(list)); + return aws_raise_error(AWS_ERROR_INVALID_INDEX); + } + + /* this will double capacity if the index isn't bigger than what the + * next allocation would be, but allocates the exact requested size if + * it is. This is largely because we don't have a good way to predict + * the usage pattern to make a smart decision about it. However, if the + * user + * is doing this in an iterative fashion, necessary_size will never be + * used.*/ + size_t next_allocation_size = list->current_size << 1; + size_t new_size = next_allocation_size > necessary_size ? next_allocation_size : necessary_size; + + if (new_size < list->current_size) { + /* this means new_size overflowed. The only way this happens is on a + * 32-bit system where size_t is 32 bits, in which case we're out of + * addressable memory anyways, or we're on a 64 bit system and we're + * most certainly out of addressable memory. But since we're simply + * going to fail fast and say, sorry can't do it, we'll just tell + * the user they can't grow the list anymore. */ + AWS_POSTCONDITION(aws_array_list_is_valid(list)); + return aws_raise_error(AWS_ERROR_LIST_EXCEEDS_MAX_SIZE); + } + + void *temp = aws_mem_acquire(list->alloc, new_size); + + if (!temp) { + AWS_POSTCONDITION(aws_array_list_is_valid(list)); + return AWS_OP_ERR; + } + + if (list->data) { + memcpy(temp, list->data, list->current_size); + +#ifdef DEBUG_BUILD + memset( + (void *)((uint8_t *)temp + list->current_size), + AWS_ARRAY_LIST_DEBUG_FILL, + new_size - list->current_size); +#endif + aws_mem_release(list->alloc, list->data); + } + list->data = temp; + list->current_size = new_size; + } + + AWS_POSTCONDITION(aws_array_list_is_valid(list)); + return AWS_OP_SUCCESS; +} + +static void aws_array_list_mem_swap(void *AWS_RESTRICT item1, void *AWS_RESTRICT item2, size_t item_size) { + enum { SLICE = 128 }; + AWS_FATAL_PRECONDITION(item1); AWS_FATAL_PRECONDITION(item2); - - /* copy SLICE sized bytes at a time */ - size_t slice_count = item_size / SLICE; - uint8_t temp[SLICE]; - for (size_t i = 0; i < slice_count; i++) { - memcpy((void *)temp, (void *)item1, SLICE); - memcpy((void *)item1, (void *)item2, SLICE); - memcpy((void *)item2, (void *)temp, SLICE); - item1 = (uint8_t *)item1 + SLICE; - item2 = (uint8_t *)item2 + SLICE; - } - - size_t remainder = item_size & (SLICE - 1); /* item_size % SLICE */ - memcpy((void *)temp, (void *)item1, remainder); - memcpy((void *)item1, (void *)item2, remainder); - memcpy((void *)item2, (void *)temp, remainder); -} - -void aws_array_list_swap(struct aws_array_list *AWS_RESTRICT list, size_t a, size_t b) { + + /* copy SLICE sized bytes at a time */ + size_t slice_count = item_size / SLICE; + uint8_t temp[SLICE]; + for (size_t i = 0; i < slice_count; i++) { + memcpy((void *)temp, (void *)item1, SLICE); + memcpy((void *)item1, (void *)item2, SLICE); + memcpy((void *)item2, (void *)temp, SLICE); + item1 = (uint8_t *)item1 + SLICE; + item2 = (uint8_t *)item2 + SLICE; + } + + size_t remainder = item_size & (SLICE - 1); /* item_size % SLICE */ + memcpy((void *)temp, (void *)item1, remainder); + memcpy((void *)item1, (void *)item2, remainder); + memcpy((void *)item2, (void *)temp, remainder); +} + +void aws_array_list_swap(struct aws_array_list *AWS_RESTRICT list, size_t a, size_t b) { AWS_FATAL_PRECONDITION(a < list->length); AWS_FATAL_PRECONDITION(b < list->length); - AWS_PRECONDITION(aws_array_list_is_valid(list)); - - if (a == b) { - AWS_POSTCONDITION(aws_array_list_is_valid(list)); - return; - } - - void *item1 = NULL, *item2 = NULL; - aws_array_list_get_at_ptr(list, &item1, a); - aws_array_list_get_at_ptr(list, &item2, b); - aws_array_list_mem_swap(item1, item2, list->item_size); - AWS_POSTCONDITION(aws_array_list_is_valid(list)); -} + AWS_PRECONDITION(aws_array_list_is_valid(list)); + + if (a == b) { + AWS_POSTCONDITION(aws_array_list_is_valid(list)); + return; + } + + void *item1 = NULL, *item2 = NULL; + aws_array_list_get_at_ptr(list, &item1, a); + aws_array_list_get_at_ptr(list, &item2, b); + aws_array_list_mem_swap(item1, item2, list->item_size); + AWS_POSTCONDITION(aws_array_list_is_valid(list)); +} diff --git a/contrib/restricted/aws/aws-c-common/source/assert.c b/contrib/restricted/aws/aws-c-common/source/assert.c index 9aaae9a19e..adfc4c408a 100644 --- a/contrib/restricted/aws/aws-c-common/source/assert.c +++ b/contrib/restricted/aws/aws-c-common/source/assert.c @@ -1,18 +1,18 @@ /** * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0. - */ - -#include <aws/common/common.h> - + */ + +#include <aws/common/common.h> + #include <aws/common/system_info.h> -#include <stdio.h> -#include <stdlib.h> - -void aws_fatal_assert(const char *cond_str, const char *file, int line) { - aws_debug_break(); - fprintf(stderr, "Fatal error condition occurred in %s:%d: %s\nExiting Application\n", file, line, cond_str); - aws_backtrace_print(stderr, NULL); - abort(); -} +#include <stdio.h> +#include <stdlib.h> + +void aws_fatal_assert(const char *cond_str, const char *file, int line) { + aws_debug_break(); + fprintf(stderr, "Fatal error condition occurred in %s:%d: %s\nExiting Application\n", file, line, cond_str); + aws_backtrace_print(stderr, NULL); + abort(); +} diff --git a/contrib/restricted/aws/aws-c-common/source/byte_buf.c b/contrib/restricted/aws/aws-c-common/source/byte_buf.c index ca18f4121b..4b2cf1d279 100644 --- a/contrib/restricted/aws/aws-c-common/source/byte_buf.c +++ b/contrib/restricted/aws/aws-c-common/source/byte_buf.c @@ -1,71 +1,71 @@ /** * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0. - */ - -#include <aws/common/byte_buf.h> + */ + +#include <aws/common/byte_buf.h> #include <aws/common/private/byte_buf.h> - -#include <stdarg.h> - -#ifdef _MSC_VER -/* disables warning non const declared initializers for Microsoft compilers */ -# pragma warning(disable : 4204) -# pragma warning(disable : 4706) -#endif - -int aws_byte_buf_init(struct aws_byte_buf *buf, struct aws_allocator *allocator, size_t capacity) { + +#include <stdarg.h> + +#ifdef _MSC_VER +/* disables warning non const declared initializers for Microsoft compilers */ +# pragma warning(disable : 4204) +# pragma warning(disable : 4706) +#endif + +int aws_byte_buf_init(struct aws_byte_buf *buf, struct aws_allocator *allocator, size_t capacity) { AWS_PRECONDITION(buf); AWS_PRECONDITION(allocator); - - buf->buffer = (capacity == 0) ? NULL : aws_mem_acquire(allocator, capacity); - if (capacity != 0 && buf->buffer == NULL) { + + buf->buffer = (capacity == 0) ? NULL : aws_mem_acquire(allocator, capacity); + if (capacity != 0 && buf->buffer == NULL) { AWS_ZERO_STRUCT(*buf); - return AWS_OP_ERR; - } - - buf->len = 0; - buf->capacity = capacity; - buf->allocator = allocator; - AWS_POSTCONDITION(aws_byte_buf_is_valid(buf)); - return AWS_OP_SUCCESS; -} - -int aws_byte_buf_init_copy(struct aws_byte_buf *dest, struct aws_allocator *allocator, const struct aws_byte_buf *src) { + return AWS_OP_ERR; + } + + buf->len = 0; + buf->capacity = capacity; + buf->allocator = allocator; + AWS_POSTCONDITION(aws_byte_buf_is_valid(buf)); + return AWS_OP_SUCCESS; +} + +int aws_byte_buf_init_copy(struct aws_byte_buf *dest, struct aws_allocator *allocator, const struct aws_byte_buf *src) { AWS_PRECONDITION(allocator); AWS_PRECONDITION(dest); AWS_ERROR_PRECONDITION(aws_byte_buf_is_valid(src)); - - if (!src->buffer) { - AWS_ZERO_STRUCT(*dest); - dest->allocator = allocator; - AWS_POSTCONDITION(aws_byte_buf_is_valid(dest)); - return AWS_OP_SUCCESS; - } - - *dest = *src; - dest->allocator = allocator; - dest->buffer = (uint8_t *)aws_mem_acquire(allocator, src->capacity); - if (dest->buffer == NULL) { - AWS_ZERO_STRUCT(*dest); - return AWS_OP_ERR; - } - memcpy(dest->buffer, src->buffer, src->len); - AWS_POSTCONDITION(aws_byte_buf_is_valid(dest)); - return AWS_OP_SUCCESS; -} - -bool aws_byte_buf_is_valid(const struct aws_byte_buf *const buf) { + + if (!src->buffer) { + AWS_ZERO_STRUCT(*dest); + dest->allocator = allocator; + AWS_POSTCONDITION(aws_byte_buf_is_valid(dest)); + return AWS_OP_SUCCESS; + } + + *dest = *src; + dest->allocator = allocator; + dest->buffer = (uint8_t *)aws_mem_acquire(allocator, src->capacity); + if (dest->buffer == NULL) { + AWS_ZERO_STRUCT(*dest); + return AWS_OP_ERR; + } + memcpy(dest->buffer, src->buffer, src->len); + AWS_POSTCONDITION(aws_byte_buf_is_valid(dest)); + return AWS_OP_SUCCESS; +} + +bool aws_byte_buf_is_valid(const struct aws_byte_buf *const buf) { return buf != NULL && ((buf->capacity == 0 && buf->len == 0 && buf->buffer == NULL) || (buf->capacity > 0 && buf->len <= buf->capacity && AWS_MEM_IS_WRITABLE(buf->buffer, buf->capacity))); -} - -bool aws_byte_cursor_is_valid(const struct aws_byte_cursor *cursor) { +} + +bool aws_byte_cursor_is_valid(const struct aws_byte_cursor *cursor) { return cursor != NULL && ((cursor->len == 0) || (cursor->len > 0 && cursor->ptr && AWS_MEM_IS_READABLE(cursor->ptr, cursor->len))); -} - +} + void aws_byte_buf_reset(struct aws_byte_buf *buf, bool zero_contents) { if (zero_contents) { aws_byte_buf_secure_zero(buf); @@ -73,33 +73,33 @@ void aws_byte_buf_reset(struct aws_byte_buf *buf, bool zero_contents) { buf->len = 0; } -void aws_byte_buf_clean_up(struct aws_byte_buf *buf) { - AWS_PRECONDITION(aws_byte_buf_is_valid(buf)); - if (buf->allocator && buf->buffer) { - aws_mem_release(buf->allocator, (void *)buf->buffer); - } - buf->allocator = NULL; - buf->buffer = NULL; - buf->len = 0; - buf->capacity = 0; -} - -void aws_byte_buf_secure_zero(struct aws_byte_buf *buf) { +void aws_byte_buf_clean_up(struct aws_byte_buf *buf) { + AWS_PRECONDITION(aws_byte_buf_is_valid(buf)); + if (buf->allocator && buf->buffer) { + aws_mem_release(buf->allocator, (void *)buf->buffer); + } + buf->allocator = NULL; + buf->buffer = NULL; + buf->len = 0; + buf->capacity = 0; +} + +void aws_byte_buf_secure_zero(struct aws_byte_buf *buf) { + AWS_PRECONDITION(aws_byte_buf_is_valid(buf)); + if (buf->buffer) { + aws_secure_zero(buf->buffer, buf->capacity); + } + buf->len = 0; + AWS_POSTCONDITION(aws_byte_buf_is_valid(buf)); +} + +void aws_byte_buf_clean_up_secure(struct aws_byte_buf *buf) { AWS_PRECONDITION(aws_byte_buf_is_valid(buf)); - if (buf->buffer) { - aws_secure_zero(buf->buffer, buf->capacity); - } - buf->len = 0; + aws_byte_buf_secure_zero(buf); + aws_byte_buf_clean_up(buf); AWS_POSTCONDITION(aws_byte_buf_is_valid(buf)); -} - -void aws_byte_buf_clean_up_secure(struct aws_byte_buf *buf) { - AWS_PRECONDITION(aws_byte_buf_is_valid(buf)); - aws_byte_buf_secure_zero(buf); - aws_byte_buf_clean_up(buf); - AWS_POSTCONDITION(aws_byte_buf_is_valid(buf)); -} - +} + bool aws_byte_buf_eq(const struct aws_byte_buf *const a, const struct aws_byte_buf *const b) { AWS_PRECONDITION(aws_byte_buf_is_valid(a)); AWS_PRECONDITION(aws_byte_buf_is_valid(b)); @@ -107,8 +107,8 @@ bool aws_byte_buf_eq(const struct aws_byte_buf *const a, const struct aws_byte_b AWS_POSTCONDITION(aws_byte_buf_is_valid(a)); AWS_POSTCONDITION(aws_byte_buf_is_valid(b)); return rval; -} - +} + bool aws_byte_buf_eq_ignore_case(const struct aws_byte_buf *const a, const struct aws_byte_buf *const b) { AWS_PRECONDITION(aws_byte_buf_is_valid(a)); AWS_PRECONDITION(aws_byte_buf_is_valid(b)); @@ -116,49 +116,49 @@ bool aws_byte_buf_eq_ignore_case(const struct aws_byte_buf *const a, const struc AWS_POSTCONDITION(aws_byte_buf_is_valid(a)); AWS_POSTCONDITION(aws_byte_buf_is_valid(b)); return rval; -} - +} + bool aws_byte_buf_eq_c_str(const struct aws_byte_buf *const buf, const char *const c_str) { AWS_PRECONDITION(aws_byte_buf_is_valid(buf)); AWS_PRECONDITION(c_str != NULL); bool rval = aws_array_eq_c_str(buf->buffer, buf->len, c_str); AWS_POSTCONDITION(aws_byte_buf_is_valid(buf)); return rval; -} - +} + bool aws_byte_buf_eq_c_str_ignore_case(const struct aws_byte_buf *const buf, const char *const c_str) { AWS_PRECONDITION(aws_byte_buf_is_valid(buf)); AWS_PRECONDITION(c_str != NULL); bool rval = aws_array_eq_c_str_ignore_case(buf->buffer, buf->len, c_str); AWS_POSTCONDITION(aws_byte_buf_is_valid(buf)); return rval; -} - -int aws_byte_buf_init_copy_from_cursor( - struct aws_byte_buf *dest, - struct aws_allocator *allocator, - struct aws_byte_cursor src) { +} + +int aws_byte_buf_init_copy_from_cursor( + struct aws_byte_buf *dest, + struct aws_allocator *allocator, + struct aws_byte_cursor src) { AWS_PRECONDITION(allocator); AWS_PRECONDITION(dest); AWS_ERROR_PRECONDITION(aws_byte_cursor_is_valid(&src)); - - AWS_ZERO_STRUCT(*dest); - - dest->buffer = (src.len > 0) ? (uint8_t *)aws_mem_acquire(allocator, src.len) : NULL; - if (src.len != 0 && dest->buffer == NULL) { - return AWS_OP_ERR; - } - - dest->len = src.len; - dest->capacity = src.len; - dest->allocator = allocator; - if (src.len > 0) { - memcpy(dest->buffer, src.ptr, src.len); - } - AWS_POSTCONDITION(aws_byte_buf_is_valid(dest)); - return AWS_OP_SUCCESS; -} - + + AWS_ZERO_STRUCT(*dest); + + dest->buffer = (src.len > 0) ? (uint8_t *)aws_mem_acquire(allocator, src.len) : NULL; + if (src.len != 0 && dest->buffer == NULL) { + return AWS_OP_ERR; + } + + dest->len = src.len; + dest->capacity = src.len; + dest->allocator = allocator; + if (src.len > 0) { + memcpy(dest->buffer, src.ptr, src.len); + } + AWS_POSTCONDITION(aws_byte_buf_is_valid(dest)); + return AWS_OP_SUCCESS; +} + int aws_byte_buf_init_cache_and_update_cursors(struct aws_byte_buf *dest, struct aws_allocator *allocator, ...) { AWS_PRECONDITION(allocator); AWS_PRECONDITION(dest); @@ -193,16 +193,16 @@ int aws_byte_buf_init_cache_and_update_cursors(struct aws_byte_buf *dest, struct return AWS_OP_SUCCESS; } -bool aws_byte_cursor_next_split( - const struct aws_byte_cursor *AWS_RESTRICT input_str, - char split_on, - struct aws_byte_cursor *AWS_RESTRICT substr) { - +bool aws_byte_cursor_next_split( + const struct aws_byte_cursor *AWS_RESTRICT input_str, + char split_on, + struct aws_byte_cursor *AWS_RESTRICT substr) { + AWS_PRECONDITION(aws_byte_cursor_is_valid(input_str)); - + /* If substr is zeroed-out, then this is the first run. */ const bool first_run = substr->ptr == NULL; - + /* It's legal for input_str to be zeroed out: {.ptr=NULL, .len=0} * Deal with this case separately */ if (AWS_UNLIKELY(input_str->ptr == NULL)) { @@ -212,14 +212,14 @@ bool aws_byte_cursor_next_split( substr->len = 0; return true; } - + /* done */ - AWS_ZERO_STRUCT(*substr); - return false; - } - + AWS_ZERO_STRUCT(*substr); + return false; + } + /* Rest of function deals with non-NULL input_str->ptr */ - + if (first_run) { *substr = *input_str; } else { @@ -235,64 +235,64 @@ bool aws_byte_cursor_next_split( /* done */ AWS_ZERO_STRUCT(*substr); return false; - } + } /* update len to be remainder of the string */ substr->len = input_str->len - (substr->ptr - input_str->ptr); - } - + } + /* substr is now remainder of string, search for next split */ - uint8_t *new_location = memchr(substr->ptr, split_on, substr->len); - if (new_location) { - - /* Character found, update string length. */ - substr->len = new_location - substr->ptr; - } - + uint8_t *new_location = memchr(substr->ptr, split_on, substr->len); + if (new_location) { + + /* Character found, update string length. */ + substr->len = new_location - substr->ptr; + } + AWS_POSTCONDITION(aws_byte_cursor_is_valid(substr)); - return true; -} - -int aws_byte_cursor_split_on_char_n( - const struct aws_byte_cursor *AWS_RESTRICT input_str, - char split_on, - size_t n, - struct aws_array_list *AWS_RESTRICT output) { + return true; +} + +int aws_byte_cursor_split_on_char_n( + const struct aws_byte_cursor *AWS_RESTRICT input_str, + char split_on, + size_t n, + struct aws_array_list *AWS_RESTRICT output) { AWS_ASSERT(aws_byte_cursor_is_valid(input_str)); - AWS_ASSERT(output); - AWS_ASSERT(output->item_size >= sizeof(struct aws_byte_cursor)); - - size_t max_splits = n > 0 ? n : SIZE_MAX; - size_t split_count = 0; - - struct aws_byte_cursor substr; - AWS_ZERO_STRUCT(substr); - - /* Until we run out of substrs or hit the max split count, keep iterating and pushing into the array list. */ - while (split_count <= max_splits && aws_byte_cursor_next_split(input_str, split_on, &substr)) { - - if (split_count == max_splits) { - /* If this is the last split, take the rest of the string. */ - substr.len = input_str->len - (substr.ptr - input_str->ptr); - } - - if (AWS_UNLIKELY(aws_array_list_push_back(output, (const void *)&substr))) { - return AWS_OP_ERR; - } - ++split_count; - } - - return AWS_OP_SUCCESS; -} - -int aws_byte_cursor_split_on_char( - const struct aws_byte_cursor *AWS_RESTRICT input_str, - char split_on, - struct aws_array_list *AWS_RESTRICT output) { - - return aws_byte_cursor_split_on_char_n(input_str, split_on, 0, output); -} - + AWS_ASSERT(output); + AWS_ASSERT(output->item_size >= sizeof(struct aws_byte_cursor)); + + size_t max_splits = n > 0 ? n : SIZE_MAX; + size_t split_count = 0; + + struct aws_byte_cursor substr; + AWS_ZERO_STRUCT(substr); + + /* Until we run out of substrs or hit the max split count, keep iterating and pushing into the array list. */ + while (split_count <= max_splits && aws_byte_cursor_next_split(input_str, split_on, &substr)) { + + if (split_count == max_splits) { + /* If this is the last split, take the rest of the string. */ + substr.len = input_str->len - (substr.ptr - input_str->ptr); + } + + if (AWS_UNLIKELY(aws_array_list_push_back(output, (const void *)&substr))) { + return AWS_OP_ERR; + } + ++split_count; + } + + return AWS_OP_SUCCESS; +} + +int aws_byte_cursor_split_on_char( + const struct aws_byte_cursor *AWS_RESTRICT input_str, + char split_on, + struct aws_array_list *AWS_RESTRICT output) { + + return aws_byte_cursor_split_on_char_n(input_str, split_on, 0, output); +} + int aws_byte_cursor_find_exact( const struct aws_byte_cursor *AWS_RESTRICT input_str, const struct aws_byte_cursor *AWS_RESTRICT to_find, @@ -331,66 +331,66 @@ int aws_byte_cursor_find_exact( return aws_raise_error(AWS_ERROR_STRING_MATCH_NOT_FOUND); } -int aws_byte_buf_cat(struct aws_byte_buf *dest, size_t number_of_args, ...) { +int aws_byte_buf_cat(struct aws_byte_buf *dest, size_t number_of_args, ...) { AWS_PRECONDITION(aws_byte_buf_is_valid(dest)); - - va_list ap; - va_start(ap, number_of_args); - - for (size_t i = 0; i < number_of_args; ++i) { - struct aws_byte_buf *buffer = va_arg(ap, struct aws_byte_buf *); - struct aws_byte_cursor cursor = aws_byte_cursor_from_buf(buffer); - - if (aws_byte_buf_append(dest, &cursor)) { - va_end(ap); + + va_list ap; + va_start(ap, number_of_args); + + for (size_t i = 0; i < number_of_args; ++i) { + struct aws_byte_buf *buffer = va_arg(ap, struct aws_byte_buf *); + struct aws_byte_cursor cursor = aws_byte_cursor_from_buf(buffer); + + if (aws_byte_buf_append(dest, &cursor)) { + va_end(ap); AWS_POSTCONDITION(aws_byte_buf_is_valid(dest)); - return AWS_OP_ERR; - } - } - - va_end(ap); + return AWS_OP_ERR; + } + } + + va_end(ap); AWS_POSTCONDITION(aws_byte_buf_is_valid(dest)); - return AWS_OP_SUCCESS; -} - -bool aws_byte_cursor_eq(const struct aws_byte_cursor *a, const struct aws_byte_cursor *b) { + return AWS_OP_SUCCESS; +} + +bool aws_byte_cursor_eq(const struct aws_byte_cursor *a, const struct aws_byte_cursor *b) { AWS_PRECONDITION(aws_byte_cursor_is_valid(a)); AWS_PRECONDITION(aws_byte_cursor_is_valid(b)); bool rv = aws_array_eq(a->ptr, a->len, b->ptr, b->len); AWS_POSTCONDITION(aws_byte_cursor_is_valid(a)); AWS_POSTCONDITION(aws_byte_cursor_is_valid(b)); return rv; -} - -bool aws_byte_cursor_eq_ignore_case(const struct aws_byte_cursor *a, const struct aws_byte_cursor *b) { +} + +bool aws_byte_cursor_eq_ignore_case(const struct aws_byte_cursor *a, const struct aws_byte_cursor *b) { AWS_PRECONDITION(aws_byte_cursor_is_valid(a)); AWS_PRECONDITION(aws_byte_cursor_is_valid(b)); bool rv = aws_array_eq_ignore_case(a->ptr, a->len, b->ptr, b->len); AWS_POSTCONDITION(aws_byte_cursor_is_valid(a)); AWS_POSTCONDITION(aws_byte_cursor_is_valid(b)); return rv; -} - -/* Every possible uint8_t value, lowercased */ +} + +/* Every possible uint8_t value, lowercased */ static const uint8_t s_tolower_table[] = { - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, - 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, - 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 'a', - 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', - 'x', 'y', 'z', 91, 92, 93, 94, 95, 96, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', - 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 123, 124, 125, 126, 127, 128, 129, 130, 131, - 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, - 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, - 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, - 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, - 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, - 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255}; + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, + 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, + 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 'a', + 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', + 'x', 'y', 'z', 91, 92, 93, 94, 95, 96, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', + 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 123, 124, 125, 126, 127, 128, 129, 130, 131, + 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, + 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, + 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, + 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, + 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, + 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255}; AWS_STATIC_ASSERT(AWS_ARRAY_SIZE(s_tolower_table) == 256); - -const uint8_t *aws_lookup_table_to_lower_get(void) { - return s_tolower_table; -} - + +const uint8_t *aws_lookup_table_to_lower_get(void) { + return s_tolower_table; +} + bool aws_array_eq_ignore_case( const void *const array_a, const size_t len_a, @@ -400,128 +400,128 @@ bool aws_array_eq_ignore_case( (len_a == 0) || AWS_MEM_IS_READABLE(array_a, len_a), "Input array [array_a] must be readable up to [len_a]."); AWS_PRECONDITION( (len_b == 0) || AWS_MEM_IS_READABLE(array_b, len_b), "Input array [array_b] must be readable up to [len_b]."); - - if (len_a != len_b) { - return false; - } - - const uint8_t *bytes_a = array_a; - const uint8_t *bytes_b = array_b; - for (size_t i = 0; i < len_a; ++i) { - if (s_tolower_table[bytes_a[i]] != s_tolower_table[bytes_b[i]]) { - return false; - } - } - - return true; -} - + + if (len_a != len_b) { + return false; + } + + const uint8_t *bytes_a = array_a; + const uint8_t *bytes_b = array_b; + for (size_t i = 0; i < len_a; ++i) { + if (s_tolower_table[bytes_a[i]] != s_tolower_table[bytes_b[i]]) { + return false; + } + } + + return true; +} + bool aws_array_eq(const void *const array_a, const size_t len_a, const void *const array_b, const size_t len_b) { AWS_PRECONDITION( (len_a == 0) || AWS_MEM_IS_READABLE(array_a, len_a), "Input array [array_a] must be readable up to [len_a]."); AWS_PRECONDITION( (len_b == 0) || AWS_MEM_IS_READABLE(array_b, len_b), "Input array [array_b] must be readable up to [len_b]."); - - if (len_a != len_b) { - return false; - } - - if (len_a == 0) { - return true; - } - - return !memcmp(array_a, array_b, len_a); -} - + + if (len_a != len_b) { + return false; + } + + if (len_a == 0) { + return true; + } + + return !memcmp(array_a, array_b, len_a); +} + bool aws_array_eq_c_str_ignore_case(const void *const array, const size_t array_len, const char *const c_str) { AWS_PRECONDITION( array || (array_len == 0), "Either input pointer [array_a] mustn't be NULL or input [array_len] mustn't be zero."); AWS_PRECONDITION(c_str != NULL); - - /* Simpler implementation could have been: - * return aws_array_eq_ignore_case(array, array_len, c_str, strlen(c_str)); - * but that would have traversed c_str twice. - * This implementation traverses c_str just once. */ - - const uint8_t *array_bytes = array; - const uint8_t *str_bytes = (const uint8_t *)c_str; - - for (size_t i = 0; i < array_len; ++i) { - uint8_t s = str_bytes[i]; - if (s == '\0') { - return false; - } - - if (s_tolower_table[array_bytes[i]] != s_tolower_table[s]) { - return false; - } - } - - return str_bytes[array_len] == '\0'; -} - + + /* Simpler implementation could have been: + * return aws_array_eq_ignore_case(array, array_len, c_str, strlen(c_str)); + * but that would have traversed c_str twice. + * This implementation traverses c_str just once. */ + + const uint8_t *array_bytes = array; + const uint8_t *str_bytes = (const uint8_t *)c_str; + + for (size_t i = 0; i < array_len; ++i) { + uint8_t s = str_bytes[i]; + if (s == '\0') { + return false; + } + + if (s_tolower_table[array_bytes[i]] != s_tolower_table[s]) { + return false; + } + } + + return str_bytes[array_len] == '\0'; +} + bool aws_array_eq_c_str(const void *const array, const size_t array_len, const char *const c_str) { AWS_PRECONDITION( array || (array_len == 0), "Either input pointer [array_a] mustn't be NULL or input [array_len] mustn't be zero."); AWS_PRECONDITION(c_str != NULL); - - /* Simpler implementation could have been: - * return aws_array_eq(array, array_len, c_str, strlen(c_str)); - * but that would have traversed c_str twice. - * This implementation traverses c_str just once. */ - - const uint8_t *array_bytes = array; - const uint8_t *str_bytes = (const uint8_t *)c_str; - - for (size_t i = 0; i < array_len; ++i) { - uint8_t s = str_bytes[i]; - if (s == '\0') { - return false; - } - - if (array_bytes[i] != s) { - return false; - } - } - - return str_bytes[array_len] == '\0'; -} - + + /* Simpler implementation could have been: + * return aws_array_eq(array, array_len, c_str, strlen(c_str)); + * but that would have traversed c_str twice. + * This implementation traverses c_str just once. */ + + const uint8_t *array_bytes = array; + const uint8_t *str_bytes = (const uint8_t *)c_str; + + for (size_t i = 0; i < array_len; ++i) { + uint8_t s = str_bytes[i]; + if (s == '\0') { + return false; + } + + if (array_bytes[i] != s) { + return false; + } + } + + return str_bytes[array_len] == '\0'; +} + uint64_t aws_hash_array_ignore_case(const void *array, const size_t len) { AWS_PRECONDITION(AWS_MEM_IS_READABLE(array, len)); - /* FNV-1a: https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function */ - const uint64_t fnv_offset_basis = 0xcbf29ce484222325ULL; - const uint64_t fnv_prime = 0x100000001b3ULL; - - const uint8_t *i = array; - const uint8_t *end = i + len; - - uint64_t hash = fnv_offset_basis; - while (i != end) { - const uint8_t lower = s_tolower_table[*i++]; - hash ^= lower; + /* FNV-1a: https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function */ + const uint64_t fnv_offset_basis = 0xcbf29ce484222325ULL; + const uint64_t fnv_prime = 0x100000001b3ULL; + + const uint8_t *i = array; + const uint8_t *end = i + len; + + uint64_t hash = fnv_offset_basis; + while (i != end) { + const uint8_t lower = s_tolower_table[*i++]; + hash ^= lower; #ifdef CBMC # pragma CPROVER check push # pragma CPROVER check disable "unsigned-overflow" #endif - hash *= fnv_prime; + hash *= fnv_prime; #ifdef CBMC # pragma CPROVER check pop #endif - } - return hash; -} - -uint64_t aws_hash_byte_cursor_ptr_ignore_case(const void *item) { + } + return hash; +} + +uint64_t aws_hash_byte_cursor_ptr_ignore_case(const void *item) { AWS_PRECONDITION(aws_byte_cursor_is_valid(item)); const struct aws_byte_cursor *const cursor = item; uint64_t rval = aws_hash_array_ignore_case(cursor->ptr, cursor->len); AWS_POSTCONDITION(aws_byte_cursor_is_valid(item)); return rval; -} - +} + bool aws_byte_cursor_eq_byte_buf(const struct aws_byte_cursor *const a, const struct aws_byte_buf *const b) { AWS_PRECONDITION(aws_byte_cursor_is_valid(a)); AWS_PRECONDITION(aws_byte_buf_is_valid(b)); @@ -529,8 +529,8 @@ bool aws_byte_cursor_eq_byte_buf(const struct aws_byte_cursor *const a, const st AWS_POSTCONDITION(aws_byte_cursor_is_valid(a)); AWS_POSTCONDITION(aws_byte_buf_is_valid(b)); return rv; -} - +} + bool aws_byte_cursor_eq_byte_buf_ignore_case( const struct aws_byte_cursor *const a, const struct aws_byte_buf *const b) { @@ -540,176 +540,176 @@ bool aws_byte_cursor_eq_byte_buf_ignore_case( AWS_POSTCONDITION(aws_byte_cursor_is_valid(a)); AWS_POSTCONDITION(aws_byte_buf_is_valid(b)); return rv; -} - +} + bool aws_byte_cursor_eq_c_str(const struct aws_byte_cursor *const cursor, const char *const c_str) { AWS_PRECONDITION(aws_byte_cursor_is_valid(cursor)); AWS_PRECONDITION(c_str != NULL); bool rv = aws_array_eq_c_str(cursor->ptr, cursor->len, c_str); AWS_POSTCONDITION(aws_byte_cursor_is_valid(cursor)); return rv; -} - +} + bool aws_byte_cursor_eq_c_str_ignore_case(const struct aws_byte_cursor *const cursor, const char *const c_str) { AWS_PRECONDITION(aws_byte_cursor_is_valid(cursor)); AWS_PRECONDITION(c_str != NULL); bool rv = aws_array_eq_c_str_ignore_case(cursor->ptr, cursor->len, c_str); AWS_POSTCONDITION(aws_byte_cursor_is_valid(cursor)); return rv; -} - -int aws_byte_buf_append(struct aws_byte_buf *to, const struct aws_byte_cursor *from) { - AWS_PRECONDITION(aws_byte_buf_is_valid(to)); - AWS_PRECONDITION(aws_byte_cursor_is_valid(from)); - - if (to->capacity - to->len < from->len) { - AWS_POSTCONDITION(aws_byte_buf_is_valid(to)); - AWS_POSTCONDITION(aws_byte_cursor_is_valid(from)); - return aws_raise_error(AWS_ERROR_DEST_COPY_TOO_SMALL); - } - - if (from->len > 0) { - /* This assert teaches clang-tidy that from->ptr and to->buffer cannot be null in a non-empty buffers */ - AWS_ASSERT(from->ptr); - AWS_ASSERT(to->buffer); - memcpy(to->buffer + to->len, from->ptr, from->len); - to->len += from->len; - } - - AWS_POSTCONDITION(aws_byte_buf_is_valid(to)); - AWS_POSTCONDITION(aws_byte_cursor_is_valid(from)); - return AWS_OP_SUCCESS; -} - -int aws_byte_buf_append_with_lookup( - struct aws_byte_buf *AWS_RESTRICT to, - const struct aws_byte_cursor *AWS_RESTRICT from, - const uint8_t *lookup_table) { - AWS_PRECONDITION(aws_byte_buf_is_valid(to)); - AWS_PRECONDITION(aws_byte_cursor_is_valid(from)); +} + +int aws_byte_buf_append(struct aws_byte_buf *to, const struct aws_byte_cursor *from) { + AWS_PRECONDITION(aws_byte_buf_is_valid(to)); + AWS_PRECONDITION(aws_byte_cursor_is_valid(from)); + + if (to->capacity - to->len < from->len) { + AWS_POSTCONDITION(aws_byte_buf_is_valid(to)); + AWS_POSTCONDITION(aws_byte_cursor_is_valid(from)); + return aws_raise_error(AWS_ERROR_DEST_COPY_TOO_SMALL); + } + + if (from->len > 0) { + /* This assert teaches clang-tidy that from->ptr and to->buffer cannot be null in a non-empty buffers */ + AWS_ASSERT(from->ptr); + AWS_ASSERT(to->buffer); + memcpy(to->buffer + to->len, from->ptr, from->len); + to->len += from->len; + } + + AWS_POSTCONDITION(aws_byte_buf_is_valid(to)); + AWS_POSTCONDITION(aws_byte_cursor_is_valid(from)); + return AWS_OP_SUCCESS; +} + +int aws_byte_buf_append_with_lookup( + struct aws_byte_buf *AWS_RESTRICT to, + const struct aws_byte_cursor *AWS_RESTRICT from, + const uint8_t *lookup_table) { + AWS_PRECONDITION(aws_byte_buf_is_valid(to)); + AWS_PRECONDITION(aws_byte_cursor_is_valid(from)); AWS_PRECONDITION( AWS_MEM_IS_READABLE(lookup_table, 256), "Input array [lookup_table] must be at least 256 bytes long."); - - if (to->capacity - to->len < from->len) { - AWS_POSTCONDITION(aws_byte_buf_is_valid(to)); - AWS_POSTCONDITION(aws_byte_cursor_is_valid(from)); - return aws_raise_error(AWS_ERROR_DEST_COPY_TOO_SMALL); - } - - for (size_t i = 0; i < from->len; ++i) { - to->buffer[to->len + i] = lookup_table[from->ptr[i]]; - } - - if (aws_add_size_checked(to->len, from->len, &to->len)) { - return AWS_OP_ERR; - } - - AWS_POSTCONDITION(aws_byte_buf_is_valid(to)); - AWS_POSTCONDITION(aws_byte_cursor_is_valid(from)); - return AWS_OP_SUCCESS; -} - + + if (to->capacity - to->len < from->len) { + AWS_POSTCONDITION(aws_byte_buf_is_valid(to)); + AWS_POSTCONDITION(aws_byte_cursor_is_valid(from)); + return aws_raise_error(AWS_ERROR_DEST_COPY_TOO_SMALL); + } + + for (size_t i = 0; i < from->len; ++i) { + to->buffer[to->len + i] = lookup_table[from->ptr[i]]; + } + + if (aws_add_size_checked(to->len, from->len, &to->len)) { + return AWS_OP_ERR; + } + + AWS_POSTCONDITION(aws_byte_buf_is_valid(to)); + AWS_POSTCONDITION(aws_byte_cursor_is_valid(from)); + return AWS_OP_SUCCESS; +} + static int s_aws_byte_buf_append_dynamic( struct aws_byte_buf *to, const struct aws_byte_cursor *from, bool clear_released_memory) { - AWS_PRECONDITION(aws_byte_buf_is_valid(to)); - AWS_PRECONDITION(aws_byte_cursor_is_valid(from)); + AWS_PRECONDITION(aws_byte_buf_is_valid(to)); + AWS_PRECONDITION(aws_byte_cursor_is_valid(from)); AWS_ERROR_PRECONDITION(to->allocator); - - if (to->capacity - to->len < from->len) { - /* - * NewCapacity = Max(OldCapacity * 2, OldCapacity + MissingCapacity) - */ - size_t missing_capacity = from->len - (to->capacity - to->len); - - size_t required_capacity = 0; - if (aws_add_size_checked(to->capacity, missing_capacity, &required_capacity)) { - AWS_POSTCONDITION(aws_byte_buf_is_valid(to)); - AWS_POSTCONDITION(aws_byte_cursor_is_valid(from)); - return AWS_OP_ERR; - } - - /* - * It's ok if this overflows, just clamp to max possible. - * In theory this lets us still grow a buffer that's larger than 1/2 size_t space - * at least enough to accommodate the append. - */ - size_t growth_capacity = aws_add_size_saturating(to->capacity, to->capacity); - - size_t new_capacity = required_capacity; - if (new_capacity < growth_capacity) { - new_capacity = growth_capacity; - } - - /* - * Attempt to resize - we intentionally do not use reserve() in order to preserve - * the (unlikely) use case of from and to being the same buffer range. - */ - - /* - * Try the max, but if that fails and the required is smaller, try it in fallback - */ - uint8_t *new_buffer = aws_mem_acquire(to->allocator, new_capacity); - if (new_buffer == NULL) { - if (new_capacity > required_capacity) { - new_capacity = required_capacity; - new_buffer = aws_mem_acquire(to->allocator, new_capacity); - if (new_buffer == NULL) { - AWS_POSTCONDITION(aws_byte_buf_is_valid(to)); - AWS_POSTCONDITION(aws_byte_cursor_is_valid(from)); - return AWS_OP_ERR; - } - } else { - AWS_POSTCONDITION(aws_byte_buf_is_valid(to)); - AWS_POSTCONDITION(aws_byte_cursor_is_valid(from)); - return AWS_OP_ERR; - } - } - - /* - * Copy old buffer -> new buffer - */ - if (to->len > 0) { - memcpy(new_buffer, to->buffer, to->len); - } - /* - * Copy what we actually wanted to append in the first place - */ - if (from->len > 0) { - memcpy(new_buffer + to->len, from->ptr, from->len); - } + + if (to->capacity - to->len < from->len) { + /* + * NewCapacity = Max(OldCapacity * 2, OldCapacity + MissingCapacity) + */ + size_t missing_capacity = from->len - (to->capacity - to->len); + + size_t required_capacity = 0; + if (aws_add_size_checked(to->capacity, missing_capacity, &required_capacity)) { + AWS_POSTCONDITION(aws_byte_buf_is_valid(to)); + AWS_POSTCONDITION(aws_byte_cursor_is_valid(from)); + return AWS_OP_ERR; + } + + /* + * It's ok if this overflows, just clamp to max possible. + * In theory this lets us still grow a buffer that's larger than 1/2 size_t space + * at least enough to accommodate the append. + */ + size_t growth_capacity = aws_add_size_saturating(to->capacity, to->capacity); + + size_t new_capacity = required_capacity; + if (new_capacity < growth_capacity) { + new_capacity = growth_capacity; + } + + /* + * Attempt to resize - we intentionally do not use reserve() in order to preserve + * the (unlikely) use case of from and to being the same buffer range. + */ + + /* + * Try the max, but if that fails and the required is smaller, try it in fallback + */ + uint8_t *new_buffer = aws_mem_acquire(to->allocator, new_capacity); + if (new_buffer == NULL) { + if (new_capacity > required_capacity) { + new_capacity = required_capacity; + new_buffer = aws_mem_acquire(to->allocator, new_capacity); + if (new_buffer == NULL) { + AWS_POSTCONDITION(aws_byte_buf_is_valid(to)); + AWS_POSTCONDITION(aws_byte_cursor_is_valid(from)); + return AWS_OP_ERR; + } + } else { + AWS_POSTCONDITION(aws_byte_buf_is_valid(to)); + AWS_POSTCONDITION(aws_byte_cursor_is_valid(from)); + return AWS_OP_ERR; + } + } + + /* + * Copy old buffer -> new buffer + */ + if (to->len > 0) { + memcpy(new_buffer, to->buffer, to->len); + } + /* + * Copy what we actually wanted to append in the first place + */ + if (from->len > 0) { + memcpy(new_buffer + to->len, from->ptr, from->len); + } if (clear_released_memory) { aws_secure_zero(to->buffer, to->capacity); } - /* - * Get rid of the old buffer - */ - aws_mem_release(to->allocator, to->buffer); - - /* - * Switch to the new buffer - */ - to->buffer = new_buffer; - to->capacity = new_capacity; - } else { - if (from->len > 0) { - /* This assert teaches clang-tidy that from->ptr and to->buffer cannot be null in a non-empty buffers */ - AWS_ASSERT(from->ptr); - AWS_ASSERT(to->buffer); - memcpy(to->buffer + to->len, from->ptr, from->len); - } - } - - to->len += from->len; - - AWS_POSTCONDITION(aws_byte_buf_is_valid(to)); - AWS_POSTCONDITION(aws_byte_cursor_is_valid(from)); - return AWS_OP_SUCCESS; -} - + /* + * Get rid of the old buffer + */ + aws_mem_release(to->allocator, to->buffer); + + /* + * Switch to the new buffer + */ + to->buffer = new_buffer; + to->capacity = new_capacity; + } else { + if (from->len > 0) { + /* This assert teaches clang-tidy that from->ptr and to->buffer cannot be null in a non-empty buffers */ + AWS_ASSERT(from->ptr); + AWS_ASSERT(to->buffer); + memcpy(to->buffer + to->len, from->ptr, from->len); + } + } + + to->len += from->len; + + AWS_POSTCONDITION(aws_byte_buf_is_valid(to)); + AWS_POSTCONDITION(aws_byte_cursor_is_valid(from)); + return AWS_OP_SUCCESS; +} + int aws_byte_buf_append_dynamic(struct aws_byte_buf *to, const struct aws_byte_cursor *from) { return s_aws_byte_buf_append_dynamic(to, from, false); } @@ -742,87 +742,87 @@ int aws_byte_buf_append_byte_dynamic_secure(struct aws_byte_buf *buffer, uint8_t return s_aws_byte_buf_append_byte_dynamic(buffer, value, true); } -int aws_byte_buf_reserve(struct aws_byte_buf *buffer, size_t requested_capacity) { +int aws_byte_buf_reserve(struct aws_byte_buf *buffer, size_t requested_capacity) { AWS_ERROR_PRECONDITION(buffer->allocator); AWS_ERROR_PRECONDITION(aws_byte_buf_is_valid(buffer)); - - if (requested_capacity <= buffer->capacity) { - AWS_POSTCONDITION(aws_byte_buf_is_valid(buffer)); - return AWS_OP_SUCCESS; - } - - if (aws_mem_realloc(buffer->allocator, (void **)&buffer->buffer, buffer->capacity, requested_capacity)) { - return AWS_OP_ERR; - } - - buffer->capacity = requested_capacity; - - AWS_POSTCONDITION(aws_byte_buf_is_valid(buffer)); - return AWS_OP_SUCCESS; -} - -int aws_byte_buf_reserve_relative(struct aws_byte_buf *buffer, size_t additional_length) { + + if (requested_capacity <= buffer->capacity) { + AWS_POSTCONDITION(aws_byte_buf_is_valid(buffer)); + return AWS_OP_SUCCESS; + } + + if (aws_mem_realloc(buffer->allocator, (void **)&buffer->buffer, buffer->capacity, requested_capacity)) { + return AWS_OP_ERR; + } + + buffer->capacity = requested_capacity; + + AWS_POSTCONDITION(aws_byte_buf_is_valid(buffer)); + return AWS_OP_SUCCESS; +} + +int aws_byte_buf_reserve_relative(struct aws_byte_buf *buffer, size_t additional_length) { AWS_ERROR_PRECONDITION(buffer->allocator); AWS_ERROR_PRECONDITION(aws_byte_buf_is_valid(buffer)); - - size_t requested_capacity = 0; - if (AWS_UNLIKELY(aws_add_size_checked(buffer->len, additional_length, &requested_capacity))) { - AWS_POSTCONDITION(aws_byte_buf_is_valid(buffer)); - return AWS_OP_ERR; - } - - return aws_byte_buf_reserve(buffer, requested_capacity); -} - -struct aws_byte_cursor aws_byte_cursor_right_trim_pred( - const struct aws_byte_cursor *source, - aws_byte_predicate_fn *predicate) { + + size_t requested_capacity = 0; + if (AWS_UNLIKELY(aws_add_size_checked(buffer->len, additional_length, &requested_capacity))) { + AWS_POSTCONDITION(aws_byte_buf_is_valid(buffer)); + return AWS_OP_ERR; + } + + return aws_byte_buf_reserve(buffer, requested_capacity); +} + +struct aws_byte_cursor aws_byte_cursor_right_trim_pred( + const struct aws_byte_cursor *source, + aws_byte_predicate_fn *predicate) { AWS_PRECONDITION(aws_byte_cursor_is_valid(source)); AWS_PRECONDITION(predicate != NULL); - struct aws_byte_cursor trimmed = *source; - - while (trimmed.len > 0 && predicate(*(trimmed.ptr + trimmed.len - 1))) { - --trimmed.len; - } + struct aws_byte_cursor trimmed = *source; + + while (trimmed.len > 0 && predicate(*(trimmed.ptr + trimmed.len - 1))) { + --trimmed.len; + } AWS_POSTCONDITION(aws_byte_cursor_is_valid(source)); AWS_POSTCONDITION(aws_byte_cursor_is_valid(&trimmed)); - return trimmed; -} - -struct aws_byte_cursor aws_byte_cursor_left_trim_pred( - const struct aws_byte_cursor *source, - aws_byte_predicate_fn *predicate) { + return trimmed; +} + +struct aws_byte_cursor aws_byte_cursor_left_trim_pred( + const struct aws_byte_cursor *source, + aws_byte_predicate_fn *predicate) { AWS_PRECONDITION(aws_byte_cursor_is_valid(source)); AWS_PRECONDITION(predicate != NULL); - struct aws_byte_cursor trimmed = *source; - - while (trimmed.len > 0 && predicate(*(trimmed.ptr))) { - --trimmed.len; - ++trimmed.ptr; - } + struct aws_byte_cursor trimmed = *source; + + while (trimmed.len > 0 && predicate(*(trimmed.ptr))) { + --trimmed.len; + ++trimmed.ptr; + } AWS_POSTCONDITION(aws_byte_cursor_is_valid(source)); AWS_POSTCONDITION(aws_byte_cursor_is_valid(&trimmed)); - return trimmed; -} - -struct aws_byte_cursor aws_byte_cursor_trim_pred( - const struct aws_byte_cursor *source, - aws_byte_predicate_fn *predicate) { + return trimmed; +} + +struct aws_byte_cursor aws_byte_cursor_trim_pred( + const struct aws_byte_cursor *source, + aws_byte_predicate_fn *predicate) { AWS_PRECONDITION(aws_byte_cursor_is_valid(source)); AWS_PRECONDITION(predicate != NULL); - struct aws_byte_cursor left_trimmed = aws_byte_cursor_left_trim_pred(source, predicate); + struct aws_byte_cursor left_trimmed = aws_byte_cursor_left_trim_pred(source, predicate); struct aws_byte_cursor dest = aws_byte_cursor_right_trim_pred(&left_trimmed, predicate); AWS_POSTCONDITION(aws_byte_cursor_is_valid(source)); AWS_POSTCONDITION(aws_byte_cursor_is_valid(&dest)); return dest; -} - -bool aws_byte_cursor_satisfies_pred(const struct aws_byte_cursor *source, aws_byte_predicate_fn *predicate) { - struct aws_byte_cursor trimmed = aws_byte_cursor_left_trim_pred(source, predicate); +} + +bool aws_byte_cursor_satisfies_pred(const struct aws_byte_cursor *source, aws_byte_predicate_fn *predicate) { + struct aws_byte_cursor trimmed = aws_byte_cursor_left_trim_pred(source, predicate); bool rval = (trimmed.len == 0); AWS_POSTCONDITION(aws_byte_cursor_is_valid(source)); return rval; -} +} int aws_byte_cursor_compare_lexical(const struct aws_byte_cursor *lhs, const struct aws_byte_cursor *rhs) { AWS_PRECONDITION(aws_byte_cursor_is_valid(lhs)); diff --git a/contrib/restricted/aws/aws-c-common/source/codegen.c b/contrib/restricted/aws/aws-c-common/source/codegen.c index ea6e95d548..1469e63f37 100644 --- a/contrib/restricted/aws/aws-c-common/source/codegen.c +++ b/contrib/restricted/aws/aws-c-common/source/codegen.c @@ -1,14 +1,14 @@ /** * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0. - */ - -/* - * This file generates exportable implementations for inlineable functions. - */ - -#define AWS_STATIC_IMPL AWS_COMMON_API - + */ + +/* + * This file generates exportable implementations for inlineable functions. + */ + +#define AWS_STATIC_IMPL AWS_COMMON_API + #include <aws/common/array_list.inl> #include <aws/common/atomics.inl> #include <aws/common/byte_order.inl> diff --git a/contrib/restricted/aws/aws-c-common/source/command_line_parser.c b/contrib/restricted/aws/aws-c-common/source/command_line_parser.c index ccbe6d1820..bfb3f9f1aa 100644 --- a/contrib/restricted/aws/aws-c-common/source/command_line_parser.c +++ b/contrib/restricted/aws/aws-c-common/source/command_line_parser.c @@ -1,109 +1,109 @@ /** * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0. - */ -#include <aws/common/command_line_parser.h> - -int aws_cli_optind = 1; -int aws_cli_opterr = -1; -int aws_cli_optopt = 0; - -const char *aws_cli_optarg = NULL; - -static const struct aws_cli_option *s_find_option_from_char( - const struct aws_cli_option *longopts, - char search_for, - int *longindex) { - int index = 0; - const struct aws_cli_option *option = &longopts[index]; - - while (option->val != 0 || option->name) { - if (option->val == search_for) { - if (longindex) { - *longindex = index; - } - return option; - } - - option = &longopts[++index]; - } - - return NULL; -} - -static const struct aws_cli_option *s_find_option_from_c_str( - const struct aws_cli_option *longopts, - const char *search_for, - int *longindex) { - int index = 0; - const struct aws_cli_option *option = &longopts[index]; - - while (option->name || option->val != 0) { - if (option->name) { - if (option->name && !strcmp(search_for, option->name)) { - if (longindex) { - *longindex = index; - } - return option; - } - } - - option = &longopts[++index]; - } - - return NULL; -} - -int aws_cli_getopt_long( - int argc, - char *const argv[], - const char *optstring, - const struct aws_cli_option *longopts, - int *longindex) { - aws_cli_optarg = NULL; - - if (aws_cli_optind >= argc) { - return -1; - } - - char first_char = argv[aws_cli_optind][0]; - char second_char = argv[aws_cli_optind][1]; - char *option_start = NULL; - const struct aws_cli_option *option = NULL; - - if (first_char == '-' && second_char != '-') { - option_start = &argv[aws_cli_optind][1]; - option = s_find_option_from_char(longopts, *option_start, longindex); - } else if (first_char == '-' && second_char == '-') { - option_start = &argv[aws_cli_optind][2]; - option = s_find_option_from_c_str(longopts, option_start, longindex); - } else { - return -1; - } - - aws_cli_optind++; - if (option) { - bool has_arg = false; - + */ +#include <aws/common/command_line_parser.h> + +int aws_cli_optind = 1; +int aws_cli_opterr = -1; +int aws_cli_optopt = 0; + +const char *aws_cli_optarg = NULL; + +static const struct aws_cli_option *s_find_option_from_char( + const struct aws_cli_option *longopts, + char search_for, + int *longindex) { + int index = 0; + const struct aws_cli_option *option = &longopts[index]; + + while (option->val != 0 || option->name) { + if (option->val == search_for) { + if (longindex) { + *longindex = index; + } + return option; + } + + option = &longopts[++index]; + } + + return NULL; +} + +static const struct aws_cli_option *s_find_option_from_c_str( + const struct aws_cli_option *longopts, + const char *search_for, + int *longindex) { + int index = 0; + const struct aws_cli_option *option = &longopts[index]; + + while (option->name || option->val != 0) { + if (option->name) { + if (option->name && !strcmp(search_for, option->name)) { + if (longindex) { + *longindex = index; + } + return option; + } + } + + option = &longopts[++index]; + } + + return NULL; +} + +int aws_cli_getopt_long( + int argc, + char *const argv[], + const char *optstring, + const struct aws_cli_option *longopts, + int *longindex) { + aws_cli_optarg = NULL; + + if (aws_cli_optind >= argc) { + return -1; + } + + char first_char = argv[aws_cli_optind][0]; + char second_char = argv[aws_cli_optind][1]; + char *option_start = NULL; + const struct aws_cli_option *option = NULL; + + if (first_char == '-' && second_char != '-') { + option_start = &argv[aws_cli_optind][1]; + option = s_find_option_from_char(longopts, *option_start, longindex); + } else if (first_char == '-' && second_char == '-') { + option_start = &argv[aws_cli_optind][2]; + option = s_find_option_from_c_str(longopts, option_start, longindex); + } else { + return -1; + } + + aws_cli_optind++; + if (option) { + bool has_arg = false; + char *opt_value = memchr(optstring, option->val, strlen(optstring)); if (!opt_value) { return '?'; - } - + } + if (opt_value[1] == ':') { has_arg = true; } - if (has_arg) { + if (has_arg) { if (aws_cli_optind >= argc) { - return '?'; - } - - aws_cli_optarg = argv[aws_cli_optind++]; - } - - return option->val; - } - - return '?'; -} + return '?'; + } + + aws_cli_optarg = argv[aws_cli_optind++]; + } + + return option->val; + } + + return '?'; +} diff --git a/contrib/restricted/aws/aws-c-common/source/common.c b/contrib/restricted/aws/aws-c-common/source/common.c index 88c5d262c8..af5b90cd2a 100644 --- a/contrib/restricted/aws/aws-c-common/source/common.c +++ b/contrib/restricted/aws/aws-c-common/source/common.c @@ -1,35 +1,35 @@ /** * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0. - */ - -#include <aws/common/common.h> + */ + +#include <aws/common/common.h> #include <aws/common/logging.h> -#include <aws/common/math.h> +#include <aws/common/math.h> #include <aws/common/private/dlloads.h> - -#include <stdarg.h> -#include <stdlib.h> - -#ifdef _WIN32 -# include <Windows.h> + +#include <stdarg.h> +#include <stdlib.h> + +#ifdef _WIN32 +# include <Windows.h> #else # include <dlfcn.h> -#endif - -#ifdef __MACH__ -# include <CoreFoundation/CoreFoundation.h> -#endif - -/* turn off unused named parameter warning on msvc.*/ -#ifdef _MSC_VER -# pragma warning(push) -# pragma warning(disable : 4100) -#endif - +#endif + +#ifdef __MACH__ +# include <CoreFoundation/CoreFoundation.h> +#endif + +/* turn off unused named parameter warning on msvc.*/ +#ifdef _MSC_VER +# pragma warning(push) +# pragma warning(disable : 4100) +#endif + long (*g_set_mempolicy_ptr)(int, const unsigned long *, unsigned long) = NULL; void *g_libnuma_handle = NULL; - + void aws_secure_zero(void *pBuf, size_t bufsize) { #if defined(_WIN32) SecureZeroMemory(pBuf, bufsize); @@ -39,7 +39,7 @@ void aws_secure_zero(void *pBuf, size_t bufsize) { * * We'll try to work around this by using inline asm on GCC-like compilers, * and by exposing the buffer pointer in a volatile local pointer elsewhere. - */ + */ # if defined(__GNUC__) || defined(__clang__) memset(pBuf, 0, bufsize); /* This inline asm serves to convince the compiler that the buffer is (somehow) still @@ -65,143 +65,143 @@ void aws_secure_zero(void *pBuf, size_t bufsize) { memset(pVolBuf, 0, bufsize); # endif // #else not GCC/clang #endif // #else not windows -} - +} + #define AWS_DEFINE_ERROR_INFO_COMMON(C, ES) [(C)-0x0000] = AWS_DEFINE_ERROR_INFO(C, ES, "aws-c-common") -/* clang-format off */ -static struct aws_error_info errors[] = { - AWS_DEFINE_ERROR_INFO_COMMON( - AWS_ERROR_SUCCESS, - "Success."), - AWS_DEFINE_ERROR_INFO_COMMON( - AWS_ERROR_OOM, - "Out of memory."), - AWS_DEFINE_ERROR_INFO_COMMON( - AWS_ERROR_UNKNOWN, - "Unknown error."), - AWS_DEFINE_ERROR_INFO_COMMON( - AWS_ERROR_SHORT_BUFFER, - "Buffer is not large enough to hold result."), - AWS_DEFINE_ERROR_INFO_COMMON( - AWS_ERROR_OVERFLOW_DETECTED, - "Fixed size value overflow was detected."), - AWS_DEFINE_ERROR_INFO_COMMON( - AWS_ERROR_UNSUPPORTED_OPERATION, - "Unsupported operation."), - AWS_DEFINE_ERROR_INFO_COMMON( - AWS_ERROR_INVALID_BUFFER_SIZE, - "Invalid buffer size."), - AWS_DEFINE_ERROR_INFO_COMMON( - AWS_ERROR_INVALID_HEX_STR, - "Invalid hex string."), - AWS_DEFINE_ERROR_INFO_COMMON( - AWS_ERROR_INVALID_BASE64_STR, - "Invalid base64 string."), - AWS_DEFINE_ERROR_INFO_COMMON( - AWS_ERROR_INVALID_INDEX, - "Invalid index for list access."), - AWS_DEFINE_ERROR_INFO_COMMON( - AWS_ERROR_THREAD_INVALID_SETTINGS, - "Invalid thread settings."), - AWS_DEFINE_ERROR_INFO_COMMON( - AWS_ERROR_THREAD_INSUFFICIENT_RESOURCE, - "Insufficent resources for thread."), - AWS_DEFINE_ERROR_INFO_COMMON( - AWS_ERROR_THREAD_NO_PERMISSIONS, - "Insufficient permissions for thread operation."), - AWS_DEFINE_ERROR_INFO_COMMON( - AWS_ERROR_THREAD_NOT_JOINABLE, - "Thread not joinable."), - AWS_DEFINE_ERROR_INFO_COMMON( - AWS_ERROR_THREAD_NO_SUCH_THREAD_ID, - "No such thread ID."), - AWS_DEFINE_ERROR_INFO_COMMON( - AWS_ERROR_THREAD_DEADLOCK_DETECTED, - "Deadlock detected in thread."), - AWS_DEFINE_ERROR_INFO_COMMON( - AWS_ERROR_MUTEX_NOT_INIT, - "Mutex not initialized."), - AWS_DEFINE_ERROR_INFO_COMMON( - AWS_ERROR_MUTEX_TIMEOUT, - "Mutex operation timed out."), - AWS_DEFINE_ERROR_INFO_COMMON( - AWS_ERROR_MUTEX_CALLER_NOT_OWNER, - "The caller of a mutex operation was not the owner."), - AWS_DEFINE_ERROR_INFO_COMMON( - AWS_ERROR_MUTEX_FAILED, - "Mutex operation failed."), - AWS_DEFINE_ERROR_INFO_COMMON( - AWS_ERROR_COND_VARIABLE_INIT_FAILED, - "Condition variable initialization failed."), - AWS_DEFINE_ERROR_INFO_COMMON( - AWS_ERROR_COND_VARIABLE_TIMED_OUT, - "Condition variable wait timed out."), - AWS_DEFINE_ERROR_INFO_COMMON( - AWS_ERROR_COND_VARIABLE_ERROR_UNKNOWN, - "Condition variable unknown error."), - AWS_DEFINE_ERROR_INFO_COMMON( - AWS_ERROR_CLOCK_FAILURE, - "Clock operation failed."), - AWS_DEFINE_ERROR_INFO_COMMON( - AWS_ERROR_LIST_EMPTY, - "Empty list."), - AWS_DEFINE_ERROR_INFO_COMMON( - AWS_ERROR_DEST_COPY_TOO_SMALL, - "Destination of copy is too small."), - AWS_DEFINE_ERROR_INFO_COMMON( - AWS_ERROR_LIST_EXCEEDS_MAX_SIZE, - "A requested operation on a list would exceed it's max size."), - AWS_DEFINE_ERROR_INFO_COMMON( - AWS_ERROR_LIST_STATIC_MODE_CANT_SHRINK, - "Attempt to shrink a list in static mode."), - AWS_DEFINE_ERROR_INFO_COMMON( - AWS_ERROR_PRIORITY_QUEUE_FULL, - "Attempt to add items to a full preallocated queue in static mode."), - AWS_DEFINE_ERROR_INFO_COMMON( - AWS_ERROR_PRIORITY_QUEUE_EMPTY, - "Attempt to pop an item from an empty queue."), - AWS_DEFINE_ERROR_INFO_COMMON( - AWS_ERROR_PRIORITY_QUEUE_BAD_NODE, - "Bad node handle passed to remove."), - AWS_DEFINE_ERROR_INFO_COMMON( - AWS_ERROR_HASHTBL_ITEM_NOT_FOUND, - "Item not found in hash table."), - AWS_DEFINE_ERROR_INFO_COMMON( - AWS_ERROR_INVALID_DATE_STR, - "Date string is invalid and cannot be parsed." - ), - AWS_DEFINE_ERROR_INFO_COMMON( - AWS_ERROR_INVALID_ARGUMENT, - "An invalid argument was passed to a function." - ), - AWS_DEFINE_ERROR_INFO_COMMON( - AWS_ERROR_RANDOM_GEN_FAILED, - "A call to the random number generator failed. Retry later." - ), - AWS_DEFINE_ERROR_INFO_COMMON( - AWS_ERROR_MALFORMED_INPUT_STRING, - "An input string was passed to a parser and the string was incorrectly formatted." - ), - AWS_DEFINE_ERROR_INFO_COMMON( - AWS_ERROR_UNIMPLEMENTED, - "A function was called, but is not implemented." - ), - AWS_DEFINE_ERROR_INFO_COMMON( - AWS_ERROR_INVALID_STATE, - "An invalid state was encountered." - ), - AWS_DEFINE_ERROR_INFO_COMMON( - AWS_ERROR_ENVIRONMENT_GET, - "System call failure when getting an environment variable." - ), - AWS_DEFINE_ERROR_INFO_COMMON( - AWS_ERROR_ENVIRONMENT_SET, - "System call failure when setting an environment variable." - ), - AWS_DEFINE_ERROR_INFO_COMMON( - AWS_ERROR_ENVIRONMENT_UNSET, - "System call failure when unsetting an environment variable." - ), +/* clang-format off */ +static struct aws_error_info errors[] = { + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_SUCCESS, + "Success."), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_OOM, + "Out of memory."), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_UNKNOWN, + "Unknown error."), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_SHORT_BUFFER, + "Buffer is not large enough to hold result."), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_OVERFLOW_DETECTED, + "Fixed size value overflow was detected."), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_UNSUPPORTED_OPERATION, + "Unsupported operation."), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_INVALID_BUFFER_SIZE, + "Invalid buffer size."), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_INVALID_HEX_STR, + "Invalid hex string."), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_INVALID_BASE64_STR, + "Invalid base64 string."), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_INVALID_INDEX, + "Invalid index for list access."), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_THREAD_INVALID_SETTINGS, + "Invalid thread settings."), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_THREAD_INSUFFICIENT_RESOURCE, + "Insufficent resources for thread."), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_THREAD_NO_PERMISSIONS, + "Insufficient permissions for thread operation."), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_THREAD_NOT_JOINABLE, + "Thread not joinable."), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_THREAD_NO_SUCH_THREAD_ID, + "No such thread ID."), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_THREAD_DEADLOCK_DETECTED, + "Deadlock detected in thread."), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_MUTEX_NOT_INIT, + "Mutex not initialized."), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_MUTEX_TIMEOUT, + "Mutex operation timed out."), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_MUTEX_CALLER_NOT_OWNER, + "The caller of a mutex operation was not the owner."), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_MUTEX_FAILED, + "Mutex operation failed."), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_COND_VARIABLE_INIT_FAILED, + "Condition variable initialization failed."), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_COND_VARIABLE_TIMED_OUT, + "Condition variable wait timed out."), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_COND_VARIABLE_ERROR_UNKNOWN, + "Condition variable unknown error."), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_CLOCK_FAILURE, + "Clock operation failed."), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_LIST_EMPTY, + "Empty list."), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_DEST_COPY_TOO_SMALL, + "Destination of copy is too small."), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_LIST_EXCEEDS_MAX_SIZE, + "A requested operation on a list would exceed it's max size."), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_LIST_STATIC_MODE_CANT_SHRINK, + "Attempt to shrink a list in static mode."), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_PRIORITY_QUEUE_FULL, + "Attempt to add items to a full preallocated queue in static mode."), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_PRIORITY_QUEUE_EMPTY, + "Attempt to pop an item from an empty queue."), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_PRIORITY_QUEUE_BAD_NODE, + "Bad node handle passed to remove."), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_HASHTBL_ITEM_NOT_FOUND, + "Item not found in hash table."), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_INVALID_DATE_STR, + "Date string is invalid and cannot be parsed." + ), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_INVALID_ARGUMENT, + "An invalid argument was passed to a function." + ), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_RANDOM_GEN_FAILED, + "A call to the random number generator failed. Retry later." + ), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_MALFORMED_INPUT_STRING, + "An input string was passed to a parser and the string was incorrectly formatted." + ), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_UNIMPLEMENTED, + "A function was called, but is not implemented." + ), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_INVALID_STATE, + "An invalid state was encountered." + ), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_ENVIRONMENT_GET, + "System call failure when getting an environment variable." + ), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_ENVIRONMENT_SET, + "System call failure when setting an environment variable." + ), + AWS_DEFINE_ERROR_INFO_COMMON( + AWS_ERROR_ENVIRONMENT_UNSET, + "System call failure when unsetting an environment variable." + ), AWS_DEFINE_ERROR_INFO_COMMON( AWS_ERROR_SYS_CALL_FAILURE, "System call failure"), @@ -226,14 +226,14 @@ static struct aws_error_info errors[] = { AWS_DEFINE_ERROR_INFO_COMMON( AWS_ERROR_DIVIDE_BY_ZERO, "Attempt to divide a number by zero."), -}; -/* clang-format on */ - -static struct aws_error_info_list s_list = { - .error_list = errors, - .count = AWS_ARRAY_SIZE(errors), -}; - +}; +/* clang-format on */ + +static struct aws_error_info_list s_list = { + .error_list = errors, + .count = AWS_ARRAY_SIZE(errors), +}; + static struct aws_log_subject_info s_common_log_subject_infos[] = { DEFINE_LOG_SUBJECT_INFO( AWS_LS_COMMON_GENERAL, @@ -260,7 +260,7 @@ void aws_common_library_init(struct aws_allocator *allocator) { if (!s_common_library_initialized) { s_common_library_initialized = true; - aws_register_error_info(&s_list); + aws_register_error_info(&s_list); aws_register_log_subject_info_list(&s_common_log_subject_list); /* NUMA is funky and we can't rely on libnuma.so being available. We also don't want to take a hard dependency on it, @@ -280,9 +280,9 @@ void aws_common_library_init(struct aws_allocator *allocator) { AWS_LOGF_INFO(AWS_LS_COMMON_GENERAL, "static: libnuma.so failed to load"); } #endif - } -} - + } +} + void aws_common_library_clean_up(void) { if (s_common_library_initialized) { s_common_library_initialized = false; @@ -294,8 +294,8 @@ void aws_common_library_clean_up(void) { } #endif } -} - +} + void aws_common_fatal_assert_library_initialized(void) { if (!s_common_library_initialized) { fprintf( @@ -305,6 +305,6 @@ void aws_common_fatal_assert_library_initialized(void) { } } -#ifdef _MSC_VER -# pragma warning(pop) -#endif +#ifdef _MSC_VER +# pragma warning(pop) +#endif diff --git a/contrib/restricted/aws/aws-c-common/source/condition_variable.c b/contrib/restricted/aws/aws-c-common/source/condition_variable.c index 6d67dbbeaa..88b6f501b6 100644 --- a/contrib/restricted/aws/aws-c-common/source/condition_variable.c +++ b/contrib/restricted/aws/aws-c-common/source/condition_variable.c @@ -1,35 +1,35 @@ /** * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0. - */ - -#include <aws/common/condition_variable.h> - -int aws_condition_variable_wait_pred( - struct aws_condition_variable *condition_variable, - struct aws_mutex *mutex, - aws_condition_predicate_fn *pred, - void *pred_ctx) { - - int err_code = 0; - while (!err_code && !pred(pred_ctx)) { - err_code = aws_condition_variable_wait(condition_variable, mutex); - } - - return err_code; -} - -int aws_condition_variable_wait_for_pred( - struct aws_condition_variable *condition_variable, - struct aws_mutex *mutex, - int64_t time_to_wait, - aws_condition_predicate_fn *pred, - void *pred_ctx) { - - int err_code = 0; - while (!err_code && !pred(pred_ctx)) { - err_code = aws_condition_variable_wait_for(condition_variable, mutex, time_to_wait); - } - - return err_code; -} + */ + +#include <aws/common/condition_variable.h> + +int aws_condition_variable_wait_pred( + struct aws_condition_variable *condition_variable, + struct aws_mutex *mutex, + aws_condition_predicate_fn *pred, + void *pred_ctx) { + + int err_code = 0; + while (!err_code && !pred(pred_ctx)) { + err_code = aws_condition_variable_wait(condition_variable, mutex); + } + + return err_code; +} + +int aws_condition_variable_wait_for_pred( + struct aws_condition_variable *condition_variable, + struct aws_mutex *mutex, + int64_t time_to_wait, + aws_condition_predicate_fn *pred, + void *pred_ctx) { + + int err_code = 0; + while (!err_code && !pred(pred_ctx)) { + err_code = aws_condition_variable_wait_for(condition_variable, mutex, time_to_wait); + } + + return err_code; +} diff --git a/contrib/restricted/aws/aws-c-common/source/date_time.c b/contrib/restricted/aws/aws-c-common/source/date_time.c index 8d08e57ad8..40a224490e 100644 --- a/contrib/restricted/aws/aws-c-common/source/date_time.c +++ b/contrib/restricted/aws/aws-c-common/source/date_time.c @@ -1,807 +1,807 @@ /** * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0. - */ -#include <aws/common/date_time.h> - -#include <aws/common/array_list.h> -#include <aws/common/byte_buf.h> -#include <aws/common/byte_order.h> -#include <aws/common/clock.h> -#include <aws/common/string.h> -#include <aws/common/time.h> - -#include <ctype.h> - -static const char *RFC822_DATE_FORMAT_STR_MINUS_Z = "%a, %d %b %Y %H:%M:%S GMT"; -static const char *RFC822_DATE_FORMAT_STR_WITH_Z = "%a, %d %b %Y %H:%M:%S %Z"; -static const char *RFC822_SHORT_DATE_FORMAT_STR = "%a, %d %b %Y"; -static const char *ISO_8601_LONG_DATE_FORMAT_STR = "%Y-%m-%dT%H:%M:%SZ"; -static const char *ISO_8601_SHORT_DATE_FORMAT_STR = "%Y-%m-%d"; -static const char *ISO_8601_LONG_BASIC_DATE_FORMAT_STR = "%Y%m%dT%H%M%SZ"; -static const char *ISO_8601_SHORT_BASIC_DATE_FORMAT_STR = "%Y%m%d"; - -#define STR_TRIPLET_TO_INDEX(str) \ - (((uint32_t)(uint8_t)tolower((str)[0]) << 0) | ((uint32_t)(uint8_t)tolower((str)[1]) << 8) | \ - ((uint32_t)(uint8_t)tolower((str)[2]) << 16)) - -static uint32_t s_jan = 0; -static uint32_t s_feb = 0; -static uint32_t s_mar = 0; -static uint32_t s_apr = 0; -static uint32_t s_may = 0; -static uint32_t s_jun = 0; -static uint32_t s_jul = 0; -static uint32_t s_aug = 0; -static uint32_t s_sep = 0; -static uint32_t s_oct = 0; -static uint32_t s_nov = 0; -static uint32_t s_dec = 0; - -static uint32_t s_utc = 0; -static uint32_t s_gmt = 0; - -static void s_check_init_str_to_int(void) { - if (!s_jan) { - s_jan = STR_TRIPLET_TO_INDEX("jan"); - s_feb = STR_TRIPLET_TO_INDEX("feb"); - s_mar = STR_TRIPLET_TO_INDEX("mar"); - s_apr = STR_TRIPLET_TO_INDEX("apr"); - s_may = STR_TRIPLET_TO_INDEX("may"); - s_jun = STR_TRIPLET_TO_INDEX("jun"); - s_jul = STR_TRIPLET_TO_INDEX("jul"); - s_aug = STR_TRIPLET_TO_INDEX("aug"); - s_sep = STR_TRIPLET_TO_INDEX("sep"); - s_oct = STR_TRIPLET_TO_INDEX("oct"); - s_nov = STR_TRIPLET_TO_INDEX("nov"); - s_dec = STR_TRIPLET_TO_INDEX("dec"); - s_utc = STR_TRIPLET_TO_INDEX("utc"); - s_gmt = STR_TRIPLET_TO_INDEX("gmt"); - } -} - -/* Get the 0-11 monthy number from a string representing Month. Case insensitive and will stop on abbreviation*/ -static int get_month_number_from_str(const char *time_string, size_t start_index, size_t stop_index) { - s_check_init_str_to_int(); - - if (stop_index - start_index < 3) { - return -1; - } - - /* This AND forces the string to lowercase (assuming ASCII) */ - uint32_t comp_val = STR_TRIPLET_TO_INDEX(time_string + start_index); - - /* this can't be a switch, because I can't make it a constant expression. */ - if (s_jan == comp_val) { - return 0; - } - - if (s_feb == comp_val) { - return 1; - } - - if (s_mar == comp_val) { - return 2; - } - - if (s_apr == comp_val) { - return 3; - } - - if (s_may == comp_val) { - return 4; - } - - if (s_jun == comp_val) { - return 5; - } - - if (s_jul == comp_val) { - return 6; - } - - if (s_aug == comp_val) { - return 7; - } - - if (s_sep == comp_val) { - return 8; - } - - if (s_oct == comp_val) { - return 9; - } - - if (s_nov == comp_val) { - return 10; - } - - if (s_dec == comp_val) { - return 11; - } - - return -1; -} - -/* Detects whether or not the passed in timezone string is a UTC zone. */ -static bool is_utc_time_zone(const char *str) { - s_check_init_str_to_int(); - - size_t len = strlen(str); - - if (len > 0) { - if (str[0] == 'Z') { - return true; - } - - /* offsets count since their usable */ - if (len == 5 && (str[0] == '+' || str[0] == '-')) { - return true; - } - - if (len == 2) { - return tolower(str[0]) == 'u' && tolower(str[1]) == 't'; - } - - if (len < 3) { - return false; - } - - uint32_t comp_val = STR_TRIPLET_TO_INDEX(str); - - if (comp_val == s_utc || comp_val == s_gmt) { - return true; - } - } - - return false; -} - -struct tm s_get_time_struct(struct aws_date_time *dt, bool local_time) { - struct tm time; - AWS_ZERO_STRUCT(time); - if (local_time) { - aws_localtime(dt->timestamp, &time); - } else { - aws_gmtime(dt->timestamp, &time); - } - - return time; -} - -void aws_date_time_init_now(struct aws_date_time *dt) { - uint64_t current_time = 0; - aws_sys_clock_get_ticks(¤t_time); - dt->timestamp = (time_t)aws_timestamp_convert(current_time, AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_SECS, NULL); - dt->gmt_time = s_get_time_struct(dt, false); - dt->local_time = s_get_time_struct(dt, true); -} - -void aws_date_time_init_epoch_millis(struct aws_date_time *dt, uint64_t ms_since_epoch) { - dt->timestamp = (time_t)(ms_since_epoch / AWS_TIMESTAMP_MILLIS); - dt->gmt_time = s_get_time_struct(dt, false); - dt->local_time = s_get_time_struct(dt, true); -} - -void aws_date_time_init_epoch_secs(struct aws_date_time *dt, double sec_ms) { - dt->timestamp = (time_t)sec_ms; - dt->gmt_time = s_get_time_struct(dt, false); - dt->local_time = s_get_time_struct(dt, true); -} - -enum parser_state { - ON_WEEKDAY, - ON_SPACE_DELIM, - ON_YEAR, - ON_MONTH, - ON_MONTH_DAY, - ON_HOUR, - ON_MINUTE, - ON_SECOND, - ON_TZ, - FINISHED, -}; - -static int s_parse_iso_8601_basic(const struct aws_byte_cursor *date_str_cursor, struct tm *parsed_time) { - size_t index = 0; - size_t state_start_index = 0; - enum parser_state state = ON_YEAR; - bool error = false; - - AWS_ZERO_STRUCT(*parsed_time); - - while (state < FINISHED && !error && index < date_str_cursor->len) { - char c = date_str_cursor->ptr[index]; - size_t sub_index = index - state_start_index; - switch (state) { - case ON_YEAR: + */ +#include <aws/common/date_time.h> + +#include <aws/common/array_list.h> +#include <aws/common/byte_buf.h> +#include <aws/common/byte_order.h> +#include <aws/common/clock.h> +#include <aws/common/string.h> +#include <aws/common/time.h> + +#include <ctype.h> + +static const char *RFC822_DATE_FORMAT_STR_MINUS_Z = "%a, %d %b %Y %H:%M:%S GMT"; +static const char *RFC822_DATE_FORMAT_STR_WITH_Z = "%a, %d %b %Y %H:%M:%S %Z"; +static const char *RFC822_SHORT_DATE_FORMAT_STR = "%a, %d %b %Y"; +static const char *ISO_8601_LONG_DATE_FORMAT_STR = "%Y-%m-%dT%H:%M:%SZ"; +static const char *ISO_8601_SHORT_DATE_FORMAT_STR = "%Y-%m-%d"; +static const char *ISO_8601_LONG_BASIC_DATE_FORMAT_STR = "%Y%m%dT%H%M%SZ"; +static const char *ISO_8601_SHORT_BASIC_DATE_FORMAT_STR = "%Y%m%d"; + +#define STR_TRIPLET_TO_INDEX(str) \ + (((uint32_t)(uint8_t)tolower((str)[0]) << 0) | ((uint32_t)(uint8_t)tolower((str)[1]) << 8) | \ + ((uint32_t)(uint8_t)tolower((str)[2]) << 16)) + +static uint32_t s_jan = 0; +static uint32_t s_feb = 0; +static uint32_t s_mar = 0; +static uint32_t s_apr = 0; +static uint32_t s_may = 0; +static uint32_t s_jun = 0; +static uint32_t s_jul = 0; +static uint32_t s_aug = 0; +static uint32_t s_sep = 0; +static uint32_t s_oct = 0; +static uint32_t s_nov = 0; +static uint32_t s_dec = 0; + +static uint32_t s_utc = 0; +static uint32_t s_gmt = 0; + +static void s_check_init_str_to_int(void) { + if (!s_jan) { + s_jan = STR_TRIPLET_TO_INDEX("jan"); + s_feb = STR_TRIPLET_TO_INDEX("feb"); + s_mar = STR_TRIPLET_TO_INDEX("mar"); + s_apr = STR_TRIPLET_TO_INDEX("apr"); + s_may = STR_TRIPLET_TO_INDEX("may"); + s_jun = STR_TRIPLET_TO_INDEX("jun"); + s_jul = STR_TRIPLET_TO_INDEX("jul"); + s_aug = STR_TRIPLET_TO_INDEX("aug"); + s_sep = STR_TRIPLET_TO_INDEX("sep"); + s_oct = STR_TRIPLET_TO_INDEX("oct"); + s_nov = STR_TRIPLET_TO_INDEX("nov"); + s_dec = STR_TRIPLET_TO_INDEX("dec"); + s_utc = STR_TRIPLET_TO_INDEX("utc"); + s_gmt = STR_TRIPLET_TO_INDEX("gmt"); + } +} + +/* Get the 0-11 monthy number from a string representing Month. Case insensitive and will stop on abbreviation*/ +static int get_month_number_from_str(const char *time_string, size_t start_index, size_t stop_index) { + s_check_init_str_to_int(); + + if (stop_index - start_index < 3) { + return -1; + } + + /* This AND forces the string to lowercase (assuming ASCII) */ + uint32_t comp_val = STR_TRIPLET_TO_INDEX(time_string + start_index); + + /* this can't be a switch, because I can't make it a constant expression. */ + if (s_jan == comp_val) { + return 0; + } + + if (s_feb == comp_val) { + return 1; + } + + if (s_mar == comp_val) { + return 2; + } + + if (s_apr == comp_val) { + return 3; + } + + if (s_may == comp_val) { + return 4; + } + + if (s_jun == comp_val) { + return 5; + } + + if (s_jul == comp_val) { + return 6; + } + + if (s_aug == comp_val) { + return 7; + } + + if (s_sep == comp_val) { + return 8; + } + + if (s_oct == comp_val) { + return 9; + } + + if (s_nov == comp_val) { + return 10; + } + + if (s_dec == comp_val) { + return 11; + } + + return -1; +} + +/* Detects whether or not the passed in timezone string is a UTC zone. */ +static bool is_utc_time_zone(const char *str) { + s_check_init_str_to_int(); + + size_t len = strlen(str); + + if (len > 0) { + if (str[0] == 'Z') { + return true; + } + + /* offsets count since their usable */ + if (len == 5 && (str[0] == '+' || str[0] == '-')) { + return true; + } + + if (len == 2) { + return tolower(str[0]) == 'u' && tolower(str[1]) == 't'; + } + + if (len < 3) { + return false; + } + + uint32_t comp_val = STR_TRIPLET_TO_INDEX(str); + + if (comp_val == s_utc || comp_val == s_gmt) { + return true; + } + } + + return false; +} + +struct tm s_get_time_struct(struct aws_date_time *dt, bool local_time) { + struct tm time; + AWS_ZERO_STRUCT(time); + if (local_time) { + aws_localtime(dt->timestamp, &time); + } else { + aws_gmtime(dt->timestamp, &time); + } + + return time; +} + +void aws_date_time_init_now(struct aws_date_time *dt) { + uint64_t current_time = 0; + aws_sys_clock_get_ticks(¤t_time); + dt->timestamp = (time_t)aws_timestamp_convert(current_time, AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_SECS, NULL); + dt->gmt_time = s_get_time_struct(dt, false); + dt->local_time = s_get_time_struct(dt, true); +} + +void aws_date_time_init_epoch_millis(struct aws_date_time *dt, uint64_t ms_since_epoch) { + dt->timestamp = (time_t)(ms_since_epoch / AWS_TIMESTAMP_MILLIS); + dt->gmt_time = s_get_time_struct(dt, false); + dt->local_time = s_get_time_struct(dt, true); +} + +void aws_date_time_init_epoch_secs(struct aws_date_time *dt, double sec_ms) { + dt->timestamp = (time_t)sec_ms; + dt->gmt_time = s_get_time_struct(dt, false); + dt->local_time = s_get_time_struct(dt, true); +} + +enum parser_state { + ON_WEEKDAY, + ON_SPACE_DELIM, + ON_YEAR, + ON_MONTH, + ON_MONTH_DAY, + ON_HOUR, + ON_MINUTE, + ON_SECOND, + ON_TZ, + FINISHED, +}; + +static int s_parse_iso_8601_basic(const struct aws_byte_cursor *date_str_cursor, struct tm *parsed_time) { + size_t index = 0; + size_t state_start_index = 0; + enum parser_state state = ON_YEAR; + bool error = false; + + AWS_ZERO_STRUCT(*parsed_time); + + while (state < FINISHED && !error && index < date_str_cursor->len) { + char c = date_str_cursor->ptr[index]; + size_t sub_index = index - state_start_index; + switch (state) { + case ON_YEAR: if (aws_isdigit(c)) { - parsed_time->tm_year = parsed_time->tm_year * 10 + (c - '0'); - if (sub_index == 3) { - state = ON_MONTH; - state_start_index = index + 1; - parsed_time->tm_year -= 1900; - } - } else { - error = true; - } - break; - - case ON_MONTH: + parsed_time->tm_year = parsed_time->tm_year * 10 + (c - '0'); + if (sub_index == 3) { + state = ON_MONTH; + state_start_index = index + 1; + parsed_time->tm_year -= 1900; + } + } else { + error = true; + } + break; + + case ON_MONTH: if (aws_isdigit(c)) { - parsed_time->tm_mon = parsed_time->tm_mon * 10 + (c - '0'); - if (sub_index == 1) { - state = ON_MONTH_DAY; - state_start_index = index + 1; - parsed_time->tm_mon -= 1; - } - } else { - error = true; - } - break; - - case ON_MONTH_DAY: - if (c == 'T' && sub_index == 2) { - state = ON_HOUR; - state_start_index = index + 1; + parsed_time->tm_mon = parsed_time->tm_mon * 10 + (c - '0'); + if (sub_index == 1) { + state = ON_MONTH_DAY; + state_start_index = index + 1; + parsed_time->tm_mon -= 1; + } + } else { + error = true; + } + break; + + case ON_MONTH_DAY: + if (c == 'T' && sub_index == 2) { + state = ON_HOUR; + state_start_index = index + 1; } else if (aws_isdigit(c)) { - parsed_time->tm_mday = parsed_time->tm_mday * 10 + (c - '0'); - } else { - error = true; - } - break; - - case ON_HOUR: + parsed_time->tm_mday = parsed_time->tm_mday * 10 + (c - '0'); + } else { + error = true; + } + break; + + case ON_HOUR: if (aws_isdigit(c)) { - parsed_time->tm_hour = parsed_time->tm_hour * 10 + (c - '0'); - if (sub_index == 1) { - state = ON_MINUTE; - state_start_index = index + 1; - } - } else { - error = true; - } - break; - - case ON_MINUTE: + parsed_time->tm_hour = parsed_time->tm_hour * 10 + (c - '0'); + if (sub_index == 1) { + state = ON_MINUTE; + state_start_index = index + 1; + } + } else { + error = true; + } + break; + + case ON_MINUTE: if (aws_isdigit(c)) { - parsed_time->tm_min = parsed_time->tm_min * 10 + (c - '0'); - if (sub_index == 1) { - state = ON_SECOND; - state_start_index = index + 1; - } - } else { - error = true; - } - break; - - case ON_SECOND: + parsed_time->tm_min = parsed_time->tm_min * 10 + (c - '0'); + if (sub_index == 1) { + state = ON_SECOND; + state_start_index = index + 1; + } + } else { + error = true; + } + break; + + case ON_SECOND: if (aws_isdigit(c)) { - parsed_time->tm_sec = parsed_time->tm_sec * 10 + (c - '0'); - if (sub_index == 1) { - state = ON_TZ; - state_start_index = index + 1; - } - } else { - error = true; - } - break; - - case ON_TZ: - if (c == 'Z' && (sub_index == 0 || sub_index == 3)) { - state = FINISHED; + parsed_time->tm_sec = parsed_time->tm_sec * 10 + (c - '0'); + if (sub_index == 1) { + state = ON_TZ; + state_start_index = index + 1; + } + } else { + error = true; + } + break; + + case ON_TZ: + if (c == 'Z' && (sub_index == 0 || sub_index == 3)) { + state = FINISHED; } else if (!aws_isdigit(c) || sub_index > 3) { - error = true; - } - break; - - default: - error = true; - break; - } - - index++; - } - - /* ISO8601 supports date only with no time portion. state ==ON_MONTH_DAY catches this case. */ - return (state == FINISHED || state == ON_MONTH_DAY) && !error ? AWS_OP_SUCCESS : AWS_OP_ERR; -} - -static int s_parse_iso_8601(const struct aws_byte_cursor *date_str_cursor, struct tm *parsed_time) { - size_t index = 0; - size_t state_start_index = 0; - enum parser_state state = ON_YEAR; - bool error = false; - bool advance = true; - - AWS_ZERO_STRUCT(*parsed_time); - - while (state < FINISHED && !error && index < date_str_cursor->len) { - char c = date_str_cursor->ptr[index]; - switch (state) { - case ON_YEAR: - if (c == '-' && index - state_start_index == 4) { - state = ON_MONTH; - state_start_index = index + 1; - parsed_time->tm_year -= 1900; + error = true; + } + break; + + default: + error = true; + break; + } + + index++; + } + + /* ISO8601 supports date only with no time portion. state ==ON_MONTH_DAY catches this case. */ + return (state == FINISHED || state == ON_MONTH_DAY) && !error ? AWS_OP_SUCCESS : AWS_OP_ERR; +} + +static int s_parse_iso_8601(const struct aws_byte_cursor *date_str_cursor, struct tm *parsed_time) { + size_t index = 0; + size_t state_start_index = 0; + enum parser_state state = ON_YEAR; + bool error = false; + bool advance = true; + + AWS_ZERO_STRUCT(*parsed_time); + + while (state < FINISHED && !error && index < date_str_cursor->len) { + char c = date_str_cursor->ptr[index]; + switch (state) { + case ON_YEAR: + if (c == '-' && index - state_start_index == 4) { + state = ON_MONTH; + state_start_index = index + 1; + parsed_time->tm_year -= 1900; } else if (aws_isdigit(c)) { - parsed_time->tm_year = parsed_time->tm_year * 10 + (c - '0'); - } else { - error = true; - } - break; - case ON_MONTH: - if (c == '-' && index - state_start_index == 2) { - state = ON_MONTH_DAY; - state_start_index = index + 1; - parsed_time->tm_mon -= 1; + parsed_time->tm_year = parsed_time->tm_year * 10 + (c - '0'); + } else { + error = true; + } + break; + case ON_MONTH: + if (c == '-' && index - state_start_index == 2) { + state = ON_MONTH_DAY; + state_start_index = index + 1; + parsed_time->tm_mon -= 1; } else if (aws_isdigit(c)) { - parsed_time->tm_mon = parsed_time->tm_mon * 10 + (c - '0'); - } else { - error = true; - } - - break; - case ON_MONTH_DAY: - if (c == 'T' && index - state_start_index == 2) { - state = ON_HOUR; - state_start_index = index + 1; + parsed_time->tm_mon = parsed_time->tm_mon * 10 + (c - '0'); + } else { + error = true; + } + + break; + case ON_MONTH_DAY: + if (c == 'T' && index - state_start_index == 2) { + state = ON_HOUR; + state_start_index = index + 1; } else if (aws_isdigit(c)) { - parsed_time->tm_mday = parsed_time->tm_mday * 10 + (c - '0'); - } else { - error = true; - } - break; - /* note: no time portion is spec compliant. */ - case ON_HOUR: - /* time parts can be delimited by ':' or just concatenated together, but must always be 2 digits. */ - if (index - state_start_index == 2) { - state = ON_MINUTE; - state_start_index = index + 1; + parsed_time->tm_mday = parsed_time->tm_mday * 10 + (c - '0'); + } else { + error = true; + } + break; + /* note: no time portion is spec compliant. */ + case ON_HOUR: + /* time parts can be delimited by ':' or just concatenated together, but must always be 2 digits. */ + if (index - state_start_index == 2) { + state = ON_MINUTE; + state_start_index = index + 1; if (aws_isdigit(c)) { - state_start_index = index; - advance = false; - } else if (c != ':') { - error = true; - } + state_start_index = index; + advance = false; + } else if (c != ':') { + error = true; + } } else if (aws_isdigit(c)) { - parsed_time->tm_hour = parsed_time->tm_hour * 10 + (c - '0'); - } else { - error = true; - } - - break; - case ON_MINUTE: - /* time parts can be delimited by ':' or just concatenated together, but must always be 2 digits. */ - if (index - state_start_index == 2) { - state = ON_SECOND; - state_start_index = index + 1; + parsed_time->tm_hour = parsed_time->tm_hour * 10 + (c - '0'); + } else { + error = true; + } + + break; + case ON_MINUTE: + /* time parts can be delimited by ':' or just concatenated together, but must always be 2 digits. */ + if (index - state_start_index == 2) { + state = ON_SECOND; + state_start_index = index + 1; if (aws_isdigit(c)) { - state_start_index = index; - advance = false; - } else if (c != ':') { - error = true; - } + state_start_index = index; + advance = false; + } else if (c != ':') { + error = true; + } } else if (aws_isdigit(c)) { - parsed_time->tm_min = parsed_time->tm_min * 10 + (c - '0'); - } else { - error = true; - } - - break; - case ON_SECOND: - if (c == 'Z' && index - state_start_index == 2) { - state = FINISHED; - state_start_index = index + 1; - } else if (c == '.' && index - state_start_index == 2) { - state = ON_TZ; - state_start_index = index + 1; + parsed_time->tm_min = parsed_time->tm_min * 10 + (c - '0'); + } else { + error = true; + } + + break; + case ON_SECOND: + if (c == 'Z' && index - state_start_index == 2) { + state = FINISHED; + state_start_index = index + 1; + } else if (c == '.' && index - state_start_index == 2) { + state = ON_TZ; + state_start_index = index + 1; } else if (aws_isdigit(c)) { - parsed_time->tm_sec = parsed_time->tm_sec * 10 + (c - '0'); - } else { - error = true; - } - - break; - case ON_TZ: - if (c == 'Z') { - state = FINISHED; - state_start_index = index + 1; + parsed_time->tm_sec = parsed_time->tm_sec * 10 + (c - '0'); + } else { + error = true; + } + + break; + case ON_TZ: + if (c == 'Z') { + state = FINISHED; + state_start_index = index + 1; } else if (!aws_isdigit(c)) { - error = true; - } - break; - default: - error = true; - break; - } - - if (advance) { - index++; - } else { - advance = true; - } - } - - /* ISO8601 supports date only with no time portion. state ==ON_MONTH_DAY catches this case. */ - return (state == FINISHED || state == ON_MONTH_DAY) && !error ? AWS_OP_SUCCESS : AWS_OP_ERR; -} - -static int s_parse_rfc_822( - const struct aws_byte_cursor *date_str_cursor, - struct tm *parsed_time, - struct aws_date_time *dt) { - size_t len = date_str_cursor->len; - - size_t index = 0; - size_t state_start_index = 0; - int state = ON_WEEKDAY; - bool error = false; - - AWS_ZERO_STRUCT(*parsed_time); - - while (!error && index < len) { - char c = date_str_cursor->ptr[index]; - - switch (state) { - /* week day abbr is optional. */ - case ON_WEEKDAY: - if (c == ',') { - state = ON_SPACE_DELIM; - state_start_index = index + 1; + error = true; + } + break; + default: + error = true; + break; + } + + if (advance) { + index++; + } else { + advance = true; + } + } + + /* ISO8601 supports date only with no time portion. state ==ON_MONTH_DAY catches this case. */ + return (state == FINISHED || state == ON_MONTH_DAY) && !error ? AWS_OP_SUCCESS : AWS_OP_ERR; +} + +static int s_parse_rfc_822( + const struct aws_byte_cursor *date_str_cursor, + struct tm *parsed_time, + struct aws_date_time *dt) { + size_t len = date_str_cursor->len; + + size_t index = 0; + size_t state_start_index = 0; + int state = ON_WEEKDAY; + bool error = false; + + AWS_ZERO_STRUCT(*parsed_time); + + while (!error && index < len) { + char c = date_str_cursor->ptr[index]; + + switch (state) { + /* week day abbr is optional. */ + case ON_WEEKDAY: + if (c == ',') { + state = ON_SPACE_DELIM; + state_start_index = index + 1; } else if (aws_isdigit(c)) { - state = ON_MONTH_DAY; + state = ON_MONTH_DAY; } else if (!aws_isalpha(c)) { - error = true; - } - break; - case ON_SPACE_DELIM: + error = true; + } + break; + case ON_SPACE_DELIM: if (aws_isspace(c)) { - state = ON_MONTH_DAY; - state_start_index = index + 1; - } else { - error = true; - } - break; - case ON_MONTH_DAY: + state = ON_MONTH_DAY; + state_start_index = index + 1; + } else { + error = true; + } + break; + case ON_MONTH_DAY: if (aws_isdigit(c)) { - parsed_time->tm_mday = parsed_time->tm_mday * 10 + (c - '0'); + parsed_time->tm_mday = parsed_time->tm_mday * 10 + (c - '0'); } else if (aws_isspace(c)) { - state = ON_MONTH; - state_start_index = index + 1; - } else { - error = true; - } - break; - case ON_MONTH: + state = ON_MONTH; + state_start_index = index + 1; + } else { + error = true; + } + break; + case ON_MONTH: if (aws_isspace(c)) { - int monthNumber = - get_month_number_from_str((const char *)date_str_cursor->ptr, state_start_index, index + 1); - - if (monthNumber > -1) { - state = ON_YEAR; - state_start_index = index + 1; - parsed_time->tm_mon = monthNumber; - } else { - error = true; - } + int monthNumber = + get_month_number_from_str((const char *)date_str_cursor->ptr, state_start_index, index + 1); + + if (monthNumber > -1) { + state = ON_YEAR; + state_start_index = index + 1; + parsed_time->tm_mon = monthNumber; + } else { + error = true; + } } else if (!aws_isalpha(c)) { - error = true; - } - break; - /* year can be 4 or 2 digits. */ - case ON_YEAR: + error = true; + } + break; + /* year can be 4 or 2 digits. */ + case ON_YEAR: if (aws_isspace(c) && index - state_start_index == 4) { - state = ON_HOUR; - state_start_index = index + 1; - parsed_time->tm_year -= 1900; + state = ON_HOUR; + state_start_index = index + 1; + parsed_time->tm_year -= 1900; } else if (aws_isspace(c) && index - state_start_index == 2) { - state = 5; - state_start_index = index + 1; - parsed_time->tm_year += 2000 - 1900; + state = 5; + state_start_index = index + 1; + parsed_time->tm_year += 2000 - 1900; } else if (aws_isdigit(c)) { - parsed_time->tm_year = parsed_time->tm_year * 10 + (c - '0'); - } else { - error = true; - } - break; - case ON_HOUR: - if (c == ':' && index - state_start_index == 2) { - state = ON_MINUTE; - state_start_index = index + 1; + parsed_time->tm_year = parsed_time->tm_year * 10 + (c - '0'); + } else { + error = true; + } + break; + case ON_HOUR: + if (c == ':' && index - state_start_index == 2) { + state = ON_MINUTE; + state_start_index = index + 1; } else if (aws_isdigit(c)) { - parsed_time->tm_hour = parsed_time->tm_hour * 10 + (c - '0'); - } else { - error = true; - } - break; - case ON_MINUTE: - if (c == ':' && index - state_start_index == 2) { - state = ON_SECOND; - state_start_index = index + 1; + parsed_time->tm_hour = parsed_time->tm_hour * 10 + (c - '0'); + } else { + error = true; + } + break; + case ON_MINUTE: + if (c == ':' && index - state_start_index == 2) { + state = ON_SECOND; + state_start_index = index + 1; } else if (aws_isdigit(c)) { - parsed_time->tm_min = parsed_time->tm_min * 10 + (c - '0'); - } else { - error = true; - } - break; - case ON_SECOND: + parsed_time->tm_min = parsed_time->tm_min * 10 + (c - '0'); + } else { + error = true; + } + break; + case ON_SECOND: if (aws_isspace(c) && index - state_start_index == 2) { - state = ON_TZ; - state_start_index = index + 1; + state = ON_TZ; + state_start_index = index + 1; } else if (aws_isdigit(c)) { - parsed_time->tm_sec = parsed_time->tm_sec * 10 + (c - '0'); - } else { - error = true; - } - break; - case ON_TZ: + parsed_time->tm_sec = parsed_time->tm_sec * 10 + (c - '0'); + } else { + error = true; + } + break; + case ON_TZ: if ((aws_isalnum(c) || c == '-' || c == '+') && (index - state_start_index) < 5) { - dt->tz[index - state_start_index] = c; - } else { - error = true; - } - - break; - default: - error = true; - break; - } - - index++; - } - - if (dt->tz[0] != 0) { - if (is_utc_time_zone(dt->tz)) { - dt->utc_assumed = true; - } else { - error = true; - } - } - - return error || state != ON_TZ ? AWS_OP_ERR : AWS_OP_SUCCESS; -} - -int aws_date_time_init_from_str_cursor( - struct aws_date_time *dt, - const struct aws_byte_cursor *date_str_cursor, - enum aws_date_format fmt) { + dt->tz[index - state_start_index] = c; + } else { + error = true; + } + + break; + default: + error = true; + break; + } + + index++; + } + + if (dt->tz[0] != 0) { + if (is_utc_time_zone(dt->tz)) { + dt->utc_assumed = true; + } else { + error = true; + } + } + + return error || state != ON_TZ ? AWS_OP_ERR : AWS_OP_SUCCESS; +} + +int aws_date_time_init_from_str_cursor( + struct aws_date_time *dt, + const struct aws_byte_cursor *date_str_cursor, + enum aws_date_format fmt) { AWS_ERROR_PRECONDITION(date_str_cursor->len <= AWS_DATE_TIME_STR_MAX_LEN, AWS_ERROR_OVERFLOW_DETECTED); - - AWS_ZERO_STRUCT(*dt); - - struct tm parsed_time; - bool successfully_parsed = false; - - time_t seconds_offset = 0; - if (fmt == AWS_DATE_FORMAT_ISO_8601 || fmt == AWS_DATE_FORMAT_AUTO_DETECT) { - if (!s_parse_iso_8601(date_str_cursor, &parsed_time)) { - dt->utc_assumed = true; - successfully_parsed = true; - } - } - - if (fmt == AWS_DATE_FORMAT_ISO_8601_BASIC || (fmt == AWS_DATE_FORMAT_AUTO_DETECT && !successfully_parsed)) { - if (!s_parse_iso_8601_basic(date_str_cursor, &parsed_time)) { - dt->utc_assumed = true; - successfully_parsed = true; - } - } - - if (fmt == AWS_DATE_FORMAT_RFC822 || (fmt == AWS_DATE_FORMAT_AUTO_DETECT && !successfully_parsed)) { - if (!s_parse_rfc_822(date_str_cursor, &parsed_time, dt)) { - successfully_parsed = true; - - if (dt->utc_assumed) { - if (dt->tz[0] == '+' || dt->tz[0] == '-') { - /* in this format, the offset is in format +/-HHMM so convert that to seconds and we'll use - * the offset later. */ - char min_str[3] = {0}; - char hour_str[3] = {0}; - hour_str[0] = dt->tz[1]; - hour_str[1] = dt->tz[2]; - min_str[0] = dt->tz[3]; - min_str[1] = dt->tz[4]; - - long hour = strtol(hour_str, NULL, 10); - long min = strtol(min_str, NULL, 10); - seconds_offset = (time_t)(hour * 3600 + min * 60); - - if (dt->tz[0] == '-') { - seconds_offset = -seconds_offset; - } - } - } - } - } - - if (!successfully_parsed) { - return aws_raise_error(AWS_ERROR_INVALID_DATE_STR); - } - - if (dt->utc_assumed || seconds_offset) { - dt->timestamp = aws_timegm(&parsed_time); - } else { - dt->timestamp = mktime(&parsed_time); - } - - /* negative means we need to move west (increase the timestamp), positive means head east, so decrease the - * timestamp. */ - dt->timestamp -= seconds_offset; - - dt->gmt_time = s_get_time_struct(dt, false); - dt->local_time = s_get_time_struct(dt, true); - - return AWS_OP_SUCCESS; -} - -int aws_date_time_init_from_str( - struct aws_date_time *dt, - const struct aws_byte_buf *date_str, - enum aws_date_format fmt) { + + AWS_ZERO_STRUCT(*dt); + + struct tm parsed_time; + bool successfully_parsed = false; + + time_t seconds_offset = 0; + if (fmt == AWS_DATE_FORMAT_ISO_8601 || fmt == AWS_DATE_FORMAT_AUTO_DETECT) { + if (!s_parse_iso_8601(date_str_cursor, &parsed_time)) { + dt->utc_assumed = true; + successfully_parsed = true; + } + } + + if (fmt == AWS_DATE_FORMAT_ISO_8601_BASIC || (fmt == AWS_DATE_FORMAT_AUTO_DETECT && !successfully_parsed)) { + if (!s_parse_iso_8601_basic(date_str_cursor, &parsed_time)) { + dt->utc_assumed = true; + successfully_parsed = true; + } + } + + if (fmt == AWS_DATE_FORMAT_RFC822 || (fmt == AWS_DATE_FORMAT_AUTO_DETECT && !successfully_parsed)) { + if (!s_parse_rfc_822(date_str_cursor, &parsed_time, dt)) { + successfully_parsed = true; + + if (dt->utc_assumed) { + if (dt->tz[0] == '+' || dt->tz[0] == '-') { + /* in this format, the offset is in format +/-HHMM so convert that to seconds and we'll use + * the offset later. */ + char min_str[3] = {0}; + char hour_str[3] = {0}; + hour_str[0] = dt->tz[1]; + hour_str[1] = dt->tz[2]; + min_str[0] = dt->tz[3]; + min_str[1] = dt->tz[4]; + + long hour = strtol(hour_str, NULL, 10); + long min = strtol(min_str, NULL, 10); + seconds_offset = (time_t)(hour * 3600 + min * 60); + + if (dt->tz[0] == '-') { + seconds_offset = -seconds_offset; + } + } + } + } + } + + if (!successfully_parsed) { + return aws_raise_error(AWS_ERROR_INVALID_DATE_STR); + } + + if (dt->utc_assumed || seconds_offset) { + dt->timestamp = aws_timegm(&parsed_time); + } else { + dt->timestamp = mktime(&parsed_time); + } + + /* negative means we need to move west (increase the timestamp), positive means head east, so decrease the + * timestamp. */ + dt->timestamp -= seconds_offset; + + dt->gmt_time = s_get_time_struct(dt, false); + dt->local_time = s_get_time_struct(dt, true); + + return AWS_OP_SUCCESS; +} + +int aws_date_time_init_from_str( + struct aws_date_time *dt, + const struct aws_byte_buf *date_str, + enum aws_date_format fmt) { AWS_ERROR_PRECONDITION(date_str->len <= AWS_DATE_TIME_STR_MAX_LEN, AWS_ERROR_OVERFLOW_DETECTED); - - struct aws_byte_cursor date_cursor = aws_byte_cursor_from_buf(date_str); - return aws_date_time_init_from_str_cursor(dt, &date_cursor, fmt); -} - -static inline int s_date_to_str(const struct tm *tm, const char *format_str, struct aws_byte_buf *output_buf) { - size_t remaining_space = output_buf->capacity - output_buf->len; - size_t bytes_written = strftime((char *)output_buf->buffer + output_buf->len, remaining_space, format_str, tm); - - if (bytes_written == 0) { - return aws_raise_error(AWS_ERROR_SHORT_BUFFER); - } - - output_buf->len += bytes_written; - - return AWS_OP_SUCCESS; -} - -int aws_date_time_to_local_time_str( - const struct aws_date_time *dt, - enum aws_date_format fmt, - struct aws_byte_buf *output_buf) { - AWS_ASSERT(fmt != AWS_DATE_FORMAT_AUTO_DETECT); - - switch (fmt) { - case AWS_DATE_FORMAT_RFC822: - return s_date_to_str(&dt->local_time, RFC822_DATE_FORMAT_STR_WITH_Z, output_buf); - - case AWS_DATE_FORMAT_ISO_8601: - return s_date_to_str(&dt->local_time, ISO_8601_LONG_DATE_FORMAT_STR, output_buf); - - case AWS_DATE_FORMAT_ISO_8601_BASIC: - return s_date_to_str(&dt->local_time, ISO_8601_LONG_BASIC_DATE_FORMAT_STR, output_buf); - - default: - return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); - } -} - -int aws_date_time_to_utc_time_str( - const struct aws_date_time *dt, - enum aws_date_format fmt, - struct aws_byte_buf *output_buf) { - AWS_ASSERT(fmt != AWS_DATE_FORMAT_AUTO_DETECT); - - switch (fmt) { - case AWS_DATE_FORMAT_RFC822: - return s_date_to_str(&dt->gmt_time, RFC822_DATE_FORMAT_STR_MINUS_Z, output_buf); - - case AWS_DATE_FORMAT_ISO_8601: - return s_date_to_str(&dt->gmt_time, ISO_8601_LONG_DATE_FORMAT_STR, output_buf); - - case AWS_DATE_FORMAT_ISO_8601_BASIC: - return s_date_to_str(&dt->gmt_time, ISO_8601_LONG_BASIC_DATE_FORMAT_STR, output_buf); - - default: - return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); - } -} - -int aws_date_time_to_local_time_short_str( - const struct aws_date_time *dt, - enum aws_date_format fmt, - struct aws_byte_buf *output_buf) { - AWS_ASSERT(fmt != AWS_DATE_FORMAT_AUTO_DETECT); - - switch (fmt) { - case AWS_DATE_FORMAT_RFC822: - return s_date_to_str(&dt->local_time, RFC822_SHORT_DATE_FORMAT_STR, output_buf); - - case AWS_DATE_FORMAT_ISO_8601: - return s_date_to_str(&dt->local_time, ISO_8601_SHORT_DATE_FORMAT_STR, output_buf); - - case AWS_DATE_FORMAT_ISO_8601_BASIC: - return s_date_to_str(&dt->local_time, ISO_8601_SHORT_BASIC_DATE_FORMAT_STR, output_buf); - - default: - return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); - } -} - -int aws_date_time_to_utc_time_short_str( - const struct aws_date_time *dt, - enum aws_date_format fmt, - struct aws_byte_buf *output_buf) { - AWS_ASSERT(fmt != AWS_DATE_FORMAT_AUTO_DETECT); - - switch (fmt) { - case AWS_DATE_FORMAT_RFC822: - return s_date_to_str(&dt->gmt_time, RFC822_SHORT_DATE_FORMAT_STR, output_buf); - - case AWS_DATE_FORMAT_ISO_8601: - return s_date_to_str(&dt->gmt_time, ISO_8601_SHORT_DATE_FORMAT_STR, output_buf); - - case AWS_DATE_FORMAT_ISO_8601_BASIC: - return s_date_to_str(&dt->gmt_time, ISO_8601_SHORT_BASIC_DATE_FORMAT_STR, output_buf); - - default: - return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); - } -} - -double aws_date_time_as_epoch_secs(const struct aws_date_time *dt) { - return (double)dt->timestamp; -} - -uint64_t aws_date_time_as_nanos(const struct aws_date_time *dt) { - return (uint64_t)dt->timestamp * AWS_TIMESTAMP_NANOS; -} - -uint64_t aws_date_time_as_millis(const struct aws_date_time *dt) { - return (uint64_t)dt->timestamp * AWS_TIMESTAMP_MILLIS; -} - -uint16_t aws_date_time_year(const struct aws_date_time *dt, bool local_time) { - const struct tm *time = local_time ? &dt->local_time : &dt->gmt_time; - - return (uint16_t)(time->tm_year + 1900); -} - -enum aws_date_month aws_date_time_month(const struct aws_date_time *dt, bool local_time) { - const struct tm *time = local_time ? &dt->local_time : &dt->gmt_time; - - return time->tm_mon; -} - -uint8_t aws_date_time_month_day(const struct aws_date_time *dt, bool local_time) { - const struct tm *time = local_time ? &dt->local_time : &dt->gmt_time; - - return (uint8_t)time->tm_mday; -} - -enum aws_date_day_of_week aws_date_time_day_of_week(const struct aws_date_time *dt, bool local_time) { - const struct tm *time = local_time ? &dt->local_time : &dt->gmt_time; - - return time->tm_wday; -} - -uint8_t aws_date_time_hour(const struct aws_date_time *dt, bool local_time) { - const struct tm *time = local_time ? &dt->local_time : &dt->gmt_time; - - return (uint8_t)time->tm_hour; -} - -uint8_t aws_date_time_minute(const struct aws_date_time *dt, bool local_time) { - const struct tm *time = local_time ? &dt->local_time : &dt->gmt_time; - - return (uint8_t)time->tm_min; -} - -uint8_t aws_date_time_second(const struct aws_date_time *dt, bool local_time) { - const struct tm *time = local_time ? &dt->local_time : &dt->gmt_time; - - return (uint8_t)time->tm_sec; -} - -bool aws_date_time_dst(const struct aws_date_time *dt, bool local_time) { - const struct tm *time = local_time ? &dt->local_time : &dt->gmt_time; - - return (bool)time->tm_isdst; -} - -time_t aws_date_time_diff(const struct aws_date_time *a, const struct aws_date_time *b) { - return a->timestamp - b->timestamp; -} + + struct aws_byte_cursor date_cursor = aws_byte_cursor_from_buf(date_str); + return aws_date_time_init_from_str_cursor(dt, &date_cursor, fmt); +} + +static inline int s_date_to_str(const struct tm *tm, const char *format_str, struct aws_byte_buf *output_buf) { + size_t remaining_space = output_buf->capacity - output_buf->len; + size_t bytes_written = strftime((char *)output_buf->buffer + output_buf->len, remaining_space, format_str, tm); + + if (bytes_written == 0) { + return aws_raise_error(AWS_ERROR_SHORT_BUFFER); + } + + output_buf->len += bytes_written; + + return AWS_OP_SUCCESS; +} + +int aws_date_time_to_local_time_str( + const struct aws_date_time *dt, + enum aws_date_format fmt, + struct aws_byte_buf *output_buf) { + AWS_ASSERT(fmt != AWS_DATE_FORMAT_AUTO_DETECT); + + switch (fmt) { + case AWS_DATE_FORMAT_RFC822: + return s_date_to_str(&dt->local_time, RFC822_DATE_FORMAT_STR_WITH_Z, output_buf); + + case AWS_DATE_FORMAT_ISO_8601: + return s_date_to_str(&dt->local_time, ISO_8601_LONG_DATE_FORMAT_STR, output_buf); + + case AWS_DATE_FORMAT_ISO_8601_BASIC: + return s_date_to_str(&dt->local_time, ISO_8601_LONG_BASIC_DATE_FORMAT_STR, output_buf); + + default: + return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); + } +} + +int aws_date_time_to_utc_time_str( + const struct aws_date_time *dt, + enum aws_date_format fmt, + struct aws_byte_buf *output_buf) { + AWS_ASSERT(fmt != AWS_DATE_FORMAT_AUTO_DETECT); + + switch (fmt) { + case AWS_DATE_FORMAT_RFC822: + return s_date_to_str(&dt->gmt_time, RFC822_DATE_FORMAT_STR_MINUS_Z, output_buf); + + case AWS_DATE_FORMAT_ISO_8601: + return s_date_to_str(&dt->gmt_time, ISO_8601_LONG_DATE_FORMAT_STR, output_buf); + + case AWS_DATE_FORMAT_ISO_8601_BASIC: + return s_date_to_str(&dt->gmt_time, ISO_8601_LONG_BASIC_DATE_FORMAT_STR, output_buf); + + default: + return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); + } +} + +int aws_date_time_to_local_time_short_str( + const struct aws_date_time *dt, + enum aws_date_format fmt, + struct aws_byte_buf *output_buf) { + AWS_ASSERT(fmt != AWS_DATE_FORMAT_AUTO_DETECT); + + switch (fmt) { + case AWS_DATE_FORMAT_RFC822: + return s_date_to_str(&dt->local_time, RFC822_SHORT_DATE_FORMAT_STR, output_buf); + + case AWS_DATE_FORMAT_ISO_8601: + return s_date_to_str(&dt->local_time, ISO_8601_SHORT_DATE_FORMAT_STR, output_buf); + + case AWS_DATE_FORMAT_ISO_8601_BASIC: + return s_date_to_str(&dt->local_time, ISO_8601_SHORT_BASIC_DATE_FORMAT_STR, output_buf); + + default: + return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); + } +} + +int aws_date_time_to_utc_time_short_str( + const struct aws_date_time *dt, + enum aws_date_format fmt, + struct aws_byte_buf *output_buf) { + AWS_ASSERT(fmt != AWS_DATE_FORMAT_AUTO_DETECT); + + switch (fmt) { + case AWS_DATE_FORMAT_RFC822: + return s_date_to_str(&dt->gmt_time, RFC822_SHORT_DATE_FORMAT_STR, output_buf); + + case AWS_DATE_FORMAT_ISO_8601: + return s_date_to_str(&dt->gmt_time, ISO_8601_SHORT_DATE_FORMAT_STR, output_buf); + + case AWS_DATE_FORMAT_ISO_8601_BASIC: + return s_date_to_str(&dt->gmt_time, ISO_8601_SHORT_BASIC_DATE_FORMAT_STR, output_buf); + + default: + return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); + } +} + +double aws_date_time_as_epoch_secs(const struct aws_date_time *dt) { + return (double)dt->timestamp; +} + +uint64_t aws_date_time_as_nanos(const struct aws_date_time *dt) { + return (uint64_t)dt->timestamp * AWS_TIMESTAMP_NANOS; +} + +uint64_t aws_date_time_as_millis(const struct aws_date_time *dt) { + return (uint64_t)dt->timestamp * AWS_TIMESTAMP_MILLIS; +} + +uint16_t aws_date_time_year(const struct aws_date_time *dt, bool local_time) { + const struct tm *time = local_time ? &dt->local_time : &dt->gmt_time; + + return (uint16_t)(time->tm_year + 1900); +} + +enum aws_date_month aws_date_time_month(const struct aws_date_time *dt, bool local_time) { + const struct tm *time = local_time ? &dt->local_time : &dt->gmt_time; + + return time->tm_mon; +} + +uint8_t aws_date_time_month_day(const struct aws_date_time *dt, bool local_time) { + const struct tm *time = local_time ? &dt->local_time : &dt->gmt_time; + + return (uint8_t)time->tm_mday; +} + +enum aws_date_day_of_week aws_date_time_day_of_week(const struct aws_date_time *dt, bool local_time) { + const struct tm *time = local_time ? &dt->local_time : &dt->gmt_time; + + return time->tm_wday; +} + +uint8_t aws_date_time_hour(const struct aws_date_time *dt, bool local_time) { + const struct tm *time = local_time ? &dt->local_time : &dt->gmt_time; + + return (uint8_t)time->tm_hour; +} + +uint8_t aws_date_time_minute(const struct aws_date_time *dt, bool local_time) { + const struct tm *time = local_time ? &dt->local_time : &dt->gmt_time; + + return (uint8_t)time->tm_min; +} + +uint8_t aws_date_time_second(const struct aws_date_time *dt, bool local_time) { + const struct tm *time = local_time ? &dt->local_time : &dt->gmt_time; + + return (uint8_t)time->tm_sec; +} + +bool aws_date_time_dst(const struct aws_date_time *dt, bool local_time) { + const struct tm *time = local_time ? &dt->local_time : &dt->gmt_time; + + return (bool)time->tm_isdst; +} + +time_t aws_date_time_diff(const struct aws_date_time *a, const struct aws_date_time *b) { + return a->timestamp - b->timestamp; +} diff --git a/contrib/restricted/aws/aws-c-common/source/device_random.c b/contrib/restricted/aws/aws-c-common/source/device_random.c index 3df8a218e7..c1731bdb46 100644 --- a/contrib/restricted/aws/aws-c-common/source/device_random.c +++ b/contrib/restricted/aws/aws-c-common/source/device_random.c @@ -1,37 +1,37 @@ /** * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0. - */ -#include <aws/common/device_random.h> - -#include <aws/common/byte_buf.h> - -#ifdef _MSC_VER -/* disables warning non const declared initializers for Microsoft compilers */ -# pragma warning(disable : 4204) -# pragma warning(disable : 4706) -#endif - -int aws_device_random_u64(uint64_t *output) { - struct aws_byte_buf buf = aws_byte_buf_from_empty_array((uint8_t *)output, sizeof(uint64_t)); - - return aws_device_random_buffer(&buf); -} - -int aws_device_random_u32(uint32_t *output) { - struct aws_byte_buf buf = aws_byte_buf_from_empty_array((uint8_t *)output, sizeof(uint32_t)); - - return aws_device_random_buffer(&buf); -} - -int aws_device_random_u16(uint16_t *output) { - struct aws_byte_buf buf = aws_byte_buf_from_empty_array((uint8_t *)output, sizeof(uint16_t)); - - return aws_device_random_buffer(&buf); -} - -int aws_device_random_u8(uint8_t *output) { - struct aws_byte_buf buf = aws_byte_buf_from_empty_array((uint8_t *)output, sizeof(uint8_t)); - - return aws_device_random_buffer(&buf); -} + */ +#include <aws/common/device_random.h> + +#include <aws/common/byte_buf.h> + +#ifdef _MSC_VER +/* disables warning non const declared initializers for Microsoft compilers */ +# pragma warning(disable : 4204) +# pragma warning(disable : 4706) +#endif + +int aws_device_random_u64(uint64_t *output) { + struct aws_byte_buf buf = aws_byte_buf_from_empty_array((uint8_t *)output, sizeof(uint64_t)); + + return aws_device_random_buffer(&buf); +} + +int aws_device_random_u32(uint32_t *output) { + struct aws_byte_buf buf = aws_byte_buf_from_empty_array((uint8_t *)output, sizeof(uint32_t)); + + return aws_device_random_buffer(&buf); +} + +int aws_device_random_u16(uint16_t *output) { + struct aws_byte_buf buf = aws_byte_buf_from_empty_array((uint8_t *)output, sizeof(uint16_t)); + + return aws_device_random_buffer(&buf); +} + +int aws_device_random_u8(uint8_t *output) { + struct aws_byte_buf buf = aws_byte_buf_from_empty_array((uint8_t *)output, sizeof(uint8_t)); + + return aws_device_random_buffer(&buf); +} diff --git a/contrib/restricted/aws/aws-c-common/source/encoding.c b/contrib/restricted/aws/aws-c-common/source/encoding.c index 26a41fa163..384780b46b 100644 --- a/contrib/restricted/aws/aws-c-common/source/encoding.c +++ b/contrib/restricted/aws/aws-c-common/source/encoding.c @@ -1,289 +1,289 @@ /** * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0. - */ - -#include <aws/common/encoding.h> - -#include <ctype.h> -#include <stdlib.h> - -#ifdef USE_SIMD_ENCODING -size_t aws_common_private_base64_decode_sse41(const unsigned char *in, unsigned char *out, size_t len); -void aws_common_private_base64_encode_sse41(const unsigned char *in, unsigned char *out, size_t len); -bool aws_common_private_has_avx2(void); -#else -/* - * When AVX2 compilation is unavailable, we use these stubs to fall back to the pure-C decoder. - * Since we force aws_common_private_has_avx2 to return false, the encode and decode functions should - * not be called - but we must provide them anyway to avoid link errors. - */ -static inline size_t aws_common_private_base64_decode_sse41(const unsigned char *in, unsigned char *out, size_t len) { - (void)in; - (void)out; - (void)len; - AWS_ASSERT(false); - return (size_t)-1; /* unreachable */ -} -static inline void aws_common_private_base64_encode_sse41(const unsigned char *in, unsigned char *out, size_t len) { - (void)in; - (void)out; - (void)len; - AWS_ASSERT(false); -} -static inline bool aws_common_private_has_avx2(void) { - return false; -} -#endif - -static const uint8_t *HEX_CHARS = (const uint8_t *)"0123456789abcdef"; - -static const uint8_t BASE64_SENTIANAL_VALUE = 0xff; -static const uint8_t BASE64_ENCODING_TABLE[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - -/* in this table, 0xDD is an invalid decoded value, if you have to do byte counting for any reason, there's 16 bytes - * per row. Reformatting is turned off to make sure this stays as 16 bytes per line. */ -/* clang-format off */ -static const uint8_t BASE64_DECODING_TABLE[256] = { - 64, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, - 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, - 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 62, 0xDD, 0xDD, 0xDD, 63, - 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0xDD, 0xDD, 0xDD, 255, 0xDD, 0xDD, - 0xDD, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, - 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, - 0xDD, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, - 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, - 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, - 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, - 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, - 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, - 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, - 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, - 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, - 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD}; -/* clang-format on */ - -int aws_hex_compute_encoded_len(size_t to_encode_len, size_t *encoded_length) { - AWS_ASSERT(encoded_length); - - size_t temp = (to_encode_len << 1) + 1; - - if (AWS_UNLIKELY(temp < to_encode_len)) { - return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED); - } - - *encoded_length = temp; - - return AWS_OP_SUCCESS; -} - -int aws_hex_encode(const struct aws_byte_cursor *AWS_RESTRICT to_encode, struct aws_byte_buf *AWS_RESTRICT output) { - AWS_PRECONDITION(aws_byte_cursor_is_valid(to_encode)); - AWS_PRECONDITION(aws_byte_buf_is_valid(output)); - - size_t encoded_len = 0; - - if (AWS_UNLIKELY(aws_hex_compute_encoded_len(to_encode->len, &encoded_len))) { - return AWS_OP_ERR; - } - - if (AWS_UNLIKELY(output->capacity < encoded_len)) { - return aws_raise_error(AWS_ERROR_SHORT_BUFFER); - } - - size_t written = 0; - for (size_t i = 0; i < to_encode->len; ++i) { - - output->buffer[written++] = HEX_CHARS[to_encode->ptr[i] >> 4 & 0x0f]; - output->buffer[written++] = HEX_CHARS[to_encode->ptr[i] & 0x0f]; - } - - output->buffer[written] = '\0'; - output->len = encoded_len; - - return AWS_OP_SUCCESS; -} - -int aws_hex_encode_append_dynamic( - const struct aws_byte_cursor *AWS_RESTRICT to_encode, - struct aws_byte_buf *AWS_RESTRICT output) { - AWS_ASSERT(to_encode->ptr); - AWS_ASSERT(aws_byte_buf_is_valid(output)); - - size_t encoded_len = 0; - if (AWS_UNLIKELY(aws_add_size_checked(to_encode->len, to_encode->len, &encoded_len))) { - return AWS_OP_ERR; - } - - if (AWS_UNLIKELY(aws_byte_buf_reserve_relative(output, encoded_len))) { - return AWS_OP_ERR; - } - - size_t written = output->len; - for (size_t i = 0; i < to_encode->len; ++i) { - - output->buffer[written++] = HEX_CHARS[to_encode->ptr[i] >> 4 & 0x0f]; - output->buffer[written++] = HEX_CHARS[to_encode->ptr[i] & 0x0f]; - } - - output->len += encoded_len; - - return AWS_OP_SUCCESS; -} - -static int s_hex_decode_char_to_int(char character, uint8_t *int_val) { - if (character >= 'a' && character <= 'f') { - *int_val = (uint8_t)(10 + (character - 'a')); - return 0; - } - - if (character >= 'A' && character <= 'F') { - *int_val = (uint8_t)(10 + (character - 'A')); - return 0; - } - - if (character >= '0' && character <= '9') { - *int_val = (uint8_t)(character - '0'); - return 0; - } - - return AWS_OP_ERR; -} - -int aws_hex_compute_decoded_len(size_t to_decode_len, size_t *decoded_len) { - AWS_ASSERT(decoded_len); - - size_t temp = (to_decode_len + 1); - - if (AWS_UNLIKELY(temp < to_decode_len)) { - return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED); - } - - *decoded_len = temp >> 1; - return AWS_OP_SUCCESS; -} - -int aws_hex_decode(const struct aws_byte_cursor *AWS_RESTRICT to_decode, struct aws_byte_buf *AWS_RESTRICT output) { - AWS_PRECONDITION(aws_byte_cursor_is_valid(to_decode)); - AWS_PRECONDITION(aws_byte_buf_is_valid(output)); - - size_t decoded_length = 0; - - if (AWS_UNLIKELY(aws_hex_compute_decoded_len(to_decode->len, &decoded_length))) { - return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED); - } - - if (AWS_UNLIKELY(output->capacity < decoded_length)) { - return aws_raise_error(AWS_ERROR_SHORT_BUFFER); - } - - size_t written = 0; - size_t i = 0; - uint8_t high_value = 0; - uint8_t low_value = 0; - - /* if the buffer isn't even, prepend a 0 to the buffer. */ - if (AWS_UNLIKELY(to_decode->len & 0x01)) { - i = 1; - if (s_hex_decode_char_to_int(to_decode->ptr[0], &low_value)) { - return aws_raise_error(AWS_ERROR_INVALID_HEX_STR); - } - - output->buffer[written++] = low_value; - } - - for (; i < to_decode->len; i += 2) { - if (AWS_UNLIKELY( - s_hex_decode_char_to_int(to_decode->ptr[i], &high_value) || - s_hex_decode_char_to_int(to_decode->ptr[i + 1], &low_value))) { - return aws_raise_error(AWS_ERROR_INVALID_HEX_STR); - } - - uint8_t value = (uint8_t)(high_value << 4); - value |= low_value; - output->buffer[written++] = value; - } - - output->len = decoded_length; - - return AWS_OP_SUCCESS; -} - -int aws_base64_compute_encoded_len(size_t to_encode_len, size_t *encoded_len) { - AWS_ASSERT(encoded_len); - - size_t tmp = to_encode_len + 2; - - if (AWS_UNLIKELY(tmp < to_encode_len)) { - return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED); - } - - tmp /= 3; - size_t overflow_check = tmp; - tmp = 4 * tmp + 1; /* plus one for the NULL terminator */ - - if (AWS_UNLIKELY(tmp < overflow_check)) { - return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED); - } - - *encoded_len = tmp; - - return AWS_OP_SUCCESS; -} - -int aws_base64_compute_decoded_len(const struct aws_byte_cursor *AWS_RESTRICT to_decode, size_t *decoded_len) { - AWS_ASSERT(to_decode); - AWS_ASSERT(decoded_len); - - const size_t len = to_decode->len; - const uint8_t *input = to_decode->ptr; - - if (len == 0) { - *decoded_len = 0; - return AWS_OP_SUCCESS; - } - - if (AWS_UNLIKELY(len & 0x03)) { - return aws_raise_error(AWS_ERROR_INVALID_BASE64_STR); - } - - size_t tmp = len * 3; - - if (AWS_UNLIKELY(tmp < len)) { - return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED); - } - - size_t padding = 0; - - if (len >= 2 && input[len - 1] == '=' && input[len - 2] == '=') { /*last two chars are = */ - padding = 2; - } else if (input[len - 1] == '=') { /*last char is = */ - padding = 1; - } - - *decoded_len = (tmp / 4 - padding); - return AWS_OP_SUCCESS; -} - -int aws_base64_encode(const struct aws_byte_cursor *AWS_RESTRICT to_encode, struct aws_byte_buf *AWS_RESTRICT output) { - AWS_ASSERT(to_encode->ptr); - AWS_ASSERT(output->buffer); - + */ + +#include <aws/common/encoding.h> + +#include <ctype.h> +#include <stdlib.h> + +#ifdef USE_SIMD_ENCODING +size_t aws_common_private_base64_decode_sse41(const unsigned char *in, unsigned char *out, size_t len); +void aws_common_private_base64_encode_sse41(const unsigned char *in, unsigned char *out, size_t len); +bool aws_common_private_has_avx2(void); +#else +/* + * When AVX2 compilation is unavailable, we use these stubs to fall back to the pure-C decoder. + * Since we force aws_common_private_has_avx2 to return false, the encode and decode functions should + * not be called - but we must provide them anyway to avoid link errors. + */ +static inline size_t aws_common_private_base64_decode_sse41(const unsigned char *in, unsigned char *out, size_t len) { + (void)in; + (void)out; + (void)len; + AWS_ASSERT(false); + return (size_t)-1; /* unreachable */ +} +static inline void aws_common_private_base64_encode_sse41(const unsigned char *in, unsigned char *out, size_t len) { + (void)in; + (void)out; + (void)len; + AWS_ASSERT(false); +} +static inline bool aws_common_private_has_avx2(void) { + return false; +} +#endif + +static const uint8_t *HEX_CHARS = (const uint8_t *)"0123456789abcdef"; + +static const uint8_t BASE64_SENTIANAL_VALUE = 0xff; +static const uint8_t BASE64_ENCODING_TABLE[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +/* in this table, 0xDD is an invalid decoded value, if you have to do byte counting for any reason, there's 16 bytes + * per row. Reformatting is turned off to make sure this stays as 16 bytes per line. */ +/* clang-format off */ +static const uint8_t BASE64_DECODING_TABLE[256] = { + 64, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, + 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, + 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 62, 0xDD, 0xDD, 0xDD, 63, + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0xDD, 0xDD, 0xDD, 255, 0xDD, 0xDD, + 0xDD, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, + 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, + 0xDD, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, + 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, + 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, + 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, + 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, + 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, + 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, + 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, + 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, + 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD}; +/* clang-format on */ + +int aws_hex_compute_encoded_len(size_t to_encode_len, size_t *encoded_length) { + AWS_ASSERT(encoded_length); + + size_t temp = (to_encode_len << 1) + 1; + + if (AWS_UNLIKELY(temp < to_encode_len)) { + return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED); + } + + *encoded_length = temp; + + return AWS_OP_SUCCESS; +} + +int aws_hex_encode(const struct aws_byte_cursor *AWS_RESTRICT to_encode, struct aws_byte_buf *AWS_RESTRICT output) { + AWS_PRECONDITION(aws_byte_cursor_is_valid(to_encode)); + AWS_PRECONDITION(aws_byte_buf_is_valid(output)); + + size_t encoded_len = 0; + + if (AWS_UNLIKELY(aws_hex_compute_encoded_len(to_encode->len, &encoded_len))) { + return AWS_OP_ERR; + } + + if (AWS_UNLIKELY(output->capacity < encoded_len)) { + return aws_raise_error(AWS_ERROR_SHORT_BUFFER); + } + + size_t written = 0; + for (size_t i = 0; i < to_encode->len; ++i) { + + output->buffer[written++] = HEX_CHARS[to_encode->ptr[i] >> 4 & 0x0f]; + output->buffer[written++] = HEX_CHARS[to_encode->ptr[i] & 0x0f]; + } + + output->buffer[written] = '\0'; + output->len = encoded_len; + + return AWS_OP_SUCCESS; +} + +int aws_hex_encode_append_dynamic( + const struct aws_byte_cursor *AWS_RESTRICT to_encode, + struct aws_byte_buf *AWS_RESTRICT output) { + AWS_ASSERT(to_encode->ptr); + AWS_ASSERT(aws_byte_buf_is_valid(output)); + + size_t encoded_len = 0; + if (AWS_UNLIKELY(aws_add_size_checked(to_encode->len, to_encode->len, &encoded_len))) { + return AWS_OP_ERR; + } + + if (AWS_UNLIKELY(aws_byte_buf_reserve_relative(output, encoded_len))) { + return AWS_OP_ERR; + } + + size_t written = output->len; + for (size_t i = 0; i < to_encode->len; ++i) { + + output->buffer[written++] = HEX_CHARS[to_encode->ptr[i] >> 4 & 0x0f]; + output->buffer[written++] = HEX_CHARS[to_encode->ptr[i] & 0x0f]; + } + + output->len += encoded_len; + + return AWS_OP_SUCCESS; +} + +static int s_hex_decode_char_to_int(char character, uint8_t *int_val) { + if (character >= 'a' && character <= 'f') { + *int_val = (uint8_t)(10 + (character - 'a')); + return 0; + } + + if (character >= 'A' && character <= 'F') { + *int_val = (uint8_t)(10 + (character - 'A')); + return 0; + } + + if (character >= '0' && character <= '9') { + *int_val = (uint8_t)(character - '0'); + return 0; + } + + return AWS_OP_ERR; +} + +int aws_hex_compute_decoded_len(size_t to_decode_len, size_t *decoded_len) { + AWS_ASSERT(decoded_len); + + size_t temp = (to_decode_len + 1); + + if (AWS_UNLIKELY(temp < to_decode_len)) { + return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED); + } + + *decoded_len = temp >> 1; + return AWS_OP_SUCCESS; +} + +int aws_hex_decode(const struct aws_byte_cursor *AWS_RESTRICT to_decode, struct aws_byte_buf *AWS_RESTRICT output) { + AWS_PRECONDITION(aws_byte_cursor_is_valid(to_decode)); + AWS_PRECONDITION(aws_byte_buf_is_valid(output)); + + size_t decoded_length = 0; + + if (AWS_UNLIKELY(aws_hex_compute_decoded_len(to_decode->len, &decoded_length))) { + return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED); + } + + if (AWS_UNLIKELY(output->capacity < decoded_length)) { + return aws_raise_error(AWS_ERROR_SHORT_BUFFER); + } + + size_t written = 0; + size_t i = 0; + uint8_t high_value = 0; + uint8_t low_value = 0; + + /* if the buffer isn't even, prepend a 0 to the buffer. */ + if (AWS_UNLIKELY(to_decode->len & 0x01)) { + i = 1; + if (s_hex_decode_char_to_int(to_decode->ptr[0], &low_value)) { + return aws_raise_error(AWS_ERROR_INVALID_HEX_STR); + } + + output->buffer[written++] = low_value; + } + + for (; i < to_decode->len; i += 2) { + if (AWS_UNLIKELY( + s_hex_decode_char_to_int(to_decode->ptr[i], &high_value) || + s_hex_decode_char_to_int(to_decode->ptr[i + 1], &low_value))) { + return aws_raise_error(AWS_ERROR_INVALID_HEX_STR); + } + + uint8_t value = (uint8_t)(high_value << 4); + value |= low_value; + output->buffer[written++] = value; + } + + output->len = decoded_length; + + return AWS_OP_SUCCESS; +} + +int aws_base64_compute_encoded_len(size_t to_encode_len, size_t *encoded_len) { + AWS_ASSERT(encoded_len); + + size_t tmp = to_encode_len + 2; + + if (AWS_UNLIKELY(tmp < to_encode_len)) { + return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED); + } + + tmp /= 3; + size_t overflow_check = tmp; + tmp = 4 * tmp + 1; /* plus one for the NULL terminator */ + + if (AWS_UNLIKELY(tmp < overflow_check)) { + return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED); + } + + *encoded_len = tmp; + + return AWS_OP_SUCCESS; +} + +int aws_base64_compute_decoded_len(const struct aws_byte_cursor *AWS_RESTRICT to_decode, size_t *decoded_len) { + AWS_ASSERT(to_decode); + AWS_ASSERT(decoded_len); + + const size_t len = to_decode->len; + const uint8_t *input = to_decode->ptr; + + if (len == 0) { + *decoded_len = 0; + return AWS_OP_SUCCESS; + } + + if (AWS_UNLIKELY(len & 0x03)) { + return aws_raise_error(AWS_ERROR_INVALID_BASE64_STR); + } + + size_t tmp = len * 3; + + if (AWS_UNLIKELY(tmp < len)) { + return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED); + } + + size_t padding = 0; + + if (len >= 2 && input[len - 1] == '=' && input[len - 2] == '=') { /*last two chars are = */ + padding = 2; + } else if (input[len - 1] == '=') { /*last char is = */ + padding = 1; + } + + *decoded_len = (tmp / 4 - padding); + return AWS_OP_SUCCESS; +} + +int aws_base64_encode(const struct aws_byte_cursor *AWS_RESTRICT to_encode, struct aws_byte_buf *AWS_RESTRICT output) { + AWS_ASSERT(to_encode->ptr); + AWS_ASSERT(output->buffer); + size_t terminated_length = 0; - size_t encoded_length = 0; + size_t encoded_length = 0; if (AWS_UNLIKELY(aws_base64_compute_encoded_len(to_encode->len, &terminated_length))) { - return AWS_OP_ERR; - } - + return AWS_OP_ERR; + } + size_t needed_capacity = 0; if (AWS_UNLIKELY(aws_add_size_checked(output->len, terminated_length, &needed_capacity))) { return AWS_OP_ERR; } if (AWS_UNLIKELY(output->capacity < needed_capacity)) { - return aws_raise_error(AWS_ERROR_SHORT_BUFFER); - } - + return aws_raise_error(AWS_ERROR_SHORT_BUFFER); + } + /* * For convenience to standard C functions expecting a null-terminated * string, the output is terminated. As the encoding itself can be used in @@ -291,123 +291,123 @@ int aws_base64_encode(const struct aws_byte_cursor *AWS_RESTRICT to_encode, stru */ encoded_length = (terminated_length - 1); - if (aws_common_private_has_avx2()) { + if (aws_common_private_has_avx2()) { aws_common_private_base64_encode_sse41(to_encode->ptr, output->buffer + output->len, to_encode->len); output->buffer[output->len + encoded_length] = 0; output->len += encoded_length; - return AWS_OP_SUCCESS; - } - - size_t buffer_length = to_encode->len; - size_t block_count = (buffer_length + 2) / 3; - size_t remainder_count = (buffer_length % 3); + return AWS_OP_SUCCESS; + } + + size_t buffer_length = to_encode->len; + size_t block_count = (buffer_length + 2) / 3; + size_t remainder_count = (buffer_length % 3); size_t str_index = output->len; - - for (size_t i = 0; i < to_encode->len; i += 3) { - uint32_t block = to_encode->ptr[i]; - - block <<= 8; - if (AWS_LIKELY(i + 1 < buffer_length)) { - block = block | to_encode->ptr[i + 1]; - } - - block <<= 8; - if (AWS_LIKELY(i + 2 < to_encode->len)) { - block = block | to_encode->ptr[i + 2]; - } - - output->buffer[str_index++] = BASE64_ENCODING_TABLE[(block >> 18) & 0x3F]; - output->buffer[str_index++] = BASE64_ENCODING_TABLE[(block >> 12) & 0x3F]; - output->buffer[str_index++] = BASE64_ENCODING_TABLE[(block >> 6) & 0x3F]; - output->buffer[str_index++] = BASE64_ENCODING_TABLE[block & 0x3F]; - } - - if (remainder_count > 0) { + + for (size_t i = 0; i < to_encode->len; i += 3) { + uint32_t block = to_encode->ptr[i]; + + block <<= 8; + if (AWS_LIKELY(i + 1 < buffer_length)) { + block = block | to_encode->ptr[i + 1]; + } + + block <<= 8; + if (AWS_LIKELY(i + 2 < to_encode->len)) { + block = block | to_encode->ptr[i + 2]; + } + + output->buffer[str_index++] = BASE64_ENCODING_TABLE[(block >> 18) & 0x3F]; + output->buffer[str_index++] = BASE64_ENCODING_TABLE[(block >> 12) & 0x3F]; + output->buffer[str_index++] = BASE64_ENCODING_TABLE[(block >> 6) & 0x3F]; + output->buffer[str_index++] = BASE64_ENCODING_TABLE[block & 0x3F]; + } + + if (remainder_count > 0) { output->buffer[output->len + block_count * 4 - 1] = '='; - if (remainder_count == 1) { + if (remainder_count == 1) { output->buffer[output->len + block_count * 4 - 2] = '='; - } - } - - /* it's a string add the null terminator. */ + } + } + + /* it's a string add the null terminator. */ output->buffer[output->len + encoded_length] = 0; - + output->len += encoded_length; - return AWS_OP_SUCCESS; -} - -static inline int s_base64_get_decoded_value(unsigned char to_decode, uint8_t *value, int8_t allow_sentinal) { - - uint8_t decode_value = BASE64_DECODING_TABLE[(size_t)to_decode]; - if (decode_value != 0xDD && (decode_value != BASE64_SENTIANAL_VALUE || allow_sentinal)) { - *value = decode_value; - return AWS_OP_SUCCESS; - } - - return AWS_OP_ERR; -} - -int aws_base64_decode(const struct aws_byte_cursor *AWS_RESTRICT to_decode, struct aws_byte_buf *AWS_RESTRICT output) { - size_t decoded_length = 0; - - if (AWS_UNLIKELY(aws_base64_compute_decoded_len(to_decode, &decoded_length))) { - return AWS_OP_ERR; - } - - if (output->capacity < decoded_length) { - return aws_raise_error(AWS_ERROR_SHORT_BUFFER); - } - - if (aws_common_private_has_avx2()) { - size_t result = aws_common_private_base64_decode_sse41(to_decode->ptr, output->buffer, to_decode->len); - if (result == -1) { - return aws_raise_error(AWS_ERROR_INVALID_BASE64_STR); - } - - output->len = result; - return AWS_OP_SUCCESS; - } - - int64_t block_count = (int64_t)to_decode->len / 4; - size_t string_index = 0; - uint8_t value1 = 0, value2 = 0, value3 = 0, value4 = 0; - int64_t buffer_index = 0; - - for (int64_t i = 0; i < block_count - 1; ++i) { - if (AWS_UNLIKELY( - s_base64_get_decoded_value(to_decode->ptr[string_index++], &value1, 0) || - s_base64_get_decoded_value(to_decode->ptr[string_index++], &value2, 0) || - s_base64_get_decoded_value(to_decode->ptr[string_index++], &value3, 0) || - s_base64_get_decoded_value(to_decode->ptr[string_index++], &value4, 0))) { - return aws_raise_error(AWS_ERROR_INVALID_BASE64_STR); - } - - buffer_index = i * 3; - output->buffer[buffer_index++] = (uint8_t)((value1 << 2) | ((value2 >> 4) & 0x03)); - output->buffer[buffer_index++] = (uint8_t)(((value2 << 4) & 0xF0) | ((value3 >> 2) & 0x0F)); - output->buffer[buffer_index] = (uint8_t)((value3 & 0x03) << 6 | value4); - } - - buffer_index = (block_count - 1) * 3; - - if (buffer_index >= 0) { - if (s_base64_get_decoded_value(to_decode->ptr[string_index++], &value1, 0) || - s_base64_get_decoded_value(to_decode->ptr[string_index++], &value2, 0) || - s_base64_get_decoded_value(to_decode->ptr[string_index++], &value3, 1) || - s_base64_get_decoded_value(to_decode->ptr[string_index], &value4, 1)) { - return aws_raise_error(AWS_ERROR_INVALID_BASE64_STR); - } - - output->buffer[buffer_index++] = (uint8_t)((value1 << 2) | ((value2 >> 4) & 0x03)); - - if (value3 != BASE64_SENTIANAL_VALUE) { - output->buffer[buffer_index++] = (uint8_t)(((value2 << 4) & 0xF0) | ((value3 >> 2) & 0x0F)); - if (value4 != BASE64_SENTIANAL_VALUE) { - output->buffer[buffer_index] = (uint8_t)((value3 & 0x03) << 6 | value4); - } - } - } - output->len = decoded_length; - return AWS_OP_SUCCESS; -} + return AWS_OP_SUCCESS; +} + +static inline int s_base64_get_decoded_value(unsigned char to_decode, uint8_t *value, int8_t allow_sentinal) { + + uint8_t decode_value = BASE64_DECODING_TABLE[(size_t)to_decode]; + if (decode_value != 0xDD && (decode_value != BASE64_SENTIANAL_VALUE || allow_sentinal)) { + *value = decode_value; + return AWS_OP_SUCCESS; + } + + return AWS_OP_ERR; +} + +int aws_base64_decode(const struct aws_byte_cursor *AWS_RESTRICT to_decode, struct aws_byte_buf *AWS_RESTRICT output) { + size_t decoded_length = 0; + + if (AWS_UNLIKELY(aws_base64_compute_decoded_len(to_decode, &decoded_length))) { + return AWS_OP_ERR; + } + + if (output->capacity < decoded_length) { + return aws_raise_error(AWS_ERROR_SHORT_BUFFER); + } + + if (aws_common_private_has_avx2()) { + size_t result = aws_common_private_base64_decode_sse41(to_decode->ptr, output->buffer, to_decode->len); + if (result == -1) { + return aws_raise_error(AWS_ERROR_INVALID_BASE64_STR); + } + + output->len = result; + return AWS_OP_SUCCESS; + } + + int64_t block_count = (int64_t)to_decode->len / 4; + size_t string_index = 0; + uint8_t value1 = 0, value2 = 0, value3 = 0, value4 = 0; + int64_t buffer_index = 0; + + for (int64_t i = 0; i < block_count - 1; ++i) { + if (AWS_UNLIKELY( + s_base64_get_decoded_value(to_decode->ptr[string_index++], &value1, 0) || + s_base64_get_decoded_value(to_decode->ptr[string_index++], &value2, 0) || + s_base64_get_decoded_value(to_decode->ptr[string_index++], &value3, 0) || + s_base64_get_decoded_value(to_decode->ptr[string_index++], &value4, 0))) { + return aws_raise_error(AWS_ERROR_INVALID_BASE64_STR); + } + + buffer_index = i * 3; + output->buffer[buffer_index++] = (uint8_t)((value1 << 2) | ((value2 >> 4) & 0x03)); + output->buffer[buffer_index++] = (uint8_t)(((value2 << 4) & 0xF0) | ((value3 >> 2) & 0x0F)); + output->buffer[buffer_index] = (uint8_t)((value3 & 0x03) << 6 | value4); + } + + buffer_index = (block_count - 1) * 3; + + if (buffer_index >= 0) { + if (s_base64_get_decoded_value(to_decode->ptr[string_index++], &value1, 0) || + s_base64_get_decoded_value(to_decode->ptr[string_index++], &value2, 0) || + s_base64_get_decoded_value(to_decode->ptr[string_index++], &value3, 1) || + s_base64_get_decoded_value(to_decode->ptr[string_index], &value4, 1)) { + return aws_raise_error(AWS_ERROR_INVALID_BASE64_STR); + } + + output->buffer[buffer_index++] = (uint8_t)((value1 << 2) | ((value2 >> 4) & 0x03)); + + if (value3 != BASE64_SENTIANAL_VALUE) { + output->buffer[buffer_index++] = (uint8_t)(((value2 << 4) & 0xF0) | ((value3 >> 2) & 0x0F)); + if (value4 != BASE64_SENTIANAL_VALUE) { + output->buffer[buffer_index] = (uint8_t)((value3 & 0x03) << 6 | value4); + } + } + } + output->len = decoded_length; + return AWS_OP_SUCCESS; +} diff --git a/contrib/restricted/aws/aws-c-common/source/error.c b/contrib/restricted/aws/aws-c-common/source/error.c index 60e6c9e799..89fc55629f 100644 --- a/contrib/restricted/aws/aws-c-common/source/error.c +++ b/contrib/restricted/aws/aws-c-common/source/error.c @@ -1,149 +1,149 @@ /** * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0. - */ - -#include <aws/common/error.h> - -#include <aws/common/common.h> - + */ + +#include <aws/common/error.h> + +#include <aws/common/common.h> + #include <errno.h> -#include <stdio.h> -#include <stdlib.h> - -static AWS_THREAD_LOCAL int tl_last_error = 0; - -static aws_error_handler_fn *s_global_handler = NULL; -static void *s_global_error_context = NULL; - -static AWS_THREAD_LOCAL aws_error_handler_fn *tl_thread_handler = NULL; -AWS_THREAD_LOCAL void *tl_thread_handler_context = NULL; - -/* Since slot size is 00000100 00000000, to divide, we need to shift right by 10 - * bits to find the slot, and to find the modulus, we use a binary and with +#include <stdio.h> +#include <stdlib.h> + +static AWS_THREAD_LOCAL int tl_last_error = 0; + +static aws_error_handler_fn *s_global_handler = NULL; +static void *s_global_error_context = NULL; + +static AWS_THREAD_LOCAL aws_error_handler_fn *tl_thread_handler = NULL; +AWS_THREAD_LOCAL void *tl_thread_handler_context = NULL; + +/* Since slot size is 00000100 00000000, to divide, we need to shift right by 10 + * bits to find the slot, and to find the modulus, we use a binary and with * 00000011 11111111 to find the index in that slot. */ #define SLOT_MASK (AWS_ERROR_ENUM_STRIDE - 1) - + static const int MAX_ERROR_CODE = AWS_ERROR_ENUM_STRIDE * AWS_PACKAGE_SLOTS; - + static const struct aws_error_info_list *volatile ERROR_SLOTS[AWS_PACKAGE_SLOTS] = {0}; - -int aws_last_error(void) { - return tl_last_error; -} - -static const struct aws_error_info *get_error_by_code(int err) { - if (err >= MAX_ERROR_CODE || err < 0) { - return NULL; - } - + +int aws_last_error(void) { + return tl_last_error; +} + +static const struct aws_error_info *get_error_by_code(int err) { + if (err >= MAX_ERROR_CODE || err < 0) { + return NULL; + } + uint32_t slot_index = (uint32_t)err >> AWS_ERROR_ENUM_STRIDE_BITS; uint32_t error_index = (uint32_t)err & SLOT_MASK; - - const struct aws_error_info_list *error_slot = ERROR_SLOTS[slot_index]; - - if (!error_slot || error_index >= error_slot->count) { - return NULL; - } - - return &error_slot->error_list[error_index]; -} - -const char *aws_error_str(int err) { - const struct aws_error_info *error_info = get_error_by_code(err); - - if (error_info) { - return error_info->error_str; - } - - return "Unknown Error Code"; -} - -const char *aws_error_name(int err) { - const struct aws_error_info *error_info = get_error_by_code(err); - - if (error_info) { - return error_info->literal_name; - } - - return "Unknown Error Code"; -} - -const char *aws_error_lib_name(int err) { - const struct aws_error_info *error_info = get_error_by_code(err); - - if (error_info) { - return error_info->lib_name; - } - - return "Unknown Error Code"; -} - -const char *aws_error_debug_str(int err) { - const struct aws_error_info *error_info = get_error_by_code(err); - - if (error_info) { - return error_info->formatted_name; - } - - return "Unknown Error Code"; -} - -void aws_raise_error_private(int err) { - tl_last_error = err; - - if (tl_thread_handler) { - tl_thread_handler(tl_last_error, tl_thread_handler_context); - } else if (s_global_handler) { - s_global_handler(tl_last_error, s_global_error_context); - } -} - -void aws_reset_error(void) { - tl_last_error = 0; -} - -void aws_restore_error(int err) { - tl_last_error = err; -} - -aws_error_handler_fn *aws_set_global_error_handler_fn(aws_error_handler_fn *handler, void *ctx) { - aws_error_handler_fn *old_handler = s_global_handler; - s_global_handler = handler; - s_global_error_context = ctx; - - return old_handler; -} - -aws_error_handler_fn *aws_set_thread_local_error_handler_fn(aws_error_handler_fn *handler, void *ctx) { - aws_error_handler_fn *old_handler = tl_thread_handler; - tl_thread_handler = handler; - tl_thread_handler_context = ctx; - - return old_handler; -} - -void aws_register_error_info(const struct aws_error_info_list *error_info) { - /* - * We're not so worried about these asserts being removed in an NDEBUG build - * - we'll either segfault immediately (for the first two) or for the count - * assert, the registration will be ineffective. - */ + + const struct aws_error_info_list *error_slot = ERROR_SLOTS[slot_index]; + + if (!error_slot || error_index >= error_slot->count) { + return NULL; + } + + return &error_slot->error_list[error_index]; +} + +const char *aws_error_str(int err) { + const struct aws_error_info *error_info = get_error_by_code(err); + + if (error_info) { + return error_info->error_str; + } + + return "Unknown Error Code"; +} + +const char *aws_error_name(int err) { + const struct aws_error_info *error_info = get_error_by_code(err); + + if (error_info) { + return error_info->literal_name; + } + + return "Unknown Error Code"; +} + +const char *aws_error_lib_name(int err) { + const struct aws_error_info *error_info = get_error_by_code(err); + + if (error_info) { + return error_info->lib_name; + } + + return "Unknown Error Code"; +} + +const char *aws_error_debug_str(int err) { + const struct aws_error_info *error_info = get_error_by_code(err); + + if (error_info) { + return error_info->formatted_name; + } + + return "Unknown Error Code"; +} + +void aws_raise_error_private(int err) { + tl_last_error = err; + + if (tl_thread_handler) { + tl_thread_handler(tl_last_error, tl_thread_handler_context); + } else if (s_global_handler) { + s_global_handler(tl_last_error, s_global_error_context); + } +} + +void aws_reset_error(void) { + tl_last_error = 0; +} + +void aws_restore_error(int err) { + tl_last_error = err; +} + +aws_error_handler_fn *aws_set_global_error_handler_fn(aws_error_handler_fn *handler, void *ctx) { + aws_error_handler_fn *old_handler = s_global_handler; + s_global_handler = handler; + s_global_error_context = ctx; + + return old_handler; +} + +aws_error_handler_fn *aws_set_thread_local_error_handler_fn(aws_error_handler_fn *handler, void *ctx) { + aws_error_handler_fn *old_handler = tl_thread_handler; + tl_thread_handler = handler; + tl_thread_handler_context = ctx; + + return old_handler; +} + +void aws_register_error_info(const struct aws_error_info_list *error_info) { + /* + * We're not so worried about these asserts being removed in an NDEBUG build + * - we'll either segfault immediately (for the first two) or for the count + * assert, the registration will be ineffective. + */ AWS_FATAL_ASSERT(error_info); AWS_FATAL_ASSERT(error_info->error_list); AWS_FATAL_ASSERT(error_info->count); - + const int min_range = error_info->error_list[0].error_code; const int slot_index = min_range >> AWS_ERROR_ENUM_STRIDE_BITS; - + if (slot_index >= AWS_PACKAGE_SLOTS || slot_index < 0) { /* This is an NDEBUG build apparently. Kill the process rather than * corrupting heap. */ fprintf(stderr, "Bad error slot index %d\n", slot_index); AWS_FATAL_ASSERT(false); } - + #if DEBUG_BUILD /* Assert that error info entries are in the right order. */ for (int i = 1; i < error_info->count; ++i) { @@ -159,7 +159,7 @@ void aws_register_error_info(const struct aws_error_info_list *error_info) { } } #endif /* DEBUG_BUILD */ - + ERROR_SLOTS[slot_index] = error_info; } @@ -172,14 +172,14 @@ void aws_unregister_error_info(const struct aws_error_info_list *error_info) { const int slot_index = min_range >> AWS_ERROR_ENUM_STRIDE_BITS; if (slot_index >= AWS_PACKAGE_SLOTS || slot_index < 0) { - /* This is an NDEBUG build apparently. Kill the process rather than - * corrupting heap. */ + /* This is an NDEBUG build apparently. Kill the process rather than + * corrupting heap. */ fprintf(stderr, "Bad error slot index %d\n", slot_index); AWS_FATAL_ASSERT(0); - } - + } + ERROR_SLOTS[slot_index] = NULL; -} +} int aws_translate_and_raise_io_error(int error_no) { switch (error_no) { diff --git a/contrib/restricted/aws/aws-c-common/source/hash_table.c b/contrib/restricted/aws/aws-c-common/source/hash_table.c index a8125a2df1..e59a30db18 100644 --- a/contrib/restricted/aws/aws-c-common/source/hash_table.c +++ b/contrib/restricted/aws/aws-c-common/source/hash_table.c @@ -1,57 +1,57 @@ /** * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0. - */ - -/* For more information on how the RH hash works and in particular how we do - * deletions, see: - * http://codecapsule.com/2013/11/17/robin-hood-hashing-backward-shift-deletion/ - */ - -#include <aws/common/hash_table.h> -#include <aws/common/math.h> -#include <aws/common/private/hash_table_impl.h> -#include <aws/common/string.h> - -#include <limits.h> -#include <stdio.h> -#include <stdlib.h> - -/* Include lookup3.c so we can (potentially) inline it and make use of the mix() - * macro. */ + */ + +/* For more information on how the RH hash works and in particular how we do + * deletions, see: + * http://codecapsule.com/2013/11/17/robin-hood-hashing-backward-shift-deletion/ + */ + +#include <aws/common/hash_table.h> +#include <aws/common/math.h> +#include <aws/common/private/hash_table_impl.h> +#include <aws/common/string.h> + +#include <limits.h> +#include <stdio.h> +#include <stdlib.h> + +/* Include lookup3.c so we can (potentially) inline it and make use of the mix() + * macro. */ #include <aws/common/private/lookup3.inl> - -static void s_suppress_unused_lookup3_func_warnings(void) { - /* We avoid making changes to lookup3 if we can avoid it, but since it has functions - * we're not using, reference them somewhere to suppress the unused function warning. - */ - (void)hashword; - (void)hashword2; - (void)hashlittle; - (void)hashbig; -} - + +static void s_suppress_unused_lookup3_func_warnings(void) { + /* We avoid making changes to lookup3 if we can avoid it, but since it has functions + * we're not using, reference them somewhere to suppress the unused function warning. + */ + (void)hashword; + (void)hashword2; + (void)hashlittle; + (void)hashbig; +} + /** * Calculate the hash for the given key. * Ensures a reasonable semantics for null keys. * Ensures that no object ever hashes to 0, which is the sentinal value for an empty hash element. */ -static uint64_t s_hash_for(struct hash_table_state *state, const void *key) { +static uint64_t s_hash_for(struct hash_table_state *state, const void *key) { AWS_PRECONDITION(hash_table_state_is_valid(state)); - s_suppress_unused_lookup3_func_warnings(); - + s_suppress_unused_lookup3_func_warnings(); + if (key == NULL) { /* The best answer */ return 42; } - uint64_t hash_code = state->hash_fn(key); - if (!hash_code) { - hash_code = 1; - } + uint64_t hash_code = state->hash_fn(key); + if (!hash_code) { + hash_code = 1; + } AWS_RETURN_WITH_POSTCONDITION(hash_code, hash_code != 0); -} - +} + /** * Check equality of two objects, with a reasonable semantics for null. */ @@ -77,270 +77,270 @@ static bool s_hash_keys_eq(struct hash_table_state *state, const void *a, const AWS_RETURN_WITH_POSTCONDITION(rval, hash_table_state_is_valid(state)); } -static size_t s_index_for(struct hash_table_state *map, struct hash_table_entry *entry) { - AWS_PRECONDITION(hash_table_state_is_valid(map)); - size_t index = entry - map->slots; +static size_t s_index_for(struct hash_table_state *map, struct hash_table_entry *entry) { + AWS_PRECONDITION(hash_table_state_is_valid(map)); + size_t index = entry - map->slots; AWS_RETURN_WITH_POSTCONDITION(index, index < map->size && hash_table_state_is_valid(map)); -} - -#if 0 -/* Useful debugging code for anyone working on this in the future */ -static uint64_t s_distance(struct hash_table_state *state, int index) { - return (index - state->slots[index].hash_code) & state->mask; -} - -void hash_dump(struct aws_hash_table *tbl) { - struct hash_table_state *state = tbl->p_impl; - - printf("Dumping hash table contents:\n"); - - for (int i = 0; i < state->size; i++) { - printf("%7d: ", i); - struct hash_table_entry *e = &state->slots[i]; - if (!e->hash_code) { - printf("EMPTY\n"); - } else { - printf("k: %p v: %p hash_code: %lld displacement: %lld\n", - e->element.key, e->element.value, e->hash_code, - (i - e->hash_code) & state->mask); - } - } -} -#endif - -#if 0 -/* Not currently exposed as an API. Should we have something like this? Useful for benchmarks */ -AWS_COMMON_API -void aws_hash_table_print_stats(struct aws_hash_table *table) { - struct hash_table_state *state = table->p_impl; - uint64_t total_disp = 0; - uint64_t max_disp = 0; - - printf("\n=== Hash table statistics ===\n"); - printf("Table size: %zu/%zu (max load %zu, remaining %zu)\n", state->entry_count, state->size, state->max_load, state->max_load - state->entry_count); - printf("Load factor: %02.2lf%% (max %02.2lf%%)\n", - 100.0 * ((double)state->entry_count / (double)state->size), - state->max_load_factor); - - for (size_t i = 0; i < state->size; i++) { - if (state->slots[i].hash_code) { - int displacement = distance(state, i); - total_disp += displacement; - if (displacement > max_disp) { - max_disp = displacement; - } - } - } - - size_t *disp_counts = calloc(sizeof(*disp_counts), max_disp + 1); - - for (size_t i = 0; i < state->size; i++) { - if (state->slots[i].hash_code) { - disp_counts[distance(state, i)]++; - } - } - - uint64_t median = 0; - uint64_t passed = 0; - for (uint64_t i = 0; i <= max_disp && passed < total_disp / 2; i++) { - median = i; - passed += disp_counts[i]; - } - - printf("Displacement statistics: Avg %02.2lf max %llu median %llu\n", (double)total_disp / (double)state->entry_count, max_disp, median); - for (uint64_t i = 0; i <= max_disp; i++) { - printf("Displacement %2lld: %zu entries\n", i, disp_counts[i]); - } - free(disp_counts); - printf("\n"); -} -#endif - -size_t aws_hash_table_get_entry_count(const struct aws_hash_table *map) { - struct hash_table_state *state = map->p_impl; - return state->entry_count; -} - -/* Given a header template, allocates space for a hash table of the appropriate - * size, and copies the state header into this allocated memory, which is - * returned. - */ -static struct hash_table_state *s_alloc_state(const struct hash_table_state *template) { - size_t required_bytes; - if (hash_table_state_required_bytes(template->size, &required_bytes)) { - return NULL; - } - - /* An empty slot has hashcode 0. So this marks all slots as empty */ - struct hash_table_state *state = aws_mem_calloc(template->alloc, 1, required_bytes); - - if (state == NULL) { - return state; - } - - *state = *template; - return state; -} - -/* Computes the correct size and max_load based on a requested size. */ -static int s_update_template_size(struct hash_table_state *template, size_t expected_elements) { - size_t min_size = expected_elements; - - if (min_size < 2) { - min_size = 2; - } - - /* size is always a power of 2 */ - size_t size; - if (aws_round_up_to_power_of_two(min_size, &size)) { - return AWS_OP_ERR; - } - - /* Update the template once we've calculated everything successfully */ - template->size = size; - template->max_load = (size_t)(template->max_load_factor * (double)template->size); - /* Ensure that there is always at least one empty slot in the hash table */ - if (template->max_load >= size) { - template->max_load = size - 1; - } - - /* Since size is a power of 2: (index & (size - 1)) == (index % size) */ - template->mask = size - 1; - - return AWS_OP_SUCCESS; -} - -int aws_hash_table_init( - struct aws_hash_table *map, - struct aws_allocator *alloc, - size_t size, - aws_hash_fn *hash_fn, - aws_hash_callback_eq_fn *equals_fn, - aws_hash_callback_destroy_fn *destroy_key_fn, - aws_hash_callback_destroy_fn *destroy_value_fn) { +} + +#if 0 +/* Useful debugging code for anyone working on this in the future */ +static uint64_t s_distance(struct hash_table_state *state, int index) { + return (index - state->slots[index].hash_code) & state->mask; +} + +void hash_dump(struct aws_hash_table *tbl) { + struct hash_table_state *state = tbl->p_impl; + + printf("Dumping hash table contents:\n"); + + for (int i = 0; i < state->size; i++) { + printf("%7d: ", i); + struct hash_table_entry *e = &state->slots[i]; + if (!e->hash_code) { + printf("EMPTY\n"); + } else { + printf("k: %p v: %p hash_code: %lld displacement: %lld\n", + e->element.key, e->element.value, e->hash_code, + (i - e->hash_code) & state->mask); + } + } +} +#endif + +#if 0 +/* Not currently exposed as an API. Should we have something like this? Useful for benchmarks */ +AWS_COMMON_API +void aws_hash_table_print_stats(struct aws_hash_table *table) { + struct hash_table_state *state = table->p_impl; + uint64_t total_disp = 0; + uint64_t max_disp = 0; + + printf("\n=== Hash table statistics ===\n"); + printf("Table size: %zu/%zu (max load %zu, remaining %zu)\n", state->entry_count, state->size, state->max_load, state->max_load - state->entry_count); + printf("Load factor: %02.2lf%% (max %02.2lf%%)\n", + 100.0 * ((double)state->entry_count / (double)state->size), + state->max_load_factor); + + for (size_t i = 0; i < state->size; i++) { + if (state->slots[i].hash_code) { + int displacement = distance(state, i); + total_disp += displacement; + if (displacement > max_disp) { + max_disp = displacement; + } + } + } + + size_t *disp_counts = calloc(sizeof(*disp_counts), max_disp + 1); + + for (size_t i = 0; i < state->size; i++) { + if (state->slots[i].hash_code) { + disp_counts[distance(state, i)]++; + } + } + + uint64_t median = 0; + uint64_t passed = 0; + for (uint64_t i = 0; i <= max_disp && passed < total_disp / 2; i++) { + median = i; + passed += disp_counts[i]; + } + + printf("Displacement statistics: Avg %02.2lf max %llu median %llu\n", (double)total_disp / (double)state->entry_count, max_disp, median); + for (uint64_t i = 0; i <= max_disp; i++) { + printf("Displacement %2lld: %zu entries\n", i, disp_counts[i]); + } + free(disp_counts); + printf("\n"); +} +#endif + +size_t aws_hash_table_get_entry_count(const struct aws_hash_table *map) { + struct hash_table_state *state = map->p_impl; + return state->entry_count; +} + +/* Given a header template, allocates space for a hash table of the appropriate + * size, and copies the state header into this allocated memory, which is + * returned. + */ +static struct hash_table_state *s_alloc_state(const struct hash_table_state *template) { + size_t required_bytes; + if (hash_table_state_required_bytes(template->size, &required_bytes)) { + return NULL; + } + + /* An empty slot has hashcode 0. So this marks all slots as empty */ + struct hash_table_state *state = aws_mem_calloc(template->alloc, 1, required_bytes); + + if (state == NULL) { + return state; + } + + *state = *template; + return state; +} + +/* Computes the correct size and max_load based on a requested size. */ +static int s_update_template_size(struct hash_table_state *template, size_t expected_elements) { + size_t min_size = expected_elements; + + if (min_size < 2) { + min_size = 2; + } + + /* size is always a power of 2 */ + size_t size; + if (aws_round_up_to_power_of_two(min_size, &size)) { + return AWS_OP_ERR; + } + + /* Update the template once we've calculated everything successfully */ + template->size = size; + template->max_load = (size_t)(template->max_load_factor * (double)template->size); + /* Ensure that there is always at least one empty slot in the hash table */ + if (template->max_load >= size) { + template->max_load = size - 1; + } + + /* Since size is a power of 2: (index & (size - 1)) == (index % size) */ + template->mask = size - 1; + + return AWS_OP_SUCCESS; +} + +int aws_hash_table_init( + struct aws_hash_table *map, + struct aws_allocator *alloc, + size_t size, + aws_hash_fn *hash_fn, + aws_hash_callback_eq_fn *equals_fn, + aws_hash_callback_destroy_fn *destroy_key_fn, + aws_hash_callback_destroy_fn *destroy_value_fn) { AWS_PRECONDITION(map != NULL); AWS_PRECONDITION(alloc != NULL); AWS_PRECONDITION(hash_fn != NULL); AWS_PRECONDITION(equals_fn != NULL); - - struct hash_table_state template; - template.hash_fn = hash_fn; - template.equals_fn = equals_fn; - template.destroy_key_fn = destroy_key_fn; - template.destroy_value_fn = destroy_value_fn; - template.alloc = alloc; - - template.entry_count = 0; - template.max_load_factor = 0.95; /* TODO - make configurable? */ - - if (s_update_template_size(&template, size)) { - return AWS_OP_ERR; - } - map->p_impl = s_alloc_state(&template); - - if (!map->p_impl) { - return AWS_OP_ERR; - } - + + struct hash_table_state template; + template.hash_fn = hash_fn; + template.equals_fn = equals_fn; + template.destroy_key_fn = destroy_key_fn; + template.destroy_value_fn = destroy_value_fn; + template.alloc = alloc; + + template.entry_count = 0; + template.max_load_factor = 0.95; /* TODO - make configurable? */ + + if (s_update_template_size(&template, size)) { + return AWS_OP_ERR; + } + map->p_impl = s_alloc_state(&template); + + if (!map->p_impl) { + return AWS_OP_ERR; + } + AWS_SUCCEED_WITH_POSTCONDITION(aws_hash_table_is_valid(map)); -} - -void aws_hash_table_clean_up(struct aws_hash_table *map) { +} + +void aws_hash_table_clean_up(struct aws_hash_table *map) { AWS_PRECONDITION(map != NULL); AWS_PRECONDITION( map->p_impl == NULL || aws_hash_table_is_valid(map), "Input aws_hash_table [map] must be valid or hash_table_state pointer [map->p_impl] must be NULL, in case " "aws_hash_table_clean_up was called twice."); - struct hash_table_state *state = map->p_impl; - - /* Ensure that we're idempotent */ - if (!state) { - return; - } - - aws_hash_table_clear(map); + struct hash_table_state *state = map->p_impl; + + /* Ensure that we're idempotent */ + if (!state) { + return; + } + + aws_hash_table_clear(map); aws_mem_release(map->p_impl->alloc, map->p_impl); - - map->p_impl = NULL; + + map->p_impl = NULL; AWS_POSTCONDITION(map->p_impl == NULL); -} - -void aws_hash_table_swap(struct aws_hash_table *AWS_RESTRICT a, struct aws_hash_table *AWS_RESTRICT b) { - AWS_PRECONDITION(a != b); - struct aws_hash_table tmp = *a; - *a = *b; - *b = tmp; -} - -void aws_hash_table_move(struct aws_hash_table *AWS_RESTRICT to, struct aws_hash_table *AWS_RESTRICT from) { +} + +void aws_hash_table_swap(struct aws_hash_table *AWS_RESTRICT a, struct aws_hash_table *AWS_RESTRICT b) { + AWS_PRECONDITION(a != b); + struct aws_hash_table tmp = *a; + *a = *b; + *b = tmp; +} + +void aws_hash_table_move(struct aws_hash_table *AWS_RESTRICT to, struct aws_hash_table *AWS_RESTRICT from) { AWS_PRECONDITION(to != NULL); AWS_PRECONDITION(from != NULL); AWS_PRECONDITION(to != from); AWS_PRECONDITION(aws_hash_table_is_valid(from)); - *to = *from; - AWS_ZERO_STRUCT(*from); - AWS_POSTCONDITION(aws_hash_table_is_valid(to)); -} - -/* Tries to find where the requested key is or where it should go if put. - * Returns AWS_ERROR_SUCCESS if the item existed (leaving it in *entry), - * or AWS_ERROR_HASHTBL_ITEM_NOT_FOUND if it did not (putting its destination - * in *entry). Note that this does not take care of displacing whatever was in - * that entry before. - * - * probe_idx is set to the probe index of the entry found. - */ - -static int s_find_entry1( - struct hash_table_state *state, - uint64_t hash_code, - const void *key, - struct hash_table_entry **p_entry, - size_t *p_probe_idx); - -/* Inlined fast path: Check the first slot, only. */ -/* TODO: Force inlining? */ -static int inline s_find_entry( - struct hash_table_state *state, - uint64_t hash_code, - const void *key, - struct hash_table_entry **p_entry, - size_t *p_probe_idx) { - struct hash_table_entry *entry = &state->slots[hash_code & state->mask]; - - if (entry->hash_code == 0) { - if (p_probe_idx) { - *p_probe_idx = 0; - } - *p_entry = entry; - return AWS_ERROR_HASHTBL_ITEM_NOT_FOUND; - } - + *to = *from; + AWS_ZERO_STRUCT(*from); + AWS_POSTCONDITION(aws_hash_table_is_valid(to)); +} + +/* Tries to find where the requested key is or where it should go if put. + * Returns AWS_ERROR_SUCCESS if the item existed (leaving it in *entry), + * or AWS_ERROR_HASHTBL_ITEM_NOT_FOUND if it did not (putting its destination + * in *entry). Note that this does not take care of displacing whatever was in + * that entry before. + * + * probe_idx is set to the probe index of the entry found. + */ + +static int s_find_entry1( + struct hash_table_state *state, + uint64_t hash_code, + const void *key, + struct hash_table_entry **p_entry, + size_t *p_probe_idx); + +/* Inlined fast path: Check the first slot, only. */ +/* TODO: Force inlining? */ +static int inline s_find_entry( + struct hash_table_state *state, + uint64_t hash_code, + const void *key, + struct hash_table_entry **p_entry, + size_t *p_probe_idx) { + struct hash_table_entry *entry = &state->slots[hash_code & state->mask]; + + if (entry->hash_code == 0) { + if (p_probe_idx) { + *p_probe_idx = 0; + } + *p_entry = entry; + return AWS_ERROR_HASHTBL_ITEM_NOT_FOUND; + } + if (entry->hash_code == hash_code && s_hash_keys_eq(state, key, entry->element.key)) { - if (p_probe_idx) { - *p_probe_idx = 0; - } - *p_entry = entry; - return AWS_OP_SUCCESS; - } - - return s_find_entry1(state, hash_code, key, p_entry, p_probe_idx); -} - -static int s_find_entry1( - struct hash_table_state *state, - uint64_t hash_code, - const void *key, - struct hash_table_entry **p_entry, - size_t *p_probe_idx) { - size_t probe_idx = 1; - /* If we find a deleted entry, we record that index and return it as our probe index (i.e. we'll keep searching to - * see if it already exists, but if not we'll overwrite the deleted entry). - */ - - int rv; - struct hash_table_entry *entry; + if (p_probe_idx) { + *p_probe_idx = 0; + } + *p_entry = entry; + return AWS_OP_SUCCESS; + } + + return s_find_entry1(state, hash_code, key, p_entry, p_probe_idx); +} + +static int s_find_entry1( + struct hash_table_state *state, + uint64_t hash_code, + const void *key, + struct hash_table_entry **p_entry, + size_t *p_probe_idx) { + size_t probe_idx = 1; + /* If we find a deleted entry, we record that index and return it as our probe index (i.e. we'll keep searching to + * see if it already exists, but if not we'll overwrite the deleted entry). + */ + + int rv; + struct hash_table_entry *entry; /* This loop is guaranteed to terminate because entry_probe is bounded above by state->mask (i.e. state->size - 1). * Since probe_idx increments every loop iteration, it will become larger than entry_probe after at most state->size * transitions and the loop will exit (if it hasn't already) @@ -350,82 +350,82 @@ static int s_find_entry1( # pragma CPROVER check push # pragma CPROVER check disable "unsigned-overflow" #endif - uint64_t index = (hash_code + probe_idx) & state->mask; + uint64_t index = (hash_code + probe_idx) & state->mask; #ifdef CBMC # pragma CPROVER check pop #endif - entry = &state->slots[index]; - if (!entry->hash_code) { - rv = AWS_ERROR_HASHTBL_ITEM_NOT_FOUND; - break; - } - + entry = &state->slots[index]; + if (!entry->hash_code) { + rv = AWS_ERROR_HASHTBL_ITEM_NOT_FOUND; + break; + } + if (entry->hash_code == hash_code && s_hash_keys_eq(state, key, entry->element.key)) { - rv = AWS_ERROR_SUCCESS; - break; - } - + rv = AWS_ERROR_SUCCESS; + break; + } + #ifdef CBMC # pragma CPROVER check push # pragma CPROVER check disable "unsigned-overflow" #endif - uint64_t entry_probe = (index - entry->hash_code) & state->mask; + uint64_t entry_probe = (index - entry->hash_code) & state->mask; #ifdef CBMC # pragma CPROVER check pop #endif - - if (entry_probe < probe_idx) { - /* We now know that our target entry cannot exist; if it did exist, - * it would be at the current location as it has a higher probe - * length than the entry we are examining and thus would have - * preempted that item - */ - rv = AWS_ERROR_HASHTBL_ITEM_NOT_FOUND; - break; - } - - probe_idx++; - } - - *p_entry = entry; - if (p_probe_idx) { - *p_probe_idx = probe_idx; + + if (entry_probe < probe_idx) { + /* We now know that our target entry cannot exist; if it did exist, + * it would be at the current location as it has a higher probe + * length than the entry we are examining and thus would have + * preempted that item + */ + rv = AWS_ERROR_HASHTBL_ITEM_NOT_FOUND; + break; + } + + probe_idx++; } - - return rv; -} - -int aws_hash_table_find(const struct aws_hash_table *map, const void *key, struct aws_hash_element **p_elem) { + + *p_entry = entry; + if (p_probe_idx) { + *p_probe_idx = probe_idx; + } + + return rv; +} + +int aws_hash_table_find(const struct aws_hash_table *map, const void *key, struct aws_hash_element **p_elem) { AWS_PRECONDITION(aws_hash_table_is_valid(map)); AWS_PRECONDITION(AWS_OBJECT_PTR_IS_WRITABLE(p_elem), "Input aws_hash_element pointer [p_elem] must be writable."); - struct hash_table_state *state = map->p_impl; - uint64_t hash_code = s_hash_for(state, key); - struct hash_table_entry *entry; - - int rv = s_find_entry(state, hash_code, key, &entry, NULL); - - if (rv == AWS_ERROR_SUCCESS) { - *p_elem = &entry->element; - } else { - *p_elem = NULL; - } + struct hash_table_state *state = map->p_impl; + uint64_t hash_code = s_hash_for(state, key); + struct hash_table_entry *entry; + + int rv = s_find_entry(state, hash_code, key, &entry, NULL); + + if (rv == AWS_ERROR_SUCCESS) { + *p_elem = &entry->element; + } else { + *p_elem = NULL; + } AWS_SUCCEED_WITH_POSTCONDITION(aws_hash_table_is_valid(map)); -} - +} + /** * Attempts to find a home for the given entry. * If the entry was empty (i.e. hash-code of 0), then the function does nothing and returns NULL * Otherwise, it emplaces the item, and returns a pointer to the newly emplaced entry. * This function is only called after the hash-table has been expanded to fit the new element, * so it should never fail. - */ -static struct hash_table_entry *s_emplace_item( - struct hash_table_state *state, - struct hash_table_entry entry, - size_t probe_idx) { + */ +static struct hash_table_entry *s_emplace_item( + struct hash_table_state *state, + struct hash_table_entry entry, + size_t probe_idx) { AWS_PRECONDITION(hash_table_state_is_valid(state)); - + if (entry.hash_code == 0) { AWS_RETURN_WITH_POSTCONDITION(NULL, hash_table_state_is_valid(state)); } @@ -439,263 +439,263 @@ static struct hash_table_entry *s_emplace_item( # pragma CPROVER check push # pragma CPROVER check disable "unsigned-overflow" #endif - size_t index = (size_t)(entry.hash_code + probe_idx) & state->mask; + size_t index = (size_t)(entry.hash_code + probe_idx) & state->mask; #ifdef CBMC # pragma CPROVER check pop #endif - struct hash_table_entry *victim = &state->slots[index]; - + struct hash_table_entry *victim = &state->slots[index]; + #ifdef CBMC # pragma CPROVER check push # pragma CPROVER check disable "unsigned-overflow" #endif - size_t victim_probe_idx = (size_t)(index - victim->hash_code) & state->mask; + size_t victim_probe_idx = (size_t)(index - victim->hash_code) & state->mask; #ifdef CBMC # pragma CPROVER check pop #endif - - if (!victim->hash_code || victim_probe_idx < probe_idx) { + + if (!victim->hash_code || victim_probe_idx < probe_idx) { /* The first thing we emplace is the entry itself. A pointer to its location becomes the rval */ if (!rval) { rval = victim; - } - - struct hash_table_entry tmp = *victim; - *victim = entry; - entry = tmp; - - probe_idx = victim_probe_idx + 1; - } else { - probe_idx++; - } - } - + } + + struct hash_table_entry tmp = *victim; + *victim = entry; + entry = tmp; + + probe_idx = victim_probe_idx + 1; + } else { + probe_idx++; + } + } + AWS_RETURN_WITH_POSTCONDITION( rval, hash_table_state_is_valid(state) && rval >= &state->slots[0] && rval < &state->slots[state->size], "Output hash_table_entry pointer [rval] must point in the slots of [state]."); -} - -static int s_expand_table(struct aws_hash_table *map) { - struct hash_table_state *old_state = map->p_impl; - struct hash_table_state template = *old_state; - +} + +static int s_expand_table(struct aws_hash_table *map) { + struct hash_table_state *old_state = map->p_impl; + struct hash_table_state template = *old_state; + size_t new_size; if (aws_mul_size_checked(template.size, 2, &new_size)) { return AWS_OP_ERR; } - + if (s_update_template_size(&template, new_size)) { return AWS_OP_ERR; } - struct hash_table_state *new_state = s_alloc_state(&template); - if (!new_state) { - return AWS_OP_ERR; - } - - for (size_t i = 0; i < old_state->size; i++) { - struct hash_table_entry entry = old_state->slots[i]; - if (entry.hash_code) { - /* We can directly emplace since we know we won't put the same item twice */ - s_emplace_item(new_state, entry, 0); - } - } - - map->p_impl = new_state; - aws_mem_release(new_state->alloc, old_state); - - return AWS_OP_SUCCESS; -} - -int aws_hash_table_create( - struct aws_hash_table *map, - const void *key, - struct aws_hash_element **p_elem, - int *was_created) { - - struct hash_table_state *state = map->p_impl; - uint64_t hash_code = s_hash_for(state, key); - struct hash_table_entry *entry; - size_t probe_idx; - int ignored; - if (!was_created) { - was_created = &ignored; - } - - int rv = s_find_entry(state, hash_code, key, &entry, &probe_idx); - - if (rv == AWS_ERROR_SUCCESS) { - if (p_elem) { - *p_elem = &entry->element; - } - *was_created = 0; - return AWS_OP_SUCCESS; - } - - /* Okay, we need to add an entry. Check the load factor first. */ - size_t incr_entry_count; - if (aws_add_size_checked(state->entry_count, 1, &incr_entry_count)) { - return AWS_OP_ERR; - } - if (incr_entry_count > state->max_load) { - rv = s_expand_table(map); - if (rv != AWS_OP_SUCCESS) { - /* Any error was already raised in expand_table */ - return rv; - } - state = map->p_impl; - /* If we expanded the table, we need to discard the probe index returned from find_entry, - * as it's likely that we can find a more desirable slot. If we don't, then later gets will - * terminate before reaching our probe index. - - * n.b. currently we ignore this probe_idx subsequently, but leaving - this here so we don't - * forget when we optimize later. */ - probe_idx = 0; - } - - state->entry_count++; - struct hash_table_entry new_entry; - new_entry.element.key = key; - new_entry.element.value = NULL; - new_entry.hash_code = hash_code; - - entry = s_emplace_item(state, new_entry, probe_idx); - - if (p_elem) { - *p_elem = &entry->element; - } - - *was_created = 1; - - return AWS_OP_SUCCESS; -} - -AWS_COMMON_API -int aws_hash_table_put(struct aws_hash_table *map, const void *key, void *value, int *was_created) { - struct aws_hash_element *p_elem; - int was_created_fallback; - - if (!was_created) { - was_created = &was_created_fallback; - } - - if (aws_hash_table_create(map, key, &p_elem, was_created)) { - return AWS_OP_ERR; - } - - /* - * aws_hash_table_create might resize the table, which results in map->p_impl changing. - * It is therefore important to wait to read p_impl until after we return. - */ - struct hash_table_state *state = map->p_impl; - - if (!*was_created) { - if (p_elem->key != key && state->destroy_key_fn) { - state->destroy_key_fn((void *)p_elem->key); - } - - if (state->destroy_value_fn) { - state->destroy_value_fn((void *)p_elem->value); - } - } - - p_elem->key = key; - p_elem->value = value; - - return AWS_OP_SUCCESS; -} - -/* Clears an entry. Does _not_ invoke destructor callbacks. - * Returns the last slot touched (note that if we wrap, we'll report an index - * lower than the original entry's index) - */ -static size_t s_remove_entry(struct hash_table_state *state, struct hash_table_entry *entry) { - AWS_PRECONDITION(hash_table_state_is_valid(state)); - AWS_PRECONDITION(state->entry_count > 0); + struct hash_table_state *new_state = s_alloc_state(&template); + if (!new_state) { + return AWS_OP_ERR; + } + + for (size_t i = 0; i < old_state->size; i++) { + struct hash_table_entry entry = old_state->slots[i]; + if (entry.hash_code) { + /* We can directly emplace since we know we won't put the same item twice */ + s_emplace_item(new_state, entry, 0); + } + } + + map->p_impl = new_state; + aws_mem_release(new_state->alloc, old_state); + + return AWS_OP_SUCCESS; +} + +int aws_hash_table_create( + struct aws_hash_table *map, + const void *key, + struct aws_hash_element **p_elem, + int *was_created) { + + struct hash_table_state *state = map->p_impl; + uint64_t hash_code = s_hash_for(state, key); + struct hash_table_entry *entry; + size_t probe_idx; + int ignored; + if (!was_created) { + was_created = &ignored; + } + + int rv = s_find_entry(state, hash_code, key, &entry, &probe_idx); + + if (rv == AWS_ERROR_SUCCESS) { + if (p_elem) { + *p_elem = &entry->element; + } + *was_created = 0; + return AWS_OP_SUCCESS; + } + + /* Okay, we need to add an entry. Check the load factor first. */ + size_t incr_entry_count; + if (aws_add_size_checked(state->entry_count, 1, &incr_entry_count)) { + return AWS_OP_ERR; + } + if (incr_entry_count > state->max_load) { + rv = s_expand_table(map); + if (rv != AWS_OP_SUCCESS) { + /* Any error was already raised in expand_table */ + return rv; + } + state = map->p_impl; + /* If we expanded the table, we need to discard the probe index returned from find_entry, + * as it's likely that we can find a more desirable slot. If we don't, then later gets will + * terminate before reaching our probe index. + + * n.b. currently we ignore this probe_idx subsequently, but leaving + this here so we don't + * forget when we optimize later. */ + probe_idx = 0; + } + + state->entry_count++; + struct hash_table_entry new_entry; + new_entry.element.key = key; + new_entry.element.value = NULL; + new_entry.hash_code = hash_code; + + entry = s_emplace_item(state, new_entry, probe_idx); + + if (p_elem) { + *p_elem = &entry->element; + } + + *was_created = 1; + + return AWS_OP_SUCCESS; +} + +AWS_COMMON_API +int aws_hash_table_put(struct aws_hash_table *map, const void *key, void *value, int *was_created) { + struct aws_hash_element *p_elem; + int was_created_fallback; + + if (!was_created) { + was_created = &was_created_fallback; + } + + if (aws_hash_table_create(map, key, &p_elem, was_created)) { + return AWS_OP_ERR; + } + + /* + * aws_hash_table_create might resize the table, which results in map->p_impl changing. + * It is therefore important to wait to read p_impl until after we return. + */ + struct hash_table_state *state = map->p_impl; + + if (!*was_created) { + if (p_elem->key != key && state->destroy_key_fn) { + state->destroy_key_fn((void *)p_elem->key); + } + + if (state->destroy_value_fn) { + state->destroy_value_fn((void *)p_elem->value); + } + } + + p_elem->key = key; + p_elem->value = value; + + return AWS_OP_SUCCESS; +} + +/* Clears an entry. Does _not_ invoke destructor callbacks. + * Returns the last slot touched (note that if we wrap, we'll report an index + * lower than the original entry's index) + */ +static size_t s_remove_entry(struct hash_table_state *state, struct hash_table_entry *entry) { + AWS_PRECONDITION(hash_table_state_is_valid(state)); + AWS_PRECONDITION(state->entry_count > 0); AWS_PRECONDITION( entry >= &state->slots[0] && entry < &state->slots[state->size], "Input hash_table_entry [entry] pointer must point in the available slots."); - state->entry_count--; - - /* Shift subsequent entries back until we find an entry that belongs at its - * current position. This is important to ensure that subsequent searches - * don't terminate at the removed element. - */ - size_t index = s_index_for(state, entry); - /* There is always at least one empty slot in the hash table, so this loop always terminates */ - while (1) { - size_t next_index = (index + 1) & state->mask; - - /* If we hit an empty slot, stop */ - if (!state->slots[next_index].hash_code) { - break; - } - /* If the next slot is at the start of the probe sequence, stop. - * We know that nothing with an earlier home slot is after this; - * otherwise this index-zero entry would have been evicted from its - * home. - */ - if ((state->slots[next_index].hash_code & state->mask) == next_index) { - break; - } - - /* Okay, shift this one back */ - state->slots[index] = state->slots[next_index]; - index = next_index; - } - - /* Clear the entry we shifted out of */ - AWS_ZERO_STRUCT(state->slots[index]); + state->entry_count--; + + /* Shift subsequent entries back until we find an entry that belongs at its + * current position. This is important to ensure that subsequent searches + * don't terminate at the removed element. + */ + size_t index = s_index_for(state, entry); + /* There is always at least one empty slot in the hash table, so this loop always terminates */ + while (1) { + size_t next_index = (index + 1) & state->mask; + + /* If we hit an empty slot, stop */ + if (!state->slots[next_index].hash_code) { + break; + } + /* If the next slot is at the start of the probe sequence, stop. + * We know that nothing with an earlier home slot is after this; + * otherwise this index-zero entry would have been evicted from its + * home. + */ + if ((state->slots[next_index].hash_code & state->mask) == next_index) { + break; + } + + /* Okay, shift this one back */ + state->slots[index] = state->slots[next_index]; + index = next_index; + } + + /* Clear the entry we shifted out of */ + AWS_ZERO_STRUCT(state->slots[index]); AWS_RETURN_WITH_POSTCONDITION(index, hash_table_state_is_valid(state) && index <= state->size); -} - -int aws_hash_table_remove( - struct aws_hash_table *map, - const void *key, - struct aws_hash_element *p_value, - int *was_present) { +} + +int aws_hash_table_remove( + struct aws_hash_table *map, + const void *key, + struct aws_hash_element *p_value, + int *was_present) { AWS_PRECONDITION(aws_hash_table_is_valid(map)); AWS_PRECONDITION( p_value == NULL || AWS_OBJECT_PTR_IS_WRITABLE(p_value), "Input pointer [p_value] must be NULL or writable."); AWS_PRECONDITION( was_present == NULL || AWS_OBJECT_PTR_IS_WRITABLE(was_present), "Input pointer [was_present] must be NULL or writable."); - - struct hash_table_state *state = map->p_impl; - uint64_t hash_code = s_hash_for(state, key); - struct hash_table_entry *entry; - int ignored; - - if (!was_present) { - was_present = &ignored; - } - - int rv = s_find_entry(state, hash_code, key, &entry, NULL); - - if (rv != AWS_ERROR_SUCCESS) { - *was_present = 0; + + struct hash_table_state *state = map->p_impl; + uint64_t hash_code = s_hash_for(state, key); + struct hash_table_entry *entry; + int ignored; + + if (!was_present) { + was_present = &ignored; + } + + int rv = s_find_entry(state, hash_code, key, &entry, NULL); + + if (rv != AWS_ERROR_SUCCESS) { + *was_present = 0; AWS_SUCCEED_WITH_POSTCONDITION(aws_hash_table_is_valid(map)); - } - - *was_present = 1; - - if (p_value) { - *p_value = entry->element; - } else { - if (state->destroy_key_fn) { - state->destroy_key_fn((void *)entry->element.key); - } - if (state->destroy_value_fn) { - state->destroy_value_fn(entry->element.value); - } - } - s_remove_entry(state, entry); - + } + + *was_present = 1; + + if (p_value) { + *p_value = entry->element; + } else { + if (state->destroy_key_fn) { + state->destroy_key_fn((void *)entry->element.key); + } + if (state->destroy_value_fn) { + state->destroy_value_fn(entry->element.value); + } + } + s_remove_entry(state, entry); + AWS_SUCCEED_WITH_POSTCONDITION(aws_hash_table_is_valid(map)); -} - +} + int aws_hash_table_remove_element(struct aws_hash_table *map, struct aws_hash_element *p_value) { AWS_PRECONDITION(aws_hash_table_is_valid(map)); AWS_PRECONDITION(p_value != NULL); @@ -708,213 +708,213 @@ int aws_hash_table_remove_element(struct aws_hash_table *map, struct aws_hash_el AWS_SUCCEED_WITH_POSTCONDITION(aws_hash_table_is_valid(map)); } -int aws_hash_table_foreach( - struct aws_hash_table *map, - int (*callback)(void *context, struct aws_hash_element *pElement), - void *context) { - - for (struct aws_hash_iter iter = aws_hash_iter_begin(map); !aws_hash_iter_done(&iter); aws_hash_iter_next(&iter)) { - int rv = callback(context, &iter.element); - - if (rv & AWS_COMMON_HASH_TABLE_ITER_DELETE) { - aws_hash_iter_delete(&iter, false); - } - - if (!(rv & AWS_COMMON_HASH_TABLE_ITER_CONTINUE)) { - break; - } - } - - return AWS_OP_SUCCESS; -} - -bool aws_hash_table_eq( - const struct aws_hash_table *a, - const struct aws_hash_table *b, - aws_hash_callback_eq_fn *value_eq) { +int aws_hash_table_foreach( + struct aws_hash_table *map, + int (*callback)(void *context, struct aws_hash_element *pElement), + void *context) { + + for (struct aws_hash_iter iter = aws_hash_iter_begin(map); !aws_hash_iter_done(&iter); aws_hash_iter_next(&iter)) { + int rv = callback(context, &iter.element); + + if (rv & AWS_COMMON_HASH_TABLE_ITER_DELETE) { + aws_hash_iter_delete(&iter, false); + } + + if (!(rv & AWS_COMMON_HASH_TABLE_ITER_CONTINUE)) { + break; + } + } + + return AWS_OP_SUCCESS; +} + +bool aws_hash_table_eq( + const struct aws_hash_table *a, + const struct aws_hash_table *b, + aws_hash_callback_eq_fn *value_eq) { AWS_PRECONDITION(aws_hash_table_is_valid(a)); AWS_PRECONDITION(aws_hash_table_is_valid(b)); AWS_PRECONDITION(value_eq != NULL); - if (aws_hash_table_get_entry_count(a) != aws_hash_table_get_entry_count(b)) { + if (aws_hash_table_get_entry_count(a) != aws_hash_table_get_entry_count(b)) { AWS_RETURN_WITH_POSTCONDITION(false, aws_hash_table_is_valid(a) && aws_hash_table_is_valid(b)); - } - - /* - * Now that we have established that the two tables have the same number of - * entries, we can simply iterate one and compare against the same key in - * the other. - */ + } + + /* + * Now that we have established that the two tables have the same number of + * entries, we can simply iterate one and compare against the same key in + * the other. + */ for (size_t i = 0; i < a->p_impl->size; ++i) { const struct hash_table_entry *const a_entry = &a->p_impl->slots[i]; if (a_entry->hash_code == 0) { continue; } - struct aws_hash_element *b_element = NULL; - + struct aws_hash_element *b_element = NULL; + aws_hash_table_find(b, a_entry->element.key, &b_element); - - if (!b_element) { - /* Key is present in A only */ + + if (!b_element) { + /* Key is present in A only */ AWS_RETURN_WITH_POSTCONDITION(false, aws_hash_table_is_valid(a) && aws_hash_table_is_valid(b)); - } - + } + if (!s_safe_eq_check(value_eq, a_entry->element.value, b_element->value)) { AWS_RETURN_WITH_POSTCONDITION(false, aws_hash_table_is_valid(a) && aws_hash_table_is_valid(b)); - } - } + } + } AWS_RETURN_WITH_POSTCONDITION(true, aws_hash_table_is_valid(a) && aws_hash_table_is_valid(b)); -} - -/** - * Given an iterator, and a start slot, find the next available filled slot if it exists - * Otherwise, return an iter that will return true for aws_hash_iter_done(). - * Note that aws_hash_iter_is_valid() need not hold on entry to the function, since - * it can be called on a partially constructed iter from aws_hash_iter_begin(). - * - * Note that calling this on an iterator which is "done" is idempotent: it will return another - * iterator which is "done". - */ -static inline void s_get_next_element(struct aws_hash_iter *iter, size_t start_slot) { - AWS_PRECONDITION(iter != NULL); +} + +/** + * Given an iterator, and a start slot, find the next available filled slot if it exists + * Otherwise, return an iter that will return true for aws_hash_iter_done(). + * Note that aws_hash_iter_is_valid() need not hold on entry to the function, since + * it can be called on a partially constructed iter from aws_hash_iter_begin(). + * + * Note that calling this on an iterator which is "done" is idempotent: it will return another + * iterator which is "done". + */ +static inline void s_get_next_element(struct aws_hash_iter *iter, size_t start_slot) { + AWS_PRECONDITION(iter != NULL); AWS_PRECONDITION(aws_hash_table_is_valid(iter->map)); - struct hash_table_state *state = iter->map->p_impl; - size_t limit = iter->limit; - - for (size_t i = start_slot; i < limit; i++) { - struct hash_table_entry *entry = &state->slots[i]; - - if (entry->hash_code) { - iter->element = entry->element; - iter->slot = i; - iter->status = AWS_HASH_ITER_STATUS_READY_FOR_USE; - return; - } - } - iter->element.key = NULL; - iter->element.value = NULL; - iter->slot = iter->limit; - iter->status = AWS_HASH_ITER_STATUS_DONE; - AWS_POSTCONDITION(aws_hash_iter_is_valid(iter)); -} - -struct aws_hash_iter aws_hash_iter_begin(const struct aws_hash_table *map) { - AWS_PRECONDITION(aws_hash_table_is_valid(map)); - struct hash_table_state *state = map->p_impl; - struct aws_hash_iter iter; + struct hash_table_state *state = iter->map->p_impl; + size_t limit = iter->limit; + + for (size_t i = start_slot; i < limit; i++) { + struct hash_table_entry *entry = &state->slots[i]; + + if (entry->hash_code) { + iter->element = entry->element; + iter->slot = i; + iter->status = AWS_HASH_ITER_STATUS_READY_FOR_USE; + return; + } + } + iter->element.key = NULL; + iter->element.value = NULL; + iter->slot = iter->limit; + iter->status = AWS_HASH_ITER_STATUS_DONE; + AWS_POSTCONDITION(aws_hash_iter_is_valid(iter)); +} + +struct aws_hash_iter aws_hash_iter_begin(const struct aws_hash_table *map) { + AWS_PRECONDITION(aws_hash_table_is_valid(map)); + struct hash_table_state *state = map->p_impl; + struct aws_hash_iter iter; AWS_ZERO_STRUCT(iter); - iter.map = map; - iter.limit = state->size; - s_get_next_element(&iter, 0); + iter.map = map; + iter.limit = state->size; + s_get_next_element(&iter, 0); AWS_RETURN_WITH_POSTCONDITION( iter, aws_hash_iter_is_valid(&iter) && (iter.status == AWS_HASH_ITER_STATUS_DONE || iter.status == AWS_HASH_ITER_STATUS_READY_FOR_USE), "The status of output aws_hash_iter [iter] must either be DONE or READY_FOR_USE."); -} - -bool aws_hash_iter_done(const struct aws_hash_iter *iter) { - AWS_PRECONDITION(aws_hash_iter_is_valid(iter)); +} + +bool aws_hash_iter_done(const struct aws_hash_iter *iter) { + AWS_PRECONDITION(aws_hash_iter_is_valid(iter)); AWS_PRECONDITION( iter->status == AWS_HASH_ITER_STATUS_DONE || iter->status == AWS_HASH_ITER_STATUS_READY_FOR_USE, "Input aws_hash_iter [iter] must either be done, or ready to use."); - /* - * SIZE_MAX is a valid (non-terminal) value for iter->slot in the event that - * we delete slot 0. See comments in aws_hash_iter_delete. - * - * As such we must use == rather than >= here. - */ - bool rval = (iter->slot == iter->limit); + /* + * SIZE_MAX is a valid (non-terminal) value for iter->slot in the event that + * we delete slot 0. See comments in aws_hash_iter_delete. + * + * As such we must use == rather than >= here. + */ + bool rval = (iter->slot == iter->limit); AWS_POSTCONDITION( iter->status == AWS_HASH_ITER_STATUS_DONE || iter->status == AWS_HASH_ITER_STATUS_READY_FOR_USE, "The status of output aws_hash_iter [iter] must either be DONE or READY_FOR_USE."); AWS_POSTCONDITION( rval == (iter->status == AWS_HASH_ITER_STATUS_DONE), "Output bool [rval] must be true if and only if the status of [iter] is DONE."); - AWS_POSTCONDITION(aws_hash_iter_is_valid(iter)); - return rval; -} - -void aws_hash_iter_next(struct aws_hash_iter *iter) { - AWS_PRECONDITION(aws_hash_iter_is_valid(iter)); + AWS_POSTCONDITION(aws_hash_iter_is_valid(iter)); + return rval; +} + +void aws_hash_iter_next(struct aws_hash_iter *iter) { + AWS_PRECONDITION(aws_hash_iter_is_valid(iter)); #ifdef CBMC # pragma CPROVER check push # pragma CPROVER check disable "unsigned-overflow" #endif - s_get_next_element(iter, iter->slot + 1); + s_get_next_element(iter, iter->slot + 1); #ifdef CBMC # pragma CPROVER check pop #endif AWS_POSTCONDITION( iter->status == AWS_HASH_ITER_STATUS_DONE || iter->status == AWS_HASH_ITER_STATUS_READY_FOR_USE, "The status of output aws_hash_iter [iter] must either be DONE or READY_FOR_USE."); - AWS_POSTCONDITION(aws_hash_iter_is_valid(iter)); -} - -void aws_hash_iter_delete(struct aws_hash_iter *iter, bool destroy_contents) { + AWS_POSTCONDITION(aws_hash_iter_is_valid(iter)); +} + +void aws_hash_iter_delete(struct aws_hash_iter *iter, bool destroy_contents) { AWS_PRECONDITION( iter->status == AWS_HASH_ITER_STATUS_READY_FOR_USE, "Input aws_hash_iter [iter] must be ready for use."); - AWS_PRECONDITION(aws_hash_iter_is_valid(iter)); + AWS_PRECONDITION(aws_hash_iter_is_valid(iter)); AWS_PRECONDITION( iter->map->p_impl->entry_count > 0, "The hash_table_state pointed by input [iter] must contain at least one entry."); - - struct hash_table_state *state = iter->map->p_impl; - if (destroy_contents) { - if (state->destroy_key_fn) { - state->destroy_key_fn((void *)iter->element.key); - } - if (state->destroy_value_fn) { - state->destroy_value_fn(iter->element.value); - } - } - - size_t last_index = s_remove_entry(state, &state->slots[iter->slot]); - - /* If we shifted elements that are not part of the window we intend to iterate - * over, it means we shifted an element that we already visited into the - * iter->limit - 1 position. To avoid double iteration, we'll now reduce the - * limit to compensate. - * - * Note that last_index cannot equal iter->slot, because slots[iter->slot] - * is empty before we start walking the table. - */ - if (last_index < iter->slot || last_index >= iter->limit) { - iter->limit--; - } - - /* - * After removing this entry, the next entry might be in the same slot, or - * in some later slot, or we might have no further entries. - * - * We also expect that the caller will call aws_hash_iter_done and aws_hash_iter_next - * after this delete call. This gets a bit tricky if we just deleted the value - * in slot 0, and a new value has shifted in. - * - * To deal with this, we'll just step back one slot, and let _next start iteration - * at our current slot. Note that if we just deleted slot 0, this will result in - * underflowing to SIZE_MAX; we have to take care in aws_hash_iter_done to avoid - * treating this as an end-of-iteration condition. - */ + + struct hash_table_state *state = iter->map->p_impl; + if (destroy_contents) { + if (state->destroy_key_fn) { + state->destroy_key_fn((void *)iter->element.key); + } + if (state->destroy_value_fn) { + state->destroy_value_fn(iter->element.value); + } + } + + size_t last_index = s_remove_entry(state, &state->slots[iter->slot]); + + /* If we shifted elements that are not part of the window we intend to iterate + * over, it means we shifted an element that we already visited into the + * iter->limit - 1 position. To avoid double iteration, we'll now reduce the + * limit to compensate. + * + * Note that last_index cannot equal iter->slot, because slots[iter->slot] + * is empty before we start walking the table. + */ + if (last_index < iter->slot || last_index >= iter->limit) { + iter->limit--; + } + + /* + * After removing this entry, the next entry might be in the same slot, or + * in some later slot, or we might have no further entries. + * + * We also expect that the caller will call aws_hash_iter_done and aws_hash_iter_next + * after this delete call. This gets a bit tricky if we just deleted the value + * in slot 0, and a new value has shifted in. + * + * To deal with this, we'll just step back one slot, and let _next start iteration + * at our current slot. Note that if we just deleted slot 0, this will result in + * underflowing to SIZE_MAX; we have to take care in aws_hash_iter_done to avoid + * treating this as an end-of-iteration condition. + */ #ifdef CBMC # pragma CPROVER check push # pragma CPROVER check disable "unsigned-overflow" #endif - iter->slot--; + iter->slot--; #ifdef CBMC # pragma CPROVER check pop #endif - iter->status = AWS_HASH_ITER_STATUS_DELETE_CALLED; + iter->status = AWS_HASH_ITER_STATUS_DELETE_CALLED; AWS_POSTCONDITION( iter->status == AWS_HASH_ITER_STATUS_DELETE_CALLED, "The status of output aws_hash_iter [iter] must be DELETE_CALLED."); - AWS_POSTCONDITION(aws_hash_iter_is_valid(iter)); -} - -void aws_hash_table_clear(struct aws_hash_table *map) { + AWS_POSTCONDITION(aws_hash_iter_is_valid(iter)); +} + +void aws_hash_table_clear(struct aws_hash_table *map) { AWS_PRECONDITION(aws_hash_table_is_valid(map)); - struct hash_table_state *state = map->p_impl; + struct hash_table_state *state = map->p_impl; /* Check that we have at least one destructor before iterating over the table */ if (state->destroy_key_fn || state->destroy_value_fn) { @@ -922,65 +922,65 @@ void aws_hash_table_clear(struct aws_hash_table *map) { struct hash_table_entry *entry = &state->slots[i]; if (!entry->hash_code) { continue; - } + } if (state->destroy_key_fn) { state->destroy_key_fn((void *)entry->element.key); - } + } if (state->destroy_value_fn) { - state->destroy_value_fn(entry->element.value); - } - } - } - /* Since hash code 0 represents an empty slot we can just zero out the - * entire table. */ - memset(state->slots, 0, sizeof(*state->slots) * state->size); - - state->entry_count = 0; + state->destroy_value_fn(entry->element.value); + } + } + } + /* Since hash code 0 represents an empty slot we can just zero out the + * entire table. */ + memset(state->slots, 0, sizeof(*state->slots) * state->size); + + state->entry_count = 0; AWS_POSTCONDITION(aws_hash_table_is_valid(map)); -} - -uint64_t aws_hash_c_string(const void *item) { +} + +uint64_t aws_hash_c_string(const void *item) { AWS_PRECONDITION(aws_c_string_is_valid(item)); - const char *str = item; - - /* first digits of pi in hex */ - uint32_t b = 0x3243F6A8, c = 0x885A308D; - hashlittle2(str, strlen(str), &c, &b); - - return ((uint64_t)b << 32) | c; -} - -uint64_t aws_hash_string(const void *item) { + const char *str = item; + + /* first digits of pi in hex */ + uint32_t b = 0x3243F6A8, c = 0x885A308D; + hashlittle2(str, strlen(str), &c, &b); + + return ((uint64_t)b << 32) | c; +} + +uint64_t aws_hash_string(const void *item) { AWS_PRECONDITION(aws_string_is_valid(item)); - const struct aws_string *str = item; - - /* first digits of pi in hex */ - uint32_t b = 0x3243F6A8, c = 0x885A308D; - hashlittle2(aws_string_bytes(str), str->len, &c, &b); + const struct aws_string *str = item; + + /* first digits of pi in hex */ + uint32_t b = 0x3243F6A8, c = 0x885A308D; + hashlittle2(aws_string_bytes(str), str->len, &c, &b); AWS_RETURN_WITH_POSTCONDITION(((uint64_t)b << 32) | c, aws_string_is_valid(str)); -} - -uint64_t aws_hash_byte_cursor_ptr(const void *item) { +} + +uint64_t aws_hash_byte_cursor_ptr(const void *item) { AWS_PRECONDITION(aws_byte_cursor_is_valid(item)); - const struct aws_byte_cursor *cur = item; - - /* first digits of pi in hex */ - uint32_t b = 0x3243F6A8, c = 0x885A308D; - hashlittle2(cur->ptr, cur->len, &c, &b); + const struct aws_byte_cursor *cur = item; + + /* first digits of pi in hex */ + uint32_t b = 0x3243F6A8, c = 0x885A308D; + hashlittle2(cur->ptr, cur->len, &c, &b); AWS_RETURN_WITH_POSTCONDITION(((uint64_t)b << 32) | c, aws_byte_cursor_is_valid(cur)); -} - -uint64_t aws_hash_ptr(const void *item) { +} + +uint64_t aws_hash_ptr(const void *item) { /* Since the numeric value of the pointer is considered, not the memory behind it, 0 is an acceptable value */ - /* first digits of e in hex - * 2.b7e 1516 28ae d2a6 */ - uint32_t b = 0x2b7e1516, c = 0x28aed2a6; - - hashlittle2(&item, sizeof(item), &c, &b); - - return ((uint64_t)b << 32) | c; -} - + /* first digits of e in hex + * 2.b7e 1516 28ae d2a6 */ + uint32_t b = 0x2b7e1516, c = 0x28aed2a6; + + hashlittle2(&item, sizeof(item), &c, &b); + + return ((uint64_t)b << 32) | c; +} + uint64_t aws_hash_combine(uint64_t item1, uint64_t item2) { uint32_t b = item2 & 0xFFFFFFFF; /* LSB */ uint32_t c = item2 >> 32; /* MSB */ @@ -989,28 +989,28 @@ uint64_t aws_hash_combine(uint64_t item1, uint64_t item2) { return ((uint64_t)b << 32) | c; } -bool aws_hash_callback_c_str_eq(const void *a, const void *b) { - AWS_PRECONDITION(aws_c_string_is_valid(a)); - AWS_PRECONDITION(aws_c_string_is_valid(b)); - bool rval = !strcmp(a, b); +bool aws_hash_callback_c_str_eq(const void *a, const void *b) { + AWS_PRECONDITION(aws_c_string_is_valid(a)); + AWS_PRECONDITION(aws_c_string_is_valid(b)); + bool rval = !strcmp(a, b); AWS_RETURN_WITH_POSTCONDITION(rval, aws_c_string_is_valid(a) && aws_c_string_is_valid(b)); -} - -bool aws_hash_callback_string_eq(const void *a, const void *b) { - AWS_PRECONDITION(aws_string_is_valid(a)); - AWS_PRECONDITION(aws_string_is_valid(b)); - bool rval = aws_string_eq(a, b); +} + +bool aws_hash_callback_string_eq(const void *a, const void *b) { + AWS_PRECONDITION(aws_string_is_valid(a)); + AWS_PRECONDITION(aws_string_is_valid(b)); + bool rval = aws_string_eq(a, b); AWS_RETURN_WITH_POSTCONDITION(rval, aws_c_string_is_valid(a) && aws_c_string_is_valid(b)); -} - -void aws_hash_callback_string_destroy(void *a) { - AWS_PRECONDITION(aws_string_is_valid(a)); - aws_string_destroy(a); -} - -bool aws_ptr_eq(const void *a, const void *b) { - return a == b; -} +} + +void aws_hash_callback_string_destroy(void *a) { + AWS_PRECONDITION(aws_string_is_valid(a)); + aws_string_destroy(a); +} + +bool aws_ptr_eq(const void *a, const void *b) { + return a == b; +} /** * Best-effort check of hash_table_state data-structure invariants diff --git a/contrib/restricted/aws/aws-c-common/source/lru_cache.c b/contrib/restricted/aws/aws-c-common/source/lru_cache.c index 15de626b96..37724fd079 100644 --- a/contrib/restricted/aws/aws-c-common/source/lru_cache.c +++ b/contrib/restricted/aws/aws-c-common/source/lru_cache.c @@ -1,18 +1,18 @@ /** * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0. - */ -#include <aws/common/lru_cache.h> + */ +#include <aws/common/lru_cache.h> static int s_lru_cache_put(struct aws_cache *cache, const void *key, void *p_value); static int s_lru_cache_find(struct aws_cache *cache, const void *key, void **p_value); static void *s_lru_cache_use_lru_element(struct aws_cache *cache); static void *s_lru_cache_get_mru_element(const struct aws_cache *cache); - + struct lru_cache_impl_vtable { void *(*use_lru_element)(struct aws_cache *cache); void *(*get_mru_element)(const struct aws_cache *cache); -}; - +}; + static struct aws_cache_vtable s_lru_cache_vtable = { .destroy = aws_cache_base_default_destroy, .find = s_lru_cache_find, @@ -21,23 +21,23 @@ static struct aws_cache_vtable s_lru_cache_vtable = { .clear = aws_cache_base_default_clear, .get_element_count = aws_cache_base_default_get_element_count, }; - + struct aws_cache *aws_cache_new_lru( - struct aws_allocator *allocator, - aws_hash_fn *hash_fn, - aws_hash_callback_eq_fn *equals_fn, - aws_hash_callback_destroy_fn *destroy_key_fn, - aws_hash_callback_destroy_fn *destroy_value_fn, - size_t max_items) { - AWS_ASSERT(allocator); - AWS_ASSERT(max_items); + struct aws_allocator *allocator, + aws_hash_fn *hash_fn, + aws_hash_callback_eq_fn *equals_fn, + aws_hash_callback_destroy_fn *destroy_key_fn, + aws_hash_callback_destroy_fn *destroy_value_fn, + size_t max_items) { + AWS_ASSERT(allocator); + AWS_ASSERT(max_items); struct aws_cache *lru_cache = NULL; struct lru_cache_impl_vtable *impl = NULL; - + if (!aws_mem_acquire_many( allocator, 2, &lru_cache, sizeof(struct aws_cache), &impl, sizeof(struct lru_cache_impl_vtable))) { return NULL; - } + } impl->use_lru_element = s_lru_cache_use_lru_element; impl->get_mru_element = s_lru_cache_get_mru_element; lru_cache->allocator = allocator; @@ -49,15 +49,15 @@ struct aws_cache *aws_cache_new_lru( return NULL; } return lru_cache; -} - +} + /* implementation for lru cache put */ static int s_lru_cache_put(struct aws_cache *cache, const void *key, void *p_value) { - + if (aws_linked_hash_table_put(&cache->table, key, p_value)) { - return AWS_OP_ERR; - } - + return AWS_OP_ERR; + } + /* Manage the space if we actually added a new element and the cache is full. */ if (aws_linked_hash_table_get_element_count(&cache->table) > cache->max_items) { /* we're over the cache size limit. Remove whatever is in the front of @@ -66,46 +66,46 @@ static int s_lru_cache_put(struct aws_cache *cache, const void *key, void *p_val struct aws_linked_list_node *node = aws_linked_list_front(list); struct aws_linked_hash_table_node *table_node = AWS_CONTAINER_OF(node, struct aws_linked_hash_table_node, node); return aws_linked_hash_table_remove(&cache->table, table_node->key); - } - - return AWS_OP_SUCCESS; -} + } + + return AWS_OP_SUCCESS; +} /* implementation for lru cache find */ static int s_lru_cache_find(struct aws_cache *cache, const void *key, void **p_value) { return (aws_linked_hash_table_find_and_move_to_back(&cache->table, key, p_value)); -} - +} + static void *s_lru_cache_use_lru_element(struct aws_cache *cache) { const struct aws_linked_list *list = aws_linked_hash_table_get_iteration_list(&cache->table); if (aws_linked_list_empty(list)) { - return NULL; - } + return NULL; + } struct aws_linked_list_node *node = aws_linked_list_front(list); struct aws_linked_hash_table_node *lru_node = AWS_CONTAINER_OF(node, struct aws_linked_hash_table_node, node); - + aws_linked_hash_table_move_node_to_end_of_list(&cache->table, lru_node); return lru_node->value; -} +} static void *s_lru_cache_get_mru_element(const struct aws_cache *cache) { const struct aws_linked_list *list = aws_linked_hash_table_get_iteration_list(&cache->table); if (aws_linked_list_empty(list)) { - return NULL; - } + return NULL; + } struct aws_linked_list_node *node = aws_linked_list_back(list); struct aws_linked_hash_table_node *mru_node = AWS_CONTAINER_OF(node, struct aws_linked_hash_table_node, node); return mru_node->value; } - + void *aws_lru_cache_use_lru_element(struct aws_cache *cache) { AWS_PRECONDITION(cache); AWS_PRECONDITION(cache->impl); struct lru_cache_impl_vtable *impl_vtable = cache->impl; return impl_vtable->use_lru_element(cache); -} - +} + void *aws_lru_cache_get_mru_element(const struct aws_cache *cache) { AWS_PRECONDITION(cache); AWS_PRECONDITION(cache->impl); struct lru_cache_impl_vtable *impl_vtable = cache->impl; return impl_vtable->get_mru_element(cache); -} +} diff --git a/contrib/restricted/aws/aws-c-common/source/posix/clock.c b/contrib/restricted/aws/aws-c-common/source/posix/clock.c index 90e213ea7c..a74515f5fe 100644 --- a/contrib/restricted/aws/aws-c-common/source/posix/clock.c +++ b/contrib/restricted/aws/aws-c-common/source/posix/clock.c @@ -1,136 +1,136 @@ /** * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0. - */ - -#include <aws/common/clock.h> - -#include <time.h> - -static const uint64_t NS_PER_SEC = 1000000000; - -#if defined(CLOCK_MONOTONIC_RAW) -# define HIGH_RES_CLOCK CLOCK_MONOTONIC_RAW -#else -# define HIGH_RES_CLOCK CLOCK_MONOTONIC -#endif - -/* This entire compilation branch has two goals. First, prior to OSX Sierra, clock_gettime does not exist on OSX, so we - * already need to branch on that. Second, even if we compile on a newer OSX, which we will always do for bindings (e.g. - * python, dotnet, java etc...), we have to worry about the same lib being loaded on an older version, and thus, we'd - * get linker errors at runtime. To avoid this, we do a dynamic load - * to keep the function out of linker tables and only use the symbol if the current running process has access to the - * function. */ -#if defined(__MACH__) -# include <AvailabilityMacros.h> -# include <aws/common/thread.h> -# include <dlfcn.h> -# include <sys/time.h> - -static int s_legacy_get_time(uint64_t *timestamp) { - struct timeval tv; - int ret_val = gettimeofday(&tv, NULL); - - if (ret_val) { - return aws_raise_error(AWS_ERROR_CLOCK_FAILURE); - } - + */ + +#include <aws/common/clock.h> + +#include <time.h> + +static const uint64_t NS_PER_SEC = 1000000000; + +#if defined(CLOCK_MONOTONIC_RAW) +# define HIGH_RES_CLOCK CLOCK_MONOTONIC_RAW +#else +# define HIGH_RES_CLOCK CLOCK_MONOTONIC +#endif + +/* This entire compilation branch has two goals. First, prior to OSX Sierra, clock_gettime does not exist on OSX, so we + * already need to branch on that. Second, even if we compile on a newer OSX, which we will always do for bindings (e.g. + * python, dotnet, java etc...), we have to worry about the same lib being loaded on an older version, and thus, we'd + * get linker errors at runtime. To avoid this, we do a dynamic load + * to keep the function out of linker tables and only use the symbol if the current running process has access to the + * function. */ +#if defined(__MACH__) +# include <AvailabilityMacros.h> +# include <aws/common/thread.h> +# include <dlfcn.h> +# include <sys/time.h> + +static int s_legacy_get_time(uint64_t *timestamp) { + struct timeval tv; + int ret_val = gettimeofday(&tv, NULL); + + if (ret_val) { + return aws_raise_error(AWS_ERROR_CLOCK_FAILURE); + } + uint64_t secs = (uint64_t)tv.tv_sec; uint64_t u_secs = (uint64_t)tv.tv_usec; *timestamp = (secs * NS_PER_SEC) + (u_secs * 1000); - return AWS_OP_SUCCESS; -} - -# if MAC_OS_X_VERSION_MAX_ALLOWED >= 101200 -static aws_thread_once s_thread_once_flag = AWS_THREAD_ONCE_STATIC_INIT; -static int (*s_gettime_fn)(clockid_t __clock_id, struct timespec *__tp) = NULL; - + return AWS_OP_SUCCESS; +} + +# if MAC_OS_X_VERSION_MAX_ALLOWED >= 101200 +static aws_thread_once s_thread_once_flag = AWS_THREAD_ONCE_STATIC_INIT; +static int (*s_gettime_fn)(clockid_t __clock_id, struct timespec *__tp) = NULL; + static void s_do_osx_loads(void *user_data) { (void)user_data; - s_gettime_fn = (int (*)(clockid_t __clock_id, struct timespec * __tp)) dlsym(RTLD_DEFAULT, "clock_gettime"); -} - -int aws_high_res_clock_get_ticks(uint64_t *timestamp) { + s_gettime_fn = (int (*)(clockid_t __clock_id, struct timespec * __tp)) dlsym(RTLD_DEFAULT, "clock_gettime"); +} + +int aws_high_res_clock_get_ticks(uint64_t *timestamp) { aws_thread_call_once(&s_thread_once_flag, s_do_osx_loads, NULL); - int ret_val = 0; - - if (s_gettime_fn) { - struct timespec ts; - ret_val = s_gettime_fn(HIGH_RES_CLOCK, &ts); - - if (ret_val) { - return aws_raise_error(AWS_ERROR_CLOCK_FAILURE); - } - + int ret_val = 0; + + if (s_gettime_fn) { + struct timespec ts; + ret_val = s_gettime_fn(HIGH_RES_CLOCK, &ts); + + if (ret_val) { + return aws_raise_error(AWS_ERROR_CLOCK_FAILURE); + } + uint64_t secs = (uint64_t)ts.tv_sec; uint64_t n_secs = (uint64_t)ts.tv_nsec; *timestamp = (secs * NS_PER_SEC) + n_secs; - return AWS_OP_SUCCESS; - } - - return s_legacy_get_time(timestamp); -} - -int aws_sys_clock_get_ticks(uint64_t *timestamp) { + return AWS_OP_SUCCESS; + } + + return s_legacy_get_time(timestamp); +} + +int aws_sys_clock_get_ticks(uint64_t *timestamp) { aws_thread_call_once(&s_thread_once_flag, s_do_osx_loads, NULL); - int ret_val = 0; - - if (s_gettime_fn) { - struct timespec ts; - ret_val = s_gettime_fn(CLOCK_REALTIME, &ts); - if (ret_val) { - return aws_raise_error(AWS_ERROR_CLOCK_FAILURE); - } - + int ret_val = 0; + + if (s_gettime_fn) { + struct timespec ts; + ret_val = s_gettime_fn(CLOCK_REALTIME, &ts); + if (ret_val) { + return aws_raise_error(AWS_ERROR_CLOCK_FAILURE); + } + uint64_t secs = (uint64_t)ts.tv_sec; uint64_t n_secs = (uint64_t)ts.tv_nsec; *timestamp = (secs * NS_PER_SEC) + n_secs; - return AWS_OP_SUCCESS; - } - return s_legacy_get_time(timestamp); -} -# else -int aws_high_res_clock_get_ticks(uint64_t *timestamp) { - return s_legacy_get_time(timestamp); -} - -int aws_sys_clock_get_ticks(uint64_t *timestamp) { - return s_legacy_get_time(timestamp); -} - -# endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= 101200 */ -/* Everywhere else, just link clock_gettime in directly */ -#else -int aws_high_res_clock_get_ticks(uint64_t *timestamp) { - int ret_val = 0; - - struct timespec ts; - - ret_val = clock_gettime(HIGH_RES_CLOCK, &ts); - - if (ret_val) { - return aws_raise_error(AWS_ERROR_CLOCK_FAILURE); - } - + return AWS_OP_SUCCESS; + } + return s_legacy_get_time(timestamp); +} +# else +int aws_high_res_clock_get_ticks(uint64_t *timestamp) { + return s_legacy_get_time(timestamp); +} + +int aws_sys_clock_get_ticks(uint64_t *timestamp) { + return s_legacy_get_time(timestamp); +} + +# endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= 101200 */ +/* Everywhere else, just link clock_gettime in directly */ +#else +int aws_high_res_clock_get_ticks(uint64_t *timestamp) { + int ret_val = 0; + + struct timespec ts; + + ret_val = clock_gettime(HIGH_RES_CLOCK, &ts); + + if (ret_val) { + return aws_raise_error(AWS_ERROR_CLOCK_FAILURE); + } + uint64_t secs = (uint64_t)ts.tv_sec; uint64_t n_secs = (uint64_t)ts.tv_nsec; *timestamp = (secs * NS_PER_SEC) + n_secs; - return AWS_OP_SUCCESS; -} - -int aws_sys_clock_get_ticks(uint64_t *timestamp) { - int ret_val = 0; - - struct timespec ts; - ret_val = clock_gettime(CLOCK_REALTIME, &ts); - if (ret_val) { - return aws_raise_error(AWS_ERROR_CLOCK_FAILURE); - } - + return AWS_OP_SUCCESS; +} + +int aws_sys_clock_get_ticks(uint64_t *timestamp) { + int ret_val = 0; + + struct timespec ts; + ret_val = clock_gettime(CLOCK_REALTIME, &ts); + if (ret_val) { + return aws_raise_error(AWS_ERROR_CLOCK_FAILURE); + } + uint64_t secs = (uint64_t)ts.tv_sec; uint64_t n_secs = (uint64_t)ts.tv_nsec; *timestamp = (secs * NS_PER_SEC) + n_secs; - return AWS_OP_SUCCESS; -} -#endif /* defined(__MACH__) */ + return AWS_OP_SUCCESS; +} +#endif /* defined(__MACH__) */ diff --git a/contrib/restricted/aws/aws-c-common/source/posix/condition_variable.c b/contrib/restricted/aws/aws-c-common/source/posix/condition_variable.c index ca321c6bfa..b4914e919b 100644 --- a/contrib/restricted/aws/aws-c-common/source/posix/condition_variable.c +++ b/contrib/restricted/aws/aws-c-common/source/posix/condition_variable.c @@ -1,39 +1,39 @@ /** * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0. - */ - -#include <aws/common/condition_variable.h> - -#include <aws/common/clock.h> -#include <aws/common/mutex.h> - -#include <errno.h> - -static int process_error_code(int err) { - switch (err) { - case ENOMEM: - return aws_raise_error(AWS_ERROR_OOM); - case ETIMEDOUT: - return aws_raise_error(AWS_ERROR_COND_VARIABLE_TIMED_OUT); - default: - return aws_raise_error(AWS_ERROR_COND_VARIABLE_ERROR_UNKNOWN); - } -} - -int aws_condition_variable_init(struct aws_condition_variable *condition_variable) { + */ + +#include <aws/common/condition_variable.h> + +#include <aws/common/clock.h> +#include <aws/common/mutex.h> + +#include <errno.h> + +static int process_error_code(int err) { + switch (err) { + case ENOMEM: + return aws_raise_error(AWS_ERROR_OOM); + case ETIMEDOUT: + return aws_raise_error(AWS_ERROR_COND_VARIABLE_TIMED_OUT); + default: + return aws_raise_error(AWS_ERROR_COND_VARIABLE_ERROR_UNKNOWN); + } +} + +int aws_condition_variable_init(struct aws_condition_variable *condition_variable) { AWS_PRECONDITION(condition_variable); - if (pthread_cond_init(&condition_variable->condition_handle, NULL)) { + if (pthread_cond_init(&condition_variable->condition_handle, NULL)) { AWS_ZERO_STRUCT(*condition_variable); - return aws_raise_error(AWS_ERROR_COND_VARIABLE_INIT_FAILED); - } - + return aws_raise_error(AWS_ERROR_COND_VARIABLE_INIT_FAILED); + } + condition_variable->initialized = true; - return AWS_OP_SUCCESS; -} - -void aws_condition_variable_clean_up(struct aws_condition_variable *condition_variable) { + return AWS_OP_SUCCESS; +} + +void aws_condition_variable_clean_up(struct aws_condition_variable *condition_variable) { AWS_PRECONDITION(condition_variable); if (condition_variable->initialized) { @@ -41,71 +41,71 @@ void aws_condition_variable_clean_up(struct aws_condition_variable *condition_va } AWS_ZERO_STRUCT(*condition_variable); -} - -int aws_condition_variable_notify_one(struct aws_condition_variable *condition_variable) { +} + +int aws_condition_variable_notify_one(struct aws_condition_variable *condition_variable) { AWS_PRECONDITION(condition_variable && condition_variable->initialized); - int err_code = pthread_cond_signal(&condition_variable->condition_handle); - - if (err_code) { - return process_error_code(err_code); - } - - return AWS_OP_SUCCESS; -} - -int aws_condition_variable_notify_all(struct aws_condition_variable *condition_variable) { + int err_code = pthread_cond_signal(&condition_variable->condition_handle); + + if (err_code) { + return process_error_code(err_code); + } + + return AWS_OP_SUCCESS; +} + +int aws_condition_variable_notify_all(struct aws_condition_variable *condition_variable) { AWS_PRECONDITION(condition_variable && condition_variable->initialized); - int err_code = pthread_cond_broadcast(&condition_variable->condition_handle); - - if (err_code) { - return process_error_code(err_code); - } - - return AWS_OP_SUCCESS; -} - -int aws_condition_variable_wait(struct aws_condition_variable *condition_variable, struct aws_mutex *mutex) { + int err_code = pthread_cond_broadcast(&condition_variable->condition_handle); + + if (err_code) { + return process_error_code(err_code); + } + + return AWS_OP_SUCCESS; +} + +int aws_condition_variable_wait(struct aws_condition_variable *condition_variable, struct aws_mutex *mutex) { AWS_PRECONDITION(condition_variable && condition_variable->initialized); AWS_PRECONDITION(mutex && mutex->initialized); - int err_code = pthread_cond_wait(&condition_variable->condition_handle, &mutex->mutex_handle); - - if (err_code) { - return process_error_code(err_code); - } - - return AWS_OP_SUCCESS; -} - -int aws_condition_variable_wait_for( - struct aws_condition_variable *condition_variable, - struct aws_mutex *mutex, - int64_t time_to_wait) { - + int err_code = pthread_cond_wait(&condition_variable->condition_handle, &mutex->mutex_handle); + + if (err_code) { + return process_error_code(err_code); + } + + return AWS_OP_SUCCESS; +} + +int aws_condition_variable_wait_for( + struct aws_condition_variable *condition_variable, + struct aws_mutex *mutex, + int64_t time_to_wait) { + AWS_PRECONDITION(condition_variable && condition_variable->initialized); AWS_PRECONDITION(mutex && mutex->initialized); - uint64_t current_sys_time = 0; - if (aws_sys_clock_get_ticks(¤t_sys_time)) { - return AWS_OP_ERR; - } - - time_to_wait += current_sys_time; - - struct timespec ts; - uint64_t remainder = 0; - ts.tv_sec = - (time_t)aws_timestamp_convert((uint64_t)time_to_wait, AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_SECS, &remainder); - ts.tv_nsec = (long)remainder; - - int err_code = pthread_cond_timedwait(&condition_variable->condition_handle, &mutex->mutex_handle, &ts); - - if (err_code) { - return process_error_code(err_code); - } - - return AWS_OP_SUCCESS; -} + uint64_t current_sys_time = 0; + if (aws_sys_clock_get_ticks(¤t_sys_time)) { + return AWS_OP_ERR; + } + + time_to_wait += current_sys_time; + + struct timespec ts; + uint64_t remainder = 0; + ts.tv_sec = + (time_t)aws_timestamp_convert((uint64_t)time_to_wait, AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_SECS, &remainder); + ts.tv_nsec = (long)remainder; + + int err_code = pthread_cond_timedwait(&condition_variable->condition_handle, &mutex->mutex_handle, &ts); + + if (err_code) { + return process_error_code(err_code); + } + + return AWS_OP_SUCCESS; +} diff --git a/contrib/restricted/aws/aws-c-common/source/posix/device_random.c b/contrib/restricted/aws/aws-c-common/source/posix/device_random.c index f446002231..995bb79aaf 100644 --- a/contrib/restricted/aws/aws-c-common/source/posix/device_random.c +++ b/contrib/restricted/aws/aws-c-common/source/posix/device_random.c @@ -1,57 +1,57 @@ /** * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0. - */ -#include <aws/common/device_random.h> - -#include <aws/common/byte_buf.h> -#include <aws/common/thread.h> - -#include <fcntl.h> -#include <unistd.h> - -static int s_rand_fd = -1; -static aws_thread_once s_rand_init = AWS_THREAD_ONCE_STATIC_INIT; - -#ifdef O_CLOEXEC -# define OPEN_FLAGS (O_RDONLY | O_CLOEXEC) -#else -# define OPEN_FLAGS (O_RDONLY) -#endif + */ +#include <aws/common/device_random.h> + +#include <aws/common/byte_buf.h> +#include <aws/common/thread.h> + +#include <fcntl.h> +#include <unistd.h> + +static int s_rand_fd = -1; +static aws_thread_once s_rand_init = AWS_THREAD_ONCE_STATIC_INIT; + +#ifdef O_CLOEXEC +# define OPEN_FLAGS (O_RDONLY | O_CLOEXEC) +#else +# define OPEN_FLAGS (O_RDONLY) +#endif static void s_init_rand(void *user_data) { (void)user_data; - s_rand_fd = open("/dev/urandom", OPEN_FLAGS); - - if (s_rand_fd == -1) { - s_rand_fd = open("/dev/urandom", O_RDONLY); - - if (s_rand_fd == -1) { - abort(); - } - } - - if (-1 == fcntl(s_rand_fd, F_SETFD, FD_CLOEXEC)) { - abort(); - } -} - -static int s_fallback_device_random_buffer(struct aws_byte_buf *output) { - + s_rand_fd = open("/dev/urandom", OPEN_FLAGS); + + if (s_rand_fd == -1) { + s_rand_fd = open("/dev/urandom", O_RDONLY); + + if (s_rand_fd == -1) { + abort(); + } + } + + if (-1 == fcntl(s_rand_fd, F_SETFD, FD_CLOEXEC)) { + abort(); + } +} + +static int s_fallback_device_random_buffer(struct aws_byte_buf *output) { + aws_thread_call_once(&s_rand_init, s_init_rand, NULL); - - size_t diff = output->capacity - output->len; - - ssize_t amount_read = read(s_rand_fd, output->buffer + output->len, diff); - - if (amount_read != diff) { - return aws_raise_error(AWS_ERROR_RANDOM_GEN_FAILED); - } - - output->len += diff; - - return AWS_OP_SUCCESS; -} - -int aws_device_random_buffer(struct aws_byte_buf *output) { - return s_fallback_device_random_buffer(output); -} + + size_t diff = output->capacity - output->len; + + ssize_t amount_read = read(s_rand_fd, output->buffer + output->len, diff); + + if (amount_read != diff) { + return aws_raise_error(AWS_ERROR_RANDOM_GEN_FAILED); + } + + output->len += diff; + + return AWS_OP_SUCCESS; +} + +int aws_device_random_buffer(struct aws_byte_buf *output) { + return s_fallback_device_random_buffer(output); +} diff --git a/contrib/restricted/aws/aws-c-common/source/posix/environment.c b/contrib/restricted/aws/aws-c-common/source/posix/environment.c index f4b69caea2..5bc7679d6e 100644 --- a/contrib/restricted/aws/aws-c-common/source/posix/environment.c +++ b/contrib/restricted/aws/aws-c-common/source/posix/environment.c @@ -1,45 +1,45 @@ /** * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0. - */ - -#include <aws/common/environment.h> - -#include <aws/common/string.h> -#include <stdlib.h> - -int aws_get_environment_value( - struct aws_allocator *allocator, - const struct aws_string *variable_name, - struct aws_string **value_out) { - + */ + +#include <aws/common/environment.h> + +#include <aws/common/string.h> +#include <stdlib.h> + +int aws_get_environment_value( + struct aws_allocator *allocator, + const struct aws_string *variable_name, + struct aws_string **value_out) { + const char *value = getenv(aws_string_c_str(variable_name)); - if (value == NULL) { - *value_out = NULL; - return AWS_OP_SUCCESS; - } - - *value_out = aws_string_new_from_c_str(allocator, value); - if (*value_out == NULL) { - return aws_raise_error(AWS_ERROR_ENVIRONMENT_GET); - } - - return AWS_OP_SUCCESS; -} - -int aws_set_environment_value(const struct aws_string *variable_name, const struct aws_string *value) { - + if (value == NULL) { + *value_out = NULL; + return AWS_OP_SUCCESS; + } + + *value_out = aws_string_new_from_c_str(allocator, value); + if (*value_out == NULL) { + return aws_raise_error(AWS_ERROR_ENVIRONMENT_GET); + } + + return AWS_OP_SUCCESS; +} + +int aws_set_environment_value(const struct aws_string *variable_name, const struct aws_string *value) { + if (setenv(aws_string_c_str(variable_name), aws_string_c_str(value), 1) != 0) { - return aws_raise_error(AWS_ERROR_ENVIRONMENT_SET); - } - - return AWS_OP_SUCCESS; -} - -int aws_unset_environment_value(const struct aws_string *variable_name) { + return aws_raise_error(AWS_ERROR_ENVIRONMENT_SET); + } + + return AWS_OP_SUCCESS; +} + +int aws_unset_environment_value(const struct aws_string *variable_name) { if (unsetenv(aws_string_c_str(variable_name)) != 0) { - return aws_raise_error(AWS_ERROR_ENVIRONMENT_UNSET); - } - - return AWS_OP_SUCCESS; -} + return aws_raise_error(AWS_ERROR_ENVIRONMENT_UNSET); + } + + return AWS_OP_SUCCESS; +} diff --git a/contrib/restricted/aws/aws-c-common/source/posix/mutex.c b/contrib/restricted/aws/aws-c-common/source/posix/mutex.c index 2cbf2db66c..adca71d8ff 100644 --- a/contrib/restricted/aws/aws-c-common/source/posix/mutex.c +++ b/contrib/restricted/aws/aws-c-common/source/posix/mutex.c @@ -1,53 +1,53 @@ /** * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0. - */ - -#include <aws/common/mutex.h> -#include <aws/common/posix/common.inl> - -#include <errno.h> - -void aws_mutex_clean_up(struct aws_mutex *mutex) { + */ + +#include <aws/common/mutex.h> +#include <aws/common/posix/common.inl> + +#include <errno.h> + +void aws_mutex_clean_up(struct aws_mutex *mutex) { AWS_PRECONDITION(mutex); if (mutex->initialized) { pthread_mutex_destroy(&mutex->mutex_handle); } AWS_ZERO_STRUCT(*mutex); -} - -int aws_mutex_init(struct aws_mutex *mutex) { +} + +int aws_mutex_init(struct aws_mutex *mutex) { AWS_PRECONDITION(mutex); - pthread_mutexattr_t attr; - int err_code = pthread_mutexattr_init(&attr); - int return_code = AWS_OP_SUCCESS; - - if (!err_code) { - if ((err_code = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL)) || - (err_code = pthread_mutex_init(&mutex->mutex_handle, &attr))) { - - return_code = aws_private_convert_and_raise_error_code(err_code); - } - pthread_mutexattr_destroy(&attr); - } else { - return_code = aws_private_convert_and_raise_error_code(err_code); - } - + pthread_mutexattr_t attr; + int err_code = pthread_mutexattr_init(&attr); + int return_code = AWS_OP_SUCCESS; + + if (!err_code) { + if ((err_code = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL)) || + (err_code = pthread_mutex_init(&mutex->mutex_handle, &attr))) { + + return_code = aws_private_convert_and_raise_error_code(err_code); + } + pthread_mutexattr_destroy(&attr); + } else { + return_code = aws_private_convert_and_raise_error_code(err_code); + } + mutex->initialized = (return_code == AWS_OP_SUCCESS); - return return_code; -} - -int aws_mutex_lock(struct aws_mutex *mutex) { + return return_code; +} + +int aws_mutex_lock(struct aws_mutex *mutex) { AWS_PRECONDITION(mutex && mutex->initialized); - return aws_private_convert_and_raise_error_code(pthread_mutex_lock(&mutex->mutex_handle)); -} - -int aws_mutex_try_lock(struct aws_mutex *mutex) { + return aws_private_convert_and_raise_error_code(pthread_mutex_lock(&mutex->mutex_handle)); +} + +int aws_mutex_try_lock(struct aws_mutex *mutex) { AWS_PRECONDITION(mutex && mutex->initialized); - return aws_private_convert_and_raise_error_code(pthread_mutex_trylock(&mutex->mutex_handle)); -} - -int aws_mutex_unlock(struct aws_mutex *mutex) { + return aws_private_convert_and_raise_error_code(pthread_mutex_trylock(&mutex->mutex_handle)); +} + +int aws_mutex_unlock(struct aws_mutex *mutex) { AWS_PRECONDITION(mutex && mutex->initialized); - return aws_private_convert_and_raise_error_code(pthread_mutex_unlock(&mutex->mutex_handle)); -} + return aws_private_convert_and_raise_error_code(pthread_mutex_unlock(&mutex->mutex_handle)); +} diff --git a/contrib/restricted/aws/aws-c-common/source/posix/rw_lock.c b/contrib/restricted/aws/aws-c-common/source/posix/rw_lock.c index 824477d6cf..94ebe1fbf2 100644 --- a/contrib/restricted/aws/aws-c-common/source/posix/rw_lock.c +++ b/contrib/restricted/aws/aws-c-common/source/posix/rw_lock.c @@ -1,49 +1,49 @@ /** * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0. - */ - -#include <aws/common/atomics.h> -#include <aws/common/rw_lock.h> - -#include <aws/common/posix/common.inl> - -int aws_rw_lock_init(struct aws_rw_lock *lock) { - - return aws_private_convert_and_raise_error_code(pthread_rwlock_init(&lock->lock_handle, NULL)); -} - -void aws_rw_lock_clean_up(struct aws_rw_lock *lock) { - - pthread_rwlock_destroy(&lock->lock_handle); -} - -int aws_rw_lock_rlock(struct aws_rw_lock *lock) { - - return aws_private_convert_and_raise_error_code(pthread_rwlock_rdlock(&lock->lock_handle)); -} - -int aws_rw_lock_wlock(struct aws_rw_lock *lock) { - - return aws_private_convert_and_raise_error_code(pthread_rwlock_wrlock(&lock->lock_handle)); -} - -int aws_rw_lock_try_rlock(struct aws_rw_lock *lock) { - - return aws_private_convert_and_raise_error_code(pthread_rwlock_tryrdlock(&lock->lock_handle)); -} - -int aws_rw_lock_try_wlock(struct aws_rw_lock *lock) { - - return aws_private_convert_and_raise_error_code(pthread_rwlock_trywrlock(&lock->lock_handle)); -} - -int aws_rw_lock_runlock(struct aws_rw_lock *lock) { - - return aws_private_convert_and_raise_error_code(pthread_rwlock_unlock(&lock->lock_handle)); -} - -int aws_rw_lock_wunlock(struct aws_rw_lock *lock) { - - return aws_private_convert_and_raise_error_code(pthread_rwlock_unlock(&lock->lock_handle)); -} + */ + +#include <aws/common/atomics.h> +#include <aws/common/rw_lock.h> + +#include <aws/common/posix/common.inl> + +int aws_rw_lock_init(struct aws_rw_lock *lock) { + + return aws_private_convert_and_raise_error_code(pthread_rwlock_init(&lock->lock_handle, NULL)); +} + +void aws_rw_lock_clean_up(struct aws_rw_lock *lock) { + + pthread_rwlock_destroy(&lock->lock_handle); +} + +int aws_rw_lock_rlock(struct aws_rw_lock *lock) { + + return aws_private_convert_and_raise_error_code(pthread_rwlock_rdlock(&lock->lock_handle)); +} + +int aws_rw_lock_wlock(struct aws_rw_lock *lock) { + + return aws_private_convert_and_raise_error_code(pthread_rwlock_wrlock(&lock->lock_handle)); +} + +int aws_rw_lock_try_rlock(struct aws_rw_lock *lock) { + + return aws_private_convert_and_raise_error_code(pthread_rwlock_tryrdlock(&lock->lock_handle)); +} + +int aws_rw_lock_try_wlock(struct aws_rw_lock *lock) { + + return aws_private_convert_and_raise_error_code(pthread_rwlock_trywrlock(&lock->lock_handle)); +} + +int aws_rw_lock_runlock(struct aws_rw_lock *lock) { + + return aws_private_convert_and_raise_error_code(pthread_rwlock_unlock(&lock->lock_handle)); +} + +int aws_rw_lock_wunlock(struct aws_rw_lock *lock) { + + return aws_private_convert_and_raise_error_code(pthread_rwlock_unlock(&lock->lock_handle)); +} diff --git a/contrib/restricted/aws/aws-c-common/source/posix/system_info.c b/contrib/restricted/aws/aws-c-common/source/posix/system_info.c index 1311be4096..5fae2812ad 100644 --- a/contrib/restricted/aws/aws-c-common/source/posix/system_info.c +++ b/contrib/restricted/aws/aws-c-common/source/posix/system_info.c @@ -1,41 +1,41 @@ /** * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0. - */ - -#include <aws/common/system_info.h> - + */ + +#include <aws/common/system_info.h> + #include <aws/common/byte_buf.h> #include <aws/common/logging.h> #include <aws/common/platform.h> -#if defined(__FreeBSD__) || defined(__NetBSD__) -# define __BSD_VISIBLE 1 -#endif - -#include <unistd.h> - -#if defined(HAVE_SYSCONF) -size_t aws_system_info_processor_count(void) { - long nprocs = sysconf(_SC_NPROCESSORS_ONLN); - if (AWS_LIKELY(nprocs >= 0)) { - return (size_t)nprocs; - } - +#if defined(__FreeBSD__) || defined(__NetBSD__) +# define __BSD_VISIBLE 1 +#endif + +#include <unistd.h> + +#if defined(HAVE_SYSCONF) +size_t aws_system_info_processor_count(void) { + long nprocs = sysconf(_SC_NPROCESSORS_ONLN); + if (AWS_LIKELY(nprocs >= 0)) { + return (size_t)nprocs; + } + AWS_FATAL_POSTCONDITION(nprocs >= 0); - return 0; -} -#else -size_t aws_system_info_processor_count(void) { -# if defined(AWS_NUM_CPU_CORES) + return 0; +} +#else +size_t aws_system_info_processor_count(void) { +# if defined(AWS_NUM_CPU_CORES) AWS_FATAL_PRECONDITION(AWS_NUM_CPU_CORES > 0); - return AWS_NUM_CPU_CORES; -# else - return 1; -# endif -} -#endif - + return AWS_NUM_CPU_CORES; +# else + return 1; +# endif +} +#endif + #include <ctype.h> #include <fcntl.h> @@ -72,37 +72,37 @@ bool aws_is_debugger_present(void) { return false; } -#include <signal.h> - -#ifndef __has_builtin -# define __has_builtin(x) 0 -#endif - -void aws_debug_break(void) { -#ifdef DEBUG_BUILD +#include <signal.h> + +#ifndef __has_builtin +# define __has_builtin(x) 0 +#endif + +void aws_debug_break(void) { +#ifdef DEBUG_BUILD if (aws_is_debugger_present()) { -# if __has_builtin(__builtin_debugtrap) +# if __has_builtin(__builtin_debugtrap) __builtin_debugtrap(); -# else +# else raise(SIGTRAP); -# endif +# endif } -#endif /* DEBUG_BUILD */ -} - -#if defined(AWS_HAVE_EXECINFO) -# include <execinfo.h> -# include <limits.h> - -# define AWS_BACKTRACE_DEPTH 128 - -struct aws_stack_frame_info { - char exe[PATH_MAX]; - char addr[32]; - char base[32]; /* base addr for dylib/exe */ - char function[128]; -}; - +#endif /* DEBUG_BUILD */ +} + +#if defined(AWS_HAVE_EXECINFO) +# include <execinfo.h> +# include <limits.h> + +# define AWS_BACKTRACE_DEPTH 128 + +struct aws_stack_frame_info { + char exe[PATH_MAX]; + char addr[32]; + char base[32]; /* base addr for dylib/exe */ + char function[128]; +}; + /* Ensure only safe characters in a path buffer in case someone tries to rename the exe and trigger shell execution via the sub commands used to resolve symbols */ @@ -119,95 +119,95 @@ char *s_whitelist_chars(char *path) { return path; } -# if defined(__APPLE__) -# include <ctype.h> -# include <dlfcn.h> -# include <mach-o/dyld.h> -static char s_exe_path[PATH_MAX]; -const char *s_get_executable_path() { - static const char *s_exe = NULL; - if (AWS_LIKELY(s_exe)) { - return s_exe; - } - uint32_t len = sizeof(s_exe_path); - if (!_NSGetExecutablePath(s_exe_path, &len)) { - s_exe = s_exe_path; - } - return s_exe; -} -int s_parse_symbol(const char *symbol, void *addr, struct aws_stack_frame_info *frame) { - /* symbols look like: <frame_idx> <exe-or-shared-lib> <addr> <function> + <offset> - */ - const char *current_exe = s_get_executable_path(); - /* parse exe/shared lib */ - const char *exe_start = strstr(symbol, " "); +# if defined(__APPLE__) +# include <ctype.h> +# include <dlfcn.h> +# include <mach-o/dyld.h> +static char s_exe_path[PATH_MAX]; +const char *s_get_executable_path() { + static const char *s_exe = NULL; + if (AWS_LIKELY(s_exe)) { + return s_exe; + } + uint32_t len = sizeof(s_exe_path); + if (!_NSGetExecutablePath(s_exe_path, &len)) { + s_exe = s_exe_path; + } + return s_exe; +} +int s_parse_symbol(const char *symbol, void *addr, struct aws_stack_frame_info *frame) { + /* symbols look like: <frame_idx> <exe-or-shared-lib> <addr> <function> + <offset> + */ + const char *current_exe = s_get_executable_path(); + /* parse exe/shared lib */ + const char *exe_start = strstr(symbol, " "); while (aws_isspace(*exe_start)) { - ++exe_start; - } - const char *exe_end = strstr(exe_start, " "); - strncpy(frame->exe, exe_start, exe_end - exe_start); - /* executables get basename'd, so restore the path */ - if (strstr(current_exe, frame->exe)) { - strncpy(frame->exe, current_exe, strlen(current_exe)); + ++exe_start; } + const char *exe_end = strstr(exe_start, " "); + strncpy(frame->exe, exe_start, exe_end - exe_start); + /* executables get basename'd, so restore the path */ + if (strstr(current_exe, frame->exe)) { + strncpy(frame->exe, current_exe, strlen(current_exe)); + } s_whitelist_chars(frame->exe); - - /* parse addr */ - const char *addr_start = strstr(exe_end, "0x"); - const char *addr_end = strstr(addr_start, " "); - strncpy(frame->addr, addr_start, addr_end - addr_start); - - /* parse function */ - const char *function_start = strstr(addr_end, " ") + 1; - const char *function_end = strstr(function_start, " "); + + /* parse addr */ + const char *addr_start = strstr(exe_end, "0x"); + const char *addr_end = strstr(addr_start, " "); + strncpy(frame->addr, addr_start, addr_end - addr_start); + + /* parse function */ + const char *function_start = strstr(addr_end, " ") + 1; + const char *function_end = strstr(function_start, " "); /* truncate function name if needed */ size_t function_len = function_end - function_start; if (function_len >= (sizeof(frame->function) - 1)) { function_len = sizeof(frame->function) - 1; } - strncpy(frame->function, function_start, function_end - function_start); - - /* find base addr for library/exe */ - Dl_info addr_info; - dladdr(addr, &addr_info); - snprintf(frame->base, sizeof(frame->base), "0x%p", addr_info.dli_fbase); - - return AWS_OP_SUCCESS; -} - -void s_resolve_cmd(char *cmd, size_t len, struct aws_stack_frame_info *frame) { - snprintf(cmd, len, "atos -o %s -l %s %s", frame->exe, frame->base, frame->addr); -} -# else -int s_parse_symbol(const char *symbol, void *addr, struct aws_stack_frame_info *frame) { + strncpy(frame->function, function_start, function_end - function_start); + + /* find base addr for library/exe */ + Dl_info addr_info; + dladdr(addr, &addr_info); + snprintf(frame->base, sizeof(frame->base), "0x%p", addr_info.dli_fbase); + + return AWS_OP_SUCCESS; +} + +void s_resolve_cmd(char *cmd, size_t len, struct aws_stack_frame_info *frame) { + snprintf(cmd, len, "atos -o %s -l %s %s", frame->exe, frame->base, frame->addr); +} +# else +int s_parse_symbol(const char *symbol, void *addr, struct aws_stack_frame_info *frame) { /* symbols look like: <exe-or-shared-lib>(<function>+<addr>) [0x<addr>] - * or: <exe-or-shared-lib> [0x<addr>] + * or: <exe-or-shared-lib> [0x<addr>] * or: [0x<addr>] - */ - (void)addr; - const char *open_paren = strstr(symbol, "("); - const char *close_paren = strstr(symbol, ")"); - const char *exe_end = open_paren; - /* there may not be a function in parens, or parens at all */ - if (open_paren == NULL || close_paren == NULL) { + */ + (void)addr; + const char *open_paren = strstr(symbol, "("); + const char *close_paren = strstr(symbol, ")"); + const char *exe_end = open_paren; + /* there may not be a function in parens, or parens at all */ + if (open_paren == NULL || close_paren == NULL) { exe_end = strstr(symbol, "["); - if (!exe_end) { - return AWS_OP_ERR; - } + if (!exe_end) { + return AWS_OP_ERR; + } /* if exe_end == symbol, there's no exe */ if (exe_end != symbol) { exe_end -= 1; } - } - - ptrdiff_t exe_len = exe_end - symbol; + } + + ptrdiff_t exe_len = exe_end - symbol; if (exe_len > 0) { strncpy(frame->exe, symbol, exe_len); - } + } s_whitelist_chars(frame->exe); - - long function_len = (open_paren && close_paren) ? close_paren - open_paren - 1 : 0; - if (function_len > 0) { /* dynamic symbol was found */ + + long function_len = (open_paren && close_paren) ? close_paren - open_paren - 1 : 0; + if (function_len > 0) { /* dynamic symbol was found */ /* there might be (<function>+<addr>) or just (<function>) */ const char *function_start = open_paren + 1; const char *plus = strstr(function_start, "+"); @@ -219,7 +219,7 @@ int s_parse_symbol(const char *symbol, void *addr, struct aws_stack_frame_info * long addr_len = close_paren - plus - 1; strncpy(frame->addr, plus + 1, addr_len); } - } + } if (frame->addr[0] == 0) { /* use the address in []'s, since it's all we have */ const char *addr_start = strstr(exe_end, "[") + 1; @@ -229,14 +229,14 @@ int s_parse_symbol(const char *symbol, void *addr, struct aws_stack_frame_info * } strncpy(frame->addr, addr_start, addr_end - addr_start); } - - return AWS_OP_SUCCESS; -} -void s_resolve_cmd(char *cmd, size_t len, struct aws_stack_frame_info *frame) { - snprintf(cmd, len, "addr2line -afips -e %s %s", frame->exe, frame->addr); -} -# endif - + + return AWS_OP_SUCCESS; +} +void s_resolve_cmd(char *cmd, size_t len, struct aws_stack_frame_info *frame) { + snprintf(cmd, len, "addr2line -afips -e %s %s", frame->exe, frame->addr); +} +# endif + size_t aws_backtrace(void **stack_frames, size_t num_frames) { return backtrace(stack_frames, (int)aws_min_size(num_frames, INT_MAX)); } @@ -294,58 +294,58 @@ char **aws_backtrace_addr2line(void *const *stack_frames, size_t stack_depth) { return (char **)lines.buffer; /* caller is responsible for freeing */ } -void aws_backtrace_print(FILE *fp, void *call_site_data) { - siginfo_t *siginfo = call_site_data; - if (siginfo) { - fprintf(fp, "Signal received: %d, errno: %d\n", siginfo->si_signo, siginfo->si_errno); - if (siginfo->si_signo == SIGSEGV) { - fprintf(fp, " SIGSEGV @ 0x%p\n", siginfo->si_addr); - } - } - - void *stack_frames[AWS_BACKTRACE_DEPTH]; +void aws_backtrace_print(FILE *fp, void *call_site_data) { + siginfo_t *siginfo = call_site_data; + if (siginfo) { + fprintf(fp, "Signal received: %d, errno: %d\n", siginfo->si_signo, siginfo->si_errno); + if (siginfo->si_signo == SIGSEGV) { + fprintf(fp, " SIGSEGV @ 0x%p\n", siginfo->si_addr); + } + } + + void *stack_frames[AWS_BACKTRACE_DEPTH]; size_t stack_depth = aws_backtrace(stack_frames, AWS_BACKTRACE_DEPTH); char **symbols = aws_backtrace_symbols(stack_frames, stack_depth); - if (symbols == NULL) { - fprintf(fp, "Unable to decode backtrace via backtrace_symbols\n"); - return; - } - + if (symbols == NULL) { + fprintf(fp, "Unable to decode backtrace via backtrace_symbols\n"); + return; + } + fprintf(fp, "################################################################################\n"); fprintf(fp, "Resolved stacktrace:\n"); fprintf(fp, "################################################################################\n"); /* symbols look like: <exe-or-shared-lib>(<function>+<addr>) [0x<addr>] - * or: <exe-or-shared-lib> [0x<addr>] + * or: <exe-or-shared-lib> [0x<addr>] * or: [0x<addr>] - * start at 1 to skip the current frame (this function) */ + * start at 1 to skip the current frame (this function) */ for (size_t frame_idx = 1; frame_idx < stack_depth; ++frame_idx) { - struct aws_stack_frame_info frame; - AWS_ZERO_STRUCT(frame); - const char *symbol = symbols[frame_idx]; - if (s_parse_symbol(symbol, stack_frames[frame_idx], &frame)) { - goto parse_failed; - } - - /* TODO: Emulate libunwind */ - char cmd[sizeof(struct aws_stack_frame_info)] = {0}; - s_resolve_cmd(cmd, sizeof(cmd), &frame); - FILE *out = popen(cmd, "r"); - if (!out) { - goto parse_failed; - } - char output[1024]; - if (fgets(output, sizeof(output), out)) { - /* if addr2line or atos don't know what to do with an address, they just echo it */ - /* if there are spaces in the output, then they resolved something */ - if (strstr(output, " ")) { - symbol = output; - } - } - pclose(out); - - parse_failed: - fprintf(fp, "%s%s", symbol, (symbol == symbols[frame_idx]) ? "\n" : ""); - } + struct aws_stack_frame_info frame; + AWS_ZERO_STRUCT(frame); + const char *symbol = symbols[frame_idx]; + if (s_parse_symbol(symbol, stack_frames[frame_idx], &frame)) { + goto parse_failed; + } + + /* TODO: Emulate libunwind */ + char cmd[sizeof(struct aws_stack_frame_info)] = {0}; + s_resolve_cmd(cmd, sizeof(cmd), &frame); + FILE *out = popen(cmd, "r"); + if (!out) { + goto parse_failed; + } + char output[1024]; + if (fgets(output, sizeof(output), out)) { + /* if addr2line or atos don't know what to do with an address, they just echo it */ + /* if there are spaces in the output, then they resolved something */ + if (strstr(output, " ")) { + symbol = output; + } + } + pclose(out); + + parse_failed: + fprintf(fp, "%s%s", symbol, (symbol == symbols[frame_idx]) ? "\n" : ""); + } fprintf(fp, "################################################################################\n"); fprintf(fp, "Raw stacktrace:\n"); @@ -356,14 +356,14 @@ void aws_backtrace_print(FILE *fp, void *call_site_data) { } fflush(fp); - free(symbols); -} - -#else -void aws_backtrace_print(FILE *fp, void *call_site_data) { + free(symbols); +} + +#else +void aws_backtrace_print(FILE *fp, void *call_site_data) { (void)call_site_data; - fprintf(fp, "No call stack information available\n"); -} + fprintf(fp, "No call stack information available\n"); +} size_t aws_backtrace(void **stack_frames, size_t size) { (void)stack_frames; @@ -382,7 +382,7 @@ char **aws_backtrace_addr2line(void *const *stack_frames, size_t stack_depth) { (void)stack_depth; return NULL; } -#endif /* AWS_HAVE_EXECINFO */ +#endif /* AWS_HAVE_EXECINFO */ void aws_backtrace_log() { void *stack_frames[1024]; diff --git a/contrib/restricted/aws/aws-c-common/source/posix/thread.c b/contrib/restricted/aws/aws-c-common/source/posix/thread.c index 064d16882f..1ae2660f1a 100644 --- a/contrib/restricted/aws/aws-c-common/source/posix/thread.c +++ b/contrib/restricted/aws/aws-c-common/source/posix/thread.c @@ -1,57 +1,57 @@ /** * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0. - */ - + */ + #if !defined(__MACH__) # define _GNU_SOURCE #endif - -#include <aws/common/clock.h> + +#include <aws/common/clock.h> #include <aws/common/logging.h> #include <aws/common/private/dlloads.h> #include <aws/common/thread.h> - + #include <dlfcn.h> -#include <errno.h> +#include <errno.h> #include <inttypes.h> -#include <limits.h> +#include <limits.h> #include <sched.h> -#include <time.h> +#include <time.h> #include <unistd.h> - + #if defined(__FreeBSD__) || defined(__NETBSD__) # include <pthread_np.h> typedef cpuset_t cpu_set_t; #endif -static struct aws_thread_options s_default_options = { - /* this will make sure platform default stack size is used. */ +static struct aws_thread_options s_default_options = { + /* this will make sure platform default stack size is used. */ .stack_size = 0, .cpu_id = -1, }; - + struct thread_atexit_callback { aws_thread_atexit_fn *callback; void *user_data; struct thread_atexit_callback *next; }; -struct thread_wrapper { - struct aws_allocator *allocator; - void (*func)(void *arg); - void *arg; +struct thread_wrapper { + struct aws_allocator *allocator; + void (*func)(void *arg); + void *arg; struct thread_atexit_callback *atexit; void (*call_once)(void *); void *once_arg; struct aws_thread *thread; bool membind; -}; - +}; + static AWS_THREAD_LOCAL struct thread_wrapper *tl_wrapper = NULL; -static void *thread_fn(void *arg) { - struct thread_wrapper wrapper = *(struct thread_wrapper *)arg; +static void *thread_fn(void *arg) { + struct thread_wrapper wrapper = *(struct thread_wrapper *)arg; struct aws_allocator *allocator = wrapper.allocator; tl_wrapper = &wrapper; if (wrapper.membind && g_set_mempolicy_ptr) { @@ -73,7 +73,7 @@ static void *thread_fn(void *arg) { } } wrapper.func(wrapper.arg); - + struct thread_atexit_callback *exit_callback_data = wrapper.atexit; aws_mem_release(allocator, arg); @@ -89,23 +89,23 @@ static void *thread_fn(void *arg) { } tl_wrapper = NULL; - return NULL; -} - -const struct aws_thread_options *aws_default_thread_options(void) { - return &s_default_options; -} - -void aws_thread_clean_up(struct aws_thread *thread) { - if (thread->detach_state == AWS_THREAD_JOINABLE) { - pthread_detach(thread->thread_id); - } -} - + return NULL; +} + +const struct aws_thread_options *aws_default_thread_options(void) { + return &s_default_options; +} + +void aws_thread_clean_up(struct aws_thread *thread) { + if (thread->detach_state == AWS_THREAD_JOINABLE) { + pthread_detach(thread->thread_id); + } +} + static void s_call_once(void) { tl_wrapper->call_once(tl_wrapper->once_arg); -} - +} + void aws_thread_call_once(aws_thread_once *flag, void (*call_once)(void *), void *user_data) { // If this is a non-aws_thread, then gin up a temp thread wrapper struct thread_wrapper temp_wrapper; @@ -122,39 +122,39 @@ void aws_thread_call_once(aws_thread_once *flag, void (*call_once)(void *), void } } -int aws_thread_init(struct aws_thread *thread, struct aws_allocator *allocator) { +int aws_thread_init(struct aws_thread *thread, struct aws_allocator *allocator) { *thread = (struct aws_thread){.allocator = allocator, .detach_state = AWS_THREAD_NOT_CREATED}; - - return AWS_OP_SUCCESS; -} - -int aws_thread_launch( - struct aws_thread *thread, - void (*func)(void *arg), - void *arg, - const struct aws_thread_options *options) { - - pthread_attr_t attributes; - pthread_attr_t *attributes_ptr = NULL; - int attr_return = 0; - int allocation_failed = 0; - - if (options) { - attr_return = pthread_attr_init(&attributes); - - if (attr_return) { - goto cleanup; - } - - attributes_ptr = &attributes; - - if (options->stack_size > PTHREAD_STACK_MIN) { - attr_return = pthread_attr_setstacksize(attributes_ptr, options->stack_size); - - if (attr_return) { - goto cleanup; - } - } + + return AWS_OP_SUCCESS; +} + +int aws_thread_launch( + struct aws_thread *thread, + void (*func)(void *arg), + void *arg, + const struct aws_thread_options *options) { + + pthread_attr_t attributes; + pthread_attr_t *attributes_ptr = NULL; + int attr_return = 0; + int allocation_failed = 0; + + if (options) { + attr_return = pthread_attr_init(&attributes); + + if (attr_return) { + goto cleanup; + } + + attributes_ptr = &attributes; + + if (options->stack_size > PTHREAD_STACK_MIN) { + attr_return = pthread_attr_setstacksize(attributes_ptr, options->stack_size); + + if (attr_return) { + goto cleanup; + } + } /* AFAIK you can't set thread affinity on apple platforms, and it doesn't really matter since all memory * NUMA or not is setup in interleave mode. @@ -184,106 +184,106 @@ int aws_thread_launch( } } #endif /* !defined(__MACH__) && !defined(__ANDROID__) */ - } - - struct thread_wrapper *wrapper = + } + + struct thread_wrapper *wrapper = (struct thread_wrapper *)aws_mem_calloc(thread->allocator, 1, sizeof(struct thread_wrapper)); - - if (!wrapper) { - allocation_failed = 1; - goto cleanup; - } - + + if (!wrapper) { + allocation_failed = 1; + goto cleanup; + } + if (options && options->cpu_id >= 0) { wrapper->membind = true; } wrapper->thread = thread; - wrapper->allocator = thread->allocator; - wrapper->func = func; - wrapper->arg = arg; - attr_return = pthread_create(&thread->thread_id, attributes_ptr, thread_fn, (void *)wrapper); - - if (attr_return) { - goto cleanup; - } - - thread->detach_state = AWS_THREAD_JOINABLE; - -cleanup: - if (attributes_ptr) { - pthread_attr_destroy(attributes_ptr); - } - - if (attr_return == EINVAL) { - return aws_raise_error(AWS_ERROR_THREAD_INVALID_SETTINGS); - } - - if (attr_return == EAGAIN) { - return aws_raise_error(AWS_ERROR_THREAD_INSUFFICIENT_RESOURCE); - } - - if (attr_return == EPERM) { - return aws_raise_error(AWS_ERROR_THREAD_NO_PERMISSIONS); - } - - if (allocation_failed || attr_return == ENOMEM) { - return aws_raise_error(AWS_ERROR_OOM); - } - - return AWS_OP_SUCCESS; -} - + wrapper->allocator = thread->allocator; + wrapper->func = func; + wrapper->arg = arg; + attr_return = pthread_create(&thread->thread_id, attributes_ptr, thread_fn, (void *)wrapper); + + if (attr_return) { + goto cleanup; + } + + thread->detach_state = AWS_THREAD_JOINABLE; + +cleanup: + if (attributes_ptr) { + pthread_attr_destroy(attributes_ptr); + } + + if (attr_return == EINVAL) { + return aws_raise_error(AWS_ERROR_THREAD_INVALID_SETTINGS); + } + + if (attr_return == EAGAIN) { + return aws_raise_error(AWS_ERROR_THREAD_INSUFFICIENT_RESOURCE); + } + + if (attr_return == EPERM) { + return aws_raise_error(AWS_ERROR_THREAD_NO_PERMISSIONS); + } + + if (allocation_failed || attr_return == ENOMEM) { + return aws_raise_error(AWS_ERROR_OOM); + } + + return AWS_OP_SUCCESS; +} + aws_thread_id_t aws_thread_get_id(struct aws_thread *thread) { return thread->thread_id; -} - -enum aws_thread_detach_state aws_thread_get_detach_state(struct aws_thread *thread) { - return thread->detach_state; -} - -int aws_thread_join(struct aws_thread *thread) { - if (thread->detach_state == AWS_THREAD_JOINABLE) { - int err_no = pthread_join(thread->thread_id, 0); - - if (err_no) { - if (err_no == EINVAL) { - return aws_raise_error(AWS_ERROR_THREAD_NOT_JOINABLE); - } - if (err_no == ESRCH) { - return aws_raise_error(AWS_ERROR_THREAD_NO_SUCH_THREAD_ID); - } - if (err_no == EDEADLK) { - return aws_raise_error(AWS_ERROR_THREAD_DEADLOCK_DETECTED); - } - } - - thread->detach_state = AWS_THREAD_JOIN_COMPLETED; - } - - return AWS_OP_SUCCESS; -} - +} + +enum aws_thread_detach_state aws_thread_get_detach_state(struct aws_thread *thread) { + return thread->detach_state; +} + +int aws_thread_join(struct aws_thread *thread) { + if (thread->detach_state == AWS_THREAD_JOINABLE) { + int err_no = pthread_join(thread->thread_id, 0); + + if (err_no) { + if (err_no == EINVAL) { + return aws_raise_error(AWS_ERROR_THREAD_NOT_JOINABLE); + } + if (err_no == ESRCH) { + return aws_raise_error(AWS_ERROR_THREAD_NO_SUCH_THREAD_ID); + } + if (err_no == EDEADLK) { + return aws_raise_error(AWS_ERROR_THREAD_DEADLOCK_DETECTED); + } + } + + thread->detach_state = AWS_THREAD_JOIN_COMPLETED; + } + + return AWS_OP_SUCCESS; +} + aws_thread_id_t aws_thread_current_thread_id(void) { return pthread_self(); -} - +} + bool aws_thread_thread_id_equal(aws_thread_id_t t1, aws_thread_id_t t2) { return pthread_equal(t1, t2) != 0; } -void aws_thread_current_sleep(uint64_t nanos) { - uint64_t nano = 0; - time_t seconds = (time_t)aws_timestamp_convert(nanos, AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_SECS, &nano); - - struct timespec tm = { - .tv_sec = seconds, - .tv_nsec = (long)nano, - }; - struct timespec output; - - nanosleep(&tm, &output); -} +void aws_thread_current_sleep(uint64_t nanos) { + uint64_t nano = 0; + time_t seconds = (time_t)aws_timestamp_convert(nanos, AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_SECS, &nano); + + struct timespec tm = { + .tv_sec = seconds, + .tv_nsec = (long)nano, + }; + struct timespec output; + + nanosleep(&tm, &output); +} int aws_thread_current_at_exit(aws_thread_atexit_fn *callback, void *user_data) { if (!tl_wrapper) { diff --git a/contrib/restricted/aws/aws-c-common/source/posix/time.c b/contrib/restricted/aws/aws-c-common/source/posix/time.c index dd49d6b0b6..73d35945c9 100644 --- a/contrib/restricted/aws/aws-c-common/source/posix/time.c +++ b/contrib/restricted/aws/aws-c-common/source/posix/time.c @@ -1,79 +1,79 @@ /** * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0. - */ -#include <aws/common/time.h> - -#if defined(__ANDROID__) && !defined(__LP64__) -/* - * This branch brought to you by the kind folks at google chromium. It's been modified a bit, but - * gotta give credit where it's due.... I'm not a lawyer so I'm just gonna drop their copyright - * notification here to avoid all of that. - */ - -/* - * Copyright 2014 The Chromium Authors. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * Neither the name of Google Inc. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - * From src/base/os_compat_android.cc: - */ -# include <time64.h> - -static const time_t s_time_max = ~(1L << ((sizeof(time_t) * __CHAR_BIT__ - 1))); -static const time_t s_time_min = (1L << ((sizeof(time_t)) * __CHAR_BIT__ - 1)); - -/* 32-bit Android has only timegm64() and not timegm(). */ -time_t aws_timegm(struct tm *const t) { - - time64_t result = timegm64(t); - if (result < s_time_min || result > s_time_max) { - return -1; - } - return (time_t)result; -} - -#else - -# ifndef __APPLE__ -/* glibc.... you disappoint me.. */ -extern time_t timegm(struct tm *); -# endif - -time_t aws_timegm(struct tm *const t) { - return timegm(t); -} - -#endif /* defined(__ANDROID__) && !defined(__LP64__) */ - -void aws_localtime(time_t time, struct tm *t) { - localtime_r(&time, t); -} - -void aws_gmtime(time_t time, struct tm *t) { - gmtime_r(&time, t); -} + */ +#include <aws/common/time.h> + +#if defined(__ANDROID__) && !defined(__LP64__) +/* + * This branch brought to you by the kind folks at google chromium. It's been modified a bit, but + * gotta give credit where it's due.... I'm not a lawyer so I'm just gonna drop their copyright + * notification here to avoid all of that. + */ + +/* + * Copyright 2014 The Chromium Authors. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + * From src/base/os_compat_android.cc: + */ +# include <time64.h> + +static const time_t s_time_max = ~(1L << ((sizeof(time_t) * __CHAR_BIT__ - 1))); +static const time_t s_time_min = (1L << ((sizeof(time_t)) * __CHAR_BIT__ - 1)); + +/* 32-bit Android has only timegm64() and not timegm(). */ +time_t aws_timegm(struct tm *const t) { + + time64_t result = timegm64(t); + if (result < s_time_min || result > s_time_max) { + return -1; + } + return (time_t)result; +} + +#else + +# ifndef __APPLE__ +/* glibc.... you disappoint me.. */ +extern time_t timegm(struct tm *); +# endif + +time_t aws_timegm(struct tm *const t) { + return timegm(t); +} + +#endif /* defined(__ANDROID__) && !defined(__LP64__) */ + +void aws_localtime(time_t time, struct tm *t) { + localtime_r(&time, t); +} + +void aws_gmtime(time_t time, struct tm *t) { + gmtime_r(&time, t); +} diff --git a/contrib/restricted/aws/aws-c-common/source/priority_queue.c b/contrib/restricted/aws/aws-c-common/source/priority_queue.c index 14ff421d5f..a985a39252 100644 --- a/contrib/restricted/aws/aws-c-common/source/priority_queue.c +++ b/contrib/restricted/aws/aws-c-common/source/priority_queue.c @@ -1,161 +1,161 @@ /** * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0. - */ - -#include <aws/common/priority_queue.h> - -#include <string.h> - -#define PARENT_OF(index) (((index)&1) ? (index) >> 1 : (index) > 1 ? ((index)-2) >> 1 : 0) -#define LEFT_OF(index) (((index) << 1) + 1) -#define RIGHT_OF(index) (((index) << 1) + 2) - -static void s_swap(struct aws_priority_queue *queue, size_t a, size_t b) { + */ + +#include <aws/common/priority_queue.h> + +#include <string.h> + +#define PARENT_OF(index) (((index)&1) ? (index) >> 1 : (index) > 1 ? ((index)-2) >> 1 : 0) +#define LEFT_OF(index) (((index) << 1) + 1) +#define RIGHT_OF(index) (((index) << 1) + 2) + +static void s_swap(struct aws_priority_queue *queue, size_t a, size_t b) { AWS_PRECONDITION(aws_priority_queue_is_valid(queue)); AWS_PRECONDITION(a < queue->container.length); AWS_PRECONDITION(b < queue->container.length); AWS_PRECONDITION(aws_priority_queue_backpointer_index_valid(queue, a)); AWS_PRECONDITION(aws_priority_queue_backpointer_index_valid(queue, b)); - aws_array_list_swap(&queue->container, a, b); - - /* Invariant: If the backpointer array is initialized, we have enough room for all elements */ + aws_array_list_swap(&queue->container, a, b); + + /* Invariant: If the backpointer array is initialized, we have enough room for all elements */ if (!AWS_IS_ZEROED(queue->backpointers)) { - AWS_ASSERT(queue->backpointers.length > a); - AWS_ASSERT(queue->backpointers.length > b); - - struct aws_priority_queue_node **bp_a = &((struct aws_priority_queue_node **)queue->backpointers.data)[a]; - struct aws_priority_queue_node **bp_b = &((struct aws_priority_queue_node **)queue->backpointers.data)[b]; - - struct aws_priority_queue_node *tmp = *bp_a; - *bp_a = *bp_b; - *bp_b = tmp; - - if (*bp_a) { - (*bp_a)->current_index = a; - } - - if (*bp_b) { - (*bp_b)->current_index = b; - } - } + AWS_ASSERT(queue->backpointers.length > a); + AWS_ASSERT(queue->backpointers.length > b); + + struct aws_priority_queue_node **bp_a = &((struct aws_priority_queue_node **)queue->backpointers.data)[a]; + struct aws_priority_queue_node **bp_b = &((struct aws_priority_queue_node **)queue->backpointers.data)[b]; + + struct aws_priority_queue_node *tmp = *bp_a; + *bp_a = *bp_b; + *bp_b = tmp; + + if (*bp_a) { + (*bp_a)->current_index = a; + } + + if (*bp_b) { + (*bp_b)->current_index = b; + } + } AWS_POSTCONDITION(aws_priority_queue_is_valid(queue)); AWS_POSTCONDITION(aws_priority_queue_backpointer_index_valid(queue, a)); AWS_POSTCONDITION(aws_priority_queue_backpointer_index_valid(queue, b)); -} - -/* Precondition: with the exception of the given root element, the container must be - * in heap order */ -static bool s_sift_down(struct aws_priority_queue *queue, size_t root) { +} + +/* Precondition: with the exception of the given root element, the container must be + * in heap order */ +static bool s_sift_down(struct aws_priority_queue *queue, size_t root) { AWS_PRECONDITION(aws_priority_queue_is_valid(queue)); AWS_PRECONDITION(root < queue->container.length); - bool did_move = false; - - size_t len = aws_array_list_length(&queue->container); - - while (LEFT_OF(root) < len) { - size_t left = LEFT_OF(root); - size_t right = RIGHT_OF(root); - size_t first = root; - void *first_item = NULL, *other_item = NULL; - - aws_array_list_get_at_ptr(&queue->container, &first_item, root); - aws_array_list_get_at_ptr(&queue->container, &other_item, left); - - if (queue->pred(first_item, other_item) > 0) { - first = left; - first_item = other_item; - } - - if (right < len) { - aws_array_list_get_at_ptr(&queue->container, &other_item, right); - - /* choose the larger/smaller of the two in case of a max/min heap - * respectively */ - if (queue->pred(first_item, other_item) > 0) { - first = right; - first_item = other_item; - } - } - - if (first != root) { - s_swap(queue, first, root); - did_move = true; - root = first; - } else { - break; - } - } - + bool did_move = false; + + size_t len = aws_array_list_length(&queue->container); + + while (LEFT_OF(root) < len) { + size_t left = LEFT_OF(root); + size_t right = RIGHT_OF(root); + size_t first = root; + void *first_item = NULL, *other_item = NULL; + + aws_array_list_get_at_ptr(&queue->container, &first_item, root); + aws_array_list_get_at_ptr(&queue->container, &other_item, left); + + if (queue->pred(first_item, other_item) > 0) { + first = left; + first_item = other_item; + } + + if (right < len) { + aws_array_list_get_at_ptr(&queue->container, &other_item, right); + + /* choose the larger/smaller of the two in case of a max/min heap + * respectively */ + if (queue->pred(first_item, other_item) > 0) { + first = right; + first_item = other_item; + } + } + + if (first != root) { + s_swap(queue, first, root); + did_move = true; + root = first; + } else { + break; + } + } + AWS_POSTCONDITION(aws_priority_queue_is_valid(queue)); - return did_move; -} - -/* Precondition: Elements prior to the specified index must be in heap order. */ -static bool s_sift_up(struct aws_priority_queue *queue, size_t index) { + return did_move; +} + +/* Precondition: Elements prior to the specified index must be in heap order. */ +static bool s_sift_up(struct aws_priority_queue *queue, size_t index) { AWS_PRECONDITION(aws_priority_queue_is_valid(queue)); AWS_PRECONDITION(index < queue->container.length); - bool did_move = false; - - void *parent_item, *child_item; - size_t parent = PARENT_OF(index); - while (index) { - /* - * These get_ats are guaranteed to be successful; if they are not, we have - * serious state corruption, so just abort. - */ - - if (aws_array_list_get_at_ptr(&queue->container, &parent_item, parent) || - aws_array_list_get_at_ptr(&queue->container, &child_item, index)) { - abort(); - } - - if (queue->pred(parent_item, child_item) > 0) { - s_swap(queue, index, parent); - did_move = true; - index = parent; - parent = PARENT_OF(index); - } else { - break; - } - } - + bool did_move = false; + + void *parent_item, *child_item; + size_t parent = PARENT_OF(index); + while (index) { + /* + * These get_ats are guaranteed to be successful; if they are not, we have + * serious state corruption, so just abort. + */ + + if (aws_array_list_get_at_ptr(&queue->container, &parent_item, parent) || + aws_array_list_get_at_ptr(&queue->container, &child_item, index)) { + abort(); + } + + if (queue->pred(parent_item, child_item) > 0) { + s_swap(queue, index, parent); + did_move = true; + index = parent; + parent = PARENT_OF(index); + } else { + break; + } + } + AWS_POSTCONDITION(aws_priority_queue_is_valid(queue)); - return did_move; -} - -/* - * Precondition: With the exception of the given index, the heap condition holds for all elements. - * In particular, the parent of the current index is a predecessor of all children of the current index. - */ -static void s_sift_either(struct aws_priority_queue *queue, size_t index) { + return did_move; +} + +/* + * Precondition: With the exception of the given index, the heap condition holds for all elements. + * In particular, the parent of the current index is a predecessor of all children of the current index. + */ +static void s_sift_either(struct aws_priority_queue *queue, size_t index) { AWS_PRECONDITION(aws_priority_queue_is_valid(queue)); AWS_PRECONDITION(index < queue->container.length); - if (!index || !s_sift_up(queue, index)) { - s_sift_down(queue, index); - } + if (!index || !s_sift_up(queue, index)) { + s_sift_down(queue, index); + } AWS_POSTCONDITION(aws_priority_queue_is_valid(queue)); -} - -int aws_priority_queue_init_dynamic( - struct aws_priority_queue *queue, - struct aws_allocator *alloc, - size_t default_size, - size_t item_size, - aws_priority_queue_compare_fn *pred) { - +} + +int aws_priority_queue_init_dynamic( + struct aws_priority_queue *queue, + struct aws_allocator *alloc, + size_t default_size, + size_t item_size, + aws_priority_queue_compare_fn *pred) { + AWS_FATAL_PRECONDITION(queue != NULL); AWS_FATAL_PRECONDITION(alloc != NULL); AWS_FATAL_PRECONDITION(item_size > 0); - queue->pred = pred; - AWS_ZERO_STRUCT(queue->backpointers); - + queue->pred = pred; + AWS_ZERO_STRUCT(queue->backpointers); + int ret = aws_array_list_init_dynamic(&queue->container, alloc, default_size, item_size); if (ret == AWS_OP_SUCCESS) { AWS_POSTCONDITION(aws_priority_queue_is_valid(queue)); @@ -164,28 +164,28 @@ int aws_priority_queue_init_dynamic( AWS_POSTCONDITION(AWS_IS_ZEROED(queue->backpointers)); } return ret; -} - -void aws_priority_queue_init_static( - struct aws_priority_queue *queue, - void *heap, - size_t item_count, - size_t item_size, - aws_priority_queue_compare_fn *pred) { - +} + +void aws_priority_queue_init_static( + struct aws_priority_queue *queue, + void *heap, + size_t item_count, + size_t item_size, + aws_priority_queue_compare_fn *pred) { + AWS_FATAL_PRECONDITION(queue != NULL); AWS_FATAL_PRECONDITION(heap != NULL); AWS_FATAL_PRECONDITION(item_count > 0); AWS_FATAL_PRECONDITION(item_size > 0); - queue->pred = pred; - AWS_ZERO_STRUCT(queue->backpointers); - - aws_array_list_init_static(&queue->container, heap, item_count, item_size); + queue->pred = pred; + AWS_ZERO_STRUCT(queue->backpointers); + + aws_array_list_init_static(&queue->container, heap, item_count, item_size); AWS_POSTCONDITION(aws_priority_queue_is_valid(queue)); -} - +} + bool aws_priority_queue_backpointer_index_valid(const struct aws_priority_queue *const queue, size_t index) { if (AWS_IS_ZEROED(queue->backpointers)) { return true; @@ -243,105 +243,105 @@ bool aws_priority_queue_backpointers_valid(const struct aws_priority_queue *cons return ((backpointer_list_is_valid && backpointer_struct_is_valid) || AWS_IS_ZEROED(queue->backpointers)); } -bool aws_priority_queue_is_valid(const struct aws_priority_queue *const queue) { +bool aws_priority_queue_is_valid(const struct aws_priority_queue *const queue) { /* Pointer validity checks */ - if (!queue) { - return false; - } - bool pred_is_valid = (queue->pred != NULL); - bool container_is_valid = aws_array_list_is_valid(&queue->container); + if (!queue) { + return false; + } + bool pred_is_valid = (queue->pred != NULL); + bool container_is_valid = aws_array_list_is_valid(&queue->container); bool backpointers_valid = aws_priority_queue_backpointers_valid(queue); return pred_is_valid && container_is_valid && backpointers_valid; -} - -void aws_priority_queue_clean_up(struct aws_priority_queue *queue) { - aws_array_list_clean_up(&queue->container); +} + +void aws_priority_queue_clean_up(struct aws_priority_queue *queue) { + aws_array_list_clean_up(&queue->container); if (!AWS_IS_ZEROED(queue->backpointers)) { aws_array_list_clean_up(&queue->backpointers); } -} - -int aws_priority_queue_push(struct aws_priority_queue *queue, void *item) { +} + +int aws_priority_queue_push(struct aws_priority_queue *queue, void *item) { AWS_PRECONDITION(aws_priority_queue_is_valid(queue)); AWS_PRECONDITION(item && AWS_MEM_IS_READABLE(item, queue->container.item_size)); int rval = aws_priority_queue_push_ref(queue, item, NULL); AWS_POSTCONDITION(aws_priority_queue_is_valid(queue)); return rval; -} - -int aws_priority_queue_push_ref( - struct aws_priority_queue *queue, - void *item, - struct aws_priority_queue_node *backpointer) { +} + +int aws_priority_queue_push_ref( + struct aws_priority_queue *queue, + void *item, + struct aws_priority_queue_node *backpointer) { AWS_PRECONDITION(aws_priority_queue_is_valid(queue)); AWS_PRECONDITION(item && AWS_MEM_IS_READABLE(item, queue->container.item_size)); - int err = aws_array_list_push_back(&queue->container, item); - if (err) { + int err = aws_array_list_push_back(&queue->container, item); + if (err) { AWS_POSTCONDITION(aws_priority_queue_is_valid(queue)); - return err; - } - size_t index = aws_array_list_length(&queue->container) - 1; - - if (backpointer && !queue->backpointers.alloc) { - if (!queue->container.alloc) { - aws_raise_error(AWS_ERROR_UNSUPPORTED_OPERATION); - goto backpointer_update_failed; - } - - if (aws_array_list_init_dynamic( - &queue->backpointers, queue->container.alloc, index + 1, sizeof(struct aws_priority_queue_node *))) { - goto backpointer_update_failed; - } - - /* When we initialize the backpointers array we need to zero out all existing entries */ - memset(queue->backpointers.data, 0, queue->backpointers.current_size); - } - - /* - * Once we have any backpointers, we want to make sure we always have room in the backpointers array - * for all elements; otherwise, sift_down gets complicated if it runs out of memory when sifting an - * element with a backpointer down in the array. - */ + return err; + } + size_t index = aws_array_list_length(&queue->container) - 1; + + if (backpointer && !queue->backpointers.alloc) { + if (!queue->container.alloc) { + aws_raise_error(AWS_ERROR_UNSUPPORTED_OPERATION); + goto backpointer_update_failed; + } + + if (aws_array_list_init_dynamic( + &queue->backpointers, queue->container.alloc, index + 1, sizeof(struct aws_priority_queue_node *))) { + goto backpointer_update_failed; + } + + /* When we initialize the backpointers array we need to zero out all existing entries */ + memset(queue->backpointers.data, 0, queue->backpointers.current_size); + } + + /* + * Once we have any backpointers, we want to make sure we always have room in the backpointers array + * for all elements; otherwise, sift_down gets complicated if it runs out of memory when sifting an + * element with a backpointer down in the array. + */ if (!AWS_IS_ZEROED(queue->backpointers)) { - if (aws_array_list_set_at(&queue->backpointers, &backpointer, index)) { - goto backpointer_update_failed; - } - } - - if (backpointer) { - backpointer->current_index = index; - } - - s_sift_up(queue, aws_array_list_length(&queue->container) - 1); - + if (aws_array_list_set_at(&queue->backpointers, &backpointer, index)) { + goto backpointer_update_failed; + } + } + + if (backpointer) { + backpointer->current_index = index; + } + + s_sift_up(queue, aws_array_list_length(&queue->container) - 1); + AWS_POSTCONDITION(aws_priority_queue_is_valid(queue)); - return AWS_OP_SUCCESS; - -backpointer_update_failed: - /* Failed to initialize or grow the backpointer array, back out the node addition */ - aws_array_list_pop_back(&queue->container); + return AWS_OP_SUCCESS; + +backpointer_update_failed: + /* Failed to initialize or grow the backpointer array, back out the node addition */ + aws_array_list_pop_back(&queue->container); AWS_POSTCONDITION(aws_priority_queue_is_valid(queue)); - return AWS_OP_ERR; -} - -static int s_remove_node(struct aws_priority_queue *queue, void *item, size_t item_index) { + return AWS_OP_ERR; +} + +static int s_remove_node(struct aws_priority_queue *queue, void *item, size_t item_index) { AWS_PRECONDITION(aws_priority_queue_is_valid(queue)); AWS_PRECONDITION(item && AWS_MEM_IS_WRITABLE(item, queue->container.item_size)); - if (aws_array_list_get_at(&queue->container, item, item_index)) { - /* shouldn't happen, but if it does we've already raised an error... */ + if (aws_array_list_get_at(&queue->container, item, item_index)) { + /* shouldn't happen, but if it does we've already raised an error... */ AWS_POSTCONDITION(aws_priority_queue_is_valid(queue)); - return AWS_OP_ERR; - } - - size_t swap_with = aws_array_list_length(&queue->container) - 1; - struct aws_priority_queue_node *backpointer = NULL; - - if (item_index != swap_with) { - s_swap(queue, item_index, swap_with); - } - + return AWS_OP_ERR; + } + + size_t swap_with = aws_array_list_length(&queue->container) - 1; + struct aws_priority_queue_node *backpointer = NULL; + + if (item_index != swap_with) { + s_swap(queue, item_index, swap_with); + } + aws_array_list_pop_back(&queue->container); if (!AWS_IS_ZEROED(queue->backpointers)) { @@ -350,51 +350,51 @@ static int s_remove_node(struct aws_priority_queue *queue, void *item, size_t it backpointer->current_index = SIZE_MAX; } aws_array_list_pop_back(&queue->backpointers); - } - - if (item_index != swap_with) { - s_sift_either(queue, item_index); - } - + } + + if (item_index != swap_with) { + s_sift_either(queue, item_index); + } + AWS_POSTCONDITION(aws_priority_queue_is_valid(queue)); - return AWS_OP_SUCCESS; -} - -int aws_priority_queue_remove( - struct aws_priority_queue *queue, - void *item, - const struct aws_priority_queue_node *node) { + return AWS_OP_SUCCESS; +} + +int aws_priority_queue_remove( + struct aws_priority_queue *queue, + void *item, + const struct aws_priority_queue_node *node) { AWS_PRECONDITION(aws_priority_queue_is_valid(queue)); AWS_PRECONDITION(item && AWS_MEM_IS_WRITABLE(item, queue->container.item_size)); AWS_PRECONDITION(node && AWS_MEM_IS_READABLE(node, sizeof(struct aws_priority_queue_node))); AWS_ERROR_PRECONDITION( node->current_index < aws_array_list_length(&queue->container), AWS_ERROR_PRIORITY_QUEUE_BAD_NODE); AWS_ERROR_PRECONDITION(queue->backpointers.data, AWS_ERROR_PRIORITY_QUEUE_BAD_NODE); - + int rval = s_remove_node(queue, item, node->current_index); AWS_POSTCONDITION(aws_priority_queue_is_valid(queue)); return rval; -} - -int aws_priority_queue_pop(struct aws_priority_queue *queue, void *item) { +} + +int aws_priority_queue_pop(struct aws_priority_queue *queue, void *item) { AWS_PRECONDITION(aws_priority_queue_is_valid(queue)); AWS_PRECONDITION(item && AWS_MEM_IS_WRITABLE(item, queue->container.item_size)); AWS_ERROR_PRECONDITION(aws_array_list_length(&queue->container) != 0, AWS_ERROR_PRIORITY_QUEUE_EMPTY); - + int rval = s_remove_node(queue, item, 0); AWS_POSTCONDITION(aws_priority_queue_is_valid(queue)); return rval; -} - -int aws_priority_queue_top(const struct aws_priority_queue *queue, void **item) { +} + +int aws_priority_queue_top(const struct aws_priority_queue *queue, void **item) { AWS_ERROR_PRECONDITION(aws_array_list_length(&queue->container) != 0, AWS_ERROR_PRIORITY_QUEUE_EMPTY); - return aws_array_list_get_at_ptr(&queue->container, item, 0); -} - -size_t aws_priority_queue_size(const struct aws_priority_queue *queue) { - return aws_array_list_length(&queue->container); -} - -size_t aws_priority_queue_capacity(const struct aws_priority_queue *queue) { - return aws_array_list_capacity(&queue->container); -} + return aws_array_list_get_at_ptr(&queue->container, item, 0); +} + +size_t aws_priority_queue_size(const struct aws_priority_queue *queue) { + return aws_array_list_length(&queue->container); +} + +size_t aws_priority_queue_capacity(const struct aws_priority_queue *queue) { + return aws_array_list_capacity(&queue->container); +} diff --git a/contrib/restricted/aws/aws-c-common/source/string.c b/contrib/restricted/aws/aws-c-common/source/string.c index d1abf0dbff..4bd67ca7b2 100644 --- a/contrib/restricted/aws/aws-c-common/source/string.c +++ b/contrib/restricted/aws/aws-c-common/source/string.c @@ -1,41 +1,41 @@ /** * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0. - */ -#include <aws/common/string.h> - -struct aws_string *aws_string_new_from_c_str(struct aws_allocator *allocator, const char *c_str) { + */ +#include <aws/common/string.h> + +struct aws_string *aws_string_new_from_c_str(struct aws_allocator *allocator, const char *c_str) { AWS_PRECONDITION(allocator && c_str); - return aws_string_new_from_array(allocator, (const uint8_t *)c_str, strlen(c_str)); -} - -struct aws_string *aws_string_new_from_array(struct aws_allocator *allocator, const uint8_t *bytes, size_t len) { + return aws_string_new_from_array(allocator, (const uint8_t *)c_str, strlen(c_str)); +} + +struct aws_string *aws_string_new_from_array(struct aws_allocator *allocator, const uint8_t *bytes, size_t len) { AWS_PRECONDITION(allocator); AWS_PRECONDITION(AWS_MEM_IS_READABLE(bytes, len)); - size_t malloc_size; - if (aws_add_size_checked(sizeof(struct aws_string) + 1, len, &malloc_size)) { - return NULL; - } - struct aws_string *str = aws_mem_acquire(allocator, malloc_size); - if (!str) { - return NULL; - } - - /* Fields are declared const, so we need to copy them in like this */ - *(struct aws_allocator **)(&str->allocator) = allocator; - *(size_t *)(&str->len) = len; + size_t malloc_size; + if (aws_add_size_checked(sizeof(struct aws_string) + 1, len, &malloc_size)) { + return NULL; + } + struct aws_string *str = aws_mem_acquire(allocator, malloc_size); + if (!str) { + return NULL; + } + + /* Fields are declared const, so we need to copy them in like this */ + *(struct aws_allocator **)(&str->allocator) = allocator; + *(size_t *)(&str->len) = len; if (len > 0) { memcpy((void *)str->bytes, bytes, len); } - *(uint8_t *)&str->bytes[len] = '\0'; + *(uint8_t *)&str->bytes[len] = '\0'; AWS_RETURN_WITH_POSTCONDITION(str, aws_string_is_valid(str)); -} - -struct aws_string *aws_string_new_from_string(struct aws_allocator *allocator, const struct aws_string *str) { +} + +struct aws_string *aws_string_new_from_string(struct aws_allocator *allocator, const struct aws_string *str) { AWS_PRECONDITION(allocator && aws_string_is_valid(str)); - return aws_string_new_from_array(allocator, str->bytes, str->len); -} - + return aws_string_new_from_array(allocator, str->bytes, str->len); +} + struct aws_string *aws_string_new_from_cursor(struct aws_allocator *allocator, const struct aws_byte_cursor *cursor) { AWS_PRECONDITION(allocator && aws_byte_cursor_is_valid(cursor)); return aws_string_new_from_array(allocator, cursor->ptr, cursor->len); @@ -46,24 +46,24 @@ struct aws_string *aws_string_new_from_buf(struct aws_allocator *allocator, cons return aws_string_new_from_array(allocator, buf->buffer, buf->len); } -void aws_string_destroy(struct aws_string *str) { +void aws_string_destroy(struct aws_string *str) { AWS_PRECONDITION(!str || aws_string_is_valid(str)); - if (str && str->allocator) { - aws_mem_release(str->allocator, str); - } -} - -void aws_string_destroy_secure(struct aws_string *str) { + if (str && str->allocator) { + aws_mem_release(str->allocator, str); + } +} + +void aws_string_destroy_secure(struct aws_string *str) { AWS_PRECONDITION(!str || aws_string_is_valid(str)); - if (str) { - aws_secure_zero((void *)aws_string_bytes(str), str->len); - if (str->allocator) { - aws_mem_release(str->allocator, str); - } - } -} - -int aws_string_compare(const struct aws_string *a, const struct aws_string *b) { + if (str) { + aws_secure_zero((void *)aws_string_bytes(str), str->len); + if (str->allocator) { + aws_mem_release(str->allocator, str); + } + } +} + +int aws_string_compare(const struct aws_string *a, const struct aws_string *b) { AWS_PRECONDITION(!a || aws_string_is_valid(a)); AWS_PRECONDITION(!b || aws_string_is_valid(b)); if (a == b) { @@ -76,26 +76,26 @@ int aws_string_compare(const struct aws_string *a, const struct aws_string *b) { return 1; } - size_t len_a = a->len; - size_t len_b = b->len; - size_t min_len = len_a < len_b ? len_a : len_b; - - int ret = memcmp(aws_string_bytes(a), aws_string_bytes(b), min_len); + size_t len_a = a->len; + size_t len_b = b->len; + size_t min_len = len_a < len_b ? len_a : len_b; + + int ret = memcmp(aws_string_bytes(a), aws_string_bytes(b), min_len); AWS_POSTCONDITION(aws_string_is_valid(a)); AWS_POSTCONDITION(aws_string_is_valid(b)); - if (ret) { - return ret; /* overlapping characters differ */ - } - if (len_a == len_b) { - return 0; /* strings identical */ - } - if (len_a > len_b) { - return 1; /* string b is first n characters of string a */ - } - return -1; /* string a is first n characters of string b */ -} - -int aws_array_list_comparator_string(const void *a, const void *b) { + if (ret) { + return ret; /* overlapping characters differ */ + } + if (len_a == len_b) { + return 0; /* strings identical */ + } + if (len_a > len_b) { + return 1; /* string b is first n characters of string a */ + } + return -1; /* string a is first n characters of string b */ +} + +int aws_array_list_comparator_string(const void *a, const void *b) { if (a == b) { return 0; /* strings identical */ } @@ -105,10 +105,10 @@ int aws_array_list_comparator_string(const void *a, const void *b) { if (b == NULL) { return 1; } - const struct aws_string *str_a = *(const struct aws_string **)a; - const struct aws_string *str_b = *(const struct aws_string **)b; - return aws_string_compare(str_a, str_b); -} + const struct aws_string *str_a = *(const struct aws_string **)a; + const struct aws_string *str_b = *(const struct aws_string **)b; + return aws_string_compare(str_a, str_b); +} /** * Returns true if bytes of string are the same, false otherwise. diff --git a/contrib/restricted/aws/aws-c-common/source/task_scheduler.c b/contrib/restricted/aws/aws-c-common/source/task_scheduler.c index 31ce7af1ab..66793d71bd 100644 --- a/contrib/restricted/aws/aws-c-common/source/task_scheduler.c +++ b/contrib/restricted/aws/aws-c-common/source/task_scheduler.c @@ -1,16 +1,16 @@ /** * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0. - */ - -#include <aws/common/task_scheduler.h> - + */ + +#include <aws/common/task_scheduler.h> + #include <aws/common/logging.h> #include <inttypes.h> -static const size_t DEFAULT_QUEUE_SIZE = 7; - +static const size_t DEFAULT_QUEUE_SIZE = 7; + void aws_task_init(struct aws_task *task, aws_task_fn *fn, void *arg, const char *type_tag) { AWS_ZERO_STRUCT(*task); task->fn = fn; @@ -43,17 +43,17 @@ void aws_task_run(struct aws_task *task, enum aws_task_status status) { task->fn(task, task->arg, status); } -static int s_compare_timestamps(const void *a, const void *b) { - uint64_t a_time = (*(struct aws_task **)a)->timestamp; - uint64_t b_time = (*(struct aws_task **)b)->timestamp; - return a_time > b_time; /* min-heap */ -} - -static void s_run_all(struct aws_task_scheduler *scheduler, uint64_t current_time, enum aws_task_status status); - -int aws_task_scheduler_init(struct aws_task_scheduler *scheduler, struct aws_allocator *alloc) { - AWS_ASSERT(alloc); - +static int s_compare_timestamps(const void *a, const void *b) { + uint64_t a_time = (*(struct aws_task **)a)->timestamp; + uint64_t b_time = (*(struct aws_task **)b)->timestamp; + return a_time > b_time; /* min-heap */ +} + +static void s_run_all(struct aws_task_scheduler *scheduler, uint64_t current_time, enum aws_task_status status); + +int aws_task_scheduler_init(struct aws_task_scheduler *scheduler, struct aws_allocator *alloc) { + AWS_ASSERT(alloc); + AWS_ZERO_STRUCT(*scheduler); if (aws_priority_queue_init_dynamic( @@ -61,95 +61,95 @@ int aws_task_scheduler_init(struct aws_task_scheduler *scheduler, struct aws_all return AWS_OP_ERR; }; - scheduler->alloc = alloc; - aws_linked_list_init(&scheduler->timed_list); - aws_linked_list_init(&scheduler->asap_list); + scheduler->alloc = alloc; + aws_linked_list_init(&scheduler->timed_list); + aws_linked_list_init(&scheduler->asap_list); AWS_POSTCONDITION(aws_task_scheduler_is_valid(scheduler)); return AWS_OP_SUCCESS; -} - -void aws_task_scheduler_clean_up(struct aws_task_scheduler *scheduler) { - AWS_ASSERT(scheduler); - +} + +void aws_task_scheduler_clean_up(struct aws_task_scheduler *scheduler) { + AWS_ASSERT(scheduler); + if (aws_task_scheduler_is_valid(scheduler)) { /* Execute all remaining tasks as CANCELED. * Do this in a loop so that tasks scheduled by other tasks are executed */ while (aws_task_scheduler_has_tasks(scheduler, NULL)) { s_run_all(scheduler, UINT64_MAX, AWS_TASK_STATUS_CANCELED); } - } - - aws_priority_queue_clean_up(&scheduler->timed_queue); + } + + aws_priority_queue_clean_up(&scheduler->timed_queue); AWS_ZERO_STRUCT(*scheduler); -} - +} + bool aws_task_scheduler_is_valid(const struct aws_task_scheduler *scheduler) { return scheduler && scheduler->alloc && aws_priority_queue_is_valid(&scheduler->timed_queue) && aws_linked_list_is_valid(&scheduler->asap_list) && aws_linked_list_is_valid(&scheduler->timed_list); } -bool aws_task_scheduler_has_tasks(const struct aws_task_scheduler *scheduler, uint64_t *next_task_time) { - AWS_ASSERT(scheduler); - - uint64_t timestamp = UINT64_MAX; - bool has_tasks = false; - - if (!aws_linked_list_empty(&scheduler->asap_list)) { - timestamp = 0; - has_tasks = true; - - } else { - /* Check whether timed_list or timed_queue has the earlier task */ - if (AWS_UNLIKELY(!aws_linked_list_empty(&scheduler->timed_list))) { - struct aws_linked_list_node *node = aws_linked_list_front(&scheduler->timed_list); - struct aws_task *task = AWS_CONTAINER_OF(node, struct aws_task, node); - timestamp = task->timestamp; - has_tasks = true; - } - - struct aws_task **task_ptrptr = NULL; - if (aws_priority_queue_top(&scheduler->timed_queue, (void **)&task_ptrptr) == AWS_OP_SUCCESS) { - if ((*task_ptrptr)->timestamp < timestamp) { - timestamp = (*task_ptrptr)->timestamp; - } - has_tasks = true; - } - } - - if (next_task_time) { - *next_task_time = timestamp; - } - return has_tasks; -} - -void aws_task_scheduler_schedule_now(struct aws_task_scheduler *scheduler, struct aws_task *task) { - AWS_ASSERT(scheduler); - AWS_ASSERT(task); - AWS_ASSERT(task->fn); - +bool aws_task_scheduler_has_tasks(const struct aws_task_scheduler *scheduler, uint64_t *next_task_time) { + AWS_ASSERT(scheduler); + + uint64_t timestamp = UINT64_MAX; + bool has_tasks = false; + + if (!aws_linked_list_empty(&scheduler->asap_list)) { + timestamp = 0; + has_tasks = true; + + } else { + /* Check whether timed_list or timed_queue has the earlier task */ + if (AWS_UNLIKELY(!aws_linked_list_empty(&scheduler->timed_list))) { + struct aws_linked_list_node *node = aws_linked_list_front(&scheduler->timed_list); + struct aws_task *task = AWS_CONTAINER_OF(node, struct aws_task, node); + timestamp = task->timestamp; + has_tasks = true; + } + + struct aws_task **task_ptrptr = NULL; + if (aws_priority_queue_top(&scheduler->timed_queue, (void **)&task_ptrptr) == AWS_OP_SUCCESS) { + if ((*task_ptrptr)->timestamp < timestamp) { + timestamp = (*task_ptrptr)->timestamp; + } + has_tasks = true; + } + } + + if (next_task_time) { + *next_task_time = timestamp; + } + return has_tasks; +} + +void aws_task_scheduler_schedule_now(struct aws_task_scheduler *scheduler, struct aws_task *task) { + AWS_ASSERT(scheduler); + AWS_ASSERT(task); + AWS_ASSERT(task->fn); + AWS_LOGF_DEBUG( AWS_LS_COMMON_TASK_SCHEDULER, "id=%p: Scheduling %s task for immediate execution", (void *)task, task->type_tag); - task->priority_queue_node.current_index = SIZE_MAX; - aws_linked_list_node_reset(&task->node); - task->timestamp = 0; - - aws_linked_list_push_back(&scheduler->asap_list, &task->node); -} - -void aws_task_scheduler_schedule_future( - struct aws_task_scheduler *scheduler, - struct aws_task *task, - uint64_t time_to_run) { - - AWS_ASSERT(scheduler); - AWS_ASSERT(task); - AWS_ASSERT(task->fn); - + task->priority_queue_node.current_index = SIZE_MAX; + aws_linked_list_node_reset(&task->node); + task->timestamp = 0; + + aws_linked_list_push_back(&scheduler->asap_list, &task->node); +} + +void aws_task_scheduler_schedule_future( + struct aws_task_scheduler *scheduler, + struct aws_task *task, + uint64_t time_to_run) { + + AWS_ASSERT(scheduler); + AWS_ASSERT(task); + AWS_ASSERT(task->fn); + AWS_LOGF_DEBUG( AWS_LS_COMMON_TASK_SCHEDULER, "id=%p: Scheduling %s task for future execution at time %" PRIu64, @@ -157,108 +157,108 @@ void aws_task_scheduler_schedule_future( task->type_tag, time_to_run); - task->timestamp = time_to_run; - - task->priority_queue_node.current_index = SIZE_MAX; - aws_linked_list_node_reset(&task->node); - int err = aws_priority_queue_push_ref(&scheduler->timed_queue, &task, &task->priority_queue_node); - if (AWS_UNLIKELY(err)) { - /* In the (very unlikely) case that we can't push into the timed_queue, - * perform a sorted insertion into timed_list. */ - struct aws_linked_list_node *node_i; - for (node_i = aws_linked_list_begin(&scheduler->timed_list); - node_i != aws_linked_list_end(&scheduler->timed_list); - node_i = aws_linked_list_next(node_i)) { - - struct aws_task *task_i = AWS_CONTAINER_OF(node_i, struct aws_task, node); - if (task_i->timestamp > time_to_run) { - break; - } - } - aws_linked_list_insert_before(node_i, &task->node); - } -} - -void aws_task_scheduler_run_all(struct aws_task_scheduler *scheduler, uint64_t current_time) { - AWS_ASSERT(scheduler); - - s_run_all(scheduler, current_time, AWS_TASK_STATUS_RUN_READY); -} - -static void s_run_all(struct aws_task_scheduler *scheduler, uint64_t current_time, enum aws_task_status status) { - - /* Move scheduled tasks to running_list before executing. - * This gives us the desired behavior that: if executing a task results in another task being scheduled, - * that new task is not executed until the next time run() is invoked. */ - struct aws_linked_list running_list; - aws_linked_list_init(&running_list); - - /* First move everything from asap_list */ - aws_linked_list_swap_contents(&running_list, &scheduler->asap_list); - - /* Next move tasks from timed_queue and timed_list, based on whichever's next-task is sooner. - * It's very unlikely that any tasks are in timed_list, so once it has no more valid tasks, - * break out of this complex loop in favor of a simpler one. */ - while (AWS_UNLIKELY(!aws_linked_list_empty(&scheduler->timed_list))) { - - struct aws_linked_list_node *timed_list_node = aws_linked_list_begin(&scheduler->timed_list); - struct aws_task *timed_list_task = AWS_CONTAINER_OF(timed_list_node, struct aws_task, node); - if (timed_list_task->timestamp > current_time) { - /* timed_list is out of valid tasks, break out of complex loop */ - break; - } - - /* Check if timed_queue has a task which is sooner */ - struct aws_task **timed_queue_task_ptrptr = NULL; - if (aws_priority_queue_top(&scheduler->timed_queue, (void **)&timed_queue_task_ptrptr) == AWS_OP_SUCCESS) { - if ((*timed_queue_task_ptrptr)->timestamp <= current_time) { - if ((*timed_queue_task_ptrptr)->timestamp < timed_list_task->timestamp) { - /* Take task from timed_queue */ - struct aws_task *timed_queue_task; - aws_priority_queue_pop(&scheduler->timed_queue, &timed_queue_task); - aws_linked_list_push_back(&running_list, &timed_queue_task->node); - continue; - } - } - } - - /* Take task from timed_list */ - aws_linked_list_pop_front(&scheduler->timed_list); - aws_linked_list_push_back(&running_list, &timed_list_task->node); - } - - /* Simpler loop that moves remaining valid tasks from timed_queue */ - struct aws_task **timed_queue_task_ptrptr = NULL; - while (aws_priority_queue_top(&scheduler->timed_queue, (void **)&timed_queue_task_ptrptr) == AWS_OP_SUCCESS) { - if ((*timed_queue_task_ptrptr)->timestamp > current_time) { - break; - } - - struct aws_task *next_timed_task; - aws_priority_queue_pop(&scheduler->timed_queue, &next_timed_task); - aws_linked_list_push_back(&running_list, &next_timed_task->node); - } - - /* Run tasks */ - while (!aws_linked_list_empty(&running_list)) { - struct aws_linked_list_node *task_node = aws_linked_list_pop_front(&running_list); - struct aws_task *task = AWS_CONTAINER_OF(task_node, struct aws_task, node); - aws_task_run(task, status); - } -} - -void aws_task_scheduler_cancel_task(struct aws_task_scheduler *scheduler, struct aws_task *task) { - /* attempt the linked lists first since those will be faster access and more likely to occur - * anyways. - */ - if (task->node.next) { - aws_linked_list_remove(&task->node); - } else { - aws_priority_queue_remove(&scheduler->timed_queue, &task, &task->priority_queue_node); - } + task->timestamp = time_to_run; + + task->priority_queue_node.current_index = SIZE_MAX; + aws_linked_list_node_reset(&task->node); + int err = aws_priority_queue_push_ref(&scheduler->timed_queue, &task, &task->priority_queue_node); + if (AWS_UNLIKELY(err)) { + /* In the (very unlikely) case that we can't push into the timed_queue, + * perform a sorted insertion into timed_list. */ + struct aws_linked_list_node *node_i; + for (node_i = aws_linked_list_begin(&scheduler->timed_list); + node_i != aws_linked_list_end(&scheduler->timed_list); + node_i = aws_linked_list_next(node_i)) { + + struct aws_task *task_i = AWS_CONTAINER_OF(node_i, struct aws_task, node); + if (task_i->timestamp > time_to_run) { + break; + } + } + aws_linked_list_insert_before(node_i, &task->node); + } +} + +void aws_task_scheduler_run_all(struct aws_task_scheduler *scheduler, uint64_t current_time) { + AWS_ASSERT(scheduler); + + s_run_all(scheduler, current_time, AWS_TASK_STATUS_RUN_READY); +} + +static void s_run_all(struct aws_task_scheduler *scheduler, uint64_t current_time, enum aws_task_status status) { + + /* Move scheduled tasks to running_list before executing. + * This gives us the desired behavior that: if executing a task results in another task being scheduled, + * that new task is not executed until the next time run() is invoked. */ + struct aws_linked_list running_list; + aws_linked_list_init(&running_list); + + /* First move everything from asap_list */ + aws_linked_list_swap_contents(&running_list, &scheduler->asap_list); + + /* Next move tasks from timed_queue and timed_list, based on whichever's next-task is sooner. + * It's very unlikely that any tasks are in timed_list, so once it has no more valid tasks, + * break out of this complex loop in favor of a simpler one. */ + while (AWS_UNLIKELY(!aws_linked_list_empty(&scheduler->timed_list))) { + + struct aws_linked_list_node *timed_list_node = aws_linked_list_begin(&scheduler->timed_list); + struct aws_task *timed_list_task = AWS_CONTAINER_OF(timed_list_node, struct aws_task, node); + if (timed_list_task->timestamp > current_time) { + /* timed_list is out of valid tasks, break out of complex loop */ + break; + } + + /* Check if timed_queue has a task which is sooner */ + struct aws_task **timed_queue_task_ptrptr = NULL; + if (aws_priority_queue_top(&scheduler->timed_queue, (void **)&timed_queue_task_ptrptr) == AWS_OP_SUCCESS) { + if ((*timed_queue_task_ptrptr)->timestamp <= current_time) { + if ((*timed_queue_task_ptrptr)->timestamp < timed_list_task->timestamp) { + /* Take task from timed_queue */ + struct aws_task *timed_queue_task; + aws_priority_queue_pop(&scheduler->timed_queue, &timed_queue_task); + aws_linked_list_push_back(&running_list, &timed_queue_task->node); + continue; + } + } + } + + /* Take task from timed_list */ + aws_linked_list_pop_front(&scheduler->timed_list); + aws_linked_list_push_back(&running_list, &timed_list_task->node); + } + + /* Simpler loop that moves remaining valid tasks from timed_queue */ + struct aws_task **timed_queue_task_ptrptr = NULL; + while (aws_priority_queue_top(&scheduler->timed_queue, (void **)&timed_queue_task_ptrptr) == AWS_OP_SUCCESS) { + if ((*timed_queue_task_ptrptr)->timestamp > current_time) { + break; + } + + struct aws_task *next_timed_task; + aws_priority_queue_pop(&scheduler->timed_queue, &next_timed_task); + aws_linked_list_push_back(&running_list, &next_timed_task->node); + } + + /* Run tasks */ + while (!aws_linked_list_empty(&running_list)) { + struct aws_linked_list_node *task_node = aws_linked_list_pop_front(&running_list); + struct aws_task *task = AWS_CONTAINER_OF(task_node, struct aws_task, node); + aws_task_run(task, status); + } +} + +void aws_task_scheduler_cancel_task(struct aws_task_scheduler *scheduler, struct aws_task *task) { + /* attempt the linked lists first since those will be faster access and more likely to occur + * anyways. + */ + if (task->node.next) { + aws_linked_list_remove(&task->node); + } else { + aws_priority_queue_remove(&scheduler->timed_queue, &task, &task->priority_queue_node); + } /* * No need to log cancellation specially; it will get logged during the run call with the canceled status */ - aws_task_run(task, AWS_TASK_STATUS_CANCELED); -} + aws_task_run(task, AWS_TASK_STATUS_CANCELED); +} diff --git a/contrib/restricted/aws/aws-c-common/source/uuid.c b/contrib/restricted/aws/aws-c-common/source/uuid.c index a962abd653..3cf681ed62 100644 --- a/contrib/restricted/aws/aws-c-common/source/uuid.c +++ b/contrib/restricted/aws/aws-c-common/source/uuid.c @@ -1,99 +1,99 @@ /** * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0. - */ -#include <aws/common/uuid.h> - -#include <aws/common/byte_buf.h> -#include <aws/common/device_random.h> - -#include <inttypes.h> -#include <stdio.h> - -#define HEX_CHAR_FMT "%02" SCNx8 - -#define UUID_FORMAT \ - HEX_CHAR_FMT HEX_CHAR_FMT HEX_CHAR_FMT HEX_CHAR_FMT \ - "-" HEX_CHAR_FMT HEX_CHAR_FMT "-" HEX_CHAR_FMT HEX_CHAR_FMT "-" HEX_CHAR_FMT HEX_CHAR_FMT \ - "-" HEX_CHAR_FMT HEX_CHAR_FMT HEX_CHAR_FMT HEX_CHAR_FMT HEX_CHAR_FMT HEX_CHAR_FMT - -#include <stdio.h> - -#ifdef _MSC_VER -/* disables warning non const declared initializers for Microsoft compilers */ -# pragma warning(disable : 4204) -# pragma warning(disable : 4706) -/* sprintf warning (we already check the bounds in this case). */ -# pragma warning(disable : 4996) -#endif - -int aws_uuid_init(struct aws_uuid *uuid) { - struct aws_byte_buf buf = aws_byte_buf_from_empty_array(uuid->uuid_data, sizeof(uuid->uuid_data)); - - return aws_device_random_buffer(&buf); -} - -int aws_uuid_init_from_str(struct aws_uuid *uuid, const struct aws_byte_cursor *uuid_str) { + */ +#include <aws/common/uuid.h> + +#include <aws/common/byte_buf.h> +#include <aws/common/device_random.h> + +#include <inttypes.h> +#include <stdio.h> + +#define HEX_CHAR_FMT "%02" SCNx8 + +#define UUID_FORMAT \ + HEX_CHAR_FMT HEX_CHAR_FMT HEX_CHAR_FMT HEX_CHAR_FMT \ + "-" HEX_CHAR_FMT HEX_CHAR_FMT "-" HEX_CHAR_FMT HEX_CHAR_FMT "-" HEX_CHAR_FMT HEX_CHAR_FMT \ + "-" HEX_CHAR_FMT HEX_CHAR_FMT HEX_CHAR_FMT HEX_CHAR_FMT HEX_CHAR_FMT HEX_CHAR_FMT + +#include <stdio.h> + +#ifdef _MSC_VER +/* disables warning non const declared initializers for Microsoft compilers */ +# pragma warning(disable : 4204) +# pragma warning(disable : 4706) +/* sprintf warning (we already check the bounds in this case). */ +# pragma warning(disable : 4996) +#endif + +int aws_uuid_init(struct aws_uuid *uuid) { + struct aws_byte_buf buf = aws_byte_buf_from_empty_array(uuid->uuid_data, sizeof(uuid->uuid_data)); + + return aws_device_random_buffer(&buf); +} + +int aws_uuid_init_from_str(struct aws_uuid *uuid, const struct aws_byte_cursor *uuid_str) { AWS_ERROR_PRECONDITION(uuid_str->len >= AWS_UUID_STR_LEN - 1, AWS_ERROR_INVALID_BUFFER_SIZE); - - char cpy[AWS_UUID_STR_LEN] = {0}; - memcpy(cpy, uuid_str->ptr, AWS_UUID_STR_LEN - 1); - - AWS_ZERO_STRUCT(*uuid); - - if (16 != sscanf( - cpy, - UUID_FORMAT, - &uuid->uuid_data[0], - &uuid->uuid_data[1], - &uuid->uuid_data[2], - &uuid->uuid_data[3], - &uuid->uuid_data[4], - &uuid->uuid_data[5], - &uuid->uuid_data[6], - &uuid->uuid_data[7], - &uuid->uuid_data[8], - &uuid->uuid_data[9], - &uuid->uuid_data[10], - &uuid->uuid_data[11], - &uuid->uuid_data[12], - &uuid->uuid_data[13], - &uuid->uuid_data[14], - &uuid->uuid_data[15])) { - return aws_raise_error(AWS_ERROR_MALFORMED_INPUT_STRING); - } - - return AWS_OP_SUCCESS; -} - -int aws_uuid_to_str(const struct aws_uuid *uuid, struct aws_byte_buf *output) { + + char cpy[AWS_UUID_STR_LEN] = {0}; + memcpy(cpy, uuid_str->ptr, AWS_UUID_STR_LEN - 1); + + AWS_ZERO_STRUCT(*uuid); + + if (16 != sscanf( + cpy, + UUID_FORMAT, + &uuid->uuid_data[0], + &uuid->uuid_data[1], + &uuid->uuid_data[2], + &uuid->uuid_data[3], + &uuid->uuid_data[4], + &uuid->uuid_data[5], + &uuid->uuid_data[6], + &uuid->uuid_data[7], + &uuid->uuid_data[8], + &uuid->uuid_data[9], + &uuid->uuid_data[10], + &uuid->uuid_data[11], + &uuid->uuid_data[12], + &uuid->uuid_data[13], + &uuid->uuid_data[14], + &uuid->uuid_data[15])) { + return aws_raise_error(AWS_ERROR_MALFORMED_INPUT_STRING); + } + + return AWS_OP_SUCCESS; +} + +int aws_uuid_to_str(const struct aws_uuid *uuid, struct aws_byte_buf *output) { AWS_ERROR_PRECONDITION(output->capacity - output->len >= AWS_UUID_STR_LEN, AWS_ERROR_SHORT_BUFFER); - - sprintf( - (char *)(output->buffer + output->len), - UUID_FORMAT, - uuid->uuid_data[0], - uuid->uuid_data[1], - uuid->uuid_data[2], - uuid->uuid_data[3], - uuid->uuid_data[4], - uuid->uuid_data[5], - uuid->uuid_data[6], - uuid->uuid_data[7], - uuid->uuid_data[8], - uuid->uuid_data[9], - uuid->uuid_data[10], - uuid->uuid_data[11], - uuid->uuid_data[12], - uuid->uuid_data[13], - uuid->uuid_data[14], - uuid->uuid_data[15]); - - output->len += AWS_UUID_STR_LEN - 1; - - return AWS_OP_SUCCESS; -} - -bool aws_uuid_equals(const struct aws_uuid *a, const struct aws_uuid *b) { - return 0 == memcmp(a->uuid_data, b->uuid_data, sizeof(a->uuid_data)); -} + + sprintf( + (char *)(output->buffer + output->len), + UUID_FORMAT, + uuid->uuid_data[0], + uuid->uuid_data[1], + uuid->uuid_data[2], + uuid->uuid_data[3], + uuid->uuid_data[4], + uuid->uuid_data[5], + uuid->uuid_data[6], + uuid->uuid_data[7], + uuid->uuid_data[8], + uuid->uuid_data[9], + uuid->uuid_data[10], + uuid->uuid_data[11], + uuid->uuid_data[12], + uuid->uuid_data[13], + uuid->uuid_data[14], + uuid->uuid_data[15]); + + output->len += AWS_UUID_STR_LEN - 1; + + return AWS_OP_SUCCESS; +} + +bool aws_uuid_equals(const struct aws_uuid *a, const struct aws_uuid *b) { + return 0 == memcmp(a->uuid_data, b->uuid_data, sizeof(a->uuid_data)); +} |