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::ReadString(std::string
* result
) {
97 const char* read_from
= GetReadPointerAndAdvance(len
);
101 result
->assign(read_from
, len
);
105 bool PickleIterator::ReadWString(std::wstring
* result
) {
109 const char* read_from
= GetReadPointerAndAdvance(len
, sizeof(wchar_t));
113 result
->assign(reinterpret_cast<const wchar_t*>(read_from
), len
);
117 bool PickleIterator::ReadString16(string16
* result
) {
121 const char* read_from
= GetReadPointerAndAdvance(len
, sizeof(char16
));
125 result
->assign(reinterpret_cast<const char16
*>(read_from
), len
);
129 bool PickleIterator::ReadData(const char** data
, int* length
) {
133 if (!ReadInt(length
))
136 return ReadBytes(data
, *length
);
139 bool PickleIterator::ReadBytes(const char** data
, int length
) {
140 const char* read_from
= GetReadPointerAndAdvance(length
);
147 // Payload is uint32 aligned.
151 header_size_(sizeof(Header
)),
153 variable_buffer_offset_(0) {
154 Resize(kPayloadUnit
);
155 header_
->payload_size
= 0;
158 Pickle::Pickle(int header_size
)
160 header_size_(AlignInt(header_size
, sizeof(uint32
))),
162 variable_buffer_offset_(0) {
163 DCHECK_GE(static_cast<size_t>(header_size
), sizeof(Header
));
164 DCHECK_LE(header_size
, kPayloadUnit
);
165 Resize(kPayloadUnit
);
166 header_
->payload_size
= 0;
169 Pickle::Pickle(const char* data
, int data_len
)
170 : header_(reinterpret_cast<Header
*>(const_cast<char*>(data
))),
172 capacity_(kCapacityReadOnly
),
173 variable_buffer_offset_(0) {
174 if (data_len
>= static_cast<int>(sizeof(Header
)))
175 header_size_
= data_len
- header_
->payload_size
;
177 if (header_size_
> static_cast<unsigned int>(data_len
))
180 if (header_size_
!= AlignInt(header_size_
, sizeof(uint32
)))
183 // If there is anything wrong with the data, we're not going to use it.
188 Pickle::Pickle(const Pickle
& other
)
190 header_size_(other
.header_size_
),
192 variable_buffer_offset_(other
.variable_buffer_offset_
) {
193 size_t payload_size
= header_size_
+ other
.header_
->payload_size
;
194 bool resized
= Resize(payload_size
);
195 CHECK(resized
); // Realloc failed.
196 memcpy(header_
, other
.header_
, payload_size
);
200 if (capacity_
!= kCapacityReadOnly
)
204 Pickle
& Pickle::operator=(const Pickle
& other
) {
205 if (this == &other
) {
209 if (capacity_
== kCapacityReadOnly
) {
213 if (header_size_
!= other
.header_size_
) {
216 header_size_
= other
.header_size_
;
218 bool resized
= Resize(other
.header_size_
+ other
.header_
->payload_size
);
219 CHECK(resized
); // Realloc failed.
220 memcpy(header_
, other
.header_
,
221 other
.header_size_
+ other
.header_
->payload_size
);
222 variable_buffer_offset_
= other
.variable_buffer_offset_
;
226 bool Pickle::WriteString(const std::string
& value
) {
227 if (!WriteInt(static_cast<int>(value
.size())))
230 return WriteBytes(value
.data(), static_cast<int>(value
.size()));
233 bool Pickle::WriteWString(const std::wstring
& value
) {
234 if (!WriteInt(static_cast<int>(value
.size())))
237 return WriteBytes(value
.data(),
238 static_cast<int>(value
.size() * sizeof(wchar_t)));
241 bool Pickle::WriteString16(const string16
& value
) {
242 if (!WriteInt(static_cast<int>(value
.size())))
245 return WriteBytes(value
.data(),
246 static_cast<int>(value
.size()) * sizeof(char16
));
249 bool Pickle::WriteData(const char* data
, int length
) {
250 return length
>= 0 && WriteInt(length
) && WriteBytes(data
, length
);
253 bool Pickle::WriteBytes(const void* data
, int data_len
) {
254 DCHECK_NE(kCapacityReadOnly
, capacity_
) << "oops: pickle is readonly";
256 char* dest
= BeginWrite(data_len
);
260 memcpy(dest
, data
, data_len
);
262 EndWrite(dest
, data_len
);
266 char* Pickle::BeginWriteData(int length
) {
267 DCHECK_EQ(variable_buffer_offset_
, 0U) <<
268 "There can only be one variable buffer in a Pickle";
270 if (length
< 0 || !WriteInt(length
))
273 char *data_ptr
= BeginWrite(length
);
277 variable_buffer_offset_
=
278 data_ptr
- reinterpret_cast<char*>(header_
) - sizeof(int);
280 // EndWrite doesn't necessarily have to be called after the write operation,
281 // so we call it here to pad out what the caller will eventually write.
282 EndWrite(data_ptr
, length
);
286 void Pickle::TrimWriteData(int new_length
) {
287 DCHECK_NE(variable_buffer_offset_
, 0U);
289 // Fetch the the variable buffer size
290 int* cur_length
= reinterpret_cast<int*>(
291 reinterpret_cast<char*>(header_
) + variable_buffer_offset_
);
293 if (new_length
< 0 || new_length
> *cur_length
) {
294 NOTREACHED() << "Invalid length in TrimWriteData.";
298 // Update the payload size and variable buffer size
299 header_
->payload_size
-= (*cur_length
- new_length
);
300 *cur_length
= new_length
;
303 char* Pickle::BeginWrite(size_t length
) {
304 // write at a uint32-aligned offset from the beginning of the header
305 size_t offset
= AlignInt(header_
->payload_size
, sizeof(uint32
));
307 size_t new_size
= offset
+ length
;
308 size_t needed_size
= header_size_
+ new_size
;
309 if (needed_size
> capacity_
&& !Resize(std::max(capacity_
* 2, needed_size
)))
312 #ifdef ARCH_CPU_64_BITS
313 DCHECK_LE(length
, kuint32max
);
316 header_
->payload_size
= static_cast<uint32
>(new_size
);
317 return payload() + offset
;
320 void Pickle::EndWrite(char* dest
, int length
) {
321 // Zero-pad to keep tools like valgrind from complaining about uninitialized
323 if (length
% sizeof(uint32
))
324 memset(dest
+ length
, 0, sizeof(uint32
) - (length
% sizeof(uint32
)));
327 bool Pickle::Resize(size_t new_capacity
) {
328 new_capacity
= AlignInt(new_capacity
, kPayloadUnit
);
330 CHECK_NE(capacity_
, kCapacityReadOnly
);
331 void* p
= realloc(header_
, new_capacity
);
335 header_
= reinterpret_cast<Header
*>(p
);
336 capacity_
= new_capacity
;
341 const char* Pickle::FindNext(size_t header_size
,
344 DCHECK_EQ(header_size
, AlignInt(header_size
, sizeof(uint32
)));
345 DCHECK_LE(header_size
, static_cast<size_t>(kPayloadUnit
));
347 if (static_cast<size_t>(end
- start
) < sizeof(Header
))
350 const Header
* hdr
= reinterpret_cast<const Header
*>(start
);
351 const char* payload_base
= start
+ header_size
;
352 const char* payload_end
= payload_base
+ hdr
->payload_size
;
353 if (payload_end
< payload_base
)
356 return (payload_end
> end
) ? NULL
: payload_end
;