OLD | NEW |
| (Empty) |
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 | |
7 #include "core/fpdfapi/fpdf_parser/cpdf_stream.h" | |
8 | |
9 #include "core/fpdfapi/fpdf_parser/cpdf_dictionary.h" | |
10 #include "core/fpdfapi/fpdf_parser/cpdf_stream_acc.h" | |
11 #include "core/fpdfapi/fpdf_parser/fpdf_parser_decode.h" | |
12 #include "third_party/base/numerics/safe_conversions.h" | |
13 #include "third_party/base/stl_util.h" | |
14 | |
15 CPDF_Stream::CPDF_Stream() {} | |
16 | |
17 CPDF_Stream::CPDF_Stream(uint8_t* pData, uint32_t size, CPDF_Dictionary* pDict) | |
18 : m_pDict(pDict), | |
19 m_dwSize(size), | |
20 m_pDataBuf(pData) {} | |
21 | |
22 CPDF_Stream::~CPDF_Stream() {} | |
23 | |
24 CPDF_Object::Type CPDF_Stream::GetType() const { | |
25 return STREAM; | |
26 } | |
27 | |
28 CPDF_Dictionary* CPDF_Stream::GetDict() const { | |
29 return m_pDict.get(); | |
30 } | |
31 | |
32 bool CPDF_Stream::IsStream() const { | |
33 return true; | |
34 } | |
35 | |
36 CPDF_Stream* CPDF_Stream::AsStream() { | |
37 return this; | |
38 } | |
39 | |
40 const CPDF_Stream* CPDF_Stream::AsStream() const { | |
41 return this; | |
42 } | |
43 | |
44 void CPDF_Stream::InitStream(const uint8_t* pData, | |
45 uint32_t size, | |
46 CPDF_Dictionary* pDict) { | |
47 m_pDict.reset(pDict); | |
48 m_bMemoryBased = true; | |
49 m_pFile = nullptr; | |
50 m_pDataBuf.reset(FX_Alloc(uint8_t, size)); | |
51 if (pData) | |
52 FXSYS_memcpy(m_pDataBuf.get(), pData, size); | |
53 m_dwSize = size; | |
54 if (m_pDict) | |
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); | |
67 } | |
68 | |
69 CPDF_Object* CPDF_Stream::Clone() const { | |
70 return CloneObjectNonCyclic(false); | |
71 } | |
72 | |
73 CPDF_Object* CPDF_Stream::CloneNonCyclic( | |
74 bool bDirect, | |
75 std::set<const CPDF_Object*>* pVisited) const { | |
76 pVisited->insert(this); | |
77 CPDF_StreamAcc acc; | |
78 acc.LoadAllData(this, TRUE); | |
79 uint32_t streamSize = acc.GetSize(); | |
80 CPDF_Dictionary* pDict = GetDict(); | |
81 if (pDict && !pdfium::ContainsKey(*pVisited, pDict)) { | |
82 pDict = ToDictionary( | |
83 static_cast<CPDF_Object*>(pDict)->CloneNonCyclic(bDirect, pVisited)); | |
84 } | |
85 | |
86 return new CPDF_Stream(acc.DetachData(), streamSize, pDict); | |
87 } | |
88 | |
89 void CPDF_Stream::SetData(const uint8_t* pData, uint32_t size) { | |
90 m_bMemoryBased = true; | |
91 m_pDataBuf.reset(FX_Alloc(uint8_t, size)); | |
92 if (pData) | |
93 FXSYS_memcpy(m_pDataBuf.get(), pData, size); | |
94 m_dwSize = size; | |
95 if (!m_pDict) | |
96 m_pDict.reset(new CPDF_Dictionary()); | |
97 m_pDict->SetIntegerFor("Length", size); | |
98 m_pDict->RemoveFor("Filter"); | |
99 m_pDict->RemoveFor("DecodeParms"); | |
100 } | |
101 | |
102 FX_BOOL CPDF_Stream::ReadRawData(FX_FILESIZE offset, | |
103 uint8_t* buf, | |
104 uint32_t size) const { | |
105 if (m_bMemoryBased && m_pFile) | |
106 return m_pFile->ReadBlock(buf, offset, size); | |
107 | |
108 if (m_pDataBuf) | |
109 FXSYS_memcpy(buf, m_pDataBuf.get() + offset, size); | |
110 | |
111 return TRUE; | |
112 } | |
113 | |
114 CFX_WideString CPDF_Stream::GetUnicodeText() const { | |
115 CPDF_StreamAcc stream; | |
116 stream.LoadAllData(this, FALSE); | |
117 return PDF_DecodeText(stream.GetData(), stream.GetSize()); | |
118 } | |
119 | |
OLD | NEW |