1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/base/io_buffer.h"
7 #include "base/logging.h"
8 #include "base/numerics/safe_math.h"
14 // TODO(eroman): IOBuffer is being converted to require buffer sizes and offsets
15 // be specified as "size_t" rather than "int" (crbug.com/488553). To facilitate
16 // this move (since LOTS of code needs to be updated), both "size_t" and "int
17 // are being accepted. When using "size_t" this function ensures that it can be
18 // safely converted to an "int" without truncation.
19 void AssertValidBufferSize(size_t size
) {
20 base::CheckedNumeric
<int>(size
).ValueOrDie();
23 void AssertValidBufferSize(int size
) {
33 IOBuffer::IOBuffer(int buffer_size
) {
34 AssertValidBufferSize(buffer_size
);
35 data_
= new char[buffer_size
];
38 IOBuffer::IOBuffer(size_t buffer_size
) {
39 AssertValidBufferSize(buffer_size
);
40 data_
= new char[buffer_size
];
43 IOBuffer::IOBuffer(char* data
)
47 IOBuffer::~IOBuffer() {
52 IOBufferWithSize::IOBufferWithSize(int size
)
55 AssertValidBufferSize(size
);
58 IOBufferWithSize::IOBufferWithSize(size_t size
) : IOBuffer(size
), size_(size
) {
59 // Note: Size check is done in superclass' constructor.
62 IOBufferWithSize::IOBufferWithSize(char* data
, int size
)
65 AssertValidBufferSize(size
);
68 IOBufferWithSize::IOBufferWithSize(char* data
, size_t size
)
69 : IOBuffer(data
), size_(size
) {
70 AssertValidBufferSize(size
);
73 IOBufferWithSize::~IOBufferWithSize() {
76 StringIOBuffer::StringIOBuffer(const std::string
& s
)
77 : IOBuffer(static_cast<char*>(NULL
)),
79 AssertValidBufferSize(s
.size());
80 data_
= const_cast<char*>(string_data_
.data());
83 StringIOBuffer::StringIOBuffer(scoped_ptr
<std::string
> s
)
84 : IOBuffer(static_cast<char*>(NULL
)) {
85 AssertValidBufferSize(s
->size());
86 string_data_
.swap(*s
.get());
87 data_
= const_cast<char*>(string_data_
.data());
90 StringIOBuffer::~StringIOBuffer() {
91 // We haven't allocated the buffer, so remove it before the base class
92 // destructor tries to delete[] it.
96 DrainableIOBuffer::DrainableIOBuffer(IOBuffer
* base
, int size
)
97 : IOBuffer(base
->data()),
101 AssertValidBufferSize(size
);
104 DrainableIOBuffer::DrainableIOBuffer(IOBuffer
* base
, size_t size
)
105 : IOBuffer(base
->data()), base_(base
), size_(size
), used_(0) {
106 AssertValidBufferSize(size
);
109 void DrainableIOBuffer::DidConsume(int bytes
) {
110 SetOffset(used_
+ bytes
);
113 int DrainableIOBuffer::BytesRemaining() const {
114 return size_
- used_
;
117 // Returns the number of consumed bytes.
118 int DrainableIOBuffer::BytesConsumed() const {
122 void DrainableIOBuffer::SetOffset(int bytes
) {
124 DCHECK_LE(bytes
, size_
);
126 data_
= base_
->data() + used_
;
129 DrainableIOBuffer::~DrainableIOBuffer() {
130 // The buffer is owned by the |base_| instance.
134 GrowableIOBuffer::GrowableIOBuffer()
140 void GrowableIOBuffer::SetCapacity(int capacity
) {
141 DCHECK_GE(capacity
, 0);
142 // realloc will crash if it fails.
143 real_data_
.reset(static_cast<char*>(realloc(real_data_
.release(), capacity
)));
144 capacity_
= capacity
;
145 if (offset_
> capacity
)
146 set_offset(capacity
);
148 set_offset(offset_
); // The pointer may have changed.
151 void GrowableIOBuffer::set_offset(int offset
) {
152 DCHECK_GE(offset
, 0);
153 DCHECK_LE(offset
, capacity_
);
155 data_
= real_data_
.get() + offset
;
158 int GrowableIOBuffer::RemainingCapacity() {
159 return capacity_
- offset_
;
162 char* GrowableIOBuffer::StartOfBuffer() {
163 return real_data_
.get();
166 GrowableIOBuffer::~GrowableIOBuffer() {
170 PickledIOBuffer::PickledIOBuffer() : IOBuffer() {
173 void PickledIOBuffer::Done() {
174 data_
= const_cast<char*>(static_cast<const char*>(pickle_
.data()));
177 PickledIOBuffer::~PickledIOBuffer() {
181 WrappedIOBuffer::WrappedIOBuffer(const char* data
)
182 : IOBuffer(const_cast<char*>(data
)) {
185 WrappedIOBuffer::~WrappedIOBuffer() {