xref: /aosp_15_r20/external/pdfium/core/fxcrt/binary_buffer.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 // Copyright 2017 The PDFium Authors
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/binary_buffer.h"
8 
9 #include <algorithm>
10 #include <utility>
11 
12 #include "core/fxcrt/fx_safe_types.h"
13 #include "core/fxcrt/span_util.h"
14 
15 namespace fxcrt {
16 
17 BinaryBuffer::BinaryBuffer() = default;
18 
BinaryBuffer(BinaryBuffer && that)19 BinaryBuffer::BinaryBuffer(BinaryBuffer&& that) noexcept
20     : m_AllocStep(that.m_AllocStep),
21       m_DataSize(that.m_DataSize),
22       m_buffer(std::move(that.m_buffer)) {
23   // Can't just default, need to leave |that| in a valid state, which means
24   // that the size members reflect the (null) moved-from buffer.
25   that.m_AllocStep = 0;
26   that.m_DataSize = 0;
27 }
28 
29 BinaryBuffer::~BinaryBuffer() = default;
30 
operator =(BinaryBuffer && that)31 BinaryBuffer& BinaryBuffer::operator=(BinaryBuffer&& that) noexcept {
32   // Can't just default, need to leave |that| in a valid state, which means
33   // that the size members reflect the (null) moved-from buffer.
34   m_AllocStep = that.m_AllocStep;
35   m_DataSize = that.m_DataSize;
36   m_buffer = std::move(that.m_buffer);
37   that.m_AllocStep = 0;
38   that.m_DataSize = 0;
39   return *this;
40 }
41 
DeleteBuf(size_t start_index,size_t count)42 void BinaryBuffer::DeleteBuf(size_t start_index, size_t count) {
43   if (m_buffer.empty() || count > GetSize() || start_index > GetSize() - count)
44     return;
45 
46   auto buffer_span = pdfium::make_span(m_buffer).first(GetSize());
47   fxcrt::spanmove(buffer_span.subspan(start_index),
48                   buffer_span.subspan(start_index + count));
49   m_DataSize -= count;
50 }
51 
GetMutableSpan()52 pdfium::span<uint8_t> BinaryBuffer::GetMutableSpan() {
53   return {m_buffer.data(), GetSize()};
54 }
55 
GetSpan() const56 pdfium::span<const uint8_t> BinaryBuffer::GetSpan() const {
57   return {m_buffer.data(), GetSize()};
58 }
59 
GetLength() const60 size_t BinaryBuffer::GetLength() const {
61   return GetSize();
62 }
63 
Clear()64 void BinaryBuffer::Clear() {
65   m_DataSize = 0;
66 }
67 
DetachBuffer()68 DataVector<uint8_t> BinaryBuffer::DetachBuffer() {
69   m_buffer.resize(GetSize());
70   m_DataSize = 0;
71   return std::move(m_buffer);
72 }
73 
EstimateSize(size_t size)74 void BinaryBuffer::EstimateSize(size_t size) {
75   if (m_buffer.size() < size)
76     ExpandBuf(size - GetSize());
77 }
78 
ExpandBuf(size_t add_size)79 void BinaryBuffer::ExpandBuf(size_t add_size) {
80   FX_SAFE_SIZE_T new_size = GetSize();
81   new_size += add_size;
82   if (m_buffer.size() >= new_size.ValueOrDie())
83     return;
84 
85   size_t alloc_step = std::max(static_cast<size_t>(128),
86                                m_AllocStep ? m_AllocStep : m_buffer.size() / 4);
87   new_size += alloc_step - 1;  // Quantize, don't combine these lines.
88   new_size /= alloc_step;
89   new_size *= alloc_step;
90   m_buffer.resize(new_size.ValueOrDie());
91 }
92 
AppendSpan(pdfium::span<const uint8_t> span)93 void BinaryBuffer::AppendSpan(pdfium::span<const uint8_t> span) {
94   if (span.empty())
95     return;
96 
97   ExpandBuf(span.size());
98   fxcrt::spancpy(pdfium::make_span(m_buffer).subspan(GetSize()), span);
99   m_DataSize += span.size();
100 }
101 
AppendString(const ByteString & str)102 void BinaryBuffer::AppendString(const ByteString& str) {
103   AppendSpan(str.raw_span());
104 }
105 
AppendUint8(uint8_t value)106 void BinaryBuffer::AppendUint8(uint8_t value) {
107   AppendSpan({&value, 1});
108 }
109 
AppendUint16(uint16_t value)110 void BinaryBuffer::AppendUint16(uint16_t value) {
111   AppendSpan({reinterpret_cast<uint8_t*>(&value), sizeof(value)});
112 }
113 
AppendUint32(uint32_t value)114 void BinaryBuffer::AppendUint32(uint32_t value) {
115   AppendSpan({reinterpret_cast<uint8_t*>(&value), sizeof(value)});
116 }
117 
AppendDouble(double value)118 void BinaryBuffer::AppendDouble(double value) {
119   AppendSpan({reinterpret_cast<uint8_t*>(&value), sizeof(value)});
120 }
121 
122 }  // namespace fxcrt
123