1
0
mirror of synced 2025-01-30 19:43:43 +01:00

fix: Allow converting very long hex strings to bytes

This commit is contained in:
WerWolv 2023-08-09 20:01:29 +02:00
parent a6aafa8cd6
commit 49d3fe65a3

View File

@ -390,16 +390,22 @@ namespace hex::crypt {
std::vector<u8> decode16(const std::string &input) { std::vector<u8> decode16(const std::string &input) {
std::vector<u8> output(input.length() / 2, 0x00); std::vector<u8> output(input.length() / 2, 0x00);
mbedtls_mpi ctx; mbedtls_mpi ctx;
mbedtls_mpi_init(&ctx); mbedtls_mpi_init(&ctx);
ON_SCOPE_EXIT { mbedtls_mpi_free(&ctx); }; ON_SCOPE_EXIT { mbedtls_mpi_free(&ctx); };
if (mbedtls_mpi_read_string(&ctx, 16, input.c_str())) constexpr static auto BufferSize = 0x100;
return {}; for (size_t offset = 0; offset < input.size(); offset += BufferSize) {
std::string inputPart = input.substr(offset, std::min<size_t>(BufferSize, input.size() - offset));
if (mbedtls_mpi_read_string(&ctx, 16, inputPart.c_str()))
return {};
if (mbedtls_mpi_write_binary(&ctx, output.data(), output.size())) auto size = std::min<size_t>(BufferSize / 2, input.size() - offset);
return {}; if (mbedtls_mpi_write_binary(&ctx, output.data() + offset / 2, size) != 0)
return {};
}
return output; return output;
} }