| 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 <algorithm> | 7 #include <algorithm> |
| 8 #include <limits> | 8 #include <limits> |
| 9 | 9 |
| 10 #include "core/include/fxcrt/fx_basic.h" | 10 #include "core/include/fxcrt/fx_basic.h" |
| 11 #include "core/include/fxcrt/fx_safe_types.h" | 11 #include "core/include/fxcrt/fx_safe_types.h" |
| 12 #include "third_party/base/numerics/safe_conversions.h" |
| 12 | 13 |
| 13 CFX_BinaryBuf::CFX_BinaryBuf() | 14 CFX_BinaryBuf::CFX_BinaryBuf() |
| 14 : m_AllocStep(0), m_AllocSize(0), m_DataSize(0) {} | 15 : m_AllocStep(0), m_AllocSize(0), m_DataSize(0) {} |
| 15 | 16 |
| 16 CFX_BinaryBuf::CFX_BinaryBuf(FX_STRSIZE size) | 17 CFX_BinaryBuf::CFX_BinaryBuf(FX_STRSIZE size) |
| 17 : m_AllocStep(0), m_AllocSize(size), m_DataSize(size) { | 18 : m_AllocStep(0), m_AllocSize(size), m_DataSize(size) { |
| 18 m_pBuffer.reset(FX_Alloc(uint8_t, size)); | 19 m_pBuffer.reset(FX_Alloc(uint8_t, size)); |
| 19 } | 20 } |
| 20 | 21 |
| 21 void CFX_BinaryBuf::Delete(int start_index, int count) { | 22 void CFX_BinaryBuf::Delete(int start_index, int count) { |
| (...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 352 while (bit_left >= 8) { | 353 while (bit_left >= 8) { |
| 353 bit_left -= 8; | 354 bit_left -= 8; |
| 354 result |= m_pData[byte_pos++] << bit_left; | 355 result |= m_pData[byte_pos++] << bit_left; |
| 355 } | 356 } |
| 356 if (bit_left) { | 357 if (bit_left) { |
| 357 result |= m_pData[byte_pos] >> (8 - bit_left); | 358 result |= m_pData[byte_pos] >> (8 - bit_left); |
| 358 } | 359 } |
| 359 m_BitPos += nBits; | 360 m_BitPos += nBits; |
| 360 return result; | 361 return result; |
| 361 } | 362 } |
| 362 IFX_BufferArchive::IFX_BufferArchive(FX_STRSIZE size) | 363 |
| 363 : m_BufSize(size), m_pBuffer(NULL), m_Length(0) {} | 364 CFX_FileBufferArchive::CFX_FileBufferArchive() |
| 364 void IFX_BufferArchive::Clear() { | 365 : m_Length(0), m_pFile(nullptr) {} |
| 366 |
| 367 void CFX_FileBufferArchive::Clear() { |
| 365 m_Length = 0; | 368 m_Length = 0; |
| 366 FX_Free(m_pBuffer); | 369 m_pBuffer.reset(); |
| 367 m_pBuffer = NULL; | 370 m_pFile = nullptr; |
| 368 } | 371 } |
| 369 FX_BOOL IFX_BufferArchive::Flush() { | 372 |
| 370 FX_BOOL bRet = DoWork(m_pBuffer, m_Length); | 373 bool CFX_FileBufferArchive::Flush() { |
| 374 size_t nRemaining = m_Length; |
| 371 m_Length = 0; | 375 m_Length = 0; |
| 372 return bRet; | 376 if (!m_pFile) |
| 377 return false; |
| 378 if (!m_pBuffer || !nRemaining) |
| 379 return true; |
| 380 return m_pFile->WriteBlock(m_pBuffer.get(), nRemaining); |
| 373 } | 381 } |
| 374 int32_t IFX_BufferArchive::AppendBlock(const void* pBuf, size_t size) { | 382 |
| 383 int32_t CFX_FileBufferArchive::AppendBlock(const void* pBuf, size_t size) { |
| 375 if (!pBuf || size < 1) { | 384 if (!pBuf || size < 1) { |
| 376 return 0; | 385 return 0; |
| 377 } | 386 } |
| 378 if (!m_pBuffer) { | 387 if (!m_pBuffer) { |
| 379 m_pBuffer = FX_Alloc(uint8_t, m_BufSize); | 388 m_pBuffer.reset(FX_Alloc(uint8_t, kBufSize)); |
| 380 } | 389 } |
| 381 uint8_t* buffer = (uint8_t*)pBuf; | 390 const uint8_t* buffer = reinterpret_cast<const uint8_t*>(pBuf); |
| 382 FX_STRSIZE temp_size = (FX_STRSIZE)size; | 391 size_t temp_size = size; |
| 383 while (temp_size > 0) { | 392 while (temp_size) { |
| 384 FX_STRSIZE buf_size = std::min(m_BufSize - m_Length, (FX_STRSIZE)temp_size); | 393 size_t buf_size = std::min(kBufSize - m_Length, temp_size); |
| 385 FXSYS_memcpy(m_pBuffer + m_Length, buffer, buf_size); | 394 FXSYS_memcpy(m_pBuffer.get() + m_Length, buffer, buf_size); |
| 386 m_Length += buf_size; | 395 m_Length += buf_size; |
| 387 if (m_Length == m_BufSize) { | 396 if (m_Length == kBufSize) { |
| 388 if (!Flush()) { | 397 if (!Flush()) { |
| 389 return -1; | 398 return -1; |
| 390 } | 399 } |
| 391 } | 400 } |
| 392 temp_size -= buf_size; | 401 temp_size -= buf_size; |
| 393 buffer += buf_size; | 402 buffer += buf_size; |
| 394 } | 403 } |
| 395 return (int32_t)size; | 404 return pdfium::base::checked_cast<int32_t>(size); |
| 396 } | 405 } |
| 397 int32_t IFX_BufferArchive::AppendByte(uint8_t byte) { | 406 |
| 407 int32_t CFX_FileBufferArchive::AppendByte(uint8_t byte) { |
| 398 return AppendBlock(&byte, 1); | 408 return AppendBlock(&byte, 1); |
| 399 } | 409 } |
| 400 int32_t IFX_BufferArchive::AppendDWord(FX_DWORD i) { | 410 |
| 411 int32_t CFX_FileBufferArchive::AppendDWord(FX_DWORD i) { |
| 401 char buf[32]; | 412 char buf[32]; |
| 402 FXSYS_itoa(i, buf, 10); | 413 FXSYS_itoa(i, buf, 10); |
| 403 return AppendBlock(buf, (size_t)FXSYS_strlen(buf)); | 414 return AppendBlock(buf, (size_t)FXSYS_strlen(buf)); |
| 404 } | 415 } |
| 405 int32_t IFX_BufferArchive::AppendString(const CFX_ByteStringC& lpsz) { | 416 |
| 417 int32_t CFX_FileBufferArchive::AppendString(const CFX_ByteStringC& lpsz) { |
| 406 return AppendBlock(lpsz.GetPtr(), lpsz.GetLength()); | 418 return AppendBlock(lpsz.GetPtr(), lpsz.GetLength()); |
| 407 } | 419 } |
| 408 CFX_FileBufferArchive::CFX_FileBufferArchive(FX_STRSIZE size) | 420 |
| 409 : IFX_BufferArchive(size), m_pFile(NULL), m_bTakeover(FALSE) {} | 421 void CFX_FileBufferArchive::AttachFile(IFX_StreamWrite* pFile) { |
| 410 CFX_FileBufferArchive::~CFX_FileBufferArchive() { | 422 FXSYS_assert(pFile); |
| 411 Clear(); | 423 m_pFile = pFile; |
| 412 } | 424 } |
| 413 void CFX_FileBufferArchive::Clear() { | |
| 414 if (m_pFile && m_bTakeover) { | |
| 415 m_pFile->Release(); | |
| 416 } | |
| 417 m_pFile = NULL; | |
| 418 m_bTakeover = FALSE; | |
| 419 IFX_BufferArchive::Clear(); | |
| 420 } | |
| 421 FX_BOOL CFX_FileBufferArchive::AttachFile(IFX_StreamWrite* pFile, | |
| 422 FX_BOOL bTakeover) { | |
| 423 if (!pFile) { | |
| 424 return FALSE; | |
| 425 } | |
| 426 if (m_pFile && m_bTakeover) { | |
| 427 m_pFile->Release(); | |
| 428 } | |
| 429 m_pFile = pFile; | |
| 430 m_bTakeover = bTakeover; | |
| 431 return TRUE; | |
| 432 } | |
| 433 FX_BOOL CFX_FileBufferArchive::DoWork(const void* pBuf, size_t size) { | |
| 434 if (!m_pFile) { | |
| 435 return FALSE; | |
| 436 } | |
| 437 if (!pBuf || size < 1) { | |
| 438 return TRUE; | |
| 439 } | |
| 440 return m_pFile->WriteBlock(pBuf, size); | |
| 441 } | |
| OLD | NEW |