Tom Sepez | 5fc239a | 2016-03-10 14:10:38 -0800 | [diff] [blame] | 1 | // Copyright 2016 PDFium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 7 | #include "core/fpdfapi/parser/cpdf_stream.h" |
Tom Sepez | 5fc239a | 2016-03-10 14:10:38 -0800 | [diff] [blame] | 8 | |
tsepez | 9e05ee1 | 2016-11-21 13:19:10 -0800 | [diff] [blame] | 9 | #include <utility> |
| 10 | |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 11 | #include "core/fpdfapi/parser/cpdf_dictionary.h" |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 12 | #include "core/fpdfapi/parser/cpdf_number.h" |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 13 | #include "core/fpdfapi/parser/cpdf_stream_acc.h" |
| 14 | #include "core/fpdfapi/parser/fpdf_parser_decode.h" |
tsepez | e6db16e | 2016-09-19 10:45:09 -0700 | [diff] [blame] | 15 | #include "third_party/base/numerics/safe_conversions.h" |
tsepez | a9caab9 | 2016-12-14 05:57:10 -0800 | [diff] [blame] | 16 | #include "third_party/base/ptr_util.h" |
weili | a470b5e | 2016-08-23 22:08:37 -0700 | [diff] [blame] | 17 | #include "third_party/base/stl_util.h" |
Tom Sepez | 5fc239a | 2016-03-10 14:10:38 -0800 | [diff] [blame] | 18 | |
tsepez | e6db16e | 2016-09-19 10:45:09 -0700 | [diff] [blame] | 19 | CPDF_Stream::CPDF_Stream() {} |
| 20 | |
tsepez | 47fb8c0 | 2016-12-15 13:51:34 -0800 | [diff] [blame] | 21 | CPDF_Stream::CPDF_Stream(std::unique_ptr<uint8_t, FxFreeDeleter> pData, |
tsepez | 9e05ee1 | 2016-11-21 13:19:10 -0800 | [diff] [blame] | 22 | uint32_t size, |
| 23 | std::unique_ptr<CPDF_Dictionary> pDict) |
tsepez | 47fb8c0 | 2016-12-15 13:51:34 -0800 | [diff] [blame] | 24 | : m_dwSize(size), m_pDict(std::move(pDict)), m_pDataBuf(std::move(pData)) {} |
Tom Sepez | 5fc239a | 2016-03-10 14:10:38 -0800 | [diff] [blame] | 25 | |
tsepez | 836f7d5 | 2016-10-10 14:31:05 -0700 | [diff] [blame] | 26 | CPDF_Stream::~CPDF_Stream() { |
| 27 | m_ObjNum = kInvalidObjNum; |
| 28 | if (m_pDict && m_pDict->GetObjNum() == kInvalidObjNum) |
| 29 | m_pDict.release(); // lowercase release, release ownership. |
| 30 | } |
Tom Sepez | 5fc239a | 2016-03-10 14:10:38 -0800 | [diff] [blame] | 31 | |
| 32 | CPDF_Object::Type CPDF_Stream::GetType() const { |
| 33 | return STREAM; |
| 34 | } |
| 35 | |
| 36 | CPDF_Dictionary* CPDF_Stream::GetDict() const { |
tsepez | e6db16e | 2016-09-19 10:45:09 -0700 | [diff] [blame] | 37 | return m_pDict.get(); |
Tom Sepez | 5fc239a | 2016-03-10 14:10:38 -0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | bool CPDF_Stream::IsStream() const { |
| 41 | return true; |
| 42 | } |
| 43 | |
| 44 | CPDF_Stream* CPDF_Stream::AsStream() { |
| 45 | return this; |
| 46 | } |
| 47 | |
| 48 | const CPDF_Stream* CPDF_Stream::AsStream() const { |
| 49 | return this; |
| 50 | } |
| 51 | |
tsepez | 596fc4c | 2016-06-07 06:41:50 -0700 | [diff] [blame] | 52 | void CPDF_Stream::InitStream(const uint8_t* pData, |
tsepez | b5e8f14 | 2016-03-25 15:18:35 -0700 | [diff] [blame] | 53 | uint32_t size, |
tsepez | 9e05ee1 | 2016-11-21 13:19:10 -0800 | [diff] [blame] | 54 | std::unique_ptr<CPDF_Dictionary> pDict) { |
| 55 | m_pDict = std::move(pDict); |
tsepez | e6db16e | 2016-09-19 10:45:09 -0700 | [diff] [blame] | 56 | m_bMemoryBased = true; |
| 57 | m_pFile = nullptr; |
| 58 | m_pDataBuf.reset(FX_Alloc(uint8_t, size)); |
Tom Sepez | 5fc239a | 2016-03-10 14:10:38 -0800 | [diff] [blame] | 59 | if (pData) |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame] | 60 | memcpy(m_pDataBuf.get(), pData, size); |
Tom Sepez | 5fc239a | 2016-03-10 14:10:38 -0800 | [diff] [blame] | 61 | m_dwSize = size; |
| 62 | if (m_pDict) |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 63 | m_pDict->SetNewFor<CPDF_Number>("Length", static_cast<int>(m_dwSize)); |
tsepez | e6db16e | 2016-09-19 10:45:09 -0700 | [diff] [blame] | 64 | } |
| 65 | |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 66 | void CPDF_Stream::InitStreamFromFile( |
| 67 | const CFX_RetainPtr<IFX_SeekableReadStream>& pFile, |
| 68 | std::unique_ptr<CPDF_Dictionary> pDict) { |
tsepez | 9e05ee1 | 2016-11-21 13:19:10 -0800 | [diff] [blame] | 69 | m_pDict = std::move(pDict); |
tsepez | e6db16e | 2016-09-19 10:45:09 -0700 | [diff] [blame] | 70 | m_bMemoryBased = false; |
| 71 | m_pDataBuf.reset(); |
| 72 | m_pFile = pFile; |
| 73 | m_dwSize = pdfium::base::checked_cast<uint32_t>(pFile->GetSize()); |
| 74 | if (m_pDict) |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 75 | m_pDict->SetNewFor<CPDF_Number>("Length", static_cast<int>(m_dwSize)); |
Tom Sepez | 5fc239a | 2016-03-10 14:10:38 -0800 | [diff] [blame] | 76 | } |
| 77 | |
tsepez | 335cf09 | 2016-11-09 13:28:26 -0800 | [diff] [blame] | 78 | std::unique_ptr<CPDF_Object> CPDF_Stream::Clone() const { |
weili | a470b5e | 2016-08-23 22:08:37 -0700 | [diff] [blame] | 79 | return CloneObjectNonCyclic(false); |
| 80 | } |
| 81 | |
tsepez | 335cf09 | 2016-11-09 13:28:26 -0800 | [diff] [blame] | 82 | std::unique_ptr<CPDF_Object> CPDF_Stream::CloneNonCyclic( |
weili | a470b5e | 2016-08-23 22:08:37 -0700 | [diff] [blame] | 83 | bool bDirect, |
| 84 | std::set<const CPDF_Object*>* pVisited) const { |
| 85 | pVisited->insert(this); |
Tom Sepez | afd0d1f | 2017-04-04 14:37:18 -0700 | [diff] [blame] | 86 | auto pAcc = pdfium::MakeRetain<CPDF_StreamAcc>(this); |
| 87 | pAcc->LoadAllData(true); |
tsepez | 9e05ee1 | 2016-11-21 13:19:10 -0800 | [diff] [blame] | 88 | |
Tom Sepez | afd0d1f | 2017-04-04 14:37:18 -0700 | [diff] [blame] | 89 | uint32_t streamSize = pAcc->GetSize(); |
Tom Sepez | 5fc239a | 2016-03-10 14:10:38 -0800 | [diff] [blame] | 90 | CPDF_Dictionary* pDict = GetDict(); |
tsepez | 9e05ee1 | 2016-11-21 13:19:10 -0800 | [diff] [blame] | 91 | std::unique_ptr<CPDF_Dictionary> pNewDict; |
weili | a470b5e | 2016-08-23 22:08:37 -0700 | [diff] [blame] | 92 | if (pDict && !pdfium::ContainsKey(*pVisited, pDict)) { |
tsepez | 9e05ee1 | 2016-11-21 13:19:10 -0800 | [diff] [blame] | 93 | pNewDict = ToDictionary( |
| 94 | static_cast<CPDF_Object*>(pDict)->CloneNonCyclic(bDirect, pVisited)); |
weili | a470b5e | 2016-08-23 22:08:37 -0700 | [diff] [blame] | 95 | } |
Tom Sepez | afd0d1f | 2017-04-04 14:37:18 -0700 | [diff] [blame] | 96 | return pdfium::MakeUnique<CPDF_Stream>(pAcc->DetachData(), streamSize, |
tsepez | 9e05ee1 | 2016-11-21 13:19:10 -0800 | [diff] [blame] | 97 | std::move(pNewDict)); |
Tom Sepez | 5fc239a | 2016-03-10 14:10:38 -0800 | [diff] [blame] | 98 | } |
| 99 | |
tsepez | e6db16e | 2016-09-19 10:45:09 -0700 | [diff] [blame] | 100 | void CPDF_Stream::SetData(const uint8_t* pData, uint32_t size) { |
| 101 | m_bMemoryBased = true; |
| 102 | m_pDataBuf.reset(FX_Alloc(uint8_t, size)); |
| 103 | if (pData) |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame] | 104 | memcpy(m_pDataBuf.get(), pData, size); |
Tom Sepez | 5fc239a | 2016-03-10 14:10:38 -0800 | [diff] [blame] | 105 | m_dwSize = size; |
| 106 | if (!m_pDict) |
tsepez | a9caab9 | 2016-12-14 05:57:10 -0800 | [diff] [blame] | 107 | m_pDict = pdfium::MakeUnique<CPDF_Dictionary>(); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 108 | m_pDict->SetNewFor<CPDF_Number>("Length", static_cast<int>(size)); |
tsepez | e6db16e | 2016-09-19 10:45:09 -0700 | [diff] [blame] | 109 | m_pDict->RemoveFor("Filter"); |
| 110 | m_pDict->RemoveFor("DecodeParms"); |
Tom Sepez | 5fc239a | 2016-03-10 14:10:38 -0800 | [diff] [blame] | 111 | } |
| 112 | |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 113 | bool CPDF_Stream::ReadRawData(FX_FILESIZE offset, |
| 114 | uint8_t* buf, |
| 115 | uint32_t size) const { |
Graeme Connell | baf318f | 2017-02-06 10:52:35 -0700 | [diff] [blame] | 116 | if (!m_bMemoryBased && m_pFile) |
Tom Sepez | 5fc239a | 2016-03-10 14:10:38 -0800 | [diff] [blame] | 117 | return m_pFile->ReadBlock(buf, offset, size); |
| 118 | |
| 119 | if (m_pDataBuf) |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame] | 120 | memcpy(buf, m_pDataBuf.get() + offset, size); |
Tom Sepez | 5fc239a | 2016-03-10 14:10:38 -0800 | [diff] [blame] | 121 | |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 122 | return true; |
Tom Sepez | 5fc239a | 2016-03-10 14:10:38 -0800 | [diff] [blame] | 123 | } |
| 124 | |
tsepez | 430ab83 | 2016-11-18 14:48:21 -0800 | [diff] [blame] | 125 | bool CPDF_Stream::HasFilter() const { |
| 126 | return m_pDict && m_pDict->KeyExist("Filter"); |
| 127 | } |
| 128 | |
Tom Sepez | 5fc239a | 2016-03-10 14:10:38 -0800 | [diff] [blame] | 129 | CFX_WideString CPDF_Stream::GetUnicodeText() const { |
Tom Sepez | afd0d1f | 2017-04-04 14:37:18 -0700 | [diff] [blame] | 130 | auto pAcc = pdfium::MakeRetain<CPDF_StreamAcc>(this); |
| 131 | pAcc->LoadAllData(false); |
| 132 | return PDF_DecodeText(pAcc->GetData(), pAcc->GetSize()); |
Tom Sepez | 5fc239a | 2016-03-10 14:10:38 -0800 | [diff] [blame] | 133 | } |
Dan Sinclair | c68b1e7 | 2017-05-08 16:59:54 -0400 | [diff] [blame^] | 134 | |
| 135 | bool CPDF_Stream::WriteTo(CFX_FileBufferArchive* archive, |
| 136 | FX_FILESIZE* offset) const { |
| 137 | if (!GetDict()->WriteTo(archive, offset)) |
| 138 | return false; |
| 139 | if (archive->AppendString("stream\r\n") < 0) |
| 140 | return false; |
| 141 | *offset += 8; |
| 142 | |
| 143 | auto pAcc = pdfium::MakeRetain<CPDF_StreamAcc>(this); |
| 144 | pAcc->LoadAllData(true); |
| 145 | if (archive->AppendBlock(pAcc->GetData(), pAcc->GetSize()) < 0) |
| 146 | return false; |
| 147 | *offset += pAcc->GetSize(); |
| 148 | |
| 149 | int32_t len = archive->AppendString("\r\nendstream"); |
| 150 | if (len < 0) |
| 151 | return false; |
| 152 | |
| 153 | *offset += len; |
| 154 | return true; |
| 155 | } |