X-Git-Url: http://downloads.foxitsoftware.com/web/?p=pdfium.git;a=blobdiff_plain;f=core%2Fsrc%2Ffpdfapi%2Ffpdf_parser%2Ffpdf_parser_parser.cpp;h=4f81be1d3f949551e1577642c30f963a51f25533;hp=f14d7d5c37034c3a206f63563ae27d5443884b93;hb=85d5c4af4a9546970b34dd413c473d10fef8534b;hpb=2526930205ca2ef0a06ec32644d41d4548d45d80 diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp index f14d7d5..4f81be1 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp @@ -4,6 +4,7 @@ // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +#include #include #include @@ -1732,9 +1733,7 @@ CPDF_SyntaxParser::CPDF_SyntaxParser() { m_bFileStream = FALSE; } CPDF_SyntaxParser::~CPDF_SyntaxParser() { - if (m_pFileBuf) { - FX_Free(m_pFileBuf); - } + FX_Free(m_pFileBuf); } FX_BOOL CPDF_SyntaxParser::GetCharAt(FX_FILESIZE pos, uint8_t& ch) { FX_FILESIZE save_pos = m_Pos; @@ -2434,7 +2433,7 @@ unsigned int CPDF_SyntaxParser::ReadEOLMarkers(FX_FILESIZE pos) { unsigned char byte1 = 0; unsigned char byte2 = 0; GetCharAt(pos, byte1); - GetCharAt(pos+1, byte2); + GetCharAt(pos + 1, byte2); unsigned int markers = 0; if (byte1 == '\r' && byte2 == '\n') { markers = 2; @@ -2454,7 +2453,7 @@ CPDF_Stream* CPDF_SyntaxParser::ReadStream(CPDF_Dictionary* pDict, ((CPDF_Reference*)pLenObj)->GetRefObjNum() != objnum))) { len = pLenObj->GetInteger(); } - //Check whether end of line markers follow the keyword 'stream'. + // Check whether end of line markers follow the keyword 'stream'. unsigned int numMarkers = ReadEOLMarkers(m_Pos); m_Pos += numMarkers; FX_FILESIZE streamStartPos = m_Pos; @@ -2463,7 +2462,8 @@ CPDF_Stream* CPDF_SyntaxParser::ReadStream(CPDF_Dictionary* pDict, } const unsigned int ENDSTREAM_LEN = sizeof("endstream") - 1; const unsigned int ENDOBJ_LEN = sizeof("endobj") - 1; - CPDF_CryptoHandler* pCryptoHandler = objnum == (FX_DWORD)m_MetadataObjnum ? nullptr : m_pCryptoHandler; + CPDF_CryptoHandler* pCryptoHandler = + objnum == (FX_DWORD)m_MetadataObjnum ? nullptr : m_pCryptoHandler; if (!pCryptoHandler) { FX_BOOL bSearchForKeyword = TRUE; unsigned int prevMarkers = 0; @@ -2483,27 +2483,28 @@ CPDF_Stream* CPDF_SyntaxParser::ReadStream(CPDF_Dictionary* pDict, } } if (bSearchForKeyword) { - //If len is not available, len needs to be calculated - //by searching the keywords "endstream" or "endobj". + // If len is not available, len needs to be calculated + // by searching the keywords "endstream" or "endobj". m_Pos = streamStartPos; FX_FILESIZE endStreamOffset = 0; while (endStreamOffset >= 0) { endStreamOffset = FindTag(FX_BSTRC("endstream"), 0); if (endStreamOffset < 0) { - //Can't find any "endstream". + // Can't find any "endstream". break; } prevMarkers = ReadEOLMarkers(streamStartPos + endStreamOffset - 1); - nextMarkers = ReadEOLMarkers(streamStartPos + endStreamOffset + ENDSTREAM_LEN); + nextMarkers = + ReadEOLMarkers(streamStartPos + endStreamOffset + ENDSTREAM_LEN); if (prevMarkers != 0 && nextMarkers != 0) { - //Stop searching when the keyword "endstream" is found. + // Stop searching when the keyword "endstream" is found. break; } else { unsigned char ch = 0x00; GetCharAt(streamStartPos + endStreamOffset + ENDSTREAM_LEN, ch); if (ch == 0x09 || ch == 0x20) { //"endstream" is treated as a keyword - //when it is followed by a tab or whitespace + // when it is followed by a tab or whitespace break; } } @@ -2514,26 +2515,27 @@ CPDF_Stream* CPDF_SyntaxParser::ReadStream(CPDF_Dictionary* pDict, while (endObjOffset >= 0) { endObjOffset = FindTag(FX_BSTRC("endobj"), 0); if (endObjOffset < 0) { - //Can't find any "endobj". + // Can't find any "endobj". break; } prevMarkers = ReadEOLMarkers(streamStartPos + endObjOffset - 1); - nextMarkers = ReadEOLMarkers(streamStartPos + endObjOffset + ENDOBJ_LEN); + nextMarkers = + ReadEOLMarkers(streamStartPos + endObjOffset + ENDOBJ_LEN); if (prevMarkers != 0 && nextMarkers != 0) { - //Stop searching when the keyword "endobj" is found. + // Stop searching when the keyword "endobj" is found. break; } m_Pos += ENDOBJ_LEN; } if (endStreamOffset < 0 && endObjOffset < 0) { - //Can't find "endstream" or "endobj". + // Can't find "endstream" or "endobj". return nullptr; } if (endStreamOffset < 0 && endObjOffset >= 0) { - //Correct the position of end stream. + // Correct the position of end stream. endStreamOffset = endObjOffset; } else if (endStreamOffset >= 0 && endObjOffset < 0) { - //Correct the position of end obj. + // Correct the position of end obj. endObjOffset = endStreamOffset; } else if (endStreamOffset > endObjOffset) { endStreamOffset = endObjOffset; @@ -2578,18 +2580,15 @@ CPDF_Stream* CPDF_SyntaxParser::ReadStream(CPDF_Dictionary* pDict, streamStartPos = m_Pos; GetNextWord(); numMarkers = ReadEOLMarkers(m_Pos); - if (m_WordSize == ENDOBJ_LEN && numMarkers != 0 && - FXSYS_memcmp(m_WordBuffer, "endobj", ENDOBJ_LEN) == 0) { + if (m_WordSize == ENDOBJ_LEN && numMarkers != 0 && + FXSYS_memcmp(m_WordBuffer, "endobj", ENDOBJ_LEN) == 0) { m_Pos = streamStartPos; } return pStream; } void CPDF_SyntaxParser::InitParser(IFX_FileRead* pFileAccess, FX_DWORD HeaderOffset) { - if (m_pFileBuf) { - FX_Free(m_pFileBuf); - m_pFileBuf = NULL; - } + FX_Free(m_pFileBuf); m_pFileBuf = FX_Alloc(uint8_t, m_BufSize); m_HeaderOffset = HeaderOffset; m_FileLen = pFileAccess->GetSize(); @@ -2812,7 +2811,7 @@ void CPDF_SyntaxParser::GetBinary(uint8_t* buffer, FX_DWORD size) { class CPDF_DataAvail final : public IPDF_DataAvail { public: CPDF_DataAvail(IFX_FileAvail* pFileAvail, IFX_FileRead* pFileRead); - ~CPDF_DataAvail(); + ~CPDF_DataAvail() override; virtual FX_BOOL IsDocAvail(IFX_DownloadHints* pHints) override; @@ -3005,9 +3004,8 @@ class CPDF_DataAvail final : public IPDF_DataAvail { CPDF_PageNode m_pageNodes; - CFX_CMapDWordToDWord* m_pageMapCheckState; - - CFX_CMapDWordToDWord* m_pagesLoadState; + std::set m_pageMapCheckState; + std::set m_pagesLoadState; }; IPDF_DataAvail::IPDF_DataAvail(IFX_FileAvail* pFileAvail, @@ -3065,13 +3063,11 @@ CPDF_DataAvail::CPDF_DataAvail(IFX_FileAvail* pFileAvail, m_pAcroForm = NULL; m_pPageDict = NULL; m_pPageResource = NULL; - m_pageMapCheckState = NULL; m_docStatus = PDF_DATAAVAIL_HEADER; m_parser.m_bOwnFileRead = FALSE; m_bTotalLoadPageTree = FALSE; m_bCurPageDictLoadOK = FALSE; m_bLinearedDataOK = FALSE; - m_pagesLoadState = NULL; } CPDF_DataAvail::~CPDF_DataAvail() { if (m_pLinearized) { @@ -3083,8 +3079,6 @@ CPDF_DataAvail::~CPDF_DataAvail() { if (m_pTrailer) { m_pTrailer->Release(); } - delete m_pageMapCheckState; - delete m_pagesLoadState; int32_t i = 0; int32_t iSize = m_arrayAcroforms.GetSize(); for (i = 0; i < iSize; ++i) { @@ -3519,29 +3513,14 @@ FX_BOOL CPDF_DataAvail::PreparePageItem() { return TRUE; } FX_BOOL CPDF_DataAvail::IsFirstCheck(int iPage) { - if (NULL == m_pageMapCheckState) { - m_pageMapCheckState = new CFX_CMapDWordToDWord(); - } - FX_DWORD dwValue = 0; - if (!m_pageMapCheckState->Lookup(iPage, dwValue)) { - m_pageMapCheckState->SetAt(iPage, 1); - return TRUE; - } - if (dwValue != 0) { + if (m_pageMapCheckState.find(iPage) != m_pageMapCheckState.end()) return FALSE; - } - m_pageMapCheckState->SetAt(iPage, 1); + + m_pageMapCheckState.insert(iPage); return TRUE; } void CPDF_DataAvail::ResetFirstCheck(int iPage) { - if (NULL == m_pageMapCheckState) { - m_pageMapCheckState = new CFX_CMapDWordToDWord(); - } - FX_DWORD dwValue = 1; - if (!m_pageMapCheckState->Lookup(iPage, dwValue)) { - return; - } - m_pageMapCheckState->SetAt(iPage, 0); + m_pageMapCheckState.erase(iPage); } FX_BOOL CPDF_DataAvail::CheckPage(IFX_DownloadHints* pHints) { FX_DWORD iPageObjs = m_PageObjList.GetSize(); @@ -4101,6 +4080,7 @@ FX_BOOL CPDF_DataAvail::CheckTrailerAppend(IFX_DownloadHints* pHints) { } return TRUE; } + FX_BOOL CPDF_DataAvail::CheckTrailer(IFX_DownloadHints* pHints) { int32_t iTrailerSize = (int32_t)(m_Pos + 512 > m_dwFileLen ? m_dwFileLen - m_Pos : 512); @@ -4118,13 +4098,14 @@ FX_BOOL CPDF_DataAvail::CheckTrailer(IFX_DownloadHints* pHints) { CFX_SmartPointer file( FX_CreateMemoryStream(pBuf, (size_t)iSize, FALSE)); m_syntaxParser.InitParser(file.Get(), 0); - CPDF_Object* pTrailer = m_syntaxParser.GetObject(NULL, 0, 0, 0); + CPDF_Object* pTrailer = m_syntaxParser.GetObject(nullptr, 0, 0); if (!pTrailer) { m_Pos += m_syntaxParser.SavePos(); pHints->AddSegment(m_Pos, iTrailerSize); return FALSE; } if (pTrailer->GetType() != PDFOBJ_DICTIONARY) { + pTrailer->Release(); return FALSE; } CPDF_Dictionary* pTrailerDict = pTrailer->GetDict(); @@ -4162,6 +4143,7 @@ FX_BOOL CPDF_DataAvail::CheckTrailer(IFX_DownloadHints* pHints) { pHints->AddSegment(m_Pos, iTrailerSize); return FALSE; } + FX_BOOL CPDF_DataAvail::CheckPage(int32_t iPage, IFX_DownloadHints* pHints) { while (TRUE) { switch (m_docStatus) { @@ -4512,16 +4494,12 @@ FX_BOOL CPDF_DataAvail::IsPageAvail(int32_t iPage, IFX_DownloadHints* pHints) { m_objs_array.RemoveAll(); m_objnum_array.RemoveAll(); } - if (m_pagesLoadState == NULL) { - m_pagesLoadState = new CFX_CMapDWordToDWord(); - } - FX_DWORD dwPageLoad = 0; - if (m_pagesLoadState->Lookup(iPage, dwPageLoad) && dwPageLoad != 0) { + if (m_pagesLoadState.find(iPage) != m_pagesLoadState.end()) { return TRUE; } if (m_bLinearized) { if ((FX_DWORD)iPage == m_dwFirstPageNo) { - m_pagesLoadState->SetAt(iPage, TRUE); + m_pagesLoadState.insert(iPage); return TRUE; } if (!CheckLinearizedData(pHints)) { @@ -4616,7 +4594,7 @@ FX_BOOL CPDF_DataAvail::IsPageAvail(int32_t iPage, IFX_DownloadHints* pHints) { m_bAnnotsLoad = FALSE; m_bCurPageDictLoadOK = FALSE; ResetFirstCheck(iPage); - m_pagesLoadState->SetAt(iPage, TRUE); + m_pagesLoadState.insert(iPage); return TRUE; } FX_BOOL CPDF_DataAvail::CheckResources(IFX_DownloadHints* pHints) {