| 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_FXCRT_EXTENSION_H_ | 7 #ifndef CORE_FXCRT_EXTENSION_H_ |
| 8 #define CORE_FXCRT_EXTENSION_H_ | 8 #define CORE_FXCRT_EXTENSION_H_ |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 virtual size_t WritePos(const void* pBuffer, | 30 virtual size_t WritePos(const void* pBuffer, |
| 31 size_t szBuffer, | 31 size_t szBuffer, |
| 32 FX_FILESIZE pos) = 0; | 32 FX_FILESIZE pos) = 0; |
| 33 virtual FX_BOOL Flush() = 0; | 33 virtual FX_BOOL Flush() = 0; |
| 34 virtual FX_BOOL Truncate(FX_FILESIZE szFile) = 0; | 34 virtual FX_BOOL Truncate(FX_FILESIZE szFile) = 0; |
| 35 }; | 35 }; |
| 36 | 36 |
| 37 #ifdef PDF_ENABLE_XFA | 37 #ifdef PDF_ENABLE_XFA |
| 38 class CFX_CRTFileAccess : public IFX_FileAccess { | 38 class CFX_CRTFileAccess : public IFX_FileAccess { |
| 39 public: | 39 public: |
| 40 CFX_CRTFileAccess() : m_RefCount(0) {} | 40 CFX_CRTFileAccess(); |
| 41 ~CFX_CRTFileAccess() override; |
| 41 | 42 |
| 42 // IFX_FileAccess | 43 // IFX_FileAccess |
| 43 void Release() override { | 44 void Release() override; |
| 44 if (--m_RefCount == 0) | 45 IFX_FileAccess* Retain() override; |
| 45 delete this; | 46 void GetPath(CFX_WideString& wsPath) override; |
| 46 } | 47 IFX_FileStream* CreateFileStream(uint32_t dwModes) override; |
| 47 | 48 |
| 48 IFX_FileAccess* Retain() override { | 49 FX_BOOL Init(const CFX_WideStringC& wsPath); |
| 49 m_RefCount++; | |
| 50 return this; | |
| 51 } | |
| 52 | |
| 53 void GetPath(CFX_WideString& wsPath) override { wsPath = m_path; } | |
| 54 | |
| 55 IFX_FileStream* CreateFileStream(uint32_t dwModes) override { | |
| 56 return FX_CreateFileStream(m_path.c_str(), dwModes); | |
| 57 } | |
| 58 | |
| 59 FX_BOOL Init(const CFX_WideStringC& wsPath) { | |
| 60 m_path = wsPath; | |
| 61 m_RefCount = 1; | |
| 62 return TRUE; | |
| 63 } | |
| 64 | 50 |
| 65 protected: | 51 protected: |
| 66 CFX_WideString m_path; | 52 CFX_WideString m_path; |
| 67 uint32_t m_RefCount; | 53 uint32_t m_RefCount; |
| 68 }; | 54 }; |
| 69 #endif // PDF_ENABLE_XFA | 55 #endif // PDF_ENABLE_XFA |
| 70 | 56 |
| 71 class CFX_CRTFileStream final : public IFX_FileStream { | 57 class CFX_CRTFileStream final : public IFX_FileStream { |
| 72 public: | 58 public: |
| 73 explicit CFX_CRTFileStream(std::unique_ptr<IFXCRT_FileAccess> pFA); | 59 explicit CFX_CRTFileStream(std::unique_ptr<IFXCRT_FileAccess> pFA); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 89 protected: | 75 protected: |
| 90 std::unique_ptr<IFXCRT_FileAccess> m_pFile; | 76 std::unique_ptr<IFXCRT_FileAccess> m_pFile; |
| 91 uint32_t m_dwCount; | 77 uint32_t m_dwCount; |
| 92 }; | 78 }; |
| 93 | 79 |
| 94 #define FX_MEMSTREAM_BlockSize (64 * 1024) | 80 #define FX_MEMSTREAM_BlockSize (64 * 1024) |
| 95 #define FX_MEMSTREAM_Consecutive 0x01 | 81 #define FX_MEMSTREAM_Consecutive 0x01 |
| 96 #define FX_MEMSTREAM_TakeOver 0x02 | 82 #define FX_MEMSTREAM_TakeOver 0x02 |
| 97 class CFX_MemoryStream final : public IFX_MemoryStream { | 83 class CFX_MemoryStream final : public IFX_MemoryStream { |
| 98 public: | 84 public: |
| 99 explicit CFX_MemoryStream(FX_BOOL bConsecutive) | 85 explicit CFX_MemoryStream(FX_BOOL bConsecutive); |
| 100 : m_dwCount(1), | 86 CFX_MemoryStream(uint8_t* pBuffer, size_t nSize, FX_BOOL bTakeOver); |
| 101 m_nTotalSize(0), | 87 ~CFX_MemoryStream() override; |
| 102 m_nCurSize(0), | |
| 103 m_nCurPos(0), | |
| 104 m_nGrowSize(FX_MEMSTREAM_BlockSize) { | |
| 105 m_dwFlags = | |
| 106 FX_MEMSTREAM_TakeOver | (bConsecutive ? FX_MEMSTREAM_Consecutive : 0); | |
| 107 } | |
| 108 CFX_MemoryStream(uint8_t* pBuffer, size_t nSize, FX_BOOL bTakeOver) | |
| 109 : m_dwCount(1), | |
| 110 m_nTotalSize(nSize), | |
| 111 m_nCurSize(nSize), | |
| 112 m_nCurPos(0), | |
| 113 m_nGrowSize(FX_MEMSTREAM_BlockSize) { | |
| 114 m_Blocks.Add(pBuffer); | |
| 115 m_dwFlags = | |
| 116 FX_MEMSTREAM_Consecutive | (bTakeOver ? FX_MEMSTREAM_TakeOver : 0); | |
| 117 } | |
| 118 ~CFX_MemoryStream() override { | |
| 119 if (m_dwFlags & FX_MEMSTREAM_TakeOver) { | |
| 120 for (int32_t i = 0; i < m_Blocks.GetSize(); i++) { | |
| 121 FX_Free(m_Blocks[i]); | |
| 122 } | |
| 123 } | |
| 124 m_Blocks.RemoveAll(); | |
| 125 } | |
| 126 | 88 |
| 127 // IFX_MemoryStream: | 89 // IFX_MemoryStream |
| 128 IFX_FileStream* Retain() override { | 90 IFX_FileStream* Retain() override; |
| 129 m_dwCount++; | 91 void Release() override; |
| 130 return this; | 92 FX_FILESIZE GetSize() override; |
| 131 } | 93 FX_BOOL IsEOF() override; |
| 132 void Release() override { | 94 FX_FILESIZE GetPosition() override; |
| 133 uint32_t nCount = --m_dwCount; | 95 FX_BOOL ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override; |
| 134 if (nCount) { | 96 size_t ReadBlock(void* buffer, size_t size) override; |
| 135 return; | |
| 136 } | |
| 137 delete this; | |
| 138 } | |
| 139 FX_FILESIZE GetSize() override { return (FX_FILESIZE)m_nCurSize; } | |
| 140 FX_BOOL IsEOF() override { return m_nCurPos >= (size_t)GetSize(); } | |
| 141 FX_FILESIZE GetPosition() override { return (FX_FILESIZE)m_nCurPos; } | |
| 142 FX_BOOL ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override { | |
| 143 if (!buffer || !size) { | |
| 144 return FALSE; | |
| 145 } | |
| 146 | |
| 147 FX_SAFE_SIZE_T newPos = size; | |
| 148 newPos += offset; | |
| 149 if (!newPos.IsValid() || newPos.ValueOrDefault(0) == 0 || | |
| 150 newPos.ValueOrDie() > m_nCurSize) { | |
| 151 return FALSE; | |
| 152 } | |
| 153 | |
| 154 m_nCurPos = newPos.ValueOrDie(); | |
| 155 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { | |
| 156 FXSYS_memcpy(buffer, m_Blocks[0] + (size_t)offset, size); | |
| 157 return TRUE; | |
| 158 } | |
| 159 size_t nStartBlock = (size_t)offset / m_nGrowSize; | |
| 160 offset -= (FX_FILESIZE)(nStartBlock * m_nGrowSize); | |
| 161 while (size) { | |
| 162 size_t nRead = m_nGrowSize - (size_t)offset; | |
| 163 if (nRead > size) { | |
| 164 nRead = size; | |
| 165 } | |
| 166 FXSYS_memcpy(buffer, m_Blocks[(int)nStartBlock] + (size_t)offset, nRead); | |
| 167 buffer = ((uint8_t*)buffer) + nRead; | |
| 168 size -= nRead; | |
| 169 nStartBlock++; | |
| 170 offset = 0; | |
| 171 } | |
| 172 return TRUE; | |
| 173 } | |
| 174 size_t ReadBlock(void* buffer, size_t size) override { | |
| 175 if (m_nCurPos >= m_nCurSize) { | |
| 176 return 0; | |
| 177 } | |
| 178 size_t nRead = std::min(size, m_nCurSize - m_nCurPos); | |
| 179 if (!ReadBlock(buffer, (int32_t)m_nCurPos, nRead)) { | |
| 180 return 0; | |
| 181 } | |
| 182 return nRead; | |
| 183 } | |
| 184 FX_BOOL WriteBlock(const void* buffer, | 97 FX_BOOL WriteBlock(const void* buffer, |
| 185 FX_FILESIZE offset, | 98 FX_FILESIZE offset, |
| 186 size_t size) override { | 99 size_t size) override; |
| 187 if (!buffer || !size) { | 100 FX_BOOL Flush() override; |
| 188 return FALSE; | 101 FX_BOOL IsConsecutive() const override; |
| 189 } | 102 void EstimateSize(size_t nInitSize, size_t nGrowSize) override; |
| 190 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { | 103 uint8_t* GetBuffer() const override; |
| 191 FX_SAFE_SIZE_T newPos = size; | |
| 192 newPos += offset; | |
| 193 if (!newPos.IsValid()) | |
| 194 return FALSE; | |
| 195 | |
| 196 m_nCurPos = newPos.ValueOrDie(); | |
| 197 if (m_nCurPos > m_nTotalSize) { | |
| 198 m_nTotalSize = | |
| 199 (m_nCurPos + m_nGrowSize - 1) / m_nGrowSize * m_nGrowSize; | |
| 200 if (m_Blocks.GetSize() < 1) { | |
| 201 uint8_t* block = FX_Alloc(uint8_t, m_nTotalSize); | |
| 202 m_Blocks.Add(block); | |
| 203 } else { | |
| 204 m_Blocks[0] = FX_Realloc(uint8_t, m_Blocks[0], m_nTotalSize); | |
| 205 } | |
| 206 if (!m_Blocks[0]) { | |
| 207 m_Blocks.RemoveAll(); | |
| 208 return FALSE; | |
| 209 } | |
| 210 } | |
| 211 FXSYS_memcpy(m_Blocks[0] + (size_t)offset, buffer, size); | |
| 212 if (m_nCurSize < m_nCurPos) { | |
| 213 m_nCurSize = m_nCurPos; | |
| 214 } | |
| 215 return TRUE; | |
| 216 } | |
| 217 | |
| 218 FX_SAFE_SIZE_T newPos = size; | |
| 219 newPos += offset; | |
| 220 if (!newPos.IsValid()) { | |
| 221 return FALSE; | |
| 222 } | |
| 223 | |
| 224 if (!ExpandBlocks(newPos.ValueOrDie())) { | |
| 225 return FALSE; | |
| 226 } | |
| 227 m_nCurPos = newPos.ValueOrDie(); | |
| 228 size_t nStartBlock = (size_t)offset / m_nGrowSize; | |
| 229 offset -= (FX_FILESIZE)(nStartBlock * m_nGrowSize); | |
| 230 while (size) { | |
| 231 size_t nWrite = m_nGrowSize - (size_t)offset; | |
| 232 if (nWrite > size) { | |
| 233 nWrite = size; | |
| 234 } | |
| 235 FXSYS_memcpy(m_Blocks[(int)nStartBlock] + (size_t)offset, buffer, nWrite); | |
| 236 buffer = ((uint8_t*)buffer) + nWrite; | |
| 237 size -= nWrite; | |
| 238 nStartBlock++; | |
| 239 offset = 0; | |
| 240 } | |
| 241 return TRUE; | |
| 242 } | |
| 243 FX_BOOL Flush() override { return TRUE; } | |
| 244 FX_BOOL IsConsecutive() const override { | |
| 245 return !!(m_dwFlags & FX_MEMSTREAM_Consecutive); | |
| 246 } | |
| 247 void EstimateSize(size_t nInitSize, size_t nGrowSize) override { | |
| 248 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { | |
| 249 if (m_Blocks.GetSize() < 1) { | |
| 250 uint8_t* pBlock = | |
| 251 FX_Alloc(uint8_t, std::max(nInitSize, static_cast<size_t>(4096))); | |
| 252 m_Blocks.Add(pBlock); | |
| 253 } | |
| 254 m_nGrowSize = std::max(nGrowSize, static_cast<size_t>(4096)); | |
| 255 } else if (m_Blocks.GetSize() < 1) { | |
| 256 m_nGrowSize = std::max(nGrowSize, static_cast<size_t>(4096)); | |
| 257 } | |
| 258 } | |
| 259 uint8_t* GetBuffer() const override { | |
| 260 return m_Blocks.GetSize() ? m_Blocks[0] : nullptr; | |
| 261 } | |
| 262 void AttachBuffer(uint8_t* pBuffer, | 104 void AttachBuffer(uint8_t* pBuffer, |
| 263 size_t nSize, | 105 size_t nSize, |
| 264 FX_BOOL bTakeOver = FALSE) override { | 106 FX_BOOL bTakeOver = FALSE) override; |
| 265 if (!(m_dwFlags & FX_MEMSTREAM_Consecutive)) { | 107 void DetachBuffer() override; |
| 266 return; | |
| 267 } | |
| 268 m_Blocks.RemoveAll(); | |
| 269 m_Blocks.Add(pBuffer); | |
| 270 m_nTotalSize = m_nCurSize = nSize; | |
| 271 m_nCurPos = 0; | |
| 272 m_dwFlags = | |
| 273 FX_MEMSTREAM_Consecutive | (bTakeOver ? FX_MEMSTREAM_TakeOver : 0); | |
| 274 } | |
| 275 void DetachBuffer() override { | |
| 276 if (!(m_dwFlags & FX_MEMSTREAM_Consecutive)) { | |
| 277 return; | |
| 278 } | |
| 279 m_Blocks.RemoveAll(); | |
| 280 m_nTotalSize = m_nCurSize = m_nCurPos = 0; | |
| 281 m_dwFlags = FX_MEMSTREAM_TakeOver; | |
| 282 } | |
| 283 | 108 |
| 284 protected: | 109 protected: |
| 285 CFX_ArrayTemplate<uint8_t*> m_Blocks; | 110 CFX_ArrayTemplate<uint8_t*> m_Blocks; |
| 286 uint32_t m_dwCount; | 111 uint32_t m_dwCount; |
| 287 size_t m_nTotalSize; | 112 size_t m_nTotalSize; |
| 288 size_t m_nCurSize; | 113 size_t m_nCurSize; |
| 289 size_t m_nCurPos; | 114 size_t m_nCurPos; |
| 290 size_t m_nGrowSize; | 115 size_t m_nGrowSize; |
| 291 uint32_t m_dwFlags; | 116 uint32_t m_dwFlags; |
| 292 FX_BOOL ExpandBlocks(size_t size) { | 117 FX_BOOL ExpandBlocks(size_t size); |
| 293 if (m_nCurSize < size) { | |
| 294 m_nCurSize = size; | |
| 295 } | |
| 296 if (size <= m_nTotalSize) { | |
| 297 return TRUE; | |
| 298 } | |
| 299 int32_t iCount = m_Blocks.GetSize(); | |
| 300 size = (size - m_nTotalSize + m_nGrowSize - 1) / m_nGrowSize; | |
| 301 m_Blocks.SetSize(m_Blocks.GetSize() + (int32_t)size); | |
| 302 while (size--) { | |
| 303 uint8_t* pBlock = FX_Alloc(uint8_t, m_nGrowSize); | |
| 304 m_Blocks.SetAt(iCount++, pBlock); | |
| 305 m_nTotalSize += m_nGrowSize; | |
| 306 } | |
| 307 return TRUE; | |
| 308 } | |
| 309 }; | 118 }; |
| 310 | 119 |
| 311 #ifdef __cplusplus | 120 #ifdef __cplusplus |
| 312 extern "C" { | 121 extern "C" { |
| 313 #endif | 122 #endif |
| 314 #define MT_N 848 | 123 #define MT_N 848 |
| 315 #define MT_M 456 | 124 #define MT_M 456 |
| 316 #define MT_Matrix_A 0x9908b0df | 125 #define MT_Matrix_A 0x9908b0df |
| 317 #define MT_Upper_Mask 0x80000000 | 126 #define MT_Upper_Mask 0x80000000 |
| 318 #define MT_Lower_Mask 0x7fffffff | 127 #define MT_Lower_Mask 0x7fffffff |
| 319 struct FX_MTRANDOMCONTEXT { | 128 struct FX_MTRANDOMCONTEXT { |
| 320 FX_MTRANDOMCONTEXT() { | 129 FX_MTRANDOMCONTEXT() { |
| 321 mti = MT_N + 1; | 130 mti = MT_N + 1; |
| 322 bHaveSeed = FALSE; | 131 bHaveSeed = FALSE; |
| 323 } | 132 } |
| 324 uint32_t mti; | 133 uint32_t mti; |
| 325 FX_BOOL bHaveSeed; | 134 FX_BOOL bHaveSeed; |
| 326 uint32_t mt[MT_N]; | 135 uint32_t mt[MT_N]; |
| 327 }; | 136 }; |
| 328 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 137 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
| 329 FX_BOOL FX_GenerateCryptoRandom(uint32_t* pBuffer, int32_t iCount); | 138 FX_BOOL FX_GenerateCryptoRandom(uint32_t* pBuffer, int32_t iCount); |
| 330 #endif | 139 #endif |
| 331 #ifdef __cplusplus | 140 #ifdef __cplusplus |
| 332 } | 141 } |
| 333 #endif | 142 #endif |
| 334 | 143 |
| 335 #endif // CORE_FXCRT_EXTENSION_H_ | 144 #endif // CORE_FXCRT_EXTENSION_H_ |
| OLD | NEW |