From 4fcdf058734b5df696420aa653ea787842678224 Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Tue, 18 Apr 2017 11:55:27 -0400 Subject: Subclass the stream implementations from CFGAS_Stream MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Rename CFGAS_TextStream to CFGAS_Stream. CFGAS_Stream is converted to a base class instead of accepting an IFGAS_StreamImp. Things which inherted from IFGAS_StreamImp now inherit from CFGAS_Stream. The stream type inputs are changed to IFX_SeekableStream so that they can accept the same type (IFX_SeekableStream is an IFX_SeekableWriteStream and an IFX_SeekableReadStream). This way the storage can be shared in the base class. Change-Id: I06645071e68e2a4d4120c0e336529f2c18c2b705 Reviewed-on: https://pdfium-review.googlesource.com/4152 Commit-Queue: dsinclair Reviewed-by: Nicolás Peña --- xfa/fgas/crt/ifgas_stream.cpp | 367 +++++++++++++++--------------------------- xfa/fgas/crt/ifgas_stream.h | 4 +- 2 files changed, 133 insertions(+), 238 deletions(-) (limited to 'xfa/fgas') diff --git a/xfa/fgas/crt/ifgas_stream.cpp b/xfa/fgas/crt/ifgas_stream.cpp index 41913b28b6..3f5069e4bb 100644 --- a/xfa/fgas/crt/ifgas_stream.cpp +++ b/xfa/fgas/crt/ifgas_stream.cpp @@ -23,91 +23,64 @@ namespace { -class IFGAS_StreamImp { +class CFGAS_Stream : public IFGAS_Stream { public: - virtual ~IFGAS_StreamImp() {} - - virtual FX_FILESIZE GetLength() const = 0; - virtual FX_FILESIZE GetPosition() = 0; - - virtual void Seek(FX_STREAMSEEK eSeek, FX_FILESIZE iOffset) = 0; - virtual bool IsEOF() const = 0; - - virtual FX_STRSIZE ReadData(uint8_t* pBuffer, FX_STRSIZE iBufferSize) = 0; - virtual void WriteData(const CFX_WideStringC& str) = 0; + // IFGAS_Stream + FX_FILESIZE GetLength() const override { return m_pStream->GetSize(); } + FX_FILESIZE GetPosition() override { return m_iPosition; } + FX_STRSIZE GetBOMLength() const override { return std::max(0, m_wBOMLength); } + FX_STRSIZE ReadString(wchar_t* pStr, + FX_STRSIZE iMaxLength, + bool* bEOS) override; + void Seek(FX_STREAMSEEK eSeek, FX_FILESIZE iOffset) override; + bool IsEOF() const override { return m_iPosition >= GetLength(); } + void WriteString(const CFX_WideStringC& str) override; + uint16_t GetCodePage() const override { return m_wCodePage; } + void SetCodePage(uint16_t wCodePage) override; protected: - IFGAS_StreamImp(); -}; - -class CFGAS_FileReadStreamImp : public IFGAS_StreamImp { - public: - explicit CFGAS_FileReadStreamImp( - const CFX_RetainPtr& pFileRead); - ~CFGAS_FileReadStreamImp() override {} + CFGAS_Stream(const CFX_RetainPtr& stream, + bool isWriteSteam); + ~CFGAS_Stream() override; - // IFGAS_StreamImp: - FX_FILESIZE GetLength() const override; - FX_FILESIZE GetPosition() override { return m_iPosition; } - void Seek(FX_STREAMSEEK eSeek, FX_FILESIZE iOffset) override; - bool IsEOF() const override; - FX_STRSIZE ReadData(uint8_t* pBuffer, FX_STRSIZE iBufferSize) override; - void WriteData(const CFX_WideStringC& str) override {} + virtual FX_STRSIZE ReadData(uint8_t* pBuffer, FX_STRSIZE iBufferSize) { + return 0; + } + virtual void WriteData(const CFX_WideStringC& str) {} - private: - CFX_RetainPtr m_pFileRead; + uint16_t m_wCodePage; + FX_STRSIZE m_wBOMLength; + bool m_IsWriteStream; FX_FILESIZE m_iPosition; + CFX_RetainPtr m_pStream; }; -class CFGAS_FileWriteStreamImp : public IFGAS_StreamImp { +class CFGAS_FileReadStreamImp : public CFGAS_Stream { public: - CFGAS_FileWriteStreamImp( - const CFX_RetainPtr& pFileWrite); - ~CFGAS_FileWriteStreamImp() override {} + template + friend CFX_RetainPtr pdfium::MakeRetain(Args&&... args); - // IFGAS_StreamImp: - FX_FILESIZE GetLength() const override; - FX_FILESIZE GetPosition() override { return m_iPosition; } - void Seek(FX_STREAMSEEK eSeek, FX_FILESIZE iOffset) override; - bool IsEOF() const override; - FX_STRSIZE ReadData(uint8_t* pBuffer, FX_STRSIZE iBufferSize) override { - return 0; - } - void WriteData(const CFX_WideStringC& str) override; + // CFGAS_Stream: + FX_STRSIZE ReadData(uint8_t* pBuffer, FX_STRSIZE iBufferSize) override; private: - CFX_RetainPtr m_pFileWrite; - FX_FILESIZE m_iPosition; + explicit CFGAS_FileReadStreamImp( + const CFX_RetainPtr& pFileRead); + ~CFGAS_FileReadStreamImp() override {} }; -class CFGAS_TextStream : public IFGAS_Stream { +class CFGAS_FileWriteStreamImp : public CFGAS_Stream { public: template friend CFX_RetainPtr pdfium::MakeRetain(Args&&... args); - // IFGAS_Stream - FX_FILESIZE GetLength() const override; - FX_FILESIZE GetPosition() override; - FX_STRSIZE GetBOMLength() const override; - void Seek(FX_STREAMSEEK eSeek, FX_FILESIZE iOffset) override; - bool IsEOF() const override; - FX_STRSIZE ReadString(wchar_t* pStr, - FX_STRSIZE iMaxLength, - bool* bEOS) override; - void WriteString(const CFX_WideStringC& str) override; - uint16_t GetCodePage() const override; - void SetCodePage(uint16_t wCodePage) override; + // CFGAS_Stream: + void WriteData(const CFX_WideStringC& str) override; private: - CFGAS_TextStream(std::unique_ptr imp, bool isWriteSteam); - ~CFGAS_TextStream() override; - - void InitStream(); - - uint16_t m_wCodePage; - FX_STRSIZE m_wBOMLength; - bool m_IsWriteStream; - std::unique_ptr m_pStreamImp; + explicit CFGAS_FileWriteStreamImp( + const CFX_RetainPtr& pFileWrite); + ~CFGAS_FileWriteStreamImp() override {} }; class CFGAS_WideStringReadStream : public IFGAS_Stream { @@ -116,16 +89,20 @@ class CFGAS_WideStringReadStream : public IFGAS_Stream { friend CFX_RetainPtr pdfium::MakeRetain(Args&&... args); // IFGAS_Stream - FX_FILESIZE GetLength() const override; - FX_FILESIZE GetPosition() override; + FX_FILESIZE GetLength() const override { + return m_wsBuffer.GetLength() * sizeof(wchar_t); + } + FX_FILESIZE GetPosition() override { return m_iPosition * sizeof(wchar_t); } FX_STRSIZE GetBOMLength() const override { return 0; } void Seek(FX_STREAMSEEK eSeek, FX_FILESIZE iOffset) override; - bool IsEOF() const override; + bool IsEOF() const override { return m_iPosition >= m_wsBuffer.GetLength(); } FX_STRSIZE ReadString(wchar_t* pStr, FX_STRSIZE iMaxLength, bool* bEOS) override; void WriteString(const CFX_WideStringC& str) override {} - uint16_t GetCodePage() const override; + uint16_t GetCodePage() const override { + return sizeof(wchar_t) == 2 ? FX_CODEPAGE_UTF16LE : FX_CODEPAGE_UTF32LE; + } void SetCodePage(uint16_t wCodePage) override {} private: @@ -228,7 +205,7 @@ void SwapByteOrder(wchar_t* pStr, FX_STRSIZE iLength) { wch = (wch >> 8) | (wch << 8); wch &= 0x00FF; *pStr = wch; - pStr++; + ++pStr; } return; } @@ -237,117 +214,10 @@ void SwapByteOrder(wchar_t* pStr, FX_STRSIZE iLength) { wch = static_cast(*pStr); wch = (wch >> 8) | (wch << 8); *pStr = wch; - pStr++; + ++pStr; } } -IFGAS_StreamImp::IFGAS_StreamImp() {} - -CFGAS_FileReadStreamImp::CFGAS_FileReadStreamImp( - const CFX_RetainPtr& pFileRead) - : m_pFileRead(pFileRead), m_iPosition(0) { - ASSERT(m_pFileRead); -} - -FX_FILESIZE CFGAS_FileReadStreamImp::GetLength() const { - return m_pFileRead->GetSize(); -} - -void CFGAS_FileReadStreamImp::Seek(FX_STREAMSEEK eSeek, FX_FILESIZE iOffset) { - switch (eSeek) { - case FX_STREAMSEEK_Begin: - m_iPosition = iOffset; - break; - case FX_STREAMSEEK_Current: - m_iPosition += iOffset; - break; - } - m_iPosition = pdfium::clamp(m_iPosition, static_cast(0), - m_pFileRead->GetSize()); -} - -bool CFGAS_FileReadStreamImp::IsEOF() const { - return m_iPosition >= m_pFileRead->GetSize(); -} - -FX_STRSIZE CFGAS_FileReadStreamImp::ReadData(uint8_t* pBuffer, - FX_STRSIZE iBufferSize) { - ASSERT(pBuffer && iBufferSize > 0); - - iBufferSize = - std::min(iBufferSize, - static_cast(m_pFileRead->GetSize() - m_iPosition)); - if (iBufferSize <= 0) - return 0; - - if (m_pFileRead->ReadBlock(pBuffer, m_iPosition, iBufferSize)) { - pdfium::base::CheckedNumeric new_pos = m_iPosition; - new_pos += iBufferSize; - if (!new_pos.IsValid()) - return 0; - - m_iPosition = new_pos.ValueOrDie(); - return iBufferSize; - } - return 0; -} - -CFGAS_FileWriteStreamImp::CFGAS_FileWriteStreamImp( - const CFX_RetainPtr& pFileWrite) - : m_pFileWrite(pFileWrite), m_iPosition(m_pFileWrite->GetSize()) { - ASSERT(m_pFileWrite); -} - -FX_FILESIZE CFGAS_FileWriteStreamImp::GetLength() const { - return m_pFileWrite->GetSize(); -} - -void CFGAS_FileWriteStreamImp::Seek(FX_STREAMSEEK eSeek, FX_FILESIZE iOffset) { - FX_FILESIZE iLength = GetLength(); - switch (eSeek) { - case FX_STREAMSEEK_Begin: - m_iPosition = iOffset; - break; - case FX_STREAMSEEK_Current: - m_iPosition += iOffset; - break; - } - m_iPosition = - pdfium::clamp(m_iPosition, static_cast(0), iLength); -} - -bool CFGAS_FileWriteStreamImp::IsEOF() const { - return m_iPosition >= GetLength(); -} - -void CFGAS_FileWriteStreamImp::WriteData(const CFX_WideStringC& str) { - if (str.GetLength() == 0) - return; - - if (m_pFileWrite->WriteBlock(str.c_str(), m_iPosition, - str.GetLength() * sizeof(wchar_t))) { - pdfium::base::CheckedNumeric new_pos = m_iPosition; - new_pos += str.GetLength() * sizeof(wchar_t); - // TODO(dsinclair): Not sure what to do if we over flow .... - if (!new_pos.IsValid()) - return; - - m_iPosition = new_pos.ValueOrDie(); - } -} - -CFGAS_TextStream::CFGAS_TextStream(std::unique_ptr imp, - bool isWriteStream) - : m_wCodePage(FX_CODEPAGE_DefANSI), - m_wBOMLength(0), - 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 @@ -362,12 +232,14 @@ CFGAS_TextStream::~CFGAS_TextStream() {} #define BOM_UTF16_LE 0xFFFE0000 #endif // _FX_ENDIAN_ == _FX_LITTLE_ENDIAN_ -void CFGAS_TextStream::InitStream() { - FX_FILESIZE iPosition = m_pStreamImp->GetPosition(); - m_pStreamImp->Seek(FX_STREAMSEEK_Begin, 0); +CFGAS_FileReadStreamImp::CFGAS_FileReadStreamImp( + const CFX_RetainPtr& pFileRead) + : CFGAS_Stream(pFileRead, false) { + FX_FILESIZE iPosition = GetPosition(); + Seek(FX_STREAMSEEK_Begin, 0); uint32_t bom; - m_pStreamImp->ReadData(reinterpret_cast(&bom), 3); + ReadData(reinterpret_cast(&bom), 3); bom &= BOM_MASK; if (bom == BOM_UTF8) { @@ -387,46 +259,88 @@ void CFGAS_TextStream::InitStream() { } } - m_pStreamImp->Seek( - FX_STREAMSEEK_Begin, - std::max(static_cast(m_wBOMLength), iPosition)); + Seek(FX_STREAMSEEK_Begin, + std::max(static_cast(m_wBOMLength), iPosition)); } -FX_FILESIZE CFGAS_TextStream::GetLength() const { - return m_pStreamImp->GetLength(); -} +FX_STRSIZE CFGAS_FileReadStreamImp::ReadData(uint8_t* pBuffer, + FX_STRSIZE iBufferSize) { + ASSERT(pBuffer && iBufferSize > 0); + + iBufferSize = std::min( + iBufferSize, static_cast(m_pStream->GetSize() - m_iPosition)); + if (iBufferSize <= 0) + return 0; -void CFGAS_TextStream::Seek(FX_STREAMSEEK eSeek, FX_FILESIZE iOffset) { - m_pStreamImp->Seek(eSeek, iOffset); + if (m_pStream->ReadBlock(pBuffer, m_iPosition, iBufferSize)) { + pdfium::base::CheckedNumeric new_pos = m_iPosition; + new_pos += iBufferSize; + if (!new_pos.IsValid()) + return 0; + + m_iPosition = new_pos.ValueOrDie(); + return iBufferSize; + } + return 0; } -FX_FILESIZE CFGAS_TextStream::GetPosition() { - return m_pStreamImp->GetPosition(); +CFGAS_FileWriteStreamImp::CFGAS_FileWriteStreamImp( + const CFX_RetainPtr& pFileWrite) + : CFGAS_Stream(pFileWrite, true) { + m_iPosition = m_pStream->GetSize(); } -bool CFGAS_TextStream::IsEOF() const { - return m_pStreamImp->IsEOF(); +void CFGAS_FileWriteStreamImp::WriteData(const CFX_WideStringC& str) { + if (str.GetLength() == 0) + return; + + if (!m_pStream->WriteBlock(str.c_str(), m_iPosition, + str.GetLength() * sizeof(wchar_t))) + return; + + pdfium::base::CheckedNumeric new_pos = m_iPosition; + new_pos += str.GetLength() * sizeof(wchar_t); + // TODO(dsinclair): Not sure what to do if we over flow .... + if (!new_pos.IsValid()) + return; + + m_iPosition = new_pos.ValueOrDie(); } -FX_STRSIZE CFGAS_TextStream::GetBOMLength() const { - if (m_wBOMLength < 1) - return 0; - return m_wBOMLength; +CFGAS_Stream::CFGAS_Stream(const CFX_RetainPtr& stream, + bool isWriteStream) + : m_wCodePage(FX_CODEPAGE_DefANSI), + m_wBOMLength(0), + m_IsWriteStream(isWriteStream), + m_iPosition(0), + m_pStream(stream) { + ASSERT(m_pStream); } -uint16_t CFGAS_TextStream::GetCodePage() const { - return m_wCodePage; +CFGAS_Stream::~CFGAS_Stream() {} + +void CFGAS_Stream::Seek(FX_STREAMSEEK eSeek, FX_FILESIZE iOffset) { + switch (eSeek) { + case FX_STREAMSEEK_Begin: + m_iPosition = iOffset; + break; + case FX_STREAMSEEK_Current: + m_iPosition += iOffset; + break; + } + m_iPosition = + pdfium::clamp(m_iPosition, static_cast(0), GetLength()); } -void CFGAS_TextStream::SetCodePage(uint16_t wCodePage) { +void CFGAS_Stream::SetCodePage(uint16_t wCodePage) { if (m_wBOMLength > 0) return; m_wCodePage = wCodePage; } -FX_STRSIZE CFGAS_TextStream::ReadString(wchar_t* pStr, - FX_STRSIZE iMaxLength, - bool* bEOS) { +FX_STRSIZE CFGAS_Stream::ReadString(wchar_t* pStr, + FX_STRSIZE iMaxLength, + bool* bEOS) { ASSERT(pStr && iMaxLength > 0); if (m_IsWriteStream) @@ -435,8 +349,7 @@ FX_STRSIZE CFGAS_TextStream::ReadString(wchar_t* pStr, if (m_wCodePage == FX_CODEPAGE_UTF16LE || m_wCodePage == FX_CODEPAGE_UTF16BE) { FX_FILESIZE iBytes = iMaxLength * 2; - FX_STRSIZE iLen = - m_pStreamImp->ReadData(reinterpret_cast(pStr), iBytes); + FX_STRSIZE iLen = ReadData(reinterpret_cast(pStr), iBytes); iMaxLength = iLen / 2; if (sizeof(wchar_t) > 2) UTF16ToWChar(pStr, iMaxLength); @@ -450,31 +363,31 @@ FX_STRSIZE CFGAS_TextStream::ReadString(wchar_t* pStr, #endif } else { - FX_FILESIZE pos = m_pStreamImp->GetPosition(); - FX_STRSIZE iBytes = std::min( - iMaxLength, static_cast(m_pStreamImp->GetLength() - pos)); + FX_FILESIZE pos = GetPosition(); + FX_STRSIZE iBytes = + std::min(iMaxLength, static_cast(GetLength() - pos)); if (iBytes > 0) { std::vector buf(iBytes); - FX_STRSIZE iLen = m_pStreamImp->ReadData(buf.data(), iBytes); + FX_STRSIZE iLen = ReadData(buf.data(), iBytes); if (m_wCodePage != FX_CODEPAGE_UTF8) return -1; FX_STRSIZE iSrc = 0; std::tie(iSrc, iMaxLength) = UTF8Decode( reinterpret_cast(buf.data()), iLen, pStr, iMaxLength); - m_pStreamImp->Seek(FX_STREAMSEEK_Current, iSrc - iLen); + Seek(FX_STREAMSEEK_Current, iSrc - iLen); } else { iMaxLength = 0; } } - *bEOS = m_pStreamImp->IsEOF(); + *bEOS = IsEOF(); return iMaxLength; } -void CFGAS_TextStream::WriteString(const CFX_WideStringC& str) { +void CFGAS_Stream::WriteString(const CFX_WideStringC& str) { if (!m_IsWriteStream) return; if (str.GetLength() == 0) @@ -482,7 +395,7 @@ void CFGAS_TextStream::WriteString(const CFX_WideStringC& str) { if (m_wCodePage != FX_CODEPAGE_UTF8) return; - m_pStreamImp->WriteData(str); + WriteData(str); } CFGAS_WideStringReadStream::CFGAS_WideStringReadStream( @@ -491,10 +404,6 @@ CFGAS_WideStringReadStream::CFGAS_WideStringReadStream( CFGAS_WideStringReadStream::~CFGAS_WideStringReadStream() {} -FX_FILESIZE CFGAS_WideStringReadStream::GetLength() const { - return m_wsBuffer.GetLength() * sizeof(wchar_t); -} - void CFGAS_WideStringReadStream::Seek(FX_STREAMSEEK eSeek, FX_FILESIZE iOffset) { switch (eSeek) { @@ -509,14 +418,6 @@ void CFGAS_WideStringReadStream::Seek(FX_STREAMSEEK eSeek, static_cast(m_wsBuffer.GetLength())); } -FX_FILESIZE CFGAS_WideStringReadStream::GetPosition() { - return m_iPosition * sizeof(wchar_t); -} - -bool CFGAS_WideStringReadStream::IsEOF() const { - return m_iPosition >= m_wsBuffer.GetLength(); -} - FX_STRSIZE CFGAS_WideStringReadStream::ReadString(wchar_t* pStr, FX_STRSIZE iMaxLength, bool* bEOS) { @@ -532,30 +433,24 @@ FX_STRSIZE CFGAS_WideStringReadStream::ReadString(wchar_t* pStr, return iMaxLength; } -uint16_t CFGAS_WideStringReadStream::GetCodePage() const { - return (sizeof(wchar_t) == 2) ? FX_CODEPAGE_UTF16LE : FX_CODEPAGE_UTF32LE; -} - } // namespace // static CFX_RetainPtr IFGAS_Stream::CreateReadStream( - const CFX_RetainPtr& pFileRead) { + const CFX_RetainPtr& pFileRead) { if (!pFileRead) return nullptr; - return pdfium::MakeRetain( - pdfium::MakeUnique(pFileRead), false); + return pdfium::MakeRetain(pFileRead); } // static CFX_RetainPtr IFGAS_Stream::CreateWriteStream( - const CFX_RetainPtr& pFileWrite) { + const CFX_RetainPtr& pFileWrite) { if (!pFileWrite) return nullptr; - return pdfium::MakeRetain( - pdfium::MakeUnique(pFileWrite), true); + return pdfium::MakeRetain(pFileWrite); } // static diff --git a/xfa/fgas/crt/ifgas_stream.h b/xfa/fgas/crt/ifgas_stream.h index be42fe5ca3..620a023dd8 100644 --- a/xfa/fgas/crt/ifgas_stream.h +++ b/xfa/fgas/crt/ifgas_stream.h @@ -19,9 +19,9 @@ enum FX_STREAMSEEK { class IFGAS_Stream : public CFX_Retainable { public: static CFX_RetainPtr CreateReadStream( - const CFX_RetainPtr& pFileRead); + const CFX_RetainPtr& pFileRead); static CFX_RetainPtr CreateWriteStream( - const CFX_RetainPtr& pFileWrite); + const CFX_RetainPtr& pFileWrite); static CFX_RetainPtr CreateWideStringReadStream( const CFX_WideString& buffer); -- cgit v1.2.3