OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/pickle.h" | 5 #include "base/pickle.h" |
6 | 6 |
7 #include <stdlib.h> | 7 #include <stdlib.h> |
8 | 8 |
9 #include <algorithm> // for max() | 9 #include <algorithm> // for max() |
10 | 10 |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 return false; | 146 return false; |
147 *data = read_from; | 147 *data = read_from; |
148 return true; | 148 return true; |
149 } | 149 } |
150 | 150 |
151 // Payload is uint32 aligned. | 151 // Payload is uint32 aligned. |
152 | 152 |
153 Pickle::Pickle() | 153 Pickle::Pickle() |
154 : header_(NULL), | 154 : header_(NULL), |
155 header_size_(sizeof(Header)), | 155 header_size_(sizeof(Header)), |
156 capacity_(0) { | 156 capacity_after_header_(0), |
| 157 write_offset_(0) { |
157 Resize(kPayloadUnit); | 158 Resize(kPayloadUnit); |
158 header_->payload_size = 0; | 159 header_->payload_size = 0; |
159 } | 160 } |
160 | 161 |
161 Pickle::Pickle(int header_size) | 162 Pickle::Pickle(int header_size) |
162 : header_(NULL), | 163 : header_(NULL), |
163 header_size_(AlignInt(header_size, sizeof(uint32))), | 164 header_size_(AlignInt(header_size, sizeof(uint32))), |
164 capacity_(0) { | 165 capacity_after_header_(0), |
| 166 write_offset_(0) { |
165 DCHECK_GE(static_cast<size_t>(header_size), sizeof(Header)); | 167 DCHECK_GE(static_cast<size_t>(header_size), sizeof(Header)); |
166 DCHECK_LE(header_size, kPayloadUnit); | 168 DCHECK_LE(header_size, kPayloadUnit); |
167 Resize(kPayloadUnit); | 169 Resize(kPayloadUnit); |
168 header_->payload_size = 0; | 170 header_->payload_size = 0; |
169 } | 171 } |
170 | 172 |
171 Pickle::Pickle(const char* data, size_t data_len) | 173 Pickle::Pickle(const char* data, size_t data_len) |
172 : header_(reinterpret_cast<Header*>(const_cast<char*>(data))), | 174 : header_(reinterpret_cast<Header*>(const_cast<char*>(data))), |
173 header_size_(0), | 175 header_size_(0), |
174 capacity_(kCapacityReadOnly) { | 176 capacity_after_header_(kCapacityReadOnly), |
| 177 write_offset_(0) { |
175 if (data_len >= sizeof(Header)) | 178 if (data_len >= sizeof(Header)) |
176 header_size_ = data_len - header_->payload_size; | 179 header_size_ = data_len - header_->payload_size; |
177 | 180 |
178 if (header_size_ > data_len) | 181 if (header_size_ > data_len) |
179 header_size_ = 0; | 182 header_size_ = 0; |
180 | 183 |
181 if (header_size_ != AlignInt(header_size_, sizeof(uint32))) | 184 if (header_size_ != AlignInt(header_size_, sizeof(uint32))) |
182 header_size_ = 0; | 185 header_size_ = 0; |
183 | 186 |
184 // If there is anything wrong with the data, we're not going to use it. | 187 // If there is anything wrong with the data, we're not going to use it. |
185 if (!header_size_) | 188 if (!header_size_) |
186 header_ = NULL; | 189 header_ = NULL; |
187 } | 190 } |
188 | 191 |
189 Pickle::Pickle(const Pickle& other) | 192 Pickle::Pickle(const Pickle& other) |
190 : header_(NULL), | 193 : header_(NULL), |
191 header_size_(other.header_size_), | 194 header_size_(other.header_size_), |
192 capacity_(0) { | 195 capacity_after_header_(0), |
| 196 write_offset_(other.write_offset_) { |
193 size_t payload_size = header_size_ + other.header_->payload_size; | 197 size_t payload_size = header_size_ + other.header_->payload_size; |
194 bool resized = Resize(payload_size); | 198 Resize(payload_size); |
195 CHECK(resized); // Realloc failed. | |
196 memcpy(header_, other.header_, payload_size); | 199 memcpy(header_, other.header_, payload_size); |
197 } | 200 } |
198 | 201 |
199 Pickle::~Pickle() { | 202 Pickle::~Pickle() { |
200 if (capacity_ != kCapacityReadOnly) | 203 if (capacity_after_header_ != kCapacityReadOnly) |
201 free(header_); | 204 free(header_); |
202 } | 205 } |
203 | 206 |
204 Pickle& Pickle::operator=(const Pickle& other) { | 207 Pickle& Pickle::operator=(const Pickle& other) { |
205 if (this == &other) { | 208 if (this == &other) { |
206 NOTREACHED(); | 209 NOTREACHED(); |
207 return *this; | 210 return *this; |
208 } | 211 } |
209 if (capacity_ == kCapacityReadOnly) { | 212 if (capacity_after_header_ == kCapacityReadOnly) { |
210 header_ = NULL; | 213 header_ = NULL; |
211 capacity_ = 0; | 214 capacity_after_header_ = 0; |
212 } | 215 } |
213 if (header_size_ != other.header_size_) { | 216 if (header_size_ != other.header_size_) { |
214 free(header_); | 217 free(header_); |
215 header_ = NULL; | 218 header_ = NULL; |
216 header_size_ = other.header_size_; | 219 header_size_ = other.header_size_; |
217 } | 220 } |
218 bool resized = Resize(other.header_size_ + other.header_->payload_size); | 221 Resize(other.header_->payload_size); |
219 CHECK(resized); // Realloc failed. | |
220 memcpy(header_, other.header_, | 222 memcpy(header_, other.header_, |
221 other.header_size_ + other.header_->payload_size); | 223 other.header_size_ + other.header_->payload_size); |
| 224 write_offset_ = other.write_offset_; |
222 return *this; | 225 return *this; |
223 } | 226 } |
224 | 227 |
225 bool Pickle::WriteString(const std::string& value) { | 228 bool Pickle::WriteString(const std::string& value) { |
226 if (!WriteInt(static_cast<int>(value.size()))) | 229 if (!WriteInt(static_cast<int>(value.size()))) |
227 return false; | 230 return false; |
228 | 231 |
229 return WriteBytes(value.data(), static_cast<int>(value.size())); | 232 return WriteBytes(value.data(), static_cast<int>(value.size())); |
230 } | 233 } |
231 | 234 |
(...skipping 10 matching lines...) Expand all Loading... |
242 return false; | 245 return false; |
243 | 246 |
244 return WriteBytes(value.data(), | 247 return WriteBytes(value.data(), |
245 static_cast<int>(value.size()) * sizeof(char16)); | 248 static_cast<int>(value.size()) * sizeof(char16)); |
246 } | 249 } |
247 | 250 |
248 bool Pickle::WriteData(const char* data, int length) { | 251 bool Pickle::WriteData(const char* data, int length) { |
249 return length >= 0 && WriteInt(length) && WriteBytes(data, length); | 252 return length >= 0 && WriteInt(length) && WriteBytes(data, length); |
250 } | 253 } |
251 | 254 |
252 bool Pickle::WriteBytes(const void* data, int data_len) { | 255 bool Pickle::WriteBytes(const void* data, int length) { |
253 DCHECK_NE(kCapacityReadOnly, capacity_) << "oops: pickle is readonly"; | 256 WriteBytesCommon(data, length); |
254 | |
255 char* dest = BeginWrite(data_len); | |
256 if (!dest) | |
257 return false; | |
258 | |
259 memcpy(dest, data, data_len); | |
260 | |
261 EndWrite(dest, data_len); | |
262 return true; | 257 return true; |
263 } | 258 } |
264 | 259 |
265 void Pickle::Reserve(size_t additional_capacity) { | 260 void Pickle::Reserve(size_t length) { |
266 // Write at a uint32-aligned offset from the beginning of the header. | 261 size_t data_len = AlignInt(length, sizeof(uint32)); |
267 size_t offset = AlignInt(header_->payload_size, sizeof(uint32)); | 262 DCHECK_GE(data_len, length); |
268 | 263 #ifdef ARCH_CPU_64_BITS |
269 size_t new_size = offset + additional_capacity; | 264 DCHECK_LE(data_len, kuint32max); |
270 size_t needed_size = header_size_ + new_size; | 265 #endif |
271 if (needed_size > capacity_) | 266 DCHECK_LE(write_offset_, kuint32max - data_len); |
272 Resize(capacity_ * 2 + needed_size); | 267 size_t new_size = write_offset_ + data_len; |
| 268 if (new_size > capacity_after_header_) |
| 269 Resize(capacity_after_header_ * 2 + new_size); |
273 } | 270 } |
274 | 271 |
275 char* Pickle::BeginWrite(size_t length) { | 272 void Pickle::Resize(size_t new_capacity) { |
276 // Write at a uint32-aligned offset from the beginning of the header. | |
277 size_t offset = AlignInt(header_->payload_size, sizeof(uint32)); | |
278 | |
279 size_t new_size = offset + length; | |
280 size_t needed_size = header_size_ + new_size; | |
281 if (needed_size > capacity_ && !Resize(std::max(capacity_ * 2, needed_size))) | |
282 return NULL; | |
283 | |
284 #ifdef ARCH_CPU_64_BITS | |
285 DCHECK_LE(length, kuint32max); | |
286 #endif | |
287 | |
288 header_->payload_size = static_cast<uint32>(new_size); | |
289 return mutable_payload() + offset; | |
290 } | |
291 | |
292 void Pickle::EndWrite(char* dest, int length) { | |
293 // Zero-pad to keep tools like valgrind from complaining about uninitialized | |
294 // memory. | |
295 if (length % sizeof(uint32)) | |
296 memset(dest + length, 0, sizeof(uint32) - (length % sizeof(uint32))); | |
297 } | |
298 | |
299 bool Pickle::Resize(size_t new_capacity) { | |
300 new_capacity = AlignInt(new_capacity, kPayloadUnit); | 273 new_capacity = AlignInt(new_capacity, kPayloadUnit); |
301 | 274 |
302 CHECK_NE(capacity_, kCapacityReadOnly); | 275 CHECK_NE(capacity_after_header_, kCapacityReadOnly); |
303 void* p = realloc(header_, new_capacity); | 276 void* p = realloc(header_, header_size_ + new_capacity); |
304 if (!p) | 277 CHECK(p); |
305 return false; | |
306 | |
307 header_ = reinterpret_cast<Header*>(p); | 278 header_ = reinterpret_cast<Header*>(p); |
308 capacity_ = new_capacity; | 279 capacity_after_header_ = new_capacity; |
309 return true; | |
310 } | 280 } |
311 | 281 |
312 // static | 282 // static |
313 const char* Pickle::FindNext(size_t header_size, | 283 const char* Pickle::FindNext(size_t header_size, |
314 const char* start, | 284 const char* start, |
315 const char* end) { | 285 const char* end) { |
316 DCHECK_EQ(header_size, AlignInt(header_size, sizeof(uint32))); | 286 DCHECK_EQ(header_size, AlignInt(header_size, sizeof(uint32))); |
317 DCHECK_LE(header_size, static_cast<size_t>(kPayloadUnit)); | 287 DCHECK_LE(header_size, static_cast<size_t>(kPayloadUnit)); |
318 | 288 |
319 if (static_cast<size_t>(end - start) < sizeof(Header)) | 289 if (static_cast<size_t>(end - start) < sizeof(Header)) |
320 return NULL; | 290 return NULL; |
321 | 291 |
322 const Header* hdr = reinterpret_cast<const Header*>(start); | 292 const Header* hdr = reinterpret_cast<const Header*>(start); |
323 const char* payload_base = start + header_size; | 293 const char* payload_base = start + header_size; |
324 const char* payload_end = payload_base + hdr->payload_size; | 294 const char* payload_end = payload_base + hdr->payload_size; |
325 if (payload_end < payload_base) | 295 if (payload_end < payload_base) |
326 return NULL; | 296 return NULL; |
327 | 297 |
328 return (payload_end > end) ? NULL : payload_end; | 298 return (payload_end > end) ? NULL : payload_end; |
329 } | 299 } |
| 300 |
| 301 template <size_t length> void Pickle::WriteBytesStatic(const void* data) { |
| 302 WriteBytesCommon(data, length); |
| 303 } |
| 304 |
| 305 template void Pickle::WriteBytesStatic<2>(const void* data); |
| 306 template void Pickle::WriteBytesStatic<4>(const void* data); |
| 307 template void Pickle::WriteBytesStatic<8>(const void* data); |
| 308 |
| 309 inline void Pickle::WriteBytesCommon(const void* data, size_t length) { |
| 310 DCHECK_NE(kCapacityReadOnly, capacity_after_header_) |
| 311 << "oops: pickle is readonly"; |
| 312 size_t data_len = AlignInt(length, sizeof(uint32)); |
| 313 DCHECK_GE(data_len, length); |
| 314 #ifdef ARCH_CPU_64_BITS |
| 315 DCHECK_LE(data_len, kuint32max); |
| 316 #endif |
| 317 DCHECK_LE(write_offset_, kuint32max - data_len); |
| 318 size_t new_size = write_offset_ + data_len; |
| 319 if (new_size > capacity_after_header_) { |
| 320 Resize(std::max(capacity_after_header_ * 2, new_size)); |
| 321 } |
| 322 |
| 323 char* write = mutable_payload() + write_offset_; |
| 324 memcpy(write, data, length); |
| 325 memset(write + length, 0, data_len - length); |
| 326 header_->payload_size = static_cast<uint32>(write_offset_ + length); |
| 327 write_offset_ = new_size; |
| 328 } |
OLD | NEW |