From cd07123993c2f6cfd2d08929e6ad27cab3ac74e9 Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Wed, 30 Aug 2017 16:21:36 -0400 Subject: Move CFX_BinaryBuf out of fx_basic This CL splits the CFX_BinaryBuf out of fx_basic into its own files. The various includes have been updated. Change-Id: I0fa616eeb4df6dd229c02dc3a0597b3dced59425 Reviewed-on: https://pdfium-review.googlesource.com/12412 Reviewed-by: Tom Sepez Commit-Queue: dsinclair --- BUILD.gn | 2 + core/fpdfapi/parser/cpdf_crypto_handler.h | 2 +- core/fpdfapi/parser/cpdf_syntax_parser.cpp | 1 + core/fxcodec/codec/fx_codec_fax.cpp | 1 + core/fxcrt/cfx_binarybuf.cpp | 92 ++++++++++++++++++++++++++++++ core/fxcrt/cfx_binarybuf.h | 52 +++++++++++++++++ core/fxcrt/fx_basic.h | 37 +----------- core/fxcrt/fx_basic_buffer.cpp | 82 -------------------------- fpdfsdk/javascript/JS_GlobalData.h | 2 +- 9 files changed, 151 insertions(+), 120 deletions(-) create mode 100644 core/fxcrt/cfx_binarybuf.cpp create mode 100644 core/fxcrt/cfx_binarybuf.h diff --git a/BUILD.gn b/BUILD.gn index 51480ed043..cbf6fa0513 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -804,6 +804,8 @@ config("fxge_warnings") { static_library("fxcrt") { sources = [ "core/fxcrt/cfx_autorestorer.h", + "core/fxcrt/cfx_binarybuf.cpp", + "core/fxcrt/cfx_binarybuf.h", "core/fxcrt/cfx_bitstream.cpp", "core/fxcrt/cfx_bitstream.h", "core/fxcrt/cfx_bytestring.cpp", diff --git a/core/fpdfapi/parser/cpdf_crypto_handler.h b/core/fpdfapi/parser/cpdf_crypto_handler.h index 9d76e67d96..14a5743d15 100644 --- a/core/fpdfapi/parser/cpdf_crypto_handler.h +++ b/core/fpdfapi/parser/cpdf_crypto_handler.h @@ -10,8 +10,8 @@ #include #include "core/fdrm/crypto/fx_crypt.h" +#include "core/fxcrt/cfx_binarybuf.h" #include "core/fxcrt/cfx_retain_ptr.h" -#include "core/fxcrt/fx_basic.h" #include "core/fxcrt/fx_memory.h" #include "core/fxcrt/fx_string.h" #include "core/fxcrt/fx_system.h" diff --git a/core/fpdfapi/parser/cpdf_syntax_parser.cpp b/core/fpdfapi/parser/cpdf_syntax_parser.cpp index c0d21217a5..e3a23cf462 100644 --- a/core/fpdfapi/parser/cpdf_syntax_parser.cpp +++ b/core/fpdfapi/parser/cpdf_syntax_parser.cpp @@ -26,6 +26,7 @@ #include "core/fpdfapi/parser/fpdf_parser_decode.h" #include "core/fpdfapi/parser/fpdf_parser_utility.h" #include "core/fxcrt/cfx_autorestorer.h" +#include "core/fxcrt/cfx_binarybuf.h" #include "core/fxcrt/fx_extension.h" #include "third_party/base/numerics/safe_math.h" #include "third_party/base/ptr_util.h" diff --git a/core/fxcodec/codec/fx_codec_fax.cpp b/core/fxcodec/codec/fx_codec_fax.cpp index 641e0fd9d1..20aada9c12 100644 --- a/core/fxcodec/codec/fx_codec_fax.cpp +++ b/core/fxcodec/codec/fx_codec_fax.cpp @@ -10,6 +10,7 @@ #include "core/fxcodec/codec/codec_int.h" #include "core/fxcodec/fx_codec.h" +#include "core/fxcrt/cfx_binarybuf.h" #include "core/fxcrt/fx_memory.h" #include "third_party/base/ptr_util.h" #include "third_party/base/stl_util.h" diff --git a/core/fxcrt/cfx_binarybuf.cpp b/core/fxcrt/cfx_binarybuf.cpp new file mode 100644 index 0000000000..a1388b8d32 --- /dev/null +++ b/core/fxcrt/cfx_binarybuf.cpp @@ -0,0 +1,92 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "core/fxcrt/cfx_binarybuf.h" + +#include +#include + +CFX_BinaryBuf::CFX_BinaryBuf() + : m_AllocStep(0), m_AllocSize(0), m_DataSize(0) {} + +CFX_BinaryBuf::CFX_BinaryBuf(FX_STRSIZE size) + : m_AllocStep(0), m_AllocSize(size), m_DataSize(size) { + m_pBuffer.reset(FX_Alloc(uint8_t, size)); +} + +CFX_BinaryBuf::~CFX_BinaryBuf() {} + +void CFX_BinaryBuf::Delete(FX_STRSIZE start_index, FX_STRSIZE count) { + if (!m_pBuffer || start_index < 0 || count < 0 || count > m_DataSize || + start_index > m_DataSize - count) { + return; + } + memmove(m_pBuffer.get() + start_index, m_pBuffer.get() + start_index + count, + m_DataSize - start_index - count); + m_DataSize -= count; +} + +void CFX_BinaryBuf::Clear() { + m_DataSize = 0; +} + +std::unique_ptr CFX_BinaryBuf::DetachBuffer() { + m_DataSize = 0; + m_AllocSize = 0; + return std::move(m_pBuffer); +} + +void CFX_BinaryBuf::EstimateSize(FX_STRSIZE size, FX_STRSIZE step) { + m_AllocStep = step; + if (m_AllocSize < size) + ExpandBuf(size - m_DataSize); +} + +void CFX_BinaryBuf::ExpandBuf(FX_STRSIZE add_size) { + FX_SAFE_STRSIZE new_size = m_DataSize; + new_size += add_size; + if (m_AllocSize >= new_size.ValueOrDie()) + return; + + int alloc_step = std::max(128, m_AllocStep ? m_AllocStep : m_AllocSize / 4); + new_size += alloc_step - 1; // Quantize, don't combine these lines. + new_size /= alloc_step; + new_size *= alloc_step; + m_AllocSize = new_size.ValueOrDie(); + m_pBuffer.reset(m_pBuffer + ? FX_Realloc(uint8_t, m_pBuffer.release(), m_AllocSize) + : FX_Alloc(uint8_t, m_AllocSize)); +} + +void CFX_BinaryBuf::AppendBlock(const void* pBuf, FX_STRSIZE size) { + if (size <= 0) + return; + + ExpandBuf(size); + if (pBuf) { + memcpy(m_pBuffer.get() + m_DataSize, pBuf, size); + } else { + memset(m_pBuffer.get() + m_DataSize, 0, size); + } + m_DataSize += size; +} + +void CFX_BinaryBuf::InsertBlock(FX_STRSIZE pos, + const void* pBuf, + FX_STRSIZE size) { + if (size <= 0) + return; + + ExpandBuf(size); + memmove(m_pBuffer.get() + pos + size, m_pBuffer.get() + pos, + m_DataSize - pos); + if (pBuf) { + memcpy(m_pBuffer.get() + pos, pBuf, size); + } else { + memset(m_pBuffer.get() + pos, 0, size); + } + m_DataSize += size; +} diff --git a/core/fxcrt/cfx_binarybuf.h b/core/fxcrt/cfx_binarybuf.h new file mode 100644 index 0000000000..3081d02902 --- /dev/null +++ b/core/fxcrt/cfx_binarybuf.h @@ -0,0 +1,52 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef CORE_FXCRT_CFX_BINARYBUF_H_ +#define CORE_FXCRT_CFX_BINARYBUF_H_ + +#include + +#include "core/fxcrt/fx_memory.h" +#include "core/fxcrt/fx_string.h" +#include "core/fxcrt/fx_system.h" + +class CFX_BinaryBuf { + public: + CFX_BinaryBuf(); + explicit CFX_BinaryBuf(FX_STRSIZE size); + ~CFX_BinaryBuf(); + + uint8_t* GetBuffer() const { return m_pBuffer.get(); } + FX_STRSIZE GetSize() const { return m_DataSize; } + + void Clear(); + void EstimateSize(FX_STRSIZE size, FX_STRSIZE alloc_step = 0); + void AppendBlock(const void* pBuf, FX_STRSIZE size); + void AppendString(const CFX_ByteString& str) { + AppendBlock(str.c_str(), str.GetLength()); + } + + void AppendByte(uint8_t byte) { + ExpandBuf(1); + m_pBuffer.get()[m_DataSize++] = byte; + } + + void InsertBlock(FX_STRSIZE pos, const void* pBuf, FX_STRSIZE size); + void Delete(FX_STRSIZE start_index, FX_STRSIZE count); + + // Releases ownership of |m_pBuffer| and returns it. + std::unique_ptr DetachBuffer(); + + protected: + void ExpandBuf(FX_STRSIZE size); + + FX_STRSIZE m_AllocStep; + FX_STRSIZE m_AllocSize; + FX_STRSIZE m_DataSize; + std::unique_ptr m_pBuffer; +}; + +#endif // CORE_FXCRT_CFX_BINARYBUF_H_ diff --git a/core/fxcrt/fx_basic.h b/core/fxcrt/fx_basic.h index 6f676696a2..6052cc84c8 100644 --- a/core/fxcrt/fx_basic.h +++ b/core/fxcrt/fx_basic.h @@ -11,6 +11,7 @@ #include #include +#include "core/fxcrt/cfx_binarybuf.h" #include "core/fxcrt/cfx_retain_ptr.h" #include "core/fxcrt/fx_memory.h" #include "core/fxcrt/fx_stream.h" @@ -21,42 +22,6 @@ #define FX_IsOdd(a) ((a)&1) #endif // PDF_ENABLE_XFA -class CFX_BinaryBuf { - public: - CFX_BinaryBuf(); - explicit CFX_BinaryBuf(FX_STRSIZE size); - ~CFX_BinaryBuf(); - - uint8_t* GetBuffer() const { return m_pBuffer.get(); } - FX_STRSIZE GetSize() const { return m_DataSize; } - - void Clear(); - void EstimateSize(FX_STRSIZE size, FX_STRSIZE alloc_step = 0); - void AppendBlock(const void* pBuf, FX_STRSIZE size); - void AppendString(const CFX_ByteString& str) { - AppendBlock(str.c_str(), str.GetLength()); - } - - void AppendByte(uint8_t byte) { - ExpandBuf(1); - m_pBuffer.get()[m_DataSize++] = byte; - } - - void InsertBlock(FX_STRSIZE pos, const void* pBuf, FX_STRSIZE size); - void Delete(FX_STRSIZE start_index, FX_STRSIZE count); - - // Releases ownership of |m_pBuffer| and returns it. - std::unique_ptr DetachBuffer(); - - protected: - void ExpandBuf(FX_STRSIZE size); - - FX_STRSIZE m_AllocStep; - FX_STRSIZE m_AllocSize; - FX_STRSIZE m_DataSize; - std::unique_ptr m_pBuffer; -}; - class CFX_WideTextBuf : public CFX_BinaryBuf { public: void AppendChar(wchar_t wch); diff --git a/core/fxcrt/fx_basic_buffer.cpp b/core/fxcrt/fx_basic_buffer.cpp index b93c1bc9da..e282aa4247 100644 --- a/core/fxcrt/fx_basic_buffer.cpp +++ b/core/fxcrt/fx_basic_buffer.cpp @@ -13,88 +13,6 @@ #include "core/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) {} - -CFX_BinaryBuf::CFX_BinaryBuf(FX_STRSIZE size) - : m_AllocStep(0), m_AllocSize(size), m_DataSize(size) { - m_pBuffer.reset(FX_Alloc(uint8_t, size)); -} - -CFX_BinaryBuf::~CFX_BinaryBuf() {} - -void CFX_BinaryBuf::Delete(FX_STRSIZE start_index, FX_STRSIZE count) { - if (!m_pBuffer || start_index < 0 || count < 0 || count > m_DataSize || - start_index > m_DataSize - count) { - return; - } - memmove(m_pBuffer.get() + start_index, m_pBuffer.get() + start_index + count, - m_DataSize - start_index - count); - m_DataSize -= count; -} - -void CFX_BinaryBuf::Clear() { - m_DataSize = 0; -} - -std::unique_ptr CFX_BinaryBuf::DetachBuffer() { - m_DataSize = 0; - m_AllocSize = 0; - return std::move(m_pBuffer); -} - -void CFX_BinaryBuf::EstimateSize(FX_STRSIZE size, FX_STRSIZE step) { - m_AllocStep = step; - if (m_AllocSize < size) - ExpandBuf(size - m_DataSize); -} - -void CFX_BinaryBuf::ExpandBuf(FX_STRSIZE add_size) { - FX_SAFE_STRSIZE new_size = m_DataSize; - new_size += add_size; - if (m_AllocSize >= new_size.ValueOrDie()) - return; - - int alloc_step = std::max(128, m_AllocStep ? m_AllocStep : m_AllocSize / 4); - new_size += alloc_step - 1; // Quantize, don't combine these lines. - new_size /= alloc_step; - new_size *= alloc_step; - m_AllocSize = new_size.ValueOrDie(); - m_pBuffer.reset(m_pBuffer - ? FX_Realloc(uint8_t, m_pBuffer.release(), m_AllocSize) - : FX_Alloc(uint8_t, m_AllocSize)); -} - -void CFX_BinaryBuf::AppendBlock(const void* pBuf, FX_STRSIZE size) { - if (size <= 0) - return; - - ExpandBuf(size); - if (pBuf) { - memcpy(m_pBuffer.get() + m_DataSize, pBuf, size); - } else { - memset(m_pBuffer.get() + m_DataSize, 0, size); - } - m_DataSize += size; -} - -void CFX_BinaryBuf::InsertBlock(FX_STRSIZE pos, - const void* pBuf, - FX_STRSIZE size) { - if (size <= 0) - return; - - ExpandBuf(size); - memmove(m_pBuffer.get() + pos + size, m_pBuffer.get() + pos, - m_DataSize - pos); - if (pBuf) { - memcpy(m_pBuffer.get() + pos, pBuf, size); - } else { - memset(m_pBuffer.get() + pos, 0, size); - } - m_DataSize += size; -} - void CFX_WideTextBuf::AppendChar(wchar_t ch) { ExpandBuf(sizeof(wchar_t)); *(wchar_t*)(m_pBuffer.get() + m_DataSize) = ch; diff --git a/fpdfsdk/javascript/JS_GlobalData.h b/fpdfsdk/javascript/JS_GlobalData.h index c4e8e48b36..24e68c2ba6 100644 --- a/fpdfsdk/javascript/JS_GlobalData.h +++ b/fpdfsdk/javascript/JS_GlobalData.h @@ -10,7 +10,7 @@ #include #include -#include "core/fxcrt/fx_basic.h" +#include "core/fxcrt/cfx_binarybuf.h" #include "fpdfsdk/javascript/JS_KeyValue.h" class CPDFSDK_FormFillEnvironment; -- cgit v1.2.3