diff options
-rw-r--r-- | BUILD.gn | 1 | ||||
-rw-r--r-- | core/fpdfapi/edit/fpdf_edit_create.cpp | 34 | ||||
-rw-r--r-- | core/fpdfapi/parser/cpdf_stream.cpp | 4 | ||||
-rw-r--r-- | core/fpdfapi/parser/cpdf_stream.h | 1 | ||||
-rw-r--r-- | core/fpdfapi/parser/cpdf_stream_acc.cpp | 20 | ||||
-rw-r--r-- | xfa/fxfa/app/xfa_ffapp_unittest.cpp | 86 |
6 files changed, 116 insertions, 30 deletions
@@ -1743,6 +1743,7 @@ test("pdfium_unittests") { "xfa/fde/css/fde_cssdatatable_unittest.cpp", "xfa/fde/xml/fde_xml_imp_unittest.cpp", "xfa/fxbarcode/pdf417/BC_PDF417HighLevelEncoder_unittest.cpp", + "xfa/fxfa/app/xfa_ffapp_unittest.cpp", "xfa/fxfa/app/xfa_textlayout_unittest.cpp", "xfa/fxfa/fm2js/xfa_simpleexpression_unittest.cpp", "xfa/fxfa/parser/xfa_utils_unittest.cpp", diff --git a/core/fpdfapi/edit/fpdf_edit_create.cpp b/core/fpdfapi/edit/fpdf_edit_create.cpp index ff12606399..360472948d 100644 --- a/core/fpdfapi/edit/fpdf_edit_create.cpp +++ b/core/fpdfapi/edit/fpdf_edit_create.cpp @@ -417,26 +417,24 @@ CPDF_FlateEncoder::CPDF_FlateEncoder(CPDF_Stream* pStream, bool bFlateEncode) m_bCloned(false), m_bNewData(false) { m_Acc.LoadAllData(pStream, true); - if ((pStream && pStream->GetDict() && - pStream->GetDict()->KeyExist("Filter")) || - !bFlateEncode) { - if (pStream->GetDict()->KeyExist("Filter") && !bFlateEncode) { - CPDF_StreamAcc destAcc; - destAcc.LoadAllData(pStream); - m_dwSize = destAcc.GetSize(); - m_pData = (uint8_t*)destAcc.DetachData(); - m_pDict = ToDictionary(pStream->GetDict()->Clone().release()); - m_pDict->RemoveFor("Filter"); - m_bNewData = true; - m_bCloned = true; - } else { - m_pData = (uint8_t*)m_Acc.GetData(); - m_dwSize = m_Acc.GetSize(); - m_pDict = pStream->GetDict(); - } + bool bHasFilter = pStream && pStream->HasFilter(); + if (bHasFilter && !bFlateEncode) { + CPDF_StreamAcc destAcc; + destAcc.LoadAllData(pStream); + m_dwSize = destAcc.GetSize(); + m_pData = (uint8_t*)destAcc.DetachData(); + m_pDict = ToDictionary(pStream->GetDict()->Clone().release()); + m_pDict->RemoveFor("Filter"); + m_bNewData = true; + m_bCloned = true; + return; + } + if (bHasFilter || !bFlateEncode) { + m_pData = (uint8_t*)m_Acc.GetData(); + m_dwSize = m_Acc.GetSize(); + m_pDict = pStream->GetDict(); return; } - m_bNewData = true; m_bCloned = true; // TODO(thestig): Move to Init() and check return value. diff --git a/core/fpdfapi/parser/cpdf_stream.cpp b/core/fpdfapi/parser/cpdf_stream.cpp index e8ee022940..64261b14d3 100644 --- a/core/fpdfapi/parser/cpdf_stream.cpp +++ b/core/fpdfapi/parser/cpdf_stream.cpp @@ -113,6 +113,10 @@ bool CPDF_Stream::ReadRawData(FX_FILESIZE offset, return true; } +bool CPDF_Stream::HasFilter() const { + return m_pDict && m_pDict->KeyExist("Filter"); +} + CFX_WideString CPDF_Stream::GetUnicodeText() const { CPDF_StreamAcc stream; stream.LoadAllData(this, false); diff --git a/core/fpdfapi/parser/cpdf_stream.h b/core/fpdfapi/parser/cpdf_stream.h index ddf7cc5b69..c4f9aacf2b 100644 --- a/core/fpdfapi/parser/cpdf_stream.h +++ b/core/fpdfapi/parser/cpdf_stream.h @@ -46,6 +46,7 @@ class CPDF_Stream : public CPDF_Object { uint32_t buf_size) const; bool IsMemoryBased() const { return m_bMemoryBased; } + bool HasFilter() const; protected: std::unique_ptr<CPDF_Object> CloneNonCyclic( diff --git a/core/fpdfapi/parser/cpdf_stream_acc.cpp b/core/fpdfapi/parser/cpdf_stream_acc.cpp index 81439bc677..01d8e148df 100644 --- a/core/fpdfapi/parser/cpdf_stream_acc.cpp +++ b/core/fpdfapi/parser/cpdf_stream_acc.cpp @@ -24,17 +24,16 @@ void CPDF_StreamAcc::LoadAllData(const CPDF_Stream* pStream, return; m_pStream = pStream; - if (pStream->IsMemoryBased() && - (!pStream->GetDict()->KeyExist("Filter") || bRawAccess)) { + if (pStream->IsMemoryBased() && (!pStream->HasFilter() || bRawAccess)) { m_dwSize = pStream->GetRawSize(); m_pData = pStream->GetRawData(); return; } - uint8_t* pSrcData; uint32_t dwSrcSize = pStream->GetRawSize(); if (dwSrcSize == 0) return; + uint8_t* pSrcData; if (!pStream->IsMemoryBased()) { pSrcData = m_pSrcData = FX_Alloc(uint8_t, dwSrcSize); if (!pStream->ReadRawData(0, pSrcData, dwSrcSize)) @@ -42,17 +41,14 @@ void CPDF_StreamAcc::LoadAllData(const CPDF_Stream* pStream, } else { pSrcData = pStream->GetRawData(); } - if (!pStream->GetDict()->KeyExist("Filter") || bRawAccess) { + if (!pStream->HasFilter() || bRawAccess) { + m_pData = pSrcData; + m_dwSize = dwSrcSize; + } else if (!PDF_DataDecode(pSrcData, dwSrcSize, m_pStream->GetDict(), m_pData, + m_dwSize, m_ImageDecoder, m_pImageParam, + estimated_size, bImageAcc)) { m_pData = pSrcData; m_dwSize = dwSrcSize; - } else { - bool bRet = PDF_DataDecode(pSrcData, dwSrcSize, m_pStream->GetDict(), - m_pData, m_dwSize, m_ImageDecoder, m_pImageParam, - estimated_size, bImageAcc); - if (!bRet) { - m_pData = pSrcData; - m_dwSize = dwSrcSize; - } } if (pSrcData != pStream->GetRawData() && pSrcData != m_pData) FX_Free(pSrcData); diff --git a/xfa/fxfa/app/xfa_ffapp_unittest.cpp b/xfa/fxfa/app/xfa_ffapp_unittest.cpp new file mode 100644 index 0000000000..7e1b9d7237 --- /dev/null +++ b/xfa/fxfa/app/xfa_ffapp_unittest.cpp @@ -0,0 +1,86 @@ +// Copyright 2016 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. + +#include "xfa/fxfa/xfa_ffapp.h" + +#include <memory> + +#include "core/fpdfapi/parser/cpdf_dictionary.h" +#include "core/fpdfapi/parser/cpdf_stream.h" +#include "core/fxcrt/fx_memory.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "third_party/base/ptr_util.h" + +using UniqueFileRead = + std::unique_ptr<CXFA_FileRead, ReleaseDeleter<CXFA_FileRead>>; + +TEST(CXFAFileRead, NoStreams) { + std::vector<CPDF_Stream*> streams; + UniqueFileRead fileread(new CXFA_FileRead(streams)); + + uint8_t output_buffer[16]; + memset(output_buffer, 0xbd, sizeof(output_buffer)); + EXPECT_FALSE(fileread->ReadBlock(output_buffer, 0, 0)); + EXPECT_EQ(0xbd, output_buffer[0]); +} + +TEST(CXFAFileRead, EmptyStreams) { + std::vector<CPDF_Stream*> streams; + std::unique_ptr<CPDF_Stream> stream1 = pdfium::MakeUnique<CPDF_Stream>(); + streams.push_back(stream1.get()); + UniqueFileRead fileread(new CXFA_FileRead(streams)); + + uint8_t output_buffer[16]; + memset(output_buffer, 0xbd, sizeof(output_buffer)); + EXPECT_FALSE(fileread->ReadBlock(output_buffer, 0, 0)); + EXPECT_EQ(0xbd, output_buffer[0]); +} + +TEST(CXFAFileRead, NormalStreams) { + std::vector<CPDF_Stream*> streams; + std::unique_ptr<CPDF_Stream> stream1 = pdfium::MakeUnique<CPDF_Stream>(); + std::unique_ptr<CPDF_Stream> stream2 = pdfium::MakeUnique<CPDF_Stream>(); + std::unique_ptr<CPDF_Stream> stream3 = pdfium::MakeUnique<CPDF_Stream>(); + + // 16 chars total. + stream1->InitStream(reinterpret_cast<const uint8_t*>("one t"), 5, + new CPDF_Dictionary()); + stream2->InitStream(reinterpret_cast<const uint8_t*>("wo "), 3, + new CPDF_Dictionary()); + stream3->InitStream(reinterpret_cast<const uint8_t*>("three!!!"), 8, + new CPDF_Dictionary()); + + streams.push_back(stream1.get()); + streams.push_back(stream2.get()); + streams.push_back(stream3.get()); + UniqueFileRead fileread(new CXFA_FileRead(streams)); + + uint8_t output_buffer[16]; + memset(output_buffer, 0xbd, sizeof(output_buffer)); + EXPECT_TRUE(fileread->ReadBlock(output_buffer, 0, 0)); + EXPECT_EQ(0xbd, output_buffer[0]); + + memset(output_buffer, 0xbd, sizeof(output_buffer)); + EXPECT_TRUE(fileread->ReadBlock(output_buffer, 1, 0)); + EXPECT_EQ(0xbd, output_buffer[0]); + + memset(output_buffer, 0xbd, sizeof(output_buffer)); + EXPECT_TRUE(fileread->ReadBlock(output_buffer, 0, 1)); + EXPECT_EQ(0, memcmp(output_buffer, "o", 1)); + EXPECT_EQ(0xbd, output_buffer[1]); + + memset(output_buffer, 0xbd, sizeof(output_buffer)); + EXPECT_TRUE(fileread->ReadBlock(output_buffer, 0, sizeof(output_buffer))); + EXPECT_EQ(0, memcmp(output_buffer, "one two three!!!", 16)); + + memset(output_buffer, 0xbd, sizeof(output_buffer)); + EXPECT_TRUE(fileread->ReadBlock(output_buffer, 2, 10)); + EXPECT_EQ(0, memcmp(output_buffer, "e two thre", 10)); + EXPECT_EQ(0xbd, output_buffer[11]); + + memset(output_buffer, 0xbd, sizeof(output_buffer)); + EXPECT_FALSE(fileread->ReadBlock(output_buffer, 1, sizeof(output_buffer))); + EXPECT_EQ(0, memcmp(output_buffer, "ne two three!!!", 15)); + EXPECT_EQ(0xbd, output_buffer[15]); +} |