Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: core/src/fpdfapi/fpdf_parser/cpdf_stream.cpp

Issue 1776913007: Split fpdf_parser_objects.cpp into per-class .cpp/.h files. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Address comments Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/include/fpdfapi/cpdf_stream.h"
8
9 #include "core/include/fpdfapi/cpdf_dictionary.h"
10 #include "core/include/fpdfapi/fpdf_parser_decode.h"
11
12 CPDF_Stream::CPDF_Stream(uint8_t* pData, FX_DWORD size, CPDF_Dictionary* pDict)
13 : m_pDict(pDict),
14 m_dwSize(size),
15 m_GenNum(kMemoryBasedGenNum),
16 m_pDataBuf(pData) {}
17
18 CPDF_Stream::~CPDF_Stream() {
19 if (IsMemoryBased())
20 FX_Free(m_pDataBuf);
21
22 if (m_pDict)
23 m_pDict->Release();
24 }
25
26 CPDF_Object::Type CPDF_Stream::GetType() const {
27 return STREAM;
28 }
29
30 CPDF_Dictionary* CPDF_Stream::GetDict() const {
31 return m_pDict;
32 }
33
34 bool CPDF_Stream::IsStream() const {
35 return true;
36 }
37
38 CPDF_Stream* CPDF_Stream::AsStream() {
39 return this;
40 }
41
42 const CPDF_Stream* CPDF_Stream::AsStream() const {
43 return this;
44 }
45
46 void CPDF_Stream::InitStreamInternal(CPDF_Dictionary* pDict) {
47 if (pDict) {
48 if (m_pDict)
49 m_pDict->Release();
50 m_pDict = pDict;
51 }
52 if (IsMemoryBased())
53 FX_Free(m_pDataBuf);
54
55 m_GenNum = 0;
56 m_pFile = nullptr;
57 }
58
59 void CPDF_Stream::InitStream(uint8_t* pData,
60 FX_DWORD size,
61 CPDF_Dictionary* pDict) {
62 InitStreamInternal(pDict);
63 m_GenNum = kMemoryBasedGenNum;
64 m_pDataBuf = FX_Alloc(uint8_t, size);
65 if (pData)
66 FXSYS_memcpy(m_pDataBuf, pData, size);
67
68 m_dwSize = size;
69 if (m_pDict)
70 m_pDict->SetAtInteger("Length", size);
71 }
72
73 CPDF_Object* CPDF_Stream::Clone(FX_BOOL bDirect) const {
74 CPDF_StreamAcc acc;
75 acc.LoadAllData(this, TRUE);
76 FX_DWORD streamSize = acc.GetSize();
77 CPDF_Dictionary* pDict = GetDict();
78 if (pDict)
79 pDict = ToDictionary(pDict->Clone(bDirect));
80
81 return new CPDF_Stream(acc.DetachData(), streamSize, pDict);
82 }
83
84 void CPDF_Stream::SetData(const uint8_t* pData,
85 FX_DWORD size,
86 FX_BOOL bCompressed,
87 FX_BOOL bKeepBuf) {
88 if (IsMemoryBased())
89 FX_Free(m_pDataBuf);
90 m_GenNum = kMemoryBasedGenNum;
91
92 if (bKeepBuf) {
93 m_pDataBuf = const_cast<uint8_t*>(pData);
94 } else {
95 m_pDataBuf = FX_Alloc(uint8_t, size);
96 if (pData) {
97 FXSYS_memcpy(m_pDataBuf, pData, size);
98 }
99 }
100 m_dwSize = size;
101 if (!m_pDict)
102 m_pDict = new CPDF_Dictionary;
103 m_pDict->SetAtInteger("Length", size);
104 if (!bCompressed) {
105 m_pDict->RemoveAt("Filter");
106 m_pDict->RemoveAt("DecodeParms");
107 }
108 }
109
110 FX_BOOL CPDF_Stream::ReadRawData(FX_FILESIZE offset,
111 uint8_t* buf,
112 FX_DWORD size) const {
113 if (!IsMemoryBased() && m_pFile)
114 return m_pFile->ReadBlock(buf, offset, size);
115
116 if (m_pDataBuf)
117 FXSYS_memcpy(buf, m_pDataBuf + offset, size);
118
119 return TRUE;
120 }
121
122 void CPDF_Stream::InitStreamFromFile(IFX_FileRead* pFile,
123 CPDF_Dictionary* pDict) {
124 InitStreamInternal(pDict);
125 m_pFile = pFile;
126 m_dwSize = (FX_DWORD)pFile->GetSize();
127 if (m_pDict)
128 m_pDict->SetAtInteger("Length", m_dwSize);
129 }
130
131 CFX_WideString CPDF_Stream::GetUnicodeText() const {
132 CPDF_StreamAcc stream;
133 stream.LoadAllData(this, FALSE);
134 return PDF_DecodeText(stream.GetData(), stream.GetSize());
135 }
136
137 CPDF_StreamAcc::CPDF_StreamAcc()
138 : m_pData(nullptr),
139 m_dwSize(0),
140 m_bNewBuf(FALSE),
141 m_pImageParam(nullptr),
142 m_pStream(nullptr),
143 m_pSrcData(nullptr) {}
144
145 void CPDF_StreamAcc::LoadAllData(const CPDF_Stream* pStream,
146 FX_BOOL bRawAccess,
147 FX_DWORD estimated_size,
148 FX_BOOL bImageAcc) {
149 if (!pStream)
150 return;
151
152 m_pStream = pStream;
153 if (pStream->IsMemoryBased() &&
154 (!pStream->GetDict()->KeyExist("Filter") || bRawAccess)) {
155 m_dwSize = pStream->GetRawSize();
156 m_pData = pStream->GetRawData();
157 return;
158 }
159 uint8_t* pSrcData;
160 FX_DWORD dwSrcSize = pStream->GetRawSize();
161 if (dwSrcSize == 0)
162 return;
163
164 if (!pStream->IsMemoryBased()) {
165 pSrcData = m_pSrcData = FX_Alloc(uint8_t, dwSrcSize);
166 if (!pStream->ReadRawData(0, pSrcData, dwSrcSize))
167 return;
168 } else {
169 pSrcData = pStream->GetRawData();
170 }
171 uint8_t* pDecryptedData = pSrcData;
172 FX_DWORD dwDecryptedSize = dwSrcSize;
173 if (!pStream->GetDict()->KeyExist("Filter") || bRawAccess) {
174 m_pData = pDecryptedData;
175 m_dwSize = dwDecryptedSize;
176 } else {
177 FX_BOOL bRet = PDF_DataDecode(
178 pDecryptedData, dwDecryptedSize, m_pStream->GetDict(), m_pData,
179 m_dwSize, m_ImageDecoder, m_pImageParam, estimated_size, bImageAcc);
180 if (!bRet) {
181 m_pData = pDecryptedData;
182 m_dwSize = dwDecryptedSize;
183 }
184 }
185 if (pSrcData != pStream->GetRawData() && pSrcData != m_pData) {
186 FX_Free(pSrcData);
187 }
188 if (pDecryptedData != pSrcData && pDecryptedData != m_pData) {
189 FX_Free(pDecryptedData);
190 }
191 m_pSrcData = nullptr;
192 m_bNewBuf = m_pData != pStream->GetRawData();
193 }
194
195 CPDF_StreamAcc::~CPDF_StreamAcc() {
196 if (m_bNewBuf) {
197 FX_Free(m_pData);
198 }
199 FX_Free(m_pSrcData);
200 }
201
202 const uint8_t* CPDF_StreamAcc::GetData() const {
203 if (m_bNewBuf) {
204 return m_pData;
205 }
206 if (!m_pStream) {
207 return nullptr;
208 }
209 return m_pStream->GetRawData();
210 }
211
212 FX_DWORD CPDF_StreamAcc::GetSize() const {
213 if (m_bNewBuf) {
214 return m_dwSize;
215 }
216 if (!m_pStream) {
217 return 0;
218 }
219 return m_pStream->GetRawSize();
220 }
221
222 uint8_t* CPDF_StreamAcc::DetachData() {
223 if (m_bNewBuf) {
224 uint8_t* p = m_pData;
225 m_pData = nullptr;
226 m_dwSize = 0;
227 return p;
228 }
229 uint8_t* p = FX_Alloc(uint8_t, m_dwSize);
230 FXSYS_memcpy(p, m_pData, m_dwSize);
231 return p;
232 }
OLDNEW
« no previous file with comments | « core/src/fpdfapi/fpdf_parser/cpdf_standard_security_handler.cpp ('k') | core/src/fpdfapi/fpdf_parser/cpdf_string.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698