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 _FXCRT_EXTENSION_IMP_ | 7 #ifndef _FXCRT_EXTENSION_IMP_ |
8 #define _FXCRT_EXTENSION_IMP_ | 8 #define _FXCRT_EXTENSION_IMP_ |
9 class IFXCRT_FileAccess | 9 class IFXCRT_FileAccess |
10 { | 10 { |
11 public: | 11 public: |
12 virtual ~IFXCRT_FileAccess() {} | 12 virtual ~IFXCRT_FileAccess() {} |
13 virtual FX_BOOL Open(FX_BSTR fileName, FX_DWORD dwMode) = 0; | 13 virtual FX_BOOL Open(FX_BSTR fileName, FX_DWORD dwMode) = 0; |
14 virtual FX_BOOL Open(FX_WSTR fileName, FX_DWORD dwMode) = 0; | 14 virtual FX_BOOL Open(FX_WSTR fileName, FX_DWORD dwMode) = 0; |
15 virtual void Close() = 0; | 15 virtual void Close() = 0; |
16 virtual void» » Release(IFX_Allocator* pAllocator = NULL) = 0; | 16 virtual void» » Release() = 0; |
17 virtual FX_FILESIZE GetSize() const = 0; | 17 virtual FX_FILESIZE GetSize() const = 0; |
18 virtual FX_FILESIZE GetPosition() const = 0; | 18 virtual FX_FILESIZE GetPosition() const = 0; |
19 virtual FX_FILESIZE SetPosition(FX_FILESIZE pos) = 0; | 19 virtual FX_FILESIZE SetPosition(FX_FILESIZE pos) = 0; |
20 virtual size_t Read(void* pBuffer, size_t szBuffer) = 0; | 20 virtual size_t Read(void* pBuffer, size_t szBuffer) = 0; |
21 virtual size_t Write(const void* pBuffer, size_t szBuffer) = 0; | 21 virtual size_t Write(const void* pBuffer, size_t szBuffer) = 0; |
22 virtual size_t ReadPos(void* pBuffer, size_t szBuffer, FX_FILES
IZE pos) = 0; | 22 virtual size_t ReadPos(void* pBuffer, size_t szBuffer, FX_FILES
IZE pos) = 0; |
23 virtual size_t WritePos(const void* pBuffer, size_t szBuffer, F
X_FILESIZE pos) = 0; | 23 virtual size_t WritePos(const void* pBuffer, size_t szBuffer, F
X_FILESIZE pos) = 0; |
24 virtual FX_BOOL Flush() = 0; | 24 virtual FX_BOOL Flush() = 0; |
25 virtual FX_BOOL Truncate(FX_FILESIZE szFile) = 0; | 25 virtual FX_BOOL Truncate(FX_FILESIZE szFile) = 0; |
26 }; | 26 }; |
27 IFXCRT_FileAccess*» FXCRT_FileAccess_Create(IFX_Allocator* pAllocator = NULL
); | 27 IFXCRT_FileAccess*» FXCRT_FileAccess_Create(); |
28 class CFX_CRTFileStream : public IFX_FileStream, public CFX_Object | 28 class CFX_CRTFileStream : public IFX_FileStream, public CFX_Object |
29 { | 29 { |
30 public: | 30 public: |
31 CFX_CRTFileStream(IFXCRT_FileAccess* pFA, IFX_Allocator* pAllocator) : m_pAl
locator(pAllocator), m_pFile(pFA), m_dwCount(1), m_bUseRange(FALSE), m_nOffset(0
), m_nSize(0) {} | 31 CFX_CRTFileStream(IFXCRT_FileAccess* pFA) : m_pFile(pFA), m_dwCount(1), m_bU
seRange(FALSE), m_nOffset(0), m_nSize(0) {} |
32 ~CFX_CRTFileStream() | 32 ~CFX_CRTFileStream() |
33 { | 33 { |
34 if (m_pFile) { | 34 if (m_pFile) { |
35 m_pFile->Release(m_pAllocator); | 35 m_pFile->Release(); |
36 } | 36 } |
37 } | 37 } |
38 virtual IFX_FileStream* Retain() | 38 virtual IFX_FileStream* Retain() |
39 { | 39 { |
40 m_dwCount ++; | 40 m_dwCount ++; |
41 return this; | 41 return this; |
42 } | 42 } |
43 virtual void Release() | 43 virtual void Release() |
44 { | 44 { |
45 FX_DWORD nCount = -- m_dwCount; | 45 FX_DWORD nCount = -- m_dwCount; |
46 if (!nCount) { | 46 if (!nCount) { |
47 if (m_pAllocator) { | 47 delete this; |
48 FX_DeleteAtAllocator(this, m_pAllocator, CFX_CRTFileStream); | |
49 } else { | |
50 delete this; | |
51 } | |
52 } | 48 } |
53 } | 49 } |
54 virtual FX_FILESIZE GetSize() | 50 virtual FX_FILESIZE GetSize() |
55 { | 51 { |
56 return m_bUseRange ? m_nSize : m_pFile->GetSize(); | 52 return m_bUseRange ? m_nSize : m_pFile->GetSize(); |
57 } | 53 } |
58 virtual FX_BOOL IsEOF() | 54 virtual FX_BOOL IsEOF() |
59 { | 55 { |
60 return GetPosition() >= GetSize(); | 56 return GetPosition() >= GetSize(); |
61 } | 57 } |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 { | 101 { |
106 if (m_bUseRange) { | 102 if (m_bUseRange) { |
107 offset += m_nOffset; | 103 offset += m_nOffset; |
108 } | 104 } |
109 return (FX_BOOL)m_pFile->WritePos(buffer, size, offset); | 105 return (FX_BOOL)m_pFile->WritePos(buffer, size, offset); |
110 } | 106 } |
111 virtual FX_BOOL Flush() | 107 virtual FX_BOOL Flush() |
112 { | 108 { |
113 return m_pFile->Flush(); | 109 return m_pFile->Flush(); |
114 } | 110 } |
115 IFX_Allocator* m_pAllocator; | |
116 IFXCRT_FileAccess* m_pFile; | 111 IFXCRT_FileAccess* m_pFile; |
117 FX_DWORD m_dwCount; | 112 FX_DWORD m_dwCount; |
118 FX_BOOL m_bUseRange; | 113 FX_BOOL m_bUseRange; |
119 FX_FILESIZE m_nOffset; | 114 FX_FILESIZE m_nOffset; |
120 FX_FILESIZE m_nSize; | 115 FX_FILESIZE m_nSize; |
121 }; | 116 }; |
122 #define FX_MEMSTREAM_BlockSize (64 * 1024) | 117 #define FX_MEMSTREAM_BlockSize (64 * 1024) |
123 #define FX_MEMSTREAM_Consecutive 0x01 | 118 #define FX_MEMSTREAM_Consecutive 0x01 |
124 #define FX_MEMSTREAM_TakeOver 0x02 | 119 #define FX_MEMSTREAM_TakeOver 0x02 |
125 class CFX_MemoryStream : public IFX_MemoryStream, public CFX_Object | 120 class CFX_MemoryStream : public IFX_MemoryStream, public CFX_Object |
126 { | 121 { |
127 public: | 122 public: |
128 CFX_MemoryStream(FX_BOOL bConsecutive, IFX_Allocator* pAllocator) | 123 CFX_MemoryStream(FX_BOOL bConsecutive) |
129 : m_Blocks(pAllocator) | 124 : m_dwCount(1) |
130 , m_dwCount(1) | |
131 , m_nTotalSize(0) | 125 , m_nTotalSize(0) |
132 , m_nCurSize(0) | 126 , m_nCurSize(0) |
133 , m_nCurPos(0) | 127 , m_nCurPos(0) |
134 , m_nGrowSize(FX_MEMSTREAM_BlockSize) | 128 , m_nGrowSize(FX_MEMSTREAM_BlockSize) |
135 , m_bUseRange(FALSE) | 129 , m_bUseRange(FALSE) |
136 { | 130 { |
137 m_dwFlags = FX_MEMSTREAM_TakeOver | (bConsecutive ? FX_MEMSTREAM_Consecu
tive : 0); | 131 m_dwFlags = FX_MEMSTREAM_TakeOver | (bConsecutive ? FX_MEMSTREAM_Consecu
tive : 0); |
138 } | 132 } |
139 CFX_MemoryStream(FX_LPBYTE pBuffer, size_t nSize, FX_BOOL bTakeOver, IFX_All
ocator* pAllocator) | 133 CFX_MemoryStream(FX_LPBYTE pBuffer, size_t nSize, FX_BOOL bTakeOver) |
140 : m_Blocks(pAllocator) | 134 : m_dwCount(1) |
141 , m_dwCount(1) | |
142 , m_nTotalSize(nSize) | 135 , m_nTotalSize(nSize) |
143 , m_nCurSize(nSize) | 136 , m_nCurSize(nSize) |
144 , m_nCurPos(0) | 137 , m_nCurPos(0) |
145 , m_nGrowSize(FX_MEMSTREAM_BlockSize) | 138 , m_nGrowSize(FX_MEMSTREAM_BlockSize) |
146 , m_bUseRange(FALSE) | 139 , m_bUseRange(FALSE) |
147 { | 140 { |
148 m_Blocks.Add(pBuffer); | 141 m_Blocks.Add(pBuffer); |
149 m_dwFlags = FX_MEMSTREAM_Consecutive | (bTakeOver ? FX_MEMSTREAM_TakeOve
r : 0); | 142 m_dwFlags = FX_MEMSTREAM_Consecutive | (bTakeOver ? FX_MEMSTREAM_TakeOve
r : 0); |
150 } | 143 } |
151 ~CFX_MemoryStream() | 144 ~CFX_MemoryStream() |
152 { | 145 { |
153 IFX_Allocator* pAllocator = m_Blocks.m_pAllocator; | |
154 if (m_dwFlags & FX_MEMSTREAM_TakeOver) { | 146 if (m_dwFlags & FX_MEMSTREAM_TakeOver) { |
155 for (FX_INT32 i = 0; i < m_Blocks.GetSize(); i ++) { | 147 for (FX_INT32 i = 0; i < m_Blocks.GetSize(); i++) { |
156 FX_Allocator_Free(pAllocator, (FX_LPBYTE)m_Blocks[i]); | 148 FX_Free((FX_LPBYTE)m_Blocks[i]); |
157 } | 149 } |
158 } | 150 } |
159 m_Blocks.RemoveAll(); | 151 m_Blocks.RemoveAll(); |
160 } | 152 } |
161 virtual IFX_FileStream* Retain() | 153 virtual IFX_FileStream* Retain() |
162 { | 154 { |
163 m_dwCount ++; | 155 m_dwCount ++; |
164 return this; | 156 return this; |
165 } | 157 } |
166 virtual void Release() | 158 virtual void Release() |
167 { | 159 { |
168 FX_DWORD nCount = -- m_dwCount; | 160 FX_DWORD nCount = -- m_dwCount; |
169 if (nCount) { | 161 if (nCount) { |
170 return; | 162 return; |
171 } | 163 } |
172 IFX_Allocator* pAllocator = m_Blocks.m_pAllocator; | 164 delete this; |
173 if (pAllocator) { | |
174 FX_DeleteAtAllocator(this, pAllocator, CFX_MemoryStream); | |
175 } else { | |
176 delete this; | |
177 } | |
178 } | 165 } |
179 virtual FX_FILESIZE GetSize() | 166 virtual FX_FILESIZE GetSize() |
180 { | 167 { |
181 return m_bUseRange ? (FX_FILESIZE) m_nSize : (FX_FILESIZE)m_nCurSize; | 168 return m_bUseRange ? (FX_FILESIZE) m_nSize : (FX_FILESIZE)m_nCurSize; |
182 } | 169 } |
183 virtual FX_BOOL IsEOF() | 170 virtual FX_BOOL IsEOF() |
184 { | 171 { |
185 return m_nCurPos >= (size_t)GetSize(); | 172 return m_nCurPos >= (size_t)GetSize(); |
186 } | 173 } |
187 virtual FX_FILESIZE GetPosition() | 174 virtual FX_FILESIZE GetPosition() |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 { | 245 { |
259 if (!buffer || !size) { | 246 if (!buffer || !size) { |
260 return FALSE; | 247 return FALSE; |
261 } | 248 } |
262 if (m_bUseRange) { | 249 if (m_bUseRange) { |
263 offset += (FX_FILESIZE)m_nOffset; | 250 offset += (FX_FILESIZE)m_nOffset; |
264 } | 251 } |
265 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { | 252 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { |
266 m_nCurPos = (size_t)offset + size; | 253 m_nCurPos = (size_t)offset + size; |
267 if (m_nCurPos > m_nTotalSize) { | 254 if (m_nCurPos > m_nTotalSize) { |
268 IFX_Allocator* pAllocator = m_Blocks.m_pAllocator; | |
269 m_nTotalSize = (m_nCurPos + m_nGrowSize - 1) / m_nGrowSize * m_n
GrowSize; | 255 m_nTotalSize = (m_nCurPos + m_nGrowSize - 1) / m_nGrowSize * m_n
GrowSize; |
270 if (m_Blocks.GetSize() < 1) { | 256 if (m_Blocks.GetSize() < 1) { |
271 void* block = FX_Allocator_Alloc(pAllocator, FX_BYTE, m_nTot
alSize); | 257 void* block = FX_Alloc(FX_BYTE, m_nTotalSize); |
272 m_Blocks.Add(block); | 258 m_Blocks.Add(block); |
273 } else { | 259 } else { |
274 m_Blocks[0] = FX_Allocator_Realloc(pAllocator, FX_BYTE, m_Bl
ocks[0], m_nTotalSize); | 260 m_Blocks[0] = FX_Realloc(FX_BYTE, m_Blocks[0], m_nTotalSize)
; |
275 } | 261 } |
276 if (!m_Blocks[0]) { | 262 if (!m_Blocks[0]) { |
277 m_Blocks.RemoveAll(); | 263 m_Blocks.RemoveAll(); |
278 return FALSE; | 264 return FALSE; |
279 } | 265 } |
280 } | 266 } |
281 FXSYS_memcpy32((FX_LPBYTE)m_Blocks[0] + (size_t)offset, buffer, size
); | 267 FXSYS_memcpy32((FX_LPBYTE)m_Blocks[0] + (size_t)offset, buffer, size
); |
282 if (m_nCurSize < m_nCurPos) { | 268 if (m_nCurSize < m_nCurPos) { |
283 m_nCurSize = m_nCurPos; | 269 m_nCurSize = m_nCurPos; |
284 } | 270 } |
(...skipping 23 matching lines...) Expand all Loading... |
308 return TRUE; | 294 return TRUE; |
309 } | 295 } |
310 virtual FX_BOOL IsConsecutive() const | 296 virtual FX_BOOL IsConsecutive() const |
311 { | 297 { |
312 return m_dwFlags & FX_MEMSTREAM_Consecutive; | 298 return m_dwFlags & FX_MEMSTREAM_Consecutive; |
313 } | 299 } |
314 virtual void EstimateSize(size_t nInitSize, s
ize_t nGrowSize) | 300 virtual void EstimateSize(size_t nInitSize, s
ize_t nGrowSize) |
315 { | 301 { |
316 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { | 302 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { |
317 if (m_Blocks.GetSize() < 1) { | 303 if (m_Blocks.GetSize() < 1) { |
318 FX_LPBYTE pBlock = FX_Allocator_Alloc(m_Blocks.m_pAllocator, FX_
BYTE, FX_MAX(nInitSize, 4096)); | 304 FX_LPBYTE pBlock = FX_Alloc(FX_BYTE, FX_MAX(nInitSize, 4096)); |
319 if (pBlock) { | 305 if (pBlock) { |
320 m_Blocks.Add(pBlock); | 306 m_Blocks.Add(pBlock); |
321 } | 307 } |
322 } | 308 } |
323 m_nGrowSize = FX_MAX(nGrowSize, 4096); | 309 m_nGrowSize = FX_MAX(nGrowSize, 4096); |
324 } else if (m_Blocks.GetSize() < 1) { | 310 } else if (m_Blocks.GetSize() < 1) { |
325 m_nGrowSize = FX_MAX(nGrowSize, 4096); | 311 m_nGrowSize = FX_MAX(nGrowSize, 4096); |
326 } | 312 } |
327 } | 313 } |
328 virtual FX_LPBYTE GetBuffer() const | 314 virtual FX_LPBYTE GetBuffer() const |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
366 { | 352 { |
367 if (m_nCurSize < size) { | 353 if (m_nCurSize < size) { |
368 m_nCurSize = size; | 354 m_nCurSize = size; |
369 } | 355 } |
370 if (size <= m_nTotalSize) { | 356 if (size <= m_nTotalSize) { |
371 return TRUE; | 357 return TRUE; |
372 } | 358 } |
373 FX_INT32 iCount = m_Blocks.GetSize(); | 359 FX_INT32 iCount = m_Blocks.GetSize(); |
374 size = (size - m_nTotalSize + m_nGrowSize - 1) / m_nGrowSize; | 360 size = (size - m_nTotalSize + m_nGrowSize - 1) / m_nGrowSize; |
375 m_Blocks.SetSize(m_Blocks.GetSize() + (FX_INT32)size, -1); | 361 m_Blocks.SetSize(m_Blocks.GetSize() + (FX_INT32)size, -1); |
376 IFX_Allocator* pAllocator = m_Blocks.m_pAllocator; | |
377 while (size --) { | 362 while (size --) { |
378 FX_LPBYTE pBlock = FX_Allocator_Alloc(pAllocator, FX_BYTE, m_nGrowSi
ze); | 363 FX_LPBYTE pBlock = FX_Alloc(FX_BYTE, m_nGrowSize); |
379 if (!pBlock) { | 364 if (!pBlock) { |
380 return FALSE; | 365 return FALSE; |
381 } | 366 } |
382 m_Blocks.SetAt(iCount ++, pBlock); | 367 m_Blocks.SetAt(iCount ++, pBlock); |
383 m_nTotalSize += m_nGrowSize; | 368 m_nTotalSize += m_nGrowSize; |
384 } | 369 } |
385 return TRUE; | 370 return TRUE; |
386 } | 371 } |
387 }; | 372 }; |
388 #ifdef __cplusplus | 373 #ifdef __cplusplus |
(...skipping 15 matching lines...) Expand all Loading... |
404 FX_DWORD mt[MT_N]; | 389 FX_DWORD mt[MT_N]; |
405 } FX_MTRANDOMCONTEXT, * FX_LPMTRANDOMCONTEXT; | 390 } FX_MTRANDOMCONTEXT, * FX_LPMTRANDOMCONTEXT; |
406 typedef FX_MTRANDOMCONTEXT const * FX_LPCMTRANDOMCONTEXT; | 391 typedef FX_MTRANDOMCONTEXT const * FX_LPCMTRANDOMCONTEXT; |
407 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 392 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
408 FX_BOOL FX_GenerateCryptoRandom(FX_LPDWORD pBuffer, FX_INT32 iCount); | 393 FX_BOOL FX_GenerateCryptoRandom(FX_LPDWORD pBuffer, FX_INT32 iCount); |
409 #endif | 394 #endif |
410 #ifdef __cplusplus | 395 #ifdef __cplusplus |
411 } | 396 } |
412 #endif | 397 #endif |
413 #endif | 398 #endif |
OLD | NEW |