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; |
20 virtual FX_FILESIZE GetSize() const = 0; | 22 virtual FX_FILESIZE GetSize() const = 0; |
21 virtual FX_FILESIZE GetPosition() const = 0; | 23 virtual FX_FILESIZE GetPosition() const = 0; |
22 virtual FX_FILESIZE SetPosition(FX_FILESIZE pos) = 0; | 24 virtual FX_FILESIZE SetPosition(FX_FILESIZE pos) = 0; |
23 virtual size_t Read(void* pBuffer, size_t szBuffer) = 0; | 25 virtual size_t Read(void* pBuffer, size_t szBuffer) = 0; |
24 virtual size_t Write(const void* pBuffer, size_t szBuffer) = 0; | 26 virtual size_t Write(const void* pBuffer, size_t szBuffer) = 0; |
25 virtual size_t ReadPos(void* pBuffer, size_t szBuffer, FX_FILESIZE pos) = 0; | 27 virtual size_t ReadPos(void* pBuffer, size_t szBuffer, FX_FILESIZE pos) = 0; |
26 virtual size_t WritePos(const void* pBuffer, | 28 virtual size_t WritePos(const void* pBuffer, |
27 size_t szBuffer, | 29 size_t szBuffer, |
28 FX_FILESIZE pos) = 0; | 30 FX_FILESIZE pos) = 0; |
29 virtual FX_BOOL Flush() = 0; | 31 virtual FX_BOOL Flush() = 0; |
30 virtual FX_BOOL Truncate(FX_FILESIZE szFile) = 0; | 32 virtual FX_BOOL Truncate(FX_FILESIZE szFile) = 0; |
31 }; | 33 }; |
32 IFXCRT_FileAccess* FXCRT_FileAccess_Create(); | 34 IFXCRT_FileAccess* FXCRT_FileAccess_Create(); |
33 | 35 |
34 class CFX_CRTFileStream final : public IFX_FileStream { | 36 class CFX_CRTFileStream final : public IFX_FileStream { |
35 public: | 37 public: |
36 CFX_CRTFileStream(IFXCRT_FileAccess* pFA) : m_pFile(pFA), m_dwCount(1) {} | 38 explicit CFX_CRTFileStream(IFXCRT_FileAccess* pFA) |
39 : m_pFile(pFA), m_dwCount(1) {} | |
37 ~CFX_CRTFileStream() override { | 40 ~CFX_CRTFileStream() override { |
Tom Sepez
2016/01/08 17:54:43
nit: discourage implementing virtual methods in he
Lei Zhang
2016/01/08 23:29:53
Moved all of CFX_CRTFileStream's impl.
| |
38 if (m_pFile) { | 41 if (m_pFile) { |
39 m_pFile->Release(); | 42 m_pFile->Release(); |
40 } | 43 } |
41 } | 44 } |
42 virtual IFX_FileStream* Retain() override { | 45 IFX_FileStream* Retain() override { |
43 m_dwCount++; | 46 m_dwCount++; |
44 return this; | 47 return this; |
45 } | 48 } |
46 virtual void Release() override { | 49 void Release() override { |
47 FX_DWORD nCount = --m_dwCount; | 50 FX_DWORD nCount = --m_dwCount; |
48 if (!nCount) { | 51 if (!nCount) { |
49 delete this; | 52 delete this; |
50 } | 53 } |
51 } | 54 } |
52 virtual FX_FILESIZE GetSize() override { return m_pFile->GetSize(); } | 55 FX_FILESIZE GetSize() override { return m_pFile->GetSize(); } |
53 virtual FX_BOOL IsEOF() override { return GetPosition() >= GetSize(); } | 56 FX_BOOL IsEOF() override { return GetPosition() >= GetSize(); } |
54 virtual FX_FILESIZE GetPosition() override { return m_pFile->GetPosition(); } | 57 FX_FILESIZE GetPosition() override { return m_pFile->GetPosition(); } |
55 virtual FX_BOOL ReadBlock(void* buffer, | 58 FX_BOOL ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override { |
56 FX_FILESIZE offset, | |
57 size_t size) override { | |
58 return (FX_BOOL)m_pFile->ReadPos(buffer, size, offset); | 59 return (FX_BOOL)m_pFile->ReadPos(buffer, size, offset); |
59 } | 60 } |
60 virtual size_t ReadBlock(void* buffer, size_t size) override { | 61 size_t ReadBlock(void* buffer, size_t size) override { |
61 return m_pFile->Read(buffer, size); | 62 return m_pFile->Read(buffer, size); |
62 } | 63 } |
63 virtual FX_BOOL WriteBlock(const void* buffer, | 64 FX_BOOL WriteBlock(const void* buffer, |
64 FX_FILESIZE offset, | 65 FX_FILESIZE offset, |
65 size_t size) override { | 66 size_t size) override { |
66 return (FX_BOOL)m_pFile->WritePos(buffer, size, offset); | 67 return (FX_BOOL)m_pFile->WritePos(buffer, size, offset); |
67 } | 68 } |
68 virtual FX_BOOL Flush() override { return m_pFile->Flush(); } | 69 FX_BOOL Flush() override { return m_pFile->Flush(); } |
69 | 70 |
70 protected: | 71 protected: |
71 IFXCRT_FileAccess* m_pFile; | 72 IFXCRT_FileAccess* m_pFile; |
72 FX_DWORD m_dwCount; | 73 FX_DWORD m_dwCount; |
73 }; | 74 }; |
74 | 75 |
75 #define FX_MEMSTREAM_BlockSize (64 * 1024) | 76 #define FX_MEMSTREAM_BlockSize (64 * 1024) |
76 #define FX_MEMSTREAM_Consecutive 0x01 | 77 #define FX_MEMSTREAM_Consecutive 0x01 |
77 #define FX_MEMSTREAM_TakeOver 0x02 | 78 #define FX_MEMSTREAM_TakeOver 0x02 |
78 class CFX_MemoryStream final : public IFX_MemoryStream { | 79 class CFX_MemoryStream final : public IFX_MemoryStream { |
79 public: | 80 public: |
80 CFX_MemoryStream(FX_BOOL bConsecutive) | 81 explicit CFX_MemoryStream(FX_BOOL bConsecutive) |
81 : m_dwCount(1), | 82 : m_dwCount(1), |
82 m_nTotalSize(0), | 83 m_nTotalSize(0), |
83 m_nCurSize(0), | 84 m_nCurSize(0), |
84 m_nCurPos(0), | 85 m_nCurPos(0), |
85 m_nGrowSize(FX_MEMSTREAM_BlockSize) { | 86 m_nGrowSize(FX_MEMSTREAM_BlockSize) { |
86 m_dwFlags = | 87 m_dwFlags = |
87 FX_MEMSTREAM_TakeOver | (bConsecutive ? FX_MEMSTREAM_Consecutive : 0); | 88 FX_MEMSTREAM_TakeOver | (bConsecutive ? FX_MEMSTREAM_Consecutive : 0); |
88 } | 89 } |
89 CFX_MemoryStream(uint8_t* pBuffer, size_t nSize, FX_BOOL bTakeOver) | 90 CFX_MemoryStream(uint8_t* pBuffer, size_t nSize, FX_BOOL bTakeOver) |
90 : m_dwCount(1), | 91 : m_dwCount(1), |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
149 size -= nRead; | 150 size -= nRead; |
150 nStartBlock++; | 151 nStartBlock++; |
151 offset = 0; | 152 offset = 0; |
152 } | 153 } |
153 return TRUE; | 154 return TRUE; |
154 } | 155 } |
155 size_t ReadBlock(void* buffer, size_t size) override { | 156 size_t ReadBlock(void* buffer, size_t size) override { |
156 if (m_nCurPos >= m_nCurSize) { | 157 if (m_nCurPos >= m_nCurSize) { |
157 return 0; | 158 return 0; |
158 } | 159 } |
159 size_t nRead = FX_MIN(size, m_nCurSize - m_nCurPos); | 160 size_t nRead = std::min(size, m_nCurSize - m_nCurPos); |
160 if (!ReadBlock(buffer, (int32_t)m_nCurPos, nRead)) { | 161 if (!ReadBlock(buffer, (int32_t)m_nCurPos, nRead)) { |
161 return 0; | 162 return 0; |
162 } | 163 } |
163 return nRead; | 164 return nRead; |
164 } | 165 } |
165 FX_BOOL WriteBlock(const void* buffer, | 166 FX_BOOL WriteBlock(const void* buffer, |
166 FX_FILESIZE offset, | 167 FX_FILESIZE offset, |
167 size_t size) override { | 168 size_t size) override { |
168 if (!buffer || !size) { | 169 if (!buffer || !size) { |
169 return FALSE; | 170 return FALSE; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
221 } | 222 } |
222 return TRUE; | 223 return TRUE; |
223 } | 224 } |
224 FX_BOOL Flush() override { return TRUE; } | 225 FX_BOOL Flush() override { return TRUE; } |
225 FX_BOOL IsConsecutive() const override { | 226 FX_BOOL IsConsecutive() const override { |
226 return m_dwFlags & FX_MEMSTREAM_Consecutive; | 227 return m_dwFlags & FX_MEMSTREAM_Consecutive; |
227 } | 228 } |
228 void EstimateSize(size_t nInitSize, size_t nGrowSize) override { | 229 void EstimateSize(size_t nInitSize, size_t nGrowSize) override { |
229 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { | 230 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { |
230 if (m_Blocks.GetSize() < 1) { | 231 if (m_Blocks.GetSize() < 1) { |
231 uint8_t* pBlock = FX_Alloc(uint8_t, FX_MAX(nInitSize, 4096)); | 232 uint8_t* pBlock = |
233 FX_Alloc(uint8_t, std::max(nInitSize, static_cast<size_t>(4096))); | |
Tom Sepez
2016/01/08 17:54:43
nit: maybe just 4096u ?
Lei Zhang
2016/01/08 23:29:53
No, std::min/max still doesn't like size_t vs unsi
| |
232 m_Blocks.Add(pBlock); | 234 m_Blocks.Add(pBlock); |
233 } | 235 } |
234 m_nGrowSize = FX_MAX(nGrowSize, 4096); | 236 m_nGrowSize = std::max(nGrowSize, static_cast<size_t>(4096)); |
235 } else if (m_Blocks.GetSize() < 1) { | 237 } else if (m_Blocks.GetSize() < 1) { |
236 m_nGrowSize = FX_MAX(nGrowSize, 4096); | 238 m_nGrowSize = std::max(nGrowSize, static_cast<size_t>(4096)); |
237 } | 239 } |
238 } | 240 } |
239 uint8_t* GetBuffer() const override { | 241 uint8_t* GetBuffer() const override { |
240 return m_Blocks.GetSize() ? m_Blocks[0] : nullptr; | 242 return m_Blocks.GetSize() ? m_Blocks[0] : nullptr; |
241 } | 243 } |
242 void AttachBuffer(uint8_t* pBuffer, | 244 void AttachBuffer(uint8_t* pBuffer, |
243 size_t nSize, | 245 size_t nSize, |
244 FX_BOOL bTakeOver = FALSE) override { | 246 FX_BOOL bTakeOver = FALSE) override { |
245 if (!(m_dwFlags & FX_MEMSTREAM_Consecutive)) { | 247 if (!(m_dwFlags & FX_MEMSTREAM_Consecutive)) { |
246 return; | 248 return; |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
307 } FX_MTRANDOMCONTEXT, *FX_LPMTRANDOMCONTEXT; | 309 } FX_MTRANDOMCONTEXT, *FX_LPMTRANDOMCONTEXT; |
308 typedef FX_MTRANDOMCONTEXT const* FX_LPCMTRANDOMCONTEXT; | 310 typedef FX_MTRANDOMCONTEXT const* FX_LPCMTRANDOMCONTEXT; |
309 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 311 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
310 FX_BOOL FX_GenerateCryptoRandom(FX_DWORD* pBuffer, int32_t iCount); | 312 FX_BOOL FX_GenerateCryptoRandom(FX_DWORD* pBuffer, int32_t iCount); |
311 #endif | 313 #endif |
312 #ifdef __cplusplus | 314 #ifdef __cplusplus |
313 } | 315 } |
314 #endif | 316 #endif |
315 | 317 |
316 #endif // CORE_SRC_FXCRT_EXTENSION_H_ | 318 #endif // CORE_SRC_FXCRT_EXTENSION_H_ |
OLD | NEW |