From 430ab8363e77c48b2c2435af4d289f85e2be1b96 Mon Sep 17 00:00:00 2001 From: tsepez Date: Fri, 18 Nov 2016 14:48:21 -0800 Subject: Add unit test for CXFA_FileRead. The FileRead's return convention is being modified at https://codereview.chromium.org/2430743003/, so first provide a test of the old behaviour. Fix some issues with null dictionaries as provided by the CPDF_Stream default ctor along the way. Review-Url: https://codereview.chromium.org/2517513003 --- core/fpdfapi/edit/fpdf_edit_create.cpp | 34 ++++++++++++++++----------------- core/fpdfapi/parser/cpdf_stream.cpp | 4 ++++ core/fpdfapi/parser/cpdf_stream.h | 1 + core/fpdfapi/parser/cpdf_stream_acc.cpp | 20 ++++++++----------- 4 files changed, 29 insertions(+), 30 deletions(-) (limited to 'core') 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 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); -- cgit v1.2.3