diff options
author | Lei Zhang <thestig@chromium.org> | 2016-01-08 14:24:37 -0800 |
---|---|---|
committer | Lei Zhang <thestig@chromium.org> | 2016-01-08 14:24:37 -0800 |
commit | 1b97664bc5b84c7a2b87cc656f8c6a0476f9b71f (patch) | |
tree | 11c6727d7fa3d2dac36b879c3bbd106326c7ca43 /core | |
parent | 6a1c51b9eb666f2e120ec098d900663627ebcb3e (diff) | |
download | pdfium-1b97664bc5b84c7a2b87cc656f8c6a0476f9b71f.tar.xz |
Merge to XFA: Clean up misc nits found while fixing bugs.
Some bits regressed in commit 338805f.
Also merge: IWYU fix after commit c5a8933.
R=tsepez@chromium.org
TBR=tsepez@chromium.org
Review URL: https://codereview.chromium.org/1544963002 .
Review URL: https://codereview.chromium.org/1570973002 .
(cherry picked from commit c5a8933f260ca1034fde902058442984e55c0f7a)
(cherry picked from commit e6bd31873c3dba3f79c5ebbbefed636948629cc9)
Review URL: https://codereview.chromium.org/1577453002 .
Diffstat (limited to 'core')
-rw-r--r-- | core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp | 239 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp | 210 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp | 14 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_page/pageint.h | 2 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp | 4 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp | 15 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_parser/fpdf_parser_parser_unittest.cpp | 12 |
7 files changed, 240 insertions, 256 deletions
diff --git a/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp b/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp index 2dc985413b..c1ef37dab6 100644 --- a/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp +++ b/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp @@ -4,7 +4,9 @@ // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com -#include "editint.h" +#include "core/src/fpdfapi/fpdf_edit/editint.h" + +#include <vector> #include "core/include/fxcrt/fx_ext.h" #include "core/include/fpdfapi/fpdf_serial.h" @@ -13,6 +15,8 @@ #define PDF_OBJECTSTREAM_MAXLENGTH (256 * 1024) #define PDF_XREFSTREAM_MAXSIZE 10000 +namespace { + int32_t PDF_CreatorAppendObject(const CPDF_Object* pObj, CFX_FileBufferArchive* pFile, FX_FILESIZE& offset) { @@ -169,6 +173,7 @@ int32_t PDF_CreatorAppendObject(const CPDF_Object* pObj, } return 1; } + int32_t PDF_CreatorWriteTrailer(CPDF_Document* pDocument, CFX_FileBufferArchive* pFile, CPDF_Array* pIDArray, @@ -259,6 +264,7 @@ int32_t PDF_CreatorWriteTrailer(CPDF_Document* pDocument, } return offset; } + int32_t PDF_CreatorWriteEncrypt(const CPDF_Dictionary* pEncryptDict, FX_DWORD dwObjNum, CFX_FileBufferArchive* pFile) { @@ -284,25 +290,89 @@ int32_t PDF_CreatorWriteEncrypt(const CPDF_Dictionary* pEncryptDict, offset += len + 6; return offset; } -FX_BOOL PDF_GenerateFileID(FX_DWORD dwSeed1, - FX_DWORD dwSeed2, - FX_DWORD* pBuffer) { - if (!pBuffer) { - return FALSE; - } + +std::vector<uint8_t> PDF_GenerateFileID(FX_DWORD dwSeed1, FX_DWORD dwSeed2) { + std::vector<uint8_t> buffer(sizeof(FX_DWORD) * 4); + FX_DWORD* pBuffer = reinterpret_cast<FX_DWORD*>(buffer.data()); void* pContext = FX_Random_MT_Start(dwSeed1); - int32_t i = 0; - for (i = 0; i < 2; i++) { + for (int i = 0; i < 2; ++i) *pBuffer++ = FX_Random_MT_Generate(pContext); - } FX_Random_MT_Close(pContext); pContext = FX_Random_MT_Start(dwSeed2); - for (i = 0; i < 2; i++) { + for (int i = 0; i < 2; ++i) *pBuffer++ = FX_Random_MT_Generate(pContext); - } FX_Random_MT_Close(pContext); - return TRUE; + return buffer; +} + +void AppendIndex0(CFX_ByteTextBuf& buffer, bool bFirstObject) { + buffer.AppendByte(0); + buffer.AppendByte(0); + buffer.AppendByte(0); + buffer.AppendByte(0); + buffer.AppendByte(0); + const uint8_t byte = bFirstObject ? 0xFF : 0; + buffer.AppendByte(byte); + buffer.AppendByte(byte); +} + +void AppendIndex1(CFX_ByteTextBuf& buffer, FX_FILESIZE offset) { + buffer.AppendByte(1); + buffer.AppendByte(FX_GETBYTEOFFSET24(offset)); + buffer.AppendByte(FX_GETBYTEOFFSET16(offset)); + buffer.AppendByte(FX_GETBYTEOFFSET8(offset)); + buffer.AppendByte(FX_GETBYTEOFFSET0(offset)); + buffer.AppendByte(0); + buffer.AppendByte(0); +} + +void AppendIndex2(CFX_ByteTextBuf& buffer, FX_DWORD objnum, int32_t index) { + buffer.AppendByte(2); + buffer.AppendByte(FX_GETBYTEOFFSET24(objnum)); + buffer.AppendByte(FX_GETBYTEOFFSET16(objnum)); + buffer.AppendByte(FX_GETBYTEOFFSET8(objnum)); + buffer.AppendByte(FX_GETBYTEOFFSET0(objnum)); + buffer.AppendByte(FX_GETBYTEOFFSET8(index)); + buffer.AppendByte(FX_GETBYTEOFFSET0(index)); +} + +bool IsXRefNeedEnd(CPDF_XRefStream* pXRef, FX_DWORD flag) { + if (!(flag & FPDFCREATE_INCREMENTAL)) + return false; + + int32_t iSize = pXRef->m_IndexArray.GetSize() / 2; + int32_t iCount = 0; + for (int32_t i = 0; i < iSize; ++i) + iCount += pXRef->m_IndexArray.ElementAt(i * 2 + 1); + return iCount >= PDF_XREFSTREAM_MAXSIZE; } + +int32_t OutputIndex(CFX_FileBufferArchive* pFile, FX_FILESIZE offset) { + if (sizeof(offset) > 4) { + if (FX_GETBYTEOFFSET32(offset)) { + if (pFile->AppendByte(FX_GETBYTEOFFSET56(offset)) < 0) + return -1; + if (pFile->AppendByte(FX_GETBYTEOFFSET48(offset)) < 0) + return -1; + if (pFile->AppendByte(FX_GETBYTEOFFSET40(offset)) < 0) + return -1; + if (pFile->AppendByte(FX_GETBYTEOFFSET32(offset)) < 0) + return -1; + } + } + if (pFile->AppendByte(FX_GETBYTEOFFSET24(offset)) < 0) + return -1; + if (pFile->AppendByte(FX_GETBYTEOFFSET16(offset)) < 0) + return -1; + if (pFile->AppendByte(FX_GETBYTEOFFSET8(offset)) < 0) + return -1; + if (pFile->AppendByte(FX_GETBYTEOFFSET0(offset)) < 0) + return -1; + if (pFile->AppendByte(0) < 0) + return -1; + return 0; +} + class CPDF_FlateEncoder { public: CPDF_FlateEncoder(); @@ -433,6 +503,9 @@ CPDF_Encryptor::~CPDF_Encryptor() { FX_Free(m_pData); } } + +} // namespace + CPDF_ObjectStream::CPDF_ObjectStream() : m_dwObjNum(0), m_index(0) {} FX_BOOL CPDF_ObjectStream::Start() { m_ObjNumArray.RemoveAll(); @@ -582,41 +655,7 @@ int32_t CPDF_XRefStream::CompressIndirectObject(FX_DWORD dwObjNum, } return EndObjectStream(pCreator); } -static void _AppendIndex0(CFX_ByteTextBuf& buffer, - FX_BOOL bFirstObject = TRUE) { - buffer.AppendByte(0); - buffer.AppendByte(0); - buffer.AppendByte(0); - buffer.AppendByte(0); - buffer.AppendByte(0); - if (bFirstObject) { - buffer.AppendByte(0xFF); - buffer.AppendByte(0xFF); - } else { - buffer.AppendByte(0); - buffer.AppendByte(0); - } -} -static void _AppendIndex1(CFX_ByteTextBuf& buffer, FX_FILESIZE offset) { - buffer.AppendByte(1); - buffer.AppendByte(FX_GETBYTEOFFSET24(offset)); - buffer.AppendByte(FX_GETBYTEOFFSET16(offset)); - buffer.AppendByte(FX_GETBYTEOFFSET8(offset)); - buffer.AppendByte(FX_GETBYTEOFFSET0(offset)); - buffer.AppendByte(0); - buffer.AppendByte(0); -} -static void _AppendIndex2(CFX_ByteTextBuf& buffer, - FX_DWORD objnum, - int32_t index) { - buffer.AppendByte(2); - buffer.AppendByte(FX_GETBYTEOFFSET24(objnum)); - buffer.AppendByte(FX_GETBYTEOFFSET16(objnum)); - buffer.AppendByte(FX_GETBYTEOFFSET8(objnum)); - buffer.AppendByte(FX_GETBYTEOFFSET0(objnum)); - buffer.AppendByte(FX_GETBYTEOFFSET8(index)); - buffer.AppendByte(FX_GETBYTEOFFSET0(index)); -} + int32_t CPDF_XRefStream::EndObjectStream(CPDF_Creator* pCreator, FX_BOOL bEOF) { FX_FILESIZE objOffset = 0; if (bEOF) { @@ -633,7 +672,7 @@ int32_t CPDF_XRefStream::EndObjectStream(CPDF_Creator* pCreator, FX_BOOL bEOF) { int32_t iSeg = m_IndexArray.GetSize() / 2; if (!(pCreator->m_dwFlags & FPDFCREATE_INCREMENTAL)) { if (m_dwTempObjNum == 0) { - _AppendIndex0(m_Buffer); + AppendIndex0(m_Buffer, true); m_dwTempObjNum++; } FX_DWORD end_num = m_IndexArray.GetAt((iSeg - 1) * 2) + @@ -644,12 +683,12 @@ int32_t CPDF_XRefStream::EndObjectStream(CPDF_Creator* pCreator, FX_BOOL bEOF) { if (offset) { if (index >= iSize || m_dwTempObjNum != m_ObjStream.m_ObjNumArray[index]) { - _AppendIndex1(m_Buffer, *offset); + AppendIndex1(m_Buffer, *offset); } else { - _AppendIndex2(m_Buffer, dwObjStmNum, index++); + AppendIndex2(m_Buffer, dwObjStmNum, index++); } } else { - _AppendIndex0(m_Buffer, FALSE); + AppendIndex0(m_Buffer, false); } } if (iSize > 0 && bEOF) { @@ -668,14 +707,14 @@ int32_t CPDF_XRefStream::EndObjectStream(CPDF_Creator* pCreator, FX_BOOL bEOF) { FX_DWORD end = m_IndexArray.ElementAt(i * 2 + 1) + start; for (FX_DWORD m = start; m < end; m++) { if (j >= iSize || m != m_ObjStream.m_ObjNumArray.ElementAt(j)) { - _AppendIndex1(m_Buffer, pCreator->m_ObjectOffset[m]); + AppendIndex1(m_Buffer, pCreator->m_ObjectOffset[m]); } else { - _AppendIndex2(m_Buffer, dwObjStmNum, j++); + AppendIndex2(m_Buffer, dwObjStmNum, j++); } } } if (iSize > 0 && bEOF) { - _AppendIndex1(m_Buffer, objOffset); + AppendIndex1(m_Buffer, objOffset); m_IndexArray.Add(dwObjStmNum); m_IndexArray.Add(1); iSeg += 1; @@ -698,13 +737,13 @@ FX_BOOL CPDF_XRefStream::GenerateXRefStream(CPDF_Creator* pCreator, for (; m_dwTempObjNum < pCreator->m_dwLastObjNum; m_dwTempObjNum++) { FX_FILESIZE* offset = pCreator->m_ObjectOffset.GetPtrAt(m_dwTempObjNum); if (offset) { - _AppendIndex1(m_Buffer, *offset); + AppendIndex1(m_Buffer, *offset); } else { - _AppendIndex0(m_Buffer, FALSE); + AppendIndex0(m_Buffer, false); } } } - _AppendIndex1(m_Buffer, offset_tmp); + AppendIndex1(m_Buffer, offset_tmp); FX_FILESIZE& offset = pCreator->m_Offset; int32_t len = pFile->AppendDWord(objnum); if (len < 0) { @@ -832,13 +871,13 @@ FX_BOOL CPDF_XRefStream::End(CPDF_Creator* pCreator, FX_BOOL bEOF) { } FX_BOOL CPDF_XRefStream::EndXRefStream(CPDF_Creator* pCreator) { if (!(pCreator->m_dwFlags & FPDFCREATE_INCREMENTAL)) { - _AppendIndex0(m_Buffer); + AppendIndex0(m_Buffer, true); for (FX_DWORD i = 1; i < pCreator->m_dwLastObjNum + 1; i++) { FX_FILESIZE* offset = pCreator->m_ObjectOffset.GetPtrAt(i); if (offset) { - _AppendIndex1(m_Buffer, *offset); + AppendIndex1(m_Buffer, *offset); } else { - _AppendIndex0(m_Buffer, FALSE); + AppendIndex0(m_Buffer, false); } } } else { @@ -847,7 +886,7 @@ FX_BOOL CPDF_XRefStream::EndXRefStream(CPDF_Creator* pCreator) { FX_DWORD start = m_IndexArray.ElementAt(i * 2); FX_DWORD end = m_IndexArray.ElementAt(i * 2 + 1) + start; for (FX_DWORD j = start; j < end; j++) { - _AppendIndex1(m_Buffer, pCreator->m_ObjectOffset[j]); + AppendIndex1(m_Buffer, pCreator->m_ObjectOffset[j]); } } } @@ -908,17 +947,7 @@ CPDF_Creator::~CPDF_Creator() { } Clear(); } -static FX_BOOL _IsXRefNeedEnd(CPDF_XRefStream* pXRef, FX_DWORD flag) { - if (!(flag & FPDFCREATE_INCREMENTAL)) { - return FALSE; - } - int32_t iSize = pXRef->m_IndexArray.GetSize() / 2; - int32_t iCount = 0; - for (int32_t i = 0; i < iSize; i++) { - iCount += pXRef->m_IndexArray.ElementAt(i * 2 + 1); - } - return (iCount >= PDF_XREFSTREAM_MAXSIZE); -} + int32_t CPDF_Creator::WriteIndirectObjectToStream(const CPDF_Object* pObj) { if (!m_pXRefStream) return 1; @@ -933,10 +962,11 @@ int32_t CPDF_Creator::WriteIndirectObjectToStream(const CPDF_Object* pObj) { return 1; CPDF_Dictionary* pDict = pObj->GetDict(); - if (pObj->IsStream()) + if (pObj->IsStream()) { if (pDict && pDict->GetString("Type") == "XRef") return 0; return 1; + } if (pDict) { if (pDict == m_pDocument->m_pRootDict || pDict == m_pEncryptDict) @@ -950,7 +980,7 @@ int32_t CPDF_Creator::WriteIndirectObjectToStream(const CPDF_Object* pObj) { m_pXRefStream->AddObjectNumberToIndexArray(objnum); if (m_pXRefStream->CompressIndirectObject(objnum, pObj, this) < 0) return -1; - if (!_IsXRefNeedEnd(m_pXRefStream, m_dwFlags)) + if (!IsXRefNeedEnd(m_pXRefStream, m_dwFlags)) return 0; if (!m_pXRefStream->End(this)) return -1; @@ -970,7 +1000,7 @@ int32_t CPDF_Creator::WriteIndirectObjectToStream(FX_DWORD objnum, if (iRet < 1) { return iRet; } - if (!_IsXRefNeedEnd(m_pXRefStream, m_dwFlags)) { + if (!IsXRefNeedEnd(m_pXRefStream, m_dwFlags)) { return 0; } if (!m_pXRefStream->End(this)) { @@ -986,7 +1016,7 @@ int32_t CPDF_Creator::AppendObjectNumberToXRef(FX_DWORD objnum) { return 1; } m_pXRefStream->AddObjectNumberToIndexArray(objnum); - if (!_IsXRefNeedEnd(m_pXRefStream, m_dwFlags)) { + if (!IsXRefNeedEnd(m_pXRefStream, m_dwFlags)) { return 0; } if (!m_pXRefStream->End(this)) { @@ -1723,41 +1753,7 @@ int32_t CPDF_Creator::WriteDoc_Stage3(IFX_Pause* pPause) { } return m_iStage; } -static int32_t _OutPutIndex(CFX_FileBufferArchive* pFile, FX_FILESIZE offset) { - FXSYS_assert(pFile); - if (sizeof(offset) > 4) { - if (FX_GETBYTEOFFSET32(offset)) { - if (pFile->AppendByte(FX_GETBYTEOFFSET56(offset)) < 0) { - return -1; - } - if (pFile->AppendByte(FX_GETBYTEOFFSET48(offset)) < 0) { - return -1; - } - if (pFile->AppendByte(FX_GETBYTEOFFSET40(offset)) < 0) { - return -1; - } - if (pFile->AppendByte(FX_GETBYTEOFFSET32(offset)) < 0) { - return -1; - } - } - } - if (pFile->AppendByte(FX_GETBYTEOFFSET24(offset)) < 0) { - return -1; - } - if (pFile->AppendByte(FX_GETBYTEOFFSET16(offset)) < 0) { - return -1; - } - if (pFile->AppendByte(FX_GETBYTEOFFSET8(offset)) < 0) { - return -1; - } - if (pFile->AppendByte(FX_GETBYTEOFFSET0(offset)) < 0) { - return -1; - } - if (pFile->AppendByte(0) < 0) { - return -1; - } - return 0; -} + int32_t CPDF_Creator::WriteDoc_Stage4(IFX_Pause* pPause) { FXSYS_assert(m_iStage >= 90); if ((m_dwFlags & FPDFCREATE_OBJECTSTREAM) == 0) { @@ -1914,7 +1910,7 @@ int32_t CPDF_Creator::WriteDoc_Stage4(IFX_Pause* pPause) { if (!offset) { continue; } - _OutPutIndex(&m_File, *offset); + OutputIndex(&m_File, *offset); } } else { int count = m_NewObjNumArray.GetSize(); @@ -1940,7 +1936,7 @@ int32_t CPDF_Creator::WriteDoc_Stage4(IFX_Pause* pPause) { for (i = 0; i < count; i++) { FX_DWORD objnum = m_NewObjNumArray.ElementAt(i); FX_FILESIZE offset = m_ObjectOffset[objnum]; - _OutPutIndex(&m_File, offset); + OutputIndex(&m_File, offset); } } if (m_File.AppendString("\r\nendstream") < 0) { @@ -1999,18 +1995,16 @@ void CPDF_Creator::InitID(FX_BOOL bDefault) { CPDF_Array* pOldIDArray = m_pParser ? m_pParser->GetIDArray() : NULL; FX_BOOL bNewId = !m_pIDArray; if (!m_pIDArray) { - FX_DWORD* pBuffer = NULL; m_pIDArray = new CPDF_Array; CPDF_Object* pID1 = pOldIDArray ? pOldIDArray->GetElement(0) : NULL; if (pID1) { m_pIDArray->Add(pID1->Clone()); } else { - pBuffer = FX_Alloc(FX_DWORD, 4); - PDF_GenerateFileID((FX_DWORD)(uintptr_t) this, m_dwLastObjNum, pBuffer); - CFX_ByteStringC bsBuffer((const uint8_t*)pBuffer, 4 * sizeof(FX_DWORD)); + std::vector<uint8_t> buffer = + PDF_GenerateFileID((FX_DWORD)(uintptr_t) this, m_dwLastObjNum); + CFX_ByteStringC bsBuffer(buffer.data(), buffer.size()); m_pIDArray->Add(new CPDF_String(bsBuffer, TRUE), m_pDocument); } - FX_Free(pBuffer); } if (!bDefault) { return; @@ -2021,11 +2015,10 @@ void CPDF_Creator::InitID(FX_BOOL bDefault) { m_pIDArray->Add(pID2->Clone()); return; } - FX_DWORD* pBuffer = FX_Alloc(FX_DWORD, 4); - PDF_GenerateFileID((FX_DWORD)(uintptr_t) this, m_dwLastObjNum, pBuffer); - CFX_ByteStringC bsBuffer((const uint8_t*)pBuffer, 4 * sizeof(FX_DWORD)); + std::vector<uint8_t> buffer = + PDF_GenerateFileID((FX_DWORD)(uintptr_t) this, m_dwLastObjNum); + CFX_ByteStringC bsBuffer(buffer.data(), buffer.size()); m_pIDArray->Add(new CPDF_String(bsBuffer, TRUE), m_pDocument); - FX_Free(pBuffer); return; } m_pIDArray->Add(m_pIDArray->GetElement(0)->Clone()); diff --git a/core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp b/core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp index 99d0c5adc0..cfc581f4c8 100644 --- a/core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp +++ b/core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp @@ -4,16 +4,79 @@ // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com -#include "pageint.h" +#include "core/src/fpdfapi/fpdf_page/pageint.h" #include "core/include/fpdfapi/fpdf_module.h" #include "core/include/fpdfapi/fpdf_page.h" #include "core/include/fpdfapi/fpdf_serial.h" -#define REQUIRE_PARAMS(count) \ - if (m_ParamCount != count) { \ - return; \ +namespace { + +struct _FX_BSTR { + const FX_CHAR* m_Ptr; + int m_Size; +}; +#define _FX_BSTRC(str) \ + { str, sizeof(str) - 1 } + +struct PDF_AbbrPairs { + _FX_BSTR full_name; + _FX_BSTR abbr; +}; + +const PDF_AbbrPairs PDF_InlineKeyAbbr[] = { + {_FX_BSTRC("BitsPerComponent"), _FX_BSTRC("BPC")}, + {_FX_BSTRC("ColorSpace"), _FX_BSTRC("CS")}, + {_FX_BSTRC("Decode"), _FX_BSTRC("D")}, + {_FX_BSTRC("DecodeParms"), _FX_BSTRC("DP")}, + {_FX_BSTRC("Filter"), _FX_BSTRC("F")}, + {_FX_BSTRC("Height"), _FX_BSTRC("H")}, + {_FX_BSTRC("ImageMask"), _FX_BSTRC("IM")}, + {_FX_BSTRC("Interpolate"), _FX_BSTRC("I")}, + {_FX_BSTRC("Width"), _FX_BSTRC("W")}, +}; + +const PDF_AbbrPairs PDF_InlineValueAbbr[] = { + {_FX_BSTRC("DeviceGray"), _FX_BSTRC("G")}, + {_FX_BSTRC("DeviceRGB"), _FX_BSTRC("RGB")}, + {_FX_BSTRC("DeviceCMYK"), _FX_BSTRC("CMYK")}, + {_FX_BSTRC("Indexed"), _FX_BSTRC("I")}, + {_FX_BSTRC("ASCIIHexDecode"), _FX_BSTRC("AHx")}, + {_FX_BSTRC("ASCII85Decode"), _FX_BSTRC("A85")}, + {_FX_BSTRC("LZWDecode"), _FX_BSTRC("LZW")}, + {_FX_BSTRC("FlateDecode"), _FX_BSTRC("Fl")}, + {_FX_BSTRC("RunLengthDecode"), _FX_BSTRC("RL")}, + {_FX_BSTRC("CCITTFaxDecode"), _FX_BSTRC("CCF")}, + {_FX_BSTRC("DCTDecode"), _FX_BSTRC("DCT")}, +}; + +CFX_ByteStringC PDF_FindFullName(const PDF_AbbrPairs* table, + size_t count, + const CFX_ByteStringC& abbr) { + for (size_t i = 0; i < count; ++i) { + if (abbr.GetLength() != table[i].abbr.m_Size) + continue; + if (memcmp(abbr.GetPtr(), table[i].abbr.m_Ptr, abbr.GetLength())) + continue; + return CFX_ByteStringC(table[i].full_name.m_Ptr, table[i].full_name.m_Size); } + return CFX_ByteStringC(); +} + +CFX_ByteStringC PDF_FindAbbrName(const PDF_AbbrPairs* table, + size_t count, + const CFX_ByteStringC& name) { + for (size_t i = 0; i < count; ++i) { + if (name.GetLength() != table[i].full_name.m_Size) + continue; + if (memcmp(name.GetPtr(), table[i].full_name.m_Ptr, name.GetLength())) + continue; + return CFX_ByteStringC(table[i].abbr.m_Ptr, table[i].abbr.m_Size); + } + return CFX_ByteStringC(); +} + +} // namespace CPDF_StreamContentParser::CPDF_StreamContentParser( CPDF_Document* pDocument, @@ -361,7 +424,7 @@ FX_BOOL CPDF_StreamContentParser::OnOperator(const FX_CHAR* op) { while (i < 4) { opid <<= 8; i++; - }; + } int low = 0, high = sizeof g_OpCodes / sizeof(OpCode) - 1; while (low <= high) { int middle = (low + high) / 2; @@ -431,60 +494,8 @@ void CPDF_StreamContentParser::Handle_BeginMarkedContent() { CFX_ByteString tag = GetString(0); m_CurContentMark.GetModify()->AddMark(tag, NULL, FALSE); } -struct _FX_BSTR { - const FX_CHAR* m_Ptr; - int m_Size; -}; -#define _FX_BSTRC(str) \ - { str, sizeof(str) - 1 } -const _FX_BSTR _PDF_InlineKeyAbbr[] = { - _FX_BSTRC("BitsPerComponent"), - _FX_BSTRC("BPC"), - _FX_BSTRC("ColorSpace"), - _FX_BSTRC("CS"), - _FX_BSTRC("Decode"), - _FX_BSTRC("D"), - _FX_BSTRC("DecodeParms"), - _FX_BSTRC("DP"), - _FX_BSTRC("Filter"), - _FX_BSTRC("F"), - _FX_BSTRC("Height"), - _FX_BSTRC("H"), - _FX_BSTRC("ImageMask"), - _FX_BSTRC("IM"), - _FX_BSTRC("Interpolate"), - _FX_BSTRC("I"), - _FX_BSTRC("Width"), - _FX_BSTRC("W"), -}; -const _FX_BSTR _PDF_InlineValueAbbr[] = { - _FX_BSTRC("DeviceGray"), _FX_BSTRC("G"), - _FX_BSTRC("DeviceRGB"), _FX_BSTRC("RGB"), - _FX_BSTRC("DeviceCMYK"), _FX_BSTRC("CMYK"), - _FX_BSTRC("Indexed"), _FX_BSTRC("I"), - _FX_BSTRC("ASCIIHexDecode"), _FX_BSTRC("AHx"), - _FX_BSTRC("ASCII85Decode"), _FX_BSTRC("A85"), - _FX_BSTRC("LZWDecode"), _FX_BSTRC("LZW"), - _FX_BSTRC("FlateDecode"), _FX_BSTRC("Fl"), - _FX_BSTRC("RunLengthDecode"), _FX_BSTRC("RL"), - _FX_BSTRC("CCITTFaxDecode"), _FX_BSTRC("CCF"), - _FX_BSTRC("DCTDecode"), _FX_BSTRC("DCT"), -}; -static CFX_ByteStringC _PDF_FindFullName(const _FX_BSTR* table, - int count, - const CFX_ByteStringC& abbr) { - int i = 0; - while (i < count) { - if (abbr.GetLength() == table[i + 1].m_Size && - FXSYS_memcmp(abbr.GetPtr(), table[i + 1].m_Ptr, abbr.GetLength()) == - 0) { - return CFX_ByteStringC(table[i].m_Ptr, table[i].m_Size); - } - i += 2; - } - return CFX_ByteStringC(); -} -void _PDF_ReplaceAbbr(CPDF_Object* pObj) { + +void PDF_ReplaceAbbr(CPDF_Object* pObj) { switch (pObj->GetType()) { case PDFOBJ_DICTIONARY: { CPDF_Dictionary* pDict = pObj->AsDictionary(); @@ -492,9 +503,8 @@ void _PDF_ReplaceAbbr(CPDF_Object* pObj) { while (pos) { CFX_ByteString key; CPDF_Object* value = pDict->GetNextElement(pos, key); - CFX_ByteStringC fullname = _PDF_FindFullName( - _PDF_InlineKeyAbbr, sizeof _PDF_InlineKeyAbbr / sizeof(_FX_BSTR), - key); + CFX_ByteStringC fullname = PDF_FindFullName( + PDF_InlineKeyAbbr, FX_ArraySize(PDF_InlineKeyAbbr), key); if (!fullname.IsEmpty()) { pDict->ReplaceKey(key, fullname); key = fullname; @@ -502,14 +512,13 @@ void _PDF_ReplaceAbbr(CPDF_Object* pObj) { if (value->IsName()) { CFX_ByteString name = value->GetString(); - fullname = _PDF_FindFullName( - _PDF_InlineValueAbbr, - sizeof _PDF_InlineValueAbbr / sizeof(_FX_BSTR), name); + fullname = PDF_FindFullName(PDF_InlineValueAbbr, + FX_ArraySize(PDF_InlineValueAbbr), name); if (!fullname.IsEmpty()) { pDict->SetAtName(key, fullname); } } else { - _PDF_ReplaceAbbr(value); + PDF_ReplaceAbbr(value); } } break; @@ -520,35 +529,21 @@ void _PDF_ReplaceAbbr(CPDF_Object* pObj) { CPDF_Object* pElement = pArray->GetElement(i); if (pElement->IsName()) { CFX_ByteString name = pElement->GetString(); - CFX_ByteStringC fullname = _PDF_FindFullName( - _PDF_InlineValueAbbr, - sizeof _PDF_InlineValueAbbr / sizeof(_FX_BSTR), name); + CFX_ByteStringC fullname = PDF_FindFullName( + PDF_InlineValueAbbr, FX_ArraySize(PDF_InlineValueAbbr), name); if (!fullname.IsEmpty()) { pArray->SetAt(i, new CPDF_Name(fullname)); } } else { - _PDF_ReplaceAbbr(pElement); + PDF_ReplaceAbbr(pElement); } } break; } } } -static CFX_ByteStringC _PDF_FindAbbrName(const _FX_BSTR* table, - int count, - const CFX_ByteStringC& fullName) { - int i = 0; - while (i < count) { - if (fullName.GetLength() == table[i].m_Size && - FXSYS_memcmp(fullName.GetPtr(), table[i].m_Ptr, fullName.GetLength()) == - 0) { - return CFX_ByteStringC(table[i + 1].m_Ptr, table[i + 1].m_Size); - } - i += 2; - } - return CFX_ByteStringC(); -} -void _PDF_ReplaceFull(CPDF_Object* pObj) { + +void PDF_ReplaceFull(CPDF_Object* pObj) { switch (pObj->GetType()) { case PDFOBJ_DICTIONARY: { CPDF_Dictionary* pDict = pObj->AsDictionary(); @@ -556,23 +551,21 @@ void _PDF_ReplaceFull(CPDF_Object* pObj) { while (pos) { CFX_ByteString key; CPDF_Object* value = pDict->GetNextElement(pos, key); - CFX_ByteStringC abbrName = _PDF_FindAbbrName( - _PDF_InlineKeyAbbr, sizeof(_PDF_InlineKeyAbbr) / sizeof(_FX_BSTR), - key); + CFX_ByteStringC abbrName = PDF_FindAbbrName( + PDF_InlineKeyAbbr, FX_ArraySize(PDF_InlineKeyAbbr), key); if (!abbrName.IsEmpty()) { pDict->ReplaceKey(key, abbrName); key = abbrName; } if (value->IsName()) { CFX_ByteString name = value->GetString(); - abbrName = _PDF_FindAbbrName( - _PDF_InlineValueAbbr, - sizeof(_PDF_InlineValueAbbr) / sizeof(_FX_BSTR), name); + abbrName = PDF_FindAbbrName(PDF_InlineValueAbbr, + FX_ArraySize(PDF_InlineValueAbbr), name); if (!abbrName.IsEmpty()) { pDict->SetAtName(key, abbrName); } } else { - _PDF_ReplaceFull(value); + PDF_ReplaceFull(value); } } break; @@ -583,14 +576,13 @@ void _PDF_ReplaceFull(CPDF_Object* pObj) { CPDF_Object* pElement = pArray->GetElement(i); if (pElement->IsName()) { CFX_ByteString name = pElement->GetString(); - CFX_ByteStringC abbrName = _PDF_FindAbbrName( - _PDF_InlineValueAbbr, - sizeof _PDF_InlineValueAbbr / sizeof(_FX_BSTR), name); + CFX_ByteStringC abbrName = PDF_FindAbbrName( + PDF_InlineValueAbbr, FX_ArraySize(PDF_InlineValueAbbr), name); if (!abbrName.IsEmpty()) { pArray->SetAt(i, new CPDF_Name(abbrName)); } } else { - _PDF_ReplaceFull(pElement); + PDF_ReplaceFull(pElement); } } break; @@ -893,7 +885,9 @@ void CPDF_StreamContentParser::Handle_SetLineCap() { (CFX_GraphStateData::LineCap)GetInteger(0); } void CPDF_StreamContentParser::Handle_SetCMYKColor_Fill() { - REQUIRE_PARAMS(4); + if (m_ParamCount != 4) + return; + FX_FLOAT values[4]; for (int i = 0; i < 4; i++) { values[i] = GetNumber(3 - i); @@ -902,7 +896,9 @@ void CPDF_StreamContentParser::Handle_SetCMYKColor_Fill() { m_pCurStates->m_ColorState.SetFillColor(pCS, values, 4); } void CPDF_StreamContentParser::Handle_SetCMYKColor_Stroke() { - REQUIRE_PARAMS(4); + if (m_ParamCount != 4) + return; + FX_FLOAT values[4]; for (int i = 0; i < 4; i++) { values[i] = GetNumber(3 - i); @@ -911,14 +907,18 @@ void CPDF_StreamContentParser::Handle_SetCMYKColor_Stroke() { m_pCurStates->m_ColorState.SetStrokeColor(pCS, values, 4); } void CPDF_StreamContentParser::Handle_LineTo() { - REQUIRE_PARAMS(2); + if (m_ParamCount != 2) + return; + if (m_Options.m_bTextOnly) { return; } AddPathPoint(GetNumber(1), GetNumber(0), FXPT_LINETO); } void CPDF_StreamContentParser::Handle_MoveTo() { - REQUIRE_PARAMS(2); + if (m_ParamCount != 2) + return; + if (m_Options.m_bTextOnly) { m_pSyntax->SkipPathObject(); return; @@ -970,7 +970,9 @@ void CPDF_StreamContentParser::AddPathRect(FX_FLOAT x, AddPathPoint(x, y, FXPT_LINETO | FXPT_CLOSEFIGURE); } void CPDF_StreamContentParser::Handle_SetRGBColor_Fill() { - REQUIRE_PARAMS(3); + if (m_ParamCount != 3) + return; + FX_FLOAT values[3]; for (int i = 0; i < 3; i++) { values[i] = GetNumber(2 - i); @@ -979,7 +981,9 @@ void CPDF_StreamContentParser::Handle_SetRGBColor_Fill() { m_pCurStates->m_ColorState.SetFillColor(pCS, values, 3); } void CPDF_StreamContentParser::Handle_SetRGBColor_Stroke() { - REQUIRE_PARAMS(3); + if (m_ParamCount != 3) + return; + FX_FLOAT values[3]; for (int i = 0; i < 3; i++) { values[i] = GetNumber(2 - i); @@ -1341,7 +1345,7 @@ void CPDF_StreamContentParser::Handle_ShowText_Positioning() { FXSYS_Mul(pArray->GetNumber(i), m_pCurStates->m_TextState.GetFontSize()) / 1000; - }; + } return; } CFX_ByteString* pStrs = new CFX_ByteString[nsegs]; 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 1c4cc24208..8846c023e1 100644 --- a/core/src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp +++ b/core/src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp @@ -4,7 +4,7 @@ // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com -#include "pageint.h" +#include "core/src/fpdfapi/fpdf_page/pageint.h" #include <limits.h> @@ -72,7 +72,7 @@ FX_DWORD CPDF_StreamContentParser::Parse(const uint8_t* pData, } return m_pSyntax->GetPos(); } -void _PDF_ReplaceAbbr(CPDF_Object* pObj); + void CPDF_StreamContentParser::Handle_BeginImage() { FX_FILESIZE savePos = m_pSyntax->GetPos(); CPDF_Dictionary* pDict = new CPDF_Dictionary; @@ -102,7 +102,7 @@ void CPDF_StreamContentParser::Handle_BeginImage() { pDict->SetAt(key, pObj.release()); } } - _PDF_ReplaceAbbr(pDict); + PDF_ReplaceAbbr(pDict); CPDF_Object* pCSObj = NULL; if (pDict->KeyExist("ColorSpace")) { pCSObj = pDict->GetElementValue("ColorSpace"); @@ -933,6 +933,7 @@ void CPDF_ContentParser::Start(CPDF_Page* pPage, CPDF_ParseOptions* pOptions) { m_Status = ToBeContinued; m_InternalStage = PAGEPARSE_STAGE_GETCONTENT; m_CurrentOffset = 0; + CPDF_Object* pContent = pPage->m_pFormDict->GetElementValue("Contents"); if (!pContent) { m_Status = Done; @@ -951,7 +952,6 @@ void CPDF_ContentParser::Start(CPDF_Page* pPage, CPDF_ParseOptions* pOptions) { m_pStreamArray = FX_Alloc(CPDF_StreamAcc*, m_nStreams); } else { m_Status = Done; - return; } } void CPDF_ContentParser::Start(CPDF_Form* pForm, @@ -1006,11 +1006,7 @@ void CPDF_ContentParser::Start(CPDF_Form* pForm, } m_nStreams = 0; m_pSingleStream = new CPDF_StreamAcc; - if (pForm->m_pDocument) { - m_pSingleStream->LoadAllData(pForm->m_pFormStream, FALSE); - } else { - m_pSingleStream->LoadAllData(pForm->m_pFormStream, FALSE); - } + m_pSingleStream->LoadAllData(pForm->m_pFormStream, FALSE); m_pData = (uint8_t*)m_pSingleStream->GetData(); m_Size = m_pSingleStream->GetSize(); m_Status = ToBeContinued; diff --git a/core/src/fpdfapi/fpdf_page/pageint.h b/core/src/fpdfapi/fpdf_page/pageint.h index 6113e5ea4d..69f06881f9 100644 --- a/core/src/fpdfapi/fpdf_page/pageint.h +++ b/core/src/fpdfapi/fpdf_page/pageint.h @@ -449,4 +449,6 @@ class CPDF_PatternCS : public CPDF_ColorSpace { CPDF_CountedColorSpace* m_pCountedBaseCS; }; +void PDF_ReplaceAbbr(CPDF_Object* pObj); + #endif // CORE_SRC_FPDFAPI_FPDF_PAGE_PAGEINT_H_ diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp index c855a7c5c2..d7783e438b 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp @@ -324,9 +324,7 @@ FX_BOOL PDF_DataDecode(const uint8_t* src_buf, CFX_ByteString& ImageEncoding, CPDF_Dictionary*& pImageParms, FX_DWORD last_estimated_size, - FX_BOOL bImageAcc) - -{ + FX_BOOL bImageAcc) { CPDF_Object* pDecoder = pDict ? pDict->GetElementValue("Filter") : nullptr; if (!pDecoder || (!pDecoder->IsArray() && !pDecoder->IsName())) return FALSE; diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp index 010b9b7c47..ad97d1f369 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp @@ -360,13 +360,15 @@ FX_BOOL CPDF_Parser::LoadAllCrossRefV4(FX_FILESIZE xrefpos) { if (!m_pTrailer) { return FALSE; } + int32_t xrefsize = GetDirectInteger(m_pTrailer, "Size"); if (xrefsize <= 0 || xrefsize > kMaxXRefSize) { return FALSE; } m_ObjectInfo[0].pos = 0; m_V5Type.SetSize(xrefsize); - CFX_FileSizeArray CrossRefList, XRefStreamList; + CFX_FileSizeArray CrossRefList; + CFX_FileSizeArray XRefStreamList; CrossRefList.Add(xrefpos); XRefStreamList.Add(GetDirectInteger(m_pTrailer, "XRefStm")); @@ -1416,9 +1418,8 @@ CPDF_Object* CPDF_Parser::ParseIndirectObjectAt(CPDF_IndirectObjects* pObjList, } m_Syntax.RestorePos(SavedPos); if (pObj) { - if (!objnum) { + if (!objnum) pObj->m_ObjNum = parser_objnum; - } pObj->m_GenNum = parser_gennum; } return pObj; @@ -1582,8 +1583,9 @@ FX_DWORD CPDF_Parser::StartAsynParse(IFX_FileRead* pFileAccess, if (bLoadV4) { m_pTrailer = LoadTrailerV4(); if (!m_pTrailer) { - return FALSE; + return PDFPARSE_ERROR_SUCCESS; } + int32_t xrefsize = GetDirectInteger(m_pTrailer, "Size"); if (xrefsize > 0) { m_ObjectInfo[0].pos = 0; @@ -2759,7 +2761,6 @@ class CPDF_DataAvail final : public IPDF_DataAvail { FX_BOOL LoadAllXref(IFX_DownloadHints* pHints); FX_BOOL LoadAllFile(IFX_DownloadHints* pHints); int32_t CheckLinearizedData(IFX_DownloadHints* pHints); - FX_BOOL CheckFileResources(IFX_DownloadHints* pHints); FX_BOOL CheckPageAnnots(int iPage, IFX_DownloadHints* pHints); FX_BOOL CheckLinearizedFirstPage(int iPage, IFX_DownloadHints* pHints); @@ -4296,7 +4297,7 @@ int CPDF_DataAvail::CheckLinearizedData(IFX_DownloadHints* pHints) { pHints->AddSegment(m_dwLastXRefOffset, data_size.ValueOrDie()); return DataNotAvailable; } - FX_DWORD dwRet = (m_pDocument->GetParser())->LoadLinearizedMainXRefTable(); + FX_DWORD dwRet = m_pDocument->GetParser()->LoadLinearizedMainXRefTable(); m_bMainXRefLoadTried = TRUE; if (dwRet != PDFPARSE_ERROR_SUCCESS) { return DataError; @@ -4929,6 +4930,7 @@ int32_t CPDF_HintTables::CheckPage(int index, IFX_DownloadHints* pHints) { } return IPDF_DataAvail::DataAvailable; } + FX_BOOL CPDF_HintTables::LoadHintStream(CPDF_Stream* pHintStream) { if (!pHintStream || !m_pLinearizedDict) return FALSE; @@ -4954,6 +4956,7 @@ FX_BOOL CPDF_HintTables::LoadHintStream(CPDF_Stream* pHintStream) { ReadSharedObjHintTable(&bs, pdfium::base::checked_cast<FX_DWORD>( shared_hint_table_offset)); } + int CPDF_HintTables::ReadPrimaryHintStreamOffset() const { if (!m_pLinearizedDict) return -1; diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser_unittest.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser_unittest.cpp index 3014ca280e..e091f14cbe 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser_unittest.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser_unittest.cpp @@ -20,18 +20,6 @@ bool CompareArray(const CFX_ArrayTemplate<TYPE>& array1, return false; return true; } -template bool CompareArray<uint8_t>(const CFX_ByteArray&, - const uint8_t*, - size_t); -template bool CompareArray<FX_WORD>(const CFX_WordArray&, - const FX_WORD*, - size_t); -template bool CompareArray<FX_DWORD>(const CFX_DWordArray&, - const FX_DWORD*, - size_t); -template bool CompareArray<FX_FILESIZE>(const CFX_FileSizeArray&, - const FX_FILESIZE*, - size_t); // Provide a way to read test data from a buffer instead of a file. class CFX_TestBufferRead : public IFX_FileRead { |