From f6baf6b1d101a889d2cfc7cb925e726dbffb89db Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Wed, 12 Apr 2017 15:31:05 -0400 Subject: Fold CFGAS_Stream into CFGAS_TextStream. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Every CFGAS_Stream is wrapped inside a CFGAS_TextStream. This CL folds the two classes together and merges the methods where needed. Change-Id: Ic56449b36baa51254d8d600ce631f285ba2cbb80 Reviewed-on: https://pdfium-review.googlesource.com/4057 Commit-Queue: dsinclair Reviewed-by: Nicolás Peña --- xfa/fde/cfde_txtedtengine.cpp | 3 +- xfa/fde/xml/cfde_xmlsyntaxparser.cpp | 4 +- xfa/fgas/crt/ifgas_stream.cpp | 281 ++++++++--------------------------- xfa/fgas/crt/ifgas_stream.h | 7 +- 4 files changed, 69 insertions(+), 226 deletions(-) diff --git a/xfa/fde/cfde_txtedtengine.cpp b/xfa/fde/cfde_txtedtengine.cpp index 633e0bc68e..b6471766db 100644 --- a/xfa/fde/cfde_txtedtengine.cpp +++ b/xfa/fde/cfde_txtedtengine.cpp @@ -117,8 +117,7 @@ void CFDE_TxtEdtEngine::SetTextByStream( } bool bPreIsCR = false; if (bValid) { - uint8_t bom[4]; - int32_t nPos = pStream->GetBOM(bom); + int32_t nPos = pStream->GetBOMLength(); pStream->Seek(FX_STREAMSEEK_Begin, nPos); int32_t nPlateSize = std::min(nStreamLength, m_pTxtBuf->GetChunkSize()); wchar_t* lpwstr = FX_Alloc(wchar_t, nPlateSize); diff --git a/xfa/fde/xml/cfde_xmlsyntaxparser.cpp b/xfa/fde/xml/cfde_xmlsyntaxparser.cpp index 57501cceca..51507526c2 100644 --- a/xfa/fde/xml/cfde_xmlsyntaxparser.cpp +++ b/xfa/fde/xml/cfde_xmlsyntaxparser.cpp @@ -106,9 +106,7 @@ CFDE_XMLSyntaxParser::CFDE_XMLSyntaxParser( m_CurNode.eNodeType = FDE_XMLNODE_Unknown; m_iXMLPlaneSize = std::min(m_iXMLPlaneSize, m_pStream->GetLength()); - - uint8_t bom[4]; - m_iCurrentPos = m_pStream->GetBOM(bom); + m_iCurrentPos = m_pStream->GetBOMLength(); FX_SAFE_INT32 alloc_size_safe = m_iXMLPlaneSize; alloc_size_safe += 1; // For NUL. diff --git a/xfa/fgas/crt/ifgas_stream.cpp b/xfa/fgas/crt/ifgas_stream.cpp index fa9b8db02d..712243d701 100644 --- a/xfa/fgas/crt/ifgas_stream.cpp +++ b/xfa/fgas/crt/ifgas_stream.cpp @@ -40,11 +40,6 @@ class IFGAS_StreamImp { protected: IFGAS_StreamImp(); - - uint32_t GetAccessModes() const { return m_dwAccess; } - - private: - uint32_t m_dwAccess; }; class CFGAS_FileReadStreamImp : public IFGAS_StreamImp { @@ -81,8 +76,7 @@ class CFGAS_FileWriteStreamImp : public IFGAS_StreamImp { CFGAS_FileWriteStreamImp(); ~CFGAS_FileWriteStreamImp() override {} - bool LoadFileWrite(const CFX_RetainPtr& pFileWrite, - uint32_t dwAccess); + bool LoadFileWrite(const CFX_RetainPtr& pFileWrite); // IFGAS_StreamImp: int32_t GetLength() const override; @@ -103,42 +97,12 @@ class CFGAS_FileWriteStreamImp : public IFGAS_StreamImp { int32_t m_iPosition; }; -class CFGAS_Stream : public IFGAS_Stream { - public: - template - friend CFX_RetainPtr pdfium::MakeRetain(Args&&... args); - - // IFGAS_Stream - uint32_t GetAccessModes() const override; - int32_t GetLength() const override; - int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset) override; - int32_t GetPosition() override; - bool IsEOF() const override; - int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize) override; - int32_t ReadString(wchar_t* pStr, int32_t iMaxLength, bool& bEOS) override; - int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize) override; - int32_t WriteString(const wchar_t* pStr, int32_t iLength) override; - void Flush() override; - bool SetLength(int32_t iLength) override; - int32_t GetBOM(uint8_t bom[4]) const override; - uint16_t GetCodePage() const override; - uint16_t SetCodePage(uint16_t wCodePage) override; - - private: - CFGAS_Stream(std::unique_ptr imp, uint32_t dwAccess); - ~CFGAS_Stream() override; - - std::unique_ptr m_pStreamImp; - uint32_t m_dwAccess; -}; - class CFGAS_TextStream : public IFGAS_Stream { public: template friend CFX_RetainPtr pdfium::MakeRetain(Args&&... args); // IFGAS_Stream - uint32_t GetAccessModes() const override; int32_t GetLength() const override; int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset) override; int32_t GetPosition() override; @@ -149,20 +113,20 @@ class CFGAS_TextStream : public IFGAS_Stream { int32_t WriteString(const wchar_t* pStr, int32_t iLength) override; void Flush() override; bool SetLength(int32_t iLength) override; - int32_t GetBOM(uint8_t bom[4]) const override; + int32_t GetBOMLength() const override; uint16_t GetCodePage() const override; uint16_t SetCodePage(uint16_t wCodePage) override; private: - explicit CFGAS_TextStream(const CFX_RetainPtr& pStream); + CFGAS_TextStream(std::unique_ptr imp, bool isWriteSteam); ~CFGAS_TextStream() override; void InitStream(); uint16_t m_wCodePage; int32_t m_wBOMLength; - uint32_t m_dwBOM; - CFX_RetainPtr m_pStreamImp; + bool m_IsWriteStream; + std::unique_ptr m_pStreamImp; }; class CFGAS_WideStringReadStream : public IFGAS_Stream { @@ -171,7 +135,6 @@ class CFGAS_WideStringReadStream : public IFGAS_Stream { friend CFX_RetainPtr pdfium::MakeRetain(Args&&... args); // IFGAS_Stream - uint32_t GetAccessModes() const override; int32_t GetLength() const override; int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset) override; int32_t GetPosition() override; @@ -182,7 +145,7 @@ class CFGAS_WideStringReadStream : public IFGAS_Stream { int32_t WriteString(const wchar_t* pStr, int32_t iLength) override; void Flush() override {} bool SetLength(int32_t iLength) override; - int32_t GetBOM(uint8_t bom[4]) const override; + int32_t GetBOMLength() const override; uint16_t GetCodePage() const override; uint16_t SetCodePage(uint16_t wCodePage) override; @@ -194,7 +157,7 @@ class CFGAS_WideStringReadStream : public IFGAS_Stream { int32_t m_iPosition; }; -IFGAS_StreamImp::IFGAS_StreamImp() : m_dwAccess(0) {} +IFGAS_StreamImp::IFGAS_StreamImp() {} CFGAS_FileReadStreamImp::CFGAS_FileReadStreamImp() : m_pFileRead(nullptr), m_iPosition(0), m_iLength(0) {} @@ -269,8 +232,7 @@ CFGAS_FileWriteStreamImp::CFGAS_FileWriteStreamImp() : m_pFileWrite(nullptr), m_iPosition(0) {} bool CFGAS_FileWriteStreamImp::LoadFileWrite( - const CFX_RetainPtr& pFileWrite, - uint32_t dwAccess) { + const CFX_RetainPtr& pFileWrite) { ASSERT(!m_pFileWrite && pFileWrite); m_iPosition = pFileWrite->GetSize(); @@ -327,65 +289,58 @@ void CFGAS_FileWriteStreamImp::Flush() { } } -CFGAS_TextStream::CFGAS_TextStream(const CFX_RetainPtr& pStream) +CFGAS_TextStream::CFGAS_TextStream(std::unique_ptr imp, + bool isWriteStream) : m_wCodePage(FX_CODEPAGE_DefANSI), m_wBOMLength(0), - m_dwBOM(0), - m_pStreamImp(pStream) { + m_IsWriteStream(isWriteStream), + m_pStreamImp(std::move(imp)) { ASSERT(m_pStreamImp); InitStream(); } CFGAS_TextStream::~CFGAS_TextStream() {} +#if _FX_ENDIAN_ == _FX_LITTLE_ENDIAN_ +#define BOM_MASK 0x00FFFFFF +#define BOM_UTF8 0x00BFBBEF +#define BOM_UTF16_MASK 0x0000FFFF +#define BOM_UTF16_BE 0x0000FFFE +#define BOM_UTF16_LE 0x0000FEFF +#else +#define BOM_MASK 0xFFFFFF00 +#define BOM_UTF8 0xEFBBBF00 +#define BOM_UTF16_MASK 0xFFFF0000 +#define BOM_UTF16_BE 0xFEFF0000 +#define BOM_UTF16_LE 0xFFFE0000 +#endif // _FX_ENDIAN_ == _FX_LITTLE_ENDIAN_ + void CFGAS_TextStream::InitStream() { int32_t iPosition = m_pStreamImp->GetPosition(); m_pStreamImp->Seek(FX_STREAMSEEK_Begin, 0); - m_pStreamImp->ReadData((uint8_t*)&m_dwBOM, 3); -#if _FX_ENDIAN_ == _FX_LITTLE_ENDIAN_ - m_dwBOM &= 0x00FFFFFF; - if (m_dwBOM == 0x00BFBBEF) { - m_wBOMLength = 3; - m_wCodePage = FX_CODEPAGE_UTF8; - } else { - m_dwBOM &= 0x0000FFFF; - if (m_dwBOM == 0x0000FFFE) { - m_wBOMLength = 2; - m_wCodePage = FX_CODEPAGE_UTF16BE; - } else if (m_dwBOM == 0x0000FEFF) { - m_wBOMLength = 2; - m_wCodePage = FX_CODEPAGE_UTF16LE; - } else { - m_wBOMLength = 0; - m_dwBOM = 0; - m_wCodePage = FXSYS_GetACP(); - } - } -#else - m_dwBOM &= 0xFFFFFF00; - if (m_dwBOM == 0xEFBBBF00) { + + uint32_t bom; + m_pStreamImp->ReadData(reinterpret_cast(&bom), 3); + + bom &= BOM_MASK; + if (bom == BOM_UTF8) { m_wBOMLength = 3; m_wCodePage = FX_CODEPAGE_UTF8; } else { - m_dwBOM &= 0xFFFF0000; - if (m_dwBOM == 0xFEFF0000) { + bom &= BOM_UTF16_MASK; + if (bom == BOM_UTF16_BE) { m_wBOMLength = 2; m_wCodePage = FX_CODEPAGE_UTF16BE; - } else if (m_dwBOM == 0xFFFE0000) { + } else if (bom == BOM_UTF16_LE) { m_wBOMLength = 2; m_wCodePage = FX_CODEPAGE_UTF16LE; } else { m_wBOMLength = 0; - m_dwBOM = 0; m_wCodePage = FXSYS_GetACP(); } } -#endif - m_pStreamImp->Seek(FX_STREAMSEEK_Begin, std::max(m_wBOMLength, iPosition)); -} -uint32_t CFGAS_TextStream::GetAccessModes() const { - return m_pStreamImp->GetAccessModes(); + m_pStreamImp->Seek(FX_STREAMSEEK_Begin, std::max(m_wBOMLength, iPosition)); } int32_t CFGAS_TextStream::GetLength() const { @@ -405,34 +360,50 @@ bool CFGAS_TextStream::IsEOF() const { } int32_t CFGAS_TextStream::ReadData(uint8_t* pBuffer, int32_t iBufferSize) { - return m_pStreamImp->ReadData(pBuffer, iBufferSize); + ASSERT(pBuffer && iBufferSize > 0); + + if (m_IsWriteStream) + return -1; + + int32_t iLen = std::min( + m_pStreamImp->GetLength() - m_pStreamImp->GetPosition(), iBufferSize); + if (iLen <= 0) + return 0; + + return m_pStreamImp->ReadData(pBuffer, iLen); } int32_t CFGAS_TextStream::WriteData(const uint8_t* pBuffer, int32_t iBufferSize) { + ASSERT(pBuffer && iBufferSize > 0); + + if (!m_IsWriteStream) + return -1; return m_pStreamImp->WriteData(pBuffer, iBufferSize); } void CFGAS_TextStream::Flush() { + if (!m_IsWriteStream) + return; m_pStreamImp->Flush(); } bool CFGAS_TextStream::SetLength(int32_t iLength) { + if (!m_IsWriteStream) + return false; return m_pStreamImp->SetLength(iLength); } -uint16_t CFGAS_TextStream::GetCodePage() const { - return m_wCodePage; -} - -int32_t CFGAS_TextStream::GetBOM(uint8_t bom[4]) const { +int32_t CFGAS_TextStream::GetBOMLength() const { if (m_wBOMLength < 1) return 0; - - *(uint32_t*)bom = m_dwBOM; return m_wBOMLength; } +uint16_t CFGAS_TextStream::GetCodePage() const { + return m_wCodePage; +} + uint16_t CFGAS_TextStream::SetCodePage(uint16_t wCodePage) { if (m_wBOMLength > 0) return m_wCodePage; @@ -446,7 +417,7 @@ int32_t CFGAS_TextStream::ReadString(wchar_t* pStr, int32_t iMaxLength, bool& bEOS) { ASSERT(pStr && iMaxLength > 0); - if (!m_pStreamImp) + if (m_IsWriteStream) return -1; if (m_wCodePage == FX_CODEPAGE_UTF16LE || @@ -489,7 +460,7 @@ int32_t CFGAS_TextStream::ReadString(wchar_t* pStr, int32_t CFGAS_TextStream::WriteString(const wchar_t* pStr, int32_t iLength) { ASSERT(pStr && iLength > 0); - if ((m_pStreamImp->GetAccessModes() & FX_STREAMACCESS_Write) == 0) + if (!m_IsWriteStream) return -1; if (m_wCodePage == FX_CODEPAGE_UTF8) { @@ -505,129 +476,12 @@ int32_t CFGAS_TextStream::WriteString(const wchar_t* pStr, int32_t iLength) { return iLength; } -CFGAS_Stream::CFGAS_Stream(std::unique_ptr imp, - uint32_t dwAccess) - : m_pStreamImp(std::move(imp)), m_dwAccess(dwAccess) {} - -CFGAS_Stream::~CFGAS_Stream() {} - -uint32_t CFGAS_Stream::GetAccessModes() const { - return m_dwAccess; -} - -int32_t CFGAS_Stream::GetLength() const { - return m_pStreamImp ? m_pStreamImp->GetLength() : -1; -} - -int32_t CFGAS_Stream::Seek(FX_STREAMSEEK eSeek, int32_t iOffset) { - if (!m_pStreamImp) - return -1; - return m_pStreamImp->Seek(eSeek, iOffset); -} - -int32_t CFGAS_Stream::GetPosition() { - if (!m_pStreamImp) - return -1; - return m_pStreamImp->GetPosition(); -} - -bool CFGAS_Stream::IsEOF() const { - return m_pStreamImp ? m_pStreamImp->IsEOF() : true; -} - -int32_t CFGAS_Stream::ReadData(uint8_t* pBuffer, int32_t iBufferSize) { - ASSERT(pBuffer && iBufferSize > 0); - if (!m_pStreamImp) - return -1; - - int32_t iLen = std::min( - m_pStreamImp->GetLength() - m_pStreamImp->GetPosition(), iBufferSize); - if (iLen <= 0) - return 0; - - return m_pStreamImp->ReadData(pBuffer, iLen); -} - -int32_t CFGAS_Stream::ReadString(wchar_t* pStr, - int32_t iMaxLength, - bool& bEOS) { - ASSERT(pStr && iMaxLength > 0); - if (!m_pStreamImp) - return -1; - - int32_t iLen = - std::min((m_pStreamImp->GetLength() - m_pStreamImp->GetPosition()) / 2, - iMaxLength); - if (iLen <= 0) - return 0; - return m_pStreamImp->ReadString(pStr, iLen, bEOS); -} - -int32_t CFGAS_Stream::WriteData(const uint8_t* pBuffer, int32_t iBufferSize) { - ASSERT(pBuffer && iBufferSize > 0); - if (!m_pStreamImp) - return -1; - if ((m_dwAccess & FX_STREAMACCESS_Write) == 0) - return -1; - return m_pStreamImp->WriteData(pBuffer, iBufferSize); -} - -int32_t CFGAS_Stream::WriteString(const wchar_t* pStr, int32_t iLength) { - ASSERT(pStr && iLength > 0); - if (!m_pStreamImp) - return -1; - if ((m_dwAccess & FX_STREAMACCESS_Write) == 0) - return -1; - return m_pStreamImp->WriteString(pStr, iLength); -} - -void CFGAS_Stream::Flush() { - if (!m_pStreamImp) - return; - if ((m_dwAccess & FX_STREAMACCESS_Write) == 0) - return; - m_pStreamImp->Flush(); -} - -bool CFGAS_Stream::SetLength(int32_t iLength) { - if (!m_pStreamImp) - return false; - if ((m_dwAccess & FX_STREAMACCESS_Write) == 0) - return false; - return m_pStreamImp->SetLength(iLength); -} - -int32_t CFGAS_Stream::GetBOM(uint8_t bom[4]) const { - if (!m_pStreamImp) - return -1; - return 0; -} - -uint16_t CFGAS_Stream::GetCodePage() const { -#if _FX_ENDIAN_ == _FX_LITTLE_ENDIAN_ - return FX_CODEPAGE_UTF16LE; -#else - return FX_CODEPAGE_UTF16BE; -#endif -} -uint16_t CFGAS_Stream::SetCodePage(uint16_t wCodePage) { -#if _FX_ENDIAN_ == _FX_LITTLE_ENDIAN_ - return FX_CODEPAGE_UTF16LE; -#else - return FX_CODEPAGE_UTF16BE; -#endif -} - CFGAS_WideStringReadStream::CFGAS_WideStringReadStream( const CFX_WideString& wsBuffer) : m_wsBuffer(wsBuffer), m_iPosition(0) {} CFGAS_WideStringReadStream::~CFGAS_WideStringReadStream() {} -uint32_t CFGAS_WideStringReadStream::GetAccessModes() const { - return 0; -} - int32_t CFGAS_WideStringReadStream::GetLength() const { return m_wsBuffer.GetLength() * sizeof(wchar_t); } @@ -688,7 +542,7 @@ bool CFGAS_WideStringReadStream::SetLength(int32_t iLength) { return false; } -int32_t CFGAS_WideStringReadStream::GetBOM(uint8_t bom[4]) const { +int32_t CFGAS_WideStringReadStream::GetBOMLength() const { return 0; } @@ -714,8 +568,7 @@ CFX_RetainPtr IFGAS_Stream::CreateReadStream( ->LoadFileRead(pFileRead)) { return nullptr; } - return pdfium::MakeRetain( - pdfium::MakeRetain(std::move(pImp), 0)); + return pdfium::MakeRetain(std::move(pImp), false); } // static @@ -727,12 +580,10 @@ CFX_RetainPtr IFGAS_Stream::CreateWriteStream( std::unique_ptr pImp = pdfium::MakeUnique(); if (!static_cast(pImp.get()) - ->LoadFileWrite(pFileWrite, FX_STREAMACCESS_Write)) { + ->LoadFileWrite(pFileWrite)) { return nullptr; } - - return pdfium::MakeRetain( - pdfium::MakeRetain(std::move(pImp), FX_STREAMACCESS_Write)); + return pdfium::MakeRetain(std::move(pImp), true); } // static diff --git a/xfa/fgas/crt/ifgas_stream.h b/xfa/fgas/crt/ifgas_stream.h index df9d93afa3..b3f8a928d8 100644 --- a/xfa/fgas/crt/ifgas_stream.h +++ b/xfa/fgas/crt/ifgas_stream.h @@ -11,10 +11,6 @@ #include "core/fxcrt/fx_stream.h" #include "core/fxcrt/fx_system.h" -enum FX_STREAMACCESS { - FX_STREAMACCESS_Write = 0x04, -}; - enum FX_STREAMSEEK { FX_STREAMSEEK_Begin = 0, FX_STREAMSEEK_Current, @@ -30,7 +26,6 @@ class IFGAS_Stream : public CFX_Retainable { static CFX_RetainPtr CreateWideStringReadStream( const CFX_WideString& buffer); - virtual uint32_t GetAccessModes() const = 0; virtual int32_t GetLength() const = 0; virtual int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset) = 0; virtual int32_t GetPosition() = 0; @@ -41,7 +36,7 @@ class IFGAS_Stream : public CFX_Retainable { virtual int32_t WriteString(const wchar_t* pStr, int32_t iLength) = 0; virtual void Flush() = 0; virtual bool SetLength(int32_t iLength) = 0; - virtual int32_t GetBOM(uint8_t bom[4]) const = 0; + virtual int32_t GetBOMLength() const = 0; virtual uint16_t GetCodePage() const = 0; virtual uint16_t SetCodePage(uint16_t wCodePage) = 0; }; -- cgit v1.2.3