| 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 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_(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, int data_len) | 173 Pickle::Pickle(const char* data, int 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_(kCapacityReadOnly), |
| 177 write_offset_(0) { |
| 175 if (data_len >= static_cast<int>(sizeof(Header))) | 178 if (data_len >= static_cast<int>(sizeof(Header))) |
| 176 header_size_ = data_len - header_->payload_size; | 179 header_size_ = data_len - header_->payload_size; |
| 177 | 180 |
| 178 if (header_size_ > static_cast<unsigned int>(data_len)) | 181 if (header_size_ > static_cast<unsigned int>(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_(0), |
| 193 size_t payload_size = header_size_ + other.header_->payload_size; | 196 write_offset_(other.write_offset_) { |
| 194 Resize(payload_size); | 197 Resize(other.header_->payload_size); |
| 195 memcpy(header_, other.header_, payload_size); | 198 memcpy(header_, other.header_, header_size_ + other.header_->payload_size); |
| 196 } | 199 } |
| 197 | 200 |
| 198 Pickle::~Pickle() { | 201 Pickle::~Pickle() { |
| 199 if (capacity_ != kCapacityReadOnly) | 202 if (capacity_ != kCapacityReadOnly) |
| 200 free(header_); | 203 free(header_); |
| 201 } | 204 } |
| 202 | 205 |
| 203 Pickle& Pickle::operator=(const Pickle& other) { | 206 Pickle& Pickle::operator=(const Pickle& other) { |
| 204 if (this == &other) { | 207 if (this == &other) { |
| 205 NOTREACHED(); | 208 NOTREACHED(); |
| 206 return *this; | 209 return *this; |
| 207 } | 210 } |
| 208 if (capacity_ == kCapacityReadOnly) { | 211 if (capacity_ == kCapacityReadOnly) { |
| 209 header_ = NULL; | 212 header_ = NULL; |
| 210 capacity_ = 0; | 213 capacity_ = 0; |
| 211 } | 214 } |
| 212 if (header_size_ != other.header_size_) { | 215 if (header_size_ != other.header_size_) { |
| 213 free(header_); | 216 free(header_); |
| 214 header_ = NULL; | 217 header_ = NULL; |
| 215 header_size_ = other.header_size_; | 218 header_size_ = other.header_size_; |
| 216 } | 219 } |
| 217 Resize(other.header_size_ + other.header_->payload_size); | 220 Resize(other.header_->payload_size); |
| 218 memcpy(header_, other.header_, | 221 memcpy(header_, other.header_, |
| 219 other.header_size_ + other.header_->payload_size); | 222 other.header_size_ + other.header_->payload_size); |
| 223 write_offset_ = other.write_offset_; |
| 220 return *this; | 224 return *this; |
| 221 } | 225 } |
| 222 | 226 |
| 223 bool Pickle::WriteString(const std::string& value) { | 227 bool Pickle::WriteString(const std::string& value) { |
| 224 if (!WriteInt(static_cast<int>(value.size()))) | 228 if (!WriteInt(static_cast<int>(value.size()))) |
| 225 return false; | 229 return false; |
| 226 | 230 |
| 227 return WriteBytes(value.data(), static_cast<int>(value.size())); | 231 return WriteBytes(value.data(), static_cast<int>(value.size())); |
| 228 } | 232 } |
| 229 | 233 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 240 return false; | 244 return false; |
| 241 | 245 |
| 242 return WriteBytes(value.data(), | 246 return WriteBytes(value.data(), |
| 243 static_cast<int>(value.size()) * sizeof(char16)); | 247 static_cast<int>(value.size()) * sizeof(char16)); |
| 244 } | 248 } |
| 245 | 249 |
| 246 bool Pickle::WriteData(const char* data, int length) { | 250 bool Pickle::WriteData(const char* data, int length) { |
| 247 return length >= 0 && WriteInt(length) && WriteBytes(data, length); | 251 return length >= 0 && WriteInt(length) && WriteBytes(data, length); |
| 248 } | 252 } |
| 249 | 253 |
| 250 bool Pickle::WriteBytes(const void* data, int data_len) { | 254 inline void Pickle::WriteBytesCommon(const void* data, size_t length) { |
| 251 DCHECK_NE(kCapacityReadOnly, capacity_) << "oops: pickle is readonly"; | 255 DCHECK_NE(kCapacityReadOnly, capacity_) << "oops: pickle is readonly"; |
| 256 size_t data_len = AlignInt(length, sizeof(uint32)); |
| 257 DCHECK_GE(data_len, length); |
| 258 #ifdef ARCH_CPU_64_BITS |
| 259 DCHECK_LE(data_len, kuint32max); |
| 260 #endif |
| 261 DCHECK_LE(write_offset_, kuint32max - data_len); |
| 262 size_t new_size = write_offset_ + data_len; |
| 263 if (new_size > capacity_) { |
| 264 Resize(std::max(capacity_ * 2, new_size)); |
| 265 } |
| 252 | 266 |
| 253 char* dest = BeginWrite(data_len); | 267 char* write = mutable_payload() + write_offset_; |
| 254 if (!dest) | 268 memcpy(write, data, length); |
| 255 return false; | 269 memset(write + length, 0, data_len - length); |
| 270 header_->payload_size = write_offset_ + length; |
| 271 write_offset_ = new_size; |
| 272 } |
| 256 | 273 |
| 257 memcpy(dest, data, data_len); | 274 bool Pickle::WriteBytes(const void* data, int length) { |
| 258 | 275 WriteBytesCommon(data, length); |
| 259 EndWrite(dest, data_len); | |
| 260 return true; | 276 return true; |
| 261 } | 277 } |
| 262 | 278 |
| 263 void Pickle::Reserve(size_t length) { | 279 template <size_t length> void Pickle::WriteBytesStatic(const void* data) { |
| 264 // write at a uint32-aligned offset from the beginning of the header | 280 WriteBytesCommon(data, length); |
| 265 size_t offset = AlignInt(header_->payload_size, sizeof(uint32)); | |
| 266 | |
| 267 size_t new_size = offset + length; | |
| 268 size_t needed_size = header_size_ + new_size; | |
| 269 if (needed_size > capacity_) | |
| 270 Resize(capacity_ * 2 + needed_size); | |
| 271 } | 281 } |
| 272 | 282 |
| 273 char* Pickle::BeginWrite(size_t length) { | 283 template void Pickle::WriteBytesStatic<2>(const void* data); |
| 274 // write at a uint32-aligned offset from the beginning of the header | 284 template void Pickle::WriteBytesStatic<4>(const void* data); |
| 275 size_t offset = AlignInt(header_->payload_size, sizeof(uint32)); | 285 template void Pickle::WriteBytesStatic<8>(const void* data); |
| 276 | 286 |
| 277 size_t new_size = offset + length; | 287 void Pickle::Reserve(size_t length) { |
| 278 size_t needed_size = header_size_ + new_size; | 288 size_t data_len = AlignInt(length, sizeof(uint32)); |
| 279 if (needed_size > capacity_) | 289 DCHECK_GE(data_len, length); |
| 280 Resize(std::max(capacity_ * 2, needed_size)); | |
| 281 | |
| 282 #ifdef ARCH_CPU_64_BITS | 290 #ifdef ARCH_CPU_64_BITS |
| 283 DCHECK_LE(length, kuint32max); | 291 DCHECK_LE(data_len, kuint32max); |
| 284 #endif | 292 #endif |
| 285 | 293 DCHECK_LE(write_offset_, kuint32max - data_len); |
| 286 header_->payload_size = static_cast<uint32>(new_size); | 294 size_t new_size = write_offset_ + data_len; |
| 287 return mutable_payload() + offset; | 295 if (new_size > capacity_) |
| 288 } | 296 Resize(capacity_ * 2 + new_size); |
| 289 | |
| 290 void Pickle::EndWrite(char* dest, int length) { | |
| 291 // Zero-pad to keep tools like valgrind from complaining about uninitialized | |
| 292 // memory. | |
| 293 if (length % sizeof(uint32)) | |
| 294 memset(dest + length, 0, sizeof(uint32) - (length % sizeof(uint32))); | |
| 295 } | 297 } |
| 296 | 298 |
| 297 void Pickle::Resize(size_t new_capacity) { | 299 void Pickle::Resize(size_t new_capacity) { |
| 298 CHECK_NE(capacity_, kCapacityReadOnly); | 300 CHECK_NE(capacity_, kCapacityReadOnly); |
| 299 | 301 |
| 300 new_capacity = AlignInt(new_capacity, kPayloadUnit); | 302 new_capacity = AlignInt(new_capacity, kPayloadUnit); |
| 301 void* p = realloc(header_, new_capacity); | 303 void* p = realloc(header_, header_size_ + new_capacity); |
| 302 header_ = reinterpret_cast<Header*>(p); | 304 header_ = reinterpret_cast<Header*>(p); |
| 303 capacity_ = new_capacity; | 305 capacity_ = new_capacity; |
| 304 } | 306 } |
| 305 | 307 |
| 306 // static | 308 // static |
| 307 const char* Pickle::FindNext(size_t header_size, | 309 const char* Pickle::FindNext(size_t header_size, |
| 308 const char* start, | 310 const char* start, |
| 309 const char* end) { | 311 const char* end) { |
| 310 DCHECK_EQ(header_size, AlignInt(header_size, sizeof(uint32))); | 312 DCHECK_EQ(header_size, AlignInt(header_size, sizeof(uint32))); |
| 311 DCHECK_LE(header_size, static_cast<size_t>(kPayloadUnit)); | 313 DCHECK_LE(header_size, static_cast<size_t>(kPayloadUnit)); |
| 312 | 314 |
| 313 if (static_cast<size_t>(end - start) < sizeof(Header)) | 315 if (static_cast<size_t>(end - start) < sizeof(Header)) |
| 314 return NULL; | 316 return NULL; |
| 315 | 317 |
| 316 const Header* hdr = reinterpret_cast<const Header*>(start); | 318 const Header* hdr = reinterpret_cast<const Header*>(start); |
| 317 const char* payload_base = start + header_size; | 319 const char* payload_base = start + header_size; |
| 318 const char* payload_end = payload_base + hdr->payload_size; | 320 const char* payload_end = payload_base + hdr->payload_size; |
| 319 if (payload_end < payload_base) | 321 if (payload_end < payload_base) |
| 320 return NULL; | 322 return NULL; |
| 321 | 323 |
| 322 return (payload_end > end) ? NULL : payload_end; | 324 return (payload_end > end) ? NULL : payload_end; |
| 323 } | 325 } |
| OLD | NEW |