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
3 // found in the LICENSE file.
5 #include "base/pickle.h"
9 #include <algorithm> // for max()
11 //------------------------------------------------------------------------------
14 const int Pickle::kPayloadUnit = 64;
16 static const size_t kCapacityReadOnly = static_cast<size_t>(-1);
18 PickleIterator::PickleIterator(const Pickle& pickle)
19 : read_ptr_(pickle.payload()),
20 read_end_ptr_(pickle.end_of_payload()) {
23 template <typename Type>
24 inline bool PickleIterator::ReadBuiltinType(Type* result) {
25 const char* read_from = GetReadPointerAndAdvance<Type>();
28 if (sizeof(Type) > sizeof(uint32))
29 memcpy(result, read_from, sizeof(*result));
31 *result = *reinterpret_cast<const Type*>(read_from);
35 template<typename Type>
36 inline const char* PickleIterator::GetReadPointerAndAdvance() {
37 const char* current_read_ptr = read_ptr_;
38 if (read_ptr_ + sizeof(Type) > read_end_ptr_)
40 if (sizeof(Type) < sizeof(uint32))
41 read_ptr_ += AlignInt(sizeof(Type), sizeof(uint32));
43 read_ptr_ += sizeof(Type);
44 return current_read_ptr;
47 const char* PickleIterator::GetReadPointerAndAdvance(int num_bytes) {
48 if (num_bytes < 0 || read_end_ptr_ - read_ptr_ < num_bytes)
50 const char* current_read_ptr = read_ptr_;
51 read_ptr_ += AlignInt(num_bytes, sizeof(uint32));
52 return current_read_ptr;
55 inline const char* PickleIterator::GetReadPointerAndAdvance(int num_elements,
56 size_t size_element) {
57 // Check for int32 overflow.
58 int64 num_bytes = static_cast<int64>(num_elements) * size_element;
59 int num_bytes32 = static_cast<int>(num_bytes);
60 if (num_bytes != static_cast<int64>(num_bytes32))
62 return GetReadPointerAndAdvance(num_bytes32);
65 bool PickleIterator::ReadBool(bool* result) {
66 return ReadBuiltinType(result);
69 bool PickleIterator::ReadInt(int* result) {
70 return ReadBuiltinType(result);
73 bool PickleIterator::ReadLong(long* result) {
74 return ReadBuiltinType(result);
77 bool PickleIterator::ReadUInt16(uint16* result) {
78 return ReadBuiltinType(result);
81 bool PickleIterator::ReadUInt32(uint32* result) {
82 return ReadBuiltinType(result);
85 bool PickleIterator::ReadInt64(int64* result) {
86 return ReadBuiltinType(result);
89 bool PickleIterator::ReadUInt64(uint64* result) {
90 return ReadBuiltinType(result);
93 bool PickleIterator::ReadFloat(float* result) {
94 return ReadBuiltinType(result);
97 bool PickleIterator::ReadString(std::string* result) {
101 const char* read_from = GetReadPointerAndAdvance(len);
105 result->assign(read_from, len);
109 bool PickleIterator::ReadWString(std::wstring* result) {
113 const char* read_from = GetReadPointerAndAdvance(len, sizeof(wchar_t));
117 result->assign(reinterpret_cast<const wchar_t*>(read_from), len);
121 bool PickleIterator::ReadString16(string16* result) {
125 const char* read_from = GetReadPointerAndAdvance(len, sizeof(char16));
129 result->assign(reinterpret_cast<const char16*>(read_from), len);
133 bool PickleIterator::ReadData(const char** data, int* length) {
137 if (!ReadInt(length))
140 return ReadBytes(data, *length);
143 bool PickleIterator::ReadBytes(const char** data, int length) {
144 const char* read_from = GetReadPointerAndAdvance(length);
151 // Payload is uint32 aligned.
155 header_size_(sizeof(Header)),
156 capacity_after_header_(0),
158 Resize(kPayloadUnit);
159 header_->payload_size = 0;
162 Pickle::Pickle(int header_size)
164 header_size_(AlignInt(header_size, sizeof(uint32))),
165 capacity_after_header_(0),
167 DCHECK_GE(static_cast<size_t>(header_size), sizeof(Header));
168 DCHECK_LE(header_size, kPayloadUnit);
169 Resize(kPayloadUnit);
170 header_->payload_size = 0;
173 Pickle::Pickle(const char* data, int data_len)
174 : header_(reinterpret_cast<Header*>(const_cast<char*>(data))),
176 capacity_after_header_(kCapacityReadOnly),
178 if (data_len >= static_cast<int>(sizeof(Header)))
179 header_size_ = data_len - header_->payload_size;
181 if (header_size_ > static_cast<unsigned int>(data_len))
184 if (header_size_ != AlignInt(header_size_, sizeof(uint32)))
187 // If there is anything wrong with the data, we're not going to use it.
192 Pickle::Pickle(const Pickle& other)
194 header_size_(other.header_size_),
195 capacity_after_header_(0),
196 write_offset_(other.write_offset_) {
197 size_t payload_size = header_size_ + other.header_->payload_size;
198 Resize(payload_size);
199 memcpy(header_, other.header_, payload_size);
203 if (capacity_after_header_ != kCapacityReadOnly)
207 Pickle& Pickle::operator=(const Pickle& other) {
208 if (this == &other) {
212 if (capacity_after_header_ == kCapacityReadOnly) {
214 capacity_after_header_ = 0;
216 if (header_size_ != other.header_size_) {
219 header_size_ = other.header_size_;
221 Resize(other.header_->payload_size);
222 memcpy(header_, other.header_,
223 other.header_size_ + other.header_->payload_size);
224 write_offset_ = other.write_offset_;
228 bool Pickle::WriteString(const std::string& value) {
229 if (!WriteInt(static_cast<int>(value.size())))
232 return WriteBytes(value.data(), static_cast<int>(value.size()));
235 bool Pickle::WriteWString(const std::wstring& value) {
236 if (!WriteInt(static_cast<int>(value.size())))
239 return WriteBytes(value.data(),
240 static_cast<int>(value.size() * sizeof(wchar_t)));
243 bool Pickle::WriteString16(const string16& value) {
244 if (!WriteInt(static_cast<int>(value.size())))
247 return WriteBytes(value.data(),
248 static_cast<int>(value.size()) * sizeof(char16));
251 bool Pickle::WriteData(const char* data, int length) {
252 return length >= 0 && WriteInt(length) && WriteBytes(data, length);
255 bool Pickle::WriteBytes(const void* data, int length) {
256 WriteBytesCommon(data, length);
260 void Pickle::Reserve(size_t length) {
261 size_t data_len = AlignInt(length, sizeof(uint32));
262 DCHECK_GE(data_len, length);
263 #ifdef ARCH_CPU_64_BITS
264 DCHECK_LE(data_len, kuint32max);
266 DCHECK_LE(write_offset_, kuint32max - data_len);
267 size_t new_size = write_offset_ + data_len;
268 if (new_size > capacity_after_header_)
269 Resize(capacity_after_header_ * 2 + new_size);
272 void Pickle::Resize(size_t new_capacity) {
273 new_capacity = AlignInt(new_capacity, kPayloadUnit);
275 CHECK_NE(capacity_after_header_, kCapacityReadOnly);
276 void* p = realloc(header_, header_size_ + new_capacity);
278 header_ = reinterpret_cast<Header*>(p);
279 capacity_after_header_ = new_capacity;
283 const char* Pickle::FindNext(size_t header_size,
286 DCHECK_EQ(header_size, AlignInt(header_size, sizeof(uint32)));
287 DCHECK_LE(header_size, static_cast<size_t>(kPayloadUnit));
289 size_t length = static_cast<size_t>(end - start);
290 if (length < sizeof(Header))
293 const Header* hdr = reinterpret_cast<const Header*>(start);
294 if (length < header_size || length - header_size < hdr->payload_size)
296 return start + header_size + hdr->payload_size;
299 template <size_t length> void Pickle::WriteBytesStatic(const void* data) {
300 WriteBytesCommon(data, length);
303 template void Pickle::WriteBytesStatic<2>(const void* data);
304 template void Pickle::WriteBytesStatic<4>(const void* data);
305 template void Pickle::WriteBytesStatic<8>(const void* data);
307 inline void Pickle::WriteBytesCommon(const void* data, size_t length) {
308 DCHECK_NE(kCapacityReadOnly, capacity_after_header_)
309 << "oops: pickle is readonly";
310 size_t data_len = AlignInt(length, sizeof(uint32));
311 DCHECK_GE(data_len, length);
312 #ifdef ARCH_CPU_64_BITS
313 DCHECK_LE(data_len, kuint32max);
315 DCHECK_LE(write_offset_, kuint32max - data_len);
316 size_t new_size = write_offset_ + data_len;
317 if (new_size > capacity_after_header_) {
318 Resize(std::max(capacity_after_header_ * 2, new_size));
321 char* write = mutable_payload() + write_offset_;
322 memcpy(write, data, length);
323 memset(write + length, 0, data_len - length);
324 header_->payload_size = static_cast<uint32>(write_offset_ + length);
325 write_offset_ = new_size;