From 490b732bbf5dfb4cc927627acb26f63199918f84 Mon Sep 17 00:00:00 2001 From: Raul Metsma Date: Tue, 2 Jun 2026 12:28:29 +0300 Subject: [PATCH] Handle BOM mark in CDoc1 file IB-8974 Signed-off-by: Raul Metsma --- CMakePresets.json | 2 +- cdoc/CDoc1Reader.cpp | 47 +++++++++++++++++--------------------------- 2 files changed, 19 insertions(+), 30 deletions(-) diff --git a/CMakePresets.json b/CMakePresets.json index 70317f93..246571a2 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -28,7 +28,7 @@ }, "cacheVariables": { "CMAKE_OSX_ARCHITECTURES": "arm64", - "CMAKE_OSX_DEPLOYMENT_TARGET": "13.3", + "CMAKE_OSX_DEPLOYMENT_TARGET": "14.0", "CMAKE_FIND_ROOT_PATH": "$env{DEST};/opt/homebrew", "FRAMEWORK_DESTINATION": "$env{DEST}/lib" }, diff --git a/cdoc/CDoc1Reader.cpp b/cdoc/CDoc1Reader.cpp index 0c9f18b8..626e70c9 100644 --- a/cdoc/CDoc1Reader.cpp +++ b/cdoc/CDoc1Reader.cpp @@ -27,12 +27,13 @@ #include "ZStream.h" #include +#include using namespace libcdoc; -static const std::string MIME_ZLIB = "http://www.isi.edu/in-noes/iana/assignments/media-types/application/zip"; -static const std::string MIME_DDOC = "http://www.sk.ee/DigiDoc/v1.3.0/digidoc.xsd"; -static const std::string MIME_DDOC_OLD = "http://www.sk.ee/DigiDoc/1.3.0/digidoc.xsd"; +constexpr std::string_view MIME_ZLIB = "http://www.isi.edu/in-noes/iana/assignments/media-types/application/zip"; +constexpr std::string_view MIME_DDOC = "http://www.sk.ee/DigiDoc/v1.3.0/digidoc.xsd"; +constexpr std::string_view MIME_DDOC_OLD = "http://www.sk.ee/DigiDoc/1.3.0/digidoc.xsd"; constexpr std::array SUPPORTED_METHODS { libcdoc::Crypto::AES128CBC_MTH, libcdoc::Crypto::AES192CBC_MTH, libcdoc::Crypto::AES256CBC_MTH, @@ -143,26 +144,6 @@ CDoc1Reader::getFMK(std::vector& fmk, unsigned int lock_idx) libcdoc::result_t CDoc1Reader::decrypt(const std::vector& fmk, libcdoc::MultiDataConsumer *dst) { -#ifdef USE_PULL - int64_t result = beginDecryption(fmk); - if (result != libcdoc::OK) return result; - std::string name; - int64_t size; - result = nextFile(name, size); - while (result == libcdoc::OK) { - result = dst->open(name, size); - if (result != libcdoc::OK) return result; - std::vector t(size); - result = readData(t.data(), size); - if (result < 0) return result; - result = dst->write(t); - if (result < 0) return result; - result = nextFile(name, size); - } - if (result != libcdoc::END_OF_STREAM) return result; - result = finishDecryption(); - return result; -#else return CDoc1Reader::decryptData(fmk, [&](DataSource &src, const std::string &mime) -> result_t { if(mime == MIME_DDOC || mime == MIME_DDOC_OLD) { LOG_DBG("Contains DDoc content {}", mime); @@ -179,7 +160,6 @@ CDoc1Reader::decrypt(const std::vector& fmk, libcdoc::MultiDataConsumer return rv; return dst->close(); }); -#endif } libcdoc::result_t @@ -328,15 +308,24 @@ CDoc1Reader::~CDoc1Reader() noexcept bool CDoc1Reader::isCDoc1File(libcdoc::DataSource *src) { - // todo: better check static constexpr std::string_view XML_TAG(" buf; - if (src->read(buf.data(), XML_TAG.size()) != XML_TAG.size()) { + static constexpr std::array UTF8_BOM{0xEF, 0xBB, 0xBF}; + std::array buf; + auto n = src->read(buf.data(), buf.size()); + if (n < 0) + return false; + size_t available = static_cast(n); + const uint8_t *start = buf.data(); + if (available >= UTF8_BOM.size() && std::equal(UTF8_BOM.begin(), UTF8_BOM.end(), start)) { + start += UTF8_BOM.size(); + available -= UTF8_BOM.size(); + } + if (available < XML_TAG.size()) { LOG_DBG("CDoc1Reader::isCDoc1File: Cannot read tag"); return false; } - if (XML_TAG.compare(0, XML_TAG.size(), (const char *) buf.data(), buf.size())) { - LOG_DBG("CDoc1Reader::isCDoc1File: Invalid tag: {}", toHex(buf)); + if (XML_TAG.compare(0, XML_TAG.size(), (const char *) start, XML_TAG.size())) { + LOG_DBG("CDoc1Reader::isCDoc1File: Invalid tag: {}", toHex(std::span{start, XML_TAG.size()})); LOG_DBG("CDoc1Reader::isCDoc1File: Should be : {}", toHex(XML_TAG)); return false; }