| 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" |
| 8 |
| 7 #include <utility> | 9 #include <utility> |
| 8 | 10 |
| 9 #include "core/fxcrt/extension.h" | |
| 10 #include "core/fxcrt/fx_basic.h" | 11 #include "core/fxcrt/fx_basic.h" |
| 11 #include "core/fxcrt/fx_ext.h" | 12 #include "core/fxcrt/fx_ext.h" |
| 12 | 13 |
| 13 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 14 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
| 14 #include <wincrypt.h> | 15 #include <wincrypt.h> |
| 15 #else | 16 #else |
| 16 #include <ctime> | 17 #include <ctime> |
| 17 #endif | 18 #endif |
| 18 | 19 |
| 19 #ifdef PDF_ENABLE_XFA | 20 #ifdef PDF_ENABLE_XFA |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 if (nCount) { | 96 if (nCount) { |
| 96 return; | 97 return; |
| 97 } | 98 } |
| 98 delete this; | 99 delete this; |
| 99 } | 100 } |
| 100 | 101 |
| 101 FX_FILESIZE CFX_MemoryStream::GetSize() { | 102 FX_FILESIZE CFX_MemoryStream::GetSize() { |
| 102 return (FX_FILESIZE)m_nCurSize; | 103 return (FX_FILESIZE)m_nCurSize; |
| 103 } | 104 } |
| 104 | 105 |
| 105 FX_BOOL CFX_MemoryStream::IsEOF() { | 106 bool CFX_MemoryStream::IsEOF() { |
| 106 return m_nCurPos >= (size_t)GetSize(); | 107 return m_nCurPos >= (size_t)GetSize(); |
| 107 } | 108 } |
| 108 | 109 |
| 109 FX_FILESIZE CFX_MemoryStream::GetPosition() { | 110 FX_FILESIZE CFX_MemoryStream::GetPosition() { |
| 110 return (FX_FILESIZE)m_nCurPos; | 111 return (FX_FILESIZE)m_nCurPos; |
| 111 } | 112 } |
| 112 | 113 |
| 113 FX_BOOL CFX_MemoryStream::ReadBlock(void* buffer, | 114 bool CFX_MemoryStream::ReadBlock(void* buffer, |
| 114 FX_FILESIZE offset, | 115 FX_FILESIZE offset, |
| 115 size_t size) { | 116 size_t size) { |
| 116 if (!buffer || !size) { | 117 if (!buffer || !size) |
| 117 return FALSE; | 118 return false; |
| 118 } | |
| 119 | 119 |
| 120 FX_SAFE_SIZE_T newPos = size; | 120 FX_SAFE_SIZE_T newPos = size; |
| 121 newPos += offset; | 121 newPos += offset; |
| 122 if (!newPos.IsValid() || newPos.ValueOrDefault(0) == 0 || | 122 if (!newPos.IsValid() || newPos.ValueOrDefault(0) == 0 || |
| 123 newPos.ValueOrDie() > m_nCurSize) { | 123 newPos.ValueOrDie() > m_nCurSize) { |
| 124 return FALSE; | 124 return false; |
| 125 } | 125 } |
| 126 | 126 |
| 127 m_nCurPos = newPos.ValueOrDie(); | 127 m_nCurPos = newPos.ValueOrDie(); |
| 128 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { | 128 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { |
| 129 FXSYS_memcpy(buffer, m_Blocks[0] + (size_t)offset, size); | 129 FXSYS_memcpy(buffer, m_Blocks[0] + (size_t)offset, size); |
| 130 return TRUE; | 130 return true; |
| 131 } | 131 } |
| 132 size_t nStartBlock = (size_t)offset / m_nGrowSize; | 132 size_t nStartBlock = (size_t)offset / m_nGrowSize; |
| 133 offset -= (FX_FILESIZE)(nStartBlock * m_nGrowSize); | 133 offset -= (FX_FILESIZE)(nStartBlock * m_nGrowSize); |
| 134 while (size) { | 134 while (size) { |
| 135 size_t nRead = m_nGrowSize - (size_t)offset; | 135 size_t nRead = m_nGrowSize - (size_t)offset; |
| 136 if (nRead > size) { | 136 if (nRead > size) { |
| 137 nRead = size; | 137 nRead = size; |
| 138 } | 138 } |
| 139 FXSYS_memcpy(buffer, m_Blocks[(int)nStartBlock] + (size_t)offset, nRead); | 139 FXSYS_memcpy(buffer, m_Blocks[(int)nStartBlock] + (size_t)offset, nRead); |
| 140 buffer = ((uint8_t*)buffer) + nRead; | 140 buffer = ((uint8_t*)buffer) + nRead; |
| 141 size -= nRead; | 141 size -= nRead; |
| 142 nStartBlock++; | 142 nStartBlock++; |
| 143 offset = 0; | 143 offset = 0; |
| 144 } | 144 } |
| 145 return TRUE; | 145 return true; |
| 146 } | 146 } |
| 147 | 147 |
| 148 size_t CFX_MemoryStream::ReadBlock(void* buffer, size_t size) { | 148 size_t CFX_MemoryStream::ReadBlock(void* buffer, size_t size) { |
| 149 if (m_nCurPos >= m_nCurSize) { | 149 if (m_nCurPos >= m_nCurSize) { |
| 150 return 0; | 150 return 0; |
| 151 } | 151 } |
| 152 size_t nRead = std::min(size, m_nCurSize - m_nCurPos); | 152 size_t nRead = std::min(size, m_nCurSize - m_nCurPos); |
| 153 if (!ReadBlock(buffer, (int32_t)m_nCurPos, nRead)) { | 153 if (!ReadBlock(buffer, (int32_t)m_nCurPos, nRead)) { |
| 154 return 0; | 154 return 0; |
| 155 } | 155 } |
| 156 return nRead; | 156 return nRead; |
| 157 } | 157 } |
| 158 | 158 |
| 159 FX_BOOL CFX_MemoryStream::WriteBlock(const void* buffer, | 159 bool CFX_MemoryStream::WriteBlock(const void* buffer, |
| 160 FX_FILESIZE offset, | 160 FX_FILESIZE offset, |
| 161 size_t size) { | 161 size_t size) { |
| 162 if (!buffer || !size) { | 162 if (!buffer || !size) |
| 163 return FALSE; | 163 return false; |
| 164 } | 164 |
| 165 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { | 165 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { |
| 166 FX_SAFE_SIZE_T newPos = size; | 166 FX_SAFE_SIZE_T newPos = size; |
| 167 newPos += offset; | 167 newPos += offset; |
| 168 if (!newPos.IsValid()) | 168 if (!newPos.IsValid()) |
| 169 return FALSE; | 169 return false; |
| 170 | 170 |
| 171 m_nCurPos = newPos.ValueOrDie(); | 171 m_nCurPos = newPos.ValueOrDie(); |
| 172 if (m_nCurPos > m_nTotalSize) { | 172 if (m_nCurPos > m_nTotalSize) { |
| 173 m_nTotalSize = (m_nCurPos + m_nGrowSize - 1) / m_nGrowSize * m_nGrowSize; | 173 m_nTotalSize = (m_nCurPos + m_nGrowSize - 1) / m_nGrowSize * m_nGrowSize; |
| 174 if (m_Blocks.GetSize() < 1) { | 174 if (m_Blocks.GetSize() < 1) { |
| 175 uint8_t* block = FX_Alloc(uint8_t, m_nTotalSize); | 175 uint8_t* block = FX_Alloc(uint8_t, m_nTotalSize); |
| 176 m_Blocks.Add(block); | 176 m_Blocks.Add(block); |
| 177 } else { | 177 } else { |
| 178 m_Blocks[0] = FX_Realloc(uint8_t, m_Blocks[0], m_nTotalSize); | 178 m_Blocks[0] = FX_Realloc(uint8_t, m_Blocks[0], m_nTotalSize); |
| 179 } | 179 } |
| 180 if (!m_Blocks[0]) { | 180 if (!m_Blocks[0]) { |
| 181 m_Blocks.RemoveAll(); | 181 m_Blocks.RemoveAll(); |
| 182 return FALSE; | 182 return false; |
| 183 } | 183 } |
| 184 } | 184 } |
| 185 FXSYS_memcpy(m_Blocks[0] + (size_t)offset, buffer, size); | 185 FXSYS_memcpy(m_Blocks[0] + (size_t)offset, buffer, size); |
| 186 if (m_nCurSize < m_nCurPos) { | 186 if (m_nCurSize < m_nCurPos) { |
| 187 m_nCurSize = m_nCurPos; | 187 m_nCurSize = m_nCurPos; |
| 188 } | 188 } |
| 189 return TRUE; | 189 return true; |
| 190 } | 190 } |
| 191 | 191 |
| 192 FX_SAFE_SIZE_T newPos = size; | 192 FX_SAFE_SIZE_T newPos = size; |
| 193 newPos += offset; | 193 newPos += offset; |
| 194 if (!newPos.IsValid()) { | 194 if (!newPos.IsValid()) { |
| 195 return FALSE; | 195 return FALSE; |
| 196 } | 196 } |
| 197 | 197 |
| 198 if (!ExpandBlocks(newPos.ValueOrDie())) { | 198 if (!ExpandBlocks(newPos.ValueOrDie())) { |
| 199 return FALSE; | 199 return FALSE; |
| 200 } | 200 } |
| 201 m_nCurPos = newPos.ValueOrDie(); | 201 m_nCurPos = newPos.ValueOrDie(); |
| 202 size_t nStartBlock = (size_t)offset / m_nGrowSize; | 202 size_t nStartBlock = (size_t)offset / m_nGrowSize; |
| 203 offset -= (FX_FILESIZE)(nStartBlock * m_nGrowSize); | 203 offset -= (FX_FILESIZE)(nStartBlock * m_nGrowSize); |
| 204 while (size) { | 204 while (size) { |
| 205 size_t nWrite = m_nGrowSize - (size_t)offset; | 205 size_t nWrite = m_nGrowSize - (size_t)offset; |
| 206 if (nWrite > size) { | 206 if (nWrite > size) { |
| 207 nWrite = size; | 207 nWrite = size; |
| 208 } | 208 } |
| 209 FXSYS_memcpy(m_Blocks[(int)nStartBlock] + (size_t)offset, buffer, nWrite); | 209 FXSYS_memcpy(m_Blocks[(int)nStartBlock] + (size_t)offset, buffer, nWrite); |
| 210 buffer = ((uint8_t*)buffer) + nWrite; | 210 buffer = ((uint8_t*)buffer) + nWrite; |
| 211 size -= nWrite; | 211 size -= nWrite; |
| 212 nStartBlock++; | 212 nStartBlock++; |
| 213 offset = 0; | 213 offset = 0; |
| 214 } | 214 } |
| 215 return TRUE; | 215 return TRUE; |
| 216 } | 216 } |
| 217 | 217 |
| 218 FX_BOOL CFX_MemoryStream::Flush() { | 218 bool CFX_MemoryStream::Flush() { |
| 219 return TRUE; | 219 return true; |
| 220 } | 220 } |
| 221 | 221 |
| 222 FX_BOOL CFX_MemoryStream::IsConsecutive() const { | 222 bool CFX_MemoryStream::IsConsecutive() const { |
| 223 return !!(m_dwFlags & FX_MEMSTREAM_Consecutive); | 223 return !!(m_dwFlags & FX_MEMSTREAM_Consecutive); |
| 224 } | 224 } |
| 225 | 225 |
| 226 void CFX_MemoryStream::EstimateSize(size_t nInitSize, size_t nGrowSize) { | 226 void CFX_MemoryStream::EstimateSize(size_t nInitSize, size_t nGrowSize) { |
| 227 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { | 227 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { |
| 228 if (m_Blocks.GetSize() < 1) { | 228 if (m_Blocks.GetSize() < 1) { |
| 229 uint8_t* pBlock = | 229 uint8_t* pBlock = |
| 230 FX_Alloc(uint8_t, std::max(nInitSize, static_cast<size_t>(4096))); | 230 FX_Alloc(uint8_t, std::max(nInitSize, static_cast<size_t>(4096))); |
| 231 m_Blocks.Add(pBlock); | 231 m_Blocks.Add(pBlock); |
| 232 } | 232 } |
| 233 m_nGrowSize = std::max(nGrowSize, static_cast<size_t>(4096)); | 233 m_nGrowSize = std::max(nGrowSize, static_cast<size_t>(4096)); |
| 234 } else if (m_Blocks.GetSize() < 1) { | 234 } else if (m_Blocks.GetSize() < 1) { |
| 235 m_nGrowSize = std::max(nGrowSize, static_cast<size_t>(4096)); | 235 m_nGrowSize = std::max(nGrowSize, static_cast<size_t>(4096)); |
| 236 } | 236 } |
| 237 } | 237 } |
| 238 | 238 |
| 239 uint8_t* CFX_MemoryStream::GetBuffer() const { | 239 uint8_t* CFX_MemoryStream::GetBuffer() const { |
| 240 return m_Blocks.GetSize() ? m_Blocks[0] : nullptr; | 240 return m_Blocks.GetSize() ? m_Blocks[0] : nullptr; |
| 241 } | 241 } |
| 242 | 242 |
| 243 void CFX_MemoryStream::AttachBuffer(uint8_t* pBuffer, | 243 void CFX_MemoryStream::AttachBuffer(uint8_t* pBuffer, |
| 244 size_t nSize, | 244 size_t nSize, |
| 245 FX_BOOL bTakeOver) { | 245 bool bTakeOver) { |
| 246 if (!(m_dwFlags & FX_MEMSTREAM_Consecutive)) { | 246 if (!(m_dwFlags & FX_MEMSTREAM_Consecutive)) |
| 247 return; | 247 return; |
| 248 } | 248 |
| 249 m_Blocks.RemoveAll(); | 249 m_Blocks.RemoveAll(); |
| 250 m_Blocks.Add(pBuffer); | 250 m_Blocks.Add(pBuffer); |
| 251 m_nTotalSize = m_nCurSize = nSize; | 251 m_nTotalSize = m_nCurSize = nSize; |
| 252 m_nCurPos = 0; | 252 m_nCurPos = 0; |
| 253 m_dwFlags = | 253 m_dwFlags = |
| 254 FX_MEMSTREAM_Consecutive | (bTakeOver ? FX_MEMSTREAM_TakeOver : 0); | 254 FX_MEMSTREAM_Consecutive | (bTakeOver ? FX_MEMSTREAM_TakeOver : 0); |
| 255 } | 255 } |
| 256 | 256 |
| 257 void CFX_MemoryStream::DetachBuffer() { | 257 void CFX_MemoryStream::DetachBuffer() { |
| 258 if (!(m_dwFlags & FX_MEMSTREAM_Consecutive)) { | 258 if (!(m_dwFlags & FX_MEMSTREAM_Consecutive)) { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 uint32_t nCount = --m_dwCount; | 290 uint32_t nCount = --m_dwCount; |
| 291 if (!nCount) { | 291 if (!nCount) { |
| 292 delete this; | 292 delete this; |
| 293 } | 293 } |
| 294 } | 294 } |
| 295 | 295 |
| 296 FX_FILESIZE CFX_CRTFileStream::GetSize() { | 296 FX_FILESIZE CFX_CRTFileStream::GetSize() { |
| 297 return m_pFile->GetSize(); | 297 return m_pFile->GetSize(); |
| 298 } | 298 } |
| 299 | 299 |
| 300 FX_BOOL CFX_CRTFileStream::IsEOF() { | 300 bool CFX_CRTFileStream::IsEOF() { |
| 301 return GetPosition() >= GetSize(); | 301 return GetPosition() >= GetSize(); |
| 302 } | 302 } |
| 303 | 303 |
| 304 FX_FILESIZE CFX_CRTFileStream::GetPosition() { | 304 FX_FILESIZE CFX_CRTFileStream::GetPosition() { |
| 305 return m_pFile->GetPosition(); | 305 return m_pFile->GetPosition(); |
| 306 } | 306 } |
| 307 | 307 |
| 308 FX_BOOL CFX_CRTFileStream::ReadBlock(void* buffer, | 308 bool CFX_CRTFileStream::ReadBlock(void* buffer, |
| 309 FX_FILESIZE offset, | 309 FX_FILESIZE offset, |
| 310 size_t size) { | 310 size_t size) { |
| 311 return (FX_BOOL)m_pFile->ReadPos(buffer, size, offset); | 311 return m_pFile->ReadPos(buffer, size, offset) > 0; |
| 312 } | 312 } |
| 313 | 313 |
| 314 size_t CFX_CRTFileStream::ReadBlock(void* buffer, size_t size) { | 314 size_t CFX_CRTFileStream::ReadBlock(void* buffer, size_t size) { |
| 315 return m_pFile->Read(buffer, size); | 315 return m_pFile->Read(buffer, size); |
| 316 } | 316 } |
| 317 | 317 |
| 318 FX_BOOL CFX_CRTFileStream::WriteBlock(const void* buffer, | 318 bool CFX_CRTFileStream::WriteBlock(const void* buffer, |
| 319 FX_FILESIZE offset, | 319 FX_FILESIZE offset, |
| 320 size_t size) { | 320 size_t size) { |
| 321 return (FX_BOOL)m_pFile->WritePos(buffer, size, offset); | 321 return !!m_pFile->WritePos(buffer, size, offset); |
| 322 } | 322 } |
| 323 | 323 |
| 324 FX_BOOL CFX_CRTFileStream::Flush() { | 324 bool CFX_CRTFileStream::Flush() { |
| 325 return m_pFile->Flush(); | 325 return m_pFile->Flush(); |
| 326 } | 326 } |
| 327 | 327 |
| 328 #ifdef PDF_ENABLE_XFA | 328 #ifdef PDF_ENABLE_XFA |
| 329 IFX_FileAccess* FX_CreateDefaultFileAccess(const CFX_WideStringC& wsPath) { | 329 IFX_FileAccess* FX_CreateDefaultFileAccess(const CFX_WideStringC& wsPath) { |
| 330 if (wsPath.GetLength() == 0) | 330 if (wsPath.GetLength() == 0) |
| 331 return nullptr; | 331 return nullptr; |
| 332 | 332 |
| 333 CFX_CRTFileAccess* pFA = new CFX_CRTFileAccess; | 333 CFX_CRTFileAccess* pFA = new CFX_CRTFileAccess; |
| 334 pFA->Init(wsPath); | 334 pFA->Init(wsPath); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 352 return new CFX_CRTFileStream(std::move(pFA)); | 352 return new CFX_CRTFileStream(std::move(pFA)); |
| 353 } | 353 } |
| 354 IFX_SeekableReadStream* FX_CreateFileRead(const FX_CHAR* filename) { | 354 IFX_SeekableReadStream* FX_CreateFileRead(const FX_CHAR* filename) { |
| 355 return FX_CreateFileStream(filename, FX_FILEMODE_ReadOnly); | 355 return FX_CreateFileStream(filename, FX_FILEMODE_ReadOnly); |
| 356 } | 356 } |
| 357 IFX_SeekableReadStream* FX_CreateFileRead(const FX_WCHAR* filename) { | 357 IFX_SeekableReadStream* FX_CreateFileRead(const FX_WCHAR* filename) { |
| 358 return FX_CreateFileStream(filename, FX_FILEMODE_ReadOnly); | 358 return FX_CreateFileStream(filename, FX_FILEMODE_ReadOnly); |
| 359 } | 359 } |
| 360 IFX_MemoryStream* FX_CreateMemoryStream(uint8_t* pBuffer, | 360 IFX_MemoryStream* FX_CreateMemoryStream(uint8_t* pBuffer, |
| 361 size_t dwSize, | 361 size_t dwSize, |
| 362 FX_BOOL bTakeOver) { | 362 bool bTakeOver) { |
| 363 return new CFX_MemoryStream(pBuffer, dwSize, bTakeOver); | 363 return new CFX_MemoryStream(pBuffer, dwSize, bTakeOver); |
| 364 } | 364 } |
| 365 IFX_MemoryStream* FX_CreateMemoryStream(FX_BOOL bConsecutive) { | 365 IFX_MemoryStream* FX_CreateMemoryStream(bool bConsecutive) { |
| 366 return new CFX_MemoryStream(bConsecutive); | 366 return new CFX_MemoryStream(bConsecutive); |
| 367 } | 367 } |
| 368 | 368 |
| 369 FX_FLOAT FXSYS_tan(FX_FLOAT a) { | 369 FX_FLOAT FXSYS_tan(FX_FLOAT a) { |
| 370 return (FX_FLOAT)tan(a); | 370 return (FX_FLOAT)tan(a); |
| 371 } | 371 } |
| 372 FX_FLOAT FXSYS_logb(FX_FLOAT b, FX_FLOAT x) { | 372 FX_FLOAT FXSYS_logb(FX_FLOAT b, FX_FLOAT x) { |
| 373 return FXSYS_log(x) / FXSYS_log(b); | 373 return FXSYS_log(x) / FXSYS_log(b); |
| 374 } | 374 } |
| 375 FX_FLOAT FXSYS_strtof(const FX_CHAR* pcsStr, | 375 FX_FLOAT FXSYS_strtof(const FX_CHAR* pcsStr, |
| (...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 610 b = ((const uint8_t*)pGUID)[i]; | 610 b = ((const uint8_t*)pGUID)[i]; |
| 611 *pBuf++ = gs_FX_pHexChars[b >> 4]; | 611 *pBuf++ = gs_FX_pHexChars[b >> 4]; |
| 612 *pBuf++ = gs_FX_pHexChars[b & 0x0F]; | 612 *pBuf++ = gs_FX_pHexChars[b & 0x0F]; |
| 613 if (bSeparator && (i == 3 || i == 5 || i == 7 || i == 9)) { | 613 if (bSeparator && (i == 3 || i == 5 || i == 7 || i == 9)) { |
| 614 *pBuf++ = L'-'; | 614 *pBuf++ = L'-'; |
| 615 } | 615 } |
| 616 } | 616 } |
| 617 bsStr.ReleaseBuffer(bSeparator ? 36 : 32); | 617 bsStr.ReleaseBuffer(bSeparator ? 36 : 32); |
| 618 } | 618 } |
| 619 #endif // PDF_ENABLE_XFA | 619 #endif // PDF_ENABLE_XFA |
| OLD | NEW |