Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
binary_buffer.cpp
Go to the documentation of this file.
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
15namespace fxcrt {
16
17BinaryBuffer::BinaryBuffer() = default;
18
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
29BinaryBuffer::~BinaryBuffer() = default;
30
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
42void 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
52pdfium::span<uint8_t> BinaryBuffer::GetMutableSpan() {
53 return {m_buffer.data(), GetSize()};
54}
55
56pdfium::span<const uint8_t> BinaryBuffer::GetSpan() const {
57 return {m_buffer.data(), GetSize()};
58}
59
61 return GetSize();
62}
63
65 m_DataSize = 0;
66}
67
69 m_buffer.resize(GetSize());
70 m_DataSize = 0;
71 return std::move(m_buffer);
72}
73
74void BinaryBuffer::EstimateSize(size_t size) {
75 if (m_buffer.size() < size)
76 ExpandBuf(size - GetSize());
77}
78
79void 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
93void 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
103 AppendSpan(str.raw_span());
104}
105
106void BinaryBuffer::AppendUint8(uint8_t value) {
107 AppendSpan(pdfium::span_from_ref(value));
108}
109
110void BinaryBuffer::AppendUint16(uint16_t value) {
111 AppendSpan(pdfium::as_bytes(pdfium::span_from_ref(value)));
112}
113
114void BinaryBuffer::AppendUint32(uint32_t value) {
115 AppendSpan(pdfium::as_bytes(pdfium::span_from_ref(value)));
116}
117
118void BinaryBuffer::AppendDouble(double value) {
119 AppendSpan(pdfium::as_bytes(pdfium::span_from_ref(value)));
120}
121
122} // namespace fxcrt
virtual size_t GetLength() const
void AppendUint8(uint8_t value)
void AppendUint16(uint16_t value)
virtual ~BinaryBuffer()
void AppendSpan(pdfium::span< const uint8_t > span)
pdfium::span< const uint8_t > GetSpan() const
void DeleteBuf(size_t start_index, size_t count)
void AppendDouble(double value)
void ExpandBuf(size_t size)
void AppendString(const ByteString &str)
void EstimateSize(size_t size)
BinaryBuffer & operator=(BinaryBuffer &&that) noexcept
BinaryBuffer(BinaryBuffer &&that) noexcept
void AppendUint32(uint32_t value)
pdfium::span< uint8_t > GetMutableSpan()
DataVector< uint8_t > DetachBuffer()