OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/pickle.h" | 5 #include "base/pickle.h" |
6 | 6 |
7 #include <stdlib.h> | 7 #include <stdlib.h> |
8 | 8 |
9 #include <algorithm> // for max() | 9 #include <algorithm> // for max() |
10 | 10 |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 if (!ReadInt(&len)) | 157 if (!ReadInt(&len)) |
158 return false; | 158 return false; |
159 const char* read_from = GetReadPointerAndAdvance(len); | 159 const char* read_from = GetReadPointerAndAdvance(len); |
160 if (!read_from) | 160 if (!read_from) |
161 return false; | 161 return false; |
162 | 162 |
163 *result = base::StringPiece(read_from, len); | 163 *result = base::StringPiece(read_from, len); |
164 return true; | 164 return true; |
165 } | 165 } |
166 | 166 |
167 bool PickleIterator::ReadWString(std::wstring* result) { | |
168 int len; | |
169 if (!ReadInt(&len)) | |
170 return false; | |
171 const char* read_from = GetReadPointerAndAdvance(len, sizeof(wchar_t)); | |
172 if (!read_from) | |
173 return false; | |
174 | |
175 result->assign(reinterpret_cast<const wchar_t*>(read_from), len); | |
176 return true; | |
177 } | |
178 | |
179 bool PickleIterator::ReadString16(string16* result) { | 167 bool PickleIterator::ReadString16(string16* result) { |
180 int len; | 168 int len; |
181 if (!ReadInt(&len)) | 169 if (!ReadInt(&len)) |
182 return false; | 170 return false; |
183 const char* read_from = GetReadPointerAndAdvance(len, sizeof(char16)); | 171 const char* read_from = GetReadPointerAndAdvance(len, sizeof(char16)); |
184 if (!read_from) | 172 if (!read_from) |
185 return false; | 173 return false; |
186 | 174 |
187 result->assign(reinterpret_cast<const char16*>(read_from), len); | 175 result->assign(reinterpret_cast<const char16*>(read_from), len); |
188 return true; | 176 return true; |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
296 return *this; | 284 return *this; |
297 } | 285 } |
298 | 286 |
299 bool Pickle::WriteString(const base::StringPiece& value) { | 287 bool Pickle::WriteString(const base::StringPiece& value) { |
300 if (!WriteInt(static_cast<int>(value.size()))) | 288 if (!WriteInt(static_cast<int>(value.size()))) |
301 return false; | 289 return false; |
302 | 290 |
303 return WriteBytes(value.data(), static_cast<int>(value.size())); | 291 return WriteBytes(value.data(), static_cast<int>(value.size())); |
304 } | 292 } |
305 | 293 |
306 bool Pickle::WriteWString(const std::wstring& value) { | |
307 if (!WriteInt(static_cast<int>(value.size()))) | |
308 return false; | |
309 | |
310 return WriteBytes(value.data(), | |
311 static_cast<int>(value.size() * sizeof(wchar_t))); | |
312 } | |
313 | |
314 bool Pickle::WriteString16(const base::StringPiece16& value) { | 294 bool Pickle::WriteString16(const base::StringPiece16& value) { |
315 if (!WriteInt(static_cast<int>(value.size()))) | 295 if (!WriteInt(static_cast<int>(value.size()))) |
316 return false; | 296 return false; |
317 | 297 |
318 return WriteBytes(value.data(), | 298 return WriteBytes(value.data(), |
319 static_cast<int>(value.size()) * sizeof(char16)); | 299 static_cast<int>(value.size()) * sizeof(char16)); |
320 } | 300 } |
321 | 301 |
322 bool Pickle::WriteData(const char* data, int length) { | 302 bool Pickle::WriteData(const char* data, int length) { |
323 return length >= 0 && WriteInt(length) && WriteBytes(data, length); | 303 return length >= 0 && WriteInt(length) && WriteBytes(data, length); |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
388 if (new_size > capacity_after_header_) { | 368 if (new_size > capacity_after_header_) { |
389 Resize(std::max(capacity_after_header_ * 2, new_size)); | 369 Resize(std::max(capacity_after_header_ * 2, new_size)); |
390 } | 370 } |
391 | 371 |
392 char* write = mutable_payload() + write_offset_; | 372 char* write = mutable_payload() + write_offset_; |
393 memcpy(write, data, length); | 373 memcpy(write, data, length); |
394 memset(write + length, 0, data_len - length); | 374 memset(write + length, 0, data_len - length); |
395 header_->payload_size = static_cast<uint32>(new_size); | 375 header_->payload_size = static_cast<uint32>(new_size); |
396 write_offset_ = new_size; | 376 write_offset_ = new_size; |
397 } | 377 } |
OLD | NEW |