| 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/fpdf_parser/include/cpdf_stream.h" | 7 #include "core/fpdfapi/fpdf_parser/include/cpdf_stream.h" |
| 8 | 8 |
| 9 #include "core/fpdfapi/fpdf_parser/include/cpdf_dictionary.h" | 9 #include "core/fpdfapi/fpdf_parser/include/cpdf_dictionary.h" |
| 10 #include "core/fpdfapi/fpdf_parser/include/cpdf_stream_acc.h" | 10 #include "core/fpdfapi/fpdf_parser/include/cpdf_stream_acc.h" |
| 11 #include "core/fpdfapi/fpdf_parser/include/fpdf_parser_decode.h" | 11 #include "core/fpdfapi/fpdf_parser/include/fpdf_parser_decode.h" |
| 12 #include "third_party/base/numerics/safe_conversions.h" |
| 12 #include "third_party/base/stl_util.h" | 13 #include "third_party/base/stl_util.h" |
| 13 | 14 |
| 15 CPDF_Stream::CPDF_Stream() {} |
| 16 |
| 14 CPDF_Stream::CPDF_Stream(uint8_t* pData, uint32_t size, CPDF_Dictionary* pDict) | 17 CPDF_Stream::CPDF_Stream(uint8_t* pData, uint32_t size, CPDF_Dictionary* pDict) |
| 15 : m_pDict(pDict), | 18 : m_pDict(pDict), |
| 16 m_dwSize(size), | 19 m_dwSize(size), |
| 17 m_GenNum(kMemoryBasedGenNum), | |
| 18 m_pDataBuf(pData) {} | 20 m_pDataBuf(pData) {} |
| 19 | 21 |
| 20 CPDF_Stream::~CPDF_Stream() { | 22 CPDF_Stream::~CPDF_Stream() {} |
| 21 m_ObjNum = kInvalidObjNum; | |
| 22 if (IsMemoryBased()) | |
| 23 FX_Free(m_pDataBuf); | |
| 24 | |
| 25 if (m_pDict) | |
| 26 m_pDict->Release(); | |
| 27 } | |
| 28 | 23 |
| 29 CPDF_Object::Type CPDF_Stream::GetType() const { | 24 CPDF_Object::Type CPDF_Stream::GetType() const { |
| 30 return STREAM; | 25 return STREAM; |
| 31 } | 26 } |
| 32 | 27 |
| 33 CPDF_Dictionary* CPDF_Stream::GetDict() const { | 28 CPDF_Dictionary* CPDF_Stream::GetDict() const { |
| 34 return m_pDict; | 29 return m_pDict.get(); |
| 35 } | 30 } |
| 36 | 31 |
| 37 bool CPDF_Stream::IsStream() const { | 32 bool CPDF_Stream::IsStream() const { |
| 38 return true; | 33 return true; |
| 39 } | 34 } |
| 40 | 35 |
| 41 CPDF_Stream* CPDF_Stream::AsStream() { | 36 CPDF_Stream* CPDF_Stream::AsStream() { |
| 42 return this; | 37 return this; |
| 43 } | 38 } |
| 44 | 39 |
| 45 const CPDF_Stream* CPDF_Stream::AsStream() const { | 40 const CPDF_Stream* CPDF_Stream::AsStream() const { |
| 46 return this; | 41 return this; |
| 47 } | 42 } |
| 48 | 43 |
| 49 void CPDF_Stream::InitStreamInternal(CPDF_Dictionary* pDict) { | |
| 50 if (pDict) { | |
| 51 if (m_pDict) | |
| 52 m_pDict->Release(); | |
| 53 m_pDict = pDict; | |
| 54 } | |
| 55 if (IsMemoryBased()) | |
| 56 FX_Free(m_pDataBuf); | |
| 57 | |
| 58 m_GenNum = 0; | |
| 59 m_pFile = nullptr; | |
| 60 } | |
| 61 | |
| 62 void CPDF_Stream::InitStream(const uint8_t* pData, | 44 void CPDF_Stream::InitStream(const uint8_t* pData, |
| 63 uint32_t size, | 45 uint32_t size, |
| 64 CPDF_Dictionary* pDict) { | 46 CPDF_Dictionary* pDict) { |
| 65 InitStreamInternal(pDict); | 47 m_pDict.reset(pDict); |
| 66 m_GenNum = kMemoryBasedGenNum; | 48 m_bMemoryBased = true; |
| 67 m_pDataBuf = FX_Alloc(uint8_t, size); | 49 m_pFile = nullptr; |
| 50 m_pDataBuf.reset(FX_Alloc(uint8_t, size)); |
| 68 if (pData) | 51 if (pData) |
| 69 FXSYS_memcpy(m_pDataBuf, pData, size); | 52 FXSYS_memcpy(m_pDataBuf.get(), pData, size); |
| 70 | |
| 71 m_dwSize = size; | 53 m_dwSize = size; |
| 72 if (m_pDict) | 54 if (m_pDict) |
| 73 m_pDict->SetIntegerFor("Length", size); | 55 m_pDict->SetIntegerFor("Length", m_dwSize); |
| 56 } |
| 57 |
| 58 void CPDF_Stream::InitStreamFromFile(IFX_FileRead* pFile, |
| 59 CPDF_Dictionary* pDict) { |
| 60 m_pDict.reset(pDict); |
| 61 m_bMemoryBased = false; |
| 62 m_pDataBuf.reset(); |
| 63 m_pFile = pFile; |
| 64 m_dwSize = pdfium::base::checked_cast<uint32_t>(pFile->GetSize()); |
| 65 if (m_pDict) |
| 66 m_pDict->SetIntegerFor("Length", m_dwSize); |
| 74 } | 67 } |
| 75 | 68 |
| 76 CPDF_Object* CPDF_Stream::Clone() const { | 69 CPDF_Object* CPDF_Stream::Clone() const { |
| 77 return CloneObjectNonCyclic(false); | 70 return CloneObjectNonCyclic(false); |
| 78 } | 71 } |
| 79 | 72 |
| 80 CPDF_Object* CPDF_Stream::CloneNonCyclic( | 73 CPDF_Object* CPDF_Stream::CloneNonCyclic( |
| 81 bool bDirect, | 74 bool bDirect, |
| 82 std::set<const CPDF_Object*>* pVisited) const { | 75 std::set<const CPDF_Object*>* pVisited) const { |
| 83 pVisited->insert(this); | 76 pVisited->insert(this); |
| 84 CPDF_StreamAcc acc; | 77 CPDF_StreamAcc acc; |
| 85 acc.LoadAllData(this, TRUE); | 78 acc.LoadAllData(this, TRUE); |
| 86 uint32_t streamSize = acc.GetSize(); | 79 uint32_t streamSize = acc.GetSize(); |
| 87 CPDF_Dictionary* pDict = GetDict(); | 80 CPDF_Dictionary* pDict = GetDict(); |
| 88 if (pDict && !pdfium::ContainsKey(*pVisited, pDict)) { | 81 if (pDict && !pdfium::ContainsKey(*pVisited, pDict)) { |
| 89 pDict = ToDictionary( | 82 pDict = ToDictionary( |
| 90 static_cast<CPDF_Object*>(pDict)->CloneNonCyclic(bDirect, pVisited)); | 83 static_cast<CPDF_Object*>(pDict)->CloneNonCyclic(bDirect, pVisited)); |
| 91 } | 84 } |
| 92 | 85 |
| 93 return new CPDF_Stream(acc.DetachData(), streamSize, pDict); | 86 return new CPDF_Stream(acc.DetachData(), streamSize, pDict); |
| 94 } | 87 } |
| 95 | 88 |
| 96 void CPDF_Stream::SetData(const uint8_t* pData, | 89 void CPDF_Stream::SetData(const uint8_t* pData, uint32_t size) { |
| 97 uint32_t size, | 90 m_bMemoryBased = true; |
| 98 FX_BOOL bCompressed, | 91 m_pDataBuf.reset(FX_Alloc(uint8_t, size)); |
| 99 FX_BOOL bKeepBuf) { | 92 if (pData) |
| 100 if (IsMemoryBased()) | 93 FXSYS_memcpy(m_pDataBuf.get(), pData, size); |
| 101 FX_Free(m_pDataBuf); | |
| 102 m_GenNum = kMemoryBasedGenNum; | |
| 103 | |
| 104 if (bKeepBuf) { | |
| 105 m_pDataBuf = const_cast<uint8_t*>(pData); | |
| 106 } else { | |
| 107 m_pDataBuf = FX_Alloc(uint8_t, size); | |
| 108 if (pData) { | |
| 109 FXSYS_memcpy(m_pDataBuf, pData, size); | |
| 110 } | |
| 111 } | |
| 112 m_dwSize = size; | 94 m_dwSize = size; |
| 113 if (!m_pDict) | 95 if (!m_pDict) |
| 114 m_pDict = new CPDF_Dictionary; | 96 m_pDict.reset(new CPDF_Dictionary); |
| 115 m_pDict->SetIntegerFor("Length", size); | 97 m_pDict->SetIntegerFor("Length", size); |
| 116 if (!bCompressed) { | 98 m_pDict->RemoveFor("Filter"); |
| 117 m_pDict->RemoveFor("Filter"); | 99 m_pDict->RemoveFor("DecodeParms"); |
| 118 m_pDict->RemoveFor("DecodeParms"); | |
| 119 } | |
| 120 } | 100 } |
| 121 | 101 |
| 122 FX_BOOL CPDF_Stream::ReadRawData(FX_FILESIZE offset, | 102 FX_BOOL CPDF_Stream::ReadRawData(FX_FILESIZE offset, |
| 123 uint8_t* buf, | 103 uint8_t* buf, |
| 124 uint32_t size) const { | 104 uint32_t size) const { |
| 125 if (!IsMemoryBased() && m_pFile) | 105 if (m_bMemoryBased && m_pFile) |
| 126 return m_pFile->ReadBlock(buf, offset, size); | 106 return m_pFile->ReadBlock(buf, offset, size); |
| 127 | 107 |
| 128 if (m_pDataBuf) | 108 if (m_pDataBuf) |
| 129 FXSYS_memcpy(buf, m_pDataBuf + offset, size); | 109 FXSYS_memcpy(buf, m_pDataBuf.get() + offset, size); |
| 130 | 110 |
| 131 return TRUE; | 111 return TRUE; |
| 132 } | 112 } |
| 133 | 113 |
| 134 void CPDF_Stream::InitStreamFromFile(IFX_FileRead* pFile, | |
| 135 CPDF_Dictionary* pDict) { | |
| 136 InitStreamInternal(pDict); | |
| 137 m_pFile = pFile; | |
| 138 m_dwSize = (uint32_t)pFile->GetSize(); | |
| 139 if (m_pDict) | |
| 140 m_pDict->SetIntegerFor("Length", m_dwSize); | |
| 141 } | |
| 142 | |
| 143 CFX_WideString CPDF_Stream::GetUnicodeText() const { | 114 CFX_WideString CPDF_Stream::GetUnicodeText() const { |
| 144 CPDF_StreamAcc stream; | 115 CPDF_StreamAcc stream; |
| 145 stream.LoadAllData(this, FALSE); | 116 stream.LoadAllData(this, FALSE); |
| 146 return PDF_DecodeText(stream.GetData(), stream.GetSize()); | 117 return PDF_DecodeText(stream.GetData(), stream.GetSize()); |
| 147 } | 118 } |
| 148 | 119 |
| OLD | NEW |