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 #include "core/fxcrt/cfx_binarybuf.h"
8
9 #include <algorithm>
10 #include <utility>
11
12 #include "core/fxcrt/fx_safe_types.h"
13
14 CFX_BinaryBuf::CFX_BinaryBuf() = default;
15
16 CFX_BinaryBuf::~CFX_BinaryBuf() = default;
17
Delete(size_t start_index,size_t count)18 void CFX_BinaryBuf::Delete(size_t start_index, size_t count) {
19 if (!m_pBuffer || count > m_DataSize || start_index > m_DataSize - count)
20 return;
21
22 memmove(m_pBuffer.get() + start_index, m_pBuffer.get() + start_index + count,
23 m_DataSize - start_index - count);
24 m_DataSize -= count;
25 }
26
GetSpan()27 pdfium::span<uint8_t> CFX_BinaryBuf::GetSpan() {
28 return {GetBuffer(), GetSize()};
29 }
30
GetSpan() const31 pdfium::span<const uint8_t> CFX_BinaryBuf::GetSpan() const {
32 return {GetBuffer(), GetSize()};
33 }
34
GetLength() const35 size_t CFX_BinaryBuf::GetLength() const {
36 return m_DataSize;
37 }
38
Clear()39 void CFX_BinaryBuf::Clear() {
40 m_DataSize = 0;
41 }
42
DetachBuffer()43 std::unique_ptr<uint8_t, FxFreeDeleter> CFX_BinaryBuf::DetachBuffer() {
44 m_DataSize = 0;
45 m_AllocSize = 0;
46 return std::move(m_pBuffer);
47 }
48
EstimateSize(size_t size)49 void CFX_BinaryBuf::EstimateSize(size_t size) {
50 if (m_AllocSize < size)
51 ExpandBuf(size - m_DataSize);
52 }
53
ExpandBuf(size_t add_size)54 void CFX_BinaryBuf::ExpandBuf(size_t add_size) {
55 FX_SAFE_SIZE_T new_size = m_DataSize;
56 new_size += add_size;
57 if (m_AllocSize >= new_size.ValueOrDie())
58 return;
59
60 size_t alloc_step = std::max(static_cast<size_t>(128),
61 m_AllocStep ? m_AllocStep : m_AllocSize / 4);
62 new_size += alloc_step - 1; // Quantize, don't combine these lines.
63 new_size /= alloc_step;
64 new_size *= alloc_step;
65 m_AllocSize = new_size.ValueOrDie();
66 m_pBuffer.reset(m_pBuffer
67 ? FX_Realloc(uint8_t, m_pBuffer.release(), m_AllocSize)
68 : FX_Alloc(uint8_t, m_AllocSize));
69 }
70
AppendSpan(pdfium::span<const uint8_t> span)71 void CFX_BinaryBuf::AppendSpan(pdfium::span<const uint8_t> span) {
72 return AppendBlock(span.data(), span.size());
73 }
74
AppendBlock(const void * pBuf,size_t size)75 void CFX_BinaryBuf::AppendBlock(const void* pBuf, size_t size) {
76 if (size == 0)
77 return;
78
79 ExpandBuf(size);
80 if (pBuf) {
81 memcpy(m_pBuffer.get() + m_DataSize, pBuf, size);
82 } else {
83 memset(m_pBuffer.get() + m_DataSize, 0, size);
84 }
85 m_DataSize += size;
86 }
87