Limit App Host installation / usage for platform apps to Google Chome only.
[chromium-blink-merge.git] / base / pickle.cc
blob0e44131f69ba316c4c7a16b7c2c2a2a3f6f65afa
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"
7 #include <stdlib.h>
9 #include <algorithm> // for max()
11 //------------------------------------------------------------------------------
13 // static
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>();
26 if (!read_from)
27 return false;
28 if (sizeof(Type) > sizeof(uint32))
29 memcpy(result, read_from, sizeof(*result));
30 else
31 *result = *reinterpret_cast<const Type*>(read_from);
32 return true;
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_)
39 return NULL;
40 if (sizeof(Type) < sizeof(uint32))
41 read_ptr_ += AlignInt(sizeof(Type), sizeof(uint32));
42 else
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)
49 return NULL;
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))
61 return NULL;
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) {
94 int len;
95 if (!ReadInt(&len))
96 return false;
97 const char* read_from = GetReadPointerAndAdvance(len);
98 if (!read_from)
99 return false;
101 result->assign(read_from, len);
102 return true;
105 bool PickleIterator::ReadWString(std::wstring* result) {
106 int len;
107 if (!ReadInt(&len))
108 return false;
109 const char* read_from = GetReadPointerAndAdvance(len, sizeof(wchar_t));
110 if (!read_from)
111 return false;
113 result->assign(reinterpret_cast<const wchar_t*>(read_from), len);
114 return true;
117 bool PickleIterator::ReadString16(string16* result) {
118 int len;
119 if (!ReadInt(&len))
120 return false;
121 const char* read_from = GetReadPointerAndAdvance(len, sizeof(char16));
122 if (!read_from)
123 return false;
125 result->assign(reinterpret_cast<const char16*>(read_from), len);
126 return true;
129 bool PickleIterator::ReadData(const char** data, int* length) {
130 *length = 0;
131 *data = 0;
133 if (!ReadInt(length))
134 return false;
136 return ReadBytes(data, *length);
139 bool PickleIterator::ReadBytes(const char** data, int length) {
140 const char* read_from = GetReadPointerAndAdvance(length);
141 if (!read_from)
142 return false;
143 *data = read_from;
144 return true;
147 // Payload is uint32 aligned.
149 Pickle::Pickle()
150 : header_(NULL),
151 header_size_(sizeof(Header)),
152 capacity_(0),
153 variable_buffer_offset_(0) {
154 Resize(kPayloadUnit);
155 header_->payload_size = 0;
158 Pickle::Pickle(int header_size)
159 : header_(NULL),
160 header_size_(AlignInt(header_size, sizeof(uint32))),
161 capacity_(0),
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))),
171 header_size_(0),
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))
178 header_size_ = 0;
180 if (header_size_ != AlignInt(header_size_, sizeof(uint32)))
181 header_size_ = 0;
183 // If there is anything wrong with the data, we're not going to use it.
184 if (!header_size_)
185 header_ = NULL;
188 Pickle::Pickle(const Pickle& other)
189 : header_(NULL),
190 header_size_(other.header_size_),
191 capacity_(0),
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);
199 Pickle::~Pickle() {
200 if (capacity_ != kCapacityReadOnly)
201 free(header_);
204 Pickle& Pickle::operator=(const Pickle& other) {
205 if (this == &other) {
206 NOTREACHED();
207 return *this;
209 if (capacity_ == kCapacityReadOnly) {
210 header_ = NULL;
211 capacity_ = 0;
213 if (header_size_ != other.header_size_) {
214 free(header_);
215 header_ = NULL;
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_;
223 return *this;
226 bool Pickle::WriteString(const std::string& value) {
227 if (!WriteInt(static_cast<int>(value.size())))
228 return false;
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())))
235 return false;
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())))
243 return false;
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);
257 if (!dest)
258 return false;
260 memcpy(dest, data, data_len);
262 EndWrite(dest, data_len);
263 return true;
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))
271 return NULL;
273 char *data_ptr = BeginWrite(length);
274 if (!data_ptr)
275 return NULL;
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);
283 return data_ptr;
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.";
295 return;
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)))
310 return NULL;
312 #ifdef ARCH_CPU_64_BITS
313 DCHECK_LE(length, kuint32max);
314 #endif
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
322 // memory.
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);
332 if (!p)
333 return false;
335 header_ = reinterpret_cast<Header*>(p);
336 capacity_ = new_capacity;
337 return true;
340 // static
341 const char* Pickle::FindNext(size_t header_size,
342 const char* start,
343 const char* end) {
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))
348 return NULL;
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)
354 return NULL;
356 return (payload_end > end) ? NULL : payload_end;