diff options
author | dakovalkov <dakovalkov@yandex-team.com> | 2023-12-03 13:33:55 +0300 |
---|---|---|
committer | dakovalkov <dakovalkov@yandex-team.com> | 2023-12-03 14:04:39 +0300 |
commit | 2a718325637e5302334b6d0a6430f63168f8dbb3 (patch) | |
tree | 64be81080b7df9ec1d86d053a0c394ae53fcf1fe /contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils | |
parent | e0d94a470142d95c3007e9c5d80380994940664a (diff) | |
download | ydb-2a718325637e5302334b6d0a6430f63168f8dbb3.tar.gz |
Update contrib/libs/aws-sdk-cpp to 1.11.37
Diffstat (limited to 'contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils')
19 files changed, 1458 insertions, 77 deletions
diff --git a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/DateTimeCommon.cpp b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/DateTimeCommon.cpp index b690c90c2dd..5ef76dcfc67 100644 --- a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/DateTimeCommon.cpp +++ b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/DateTimeCommon.cpp @@ -176,7 +176,7 @@ static int GetWeekDayNumberFromStr(const char* timeString, size_t startIndex, si } } -//Get the 0-11 monthy number from a string representing Month. Case insensitive and will stop on abbreviation +//Get the 0-11 monthly number from a string representing Month. Case insensitive and will stop on abbreviation static int GetMonthNumberFromStr(const char* timeString, size_t startIndex, size_t stopIndex) { if (stopIndex - startIndex < 3) @@ -842,7 +842,9 @@ public: break; case 6: - if ((c == 'Z' || c == '+' || c == '-' ) && (index - stateStartIndex == 3)) + if ((c == 'Z' || c == '+' || c == '-' ) && + (index - stateStartIndex >= 3) && + (index - stateStartIndex <= 9)) { m_tz[0] = c; m_state = 7; @@ -1268,6 +1270,12 @@ double DateTime::SecondsWithMSPrecision() const return timestamp.count(); } +int64_t DateTime::Seconds() const +{ + auto timestamp = std::chrono::duration_cast<std::chrono::seconds>(m_time.time_since_epoch()); + return timestamp.count(); +} + int64_t DateTime::Millis() const { auto timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(m_time.time_since_epoch()); diff --git a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/Document.cpp b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/Document.cpp new file mode 100644 index 00000000000..ef8210aeb1e --- /dev/null +++ b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/Document.cpp @@ -0,0 +1,673 @@ +/** + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ + +#include <aws/core/utils/Document.h> + +#include <iterator> +#include <algorithm> +#include <aws/core/utils/memory/stl/AWSStringStream.h> +#include <aws/core/utils/StringUtils.h> +#include <aws/core/utils/json/JsonSerializer.h> + +using namespace Aws::Utils; + +Document::Document() : m_wasParseSuccessful(true) +{ + m_json = nullptr; +} + +Document::Document(cJSON* value) : + m_json(cJSON_AS4CPP_Duplicate(value, true /* recurse */)), + m_wasParseSuccessful(true) +{ +} + +Document::Document(const Aws::String& value) : m_wasParseSuccessful(true) +{ + const char* return_parse_end; + m_json = cJSON_AS4CPP_ParseWithOpts(value.c_str(), &return_parse_end, 1/*require_null_terminated*/); + + if (!m_json || cJSON_AS4CPP_IsInvalid(m_json)) + { + m_wasParseSuccessful = false; + m_errorMessage = "Failed to parse JSON at: "; + m_errorMessage += return_parse_end; + } +} + +Document::Document(Aws::IStream& istream) : m_wasParseSuccessful(true) +{ + Aws::StringStream memoryStream; + std::copy(std::istreambuf_iterator<char>(istream), std::istreambuf_iterator<char>(), std::ostreambuf_iterator<char>(memoryStream)); + const char* return_parse_end; + const auto input = memoryStream.str(); + m_json = cJSON_AS4CPP_ParseWithOpts(input.c_str(), &return_parse_end, 1/*require_null_terminated*/); + + if (!m_json || cJSON_AS4CPP_IsInvalid(m_json)) + { + m_wasParseSuccessful = false; + m_errorMessage = "Failed to parse JSON. Invalid input at: "; + m_errorMessage += return_parse_end; + } +} + +Document::Document(const Document& value) : + m_json(cJSON_AS4CPP_Duplicate(value.m_json, true/*recurse*/)), + m_wasParseSuccessful(value.m_wasParseSuccessful), + m_errorMessage(value.m_errorMessage) +{ +} + +Document::Document(Document&& value) : + m_json(value.m_json), + m_wasParseSuccessful(value.m_wasParseSuccessful), + m_errorMessage(std::move(value.m_errorMessage)) +{ + value.m_json = nullptr; +} + +Document::Document(const Json::JsonView& view) : + m_json(cJSON_AS4CPP_Duplicate(view.m_value, true/*recurse*/)), + m_wasParseSuccessful(true), + m_errorMessage({}) +{ +} + +void Document::Destroy() +{ + cJSON_AS4CPP_Delete(m_json); +} + +Document::~Document() +{ + Destroy(); +} + +Document& Document::operator=(const Document& other) +{ + if (this == &other) + { + return *this; + } + + Destroy(); + m_json = cJSON_AS4CPP_Duplicate(other.m_json, true /*recurse*/); + m_wasParseSuccessful = other.m_wasParseSuccessful; + m_errorMessage = other.m_errorMessage; + return *this; +} + +Document& Document::operator=(Document&& other) +{ + if (this == &other) + { + return *this; + } + + using std::swap; + swap(m_json, other.m_json); + swap(m_errorMessage, other.m_errorMessage); + m_wasParseSuccessful = other.m_wasParseSuccessful; + return *this; +} + +Document& Document::operator=(const Json::JsonView& other) +{ + Destroy(); + m_json = cJSON_AS4CPP_Duplicate(other.m_value, true /*recurse*/); + m_wasParseSuccessful = true; + m_errorMessage = {}; + return *this; +} + +bool Document::operator==(const Document& other) const +{ + return cJSON_AS4CPP_Compare(m_json, other.m_json, true /*case-sensitive*/) != 0; +} + +bool Document::operator!=(const Document& other) const +{ + return !(*this == other); +} + +static void AddOrReplace(cJSON* root, const char* key, cJSON* value) +{ + const auto existing = cJSON_AS4CPP_GetObjectItemCaseSensitive(root, key); + if (existing) + { + cJSON_AS4CPP_ReplaceItemInObjectCaseSensitive(root, key, value); + } + else + { + cJSON_AS4CPP_AddItemToObject(root, key, value); + } +} + +Document& Document::WithString(const char* key, const Aws::String& value) +{ + if (!m_json) + { + m_json = cJSON_AS4CPP_CreateObject(); + } + + const auto val = cJSON_AS4CPP_CreateString(value.c_str()); + AddOrReplace(m_json, key, val); + return *this; +} + +Document& Document::WithString(const Aws::String& key, const Aws::String& value) +{ + return WithString(key.c_str(), value); +} + +Document& Document::AsString(const Aws::String& value) +{ + Destroy(); + m_json = cJSON_AS4CPP_CreateString(value.c_str()); + return *this; +} + +Document& Document::WithBool(const char* key, bool value) +{ + if (!m_json) + { + m_json = cJSON_AS4CPP_CreateObject(); + } + + const auto val = cJSON_AS4CPP_CreateBool(value); + AddOrReplace(m_json, key, val); + return *this; +} + +Document& Document::WithBool(const Aws::String& key, bool value) +{ + return WithBool(key.c_str(), value); +} + +Document& Document::AsBool(bool value) +{ + Destroy(); + m_json = cJSON_AS4CPP_CreateBool(value); + return *this; +} + +Document& Document::WithInteger(const char* key, int value) +{ + return WithDouble(key, static_cast<double>(value)); +} + +Document& Document::WithInteger(const Aws::String& key, int value) +{ + return WithDouble(key.c_str(), static_cast<double>(value)); +} + +Document& Document::AsInteger(int value) +{ + Destroy(); + m_json = cJSON_AS4CPP_CreateNumber(static_cast<double>(value)); + return *this; +} + +Document& Document::WithInt64(const char* key, long long value) +{ + if (!m_json) + { + m_json = cJSON_AS4CPP_CreateObject(); + } + + const auto val = cJSON_AS4CPP_CreateInt64(value); + AddOrReplace(m_json, key, val); + return *this; +} + +Document& Document::WithInt64(const Aws::String& key, long long value) +{ + return WithInt64(key.c_str(), value); +} + +Document& Document::AsInt64(long long value) +{ + Destroy(); + m_json = cJSON_AS4CPP_CreateInt64(value); + return *this; +} + +Document& Document::WithDouble(const char* key, double value) +{ + if (!m_json) + { + m_json = cJSON_AS4CPP_CreateObject(); + } + + const auto val = cJSON_AS4CPP_CreateNumber(value); + AddOrReplace(m_json, key, val); + return *this; +} + +Document& Document::WithDouble(const Aws::String& key, double value) +{ + return WithDouble(key.c_str(), value); +} + +Document& Document::AsDouble(double value) +{ + Destroy(); + m_json = cJSON_AS4CPP_CreateNumber(value); + return *this; +} + +Document& Document::WithArray(const char* key, const Array<Aws::String>& array) +{ + if (!m_json) + { + m_json = cJSON_AS4CPP_CreateObject(); + } + + auto arrayValue = cJSON_AS4CPP_CreateArray(); + for (unsigned i = 0; i < array.GetLength(); ++i) + { + cJSON_AS4CPP_AddItemToArray(arrayValue, cJSON_AS4CPP_CreateString(array[i].c_str())); + } + + AddOrReplace(m_json, key, arrayValue); + return *this; +} + +Document& Document::WithArray(const Aws::String& key, const Array<Aws::String>& array) +{ + return WithArray(key.c_str(), array); +} + +Document& Document::WithArray(const Aws::String& key, const Array<Document>& array) +{ + if (!m_json) + { + m_json = cJSON_AS4CPP_CreateObject(); + } + + auto arrayValue = cJSON_AS4CPP_CreateArray(); + for (unsigned i = 0; i < array.GetLength(); ++i) + { + cJSON_AS4CPP_AddItemToArray(arrayValue, cJSON_AS4CPP_Duplicate(array[i].m_json, true /*recurse*/)); + } + + AddOrReplace(m_json, key.c_str(), arrayValue); + return *this; +} + +Document& Document::WithArray(const Aws::String& key, Array<Document>&& array) +{ + if (!m_json) + { + m_json = cJSON_AS4CPP_CreateObject(); + } + + auto arrayValue = cJSON_AS4CPP_CreateArray(); + for (unsigned i = 0; i < array.GetLength(); ++i) + { + cJSON_AS4CPP_AddItemToArray(arrayValue, array[i].m_json); + array[i].m_json = nullptr; + } + + AddOrReplace(m_json, key.c_str(), arrayValue); + return *this; +} + +Document& Document::AsArray(const Array<Document>& array) +{ + auto arrayValue = cJSON_AS4CPP_CreateArray(); + for (unsigned i = 0; i < array.GetLength(); ++i) + { + cJSON_AS4CPP_AddItemToArray(arrayValue, cJSON_AS4CPP_Duplicate(array[i].m_json, true /*recurse*/)); + } + + Destroy(); + m_json = arrayValue; + return *this; +} + +Document& Document::AsArray(Array<Document>&& array) +{ + auto arrayValue = cJSON_AS4CPP_CreateArray(); + for (unsigned i = 0; i < array.GetLength(); ++i) + { + cJSON_AS4CPP_AddItemToArray(arrayValue, array[i].m_json); + array[i].m_json = nullptr; + } + + Destroy(); + m_json = arrayValue; + return *this; +} + +Document& Document::WithObject(const char* key, const Document& value) +{ + if (!m_json) + { + m_json = cJSON_AS4CPP_CreateObject(); + } + + const auto copy = value.m_json == nullptr ? cJSON_AS4CPP_CreateObject() : cJSON_AS4CPP_Duplicate(value.m_json, true /*recurse*/); + AddOrReplace(m_json, key, copy); + return *this; +} + +Document& Document::WithObject(const Aws::String& key, const Document& value) +{ + return WithObject(key.c_str(), value); +} + +Document& Document::WithObject(const char* key, Document&& value) +{ + if (!m_json) + { + m_json = cJSON_AS4CPP_CreateObject(); + } + + AddOrReplace(m_json, key, value.m_json == nullptr ? cJSON_AS4CPP_CreateObject() : value.m_json); + value.m_json = nullptr; + return *this; +} + +Document& Document::WithObject(const Aws::String& key, Document&& value) +{ + return WithObject(key.c_str(), std::move(value)); +} + +Document& Document::AsObject(const Document& value) +{ + *this = value; + return *this; +} + +Document& Document::AsObject(Document && value) +{ + *this = std::move(value); + return *this; +} + +DocumentView Document::View() const +{ + return *this; +} + +DocumentView::DocumentView() : m_json(nullptr) +{ +} + +DocumentView::DocumentView(const Document& value) : m_json(value.m_json) +{ +} + +DocumentView::DocumentView(cJSON* v) : m_json(v) +{ +} + +DocumentView& DocumentView::operator=(const Document& value) +{ + m_json = value.m_json; + return *this; +} + +DocumentView& DocumentView::operator=(cJSON* value) +{ + m_json = value; + return *this; +} + +Aws::String DocumentView::GetString(const Aws::String& key) const +{ + assert(m_json); + auto item = cJSON_AS4CPP_GetObjectItemCaseSensitive(m_json, key.c_str()); + auto str = cJSON_AS4CPP_GetStringValue(item); + return str ? str : ""; +} + +Aws::String DocumentView::AsString() const +{ + const char* str = cJSON_AS4CPP_GetStringValue(m_json); + if (str == nullptr) + { + return {}; + } + return str; +} + +bool DocumentView::IsString() const +{ + return cJSON_AS4CPP_IsString(m_json) != 0; +} + +bool DocumentView::GetBool(const Aws::String& key) const +{ + assert(m_json); + auto item = cJSON_AS4CPP_GetObjectItemCaseSensitive(m_json, key.c_str()); + assert(item); + return item->valueint != 0; +} + +bool DocumentView::AsBool() const +{ + assert(cJSON_AS4CPP_IsBool(m_json)); + return cJSON_AS4CPP_IsTrue(m_json) != 0; +} + +bool DocumentView::IsBool() const +{ + return cJSON_AS4CPP_IsBool(m_json) != 0; +} + +int DocumentView::GetInteger(const Aws::String& key) const +{ + assert(m_json); + auto item = cJSON_AS4CPP_GetObjectItemCaseSensitive(m_json, key.c_str()); + assert(item); + return item->valueint; +} + +int DocumentView::AsInteger() const +{ + assert(cJSON_AS4CPP_IsNumber(m_json)); // can be double or value larger than int_max, but at least not UB + return m_json->valueint; +} + +bool DocumentView::IsIntegerType() const +{ + if (!cJSON_AS4CPP_IsNumber(m_json)) + { + return false; + } + + if (m_json->valuestring) + { + Aws::String valueString = m_json->valuestring; + return std::all_of(valueString.begin(), valueString.end(), [](unsigned char c){ return ::isdigit(c) || c == '+' || c == '-'; }); + } + return m_json->valuedouble == static_cast<long long>(m_json->valuedouble); +} + +int64_t DocumentView::GetInt64(const Aws::String& key) const +{ + assert(m_json); + auto item = cJSON_AS4CPP_GetObjectItemCaseSensitive(m_json, key.c_str()); + assert(item); + if (item->valuestring) + { + return Aws::Utils::StringUtils::ConvertToInt64(item->valuestring); + } + else + { + return static_cast<int64_t>(item->valuedouble); + } +} + +int64_t DocumentView::AsInt64() const +{ + assert(cJSON_AS4CPP_IsNumber(m_json)); + if (m_json->valuestring) + { + return Aws::Utils::StringUtils::ConvertToInt64(m_json->valuestring); + } + else + { + return static_cast<int64_t>(m_json->valuedouble); + } +} + +double DocumentView::GetDouble(const Aws::String& key) const +{ + assert(m_json); + auto item = cJSON_AS4CPP_GetObjectItemCaseSensitive(m_json, key.c_str()); + assert(item); + return item->valuedouble; +} + +double DocumentView::AsDouble() const +{ + assert(cJSON_AS4CPP_IsNumber(m_json)); + return m_json->valuedouble; +} + +bool DocumentView::IsFloatingPointType() const +{ + if (!cJSON_AS4CPP_IsNumber(m_json)) + { + return false; + } + + if (m_json->valuestring) + { + Aws::String valueString = m_json->valuestring; + return std::any_of(valueString.begin(), valueString.end(), [](unsigned char c){ return !::isdigit(c) && c != '+' && c != '-'; }); + } + return m_json->valuedouble != static_cast<long long>(m_json->valuedouble); +} + +Array<DocumentView> DocumentView::GetArray(const Aws::String& key) const +{ + assert(m_json); + auto array = cJSON_AS4CPP_GetObjectItemCaseSensitive(m_json, key.c_str()); + assert(cJSON_AS4CPP_IsArray(array)); + Array<DocumentView> returnArray(cJSON_AS4CPP_GetArraySize(array)); + + auto element = array->child; + for (unsigned i = 0; element && i < returnArray.GetLength(); ++i, element = element->next) + { + returnArray[i] = element; + } + + return returnArray; +} + +Array<DocumentView> DocumentView::AsArray() const +{ + assert(cJSON_AS4CPP_IsArray(m_json)); + Array<DocumentView> returnArray(cJSON_AS4CPP_GetArraySize(m_json)); + + auto element = m_json->child; + + for (unsigned i = 0; element && i < returnArray.GetLength(); ++i, element = element->next) + { + returnArray[i] = element; + } + + return returnArray; +} + +bool DocumentView::IsListType() const +{ + return cJSON_AS4CPP_IsArray(m_json) != 0; +} + +DocumentView DocumentView::GetObject(const Aws::String& key) const +{ + assert(m_json); + auto item = cJSON_AS4CPP_GetObjectItemCaseSensitive(m_json, key.c_str()); + return item; +} + +DocumentView DocumentView::AsObject() const +{ + assert(cJSON_AS4CPP_IsObject(m_json) || cJSON_AS4CPP_IsNull(m_json)); + return m_json; +} + +bool DocumentView::IsObject() const +{ + return cJSON_AS4CPP_IsObject(m_json) != 0; +} + +bool DocumentView::IsNull() const +{ + return cJSON_AS4CPP_IsNull(m_json) != 0; +} + +Aws::Map<Aws::String, DocumentView> DocumentView::GetAllObjects() const +{ + Aws::Map<Aws::String, DocumentView> valueMap; + if (!m_json) + { + return valueMap; + } + + for (auto iter = m_json->child; iter; iter = iter->next) + { + valueMap.emplace(std::make_pair(Aws::String(iter->string), DocumentView(iter))); + } + + return valueMap; +} + +bool DocumentView::ValueExists(const Aws::String& key) const +{ + if (!cJSON_AS4CPP_IsObject(m_json)) + { + return false; + } + + auto item = cJSON_AS4CPP_GetObjectItemCaseSensitive(m_json, key.c_str()); + return !(item == nullptr || cJSON_AS4CPP_IsNull(item)); +} + +bool DocumentView::KeyExists(const Aws::String& key) const +{ + if (!cJSON_AS4CPP_IsObject(m_json)) + { + return false; + } + + return cJSON_AS4CPP_GetObjectItemCaseSensitive(m_json, key.c_str()) != nullptr;; +} + +Aws::String DocumentView::WriteCompact() const +{ + if (!m_json) + { + return "null"; + } + + auto temp = cJSON_AS4CPP_PrintUnformatted(m_json); + Aws::String out(temp); + cJSON_AS4CPP_free(temp); + return out; +} + +Aws::String DocumentView::WriteReadable() const +{ + if (!m_json) + { + return "null"; + } + + auto temp = cJSON_AS4CPP_Print(m_json); + Aws::String out(temp); + cJSON_AS4CPP_free(temp); + return out; +} + +Document DocumentView::Materialize() const +{ + return m_json; +} diff --git a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/HashingUtils.cpp b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/HashingUtils.cpp index 0e49a616343..0431835a615 100644 --- a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/HashingUtils.cpp +++ b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/HashingUtils.cpp @@ -11,6 +11,7 @@ #include <aws/core/utils/crypto/Sha256HMAC.h> #include <aws/core/utils/crypto/Sha1.h> #include <aws/core/utils/crypto/MD5.h> +#include <aws/core/utils/crypto/CRC32.h> #include <aws/core/utils/Outcome.h> #include <aws/core/utils/memory/stl/AWSStringStream.h> #include <aws/core/utils/memory/stl/AWSList.h> @@ -234,6 +235,30 @@ ByteBuffer HashingUtils::CalculateMD5(Aws::IOStream& stream) return hash.Calculate(stream).GetResult(); } +ByteBuffer HashingUtils::CalculateCRC32(const Aws::String& str) +{ + CRC32 hash; + return hash.Calculate(str).GetResult(); +} + +ByteBuffer HashingUtils::CalculateCRC32(Aws::IOStream& stream) +{ + CRC32 hash; + return hash.Calculate(stream).GetResult(); +} + +ByteBuffer HashingUtils::CalculateCRC32C(const Aws::String& str) +{ + CRC32C hash; + return hash.Calculate(str).GetResult(); +} + +ByteBuffer HashingUtils::CalculateCRC32C(Aws::IOStream& stream) +{ + CRC32C hash; + return hash.Calculate(stream).GetResult(); +} + int HashingUtils::HashString(const char* strToHash) { if (!strToHash) @@ -242,7 +267,7 @@ int HashingUtils::HashString(const char* strToHash) unsigned hash = 0; while (char charValue = *strToHash++) { - hash = charValue + 31 * hash; + hash = charValue + 31 * hash; } return hash; diff --git a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/crypto/CRC32.cpp b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/crypto/CRC32.cpp new file mode 100644 index 00000000000..c09806fbe0b --- /dev/null +++ b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/crypto/CRC32.cpp @@ -0,0 +1,218 @@ +/** + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ + + +#include <aws/core/utils/crypto/CRC32.h> +#include <aws/core/utils/Outcome.h> +#include <aws/core/utils/crypto/Factories.h> +#include <aws/crt/Types.h> +#include <aws/checksums/crc.h> +#include <aws/common/byte_buf.h> + +using namespace Aws::Utils::Crypto; + +static Aws::Utils::ByteBuffer ByteBufferFromInt32(uint32_t value) +{ + Aws::Utils::ByteBuffer buffer(4); + buffer[0] = (value >> 24) & 0xFF; + buffer[1] = (value >> 16) & 0xFF; + buffer[2] = (value >> 8) & 0xFF; + buffer[3] = value & 0xFF; + return buffer; +} + +CRC32::CRC32() : + m_hashImpl(CreateCRC32Implementation()) +{ +} + +CRC32::~CRC32() +{ +} + +HashResult CRC32::Calculate(const Aws::String& str) +{ + return m_hashImpl->Calculate(str); +} + +HashResult CRC32::Calculate(Aws::IStream& stream) +{ + return m_hashImpl->Calculate(stream); +} + +void CRC32::Update(unsigned char* buffer, size_t bufferSize) +{ + m_hashImpl->Update(buffer, bufferSize); +} + +HashResult CRC32::GetHash() +{ + return m_hashImpl->GetHash(); +} + +CRC32C::CRC32C() : + m_hashImpl(CreateCRC32CImplementation()) +{ +} + +CRC32C::~CRC32C() +{ +} + +HashResult CRC32C::Calculate(const Aws::String& str) +{ + return m_hashImpl->Calculate(str); +} + +HashResult CRC32C::Calculate(Aws::IStream& stream) +{ + return m_hashImpl->Calculate(stream); +} + + +void CRC32C::Update(unsigned char* buffer, size_t bufferSize) +{ + m_hashImpl->Update(buffer, bufferSize); +} + +HashResult CRC32C::GetHash() +{ + return m_hashImpl->GetHash(); +} + + +CRC32Impl::CRC32Impl() : m_runningCrc32(0) {} + +HashResult CRC32Impl::Calculate(const Aws::String& str) +{ + Aws::Crt::ByteCursor byteCursor = Aws::Crt::ByteCursorFromArray(reinterpret_cast<const uint8_t*>(str.data()), str.size()); + + uint32_t runningCrc32 = 0; + while (byteCursor.len > INT_MAX) + { + runningCrc32 = aws_checksums_crc32(byteCursor.ptr, INT_MAX, runningCrc32); + aws_byte_cursor_advance(&byteCursor, INT_MAX); + } + runningCrc32 = aws_checksums_crc32(byteCursor.ptr, static_cast<int>(byteCursor.len), runningCrc32); + const Aws::Utils::ByteBuffer& hash = ByteBufferFromInt32(runningCrc32); + return HashResult(std::move(hash)); +} + +HashResult CRC32Impl::Calculate(Aws::IStream& stream) +{ + uint32_t runningCrc32 = 0; + + auto currentPos = stream.tellg(); + if (currentPos == std::ios::pos_type(-1)) + { + currentPos = 0; + stream.clear(); + } + + stream.seekg(0, stream.beg); + + uint8_t streamBuffer[Aws::Utils::Crypto::Hash::INTERNAL_HASH_STREAM_BUFFER_SIZE]; + while (stream.good()) + { + stream.read(reinterpret_cast<char*>(streamBuffer), Aws::Utils::Crypto::Hash::INTERNAL_HASH_STREAM_BUFFER_SIZE); + auto bytesRead = stream.gcount(); + + if (bytesRead > 0) + { + runningCrc32 = aws_checksums_crc32(streamBuffer, static_cast<int>(bytesRead), runningCrc32); + } + } + + stream.clear(); + stream.seekg(currentPos, stream.beg); + + const Aws::Utils::ByteBuffer& hash = ByteBufferFromInt32(runningCrc32); + return HashResult(std::move(hash)); +} + +void CRC32Impl::Update(unsigned char* buffer, size_t bufferSize) +{ + Aws::Crt::ByteCursor byteCursor = Aws::Crt::ByteCursorFromArray(buffer, bufferSize); + + while (byteCursor.len > INT_MAX) + { + m_runningCrc32 = aws_checksums_crc32(byteCursor.ptr, INT_MAX, m_runningCrc32); + aws_byte_cursor_advance(&byteCursor, INT_MAX); + } + m_runningCrc32 = aws_checksums_crc32(byteCursor.ptr, static_cast<int>(byteCursor.len), m_runningCrc32); +} + +HashResult CRC32Impl::GetHash() +{ + const Aws::Utils::ByteBuffer& hash = ByteBufferFromInt32(m_runningCrc32); + return HashResult(std::move(hash)); +} + +CRC32CImpl::CRC32CImpl() : m_runningCrc32c(0) {} + +HashResult CRC32CImpl::Calculate(const Aws::String& str) +{ + Aws::Crt::ByteCursor byteCursor = Aws::Crt::ByteCursorFromArray(reinterpret_cast<const uint8_t*>(str.data()), str.size()); + + uint32_t runningCrc32c = 0; + while (byteCursor.len > INT_MAX) + { + runningCrc32c = aws_checksums_crc32c(byteCursor.ptr, INT_MAX, runningCrc32c); + aws_byte_cursor_advance(&byteCursor, INT_MAX); + } + runningCrc32c = aws_checksums_crc32c(byteCursor.ptr, static_cast<int>(byteCursor.len), runningCrc32c); + const Aws::Utils::ByteBuffer& hash = ByteBufferFromInt32(runningCrc32c); + return HashResult(std::move(hash)); +} + +HashResult CRC32CImpl::Calculate(Aws::IStream& stream) +{ + uint32_t runningCrc32c = 0; + + auto currentPos = stream.tellg(); + if (currentPos == std::ios::pos_type(-1)) + { + currentPos = 0; + stream.clear(); + } + + stream.seekg(0, stream.beg); + + uint8_t streamBuffer[Aws::Utils::Crypto::Hash::INTERNAL_HASH_STREAM_BUFFER_SIZE]; + while (stream.good()) + { + stream.read(reinterpret_cast<char*>(streamBuffer), Aws::Utils::Crypto::Hash::INTERNAL_HASH_STREAM_BUFFER_SIZE); + auto bytesRead = stream.gcount(); + + if (bytesRead > 0) + { + runningCrc32c = aws_checksums_crc32c(streamBuffer, static_cast<int>(bytesRead), runningCrc32c); + } + } + + stream.clear(); + stream.seekg(currentPos, stream.beg); + + const Aws::Utils::ByteBuffer& hash = ByteBufferFromInt32(runningCrc32c); + return HashResult(std::move(hash)); +} + +void CRC32CImpl::Update(unsigned char* buffer, size_t bufferSize) +{ + Aws::Crt::ByteCursor byteCursor = Aws::Crt::ByteCursorFromArray(buffer, bufferSize); + + while (byteCursor.len > INT_MAX) + { + m_runningCrc32c = aws_checksums_crc32c(byteCursor.ptr, INT_MAX, m_runningCrc32c); + aws_byte_cursor_advance(&byteCursor, INT_MAX); + } + m_runningCrc32c = aws_checksums_crc32c(byteCursor.ptr, static_cast<int>(byteCursor.len), m_runningCrc32c); +} + +HashResult CRC32CImpl::GetHash() +{ + const Aws::Utils::ByteBuffer& hash = ByteBufferFromInt32(m_runningCrc32c); + return HashResult(std::move(hash)); +} diff --git a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/crypto/MD5.cpp b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/crypto/MD5.cpp index bf14ace1ad3..f442878a90b 100644 --- a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/crypto/MD5.cpp +++ b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/crypto/MD5.cpp @@ -11,7 +11,7 @@ using namespace Aws::Utils::Crypto; -MD5::MD5() : +MD5::MD5() : m_hashImpl(CreateMD5Implementation()) { } @@ -28,4 +28,14 @@ HashResult MD5::Calculate(const Aws::String& str) HashResult MD5::Calculate(Aws::IStream& stream) { return m_hashImpl->Calculate(stream); -}
\ No newline at end of file +} + +void MD5::Update(unsigned char* buffer, size_t bufferSize) +{ + return m_hashImpl->Update(buffer, bufferSize); +} + +HashResult MD5::GetHash() +{ + return m_hashImpl->GetHash(); +} diff --git a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/crypto/Sha1.cpp b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/crypto/Sha1.cpp index 5da3e63d28f..a6783e18f0f 100644 --- a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/crypto/Sha1.cpp +++ b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/crypto/Sha1.cpp @@ -28,3 +28,13 @@ HashResult Sha1::Calculate(Aws::IStream& stream) { return m_hashImpl->Calculate(stream); } + +void Sha1::Update(unsigned char* buffer, size_t bufferSize) +{ + return m_hashImpl->Update(buffer, bufferSize); +} + +HashResult Sha1::GetHash() +{ + return m_hashImpl->GetHash(); +}
\ No newline at end of file diff --git a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/crypto/Sha256.cpp b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/crypto/Sha256.cpp index a8aa5ae8790..48612e8cf03 100644 --- a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/crypto/Sha256.cpp +++ b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/crypto/Sha256.cpp @@ -27,4 +27,14 @@ HashResult Sha256::Calculate(const Aws::String& str) HashResult Sha256::Calculate(Aws::IStream& stream) { return m_hashImpl->Calculate(stream); -}
\ No newline at end of file +} + +void Sha256::Update(unsigned char* buffer, size_t bufferSize) +{ + return m_hashImpl->Update(buffer, bufferSize); +} + +HashResult Sha256::GetHash() +{ + return m_hashImpl->GetHash(); +} diff --git a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/crypto/factory/Factories.cpp b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/crypto/factory/Factories.cpp index 88ca147d116..cba90af4f4d 100644 --- a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/crypto/factory/Factories.cpp +++ b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/crypto/factory/Factories.cpp @@ -7,6 +7,7 @@ #include <aws/core/utils/crypto/Factories.h> #include <aws/core/utils/crypto/Hash.h> #include <aws/core/utils/crypto/HMAC.h> +#include <aws/core/utils/crypto/CRC32.h> #if ENABLE_BCRYPT_ENCRYPTION #error #include <aws/core/utils/crypto/bcrypt/CryptoImpl.h> @@ -35,6 +36,18 @@ static std::shared_ptr<HashFactory>& GetMD5Factory() return s_MD5Factory; } +static std::shared_ptr<HashFactory>& GetCRC32Factory() +{ + static std::shared_ptr<HashFactory> s_CRC32Factory(nullptr); + return s_CRC32Factory; +} + +static std::shared_ptr<HashFactory>& GetCRC32CFactory() +{ + static std::shared_ptr<HashFactory> s_CRC32CFactory(nullptr); + return s_CRC32CFactory; +} + static std::shared_ptr<HashFactory>& GetSha1Factory() { static std::shared_ptr<HashFactory> s_Sha1Factory(nullptr); @@ -136,6 +149,24 @@ public: } }; +class DefaultCRC32Factory : public HashFactory +{ +public: + std::shared_ptr<Hash> CreateImplementation() const override + { + return Aws::MakeShared<CRC32Impl>(s_allocationTag); + } +}; + +class DefaultCRC32CFactory : public HashFactory +{ +public: + std::shared_ptr<Hash> CreateImplementation() const override + { + return Aws::MakeShared<CRC32CImpl>(s_allocationTag); + } +}; + class DefaultSHA1Factory : public HashFactory { public: @@ -667,6 +698,16 @@ void Aws::Utils::Crypto::InitCrypto() GetMD5Factory()->InitStaticState(); } + if(!GetCRC32Factory()) + { + GetCRC32Factory() = Aws::MakeShared<DefaultCRC32Factory>(s_allocationTag); + } + + if(!GetCRC32CFactory()) + { + GetCRC32CFactory() = Aws::MakeShared<DefaultCRC32CFactory>(s_allocationTag); + } + if(GetSha1Factory()) { GetSha1Factory()->InitStaticState(); @@ -754,6 +795,16 @@ void Aws::Utils::Crypto::CleanupCrypto() GetMD5Factory() = nullptr; } + if(GetCRC32CFactory()) + { + GetCRC32Factory() = nullptr; + } + + if(GetCRC32CFactory()) + { + GetCRC32CFactory() = nullptr; + } + if(GetSha1Factory()) { GetSha1Factory()->CleanupStaticState(); @@ -809,6 +860,16 @@ void Aws::Utils::Crypto::SetMD5Factory(const std::shared_ptr<HashFactory>& facto GetMD5Factory() = factory; } +void Aws::Utils::Crypto::SetCRC32Factory(const std::shared_ptr<HashFactory>& factory) +{ + GetCRC32Factory() = factory; +} + +void Aws::Utils::Crypto::SetCRC32CFactory(const std::shared_ptr<HashFactory>& factory) +{ + GetCRC32CFactory() = factory; +} + void Aws::Utils::Crypto::SetSha1Factory(const std::shared_ptr<HashFactory>& factory) { GetSha1Factory() = factory; @@ -854,6 +915,16 @@ std::shared_ptr<Hash> Aws::Utils::Crypto::CreateMD5Implementation() return GetMD5Factory()->CreateImplementation(); } +std::shared_ptr<Hash> Aws::Utils::Crypto::CreateCRC32Implementation() +{ + return GetCRC32Factory()->CreateImplementation(); +} + +std::shared_ptr<Hash> Aws::Utils::Crypto::CreateCRC32CImplementation() +{ + return GetCRC32CFactory()->CreateImplementation(); +} + std::shared_ptr<Hash> Aws::Utils::Crypto::CreateSha1Implementation() { return GetSha1Factory()->CreateImplementation(); @@ -967,5 +1038,5 @@ std::shared_ptr<SymmetricCipher> Aws::Utils::Crypto::CreateAES_KeyWrapImplementa std::shared_ptr<SecureRandomBytes> Aws::Utils::Crypto::CreateSecureRandomBytesImplementation() { - return GetSecureRandom(); + return GetSecureRandomFactory()->CreateImplementation(); } diff --git a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/crypto/openssl/CryptoImpl.cpp b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/crypto/openssl/CryptoImpl.cpp index 3a89265e6ec..faebde3a8d5 100644 --- a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/crypto/openssl/CryptoImpl.cpp +++ b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/crypto/openssl/CryptoImpl.cpp @@ -8,6 +8,7 @@ #include <aws/core/utils/memory/AWSMemory.h> #include <aws/core/utils/crypto/openssl/CryptoImpl.h> #include <aws/core/utils/Outcome.h> +#include <openssl/crypto.h> #include <openssl/md5.h> #ifdef OPENSSL_IS_BORINGSSL @@ -47,9 +48,19 @@ namespace Aws */ #if defined(LIBRESSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER == 0x20000000L) #undef OPENSSL_VERSION_NUMBER +#if LIBRESSL_VERSION_NUMBER < 0x3050000fL #define OPENSSL_VERSION_NUMBER 0x1000107fL +#else +#define OPENSSL_VERSION_NUMBER 0x1010000fL +#endif #endif + #define OPENSSL_VERSION_LESS_1_1 (OPENSSL_VERSION_NUMBER < 0x10100003L) +#define OPENSSL_VERSION_LESS_3_0 (OPENSSL_VERSION_NUMBER < 0x30000000L) + +#if !OPENSSL_VERSION_LESS_3_0 +#error #include <openssl/core_names.h> +#endif #if OPENSSL_VERSION_LESS_1_1 static const char* OPENSSL_INTERNALS_TAG = "OpenSSLCallbackState"; @@ -65,7 +76,7 @@ namespace Aws #else OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS /*options*/ ,NULL /* OpenSSL init settings*/ ); #endif -#if !defined(OPENSSL_IS_BORINGSSL) +#if !(defined(OPENSSL_IS_BORINGSSL) || defined(OPENSSL_IS_AWSLC)) OPENSSL_add_all_algorithms_noconf(); #endif #if OPENSSL_VERSION_LESS_1_1 @@ -168,6 +179,22 @@ namespace Aws EVP_MD_CTX *m_ctx; }; + MD5OpenSSLImpl::MD5OpenSSLImpl() + { + m_ctx = EVP_MD_CTX_create(); + assert(m_ctx != nullptr); +#if !defined(OPENSSL_IS_BORINGSSL) + EVP_MD_CTX_set_flags(m_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW); +#endif + EVP_DigestInit_ex(m_ctx, EVP_md5(), nullptr); + } + + MD5OpenSSLImpl::~MD5OpenSSLImpl() + { + EVP_MD_CTX_destroy(m_ctx); + m_ctx = nullptr; + } + HashResult MD5OpenSSLImpl::Calculate(const Aws::String& str) { OpensslCtxRAIIGuard guard; @@ -222,6 +249,34 @@ namespace Aws return HashResult(std::move(hash)); } + void MD5OpenSSLImpl::Update(unsigned char* buffer, size_t bufferSize) + { + EVP_DigestUpdate(m_ctx, buffer, bufferSize); + } + + HashResult MD5OpenSSLImpl::GetHash() + { + ByteBuffer hash(EVP_MD_size(EVP_md5())); + EVP_DigestFinal(m_ctx, hash.GetUnderlyingData(), nullptr); + return HashResult(std::move(hash)); + } + + Sha1OpenSSLImpl::Sha1OpenSSLImpl() + { + m_ctx = EVP_MD_CTX_create(); + assert(m_ctx != nullptr); +#if !defined(OPENSSL_IS_BORINGSSL) + EVP_MD_CTX_set_flags(m_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW); +#endif + EVP_DigestInit_ex(m_ctx, EVP_sha1(), nullptr); + } + + Sha1OpenSSLImpl::~Sha1OpenSSLImpl() + { + EVP_MD_CTX_destroy(m_ctx); + m_ctx = nullptr; + } + HashResult Sha1OpenSSLImpl::Calculate(const Aws::String& str) { OpensslCtxRAIIGuard guard; @@ -272,6 +327,34 @@ namespace Aws return HashResult(std::move(hash)); } + void Sha1OpenSSLImpl::Update(unsigned char* buffer, size_t bufferSize) + { + EVP_DigestUpdate(m_ctx, buffer, bufferSize); + } + + HashResult Sha1OpenSSLImpl::GetHash() + { + ByteBuffer hash(EVP_MD_size(EVP_sha1())); + EVP_DigestFinal(m_ctx, hash.GetUnderlyingData(), nullptr); + return HashResult(std::move(hash)); + } + + Sha256OpenSSLImpl::Sha256OpenSSLImpl() + { + m_ctx = EVP_MD_CTX_create(); + assert(m_ctx != nullptr); +#if !defined(OPENSSL_IS_BORINGSSL) + EVP_MD_CTX_set_flags(m_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW); +#endif + EVP_DigestInit_ex(m_ctx, EVP_sha256(), nullptr); + } + + Sha256OpenSSLImpl::~Sha256OpenSSLImpl() + { + EVP_MD_CTX_destroy(m_ctx); + m_ctx = nullptr; + } + HashResult Sha256OpenSSLImpl::Calculate(const Aws::String& str) { OpensslCtxRAIIGuard guard; @@ -322,13 +405,28 @@ namespace Aws return HashResult(std::move(hash)); } + void Sha256OpenSSLImpl::Update(unsigned char* buffer, size_t bufferSize) + { + EVP_DigestUpdate(m_ctx, buffer, bufferSize); + } + + HashResult Sha256OpenSSLImpl::GetHash() + { + ByteBuffer hash(EVP_MD_size(EVP_sha256())); + EVP_DigestFinal(m_ctx, hash.GetUnderlyingData(), nullptr); + return HashResult(std::move(hash)); + } + class HMACRAIIGuard { public: HMACRAIIGuard() { #if OPENSSL_VERSION_LESS_1_1 m_ctx = Aws::New<HMAC_CTX>("AllocSha256HAMCOpenSSLContext"); -#else +#elif OPENSSL_VERSION_LESS_3_0 m_ctx = HMAC_CTX_new(); +#else + m_mac = EVP_MAC_fetch(NULL, "HMAC", NULL); + m_ctx = EVP_MAC_CTX_new(m_mac); #endif assert(m_ctx != nullptr); } @@ -336,17 +434,29 @@ namespace Aws ~HMACRAIIGuard() { #if OPENSSL_VERSION_LESS_1_1 Aws::Delete<HMAC_CTX>(m_ctx); -#else +#elif OPENSSL_VERSION_LESS_3_0 HMAC_CTX_free(m_ctx); +#else + EVP_MAC_free(m_mac); + EVP_MAC_CTX_free(m_ctx); #endif m_ctx = nullptr; } +#if OPENSSL_VERSION_LESS_3_0 HMAC_CTX* getResource() { +#else + EVP_MAC_CTX* getResource() { +#endif return m_ctx; } private: +#if OPENSSL_VERSION_LESS_3_0 HMAC_CTX *m_ctx; +#else + EVP_MAC *m_mac; + EVP_MAC_CTX *m_ctx; +#endif }; HashResult Sha256HMACOpenSSLImpl::Calculate(const ByteBuffer& toSign, const ByteBuffer& secret) @@ -356,20 +466,36 @@ namespace Aws memset(digest.GetUnderlyingData(), 0, length); HMACRAIIGuard guard; +#if OPENSSL_VERSION_LESS_3_0 HMAC_CTX* m_ctx = guard.getResource(); +#else + EVP_MAC_CTX* m_ctx = guard.getResource(); +#endif #if OPENSSL_VERSION_LESS_1_1 HMAC_CTX_init(m_ctx); #endif +#if OPENSSL_VERSION_LESS_3_0 HMAC_Init_ex(m_ctx, secret.GetUnderlyingData(), static_cast<int>(secret.GetLength()), EVP_sha256(), NULL); HMAC_Update(m_ctx, toSign.GetUnderlyingData(), toSign.GetLength()); HMAC_Final(m_ctx, digest.GetUnderlyingData(), &length); +#else + char sha256[] {"SHA256"}; + OSSL_PARAM ossl_params[2]; + ossl_params[0] = + OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, sha256, 0); + ossl_params[1] = OSSL_PARAM_construct_end(); + EVP_MAC_init(m_ctx, secret.GetUnderlyingData(), + static_cast<int>(secret.GetLength()), ossl_params); + EVP_MAC_update(m_ctx, toSign.GetUnderlyingData(), toSign.GetLength()); + EVP_MAC_final(m_ctx, digest.GetUnderlyingData(), NULL, length); +#endif #if OPENSSL_VERSION_LESS_1_1 HMAC_CTX_cleanup(m_ctx); -#else +#elif OPENSSL_VERSION_LESS_3_0 HMAC_CTX_reset(m_ctx); #endif return HashResult(std::move(digest)); @@ -547,7 +673,7 @@ namespace Aws CryptoBuffer finalBlock(GetBlockSizeBytes()); int writtenSize = static_cast<int>(finalBlock.GetLength()); int ret = EVP_DecryptFinal_ex(m_decryptor_ctx, finalBlock.GetUnderlyingData(), &writtenSize); -#if OPENSSL_VERSION_NUMBER > 0x1010104fL //1.1.1d +#if !defined(OPENSSL_IS_AWSLC) && OPENSSL_VERSION_NUMBER > 0x1010104fL //1.1.1d if (ret <= 0) #else if (ret <= 0 && !m_emptyPlaintext) // see details why making exception for empty string at: https://github.com/aws/aws-sdk-cpp/issues/1413 diff --git a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/event/EventStreamDecoder.cpp b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/event/EventStreamDecoder.cpp index f70a6c88f61..053ff938d44 100644 --- a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/event/EventStreamDecoder.cpp +++ b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/event/EventStreamDecoder.cpp @@ -72,9 +72,7 @@ namespace Aws assert(handler); if (!handler) { - AWS_LOGSTREAM_ERROR(EVENT_STREAM_DECODER_CLASS_TAG, "Payload received, but decoder encountered internal errors before." - "ErrorCode: " << EventStreamErrorsMapper::GetNameForError(handler->GetInternalError()) << ", " - "ErrorMessage: " << handler->GetEventPayloadAsString()); + AWS_LOGSTREAM_ERROR(EVENT_STREAM_DECODER_CLASS_TAG, "Payload received, but handler is null."); return; } handler->WriteMessageEventPayload(static_cast<unsigned char*>(payload->buffer), payload->len); @@ -129,9 +127,7 @@ namespace Aws assert(handler); if (!handler) { - AWS_LOGSTREAM_ERROR(EVENT_STREAM_DECODER_CLASS_TAG, "Payload received, but decoder encountered internal errors before." - "ErrorCode: " << EventStreamErrorsMapper::GetNameForError(handler->GetInternalError()) << ", " - "ErrorMessage: " << handler->GetEventPayloadAsString()); + AWS_LOGSTREAM_ERROR(EVENT_STREAM_DECODER_CLASS_TAG, "Header received, but handler is null."); return; } diff --git a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/event/EventStreamEncoder.cpp b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/event/EventStreamEncoder.cpp index ef7104e839c..750bf9e1e6d 100644 --- a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/event/EventStreamEncoder.cpp +++ b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/event/EventStreamEncoder.cpp @@ -80,80 +80,83 @@ namespace Aws Aws::Vector<unsigned char> EventStreamEncoder::EncodeAndSign(const Aws::Utils::Event::Message& msg) { - aws_event_stream_message encoded = Encode(msg); - aws_event_stream_message signedMessage = Sign(&encoded); + Aws::Vector<unsigned char> outputBits; - const auto signedMessageLength = signedMessage.message_buffer ? aws_event_stream_message_total_length(&signedMessage) : 0; + aws_event_stream_message encoded; + if (InitEncodedStruct(msg, &encoded)) + { + aws_event_stream_message signedMessage; + if (InitSignedStruct(&encoded, &signedMessage)) + { + // success! + const auto signedMessageBuffer = aws_event_stream_message_buffer(&signedMessage); + const auto signedMessageLength = aws_event_stream_message_total_length(&signedMessage); + outputBits.reserve(signedMessageLength); + outputBits.insert(outputBits.end(), signedMessageBuffer, signedMessageBuffer + signedMessageLength); + + aws_event_stream_message_clean_up(&signedMessage); + } + aws_event_stream_message_clean_up(&encoded); + } - Aws::Vector<unsigned char> outputBits(signedMessage.message_buffer, signedMessage.message_buffer + signedMessageLength); - aws_event_stream_message_clean_up(&encoded); - aws_event_stream_message_clean_up(&signedMessage); return outputBits; } - aws_event_stream_message EventStreamEncoder::Encode(const Aws::Utils::Event::Message& msg) + bool EventStreamEncoder::InitEncodedStruct(const Aws::Utils::Event::Message& msg, aws_event_stream_message* encoded) { + bool success = false; + aws_array_list headers; EncodeHeaders(msg, &headers); - aws_byte_buf payload; - payload.len = msg.GetEventPayload().size(); - // this const_cast is OK because aws_byte_buf will only be "read from" by the following functions. - payload.buffer = const_cast<uint8_t*>(msg.GetEventPayload().data()); - payload.capacity = 0; - payload.allocator = nullptr; + aws_byte_buf payload = aws_byte_buf_from_array(msg.GetEventPayload().data(), msg.GetEventPayload().size()); - aws_event_stream_message encoded; - if(aws_event_stream_message_init(&encoded, get_aws_allocator(), &headers, &payload) == AWS_OP_ERR) + if(aws_event_stream_message_init(encoded, get_aws_allocator(), &headers, &payload) == AWS_OP_SUCCESS) + { + success = true; + } + else { AWS_LOGSTREAM_ERROR(TAG, "Error creating event-stream message from payload."); - aws_event_stream_headers_list_cleanup(&headers); - // GCC 4.9.4 issues a warning with -Wextra if we simply do - // return {}; - aws_event_stream_message empty{nullptr, nullptr, 0}; - return empty; } + aws_event_stream_headers_list_cleanup(&headers); - return encoded; + return success; } - aws_event_stream_message EventStreamEncoder::Sign(aws_event_stream_message* msg) + bool EventStreamEncoder::InitSignedStruct(const aws_event_stream_message* msg, aws_event_stream_message* signedmsg) { - const auto msglen = msg->message_buffer ? aws_event_stream_message_total_length(msg) : 0; + bool success = false; + + const auto msgbuf = aws_event_stream_message_buffer(msg); + const auto msglen = aws_event_stream_message_total_length(msg); Event::Message signedMessage; - signedMessage.WriteEventPayload(msg->message_buffer, msglen); + signedMessage.WriteEventPayload(msgbuf, msglen); assert(m_signer); - if (!m_signer->SignEventMessage(signedMessage, m_signatureSeed)) + if (m_signer->SignEventMessage(signedMessage, m_signatureSeed)) { - AWS_LOGSTREAM_ERROR(TAG, "Failed to sign event message frame."); - // GCC 4.9.4 issues a warning with -Wextra if we simply do - // return {}; - aws_event_stream_message empty{nullptr, nullptr, 0}; - return empty; - } - - aws_array_list headers; - EncodeHeaders(signedMessage, &headers); + aws_array_list headers; + EncodeHeaders(signedMessage, &headers); - aws_byte_buf payload; - payload.len = signedMessage.GetEventPayload().size(); - payload.buffer = signedMessage.GetEventPayload().data(); - payload.capacity = 0; - payload.allocator = nullptr; + aws_byte_buf payload = aws_byte_buf_from_array(signedMessage.GetEventPayload().data(), signedMessage.GetEventPayload().size()); - aws_event_stream_message signedmsg; - if(aws_event_stream_message_init(&signedmsg, get_aws_allocator(), &headers, &payload)) - { - AWS_LOGSTREAM_ERROR(TAG, "Error creating event-stream message from payload."); + if(aws_event_stream_message_init(signedmsg, get_aws_allocator(), &headers, &payload) == AWS_OP_SUCCESS) + { + success = true; + } + else + { + AWS_LOGSTREAM_ERROR(TAG, "Error creating event-stream message from payload."); + } aws_event_stream_headers_list_cleanup(&headers); - // GCC 4.9.4 issues a warning with -Wextra if we simply do - // return {}; - aws_event_stream_message empty{nullptr, nullptr, 0}; - return empty; } - aws_event_stream_headers_list_cleanup(&headers); - return signedmsg; + else + { + AWS_LOGSTREAM_ERROR(TAG, "Failed to sign event message frame."); + } + + return success; } } // namespace Event diff --git a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/json/JsonSerializer.cpp b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/json/JsonSerializer.cpp index 9358d00c0a8..ebfd5d44568 100644 --- a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/json/JsonSerializer.cpp +++ b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/json/JsonSerializer.cpp @@ -9,6 +9,7 @@ #include <algorithm> #include <aws/core/utils/memory/stl/AWSStringStream.h> #include <aws/core/utils/StringUtils.h> +#include <aws/core/utils/Document.h> using namespace Aws::Utils; using namespace Aws::Utils::Json; @@ -68,6 +69,13 @@ JsonValue::JsonValue(JsonValue&& value) : value.m_value = nullptr; } +JsonValue::JsonValue(const Aws::Utils::DocumentView& value) : + m_value(cJSON_AS4CPP_Duplicate(value.m_json, true/*recurse*/)), + m_wasParseSuccessful(true), + m_errorMessage({}) +{ +} + void JsonValue::Destroy() { cJSON_AS4CPP_Delete(m_value); @@ -106,6 +114,15 @@ JsonValue& JsonValue::operator=(JsonValue&& other) return *this; } +JsonValue& JsonValue::operator=(const Aws::Utils::DocumentView& other) +{ + Destroy(); + m_value = cJSON_AS4CPP_Duplicate(other.m_json, true /*recurse*/); + m_wasParseSuccessful = true; + m_errorMessage = {}; + return *this; +} + static void AddOrReplace(cJSON* root, const char* key, cJSON* value) { const auto existing = cJSON_AS4CPP_GetObjectItemCaseSensitive(root, key); diff --git a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/logging/CRTLogSystem.cpp b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/logging/CRTLogSystem.cpp new file mode 100644 index 00000000000..81f94d0d3af --- /dev/null +++ b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/logging/CRTLogSystem.cpp @@ -0,0 +1,107 @@ +/** + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ + +#include <aws/core/utils/logging/CRTLogSystem.h> +#include <aws/core/utils/logging/AWSLogging.h> +#include <aws/core/utils/logging/LogSystemInterface.h> +#include <aws/core/utils/Array.h> +#include <aws/common/common.h> +#include <cstdarg> + +using namespace Aws::Utils; +using namespace Aws::Utils::Logging; + +namespace Aws +{ + namespace Utils + { + namespace Logging + { + static int s_aws_logger_redirect_log( + struct aws_logger *logger, + enum aws_log_level log_level, + aws_log_subject_t subject, + const char *format, ...) + { + DefaultCRTLogSystem* crtLogSystem = reinterpret_cast<DefaultCRTLogSystem*>(logger->p_impl); + Logging::LogLevel logLevel = static_cast<LogLevel>(log_level); + const char* subjectName = aws_log_subject_name(subject); + va_list args; + va_start(args, format); + crtLogSystem->Log(logLevel, subjectName, format, args); + va_end(args); + return AWS_OP_SUCCESS; + } + + static enum aws_log_level s_aws_logger_redirect_get_log_level(struct aws_logger *logger, aws_log_subject_t subject) { + (void)subject; + DefaultCRTLogSystem* crtLogSystem = reinterpret_cast<DefaultCRTLogSystem*>(logger->p_impl); + return (aws_log_level)(crtLogSystem->GetLogLevel()); + } + + static void s_aws_logger_redirect_clean_up(struct aws_logger *logger) { + (void)logger; + } + + static int s_aws_logger_redirect_set_log_level(struct aws_logger *logger, enum aws_log_level log_level) + { + DefaultCRTLogSystem* crtLogSystem = reinterpret_cast<DefaultCRTLogSystem*>(logger->p_impl); + crtLogSystem->SetLogLevel(static_cast<LogLevel>(log_level)); + return AWS_OP_SUCCESS; + } + + static struct aws_logger_vtable s_aws_logger_redirect_vtable = { + s_aws_logger_redirect_log, // .log + s_aws_logger_redirect_get_log_level, // .get_log_level + s_aws_logger_redirect_clean_up, // .clean_up + s_aws_logger_redirect_set_log_level // set_log_level + }; + + DefaultCRTLogSystem::DefaultCRTLogSystem(LogLevel logLevel) : + m_logLevel(logLevel), + m_logger() + { + m_logger.vtable = &s_aws_logger_redirect_vtable; + m_logger.allocator = Aws::get_aws_allocator(); + m_logger.p_impl = this; + + aws_logger_set(&m_logger); + } + + DefaultCRTLogSystem::~DefaultCRTLogSystem() + { + if (aws_logger_get() == &m_logger) + { + aws_logger_set(NULL); + aws_logger_clean_up(&m_logger); + } + } + + void DefaultCRTLogSystem::Log(LogLevel logLevel, const char* subjectName, const char* formatStr, va_list args) + { + va_list tmp_args; + va_copy(tmp_args, args); + #ifdef _WIN32 + const int requiredLength = _vscprintf(formatStr, tmp_args) + 1; + #else + const int requiredLength = vsnprintf(nullptr, 0, formatStr, tmp_args) + 1; + #endif + va_end(tmp_args); + + Array<char> outputBuff(requiredLength); + #ifdef _WIN32 + vsnprintf_s(outputBuff.GetUnderlyingData(), requiredLength, _TRUNCATE, formatStr, args); + #else + vsnprintf(outputBuff.GetUnderlyingData(), requiredLength, formatStr, args); + #endif // _WIN32 + + Aws::OStringStream logStream; + logStream << outputBuff.GetUnderlyingData(); + Logging::GetLogSystem()->LogStream(logLevel, subjectName, logStream); + } + } + } +} + diff --git a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/logging/CRTLogging.cpp b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/logging/CRTLogging.cpp new file mode 100644 index 00000000000..5875ead9c01 --- /dev/null +++ b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/logging/CRTLogging.cpp @@ -0,0 +1,31 @@ +/** + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ + +#include <aws/core/utils/logging/CRTLogging.h> +#include <aws/common/logging.h> +#include <memory> + +using namespace Aws::Utils; +using namespace Aws::Utils::Logging; + +namespace Aws +{ +namespace Utils +{ +namespace Logging { + +static std::shared_ptr<CRTLogSystemInterface> CRTLogSystem(nullptr); + +void InitializeCRTLogging(const std::shared_ptr<CRTLogSystemInterface>& crtLogSystem) { + CRTLogSystem = crtLogSystem; +} + +void ShutdownCRTLogging() { + CRTLogSystem = nullptr; +} + +} // namespace Logging +} // namespace Utils +} // namespace Aws diff --git a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/logging/FormattedLogSystem.cpp b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/logging/FormattedLogSystem.cpp index 41c4d7e09c5..26348b68fe2 100644 --- a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/logging/FormattedLogSystem.cpp +++ b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/logging/FormattedLogSystem.cpp @@ -72,7 +72,7 @@ void FormattedLogSystem::Log(LogLevel logLevel, const char* tag, const char* for va_list tmp_args; //unfortunately you cannot consume a va_list twice va_copy(tmp_args, args); //so we have to copy it - #ifdef WIN32 + #ifdef _WIN32 const int requiredLength = _vscprintf(formatStr, tmp_args) + 1; #else const int requiredLength = vsnprintf(nullptr, 0, formatStr, tmp_args) + 1; @@ -80,11 +80,11 @@ void FormattedLogSystem::Log(LogLevel logLevel, const char* tag, const char* for va_end(tmp_args); Array<char> outputBuff(requiredLength); - #ifdef WIN32 + #ifdef _WIN32 vsnprintf_s(outputBuff.GetUnderlyingData(), requiredLength, _TRUNCATE, formatStr, args); #else vsnprintf(outputBuff.GetUnderlyingData(), requiredLength, formatStr, args); - #endif // WIN32 + #endif // _WIN32 ss << outputBuff.GetUnderlyingData() << std::endl; diff --git a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/stream/ResponseStream.cpp b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/stream/ResponseStream.cpp index 6d1f90ed124..26c92eaafdf 100644 --- a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/stream/ResponseStream.cpp +++ b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/stream/ResponseStream.cpp @@ -5,6 +5,7 @@ #include <aws/core/utils/stream/ResponseStream.h> #include <aws/core/utils/memory/stl/AWSStringStream.h> +#include <aws/core/utils/logging/LogMacros.h> #if defined(_GLIBCXX_FULLY_DYNAMIC_STRING) && _GLIBCXX_FULLY_DYNAMIC_STRING == 0 && defined(__ANDROID__) #include <aws/core/utils/stream/SimpleStreamBuf.h> @@ -15,6 +16,8 @@ using DefaultStreamBufType = Aws::StringBuf; using namespace Aws::Utils::Stream; +const int ResponseStream::ResponseStream::xindex = std::ios_base::xalloc(); + ResponseStream::ResponseStream(void) : m_underlyingStream(nullptr) { @@ -23,16 +26,20 @@ ResponseStream::ResponseStream(void) : ResponseStream::ResponseStream(Aws::IOStream* underlyingStreamToManage) : m_underlyingStream(underlyingStreamToManage) { + RegisterStream(); } ResponseStream::ResponseStream(const Aws::IOStreamFactory& factory) : m_underlyingStream(factory()) { + RegisterStream(); } ResponseStream::ResponseStream(ResponseStream&& toMove) : m_underlyingStream(toMove.m_underlyingStream) { + toMove.DeregisterStream(); toMove.m_underlyingStream = nullptr; + RegisterStream(); } ResponseStream& ResponseStream::operator=(ResponseStream&& toMove) @@ -43,12 +50,26 @@ ResponseStream& ResponseStream::operator=(ResponseStream&& toMove) } ReleaseStream(); + toMove.DeregisterStream(); m_underlyingStream = toMove.m_underlyingStream; toMove.m_underlyingStream = nullptr; + RegisterStream(); return *this; } +Aws::IOStream& ResponseStream::GetUnderlyingStream() const +{ + if (!m_underlyingStream) + { + assert(m_underlyingStream); + AWS_LOGSTREAM_FATAL("ResponseStream", "Unexpected nullptr m_underlyingStream"); + static DefaultUnderlyingStream fallbackStream; // we are already in UB, let's just not crash existing apps + return fallbackStream; + } + return *m_underlyingStream; +} + ResponseStream::~ResponseStream() { ReleaseStream(); @@ -58,13 +79,53 @@ void ResponseStream::ReleaseStream() { if (m_underlyingStream) { - m_underlyingStream->flush(); + DeregisterStream(); Aws::Delete(m_underlyingStream); } m_underlyingStream = nullptr; } +void ResponseStream::RegisterStream() +{ + if (m_underlyingStream) + { + ResponseStream* pThat = static_cast<ResponseStream*>(m_underlyingStream->pword(ResponseStream::xindex)); + if (pThat != nullptr) + { + // callback is already registered + assert(pThat != this); // Underlying stream must not be owned by more than one ResponseStream + } + else + { + m_underlyingStream->register_callback(ResponseStream::StreamCallback, ResponseStream::xindex); + } + m_underlyingStream->pword(ResponseStream::xindex) = this; + } +} + +void ResponseStream::DeregisterStream() +{ + if (m_underlyingStream) + { + assert(static_cast<ResponseStream*>(m_underlyingStream->pword(ResponseStream::xindex)) == this); // Attempt to deregister another ResponseStream's stream + m_underlyingStream->pword(ResponseStream::xindex) = nullptr; // ios does not support deregister, so just erasing the context + } +} + +void ResponseStream::StreamCallback(Aws::IOStream::event evt, std::ios_base& stream, int idx) +{ + if (evt == std::ios_base::erase_event) + { + ResponseStream* pThis = static_cast<ResponseStream*>(stream.pword(idx)); + if (pThis) + { + // m_underlyingStream is being destructed, let's avoid double destruction or having a dangling pointer + pThis->m_underlyingStream = nullptr; + } + } +} + static const char *DEFAULT_STREAM_TAG = "DefaultUnderlyingStream"; DefaultUnderlyingStream::DefaultUnderlyingStream() : diff --git a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/stream/SimpleStreamBuf.cpp b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/stream/SimpleStreamBuf.cpp index 6e429947445..dbf77ab6468 100644 --- a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/stream/SimpleStreamBuf.cpp +++ b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/stream/SimpleStreamBuf.cpp @@ -5,6 +5,7 @@ */ #include <aws/core/utils/stream/SimpleStreamBuf.h> +#include <aws/core/utils/logging/LogMacros.h> #include <algorithm> #include <cassert> @@ -123,7 +124,14 @@ bool SimpleStreamBuf::GrowBuffer() if(currentSize > 0) { - std::memcpy(newBuffer, m_buffer, currentSize); + if(m_buffer) + { + std::memcpy(newBuffer, m_buffer, currentSize); + } + else + { + AWS_LOGSTREAM_FATAL(SIMPLE_STREAMBUF_ALLOCATION_TAG, "Unexpected nullptr m_buffer"); + } } if(m_buffer) diff --git a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/threading/Executor.cpp b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/threading/Executor.cpp index 4a3c4209c41..f9538f00336 100644 --- a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/threading/Executor.cpp +++ b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/threading/Executor.cpp @@ -14,10 +14,15 @@ using namespace Aws::Utils::Threading; bool DefaultExecutor::SubmitToThread(std::function<void()>&& fx) { - auto main = [fx, this] { - fx(); - Detach(std::this_thread::get_id()); - }; + // Generalized lambda capture is C++14, using std::bind as a workaround to force moving fx (instead of copying) + std::function<void()> main = std::bind( + [this](std::function<void()>& storedFx) + { + storedFx(); + Detach(std::this_thread::get_id()); + }, + std::move(fx) + ); State expected; do @@ -25,7 +30,7 @@ bool DefaultExecutor::SubmitToThread(std::function<void()>&& fx) expected = State::Free; if(m_state.compare_exchange_strong(expected, State::Locked)) { - std::thread t(main); + std::thread t(std::move(main)); const auto id = t.get_id(); // copy the id before we std::move the thread m_threads.emplace(id, std::move(t)); m_state = State::Free; diff --git a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/xml/XmlSerializer.cpp b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/xml/XmlSerializer.cpp index c06befaf9b0..2d91f700005 100644 --- a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/xml/XmlSerializer.cpp +++ b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/xml/XmlSerializer.cpp @@ -23,6 +23,8 @@ Aws::String Aws::Utils::Xml::DecodeEscapedXmlText(const Aws::String& textToDecod StringUtils::Replace(decodedString, "<", "<"); StringUtils::Replace(decodedString, ">", ">"); StringUtils::Replace(decodedString, "&", "&"); + StringUtils::Replace(decodedString, "
", "\n"); + StringUtils::Replace(decodedString, "
", "\r"); return decodedString; } |