| 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_(0) { |
| 157 variable_buffer_offset_(0) { | |
| 158 Resize(kPayloadUnit); | 157 Resize(kPayloadUnit); |
| 159 header_->payload_size = 0; | 158 header_->payload_size = 0; |
| 160 } | 159 } |
| 161 | 160 |
| 162 Pickle::Pickle(int header_size) | 161 Pickle::Pickle(int header_size) |
| 163 : header_(NULL), | 162 : header_(NULL), |
| 164 header_size_(AlignInt(header_size, sizeof(uint32))), | 163 header_size_(AlignInt(header_size, sizeof(uint32))), |
| 165 capacity_(0), | 164 capacity_(0) { |
| 166 variable_buffer_offset_(0) { | |
| 167 DCHECK_GE(static_cast<size_t>(header_size), sizeof(Header)); | 165 DCHECK_GE(static_cast<size_t>(header_size), sizeof(Header)); |
| 168 DCHECK_LE(header_size, kPayloadUnit); | 166 DCHECK_LE(header_size, kPayloadUnit); |
| 169 Resize(kPayloadUnit); | 167 Resize(kPayloadUnit); |
| 170 header_->payload_size = 0; | 168 header_->payload_size = 0; |
| 171 } | 169 } |
| 172 | 170 |
| 173 Pickle::Pickle(const char* data, size_t data_len) | 171 Pickle::Pickle(const char* data, size_t data_len) |
| 174 : header_(reinterpret_cast<Header*>(const_cast<char*>(data))), | 172 : header_(reinterpret_cast<Header*>(const_cast<char*>(data))), |
| 175 header_size_(0), | 173 header_size_(0), |
| 176 capacity_(kCapacityReadOnly), | 174 capacity_(kCapacityReadOnly) { |
| 177 variable_buffer_offset_(0) { | |
| 178 if (data_len >= sizeof(Header)) | 175 if (data_len >= sizeof(Header)) |
| 179 header_size_ = data_len - header_->payload_size; | 176 header_size_ = data_len - header_->payload_size; |
| 180 | 177 |
| 181 if (header_size_ > data_len) | 178 if (header_size_ > data_len) |
| 182 header_size_ = 0; | 179 header_size_ = 0; |
| 183 | 180 |
| 184 if (header_size_ != AlignInt(header_size_, sizeof(uint32))) | 181 if (header_size_ != AlignInt(header_size_, sizeof(uint32))) |
| 185 header_size_ = 0; | 182 header_size_ = 0; |
| 186 | 183 |
| 187 // If there is anything wrong with the data, we're not going to use it. | 184 // If there is anything wrong with the data, we're not going to use it. |
| 188 if (!header_size_) | 185 if (!header_size_) |
| 189 header_ = NULL; | 186 header_ = NULL; |
| 190 } | 187 } |
| 191 | 188 |
| 192 Pickle::Pickle(const Pickle& other) | 189 Pickle::Pickle(const Pickle& other) |
| 193 : header_(NULL), | 190 : header_(NULL), |
| 194 header_size_(other.header_size_), | 191 header_size_(other.header_size_), |
| 195 capacity_(0), | 192 capacity_(0) { |
| 196 variable_buffer_offset_(other.variable_buffer_offset_) { | |
| 197 size_t payload_size = header_size_ + other.header_->payload_size; | 193 size_t payload_size = header_size_ + other.header_->payload_size; |
| 198 bool resized = Resize(payload_size); | 194 bool resized = Resize(payload_size); |
| 199 CHECK(resized); // Realloc failed. | 195 CHECK(resized); // Realloc failed. |
| 200 memcpy(header_, other.header_, payload_size); | 196 memcpy(header_, other.header_, payload_size); |
| 201 } | 197 } |
| 202 | 198 |
| 203 Pickle::~Pickle() { | 199 Pickle::~Pickle() { |
| 204 if (capacity_ != kCapacityReadOnly) | 200 if (capacity_ != kCapacityReadOnly) |
| 205 free(header_); | 201 free(header_); |
| 206 } | 202 } |
| 207 | 203 |
| 208 Pickle& Pickle::operator=(const Pickle& other) { | 204 Pickle& Pickle::operator=(const Pickle& other) { |
| 209 if (this == &other) { | 205 if (this == &other) { |
| 210 NOTREACHED(); | 206 NOTREACHED(); |
| 211 return *this; | 207 return *this; |
| 212 } | 208 } |
| 213 if (capacity_ == kCapacityReadOnly) { | 209 if (capacity_ == kCapacityReadOnly) { |
| 214 header_ = NULL; | 210 header_ = NULL; |
| 215 capacity_ = 0; | 211 capacity_ = 0; |
| 216 } | 212 } |
| 217 if (header_size_ != other.header_size_) { | 213 if (header_size_ != other.header_size_) { |
| 218 free(header_); | 214 free(header_); |
| 219 header_ = NULL; | 215 header_ = NULL; |
| 220 header_size_ = other.header_size_; | 216 header_size_ = other.header_size_; |
| 221 } | 217 } |
| 222 bool resized = Resize(other.header_size_ + other.header_->payload_size); | 218 bool resized = Resize(other.header_size_ + other.header_->payload_size); |
| 223 CHECK(resized); // Realloc failed. | 219 CHECK(resized); // Realloc failed. |
| 224 memcpy(header_, other.header_, | 220 memcpy(header_, other.header_, |
| 225 other.header_size_ + other.header_->payload_size); | 221 other.header_size_ + other.header_->payload_size); |
| 226 variable_buffer_offset_ = other.variable_buffer_offset_; | |
| 227 return *this; | 222 return *this; |
| 228 } | 223 } |
| 229 | 224 |
| 230 bool Pickle::WriteString(const std::string& value) { | 225 bool Pickle::WriteString(const std::string& value) { |
| 231 if (!WriteInt(static_cast<int>(value.size()))) | 226 if (!WriteInt(static_cast<int>(value.size()))) |
| 232 return false; | 227 return false; |
| 233 | 228 |
| 234 return WriteBytes(value.data(), static_cast<int>(value.size())); | 229 return WriteBytes(value.data(), static_cast<int>(value.size())); |
| 235 } | 230 } |
| 236 | 231 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 260 char* dest = BeginWrite(data_len); | 255 char* dest = BeginWrite(data_len); |
| 261 if (!dest) | 256 if (!dest) |
| 262 return false; | 257 return false; |
| 263 | 258 |
| 264 memcpy(dest, data, data_len); | 259 memcpy(dest, data, data_len); |
| 265 | 260 |
| 266 EndWrite(dest, data_len); | 261 EndWrite(dest, data_len); |
| 267 return true; | 262 return true; |
| 268 } | 263 } |
| 269 | 264 |
| 270 char* Pickle::BeginWriteData(int length) { | |
| 271 DCHECK_EQ(variable_buffer_offset_, 0U) << | |
| 272 "There can only be one variable buffer in a Pickle"; | |
| 273 | |
| 274 if (length < 0 || !WriteInt(length)) | |
| 275 return NULL; | |
| 276 | |
| 277 char *data_ptr = BeginWrite(length); | |
| 278 if (!data_ptr) | |
| 279 return NULL; | |
| 280 | |
| 281 variable_buffer_offset_ = | |
| 282 data_ptr - reinterpret_cast<char*>(header_) - sizeof(int); | |
| 283 | |
| 284 // EndWrite doesn't necessarily have to be called after the write operation, | |
| 285 // so we call it here to pad out what the caller will eventually write. | |
| 286 EndWrite(data_ptr, length); | |
| 287 return data_ptr; | |
| 288 } | |
| 289 | |
| 290 void Pickle::TrimWriteData(int new_length) { | |
| 291 DCHECK_NE(variable_buffer_offset_, 0U); | |
| 292 | |
| 293 // Fetch the the variable buffer size | |
| 294 int* cur_length = reinterpret_cast<int*>( | |
| 295 reinterpret_cast<char*>(header_) + variable_buffer_offset_); | |
| 296 | |
| 297 if (new_length < 0 || new_length > *cur_length) { | |
| 298 NOTREACHED() << "Invalid length in TrimWriteData."; | |
| 299 return; | |
| 300 } | |
| 301 | |
| 302 // Update the payload size and variable buffer size | |
| 303 header_->payload_size -= (*cur_length - new_length); | |
| 304 *cur_length = new_length; | |
| 305 } | |
| 306 | |
| 307 void Pickle::Reserve(size_t additional_capacity) { | 265 void Pickle::Reserve(size_t additional_capacity) { |
| 308 // Write at a uint32-aligned offset from the beginning of the header. | 266 // Write at a uint32-aligned offset from the beginning of the header. |
| 309 size_t offset = AlignInt(header_->payload_size, sizeof(uint32)); | 267 size_t offset = AlignInt(header_->payload_size, sizeof(uint32)); |
| 310 | 268 |
| 311 size_t new_size = offset + additional_capacity; | 269 size_t new_size = offset + additional_capacity; |
| 312 size_t needed_size = header_size_ + new_size; | 270 size_t needed_size = header_size_ + new_size; |
| 313 if (needed_size > capacity_) | 271 if (needed_size > capacity_) |
| 314 Resize(capacity_ * 2 + needed_size); | 272 Resize(capacity_ * 2 + needed_size); |
| 315 } | 273 } |
| 316 | 274 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 362 return NULL; | 320 return NULL; |
| 363 | 321 |
| 364 const Header* hdr = reinterpret_cast<const Header*>(start); | 322 const Header* hdr = reinterpret_cast<const Header*>(start); |
| 365 const char* payload_base = start + header_size; | 323 const char* payload_base = start + header_size; |
| 366 const char* payload_end = payload_base + hdr->payload_size; | 324 const char* payload_end = payload_base + hdr->payload_size; |
| 367 if (payload_end < payload_base) | 325 if (payload_end < payload_base) |
| 368 return NULL; | 326 return NULL; |
| 369 | 327 |
| 370 return (payload_end > end) ? NULL : payload_end; | 328 return (payload_end > end) ? NULL : payload_end; |
| 371 } | 329 } |
| OLD | NEW |