add test/data for video support : rework readStringWcharTag method

main
Mohamed Ali Chebbi 2 years ago
parent e388ba523f
commit 579cae8e77

@ -3,7 +3,9 @@
#include "helper_functions.hpp" #include "helper_functions.hpp"
#include <cmath> #include <cmath>
#include <codecvt>
#include <cstring> #include <cstring>
#include <locale>
#include "enforce.hpp" #include "enforce.hpp"
std::string string_from_unterminated(const char* data, size_t data_length) { std::string string_from_unterminated(const char* data, size_t data_length) {
@ -21,20 +23,13 @@ char returnHex(int n) {
return static_cast<char>(n + 55); return static_cast<char>(n + 55);
} }
std::string toString16(Exiv2::DataBuf& buf) { std::string utf16ToUtf8(const std::wstring& wstr) {
std::ostringstream os; using convert_typeX = std::codecvt_utf8<wchar_t>;
char t; std::wstring_convert<convert_typeX, wchar_t> converterX;
for (size_t i = 0; i < buf.size(); i += 2) { std::string str = converterX.to_bytes(wstr);
t = buf.data()[i] + 16 * buf.data()[i + 1]; str.erase(std::remove(str.begin(), str.end(), NULL), str.end());
if (t == 0) { return str;
if (i)
os << '\0';
break;
}
os << t;
}
return os.str();
} }
uint64_t readQWORDTag(BasicIo::UniquePtr& io) { 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) { std::string readStringWcharTag(BasicIo::UniquePtr& io, size_t length) {
Internal::enforce(length <= io->size() - io->tell(), Exiv2::ErrorCode::kerCorruptedMetadata); Internal::enforce(length <= io->size() - io->tell(), Exiv2::ErrorCode::kerCorruptedMetadata);
DataBuf FieldBuf = io->read(length); DataBuf FieldBuf(length + 1);
return toString16(FieldBuf); 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) { std::string readStringTag(BasicIo::UniquePtr& io, size_t length) {

@ -32,13 +32,6 @@ namespace Exiv2 {
*/ */
char returnHex(int n); 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 BYTE = 0x1;
static constexpr size_t WCHAR = 0x2; static constexpr size_t WCHAR = 0x2;
static constexpr size_t WORD = 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 QWORD = 0x8;
static constexpr size_t GUID = 0x10; 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); [[nodiscard]] uint64_t readQWORDTag(Exiv2::BasicIo::UniquePtr& io);

Loading…
Cancel
Save