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 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 m_nCurSize(nSize), | 92 m_nCurSize(nSize), |
93 m_nCurPos(0), | 93 m_nCurPos(0), |
94 m_nGrowSize(FX_MEMSTREAM_BlockSize) { | 94 m_nGrowSize(FX_MEMSTREAM_BlockSize) { |
95 m_Blocks.Add(pBuffer); | 95 m_Blocks.Add(pBuffer); |
96 m_dwFlags = | 96 m_dwFlags = |
97 FX_MEMSTREAM_Consecutive | (bTakeOver ? FX_MEMSTREAM_TakeOver : 0); | 97 FX_MEMSTREAM_Consecutive | (bTakeOver ? FX_MEMSTREAM_TakeOver : 0); |
98 } | 98 } |
99 ~CFX_MemoryStream() override { | 99 ~CFX_MemoryStream() override { |
100 if (m_dwFlags & FX_MEMSTREAM_TakeOver) { | 100 if (m_dwFlags & FX_MEMSTREAM_TakeOver) { |
101 for (int32_t i = 0; i < m_Blocks.GetSize(); i++) { | 101 for (int32_t i = 0; i < m_Blocks.GetSize(); i++) { |
102 FX_Free((uint8_t*)m_Blocks[i]); | 102 FX_Free(m_Blocks[i]); |
103 } | 103 } |
104 } | 104 } |
105 m_Blocks.RemoveAll(); | 105 m_Blocks.RemoveAll(); |
106 } | 106 } |
107 | 107 |
108 // IFX_MemoryStream: | 108 // IFX_MemoryStream: |
109 IFX_FileStream* Retain() override { | 109 IFX_FileStream* Retain() override { |
110 m_dwCount++; | 110 m_dwCount++; |
111 return this; | 111 return this; |
112 } | 112 } |
(...skipping 14 matching lines...) Expand all Loading... |
127 | 127 |
128 FX_SAFE_SIZE_T newPos = size; | 128 FX_SAFE_SIZE_T newPos = size; |
129 newPos += offset; | 129 newPos += offset; |
130 if (!newPos.IsValid() || newPos.ValueOrDefault(0) == 0 || | 130 if (!newPos.IsValid() || newPos.ValueOrDefault(0) == 0 || |
131 newPos.ValueOrDie() > m_nCurSize) { | 131 newPos.ValueOrDie() > m_nCurSize) { |
132 return FALSE; | 132 return FALSE; |
133 } | 133 } |
134 | 134 |
135 m_nCurPos = newPos.ValueOrDie(); | 135 m_nCurPos = newPos.ValueOrDie(); |
136 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { | 136 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { |
137 FXSYS_memcpy(buffer, (uint8_t*)m_Blocks[0] + (size_t)offset, size); | 137 FXSYS_memcpy(buffer, m_Blocks[0] + (size_t)offset, size); |
138 return TRUE; | 138 return TRUE; |
139 } | 139 } |
140 size_t nStartBlock = (size_t)offset / m_nGrowSize; | 140 size_t nStartBlock = (size_t)offset / m_nGrowSize; |
141 offset -= (FX_FILESIZE)(nStartBlock * m_nGrowSize); | 141 offset -= (FX_FILESIZE)(nStartBlock * m_nGrowSize); |
142 while (size) { | 142 while (size) { |
143 size_t nRead = m_nGrowSize - (size_t)offset; | 143 size_t nRead = m_nGrowSize - (size_t)offset; |
144 if (nRead > size) { | 144 if (nRead > size) { |
145 nRead = size; | 145 nRead = size; |
146 } | 146 } |
147 FXSYS_memcpy( | 147 FXSYS_memcpy(buffer, m_Blocks[(int)nStartBlock] + (size_t)offset, nRead); |
148 buffer, (uint8_t*)m_Blocks[(int)nStartBlock] + (size_t)offset, nRead); | |
149 buffer = ((uint8_t*)buffer) + nRead; | 148 buffer = ((uint8_t*)buffer) + nRead; |
150 size -= nRead; | 149 size -= nRead; |
151 nStartBlock++; | 150 nStartBlock++; |
152 offset = 0; | 151 offset = 0; |
153 } | 152 } |
154 return TRUE; | 153 return TRUE; |
155 } | 154 } |
156 size_t ReadBlock(void* buffer, size_t size) override { | 155 size_t ReadBlock(void* buffer, size_t size) override { |
157 if (m_nCurPos >= m_nCurSize) { | 156 if (m_nCurPos >= m_nCurSize) { |
158 return 0; | 157 return 0; |
(...skipping 14 matching lines...) Expand all Loading... |
173 FX_SAFE_SIZE_T newPos = size; | 172 FX_SAFE_SIZE_T newPos = size; |
174 newPos += offset; | 173 newPos += offset; |
175 if (!newPos.IsValid()) | 174 if (!newPos.IsValid()) |
176 return FALSE; | 175 return FALSE; |
177 | 176 |
178 m_nCurPos = newPos.ValueOrDie(); | 177 m_nCurPos = newPos.ValueOrDie(); |
179 if (m_nCurPos > m_nTotalSize) { | 178 if (m_nCurPos > m_nTotalSize) { |
180 m_nTotalSize = | 179 m_nTotalSize = |
181 (m_nCurPos + m_nGrowSize - 1) / m_nGrowSize * m_nGrowSize; | 180 (m_nCurPos + m_nGrowSize - 1) / m_nGrowSize * m_nGrowSize; |
182 if (m_Blocks.GetSize() < 1) { | 181 if (m_Blocks.GetSize() < 1) { |
183 void* block = FX_Alloc(uint8_t, m_nTotalSize); | 182 uint8_t* block = FX_Alloc(uint8_t, m_nTotalSize); |
184 m_Blocks.Add(block); | 183 m_Blocks.Add(block); |
185 } else { | 184 } else { |
186 m_Blocks[0] = FX_Realloc(uint8_t, m_Blocks[0], m_nTotalSize); | 185 m_Blocks[0] = FX_Realloc(uint8_t, m_Blocks[0], m_nTotalSize); |
187 } | 186 } |
188 if (!m_Blocks[0]) { | 187 if (!m_Blocks[0]) { |
189 m_Blocks.RemoveAll(); | 188 m_Blocks.RemoveAll(); |
190 return FALSE; | 189 return FALSE; |
191 } | 190 } |
192 } | 191 } |
193 FXSYS_memcpy((uint8_t*)m_Blocks[0] + (size_t)offset, buffer, size); | 192 FXSYS_memcpy(m_Blocks[0] + (size_t)offset, buffer, size); |
194 if (m_nCurSize < m_nCurPos) { | 193 if (m_nCurSize < m_nCurPos) { |
195 m_nCurSize = m_nCurPos; | 194 m_nCurSize = m_nCurPos; |
196 } | 195 } |
197 return TRUE; | 196 return TRUE; |
198 } | 197 } |
199 | 198 |
200 FX_SAFE_SIZE_T newPos = size; | 199 FX_SAFE_SIZE_T newPos = size; |
201 newPos += offset; | 200 newPos += offset; |
202 if (!newPos.IsValid()) { | 201 if (!newPos.IsValid()) { |
203 return FALSE; | 202 return FALSE; |
204 } | 203 } |
205 | 204 |
206 if (!ExpandBlocks(newPos.ValueOrDie())) { | 205 if (!ExpandBlocks(newPos.ValueOrDie())) { |
207 return FALSE; | 206 return FALSE; |
208 } | 207 } |
209 m_nCurPos = newPos.ValueOrDie(); | 208 m_nCurPos = newPos.ValueOrDie(); |
210 size_t nStartBlock = (size_t)offset / m_nGrowSize; | 209 size_t nStartBlock = (size_t)offset / m_nGrowSize; |
211 offset -= (FX_FILESIZE)(nStartBlock * m_nGrowSize); | 210 offset -= (FX_FILESIZE)(nStartBlock * m_nGrowSize); |
212 while (size) { | 211 while (size) { |
213 size_t nWrite = m_nGrowSize - (size_t)offset; | 212 size_t nWrite = m_nGrowSize - (size_t)offset; |
214 if (nWrite > size) { | 213 if (nWrite > size) { |
215 nWrite = size; | 214 nWrite = size; |
216 } | 215 } |
217 FXSYS_memcpy((uint8_t*)m_Blocks[(int)nStartBlock] + (size_t)offset, | 216 FXSYS_memcpy(m_Blocks[(int)nStartBlock] + (size_t)offset, buffer, nWrite); |
218 buffer, nWrite); | |
219 buffer = ((uint8_t*)buffer) + nWrite; | 217 buffer = ((uint8_t*)buffer) + nWrite; |
220 size -= nWrite; | 218 size -= nWrite; |
221 nStartBlock++; | 219 nStartBlock++; |
222 offset = 0; | 220 offset = 0; |
223 } | 221 } |
224 return TRUE; | 222 return TRUE; |
225 } | 223 } |
226 FX_BOOL Flush() override { return TRUE; } | 224 FX_BOOL Flush() override { return TRUE; } |
227 FX_BOOL IsConsecutive() const override { | 225 FX_BOOL IsConsecutive() const override { |
228 return m_dwFlags & FX_MEMSTREAM_Consecutive; | 226 return m_dwFlags & FX_MEMSTREAM_Consecutive; |
229 } | 227 } |
230 void EstimateSize(size_t nInitSize, size_t nGrowSize) override { | 228 void EstimateSize(size_t nInitSize, size_t nGrowSize) override { |
231 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { | 229 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { |
232 if (m_Blocks.GetSize() < 1) { | 230 if (m_Blocks.GetSize() < 1) { |
233 uint8_t* pBlock = FX_Alloc(uint8_t, FX_MAX(nInitSize, 4096)); | 231 uint8_t* pBlock = FX_Alloc(uint8_t, FX_MAX(nInitSize, 4096)); |
234 m_Blocks.Add(pBlock); | 232 m_Blocks.Add(pBlock); |
235 } | 233 } |
236 m_nGrowSize = FX_MAX(nGrowSize, 4096); | 234 m_nGrowSize = FX_MAX(nGrowSize, 4096); |
237 } else if (m_Blocks.GetSize() < 1) { | 235 } else if (m_Blocks.GetSize() < 1) { |
238 m_nGrowSize = FX_MAX(nGrowSize, 4096); | 236 m_nGrowSize = FX_MAX(nGrowSize, 4096); |
239 } | 237 } |
240 } | 238 } |
241 uint8_t* GetBuffer() const override { | 239 uint8_t* GetBuffer() const override { |
242 return m_Blocks.GetSize() ? (uint8_t*)m_Blocks[0] : NULL; | 240 return m_Blocks.GetSize() ? m_Blocks[0] : nullptr; |
243 } | 241 } |
244 void AttachBuffer(uint8_t* pBuffer, | 242 void AttachBuffer(uint8_t* pBuffer, |
245 size_t nSize, | 243 size_t nSize, |
246 FX_BOOL bTakeOver = FALSE) override { | 244 FX_BOOL bTakeOver = FALSE) override { |
247 if (!(m_dwFlags & FX_MEMSTREAM_Consecutive)) { | 245 if (!(m_dwFlags & FX_MEMSTREAM_Consecutive)) { |
248 return; | 246 return; |
249 } | 247 } |
250 m_Blocks.RemoveAll(); | 248 m_Blocks.RemoveAll(); |
251 m_Blocks.Add(pBuffer); | 249 m_Blocks.Add(pBuffer); |
252 m_nTotalSize = m_nCurSize = nSize; | 250 m_nTotalSize = m_nCurSize = nSize; |
253 m_nCurPos = 0; | 251 m_nCurPos = 0; |
254 m_dwFlags = | 252 m_dwFlags = |
255 FX_MEMSTREAM_Consecutive | (bTakeOver ? FX_MEMSTREAM_TakeOver : 0); | 253 FX_MEMSTREAM_Consecutive | (bTakeOver ? FX_MEMSTREAM_TakeOver : 0); |
256 } | 254 } |
257 void DetachBuffer() override { | 255 void DetachBuffer() override { |
258 if (!(m_dwFlags & FX_MEMSTREAM_Consecutive)) { | 256 if (!(m_dwFlags & FX_MEMSTREAM_Consecutive)) { |
259 return; | 257 return; |
260 } | 258 } |
261 m_Blocks.RemoveAll(); | 259 m_Blocks.RemoveAll(); |
262 m_nTotalSize = m_nCurSize = m_nCurPos = 0; | 260 m_nTotalSize = m_nCurSize = m_nCurPos = 0; |
263 m_dwFlags = FX_MEMSTREAM_TakeOver; | 261 m_dwFlags = FX_MEMSTREAM_TakeOver; |
264 } | 262 } |
265 | 263 |
266 protected: | 264 protected: |
267 CFX_PtrArray m_Blocks; | 265 CFX_ArrayTemplate<uint8_t*> m_Blocks; |
268 FX_DWORD m_dwCount; | 266 FX_DWORD m_dwCount; |
269 size_t m_nTotalSize; | 267 size_t m_nTotalSize; |
270 size_t m_nCurSize; | 268 size_t m_nCurSize; |
271 size_t m_nCurPos; | 269 size_t m_nCurPos; |
272 size_t m_nGrowSize; | 270 size_t m_nGrowSize; |
273 FX_DWORD m_dwFlags; | 271 FX_DWORD m_dwFlags; |
274 FX_BOOL ExpandBlocks(size_t size) { | 272 FX_BOOL ExpandBlocks(size_t size) { |
275 if (m_nCurSize < size) { | 273 if (m_nCurSize < size) { |
276 m_nCurSize = size; | 274 m_nCurSize = size; |
277 } | 275 } |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 } FX_MTRANDOMCONTEXT, *FX_LPMTRANDOMCONTEXT; | 307 } FX_MTRANDOMCONTEXT, *FX_LPMTRANDOMCONTEXT; |
310 typedef FX_MTRANDOMCONTEXT const* FX_LPCMTRANDOMCONTEXT; | 308 typedef FX_MTRANDOMCONTEXT const* FX_LPCMTRANDOMCONTEXT; |
311 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 309 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
312 FX_BOOL FX_GenerateCryptoRandom(FX_DWORD* pBuffer, int32_t iCount); | 310 FX_BOOL FX_GenerateCryptoRandom(FX_DWORD* pBuffer, int32_t iCount); |
313 #endif | 311 #endif |
314 #ifdef __cplusplus | 312 #ifdef __cplusplus |
315 } | 313 } |
316 #endif | 314 #endif |
317 | 315 |
318 #endif // CORE_SRC_FXCRT_EXTENSION_H_ | 316 #endif // CORE_SRC_FXCRT_EXTENSION_H_ |
OLD | NEW |