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