| 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 #include <limits> |
| 10 | 11 |
| 11 #include "base/bits.h" | 12 #include "base/bits.h" |
| 12 #include "base/macros.h" | 13 #include "base/macros.h" |
| 13 | 14 |
| 14 namespace base { | 15 namespace base { |
| 15 | 16 |
| 16 // static | 17 // static |
| 17 const int Pickle::kPayloadUnit = 64; | 18 const int Pickle::kPayloadUnit = 64; |
| 18 | 19 |
| 19 static const size_t kCapacityReadOnly = static_cast<size_t>(-1); | 20 static const size_t kCapacityReadOnly = static_cast<size_t>(-1); |
| (...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 331 size_t Pickle::GetTotalAllocatedSize() const { | 332 size_t Pickle::GetTotalAllocatedSize() const { |
| 332 if (capacity_after_header_ == kCapacityReadOnly) | 333 if (capacity_after_header_ == kCapacityReadOnly) |
| 333 return 0; | 334 return 0; |
| 334 return header_size_ + capacity_after_header_; | 335 return header_size_ + capacity_after_header_; |
| 335 } | 336 } |
| 336 | 337 |
| 337 // static | 338 // static |
| 338 const char* Pickle::FindNext(size_t header_size, | 339 const char* Pickle::FindNext(size_t header_size, |
| 339 const char* start, | 340 const char* start, |
| 340 const char* end) { | 341 const char* end) { |
| 342 size_t pickle_size = 0; |
| 343 if (!PeekNext(header_size, start, end, &pickle_size)) |
| 344 return NULL; |
| 345 |
| 346 if (pickle_size > static_cast<size_t>(end - start)) |
| 347 return NULL; |
| 348 |
| 349 return start + pickle_size; |
| 350 } |
| 351 |
| 352 // static |
| 353 bool Pickle::PeekNext(size_t header_size, |
| 354 const char* start, |
| 355 const char* end, |
| 356 size_t* pickle_size) { |
| 341 DCHECK_EQ(header_size, bits::Align(header_size, sizeof(uint32))); | 357 DCHECK_EQ(header_size, bits::Align(header_size, sizeof(uint32))); |
| 358 DCHECK_GE(header_size, sizeof(Header)); |
| 342 DCHECK_LE(header_size, static_cast<size_t>(kPayloadUnit)); | 359 DCHECK_LE(header_size, static_cast<size_t>(kPayloadUnit)); |
| 343 | 360 |
| 344 size_t length = static_cast<size_t>(end - start); | 361 size_t length = static_cast<size_t>(end - start); |
| 345 if (length < sizeof(Header)) | 362 if (length < sizeof(Header)) |
| 346 return NULL; | 363 return false; |
| 347 | 364 |
| 348 const Header* hdr = reinterpret_cast<const Header*>(start); | 365 const Header* hdr = reinterpret_cast<const Header*>(start); |
| 349 if (length < header_size || length - header_size < hdr->payload_size) | 366 if (length < header_size) |
| 350 return NULL; | 367 return false; |
| 351 return start + header_size + hdr->payload_size; | 368 |
| 369 if (hdr->payload_size > std::numeric_limits<size_t>::max() - header_size) { |
| 370 // If payload_size causes an overflow, we return maximum possible |
| 371 // pickle size to indicate that. |
| 372 *pickle_size = std::numeric_limits<size_t>::max(); |
| 373 } else { |
| 374 *pickle_size = header_size + hdr->payload_size; |
| 375 } |
| 376 return true; |
| 352 } | 377 } |
| 353 | 378 |
| 354 template <size_t length> void Pickle::WriteBytesStatic(const void* data) { | 379 template <size_t length> void Pickle::WriteBytesStatic(const void* data) { |
| 355 WriteBytesCommon(data, length); | 380 WriteBytesCommon(data, length); |
| 356 } | 381 } |
| 357 | 382 |
| 358 template void Pickle::WriteBytesStatic<2>(const void* data); | 383 template void Pickle::WriteBytesStatic<2>(const void* data); |
| 359 template void Pickle::WriteBytesStatic<4>(const void* data); | 384 template void Pickle::WriteBytesStatic<4>(const void* data); |
| 360 template void Pickle::WriteBytesStatic<8>(const void* data); | 385 template void Pickle::WriteBytesStatic<8>(const void* data); |
| 361 | 386 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 379 } | 404 } |
| 380 | 405 |
| 381 char* write = mutable_payload() + write_offset_; | 406 char* write = mutable_payload() + write_offset_; |
| 382 memcpy(write, data, length); | 407 memcpy(write, data, length); |
| 383 memset(write + length, 0, data_len - length); | 408 memset(write + length, 0, data_len - length); |
| 384 header_->payload_size = static_cast<uint32>(new_size); | 409 header_->payload_size = static_cast<uint32>(new_size); |
| 385 write_offset_ = new_size; | 410 write_offset_ = new_size; |
| 386 } | 411 } |
| 387 | 412 |
| 388 } // namespace base | 413 } // namespace base |
| OLD | NEW |