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> |
11 #include <utility> | 11 #include <utility> |
12 | 12 |
13 #include "core/fxcrt/fx_basic.h" | 13 #include "core/fxcrt/fx_basic.h" |
14 #include "core/fxcrt/fx_ext.h" | 14 #include "core/fxcrt/fx_ext.h" |
15 | 15 |
16 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 16 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
17 #include <wincrypt.h> | 17 #include <wincrypt.h> |
18 #else | 18 #else |
19 #include <ctime> | 19 #include <ctime> |
20 #endif | 20 #endif |
21 | 21 |
| 22 namespace { |
| 23 |
22 #ifdef PDF_ENABLE_XFA | 24 #ifdef PDF_ENABLE_XFA |
23 | 25 |
| 26 class CFX_CRTFileAccess : public IFX_FileAccess { |
| 27 public: |
| 28 CFX_CRTFileAccess(); |
| 29 ~CFX_CRTFileAccess() override; |
| 30 |
| 31 // IFX_FileAccess |
| 32 void Release() override; |
| 33 IFX_FileAccess* Retain() override; |
| 34 void GetPath(CFX_WideString& wsPath) override; |
| 35 IFX_SeekableStream* CreateFileStream(uint32_t dwModes) override; |
| 36 |
| 37 bool Init(const CFX_WideStringC& wsPath); |
| 38 |
| 39 private: |
| 40 CFX_WideString m_path; |
| 41 uint32_t m_RefCount; |
| 42 }; |
| 43 |
24 CFX_CRTFileAccess::CFX_CRTFileAccess() : m_RefCount(0) {} | 44 CFX_CRTFileAccess::CFX_CRTFileAccess() : m_RefCount(0) {} |
25 | 45 |
26 CFX_CRTFileAccess::~CFX_CRTFileAccess() {} | 46 CFX_CRTFileAccess::~CFX_CRTFileAccess() {} |
27 | 47 |
28 void CFX_CRTFileAccess::Release() { | 48 void CFX_CRTFileAccess::Release() { |
29 if (--m_RefCount == 0) | 49 if (--m_RefCount == 0) |
30 delete this; | 50 delete this; |
31 } | 51 } |
32 | 52 |
33 IFX_FileAccess* CFX_CRTFileAccess::Retain() { | 53 IFX_FileAccess* CFX_CRTFileAccess::Retain() { |
(...skipping 10 matching lines...) Expand all Loading... |
44 } | 64 } |
45 | 65 |
46 bool CFX_CRTFileAccess::Init(const CFX_WideStringC& wsPath) { | 66 bool CFX_CRTFileAccess::Init(const CFX_WideStringC& wsPath) { |
47 m_path = wsPath; | 67 m_path = wsPath; |
48 m_RefCount = 1; | 68 m_RefCount = 1; |
49 return true; | 69 return true; |
50 } | 70 } |
51 | 71 |
52 #endif // PDF_ENABLE_XFA | 72 #endif // PDF_ENABLE_XFA |
53 | 73 |
| 74 class CFX_CRTFileStream final : public IFX_SeekableStream { |
| 75 public: |
| 76 explicit CFX_CRTFileStream(std::unique_ptr<IFXCRT_FileAccess> pFA); |
| 77 ~CFX_CRTFileStream() override; |
| 78 |
| 79 // IFX_SeekableStream: |
| 80 IFX_SeekableStream* Retain() override; |
| 81 void Release() override; |
| 82 FX_FILESIZE GetSize() override; |
| 83 bool IsEOF() override; |
| 84 FX_FILESIZE GetPosition() override; |
| 85 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override; |
| 86 size_t ReadBlock(void* buffer, size_t size) override; |
| 87 bool WriteBlock(const void* buffer, FX_FILESIZE offset, size_t size) override; |
| 88 bool Flush() override; |
| 89 |
| 90 private: |
| 91 std::unique_ptr<IFXCRT_FileAccess> m_pFile; |
| 92 uint32_t m_dwCount; |
| 93 }; |
| 94 |
54 CFX_CRTFileStream::CFX_CRTFileStream(std::unique_ptr<IFXCRT_FileAccess> pFA) | 95 CFX_CRTFileStream::CFX_CRTFileStream(std::unique_ptr<IFXCRT_FileAccess> pFA) |
55 : m_pFile(std::move(pFA)), m_dwCount(1) {} | 96 : m_pFile(std::move(pFA)), m_dwCount(1) {} |
56 | 97 |
57 CFX_CRTFileStream::~CFX_CRTFileStream() {} | 98 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 |
| 110 FX_FILESIZE CFX_CRTFileStream::GetSize() { |
| 111 return m_pFile->GetSize(); |
| 112 } |
| 113 |
| 114 bool CFX_CRTFileStream::IsEOF() { |
| 115 return GetPosition() >= GetSize(); |
| 116 } |
| 117 |
| 118 FX_FILESIZE CFX_CRTFileStream::GetPosition() { |
| 119 return m_pFile->GetPosition(); |
| 120 } |
| 121 |
| 122 bool CFX_CRTFileStream::ReadBlock(void* buffer, |
| 123 FX_FILESIZE offset, |
| 124 size_t size) { |
| 125 return m_pFile->ReadPos(buffer, size, offset) > 0; |
| 126 } |
| 127 |
| 128 size_t CFX_CRTFileStream::ReadBlock(void* buffer, size_t size) { |
| 129 return m_pFile->Read(buffer, size); |
| 130 } |
| 131 |
| 132 bool CFX_CRTFileStream::WriteBlock(const void* buffer, |
| 133 FX_FILESIZE offset, |
| 134 size_t size) { |
| 135 return !!m_pFile->WritePos(buffer, size, offset); |
| 136 } |
| 137 |
| 138 bool CFX_CRTFileStream::Flush() { |
| 139 return m_pFile->Flush(); |
| 140 } |
| 141 |
| 142 #define FX_MEMSTREAM_BlockSize (64 * 1024) |
| 143 #define FX_MEMSTREAM_Consecutive 0x01 |
| 144 #define FX_MEMSTREAM_TakeOver 0x02 |
| 145 |
| 146 class CFX_MemoryStream final : public IFX_MemoryStream { |
| 147 public: |
| 148 explicit CFX_MemoryStream(bool bConsecutive); |
| 149 CFX_MemoryStream(uint8_t* pBuffer, size_t nSize, bool bTakeOver); |
| 150 ~CFX_MemoryStream() override; |
| 151 |
| 152 // IFX_MemoryStream |
| 153 IFX_SeekableStream* Retain() override; |
| 154 void Release() override; |
| 155 FX_FILESIZE GetSize() override; |
| 156 bool IsEOF() override; |
| 157 FX_FILESIZE GetPosition() override; |
| 158 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override; |
| 159 size_t ReadBlock(void* buffer, size_t size) override; |
| 160 bool WriteBlock(const void* buffer, FX_FILESIZE offset, size_t size) override; |
| 161 bool Flush() override; |
| 162 bool IsConsecutive() const override; |
| 163 void EstimateSize(size_t nInitSize, size_t nGrowSize) override; |
| 164 uint8_t* GetBuffer() const override; |
| 165 void AttachBuffer(uint8_t* pBuffer, |
| 166 size_t nSize, |
| 167 bool bTakeOver = false) override; |
| 168 void DetachBuffer() override; |
| 169 |
| 170 private: |
| 171 CFX_ArrayTemplate<uint8_t*> m_Blocks; |
| 172 uint32_t m_dwCount; |
| 173 size_t m_nTotalSize; |
| 174 size_t m_nCurSize; |
| 175 size_t m_nCurPos; |
| 176 size_t m_nGrowSize; |
| 177 uint32_t m_dwFlags; |
| 178 bool ExpandBlocks(size_t size); |
| 179 }; |
58 | 180 |
59 CFX_MemoryStream::CFX_MemoryStream(bool bConsecutive) | 181 CFX_MemoryStream::CFX_MemoryStream(bool bConsecutive) |
60 : m_dwCount(1), | 182 : m_dwCount(1), |
61 m_nTotalSize(0), | 183 m_nTotalSize(0), |
62 m_nCurSize(0), | 184 m_nCurSize(0), |
63 m_nCurPos(0), | 185 m_nCurPos(0), |
64 m_nGrowSize(FX_MEMSTREAM_BlockSize) { | 186 m_nGrowSize(FX_MEMSTREAM_BlockSize) { |
65 m_dwFlags = | 187 m_dwFlags = |
66 FX_MEMSTREAM_TakeOver | (bConsecutive ? FX_MEMSTREAM_Consecutive : 0); | 188 FX_MEMSTREAM_TakeOver | (bConsecutive ? FX_MEMSTREAM_Consecutive : 0); |
67 } | 189 } |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 size = (size - m_nTotalSize + m_nGrowSize - 1) / m_nGrowSize; | 398 size = (size - m_nTotalSize + m_nGrowSize - 1) / m_nGrowSize; |
277 m_Blocks.SetSize(m_Blocks.GetSize() + (int32_t)size); | 399 m_Blocks.SetSize(m_Blocks.GetSize() + (int32_t)size); |
278 while (size--) { | 400 while (size--) { |
279 uint8_t* pBlock = FX_Alloc(uint8_t, m_nGrowSize); | 401 uint8_t* pBlock = FX_Alloc(uint8_t, m_nGrowSize); |
280 m_Blocks.SetAt(iCount++, pBlock); | 402 m_Blocks.SetAt(iCount++, pBlock); |
281 m_nTotalSize += m_nGrowSize; | 403 m_nTotalSize += m_nGrowSize; |
282 } | 404 } |
283 return true; | 405 return true; |
284 } | 406 } |
285 | 407 |
286 IFX_SeekableStream* CFX_CRTFileStream::Retain() { | 408 } // namespace |
287 m_dwCount++; | |
288 return this; | |
289 } | |
290 | |
291 void CFX_CRTFileStream::Release() { | |
292 uint32_t nCount = --m_dwCount; | |
293 if (!nCount) { | |
294 delete this; | |
295 } | |
296 } | |
297 | |
298 FX_FILESIZE CFX_CRTFileStream::GetSize() { | |
299 return m_pFile->GetSize(); | |
300 } | |
301 | |
302 bool CFX_CRTFileStream::IsEOF() { | |
303 return GetPosition() >= GetSize(); | |
304 } | |
305 | |
306 FX_FILESIZE CFX_CRTFileStream::GetPosition() { | |
307 return m_pFile->GetPosition(); | |
308 } | |
309 | |
310 bool CFX_CRTFileStream::ReadBlock(void* buffer, | |
311 FX_FILESIZE offset, | |
312 size_t size) { | |
313 return m_pFile->ReadPos(buffer, size, offset) > 0; | |
314 } | |
315 | |
316 size_t CFX_CRTFileStream::ReadBlock(void* buffer, size_t size) { | |
317 return m_pFile->Read(buffer, size); | |
318 } | |
319 | |
320 bool CFX_CRTFileStream::WriteBlock(const void* buffer, | |
321 FX_FILESIZE offset, | |
322 size_t size) { | |
323 return !!m_pFile->WritePos(buffer, size, offset); | |
324 } | |
325 | |
326 bool CFX_CRTFileStream::Flush() { | |
327 return m_pFile->Flush(); | |
328 } | |
329 | 409 |
330 #ifdef PDF_ENABLE_XFA | 410 #ifdef PDF_ENABLE_XFA |
331 IFX_FileAccess* FX_CreateDefaultFileAccess(const CFX_WideStringC& wsPath) { | 411 IFX_FileAccess* FX_CreateDefaultFileAccess(const CFX_WideStringC& wsPath) { |
332 if (wsPath.GetLength() == 0) | 412 if (wsPath.GetLength() == 0) |
333 return nullptr; | 413 return nullptr; |
334 | 414 |
335 CFX_CRTFileAccess* pFA = new CFX_CRTFileAccess; | 415 CFX_CRTFileAccess* pFA = new CFX_CRTFileAccess; |
336 pFA->Init(wsPath); | 416 pFA->Init(wsPath); |
337 return pFA; | 417 return pFA; |
338 } | 418 } |
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
620 b = ((const uint8_t*)pGUID)[i]; | 700 b = ((const uint8_t*)pGUID)[i]; |
621 *pBuf++ = gs_FX_pHexChars[b >> 4]; | 701 *pBuf++ = gs_FX_pHexChars[b >> 4]; |
622 *pBuf++ = gs_FX_pHexChars[b & 0x0F]; | 702 *pBuf++ = gs_FX_pHexChars[b & 0x0F]; |
623 if (bSeparator && (i == 3 || i == 5 || i == 7 || i == 9)) { | 703 if (bSeparator && (i == 3 || i == 5 || i == 7 || i == 9)) { |
624 *pBuf++ = L'-'; | 704 *pBuf++ = L'-'; |
625 } | 705 } |
626 } | 706 } |
627 bsStr.ReleaseBuffer(bSeparator ? 36 : 32); | 707 bsStr.ReleaseBuffer(bSeparator ? 36 : 32); |
628 } | 708 } |
629 #endif // PDF_ENABLE_XFA | 709 #endif // PDF_ENABLE_XFA |
OLD | NEW |