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 <algorithm> |
| 11 |
10 #include "core/include/fxcrt/fx_basic.h" | 12 #include "core/include/fxcrt/fx_basic.h" |
11 #include "core/include/fxcrt/fx_safe_types.h" | 13 #include "core/include/fxcrt/fx_safe_types.h" |
12 | 14 |
13 class IFXCRT_FileAccess { | 15 class IFXCRT_FileAccess { |
14 public: | 16 public: |
15 virtual ~IFXCRT_FileAccess() {} | 17 virtual ~IFXCRT_FileAccess() {} |
16 virtual FX_BOOL Open(const CFX_ByteStringC& fileName, FX_DWORD dwMode) = 0; | 18 virtual FX_BOOL Open(const CFX_ByteStringC& fileName, FX_DWORD dwMode) = 0; |
17 virtual FX_BOOL Open(const CFX_WideStringC& fileName, FX_DWORD dwMode) = 0; | 19 virtual FX_BOOL Open(const CFX_WideStringC& fileName, FX_DWORD dwMode) = 0; |
18 virtual void Close() = 0; | 20 virtual void Close() = 0; |
19 virtual void Release() = 0; | 21 virtual void Release() = 0; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 } | 62 } |
61 | 63 |
62 protected: | 64 protected: |
63 CFX_WideString m_path; | 65 CFX_WideString m_path; |
64 FX_DWORD m_RefCount; | 66 FX_DWORD m_RefCount; |
65 }; | 67 }; |
66 #endif // PDF_ENABLE_XFA | 68 #endif // PDF_ENABLE_XFA |
67 | 69 |
68 class CFX_CRTFileStream final : public IFX_FileStream { | 70 class CFX_CRTFileStream final : public IFX_FileStream { |
69 public: | 71 public: |
70 CFX_CRTFileStream(IFXCRT_FileAccess* pFA) : m_pFile(pFA), m_dwCount(1) {} | 72 explicit CFX_CRTFileStream(IFXCRT_FileAccess* pFA); |
71 ~CFX_CRTFileStream() override { | 73 ~CFX_CRTFileStream() override; |
72 if (m_pFile) { | 74 |
73 m_pFile->Release(); | 75 // IFX_FileStream: |
74 } | 76 IFX_FileStream* Retain() override; |
75 } | 77 void Release() override; |
76 virtual IFX_FileStream* Retain() override { | 78 FX_FILESIZE GetSize() override; |
77 m_dwCount++; | 79 FX_BOOL IsEOF() override; |
78 return this; | 80 FX_FILESIZE GetPosition() override; |
79 } | 81 FX_BOOL ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override; |
80 virtual void Release() override { | 82 size_t ReadBlock(void* buffer, size_t size) override; |
81 FX_DWORD nCount = --m_dwCount; | 83 FX_BOOL WriteBlock(const void* buffer, |
82 if (!nCount) { | 84 FX_FILESIZE offset, |
83 delete this; | 85 size_t size) override; |
84 } | 86 FX_BOOL Flush() override; |
85 } | |
86 virtual FX_FILESIZE GetSize() override { return m_pFile->GetSize(); } | |
87 virtual FX_BOOL IsEOF() override { return GetPosition() >= GetSize(); } | |
88 virtual FX_FILESIZE GetPosition() override { return m_pFile->GetPosition(); } | |
89 virtual FX_BOOL ReadBlock(void* buffer, | |
90 FX_FILESIZE offset, | |
91 size_t size) override { | |
92 return (FX_BOOL)m_pFile->ReadPos(buffer, size, offset); | |
93 } | |
94 virtual size_t ReadBlock(void* buffer, size_t size) override { | |
95 return m_pFile->Read(buffer, size); | |
96 } | |
97 virtual FX_BOOL WriteBlock(const void* buffer, | |
98 FX_FILESIZE offset, | |
99 size_t size) override { | |
100 return (FX_BOOL)m_pFile->WritePos(buffer, size, offset); | |
101 } | |
102 virtual FX_BOOL Flush() override { return m_pFile->Flush(); } | |
103 | 87 |
104 protected: | 88 protected: |
105 IFXCRT_FileAccess* m_pFile; | 89 IFXCRT_FileAccess* m_pFile; |
106 FX_DWORD m_dwCount; | 90 FX_DWORD m_dwCount; |
107 }; | 91 }; |
108 | 92 |
109 #define FX_MEMSTREAM_BlockSize (64 * 1024) | 93 #define FX_MEMSTREAM_BlockSize (64 * 1024) |
110 #define FX_MEMSTREAM_Consecutive 0x01 | 94 #define FX_MEMSTREAM_Consecutive 0x01 |
111 #define FX_MEMSTREAM_TakeOver 0x02 | 95 #define FX_MEMSTREAM_TakeOver 0x02 |
112 class CFX_MemoryStream final : public IFX_MemoryStream { | 96 class CFX_MemoryStream final : public IFX_MemoryStream { |
113 public: | 97 public: |
114 CFX_MemoryStream(FX_BOOL bConsecutive) | 98 explicit CFX_MemoryStream(FX_BOOL bConsecutive) |
115 : m_dwCount(1), | 99 : m_dwCount(1), |
116 m_nTotalSize(0), | 100 m_nTotalSize(0), |
117 m_nCurSize(0), | 101 m_nCurSize(0), |
118 m_nCurPos(0), | 102 m_nCurPos(0), |
119 m_nGrowSize(FX_MEMSTREAM_BlockSize) { | 103 m_nGrowSize(FX_MEMSTREAM_BlockSize) { |
120 m_dwFlags = | 104 m_dwFlags = |
121 FX_MEMSTREAM_TakeOver | (bConsecutive ? FX_MEMSTREAM_Consecutive : 0); | 105 FX_MEMSTREAM_TakeOver | (bConsecutive ? FX_MEMSTREAM_Consecutive : 0); |
122 } | 106 } |
123 CFX_MemoryStream(uint8_t* pBuffer, size_t nSize, FX_BOOL bTakeOver) | 107 CFX_MemoryStream(uint8_t* pBuffer, size_t nSize, FX_BOOL bTakeOver) |
124 : m_dwCount(1), | 108 : m_dwCount(1), |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 size -= nRead; | 167 size -= nRead; |
184 nStartBlock++; | 168 nStartBlock++; |
185 offset = 0; | 169 offset = 0; |
186 } | 170 } |
187 return TRUE; | 171 return TRUE; |
188 } | 172 } |
189 size_t ReadBlock(void* buffer, size_t size) override { | 173 size_t ReadBlock(void* buffer, size_t size) override { |
190 if (m_nCurPos >= m_nCurSize) { | 174 if (m_nCurPos >= m_nCurSize) { |
191 return 0; | 175 return 0; |
192 } | 176 } |
193 size_t nRead = FX_MIN(size, m_nCurSize - m_nCurPos); | 177 size_t nRead = std::min(size, m_nCurSize - m_nCurPos); |
194 if (!ReadBlock(buffer, (int32_t)m_nCurPos, nRead)) { | 178 if (!ReadBlock(buffer, (int32_t)m_nCurPos, nRead)) { |
195 return 0; | 179 return 0; |
196 } | 180 } |
197 return nRead; | 181 return nRead; |
198 } | 182 } |
199 FX_BOOL WriteBlock(const void* buffer, | 183 FX_BOOL WriteBlock(const void* buffer, |
200 FX_FILESIZE offset, | 184 FX_FILESIZE offset, |
201 size_t size) override { | 185 size_t size) override { |
202 if (!buffer || !size) { | 186 if (!buffer || !size) { |
203 return FALSE; | 187 return FALSE; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 } | 239 } |
256 return TRUE; | 240 return TRUE; |
257 } | 241 } |
258 FX_BOOL Flush() override { return TRUE; } | 242 FX_BOOL Flush() override { return TRUE; } |
259 FX_BOOL IsConsecutive() const override { | 243 FX_BOOL IsConsecutive() const override { |
260 return m_dwFlags & FX_MEMSTREAM_Consecutive; | 244 return m_dwFlags & FX_MEMSTREAM_Consecutive; |
261 } | 245 } |
262 void EstimateSize(size_t nInitSize, size_t nGrowSize) override { | 246 void EstimateSize(size_t nInitSize, size_t nGrowSize) override { |
263 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { | 247 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { |
264 if (m_Blocks.GetSize() < 1) { | 248 if (m_Blocks.GetSize() < 1) { |
265 uint8_t* pBlock = FX_Alloc(uint8_t, FX_MAX(nInitSize, 4096)); | 249 uint8_t* pBlock = |
| 250 FX_Alloc(uint8_t, std::max(nInitSize, static_cast<size_t>(4096))); |
266 m_Blocks.Add(pBlock); | 251 m_Blocks.Add(pBlock); |
267 } | 252 } |
268 m_nGrowSize = FX_MAX(nGrowSize, 4096); | 253 m_nGrowSize = std::max(nGrowSize, static_cast<size_t>(4096)); |
269 } else if (m_Blocks.GetSize() < 1) { | 254 } else if (m_Blocks.GetSize() < 1) { |
270 m_nGrowSize = FX_MAX(nGrowSize, 4096); | 255 m_nGrowSize = std::max(nGrowSize, static_cast<size_t>(4096)); |
271 } | 256 } |
272 } | 257 } |
273 uint8_t* GetBuffer() const override { | 258 uint8_t* GetBuffer() const override { |
274 return m_Blocks.GetSize() ? m_Blocks[0] : nullptr; | 259 return m_Blocks.GetSize() ? m_Blocks[0] : nullptr; |
275 } | 260 } |
276 void AttachBuffer(uint8_t* pBuffer, | 261 void AttachBuffer(uint8_t* pBuffer, |
277 size_t nSize, | 262 size_t nSize, |
278 FX_BOOL bTakeOver = FALSE) override { | 263 FX_BOOL bTakeOver = FALSE) override { |
279 if (!(m_dwFlags & FX_MEMSTREAM_Consecutive)) { | 264 if (!(m_dwFlags & FX_MEMSTREAM_Consecutive)) { |
280 return; | 265 return; |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
341 } FX_MTRANDOMCONTEXT, *FX_LPMTRANDOMCONTEXT; | 326 } FX_MTRANDOMCONTEXT, *FX_LPMTRANDOMCONTEXT; |
342 typedef FX_MTRANDOMCONTEXT const* FX_LPCMTRANDOMCONTEXT; | 327 typedef FX_MTRANDOMCONTEXT const* FX_LPCMTRANDOMCONTEXT; |
343 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 328 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
344 FX_BOOL FX_GenerateCryptoRandom(FX_DWORD* pBuffer, int32_t iCount); | 329 FX_BOOL FX_GenerateCryptoRandom(FX_DWORD* pBuffer, int32_t iCount); |
345 #endif | 330 #endif |
346 #ifdef __cplusplus | 331 #ifdef __cplusplus |
347 } | 332 } |
348 #endif | 333 #endif |
349 | 334 |
350 #endif // CORE_SRC_FXCRT_EXTENSION_H_ | 335 #endif // CORE_SRC_FXCRT_EXTENSION_H_ |
OLD | NEW |