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.h"
13#include "core/fxcrt/fx_string.h"
14#include "core/fxcrt/fx_system.h"
15
16class CFX_BinaryBuf {
17 public:
18  CFX_BinaryBuf();
19  virtual ~CFX_BinaryBuf();
20
21  uint8_t* GetBuffer() const { return m_pBuffer.get(); }
22  size_t GetSize() const { return m_DataSize; }
23  virtual size_t GetLength() const;
24  bool IsEmpty() const { return GetLength() == 0; }
25
26  void Clear();
27  void EstimateSize(size_t size, size_t alloc_step = 0);
28  void AppendBlock(const void* pBuf, size_t size);
29  void AppendString(const ByteString& str) {
30    AppendBlock(str.c_str(), str.GetLength());
31  }
32
33  void AppendByte(uint8_t byte) {
34    ExpandBuf(1);
35    m_pBuffer.get()[m_DataSize++] = byte;
36  }
37
38  void Delete(size_t start_index, size_t count);
39
40  // Releases ownership of |m_pBuffer| and returns it.
41  std::unique_ptr<uint8_t, FxFreeDeleter> DetachBuffer();
42
43 protected:
44  void ExpandBuf(size_t size);
45
46  size_t m_AllocStep;
47  size_t m_AllocSize;
48  size_t m_DataSize;
49  std::unique_ptr<uint8_t, FxFreeDeleter> m_pBuffer;
50};
51
52#endif  // CORE_FXCRT_CFX_BINARYBUF_H_
53