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 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 if (!ReadInt(&len)) | 145 if (!ReadInt(&len)) |
146 return false; | 146 return false; |
147 const char* read_from = GetReadPointerAndAdvance(len); | 147 const char* read_from = GetReadPointerAndAdvance(len); |
148 if (!read_from) | 148 if (!read_from) |
149 return false; | 149 return false; |
150 | 150 |
151 result->assign(read_from, len); | 151 result->assign(read_from, len); |
152 return true; | 152 return true; |
153 } | 153 } |
154 | 154 |
| 155 bool PickleIterator::ReadStringPiece(base::StringPiece* result) { |
| 156 int len; |
| 157 if (!ReadInt(&len)) |
| 158 return false; |
| 159 const char* read_from = GetReadPointerAndAdvance(len); |
| 160 if (!read_from) |
| 161 return false; |
| 162 |
| 163 *result = base::StringPiece(read_from, len); |
| 164 return true; |
| 165 } |
| 166 |
155 bool PickleIterator::ReadWString(std::wstring* result) { | 167 bool PickleIterator::ReadWString(std::wstring* result) { |
156 int len; | 168 int len; |
157 if (!ReadInt(&len)) | 169 if (!ReadInt(&len)) |
158 return false; | 170 return false; |
159 const char* read_from = GetReadPointerAndAdvance(len, sizeof(wchar_t)); | 171 const char* read_from = GetReadPointerAndAdvance(len, sizeof(wchar_t)); |
160 if (!read_from) | 172 if (!read_from) |
161 return false; | 173 return false; |
162 | 174 |
163 result->assign(reinterpret_cast<const wchar_t*>(read_from), len); | 175 result->assign(reinterpret_cast<const wchar_t*>(read_from), len); |
164 return true; | 176 return true; |
165 } | 177 } |
166 | 178 |
167 bool PickleIterator::ReadString16(string16* result) { | 179 bool PickleIterator::ReadString16(string16* result) { |
168 int len; | 180 int len; |
169 if (!ReadInt(&len)) | 181 if (!ReadInt(&len)) |
170 return false; | 182 return false; |
171 const char* read_from = GetReadPointerAndAdvance(len, sizeof(char16)); | 183 const char* read_from = GetReadPointerAndAdvance(len, sizeof(char16)); |
172 if (!read_from) | 184 if (!read_from) |
173 return false; | 185 return false; |
174 | 186 |
175 result->assign(reinterpret_cast<const char16*>(read_from), len); | 187 result->assign(reinterpret_cast<const char16*>(read_from), len); |
176 return true; | 188 return true; |
177 } | 189 } |
178 | 190 |
| 191 bool PickleIterator::ReadStringPiece16(base::StringPiece16* result) { |
| 192 int len; |
| 193 if (!ReadInt(&len)) |
| 194 return false; |
| 195 const char* read_from = GetReadPointerAndAdvance(len, sizeof(char16)); |
| 196 if (!read_from) |
| 197 return false; |
| 198 |
| 199 *result = base::StringPiece16(reinterpret_cast<const char16*>(read_from), |
| 200 len); |
| 201 return true; |
| 202 } |
| 203 |
179 bool PickleIterator::ReadData(const char** data, int* length) { | 204 bool PickleIterator::ReadData(const char** data, int* length) { |
180 *length = 0; | 205 *length = 0; |
181 *data = 0; | 206 *data = 0; |
182 | 207 |
183 if (!ReadInt(length)) | 208 if (!ReadInt(length)) |
184 return false; | 209 return false; |
185 | 210 |
186 return ReadBytes(data, *length); | 211 return ReadBytes(data, *length); |
187 } | 212 } |
188 | 213 |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 header_ = NULL; | 289 header_ = NULL; |
265 header_size_ = other.header_size_; | 290 header_size_ = other.header_size_; |
266 } | 291 } |
267 Resize(other.header_->payload_size); | 292 Resize(other.header_->payload_size); |
268 memcpy(header_, other.header_, | 293 memcpy(header_, other.header_, |
269 other.header_size_ + other.header_->payload_size); | 294 other.header_size_ + other.header_->payload_size); |
270 write_offset_ = other.write_offset_; | 295 write_offset_ = other.write_offset_; |
271 return *this; | 296 return *this; |
272 } | 297 } |
273 | 298 |
274 bool Pickle::WriteString(const std::string& value) { | 299 bool Pickle::WriteString(const base::StringPiece& value) { |
275 if (!WriteInt(static_cast<int>(value.size()))) | 300 if (!WriteInt(static_cast<int>(value.size()))) |
276 return false; | 301 return false; |
277 | 302 |
278 return WriteBytes(value.data(), static_cast<int>(value.size())); | 303 return WriteBytes(value.data(), static_cast<int>(value.size())); |
279 } | 304 } |
280 | 305 |
281 bool Pickle::WriteWString(const std::wstring& value) { | 306 bool Pickle::WriteWString(const std::wstring& value) { |
282 if (!WriteInt(static_cast<int>(value.size()))) | 307 if (!WriteInt(static_cast<int>(value.size()))) |
283 return false; | 308 return false; |
284 | 309 |
285 return WriteBytes(value.data(), | 310 return WriteBytes(value.data(), |
286 static_cast<int>(value.size() * sizeof(wchar_t))); | 311 static_cast<int>(value.size() * sizeof(wchar_t))); |
287 } | 312 } |
288 | 313 |
289 bool Pickle::WriteString16(const string16& value) { | 314 bool Pickle::WriteString16(const base::StringPiece16& value) { |
290 if (!WriteInt(static_cast<int>(value.size()))) | 315 if (!WriteInt(static_cast<int>(value.size()))) |
291 return false; | 316 return false; |
292 | 317 |
293 return WriteBytes(value.data(), | 318 return WriteBytes(value.data(), |
294 static_cast<int>(value.size()) * sizeof(char16)); | 319 static_cast<int>(value.size()) * sizeof(char16)); |
295 } | 320 } |
296 | 321 |
297 bool Pickle::WriteData(const char* data, int length) { | 322 bool Pickle::WriteData(const char* data, int length) { |
298 return length >= 0 && WriteInt(length) && WriteBytes(data, length); | 323 return length >= 0 && WriteInt(length) && WriteBytes(data, length); |
299 } | 324 } |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
363 if (new_size > capacity_after_header_) { | 388 if (new_size > capacity_after_header_) { |
364 Resize(std::max(capacity_after_header_ * 2, new_size)); | 389 Resize(std::max(capacity_after_header_ * 2, new_size)); |
365 } | 390 } |
366 | 391 |
367 char* write = mutable_payload() + write_offset_; | 392 char* write = mutable_payload() + write_offset_; |
368 memcpy(write, data, length); | 393 memcpy(write, data, length); |
369 memset(write + length, 0, data_len - length); | 394 memset(write + length, 0, data_len - length); |
370 header_->payload_size = static_cast<uint32>(new_size); | 395 header_->payload_size = static_cast<uint32>(new_size); |
371 write_offset_ = new_size; | 396 write_offset_ = new_size; |
372 } | 397 } |
OLD | NEW |