Avi Drissman | 6459548 | 2022-09-14 20:52:29 | [diff] [blame] | 1 | // Copyright 2011 The Chromium Authors |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/base/io_buffer.h" |
| 6 | |
Hans Wennborg | 0924470b | 2020-04-27 21:08:05 | [diff] [blame] | 7 | #include "base/check_op.h" |
eroman | 23ba60b | 2015-05-27 00:09:39 | [diff] [blame] | 8 | #include "base/numerics/safe_math.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 9 | |
| 10 | namespace net { |
| 11 | |
eroman | 23ba60b | 2015-05-27 00:09:39 | [diff] [blame] | 12 | // TODO(eroman): IOBuffer is being converted to require buffer sizes and offsets |
| 13 | // be specified as "size_t" rather than "int" (crbug.com/488553). To facilitate |
| 14 | // this move (since LOTS of code needs to be updated), both "size_t" and "int |
| 15 | // are being accepted. When using "size_t" this function ensures that it can be |
| 16 | // safely converted to an "int" without truncation. |
Victor Vasiliev | 4d53d20 | 2019-12-27 18:47:36 | [diff] [blame] | 17 | void IOBuffer::AssertValidBufferSize(size_t size) { |
eroman | 23ba60b | 2015-05-27 00:09:39 | [diff] [blame] | 18 | base::CheckedNumeric<int>(size).ValueOrDie(); |
| 19 | } |
| 20 | |
Victor Vasiliev | 4d53d20 | 2019-12-27 18:47:36 | [diff] [blame] | 21 | void IOBuffer::AssertValidBufferSize(int size) { |
eroman | 23ba60b | 2015-05-27 00:09:39 | [diff] [blame] | 22 | CHECK_GE(size, 0); |
| 23 | } |
| 24 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 25 | IOBuffer::IOBuffer() : data_(nullptr) {} |
[email protected] | 6d22a97 | 2010-07-21 15:47:19 | [diff] [blame] | 26 | |
eroman | 23ba60b | 2015-05-27 00:09:39 | [diff] [blame] | 27 | IOBuffer::IOBuffer(size_t buffer_size) { |
| 28 | AssertValidBufferSize(buffer_size); |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 29 | data_ = new char[buffer_size]; |
Nidhi Jaju | a7a017b | 2022-10-21 02:43:16 | [diff] [blame] | 30 | #if BUILDFLAG(IS_IOS) |
| 31 | // TODO(crbug.com/1335423): Investigating crashes on iOS. |
| 32 | CHECK(data_); |
| 33 | #endif // BUILDFLAG(IS_IOS) |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 34 | } |
[email protected] | c19c715 | 2009-10-14 17:35:37 | [diff] [blame] | 35 | |
[email protected] | 6d22a97 | 2010-07-21 15:47:19 | [diff] [blame] | 36 | IOBuffer::IOBuffer(char* data) |
| 37 | : data_(data) { |
| 38 | } |
| 39 | |
| 40 | IOBuffer::~IOBuffer() { |
Arthur Sonzogni | 53809e57 | 2022-02-01 17:51:54 | [diff] [blame] | 41 | data_.ClearAndDeleteArray(); |
[email protected] | 6d22a97 | 2010-07-21 15:47:19 | [diff] [blame] | 42 | } |
| 43 | |
eroman | 23ba60b | 2015-05-27 00:09:39 | [diff] [blame] | 44 | IOBufferWithSize::IOBufferWithSize(size_t size) : IOBuffer(size), size_(size) { |
| 45 | // Note: Size check is done in superclass' constructor. |
[email protected] | 6d22a97 | 2010-07-21 15:47:19 | [diff] [blame] | 46 | } |
| 47 | |
eroman | 23ba60b | 2015-05-27 00:09:39 | [diff] [blame] | 48 | IOBufferWithSize::IOBufferWithSize(char* data, size_t size) |
| 49 | : IOBuffer(data), size_(size) { |
| 50 | AssertValidBufferSize(size); |
[email protected] | 5ddffb8 | 2011-10-14 17:48:07 | [diff] [blame] | 51 | } |
| 52 | |
Chris Watkins | 68b1503 | 2017-12-01 03:07:13 | [diff] [blame] | 53 | IOBufferWithSize::~IOBufferWithSize() = default; |
[email protected] | 7e4468d5 | 2010-09-22 19:42:00 | [diff] [blame] | 54 | |
[email protected] | 6d22a97 | 2010-07-21 15:47:19 | [diff] [blame] | 55 | StringIOBuffer::StringIOBuffer(const std::string& s) |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 56 | : IOBuffer(static_cast<char*>(nullptr)), string_data_(s) { |
eroman | 23ba60b | 2015-05-27 00:09:39 | [diff] [blame] | 57 | AssertValidBufferSize(s.size()); |
[email protected] | 6d22a97 | 2010-07-21 15:47:19 | [diff] [blame] | 58 | data_ = const_cast<char*>(string_data_.data()); |
| 59 | } |
| 60 | |
danakj | 7f767e6 | 2016-04-16 23:20:23 | [diff] [blame] | 61 | StringIOBuffer::StringIOBuffer(std::unique_ptr<std::string> s) |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 62 | : IOBuffer(static_cast<char*>(nullptr)) { |
eroman | 23ba60b | 2015-05-27 00:09:39 | [diff] [blame] | 63 | AssertValidBufferSize(s->size()); |
jkarlin | b5ca81a3 | 2014-08-26 03:59:29 | [diff] [blame] | 64 | string_data_.swap(*s.get()); |
| 65 | data_ = const_cast<char*>(string_data_.data()); |
| 66 | } |
| 67 | |
[email protected] | 6d22a97 | 2010-07-21 15:47:19 | [diff] [blame] | 68 | StringIOBuffer::~StringIOBuffer() { |
| 69 | // We haven't allocated the buffer, so remove it before the base class |
| 70 | // destructor tries to delete[] it. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 71 | data_ = nullptr; |
[email protected] | 6d22a97 | 2010-07-21 15:47:19 | [diff] [blame] | 72 | } |
| 73 | |
Victor Costan | cd43978 | 2018-08-30 07:27:57 | [diff] [blame] | 74 | DrainableIOBuffer::DrainableIOBuffer(scoped_refptr<IOBuffer> base, int size) |
Tsuyoshi Horo | 432981d5 | 2022-06-09 09:50:13 | [diff] [blame] | 75 | : IOBuffer(base->data()), base_(std::move(base)), size_(size) { |
eroman | 23ba60b | 2015-05-27 00:09:39 | [diff] [blame] | 76 | AssertValidBufferSize(size); |
| 77 | } |
| 78 | |
Victor Costan | cd43978 | 2018-08-30 07:27:57 | [diff] [blame] | 79 | DrainableIOBuffer::DrainableIOBuffer(scoped_refptr<IOBuffer> base, size_t size) |
Tsuyoshi Horo | 432981d5 | 2022-06-09 09:50:13 | [diff] [blame] | 80 | : IOBuffer(base->data()), base_(std::move(base)), size_(size) { |
eroman | 23ba60b | 2015-05-27 00:09:39 | [diff] [blame] | 81 | AssertValidBufferSize(size); |
[email protected] | 6d22a97 | 2010-07-21 15:47:19 | [diff] [blame] | 82 | } |
| 83 | |
[email protected] | 6d22a97 | 2010-07-21 15:47:19 | [diff] [blame] | 84 | void DrainableIOBuffer::DidConsume(int bytes) { |
| 85 | SetOffset(used_ + bytes); |
| 86 | } |
| 87 | |
| 88 | int DrainableIOBuffer::BytesRemaining() const { |
| 89 | return size_ - used_; |
| 90 | } |
| 91 | |
| 92 | // Returns the number of consumed bytes. |
| 93 | int DrainableIOBuffer::BytesConsumed() const { |
| 94 | return used_; |
| 95 | } |
| 96 | |
[email protected] | c19c715 | 2009-10-14 17:35:37 | [diff] [blame] | 97 | void DrainableIOBuffer::SetOffset(int bytes) { |
[email protected] | 52af5d5 | 2011-05-05 09:59:28 | [diff] [blame] | 98 | DCHECK_GE(bytes, 0); |
| 99 | DCHECK_LE(bytes, size_); |
[email protected] | c19c715 | 2009-10-14 17:35:37 | [diff] [blame] | 100 | used_ = bytes; |
| 101 | data_ = base_->data() + used_; |
| 102 | } |
| 103 | |
[email protected] | be1a48b | 2011-01-20 00:12:13 | [diff] [blame] | 104 | DrainableIOBuffer::~DrainableIOBuffer() { |
| 105 | // The buffer is owned by the |base_| instance. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 106 | data_ = nullptr; |
[email protected] | be1a48b | 2011-01-20 00:12:13 | [diff] [blame] | 107 | } |
| 108 | |
Tsuyoshi Horo | e0235ed6 | 2022-06-09 01:42:30 | [diff] [blame] | 109 | GrowableIOBuffer::GrowableIOBuffer() = default; |
[email protected] | 6d22a97 | 2010-07-21 15:47:19 | [diff] [blame] | 110 | |
[email protected] | 776e175 | 2009-11-10 18:51:34 | [diff] [blame] | 111 | void GrowableIOBuffer::SetCapacity(int capacity) { |
[email protected] | 52af5d5 | 2011-05-05 09:59:28 | [diff] [blame] | 112 | DCHECK_GE(capacity, 0); |
Paul Semel | e8088bd | 2022-08-16 10:33:13 | [diff] [blame] | 113 | // this will get reset in `set_offset`. |
| 114 | data_ = nullptr; |
[email protected] | 776e175 | 2009-11-10 18:51:34 | [diff] [blame] | 115 | // realloc will crash if it fails. |
| 116 | real_data_.reset(static_cast<char*>(realloc(real_data_.release(), capacity))); |
Paul Semel | e8088bd | 2022-08-16 10:33:13 | [diff] [blame] | 117 | |
[email protected] | c19c715 | 2009-10-14 17:35:37 | [diff] [blame] | 118 | capacity_ = capacity; |
[email protected] | c19c715 | 2009-10-14 17:35:37 | [diff] [blame] | 119 | if (offset_ > capacity) |
| 120 | set_offset(capacity); |
| 121 | else |
| 122 | set_offset(offset_); // The pointer may have changed. |
| 123 | } |
| 124 | |
| 125 | void GrowableIOBuffer::set_offset(int offset) { |
[email protected] | 52af5d5 | 2011-05-05 09:59:28 | [diff] [blame] | 126 | DCHECK_GE(offset, 0); |
| 127 | DCHECK_LE(offset, capacity_); |
[email protected] | c19c715 | 2009-10-14 17:35:37 | [diff] [blame] | 128 | offset_ = offset; |
| 129 | data_ = real_data_.get() + offset; |
[email protected] | 84d4cee | 2009-06-18 23:46:58 | [diff] [blame] | 130 | } |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 131 | |
[email protected] | 6d22a97 | 2010-07-21 15:47:19 | [diff] [blame] | 132 | int GrowableIOBuffer::RemainingCapacity() { |
| 133 | return capacity_ - offset_; |
| 134 | } |
| 135 | |
| 136 | char* GrowableIOBuffer::StartOfBuffer() { |
| 137 | return real_data_.get(); |
| 138 | } |
| 139 | |
[email protected] | be1a48b | 2011-01-20 00:12:13 | [diff] [blame] | 140 | GrowableIOBuffer::~GrowableIOBuffer() { |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 141 | data_ = nullptr; |
[email protected] | be1a48b | 2011-01-20 00:12:13 | [diff] [blame] | 142 | } |
[email protected] | 6d22a97 | 2010-07-21 15:47:19 | [diff] [blame] | 143 | |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 144 | PickledIOBuffer::PickledIOBuffer() : IOBuffer() { |
| 145 | } |
[email protected] | 6d22a97 | 2010-07-21 15:47:19 | [diff] [blame] | 146 | |
| 147 | void PickledIOBuffer::Done() { |
| 148 | data_ = const_cast<char*>(static_cast<const char*>(pickle_.data())); |
| 149 | } |
| 150 | |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 151 | PickledIOBuffer::~PickledIOBuffer() { |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 152 | data_ = nullptr; |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 153 | } |
[email protected] | 6d22a97 | 2010-07-21 15:47:19 | [diff] [blame] | 154 | |
| 155 | WrappedIOBuffer::WrappedIOBuffer(const char* data) |
| 156 | : IOBuffer(const_cast<char*>(data)) { |
| 157 | } |
| 158 | |
| 159 | WrappedIOBuffer::~WrappedIOBuffer() { |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 160 | data_ = nullptr; |
[email protected] | 6d22a97 | 2010-07-21 15:47:19 | [diff] [blame] | 161 | } |
| 162 | |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 163 | } // namespace net |