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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/base/io_buffer.h"
#include <utility>
#include "base/check_op.h"
#include "base/compiler_specific.h"
#include "base/containers/heap_array.h"
#include "base/numerics/safe_math.h"
#include "base/pickle.h"
namespace net {
// TODO(eroman): IOBuffer is being converted to require buffer sizes and offsets
// be specified as "size_t" rather than "int" (crbug.com/488553). To facilitate
// this move (since LOTS of code needs to be updated), this function ensures
// that sizes can be safely converted to an "int" without truncation. The
// assert ensures calling this with an "int" argument is also safe.
void IOBuffer::AssertValidBufferSize(size_t size) {
static_assert(sizeof(size_t) >= sizeof(int));
base::CheckedNumeric<int>(size).ValueOrDie();
}
IOBuffer::IOBuffer() = default;
IOBuffer::IOBuffer(base::span<char> span)
: IOBuffer(base::as_writable_bytes(span)) {}
IOBuffer::IOBuffer(base::span<uint8_t> span) : span_(span) {
AssertValidBufferSize(span_.size());
}
IOBuffer::~IOBuffer() = default;
void IOBuffer::SetSpan(base::span<uint8_t> span) {
AssertValidBufferSize(span.size());
span_ = span;
}
void IOBuffer::ClearSpan() {
span_ = base::span<uint8_t>();
}
IOBufferWithSize::IOBufferWithSize() = default;
IOBufferWithSize::IOBufferWithSize(size_t buffer_size)
: storage_(base::HeapArray<uint8_t>::Uninit(buffer_size)) {
SetSpan(storage_);
}
IOBufferWithSize::~IOBufferWithSize() {
// Clear pointer before this destructor makes it dangle.
ClearSpan();
}
VectorIOBuffer::VectorIOBuffer(std::vector<uint8_t> vector)
: vector_(std::move(vector)) {
SetSpan(vector_);
}
VectorIOBuffer::VectorIOBuffer(base::span<const uint8_t> span)
: VectorIOBuffer(std::vector(span.begin(), span.end())) {}
VectorIOBuffer::~VectorIOBuffer() {
// Clear pointer before this destructor makes it dangle.
ClearSpan();
}
StringIOBuffer::StringIOBuffer(std::string s) : string_data_(std::move(s)) {
// Can't pass `s.data()` directly to IOBuffer constructor since moving
// from `s` may invalidate it. This is especially true for libc++ short
// string optimization where the data may be held in the string variable
// itself, instead of in a movable backing store.
SetSpan(base::as_writable_bytes(base::span(string_data_)));
}
StringIOBuffer::~StringIOBuffer() {
// Clear pointer before this destructor makes it dangle.
ClearSpan();
}
DrainableIOBuffer::DrainableIOBuffer(scoped_refptr<IOBuffer> base, size_t size)
: IOBuffer(base->first(size)), base_(std::move(base)) {}
void DrainableIOBuffer::DidConsume(int bytes) {
SetOffset(used_ + bytes);
}
int DrainableIOBuffer::BytesRemaining() const {
return size();
}
// Returns the number of consumed bytes.
int DrainableIOBuffer::BytesConsumed() const {
return used_;
}
void DrainableIOBuffer::SetOffset(int bytes) {
CHECK_GE(bytes, 0);
// Length from the start of `base_` to the end of the buffer passed in to the
// constructor isn't stored anywhere, so need to calculate it.
int length = size() + used_;
CHECK_LE(bytes, length);
used_ = bytes;
SetSpan(base_->span().subspan(base::checked_cast<size_t>(used_),
base::checked_cast<size_t>(length - bytes)));
}
DrainableIOBuffer::~DrainableIOBuffer() {
// Clear ptr before this destructor destroys the |base_| instance,
// making it dangle.
ClearSpan();
}
GrowableIOBuffer::GrowableIOBuffer() = default;
void GrowableIOBuffer::SetCapacity(int capacity) {
CHECK_GE(capacity, 0);
// The span will be set again in `set_offset()`. Need to clear raw pointers to
// the data before reallocating the buffer.
ClearSpan();
// realloc will crash if it fails.
real_data_.reset(
static_cast<uint8_t*>(realloc(real_data_.release(), capacity)));
capacity_ = capacity;
if (offset_ > capacity)
set_offset(capacity);
else
set_offset(offset_); // The pointer may have changed.
}
void GrowableIOBuffer::set_offset(int offset) {
CHECK_GE(offset, 0);
CHECK_LE(offset, capacity_);
offset_ = offset;
UNSAFE_TODO(SetSpan(base::span<uint8_t>(
real_data_.get() + offset, static_cast<size_t>(capacity_ - offset))));
}
void GrowableIOBuffer::DidConsume(int bytes) {
CHECK_LE(0, bytes);
CHECK_LE(bytes, size());
set_offset(offset_ + bytes);
}
int GrowableIOBuffer::RemainingCapacity() {
return size();
}
base::span<uint8_t> GrowableIOBuffer::everything() {
return base::as_writable_bytes(
// SAFETY: The capacity_ is the size of the allocation.
UNSAFE_BUFFERS(
base::span(real_data_.get(), base::checked_cast<size_t>(capacity_))));
}
base::span<const uint8_t> GrowableIOBuffer::everything() const {
return base::as_bytes(
// SAFETY: The capacity_ is the size of the allocation.
UNSAFE_BUFFERS(
base::span(real_data_.get(), base::checked_cast<size_t>(capacity_))));
}
base::span<uint8_t> GrowableIOBuffer::span_before_offset() {
return everything().first(base::checked_cast<size_t>(offset_));
}
base::span<const uint8_t> GrowableIOBuffer::span_before_offset() const {
return everything().first(base::checked_cast<size_t>(offset_));
}
GrowableIOBuffer::~GrowableIOBuffer() {
ClearSpan();
}
PickledIOBuffer::PickledIOBuffer(std::unique_ptr<const base::Pickle> pickle)
: // SAFETY: const cast does not affect size.
IOBuffer(UNSAFE_BUFFERS(
base::span(const_cast<uint8_t*>(pickle->data()), pickle->size()))),
pickle_(std::move(pickle)) {}
PickledIOBuffer::~PickledIOBuffer() {
// Avoid dangling ptr when this destructor destroys the pickle.
ClearSpan();
}
WrappedIOBuffer::WrappedIOBuffer(base::span<const char> data)
: WrappedIOBuffer(base::as_byte_span(data)) {}
WrappedIOBuffer::WrappedIOBuffer(base::span<const uint8_t> data)
// SAFETY: const cast does not affect size.
: IOBuffer(UNSAFE_BUFFERS(
base::span(const_cast<uint8_t*>(data.data()), data.size()))) {}
WrappedIOBuffer::~WrappedIOBuffer() = default;
} // namespace net
|