| OLD | NEW |
| 1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "../../../include/fpdfapi/fpdf_module.h" | 10 #include "../../../include/fpdfapi/fpdf_module.h" |
| (...skipping 2416 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2427 } | 2427 } |
| 2428 if (bTypeOnly) { | 2428 if (bTypeOnly) { |
| 2429 return (CPDF_Object*)PDFOBJ_INVALID; | 2429 return (CPDF_Object*)PDFOBJ_INVALID; |
| 2430 } | 2430 } |
| 2431 return NULL; | 2431 return NULL; |
| 2432 } | 2432 } |
| 2433 unsigned int CPDF_SyntaxParser::ReadEOLMarkers(FX_FILESIZE pos) { | 2433 unsigned int CPDF_SyntaxParser::ReadEOLMarkers(FX_FILESIZE pos) { |
| 2434 unsigned char byte1 = 0; | 2434 unsigned char byte1 = 0; |
| 2435 unsigned char byte2 = 0; | 2435 unsigned char byte2 = 0; |
| 2436 GetCharAt(pos, byte1); | 2436 GetCharAt(pos, byte1); |
| 2437 GetCharAt(pos+1, byte2); | 2437 GetCharAt(pos + 1, byte2); |
| 2438 unsigned int markers = 0; | 2438 unsigned int markers = 0; |
| 2439 if (byte1 == '\r' && byte2 == '\n') { | 2439 if (byte1 == '\r' && byte2 == '\n') { |
| 2440 markers = 2; | 2440 markers = 2; |
| 2441 } else if (byte1 == '\r' || byte1 == '\n') { | 2441 } else if (byte1 == '\r' || byte1 == '\n') { |
| 2442 markers = 1; | 2442 markers = 1; |
| 2443 } | 2443 } |
| 2444 return markers; | 2444 return markers; |
| 2445 } | 2445 } |
| 2446 CPDF_Stream* CPDF_SyntaxParser::ReadStream(CPDF_Dictionary* pDict, | 2446 CPDF_Stream* CPDF_SyntaxParser::ReadStream(CPDF_Dictionary* pDict, |
| 2447 PARSE_CONTEXT* pContext, | 2447 PARSE_CONTEXT* pContext, |
| 2448 FX_DWORD objnum, | 2448 FX_DWORD objnum, |
| 2449 FX_DWORD gennum) { | 2449 FX_DWORD gennum) { |
| 2450 CPDF_Object* pLenObj = pDict->GetElement(FX_BSTRC("Length")); | 2450 CPDF_Object* pLenObj = pDict->GetElement(FX_BSTRC("Length")); |
| 2451 FX_FILESIZE len = -1; | 2451 FX_FILESIZE len = -1; |
| 2452 if (pLenObj && ((pLenObj->GetType() != PDFOBJ_REFERENCE) || | 2452 if (pLenObj && ((pLenObj->GetType() != PDFOBJ_REFERENCE) || |
| 2453 ((((CPDF_Reference*)pLenObj)->GetObjList()) && | 2453 ((((CPDF_Reference*)pLenObj)->GetObjList()) && |
| 2454 ((CPDF_Reference*)pLenObj)->GetRefObjNum() != objnum))) { | 2454 ((CPDF_Reference*)pLenObj)->GetRefObjNum() != objnum))) { |
| 2455 len = pLenObj->GetInteger(); | 2455 len = pLenObj->GetInteger(); |
| 2456 } | 2456 } |
| 2457 //Check whether end of line markers follow the keyword 'stream'. | 2457 // Check whether end of line markers follow the keyword 'stream'. |
| 2458 unsigned int numMarkers = ReadEOLMarkers(m_Pos); | 2458 unsigned int numMarkers = ReadEOLMarkers(m_Pos); |
| 2459 m_Pos += numMarkers; | 2459 m_Pos += numMarkers; |
| 2460 FX_FILESIZE streamStartPos = m_Pos; | 2460 FX_FILESIZE streamStartPos = m_Pos; |
| 2461 if (pContext) { | 2461 if (pContext) { |
| 2462 pContext->m_DataStart = streamStartPos; | 2462 pContext->m_DataStart = streamStartPos; |
| 2463 } | 2463 } |
| 2464 const unsigned int ENDSTREAM_LEN = sizeof("endstream") - 1; | 2464 const unsigned int ENDSTREAM_LEN = sizeof("endstream") - 1; |
| 2465 const unsigned int ENDOBJ_LEN = sizeof("endobj") - 1; | 2465 const unsigned int ENDOBJ_LEN = sizeof("endobj") - 1; |
| 2466 CPDF_CryptoHandler* pCryptoHandler = objnum == (FX_DWORD)m_MetadataObjnum ? nu
llptr : m_pCryptoHandler; | 2466 CPDF_CryptoHandler* pCryptoHandler = |
| 2467 objnum == (FX_DWORD)m_MetadataObjnum ? nullptr : m_pCryptoHandler; |
| 2467 if (!pCryptoHandler) { | 2468 if (!pCryptoHandler) { |
| 2468 FX_BOOL bSearchForKeyword = TRUE; | 2469 FX_BOOL bSearchForKeyword = TRUE; |
| 2469 unsigned int prevMarkers = 0; | 2470 unsigned int prevMarkers = 0; |
| 2470 unsigned int nextMarkers = 0; | 2471 unsigned int nextMarkers = 0; |
| 2471 if (len >= 0) { | 2472 if (len >= 0) { |
| 2472 pdfium::base::CheckedNumeric<FX_FILESIZE> pos = m_Pos; | 2473 pdfium::base::CheckedNumeric<FX_FILESIZE> pos = m_Pos; |
| 2473 pos += len; | 2474 pos += len; |
| 2474 if (pos.IsValid() && pos.ValueOrDie() < m_FileLen) { | 2475 if (pos.IsValid() && pos.ValueOrDie() < m_FileLen) { |
| 2475 m_Pos = pos.ValueOrDie(); | 2476 m_Pos = pos.ValueOrDie(); |
| 2476 } | 2477 } |
| 2477 prevMarkers = ReadEOLMarkers(m_Pos); | 2478 prevMarkers = ReadEOLMarkers(m_Pos); |
| 2478 GetNextWord(); | 2479 GetNextWord(); |
| 2479 nextMarkers = ReadEOLMarkers(m_Pos); | 2480 nextMarkers = ReadEOLMarkers(m_Pos); |
| 2480 if (m_WordSize == ENDSTREAM_LEN && prevMarkers != 0 && nextMarkers != 0 && | 2481 if (m_WordSize == ENDSTREAM_LEN && prevMarkers != 0 && nextMarkers != 0 && |
| 2481 FXSYS_memcmp(m_WordBuffer, "endstream", ENDSTREAM_LEN) == 0) { | 2482 FXSYS_memcmp(m_WordBuffer, "endstream", ENDSTREAM_LEN) == 0) { |
| 2482 bSearchForKeyword = FALSE; | 2483 bSearchForKeyword = FALSE; |
| 2483 } | 2484 } |
| 2484 } | 2485 } |
| 2485 if (bSearchForKeyword) { | 2486 if (bSearchForKeyword) { |
| 2486 //If len is not available, len needs to be calculated | 2487 // If len is not available, len needs to be calculated |
| 2487 //by searching the keywords "endstream" or "endobj". | 2488 // by searching the keywords "endstream" or "endobj". |
| 2488 m_Pos = streamStartPos; | 2489 m_Pos = streamStartPos; |
| 2489 FX_FILESIZE endStreamOffset = 0; | 2490 FX_FILESIZE endStreamOffset = 0; |
| 2490 while (endStreamOffset >= 0) { | 2491 while (endStreamOffset >= 0) { |
| 2491 endStreamOffset = FindTag(FX_BSTRC("endstream"), 0); | 2492 endStreamOffset = FindTag(FX_BSTRC("endstream"), 0); |
| 2492 if (endStreamOffset < 0) { | 2493 if (endStreamOffset < 0) { |
| 2493 //Can't find any "endstream". | 2494 // Can't find any "endstream". |
| 2494 break; | 2495 break; |
| 2495 } | 2496 } |
| 2496 prevMarkers = ReadEOLMarkers(streamStartPos + endStreamOffset - 1); | 2497 prevMarkers = ReadEOLMarkers(streamStartPos + endStreamOffset - 1); |
| 2497 nextMarkers = ReadEOLMarkers(streamStartPos + endStreamOffset + ENDSTREA
M_LEN); | 2498 nextMarkers = |
| 2499 ReadEOLMarkers(streamStartPos + endStreamOffset + ENDSTREAM_LEN); |
| 2498 if (prevMarkers != 0 && nextMarkers != 0) { | 2500 if (prevMarkers != 0 && nextMarkers != 0) { |
| 2499 //Stop searching when the keyword "endstream" is found. | 2501 // Stop searching when the keyword "endstream" is found. |
| 2500 break; | 2502 break; |
| 2501 } else { | 2503 } else { |
| 2502 unsigned char ch = 0x00; | 2504 unsigned char ch = 0x00; |
| 2503 GetCharAt(streamStartPos + endStreamOffset + ENDSTREAM_LEN, ch); | 2505 GetCharAt(streamStartPos + endStreamOffset + ENDSTREAM_LEN, ch); |
| 2504 if (ch == 0x09 || ch == 0x20) { | 2506 if (ch == 0x09 || ch == 0x20) { |
| 2505 //"endstream" is treated as a keyword | 2507 //"endstream" is treated as a keyword |
| 2506 //when it is followed by a tab or whitespace | 2508 // when it is followed by a tab or whitespace |
| 2507 break; | 2509 break; |
| 2508 } | 2510 } |
| 2509 } | 2511 } |
| 2510 m_Pos += ENDSTREAM_LEN; | 2512 m_Pos += ENDSTREAM_LEN; |
| 2511 } | 2513 } |
| 2512 m_Pos = streamStartPos; | 2514 m_Pos = streamStartPos; |
| 2513 FX_FILESIZE endObjOffset = 0; | 2515 FX_FILESIZE endObjOffset = 0; |
| 2514 while (endObjOffset >= 0) { | 2516 while (endObjOffset >= 0) { |
| 2515 endObjOffset = FindTag(FX_BSTRC("endobj"), 0); | 2517 endObjOffset = FindTag(FX_BSTRC("endobj"), 0); |
| 2516 if (endObjOffset < 0) { | 2518 if (endObjOffset < 0) { |
| 2517 //Can't find any "endobj". | 2519 // Can't find any "endobj". |
| 2518 break; | 2520 break; |
| 2519 } | 2521 } |
| 2520 prevMarkers = ReadEOLMarkers(streamStartPos + endObjOffset - 1); | 2522 prevMarkers = ReadEOLMarkers(streamStartPos + endObjOffset - 1); |
| 2521 nextMarkers = ReadEOLMarkers(streamStartPos + endObjOffset + ENDOBJ_LEN)
; | 2523 nextMarkers = |
| 2524 ReadEOLMarkers(streamStartPos + endObjOffset + ENDOBJ_LEN); |
| 2522 if (prevMarkers != 0 && nextMarkers != 0) { | 2525 if (prevMarkers != 0 && nextMarkers != 0) { |
| 2523 //Stop searching when the keyword "endobj" is found. | 2526 // Stop searching when the keyword "endobj" is found. |
| 2524 break; | 2527 break; |
| 2525 } | 2528 } |
| 2526 m_Pos += ENDOBJ_LEN; | 2529 m_Pos += ENDOBJ_LEN; |
| 2527 } | 2530 } |
| 2528 if (endStreamOffset < 0 && endObjOffset < 0) { | 2531 if (endStreamOffset < 0 && endObjOffset < 0) { |
| 2529 //Can't find "endstream" or "endobj". | 2532 // Can't find "endstream" or "endobj". |
| 2530 return nullptr; | 2533 return nullptr; |
| 2531 } | 2534 } |
| 2532 if (endStreamOffset < 0 && endObjOffset >= 0) { | 2535 if (endStreamOffset < 0 && endObjOffset >= 0) { |
| 2533 //Correct the position of end stream. | 2536 // Correct the position of end stream. |
| 2534 endStreamOffset = endObjOffset; | 2537 endStreamOffset = endObjOffset; |
| 2535 } else if (endStreamOffset >= 0 && endObjOffset < 0) { | 2538 } else if (endStreamOffset >= 0 && endObjOffset < 0) { |
| 2536 //Correct the position of end obj. | 2539 // Correct the position of end obj. |
| 2537 endObjOffset = endStreamOffset; | 2540 endObjOffset = endStreamOffset; |
| 2538 } else if (endStreamOffset > endObjOffset) { | 2541 } else if (endStreamOffset > endObjOffset) { |
| 2539 endStreamOffset = endObjOffset; | 2542 endStreamOffset = endObjOffset; |
| 2540 } | 2543 } |
| 2541 len = endStreamOffset; | 2544 len = endStreamOffset; |
| 2542 numMarkers = ReadEOLMarkers(streamStartPos + endStreamOffset - 2); | 2545 numMarkers = ReadEOLMarkers(streamStartPos + endStreamOffset - 2); |
| 2543 if (numMarkers == 2) { | 2546 if (numMarkers == 2) { |
| 2544 len -= 2; | 2547 len -= 2; |
| 2545 } else { | 2548 } else { |
| 2546 numMarkers = ReadEOLMarkers(streamStartPos + endStreamOffset - 1); | 2549 numMarkers = ReadEOLMarkers(streamStartPos + endStreamOffset - 1); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 2571 len = dest_buf.GetSize(); | 2574 len = dest_buf.GetSize(); |
| 2572 dest_buf.DetachBuffer(); | 2575 dest_buf.DetachBuffer(); |
| 2573 } | 2576 } |
| 2574 CPDF_Stream* pStream = new CPDF_Stream(pData, len, pDict); | 2577 CPDF_Stream* pStream = new CPDF_Stream(pData, len, pDict); |
| 2575 if (pContext) { | 2578 if (pContext) { |
| 2576 pContext->m_DataEnd = pContext->m_DataStart + len; | 2579 pContext->m_DataEnd = pContext->m_DataStart + len; |
| 2577 } | 2580 } |
| 2578 streamStartPos = m_Pos; | 2581 streamStartPos = m_Pos; |
| 2579 GetNextWord(); | 2582 GetNextWord(); |
| 2580 numMarkers = ReadEOLMarkers(m_Pos); | 2583 numMarkers = ReadEOLMarkers(m_Pos); |
| 2581 if (m_WordSize == ENDOBJ_LEN && numMarkers != 0 && | 2584 if (m_WordSize == ENDOBJ_LEN && numMarkers != 0 && |
| 2582 FXSYS_memcmp(m_WordBuffer, "endobj", ENDOBJ_LEN) == 0) { | 2585 FXSYS_memcmp(m_WordBuffer, "endobj", ENDOBJ_LEN) == 0) { |
| 2583 m_Pos = streamStartPos; | 2586 m_Pos = streamStartPos; |
| 2584 } | 2587 } |
| 2585 return pStream; | 2588 return pStream; |
| 2586 } | 2589 } |
| 2587 void CPDF_SyntaxParser::InitParser(IFX_FileRead* pFileAccess, | 2590 void CPDF_SyntaxParser::InitParser(IFX_FileRead* pFileAccess, |
| 2588 FX_DWORD HeaderOffset) { | 2591 FX_DWORD HeaderOffset) { |
| 2589 if (m_pFileBuf) { | 2592 if (m_pFileBuf) { |
| 2590 FX_Free(m_pFileBuf); | 2593 FX_Free(m_pFileBuf); |
| 2591 m_pFileBuf = NULL; | 2594 m_pFileBuf = NULL; |
| 2592 } | 2595 } |
| (...skipping 2114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4707 return FALSE; | 4710 return FALSE; |
| 4708 } | 4711 } |
| 4709 CPDF_PageNode::~CPDF_PageNode() { | 4712 CPDF_PageNode::~CPDF_PageNode() { |
| 4710 int32_t iSize = m_childNode.GetSize(); | 4713 int32_t iSize = m_childNode.GetSize(); |
| 4711 for (int32_t i = 0; i < iSize; ++i) { | 4714 for (int32_t i = 0; i < iSize; ++i) { |
| 4712 CPDF_PageNode* pNode = (CPDF_PageNode*)m_childNode[i]; | 4715 CPDF_PageNode* pNode = (CPDF_PageNode*)m_childNode[i]; |
| 4713 delete pNode; | 4716 delete pNode; |
| 4714 } | 4717 } |
| 4715 m_childNode.RemoveAll(); | 4718 m_childNode.RemoveAll(); |
| 4716 } | 4719 } |
| OLD | NEW |