1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/pickle.h"
9 #include <algorithm> // for max()
12 //------------------------------------------------------------------------------
15 const int Pickle::kPayloadUnit = 64;
17 // We mark a read only pickle with a special capacity_.
18 static const size_t kCapacityReadOnly = std::numeric_limits<size_t>::max();
20 // Payload is uint32 aligned.
24 header_size_(sizeof(Header)),
26 variable_buffer_offset_(0) {
28 header_->payload_size = 0;
31 Pickle::Pickle(int header_size)
33 header_size_(AlignInt(header_size, sizeof(uint32))),
35 variable_buffer_offset_(0) {
36 DCHECK_GE(static_cast<size_t>(header_size), sizeof(Header));
37 DCHECK_LE(header_size, kPayloadUnit);
39 header_->payload_size = 0;
42 Pickle::Pickle(const char* data, int data_len)
43 : header_(reinterpret_cast<Header*>(const_cast<char*>(data))),
45 capacity_(kCapacityReadOnly),
46 variable_buffer_offset_(0)
49 if (data_len >= static_cast<int>(sizeof(Header)))
51 header_size_ = data_len - header_->payload_size;
54 if (header_size_ > static_cast<unsigned int>(data_len))
60 if (header_size_ != AlignInt(header_size_, sizeof(uint32)))
66 // If there is anything wrong with the data, we're not going to use it.
71 Pickle::Pickle(const Pickle& other)
73 header_size_(other.header_size_),
75 variable_buffer_offset_(other.variable_buffer_offset_) {
76 size_t payload_size = header_size_ + other.header_->payload_size;
77 bool resized = Resize(payload_size);
78 CHECK(resized); // Realloc failed.
79 memcpy(header_, other.header_, payload_size);
83 if (capacity_ != kCapacityReadOnly)
87 Pickle& Pickle::operator=(const Pickle& other) {
92 if (capacity_ == kCapacityReadOnly) {
96 if (header_size_ != other.header_size_) {
99 header_size_ = other.header_size_;
101 bool resized = Resize(other.header_size_ + other.header_->payload_size);
102 CHECK(resized); // Realloc failed.
103 memcpy(header_, other.header_,
104 other.header_size_ + other.header_->payload_size);
105 variable_buffer_offset_ = other.variable_buffer_offset_;
109 bool Pickle::ReadBool(void** iter, bool* result) const {
113 if (!ReadInt(iter, &tmp))
115 DCHECK(0 == tmp || 1 == tmp);
116 *result = tmp ? true : false;
120 bool Pickle::ReadInt(void** iter, int* result) const {
123 *iter = const_cast<char*>(payload());
125 if (!IteratorHasRoomFor(*iter, sizeof(*result)))
128 // TODO(jar): http://crbug.com/13108 Pickle should be cleaned up, and not
129 // dependent on alignment.
130 // Next line is otherwise the same as: memcpy(result, *iter, sizeof(*result));
131 *result = *reinterpret_cast<int*>(*iter);
133 UpdateIter(iter, sizeof(*result));
137 bool Pickle::ReadLong(void** iter, long* result) const {
140 *iter = const_cast<char*>(payload());
142 if (!IteratorHasRoomFor(*iter, sizeof(*result)))
145 // TODO(jar): http://crbug.com/13108 Pickle should be cleaned up, and not
146 // dependent on alignment.
147 memcpy(result, *iter, sizeof(*result));
149 UpdateIter(iter, sizeof(*result));
153 bool Pickle::ReadSize(void** iter, size_t* result) const {
156 *iter = const_cast<char*>(payload());
158 if (!IteratorHasRoomFor(*iter, sizeof(*result)))
161 // TODO(jar): http://crbug.com/13108 Pickle should be cleaned up, and not
162 // dependent on alignment.
163 // Next line is otherwise the same as: memcpy(result, *iter, sizeof(*result));
164 *result = *reinterpret_cast<size_t*>(*iter);
166 UpdateIter(iter, sizeof(*result));
170 bool Pickle::ReadUInt16(void** iter, uint16* result) const {
173 *iter = const_cast<char*>(payload());
175 if (!IteratorHasRoomFor(*iter, sizeof(*result)))
178 memcpy(result, *iter, sizeof(*result));
180 UpdateIter(iter, sizeof(*result));
184 bool Pickle::ReadUInt32(void** iter, uint32* result) const {
187 *iter = const_cast<char*>(payload());
189 if (!IteratorHasRoomFor(*iter, sizeof(*result)))
192 memcpy(result, *iter, sizeof(*result));
194 UpdateIter(iter, sizeof(*result));
198 bool Pickle::ReadInt64(void** iter, int64* result) const {
201 *iter = const_cast<char*>(payload());
203 if (!IteratorHasRoomFor(*iter, sizeof(*result)))
206 memcpy(result, *iter, sizeof(*result));
208 UpdateIter(iter, sizeof(*result));
212 bool Pickle::ReadUInt64(void** iter, uint64* result) const {
215 *iter = const_cast<char*>(payload());
217 if (!IteratorHasRoomFor(*iter, sizeof(*result)))
220 memcpy(result, *iter, sizeof(*result));
222 UpdateIter(iter, sizeof(*result));
227 bool Pickle::ReadString(void** iter, std::string* result) const {
231 if (!ReadLength(iter, &len))
233 if (!IteratorHasRoomFor(*iter, len))
236 char* chars = reinterpret_cast<char*>(*iter);
237 result->assign(chars, len);
239 UpdateIter(iter, len);
244 bool Pickle::ReadWString(void** iter, std::wstring* result) const {
248 if (!ReadLength(iter, &len))
250 // Avoid integer overflow.
251 if (len > INT_MAX / static_cast<int>(sizeof(wchar_t)))
253 if (!IteratorHasRoomFor(*iter, len * sizeof(wchar_t)))
256 wchar_t* chars = reinterpret_cast<wchar_t*>(*iter);
257 result->assign(chars, len);
259 UpdateIter(iter, len * sizeof(wchar_t));
263 bool Pickle::ReadString16(void** iter, string16* result) const {
267 if (!ReadLength(iter, &len))
269 if (!IteratorHasRoomFor(*iter, len * sizeof(char16)))
272 char16* chars = reinterpret_cast<char16*>(*iter);
273 result->assign(chars, len);
275 UpdateIter(iter, len * sizeof(char16));
280 bool Pickle::ReadData(void** iter, const char** data, int* length) const {
287 if (!ReadLength(iter, length))
290 return ReadBytes(iter, data, *length);
293 bool Pickle::ReadBytes(void** iter, const char** data, int length) const {
298 *iter = const_cast<char*>(payload());
300 if (!IteratorHasRoomFor(*iter, length))
303 *data = reinterpret_cast<const char*>(*iter);
305 UpdateIter(iter, length);
309 bool Pickle::ReadLength(void** iter, int* result) const {
310 if (!ReadInt(iter, result))
312 return ((*result) >= 0);
316 bool Pickle::WriteString(const std::string& value) {
317 if (!WriteInt(static_cast<int>(value.size())))
320 return WriteBytes(value.data(), static_cast<int>(value.size()));
324 bool Pickle::WriteWString(const std::wstring& value) {
325 if (!WriteInt(static_cast<int>(value.size())))
328 return WriteBytes(value.data(),
329 static_cast<int>(value.size() * sizeof(wchar_t)));
332 bool Pickle::WriteString16(const string16& value) {
333 if (!WriteInt(static_cast<int>(value.size())))
336 return WriteBytes(value.data(),
337 static_cast<int>(value.size()) * sizeof(char16));
341 bool Pickle::WriteData(const char* data, int length) {
342 return length >= 0 && WriteInt(length) && WriteBytes(data, length);
345 bool Pickle::WriteBytes(const void* data, int data_len) {
346 DCHECK_NE(kCapacityReadOnly, capacity_) << "oops: pickle is readonly";
348 char* dest = BeginWrite(data_len);
352 memcpy(dest, data, data_len);
354 EndWrite(dest, data_len);
358 char* Pickle::BeginWriteData(int length) {
359 DCHECK_EQ(variable_buffer_offset_, 0U) <<
360 "There can only be one variable buffer in a Pickle";
362 if (length < 0 || !WriteInt(length))
365 char *data_ptr = BeginWrite(length);
369 variable_buffer_offset_ =
370 data_ptr - reinterpret_cast<char*>(header_) - sizeof(int);
372 // EndWrite doesn't necessarily have to be called after the write operation,
373 // so we call it here to pad out what the caller will eventually write.
374 EndWrite(data_ptr, length);
378 void Pickle::TrimWriteData(int new_length) {
379 DCHECK_NE(variable_buffer_offset_, 0U);
381 // Fetch the the variable buffer size
382 int* cur_length = reinterpret_cast<int*>(
383 reinterpret_cast<char*>(header_) + variable_buffer_offset_);
385 if (new_length < 0 || new_length > *cur_length) {
386 NOTREACHED() << "Invalid length in TrimWriteData.";
390 // Update the payload size and variable buffer size
391 header_->payload_size -= (*cur_length - new_length);
392 *cur_length = new_length;
395 char* Pickle::BeginWrite(size_t length) {
396 // write at a uint32-aligned offset from the beginning of the header
397 size_t offset = AlignInt(header_->payload_size, sizeof(uint32));
399 size_t new_size = offset + length;
400 size_t needed_size = header_size_ + new_size;
401 if (needed_size > capacity_ && !Resize(std::max(capacity_ * 2, needed_size)))
404 #ifdef ARCH_CPU_64_BITS
405 DCHECK_LE(length, std::numeric_limits<uint32>::max());
408 header_->payload_size = static_cast<uint32>(new_size);
409 return payload() + offset;
412 void Pickle::EndWrite(char* dest, int length) {
413 // Zero-pad to keep tools like purify from complaining about uninitialized
415 if (length % sizeof(uint32))
416 memset(dest + length, 0, sizeof(uint32) - (length % sizeof(uint32)));
419 bool Pickle::Resize(size_t new_capacity) {
420 new_capacity = AlignInt(new_capacity, kPayloadUnit);
422 CHECK_NE(capacity_, kCapacityReadOnly);
423 void* p = realloc(header_, new_capacity);
427 header_ = reinterpret_cast<Header*>(p);
428 capacity_ = new_capacity;
433 const char* Pickle::FindNext(size_t header_size,
436 DCHECK_EQ(header_size, AlignInt(header_size, sizeof(uint32)));
437 DCHECK_LE(header_size, static_cast<size_t>(kPayloadUnit));
439 if (static_cast<size_t>(end - start) < sizeof(Header))
442 const Header* hdr = reinterpret_cast<const Header*>(start);
443 const char* payload_base = start + header_size;
444 const char* payload_end = payload_base + hdr->payload_size;
445 if (payload_end < payload_base)
448 return (payload_end > end) ? NULL : payload_end;