| 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 "core/include/fxcrt/fx_basic.h" | 10 #include "core/include/fxcrt/fx_basic.h" |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 FX_MEMSTREAM_Consecutive | (bTakeOver ? FX_MEMSTREAM_TakeOver : 0); | 129 FX_MEMSTREAM_Consecutive | (bTakeOver ? FX_MEMSTREAM_TakeOver : 0); |
| 130 } | 130 } |
| 131 ~CFX_MemoryStream() override { | 131 ~CFX_MemoryStream() override { |
| 132 if (m_dwFlags & FX_MEMSTREAM_TakeOver) { | 132 if (m_dwFlags & FX_MEMSTREAM_TakeOver) { |
| 133 for (int32_t i = 0; i < m_Blocks.GetSize(); i++) { | 133 for (int32_t i = 0; i < m_Blocks.GetSize(); i++) { |
| 134 FX_Free((uint8_t*)m_Blocks[i]); | 134 FX_Free((uint8_t*)m_Blocks[i]); |
| 135 } | 135 } |
| 136 } | 136 } |
| 137 m_Blocks.RemoveAll(); | 137 m_Blocks.RemoveAll(); |
| 138 } | 138 } |
| 139 virtual IFX_FileStream* Retain() override { | 139 |
| 140 // IFX_MemoryStream: |
| 141 IFX_FileStream* Retain() override { |
| 140 m_dwCount++; | 142 m_dwCount++; |
| 141 return this; | 143 return this; |
| 142 } | 144 } |
| 143 virtual void Release() override { | 145 void Release() override { |
| 144 FX_DWORD nCount = --m_dwCount; | 146 FX_DWORD nCount = --m_dwCount; |
| 145 if (nCount) { | 147 if (nCount) { |
| 146 return; | 148 return; |
| 147 } | 149 } |
| 148 delete this; | 150 delete this; |
| 149 } | 151 } |
| 150 virtual FX_FILESIZE GetSize() override { return (FX_FILESIZE)m_nCurSize; } | 152 FX_FILESIZE GetSize() override { return (FX_FILESIZE)m_nCurSize; } |
| 151 virtual FX_BOOL IsEOF() override { return m_nCurPos >= (size_t)GetSize(); } | 153 FX_BOOL IsEOF() override { return m_nCurPos >= (size_t)GetSize(); } |
| 152 virtual FX_FILESIZE GetPosition() override { return (FX_FILESIZE)m_nCurPos; } | 154 FX_FILESIZE GetPosition() override { return (FX_FILESIZE)m_nCurPos; } |
| 153 virtual FX_BOOL ReadBlock(void* buffer, | 155 FX_BOOL ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override { |
| 154 FX_FILESIZE offset, | |
| 155 size_t size) override { | |
| 156 if (!buffer || !size) { | 156 if (!buffer || !size) { |
| 157 return FALSE; | 157 return FALSE; |
| 158 } | 158 } |
| 159 | 159 |
| 160 FX_SAFE_SIZE_T newPos = size; | 160 FX_SAFE_SIZE_T newPos = size; |
| 161 newPos += offset; | 161 newPos += offset; |
| 162 if (!newPos.IsValid() || newPos.ValueOrDefault(0) == 0 || | 162 if (!newPos.IsValid() || newPos.ValueOrDefault(0) == 0 || |
| 163 newPos.ValueOrDie() > m_nCurSize) { | 163 newPos.ValueOrDie() > m_nCurSize) { |
| 164 return FALSE; | 164 return FALSE; |
| 165 } | 165 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 178 } | 178 } |
| 179 FXSYS_memcpy( | 179 FXSYS_memcpy( |
| 180 buffer, (uint8_t*)m_Blocks[(int)nStartBlock] + (size_t)offset, nRead); | 180 buffer, (uint8_t*)m_Blocks[(int)nStartBlock] + (size_t)offset, nRead); |
| 181 buffer = ((uint8_t*)buffer) + nRead; | 181 buffer = ((uint8_t*)buffer) + nRead; |
| 182 size -= nRead; | 182 size -= nRead; |
| 183 nStartBlock++; | 183 nStartBlock++; |
| 184 offset = 0; | 184 offset = 0; |
| 185 } | 185 } |
| 186 return TRUE; | 186 return TRUE; |
| 187 } | 187 } |
| 188 virtual size_t ReadBlock(void* buffer, size_t size) override { | 188 size_t ReadBlock(void* buffer, size_t size) override { |
| 189 if (m_nCurPos >= m_nCurSize) { | 189 if (m_nCurPos >= m_nCurSize) { |
| 190 return 0; | 190 return 0; |
| 191 } | 191 } |
| 192 size_t nRead = FX_MIN(size, m_nCurSize - m_nCurPos); | 192 size_t nRead = FX_MIN(size, m_nCurSize - m_nCurPos); |
| 193 if (!ReadBlock(buffer, (int32_t)m_nCurPos, nRead)) { | 193 if (!ReadBlock(buffer, (int32_t)m_nCurPos, nRead)) { |
| 194 return 0; | 194 return 0; |
| 195 } | 195 } |
| 196 return nRead; | 196 return nRead; |
| 197 } | 197 } |
| 198 virtual FX_BOOL WriteBlock(const void* buffer, | 198 FX_BOOL WriteBlock(const void* buffer, |
| 199 FX_FILESIZE offset, | 199 FX_FILESIZE offset, |
| 200 size_t size) override { | 200 size_t size) override { |
| 201 if (!buffer || !size) { | 201 if (!buffer || !size) { |
| 202 return FALSE; | 202 return FALSE; |
| 203 } | 203 } |
| 204 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { | 204 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { |
| 205 FX_SAFE_SIZE_T newPos = size; | 205 FX_SAFE_SIZE_T newPos = size; |
| 206 newPos += offset; | 206 newPos += offset; |
| 207 if (!newPos.IsValid()) | 207 if (!newPos.IsValid()) |
| 208 return FALSE; | 208 return FALSE; |
| 209 | 209 |
| 210 m_nCurPos = newPos.ValueOrDie(); | 210 m_nCurPos = newPos.ValueOrDie(); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 } | 248 } |
| 249 FXSYS_memcpy((uint8_t*)m_Blocks[(int)nStartBlock] + (size_t)offset, | 249 FXSYS_memcpy((uint8_t*)m_Blocks[(int)nStartBlock] + (size_t)offset, |
| 250 buffer, nWrite); | 250 buffer, nWrite); |
| 251 buffer = ((uint8_t*)buffer) + nWrite; | 251 buffer = ((uint8_t*)buffer) + nWrite; |
| 252 size -= nWrite; | 252 size -= nWrite; |
| 253 nStartBlock++; | 253 nStartBlock++; |
| 254 offset = 0; | 254 offset = 0; |
| 255 } | 255 } |
| 256 return TRUE; | 256 return TRUE; |
| 257 } | 257 } |
| 258 virtual FX_BOOL Flush() override { return TRUE; } | 258 FX_BOOL Flush() override { return TRUE; } |
| 259 virtual FX_BOOL IsConsecutive() const override { | 259 FX_BOOL IsConsecutive() const override { |
| 260 return m_dwFlags & FX_MEMSTREAM_Consecutive; | 260 return m_dwFlags & FX_MEMSTREAM_Consecutive; |
| 261 } | 261 } |
| 262 virtual void EstimateSize(size_t nInitSize, size_t nGrowSize) override { | 262 void EstimateSize(size_t nInitSize, size_t nGrowSize) override { |
| 263 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { | 263 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { |
| 264 if (m_Blocks.GetSize() < 1) { | 264 if (m_Blocks.GetSize() < 1) { |
| 265 uint8_t* pBlock = FX_Alloc(uint8_t, FX_MAX(nInitSize, 4096)); | 265 uint8_t* pBlock = FX_Alloc(uint8_t, FX_MAX(nInitSize, 4096)); |
| 266 m_Blocks.Add(pBlock); | 266 m_Blocks.Add(pBlock); |
| 267 } | 267 } |
| 268 m_nGrowSize = FX_MAX(nGrowSize, 4096); | 268 m_nGrowSize = FX_MAX(nGrowSize, 4096); |
| 269 } else if (m_Blocks.GetSize() < 1) { | 269 } else if (m_Blocks.GetSize() < 1) { |
| 270 m_nGrowSize = FX_MAX(nGrowSize, 4096); | 270 m_nGrowSize = FX_MAX(nGrowSize, 4096); |
| 271 } | 271 } |
| 272 } | 272 } |
| 273 virtual uint8_t* GetBuffer() const override { | 273 uint8_t* GetBuffer() const override { |
| 274 return m_Blocks.GetSize() ? (uint8_t*)m_Blocks[0] : NULL; | 274 return m_Blocks.GetSize() ? (uint8_t*)m_Blocks[0] : NULL; |
| 275 } | 275 } |
| 276 virtual void AttachBuffer(uint8_t* pBuffer, | 276 void AttachBuffer(uint8_t* pBuffer, |
| 277 size_t nSize, | 277 size_t nSize, |
| 278 FX_BOOL bTakeOver = FALSE) override { | 278 FX_BOOL bTakeOver = FALSE) override { |
| 279 if (!(m_dwFlags & FX_MEMSTREAM_Consecutive)) { | 279 if (!(m_dwFlags & FX_MEMSTREAM_Consecutive)) { |
| 280 return; | 280 return; |
| 281 } | 281 } |
| 282 m_Blocks.RemoveAll(); | 282 m_Blocks.RemoveAll(); |
| 283 m_Blocks.Add(pBuffer); | 283 m_Blocks.Add(pBuffer); |
| 284 m_nTotalSize = m_nCurSize = nSize; | 284 m_nTotalSize = m_nCurSize = nSize; |
| 285 m_nCurPos = 0; | 285 m_nCurPos = 0; |
| 286 m_dwFlags = | 286 m_dwFlags = |
| 287 FX_MEMSTREAM_Consecutive | (bTakeOver ? FX_MEMSTREAM_TakeOver : 0); | 287 FX_MEMSTREAM_Consecutive | (bTakeOver ? FX_MEMSTREAM_TakeOver : 0); |
| 288 } | 288 } |
| 289 virtual void DetachBuffer() override { | 289 void DetachBuffer() override { |
| 290 if (!(m_dwFlags & FX_MEMSTREAM_Consecutive)) { | 290 if (!(m_dwFlags & FX_MEMSTREAM_Consecutive)) { |
| 291 return; | 291 return; |
| 292 } | 292 } |
| 293 m_Blocks.RemoveAll(); | 293 m_Blocks.RemoveAll(); |
| 294 m_nTotalSize = m_nCurSize = m_nCurPos = 0; | 294 m_nTotalSize = m_nCurSize = m_nCurPos = 0; |
| 295 m_dwFlags = FX_MEMSTREAM_TakeOver; | 295 m_dwFlags = FX_MEMSTREAM_TakeOver; |
| 296 } | 296 } |
| 297 | 297 |
| 298 protected: | 298 protected: |
| 299 CFX_PtrArray m_Blocks; | 299 CFX_PtrArray m_Blocks; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 } FX_MTRANDOMCONTEXT, *FX_LPMTRANDOMCONTEXT; | 341 } FX_MTRANDOMCONTEXT, *FX_LPMTRANDOMCONTEXT; |
| 342 typedef FX_MTRANDOMCONTEXT const* FX_LPCMTRANDOMCONTEXT; | 342 typedef FX_MTRANDOMCONTEXT const* FX_LPCMTRANDOMCONTEXT; |
| 343 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 343 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
| 344 FX_BOOL FX_GenerateCryptoRandom(FX_DWORD* pBuffer, int32_t iCount); | 344 FX_BOOL FX_GenerateCryptoRandom(FX_DWORD* pBuffer, int32_t iCount); |
| 345 #endif | 345 #endif |
| 346 #ifdef __cplusplus | 346 #ifdef __cplusplus |
| 347 } | 347 } |
| 348 #endif | 348 #endif |
| 349 | 349 |
| 350 #endif // CORE_SRC_FXCRT_EXTENSION_H_ | 350 #endif // CORE_SRC_FXCRT_EXTENSION_H_ |
| OLD | NEW |