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