summaryrefslogtreecommitdiff
path: root/xfa/fgas/crt
diff options
context:
space:
mode:
Diffstat (limited to 'xfa/fgas/crt')
-rw-r--r--xfa/fgas/crt/cfgas_stream.cpp (renamed from xfa/fgas/crt/ifgas_stream.cpp)105
-rw-r--r--xfa/fgas/crt/cfgas_stream.h54
-rw-r--r--xfa/fgas/crt/ifgas_stream.h44
3 files changed, 80 insertions, 123 deletions
diff --git a/xfa/fgas/crt/ifgas_stream.cpp b/xfa/fgas/crt/cfgas_stream.cpp
index 7a90626fe8..809c19bdae 100644
--- a/xfa/fgas/crt/ifgas_stream.cpp
+++ b/xfa/fgas/crt/cfgas_stream.cpp
@@ -4,7 +4,7 @@
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-#include "xfa/fgas/crt/ifgas_stream.h"
+#include "xfa/fgas/crt/cfgas_stream.h"
#if _FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN32_MOBILE_ || \
_FX_OS_ == _FX_WIN64_
@@ -12,6 +12,7 @@
#endif
#include <algorithm>
+#include <limits>
#include <memory>
#include <utility>
#include <vector>
@@ -23,39 +24,6 @@
namespace {
-class CFGAS_Stream : public IFGAS_Stream {
- public:
- template <typename T, typename... Args>
- friend CFX_RetainPtr<T> pdfium::MakeRetain(Args&&... args);
-
- // 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;
-
- private:
- CFGAS_Stream(const CFX_RetainPtr<IFX_SeekableStream>& stream,
- bool isWriteSteam);
- ~CFGAS_Stream() override;
-
- FX_STRSIZE ReadData(uint8_t* pBuffer, FX_STRSIZE iBufferSize);
- void WriteData(const CFX_WideStringC& str);
-
- uint16_t m_wCodePage;
- FX_STRSIZE m_wBOMLength;
- bool m_IsWriteStream;
- FX_FILESIZE m_iPosition;
- CFX_RetainPtr<IFX_SeekableStream> m_pStream;
-};
-
// Returns {src bytes consumed, dst bytes produced}.
std::pair<FX_STRSIZE, FX_STRSIZE> UTF8Decode(const char* pSrc,
FX_STRSIZE srcLen,
@@ -161,6 +129,8 @@ void SwapByteOrder(wchar_t* pStr, FX_STRSIZE iLength) {
}
}
+} // namespace
+
#if _FX_ENDIAN_ == _FX_LITTLE_ENDIAN_
#define BOM_MASK 0x00FFFFFF
#define BOM_UTF8 0x00BFBBEF
@@ -190,7 +160,7 @@ CFGAS_Stream::CFGAS_Stream(const CFX_RetainPtr<IFX_SeekableStream>& stream,
}
FX_FILESIZE iPosition = GetPosition();
- Seek(FX_STREAMSEEK_Begin, 0);
+ Seek(CFGAS_Stream::Pos::Begin, 0);
uint32_t bom;
ReadData(reinterpret_cast<uint8_t*>(&bom), 3);
@@ -213,18 +183,21 @@ CFGAS_Stream::CFGAS_Stream(const CFX_RetainPtr<IFX_SeekableStream>& stream,
}
}
- Seek(FX_STREAMSEEK_Begin,
+ Seek(CFGAS_Stream::Pos::Begin,
std::max(static_cast<FX_FILESIZE>(m_wBOMLength), iPosition));
}
+CFGAS_Stream::CFGAS_Stream(uint8_t* data, FX_STRSIZE size)
+ : CFGAS_Stream(IFX_MemoryStream::Create(data, size), false) {}
+
CFGAS_Stream::~CFGAS_Stream() {}
-void CFGAS_Stream::Seek(FX_STREAMSEEK eSeek, FX_FILESIZE iOffset) {
+void CFGAS_Stream::Seek(CFGAS_Stream::Pos eSeek, FX_FILESIZE iOffset) {
switch (eSeek) {
- case FX_STREAMSEEK_Begin:
+ case CFGAS_Stream::Pos::Begin:
m_iPosition = iOffset;
break;
- case FX_STREAMSEEK_Current:
+ case CFGAS_Stream::Pos::Current:
m_iPosition += iOffset;
break;
}
@@ -300,7 +273,7 @@ FX_STRSIZE CFGAS_Stream::ReadString(wchar_t* pStr,
FX_STRSIZE iSrc = 0;
std::tie(iSrc, iMaxLength) = UTF8Decode(
reinterpret_cast<const char*>(buf.data()), iLen, pStr, iMaxLength);
- Seek(FX_STREAMSEEK_Current, iSrc - iLen);
+ Seek(CFGAS_Stream::Pos::Current, iSrc - iLen);
} else {
iMaxLength = 0;
}
@@ -310,48 +283,22 @@ FX_STRSIZE CFGAS_Stream::ReadString(wchar_t* pStr,
return iMaxLength;
}
-void CFGAS_Stream::WriteData(const CFX_WideStringC& str) {
- if (!m_IsWriteStream || str.GetLength() == 0)
- return;
- if (m_pStream->WriteBlock(str.c_str(), m_iPosition,
- str.GetLength() * sizeof(wchar_t))) {
- pdfium::base::CheckedNumeric<FX_STRSIZE> 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();
- }
-}
-
void CFGAS_Stream::WriteString(const CFX_WideStringC& str) {
- if (!m_IsWriteStream)
- return;
- if (str.GetLength() == 0)
+ if (!m_IsWriteStream || str.GetLength() == 0 ||
+ m_wCodePage != FX_CODEPAGE_UTF8) {
return;
- if (m_wCodePage != FX_CODEPAGE_UTF8)
+ }
+ if (!m_pStream->WriteBlock(str.c_str(), m_iPosition,
+ str.GetLength() * sizeof(wchar_t))) {
return;
+ }
- WriteData(str);
-}
-
-} // namespace
-
-// static
-CFX_RetainPtr<IFGAS_Stream> IFGAS_Stream::CreateReadStream(
- const CFX_RetainPtr<IFX_SeekableStream>& pFileRead) {
- if (!pFileRead)
- return nullptr;
-
- return pdfium::MakeRetain<CFGAS_Stream>(pFileRead, false);
-}
-
-// static
-CFX_RetainPtr<IFGAS_Stream> IFGAS_Stream::CreateWriteStream(
- const CFX_RetainPtr<IFX_SeekableStream>& pFileWrite) {
- if (!pFileWrite)
- return nullptr;
+ pdfium::base::CheckedNumeric<FX_STRSIZE> new_pos = m_iPosition;
+ new_pos += str.GetLength() * sizeof(wchar_t);
+ if (!new_pos.IsValid()) {
+ m_iPosition = std::numeric_limits<FX_STRSIZE>::max();
+ return;
+ }
- return pdfium::MakeRetain<CFGAS_Stream>(pFileWrite, true);
+ m_iPosition = new_pos.ValueOrDie();
}
diff --git a/xfa/fgas/crt/cfgas_stream.h b/xfa/fgas/crt/cfgas_stream.h
new file mode 100644
index 0000000000..5d2e5e6242
--- /dev/null
+++ b/xfa/fgas/crt/cfgas_stream.h
@@ -0,0 +1,54 @@
+// Copyright 2017 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#ifndef XFA_FGAS_CRT_CFGAS_STREAM_H_
+#define XFA_FGAS_CRT_CFGAS_STREAM_H_
+
+#include <algorithm>
+
+#include "core/fxcrt/cfx_retain_ptr.h"
+#include "core/fxcrt/fx_stream.h"
+#include "core/fxcrt/fx_system.h"
+
+class CFGAS_Stream : public CFX_Retainable {
+ public:
+ enum class Pos {
+ Begin = 0,
+ Current,
+ };
+
+ template <typename T, typename... Args>
+ friend CFX_RetainPtr<T> pdfium::MakeRetain(Args&&... args);
+
+ FX_FILESIZE GetLength() const { return m_pStream->GetSize(); }
+ FX_FILESIZE GetPosition() { return m_iPosition; }
+ FX_STRSIZE GetBOMLength() const { return std::max(0, m_wBOMLength); }
+ bool IsEOF() const { return m_iPosition >= GetLength(); }
+
+ void Seek(CFGAS_Stream::Pos eSeek, FX_FILESIZE iOffset);
+ FX_STRSIZE ReadString(wchar_t* pStr, FX_STRSIZE iMaxLength, bool* bEOS);
+
+ void WriteString(const CFX_WideStringC& str);
+
+ uint16_t GetCodePage() const { return m_wCodePage; }
+ void SetCodePage(uint16_t wCodePage);
+
+ private:
+ CFGAS_Stream(const CFX_RetainPtr<IFX_SeekableStream>& stream,
+ bool isWriteSteam);
+ CFGAS_Stream(uint8_t* data, FX_STRSIZE size);
+ ~CFGAS_Stream() override;
+
+ FX_STRSIZE ReadData(uint8_t* pBuffer, FX_STRSIZE iBufferSize);
+
+ uint16_t m_wCodePage;
+ FX_STRSIZE m_wBOMLength;
+ bool m_IsWriteStream;
+ FX_FILESIZE m_iPosition;
+ CFX_RetainPtr<IFX_SeekableStream> m_pStream;
+};
+
+#endif // XFA_FGAS_CRT_CFGAS_STREAM_H_
diff --git a/xfa/fgas/crt/ifgas_stream.h b/xfa/fgas/crt/ifgas_stream.h
deleted file mode 100644
index acc9f1c4eb..0000000000
--- a/xfa/fgas/crt/ifgas_stream.h
+++ /dev/null
@@ -1,44 +0,0 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#ifndef XFA_FGAS_CRT_IFGAS_STREAM_H_
-#define XFA_FGAS_CRT_IFGAS_STREAM_H_
-
-#include "core/fxcrt/cfx_retain_ptr.h"
-#include "core/fxcrt/fx_stream.h"
-#include "core/fxcrt/fx_system.h"
-
-enum FX_STREAMSEEK {
- FX_STREAMSEEK_Begin = 0,
- FX_STREAMSEEK_Current,
-};
-
-class IFGAS_Stream : public CFX_Retainable {
- public:
- static CFX_RetainPtr<IFGAS_Stream> CreateReadStream(
- const CFX_RetainPtr<IFX_SeekableStream>& pFileRead);
- static CFX_RetainPtr<IFGAS_Stream> CreateWriteStream(
- const CFX_RetainPtr<IFX_SeekableStream>& pFileWrite);
-
- 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 WriteString(const CFX_WideStringC& str) = 0;
-
- virtual uint16_t GetCodePage() const = 0;
- virtual void SetCodePage(uint16_t wCodePage) = 0;
-
- protected:
- virtual bool IsEOF() const = 0;
-};
-
-#endif // XFA_FGAS_CRT_IFGAS_STREAM_H_