| 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); |
| 37 ~CFX_CRTFileStream() override { | 39 ~CFX_CRTFileStream() override; |
| 38 if (m_pFile) { | 40 |
| 39 m_pFile->Release(); | 41 // IFX_FileStream: |
| 40 } | 42 IFX_FileStream* Retain() override; |
| 41 } | 43 void Release() override; |
| 42 virtual IFX_FileStream* Retain() override { | 44 FX_FILESIZE GetSize() override; |
| 43 m_dwCount++; | 45 FX_BOOL IsEOF() override; |
| 44 return this; | 46 FX_FILESIZE GetPosition() override; |
| 45 } | 47 FX_BOOL ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override; |
| 46 virtual void Release() override { | 48 size_t ReadBlock(void* buffer, size_t size) override; |
| 47 FX_DWORD nCount = --m_dwCount; | 49 FX_BOOL WriteBlock(const void* buffer, |
| 48 if (!nCount) { | 50 FX_FILESIZE offset, |
| 49 delete this; | 51 size_t size) override; |
| 50 } | 52 FX_BOOL Flush() override; |
| 51 } | |
| 52 virtual FX_FILESIZE GetSize() override { return m_pFile->GetSize(); } | |
| 53 virtual FX_BOOL IsEOF() override { return GetPosition() >= GetSize(); } | |
| 54 virtual FX_FILESIZE GetPosition() override { return m_pFile->GetPosition(); } | |
| 55 virtual FX_BOOL ReadBlock(void* buffer, | |
| 56 FX_FILESIZE offset, | |
| 57 size_t size) override { | |
| 58 return (FX_BOOL)m_pFile->ReadPos(buffer, size, offset); | |
| 59 } | |
| 60 virtual size_t ReadBlock(void* buffer, size_t size) override { | |
| 61 return m_pFile->Read(buffer, size); | |
| 62 } | |
| 63 virtual FX_BOOL WriteBlock(const void* buffer, | |
| 64 FX_FILESIZE offset, | |
| 65 size_t size) override { | |
| 66 return (FX_BOOL)m_pFile->WritePos(buffer, size, offset); | |
| 67 } | |
| 68 virtual FX_BOOL Flush() override { return m_pFile->Flush(); } | |
| 69 | 53 |
| 70 protected: | 54 protected: |
| 71 IFXCRT_FileAccess* m_pFile; | 55 IFXCRT_FileAccess* m_pFile; |
| 72 FX_DWORD m_dwCount; | 56 FX_DWORD m_dwCount; |
| 73 }; | 57 }; |
| 74 | 58 |
| 75 #define FX_MEMSTREAM_BlockSize (64 * 1024) | 59 #define FX_MEMSTREAM_BlockSize (64 * 1024) |
| 76 #define FX_MEMSTREAM_Consecutive 0x01 | 60 #define FX_MEMSTREAM_Consecutive 0x01 |
| 77 #define FX_MEMSTREAM_TakeOver 0x02 | 61 #define FX_MEMSTREAM_TakeOver 0x02 |
| 78 class CFX_MemoryStream final : public IFX_MemoryStream { | 62 class CFX_MemoryStream final : public IFX_MemoryStream { |
| 79 public: | 63 public: |
| 80 CFX_MemoryStream(FX_BOOL bConsecutive) | 64 explicit CFX_MemoryStream(FX_BOOL bConsecutive) |
| 81 : m_dwCount(1), | 65 : m_dwCount(1), |
| 82 m_nTotalSize(0), | 66 m_nTotalSize(0), |
| 83 m_nCurSize(0), | 67 m_nCurSize(0), |
| 84 m_nCurPos(0), | 68 m_nCurPos(0), |
| 85 m_nGrowSize(FX_MEMSTREAM_BlockSize) { | 69 m_nGrowSize(FX_MEMSTREAM_BlockSize) { |
| 86 m_dwFlags = | 70 m_dwFlags = |
| 87 FX_MEMSTREAM_TakeOver | (bConsecutive ? FX_MEMSTREAM_Consecutive : 0); | 71 FX_MEMSTREAM_TakeOver | (bConsecutive ? FX_MEMSTREAM_Consecutive : 0); |
| 88 } | 72 } |
| 89 CFX_MemoryStream(uint8_t* pBuffer, size_t nSize, FX_BOOL bTakeOver) | 73 CFX_MemoryStream(uint8_t* pBuffer, size_t nSize, FX_BOOL bTakeOver) |
| 90 : m_dwCount(1), | 74 : m_dwCount(1), |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 size -= nRead; | 133 size -= nRead; |
| 150 nStartBlock++; | 134 nStartBlock++; |
| 151 offset = 0; | 135 offset = 0; |
| 152 } | 136 } |
| 153 return TRUE; | 137 return TRUE; |
| 154 } | 138 } |
| 155 size_t ReadBlock(void* buffer, size_t size) override { | 139 size_t ReadBlock(void* buffer, size_t size) override { |
| 156 if (m_nCurPos >= m_nCurSize) { | 140 if (m_nCurPos >= m_nCurSize) { |
| 157 return 0; | 141 return 0; |
| 158 } | 142 } |
| 159 size_t nRead = FX_MIN(size, m_nCurSize - m_nCurPos); | 143 size_t nRead = std::min(size, m_nCurSize - m_nCurPos); |
| 160 if (!ReadBlock(buffer, (int32_t)m_nCurPos, nRead)) { | 144 if (!ReadBlock(buffer, (int32_t)m_nCurPos, nRead)) { |
| 161 return 0; | 145 return 0; |
| 162 } | 146 } |
| 163 return nRead; | 147 return nRead; |
| 164 } | 148 } |
| 165 FX_BOOL WriteBlock(const void* buffer, | 149 FX_BOOL WriteBlock(const void* buffer, |
| 166 FX_FILESIZE offset, | 150 FX_FILESIZE offset, |
| 167 size_t size) override { | 151 size_t size) override { |
| 168 if (!buffer || !size) { | 152 if (!buffer || !size) { |
| 169 return FALSE; | 153 return FALSE; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 } | 205 } |
| 222 return TRUE; | 206 return TRUE; |
| 223 } | 207 } |
| 224 FX_BOOL Flush() override { return TRUE; } | 208 FX_BOOL Flush() override { return TRUE; } |
| 225 FX_BOOL IsConsecutive() const override { | 209 FX_BOOL IsConsecutive() const override { |
| 226 return m_dwFlags & FX_MEMSTREAM_Consecutive; | 210 return m_dwFlags & FX_MEMSTREAM_Consecutive; |
| 227 } | 211 } |
| 228 void EstimateSize(size_t nInitSize, size_t nGrowSize) override { | 212 void EstimateSize(size_t nInitSize, size_t nGrowSize) override { |
| 229 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { | 213 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { |
| 230 if (m_Blocks.GetSize() < 1) { | 214 if (m_Blocks.GetSize() < 1) { |
| 231 uint8_t* pBlock = FX_Alloc(uint8_t, FX_MAX(nInitSize, 4096)); | 215 uint8_t* pBlock = |
| 216 FX_Alloc(uint8_t, std::max(nInitSize, static_cast<size_t>(4096))); |
| 232 m_Blocks.Add(pBlock); | 217 m_Blocks.Add(pBlock); |
| 233 } | 218 } |
| 234 m_nGrowSize = FX_MAX(nGrowSize, 4096); | 219 m_nGrowSize = std::max(nGrowSize, static_cast<size_t>(4096)); |
| 235 } else if (m_Blocks.GetSize() < 1) { | 220 } else if (m_Blocks.GetSize() < 1) { |
| 236 m_nGrowSize = FX_MAX(nGrowSize, 4096); | 221 m_nGrowSize = std::max(nGrowSize, static_cast<size_t>(4096)); |
| 237 } | 222 } |
| 238 } | 223 } |
| 239 uint8_t* GetBuffer() const override { | 224 uint8_t* GetBuffer() const override { |
| 240 return m_Blocks.GetSize() ? m_Blocks[0] : nullptr; | 225 return m_Blocks.GetSize() ? m_Blocks[0] : nullptr; |
| 241 } | 226 } |
| 242 void AttachBuffer(uint8_t* pBuffer, | 227 void AttachBuffer(uint8_t* pBuffer, |
| 243 size_t nSize, | 228 size_t nSize, |
| 244 FX_BOOL bTakeOver = FALSE) override { | 229 FX_BOOL bTakeOver = FALSE) override { |
| 245 if (!(m_dwFlags & FX_MEMSTREAM_Consecutive)) { | 230 if (!(m_dwFlags & FX_MEMSTREAM_Consecutive)) { |
| 246 return; | 231 return; |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 307 } FX_MTRANDOMCONTEXT, *FX_LPMTRANDOMCONTEXT; | 292 } FX_MTRANDOMCONTEXT, *FX_LPMTRANDOMCONTEXT; |
| 308 typedef FX_MTRANDOMCONTEXT const* FX_LPCMTRANDOMCONTEXT; | 293 typedef FX_MTRANDOMCONTEXT const* FX_LPCMTRANDOMCONTEXT; |
| 309 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 294 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
| 310 FX_BOOL FX_GenerateCryptoRandom(FX_DWORD* pBuffer, int32_t iCount); | 295 FX_BOOL FX_GenerateCryptoRandom(FX_DWORD* pBuffer, int32_t iCount); |
| 311 #endif | 296 #endif |
| 312 #ifdef __cplusplus | 297 #ifdef __cplusplus |
| 313 } | 298 } |
| 314 #endif | 299 #endif |
| 315 | 300 |
| 316 #endif // CORE_SRC_FXCRT_EXTENSION_H_ | 301 #endif // CORE_SRC_FXCRT_EXTENSION_H_ |
| OLD | NEW |