| 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 |
| 11 //------------------------------------------------------------------------------ | 11 namespace base { |
| 12 | |
| 13 using base::char16; | |
| 14 using base::string16; | |
| 15 | 12 |
| 16 // static | 13 // static |
| 17 const int Pickle::kPayloadUnit = 64; | 14 const int Pickle::kPayloadUnit = 64; |
| 18 | 15 |
| 19 static const size_t kCapacityReadOnly = static_cast<size_t>(-1); | 16 static const size_t kCapacityReadOnly = static_cast<size_t>(-1); |
| 20 | 17 |
| 21 PickleIterator::PickleIterator(const Pickle& pickle) | 18 PickleIterator::PickleIterator(const Pickle& pickle) |
| 22 : payload_(pickle.payload()), | 19 : payload_(pickle.payload()), |
| 23 read_index_(0), | 20 read_index_(0), |
| 24 end_index_(pickle.payload_size()) { | 21 end_index_(pickle.payload_size()) { |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 if (!ReadInt(&len)) | 142 if (!ReadInt(&len)) |
| 146 return false; | 143 return false; |
| 147 const char* read_from = GetReadPointerAndAdvance(len); | 144 const char* read_from = GetReadPointerAndAdvance(len); |
| 148 if (!read_from) | 145 if (!read_from) |
| 149 return false; | 146 return false; |
| 150 | 147 |
| 151 result->assign(read_from, len); | 148 result->assign(read_from, len); |
| 152 return true; | 149 return true; |
| 153 } | 150 } |
| 154 | 151 |
| 155 bool PickleIterator::ReadStringPiece(base::StringPiece* result) { | 152 bool PickleIterator::ReadStringPiece(StringPiece* result) { |
| 156 int len; | 153 int len; |
| 157 if (!ReadInt(&len)) | 154 if (!ReadInt(&len)) |
| 158 return false; | 155 return false; |
| 159 const char* read_from = GetReadPointerAndAdvance(len); | 156 const char* read_from = GetReadPointerAndAdvance(len); |
| 160 if (!read_from) | 157 if (!read_from) |
| 161 return false; | 158 return false; |
| 162 | 159 |
| 163 *result = base::StringPiece(read_from, len); | 160 *result = StringPiece(read_from, len); |
| 164 return true; | 161 return true; |
| 165 } | 162 } |
| 166 | 163 |
| 167 bool PickleIterator::ReadString16(string16* result) { | 164 bool PickleIterator::ReadString16(string16* result) { |
| 168 int len; | 165 int len; |
| 169 if (!ReadInt(&len)) | 166 if (!ReadInt(&len)) |
| 170 return false; | 167 return false; |
| 171 const char* read_from = GetReadPointerAndAdvance(len, sizeof(char16)); | 168 const char* read_from = GetReadPointerAndAdvance(len, sizeof(char16)); |
| 172 if (!read_from) | 169 if (!read_from) |
| 173 return false; | 170 return false; |
| 174 | 171 |
| 175 result->assign(reinterpret_cast<const char16*>(read_from), len); | 172 result->assign(reinterpret_cast<const char16*>(read_from), len); |
| 176 return true; | 173 return true; |
| 177 } | 174 } |
| 178 | 175 |
| 179 bool PickleIterator::ReadStringPiece16(base::StringPiece16* result) { | 176 bool PickleIterator::ReadStringPiece16(StringPiece16* result) { |
| 180 int len; | 177 int len; |
| 181 if (!ReadInt(&len)) | 178 if (!ReadInt(&len)) |
| 182 return false; | 179 return false; |
| 183 const char* read_from = GetReadPointerAndAdvance(len, sizeof(char16)); | 180 const char* read_from = GetReadPointerAndAdvance(len, sizeof(char16)); |
| 184 if (!read_from) | 181 if (!read_from) |
| 185 return false; | 182 return false; |
| 186 | 183 |
| 187 *result = base::StringPiece16(reinterpret_cast<const char16*>(read_from), | 184 *result = StringPiece16(reinterpret_cast<const char16*>(read_from), len); |
| 188 len); | |
| 189 return true; | 185 return true; |
| 190 } | 186 } |
| 191 | 187 |
| 192 bool PickleIterator::ReadData(const char** data, int* length) { | 188 bool PickleIterator::ReadData(const char** data, int* length) { |
| 193 *length = 0; | 189 *length = 0; |
| 194 *data = 0; | 190 *data = 0; |
| 195 | 191 |
| 196 if (!ReadInt(length)) | 192 if (!ReadInt(length)) |
| 197 return false; | 193 return false; |
| 198 | 194 |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 277 header_ = NULL; | 273 header_ = NULL; |
| 278 header_size_ = other.header_size_; | 274 header_size_ = other.header_size_; |
| 279 } | 275 } |
| 280 Resize(other.header_->payload_size); | 276 Resize(other.header_->payload_size); |
| 281 memcpy(header_, other.header_, | 277 memcpy(header_, other.header_, |
| 282 other.header_size_ + other.header_->payload_size); | 278 other.header_size_ + other.header_->payload_size); |
| 283 write_offset_ = other.write_offset_; | 279 write_offset_ = other.write_offset_; |
| 284 return *this; | 280 return *this; |
| 285 } | 281 } |
| 286 | 282 |
| 287 bool Pickle::WriteString(const base::StringPiece& value) { | 283 bool Pickle::WriteString(const StringPiece& value) { |
| 288 if (!WriteInt(static_cast<int>(value.size()))) | 284 if (!WriteInt(static_cast<int>(value.size()))) |
| 289 return false; | 285 return false; |
| 290 | 286 |
| 291 return WriteBytes(value.data(), static_cast<int>(value.size())); | 287 return WriteBytes(value.data(), static_cast<int>(value.size())); |
| 292 } | 288 } |
| 293 | 289 |
| 294 bool Pickle::WriteString16(const base::StringPiece16& value) { | 290 bool Pickle::WriteString16(const StringPiece16& value) { |
| 295 if (!WriteInt(static_cast<int>(value.size()))) | 291 if (!WriteInt(static_cast<int>(value.size()))) |
| 296 return false; | 292 return false; |
| 297 | 293 |
| 298 return WriteBytes(value.data(), | 294 return WriteBytes(value.data(), |
| 299 static_cast<int>(value.size()) * sizeof(char16)); | 295 static_cast<int>(value.size()) * sizeof(char16)); |
| 300 } | 296 } |
| 301 | 297 |
| 302 bool Pickle::WriteData(const char* data, int length) { | 298 bool Pickle::WriteData(const char* data, int length) { |
| 303 return length >= 0 && WriteInt(length) && WriteBytes(data, length); | 299 return length >= 0 && WriteInt(length) && WriteBytes(data, length); |
| 304 } | 300 } |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 369 if (new_size > capacity_after_header_) { | 365 if (new_size > capacity_after_header_) { |
| 370 Resize(std::max(capacity_after_header_ * 2, new_size)); | 366 Resize(std::max(capacity_after_header_ * 2, new_size)); |
| 371 } | 367 } |
| 372 | 368 |
| 373 char* write = mutable_payload() + write_offset_; | 369 char* write = mutable_payload() + write_offset_; |
| 374 memcpy(write, data, length); | 370 memcpy(write, data, length); |
| 375 memset(write + length, 0, data_len - length); | 371 memset(write + length, 0, data_len - length); |
| 376 header_->payload_size = static_cast<uint32>(new_size); | 372 header_->payload_size = static_cast<uint32>(new_size); |
| 377 write_offset_ = new_size; | 373 write_offset_ = new_size; |
| 378 } | 374 } |
| 375 |
| 376 } // namespace base |
| OLD | NEW |