From 7b7c6532310eeeabadae7b34fdf86f4a890951e8 Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Thu, 13 Apr 2017 17:03:37 -0400 Subject: Fold LoadFile{Read|Write} back into constructors The load file methods are always called right after creating the class. This Cl moves their code up into the constructor and then changes the other code to assume that the m_pFile{Read|Write} always exists. Change-Id: I015abf71ea4804d02d4f6f94b97eb1e7855e1fc4 Reviewed-on: https://pdfium-review.googlesource.com/4110 Commit-Queue: dsinclair Reviewed-by: Tom Sepez --- xfa/fgas/crt/fgas_codepage.cpp | 124 --------- xfa/fgas/crt/fgas_codepage.h | 13 - xfa/fgas/crt/ifgas_stream.cpp | 557 ++++++++++++++++++++++------------------- xfa/fgas/crt/ifgas_stream.h | 30 ++- 4 files changed, 315 insertions(+), 409 deletions(-) (limited to 'xfa/fgas/crt') diff --git a/xfa/fgas/crt/fgas_codepage.cpp b/xfa/fgas/crt/fgas_codepage.cpp index cae2dd7234..c1a4322883 100644 --- a/xfa/fgas/crt/fgas_codepage.cpp +++ b/xfa/fgas/crt/fgas_codepage.cpp @@ -68,127 +68,3 @@ uint16_t FX_GetCodePageFromCharset(uint8_t charset) { } while (iStart <= iEnd); return 0xFFFF; } - -void FX_SwapByteOrder(wchar_t* pStr, int32_t iLength) { - ASSERT(pStr); - - if (iLength < 0) - iLength = FXSYS_wcslen(pStr); - - uint16_t wch; - if (sizeof(wchar_t) > 2) { - while (iLength-- > 0) { - wch = (uint16_t)*pStr; - wch = (wch >> 8) | (wch << 8); - wch &= 0x00FF; - *pStr++ = wch; - } - return; - } - - while (iLength-- > 0) { - wch = (uint16_t)*pStr; - wch = (wch >> 8) | (wch << 8); - *pStr++ = wch; - } -} - -void FX_UTF16ToWChar(void* pBuffer, int32_t iLength) { - ASSERT(pBuffer && iLength > 0); - if (sizeof(wchar_t) == 2) - return; - - uint16_t* pSrc = static_cast(pBuffer); - wchar_t* pDst = static_cast(pBuffer); - while (--iLength >= 0) - pDst[iLength] = static_cast(pSrc[iLength]); -} - -int32_t FX_DecodeString(uint16_t wCodePage, - const char* pSrc, - int32_t* pSrcLen, - wchar_t* pDst, - int32_t* pDstLen, - bool bErrBreak) { - if (wCodePage == FX_CODEPAGE_UTF8) - return FX_UTF8Decode(pSrc, pSrcLen, pDst, pDstLen); - return -1; -} - -int32_t FX_UTF8Decode(const char* pSrc, - int32_t* pSrcLen, - wchar_t* pDst, - int32_t* pDstLen) { - if (!pSrcLen || !pDstLen) - return -1; - - int32_t iSrcLen = *pSrcLen; - if (iSrcLen < 1) { - *pSrcLen = *pDstLen = 0; - return 1; - } - - int32_t iDstLen = *pDstLen; - bool bValidDst = (pDst && iDstLen > 0); - uint32_t dwCode = 0; - int32_t iPending = 0; - int32_t iSrcNum = 0; - int32_t iDstNum = 0; - int32_t iIndex = 0; - int32_t k = 1; - while (iIndex < iSrcLen) { - uint8_t byte = static_cast(*(pSrc + iIndex)); - if (byte < 0x80) { - iPending = 0; - k = 1; - iDstNum++; - iSrcNum += k; - if (bValidDst) { - *pDst++ = byte; - if (iDstNum >= iDstLen) - break; - } - } else if (byte < 0xc0) { - if (iPending < 1) - break; - - iPending--; - dwCode |= (byte & 0x3f) << (iPending * 6); - if (iPending == 0) { - iDstNum++; - iSrcNum += k; - if (bValidDst) { - *pDst++ = dwCode; - if (iDstNum >= iDstLen) - break; - } - } - } else if (byte < 0xe0) { - iPending = 1; - k = 2; - dwCode = (byte & 0x1f) << 6; - } else if (byte < 0xf0) { - iPending = 2; - k = 3; - dwCode = (byte & 0x0f) << 12; - } else if (byte < 0xf8) { - iPending = 3; - k = 4; - dwCode = (byte & 0x07) << 18; - } else if (byte < 0xfc) { - iPending = 4; - k = 5; - dwCode = (byte & 0x03) << 24; - } else if (byte < 0xfe) { - iPending = 5; - k = 6; - dwCode = (byte & 0x01) << 30; - } else { - break; - } - iIndex++; - } - *pSrcLen = iSrcNum; - *pDstLen = iDstNum; - return 1; -} diff --git a/xfa/fgas/crt/fgas_codepage.h b/xfa/fgas/crt/fgas_codepage.h index 3dfabe600e..8e29736149 100644 --- a/xfa/fgas/crt/fgas_codepage.h +++ b/xfa/fgas/crt/fgas_codepage.h @@ -134,18 +134,5 @@ #define FX_CHARSET_OEM 255 uint16_t FX_GetCodePageFromCharset(uint8_t charset); -void FX_SwapByteOrder(wchar_t* pStr, int32_t iLength); - -void FX_UTF16ToWChar(void* pBuffer, int32_t iLength); -int32_t FX_DecodeString(uint16_t wCodePage, - const char* pSrc, - int32_t* pSrcLen, - wchar_t* pDst, - int32_t* pDstLen, - bool bErrBreak); -int32_t FX_UTF8Decode(const char* pSrc, - int32_t* pSrcLen, - wchar_t* pDst, - int32_t* pDstLen); #endif // XFA_FGAS_CRT_FGAS_CODEPAGE_H_ diff --git a/xfa/fgas/crt/ifgas_stream.cpp b/xfa/fgas/crt/ifgas_stream.cpp index 712243d701..b4d03cf810 100644 --- a/xfa/fgas/crt/ifgas_stream.cpp +++ b/xfa/fgas/crt/ifgas_stream.cpp @@ -27,16 +27,16 @@ class IFGAS_StreamImp { public: virtual ~IFGAS_StreamImp() {} - virtual int32_t GetLength() const = 0; - virtual int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset) = 0; - virtual int32_t GetPosition() = 0; + 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 int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize) = 0; - virtual int32_t ReadString(wchar_t* pStr, int32_t iMaxLength, bool& bEOS) = 0; - virtual int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize) = 0; - virtual int32_t WriteString(const wchar_t* pStr, int32_t iLength) = 0; - virtual void Flush() = 0; - virtual bool SetLength(int32_t iLength) = 0; + virtual FX_STRSIZE ReadData(uint8_t* pBuffer, FX_STRSIZE iBufferSize) = 0; + virtual FX_STRSIZE ReadString(wchar_t* pStr, + FX_STRSIZE iMaxLength, + bool* bEOS) = 0; + virtual void WriteData(const uint8_t* pBuffer, FX_STRSIZE iBufferSize) = 0; + virtual void WriteString(const wchar_t* pStr, FX_STRSIZE iLength) = 0; protected: IFGAS_StreamImp(); @@ -44,57 +44,52 @@ class IFGAS_StreamImp { class CFGAS_FileReadStreamImp : public IFGAS_StreamImp { public: - CFGAS_FileReadStreamImp(); + explicit CFGAS_FileReadStreamImp( + const CFX_RetainPtr& pFileRead); ~CFGAS_FileReadStreamImp() override {} - bool LoadFileRead(const CFX_RetainPtr& pFileRead); - // IFGAS_StreamImp: - int32_t GetLength() const override; - int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset) override; - int32_t GetPosition() override { return m_iPosition; } + 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; - 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 { - return 0; - } - int32_t WriteString(const wchar_t* pStr, int32_t iLength) override { - return 0; - } - void Flush() override {} - bool SetLength(int32_t iLength) override { return false; } + FX_STRSIZE ReadData(uint8_t* pBuffer, FX_STRSIZE iBufferSize) override; + FX_STRSIZE ReadString(wchar_t* pStr, + FX_STRSIZE iMaxLength, + bool* bEOS) override; + void WriteData(const uint8_t* pBuffer, FX_STRSIZE iBufferSize) override {} + void WriteString(const wchar_t* pStr, FX_STRSIZE iLength) override {} private: CFX_RetainPtr m_pFileRead; - int32_t m_iPosition; - int32_t m_iLength; + FX_FILESIZE m_iPosition; }; class CFGAS_FileWriteStreamImp : public IFGAS_StreamImp { public: - CFGAS_FileWriteStreamImp(); + CFGAS_FileWriteStreamImp( + const CFX_RetainPtr& pFileWrite); ~CFGAS_FileWriteStreamImp() override {} - bool LoadFileWrite(const CFX_RetainPtr& pFileWrite); - // IFGAS_StreamImp: - int32_t GetLength() const override; - int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset) override; - int32_t GetPosition() override { return m_iPosition; } + 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; - int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize) override { return 0; } - int32_t ReadString(wchar_t* pStr, int32_t iMaxLength, bool& bEOS) override { + FX_STRSIZE ReadData(uint8_t* pBuffer, FX_STRSIZE iBufferSize) override { return 0; } - 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 { return false; } + FX_STRSIZE ReadString(wchar_t* pStr, + FX_STRSIZE iMaxLength, + bool* bEOS) override { + return 0; + } + void WriteData(const uint8_t* pBuffer, FX_STRSIZE iBufferSize) override; + void WriteString(const wchar_t* pStr, FX_STRSIZE iLength) override; private: CFX_RetainPtr m_pFileWrite; - int32_t m_iPosition; + FX_FILESIZE m_iPosition; }; class CFGAS_TextStream : public IFGAS_Stream { @@ -103,19 +98,19 @@ class CFGAS_TextStream : public IFGAS_Stream { friend CFX_RetainPtr pdfium::MakeRetain(Args&&... args); // IFGAS_Stream - int32_t GetLength() const override; - int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset) override; - int32_t GetPosition() override; + 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; - 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 GetBOMLength() const override; + FX_STRSIZE ReadData(uint8_t* pBuffer, FX_STRSIZE iBufferSize) override; + FX_STRSIZE ReadString(wchar_t* pStr, + FX_STRSIZE iMaxLength, + bool* bEOS) override; + void WriteData(const uint8_t* pBuffer, FX_STRSIZE iBufferSize) override; + void WriteString(const wchar_t* pStr, FX_STRSIZE iLength) override; uint16_t GetCodePage() const override; - uint16_t SetCodePage(uint16_t wCodePage) override; + void SetCodePage(uint16_t wCodePage) override; private: CFGAS_TextStream(std::unique_ptr imp, bool isWriteSteam); @@ -124,7 +119,7 @@ class CFGAS_TextStream : public IFGAS_Stream { void InitStream(); uint16_t m_wCodePage; - int32_t m_wBOMLength; + FX_STRSIZE m_wBOMLength; bool m_IsWriteStream; std::unique_ptr m_pStreamImp; }; @@ -135,47 +130,148 @@ class CFGAS_WideStringReadStream : public IFGAS_Stream { friend CFX_RetainPtr pdfium::MakeRetain(Args&&... args); // IFGAS_Stream - int32_t GetLength() const override; - int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset) override; - int32_t GetPosition() override; + FX_FILESIZE GetLength() const override; + FX_FILESIZE GetPosition() override; + FX_STRSIZE GetBOMLength() const override { return 0; } + void Seek(FX_STREAMSEEK eSeek, FX_FILESIZE iOffset) 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 GetBOMLength() const override; + FX_STRSIZE ReadData(uint8_t* pBuffer, FX_STRSIZE iBufferSize) override { + return 0; + } + FX_STRSIZE ReadString(wchar_t* pStr, + FX_STRSIZE iMaxLength, + bool* bEOS) override; + void WriteData(const uint8_t* pBuffer, FX_STRSIZE iBufferSize) override {} + void WriteString(const wchar_t* pStr, FX_STRSIZE iLength) override {} uint16_t GetCodePage() const override; - uint16_t SetCodePage(uint16_t wCodePage) override; + void SetCodePage(uint16_t wCodePage) override {} private: explicit CFGAS_WideStringReadStream(const CFX_WideString& wsBuffer); ~CFGAS_WideStringReadStream() override; CFX_WideString m_wsBuffer; - int32_t m_iPosition; + FX_FILESIZE m_iPosition; }; -IFGAS_StreamImp::IFGAS_StreamImp() {} +// Returns {src bytes consumed, dst bytes produced}. +std::pair UTF8Decode(const char* pSrc, + FX_STRSIZE srcLen, + wchar_t* pDst, + FX_STRSIZE dstLen) { + ASSERT(pDst && dstLen > 0); + + if (srcLen < 1) + return {0, 0}; + + uint32_t dwCode = 0; + int32_t iPending = 0; + FX_STRSIZE iSrcNum = 0; + FX_STRSIZE iDstNum = 0; + FX_STRSIZE iIndex = 0; + int32_t k = 1; + while (iIndex < srcLen) { + uint8_t byte = static_cast(*(pSrc + iIndex)); + if (byte < 0x80) { + iPending = 0; + k = 1; + iDstNum++; + iSrcNum += k; + *pDst++ = byte; + if (iDstNum >= dstLen) + break; + } else if (byte < 0xc0) { + if (iPending < 1) + break; + + iPending--; + dwCode |= (byte & 0x3f) << (iPending * 6); + if (iPending == 0) { + iDstNum++; + iSrcNum += k; + *pDst++ = dwCode; + if (iDstNum >= dstLen) + break; + } + } else if (byte < 0xe0) { + iPending = 1; + k = 2; + dwCode = (byte & 0x1f) << 6; + } else if (byte < 0xf0) { + iPending = 2; + k = 3; + dwCode = (byte & 0x0f) << 12; + } else if (byte < 0xf8) { + iPending = 3; + k = 4; + dwCode = (byte & 0x07) << 18; + } else if (byte < 0xfc) { + iPending = 4; + k = 5; + dwCode = (byte & 0x03) << 24; + } else if (byte < 0xfe) { + iPending = 5; + k = 6; + dwCode = (byte & 0x01) << 30; + } else { + break; + } + iIndex++; + } + return {iSrcNum, iDstNum}; +} -CFGAS_FileReadStreamImp::CFGAS_FileReadStreamImp() - : m_pFileRead(nullptr), m_iPosition(0), m_iLength(0) {} +void UTF16ToWChar(void* pBuffer, FX_STRSIZE iLength) { + ASSERT(pBuffer && iLength > 0); -bool CFGAS_FileReadStreamImp::LoadFileRead( - const CFX_RetainPtr& pFileRead) { - ASSERT(!m_pFileRead && pFileRead); + if (sizeof(wchar_t) == 2) + return; + + uint16_t* pSrc = static_cast(pBuffer); + wchar_t* pDst = static_cast(pBuffer); + while (--iLength >= 0) + pDst[iLength] = static_cast(pSrc[iLength]); +} + +void SwapByteOrder(wchar_t* pStr, FX_STRSIZE iLength) { + ASSERT(pStr); + + if (iLength < 0) + iLength = FXSYS_wcslen(pStr); + + uint16_t wch; + if (sizeof(wchar_t) > 2) { + while (iLength-- > 0) { + wch = static_cast(*pStr); + wch = (wch >> 8) | (wch << 8); + wch &= 0x00FF; + *pStr = wch; + pStr++; + } + return; + } - m_pFileRead = pFileRead; - m_iLength = m_pFileRead->GetSize(); - return true; + while (iLength-- > 0) { + wch = static_cast(*pStr); + wch = (wch >> 8) | (wch << 8); + *pStr = wch; + pStr++; + } } -int32_t CFGAS_FileReadStreamImp::GetLength() const { - return m_iLength; +IFGAS_StreamImp::IFGAS_StreamImp() {} + +CFGAS_FileReadStreamImp::CFGAS_FileReadStreamImp( + const CFX_RetainPtr& pFileRead) + : m_pFileRead(pFileRead), m_iPosition(0) { + ASSERT(m_pFileRead); } -int32_t CFGAS_FileReadStreamImp::Seek(FX_STREAMSEEK eSeek, int32_t iOffset) { +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; @@ -183,71 +279,63 @@ int32_t CFGAS_FileReadStreamImp::Seek(FX_STREAMSEEK eSeek, int32_t iOffset) { case FX_STREAMSEEK_Current: m_iPosition += iOffset; break; - case FX_STREAMSEEK_End: - m_iPosition = m_iLength + iOffset; - break; } - if (m_iPosition < 0) { - m_iPosition = 0; - } else if (m_iPosition >= m_iLength) { - m_iPosition = m_iLength; - } - return m_iPosition; + m_iPosition = pdfium::clamp(m_iPosition, static_cast(0), + m_pFileRead->GetSize()); } bool CFGAS_FileReadStreamImp::IsEOF() const { - return m_iPosition >= m_iLength; + return m_iPosition >= m_pFileRead->GetSize(); } -int32_t CFGAS_FileReadStreamImp::ReadData(uint8_t* pBuffer, - int32_t iBufferSize) { - ASSERT(m_pFileRead); + +FX_STRSIZE CFGAS_FileReadStreamImp::ReadData(uint8_t* pBuffer, + FX_STRSIZE iBufferSize) { ASSERT(pBuffer && iBufferSize > 0); - if (iBufferSize > m_iLength - m_iPosition) { - iBufferSize = m_iLength - m_iPosition; - } + + iBufferSize = + std::min(iBufferSize, + static_cast(m_pFileRead->GetSize() - m_iPosition)); if (m_pFileRead->ReadBlock(pBuffer, m_iPosition, iBufferSize)) { - 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; } -int32_t CFGAS_FileReadStreamImp::ReadString(wchar_t* pStr, - int32_t iMaxLength, - bool& bEOS) { - ASSERT(m_pFileRead); + +FX_STRSIZE CFGAS_FileReadStreamImp::ReadString(wchar_t* pStr, + FX_STRSIZE iMaxLength, + bool* bEOS) { ASSERT(pStr && iMaxLength > 0); - iMaxLength = ReadData((uint8_t*)pStr, iMaxLength * 2) / 2; - if (iMaxLength <= 0) { + + iMaxLength = ReadData(reinterpret_cast(pStr), iMaxLength * 2) / 2; + if (iMaxLength <= 0) return 0; - } - int32_t i = 0; - while (i < iMaxLength && pStr[i] != L'\0') { + + FX_STRSIZE i = 0; + while (i < iMaxLength && pStr[i] != L'\0') ++i; - } - bEOS = (m_iPosition >= m_iLength) || pStr[i] == L'\0'; + + *bEOS = m_iPosition >= m_pFileRead->GetSize() || pStr[i] == L'\0'; return i; } -CFGAS_FileWriteStreamImp::CFGAS_FileWriteStreamImp() - : m_pFileWrite(nullptr), m_iPosition(0) {} - -bool CFGAS_FileWriteStreamImp::LoadFileWrite( - const CFX_RetainPtr& pFileWrite) { - ASSERT(!m_pFileWrite && pFileWrite); - - m_iPosition = pFileWrite->GetSize(); - m_pFileWrite = pFileWrite; - return true; +CFGAS_FileWriteStreamImp::CFGAS_FileWriteStreamImp( + const CFX_RetainPtr& pFileWrite) + : m_pFileWrite(pFileWrite), m_iPosition(m_pFileWrite->GetSize()) { + ASSERT(m_pFileWrite); } -int32_t CFGAS_FileWriteStreamImp::GetLength() const { - if (!m_pFileWrite) - return 0; - - return (int32_t)m_pFileWrite->GetSize(); +FX_FILESIZE CFGAS_FileWriteStreamImp::GetLength() const { + return m_pFileWrite->GetSize(); } -int32_t CFGAS_FileWriteStreamImp::Seek(FX_STREAMSEEK eSeek, int32_t iOffset) { - int32_t iLength = GetLength(); + +void CFGAS_FileWriteStreamImp::Seek(FX_STREAMSEEK eSeek, FX_FILESIZE iOffset) { + FX_FILESIZE iLength = GetLength(); switch (eSeek) { case FX_STREAMSEEK_Begin: m_iPosition = iOffset; @@ -255,38 +343,31 @@ int32_t CFGAS_FileWriteStreamImp::Seek(FX_STREAMSEEK eSeek, int32_t iOffset) { case FX_STREAMSEEK_Current: m_iPosition += iOffset; break; - case FX_STREAMSEEK_End: - m_iPosition = iLength + iOffset; - break; - } - if (m_iPosition < 0) { - m_iPosition = 0; - } else if (m_iPosition >= iLength) { - m_iPosition = iLength; } - return m_iPosition; + m_iPosition = + pdfium::clamp(m_iPosition, static_cast(0), iLength); } + bool CFGAS_FileWriteStreamImp::IsEOF() const { return m_iPosition >= GetLength(); } -int32_t CFGAS_FileWriteStreamImp::WriteData(const uint8_t* pBuffer, - int32_t iBufferSize) { - if (!m_pFileWrite) { - return 0; - } + +void CFGAS_FileWriteStreamImp::WriteData(const uint8_t* pBuffer, + FX_STRSIZE iBufferSize) { if (m_pFileWrite->WriteBlock(pBuffer, m_iPosition, iBufferSize)) { - m_iPosition += iBufferSize; + pdfium::base::CheckedNumeric new_pos = m_iPosition; + new_pos += iBufferSize; + // TODO(dsinclair): Not sure what to do if we over flow .... + if (!new_pos.IsValid()) + return; + + m_iPosition = new_pos.ValueOrDie(); } - return iBufferSize; } -int32_t CFGAS_FileWriteStreamImp::WriteString(const wchar_t* pStr, - int32_t iLength) { - return WriteData((const uint8_t*)pStr, iLength * sizeof(wchar_t)); -} -void CFGAS_FileWriteStreamImp::Flush() { - if (m_pFileWrite) { - m_pFileWrite->Flush(); - } + +void CFGAS_FileWriteStreamImp::WriteString(const wchar_t* pStr, + FX_STRSIZE iLength) { + WriteData(reinterpret_cast(pStr), iLength * sizeof(wchar_t)); } CFGAS_TextStream::CFGAS_TextStream(std::unique_ptr imp, @@ -316,7 +397,7 @@ CFGAS_TextStream::~CFGAS_TextStream() {} #endif // _FX_ENDIAN_ == _FX_LITTLE_ENDIAN_ void CFGAS_TextStream::InitStream() { - int32_t iPosition = m_pStreamImp->GetPosition(); + FX_FILESIZE iPosition = m_pStreamImp->GetPosition(); m_pStreamImp->Seek(FX_STREAMSEEK_Begin, 0); uint32_t bom; @@ -340,18 +421,20 @@ void CFGAS_TextStream::InitStream() { } } - m_pStreamImp->Seek(FX_STREAMSEEK_Begin, std::max(m_wBOMLength, iPosition)); + m_pStreamImp->Seek( + FX_STREAMSEEK_Begin, + std::max(static_cast(m_wBOMLength), iPosition)); } -int32_t CFGAS_TextStream::GetLength() const { +FX_FILESIZE CFGAS_TextStream::GetLength() const { return m_pStreamImp->GetLength(); } -int32_t CFGAS_TextStream::Seek(FX_STREAMSEEK eSeek, int32_t iOffset) { - return m_pStreamImp->Seek(eSeek, iOffset); +void CFGAS_TextStream::Seek(FX_STREAMSEEK eSeek, FX_FILESIZE iOffset) { + m_pStreamImp->Seek(eSeek, iOffset); } -int32_t CFGAS_TextStream::GetPosition() { +FX_FILESIZE CFGAS_TextStream::GetPosition() { return m_pStreamImp->GetPosition(); } @@ -359,42 +442,33 @@ bool CFGAS_TextStream::IsEOF() const { return m_pStreamImp->IsEOF(); } -int32_t CFGAS_TextStream::ReadData(uint8_t* pBuffer, int32_t iBufferSize) { +FX_STRSIZE CFGAS_TextStream::ReadData(uint8_t* pBuffer, + FX_STRSIZE iBufferSize) { ASSERT(pBuffer && iBufferSize > 0); if (m_IsWriteStream) return -1; - int32_t iLen = std::min( - m_pStreamImp->GetLength() - m_pStreamImp->GetPosition(), iBufferSize); + FX_STRSIZE iLen = + std::min(static_cast(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) { +void CFGAS_TextStream::WriteData(const uint8_t* pBuffer, + FX_STRSIZE 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); + m_pStreamImp->WriteData(pBuffer, iBufferSize); } -int32_t CFGAS_TextStream::GetBOMLength() const { +FX_STRSIZE CFGAS_TextStream::GetBOMLength() const { if (m_wBOMLength < 1) return 0; return m_wBOMLength; @@ -404,76 +478,78 @@ uint16_t CFGAS_TextStream::GetCodePage() const { return m_wCodePage; } -uint16_t CFGAS_TextStream::SetCodePage(uint16_t wCodePage) { +void CFGAS_TextStream::SetCodePage(uint16_t wCodePage) { if (m_wBOMLength > 0) - return m_wCodePage; + return; - uint16_t v = m_wCodePage; m_wCodePage = wCodePage; - return v; } -int32_t CFGAS_TextStream::ReadString(wchar_t* pStr, - int32_t iMaxLength, - bool& bEOS) { +FX_STRSIZE CFGAS_TextStream::ReadString(wchar_t* pStr, + FX_STRSIZE iMaxLength, + bool* bEOS) { ASSERT(pStr && iMaxLength > 0); + if (m_IsWriteStream) return -1; if (m_wCodePage == FX_CODEPAGE_UTF16LE || m_wCodePage == FX_CODEPAGE_UTF16BE) { - int32_t iBytes = iMaxLength * 2; - int32_t iLen = m_pStreamImp->ReadData((uint8_t*)pStr, iBytes); + FX_FILESIZE iBytes = iMaxLength * 2; + FX_STRSIZE iLen = m_pStreamImp->ReadData((uint8_t*)pStr, iBytes); iMaxLength = iLen / 2; if (sizeof(wchar_t) > 2) - FX_UTF16ToWChar(pStr, iMaxLength); + UTF16ToWChar(pStr, iMaxLength); #if _FX_ENDIAN_ == _FX_BIG_ENDIAN_ if (m_wCodePage == FX_CODEPAGE_UTF16LE) - FX_SwapByteOrder(pStr, iMaxLength); + SwapByteOrder(pStr, iMaxLength); #else if (m_wCodePage == FX_CODEPAGE_UTF16BE) - FX_SwapByteOrder(pStr, iMaxLength); + SwapByteOrder(pStr, iMaxLength); #endif } else { - int32_t pos = m_pStreamImp->GetPosition(); - int32_t iBytes = std::min(iMaxLength, m_pStreamImp->GetLength() - pos); + FX_FILESIZE pos = m_pStreamImp->GetPosition(); + FX_STRSIZE iBytes = std::min( + iMaxLength, static_cast(m_pStreamImp->GetLength() - pos)); + if (iBytes > 0) { std::vector buf(iBytes); - int32_t iLen = m_pStreamImp->ReadData(buf.data(), iBytes); - int32_t iSrc = iLen; - int32_t iDecode = FX_DecodeString( - m_wCodePage, reinterpret_cast(buf.data()), &iSrc, pStr, - &iMaxLength, true); - m_pStreamImp->Seek(FX_STREAMSEEK_Current, iSrc - iLen); - if (iDecode < 1) + FX_STRSIZE iLen = m_pStreamImp->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); } else { iMaxLength = 0; } } - bEOS = m_pStreamImp->IsEOF(); + + *bEOS = m_pStreamImp->IsEOF(); return iMaxLength; } -int32_t CFGAS_TextStream::WriteString(const wchar_t* pStr, int32_t iLength) { +void CFGAS_TextStream::WriteString(const wchar_t* pStr, FX_STRSIZE iLength) { ASSERT(pStr && iLength > 0); if (!m_IsWriteStream) - return -1; + return; - if (m_wCodePage == FX_CODEPAGE_UTF8) { - int32_t len = iLength; - CFX_UTF8Encoder encoder; - while (len-- > 0) { - encoder.Input(*pStr++); - } - CFX_ByteStringC bsResult = encoder.GetResult(); - m_pStreamImp->WriteData((const uint8_t*)bsResult.c_str(), - bsResult.GetLength()); - } - return iLength; + if (m_wCodePage != FX_CODEPAGE_UTF8) + return; + + FX_STRSIZE len = iLength; + CFX_UTF8Encoder encoder; + while (len-- > 0) + encoder.Input(*pStr++); + + CFX_ByteStringC bsResult = encoder.GetResult(); + m_pStreamImp->WriteData(reinterpret_cast(bsResult.c_str()), + bsResult.GetLength()); } CFGAS_WideStringReadStream::CFGAS_WideStringReadStream( @@ -482,11 +558,12 @@ CFGAS_WideStringReadStream::CFGAS_WideStringReadStream( CFGAS_WideStringReadStream::~CFGAS_WideStringReadStream() {} -int32_t CFGAS_WideStringReadStream::GetLength() const { +FX_FILESIZE CFGAS_WideStringReadStream::GetLength() const { return m_wsBuffer.GetLength() * sizeof(wchar_t); } -int32_t CFGAS_WideStringReadStream::Seek(FX_STREAMSEEK eSeek, int32_t iOffset) { +void CFGAS_WideStringReadStream::Seek(FX_STREAMSEEK eSeek, + FX_FILESIZE iOffset) { switch (eSeek) { case FX_STREAMSEEK_Begin: m_iPosition = iOffset; @@ -494,15 +571,12 @@ int32_t CFGAS_WideStringReadStream::Seek(FX_STREAMSEEK eSeek, int32_t iOffset) { case FX_STREAMSEEK_Current: m_iPosition += iOffset; break; - case FX_STREAMSEEK_End: - m_iPosition = m_wsBuffer.GetLength() + iOffset; - break; } - m_iPosition = pdfium::clamp(0, m_iPosition, m_wsBuffer.GetLength()); - return GetPosition(); + m_iPosition = pdfium::clamp(m_iPosition, static_cast(0), + static_cast(m_wsBuffer.GetLength())); } -int32_t CFGAS_WideStringReadStream::GetPosition() { +FX_FILESIZE CFGAS_WideStringReadStream::GetPosition() { return m_iPosition * sizeof(wchar_t); } @@ -510,50 +584,25 @@ bool CFGAS_WideStringReadStream::IsEOF() const { return m_iPosition >= m_wsBuffer.GetLength(); } -int32_t CFGAS_WideStringReadStream::ReadData(uint8_t* pBuffer, - int32_t iBufferSize) { - return 0; -} - -int32_t CFGAS_WideStringReadStream::ReadString(wchar_t* pStr, - int32_t iMaxLength, - bool& bEOS) { - iMaxLength = std::min(iMaxLength, m_wsBuffer.GetLength() - m_iPosition); +FX_STRSIZE CFGAS_WideStringReadStream::ReadString(wchar_t* pStr, + FX_STRSIZE iMaxLength, + bool* bEOS) { + iMaxLength = + std::min(iMaxLength, + static_cast(m_wsBuffer.GetLength() - m_iPosition)); if (iMaxLength == 0) return 0; FXSYS_wcsncpy(pStr, m_wsBuffer.c_str() + m_iPosition, iMaxLength); m_iPosition += iMaxLength; - bEOS = IsEOF(); + *bEOS = IsEOF(); return iMaxLength; } -int32_t CFGAS_WideStringReadStream::WriteData(const uint8_t* pBuffer, - int32_t iBufferSize) { - return 0; -} - -int32_t CFGAS_WideStringReadStream::WriteString(const wchar_t* pStr, - int32_t iLength) { - return 0; -} - -bool CFGAS_WideStringReadStream::SetLength(int32_t iLength) { - return false; -} - -int32_t CFGAS_WideStringReadStream::GetBOMLength() const { - return 0; -} - uint16_t CFGAS_WideStringReadStream::GetCodePage() const { return (sizeof(wchar_t) == 2) ? FX_CODEPAGE_UTF16LE : FX_CODEPAGE_UTF32LE; } -uint16_t CFGAS_WideStringReadStream::SetCodePage(uint16_t wCodePage) { - return GetCodePage(); -} - } // namespace // static @@ -562,13 +611,8 @@ CFX_RetainPtr IFGAS_Stream::CreateReadStream( if (!pFileRead) return nullptr; - std::unique_ptr pImp = - pdfium::MakeUnique(); - if (!static_cast(pImp.get()) - ->LoadFileRead(pFileRead)) { - return nullptr; - } - return pdfium::MakeRetain(std::move(pImp), false); + return pdfium::MakeRetain( + pdfium::MakeUnique(pFileRead), false); } // static @@ -577,13 +621,8 @@ CFX_RetainPtr IFGAS_Stream::CreateWriteStream( if (!pFileWrite) return nullptr; - std::unique_ptr pImp = - pdfium::MakeUnique(); - if (!static_cast(pImp.get()) - ->LoadFileWrite(pFileWrite)) { - return nullptr; - } - return pdfium::MakeRetain(std::move(pImp), true); + return pdfium::MakeRetain( + pdfium::MakeUnique(pFileWrite), true); } // static diff --git a/xfa/fgas/crt/ifgas_stream.h b/xfa/fgas/crt/ifgas_stream.h index b3f8a928d8..658afe73bf 100644 --- a/xfa/fgas/crt/ifgas_stream.h +++ b/xfa/fgas/crt/ifgas_stream.h @@ -14,7 +14,6 @@ enum FX_STREAMSEEK { FX_STREAMSEEK_Begin = 0, FX_STREAMSEEK_Current, - FX_STREAMSEEK_End, }; class IFGAS_Stream : public CFX_Retainable { @@ -26,19 +25,24 @@ class IFGAS_Stream : public CFX_Retainable { static CFX_RetainPtr CreateWideStringReadStream( const CFX_WideString& buffer); - virtual int32_t GetLength() const = 0; - virtual int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset) = 0; - virtual int32_t GetPosition() = 0; - virtual bool IsEOF() const = 0; - virtual int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize) = 0; - virtual int32_t ReadString(wchar_t* pStr, int32_t iMaxLength, bool& bEOS) = 0; - virtual int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize) = 0; - 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 GetBOMLength() const = 0; + virtual FX_FILESIZE GetLength() const = 0; + virtual FX_FILESIZE GetPosition() = 0; + virtual FX_STRSIZE GetBOMLength() const = 0; + + virtual void Seek(FX_STREAMSEEK eSeek, FX_FILESIZE iOffset) = 0; + + virtual FX_STRSIZE ReadString(wchar_t* pStr, + FX_STRSIZE iMaxLength, + bool* bEOS) = 0; + virtual void WriteData(const uint8_t* pBuffer, FX_STRSIZE iBufferSize) = 0; + virtual void WriteString(const wchar_t* pStr, FX_STRSIZE iLength) = 0; + virtual uint16_t GetCodePage() const = 0; - virtual uint16_t SetCodePage(uint16_t wCodePage) = 0; + virtual void SetCodePage(uint16_t wCodePage) = 0; + + protected: + virtual bool IsEOF() const = 0; + virtual FX_STRSIZE ReadData(uint8_t* pBuffer, FX_STRSIZE iBufferSize) = 0; }; #endif // XFA_FGAS_CRT_IFGAS_STREAM_H_ -- cgit v1.2.3