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 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 m_nCurSize(nSize), | 126 m_nCurSize(nSize), |
127 m_nCurPos(0), | 127 m_nCurPos(0), |
128 m_nGrowSize(FX_MEMSTREAM_BlockSize) { | 128 m_nGrowSize(FX_MEMSTREAM_BlockSize) { |
129 m_Blocks.Add(pBuffer); | 129 m_Blocks.Add(pBuffer); |
130 m_dwFlags = | 130 m_dwFlags = |
131 FX_MEMSTREAM_Consecutive | (bTakeOver ? FX_MEMSTREAM_TakeOver : 0); | 131 FX_MEMSTREAM_Consecutive | (bTakeOver ? FX_MEMSTREAM_TakeOver : 0); |
132 } | 132 } |
133 ~CFX_MemoryStream() override { | 133 ~CFX_MemoryStream() override { |
134 if (m_dwFlags & FX_MEMSTREAM_TakeOver) { | 134 if (m_dwFlags & FX_MEMSTREAM_TakeOver) { |
135 for (int32_t i = 0; i < m_Blocks.GetSize(); i++) { | 135 for (int32_t i = 0; i < m_Blocks.GetSize(); i++) { |
136 FX_Free((uint8_t*)m_Blocks[i]); | 136 FX_Free(m_Blocks[i]); |
137 } | 137 } |
138 } | 138 } |
139 m_Blocks.RemoveAll(); | 139 m_Blocks.RemoveAll(); |
140 } | 140 } |
141 | 141 |
142 // IFX_MemoryStream: | 142 // IFX_MemoryStream: |
143 IFX_FileStream* Retain() override { | 143 IFX_FileStream* Retain() override { |
144 m_dwCount++; | 144 m_dwCount++; |
145 return this; | 145 return this; |
146 } | 146 } |
(...skipping 14 matching lines...) Expand all Loading... |
161 | 161 |
162 FX_SAFE_SIZE_T newPos = size; | 162 FX_SAFE_SIZE_T newPos = size; |
163 newPos += offset; | 163 newPos += offset; |
164 if (!newPos.IsValid() || newPos.ValueOrDefault(0) == 0 || | 164 if (!newPos.IsValid() || newPos.ValueOrDefault(0) == 0 || |
165 newPos.ValueOrDie() > m_nCurSize) { | 165 newPos.ValueOrDie() > m_nCurSize) { |
166 return FALSE; | 166 return FALSE; |
167 } | 167 } |
168 | 168 |
169 m_nCurPos = newPos.ValueOrDie(); | 169 m_nCurPos = newPos.ValueOrDie(); |
170 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { | 170 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { |
171 FXSYS_memcpy(buffer, (uint8_t*)m_Blocks[0] + (size_t)offset, size); | 171 FXSYS_memcpy(buffer, m_Blocks[0] + (size_t)offset, size); |
172 return TRUE; | 172 return TRUE; |
173 } | 173 } |
174 size_t nStartBlock = (size_t)offset / m_nGrowSize; | 174 size_t nStartBlock = (size_t)offset / m_nGrowSize; |
175 offset -= (FX_FILESIZE)(nStartBlock * m_nGrowSize); | 175 offset -= (FX_FILESIZE)(nStartBlock * m_nGrowSize); |
176 while (size) { | 176 while (size) { |
177 size_t nRead = m_nGrowSize - (size_t)offset; | 177 size_t nRead = m_nGrowSize - (size_t)offset; |
178 if (nRead > size) { | 178 if (nRead > size) { |
179 nRead = size; | 179 nRead = size; |
180 } | 180 } |
181 FXSYS_memcpy( | 181 FXSYS_memcpy(buffer, m_Blocks[(int)nStartBlock] + (size_t)offset, nRead); |
182 buffer, (uint8_t*)m_Blocks[(int)nStartBlock] + (size_t)offset, nRead); | |
183 buffer = ((uint8_t*)buffer) + nRead; | 182 buffer = ((uint8_t*)buffer) + nRead; |
184 size -= nRead; | 183 size -= nRead; |
185 nStartBlock++; | 184 nStartBlock++; |
186 offset = 0; | 185 offset = 0; |
187 } | 186 } |
188 return TRUE; | 187 return TRUE; |
189 } | 188 } |
190 size_t ReadBlock(void* buffer, size_t size) override { | 189 size_t ReadBlock(void* buffer, size_t size) override { |
191 if (m_nCurPos >= m_nCurSize) { | 190 if (m_nCurPos >= m_nCurSize) { |
192 return 0; | 191 return 0; |
(...skipping 14 matching lines...) Expand all Loading... |
207 FX_SAFE_SIZE_T newPos = size; | 206 FX_SAFE_SIZE_T newPos = size; |
208 newPos += offset; | 207 newPos += offset; |
209 if (!newPos.IsValid()) | 208 if (!newPos.IsValid()) |
210 return FALSE; | 209 return FALSE; |
211 | 210 |
212 m_nCurPos = newPos.ValueOrDie(); | 211 m_nCurPos = newPos.ValueOrDie(); |
213 if (m_nCurPos > m_nTotalSize) { | 212 if (m_nCurPos > m_nTotalSize) { |
214 m_nTotalSize = | 213 m_nTotalSize = |
215 (m_nCurPos + m_nGrowSize - 1) / m_nGrowSize * m_nGrowSize; | 214 (m_nCurPos + m_nGrowSize - 1) / m_nGrowSize * m_nGrowSize; |
216 if (m_Blocks.GetSize() < 1) { | 215 if (m_Blocks.GetSize() < 1) { |
217 void* block = FX_Alloc(uint8_t, m_nTotalSize); | 216 uint8_t* block = FX_Alloc(uint8_t, m_nTotalSize); |
218 m_Blocks.Add(block); | 217 m_Blocks.Add(block); |
219 } else { | 218 } else { |
220 m_Blocks[0] = FX_Realloc(uint8_t, m_Blocks[0], m_nTotalSize); | 219 m_Blocks[0] = FX_Realloc(uint8_t, m_Blocks[0], m_nTotalSize); |
221 } | 220 } |
222 if (!m_Blocks[0]) { | 221 if (!m_Blocks[0]) { |
223 m_Blocks.RemoveAll(); | 222 m_Blocks.RemoveAll(); |
224 return FALSE; | 223 return FALSE; |
225 } | 224 } |
226 } | 225 } |
227 FXSYS_memcpy((uint8_t*)m_Blocks[0] + (size_t)offset, buffer, size); | 226 FXSYS_memcpy(m_Blocks[0] + (size_t)offset, buffer, size); |
228 if (m_nCurSize < m_nCurPos) { | 227 if (m_nCurSize < m_nCurPos) { |
229 m_nCurSize = m_nCurPos; | 228 m_nCurSize = m_nCurPos; |
230 } | 229 } |
231 return TRUE; | 230 return TRUE; |
232 } | 231 } |
233 | 232 |
234 FX_SAFE_SIZE_T newPos = size; | 233 FX_SAFE_SIZE_T newPos = size; |
235 newPos += offset; | 234 newPos += offset; |
236 if (!newPos.IsValid()) { | 235 if (!newPos.IsValid()) { |
237 return FALSE; | 236 return FALSE; |
238 } | 237 } |
239 | 238 |
240 if (!ExpandBlocks(newPos.ValueOrDie())) { | 239 if (!ExpandBlocks(newPos.ValueOrDie())) { |
241 return FALSE; | 240 return FALSE; |
242 } | 241 } |
243 m_nCurPos = newPos.ValueOrDie(); | 242 m_nCurPos = newPos.ValueOrDie(); |
244 size_t nStartBlock = (size_t)offset / m_nGrowSize; | 243 size_t nStartBlock = (size_t)offset / m_nGrowSize; |
245 offset -= (FX_FILESIZE)(nStartBlock * m_nGrowSize); | 244 offset -= (FX_FILESIZE)(nStartBlock * m_nGrowSize); |
246 while (size) { | 245 while (size) { |
247 size_t nWrite = m_nGrowSize - (size_t)offset; | 246 size_t nWrite = m_nGrowSize - (size_t)offset; |
248 if (nWrite > size) { | 247 if (nWrite > size) { |
249 nWrite = size; | 248 nWrite = size; |
250 } | 249 } |
251 FXSYS_memcpy((uint8_t*)m_Blocks[(int)nStartBlock] + (size_t)offset, | 250 FXSYS_memcpy(m_Blocks[(int)nStartBlock] + (size_t)offset, buffer, nWrite); |
252 buffer, nWrite); | |
253 buffer = ((uint8_t*)buffer) + nWrite; | 251 buffer = ((uint8_t*)buffer) + nWrite; |
254 size -= nWrite; | 252 size -= nWrite; |
255 nStartBlock++; | 253 nStartBlock++; |
256 offset = 0; | 254 offset = 0; |
257 } | 255 } |
258 return TRUE; | 256 return TRUE; |
259 } | 257 } |
260 FX_BOOL Flush() override { return TRUE; } | 258 FX_BOOL Flush() override { return TRUE; } |
261 FX_BOOL IsConsecutive() const override { | 259 FX_BOOL IsConsecutive() const override { |
262 return m_dwFlags & FX_MEMSTREAM_Consecutive; | 260 return m_dwFlags & FX_MEMSTREAM_Consecutive; |
263 } | 261 } |
264 void EstimateSize(size_t nInitSize, size_t nGrowSize) override { | 262 void EstimateSize(size_t nInitSize, size_t nGrowSize) override { |
265 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { | 263 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { |
266 if (m_Blocks.GetSize() < 1) { | 264 if (m_Blocks.GetSize() < 1) { |
267 uint8_t* pBlock = FX_Alloc(uint8_t, FX_MAX(nInitSize, 4096)); | 265 uint8_t* pBlock = FX_Alloc(uint8_t, FX_MAX(nInitSize, 4096)); |
268 m_Blocks.Add(pBlock); | 266 m_Blocks.Add(pBlock); |
269 } | 267 } |
270 m_nGrowSize = FX_MAX(nGrowSize, 4096); | 268 m_nGrowSize = FX_MAX(nGrowSize, 4096); |
271 } else if (m_Blocks.GetSize() < 1) { | 269 } else if (m_Blocks.GetSize() < 1) { |
272 m_nGrowSize = FX_MAX(nGrowSize, 4096); | 270 m_nGrowSize = FX_MAX(nGrowSize, 4096); |
273 } | 271 } |
274 } | 272 } |
275 uint8_t* GetBuffer() const override { | 273 uint8_t* GetBuffer() const override { |
276 return m_Blocks.GetSize() ? (uint8_t*)m_Blocks[0] : NULL; | 274 return m_Blocks.GetSize() ? m_Blocks[0] : nullptr; |
277 } | 275 } |
278 void AttachBuffer(uint8_t* pBuffer, | 276 void AttachBuffer(uint8_t* pBuffer, |
279 size_t nSize, | 277 size_t nSize, |
280 FX_BOOL bTakeOver = FALSE) override { | 278 FX_BOOL bTakeOver = FALSE) override { |
281 if (!(m_dwFlags & FX_MEMSTREAM_Consecutive)) { | 279 if (!(m_dwFlags & FX_MEMSTREAM_Consecutive)) { |
282 return; | 280 return; |
283 } | 281 } |
284 m_Blocks.RemoveAll(); | 282 m_Blocks.RemoveAll(); |
285 m_Blocks.Add(pBuffer); | 283 m_Blocks.Add(pBuffer); |
286 m_nTotalSize = m_nCurSize = nSize; | 284 m_nTotalSize = m_nCurSize = nSize; |
287 m_nCurPos = 0; | 285 m_nCurPos = 0; |
288 m_dwFlags = | 286 m_dwFlags = |
289 FX_MEMSTREAM_Consecutive | (bTakeOver ? FX_MEMSTREAM_TakeOver : 0); | 287 FX_MEMSTREAM_Consecutive | (bTakeOver ? FX_MEMSTREAM_TakeOver : 0); |
290 } | 288 } |
291 void DetachBuffer() override { | 289 void DetachBuffer() override { |
292 if (!(m_dwFlags & FX_MEMSTREAM_Consecutive)) { | 290 if (!(m_dwFlags & FX_MEMSTREAM_Consecutive)) { |
293 return; | 291 return; |
294 } | 292 } |
295 m_Blocks.RemoveAll(); | 293 m_Blocks.RemoveAll(); |
296 m_nTotalSize = m_nCurSize = m_nCurPos = 0; | 294 m_nTotalSize = m_nCurSize = m_nCurPos = 0; |
297 m_dwFlags = FX_MEMSTREAM_TakeOver; | 295 m_dwFlags = FX_MEMSTREAM_TakeOver; |
298 } | 296 } |
299 | 297 |
300 protected: | 298 protected: |
301 CFX_PtrArray m_Blocks; | 299 CFX_ArrayTemplate<uint8_t*> m_Blocks; |
302 FX_DWORD m_dwCount; | 300 FX_DWORD m_dwCount; |
303 size_t m_nTotalSize; | 301 size_t m_nTotalSize; |
304 size_t m_nCurSize; | 302 size_t m_nCurSize; |
305 size_t m_nCurPos; | 303 size_t m_nCurPos; |
306 size_t m_nGrowSize; | 304 size_t m_nGrowSize; |
307 FX_DWORD m_dwFlags; | 305 FX_DWORD m_dwFlags; |
308 FX_BOOL ExpandBlocks(size_t size) { | 306 FX_BOOL ExpandBlocks(size_t size) { |
309 if (m_nCurSize < size) { | 307 if (m_nCurSize < size) { |
310 m_nCurSize = size; | 308 m_nCurSize = size; |
311 } | 309 } |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 } FX_MTRANDOMCONTEXT, *FX_LPMTRANDOMCONTEXT; | 341 } FX_MTRANDOMCONTEXT, *FX_LPMTRANDOMCONTEXT; |
344 typedef FX_MTRANDOMCONTEXT const* FX_LPCMTRANDOMCONTEXT; | 342 typedef FX_MTRANDOMCONTEXT const* FX_LPCMTRANDOMCONTEXT; |
345 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 343 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
346 FX_BOOL FX_GenerateCryptoRandom(FX_DWORD* pBuffer, int32_t iCount); | 344 FX_BOOL FX_GenerateCryptoRandom(FX_DWORD* pBuffer, int32_t iCount); |
347 #endif | 345 #endif |
348 #ifdef __cplusplus | 346 #ifdef __cplusplus |
349 } | 347 } |
350 #endif | 348 #endif |
351 | 349 |
352 #endif // CORE_SRC_FXCRT_EXTENSION_H_ | 350 #endif // CORE_SRC_FXCRT_EXTENSION_H_ |
OLD | NEW |