From bf510b7c520bccbd2edf5bb3e2f91b125ebfd6d7 Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Tue, 18 Apr 2017 16:35:55 -0400 Subject: Rename IFGAS_Stream to CFGAS_Stream MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This CL replaces IFGAS_Stream with the only implementation CFGAS_Stream. The CreateReadStream and CreateWriteStream methods are removed in favour of calling MakeRetain directly. Change-Id: I882a89258f642e24fc3d631587db05652bd53ded Reviewed-on: https://pdfium-review.googlesource.com/4210 Commit-Queue: dsinclair Reviewed-by: Nicolás Peña Reviewed-by: Tom Sepez --- BUILD.gn | 4 +- testing/libfuzzer/pdf_cfx_saxreader_fuzzer.cc | 2 +- testing/libfuzzer/pdf_css_fuzzer.cc | 2 +- testing/libfuzzer/pdf_xml_fuzzer.cc | 9 +- xfa/fde/cfde_txtedtengine.cpp | 4 +- xfa/fde/cfde_txtedtengine.h | 2 +- xfa/fde/css/cfde_csstextbuf.cpp | 18 -- xfa/fde/css/cfde_csstextbuf.h | 6 +- xfa/fde/css/fde_css.h | 2 +- xfa/fde/xml/cfde_xmldoc.cpp | 2 +- xfa/fde/xml/cfde_xmldoc.h | 6 +- xfa/fde/xml/cfde_xmlnode.cpp | 2 +- xfa/fde/xml/cfde_xmlnode.h | 4 +- xfa/fde/xml/cfde_xmlparser.cpp | 2 +- xfa/fde/xml/cfde_xmlparser.h | 6 +- xfa/fde/xml/cfde_xmlsyntaxparser.cpp | 8 +- xfa/fde/xml/cfde_xmlsyntaxparser.h | 6 +- xfa/fde/xml/cfde_xmlsyntaxparser_unittest.cpp | 77 +++--- xfa/fgas/crt/cfgas_stream.cpp | 304 ++++++++++++++++++++++ xfa/fgas/crt/cfgas_stream.h | 54 ++++ xfa/fgas/crt/ifgas_stream.cpp | 357 -------------------------- xfa/fgas/crt/ifgas_stream.h | 44 ---- xfa/fgas/font/cfgas_fontmgr.h | 2 +- xfa/fgas/font/cfgas_gefont.h | 4 +- xfa/fxfa/parser/cxfa_dataexporter.cpp | 18 +- xfa/fxfa/parser/cxfa_dataexporter.h | 4 +- xfa/fxfa/parser/cxfa_node.cpp | 9 +- xfa/fxfa/parser/cxfa_simple_parser.cpp | 12 +- xfa/fxfa/parser/cxfa_simple_parser.h | 4 +- xfa/fxfa/parser/xfa_utils.h | 4 +- 30 files changed, 440 insertions(+), 538 deletions(-) create mode 100644 xfa/fgas/crt/cfgas_stream.cpp create mode 100644 xfa/fgas/crt/cfgas_stream.h delete mode 100644 xfa/fgas/crt/ifgas_stream.cpp delete mode 100644 xfa/fgas/crt/ifgas_stream.h diff --git a/BUILD.gn b/BUILD.gn index ff9a4a4d44..4b42ac7d76 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -1427,10 +1427,10 @@ if (pdf_enable_xfa) { "xfa/fde/xml/cfde_xmltext.h", "xfa/fgas/crt/cfgas_formatstring.cpp", "xfa/fgas/crt/cfgas_formatstring.h", + "xfa/fgas/crt/cfgas_stream.cpp", + "xfa/fgas/crt/cfgas_stream.h", "xfa/fgas/crt/fgas_codepage.h", "xfa/fgas/crt/fgas_language.h", - "xfa/fgas/crt/ifgas_stream.cpp", - "xfa/fgas/crt/ifgas_stream.h", "xfa/fgas/font/cfgas_fontmgr.cpp", "xfa/fgas/font/cfgas_fontmgr.h", "xfa/fgas/font/cfgas_gefont.cpp", diff --git a/testing/libfuzzer/pdf_cfx_saxreader_fuzzer.cc b/testing/libfuzzer/pdf_cfx_saxreader_fuzzer.cc index 31e24e82f2..c27273f29b 100644 --- a/testing/libfuzzer/pdf_cfx_saxreader_fuzzer.cc +++ b/testing/libfuzzer/pdf_cfx_saxreader_fuzzer.cc @@ -6,7 +6,7 @@ #include "core/fxcrt/cfx_retain_ptr.h" #include "core/fxcrt/xml/cfx_saxreader.h" -#include "xfa/fgas/crt/ifgas_stream.h" +#include "xfa/fgas/crt/cfgas_stream.h" extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { CFX_SAXReader reader; diff --git a/testing/libfuzzer/pdf_css_fuzzer.cc b/testing/libfuzzer/pdf_css_fuzzer.cc index 9f5cdfec45..fef3de74b7 100644 --- a/testing/libfuzzer/pdf_css_fuzzer.cc +++ b/testing/libfuzzer/pdf_css_fuzzer.cc @@ -8,7 +8,7 @@ #include "core/fxcrt/fx_string.h" #include "xfa/fde/css/cfde_csssyntaxparser.h" #include "xfa/fde/css/fde_css.h" -#include "xfa/fgas/crt/ifgas_stream.h" +#include "xfa/fgas/crt/cfgas_stream.h" extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { CFX_WideString input = CFX_WideString::FromUTF8( diff --git a/testing/libfuzzer/pdf_xml_fuzzer.cc b/testing/libfuzzer/pdf_xml_fuzzer.cc index 1f368699f4..3806f59781 100644 --- a/testing/libfuzzer/pdf_xml_fuzzer.cc +++ b/testing/libfuzzer/pdf_xml_fuzzer.cc @@ -13,7 +13,7 @@ #include "xfa/fde/xml/cfde_xmldoc.h" #include "xfa/fde/xml/cfde_xmlnode.h" #include "xfa/fde/xml/cfde_xmlparser.h" -#include "xfa/fgas/crt/ifgas_stream.h" +#include "xfa/fgas/crt/cfgas_stream.h" namespace { @@ -51,11 +51,8 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { if (!safe_size.IsValid()) return 0; - CFX_RetainPtr stream = IFGAS_Stream::CreateReadStream( - IFX_MemoryStream::Create(const_cast(data), size)); - if (!stream) - return 0; - + CFX_RetainPtr stream = + pdfium::MakeRetain(const_cast(data), size); auto doc = pdfium::MakeUnique(); if (!doc->LoadXML(pdfium::MakeUnique(doc->GetRoot(), stream))) return 0; diff --git a/xfa/fde/cfde_txtedtengine.cpp b/xfa/fde/cfde_txtedtengine.cpp index 84aafed433..136735c16e 100644 --- a/xfa/fde/cfde_txtedtengine.cpp +++ b/xfa/fde/cfde_txtedtengine.cpp @@ -106,7 +106,7 @@ CFDE_TxtEdtPage* CFDE_TxtEdtEngine::GetPage(int32_t nIndex) { } void CFDE_TxtEdtEngine::SetTextByStream( - const CFX_RetainPtr& pStream) { + const CFX_RetainPtr& pStream) { ResetEngine(); int32_t nIndex = 0; if (pStream && pStream->GetLength()) { @@ -118,7 +118,7 @@ void CFDE_TxtEdtEngine::SetTextByStream( bool bPreIsCR = false; if (bValid) { int32_t nPos = pStream->GetBOMLength(); - pStream->Seek(FX_STREAMSEEK_Begin, nPos); + pStream->Seek(CFGAS_Stream::Pos::Begin, nPos); int32_t nPlateSize = std::min(nStreamLength, m_pTxtBuf->GetChunkSize()); wchar_t* lpwstr = FX_Alloc(wchar_t, nPlateSize); bool bEos = false; diff --git a/xfa/fde/cfde_txtedtengine.h b/xfa/fde/cfde_txtedtengine.h index 020b8c93df..bbdef8fd7a 100644 --- a/xfa/fde/cfde_txtedtengine.h +++ b/xfa/fde/cfde_txtedtengine.h @@ -31,7 +31,7 @@ class CFDE_TxtEdtEngine { int32_t CountPages() const; CFDE_TxtEdtPage* GetPage(int32_t nIndex); - void SetTextByStream(const CFX_RetainPtr& pStream); + void SetTextByStream(const CFX_RetainPtr& pStream); void SetText(const CFX_WideString& wsText); int32_t GetTextLength() const; CFX_WideString GetText(int32_t nStart, int32_t nCount) const; diff --git a/xfa/fde/css/cfde_csstextbuf.cpp b/xfa/fde/css/cfde_csstextbuf.cpp index 149ed31e98..2d8f93f2b3 100644 --- a/xfa/fde/css/cfde_csstextbuf.cpp +++ b/xfa/fde/css/cfde_csstextbuf.cpp @@ -41,24 +41,6 @@ bool CFDE_CSSTextBuf::EstimateSize(int32_t iAllocSize) { return ExpandBuf(iAllocSize); } -int32_t CFDE_CSSTextBuf::LoadFromStream( - const CFX_RetainPtr& pTxtStream, - int32_t iStreamOffset, - int32_t iMaxChars, - bool& bEOS) { - ASSERT(iStreamOffset >= 0 && iMaxChars > 0); - Clear(); - m_bExtBuf = false; - if (!ExpandBuf(iMaxChars)) - return 0; - - if (pTxtStream->GetPosition() != iStreamOffset) - pTxtStream->Seek(FX_STREAMSEEK_Begin, iStreamOffset); - - m_iDatLen = pTxtStream->ReadString(m_pBuffer, iMaxChars, &bEOS); - return m_iDatLen; -} - bool CFDE_CSSTextBuf::ExpandBuf(int32_t iDesiredSize) { if (m_bExtBuf) return false; diff --git a/xfa/fde/css/cfde_csstextbuf.h b/xfa/fde/css/cfde_csstextbuf.h index 52c76210ce..43ff03aa07 100644 --- a/xfa/fde/css/cfde_csstextbuf.h +++ b/xfa/fde/css/cfde_csstextbuf.h @@ -10,7 +10,7 @@ #include "core/fxcrt/cfx_retain_ptr.h" #include "core/fxcrt/fx_memory.h" #include "core/fxcrt/fx_system.h" -#include "xfa/fgas/crt/ifgas_stream.h" +#include "xfa/fgas/crt/cfgas_stream.h" class CFDE_CSSTextBuf { public: @@ -19,10 +19,6 @@ class CFDE_CSSTextBuf { bool AttachBuffer(const wchar_t* pBuffer, int32_t iBufLen); bool EstimateSize(int32_t iAllocSize); - int32_t LoadFromStream(const CFX_RetainPtr& pTxtStream, - int32_t iStreamOffset, - int32_t iMaxChars, - bool& bEOS); bool AppendChar(wchar_t wch) { if (m_iDatLen >= m_iBufLen && !ExpandBuf(m_iBufLen * 2)) return false; diff --git a/xfa/fde/css/fde_css.h b/xfa/fde/css/fde_css.h index ae4f36c93d..9e56fce59a 100644 --- a/xfa/fde/css/fde_css.h +++ b/xfa/fde/css/fde_css.h @@ -8,7 +8,7 @@ #define XFA_FDE_CSS_FDE_CSS_H_ #include "core/fxge/fx_dib.h" -#include "xfa/fgas/crt/ifgas_stream.h" +#include "xfa/fgas/crt/cfgas_stream.h" #include "xfa/fgas/font/cfgas_fontmgr.h" enum FDE_CSSVALUETYPE { diff --git a/xfa/fde/xml/cfde_xmldoc.cpp b/xfa/fde/xml/cfde_xmldoc.cpp index 209ce9c68f..256164c5f8 100644 --- a/xfa/fde/xml/cfde_xmldoc.cpp +++ b/xfa/fde/xml/cfde_xmldoc.cpp @@ -47,7 +47,7 @@ void CFDE_XMLDoc::CloseXML() { m_pXMLParser.reset(); } -void CFDE_XMLDoc::SaveXMLNode(const CFX_RetainPtr& pXMLStream, +void CFDE_XMLDoc::SaveXMLNode(const CFX_RetainPtr& pXMLStream, CFDE_XMLNode* pINode) { CFDE_XMLNode* pNode = (CFDE_XMLNode*)pINode; switch (pNode->GetType()) { diff --git a/xfa/fde/xml/cfde_xmldoc.h b/xfa/fde/xml/cfde_xmldoc.h index 6143750428..b84c9c00a0 100644 --- a/xfa/fde/xml/cfde_xmldoc.h +++ b/xfa/fde/xml/cfde_xmldoc.h @@ -12,7 +12,7 @@ #include "core/fxcrt/cfx_retain_ptr.h" #include "xfa/fde/xml/cfde_xmlnode.h" #include "xfa/fde/xml/cfde_xmlparser.h" -#include "xfa/fgas/crt/ifgas_stream.h" +#include "xfa/fgas/crt/cfgas_stream.h" class CFDE_XMLDoc { public: @@ -24,14 +24,14 @@ class CFDE_XMLDoc { void CloseXML(); CFDE_XMLNode* GetRoot() const { return m_pRoot.get(); } - void SaveXMLNode(const CFX_RetainPtr& pXMLStream, + void SaveXMLNode(const CFX_RetainPtr& pXMLStream, CFDE_XMLNode* pNode); private: int32_t m_iStatus; std::unique_ptr m_pRoot; std::unique_ptr m_pXMLParser; - CFX_RetainPtr m_pStream; + CFX_RetainPtr m_pStream; }; #endif // XFA_FDE_XML_CFDE_XMLDOC_H_ diff --git a/xfa/fde/xml/cfde_xmlnode.cpp b/xfa/fde/xml/cfde_xmlnode.cpp index e4d7880cac..149d18c3d6 100644 --- a/xfa/fde/xml/cfde_xmlnode.cpp +++ b/xfa/fde/xml/cfde_xmlnode.cpp @@ -329,7 +329,7 @@ std::unique_ptr CFDE_XMLNode::Clone() { return nullptr; } -void CFDE_XMLNode::SaveXMLNode(const CFX_RetainPtr& pXMLStream) { +void CFDE_XMLNode::SaveXMLNode(const CFX_RetainPtr& pXMLStream) { CFDE_XMLNode* pNode = (CFDE_XMLNode*)this; switch (pNode->GetType()) { case FDE_XMLNODE_Instruction: { diff --git a/xfa/fde/xml/cfde_xmlnode.h b/xfa/fde/xml/cfde_xmlnode.h index 3cfcd5c980..a04bd8c812 100644 --- a/xfa/fde/xml/cfde_xmlnode.h +++ b/xfa/fde/xml/cfde_xmlnode.h @@ -10,7 +10,7 @@ #include #include "core/fxcrt/cfx_retain_ptr.h" -#include "xfa/fgas/crt/ifgas_stream.h" +#include "xfa/fgas/crt/cfgas_stream.h" enum FDE_XMLNODETYPE { FDE_XMLNODE_Unknown = 0, @@ -64,7 +64,7 @@ class CFDE_XMLNode { bool InsertNodeItem(CFDE_XMLNode::NodeItem eItem, CFDE_XMLNode* pNode); CFDE_XMLNode* RemoveNodeItem(CFDE_XMLNode::NodeItem eItem); - void SaveXMLNode(const CFX_RetainPtr& pXMLStream); + void SaveXMLNode(const CFX_RetainPtr& pXMLStream); CFDE_XMLNode* m_pParent; CFDE_XMLNode* m_pChild; diff --git a/xfa/fde/xml/cfde_xmlparser.cpp b/xfa/fde/xml/cfde_xmlparser.cpp index 5b8993de1b..c29dfc38ab 100644 --- a/xfa/fde/xml/cfde_xmlparser.cpp +++ b/xfa/fde/xml/cfde_xmlparser.cpp @@ -15,7 +15,7 @@ #include "xfa/fde/xml/cfde_xmltext.h" CFDE_XMLParser::CFDE_XMLParser(CFDE_XMLNode* pParent, - const CFX_RetainPtr& pStream) + const CFX_RetainPtr& pStream) : m_nElementStart(0), m_dwCheckStatus(0), m_dwCurrentCheckStatus(0), diff --git a/xfa/fde/xml/cfde_xmlparser.h b/xfa/fde/xml/cfde_xmlparser.h index 42f590ce21..f1a7888fa7 100644 --- a/xfa/fde/xml/cfde_xmlparser.h +++ b/xfa/fde/xml/cfde_xmlparser.h @@ -16,13 +16,13 @@ class CFDE_XMLElement; class CFDE_XMLNode; -class IFGAS_Stream; +class CFGAS_Stream; class IFX_Pause; class CFDE_XMLParser { public: CFDE_XMLParser(CFDE_XMLNode* pParent, - const CFX_RetainPtr& pStream); + const CFX_RetainPtr& pStream); ~CFDE_XMLParser(); int32_t DoParser(IFX_Pause* pPause); @@ -34,7 +34,7 @@ class CFDE_XMLParser { uint16_t m_dwCurrentCheckStatus; private: - CFX_RetainPtr m_pStream; + CFX_RetainPtr m_pStream; std::unique_ptr m_pParser; CFDE_XMLNode* m_pParent; CFDE_XMLNode* m_pChild; diff --git a/xfa/fde/xml/cfde_xmlsyntaxparser.cpp b/xfa/fde/xml/cfde_xmlsyntaxparser.cpp index 78382058ec..10eefdc012 100644 --- a/xfa/fde/xml/cfde_xmlsyntaxparser.cpp +++ b/xfa/fde/xml/cfde_xmlsyntaxparser.cpp @@ -80,7 +80,7 @@ int32_t GetUTF8EncodeLength(const std::vector& src, } // namespace CFDE_XMLSyntaxParser::CFDE_XMLSyntaxParser( - const CFX_RetainPtr& pStream) + const CFX_RetainPtr& pStream) : m_pStream(pStream), m_iXMLPlaneSize(32 * 1024), m_iCurrentPos(0), @@ -144,9 +144,9 @@ FDE_XmlSyntaxResult CFDE_XMLSyntaxParser::DoSyntaxParse() { } m_ParsedChars += m_End; m_iParsedBytes = m_iCurrentPos; - if (m_pStream->GetPosition() != m_iCurrentPos) { - m_pStream->Seek(FX_STREAMSEEK_Begin, m_iCurrentPos); - } + if (m_pStream->GetPosition() != m_iCurrentPos) + m_pStream->Seek(CFGAS_Stream::Pos::Begin, m_iCurrentPos); + m_iBufferChars = m_pStream->ReadString(m_Buffer.data(), m_iXMLPlaneSize, &m_bEOS); iPos = m_pStream->GetPosition(); diff --git a/xfa/fde/xml/cfde_xmlsyntaxparser.h b/xfa/fde/xml/cfde_xmlsyntaxparser.h index f229cc02b1..aa4e91827e 100644 --- a/xfa/fde/xml/cfde_xmlsyntaxparser.h +++ b/xfa/fde/xml/cfde_xmlsyntaxparser.h @@ -14,7 +14,7 @@ #include "core/fxcrt/cfx_retain_ptr.h" #include "core/fxcrt/fx_string.h" #include "xfa/fde/xml/cfde_xmlnode.h" -#include "xfa/fgas/crt/ifgas_stream.h" +#include "xfa/fgas/crt/cfgas_stream.h" enum class FDE_XmlSyntaxResult { None, @@ -36,7 +36,7 @@ enum class FDE_XmlSyntaxResult { class CFDE_XMLSyntaxParser { public: - explicit CFDE_XMLSyntaxParser(const CFX_RetainPtr& pStream); + explicit CFDE_XMLSyntaxParser(const CFX_RetainPtr& pStream); ~CFDE_XMLSyntaxParser(); FDE_XmlSyntaxResult DoSyntaxParse(); @@ -97,7 +97,7 @@ class CFDE_XMLSyntaxParser { void ParseTextChar(wchar_t ch); - CFX_RetainPtr m_pStream; + CFX_RetainPtr m_pStream; FX_STRSIZE m_iXMLPlaneSize; int32_t m_iCurrentPos; int32_t m_iCurrentNodeNum; diff --git a/xfa/fde/xml/cfde_xmlsyntaxparser_unittest.cpp b/xfa/fde/xml/cfde_xmlsyntaxparser_unittest.cpp index a45bb1276a..aecb3ee39e 100644 --- a/xfa/fde/xml/cfde_xmlsyntaxparser_unittest.cpp +++ b/xfa/fde/xml/cfde_xmlsyntaxparser_unittest.cpp @@ -8,8 +8,8 @@ #include "testing/gtest/include/gtest/gtest.h" #include "testing/test_support.h" +#include "xfa/fgas/crt/cfgas_stream.h" #include "xfa/fgas/crt/fgas_codepage.h" -#include "xfa/fgas/crt/ifgas_stream.h" class CFDE_XMLSyntaxParserTest : public pdfium::FPDF_Test {}; @@ -28,9 +28,8 @@ TEST_F(CFDE_XMLSyntaxParserTest, CData) { L" app.alert(\"Tclams\");\n" L" "; - CFX_RetainPtr stream = - IFGAS_Stream::CreateReadStream(IFX_MemoryStream::Create( - reinterpret_cast(const_cast(input)), strlen(input))); + CFX_RetainPtr stream = pdfium::MakeRetain( + reinterpret_cast(const_cast(input)), strlen(input)); stream->SetCodePage(FX_CODEPAGE_UTF8); CFDE_XMLSyntaxParser parser(stream); @@ -76,9 +75,8 @@ TEST_F(CFDE_XMLSyntaxParserTest, CDataWithInnerScript) { L" \n" L" "; - CFX_RetainPtr stream = - IFGAS_Stream::CreateReadStream(IFX_MemoryStream::Create( - reinterpret_cast(const_cast(input)), strlen(input))); + CFX_RetainPtr stream = pdfium::MakeRetain( + reinterpret_cast(const_cast(input)), strlen(input)); stream->SetCodePage(FX_CODEPAGE_UTF8); CFDE_XMLSyntaxParser parser(stream); @@ -113,9 +111,8 @@ TEST_F(CFDE_XMLSyntaxParserTest, ArrowBangArrow) { " \n" ""; - CFX_RetainPtr stream = - IFGAS_Stream::CreateReadStream(IFX_MemoryStream::Create( - reinterpret_cast(const_cast(input)), strlen(input))); + CFX_RetainPtr stream = pdfium::MakeRetain( + reinterpret_cast(const_cast(input)), strlen(input)); stream->SetCodePage(FX_CODEPAGE_UTF8); CFDE_XMLSyntaxParser parser(stream); @@ -148,9 +145,8 @@ TEST_F(CFDE_XMLSyntaxParserTest, ArrowBangBracketArrow) { " \n" ""; - CFX_RetainPtr stream = - IFGAS_Stream::CreateReadStream(IFX_MemoryStream::Create( - reinterpret_cast(const_cast(input)), strlen(input))); + CFX_RetainPtr stream = pdfium::MakeRetain( + reinterpret_cast(const_cast(input)), strlen(input)); stream->SetCodePage(FX_CODEPAGE_UTF8); CFDE_XMLSyntaxParser parser(stream); @@ -178,9 +174,8 @@ TEST_F(CFDE_XMLSyntaxParserTest, IncompleteCData) { " \n" ""; - CFX_RetainPtr stream = - IFGAS_Stream::CreateReadStream(IFX_MemoryStream::Create( - reinterpret_cast(const_cast(input)), strlen(input))); + CFX_RetainPtr stream = pdfium::MakeRetain( + reinterpret_cast(const_cast(input)), strlen(input)); stream->SetCodePage(FX_CODEPAGE_UTF8); CFDE_XMLSyntaxParser parser(stream); @@ -208,9 +203,8 @@ TEST_F(CFDE_XMLSyntaxParserTest, UnClosedCData) { " "; - CFX_RetainPtr stream = - IFGAS_Stream::CreateReadStream(IFX_MemoryStream::Create( - reinterpret_cast(const_cast(input)), strlen(input))); + CFX_RetainPtr stream = pdfium::MakeRetain( + reinterpret_cast(const_cast(input)), strlen(input)); stream->SetCodePage(FX_CODEPAGE_UTF8); CFDE_XMLSyntaxParser parser(stream); @@ -238,9 +232,8 @@ TEST_F(CFDE_XMLSyntaxParserTest, EmptyCData) { " \n" ""; - CFX_RetainPtr stream = - IFGAS_Stream::CreateReadStream(IFX_MemoryStream::Create( - reinterpret_cast(const_cast(input)), strlen(input))); + CFX_RetainPtr stream = pdfium::MakeRetain( + reinterpret_cast(const_cast(input)), strlen(input)); stream->SetCodePage(FX_CODEPAGE_UTF8); CFDE_XMLSyntaxParser parser(stream); @@ -275,9 +268,8 @@ TEST_F(CFDE_XMLSyntaxParserTest, Comment) { " \n" ""; - CFX_RetainPtr stream = - IFGAS_Stream::CreateReadStream(IFX_MemoryStream::Create( - reinterpret_cast(const_cast(input)), strlen(input))); + CFX_RetainPtr stream = pdfium::MakeRetain( + reinterpret_cast(const_cast(input)), strlen(input)); stream->SetCodePage(FX_CODEPAGE_UTF8); CFDE_XMLSyntaxParser parser(stream); @@ -309,9 +301,8 @@ TEST_F(CFDE_XMLSyntaxParserTest, IncorrectCommentStart) { " \n" ""; - CFX_RetainPtr stream = - IFGAS_Stream::CreateReadStream(IFX_MemoryStream::Create( - reinterpret_cast(const_cast(input)), strlen(input))); + CFX_RetainPtr stream = pdfium::MakeRetain( + reinterpret_cast(const_cast(input)), strlen(input)); stream->SetCodePage(FX_CODEPAGE_UTF8); CFDE_XMLSyntaxParser parser(stream); @@ -343,9 +334,8 @@ TEST_F(CFDE_XMLSyntaxParserTest, CommentEmpty) { " \n" ""; - CFX_RetainPtr stream = - IFGAS_Stream::CreateReadStream(IFX_MemoryStream::Create( - reinterpret_cast(const_cast(input)), strlen(input))); + CFX_RetainPtr stream = pdfium::MakeRetain( + reinterpret_cast(const_cast(input)), strlen(input)); stream->SetCodePage(FX_CODEPAGE_UTF8); CFDE_XMLSyntaxParser parser(stream); @@ -377,9 +367,8 @@ TEST_F(CFDE_XMLSyntaxParserTest, CommentThreeDash) { " \n" ""; - CFX_RetainPtr stream = - IFGAS_Stream::CreateReadStream(IFX_MemoryStream::Create( - reinterpret_cast(const_cast(input)), strlen(input))); + CFX_RetainPtr stream = pdfium::MakeRetain( + reinterpret_cast(const_cast(input)), strlen(input)); stream->SetCodePage(FX_CODEPAGE_UTF8); CFDE_XMLSyntaxParser parser(stream); @@ -405,9 +394,8 @@ TEST_F(CFDE_XMLSyntaxParserTest, CommentTwoDash) { " \n" ""; - CFX_RetainPtr stream = - IFGAS_Stream::CreateReadStream(IFX_MemoryStream::Create( - reinterpret_cast(const_cast(input)), strlen(input))); + CFX_RetainPtr stream = pdfium::MakeRetain( + reinterpret_cast(const_cast(input)), strlen(input)); stream->SetCodePage(FX_CODEPAGE_UTF8); CFDE_XMLSyntaxParser parser(stream); @@ -437,9 +425,8 @@ TEST_F(CFDE_XMLSyntaxParserTest, Entities) { "�" ""; - CFX_RetainPtr stream = - IFGAS_Stream::CreateReadStream(IFX_MemoryStream::Create( - reinterpret_cast(const_cast(input)), strlen(input))); + CFX_RetainPtr stream = pdfium::MakeRetain( + reinterpret_cast(const_cast(input)), strlen(input)); stream->SetCodePage(FX_CODEPAGE_UTF8); CFDE_XMLSyntaxParser parser(stream); @@ -469,9 +456,8 @@ TEST_F(CFDE_XMLSyntaxParserTest, EntityOverflowHex) { "�" ""; - CFX_RetainPtr stream = - IFGAS_Stream::CreateReadStream(IFX_MemoryStream::Create( - reinterpret_cast(const_cast(input)), strlen(input))); + CFX_RetainPtr stream = pdfium::MakeRetain( + reinterpret_cast(const_cast(input)), strlen(input)); stream->SetCodePage(FX_CODEPAGE_UTF8); CFDE_XMLSyntaxParser parser(stream); @@ -501,9 +487,8 @@ TEST_F(CFDE_XMLSyntaxParserTest, EntityOverflowDecimal) { "�" ""; - CFX_RetainPtr stream = - IFGAS_Stream::CreateReadStream(IFX_MemoryStream::Create( - reinterpret_cast(const_cast(input)), strlen(input))); + CFX_RetainPtr stream = pdfium::MakeRetain( + reinterpret_cast(const_cast(input)), strlen(input)); stream->SetCodePage(FX_CODEPAGE_UTF8); CFDE_XMLSyntaxParser parser(stream); diff --git a/xfa/fgas/crt/cfgas_stream.cpp b/xfa/fgas/crt/cfgas_stream.cpp new file mode 100644 index 0000000000..809c19bdae --- /dev/null +++ b/xfa/fgas/crt/cfgas_stream.cpp @@ -0,0 +1,304 @@ +// 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 + +#include "xfa/fgas/crt/cfgas_stream.h" + +#if _FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN32_MOBILE_ || \ + _FX_OS_ == _FX_WIN64_ +#include +#endif + +#include +#include +#include +#include +#include + +#include "core/fxcrt/fx_ext.h" +#include "third_party/base/ptr_util.h" +#include "third_party/base/stl_util.h" +#include "xfa/fgas/crt/fgas_codepage.h" + +namespace { + +// 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}; +} + +void UTF16ToWChar(void* pBuffer, FX_STRSIZE 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]); +} + +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; + } + + while (iLength-- > 0) { + wch = static_cast(*pStr); + wch = (wch >> 8) | (wch << 8); + *pStr = wch; + ++pStr; + } +} + +} // namespace + +#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_ + +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); + + if (isWriteStream) { + m_iPosition = m_pStream->GetSize(); + return; + } + + FX_FILESIZE iPosition = GetPosition(); + Seek(CFGAS_Stream::Pos::Begin, 0); + + uint32_t bom; + ReadData(reinterpret_cast(&bom), 3); + + bom &= BOM_MASK; + if (bom == BOM_UTF8) { + m_wBOMLength = 3; + m_wCodePage = FX_CODEPAGE_UTF8; + } else { + bom &= BOM_UTF16_MASK; + if (bom == BOM_UTF16_BE) { + m_wBOMLength = 2; + m_wCodePage = FX_CODEPAGE_UTF16BE; + } else if (bom == BOM_UTF16_LE) { + m_wBOMLength = 2; + m_wCodePage = FX_CODEPAGE_UTF16LE; + } else { + m_wBOMLength = 0; + m_wCodePage = FXSYS_GetACP(); + } + } + + Seek(CFGAS_Stream::Pos::Begin, + std::max(static_cast(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(CFGAS_Stream::Pos eSeek, FX_FILESIZE iOffset) { + switch (eSeek) { + case CFGAS_Stream::Pos::Begin: + m_iPosition = iOffset; + break; + case CFGAS_Stream::Pos::Current: + m_iPosition += iOffset; + break; + } + m_iPosition = + pdfium::clamp(m_iPosition, static_cast(0), GetLength()); +} + +void CFGAS_Stream::SetCodePage(uint16_t wCodePage) { + if (m_wBOMLength > 0) + return; + m_wCodePage = wCodePage; +} + +FX_STRSIZE CFGAS_Stream::ReadData(uint8_t* pBuffer, FX_STRSIZE iBufferSize) { + ASSERT(pBuffer && iBufferSize > 0); + + if (m_IsWriteStream) + return -1; + + iBufferSize = std::min( + iBufferSize, static_cast(m_pStream->GetSize() - m_iPosition)); + if (iBufferSize <= 0) + return 0; + + 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_STRSIZE CFGAS_Stream::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) { + FX_FILESIZE iBytes = iMaxLength * 2; + FX_STRSIZE iLen = ReadData(reinterpret_cast(pStr), iBytes); + iMaxLength = iLen / 2; + if (sizeof(wchar_t) > 2) + UTF16ToWChar(pStr, iMaxLength); + +#if _FX_ENDIAN_ == _FX_BIG_ENDIAN_ + if (m_wCodePage == FX_CODEPAGE_UTF16LE) + SwapByteOrder(pStr, iMaxLength); +#else + if (m_wCodePage == FX_CODEPAGE_UTF16BE) + SwapByteOrder(pStr, iMaxLength); +#endif + + } else { + FX_FILESIZE pos = GetPosition(); + FX_STRSIZE iBytes = + std::min(iMaxLength, static_cast(GetLength() - pos)); + + if (iBytes > 0) { + std::vector buf(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); + Seek(CFGAS_Stream::Pos::Current, iSrc - iLen); + } else { + iMaxLength = 0; + } + } + + *bEOS = IsEOF(); + return iMaxLength; +} + +void CFGAS_Stream::WriteString(const CFX_WideStringC& str) { + if (!m_IsWriteStream || str.GetLength() == 0 || + m_wCodePage != FX_CODEPAGE_UTF8) { + 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); + if (!new_pos.IsValid()) { + m_iPosition = std::numeric_limits::max(); + return; + } + + 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 + +#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 + friend CFX_RetainPtr 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& 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 m_pStream; +}; + +#endif // XFA_FGAS_CRT_CFGAS_STREAM_H_ diff --git a/xfa/fgas/crt/ifgas_stream.cpp b/xfa/fgas/crt/ifgas_stream.cpp deleted file mode 100644 index 7a90626fe8..0000000000 --- a/xfa/fgas/crt/ifgas_stream.cpp +++ /dev/null @@ -1,357 +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 - -#include "xfa/fgas/crt/ifgas_stream.h" - -#if _FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN32_MOBILE_ || \ - _FX_OS_ == _FX_WIN64_ -#include -#endif - -#include -#include -#include -#include - -#include "core/fxcrt/fx_ext.h" -#include "third_party/base/ptr_util.h" -#include "third_party/base/stl_util.h" -#include "xfa/fgas/crt/fgas_codepage.h" - -namespace { - -class CFGAS_Stream : public IFGAS_Stream { - public: - template - friend CFX_RetainPtr 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& 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 m_pStream; -}; - -// 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}; -} - -void UTF16ToWChar(void* pBuffer, FX_STRSIZE 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]); -} - -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; - } - - while (iLength-- > 0) { - wch = static_cast(*pStr); - wch = (wch >> 8) | (wch << 8); - *pStr = wch; - ++pStr; - } -} - -#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_ - -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); - - if (isWriteStream) { - m_iPosition = m_pStream->GetSize(); - return; - } - - FX_FILESIZE iPosition = GetPosition(); - Seek(FX_STREAMSEEK_Begin, 0); - - uint32_t bom; - ReadData(reinterpret_cast(&bom), 3); - - bom &= BOM_MASK; - if (bom == BOM_UTF8) { - m_wBOMLength = 3; - m_wCodePage = FX_CODEPAGE_UTF8; - } else { - bom &= BOM_UTF16_MASK; - if (bom == BOM_UTF16_BE) { - m_wBOMLength = 2; - m_wCodePage = FX_CODEPAGE_UTF16BE; - } else if (bom == BOM_UTF16_LE) { - m_wBOMLength = 2; - m_wCodePage = FX_CODEPAGE_UTF16LE; - } else { - m_wBOMLength = 0; - m_wCodePage = FXSYS_GetACP(); - } - } - - Seek(FX_STREAMSEEK_Begin, - std::max(static_cast(m_wBOMLength), iPosition)); -} - -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_Stream::SetCodePage(uint16_t wCodePage) { - if (m_wBOMLength > 0) - return; - m_wCodePage = wCodePage; -} - -FX_STRSIZE CFGAS_Stream::ReadData(uint8_t* pBuffer, FX_STRSIZE iBufferSize) { - ASSERT(pBuffer && iBufferSize > 0); - - if (m_IsWriteStream) - return -1; - - iBufferSize = std::min( - iBufferSize, static_cast(m_pStream->GetSize() - m_iPosition)); - if (iBufferSize <= 0) - return 0; - - 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_STRSIZE CFGAS_Stream::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) { - FX_FILESIZE iBytes = iMaxLength * 2; - FX_STRSIZE iLen = ReadData(reinterpret_cast(pStr), iBytes); - iMaxLength = iLen / 2; - if (sizeof(wchar_t) > 2) - UTF16ToWChar(pStr, iMaxLength); - -#if _FX_ENDIAN_ == _FX_BIG_ENDIAN_ - if (m_wCodePage == FX_CODEPAGE_UTF16LE) - SwapByteOrder(pStr, iMaxLength); -#else - if (m_wCodePage == FX_CODEPAGE_UTF16BE) - SwapByteOrder(pStr, iMaxLength); -#endif - - } else { - FX_FILESIZE pos = GetPosition(); - FX_STRSIZE iBytes = - std::min(iMaxLength, static_cast(GetLength() - pos)); - - if (iBytes > 0) { - std::vector buf(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); - Seek(FX_STREAMSEEK_Current, iSrc - iLen); - } else { - iMaxLength = 0; - } - } - - *bEOS = IsEOF(); - 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 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) - return; - if (m_wCodePage != FX_CODEPAGE_UTF8) - return; - - WriteData(str); -} - -} // namespace - -// static -CFX_RetainPtr IFGAS_Stream::CreateReadStream( - const CFX_RetainPtr& pFileRead) { - if (!pFileRead) - return nullptr; - - return pdfium::MakeRetain(pFileRead, false); -} - -// static -CFX_RetainPtr IFGAS_Stream::CreateWriteStream( - const CFX_RetainPtr& pFileWrite) { - if (!pFileWrite) - return nullptr; - - return pdfium::MakeRetain(pFileWrite, true); -} 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 CreateReadStream( - const CFX_RetainPtr& pFileRead); - static CFX_RetainPtr CreateWriteStream( - const CFX_RetainPtr& 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_ diff --git a/xfa/fgas/font/cfgas_fontmgr.h b/xfa/fgas/font/cfgas_fontmgr.h index c0b59f4dd7..8a3705335e 100644 --- a/xfa/fgas/font/cfgas_fontmgr.h +++ b/xfa/fgas/font/cfgas_fontmgr.h @@ -18,7 +18,7 @@ #include "core/fxge/cfx_fontmapper.h" #include "core/fxge/fx_freetype.h" #include "core/fxge/ifx_systemfontinfo.h" -#include "xfa/fgas/crt/ifgas_stream.h" +#include "xfa/fgas/crt/cfgas_stream.h" #define FX_FONTSTYLE_Normal 0x00 #define FX_FONTSTYLE_FixedPitch 0x01 diff --git a/xfa/fgas/font/cfgas_gefont.h b/xfa/fgas/font/cfgas_gefont.h index 14029be1e0..6c9f2a168d 100644 --- a/xfa/fgas/font/cfgas_gefont.h +++ b/xfa/fgas/font/cfgas_gefont.h @@ -67,7 +67,7 @@ class CFGAS_GEFont : public CFX_Retainable { uint32_t dwFontStyles, uint16_t wCodePage); bool LoadFontInternal(const uint8_t* pBuffer, int32_t length); - bool LoadFontInternal(const CFX_RetainPtr& pFontStream, + bool LoadFontInternal(const CFX_RetainPtr& pFontStream, bool bSaveStream); #endif bool LoadFontInternal(CFX_Font* pExternalFont); @@ -95,7 +95,7 @@ class CFGAS_GEFont : public CFX_Retainable { CFX_RetainPtr m_pSrcFont; // Only set by ctor, so no cycles. CFGAS_FontMgr* const m_pFontMgr; bool m_bExternalFont; - CFX_RetainPtr m_pStream; + CFX_RetainPtr m_pStream; CFX_RetainPtr m_pFileRead; std::unique_ptr m_pFontEncoding; std::map m_CharWidthMap; diff --git a/xfa/fxfa/parser/cxfa_dataexporter.cpp b/xfa/fxfa/parser/cxfa_dataexporter.cpp index 4e3b0b3ffa..442195b1dd 100644 --- a/xfa/fxfa/parser/cxfa_dataexporter.cpp +++ b/xfa/fxfa/parser/cxfa_dataexporter.cpp @@ -226,8 +226,7 @@ void RegenerateFormFile_Changed(CXFA_Node* pNode, CFX_RetainPtr pMemStream = IFX_MemoryStream::Create(true); - CFX_RetainPtr pTempStream = - IFGAS_Stream::CreateWriteStream(pMemStream); + auto pTempStream = pdfium::MakeRetain(pMemStream, true); pTempStream->SetCodePage(FX_CODEPAGE_UTF8); pRichTextXML->SaveXMLNode(pTempStream); @@ -343,8 +342,8 @@ void RegenerateFormFile_Changed(CXFA_Node* pNode, } void RegenerateFormFile_Container(CXFA_Node* pNode, - const CFX_RetainPtr& pStream, - bool bSaveXML = false) { + const CFX_RetainPtr& pStream, + bool bSaveXML) { XFA_Element eType = pNode->GetElementType(); if (eType == XFA_Element::Field || eType == XFA_Element::Draw || !pNode->IsContainerNode()) { @@ -400,7 +399,7 @@ void RegenerateFormFile_Container(CXFA_Node* pNode, void XFA_DataExporter_RegenerateFormFile( CXFA_Node* pNode, - const CFX_RetainPtr& pStream, + const CFX_RetainPtr& pStream, const char* pChecksum, bool bSaveXML) { if (pNode->IsModelNode()) { @@ -428,7 +427,7 @@ void XFA_DataExporter_RegenerateFormFile( CXFA_Node* pChildNode = pNode->GetNodeItem(XFA_NODEITEM_FirstChild); while (pChildNode) { - RegenerateFormFile_Container(pChildNode, pStream); + RegenerateFormFile_Container(pChildNode, pStream, false); pChildNode = pChildNode->GetNodeItem(XFA_NODEITEM_NextSibling); } pStream->WriteString(L""); @@ -486,15 +485,12 @@ bool CXFA_DataExporter::Export(const CFX_RetainPtr& pWrite, if (!pWrite) return false; - CFX_RetainPtr pStream = IFGAS_Stream::CreateWriteStream(pWrite); - if (!pStream) - return false; - + auto pStream = pdfium::MakeRetain(pWrite, true); pStream->SetCodePage(FX_CODEPAGE_UTF8); return Export(pStream, pNode, dwFlag, pChecksum); } -bool CXFA_DataExporter::Export(const CFX_RetainPtr& pStream, +bool CXFA_DataExporter::Export(const CFX_RetainPtr& pStream, CXFA_Node* pNode, uint32_t dwFlag, const char* pChecksum) { diff --git a/xfa/fxfa/parser/cxfa_dataexporter.h b/xfa/fxfa/parser/cxfa_dataexporter.h index 8f405b188d..c7e5c7b70b 100644 --- a/xfa/fxfa/parser/cxfa_dataexporter.h +++ b/xfa/fxfa/parser/cxfa_dataexporter.h @@ -13,7 +13,7 @@ class CXFA_Document; class CXFA_Node; class IFX_SeekableStream; -class IFGAS_Stream; +class CFGAS_Stream; class CXFA_DataExporter { public: @@ -26,7 +26,7 @@ class CXFA_DataExporter { const char* pChecksum); private: - bool Export(const CFX_RetainPtr& pStream, + bool Export(const CFX_RetainPtr& pStream, CXFA_Node* pNode, uint32_t dwFlag, const char* pChecksum); diff --git a/xfa/fxfa/parser/cxfa_node.cpp b/xfa/fxfa/parser/cxfa_node.cpp index 90e93fbda5..26dc6bd831 100644 --- a/xfa/fxfa/parser/cxfa_node.cpp +++ b/xfa/fxfa/parser/cxfa_node.cpp @@ -1408,14 +1408,7 @@ void CXFA_Node::Script_NodeClass_SaveXML(CFXJSE_Arguments* pArguments) { } CFX_RetainPtr pMemoryStream = IFX_MemoryStream::Create(true); - CFX_RetainPtr pStream = - IFGAS_Stream::CreateWriteStream(pMemoryStream); - - if (!pStream) { - pArguments->GetReturnValue()->SetString( - bsXMLHeader.UTF8Encode().AsStringC()); - return; - } + auto pStream = pdfium::MakeRetain(pMemoryStream, true); pStream->SetCodePage(FX_CODEPAGE_UTF8); pStream->WriteString(bsXMLHeader.AsStringC()); diff --git a/xfa/fxfa/parser/cxfa_simple_parser.cpp b/xfa/fxfa/parser/cxfa_simple_parser.cpp index 1ebed10b4d..92196ec957 100644 --- a/xfa/fxfa/parser/cxfa_simple_parser.cpp +++ b/xfa/fxfa/parser/cxfa_simple_parser.cpp @@ -19,8 +19,8 @@ #include "xfa/fde/xml/cfde_xmlnode.h" #include "xfa/fde/xml/cfde_xmlparser.h" #include "xfa/fde/xml/cfde_xmltext.h" +#include "xfa/fgas/crt/cfgas_stream.h" #include "xfa/fgas/crt/fgas_codepage.h" -#include "xfa/fgas/crt/ifgas_stream.h" #include "xfa/fxfa/fxfa.h" #include "xfa/fxfa/parser/cxfa_document.h" #include "xfa/fxfa/parser/cxfa_node.h" @@ -277,10 +277,7 @@ int32_t CXFA_SimpleParser::StartParse( XFA_XDPPACKET ePacketID) { CloseParser(); m_pFileRead = pStream; - m_pStream = IFGAS_Stream::CreateReadStream(pStream); - if (!m_pStream) - return XFA_PARSESTATUS_StreamErr; - + m_pStream = pdfium::MakeRetain(pStream, false); uint16_t wCodePage = m_pStream->GetCodePage(); if (wCodePage != FX_CODEPAGE_UTF16LE && wCodePage != FX_CODEPAGE_UTF16BE && wCodePage != FX_CODEPAGE_UTF8) { @@ -322,9 +319,8 @@ CFDE_XMLNode* CXFA_SimpleParser::ParseXMLData(const CFX_ByteString& wsXML, CloseParser(); m_pXMLDoc = pdfium::MakeUnique(); - CFX_RetainPtr pStream = - IFGAS_Stream::CreateReadStream(IFX_MemoryStream::Create( - const_cast(wsXML.raw_str()), wsXML.GetLength())); + auto pStream = pdfium::MakeRetain( + const_cast(wsXML.raw_str()), wsXML.GetLength()); auto pParser = pdfium::MakeUnique(m_pXMLDoc->GetRoot(), pStream); pParser->m_dwCheckStatus = 0x03; diff --git a/xfa/fxfa/parser/cxfa_simple_parser.h b/xfa/fxfa/parser/cxfa_simple_parser.h index 7671e8d740..32b5518fab 100644 --- a/xfa/fxfa/parser/cxfa_simple_parser.h +++ b/xfa/fxfa/parser/cxfa_simple_parser.h @@ -19,7 +19,7 @@ class CFDE_XMLNode; class CFDE_XMLParser; class IFX_SeekableStream; class IFX_Pause; -class IFGAS_Stream; +class CFGAS_Stream; class CXFA_SimpleParser { public: @@ -78,7 +78,7 @@ class CXFA_SimpleParser { CFDE_XMLParser* m_pXMLParser; std::unique_ptr m_pXMLDoc; - CFX_RetainPtr m_pStream; + CFX_RetainPtr m_pStream; CFX_RetainPtr m_pFileRead; CXFA_Document* m_pFactory; CXFA_Node* m_pRootNode; diff --git a/xfa/fxfa/parser/xfa_utils.h b/xfa/fxfa/parser/xfa_utils.h index 36d62c7602..ae51c8fdc6 100644 --- a/xfa/fxfa/parser/xfa_utils.h +++ b/xfa/fxfa/parser/xfa_utils.h @@ -7,7 +7,7 @@ #ifndef XFA_FXFA_PARSER_XFA_UTILS_H_ #define XFA_FXFA_PARSER_XFA_UTILS_H_ -#include "xfa/fgas/crt/ifgas_stream.h" +#include "xfa/fgas/crt/cfgas_stream.h" #include "xfa/fxfa/fxfa_basic.h" class CFDE_XMLElement; @@ -155,7 +155,7 @@ bool XFA_FieldIsMultiListBox(CXFA_Node* pFieldNode); void XFA_DataExporter_DealWithDataGroupNode(CXFA_Node* pDataNode); void XFA_DataExporter_RegenerateFormFile( CXFA_Node* pNode, - const CFX_RetainPtr& pStream, + const CFX_RetainPtr& pStream, const char* pChecksum = nullptr, bool bSaveXML = false); -- cgit v1.2.3