diff --git a/src/helper_functions.cpp b/src/helper_functions.cpp index faa3209e..ba2e15a5 100644 --- a/src/helper_functions.cpp +++ b/src/helper_functions.cpp @@ -3,7 +3,9 @@ #include "helper_functions.hpp" #include +#include #include +#include #include "enforce.hpp" std::string string_from_unterminated(const char* data, size_t data_length) { @@ -21,20 +23,13 @@ char returnHex(int n) { return static_cast(n + 55); } -std::string toString16(Exiv2::DataBuf& buf) { - std::ostringstream os; - char t; +std::string utf16ToUtf8(const std::wstring& wstr) { + using convert_typeX = std::codecvt_utf8; + std::wstring_convert converterX; - for (size_t i = 0; i < buf.size(); i += 2) { - t = buf.data()[i] + 16 * buf.data()[i + 1]; - if (t == 0) { - if (i) - os << '\0'; - break; - } - os << t; - } - return os.str(); + std::string str = converterX.to_bytes(wstr); + str.erase(std::remove(str.begin(), str.end(), NULL), str.end()); + return str; } uint64_t readQWORDTag(BasicIo::UniquePtr& io) { @@ -57,8 +52,10 @@ uint16_t readWORDTag(BasicIo::UniquePtr& io) { std::string readStringWcharTag(BasicIo::UniquePtr& io, size_t length) { Internal::enforce(length <= io->size() - io->tell(), Exiv2::ErrorCode::kerCorruptedMetadata); - DataBuf FieldBuf = io->read(length); - return toString16(FieldBuf); + DataBuf FieldBuf(length + 1); + io->readOrThrow(FieldBuf.data(), length, ErrorCode::kerFailedToReadImageData); + std::wstring wst(FieldBuf.begin(), FieldBuf.end()); + return utf16ToUtf8(wst); } std::string readStringTag(BasicIo::UniquePtr& io, size_t length) { diff --git a/src/helper_functions.hpp b/src/helper_functions.hpp index f323977b..bc070df2 100644 --- a/src/helper_functions.hpp +++ b/src/helper_functions.hpp @@ -32,13 +32,6 @@ namespace Exiv2 { */ char returnHex(int n); -/*! - @brief Function used to read data from data buffer, reads 16-bit character - array and stores it in std::string object. - @param buf Exiv2 data buffer, which stores the information - @return Returns std::string object . - */ - static constexpr size_t BYTE = 0x1; static constexpr size_t WCHAR = 0x2; static constexpr size_t WORD = 0X2; @@ -46,7 +39,16 @@ static constexpr size_t DWORD = 0x4; static constexpr size_t QWORD = 0x8; static constexpr size_t GUID = 0x10; -std::string toString16(Exiv2::DataBuf& buf); +// @brief + +/*! + @brief The function utf16ToUtf8 takes a wide string wstr as input and converts it to a narrow string + The conversion is performed using the std::wstring_convert class template and a std::codecvt_utf8 facet, which + implements conversion between UTF-8 and wide characters. + @param wstr : wide string + @return Returns std::string object +*/ +std::string utf16ToUtf8(const std::wstring& wstr); [[nodiscard]] uint64_t readQWORDTag(Exiv2::BasicIo::UniquePtr& io); diff --git a/test/data/test_reference_files/sample_960x540.asf.out b/test/data/test_reference_files/sample_960x540.asf.out index 3bc4cb60..38c508d4 100644 Binary files a/test/data/test_reference_files/sample_960x540.asf.out and b/test/data/test_reference_files/sample_960x540.asf.out differ