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 <utility> | 7 #include <utility> |
8 | 8 |
9 #include "core/fxcrt/extension.h" | 9 #include "core/fxcrt/extension.h" |
10 #include "core/fxcrt/include/fx_basic.h" | 10 #include "core/fxcrt/include/fx_basic.h" |
11 #include "core/fxcrt/include/fx_ext.h" | 11 #include "core/fxcrt/include/fx_ext.h" |
12 | 12 |
13 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 13 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
14 #include <wincrypt.h> | 14 #include <wincrypt.h> |
15 #else | 15 #else |
16 #include <ctime> | 16 #include <ctime> |
17 #endif | 17 #endif |
18 | 18 |
| 19 #ifdef PDF_ENABLE_XFA |
| 20 |
| 21 CFX_CRTFileAccess::CFX_CRTFileAccess() : m_RefCount(0) {} |
| 22 |
| 23 CFX_CRTFileAccess::~CFX_CRTFileAccess() {} |
| 24 |
| 25 void CFX_CRTFileAccess::Release() { |
| 26 if (--m_RefCount == 0) |
| 27 delete this; |
| 28 } |
| 29 |
| 30 IFX_FileAccess* CFX_CRTFileAccess::Retain() { |
| 31 m_RefCount++; |
| 32 return (IFX_FileAccess*)this; |
| 33 } |
| 34 |
| 35 void CFX_CRTFileAccess::GetPath(CFX_WideString& wsPath) { |
| 36 wsPath = m_path; |
| 37 } |
| 38 |
| 39 IFX_FileStream* CFX_CRTFileAccess::CreateFileStream(uint32_t dwModes) { |
| 40 return FX_CreateFileStream(m_path.c_str(), dwModes); |
| 41 } |
| 42 |
| 43 FX_BOOL CFX_CRTFileAccess::Init(const CFX_WideStringC& wsPath) { |
| 44 m_path = wsPath; |
| 45 m_RefCount = 1; |
| 46 return TRUE; |
| 47 } |
| 48 |
| 49 #endif // PDF_ENABLE_XFA |
| 50 |
19 CFX_CRTFileStream::CFX_CRTFileStream(std::unique_ptr<IFXCRT_FileAccess> pFA) | 51 CFX_CRTFileStream::CFX_CRTFileStream(std::unique_ptr<IFXCRT_FileAccess> pFA) |
20 : m_pFile(std::move(pFA)), m_dwCount(1) {} | 52 : m_pFile(std::move(pFA)), m_dwCount(1) {} |
21 | 53 |
22 CFX_CRTFileStream::~CFX_CRTFileStream() {} | 54 CFX_CRTFileStream::~CFX_CRTFileStream() {} |
23 | 55 |
| 56 CFX_MemoryStream::CFX_MemoryStream(FX_BOOL bConsecutive) |
| 57 : m_dwCount(1), |
| 58 m_nTotalSize(0), |
| 59 m_nCurSize(0), |
| 60 m_nCurPos(0), |
| 61 m_nGrowSize(FX_MEMSTREAM_BlockSize) { |
| 62 m_dwFlags = |
| 63 FX_MEMSTREAM_TakeOver | (bConsecutive ? FX_MEMSTREAM_Consecutive : 0); |
| 64 } |
| 65 |
| 66 CFX_MemoryStream::CFX_MemoryStream(uint8_t* pBuffer, |
| 67 size_t nSize, |
| 68 FX_BOOL bTakeOver) |
| 69 : m_dwCount(1), |
| 70 m_nTotalSize(nSize), |
| 71 m_nCurSize(nSize), |
| 72 m_nCurPos(0), |
| 73 m_nGrowSize(FX_MEMSTREAM_BlockSize) { |
| 74 m_Blocks.Add(pBuffer); |
| 75 m_dwFlags = |
| 76 FX_MEMSTREAM_Consecutive | (bTakeOver ? FX_MEMSTREAM_TakeOver : 0); |
| 77 } |
| 78 |
| 79 CFX_MemoryStream::~CFX_MemoryStream() { |
| 80 if (m_dwFlags & FX_MEMSTREAM_TakeOver) { |
| 81 for (int32_t i = 0; i < m_Blocks.GetSize(); i++) { |
| 82 FX_Free(m_Blocks[i]); |
| 83 } |
| 84 } |
| 85 m_Blocks.RemoveAll(); |
| 86 } |
| 87 |
| 88 IFX_FileStream* CFX_MemoryStream::Retain() { |
| 89 m_dwCount++; |
| 90 return this; |
| 91 } |
| 92 |
| 93 void CFX_MemoryStream::Release() { |
| 94 uint32_t nCount = --m_dwCount; |
| 95 if (nCount) { |
| 96 return; |
| 97 } |
| 98 delete this; |
| 99 } |
| 100 |
| 101 FX_FILESIZE CFX_MemoryStream::GetSize() { |
| 102 return (FX_FILESIZE)m_nCurSize; |
| 103 } |
| 104 |
| 105 FX_BOOL CFX_MemoryStream::IsEOF() { |
| 106 return m_nCurPos >= (size_t)GetSize(); |
| 107 } |
| 108 |
| 109 FX_FILESIZE CFX_MemoryStream::GetPosition() { |
| 110 return (FX_FILESIZE)m_nCurPos; |
| 111 } |
| 112 |
| 113 FX_BOOL CFX_MemoryStream::ReadBlock(void* buffer, |
| 114 FX_FILESIZE offset, |
| 115 size_t size) { |
| 116 if (!buffer || !size) { |
| 117 return FALSE; |
| 118 } |
| 119 |
| 120 FX_SAFE_SIZE_T newPos = size; |
| 121 newPos += offset; |
| 122 if (!newPos.IsValid() || newPos.ValueOrDefault(0) == 0 || |
| 123 newPos.ValueOrDie() > m_nCurSize) { |
| 124 return FALSE; |
| 125 } |
| 126 |
| 127 m_nCurPos = newPos.ValueOrDie(); |
| 128 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { |
| 129 FXSYS_memcpy(buffer, m_Blocks[0] + (size_t)offset, size); |
| 130 return TRUE; |
| 131 } |
| 132 size_t nStartBlock = (size_t)offset / m_nGrowSize; |
| 133 offset -= (FX_FILESIZE)(nStartBlock * m_nGrowSize); |
| 134 while (size) { |
| 135 size_t nRead = m_nGrowSize - (size_t)offset; |
| 136 if (nRead > size) { |
| 137 nRead = size; |
| 138 } |
| 139 FXSYS_memcpy(buffer, m_Blocks[(int)nStartBlock] + (size_t)offset, nRead); |
| 140 buffer = ((uint8_t*)buffer) + nRead; |
| 141 size -= nRead; |
| 142 nStartBlock++; |
| 143 offset = 0; |
| 144 } |
| 145 return TRUE; |
| 146 } |
| 147 |
| 148 size_t CFX_MemoryStream::ReadBlock(void* buffer, size_t size) { |
| 149 if (m_nCurPos >= m_nCurSize) { |
| 150 return 0; |
| 151 } |
| 152 size_t nRead = std::min(size, m_nCurSize - m_nCurPos); |
| 153 if (!ReadBlock(buffer, (int32_t)m_nCurPos, nRead)) { |
| 154 return 0; |
| 155 } |
| 156 return nRead; |
| 157 } |
| 158 |
| 159 FX_BOOL CFX_MemoryStream::WriteBlock(const void* buffer, |
| 160 FX_FILESIZE offset, |
| 161 size_t size) { |
| 162 if (!buffer || !size) { |
| 163 return FALSE; |
| 164 } |
| 165 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { |
| 166 FX_SAFE_SIZE_T newPos = size; |
| 167 newPos += offset; |
| 168 if (!newPos.IsValid()) |
| 169 return FALSE; |
| 170 |
| 171 m_nCurPos = newPos.ValueOrDie(); |
| 172 if (m_nCurPos > m_nTotalSize) { |
| 173 m_nTotalSize = (m_nCurPos + m_nGrowSize - 1) / m_nGrowSize * m_nGrowSize; |
| 174 if (m_Blocks.GetSize() < 1) { |
| 175 uint8_t* block = FX_Alloc(uint8_t, m_nTotalSize); |
| 176 m_Blocks.Add(block); |
| 177 } else { |
| 178 m_Blocks[0] = FX_Realloc(uint8_t, m_Blocks[0], m_nTotalSize); |
| 179 } |
| 180 if (!m_Blocks[0]) { |
| 181 m_Blocks.RemoveAll(); |
| 182 return FALSE; |
| 183 } |
| 184 } |
| 185 FXSYS_memcpy(m_Blocks[0] + (size_t)offset, buffer, size); |
| 186 if (m_nCurSize < m_nCurPos) { |
| 187 m_nCurSize = m_nCurPos; |
| 188 } |
| 189 return TRUE; |
| 190 } |
| 191 |
| 192 FX_SAFE_SIZE_T newPos = size; |
| 193 newPos += offset; |
| 194 if (!newPos.IsValid()) { |
| 195 return FALSE; |
| 196 } |
| 197 |
| 198 if (!ExpandBlocks(newPos.ValueOrDie())) { |
| 199 return FALSE; |
| 200 } |
| 201 m_nCurPos = newPos.ValueOrDie(); |
| 202 size_t nStartBlock = (size_t)offset / m_nGrowSize; |
| 203 offset -= (FX_FILESIZE)(nStartBlock * m_nGrowSize); |
| 204 while (size) { |
| 205 size_t nWrite = m_nGrowSize - (size_t)offset; |
| 206 if (nWrite > size) { |
| 207 nWrite = size; |
| 208 } |
| 209 FXSYS_memcpy(m_Blocks[(int)nStartBlock] + (size_t)offset, buffer, nWrite); |
| 210 buffer = ((uint8_t*)buffer) + nWrite; |
| 211 size -= nWrite; |
| 212 nStartBlock++; |
| 213 offset = 0; |
| 214 } |
| 215 return TRUE; |
| 216 } |
| 217 |
| 218 FX_BOOL CFX_MemoryStream::Flush() { |
| 219 return TRUE; |
| 220 } |
| 221 |
| 222 FX_BOOL CFX_MemoryStream::IsConsecutive() const { |
| 223 return !!(m_dwFlags & FX_MEMSTREAM_Consecutive); |
| 224 } |
| 225 |
| 226 void CFX_MemoryStream::EstimateSize(size_t nInitSize, size_t nGrowSize) { |
| 227 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { |
| 228 if (m_Blocks.GetSize() < 1) { |
| 229 uint8_t* pBlock = |
| 230 FX_Alloc(uint8_t, std::max(nInitSize, static_cast<size_t>(4096))); |
| 231 m_Blocks.Add(pBlock); |
| 232 } |
| 233 m_nGrowSize = std::max(nGrowSize, static_cast<size_t>(4096)); |
| 234 } else if (m_Blocks.GetSize() < 1) { |
| 235 m_nGrowSize = std::max(nGrowSize, static_cast<size_t>(4096)); |
| 236 } |
| 237 } |
| 238 |
| 239 uint8_t* CFX_MemoryStream::GetBuffer() const { |
| 240 return m_Blocks.GetSize() ? m_Blocks[0] : nullptr; |
| 241 } |
| 242 |
| 243 void CFX_MemoryStream::AttachBuffer(uint8_t* pBuffer, |
| 244 size_t nSize, |
| 245 FX_BOOL bTakeOver) { |
| 246 if (!(m_dwFlags & FX_MEMSTREAM_Consecutive)) { |
| 247 return; |
| 248 } |
| 249 m_Blocks.RemoveAll(); |
| 250 m_Blocks.Add(pBuffer); |
| 251 m_nTotalSize = m_nCurSize = nSize; |
| 252 m_nCurPos = 0; |
| 253 m_dwFlags = |
| 254 FX_MEMSTREAM_Consecutive | (bTakeOver ? FX_MEMSTREAM_TakeOver : 0); |
| 255 } |
| 256 |
| 257 void CFX_MemoryStream::DetachBuffer() { |
| 258 if (!(m_dwFlags & FX_MEMSTREAM_Consecutive)) { |
| 259 return; |
| 260 } |
| 261 m_Blocks.RemoveAll(); |
| 262 m_nTotalSize = m_nCurSize = m_nCurPos = 0; |
| 263 m_dwFlags = FX_MEMSTREAM_TakeOver; |
| 264 } |
| 265 |
| 266 FX_BOOL CFX_MemoryStream::ExpandBlocks(size_t size) { |
| 267 if (m_nCurSize < size) { |
| 268 m_nCurSize = size; |
| 269 } |
| 270 if (size <= m_nTotalSize) { |
| 271 return TRUE; |
| 272 } |
| 273 int32_t iCount = m_Blocks.GetSize(); |
| 274 size = (size - m_nTotalSize + m_nGrowSize - 1) / m_nGrowSize; |
| 275 m_Blocks.SetSize(m_Blocks.GetSize() + (int32_t)size); |
| 276 while (size--) { |
| 277 uint8_t* pBlock = FX_Alloc(uint8_t, m_nGrowSize); |
| 278 m_Blocks.SetAt(iCount++, pBlock); |
| 279 m_nTotalSize += m_nGrowSize; |
| 280 } |
| 281 return TRUE; |
| 282 } |
| 283 |
24 IFX_FileStream* CFX_CRTFileStream::Retain() { | 284 IFX_FileStream* CFX_CRTFileStream::Retain() { |
25 m_dwCount++; | 285 m_dwCount++; |
26 return this; | 286 return this; |
27 } | 287 } |
28 | 288 |
29 void CFX_CRTFileStream::Release() { | 289 void CFX_CRTFileStream::Release() { |
30 uint32_t nCount = --m_dwCount; | 290 uint32_t nCount = --m_dwCount; |
31 if (!nCount) { | 291 if (!nCount) { |
32 delete this; | 292 delete this; |
33 } | 293 } |
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 b = ((const uint8_t*)pGUID)[i]; | 609 b = ((const uint8_t*)pGUID)[i]; |
350 *pBuf++ = gs_FX_pHexChars[b >> 4]; | 610 *pBuf++ = gs_FX_pHexChars[b >> 4]; |
351 *pBuf++ = gs_FX_pHexChars[b & 0x0F]; | 611 *pBuf++ = gs_FX_pHexChars[b & 0x0F]; |
352 if (bSeparator && (i == 3 || i == 5 || i == 7 || i == 9)) { | 612 if (bSeparator && (i == 3 || i == 5 || i == 7 || i == 9)) { |
353 *pBuf++ = L'-'; | 613 *pBuf++ = L'-'; |
354 } | 614 } |
355 } | 615 } |
356 bsStr.ReleaseBuffer(bSeparator ? 36 : 32); | 616 bsStr.ReleaseBuffer(bSeparator ? 36 : 32); |
357 } | 617 } |
358 #endif // PDF_ENABLE_XFA | 618 #endif // PDF_ENABLE_XFA |
OLD | NEW |