diff --git a/src/helper_functions.cpp b/src/helper_functions.cpp index 7f62e4e8..6894ab39 100644 --- a/src/helper_functions.cpp +++ b/src/helper_functions.cpp @@ -5,6 +5,7 @@ #include #include #include +#include "convert.hpp" #include "enforce.hpp" std::string string_from_unterminated(const char* data, size_t data_length) { @@ -15,21 +16,6 @@ std::string string_from_unterminated(const char* data, size_t data_length) { return {data, StringLength}; } -namespace { -std::string utf16ToUtf8(const std::u16string& wstr) { -#ifdef _MSC_VER -#pragma warning(push) -#pragma warning(disable : 4244) - auto str = std::string(wstr.begin(), wstr.end()); -#pragma warning(pop) -#else - auto str = std::string(wstr.begin(), wstr.end()); -#endif - str.erase(std::remove(str.begin(), str.end(), '\0'), str.end()); - return str; -} -} // namespace - namespace Exiv2 { uint64_t readQWORDTag(const BasicIo::UniquePtr& io) { Internal::enforce(QWORD <= io->size() - io->tell(), Exiv2::ErrorCode::kerCorruptedMetadata); @@ -53,8 +39,11 @@ std::string readStringWcharTag(const BasicIo::UniquePtr& io, size_t length) { Internal::enforce(length <= io->size() - io->tell(), Exiv2::ErrorCode::kerCorruptedMetadata); DataBuf FieldBuf(length + 1); io->readOrThrow(FieldBuf.data(), length, ErrorCode::kerFailedToReadImageData); - std::u16string wst(FieldBuf.begin(), FieldBuf.end()); - return utf16ToUtf8(wst); + std::string wst(FieldBuf.begin(), FieldBuf.end() - 3); + if (wst.size() % 2 != 0) + Exiv2::convertStringCharset(wst, "UCS-2LE", "UTF-8"); + Exiv2::convertStringCharset(wst, "UCS-2LE", "UTF-8"); + return wst; } std::string readStringTag(const BasicIo::UniquePtr& io, size_t length) {