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

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: 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 }
dsinclair 2016/03/10 21:23:45 nit: {}s
Tom Sepez 2016/03/10 22:05:19 Done.
68 m_dwSize = size;
69 if (m_pDict) {
70 m_pDict->SetAtInteger("Length", size);
dsinclair 2016/03/10 21:23:45 nit: {}s
Tom Sepez 2016/03/10 22:05:19 Done.
71 }
72 }
73
74 CPDF_Object* CPDF_Stream::Clone(FX_BOOL bDirect) const {
75 CPDF_StreamAcc acc;
76 acc.LoadAllData(this, TRUE);
77 FX_DWORD streamSize = acc.GetSize();
78 CPDF_Dictionary* pDict = GetDict();
79 if (pDict) {
80 pDict = ToDictionary(pDict->Clone(bDirect));
81 }
dsinclair 2016/03/10 21:23:45 nit: {}s
Tom Sepez 2016/03/10 22:05:19 Done.
82 return new CPDF_Stream(acc.DetachData(), streamSize, pDict);
83 }
84
85 void CPDF_Stream::SetData(const uint8_t* pData,
86 FX_DWORD size,
87 FX_BOOL bCompressed,
88 FX_BOOL bKeepBuf) {
89 if (IsMemoryBased())
90 FX_Free(m_pDataBuf);
91 m_GenNum = kMemoryBasedGenNum;
92
93 if (bKeepBuf) {
94 m_pDataBuf = const_cast<uint8_t*>(pData);
95 } else {
96 m_pDataBuf = FX_Alloc(uint8_t, size);
97 if (pData) {
98 FXSYS_memcpy(m_pDataBuf, pData, size);
99 }
100 }
101 m_dwSize = size;
102 if (!m_pDict)
103 m_pDict = new CPDF_Dictionary;
104 m_pDict->SetAtInteger("Length", size);
105 if (!bCompressed) {
106 m_pDict->RemoveAt("Filter");
107 m_pDict->RemoveAt("DecodeParms");
108 }
109 }
110
111 FX_BOOL CPDF_Stream::ReadRawData(FX_FILESIZE offset,
112 uint8_t* buf,
113 FX_DWORD size) const {
114 if (!IsMemoryBased() && m_pFile)
115 return m_pFile->ReadBlock(buf, offset, size);
116
117 if (m_pDataBuf)
118 FXSYS_memcpy(buf, m_pDataBuf + offset, size);
119 return TRUE;
120 }
121 void CPDF_Stream::InitStreamFromFile(IFX_FileRead* pFile,
122 CPDF_Dictionary* pDict) {
123 InitStreamInternal(pDict);
124 m_pFile = pFile;
125 m_dwSize = (FX_DWORD)pFile->GetSize();
126 if (m_pDict) {
127 m_pDict->SetAtInteger("Length", m_dwSize);
128 }
dsinclair 2016/03/10 21:23:45 nit: {}
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() {
dsinclair 2016/03/10 21:23:45 Move to its own cpp file?
Tom Sepez 2016/03/10 22:05:19 See previous answer.
138 m_bNewBuf = FALSE;
dsinclair 2016/03/10 21:23:45 Move these to initializer list.
Tom Sepez 2016/03/10 22:05:19 Done.
139 m_pData = NULL;
140 m_dwSize = 0;
141 m_pImageParam = NULL;
142 m_pStream = NULL;
143 m_pSrcData = NULL;
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 = NULL;
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 NULL;
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 = NULL;
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

Powered by Google App Engine
This is Rietveld 408576698