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_stream.h" | 7 #include "core/fpdfapi/parser/cpdf_stream.h" |
8 | 8 |
| 9 #include <utility> |
| 10 |
9 #include "core/fpdfapi/parser/cpdf_dictionary.h" | 11 #include "core/fpdfapi/parser/cpdf_dictionary.h" |
10 #include "core/fpdfapi/parser/cpdf_number.h" | 12 #include "core/fpdfapi/parser/cpdf_number.h" |
11 #include "core/fpdfapi/parser/cpdf_stream_acc.h" | 13 #include "core/fpdfapi/parser/cpdf_stream_acc.h" |
12 #include "core/fpdfapi/parser/fpdf_parser_decode.h" | 14 #include "core/fpdfapi/parser/fpdf_parser_decode.h" |
13 #include "third_party/base/numerics/safe_conversions.h" | 15 #include "third_party/base/numerics/safe_conversions.h" |
14 #include "third_party/base/stl_util.h" | 16 #include "third_party/base/stl_util.h" |
15 | 17 |
16 CPDF_Stream::CPDF_Stream() {} | 18 CPDF_Stream::CPDF_Stream() {} |
17 | 19 |
18 CPDF_Stream::CPDF_Stream(uint8_t* pData, uint32_t size, CPDF_Dictionary* pDict) | 20 CPDF_Stream::CPDF_Stream(uint8_t* pData, |
19 : m_dwSize(size), m_pDict(pDict), m_pDataBuf(pData) {} | 21 uint32_t size, |
| 22 std::unique_ptr<CPDF_Dictionary> pDict) |
| 23 : m_dwSize(size), m_pDict(std::move(pDict)), m_pDataBuf(pData) {} |
20 | 24 |
21 CPDF_Stream::~CPDF_Stream() { | 25 CPDF_Stream::~CPDF_Stream() { |
22 m_ObjNum = kInvalidObjNum; | 26 m_ObjNum = kInvalidObjNum; |
23 if (m_pDict && m_pDict->GetObjNum() == kInvalidObjNum) | 27 if (m_pDict && m_pDict->GetObjNum() == kInvalidObjNum) |
24 m_pDict.release(); // lowercase release, release ownership. | 28 m_pDict.release(); // lowercase release, release ownership. |
25 } | 29 } |
26 | 30 |
27 CPDF_Object::Type CPDF_Stream::GetType() const { | 31 CPDF_Object::Type CPDF_Stream::GetType() const { |
28 return STREAM; | 32 return STREAM; |
29 } | 33 } |
30 | 34 |
31 CPDF_Dictionary* CPDF_Stream::GetDict() const { | 35 CPDF_Dictionary* CPDF_Stream::GetDict() const { |
32 return m_pDict.get(); | 36 return m_pDict.get(); |
33 } | 37 } |
34 | 38 |
35 bool CPDF_Stream::IsStream() const { | 39 bool CPDF_Stream::IsStream() const { |
36 return true; | 40 return true; |
37 } | 41 } |
38 | 42 |
39 CPDF_Stream* CPDF_Stream::AsStream() { | 43 CPDF_Stream* CPDF_Stream::AsStream() { |
40 return this; | 44 return this; |
41 } | 45 } |
42 | 46 |
43 const CPDF_Stream* CPDF_Stream::AsStream() const { | 47 const CPDF_Stream* CPDF_Stream::AsStream() const { |
44 return this; | 48 return this; |
45 } | 49 } |
46 | 50 |
47 void CPDF_Stream::InitStream(const uint8_t* pData, | 51 void CPDF_Stream::InitStream(const uint8_t* pData, |
48 uint32_t size, | 52 uint32_t size, |
49 CPDF_Dictionary* pDict) { | 53 std::unique_ptr<CPDF_Dictionary> pDict) { |
50 m_pDict.reset(pDict); | 54 m_pDict = std::move(pDict); |
51 m_bMemoryBased = true; | 55 m_bMemoryBased = true; |
52 m_pFile = nullptr; | 56 m_pFile = nullptr; |
53 m_pDataBuf.reset(FX_Alloc(uint8_t, size)); | 57 m_pDataBuf.reset(FX_Alloc(uint8_t, size)); |
54 if (pData) | 58 if (pData) |
55 FXSYS_memcpy(m_pDataBuf.get(), pData, size); | 59 FXSYS_memcpy(m_pDataBuf.get(), pData, size); |
56 m_dwSize = size; | 60 m_dwSize = size; |
57 if (m_pDict) | 61 if (m_pDict) |
58 m_pDict->SetNewFor<CPDF_Number>("Length", static_cast<int>(m_dwSize)); | 62 m_pDict->SetNewFor<CPDF_Number>("Length", static_cast<int>(m_dwSize)); |
59 } | 63 } |
60 | 64 |
61 void CPDF_Stream::InitStreamFromFile(IFX_SeekableReadStream* pFile, | 65 void CPDF_Stream::InitStreamFromFile(IFX_SeekableReadStream* pFile, |
62 CPDF_Dictionary* pDict) { | 66 std::unique_ptr<CPDF_Dictionary> pDict) { |
63 m_pDict.reset(pDict); | 67 m_pDict = std::move(pDict); |
64 m_bMemoryBased = false; | 68 m_bMemoryBased = false; |
65 m_pDataBuf.reset(); | 69 m_pDataBuf.reset(); |
66 m_pFile = pFile; | 70 m_pFile = pFile; |
67 m_dwSize = pdfium::base::checked_cast<uint32_t>(pFile->GetSize()); | 71 m_dwSize = pdfium::base::checked_cast<uint32_t>(pFile->GetSize()); |
68 if (m_pDict) | 72 if (m_pDict) |
69 m_pDict->SetNewFor<CPDF_Number>("Length", static_cast<int>(m_dwSize)); | 73 m_pDict->SetNewFor<CPDF_Number>("Length", static_cast<int>(m_dwSize)); |
70 } | 74 } |
71 | 75 |
72 std::unique_ptr<CPDF_Object> CPDF_Stream::Clone() const { | 76 std::unique_ptr<CPDF_Object> CPDF_Stream::Clone() const { |
73 return CloneObjectNonCyclic(false); | 77 return CloneObjectNonCyclic(false); |
74 } | 78 } |
75 | 79 |
76 std::unique_ptr<CPDF_Object> CPDF_Stream::CloneNonCyclic( | 80 std::unique_ptr<CPDF_Object> CPDF_Stream::CloneNonCyclic( |
77 bool bDirect, | 81 bool bDirect, |
78 std::set<const CPDF_Object*>* pVisited) const { | 82 std::set<const CPDF_Object*>* pVisited) const { |
79 pVisited->insert(this); | 83 pVisited->insert(this); |
80 CPDF_StreamAcc acc; | 84 CPDF_StreamAcc acc; |
81 acc.LoadAllData(this, true); | 85 acc.LoadAllData(this, true); |
| 86 |
82 uint32_t streamSize = acc.GetSize(); | 87 uint32_t streamSize = acc.GetSize(); |
83 CPDF_Dictionary* pDict = GetDict(); | 88 CPDF_Dictionary* pDict = GetDict(); |
| 89 std::unique_ptr<CPDF_Dictionary> pNewDict; |
84 if (pDict && !pdfium::ContainsKey(*pVisited, pDict)) { | 90 if (pDict && !pdfium::ContainsKey(*pVisited, pDict)) { |
85 pDict = ToDictionary(static_cast<CPDF_Object*>(pDict) | 91 pNewDict = ToDictionary( |
86 ->CloneNonCyclic(bDirect, pVisited) | 92 static_cast<CPDF_Object*>(pDict)->CloneNonCyclic(bDirect, pVisited)); |
87 .release()); | |
88 } | 93 } |
89 return pdfium::MakeUnique<CPDF_Stream>(acc.DetachData(), streamSize, pDict); | 94 return pdfium::MakeUnique<CPDF_Stream>(acc.DetachData(), streamSize, |
| 95 std::move(pNewDict)); |
90 } | 96 } |
91 | 97 |
92 void CPDF_Stream::SetData(const uint8_t* pData, uint32_t size) { | 98 void CPDF_Stream::SetData(const uint8_t* pData, uint32_t size) { |
93 m_bMemoryBased = true; | 99 m_bMemoryBased = true; |
94 m_pDataBuf.reset(FX_Alloc(uint8_t, size)); | 100 m_pDataBuf.reset(FX_Alloc(uint8_t, size)); |
95 if (pData) | 101 if (pData) |
96 FXSYS_memcpy(m_pDataBuf.get(), pData, size); | 102 FXSYS_memcpy(m_pDataBuf.get(), pData, size); |
97 m_dwSize = size; | 103 m_dwSize = size; |
98 if (!m_pDict) | 104 if (!m_pDict) |
99 m_pDict.reset(new CPDF_Dictionary()); | 105 m_pDict.reset(new CPDF_Dictionary()); |
(...skipping 16 matching lines...) Expand all Loading... |
116 | 122 |
117 bool CPDF_Stream::HasFilter() const { | 123 bool CPDF_Stream::HasFilter() const { |
118 return m_pDict && m_pDict->KeyExist("Filter"); | 124 return m_pDict && m_pDict->KeyExist("Filter"); |
119 } | 125 } |
120 | 126 |
121 CFX_WideString CPDF_Stream::GetUnicodeText() const { | 127 CFX_WideString CPDF_Stream::GetUnicodeText() const { |
122 CPDF_StreamAcc stream; | 128 CPDF_StreamAcc stream; |
123 stream.LoadAllData(this, false); | 129 stream.LoadAllData(this, false); |
124 return PDF_DecodeText(stream.GetData(), stream.GetSize()); | 130 return PDF_DecodeText(stream.GetData(), stream.GetSize()); |
125 } | 131 } |
OLD | NEW |