| 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 #ifndef BASE_PICKLE_H_ | 5 #ifndef BASE_PICKLE_H_ |
| 6 #define BASE_PICKLE_H_ | 6 #define BASE_PICKLE_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/base_export.h" | 10 #include "base/base_export.h" |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/strings/string16.h" | 15 #include "base/strings/string16.h" |
| 16 #include "base/strings/string_piece.h" | 16 #include "base/strings/string_piece.h" |
| 17 | 17 |
| 18 namespace base { |
| 19 |
| 18 class Pickle; | 20 class Pickle; |
| 19 | 21 |
| 20 // PickleIterator reads data from a Pickle. The Pickle object must remain valid | 22 // PickleIterator reads data from a Pickle. The Pickle object must remain valid |
| 21 // while the PickleIterator object is in use. | 23 // while the PickleIterator object is in use. |
| 22 class BASE_EXPORT PickleIterator { | 24 class BASE_EXPORT PickleIterator { |
| 23 public: | 25 public: |
| 24 PickleIterator() : payload_(NULL), read_index_(0), end_index_(0) {} | 26 PickleIterator() : payload_(NULL), read_index_(0), end_index_(0) {} |
| 25 explicit PickleIterator(const Pickle& pickle); | 27 explicit PickleIterator(const Pickle& pickle); |
| 26 | 28 |
| 27 // Methods for reading the payload of the Pickle. To read from the start of | 29 // Methods for reading the payload of the Pickle. To read from the start of |
| 28 // the Pickle, create a PickleIterator from a Pickle. If successful, these | 30 // the Pickle, create a PickleIterator from a Pickle. If successful, these |
| 29 // methods return true. Otherwise, false is returned to indicate that the | 31 // methods return true. Otherwise, false is returned to indicate that the |
| 30 // result could not be extracted. It is not possible to read from the iterator | 32 // result could not be extracted. It is not possible to read from the iterator |
| 31 // after that. | 33 // after that. |
| 32 bool ReadBool(bool* result) WARN_UNUSED_RESULT; | 34 bool ReadBool(bool* result) WARN_UNUSED_RESULT; |
| 33 bool ReadInt(int* result) WARN_UNUSED_RESULT; | 35 bool ReadInt(int* result) WARN_UNUSED_RESULT; |
| 34 bool ReadLong(long* result) WARN_UNUSED_RESULT; | 36 bool ReadLong(long* result) WARN_UNUSED_RESULT; |
| 35 bool ReadUInt16(uint16* result) WARN_UNUSED_RESULT; | 37 bool ReadUInt16(uint16* result) WARN_UNUSED_RESULT; |
| 36 bool ReadUInt32(uint32* result) WARN_UNUSED_RESULT; | 38 bool ReadUInt32(uint32* result) WARN_UNUSED_RESULT; |
| 37 bool ReadInt64(int64* result) WARN_UNUSED_RESULT; | 39 bool ReadInt64(int64* result) WARN_UNUSED_RESULT; |
| 38 bool ReadUInt64(uint64* result) WARN_UNUSED_RESULT; | 40 bool ReadUInt64(uint64* result) WARN_UNUSED_RESULT; |
| 39 bool ReadSizeT(size_t* result) WARN_UNUSED_RESULT; | 41 bool ReadSizeT(size_t* result) WARN_UNUSED_RESULT; |
| 40 bool ReadFloat(float* result) WARN_UNUSED_RESULT; | 42 bool ReadFloat(float* result) WARN_UNUSED_RESULT; |
| 41 bool ReadDouble(double* result) WARN_UNUSED_RESULT; | 43 bool ReadDouble(double* result) WARN_UNUSED_RESULT; |
| 42 bool ReadString(std::string* result) WARN_UNUSED_RESULT; | 44 bool ReadString(std::string* result) WARN_UNUSED_RESULT; |
| 43 // The StringPiece data will only be valid for the lifetime of the message. | 45 // The StringPiece data will only be valid for the lifetime of the message. |
| 44 bool ReadStringPiece(base::StringPiece* result) WARN_UNUSED_RESULT; | 46 bool ReadStringPiece(StringPiece* result) WARN_UNUSED_RESULT; |
| 45 bool ReadString16(base::string16* result) WARN_UNUSED_RESULT; | 47 bool ReadString16(string16* result) WARN_UNUSED_RESULT; |
| 46 // The StringPiece16 data will only be valid for the lifetime of the message. | 48 // The StringPiece16 data will only be valid for the lifetime of the message. |
| 47 bool ReadStringPiece16(base::StringPiece16* result) WARN_UNUSED_RESULT; | 49 bool ReadStringPiece16(StringPiece16* result) WARN_UNUSED_RESULT; |
| 48 | 50 |
| 49 // A pointer to the data will be placed in |*data|, and the length will be | 51 // A pointer to the data will be placed in |*data|, and the length will be |
| 50 // placed in |*length|. The pointer placed into |*data| points into the | 52 // placed in |*length|. The pointer placed into |*data| points into the |
| 51 // message's buffer so it will be scoped to the lifetime of the message (or | 53 // message's buffer so it will be scoped to the lifetime of the message (or |
| 52 // until the message data is mutated). Do not keep the pointer around! | 54 // until the message data is mutated). Do not keep the pointer around! |
| 53 bool ReadData(const char** data, int* length) WARN_UNUSED_RESULT; | 55 bool ReadData(const char** data, int* length) WARN_UNUSED_RESULT; |
| 54 | 56 |
| 55 // A pointer to the data will be placed in |*data|. The caller specifies the | 57 // A pointer to the data will be placed in |*data|. The caller specifies the |
| 56 // number of bytes to read, and ReadBytes will validate this length. The | 58 // number of bytes to read, and ReadBytes will validate this length. The |
| 57 // pointer placed into |*data| points into the message's buffer so it will be | 59 // pointer placed into |*data| points into the message's buffer so it will be |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 // Always write size_t as a 64-bit value to ensure compatibility between | 194 // Always write size_t as a 64-bit value to ensure compatibility between |
| 193 // 32-bit and 64-bit processes. | 195 // 32-bit and 64-bit processes. |
| 194 return WritePOD(static_cast<uint64>(value)); | 196 return WritePOD(static_cast<uint64>(value)); |
| 195 } | 197 } |
| 196 bool WriteFloat(float value) { | 198 bool WriteFloat(float value) { |
| 197 return WritePOD(value); | 199 return WritePOD(value); |
| 198 } | 200 } |
| 199 bool WriteDouble(double value) { | 201 bool WriteDouble(double value) { |
| 200 return WritePOD(value); | 202 return WritePOD(value); |
| 201 } | 203 } |
| 202 bool WriteString(const base::StringPiece& value); | 204 bool WriteString(const StringPiece& value); |
| 203 bool WriteString16(const base::StringPiece16& value); | 205 bool WriteString16(const StringPiece16& value); |
| 204 // "Data" is a blob with a length. When you read it out you will be given the | 206 // "Data" is a blob with a length. When you read it out you will be given the |
| 205 // length. See also WriteBytes. | 207 // length. See also WriteBytes. |
| 206 bool WriteData(const char* data, int length); | 208 bool WriteData(const char* data, int length); |
| 207 // "Bytes" is a blob with no length. The caller must specify the length both | 209 // "Bytes" is a blob with no length. The caller must specify the length both |
| 208 // when reading and writing. It is normally used to serialize PoD types of a | 210 // when reading and writing. It is normally used to serialize PoD types of a |
| 209 // known size. See also WriteData. | 211 // known size. See also WriteData. |
| 210 bool WriteBytes(const void* data, int length); | 212 bool WriteBytes(const void* data, int length); |
| 211 | 213 |
| 212 // Reserves space for upcoming writes when multiple writes will be made and | 214 // Reserves space for upcoming writes when multiple writes will be made and |
| 213 // their sizes are computed in advance. It can be significantly faster to call | 215 // their sizes are computed in advance. It can be significantly faster to call |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 return true; | 299 return true; |
| 298 } | 300 } |
| 299 inline void WriteBytesCommon(const void* data, size_t length); | 301 inline void WriteBytesCommon(const void* data, size_t length); |
| 300 | 302 |
| 301 FRIEND_TEST_ALL_PREFIXES(PickleTest, Resize); | 303 FRIEND_TEST_ALL_PREFIXES(PickleTest, Resize); |
| 302 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNext); | 304 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNext); |
| 303 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextWithIncompleteHeader); | 305 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextWithIncompleteHeader); |
| 304 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextOverflow); | 306 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextOverflow); |
| 305 }; | 307 }; |
| 306 | 308 |
| 309 } // namespace base |
| 310 |
| 307 #endif // BASE_PICKLE_H_ | 311 #endif // BASE_PICKLE_H_ |
| OLD | NEW |