[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 5 | #include "base/pickle.h" |
| 6 | |
[email protected] | c9046af | 2008-08-06 20:35:17 | [diff] [blame] | 7 | #include <stdlib.h> |
| 8 | |
[email protected] | b7a5d99 | 2009-10-28 04:21:01 | [diff] [blame] | 9 | #include <algorithm> // for max() |
dskiba | 6f3790a | 2015-09-30 17:24:30 | [diff] [blame] | 10 | #include <limits> |
[email protected] | c9046af | 2008-08-06 20:35:17 | [diff] [blame] | 11 | |
primiano | 32a7f50 | 2015-07-24 20:13:32 | [diff] [blame] | 12 | #include "base/bits.h" |
| 13 | #include "base/macros.h" |
jam | 03d8a78 | 2016-02-10 20:13:39 | [diff] [blame] | 14 | #include "base/numerics/safe_conversions.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 15 | #include "build/build_config.h" |
primiano | 32a7f50 | 2015-07-24 20:13:32 | [diff] [blame] | 16 | |
brettw | a89dd42 | 2015-06-03 16:20:14 | [diff] [blame] | 17 | namespace base { |
[email protected] | 476dafb | 2013-12-03 00:39:26 | [diff] [blame] | 18 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 19 | // static |
| 20 | const int Pickle::kPayloadUnit = 64; |
| 21 | |
[email protected] | 9193d2f | 2011-10-10 22:20:33 | [diff] [blame] | 22 | static const size_t kCapacityReadOnly = static_cast<size_t>(-1); |
[email protected] | 836061b | 2008-08-13 14:57:51 | [diff] [blame] | 23 | |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 24 | PickleIterator::PickleIterator(const Pickle& pickle) |
[email protected] | a15016f | 2014-06-02 23:23:49 | [diff] [blame] | 25 | : payload_(pickle.payload()), |
| 26 | read_index_(0), |
| 27 | end_index_(pickle.payload_size()) { |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | template <typename Type> |
| 31 | inline bool PickleIterator::ReadBuiltinType(Type* result) { |
| 32 | const char* read_from = GetReadPointerAndAdvance<Type>(); |
| 33 | if (!read_from) |
| 34 | return false; |
avi | c027914 | 2015-12-04 22:38:52 | [diff] [blame] | 35 | if (sizeof(Type) > sizeof(uint32_t)) |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 36 | memcpy(result, read_from, sizeof(*result)); |
| 37 | else |
| 38 | *result = *reinterpret_cast<const Type*>(read_from); |
| 39 | return true; |
| 40 | } |
| 41 | |
[email protected] | a15016f | 2014-06-02 23:23:49 | [diff] [blame] | 42 | inline void PickleIterator::Advance(size_t size) { |
primiano | 32a7f50 | 2015-07-24 20:13:32 | [diff] [blame] | 43 | size_t aligned_size = bits::Align(size, sizeof(uint32_t)); |
[email protected] | a15016f | 2014-06-02 23:23:49 | [diff] [blame] | 44 | if (end_index_ - read_index_ < aligned_size) { |
| 45 | read_index_ = end_index_; |
| 46 | } else { |
| 47 | read_index_ += aligned_size; |
| 48 | } |
| 49 | } |
| 50 | |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 51 | template<typename Type> |
| 52 | inline const char* PickleIterator::GetReadPointerAndAdvance() { |
[email protected] | a15016f | 2014-06-02 23:23:49 | [diff] [blame] | 53 | if (sizeof(Type) > end_index_ - read_index_) { |
| 54 | read_index_ = end_index_; |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 55 | return NULL; |
[email protected] | a15016f | 2014-06-02 23:23:49 | [diff] [blame] | 56 | } |
| 57 | const char* current_read_ptr = payload_ + read_index_; |
| 58 | Advance(sizeof(Type)); |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 59 | return current_read_ptr; |
| 60 | } |
| 61 | |
| 62 | const char* PickleIterator::GetReadPointerAndAdvance(int num_bytes) { |
[email protected] | a15016f | 2014-06-02 23:23:49 | [diff] [blame] | 63 | if (num_bytes < 0 || |
| 64 | end_index_ - read_index_ < static_cast<size_t>(num_bytes)) { |
| 65 | read_index_ = end_index_; |
[email protected] | 2d93605 | 2012-03-13 17:17:56 | [diff] [blame] | 66 | return NULL; |
[email protected] | a15016f | 2014-06-02 23:23:49 | [diff] [blame] | 67 | } |
| 68 | const char* current_read_ptr = payload_ + read_index_; |
| 69 | Advance(num_bytes); |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 70 | return current_read_ptr; |
| 71 | } |
| 72 | |
[email protected] | a15016f | 2014-06-02 23:23:49 | [diff] [blame] | 73 | inline const char* PickleIterator::GetReadPointerAndAdvance( |
| 74 | int num_elements, |
| 75 | size_t size_element) { |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 76 | // Check for int32_t overflow. |
avi | c027914 | 2015-12-04 22:38:52 | [diff] [blame] | 77 | int64_t num_bytes = static_cast<int64_t>(num_elements) * size_element; |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 78 | int num_bytes32 = static_cast<int>(num_bytes); |
avi | c027914 | 2015-12-04 22:38:52 | [diff] [blame] | 79 | if (num_bytes != static_cast<int64_t>(num_bytes32)) |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 80 | return NULL; |
| 81 | return GetReadPointerAndAdvance(num_bytes32); |
| 82 | } |
| 83 | |
| 84 | bool PickleIterator::ReadBool(bool* result) { |
| 85 | return ReadBuiltinType(result); |
| 86 | } |
| 87 | |
| 88 | bool PickleIterator::ReadInt(int* result) { |
| 89 | return ReadBuiltinType(result); |
| 90 | } |
| 91 | |
| 92 | bool PickleIterator::ReadLong(long* result) { |
jam | 03d8a78 | 2016-02-10 20:13:39 | [diff] [blame] | 93 | // Always read long as a 64-bit value to ensure compatibility between 32-bit |
| 94 | // and 64-bit processes. |
| 95 | int64_t result_int64 = 0; |
| 96 | if (!ReadBuiltinType(&result_int64)) |
| 97 | return false; |
| 98 | // CHECK if the cast truncates the value so that we know to change this IPC |
| 99 | // parameter to use int64_t. |
| 100 | *result = base::checked_cast<long>(result_int64); |
| 101 | return true; |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 102 | } |
| 103 | |
avi | c027914 | 2015-12-04 22:38:52 | [diff] [blame] | 104 | bool PickleIterator::ReadUInt16(uint16_t* result) { |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 105 | return ReadBuiltinType(result); |
| 106 | } |
| 107 | |
avi | c027914 | 2015-12-04 22:38:52 | [diff] [blame] | 108 | bool PickleIterator::ReadUInt32(uint32_t* result) { |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 109 | return ReadBuiltinType(result); |
| 110 | } |
| 111 | |
avi | c027914 | 2015-12-04 22:38:52 | [diff] [blame] | 112 | bool PickleIterator::ReadInt64(int64_t* result) { |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 113 | return ReadBuiltinType(result); |
| 114 | } |
| 115 | |
avi | c027914 | 2015-12-04 22:38:52 | [diff] [blame] | 116 | bool PickleIterator::ReadUInt64(uint64_t* result) { |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 117 | return ReadBuiltinType(result); |
| 118 | } |
| 119 | |
[email protected] | b1f61b03 | 2012-11-28 15:40:58 | [diff] [blame] | 120 | bool PickleIterator::ReadFloat(float* result) { |
[email protected] | 522fbea | 2013-11-18 00:50:25 | [diff] [blame] | 121 | // crbug.com/315213 |
| 122 | // The source data may not be properly aligned, and unaligned float reads |
| 123 | // cause SIGBUS on some ARM platforms, so force using memcpy to copy the data |
| 124 | // into the result. |
| 125 | const char* read_from = GetReadPointerAndAdvance<float>(); |
| 126 | if (!read_from) |
| 127 | return false; |
| 128 | memcpy(result, read_from, sizeof(*result)); |
| 129 | return true; |
[email protected] | b1f61b03 | 2012-11-28 15:40:58 | [diff] [blame] | 130 | } |
| 131 | |
[email protected] | 915cc7d | 2014-07-14 22:50:32 | [diff] [blame] | 132 | bool PickleIterator::ReadDouble(double* result) { |
| 133 | // crbug.com/315213 |
| 134 | // The source data may not be properly aligned, and unaligned double reads |
| 135 | // cause SIGBUS on some ARM platforms, so force using memcpy to copy the data |
| 136 | // into the result. |
| 137 | const char* read_from = GetReadPointerAndAdvance<double>(); |
| 138 | if (!read_from) |
| 139 | return false; |
| 140 | memcpy(result, read_from, sizeof(*result)); |
| 141 | return true; |
| 142 | } |
| 143 | |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 144 | bool PickleIterator::ReadString(std::string* result) { |
| 145 | int len; |
| 146 | if (!ReadInt(&len)) |
| 147 | return false; |
| 148 | const char* read_from = GetReadPointerAndAdvance(len); |
| 149 | if (!read_from) |
| 150 | return false; |
| 151 | |
| 152 | result->assign(read_from, len); |
| 153 | return true; |
| 154 | } |
| 155 | |
brettw | a89dd42 | 2015-06-03 16:20:14 | [diff] [blame] | 156 | bool PickleIterator::ReadStringPiece(StringPiece* result) { |
brucedawson | eaa3896 | 2015-03-10 01:46:50 | [diff] [blame] | 157 | int len; |
| 158 | if (!ReadInt(&len)) |
| 159 | return false; |
| 160 | const char* read_from = GetReadPointerAndAdvance(len); |
| 161 | if (!read_from) |
| 162 | return false; |
| 163 | |
brettw | a89dd42 | 2015-06-03 16:20:14 | [diff] [blame] | 164 | *result = StringPiece(read_from, len); |
brucedawson | eaa3896 | 2015-03-10 01:46:50 | [diff] [blame] | 165 | return true; |
| 166 | } |
| 167 | |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 168 | bool PickleIterator::ReadString16(string16* result) { |
| 169 | int len; |
| 170 | if (!ReadInt(&len)) |
| 171 | return false; |
| 172 | const char* read_from = GetReadPointerAndAdvance(len, sizeof(char16)); |
| 173 | if (!read_from) |
| 174 | return false; |
| 175 | |
| 176 | result->assign(reinterpret_cast<const char16*>(read_from), len); |
| 177 | return true; |
| 178 | } |
| 179 | |
brettw | a89dd42 | 2015-06-03 16:20:14 | [diff] [blame] | 180 | bool PickleIterator::ReadStringPiece16(StringPiece16* result) { |
brucedawson | eaa3896 | 2015-03-10 01:46:50 | [diff] [blame] | 181 | int len; |
| 182 | if (!ReadInt(&len)) |
| 183 | return false; |
| 184 | const char* read_from = GetReadPointerAndAdvance(len, sizeof(char16)); |
| 185 | if (!read_from) |
| 186 | return false; |
| 187 | |
brettw | a89dd42 | 2015-06-03 16:20:14 | [diff] [blame] | 188 | *result = StringPiece16(reinterpret_cast<const char16*>(read_from), len); |
brucedawson | eaa3896 | 2015-03-10 01:46:50 | [diff] [blame] | 189 | return true; |
| 190 | } |
| 191 | |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 192 | bool PickleIterator::ReadData(const char** data, int* length) { |
| 193 | *length = 0; |
| 194 | *data = 0; |
| 195 | |
| 196 | if (!ReadInt(length)) |
| 197 | return false; |
| 198 | |
| 199 | return ReadBytes(data, *length); |
| 200 | } |
| 201 | |
| 202 | bool PickleIterator::ReadBytes(const char** data, int length) { |
| 203 | const char* read_from = GetReadPointerAndAdvance(length); |
| 204 | if (!read_from) |
| 205 | return false; |
| 206 | *data = read_from; |
| 207 | return true; |
| 208 | } |
| 209 | |
rockot | 0457af10 | 2016-02-05 02:12:32 | [diff] [blame] | 210 | PickleSizer::PickleSizer() {} |
| 211 | |
| 212 | PickleSizer::~PickleSizer() {} |
| 213 | |
| 214 | void PickleSizer::AddString(const StringPiece& value) { |
| 215 | AddInt(); |
| 216 | AddBytes(static_cast<int>(value.size())); |
| 217 | } |
| 218 | |
| 219 | void PickleSizer::AddString16(const StringPiece16& value) { |
| 220 | AddInt(); |
| 221 | AddBytes(static_cast<int>(value.size() * sizeof(char16))); |
| 222 | } |
| 223 | |
| 224 | void PickleSizer::AddData(int length) { |
| 225 | CHECK_GE(length, 0); |
| 226 | AddInt(); |
| 227 | AddBytes(length); |
| 228 | } |
| 229 | |
| 230 | void PickleSizer::AddBytes(int length) { |
| 231 | payload_size_ += bits::Align(length, sizeof(uint32_t)); |
| 232 | } |
| 233 | |
jam | 45eceef | 2016-05-11 21:05:05 | [diff] [blame] | 234 | void PickleSizer::AddAttachment() { |
| 235 | // From IPC::Message::WriteAttachment |
| 236 | AddBool(); |
| 237 | AddInt(); |
| 238 | } |
| 239 | |
rockot | 0457af10 | 2016-02-05 02:12:32 | [diff] [blame] | 240 | template <size_t length> void PickleSizer::AddBytesStatic() { |
| 241 | DCHECK_LE(length, static_cast<size_t>(std::numeric_limits<int>::max())); |
| 242 | AddBytes(length); |
| 243 | } |
| 244 | |
| 245 | template void PickleSizer::AddBytesStatic<2>(); |
| 246 | template void PickleSizer::AddBytesStatic<4>(); |
| 247 | template void PickleSizer::AddBytesStatic<8>(); |
| 248 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 249 | Pickle::Attachment::Attachment() {} |
| 250 | |
| 251 | Pickle::Attachment::~Attachment() {} |
| 252 | |
avi | c027914 | 2015-12-04 22:38:52 | [diff] [blame] | 253 | // Payload is uint32_t aligned. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 254 | |
| 255 | Pickle::Pickle() |
| 256 | : header_(NULL), |
| 257 | header_size_(sizeof(Header)), |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 258 | capacity_after_header_(0), |
| 259 | write_offset_(0) { |
primiano | 32a7f50 | 2015-07-24 20:13:32 | [diff] [blame] | 260 | static_assert((Pickle::kPayloadUnit & (Pickle::kPayloadUnit - 1)) == 0, |
| 261 | "Pickle::kPayloadUnit must be a power of two"); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 262 | Resize(kPayloadUnit); |
| 263 | header_->payload_size = 0; |
| 264 | } |
| 265 | |
| 266 | Pickle::Pickle(int header_size) |
| 267 | : header_(NULL), |
avi | c027914 | 2015-12-04 22:38:52 | [diff] [blame] | 268 | header_size_(bits::Align(header_size, sizeof(uint32_t))), |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 269 | capacity_after_header_(0), |
| 270 | write_offset_(0) { |
[email protected] | 8e03fec | 2011-03-31 20:34:25 | [diff] [blame] | 271 | DCHECK_GE(static_cast<size_t>(header_size), sizeof(Header)); |
[email protected] | d7a93ad | 2011-04-22 13:13:07 | [diff] [blame] | 272 | DCHECK_LE(header_size, kPayloadUnit); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 273 | Resize(kPayloadUnit); |
| 274 | header_->payload_size = 0; |
| 275 | } |
| 276 | |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 277 | Pickle::Pickle(const char* data, int data_len) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 278 | : header_(reinterpret_cast<Header*>(const_cast<char*>(data))), |
[email protected] | d87f8e6f | 2010-11-15 19:31:23 | [diff] [blame] | 279 | header_size_(0), |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 280 | capacity_after_header_(kCapacityReadOnly), |
| 281 | write_offset_(0) { |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 282 | if (data_len >= static_cast<int>(sizeof(Header))) |
[email protected] | d87f8e6f | 2010-11-15 19:31:23 | [diff] [blame] | 283 | header_size_ = data_len - header_->payload_size; |
| 284 | |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 285 | if (header_size_ > static_cast<unsigned int>(data_len)) |
[email protected] | d87f8e6f | 2010-11-15 19:31:23 | [diff] [blame] | 286 | header_size_ = 0; |
| 287 | |
avi | c027914 | 2015-12-04 22:38:52 | [diff] [blame] | 288 | if (header_size_ != bits::Align(header_size_, sizeof(uint32_t))) |
[email protected] | d87f8e6f | 2010-11-15 19:31:23 | [diff] [blame] | 289 | header_size_ = 0; |
| 290 | |
| 291 | // If there is anything wrong with the data, we're not going to use it. |
| 292 | if (!header_size_) |
| 293 | header_ = NULL; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | Pickle::Pickle(const Pickle& other) |
| 297 | : header_(NULL), |
| 298 | header_size_(other.header_size_), |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 299 | capacity_after_header_(0), |
| 300 | write_offset_(other.write_offset_) { |
erikchen | f9ca8f5f0 | 2015-09-08 23:36:29 | [diff] [blame] | 301 | Resize(other.header_->payload_size); |
| 302 | memcpy(header_, other.header_, header_size_ + other.header_->payload_size); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | Pickle::~Pickle() { |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 306 | if (capacity_after_header_ != kCapacityReadOnly) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 307 | free(header_); |
| 308 | } |
| 309 | |
| 310 | Pickle& Pickle::operator=(const Pickle& other) { |
[email protected] | b944f61 | 2009-08-07 23:13:35 | [diff] [blame] | 311 | if (this == &other) { |
| 312 | NOTREACHED(); |
| 313 | return *this; |
| 314 | } |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 315 | if (capacity_after_header_ == kCapacityReadOnly) { |
[email protected] | fb6ec999 | 2009-08-03 07:01:47 | [diff] [blame] | 316 | header_ = NULL; |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 317 | capacity_after_header_ = 0; |
[email protected] | fb6ec999 | 2009-08-03 07:01:47 | [diff] [blame] | 318 | } |
| 319 | if (header_size_ != other.header_size_) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 320 | free(header_); |
| 321 | header_ = NULL; |
| 322 | header_size_ = other.header_size_; |
| 323 | } |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 324 | Resize(other.header_->payload_size); |
[email protected] | b944f61 | 2009-08-07 23:13:35 | [diff] [blame] | 325 | memcpy(header_, other.header_, |
| 326 | other.header_size_ + other.header_->payload_size); |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 327 | write_offset_ = other.write_offset_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 328 | return *this; |
| 329 | } |
| 330 | |
brettw | a89dd42 | 2015-06-03 16:20:14 | [diff] [blame] | 331 | bool Pickle::WriteString(const StringPiece& value) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 332 | if (!WriteInt(static_cast<int>(value.size()))) |
| 333 | return false; |
| 334 | |
| 335 | return WriteBytes(value.data(), static_cast<int>(value.size())); |
| 336 | } |
| 337 | |
brettw | a89dd42 | 2015-06-03 16:20:14 | [diff] [blame] | 338 | bool Pickle::WriteString16(const StringPiece16& value) { |
[email protected] | 3a2a5d2 | 2009-03-04 03:36:36 | [diff] [blame] | 339 | if (!WriteInt(static_cast<int>(value.size()))) |
| 340 | return false; |
| 341 | |
| 342 | return WriteBytes(value.data(), |
| 343 | static_cast<int>(value.size()) * sizeof(char16)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | bool Pickle::WriteData(const char* data, int length) { |
[email protected] | e64ff5e | 2009-07-28 21:00:03 | [diff] [blame] | 347 | return length >= 0 && WriteInt(length) && WriteBytes(data, length); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 348 | } |
| 349 | |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 350 | bool Pickle::WriteBytes(const void* data, int length) { |
| 351 | WriteBytesCommon(data, length); |
[email protected] | 9989c9bb | 2011-01-07 20:23:43 | [diff] [blame] | 352 | return true; |
| 353 | } |
| 354 | |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 355 | void Pickle::Reserve(size_t length) { |
avi | c027914 | 2015-12-04 22:38:52 | [diff] [blame] | 356 | size_t data_len = bits::Align(length, sizeof(uint32_t)); |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 357 | DCHECK_GE(data_len, length); |
[email protected] | 9989c9bb | 2011-01-07 20:23:43 | [diff] [blame] | 358 | #ifdef ARCH_CPU_64_BITS |
avi | c027914 | 2015-12-04 22:38:52 | [diff] [blame] | 359 | DCHECK_LE(data_len, std::numeric_limits<uint32_t>::max()); |
[email protected] | 9989c9bb | 2011-01-07 20:23:43 | [diff] [blame] | 360 | #endif |
avi | c027914 | 2015-12-04 22:38:52 | [diff] [blame] | 361 | DCHECK_LE(write_offset_, std::numeric_limits<uint32_t>::max() - data_len); |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 362 | size_t new_size = write_offset_ + data_len; |
| 363 | if (new_size > capacity_after_header_) |
| 364 | Resize(capacity_after_header_ * 2 + new_size); |
[email protected] | 9989c9bb | 2011-01-07 20:23:43 | [diff] [blame] | 365 | } |
| 366 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 367 | bool Pickle::WriteAttachment(scoped_refptr<Attachment> attachment) { |
| 368 | return false; |
| 369 | } |
| 370 | |
| 371 | bool Pickle::ReadAttachment(base::PickleIterator* iter, |
| 372 | scoped_refptr<Attachment>* attachment) const { |
| 373 | return false; |
| 374 | } |
| 375 | |
| 376 | bool Pickle::HasAttachments() const { |
| 377 | return false; |
| 378 | } |
| 379 | |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 380 | void Pickle::Resize(size_t new_capacity) { |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 381 | CHECK_NE(capacity_after_header_, kCapacityReadOnly); |
primiano | 32a7f50 | 2015-07-24 20:13:32 | [diff] [blame] | 382 | capacity_after_header_ = bits::Align(new_capacity, kPayloadUnit); |
primiano | 9882cf34 | 2015-06-11 21:40:10 | [diff] [blame] | 383 | void* p = realloc(header_, GetTotalAllocatedSize()); |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 384 | CHECK(p); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 385 | header_ = reinterpret_cast<Header*>(p); |
primiano | 9882cf34 | 2015-06-11 21:40:10 | [diff] [blame] | 386 | } |
| 387 | |
rockot | 0776a50 | 2015-12-17 06:19:49 | [diff] [blame] | 388 | void* Pickle::ClaimBytes(size_t num_bytes) { |
| 389 | void* p = ClaimUninitializedBytesInternal(num_bytes); |
| 390 | CHECK(p); |
| 391 | memset(p, 0, num_bytes); |
| 392 | return p; |
| 393 | } |
| 394 | |
primiano | 9882cf34 | 2015-06-11 21:40:10 | [diff] [blame] | 395 | size_t Pickle::GetTotalAllocatedSize() const { |
| 396 | if (capacity_after_header_ == kCapacityReadOnly) |
| 397 | return 0; |
| 398 | return header_size_ + capacity_after_header_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | // static |
| 402 | const char* Pickle::FindNext(size_t header_size, |
| 403 | const char* start, |
| 404 | const char* end) { |
dskiba | 6f3790a | 2015-09-30 17:24:30 | [diff] [blame] | 405 | size_t pickle_size = 0; |
| 406 | if (!PeekNext(header_size, start, end, &pickle_size)) |
| 407 | return NULL; |
| 408 | |
| 409 | if (pickle_size > static_cast<size_t>(end - start)) |
| 410 | return NULL; |
| 411 | |
| 412 | return start + pickle_size; |
| 413 | } |
| 414 | |
| 415 | // static |
| 416 | bool Pickle::PeekNext(size_t header_size, |
| 417 | const char* start, |
| 418 | const char* end, |
| 419 | size_t* pickle_size) { |
avi | c027914 | 2015-12-04 22:38:52 | [diff] [blame] | 420 | DCHECK_EQ(header_size, bits::Align(header_size, sizeof(uint32_t))); |
dskiba | 6f3790a | 2015-09-30 17:24:30 | [diff] [blame] | 421 | DCHECK_GE(header_size, sizeof(Header)); |
[email protected] | d7a93ad | 2011-04-22 13:13:07 | [diff] [blame] | 422 | DCHECK_LE(header_size, static_cast<size_t>(kPayloadUnit)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 423 | |
[email protected] | 33a38dd | 2013-11-01 09:06:26 | [diff] [blame] | 424 | size_t length = static_cast<size_t>(end - start); |
| 425 | if (length < sizeof(Header)) |
dskiba | 6f3790a | 2015-09-30 17:24:30 | [diff] [blame] | 426 | return false; |
[email protected] | 137d237 | 2011-01-26 13:02:27 | [diff] [blame] | 427 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 428 | const Header* hdr = reinterpret_cast<const Header*>(start); |
dskiba | 6f3790a | 2015-09-30 17:24:30 | [diff] [blame] | 429 | if (length < header_size) |
| 430 | return false; |
| 431 | |
| 432 | if (hdr->payload_size > std::numeric_limits<size_t>::max() - header_size) { |
| 433 | // If payload_size causes an overflow, we return maximum possible |
| 434 | // pickle size to indicate that. |
| 435 | *pickle_size = std::numeric_limits<size_t>::max(); |
| 436 | } else { |
| 437 | *pickle_size = header_size + hdr->payload_size; |
| 438 | } |
| 439 | return true; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 440 | } |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 441 | |
| 442 | template <size_t length> void Pickle::WriteBytesStatic(const void* data) { |
| 443 | WriteBytesCommon(data, length); |
| 444 | } |
| 445 | |
| 446 | template void Pickle::WriteBytesStatic<2>(const void* data); |
| 447 | template void Pickle::WriteBytesStatic<4>(const void* data); |
| 448 | template void Pickle::WriteBytesStatic<8>(const void* data); |
| 449 | |
rockot | 0776a50 | 2015-12-17 06:19:49 | [diff] [blame] | 450 | inline void* Pickle::ClaimUninitializedBytesInternal(size_t length) { |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 451 | DCHECK_NE(kCapacityReadOnly, capacity_after_header_) |
| 452 | << "oops: pickle is readonly"; |
avi | c027914 | 2015-12-04 22:38:52 | [diff] [blame] | 453 | size_t data_len = bits::Align(length, sizeof(uint32_t)); |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 454 | DCHECK_GE(data_len, length); |
| 455 | #ifdef ARCH_CPU_64_BITS |
avi | c027914 | 2015-12-04 22:38:52 | [diff] [blame] | 456 | DCHECK_LE(data_len, std::numeric_limits<uint32_t>::max()); |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 457 | #endif |
avi | c027914 | 2015-12-04 22:38:52 | [diff] [blame] | 458 | DCHECK_LE(write_offset_, std::numeric_limits<uint32_t>::max() - data_len); |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 459 | size_t new_size = write_offset_ + data_len; |
| 460 | if (new_size > capacity_after_header_) { |
primiano | 32a7f50 | 2015-07-24 20:13:32 | [diff] [blame] | 461 | size_t new_capacity = capacity_after_header_ * 2; |
| 462 | const size_t kPickleHeapAlign = 4096; |
| 463 | if (new_capacity > kPickleHeapAlign) |
| 464 | new_capacity = bits::Align(new_capacity, kPickleHeapAlign) - kPayloadUnit; |
| 465 | Resize(std::max(new_capacity, new_size)); |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | char* write = mutable_payload() + write_offset_; |
rockot | 0776a50 | 2015-12-17 06:19:49 | [diff] [blame] | 469 | memset(write + length, 0, data_len - length); // Always initialize padding |
avi | c027914 | 2015-12-04 22:38:52 | [diff] [blame] | 470 | header_->payload_size = static_cast<uint32_t>(new_size); |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 471 | write_offset_ = new_size; |
rockot | 0776a50 | 2015-12-17 06:19:49 | [diff] [blame] | 472 | return write; |
| 473 | } |
| 474 | |
| 475 | inline void Pickle::WriteBytesCommon(const void* data, size_t length) { |
| 476 | DCHECK_NE(kCapacityReadOnly, capacity_after_header_) |
| 477 | << "oops: pickle is readonly"; |
| 478 | MSAN_CHECK_MEM_IS_INITIALIZED(data, length); |
| 479 | void* write = ClaimUninitializedBytesInternal(length); |
| 480 | memcpy(write, data, length); |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 481 | } |
brettw | a89dd42 | 2015-06-03 16:20:14 | [diff] [blame] | 482 | |
| 483 | } // namespace base |