1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
|
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/pickle.h"
#include <stdlib.h>
#include <algorithm> // for max()
#include <limits>
//------------------------------------------------------------------------------
// static
const int Pickle::kPayloadUnit = 64;
// We mark a read only pickle with a special capacity_.
static const size_t kCapacityReadOnly = std::numeric_limits<size_t>::max();
// Payload is uint32 aligned.
Pickle::Pickle()
: header_(NULL),
header_size_(sizeof(Header)),
capacity_(0),
variable_buffer_offset_(0) {
Resize(kPayloadUnit);
header_->payload_size = 0;
}
Pickle::Pickle(int header_size)
: header_(NULL),
header_size_(AlignInt(header_size, sizeof(uint32))),
capacity_(0),
variable_buffer_offset_(0) {
DCHECK(static_cast<size_t>(header_size) >= sizeof(Header));
DCHECK(header_size <= kPayloadUnit);
Resize(kPayloadUnit);
header_->payload_size = 0;
}
Pickle::Pickle(const char* data, int data_len)
: header_(reinterpret_cast<Header*>(const_cast<char*>(data))),
header_size_(0),
capacity_(kCapacityReadOnly),
variable_buffer_offset_(0) {
if (data_len >= static_cast<int>(sizeof(Header)))
header_size_ = data_len - header_->payload_size;
if (header_size_ > static_cast<unsigned int>(data_len))
header_size_ = 0;
if (header_size_ != AlignInt(header_size_, sizeof(uint32)))
header_size_ = 0;
// If there is anything wrong with the data, we're not going to use it.
if (!header_size_)
header_ = NULL;
}
Pickle::Pickle(const Pickle& other)
: header_(NULL),
header_size_(other.header_size_),
capacity_(0),
variable_buffer_offset_(other.variable_buffer_offset_) {
size_t payload_size = header_size_ + other.header_->payload_size;
bool resized = Resize(payload_size);
CHECK(resized); // Realloc failed.
memcpy(header_, other.header_, payload_size);
}
Pickle::~Pickle() {
if (capacity_ != kCapacityReadOnly)
free(header_);
}
Pickle& Pickle::operator=(const Pickle& other) {
if (this == &other) {
NOTREACHED();
return *this;
}
if (capacity_ == kCapacityReadOnly) {
header_ = NULL;
capacity_ = 0;
}
if (header_size_ != other.header_size_) {
free(header_);
header_ = NULL;
header_size_ = other.header_size_;
}
bool resized = Resize(other.header_size_ + other.header_->payload_size);
CHECK(resized); // Realloc failed.
memcpy(header_, other.header_,
other.header_size_ + other.header_->payload_size);
variable_buffer_offset_ = other.variable_buffer_offset_;
return *this;
}
bool Pickle::ReadBool(void** iter, bool* result) const {
DCHECK(iter);
int tmp;
if (!ReadInt(iter, &tmp))
return false;
DCHECK(0 == tmp || 1 == tmp);
*result = tmp ? true : false;
return true;
}
bool Pickle::ReadInt(void** iter, int* result) const {
DCHECK(iter);
if (!*iter)
*iter = const_cast<char*>(payload());
if (!IteratorHasRoomFor(*iter, sizeof(*result)))
return false;
// TODO(jar): http://crbug.com/13108 Pickle should be cleaned up, and not
// dependent on alignment.
// Next line is otherwise the same as: memcpy(result, *iter, sizeof(*result));
*result = *reinterpret_cast<int*>(*iter);
UpdateIter(iter, sizeof(*result));
return true;
}
bool Pickle::ReadLong(void** iter, long* result) const {
DCHECK(iter);
if (!*iter)
*iter = const_cast<char*>(payload());
if (!IteratorHasRoomFor(*iter, sizeof(*result)))
return false;
// TODO(jar): http://crbug.com/13108 Pickle should be cleaned up, and not
// dependent on alignment.
memcpy(result, *iter, sizeof(*result));
UpdateIter(iter, sizeof(*result));
return true;
}
bool Pickle::ReadLength(void** iter, int* result) const {
if (!ReadInt(iter, result))
return false;
return ((*result) >= 0);
}
bool Pickle::ReadSize(void** iter, size_t* result) const {
DCHECK(iter);
if (!*iter)
*iter = const_cast<char*>(payload());
if (!IteratorHasRoomFor(*iter, sizeof(*result)))
return false;
// TODO(jar): http://crbug.com/13108 Pickle should be cleaned up, and not
// dependent on alignment.
// Next line is otherwise the same as: memcpy(result, *iter, sizeof(*result));
*result = *reinterpret_cast<size_t*>(*iter);
UpdateIter(iter, sizeof(*result));
return true;
}
bool Pickle::ReadUInt32(void** iter, uint32* result) const {
DCHECK(iter);
if (!*iter)
*iter = const_cast<char*>(payload());
if (!IteratorHasRoomFor(*iter, sizeof(*result)))
return false;
memcpy(result, *iter, sizeof(*result));
UpdateIter(iter, sizeof(*result));
return true;
}
bool Pickle::ReadInt64(void** iter, int64* result) const {
DCHECK(iter);
if (!*iter)
*iter = const_cast<char*>(payload());
if (!IteratorHasRoomFor(*iter, sizeof(*result)))
return false;
memcpy(result, *iter, sizeof(*result));
UpdateIter(iter, sizeof(*result));
return true;
}
bool Pickle::ReadUInt64(void** iter, uint64* result) const {
DCHECK(iter);
if (!*iter)
*iter = const_cast<char*>(payload());
if (!IteratorHasRoomFor(*iter, sizeof(*result)))
return false;
memcpy(result, *iter, sizeof(*result));
UpdateIter(iter, sizeof(*result));
return true;
}
bool Pickle::ReadString(void** iter, std::string* result) const {
DCHECK(iter);
int len;
if (!ReadLength(iter, &len))
return false;
if (!IteratorHasRoomFor(*iter, len))
return false;
char* chars = reinterpret_cast<char*>(*iter);
result->assign(chars, len);
UpdateIter(iter, len);
return true;
}
bool Pickle::ReadWString(void** iter, std::wstring* result) const {
DCHECK(iter);
int len;
if (!ReadLength(iter, &len))
return false;
// Avoid integer overflow.
if (len > INT_MAX / static_cast<int>(sizeof(wchar_t)))
return false;
if (!IteratorHasRoomFor(*iter, len * sizeof(wchar_t)))
return false;
wchar_t* chars = reinterpret_cast<wchar_t*>(*iter);
result->assign(chars, len);
UpdateIter(iter, len * sizeof(wchar_t));
return true;
}
bool Pickle::ReadString16(void** iter, string16* result) const {
DCHECK(iter);
int len;
if (!ReadLength(iter, &len))
return false;
if (!IteratorHasRoomFor(*iter, len * sizeof(char16)))
return false;
char16* chars = reinterpret_cast<char16*>(*iter);
result->assign(chars, len);
UpdateIter(iter, len * sizeof(char16));
return true;
}
bool Pickle::ReadBytes(void** iter, const char** data, int length) const {
DCHECK(iter);
DCHECK(data);
*data = 0;
if (!*iter)
*iter = const_cast<char*>(payload());
if (!IteratorHasRoomFor(*iter, length))
return false;
*data = reinterpret_cast<const char*>(*iter);
UpdateIter(iter, length);
return true;
}
bool Pickle::ReadData(void** iter, const char** data, int* length) const {
DCHECK(iter);
DCHECK(data);
DCHECK(length);
*length = 0;
*data = 0;
if (!ReadLength(iter, length))
return false;
return ReadBytes(iter, data, *length);
}
char* Pickle::BeginWrite(size_t length) {
// write at a uint32-aligned offset from the beginning of the header
size_t offset = AlignInt(header_->payload_size, sizeof(uint32));
size_t new_size = offset + length;
size_t needed_size = header_size_ + new_size;
if (needed_size > capacity_ && !Resize(std::max(capacity_ * 2, needed_size)))
return NULL;
#ifdef ARCH_CPU_64_BITS
DCHECK_LE(length, std::numeric_limits<uint32>::max());
#endif
header_->payload_size = static_cast<uint32>(new_size);
return payload() + offset;
}
void Pickle::EndWrite(char* dest, int length) {
// Zero-pad to keep tools like purify from complaining about uninitialized
// memory.
if (length % sizeof(uint32))
memset(dest + length, 0, sizeof(uint32) - (length % sizeof(uint32)));
}
bool Pickle::WriteBytes(const void* data, int data_len) {
DCHECK(capacity_ != kCapacityReadOnly) << "oops: pickle is readonly";
char* dest = BeginWrite(data_len);
if (!dest)
return false;
memcpy(dest, data, data_len);
EndWrite(dest, data_len);
return true;
}
bool Pickle::WriteString(const std::string& value) {
if (!WriteInt(static_cast<int>(value.size())))
return false;
return WriteBytes(value.data(), static_cast<int>(value.size()));
}
bool Pickle::WriteWString(const std::wstring& value) {
if (!WriteInt(static_cast<int>(value.size())))
return false;
return WriteBytes(value.data(),
static_cast<int>(value.size() * sizeof(wchar_t)));
}
bool Pickle::WriteString16(const string16& value) {
if (!WriteInt(static_cast<int>(value.size())))
return false;
return WriteBytes(value.data(),
static_cast<int>(value.size()) * sizeof(char16));
}
bool Pickle::WriteData(const char* data, int length) {
return length >= 0 && WriteInt(length) && WriteBytes(data, length);
}
char* Pickle::BeginWriteData(int length) {
DCHECK_EQ(variable_buffer_offset_, 0U) <<
"There can only be one variable buffer in a Pickle";
if (length < 0 || !WriteInt(length))
return NULL;
char *data_ptr = BeginWrite(length);
if (!data_ptr)
return NULL;
variable_buffer_offset_ =
data_ptr - reinterpret_cast<char*>(header_) - sizeof(int);
// EndWrite doesn't necessarily have to be called after the write operation,
// so we call it here to pad out what the caller will eventually write.
EndWrite(data_ptr, length);
return data_ptr;
}
void Pickle::TrimWriteData(int new_length) {
DCHECK_NE(variable_buffer_offset_, 0U);
// Fetch the the variable buffer size
int* cur_length = reinterpret_cast<int*>(
reinterpret_cast<char*>(header_) + variable_buffer_offset_);
if (new_length < 0 || new_length > *cur_length) {
NOTREACHED() << "Invalid length in TrimWriteData.";
return;
}
// Update the payload size and variable buffer size
header_->payload_size -= (*cur_length - new_length);
*cur_length = new_length;
}
bool Pickle::Resize(size_t new_capacity) {
new_capacity = AlignInt(new_capacity, kPayloadUnit);
CHECK_NE(capacity_, kCapacityReadOnly);
void* p = realloc(header_, new_capacity);
if (!p)
return false;
header_ = reinterpret_cast<Header*>(p);
capacity_ = new_capacity;
return true;
}
// static
const char* Pickle::FindNext(size_t header_size,
const char* start,
const char* end) {
DCHECK(header_size == AlignInt(header_size, sizeof(uint32)));
DCHECK(header_size <= static_cast<size_t>(kPayloadUnit));
const Header* hdr = reinterpret_cast<const Header*>(start);
const char* payload_base = start + header_size;
const char* payload_end = payload_base + hdr->payload_size;
if (payload_end < payload_base)
return NULL;
return (payload_end > end) ? NULL : payload_end;
}
|