summaryrefslogtreecommitdiff
path: root/core/fxcrt/cfx_binarybuf.h
blob: 1a1c821bb90ec413ab1daea461b4f8a978f32037 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// 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 <memory>

#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(size_t size);
  virtual ~CFX_BinaryBuf();

  uint8_t* GetBuffer() const { return m_pBuffer.get(); }
  size_t GetSize() const { return m_DataSize; }
  virtual size_t GetLength() const;
  bool IsEmpty() const { return GetLength() == 0; }

  void Clear();
  void EstimateSize(size_t size, size_t alloc_step = 0);
  void AppendBlock(const void* pBuf, size_t size);
  void AppendString(const ByteString& str) {
    AppendBlock(str.c_str(), str.GetLength());
  }

  void AppendByte(uint8_t byte) {
    ExpandBuf(1);
    m_pBuffer.get()[m_DataSize++] = byte;
  }

  void InsertBlock(size_t pos, const void* pBuf, size_t size);
  void Delete(size_t start_index, size_t count);

  // Releases ownership of |m_pBuffer| and returns it.
  std::unique_ptr<uint8_t, FxFreeDeleter> DetachBuffer();

 protected:
  void ExpandBuf(size_t size);

  size_t m_AllocStep;
  size_t m_AllocSize;
  size_t m_DataSize;
  std::unique_ptr<uint8_t, FxFreeDeleter> m_pBuffer;
};

#endif  // CORE_FXCRT_CFX_BINARYBUF_H_