| 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 <stdint.h> |
| 9 |
| 8 #include <string> | 10 #include <string> |
| 9 | 11 |
| 10 #include "base/base_export.h" | 12 #include "base/base_export.h" |
| 11 #include "base/basictypes.h" | |
| 12 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 13 #include "base/gtest_prod_util.h" | 14 #include "base/gtest_prod_util.h" |
| 14 #include "base/logging.h" | 15 #include "base/logging.h" |
| 15 #include "base/strings/string16.h" | 16 #include "base/strings/string16.h" |
| 16 #include "base/strings/string_piece.h" | 17 #include "base/strings/string_piece.h" |
| 17 | 18 |
| 18 namespace base { | 19 namespace base { |
| 19 | 20 |
| 20 class Pickle; | 21 class Pickle; |
| 21 | 22 |
| 22 // PickleIterator reads data from a Pickle. The Pickle object must remain valid | 23 // PickleIterator reads data from a Pickle. The Pickle object must remain valid |
| 23 // while the PickleIterator object is in use. | 24 // while the PickleIterator object is in use. |
| 24 class BASE_EXPORT PickleIterator { | 25 class BASE_EXPORT PickleIterator { |
| 25 public: | 26 public: |
| 26 PickleIterator() : payload_(NULL), read_index_(0), end_index_(0) {} | 27 PickleIterator() : payload_(NULL), read_index_(0), end_index_(0) {} |
| 27 explicit PickleIterator(const Pickle& pickle); | 28 explicit PickleIterator(const Pickle& pickle); |
| 28 | 29 |
| 29 // Methods for reading the payload of the Pickle. To read from the start of | 30 // Methods for reading the payload of the Pickle. To read from the start of |
| 30 // the Pickle, create a PickleIterator from a Pickle. If successful, these | 31 // the Pickle, create a PickleIterator from a Pickle. If successful, these |
| 31 // methods return true. Otherwise, false is returned to indicate that the | 32 // methods return true. Otherwise, false is returned to indicate that the |
| 32 // result could not be extracted. It is not possible to read from the iterator | 33 // result could not be extracted. It is not possible to read from the iterator |
| 33 // after that. | 34 // after that. |
| 34 bool ReadBool(bool* result) WARN_UNUSED_RESULT; | 35 bool ReadBool(bool* result) WARN_UNUSED_RESULT; |
| 35 bool ReadInt(int* result) WARN_UNUSED_RESULT; | 36 bool ReadInt(int* result) WARN_UNUSED_RESULT; |
| 36 bool ReadLong(long* result) WARN_UNUSED_RESULT; | 37 bool ReadLong(long* result) WARN_UNUSED_RESULT; |
| 37 bool ReadUInt16(uint16* result) WARN_UNUSED_RESULT; | 38 bool ReadUInt16(uint16_t* result) WARN_UNUSED_RESULT; |
| 38 bool ReadUInt32(uint32* result) WARN_UNUSED_RESULT; | 39 bool ReadUInt32(uint32_t* result) WARN_UNUSED_RESULT; |
| 39 bool ReadInt64(int64* result) WARN_UNUSED_RESULT; | 40 bool ReadInt64(int64_t* result) WARN_UNUSED_RESULT; |
| 40 bool ReadUInt64(uint64* result) WARN_UNUSED_RESULT; | 41 bool ReadUInt64(uint64_t* result) WARN_UNUSED_RESULT; |
| 41 bool ReadSizeT(size_t* result) WARN_UNUSED_RESULT; | 42 bool ReadSizeT(size_t* result) WARN_UNUSED_RESULT; |
| 42 bool ReadFloat(float* result) WARN_UNUSED_RESULT; | 43 bool ReadFloat(float* result) WARN_UNUSED_RESULT; |
| 43 bool ReadDouble(double* result) WARN_UNUSED_RESULT; | 44 bool ReadDouble(double* result) WARN_UNUSED_RESULT; |
| 44 bool ReadString(std::string* result) WARN_UNUSED_RESULT; | 45 bool ReadString(std::string* result) WARN_UNUSED_RESULT; |
| 45 // The StringPiece data will only be valid for the lifetime of the message. | 46 // The StringPiece data will only be valid for the lifetime of the message. |
| 46 bool ReadStringPiece(StringPiece* result) WARN_UNUSED_RESULT; | 47 bool ReadStringPiece(StringPiece* result) WARN_UNUSED_RESULT; |
| 47 bool ReadString16(string16* result) WARN_UNUSED_RESULT; | 48 bool ReadString16(string16* result) WARN_UNUSED_RESULT; |
| 48 // The StringPiece16 data will only be valid for the lifetime of the message. | 49 // The StringPiece16 data will only be valid for the lifetime of the message. |
| 49 bool ReadStringPiece16(StringPiece16* result) WARN_UNUSED_RESULT; | 50 bool ReadStringPiece16(StringPiece16* result) WARN_UNUSED_RESULT; |
| 50 | 51 |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 return WritePOD(value); | 173 return WritePOD(value); |
| 173 } | 174 } |
| 174 // WARNING: DO NOT USE THIS METHOD IF PICKLES ARE PERSISTED IN ANY WAY. | 175 // WARNING: DO NOT USE THIS METHOD IF PICKLES ARE PERSISTED IN ANY WAY. |
| 175 // It will write whatever a "long" is on this architecture. On 32-bit | 176 // It will write whatever a "long" is on this architecture. On 32-bit |
| 176 // platforms, it is 32 bits. On 64-bit platforms, it is 64 bits. If persisted | 177 // platforms, it is 32 bits. On 64-bit platforms, it is 64 bits. If persisted |
| 177 // pickles are still around after upgrading to 64-bit, or if they are copied | 178 // pickles are still around after upgrading to 64-bit, or if they are copied |
| 178 // between dissimilar systems, YOUR PICKLES WILL HAVE GONE BAD. | 179 // between dissimilar systems, YOUR PICKLES WILL HAVE GONE BAD. |
| 179 bool WriteLongUsingDangerousNonPortableLessPersistableForm(long value) { | 180 bool WriteLongUsingDangerousNonPortableLessPersistableForm(long value) { |
| 180 return WritePOD(value); | 181 return WritePOD(value); |
| 181 } | 182 } |
| 182 bool WriteUInt16(uint16 value) { | 183 bool WriteUInt16(uint16_t value) { return WritePOD(value); } |
| 183 return WritePOD(value); | 184 bool WriteUInt32(uint32_t value) { return WritePOD(value); } |
| 184 } | 185 bool WriteInt64(int64_t value) { return WritePOD(value); } |
| 185 bool WriteUInt32(uint32 value) { | 186 bool WriteUInt64(uint64_t value) { return WritePOD(value); } |
| 186 return WritePOD(value); | |
| 187 } | |
| 188 bool WriteInt64(int64 value) { | |
| 189 return WritePOD(value); | |
| 190 } | |
| 191 bool WriteUInt64(uint64 value) { | |
| 192 return WritePOD(value); | |
| 193 } | |
| 194 bool WriteSizeT(size_t value) { | 187 bool WriteSizeT(size_t value) { |
| 195 // Always write size_t as a 64-bit value to ensure compatibility between | 188 // Always write size_t as a 64-bit value to ensure compatibility between |
| 196 // 32-bit and 64-bit processes. | 189 // 32-bit and 64-bit processes. |
| 197 return WritePOD(static_cast<uint64>(value)); | 190 return WritePOD(static_cast<uint64_t>(value)); |
| 198 } | 191 } |
| 199 bool WriteFloat(float value) { | 192 bool WriteFloat(float value) { |
| 200 return WritePOD(value); | 193 return WritePOD(value); |
| 201 } | 194 } |
| 202 bool WriteDouble(double value) { | 195 bool WriteDouble(double value) { |
| 203 return WritePOD(value); | 196 return WritePOD(value); |
| 204 } | 197 } |
| 205 bool WriteString(const StringPiece& value); | 198 bool WriteString(const StringPiece& value); |
| 206 bool WriteString16(const StringPiece16& value); | 199 bool WriteString16(const StringPiece16& value); |
| 207 // "Data" is a blob with a length. When you read it out you will be given the | 200 // "Data" is a blob with a length. When you read it out you will be given the |
| 208 // length. See also WriteBytes. | 201 // length. See also WriteBytes. |
| 209 bool WriteData(const char* data, int length); | 202 bool WriteData(const char* data, int length); |
| 210 // "Bytes" is a blob with no length. The caller must specify the length both | 203 // "Bytes" is a blob with no length. The caller must specify the length both |
| 211 // when reading and writing. It is normally used to serialize PoD types of a | 204 // when reading and writing. It is normally used to serialize PoD types of a |
| 212 // known size. See also WriteData. | 205 // known size. See also WriteData. |
| 213 bool WriteBytes(const void* data, int length); | 206 bool WriteBytes(const void* data, int length); |
| 214 | 207 |
| 215 // Reserves space for upcoming writes when multiple writes will be made and | 208 // Reserves space for upcoming writes when multiple writes will be made and |
| 216 // their sizes are computed in advance. It can be significantly faster to call | 209 // their sizes are computed in advance. It can be significantly faster to call |
| 217 // Reserve() before calling WriteFoo() multiple times. | 210 // Reserve() before calling WriteFoo() multiple times. |
| 218 void Reserve(size_t additional_capacity); | 211 void Reserve(size_t additional_capacity); |
| 219 | 212 |
| 220 // Payload follows after allocation of Header (header size is customizable). | 213 // Payload follows after allocation of Header (header size is customizable). |
| 221 struct Header { | 214 struct Header { |
| 222 uint32 payload_size; // Specifies the size of the payload. | 215 uint32_t payload_size; // Specifies the size of the payload. |
| 223 }; | 216 }; |
| 224 | 217 |
| 225 // Returns the header, cast to a user-specified type T. The type T must be a | 218 // Returns the header, cast to a user-specified type T. The type T must be a |
| 226 // subclass of Header and its size must correspond to the header_size passed | 219 // subclass of Header and its size must correspond to the header_size passed |
| 227 // to the Pickle constructor. | 220 // to the Pickle constructor. |
| 228 template <class T> | 221 template <class T> |
| 229 T* headerT() { | 222 T* headerT() { |
| 230 DCHECK_EQ(header_size_, sizeof(T)); | 223 DCHECK_EQ(header_size_, sizeof(T)); |
| 231 return static_cast<T*>(header_); | 224 return static_cast<T*>(header_); |
| 232 } | 225 } |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 313 FRIEND_TEST_ALL_PREFIXES(PickleTest, PeekNext); | 306 FRIEND_TEST_ALL_PREFIXES(PickleTest, PeekNext); |
| 314 FRIEND_TEST_ALL_PREFIXES(PickleTest, PeekNextOverflow); | 307 FRIEND_TEST_ALL_PREFIXES(PickleTest, PeekNextOverflow); |
| 315 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNext); | 308 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNext); |
| 316 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextWithIncompleteHeader); | 309 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextWithIncompleteHeader); |
| 317 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextOverflow); | 310 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextOverflow); |
| 318 }; | 311 }; |
| 319 | 312 |
| 320 } // namespace base | 313 } // namespace base |
| 321 | 314 |
| 322 #endif // BASE_PICKLE_H_ | 315 #endif // BASE_PICKLE_H_ |
| OLD | NEW |