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) { | |
npm
2016/12/01 21:42:20
Nit: no {}
Tom Sepez
2016/12/01 21:43:42
yup.
| |
107 delete this; | |
108 } | |
109 } | |
110 | |
111 FX_FILESIZE CFX_CRTFileStream::GetSize() { | |
112 return m_pFile->GetSize(); | |
113 } | |
114 | |
115 bool CFX_CRTFileStream::IsEOF() { | |
116 return GetPosition() >= GetSize(); | |
117 } | |
118 | |
119 FX_FILESIZE CFX_CRTFileStream::GetPosition() { | |
120 return m_pFile->GetPosition(); | |
121 } | |
122 | |
123 bool CFX_CRTFileStream::ReadBlock(void* buffer, | |
124 FX_FILESIZE offset, | |
125 size_t size) { | |
126 return m_pFile->ReadPos(buffer, size, offset) > 0; | |
127 } | |
128 | |
129 size_t CFX_CRTFileStream::ReadBlock(void* buffer, size_t size) { | |
130 return m_pFile->Read(buffer, size); | |
131 } | |
132 | |
133 bool CFX_CRTFileStream::WriteBlock(const void* buffer, | |
134 FX_FILESIZE offset, | |
135 size_t size) { | |
136 return !!m_pFile->WritePos(buffer, size, offset); | |
137 } | |
138 | |
139 bool CFX_CRTFileStream::Flush() { | |
140 return m_pFile->Flush(); | |
141 } | |
142 | |
143 #define FX_MEMSTREAM_BlockSize (64 * 1024) | |
144 #define FX_MEMSTREAM_Consecutive 0x01 | |
145 #define FX_MEMSTREAM_TakeOver 0x02 | |
146 | |
147 class CFX_MemoryStream final : public IFX_MemoryStream { | |
148 public: | |
149 explicit CFX_MemoryStream(bool bConsecutive); | |
150 CFX_MemoryStream(uint8_t* pBuffer, size_t nSize, bool bTakeOver); | |
151 ~CFX_MemoryStream() override; | |
152 | |
153 // IFX_MemoryStream | |
154 IFX_SeekableStream* Retain() override; | |
155 void Release() override; | |
156 FX_FILESIZE GetSize() override; | |
157 bool IsEOF() override; | |
158 FX_FILESIZE GetPosition() override; | |
159 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override; | |
160 size_t ReadBlock(void* buffer, size_t size) override; | |
161 bool WriteBlock(const void* buffer, FX_FILESIZE offset, size_t size) override; | |
162 bool Flush() override; | |
163 bool IsConsecutive() const override; | |
164 void EstimateSize(size_t nInitSize, size_t nGrowSize) override; | |
165 uint8_t* GetBuffer() const override; | |
166 void AttachBuffer(uint8_t* pBuffer, | |
167 size_t nSize, | |
168 bool bTakeOver = false) override; | |
169 void DetachBuffer() override; | |
170 | |
171 private: | |
172 CFX_ArrayTemplate<uint8_t*> m_Blocks; | |
173 uint32_t m_dwCount; | |
174 size_t m_nTotalSize; | |
175 size_t m_nCurSize; | |
176 size_t m_nCurPos; | |
177 size_t m_nGrowSize; | |
178 uint32_t m_dwFlags; | |
179 bool ExpandBlocks(size_t size); | |
180 }; | |
58 | 181 |
59 CFX_MemoryStream::CFX_MemoryStream(bool bConsecutive) | 182 CFX_MemoryStream::CFX_MemoryStream(bool bConsecutive) |
60 : m_dwCount(1), | 183 : m_dwCount(1), |
61 m_nTotalSize(0), | 184 m_nTotalSize(0), |
62 m_nCurSize(0), | 185 m_nCurSize(0), |
63 m_nCurPos(0), | 186 m_nCurPos(0), |
64 m_nGrowSize(FX_MEMSTREAM_BlockSize) { | 187 m_nGrowSize(FX_MEMSTREAM_BlockSize) { |
65 m_dwFlags = | 188 m_dwFlags = |
66 FX_MEMSTREAM_TakeOver | (bConsecutive ? FX_MEMSTREAM_Consecutive : 0); | 189 FX_MEMSTREAM_TakeOver | (bConsecutive ? FX_MEMSTREAM_Consecutive : 0); |
67 } | 190 } |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
276 size = (size - m_nTotalSize + m_nGrowSize - 1) / m_nGrowSize; | 399 size = (size - m_nTotalSize + m_nGrowSize - 1) / m_nGrowSize; |
277 m_Blocks.SetSize(m_Blocks.GetSize() + (int32_t)size); | 400 m_Blocks.SetSize(m_Blocks.GetSize() + (int32_t)size); |
278 while (size--) { | 401 while (size--) { |
279 uint8_t* pBlock = FX_Alloc(uint8_t, m_nGrowSize); | 402 uint8_t* pBlock = FX_Alloc(uint8_t, m_nGrowSize); |
280 m_Blocks.SetAt(iCount++, pBlock); | 403 m_Blocks.SetAt(iCount++, pBlock); |
281 m_nTotalSize += m_nGrowSize; | 404 m_nTotalSize += m_nGrowSize; |
282 } | 405 } |
283 return true; | 406 return true; |
284 } | 407 } |
285 | 408 |
286 IFX_SeekableStream* CFX_CRTFileStream::Retain() { | 409 } // 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 | 410 |
330 #ifdef PDF_ENABLE_XFA | 411 #ifdef PDF_ENABLE_XFA |
331 IFX_FileAccess* FX_CreateDefaultFileAccess(const CFX_WideStringC& wsPath) { | 412 IFX_FileAccess* FX_CreateDefaultFileAccess(const CFX_WideStringC& wsPath) { |
332 if (wsPath.GetLength() == 0) | 413 if (wsPath.GetLength() == 0) |
333 return nullptr; | 414 return nullptr; |
334 | 415 |
335 CFX_CRTFileAccess* pFA = new CFX_CRTFileAccess; | 416 CFX_CRTFileAccess* pFA = new CFX_CRTFileAccess; |
336 pFA->Init(wsPath); | 417 pFA->Init(wsPath); |
337 return pFA; | 418 return pFA; |
338 } | 419 } |
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
620 b = ((const uint8_t*)pGUID)[i]; | 701 b = ((const uint8_t*)pGUID)[i]; |
621 *pBuf++ = gs_FX_pHexChars[b >> 4]; | 702 *pBuf++ = gs_FX_pHexChars[b >> 4]; |
622 *pBuf++ = gs_FX_pHexChars[b & 0x0F]; | 703 *pBuf++ = gs_FX_pHexChars[b & 0x0F]; |
623 if (bSeparator && (i == 3 || i == 5 || i == 7 || i == 9)) { | 704 if (bSeparator && (i == 3 || i == 5 || i == 7 || i == 9)) { |
624 *pBuf++ = L'-'; | 705 *pBuf++ = L'-'; |
625 } | 706 } |
626 } | 707 } |
627 bsStr.ReleaseBuffer(bSeparator ? 36 : 32); | 708 bsStr.ReleaseBuffer(bSeparator ? 36 : 32); |
628 } | 709 } |
629 #endif // PDF_ENABLE_XFA | 710 #endif // PDF_ENABLE_XFA |
OLD | NEW |