summaryrefslogtreecommitdiff
path: root/xfa/fgas/crt
diff options
context:
space:
mode:
authorDan Sinclair <dsinclair@chromium.org>2017-04-12 15:31:05 -0400
committerChromium commit bot <commit-bot@chromium.org>2017-04-12 20:14:48 +0000
commitf6baf6b1d101a889d2cfc7cb925e726dbffb89db (patch)
tree6e174dc5228f3e4ea63bf5e0d4950517234278fd /xfa/fgas/crt
parent81513c8243390783540b8bb6d0f9b26d9cb0c2c1 (diff)
downloadpdfium-f6baf6b1d101a889d2cfc7cb925e726dbffb89db.tar.xz
Fold CFGAS_Stream into CFGAS_TextStream.
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 <dsinclair@chromium.org> Reviewed-by: Nicolás Peña <npm@chromium.org>
Diffstat (limited to 'xfa/fgas/crt')
-rw-r--r--xfa/fgas/crt/ifgas_stream.cpp281
-rw-r--r--xfa/fgas/crt/ifgas_stream.h7
2 files changed, 67 insertions, 221 deletions
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<IFX_SeekableWriteStream>& pFileWrite,
- uint32_t dwAccess);
+ bool LoadFileWrite(const CFX_RetainPtr<IFX_SeekableWriteStream>& 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 <typename T, typename... Args>
- friend CFX_RetainPtr<T> 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<IFGAS_StreamImp> imp, uint32_t dwAccess);
- ~CFGAS_Stream() override;
-
- std::unique_ptr<IFGAS_StreamImp> m_pStreamImp;
- uint32_t m_dwAccess;
-};
-
class CFGAS_TextStream : public IFGAS_Stream {
public:
template <typename T, typename... Args>
friend CFX_RetainPtr<T> 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<IFGAS_Stream>& pStream);
+ CFGAS_TextStream(std::unique_ptr<IFGAS_StreamImp> imp, bool isWriteSteam);
~CFGAS_TextStream() override;
void InitStream();
uint16_t m_wCodePage;
int32_t m_wBOMLength;
- uint32_t m_dwBOM;
- CFX_RetainPtr<IFGAS_Stream> m_pStreamImp;
+ bool m_IsWriteStream;
+ std::unique_ptr<IFGAS_StreamImp> m_pStreamImp;
};
class CFGAS_WideStringReadStream : public IFGAS_Stream {
@@ -171,7 +135,6 @@ class CFGAS_WideStringReadStream : public IFGAS_Stream {
friend CFX_RetainPtr<T> 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<IFX_SeekableWriteStream>& pFileWrite,
- uint32_t dwAccess) {
+ const CFX_RetainPtr<IFX_SeekableWriteStream>& pFileWrite) {
ASSERT(!m_pFileWrite && pFileWrite);
m_iPosition = pFileWrite->GetSize();
@@ -327,65 +289,58 @@ void CFGAS_FileWriteStreamImp::Flush() {
}
}
-CFGAS_TextStream::CFGAS_TextStream(const CFX_RetainPtr<IFGAS_Stream>& pStream)
+CFGAS_TextStream::CFGAS_TextStream(std::unique_ptr<IFGAS_StreamImp> 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<uint8_t*>(&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<IFGAS_StreamImp> 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> IFGAS_Stream::CreateReadStream(
->LoadFileRead(pFileRead)) {
return nullptr;
}
- return pdfium::MakeRetain<CFGAS_TextStream>(
- pdfium::MakeRetain<CFGAS_Stream>(std::move(pImp), 0));
+ return pdfium::MakeRetain<CFGAS_TextStream>(std::move(pImp), false);
}
// static
@@ -727,12 +580,10 @@ CFX_RetainPtr<IFGAS_Stream> IFGAS_Stream::CreateWriteStream(
std::unique_ptr<IFGAS_StreamImp> pImp =
pdfium::MakeUnique<CFGAS_FileWriteStreamImp>();
if (!static_cast<CFGAS_FileWriteStreamImp*>(pImp.get())
- ->LoadFileWrite(pFileWrite, FX_STREAMACCESS_Write)) {
+ ->LoadFileWrite(pFileWrite)) {
return nullptr;
}
-
- return pdfium::MakeRetain<CFGAS_TextStream>(
- pdfium::MakeRetain<CFGAS_Stream>(std::move(pImp), FX_STREAMACCESS_Write));
+ return pdfium::MakeRetain<CFGAS_TextStream>(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<IFGAS_Stream> 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;
};