1 // Copyright 2017 PDFium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 6 7 #ifndef CORE_FXCRT_CFX_BINARYBUF_H_ 8 #define CORE_FXCRT_CFX_BINARYBUF_H_ 9 10 #include <memory> 11 12 #include "core/fxcrt/fx_memory_wrappers.h" 13 #include "core/fxcrt/fx_string.h" 14 #include "core/fxcrt/fx_system.h" 15 #include "third_party/base/span.h" 16 17 class CFX_BinaryBuf { 18 public: 19 CFX_BinaryBuf(); 20 virtual ~CFX_BinaryBuf(); 21 22 pdfium::span<uint8_t> GetSpan(); 23 pdfium::span<const uint8_t> GetSpan() const; GetBuffer()24 uint8_t* GetBuffer() const { return m_pBuffer.get(); } GetSize()25 size_t GetSize() const { return m_DataSize; } 26 virtual size_t GetLength() const; IsEmpty()27 bool IsEmpty() const { return GetLength() == 0; } 28 29 void Clear(); SetAllocStep(size_t step)30 void SetAllocStep(size_t step) { m_AllocStep = step; } 31 void EstimateSize(size_t size); 32 void AppendSpan(pdfium::span<const uint8_t> span); 33 void AppendBlock(const void* pBuf, size_t size); AppendString(const ByteString & str)34 void AppendString(const ByteString& str) { 35 AppendBlock(str.c_str(), str.GetLength()); 36 } 37 AppendByte(uint8_t byte)38 void AppendByte(uint8_t byte) { 39 ExpandBuf(1); 40 m_pBuffer.get()[m_DataSize++] = byte; 41 } 42 43 void Delete(size_t start_index, size_t count); 44 45 // Releases ownership of |m_pBuffer| and returns it. 46 std::unique_ptr<uint8_t, FxFreeDeleter> DetachBuffer(); 47 48 protected: 49 void ExpandBuf(size_t size); 50 51 size_t m_AllocStep = 0; 52 size_t m_AllocSize = 0; 53 size_t m_DataSize = 0; 54 std::unique_ptr<uint8_t, FxFreeDeleter> m_pBuffer; 55 }; 56 57 #endif // CORE_FXCRT_CFX_BINARYBUF_H_ 58