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 #include "core/fxcrt/extension.h" | 7 #include "core/fxcrt/extension.h" |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <memory> | 10 #include <memory> |
(...skipping 14 matching lines...) Expand all Loading... |
25 | 25 |
26 class CFX_CRTFileAccess : public IFX_FileAccess { | 26 class CFX_CRTFileAccess : public IFX_FileAccess { |
27 public: | 27 public: |
28 CFX_CRTFileAccess(); | 28 CFX_CRTFileAccess(); |
29 ~CFX_CRTFileAccess() override; | 29 ~CFX_CRTFileAccess() override; |
30 | 30 |
31 // IFX_FileAccess | 31 // IFX_FileAccess |
32 void Release() override; | 32 void Release() override; |
33 IFX_FileAccess* Retain() override; | 33 IFX_FileAccess* Retain() override; |
34 void GetPath(CFX_WideString& wsPath) override; | 34 void GetPath(CFX_WideString& wsPath) override; |
35 IFX_SeekableStream* CreateFileStream(uint32_t dwModes) override; | 35 CFX_RetainPtr<IFX_SeekableStream> CreateFileStream(uint32_t dwModes) override; |
36 | 36 |
37 bool Init(const CFX_WideStringC& wsPath); | 37 bool Init(const CFX_WideStringC& wsPath); |
38 | 38 |
39 private: | 39 private: |
40 CFX_WideString m_path; | 40 CFX_WideString m_path; |
41 uint32_t m_RefCount; | 41 uint32_t m_RefCount; |
42 }; | 42 }; |
43 | 43 |
44 CFX_CRTFileAccess::CFX_CRTFileAccess() : m_RefCount(0) {} | 44 CFX_CRTFileAccess::CFX_CRTFileAccess() : m_RefCount(0) {} |
45 | 45 |
46 CFX_CRTFileAccess::~CFX_CRTFileAccess() {} | 46 CFX_CRTFileAccess::~CFX_CRTFileAccess() {} |
47 | 47 |
48 void CFX_CRTFileAccess::Release() { | 48 void CFX_CRTFileAccess::Release() { |
49 if (--m_RefCount == 0) | 49 if (--m_RefCount == 0) |
50 delete this; | 50 delete this; |
51 } | 51 } |
52 | 52 |
53 IFX_FileAccess* CFX_CRTFileAccess::Retain() { | 53 IFX_FileAccess* CFX_CRTFileAccess::Retain() { |
54 m_RefCount++; | 54 m_RefCount++; |
55 return (IFX_FileAccess*)this; | 55 return (IFX_FileAccess*)this; |
56 } | 56 } |
57 | 57 |
58 void CFX_CRTFileAccess::GetPath(CFX_WideString& wsPath) { | 58 void CFX_CRTFileAccess::GetPath(CFX_WideString& wsPath) { |
59 wsPath = m_path; | 59 wsPath = m_path; |
60 } | 60 } |
61 | 61 |
62 IFX_SeekableStream* CFX_CRTFileAccess::CreateFileStream(uint32_t dwModes) { | 62 CFX_RetainPtr<IFX_SeekableStream> CFX_CRTFileAccess::CreateFileStream( |
| 63 uint32_t dwModes) { |
63 return IFX_SeekableStream::CreateFromFilename(m_path.c_str(), dwModes); | 64 return IFX_SeekableStream::CreateFromFilename(m_path.c_str(), dwModes); |
64 } | 65 } |
65 | 66 |
66 bool CFX_CRTFileAccess::Init(const CFX_WideStringC& wsPath) { | 67 bool CFX_CRTFileAccess::Init(const CFX_WideStringC& wsPath) { |
67 m_path = wsPath; | 68 m_path = wsPath; |
68 m_RefCount = 1; | 69 m_RefCount = 1; |
69 return true; | 70 return true; |
70 } | 71 } |
71 | 72 |
72 #endif // PDF_ENABLE_XFA | 73 #endif // PDF_ENABLE_XFA |
73 | 74 |
74 class CFX_CRTFileStream final : public IFX_SeekableStream { | 75 class CFX_CRTFileStream final : public IFX_SeekableStream { |
75 public: | 76 public: |
76 explicit CFX_CRTFileStream(std::unique_ptr<IFXCRT_FileAccess> pFA); | 77 explicit CFX_CRTFileStream(std::unique_ptr<IFXCRT_FileAccess> pFA); |
77 ~CFX_CRTFileStream() override; | 78 ~CFX_CRTFileStream() override; |
78 | 79 |
79 // IFX_SeekableStream: | 80 // IFX_SeekableStream: |
80 IFX_SeekableStream* Retain() override; | |
81 void Release() override; | |
82 FX_FILESIZE GetSize() override; | 81 FX_FILESIZE GetSize() override; |
83 bool IsEOF() override; | 82 bool IsEOF() override; |
84 FX_FILESIZE GetPosition() override; | 83 FX_FILESIZE GetPosition() override; |
85 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override; | 84 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override; |
86 size_t ReadBlock(void* buffer, size_t size) override; | 85 size_t ReadBlock(void* buffer, size_t size) override; |
87 bool WriteBlock(const void* buffer, FX_FILESIZE offset, size_t size) override; | 86 bool WriteBlock(const void* buffer, FX_FILESIZE offset, size_t size) override; |
88 bool Flush() override; | 87 bool Flush() override; |
89 | 88 |
90 private: | 89 private: |
91 std::unique_ptr<IFXCRT_FileAccess> m_pFile; | 90 std::unique_ptr<IFXCRT_FileAccess> m_pFile; |
92 uint32_t m_dwCount; | |
93 }; | 91 }; |
94 | 92 |
95 CFX_CRTFileStream::CFX_CRTFileStream(std::unique_ptr<IFXCRT_FileAccess> pFA) | 93 CFX_CRTFileStream::CFX_CRTFileStream(std::unique_ptr<IFXCRT_FileAccess> pFA) |
96 : m_pFile(std::move(pFA)), m_dwCount(1) {} | 94 : m_pFile(std::move(pFA)) {} |
97 | 95 |
98 CFX_CRTFileStream::~CFX_CRTFileStream() {} | 96 CFX_CRTFileStream::~CFX_CRTFileStream() {} |
99 IFX_SeekableStream* CFX_CRTFileStream::Retain() { | |
100 m_dwCount++; | |
101 return this; | |
102 } | |
103 | |
104 void CFX_CRTFileStream::Release() { | |
105 uint32_t nCount = --m_dwCount; | |
106 if (!nCount) | |
107 delete this; | |
108 } | |
109 | 97 |
110 FX_FILESIZE CFX_CRTFileStream::GetSize() { | 98 FX_FILESIZE CFX_CRTFileStream::GetSize() { |
111 return m_pFile->GetSize(); | 99 return m_pFile->GetSize(); |
112 } | 100 } |
113 | 101 |
114 bool CFX_CRTFileStream::IsEOF() { | 102 bool CFX_CRTFileStream::IsEOF() { |
115 return GetPosition() >= GetSize(); | 103 return GetPosition() >= GetSize(); |
116 } | 104 } |
117 | 105 |
118 FX_FILESIZE CFX_CRTFileStream::GetPosition() { | 106 FX_FILESIZE CFX_CRTFileStream::GetPosition() { |
(...skipping 24 matching lines...) Expand all Loading... |
143 #define FX_MEMSTREAM_Consecutive 0x01 | 131 #define FX_MEMSTREAM_Consecutive 0x01 |
144 #define FX_MEMSTREAM_TakeOver 0x02 | 132 #define FX_MEMSTREAM_TakeOver 0x02 |
145 | 133 |
146 class CFX_MemoryStream final : public IFX_MemoryStream { | 134 class CFX_MemoryStream final : public IFX_MemoryStream { |
147 public: | 135 public: |
148 explicit CFX_MemoryStream(bool bConsecutive); | 136 explicit CFX_MemoryStream(bool bConsecutive); |
149 CFX_MemoryStream(uint8_t* pBuffer, size_t nSize, bool bTakeOver); | 137 CFX_MemoryStream(uint8_t* pBuffer, size_t nSize, bool bTakeOver); |
150 ~CFX_MemoryStream() override; | 138 ~CFX_MemoryStream() override; |
151 | 139 |
152 // IFX_MemoryStream | 140 // IFX_MemoryStream |
153 IFX_SeekableStream* Retain() override; | |
154 void Release() override; | |
155 FX_FILESIZE GetSize() override; | 141 FX_FILESIZE GetSize() override; |
156 bool IsEOF() override; | 142 bool IsEOF() override; |
157 FX_FILESIZE GetPosition() override; | 143 FX_FILESIZE GetPosition() override; |
158 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override; | 144 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override; |
159 size_t ReadBlock(void* buffer, size_t size) override; | 145 size_t ReadBlock(void* buffer, size_t size) override; |
160 bool WriteBlock(const void* buffer, FX_FILESIZE offset, size_t size) override; | 146 bool WriteBlock(const void* buffer, FX_FILESIZE offset, size_t size) override; |
161 bool Flush() override; | 147 bool Flush() override; |
162 bool IsConsecutive() const override; | 148 bool IsConsecutive() const override; |
163 void EstimateSize(size_t nInitSize, size_t nGrowSize) override; | 149 void EstimateSize(size_t nInitSize, size_t nGrowSize) override; |
164 uint8_t* GetBuffer() const override; | 150 uint8_t* GetBuffer() const override; |
165 void AttachBuffer(uint8_t* pBuffer, | 151 void AttachBuffer(uint8_t* pBuffer, |
166 size_t nSize, | 152 size_t nSize, |
167 bool bTakeOver = false) override; | 153 bool bTakeOver = false) override; |
168 void DetachBuffer() override; | 154 void DetachBuffer() override; |
169 | 155 |
170 private: | 156 private: |
| 157 bool ExpandBlocks(size_t size); |
| 158 |
171 CFX_ArrayTemplate<uint8_t*> m_Blocks; | 159 CFX_ArrayTemplate<uint8_t*> m_Blocks; |
172 uint32_t m_dwCount; | |
173 size_t m_nTotalSize; | 160 size_t m_nTotalSize; |
174 size_t m_nCurSize; | 161 size_t m_nCurSize; |
175 size_t m_nCurPos; | 162 size_t m_nCurPos; |
176 size_t m_nGrowSize; | 163 size_t m_nGrowSize; |
177 uint32_t m_dwFlags; | 164 uint32_t m_dwFlags; |
178 bool ExpandBlocks(size_t size); | |
179 }; | 165 }; |
180 | 166 |
181 CFX_MemoryStream::CFX_MemoryStream(bool bConsecutive) | 167 CFX_MemoryStream::CFX_MemoryStream(bool bConsecutive) |
182 : m_dwCount(1), | 168 : m_nTotalSize(0), |
183 m_nTotalSize(0), | |
184 m_nCurSize(0), | 169 m_nCurSize(0), |
185 m_nCurPos(0), | 170 m_nCurPos(0), |
186 m_nGrowSize(FX_MEMSTREAM_BlockSize) { | 171 m_nGrowSize(FX_MEMSTREAM_BlockSize) { |
187 m_dwFlags = | 172 m_dwFlags = |
188 FX_MEMSTREAM_TakeOver | (bConsecutive ? FX_MEMSTREAM_Consecutive : 0); | 173 FX_MEMSTREAM_TakeOver | (bConsecutive ? FX_MEMSTREAM_Consecutive : 0); |
189 } | 174 } |
190 | 175 |
191 CFX_MemoryStream::CFX_MemoryStream(uint8_t* pBuffer, | 176 CFX_MemoryStream::CFX_MemoryStream(uint8_t* pBuffer, |
192 size_t nSize, | 177 size_t nSize, |
193 bool bTakeOver) | 178 bool bTakeOver) |
194 : m_dwCount(1), | 179 : m_nTotalSize(nSize), |
195 m_nTotalSize(nSize), | |
196 m_nCurSize(nSize), | 180 m_nCurSize(nSize), |
197 m_nCurPos(0), | 181 m_nCurPos(0), |
198 m_nGrowSize(FX_MEMSTREAM_BlockSize) { | 182 m_nGrowSize(FX_MEMSTREAM_BlockSize) { |
199 m_Blocks.Add(pBuffer); | 183 m_Blocks.Add(pBuffer); |
200 m_dwFlags = | 184 m_dwFlags = |
201 FX_MEMSTREAM_Consecutive | (bTakeOver ? FX_MEMSTREAM_TakeOver : 0); | 185 FX_MEMSTREAM_Consecutive | (bTakeOver ? FX_MEMSTREAM_TakeOver : 0); |
202 } | 186 } |
203 | 187 |
204 CFX_MemoryStream::~CFX_MemoryStream() { | 188 CFX_MemoryStream::~CFX_MemoryStream() { |
205 if (m_dwFlags & FX_MEMSTREAM_TakeOver) { | 189 if (m_dwFlags & FX_MEMSTREAM_TakeOver) { |
206 for (int32_t i = 0; i < m_Blocks.GetSize(); i++) { | 190 for (int32_t i = 0; i < m_Blocks.GetSize(); i++) { |
207 FX_Free(m_Blocks[i]); | 191 FX_Free(m_Blocks[i]); |
208 } | 192 } |
209 } | 193 } |
210 m_Blocks.RemoveAll(); | 194 m_Blocks.RemoveAll(); |
211 } | 195 } |
212 | 196 |
213 IFX_SeekableStream* CFX_MemoryStream::Retain() { | |
214 m_dwCount++; | |
215 return this; | |
216 } | |
217 | |
218 void CFX_MemoryStream::Release() { | |
219 uint32_t nCount = --m_dwCount; | |
220 if (nCount) { | |
221 return; | |
222 } | |
223 delete this; | |
224 } | |
225 | |
226 FX_FILESIZE CFX_MemoryStream::GetSize() { | 197 FX_FILESIZE CFX_MemoryStream::GetSize() { |
227 return (FX_FILESIZE)m_nCurSize; | 198 return (FX_FILESIZE)m_nCurSize; |
228 } | 199 } |
229 | 200 |
230 bool CFX_MemoryStream::IsEOF() { | 201 bool CFX_MemoryStream::IsEOF() { |
231 return m_nCurPos >= (size_t)GetSize(); | 202 return m_nCurPos >= (size_t)GetSize(); |
232 } | 203 } |
233 | 204 |
234 FX_FILESIZE CFX_MemoryStream::GetPosition() { | 205 FX_FILESIZE CFX_MemoryStream::GetPosition() { |
235 return (FX_FILESIZE)m_nCurPos; | 206 return (FX_FILESIZE)m_nCurPos; |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
412 if (wsPath.GetLength() == 0) | 383 if (wsPath.GetLength() == 0) |
413 return nullptr; | 384 return nullptr; |
414 | 385 |
415 CFX_CRTFileAccess* pFA = new CFX_CRTFileAccess; | 386 CFX_CRTFileAccess* pFA = new CFX_CRTFileAccess; |
416 pFA->Init(wsPath); | 387 pFA->Init(wsPath); |
417 return pFA; | 388 return pFA; |
418 } | 389 } |
419 #endif // PDF_ENABLE_XFA | 390 #endif // PDF_ENABLE_XFA |
420 | 391 |
421 // static | 392 // static |
422 IFX_SeekableStream* IFX_SeekableStream::CreateFromFilename( | 393 CFX_RetainPtr<IFX_SeekableStream> IFX_SeekableStream::CreateFromFilename( |
423 const FX_CHAR* filename, | 394 const FX_CHAR* filename, |
424 uint32_t dwModes) { | 395 uint32_t dwModes) { |
425 std::unique_ptr<IFXCRT_FileAccess> pFA(IFXCRT_FileAccess::Create()); | 396 std::unique_ptr<IFXCRT_FileAccess> pFA(IFXCRT_FileAccess::Create()); |
426 if (!pFA->Open(filename, dwModes)) | 397 if (!pFA->Open(filename, dwModes)) |
427 return nullptr; | 398 return nullptr; |
428 return new CFX_CRTFileStream(std::move(pFA)); | 399 return CFX_RetainPtr<IFX_SeekableStream>( |
| 400 new CFX_CRTFileStream(std::move(pFA))); |
429 } | 401 } |
430 | 402 |
431 // static | 403 // static |
432 IFX_SeekableStream* IFX_SeekableStream::CreateFromFilename( | 404 CFX_RetainPtr<IFX_SeekableStream> IFX_SeekableStream::CreateFromFilename( |
433 const FX_WCHAR* filename, | 405 const FX_WCHAR* filename, |
434 uint32_t dwModes) { | 406 uint32_t dwModes) { |
435 std::unique_ptr<IFXCRT_FileAccess> pFA(IFXCRT_FileAccess::Create()); | 407 std::unique_ptr<IFXCRT_FileAccess> pFA(IFXCRT_FileAccess::Create()); |
436 if (!pFA->Open(filename, dwModes)) | 408 if (!pFA->Open(filename, dwModes)) |
437 return nullptr; | 409 return nullptr; |
438 return new CFX_CRTFileStream(std::move(pFA)); | 410 return CFX_RetainPtr<IFX_SeekableStream>( |
| 411 new CFX_CRTFileStream(std::move(pFA))); |
439 } | 412 } |
440 | 413 |
441 // static | 414 // static |
442 IFX_SeekableReadStream* IFX_SeekableReadStream::CreateFromFilename( | 415 CFX_RetainPtr<IFX_SeekableReadStream> |
443 const FX_CHAR* filename) { | 416 IFX_SeekableReadStream::CreateFromFilename(const FX_CHAR* filename) { |
444 return IFX_SeekableStream::CreateFromFilename(filename, FX_FILEMODE_ReadOnly); | 417 return IFX_SeekableStream::CreateFromFilename(filename, FX_FILEMODE_ReadOnly); |
445 } | 418 } |
446 | 419 |
447 // static | 420 // static |
448 IFX_MemoryStream* IFX_MemoryStream::Create(uint8_t* pBuffer, | 421 CFX_RetainPtr<IFX_MemoryStream> IFX_MemoryStream::Create(uint8_t* pBuffer, |
449 size_t dwSize, | 422 size_t dwSize, |
450 bool bTakeOver) { | 423 bool bTakeOver) { |
451 return new CFX_MemoryStream(pBuffer, dwSize, bTakeOver); | 424 return CFX_RetainPtr<IFX_MemoryStream>( |
| 425 new CFX_MemoryStream(pBuffer, dwSize, bTakeOver)); |
452 } | 426 } |
453 | 427 |
454 // static | 428 // static |
455 IFX_MemoryStream* IFX_MemoryStream::Create(bool bConsecutive) { | 429 CFX_RetainPtr<IFX_MemoryStream> IFX_MemoryStream::Create(bool bConsecutive) { |
456 return new CFX_MemoryStream(bConsecutive); | 430 return CFX_RetainPtr<IFX_MemoryStream>(new CFX_MemoryStream(bConsecutive)); |
457 } | 431 } |
458 | 432 |
459 FX_FLOAT FXSYS_tan(FX_FLOAT a) { | 433 FX_FLOAT FXSYS_tan(FX_FLOAT a) { |
460 return (FX_FLOAT)tan(a); | 434 return (FX_FLOAT)tan(a); |
461 } | 435 } |
462 FX_FLOAT FXSYS_logb(FX_FLOAT b, FX_FLOAT x) { | 436 FX_FLOAT FXSYS_logb(FX_FLOAT b, FX_FLOAT x) { |
463 return FXSYS_log(x) / FXSYS_log(b); | 437 return FXSYS_log(x) / FXSYS_log(b); |
464 } | 438 } |
465 FX_FLOAT FXSYS_strtof(const FX_CHAR* pcsStr, | 439 FX_FLOAT FXSYS_strtof(const FX_CHAR* pcsStr, |
466 int32_t iLength, | 440 int32_t iLength, |
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
700 b = ((const uint8_t*)pGUID)[i]; | 674 b = ((const uint8_t*)pGUID)[i]; |
701 *pBuf++ = gs_FX_pHexChars[b >> 4]; | 675 *pBuf++ = gs_FX_pHexChars[b >> 4]; |
702 *pBuf++ = gs_FX_pHexChars[b & 0x0F]; | 676 *pBuf++ = gs_FX_pHexChars[b & 0x0F]; |
703 if (bSeparator && (i == 3 || i == 5 || i == 7 || i == 9)) { | 677 if (bSeparator && (i == 3 || i == 5 || i == 7 || i == 9)) { |
704 *pBuf++ = L'-'; | 678 *pBuf++ = L'-'; |
705 } | 679 } |
706 } | 680 } |
707 bsStr.ReleaseBuffer(bSeparator ? 36 : 32); | 681 bsStr.ReleaseBuffer(bSeparator ? 36 : 32); |
708 } | 682 } |
709 #endif // PDF_ENABLE_XFA | 683 #endif // PDF_ENABLE_XFA |
OLD | NEW |