Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(286)

Side by Side Diff: base/pickle.cc

Issue 1647803004: Move base to DEPS (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/pickle.h ('k') | base/pickle_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "base/pickle.h"
6
7 #include <stdlib.h>
8
9 #include <algorithm> // for max()
10
11 namespace base {
12
13 // static
14 const int Pickle::kPayloadUnit = 64;
15
16 static const size_t kCapacityReadOnly = static_cast<size_t>(-1);
17
18 PickleIterator::PickleIterator(const Pickle& pickle)
19 : payload_(pickle.payload()),
20 read_index_(0),
21 end_index_(pickle.payload_size()) {
22 }
23
24 template <typename Type>
25 inline bool PickleIterator::ReadBuiltinType(Type* result) {
26 const char* read_from = GetReadPointerAndAdvance<Type>();
27 if (!read_from)
28 return false;
29 if (sizeof(Type) > sizeof(uint32))
30 memcpy(result, read_from, sizeof(*result));
31 else
32 *result = *reinterpret_cast<const Type*>(read_from);
33 return true;
34 }
35
36 inline void PickleIterator::Advance(size_t size) {
37 size_t aligned_size = AlignInt(size, sizeof(uint32_t));
38 if (end_index_ - read_index_ < aligned_size) {
39 read_index_ = end_index_;
40 } else {
41 read_index_ += aligned_size;
42 }
43 }
44
45 template<typename Type>
46 inline const char* PickleIterator::GetReadPointerAndAdvance() {
47 if (sizeof(Type) > end_index_ - read_index_) {
48 read_index_ = end_index_;
49 return NULL;
50 }
51 const char* current_read_ptr = payload_ + read_index_;
52 Advance(sizeof(Type));
53 return current_read_ptr;
54 }
55
56 const char* PickleIterator::GetReadPointerAndAdvance(int num_bytes) {
57 if (num_bytes < 0 ||
58 end_index_ - read_index_ < static_cast<size_t>(num_bytes)) {
59 read_index_ = end_index_;
60 return NULL;
61 }
62 const char* current_read_ptr = payload_ + read_index_;
63 Advance(num_bytes);
64 return current_read_ptr;
65 }
66
67 inline const char* PickleIterator::GetReadPointerAndAdvance(
68 int num_elements,
69 size_t size_element) {
70 // Check for int32 overflow.
71 int64 num_bytes = static_cast<int64>(num_elements) * size_element;
72 int num_bytes32 = static_cast<int>(num_bytes);
73 if (num_bytes != static_cast<int64>(num_bytes32))
74 return NULL;
75 return GetReadPointerAndAdvance(num_bytes32);
76 }
77
78 bool PickleIterator::ReadBool(bool* result) {
79 return ReadBuiltinType(result);
80 }
81
82 bool PickleIterator::ReadInt(int* result) {
83 return ReadBuiltinType(result);
84 }
85
86 bool PickleIterator::ReadLong(long* result) {
87 return ReadBuiltinType(result);
88 }
89
90 bool PickleIterator::ReadUInt16(uint16* result) {
91 return ReadBuiltinType(result);
92 }
93
94 bool PickleIterator::ReadUInt32(uint32* result) {
95 return ReadBuiltinType(result);
96 }
97
98 bool PickleIterator::ReadInt64(int64* result) {
99 return ReadBuiltinType(result);
100 }
101
102 bool PickleIterator::ReadUInt64(uint64* result) {
103 return ReadBuiltinType(result);
104 }
105
106 bool PickleIterator::ReadSizeT(size_t* result) {
107 // Always read size_t as a 64-bit value to ensure compatibility between 32-bit
108 // and 64-bit processes.
109 uint64 result_uint64 = 0;
110 bool success = ReadBuiltinType(&result_uint64);
111 *result = static_cast<size_t>(result_uint64);
112 // Fail if the cast above truncates the value.
113 return success && (*result == result_uint64);
114 }
115
116 bool PickleIterator::ReadFloat(float* result) {
117 // crbug.com/315213
118 // The source data may not be properly aligned, and unaligned float reads
119 // cause SIGBUS on some ARM platforms, so force using memcpy to copy the data
120 // into the result.
121 const char* read_from = GetReadPointerAndAdvance<float>();
122 if (!read_from)
123 return false;
124 memcpy(result, read_from, sizeof(*result));
125 return true;
126 }
127
128 bool PickleIterator::ReadDouble(double* result) {
129 // crbug.com/315213
130 // The source data may not be properly aligned, and unaligned double reads
131 // cause SIGBUS on some ARM platforms, so force using memcpy to copy the data
132 // into the result.
133 const char* read_from = GetReadPointerAndAdvance<double>();
134 if (!read_from)
135 return false;
136 memcpy(result, read_from, sizeof(*result));
137 return true;
138 }
139
140 bool PickleIterator::ReadString(std::string* result) {
141 int len;
142 if (!ReadInt(&len))
143 return false;
144 const char* read_from = GetReadPointerAndAdvance(len);
145 if (!read_from)
146 return false;
147
148 result->assign(read_from, len);
149 return true;
150 }
151
152 bool PickleIterator::ReadStringPiece(StringPiece* result) {
153 int len;
154 if (!ReadInt(&len))
155 return false;
156 const char* read_from = GetReadPointerAndAdvance(len);
157 if (!read_from)
158 return false;
159
160 *result = StringPiece(read_from, len);
161 return true;
162 }
163
164 bool PickleIterator::ReadString16(string16* result) {
165 int len;
166 if (!ReadInt(&len))
167 return false;
168 const char* read_from = GetReadPointerAndAdvance(len, sizeof(char16));
169 if (!read_from)
170 return false;
171
172 result->assign(reinterpret_cast<const char16*>(read_from), len);
173 return true;
174 }
175
176 bool PickleIterator::ReadStringPiece16(StringPiece16* result) {
177 int len;
178 if (!ReadInt(&len))
179 return false;
180 const char* read_from = GetReadPointerAndAdvance(len, sizeof(char16));
181 if (!read_from)
182 return false;
183
184 *result = StringPiece16(reinterpret_cast<const char16*>(read_from), len);
185 return true;
186 }
187
188 bool PickleIterator::ReadData(const char** data, int* length) {
189 *length = 0;
190 *data = 0;
191
192 if (!ReadInt(length))
193 return false;
194
195 return ReadBytes(data, *length);
196 }
197
198 bool PickleIterator::ReadBytes(const char** data, int length) {
199 const char* read_from = GetReadPointerAndAdvance(length);
200 if (!read_from)
201 return false;
202 *data = read_from;
203 return true;
204 }
205
206 // Payload is uint32 aligned.
207
208 Pickle::Pickle()
209 : header_(NULL),
210 header_size_(sizeof(Header)),
211 capacity_after_header_(0),
212 write_offset_(0) {
213 Resize(kPayloadUnit);
214 header_->payload_size = 0;
215 }
216
217 Pickle::Pickle(int header_size)
218 : header_(NULL),
219 header_size_(AlignInt(header_size, sizeof(uint32))),
220 capacity_after_header_(0),
221 write_offset_(0) {
222 DCHECK_GE(static_cast<size_t>(header_size), sizeof(Header));
223 DCHECK_LE(header_size, kPayloadUnit);
224 Resize(kPayloadUnit);
225 header_->payload_size = 0;
226 }
227
228 Pickle::Pickle(const char* data, int data_len)
229 : header_(reinterpret_cast<Header*>(const_cast<char*>(data))),
230 header_size_(0),
231 capacity_after_header_(kCapacityReadOnly),
232 write_offset_(0) {
233 if (data_len >= static_cast<int>(sizeof(Header)))
234 header_size_ = data_len - header_->payload_size;
235
236 if (header_size_ > static_cast<unsigned int>(data_len))
237 header_size_ = 0;
238
239 if (header_size_ != AlignInt(header_size_, sizeof(uint32)))
240 header_size_ = 0;
241
242 // If there is anything wrong with the data, we're not going to use it.
243 if (!header_size_)
244 header_ = NULL;
245 }
246
247 Pickle::Pickle(const Pickle& other)
248 : header_(NULL),
249 header_size_(other.header_size_),
250 capacity_after_header_(0),
251 write_offset_(other.write_offset_) {
252 size_t payload_size = header_size_ + other.header_->payload_size;
253 Resize(payload_size);
254 memcpy(header_, other.header_, payload_size);
255 }
256
257 Pickle::~Pickle() {
258 if (capacity_after_header_ != kCapacityReadOnly)
259 free(header_);
260 }
261
262 Pickle& Pickle::operator=(const Pickle& other) {
263 if (this == &other) {
264 NOTREACHED();
265 return *this;
266 }
267 if (capacity_after_header_ == kCapacityReadOnly) {
268 header_ = NULL;
269 capacity_after_header_ = 0;
270 }
271 if (header_size_ != other.header_size_) {
272 free(header_);
273 header_ = NULL;
274 header_size_ = other.header_size_;
275 }
276 Resize(other.header_->payload_size);
277 memcpy(header_, other.header_,
278 other.header_size_ + other.header_->payload_size);
279 write_offset_ = other.write_offset_;
280 return *this;
281 }
282
283 bool Pickle::WriteString(const StringPiece& value) {
284 if (!WriteInt(static_cast<int>(value.size())))
285 return false;
286
287 return WriteBytes(value.data(), static_cast<int>(value.size()));
288 }
289
290 bool Pickle::WriteString16(const StringPiece16& value) {
291 if (!WriteInt(static_cast<int>(value.size())))
292 return false;
293
294 return WriteBytes(value.data(),
295 static_cast<int>(value.size()) * sizeof(char16));
296 }
297
298 bool Pickle::WriteData(const char* data, int length) {
299 return length >= 0 && WriteInt(length) && WriteBytes(data, length);
300 }
301
302 bool Pickle::WriteBytes(const void* data, int length) {
303 WriteBytesCommon(data, length);
304 return true;
305 }
306
307 void Pickle::Reserve(size_t length) {
308 size_t data_len = AlignInt(length, sizeof(uint32));
309 DCHECK_GE(data_len, length);
310 #ifdef ARCH_CPU_64_BITS
311 DCHECK_LE(data_len, kuint32max);
312 #endif
313 DCHECK_LE(write_offset_, kuint32max - data_len);
314 size_t new_size = write_offset_ + data_len;
315 if (new_size > capacity_after_header_)
316 Resize(capacity_after_header_ * 2 + new_size);
317 }
318
319 void Pickle::Resize(size_t new_capacity) {
320 CHECK_NE(capacity_after_header_, kCapacityReadOnly);
321 capacity_after_header_ = AlignInt(new_capacity, kPayloadUnit);
322 void* p = realloc(header_, GetTotalAllocatedSize());
323 CHECK(p);
324 header_ = reinterpret_cast<Header*>(p);
325 }
326
327 size_t Pickle::GetTotalAllocatedSize() const {
328 if (capacity_after_header_ == kCapacityReadOnly)
329 return 0;
330 return header_size_ + capacity_after_header_;
331 }
332
333 // static
334 const char* Pickle::FindNext(size_t header_size,
335 const char* start,
336 const char* end) {
337 DCHECK_EQ(header_size, AlignInt(header_size, sizeof(uint32)));
338 DCHECK_LE(header_size, static_cast<size_t>(kPayloadUnit));
339
340 size_t length = static_cast<size_t>(end - start);
341 if (length < sizeof(Header))
342 return NULL;
343
344 const Header* hdr = reinterpret_cast<const Header*>(start);
345 if (length < header_size || length - header_size < hdr->payload_size)
346 return NULL;
347 return start + header_size + hdr->payload_size;
348 }
349
350 template <size_t length> void Pickle::WriteBytesStatic(const void* data) {
351 WriteBytesCommon(data, length);
352 }
353
354 template void Pickle::WriteBytesStatic<2>(const void* data);
355 template void Pickle::WriteBytesStatic<4>(const void* data);
356 template void Pickle::WriteBytesStatic<8>(const void* data);
357
358 inline void Pickle::WriteBytesCommon(const void* data, size_t length) {
359 DCHECK_NE(kCapacityReadOnly, capacity_after_header_)
360 << "oops: pickle is readonly";
361 MSAN_CHECK_MEM_IS_INITIALIZED(data, length);
362 size_t data_len = AlignInt(length, sizeof(uint32));
363 DCHECK_GE(data_len, length);
364 #ifdef ARCH_CPU_64_BITS
365 DCHECK_LE(data_len, kuint32max);
366 #endif
367 DCHECK_LE(write_offset_, kuint32max - data_len);
368 size_t new_size = write_offset_ + data_len;
369 if (new_size > capacity_after_header_) {
370 Resize(std::max(capacity_after_header_ * 2, new_size));
371 }
372
373 char* write = mutable_payload() + write_offset_;
374 memcpy(write, data, length);
375 memset(write + length, 0, data_len - length);
376 header_->payload_size = static_cast<uint32>(new_size);
377 write_offset_ = new_size;
378 }
379
380 } // namespace base
OLDNEW
« no previous file with comments | « base/pickle.h ('k') | base/pickle_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698