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

Side by Side Diff: core/src/fxcrt/extension.h

Issue 1274883002: Remove the unused IFX_StreamRead::SetRange() mechanism. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Add protected: Created 5 years, 4 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
« no previous file with comments | « core/include/fxcrt/fx_stream.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 PDFium Authors. All rights reserved. 1 // Copyright 2014 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 #ifndef CORE_SRC_FXCRT_EXTENSION_H_ 7 #ifndef CORE_SRC_FXCRT_EXTENSION_H_
8 #define CORE_SRC_FXCRT_EXTENSION_H_ 8 #define CORE_SRC_FXCRT_EXTENSION_H_
9 9
10 #include "../../include/fxcrt/fx_basic.h" 10 #include "../../include/fxcrt/fx_basic.h"
(...skipping 17 matching lines...) Expand all
28 FX_FILESIZE pos) = 0; 28 FX_FILESIZE pos) = 0;
29 virtual FX_BOOL Flush() = 0; 29 virtual FX_BOOL Flush() = 0;
30 virtual FX_BOOL Truncate(FX_FILESIZE szFile) = 0; 30 virtual FX_BOOL Truncate(FX_FILESIZE szFile) = 0;
31 }; 31 };
32 IFXCRT_FileAccess* FXCRT_FileAccess_Create(); 32 IFXCRT_FileAccess* FXCRT_FileAccess_Create();
33 class CFX_CRTFileStream final : public IFX_FileStream { 33 class CFX_CRTFileStream final : public IFX_FileStream {
34 public: 34 public:
35 CFX_CRTFileStream(IFXCRT_FileAccess* pFA) 35 CFX_CRTFileStream(IFXCRT_FileAccess* pFA)
36 : m_pFile(pFA), 36 : m_pFile(pFA),
37 m_dwCount(1), 37 m_dwCount(1),
38 m_bUseRange(FALSE),
39 m_nOffset(0), 38 m_nOffset(0),
40 m_nSize(0) {} 39 m_nSize(0) {}
41 ~CFX_CRTFileStream() { 40 ~CFX_CRTFileStream() {
42 if (m_pFile) { 41 if (m_pFile) {
43 m_pFile->Release(); 42 m_pFile->Release();
44 } 43 }
45 } 44 }
46 virtual IFX_FileStream* Retain() override { 45 virtual IFX_FileStream* Retain() override {
47 m_dwCount++; 46 m_dwCount++;
48 return this; 47 return this;
49 } 48 }
50 virtual void Release() override { 49 virtual void Release() override {
51 FX_DWORD nCount = --m_dwCount; 50 FX_DWORD nCount = --m_dwCount;
52 if (!nCount) { 51 if (!nCount) {
53 delete this; 52 delete this;
54 } 53 }
55 } 54 }
56 virtual FX_FILESIZE GetSize() override { 55 virtual FX_FILESIZE GetSize() override {
57 return m_bUseRange ? m_nSize : m_pFile->GetSize(); 56 return m_pFile->GetSize();
58 } 57 }
59 virtual FX_BOOL IsEOF() override { return GetPosition() >= GetSize(); } 58 virtual FX_BOOL IsEOF() override { return GetPosition() >= GetSize(); }
60 virtual FX_FILESIZE GetPosition() override { 59 virtual FX_FILESIZE GetPosition() override {
61 FX_FILESIZE pos = m_pFile->GetPosition(); 60 return m_pFile->GetPosition();
62 if (m_bUseRange) {
63 pos -= m_nOffset;
64 }
65 return pos;
66 } 61 }
67 virtual FX_BOOL SetRange(FX_FILESIZE offset, FX_FILESIZE size) override {
68 if (offset < 0 || size < 0) {
69 return FALSE;
70 }
71
72 FX_SAFE_FILESIZE pos = size;
73 pos += offset;
74
75 if (!pos.IsValid() || pos.ValueOrDie() > m_pFile->GetSize()) {
76 return FALSE;
77 }
78
79 m_nOffset = offset, m_nSize = size;
80 m_bUseRange = TRUE;
81 m_pFile->SetPosition(m_nOffset);
82 return TRUE;
83 }
84 virtual void ClearRange() override { m_bUseRange = FALSE; }
85 virtual FX_BOOL ReadBlock(void* buffer, 62 virtual FX_BOOL ReadBlock(void* buffer,
86 FX_FILESIZE offset, 63 FX_FILESIZE offset,
87 size_t size) override { 64 size_t size) override {
88 if (m_bUseRange && offset < 0) { 65 return (FX_BOOL)m_pFile->ReadPos(buffer, size, offset);
89 return FALSE;
90 }
91 FX_SAFE_FILESIZE pos = offset;
92
93 if (m_bUseRange) {
94 pos += m_nOffset;
95 if (!pos.IsValid() || pos.ValueOrDie() > (size_t)GetSize()) {
96 return FALSE;
97 }
98 }
99 return (FX_BOOL)m_pFile->ReadPos(buffer, size, pos.ValueOrDie());
100 } 66 }
101 virtual size_t ReadBlock(void* buffer, size_t size) override { 67 virtual size_t ReadBlock(void* buffer, size_t size) override {
102 if (m_bUseRange) {
103 FX_FILESIZE availSize = m_nOffset + m_nSize - m_pFile->GetPosition();
104 if ((size_t)availSize < size) {
105 size -= size - (size_t)availSize;
106 }
107 }
108 return m_pFile->Read(buffer, size); 68 return m_pFile->Read(buffer, size);
109 } 69 }
110 virtual FX_BOOL WriteBlock(const void* buffer, 70 virtual FX_BOOL WriteBlock(const void* buffer,
111 FX_FILESIZE offset, 71 FX_FILESIZE offset,
112 size_t size) override { 72 size_t size) override {
113 if (m_bUseRange) {
114 offset += m_nOffset;
115 }
116 return (FX_BOOL)m_pFile->WritePos(buffer, size, offset); 73 return (FX_BOOL)m_pFile->WritePos(buffer, size, offset);
117 } 74 }
118 virtual FX_BOOL Flush() override { return m_pFile->Flush(); } 75 virtual FX_BOOL Flush() override { return m_pFile->Flush(); }
76
77 protected:
119 IFXCRT_FileAccess* m_pFile; 78 IFXCRT_FileAccess* m_pFile;
120 FX_DWORD m_dwCount; 79 FX_DWORD m_dwCount;
121 FX_BOOL m_bUseRange;
122 FX_FILESIZE m_nOffset; 80 FX_FILESIZE m_nOffset;
Lei Zhang 2015/08/05 21:05:39 Is this also no longer used? Same for m_nSize, and
Tom Sepez 2015/08/05 22:09:28 Indeed. Done.
123 FX_FILESIZE m_nSize; 81 FX_FILESIZE m_nSize;
124 }; 82 };
125 #define FX_MEMSTREAM_BlockSize (64 * 1024) 83 #define FX_MEMSTREAM_BlockSize (64 * 1024)
126 #define FX_MEMSTREAM_Consecutive 0x01 84 #define FX_MEMSTREAM_Consecutive 0x01
127 #define FX_MEMSTREAM_TakeOver 0x02 85 #define FX_MEMSTREAM_TakeOver 0x02
128 class CFX_MemoryStream final : public IFX_MemoryStream { 86 class CFX_MemoryStream final : public IFX_MemoryStream {
129 public: 87 public:
130 CFX_MemoryStream(FX_BOOL bConsecutive) 88 CFX_MemoryStream(FX_BOOL bConsecutive)
131 : m_dwCount(1), 89 : m_dwCount(1),
132 m_nTotalSize(0), 90 m_nTotalSize(0),
133 m_nCurSize(0), 91 m_nCurSize(0),
134 m_nCurPos(0), 92 m_nCurPos(0),
135 m_nGrowSize(FX_MEMSTREAM_BlockSize), 93 m_nGrowSize(FX_MEMSTREAM_BlockSize) {
136 m_bUseRange(FALSE) {
137 m_dwFlags = 94 m_dwFlags =
138 FX_MEMSTREAM_TakeOver | (bConsecutive ? FX_MEMSTREAM_Consecutive : 0); 95 FX_MEMSTREAM_TakeOver | (bConsecutive ? FX_MEMSTREAM_Consecutive : 0);
139 } 96 }
140 CFX_MemoryStream(uint8_t* pBuffer, size_t nSize, FX_BOOL bTakeOver) 97 CFX_MemoryStream(uint8_t* pBuffer, size_t nSize, FX_BOOL bTakeOver)
141 : m_dwCount(1), 98 : m_dwCount(1),
142 m_nTotalSize(nSize), 99 m_nTotalSize(nSize),
143 m_nCurSize(nSize), 100 m_nCurSize(nSize),
144 m_nCurPos(0), 101 m_nCurPos(0),
145 m_nGrowSize(FX_MEMSTREAM_BlockSize), 102 m_nGrowSize(FX_MEMSTREAM_BlockSize) {
146 m_bUseRange(FALSE) {
147 m_Blocks.Add(pBuffer); 103 m_Blocks.Add(pBuffer);
148 m_dwFlags = 104 m_dwFlags =
149 FX_MEMSTREAM_Consecutive | (bTakeOver ? FX_MEMSTREAM_TakeOver : 0); 105 FX_MEMSTREAM_Consecutive | (bTakeOver ? FX_MEMSTREAM_TakeOver : 0);
150 } 106 }
151 ~CFX_MemoryStream() { 107 ~CFX_MemoryStream() {
152 if (m_dwFlags & FX_MEMSTREAM_TakeOver) { 108 if (m_dwFlags & FX_MEMSTREAM_TakeOver) {
153 for (int32_t i = 0; i < m_Blocks.GetSize(); i++) { 109 for (int32_t i = 0; i < m_Blocks.GetSize(); i++) {
154 FX_Free((uint8_t*)m_Blocks[i]); 110 FX_Free((uint8_t*)m_Blocks[i]);
155 } 111 }
156 } 112 }
157 m_Blocks.RemoveAll(); 113 m_Blocks.RemoveAll();
158 } 114 }
159 virtual IFX_FileStream* Retain() override { 115 virtual IFX_FileStream* Retain() override {
160 m_dwCount++; 116 m_dwCount++;
161 return this; 117 return this;
162 } 118 }
163 virtual void Release() override { 119 virtual void Release() override {
164 FX_DWORD nCount = --m_dwCount; 120 FX_DWORD nCount = --m_dwCount;
165 if (nCount) { 121 if (nCount) {
166 return; 122 return;
167 } 123 }
168 delete this; 124 delete this;
169 } 125 }
170 virtual FX_FILESIZE GetSize() override { 126 virtual FX_FILESIZE GetSize() override {
171 return m_bUseRange ? (FX_FILESIZE)m_nSize : (FX_FILESIZE)m_nCurSize; 127 return (FX_FILESIZE)m_nCurSize;
172 } 128 }
173 virtual FX_BOOL IsEOF() override { return m_nCurPos >= (size_t)GetSize(); } 129 virtual FX_BOOL IsEOF() override { return m_nCurPos >= (size_t)GetSize(); }
174 virtual FX_FILESIZE GetPosition() override { 130 virtual FX_FILESIZE GetPosition() override {
175 FX_FILESIZE pos = (FX_FILESIZE)m_nCurPos; 131 return (FX_FILESIZE)m_nCurPos;
176 if (m_bUseRange) {
177 pos -= (FX_FILESIZE)m_nOffset;
178 }
179 return pos;
180 } 132 }
181 virtual FX_BOOL SetRange(FX_FILESIZE offset, FX_FILESIZE size) override {
182 if (offset < 0 || size < 0) {
183 return FALSE;
184 }
185 FX_SAFE_FILESIZE range = size;
186 range += offset;
187 if (!range.IsValid() || range.ValueOrDie() > m_nCurSize) {
188 return FALSE;
189 }
190
191 m_nOffset = (size_t)offset, m_nSize = (size_t)size;
192 m_bUseRange = TRUE;
193 m_nCurPos = m_nOffset;
194 return TRUE;
195 }
196 virtual void ClearRange() override { m_bUseRange = FALSE; }
197 virtual FX_BOOL ReadBlock(void* buffer, 133 virtual FX_BOOL ReadBlock(void* buffer,
198 FX_FILESIZE offset, 134 FX_FILESIZE offset,
199 size_t size) override { 135 size_t size) override {
200 if (!buffer || !size) { 136 if (!buffer || !size) {
201 return FALSE; 137 return FALSE;
202 } 138 }
203 139
204 FX_SAFE_FILESIZE safeOffset = offset;
205 if (m_bUseRange) {
206 safeOffset += m_nOffset;
207 }
208
209 if (!safeOffset.IsValid()) {
210 return FALSE;
211 }
212
213 offset = safeOffset.ValueOrDie();
214
215 FX_SAFE_SIZE_T newPos = size; 140 FX_SAFE_SIZE_T newPos = size;
216 newPos += offset; 141 newPos += offset;
217 if (!newPos.IsValid() || newPos.ValueOrDefault(0) == 0 || 142 if (!newPos.IsValid() || newPos.ValueOrDefault(0) == 0 ||
218 newPos.ValueOrDie() > m_nCurSize) { 143 newPos.ValueOrDie() > m_nCurSize) {
219 return FALSE; 144 return FALSE;
220 } 145 }
221 146
222 m_nCurPos = newPos.ValueOrDie(); 147 m_nCurPos = newPos.ValueOrDie();
223 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { 148 if (m_dwFlags & FX_MEMSTREAM_Consecutive) {
224 FXSYS_memcpy(buffer, (uint8_t*)m_Blocks[0] + (size_t)offset, size); 149 FXSYS_memcpy(buffer, (uint8_t*)m_Blocks[0] + (size_t)offset, size);
(...skipping 12 matching lines...) Expand all
237 size -= nRead; 162 size -= nRead;
238 nStartBlock++; 163 nStartBlock++;
239 offset = 0; 164 offset = 0;
240 } 165 }
241 return TRUE; 166 return TRUE;
242 } 167 }
243 virtual size_t ReadBlock(void* buffer, size_t size) override { 168 virtual size_t ReadBlock(void* buffer, size_t size) override {
244 if (m_nCurPos >= m_nCurSize) { 169 if (m_nCurPos >= m_nCurSize) {
245 return 0; 170 return 0;
246 } 171 }
247 if (m_bUseRange) {
248 size_t availSize = m_nOffset + m_nSize - m_nCurPos;
249 if (availSize < size) {
250 size -= size - (size_t)availSize;
251 }
252 }
253 size_t nRead = FX_MIN(size, m_nCurSize - m_nCurPos); 172 size_t nRead = FX_MIN(size, m_nCurSize - m_nCurPos);
254 if (!ReadBlock(buffer, (int32_t)m_nCurPos, nRead)) { 173 if (!ReadBlock(buffer, (int32_t)m_nCurPos, nRead)) {
255 return 0; 174 return 0;
256 } 175 }
257 return nRead; 176 return nRead;
258 } 177 }
259 virtual FX_BOOL WriteBlock(const void* buffer, 178 virtual FX_BOOL WriteBlock(const void* buffer,
260 FX_FILESIZE offset, 179 FX_FILESIZE offset,
261 size_t size) override { 180 size_t size) override {
262 if (!buffer || !size) { 181 if (!buffer || !size) {
263 return FALSE; 182 return FALSE;
264 } 183 }
265 if (m_bUseRange) {
266 offset += (FX_FILESIZE)m_nOffset;
267 }
268 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { 184 if (m_dwFlags & FX_MEMSTREAM_Consecutive) {
269 FX_SAFE_SIZE_T newPos = size; 185 FX_SAFE_SIZE_T newPos = size;
270 newPos += offset; 186 newPos += offset;
271 if (!newPos.IsValid()) 187 if (!newPos.IsValid())
272 return FALSE; 188 return FALSE;
273 189
274 m_nCurPos = newPos.ValueOrDie(); 190 m_nCurPos = newPos.ValueOrDie();
275 if (m_nCurPos > m_nTotalSize) { 191 if (m_nCurPos > m_nTotalSize) {
276 m_nTotalSize = 192 m_nTotalSize =
277 (m_nCurPos + m_nGrowSize - 1) / m_nGrowSize * m_nGrowSize; 193 (m_nCurPos + m_nGrowSize - 1) / m_nGrowSize * m_nGrowSize;
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
342 FX_BOOL bTakeOver = FALSE) override { 258 FX_BOOL bTakeOver = FALSE) override {
343 if (!(m_dwFlags & FX_MEMSTREAM_Consecutive)) { 259 if (!(m_dwFlags & FX_MEMSTREAM_Consecutive)) {
344 return; 260 return;
345 } 261 }
346 m_Blocks.RemoveAll(); 262 m_Blocks.RemoveAll();
347 m_Blocks.Add(pBuffer); 263 m_Blocks.Add(pBuffer);
348 m_nTotalSize = m_nCurSize = nSize; 264 m_nTotalSize = m_nCurSize = nSize;
349 m_nCurPos = 0; 265 m_nCurPos = 0;
350 m_dwFlags = 266 m_dwFlags =
351 FX_MEMSTREAM_Consecutive | (bTakeOver ? FX_MEMSTREAM_TakeOver : 0); 267 FX_MEMSTREAM_Consecutive | (bTakeOver ? FX_MEMSTREAM_TakeOver : 0);
352 ClearRange();
353 } 268 }
354 virtual void DetachBuffer() override { 269 virtual void DetachBuffer() override {
355 if (!(m_dwFlags & FX_MEMSTREAM_Consecutive)) { 270 if (!(m_dwFlags & FX_MEMSTREAM_Consecutive)) {
356 return; 271 return;
357 } 272 }
358 m_Blocks.RemoveAll(); 273 m_Blocks.RemoveAll();
359 m_nTotalSize = m_nCurSize = m_nCurPos = 0; 274 m_nTotalSize = m_nCurSize = m_nCurPos = 0;
360 m_dwFlags = FX_MEMSTREAM_TakeOver; 275 m_dwFlags = FX_MEMSTREAM_TakeOver;
361 ClearRange();
362 } 276 }
363 277
364 protected: 278 protected:
365 CFX_PtrArray m_Blocks; 279 CFX_PtrArray m_Blocks;
366 FX_DWORD m_dwCount; 280 FX_DWORD m_dwCount;
367 size_t m_nTotalSize; 281 size_t m_nTotalSize;
368 size_t m_nCurSize; 282 size_t m_nCurSize;
369 size_t m_nCurPos; 283 size_t m_nCurPos;
370 size_t m_nGrowSize; 284 size_t m_nGrowSize;
371 FX_DWORD m_dwFlags; 285 FX_DWORD m_dwFlags;
372 FX_BOOL m_bUseRange;
373 size_t m_nOffset; 286 size_t m_nOffset;
374 size_t m_nSize; 287 size_t m_nSize;
375 FX_BOOL ExpandBlocks(size_t size) { 288 FX_BOOL ExpandBlocks(size_t size) {
376 if (m_nCurSize < size) { 289 if (m_nCurSize < size) {
377 m_nCurSize = size; 290 m_nCurSize = size;
378 } 291 }
379 if (size <= m_nTotalSize) { 292 if (size <= m_nTotalSize) {
380 return TRUE; 293 return TRUE;
381 } 294 }
382 int32_t iCount = m_Blocks.GetSize(); 295 int32_t iCount = m_Blocks.GetSize();
(...skipping 26 matching lines...) Expand all
409 } FX_MTRANDOMCONTEXT, *FX_LPMTRANDOMCONTEXT; 322 } FX_MTRANDOMCONTEXT, *FX_LPMTRANDOMCONTEXT;
410 typedef FX_MTRANDOMCONTEXT const* FX_LPCMTRANDOMCONTEXT; 323 typedef FX_MTRANDOMCONTEXT const* FX_LPCMTRANDOMCONTEXT;
411 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ 324 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
412 FX_BOOL FX_GenerateCryptoRandom(FX_DWORD* pBuffer, int32_t iCount); 325 FX_BOOL FX_GenerateCryptoRandom(FX_DWORD* pBuffer, int32_t iCount);
413 #endif 326 #endif
414 #ifdef __cplusplus 327 #ifdef __cplusplus
415 } 328 }
416 #endif 329 #endif
417 330
418 #endif // CORE_SRC_FXCRT_EXTENSION_H_ 331 #endif // CORE_SRC_FXCRT_EXTENSION_H_
OLDNEW
« no previous file with comments | « core/include/fxcrt/fx_stream.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698