OLD | NEW |
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 16 matching lines...) Expand all Loading... |
27 size_t szBuffer, | 27 size_t szBuffer, |
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), | 38 } |
39 m_nOffset(0), | |
40 m_nSize(0) {} | |
41 ~CFX_CRTFileStream() { | 39 ~CFX_CRTFileStream() { |
42 if (m_pFile) { | 40 if (m_pFile) { |
43 m_pFile->Release(); | 41 m_pFile->Release(); |
44 } | 42 } |
45 } | 43 } |
46 virtual IFX_FileStream* Retain() override { | 44 virtual IFX_FileStream* Retain() override { |
47 m_dwCount++; | 45 m_dwCount++; |
48 return this; | 46 return this; |
49 } | 47 } |
50 virtual void Release() override { | 48 virtual void Release() override { |
51 FX_DWORD nCount = --m_dwCount; | 49 FX_DWORD nCount = --m_dwCount; |
52 if (!nCount) { | 50 if (!nCount) { |
53 delete this; | 51 delete this; |
54 } | 52 } |
55 } | 53 } |
56 virtual FX_FILESIZE GetSize() override { | 54 virtual FX_FILESIZE GetSize() override { |
57 return m_bUseRange ? m_nSize : m_pFile->GetSize(); | 55 return m_pFile->GetSize(); |
58 } | 56 } |
59 virtual FX_BOOL IsEOF() override { return GetPosition() >= GetSize(); } | 57 virtual FX_BOOL IsEOF() override { return GetPosition() >= GetSize(); } |
60 virtual FX_FILESIZE GetPosition() override { | 58 virtual FX_FILESIZE GetPosition() override { |
61 FX_FILESIZE pos = m_pFile->GetPosition(); | 59 return m_pFile->GetPosition(); |
62 if (m_bUseRange) { | |
63 pos -= m_nOffset; | |
64 } | |
65 return pos; | |
66 } | 60 } |
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, | 61 virtual FX_BOOL ReadBlock(void* buffer, |
86 FX_FILESIZE offset, | 62 FX_FILESIZE offset, |
87 size_t size) override { | 63 size_t size) override { |
88 if (m_bUseRange && offset < 0) { | 64 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 } | 65 } |
101 virtual size_t ReadBlock(void* buffer, size_t size) override { | 66 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); | 67 return m_pFile->Read(buffer, size); |
109 } | 68 } |
110 virtual FX_BOOL WriteBlock(const void* buffer, | 69 virtual FX_BOOL WriteBlock(const void* buffer, |
111 FX_FILESIZE offset, | 70 FX_FILESIZE offset, |
112 size_t size) override { | 71 size_t size) override { |
113 if (m_bUseRange) { | |
114 offset += m_nOffset; | |
115 } | |
116 return (FX_BOOL)m_pFile->WritePos(buffer, size, offset); | 72 return (FX_BOOL)m_pFile->WritePos(buffer, size, offset); |
117 } | 73 } |
118 virtual FX_BOOL Flush() override { return m_pFile->Flush(); } | 74 virtual FX_BOOL Flush() override { return m_pFile->Flush(); } |
| 75 |
| 76 protected: |
119 IFXCRT_FileAccess* m_pFile; | 77 IFXCRT_FileAccess* m_pFile; |
120 FX_DWORD m_dwCount; | 78 FX_DWORD m_dwCount; |
121 FX_BOOL m_bUseRange; | |
122 FX_FILESIZE m_nOffset; | |
123 FX_FILESIZE m_nSize; | |
124 }; | 79 }; |
125 #define FX_MEMSTREAM_BlockSize (64 * 1024) | 80 #define FX_MEMSTREAM_BlockSize (64 * 1024) |
126 #define FX_MEMSTREAM_Consecutive 0x01 | 81 #define FX_MEMSTREAM_Consecutive 0x01 |
127 #define FX_MEMSTREAM_TakeOver 0x02 | 82 #define FX_MEMSTREAM_TakeOver 0x02 |
128 class CFX_MemoryStream final : public IFX_MemoryStream { | 83 class CFX_MemoryStream final : public IFX_MemoryStream { |
129 public: | 84 public: |
130 CFX_MemoryStream(FX_BOOL bConsecutive) | 85 CFX_MemoryStream(FX_BOOL bConsecutive) |
131 : m_dwCount(1), | 86 : m_dwCount(1), |
132 m_nTotalSize(0), | 87 m_nTotalSize(0), |
133 m_nCurSize(0), | 88 m_nCurSize(0), |
134 m_nCurPos(0), | 89 m_nCurPos(0), |
135 m_nGrowSize(FX_MEMSTREAM_BlockSize), | 90 m_nGrowSize(FX_MEMSTREAM_BlockSize) { |
136 m_bUseRange(FALSE) { | |
137 m_dwFlags = | 91 m_dwFlags = |
138 FX_MEMSTREAM_TakeOver | (bConsecutive ? FX_MEMSTREAM_Consecutive : 0); | 92 FX_MEMSTREAM_TakeOver | (bConsecutive ? FX_MEMSTREAM_Consecutive : 0); |
139 } | 93 } |
140 CFX_MemoryStream(uint8_t* pBuffer, size_t nSize, FX_BOOL bTakeOver) | 94 CFX_MemoryStream(uint8_t* pBuffer, size_t nSize, FX_BOOL bTakeOver) |
141 : m_dwCount(1), | 95 : m_dwCount(1), |
142 m_nTotalSize(nSize), | 96 m_nTotalSize(nSize), |
143 m_nCurSize(nSize), | 97 m_nCurSize(nSize), |
144 m_nCurPos(0), | 98 m_nCurPos(0), |
145 m_nGrowSize(FX_MEMSTREAM_BlockSize), | 99 m_nGrowSize(FX_MEMSTREAM_BlockSize) { |
146 m_bUseRange(FALSE) { | |
147 m_Blocks.Add(pBuffer); | 100 m_Blocks.Add(pBuffer); |
148 m_dwFlags = | 101 m_dwFlags = |
149 FX_MEMSTREAM_Consecutive | (bTakeOver ? FX_MEMSTREAM_TakeOver : 0); | 102 FX_MEMSTREAM_Consecutive | (bTakeOver ? FX_MEMSTREAM_TakeOver : 0); |
150 } | 103 } |
151 ~CFX_MemoryStream() { | 104 ~CFX_MemoryStream() { |
152 if (m_dwFlags & FX_MEMSTREAM_TakeOver) { | 105 if (m_dwFlags & FX_MEMSTREAM_TakeOver) { |
153 for (int32_t i = 0; i < m_Blocks.GetSize(); i++) { | 106 for (int32_t i = 0; i < m_Blocks.GetSize(); i++) { |
154 FX_Free((uint8_t*)m_Blocks[i]); | 107 FX_Free((uint8_t*)m_Blocks[i]); |
155 } | 108 } |
156 } | 109 } |
157 m_Blocks.RemoveAll(); | 110 m_Blocks.RemoveAll(); |
158 } | 111 } |
159 virtual IFX_FileStream* Retain() override { | 112 virtual IFX_FileStream* Retain() override { |
160 m_dwCount++; | 113 m_dwCount++; |
161 return this; | 114 return this; |
162 } | 115 } |
163 virtual void Release() override { | 116 virtual void Release() override { |
164 FX_DWORD nCount = --m_dwCount; | 117 FX_DWORD nCount = --m_dwCount; |
165 if (nCount) { | 118 if (nCount) { |
166 return; | 119 return; |
167 } | 120 } |
168 delete this; | 121 delete this; |
169 } | 122 } |
170 virtual FX_FILESIZE GetSize() override { | 123 virtual FX_FILESIZE GetSize() override { |
171 return m_bUseRange ? (FX_FILESIZE)m_nSize : (FX_FILESIZE)m_nCurSize; | 124 return (FX_FILESIZE)m_nCurSize; |
172 } | 125 } |
173 virtual FX_BOOL IsEOF() override { return m_nCurPos >= (size_t)GetSize(); } | 126 virtual FX_BOOL IsEOF() override { return m_nCurPos >= (size_t)GetSize(); } |
174 virtual FX_FILESIZE GetPosition() override { | 127 virtual FX_FILESIZE GetPosition() override { |
175 FX_FILESIZE pos = (FX_FILESIZE)m_nCurPos; | 128 return (FX_FILESIZE)m_nCurPos; |
176 if (m_bUseRange) { | |
177 pos -= (FX_FILESIZE)m_nOffset; | |
178 } | |
179 return pos; | |
180 } | 129 } |
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, | 130 virtual FX_BOOL ReadBlock(void* buffer, |
198 FX_FILESIZE offset, | 131 FX_FILESIZE offset, |
199 size_t size) override { | 132 size_t size) override { |
200 if (!buffer || !size) { | 133 if (!buffer || !size) { |
201 return FALSE; | 134 return FALSE; |
202 } | 135 } |
203 | 136 |
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; | 137 FX_SAFE_SIZE_T newPos = size; |
216 newPos += offset; | 138 newPos += offset; |
217 if (!newPos.IsValid() || newPos.ValueOrDefault(0) == 0 || | 139 if (!newPos.IsValid() || newPos.ValueOrDefault(0) == 0 || |
218 newPos.ValueOrDie() > m_nCurSize) { | 140 newPos.ValueOrDie() > m_nCurSize) { |
219 return FALSE; | 141 return FALSE; |
220 } | 142 } |
221 | 143 |
222 m_nCurPos = newPos.ValueOrDie(); | 144 m_nCurPos = newPos.ValueOrDie(); |
223 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { | 145 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { |
224 FXSYS_memcpy(buffer, (uint8_t*)m_Blocks[0] + (size_t)offset, size); | 146 FXSYS_memcpy(buffer, (uint8_t*)m_Blocks[0] + (size_t)offset, size); |
(...skipping 12 matching lines...) Expand all Loading... |
237 size -= nRead; | 159 size -= nRead; |
238 nStartBlock++; | 160 nStartBlock++; |
239 offset = 0; | 161 offset = 0; |
240 } | 162 } |
241 return TRUE; | 163 return TRUE; |
242 } | 164 } |
243 virtual size_t ReadBlock(void* buffer, size_t size) override { | 165 virtual size_t ReadBlock(void* buffer, size_t size) override { |
244 if (m_nCurPos >= m_nCurSize) { | 166 if (m_nCurPos >= m_nCurSize) { |
245 return 0; | 167 return 0; |
246 } | 168 } |
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); | 169 size_t nRead = FX_MIN(size, m_nCurSize - m_nCurPos); |
254 if (!ReadBlock(buffer, (int32_t)m_nCurPos, nRead)) { | 170 if (!ReadBlock(buffer, (int32_t)m_nCurPos, nRead)) { |
255 return 0; | 171 return 0; |
256 } | 172 } |
257 return nRead; | 173 return nRead; |
258 } | 174 } |
259 virtual FX_BOOL WriteBlock(const void* buffer, | 175 virtual FX_BOOL WriteBlock(const void* buffer, |
260 FX_FILESIZE offset, | 176 FX_FILESIZE offset, |
261 size_t size) override { | 177 size_t size) override { |
262 if (!buffer || !size) { | 178 if (!buffer || !size) { |
263 return FALSE; | 179 return FALSE; |
264 } | 180 } |
265 if (m_bUseRange) { | |
266 offset += (FX_FILESIZE)m_nOffset; | |
267 } | |
268 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { | 181 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { |
269 FX_SAFE_SIZE_T newPos = size; | 182 FX_SAFE_SIZE_T newPos = size; |
270 newPos += offset; | 183 newPos += offset; |
271 if (!newPos.IsValid()) | 184 if (!newPos.IsValid()) |
272 return FALSE; | 185 return FALSE; |
273 | 186 |
274 m_nCurPos = newPos.ValueOrDie(); | 187 m_nCurPos = newPos.ValueOrDie(); |
275 if (m_nCurPos > m_nTotalSize) { | 188 if (m_nCurPos > m_nTotalSize) { |
276 m_nTotalSize = | 189 m_nTotalSize = |
277 (m_nCurPos + m_nGrowSize - 1) / m_nGrowSize * m_nGrowSize; | 190 (m_nCurPos + m_nGrowSize - 1) / m_nGrowSize * m_nGrowSize; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
342 FX_BOOL bTakeOver = FALSE) override { | 255 FX_BOOL bTakeOver = FALSE) override { |
343 if (!(m_dwFlags & FX_MEMSTREAM_Consecutive)) { | 256 if (!(m_dwFlags & FX_MEMSTREAM_Consecutive)) { |
344 return; | 257 return; |
345 } | 258 } |
346 m_Blocks.RemoveAll(); | 259 m_Blocks.RemoveAll(); |
347 m_Blocks.Add(pBuffer); | 260 m_Blocks.Add(pBuffer); |
348 m_nTotalSize = m_nCurSize = nSize; | 261 m_nTotalSize = m_nCurSize = nSize; |
349 m_nCurPos = 0; | 262 m_nCurPos = 0; |
350 m_dwFlags = | 263 m_dwFlags = |
351 FX_MEMSTREAM_Consecutive | (bTakeOver ? FX_MEMSTREAM_TakeOver : 0); | 264 FX_MEMSTREAM_Consecutive | (bTakeOver ? FX_MEMSTREAM_TakeOver : 0); |
352 ClearRange(); | |
353 } | 265 } |
354 virtual void DetachBuffer() override { | 266 virtual void DetachBuffer() override { |
355 if (!(m_dwFlags & FX_MEMSTREAM_Consecutive)) { | 267 if (!(m_dwFlags & FX_MEMSTREAM_Consecutive)) { |
356 return; | 268 return; |
357 } | 269 } |
358 m_Blocks.RemoveAll(); | 270 m_Blocks.RemoveAll(); |
359 m_nTotalSize = m_nCurSize = m_nCurPos = 0; | 271 m_nTotalSize = m_nCurSize = m_nCurPos = 0; |
360 m_dwFlags = FX_MEMSTREAM_TakeOver; | 272 m_dwFlags = FX_MEMSTREAM_TakeOver; |
361 ClearRange(); | |
362 } | 273 } |
363 | 274 |
364 protected: | 275 protected: |
365 CFX_PtrArray m_Blocks; | 276 CFX_PtrArray m_Blocks; |
366 FX_DWORD m_dwCount; | 277 FX_DWORD m_dwCount; |
367 size_t m_nTotalSize; | 278 size_t m_nTotalSize; |
368 size_t m_nCurSize; | 279 size_t m_nCurSize; |
369 size_t m_nCurPos; | 280 size_t m_nCurPos; |
370 size_t m_nGrowSize; | 281 size_t m_nGrowSize; |
371 FX_DWORD m_dwFlags; | 282 FX_DWORD m_dwFlags; |
372 FX_BOOL m_bUseRange; | |
373 size_t m_nOffset; | |
374 size_t m_nSize; | |
375 FX_BOOL ExpandBlocks(size_t size) { | 283 FX_BOOL ExpandBlocks(size_t size) { |
376 if (m_nCurSize < size) { | 284 if (m_nCurSize < size) { |
377 m_nCurSize = size; | 285 m_nCurSize = size; |
378 } | 286 } |
379 if (size <= m_nTotalSize) { | 287 if (size <= m_nTotalSize) { |
380 return TRUE; | 288 return TRUE; |
381 } | 289 } |
382 int32_t iCount = m_Blocks.GetSize(); | 290 int32_t iCount = m_Blocks.GetSize(); |
383 size = (size - m_nTotalSize + m_nGrowSize - 1) / m_nGrowSize; | 291 size = (size - m_nTotalSize + m_nGrowSize - 1) / m_nGrowSize; |
384 m_Blocks.SetSize(m_Blocks.GetSize() + (int32_t)size); | 292 m_Blocks.SetSize(m_Blocks.GetSize() + (int32_t)size); |
(...skipping 24 matching lines...) Expand all Loading... |
409 } FX_MTRANDOMCONTEXT, *FX_LPMTRANDOMCONTEXT; | 317 } FX_MTRANDOMCONTEXT, *FX_LPMTRANDOMCONTEXT; |
410 typedef FX_MTRANDOMCONTEXT const* FX_LPCMTRANDOMCONTEXT; | 318 typedef FX_MTRANDOMCONTEXT const* FX_LPCMTRANDOMCONTEXT; |
411 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 319 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
412 FX_BOOL FX_GenerateCryptoRandom(FX_DWORD* pBuffer, int32_t iCount); | 320 FX_BOOL FX_GenerateCryptoRandom(FX_DWORD* pBuffer, int32_t iCount); |
413 #endif | 321 #endif |
414 #ifdef __cplusplus | 322 #ifdef __cplusplus |
415 } | 323 } |
416 #endif | 324 #endif |
417 | 325 |
418 #endif // CORE_SRC_FXCRT_EXTENSION_H_ | 326 #endif // CORE_SRC_FXCRT_EXTENSION_H_ |
OLD | NEW |