| OLD | NEW |
| 1 // Copyright 2016 PDFium Authors. All rights reserved. | 1 // Copyright 2016 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 "core/fpdfapi/parser/cpdf_syntax_parser.h" | 7 #include "core/fpdfapi/parser/cpdf_syntax_parser.h" |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 470 m_Pos = dwSignValuePos; | 470 m_Pos = dwSignValuePos; |
| 471 pDict->SetFor("Contents", GetObject(pObjList, objnum, gennum, false)); | 471 pDict->SetFor("Contents", GetObject(pObjList, objnum, gennum, false)); |
| 472 } | 472 } |
| 473 | 473 |
| 474 FX_FILESIZE SavedPos = m_Pos; | 474 FX_FILESIZE SavedPos = m_Pos; |
| 475 CFX_ByteString nextword = GetNextWord(nullptr); | 475 CFX_ByteString nextword = GetNextWord(nullptr); |
| 476 if (nextword != "stream") { | 476 if (nextword != "stream") { |
| 477 m_Pos = SavedPos; | 477 m_Pos = SavedPos; |
| 478 return std::move(pDict); | 478 return std::move(pDict); |
| 479 } | 479 } |
| 480 return ReadStream(pDict.release(), objnum, gennum); | 480 return ReadStream(std::move(pDict), objnum, gennum); |
| 481 } | 481 } |
| 482 | |
| 483 if (word == ">>") | 482 if (word == ">>") |
| 484 m_Pos = SavedObjPos; | 483 m_Pos = SavedObjPos; |
| 485 | 484 |
| 486 return nullptr; | 485 return nullptr; |
| 487 } | 486 } |
| 488 | 487 |
| 489 std::unique_ptr<CPDF_Object> CPDF_SyntaxParser::GetObjectForStrict( | 488 std::unique_ptr<CPDF_Object> CPDF_SyntaxParser::GetObjectForStrict( |
| 490 CPDF_IndirectObjectHolder* pObjList, | 489 CPDF_IndirectObjectHolder* pObjList, |
| 491 uint32_t objnum, | 490 uint32_t objnum, |
| 492 uint32_t gennum) { | 491 uint32_t gennum) { |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 580 std::move(obj)); | 579 std::move(obj)); |
| 581 } | 580 } |
| 582 } | 581 } |
| 583 | 582 |
| 584 FX_FILESIZE SavedPos = m_Pos; | 583 FX_FILESIZE SavedPos = m_Pos; |
| 585 CFX_ByteString nextword = GetNextWord(nullptr); | 584 CFX_ByteString nextword = GetNextWord(nullptr); |
| 586 if (nextword != "stream") { | 585 if (nextword != "stream") { |
| 587 m_Pos = SavedPos; | 586 m_Pos = SavedPos; |
| 588 return std::move(pDict); | 587 return std::move(pDict); |
| 589 } | 588 } |
| 590 | 589 return ReadStream(std::move(pDict), objnum, gennum); |
| 591 return ReadStream(pDict.release(), objnum, gennum); | |
| 592 } | 590 } |
| 593 | |
| 594 if (word == ">>") | 591 if (word == ">>") |
| 595 m_Pos = SavedObjPos; | 592 m_Pos = SavedObjPos; |
| 596 | 593 |
| 597 return nullptr; | 594 return nullptr; |
| 598 } | 595 } |
| 599 | 596 |
| 600 unsigned int CPDF_SyntaxParser::ReadEOLMarkers(FX_FILESIZE pos) { | 597 unsigned int CPDF_SyntaxParser::ReadEOLMarkers(FX_FILESIZE pos) { |
| 601 unsigned char byte1 = 0; | 598 unsigned char byte1 = 0; |
| 602 unsigned char byte2 = 0; | 599 unsigned char byte2 = 0; |
| 603 | 600 |
| 604 GetCharAt(pos, byte1); | 601 GetCharAt(pos, byte1); |
| 605 GetCharAt(pos + 1, byte2); | 602 GetCharAt(pos + 1, byte2); |
| 606 | 603 |
| 607 if (byte1 == '\r' && byte2 == '\n') | 604 if (byte1 == '\r' && byte2 == '\n') |
| 608 return 2; | 605 return 2; |
| 609 | 606 |
| 610 if (byte1 == '\r' || byte1 == '\n') | 607 if (byte1 == '\r' || byte1 == '\n') |
| 611 return 1; | 608 return 1; |
| 612 | 609 |
| 613 return 0; | 610 return 0; |
| 614 } | 611 } |
| 615 | 612 |
| 616 std::unique_ptr<CPDF_Stream> CPDF_SyntaxParser::ReadStream( | 613 std::unique_ptr<CPDF_Stream> CPDF_SyntaxParser::ReadStream( |
| 617 CPDF_Dictionary* pDict, | 614 std::unique_ptr<CPDF_Dictionary> pDict, |
| 618 uint32_t objnum, | 615 uint32_t objnum, |
| 619 uint32_t gennum) { | 616 uint32_t gennum) { |
| 620 CPDF_Object* pLenObj = pDict->GetObjectFor("Length"); | 617 CPDF_Object* pLenObj = pDict->GetObjectFor("Length"); |
| 621 FX_FILESIZE len = -1; | 618 FX_FILESIZE len = -1; |
| 622 CPDF_Reference* pLenObjRef = ToReference(pLenObj); | 619 CPDF_Reference* pLenObjRef = ToReference(pLenObj); |
| 623 | 620 |
| 624 bool differingObjNum = !pLenObjRef || (pLenObjRef->GetObjList() && | 621 bool differingObjNum = !pLenObjRef || (pLenObjRef->GetObjList() && |
| 625 pLenObjRef->GetRefObjNum() != objnum); | 622 pLenObjRef->GetRefObjNum() != objnum); |
| 626 if (pLenObj && differingObjNum) | 623 if (pLenObj && differingObjNum) |
| 627 len = pLenObj->GetInteger(); | 624 len = pLenObj->GetInteger(); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 686 | 683 |
| 687 // Stop searching when "endobj" is found. | 684 // Stop searching when "endobj" is found. |
| 688 if (IsWholeWord(m_Pos - kEndObjStr.GetLength(), m_FileLen, kEndObjStr, | 685 if (IsWholeWord(m_Pos - kEndObjStr.GetLength(), m_FileLen, kEndObjStr, |
| 689 true)) { | 686 true)) { |
| 690 endObjOffset = m_Pos - streamStartPos - kEndObjStr.GetLength(); | 687 endObjOffset = m_Pos - streamStartPos - kEndObjStr.GetLength(); |
| 691 break; | 688 break; |
| 692 } | 689 } |
| 693 } | 690 } |
| 694 | 691 |
| 695 // Can't find "endstream" or "endobj". | 692 // Can't find "endstream" or "endobj". |
| 696 if (endStreamOffset < 0 && endObjOffset < 0) { | 693 if (endStreamOffset < 0 && endObjOffset < 0) |
| 697 delete pDict; | |
| 698 return nullptr; | 694 return nullptr; |
| 699 } | |
| 700 | 695 |
| 701 if (endStreamOffset < 0 && endObjOffset >= 0) { | 696 if (endStreamOffset < 0 && endObjOffset >= 0) { |
| 702 // Correct the position of end stream. | 697 // Correct the position of end stream. |
| 703 endStreamOffset = endObjOffset; | 698 endStreamOffset = endObjOffset; |
| 704 } else if (endStreamOffset >= 0 && endObjOffset < 0) { | 699 } else if (endStreamOffset >= 0 && endObjOffset < 0) { |
| 705 // Correct the position of end obj. | 700 // Correct the position of end obj. |
| 706 endObjOffset = endStreamOffset; | 701 endObjOffset = endStreamOffset; |
| 707 } else if (endStreamOffset > endObjOffset) { | 702 } else if (endStreamOffset > endObjOffset) { |
| 708 endStreamOffset = endObjOffset; | 703 endStreamOffset = endObjOffset; |
| 709 } | 704 } |
| 705 len = endStreamOffset; |
| 710 | 706 |
| 711 len = endStreamOffset; | |
| 712 int numMarkers = ReadEOLMarkers(streamStartPos + endStreamOffset - 2); | 707 int numMarkers = ReadEOLMarkers(streamStartPos + endStreamOffset - 2); |
| 713 if (numMarkers == 2) { | 708 if (numMarkers == 2) { |
| 714 len -= 2; | 709 len -= 2; |
| 715 } else { | 710 } else { |
| 716 numMarkers = ReadEOLMarkers(streamStartPos + endStreamOffset - 1); | 711 numMarkers = ReadEOLMarkers(streamStartPos + endStreamOffset - 1); |
| 717 if (numMarkers == 1) { | 712 if (numMarkers == 1) { |
| 718 len -= 1; | 713 len -= 1; |
| 719 } | 714 } |
| 720 } | 715 } |
| 716 if (len < 0) |
| 717 return nullptr; |
| 721 | 718 |
| 722 if (len < 0) { | |
| 723 delete pDict; | |
| 724 return nullptr; | |
| 725 } | |
| 726 pDict->SetNewFor<CPDF_Number>("Length", static_cast<int>(len)); | 719 pDict->SetNewFor<CPDF_Number>("Length", static_cast<int>(len)); |
| 727 } | 720 } |
| 728 m_Pos = streamStartPos; | 721 m_Pos = streamStartPos; |
| 729 } | 722 } |
| 730 | 723 if (len < 0) |
| 731 if (len < 0) { | |
| 732 delete pDict; | |
| 733 return nullptr; | 724 return nullptr; |
| 734 } | |
| 735 | 725 |
| 736 uint8_t* pData = nullptr; | 726 uint8_t* pData = nullptr; |
| 737 if (len > 0) { | 727 if (len > 0) { |
| 738 pData = FX_Alloc(uint8_t, len); | 728 pData = FX_Alloc(uint8_t, len); |
| 739 ReadBlock(pData, len); | 729 ReadBlock(pData, len); |
| 740 if (pCryptoHandler) { | 730 if (pCryptoHandler) { |
| 741 CFX_BinaryBuf dest_buf; | 731 CFX_BinaryBuf dest_buf; |
| 742 dest_buf.EstimateSize(pCryptoHandler->DecryptGetSize(len)); | 732 dest_buf.EstimateSize(pCryptoHandler->DecryptGetSize(len)); |
| 743 | 733 |
| 744 void* context = pCryptoHandler->DecryptStart(objnum, gennum); | 734 void* context = pCryptoHandler->DecryptStart(objnum, gennum); |
| 745 pCryptoHandler->DecryptStream(context, pData, len, dest_buf); | 735 pCryptoHandler->DecryptStream(context, pData, len, dest_buf); |
| 746 pCryptoHandler->DecryptFinish(context, dest_buf); | 736 pCryptoHandler->DecryptFinish(context, dest_buf); |
| 747 | |
| 748 FX_Free(pData); | 737 FX_Free(pData); |
| 749 pData = dest_buf.GetBuffer(); | 738 pData = dest_buf.GetBuffer(); |
| 750 len = dest_buf.GetSize(); | 739 len = dest_buf.GetSize(); |
| 751 dest_buf.DetachBuffer(); | 740 dest_buf.DetachBuffer(); |
| 752 } | 741 } |
| 753 } | 742 } |
| 754 | 743 |
| 755 auto pStream = | 744 auto pStream = pdfium::MakeUnique<CPDF_Stream>(pData, len, std::move(pDict)); |
| 756 pdfium::MakeUnique<CPDF_Stream>(pData, len, pdfium::WrapUnique(pDict)); | |
| 757 streamStartPos = m_Pos; | 745 streamStartPos = m_Pos; |
| 758 FXSYS_memset(m_WordBuffer, 0, kEndObjStr.GetLength() + 1); | 746 FXSYS_memset(m_WordBuffer, 0, kEndObjStr.GetLength() + 1); |
| 759 GetNextWordInternal(nullptr); | 747 GetNextWordInternal(nullptr); |
| 760 | 748 |
| 761 int numMarkers = ReadEOLMarkers(m_Pos); | 749 int numMarkers = ReadEOLMarkers(m_Pos); |
| 762 if (m_WordSize == static_cast<unsigned int>(kEndObjStr.GetLength()) && | 750 if (m_WordSize == static_cast<unsigned int>(kEndObjStr.GetLength()) && |
| 763 numMarkers != 0 && | 751 numMarkers != 0 && |
| 764 FXSYS_memcmp(m_WordBuffer, kEndObjStr.raw_str(), | 752 FXSYS_memcmp(m_WordBuffer, kEndObjStr.raw_str(), |
| 765 kEndObjStr.GetLength()) == 0) { | 753 kEndObjStr.GetLength()) == 0) { |
| 766 m_Pos = streamStartPos; | 754 m_Pos = streamStartPos; |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 913 if (limit && m_Pos == limit) | 901 if (limit && m_Pos == limit) |
| 914 return -1; | 902 return -1; |
| 915 } | 903 } |
| 916 return -1; | 904 return -1; |
| 917 } | 905 } |
| 918 | 906 |
| 919 void CPDF_SyntaxParser::SetEncrypt( | 907 void CPDF_SyntaxParser::SetEncrypt( |
| 920 std::unique_ptr<CPDF_CryptoHandler> pCryptoHandler) { | 908 std::unique_ptr<CPDF_CryptoHandler> pCryptoHandler) { |
| 921 m_pCryptoHandler = std::move(pCryptoHandler); | 909 m_pCryptoHandler = std::move(pCryptoHandler); |
| 922 } | 910 } |
| OLD | NEW |