diff options
Diffstat (limited to 'core/fxcrt')
-rw-r--r-- | core/fxcrt/cfx_seekablestreamproxy.cpp | 54 | ||||
-rw-r--r-- | core/fxcrt/cfx_seekablestreamproxy.h | 19 | ||||
-rw-r--r-- | core/fxcrt/xml/cfx_xmlparser.cpp | 35 | ||||
-rw-r--r-- | core/fxcrt/xml/cfx_xmlparser.h | 9 |
4 files changed, 59 insertions, 58 deletions
diff --git a/core/fxcrt/cfx_seekablestreamproxy.cpp b/core/fxcrt/cfx_seekablestreamproxy.cpp index eb5874b7a2..d8fe4f4832 100644 --- a/core/fxcrt/cfx_seekablestreamproxy.cpp +++ b/core/fxcrt/cfx_seekablestreamproxy.cpp @@ -134,7 +134,7 @@ void SwapByteOrder(wchar_t* pStr, size_t iLength) { #define BOM_UTF16_LE 0x0000FEFF CFX_SeekableStreamProxy::CFX_SeekableStreamProxy( - const RetainPtr<IFX_SeekableReadStream>& stream) + const RetainPtr<IFX_SeekableStream>& stream) : m_wCodePage(FX_CODEPAGE_DefANSI), m_wBOMLength(0), m_iPosition(0), @@ -169,18 +169,6 @@ CFX_SeekableStreamProxy::CFX_SeekableStreamProxy( CFX_SeekableStreamProxy::~CFX_SeekableStreamProxy() = default; -FX_FILESIZE CFX_SeekableStreamProxy::GetSize() { - return m_pStream->GetSize(); -} - -FX_FILESIZE CFX_SeekableStreamProxy::GetPosition() { - return m_iPosition; -} - -bool CFX_SeekableStreamProxy::IsEOF() { - return m_iPosition >= GetSize(); -} - void CFX_SeekableStreamProxy::Seek(From eSeek, FX_FILESIZE iOffset) { switch (eSeek) { case From::Begin: @@ -194,7 +182,7 @@ void CFX_SeekableStreamProxy::Seek(From eSeek, FX_FILESIZE iOffset) { } break; } m_iPosition = - pdfium::clamp(m_iPosition, static_cast<FX_FILESIZE>(0), GetSize()); + pdfium::clamp(m_iPosition, static_cast<FX_FILESIZE>(0), GetLength()); } void CFX_SeekableStreamProxy::SetCodePage(uint16_t wCodePage) { @@ -207,7 +195,7 @@ size_t CFX_SeekableStreamProxy::ReadData(uint8_t* pBuffer, size_t iBufferSize) { ASSERT(pBuffer && iBufferSize > 0); iBufferSize = - std::min(iBufferSize, static_cast<size_t>(GetSize() - m_iPosition)); + std::min(iBufferSize, static_cast<size_t>(GetLength() - m_iPosition)); if (iBufferSize <= 0) return 0; @@ -220,24 +208,27 @@ size_t CFX_SeekableStreamProxy::ReadData(uint8_t* pBuffer, size_t iBufferSize) { return new_pos.IsValid() ? iBufferSize : 0; } -size_t CFX_SeekableStreamProxy::ReadBlock(void* pStr, size_t size) { - if (!pStr || size == 0) +size_t CFX_SeekableStreamProxy::ReadString(wchar_t* pStr, + size_t iMaxLength, + bool* bEOS) { + if (!pStr || iMaxLength == 0) return 0; if (m_wCodePage == FX_CODEPAGE_UTF16LE || m_wCodePage == FX_CODEPAGE_UTF16BE) { - size_t iBytes = size * 2; + size_t iBytes = iMaxLength * 2; size_t iLen = ReadData(reinterpret_cast<uint8_t*>(pStr), iBytes); - size = iLen / 2; - if (sizeof(wchar_t) > 2 && size > 0) - UTF16ToWChar(pStr, size); + iMaxLength = iLen / 2; + if (sizeof(wchar_t) > 2 && iMaxLength > 0) + UTF16ToWChar(pStr, iMaxLength); if (m_wCodePage == FX_CODEPAGE_UTF16BE) - SwapByteOrder(static_cast<wchar_t*>(pStr), size); + SwapByteOrder(pStr, iMaxLength); } else { FX_FILESIZE pos = GetPosition(); - size_t iBytes = std::min(size, static_cast<size_t>(GetSize() - pos)); + size_t iBytes = + std::min(iMaxLength, static_cast<size_t>(GetLength() - pos)); if (iBytes > 0) { std::vector<uint8_t> buf(iBytes); @@ -247,21 +238,14 @@ size_t CFX_SeekableStreamProxy::ReadBlock(void* pStr, size_t size) { return 0; size_t iSrc = 0; - std::tie(iSrc, size) = - UTF8Decode(reinterpret_cast<const char*>(buf.data()), iLen, - static_cast<wchar_t*>(pStr), size); + std::tie(iSrc, iMaxLength) = UTF8Decode( + reinterpret_cast<const char*>(buf.data()), iLen, pStr, iMaxLength); Seek(From::Current, iSrc - iLen); } else { - size = 0; + iMaxLength = 0; } } - return size; -} - -bool CFX_SeekableStreamProxy::ReadBlock(void* pStr, - FX_FILESIZE offset, - size_t size) { - NOTREACHED(); - return false; + *bEOS = IsEOF(); + return iMaxLength; } diff --git a/core/fxcrt/cfx_seekablestreamproxy.h b/core/fxcrt/cfx_seekablestreamproxy.h index d389baf13f..5e0eecb87b 100644 --- a/core/fxcrt/cfx_seekablestreamproxy.h +++ b/core/fxcrt/cfx_seekablestreamproxy.h @@ -13,7 +13,7 @@ #include "core/fxcrt/fx_system.h" #include "core/fxcrt/retain_ptr.h" -class CFX_SeekableStreamProxy : public IFX_SeekableReadStream { +class CFX_SeekableStreamProxy : public Retainable { public: enum class From { Begin = 0, @@ -23,28 +23,27 @@ class CFX_SeekableStreamProxy : public IFX_SeekableReadStream { template <typename T, typename... Args> friend RetainPtr<T> pdfium::MakeRetain(Args&&... args); - FX_FILESIZE GetSize() override; - FX_FILESIZE GetPosition() override; - bool IsEOF() override; + FX_FILESIZE GetLength() const { return m_pStream->GetSize(); } + FX_FILESIZE GetPosition() { return m_iPosition; } + size_t GetBOMLength() const { return m_wBOMLength; } + bool IsEOF() const { return m_iPosition >= GetLength(); } - size_t ReadBlock(void* pStr, size_t size) override; - bool ReadBlock(void* pStr, FX_FILESIZE offset, size_t size) override; + void Seek(From eSeek, FX_FILESIZE iOffset); + size_t ReadString(wchar_t* pStr, size_t iMaxLength, bool* bEOS); uint16_t GetCodePage() const { return m_wCodePage; } void SetCodePage(uint16_t wCodePage); private: - explicit CFX_SeekableStreamProxy( - const RetainPtr<IFX_SeekableReadStream>& stream); + explicit CFX_SeekableStreamProxy(const RetainPtr<IFX_SeekableStream>& stream); ~CFX_SeekableStreamProxy() override; - void Seek(From eSeek, FX_FILESIZE iOffset); size_t ReadData(uint8_t* pBuffer, size_t iBufferSize); uint16_t m_wCodePage; size_t m_wBOMLength; FX_FILESIZE m_iPosition; - RetainPtr<IFX_SeekableReadStream> m_pStream; + RetainPtr<IFX_SeekableStream> m_pStream; }; #endif // CORE_FXCRT_CFX_SEEKABLESTREAMPROXY_H_ diff --git a/core/fxcrt/xml/cfx_xmlparser.cpp b/core/fxcrt/xml/cfx_xmlparser.cpp index bf578291e6..7788a42907 100644 --- a/core/fxcrt/xml/cfx_xmlparser.cpp +++ b/core/fxcrt/xml/cfx_xmlparser.cpp @@ -11,7 +11,6 @@ #include <iterator> #include <utility> -#include "core/fxcrt/cfx_seekablestreamproxy.h" #include "core/fxcrt/fx_codepage.h" #include "core/fxcrt/fx_extension.h" #include "core/fxcrt/fx_safe_types.h" @@ -58,10 +57,13 @@ bool CFX_XMLParser::IsXMLNameChar(wchar_t ch, bool bFirstChar) { } CFX_XMLParser::CFX_XMLParser(CFX_XMLNode* pParent, - const RetainPtr<IFX_SeekableReadStream>& pStream) + const RetainPtr<IFX_SeekableStream>& pStream) : m_pParent(pParent), m_pChild(nullptr), + m_pStream(pdfium::MakeRetain<CFX_SeekableStreamProxy>(pStream)), m_iXMLPlaneSize(1024), + m_iCurrentPos(0), + m_bEOS(false), m_Start(0), m_End(0), m_CurNodeType(FX_XMLNODE_Unknown), @@ -75,19 +77,18 @@ CFX_XMLParser::CFX_XMLParser(CFX_XMLNode* pParent, ASSERT(m_pParent); ASSERT(pStream); - auto proxy = pdfium::MakeRetain<CFX_SeekableStreamProxy>(pStream); - uint16_t wCodePage = proxy->GetCodePage(); + uint16_t wCodePage = m_pStream->GetCodePage(); if (wCodePage != FX_CODEPAGE_UTF16LE && wCodePage != FX_CODEPAGE_UTF16BE && wCodePage != FX_CODEPAGE_UTF8) { - proxy->SetCodePage(FX_CODEPAGE_UTF8); + m_pStream->SetCodePage(FX_CODEPAGE_UTF8); } - m_pStream = proxy; m_NodeStack.push(m_pParent); m_iXMLPlaneSize = std::min(m_iXMLPlaneSize, - pdfium::base::checked_cast<size_t>(m_pStream->GetSize())); + pdfium::base::checked_cast<size_t>(m_pStream->GetLength())); + m_iCurrentPos = m_pStream->GetBOMLength(); FX_SAFE_SIZE_T alloc_size_safe = m_iXMLPlaneSize; alloc_size_safe += 1; // For NUL. @@ -216,20 +217,28 @@ FX_XmlSyntaxResult CFX_XMLParser::DoSyntaxParse() { return m_syntaxParserResult; } + FX_FILESIZE iStreamLength = m_pStream->GetLength(); + FX_FILESIZE iPos; + FX_XmlSyntaxResult syntaxParserResult = FX_XmlSyntaxResult::None; while (true) { if (m_Start >= m_End) { - if (m_pStream->IsEOF()) { + if (m_bEOS || m_iCurrentPos >= iStreamLength) { m_syntaxParserResult = FX_XmlSyntaxResult::EndOfString; return m_syntaxParserResult; } + if (m_pStream->GetPosition() != m_iCurrentPos) + m_pStream->Seek(CFX_SeekableStreamProxy::From::Begin, m_iCurrentPos); size_t buffer_chars = - m_pStream->ReadBlock(m_Buffer.data(), m_iXMLPlaneSize); + m_pStream->ReadString(m_Buffer.data(), m_iXMLPlaneSize, &m_bEOS); + iPos = m_pStream->GetPosition(); if (buffer_chars == 0) { + m_iCurrentPos = iStreamLength; m_syntaxParserResult = FX_XmlSyntaxResult::EndOfString; return m_syntaxParserResult; } + m_iCurrentPos = iPos; m_Start = 0; m_End = buffer_chars; } @@ -656,7 +665,13 @@ FX_XmlSyntaxResult CFX_XMLParser::DoSyntaxParse() { } bool CFX_XMLParser::GetStatus() const { - return m_pStream && m_syntaxParserResult != FX_XmlSyntaxResult::Error; + if (!m_pStream) + return false; + + int32_t iStreamLength = m_pStream->GetLength(); + if (iStreamLength < 1) + return true; + return m_syntaxParserResult != FX_XmlSyntaxResult::Error; } void CFX_XMLParser::ParseTextChar(wchar_t character) { diff --git a/core/fxcrt/xml/cfx_xmlparser.h b/core/fxcrt/xml/cfx_xmlparser.h index 6405b61a64..f369453de1 100644 --- a/core/fxcrt/xml/cfx_xmlparser.h +++ b/core/fxcrt/xml/cfx_xmlparser.h @@ -12,13 +12,14 @@ #include <vector> #include "core/fxcrt/cfx_blockbuffer.h" +#include "core/fxcrt/cfx_seekablestreamproxy.h" #include "core/fxcrt/fx_string.h" #include "core/fxcrt/retain_ptr.h" #include "core/fxcrt/xml/cfx_xmlnode.h" class CFX_XMLElement; class CFX_XMLNode; -class IFX_SeekableReadStream; +class IFX_SeekableStream; enum class FX_XmlSyntaxResult { None, @@ -43,7 +44,7 @@ class CFX_XMLParser { static bool IsXMLNameChar(wchar_t ch, bool bFirstChar); CFX_XMLParser(CFX_XMLNode* pParent, - const RetainPtr<IFX_SeekableReadStream>& pStream); + const RetainPtr<IFX_SeekableStream>& pStream); virtual ~CFX_XMLParser(); bool Parse(); @@ -99,9 +100,11 @@ class CFX_XMLParser { std::stack<CFX_XMLNode*> m_NodeStack; WideString m_ws1; - RetainPtr<IFX_SeekableReadStream> m_pStream; + RetainPtr<CFX_SeekableStreamProxy> m_pStream; size_t m_iXMLPlaneSize; + FX_FILESIZE m_iCurrentPos; std::vector<wchar_t> m_Buffer; + bool m_bEOS; FX_FILESIZE m_Start; // Start position in m_Buffer FX_FILESIZE m_End; // End position in m_Buffer FX_XMLNODETYPE m_CurNodeType; |