From 61aad4fbb6fb1081a40b2e144106f57cf443846c Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Tue, 10 Nov 2015 10:28:56 -0800 Subject: Merge to XFA: Cleanup CPDF_Stream: - Remove CPDF_Stream::Create() and checks that it might fail. - Remove m_pCryptoHandler that's always a nullptr. - Remove m_FileOffset that's always 0. - Rename file version of InitStream() to InitStreamFromFile(). - Use IsMemoryBased() in more places. TBR=dsinclair@chromium.org Review URL: https://codereview.chromium.org/1415163009 . (cherry picked from commit e6e16954f0cdc324849fca9da883be8f131b3834) Review URL: https://codereview.chromium.org/1418493006 . --- core/include/fpdfapi/fpdf_objects.h | 27 ++-- core/src/fpdfapi/fpdf_edit/fpdf_edit_image.cpp | 8 +- .../src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp | 3 +- .../fpdfapi/fpdf_parser/fpdf_parser_objects.cpp | 160 +++++++++------------ core/src/fpdfdoc/doc_ap.cpp | 7 +- 5 files changed, 88 insertions(+), 117 deletions(-) (limited to 'core') diff --git a/core/include/fpdfapi/fpdf_objects.h b/core/include/fpdfapi/fpdf_objects.h index d6050aec0b..11589e65a2 100644 --- a/core/include/fpdfapi/fpdf_objects.h +++ b/core/include/fpdfapi/fpdf_objects.h @@ -466,12 +466,6 @@ inline const CPDF_Dictionary* ToDictionary(const CPDF_Object* obj) { class CPDF_Stream : public CPDF_Object { public: - static CPDF_Stream* Create(uint8_t* pData, - FX_DWORD size, - CPDF_Dictionary* pDict) { - return new CPDF_Stream(pData, size, pDict); - } - CPDF_Stream(uint8_t* pData, FX_DWORD size, CPDF_Dictionary* pDict); CPDF_Dictionary* GetDict() const { return m_pDict; } @@ -483,7 +477,7 @@ class CPDF_Stream : public CPDF_Object { void InitStream(uint8_t* pData, FX_DWORD size, CPDF_Dictionary* pDict); - void InitStream(IFX_FileRead* pFile, CPDF_Dictionary* pDict); + void InitStreamFromFile(IFX_FileRead* pFile, CPDF_Dictionary* pDict); FX_BOOL Identical(CPDF_Stream* pOther) const; @@ -493,11 +487,17 @@ class CPDF_Stream : public CPDF_Object { uint8_t* pBuf, FX_DWORD buf_size) const; - FX_BOOL IsMemoryBased() const { return m_GenNum == (FX_DWORD)-1; } + FX_BOOL IsMemoryBased() const { return m_GenNum == kMemoryBasedGenNum; } protected: + friend class CPDF_Object; + friend class CPDF_StreamAcc; + + static const FX_DWORD kMemoryBasedGenNum = (FX_DWORD)-1; ~CPDF_Stream(); + void InitStreamInternal(CPDF_Dictionary* pDict); + CPDF_Dictionary* m_pDict; FX_DWORD m_dwSize; @@ -509,15 +509,6 @@ class CPDF_Stream : public CPDF_Object { IFX_FileRead* m_pFile; }; - - FX_FILESIZE m_FileOffset; - - CPDF_CryptoHandler* m_pCryptoHandler; - - void InitStream(CPDF_Dictionary* pDict); - friend class CPDF_Object; - friend class CPDF_StreamAcc; - friend class CPDF_AttachmentAcc; }; inline CPDF_Stream* ToStream(CPDF_Object* obj) { return obj ? obj->AsStream() : nullptr; @@ -540,7 +531,7 @@ class CPDF_StreamAcc { const CPDF_Stream* GetStream() const { return m_pStream; } CPDF_Dictionary* GetDict() const { - return m_pStream ? m_pStream->GetDict() : NULL; + return m_pStream ? m_pStream->GetDict() : nullptr; } const uint8_t* GetData() const; diff --git a/core/src/fpdfapi/fpdf_edit/fpdf_edit_image.cpp b/core/src/fpdfapi/fpdf_edit/fpdf_edit_image.cpp index 2ec5727444..3ffe482026 100644 --- a/core/src/fpdfapi/fpdf_edit/fpdf_edit_image.cpp +++ b/core/src/fpdfapi/fpdf_edit/fpdf_edit_image.cpp @@ -85,7 +85,7 @@ void CPDF_Image::SetJpegImage(IFX_FileRead* pFile) { if (!pDict) { return; } - m_pStream->InitStream(pFile, pDict); + m_pStream->InitStreamFromFile(pFile, pDict); } void _DCTEncodeBitmap(CPDF_Dictionary* pBitmapDict, const CFX_DIBitmap* pBitmap, @@ -178,8 +178,8 @@ void CPDF_Image::SetImage(const CFX_DIBitmap* pBitmap, ptr[2] = (uint8_t)argb; ptr += 3; } - CPDF_Stream* pCTS = CPDF_Stream::Create(pColorTable, iPalette * 3, - CPDF_Dictionary::Create()); + CPDF_Stream* pCTS = + new CPDF_Stream(pColorTable, iPalette * 3, new CPDF_Dictionary); m_pDocument->AddIndirectObject(pCTS); pCS->AddReference(m_pDocument, pCTS); pDict->SetAtReference(FX_BSTRC("ColorSpace"), m_pDocument, pCS); @@ -374,7 +374,7 @@ void CPDF_Image::SetImage(const CFX_DIBitmap* pBitmap, m_pStream->InitStream(dest_buf, dest_size, pDict); } else { pFileWrite->Flush(); - m_pStream->InitStream(pFileRead, pDict); + m_pStream->InitStreamFromFile(pFileRead, pDict); } m_bIsMask = pBitmap->IsAlphaMask(); m_Width = BitmapWidth; diff --git a/core/src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp b/core/src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp index 0c48b5a137..8d155e787b 100644 --- a/core/src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp +++ b/core/src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp @@ -443,8 +443,9 @@ CPDF_Stream* CPDF_StreamParser::ReadInlineStream(CPDF_Document* pDoc, } } pDict->SetAtInteger(FX_BSTRC("Length"), (int)dwStreamSize); - return CPDF_Stream::Create(pData, dwStreamSize, pDict); + return new CPDF_Stream(pData, dwStreamSize, pDict); } + #define MAX_WORD_BUFFER 256 #define MAX_STRING_LENGTH 32767 #define FXDWORD_TRUE FXDWORD_FROM_LSBFIRST(0x65757274) diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp index c36e1466a2..990bf5f0e2 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp @@ -8,6 +8,12 @@ #include "core/include/fxcrt/fx_string.h" +namespace { + +const FX_DWORD kBlockSize = 1024; + +} // namespace + // static int CPDF_Object::s_nCurRefDepth = 0; @@ -816,41 +822,38 @@ void CPDF_Dictionary::SetAtMatrix(const CFX_ByteStringC& key, SetAt(key, pArray); } CPDF_Stream::CPDF_Stream(uint8_t* pData, FX_DWORD size, CPDF_Dictionary* pDict) - : CPDF_Object(PDFOBJ_STREAM) { - m_pDict = pDict; - m_dwSize = size; - m_GenNum = (FX_DWORD)-1; - m_pDataBuf = pData; - m_pCryptoHandler = NULL; -} + : CPDF_Object(PDFOBJ_STREAM), + m_pDict(pDict), + m_dwSize(size), + m_GenNum(kMemoryBasedGenNum), + m_pDataBuf(pData) {} + CPDF_Stream::~CPDF_Stream() { - if (m_GenNum == (FX_DWORD)-1) { + if (IsMemoryBased()) FX_Free(m_pDataBuf); - } - if (m_pDict) { + + if (m_pDict) m_pDict->Release(); - } } -void CPDF_Stream::InitStream(CPDF_Dictionary* pDict) { + +void CPDF_Stream::InitStreamInternal(CPDF_Dictionary* pDict) { if (pDict) { - if (m_pDict) { + if (m_pDict) m_pDict->Release(); - } m_pDict = pDict; } - if (m_GenNum == (FX_DWORD)-1) { + if (IsMemoryBased()) FX_Free(m_pDataBuf); - } + m_GenNum = 0; - m_pFile = NULL; - m_pCryptoHandler = NULL; - m_FileOffset = 0; + m_pFile = nullptr; } + void CPDF_Stream::InitStream(uint8_t* pData, FX_DWORD size, CPDF_Dictionary* pDict) { - InitStream(pDict); - m_GenNum = (FX_DWORD)-1; + InitStreamInternal(pDict); + m_GenNum = kMemoryBasedGenNum; m_pDataBuf = FX_Alloc(uint8_t, size); if (pData) { FXSYS_memcpy(m_pDataBuf, pData, size); @@ -864,12 +867,10 @@ void CPDF_Stream::SetData(const uint8_t* pData, FX_DWORD size, FX_BOOL bCompressed, FX_BOOL bKeepBuf) { - if (m_GenNum == (FX_DWORD)-1) { + if (IsMemoryBased()) FX_Free(m_pDataBuf); - } else { - m_GenNum = (FX_DWORD)-1; - m_pCryptoHandler = NULL; - } + m_GenNum = kMemoryBasedGenNum; + if (bKeepBuf) { m_pDataBuf = (uint8_t*)pData; } else { @@ -879,9 +880,8 @@ void CPDF_Stream::SetData(const uint8_t* pData, } } m_dwSize = size; - if (m_pDict == NULL) { + if (!m_pDict) m_pDict = new CPDF_Dictionary; - } m_pDict->SetAtInteger(FX_BSTRC("Length"), size); if (!bCompressed) { m_pDict->RemoveAt(FX_BSTRC("Filter")); @@ -891,16 +891,16 @@ void CPDF_Stream::SetData(const uint8_t* pData, FX_BOOL CPDF_Stream::ReadRawData(FX_FILESIZE offset, uint8_t* buf, FX_DWORD size) const { - if ((m_GenNum != (FX_DWORD)-1) && m_pFile) { - return m_pFile->ReadBlock(buf, m_FileOffset + offset, size); - } - if (m_pDataBuf) { + if (!IsMemoryBased() && m_pFile) + return m_pFile->ReadBlock(buf, offset, size); + + if (m_pDataBuf) FXSYS_memcpy(buf, m_pDataBuf + offset, size); - } return TRUE; } -void CPDF_Stream::InitStream(IFX_FileRead* pFile, CPDF_Dictionary* pDict) { - InitStream(pDict); +void CPDF_Stream::InitStreamFromFile(IFX_FileRead* pFile, + CPDF_Dictionary* pDict) { + InitStreamInternal(pDict); m_pFile = pFile; m_dwSize = (FX_DWORD)pFile->GetSize(); if (m_pDict) { @@ -910,66 +910,63 @@ void CPDF_Stream::InitStream(IFX_FileRead* pFile, CPDF_Dictionary* pDict) { FX_BOOL CPDF_Stream::Identical(CPDF_Stream* pOther) const { if (!m_pDict) - return pOther->m_pDict ? FALSE : TRUE; + return !pOther->m_pDict; - if (!m_pDict->Identical(pOther->m_pDict)) { + if (!m_pDict->Identical(pOther->m_pDict)) return FALSE; - } - if (m_dwSize != pOther->m_dwSize) { + + if (m_dwSize != pOther->m_dwSize) return FALSE; - } - if (m_GenNum != (FX_DWORD)-1 && pOther->m_GenNum != (FX_DWORD)-1) { - if (m_pFile == pOther->m_pFile && m_pFile == NULL) { + + if (!IsMemoryBased() && !pOther->IsMemoryBased()) { + if (m_pFile == pOther->m_pFile && !m_pFile) return TRUE; - } - if (!m_pFile || !pOther->m_pFile) { + + if (!m_pFile || !pOther->m_pFile) return FALSE; - } - uint8_t srcBuf[1024]; - uint8_t destBuf[1024]; + + uint8_t srcBuf[kBlockSize]; + uint8_t destBuf[kBlockSize]; FX_DWORD size = m_dwSize; - FX_DWORD srcOffset = m_FileOffset; - FX_DWORD destOffset = pOther->m_FileOffset; - if (m_pFile == pOther->m_pFile && srcOffset == destOffset) { + if (m_pFile == pOther->m_pFile) return TRUE; - } + + FX_DWORD offset = 0; while (size > 0) { - FX_DWORD actualSize = size > 1024 ? 1024 : size; - m_pFile->ReadBlock(srcBuf, srcOffset, actualSize); - pOther->m_pFile->ReadBlock(destBuf, destOffset, actualSize); - if (FXSYS_memcmp(srcBuf, destBuf, actualSize) != 0) { + FX_DWORD actualSize = std::min(size, kBlockSize); + m_pFile->ReadBlock(srcBuf, offset, actualSize); + pOther->m_pFile->ReadBlock(destBuf, offset, actualSize); + if (FXSYS_memcmp(srcBuf, destBuf, actualSize) != 0) return FALSE; - } + size -= actualSize; - srcOffset += actualSize; - destOffset += actualSize; + offset += actualSize; } return TRUE; } - if (m_GenNum != (FX_DWORD)-1 || pOther->m_GenNum != (FX_DWORD)-1) { - IFX_FileRead* pFile = NULL; - uint8_t* pBuf = NULL; - FX_DWORD offset = 0; - if (pOther->m_GenNum != (FX_DWORD)-1) { + + if (!IsMemoryBased() || !pOther->IsMemoryBased()) { + IFX_FileRead* pFile = nullptr; + uint8_t* pBuf = nullptr; + if (!pOther->IsMemoryBased()) { pFile = pOther->m_pFile; pBuf = m_pDataBuf; - offset = pOther->m_FileOffset; - } else if (m_GenNum != (FX_DWORD)-1) { + } else if (!IsMemoryBased()) { pFile = m_pFile; pBuf = pOther->m_pDataBuf; - offset = m_FileOffset; } - if (NULL == pBuf) { + if (!pBuf) return FALSE; - } - uint8_t srcBuf[1024]; + + uint8_t srcBuf[kBlockSize]; FX_DWORD size = m_dwSize; + FX_DWORD offset = 0; while (size > 0) { - FX_DWORD actualSize = std::min(size, 1024U); + FX_DWORD actualSize = std::min(size, kBlockSize); pFile->ReadBlock(srcBuf, offset, actualSize); - if (FXSYS_memcmp(srcBuf, pBuf, actualSize) != 0) { + if (FXSYS_memcmp(srcBuf, pBuf, actualSize) != 0) return FALSE; - } + pBuf += actualSize; size -= actualSize; offset += actualSize; @@ -1013,23 +1010,8 @@ void CPDF_StreamAcc::LoadAllData(const CPDF_Stream* pStream, } else { pSrcData = pStream->m_pDataBuf; } - uint8_t* pDecryptedData; - FX_DWORD dwDecryptedSize; - if (pStream->m_pCryptoHandler) { - CFX_BinaryBuf dest_buf; - dest_buf.EstimateSize(pStream->m_pCryptoHandler->DecryptGetSize(dwSrcSize)); - void* context = pStream->m_pCryptoHandler->DecryptStart( - pStream->GetObjNum(), pStream->m_GenNum); - pStream->m_pCryptoHandler->DecryptStream(context, pSrcData, dwSrcSize, - dest_buf); - pStream->m_pCryptoHandler->DecryptFinish(context, dest_buf); - pDecryptedData = dest_buf.GetBuffer(); - dwDecryptedSize = dest_buf.GetSize(); - dest_buf.DetachBuffer(); - } else { - pDecryptedData = pSrcData; - dwDecryptedSize = dwSrcSize; - } + uint8_t* pDecryptedData = pSrcData; + FX_DWORD dwDecryptedSize = dwSrcSize; if (!pStream->GetDict()->KeyExist(FX_BSTRC("Filter")) || bRawAccess) { m_pData = pDecryptedData; m_dwSize = dwDecryptedSize; diff --git a/core/src/fpdfdoc/doc_ap.cpp b/core/src/fpdfdoc/doc_ap.cpp index d6c25f31e3..7fc9407c73 100644 --- a/core/src/fpdfdoc/doc_ap.cpp +++ b/core/src/fpdfdoc/doc_ap.cpp @@ -427,11 +427,8 @@ static FX_BOOL GenerateWidgetAP(CPDF_Document* pDoc, pAnnotDict->SetAt("AP", pAPDict); } CPDF_Stream* pNormalStream = pAPDict->GetStream("N"); - if (pNormalStream == NULL) { - pNormalStream = CPDF_Stream::Create(NULL, 0, NULL); - if (pNormalStream == NULL) { - return FALSE; - } + if (!pNormalStream) { + pNormalStream = new CPDF_Stream(nullptr, 0, nullptr); int32_t objnum = pDoc->AddIndirectObject(pNormalStream); pAnnotDict->GetDict("AP")->SetAtReference("N", pDoc, objnum); } -- cgit v1.2.3