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
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "services/network/web_bundle/web_bundle_chunked_buffer.h"
#include <algorithm>
#include "base/check.h"
#include "base/memory/ptr_util.h"
#include "base/not_fatal_until.h"
#include "base/numerics/checked_math.h"
namespace network {
namespace {
class ChunkedBufferDataSource : public mojo::DataPipeProducer::DataSource {
public:
ChunkedBufferDataSource(std::unique_ptr<const WebBundleChunkedBuffer> buffer,
uint64_t offset,
uint64_t length)
: buffer_(std::move(buffer)), offset_(offset), length_(length) {
DCHECK(buffer_);
}
~ChunkedBufferDataSource() override = default;
// Disallow copy and assign.
ChunkedBufferDataSource(const ChunkedBufferDataSource&) = delete;
ChunkedBufferDataSource& operator=(const ChunkedBufferDataSource&) = delete;
uint64_t GetLength() const override { return length_; }
ReadResult Read(uint64_t offset, base::span<char> buffer) override {
ReadResult result;
if (offset >= length_) {
result.result = MOJO_RESULT_OUT_OF_RANGE;
return result;
}
uint64_t read_start = offset_ + offset;
uint64_t len = std::min<uint64_t>(length_ - offset, buffer.size());
result.bytes_read = buffer_->ReadData(
read_start,
base::as_writable_bytes(buffer).first(base::checked_cast<size_t>(len)));
return result;
}
private:
const std::unique_ptr<const WebBundleChunkedBuffer> buffer_;
const uint64_t offset_;
const uint64_t length_;
};
} // namespace
WebBundleChunkedBuffer::Chunk::Chunk(
uint64_t start_pos,
scoped_refptr<const base::RefCountedBytes> bytes)
: start_pos_(start_pos), bytes_(std::move(bytes)) {
DCHECK(bytes_);
DCHECK(bytes_->size() != 0);
CHECK(base::CheckAdd<uint64_t>(start_pos_, bytes_->size()).IsValid());
}
WebBundleChunkedBuffer::Chunk::~Chunk() = default;
WebBundleChunkedBuffer::Chunk::Chunk(const WebBundleChunkedBuffer::Chunk&) =
default;
WebBundleChunkedBuffer::Chunk::Chunk(WebBundleChunkedBuffer::Chunk&&) = default;
WebBundleChunkedBuffer::WebBundleChunkedBuffer() = default;
WebBundleChunkedBuffer::WebBundleChunkedBuffer(ChunkVector chunks)
: chunks_(std::move(chunks)) {}
WebBundleChunkedBuffer::~WebBundleChunkedBuffer() = default;
void WebBundleChunkedBuffer::Append(base::span<const uint8_t> data) {
if (data.empty()) {
return;
}
auto bytes = base::MakeRefCounted<base::RefCountedBytes>(data);
chunks_.emplace_back(end_pos(), std::move(bytes));
}
bool WebBundleChunkedBuffer::ContainsAll(uint64_t offset,
uint64_t length) const {
DCHECK(base::CheckAdd<uint64_t>(offset, length).IsValid());
if (length == 0)
return true;
if (offset < start_pos())
return false;
if (offset + length > end_pos())
return false;
return true;
}
std::unique_ptr<mojo::DataPipeProducer::DataSource>
WebBundleChunkedBuffer::CreateDataSource(uint64_t offset,
uint64_t max_length) const {
uint64_t length = GetAvailableLength(offset, max_length);
if (length == 0)
return nullptr;
return std::make_unique<ChunkedBufferDataSource>(
CreatePartialBuffer(offset, length), offset, length);
}
uint64_t WebBundleChunkedBuffer::size() const {
DCHECK_GE(end_pos(), start_pos());
return end_pos() - start_pos();
}
WebBundleChunkedBuffer::ChunkVector::const_iterator
WebBundleChunkedBuffer::FindChunk(uint64_t pos) const {
if (empty())
return chunks_.end();
// |pos| ls before everything
if (pos < chunks_.begin()->start_pos())
return chunks_.end();
// As an optimization, check the last region first
if (chunks_.back().start_pos() <= pos) {
if (chunks_.back().end_pos() <= pos)
return chunks_.end();
return chunks_.end() - 1;
}
// Binary search
return std::partition_point(
chunks_.begin(), chunks_.end(),
[pos](const Chunk& chunk) { return chunk.end_pos() <= pos; });
}
std::unique_ptr<const WebBundleChunkedBuffer>
WebBundleChunkedBuffer::CreatePartialBuffer(uint64_t offset,
uint64_t length) const {
DCHECK(ContainsAll(offset, length));
ChunkVector::const_iterator it = FindChunk(offset);
CHECK(it != chunks_.end());
ChunkVector new_chunks;
while (it != chunks_.end() && it->start_pos() < offset + length) {
new_chunks.push_back(*it);
++it;
}
return base::WrapUnique(new WebBundleChunkedBuffer(std::move(new_chunks)));
}
bool WebBundleChunkedBuffer::empty() const {
return chunks_.empty();
}
uint64_t WebBundleChunkedBuffer::start_pos() const {
if (empty())
return 0;
return chunks_.front().start_pos();
}
uint64_t WebBundleChunkedBuffer::end_pos() const {
if (empty())
return 0;
return chunks_.back().end_pos();
}
uint64_t WebBundleChunkedBuffer::GetAvailableLength(uint64_t offset,
uint64_t max_length) const {
if (offset < start_pos())
return 0;
if (end_pos() <= offset)
return 0;
return std::min(max_length, end_pos() - offset);
}
uint64_t WebBundleChunkedBuffer::ReadData(uint64_t offset,
base::span<uint8_t> out) const {
uint64_t length = GetAvailableLength(offset, out.size());
if (length == 0) {
return 0;
}
ChunkVector::const_iterator it = FindChunk(offset);
uint64_t written = 0;
while (length > written && it != chunks_.end()) {
auto it_span = base::span(*it);
uint64_t offset_in_chunk = offset + written - it->start_pos();
uint64_t length_in_chunk =
std::min(it->size() - offset_in_chunk, length - written);
size_t checked_offset = base::checked_cast<size_t>(offset_in_chunk);
size_t checked_length = base::checked_cast<size_t>(length_in_chunk);
out.copy_prefix_from(it_span.subspan(checked_offset, checked_length));
out = out.subspan(checked_length);
written += checked_length;
++it;
}
return written;
}
} // namespace network
|