From ac121aa23a5ee3290af151dddbbdc0f4644098d4 Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Fri, 19 Feb 2016 15:25:02 -0800 Subject: Remove IFX_BufferArchive. Merge it into CFX_FileBufferArchive, since both are in the same header file and it is the only subclass. Also, bTakeOver is always false, and the buf size is always 32K. R=thestig@chromium.org Review URL: https://codereview.chromium.org/1712353002 . --- core/include/fxcrt/fx_basic.h | 38 +++-------- core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp | 7 +- core/src/fxcrt/fx_basic_buffer.cpp | 87 ++++++++++--------------- 3 files changed, 45 insertions(+), 87 deletions(-) diff --git a/core/include/fxcrt/fx_basic.h b/core/include/fxcrt/fx_basic.h index 3e30b1d1a1..018a91462e 100644 --- a/core/include/fxcrt/fx_basic.h +++ b/core/include/fxcrt/fx_basic.h @@ -179,46 +179,26 @@ class CFX_ArchiveLoader { }; #endif // PDF_ENABLE_XFA -class IFX_BufferArchive { +class CFX_FileBufferArchive { public: - IFX_BufferArchive(FX_STRSIZE size); - virtual ~IFX_BufferArchive() {} - - virtual void Clear(); - - FX_BOOL Flush(); + CFX_FileBufferArchive(); + void Clear(); + bool Flush(); int32_t AppendBlock(const void* pBuf, size_t size); - int32_t AppendByte(uint8_t byte); - int32_t AppendDWord(FX_DWORD i); - int32_t AppendString(const CFX_ByteStringC& lpsz); - protected: - virtual FX_BOOL DoWork(const void* pBuf, size_t size) = 0; - - FX_STRSIZE m_BufSize; - - uint8_t* m_pBuffer; - - FX_STRSIZE m_Length; -}; - -class CFX_FileBufferArchive : public IFX_BufferArchive { - public: - CFX_FileBufferArchive(FX_STRSIZE size = 32768); - ~CFX_FileBufferArchive() override; - - void Clear() override; - FX_BOOL AttachFile(IFX_StreamWrite* pFile, FX_BOOL bTakeover = FALSE); + // |pFile| must outlive the CFX_FileBufferArchive. + void AttachFile(IFX_StreamWrite* pFile); private: - FX_BOOL DoWork(const void* pBuf, size_t size) override; + static const size_t kBufSize = 32768; + size_t m_Length; + std::unique_ptr m_pBuffer; IFX_StreamWrite* m_pFile; - FX_BOOL m_bTakeover; }; class CFX_CharMap { diff --git a/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp b/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp index 7d294c4c52..f103786350 100644 --- a/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp +++ b/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp @@ -1945,12 +1945,7 @@ void CPDF_Creator::Clear() { } } FX_BOOL CPDF_Creator::Create(IFX_StreamWrite* pFile, FX_DWORD flags) { - if (!pFile) { - return FALSE; - } - if (!m_File.AttachFile(pFile, FALSE)) { - return FALSE; - } + m_File.AttachFile(pFile); return Create(flags); } FX_BOOL CPDF_Creator::Create(FX_DWORD flags) { diff --git a/core/src/fxcrt/fx_basic_buffer.cpp b/core/src/fxcrt/fx_basic_buffer.cpp index 835d43f785..9578653019 100644 --- a/core/src/fxcrt/fx_basic_buffer.cpp +++ b/core/src/fxcrt/fx_basic_buffer.cpp @@ -9,6 +9,7 @@ #include "core/include/fxcrt/fx_basic.h" #include "core/include/fxcrt/fx_safe_types.h" +#include "third_party/base/numerics/safe_conversions.h" CFX_BinaryBuf::CFX_BinaryBuf() : m_AllocStep(0), m_AllocSize(0), m_DataSize(0) {} @@ -359,32 +360,40 @@ FX_DWORD CFX_BitStream::GetBits(FX_DWORD nBits) { m_BitPos += nBits; return result; } -IFX_BufferArchive::IFX_BufferArchive(FX_STRSIZE size) - : m_BufSize(size), m_pBuffer(NULL), m_Length(0) {} -void IFX_BufferArchive::Clear() { + +CFX_FileBufferArchive::CFX_FileBufferArchive() + : m_Length(0), m_pFile(nullptr) {} + +void CFX_FileBufferArchive::Clear() { m_Length = 0; - FX_Free(m_pBuffer); - m_pBuffer = NULL; + m_pBuffer.reset(); + m_pFile = nullptr; } -FX_BOOL IFX_BufferArchive::Flush() { - FX_BOOL bRet = DoWork(m_pBuffer, m_Length); + +bool CFX_FileBufferArchive::Flush() { + size_t nRemaining = m_Length; m_Length = 0; - return bRet; + if (!m_pFile) + return false; + if (!m_pBuffer || !nRemaining) + return true; + return m_pFile->WriteBlock(m_pBuffer.get(), nRemaining); } -int32_t IFX_BufferArchive::AppendBlock(const void* pBuf, size_t size) { + +int32_t CFX_FileBufferArchive::AppendBlock(const void* pBuf, size_t size) { if (!pBuf || size < 1) { return 0; } if (!m_pBuffer) { - m_pBuffer = FX_Alloc(uint8_t, m_BufSize); + m_pBuffer.reset(FX_Alloc(uint8_t, kBufSize)); } - uint8_t* buffer = (uint8_t*)pBuf; - FX_STRSIZE temp_size = (FX_STRSIZE)size; - while (temp_size > 0) { - FX_STRSIZE buf_size = std::min(m_BufSize - m_Length, (FX_STRSIZE)temp_size); - FXSYS_memcpy(m_pBuffer + m_Length, buffer, buf_size); + const uint8_t* buffer = reinterpret_cast(pBuf); + size_t temp_size = size; + while (temp_size) { + size_t buf_size = std::min(kBufSize - m_Length, temp_size); + FXSYS_memcpy(m_pBuffer.get() + m_Length, buffer, buf_size); m_Length += buf_size; - if (m_Length == m_BufSize) { + if (m_Length == kBufSize) { if (!Flush()) { return -1; } @@ -392,50 +401,24 @@ int32_t IFX_BufferArchive::AppendBlock(const void* pBuf, size_t size) { temp_size -= buf_size; buffer += buf_size; } - return (int32_t)size; + return pdfium::base::checked_cast(size); } -int32_t IFX_BufferArchive::AppendByte(uint8_t byte) { + +int32_t CFX_FileBufferArchive::AppendByte(uint8_t byte) { return AppendBlock(&byte, 1); } -int32_t IFX_BufferArchive::AppendDWord(FX_DWORD i) { + +int32_t CFX_FileBufferArchive::AppendDWord(FX_DWORD i) { char buf[32]; FXSYS_itoa(i, buf, 10); return AppendBlock(buf, (size_t)FXSYS_strlen(buf)); } -int32_t IFX_BufferArchive::AppendString(const CFX_ByteStringC& lpsz) { + +int32_t CFX_FileBufferArchive::AppendString(const CFX_ByteStringC& lpsz) { return AppendBlock(lpsz.GetPtr(), lpsz.GetLength()); } -CFX_FileBufferArchive::CFX_FileBufferArchive(FX_STRSIZE size) - : IFX_BufferArchive(size), m_pFile(NULL), m_bTakeover(FALSE) {} -CFX_FileBufferArchive::~CFX_FileBufferArchive() { - Clear(); -} -void CFX_FileBufferArchive::Clear() { - if (m_pFile && m_bTakeover) { - m_pFile->Release(); - } - m_pFile = NULL; - m_bTakeover = FALSE; - IFX_BufferArchive::Clear(); -} -FX_BOOL CFX_FileBufferArchive::AttachFile(IFX_StreamWrite* pFile, - FX_BOOL bTakeover) { - if (!pFile) { - return FALSE; - } - if (m_pFile && m_bTakeover) { - m_pFile->Release(); - } + +void CFX_FileBufferArchive::AttachFile(IFX_StreamWrite* pFile) { + FXSYS_assert(pFile); m_pFile = pFile; - m_bTakeover = bTakeover; - return TRUE; -} -FX_BOOL CFX_FileBufferArchive::DoWork(const void* pBuf, size_t size) { - if (!m_pFile) { - return FALSE; - } - if (!pBuf || size < 1) { - return TRUE; - } - return m_pFile->WriteBlock(pBuf, size); } -- cgit v1.2.3