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 | 16 |
17 class Pickle; | 17 class Pickle; |
18 | 18 |
19 // 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 |
20 // while the PickleIterator object is in use. | 20 // while the PickleIterator object is in use. |
21 class BASE_EXPORT PickleIterator { | 21 class BASE_EXPORT PickleIterator { |
22 public: | 22 public: |
23 PickleIterator() : payload_(NULL), read_index_(0), end_index_(0) {} | 23 PickleIterator() : payload_(NULL), read_index_(0), end_index_(0) {} |
24 explicit PickleIterator(const Pickle& pickle); | 24 explicit PickleIterator(const Pickle& pickle); |
25 | 25 |
26 // 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 |
27 // the Pickle, create a PickleIterator from a Pickle. If successful, these | 27 // the Pickle, create a PickleIterator from a Pickle. If successful, these |
28 // methods return true. Otherwise, false is returned to indicate that the | 28 // methods return true. Otherwise, false is returned to indicate that the |
29 // 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 iterator |
30 // after that. | 30 // after that. |
31 bool ReadBool(bool* result) WARN_UNUSED_RESULT; | 31 bool ReadBool(bool* result) WARN_UNUSED_RESULT; |
32 bool ReadInt(int* result) WARN_UNUSED_RESULT; | 32 bool ReadInt(int* result) WARN_UNUSED_RESULT; |
33 bool ReadLong(long* result) WARN_UNUSED_RESULT; | 33 bool ReadLong(long* result) WARN_UNUSED_RESULT; |
34 bool ReadUInt16(uint16* result) WARN_UNUSED_RESULT; | 34 bool ReadUInt16(uint16* result) WARN_UNUSED_RESULT; |
35 bool ReadUInt32(uint32* result) WARN_UNUSED_RESULT; | 35 bool ReadUInt32(uint32* result) WARN_UNUSED_RESULT; |
36 bool ReadInt64(int64* result) WARN_UNUSED_RESULT; | 36 bool ReadInt64(int64* result) WARN_UNUSED_RESULT; |
37 bool ReadUInt64(uint64* result) WARN_UNUSED_RESULT; | 37 bool ReadUInt64(uint64* result) WARN_UNUSED_RESULT; |
38 bool ReadSizeT(size_t* result) WARN_UNUSED_RESULT; | 38 bool ReadSizeT(size_t* result) WARN_UNUSED_RESULT; |
39 bool ReadFloat(float* result) WARN_UNUSED_RESULT; | 39 bool ReadFloat(float* result) WARN_UNUSED_RESULT; |
40 bool ReadDouble(double* result) WARN_UNUSED_RESULT; | 40 bool ReadDouble(double* result) WARN_UNUSED_RESULT; |
41 bool ReadString(std::string* result) WARN_UNUSED_RESULT; | 41 bool ReadString(std::string* result) WARN_UNUSED_RESULT; |
42 bool ReadWString(std::wstring* result) WARN_UNUSED_RESULT; | 42 bool ReadWString(std::wstring* result) WARN_UNUSED_RESULT; |
43 bool ReadString16(base::string16* result) WARN_UNUSED_RESULT; | 43 bool ReadString16(base::string16* result) WARN_UNUSED_RESULT; |
44 | |
45 // A pointer to the data will be placed in |*data|, and the length will be | |
46 // placed in |*length|. The pointer placed into |*data| points into the | |
47 // message's buffer so it will be scoped to the lifetime of the message (or | |
48 // until the message data is mutated). Do not keep the pointer around! | |
49 bool ReadData(const char** data, int* length) WARN_UNUSED_RESULT; | 44 bool ReadData(const char** data, int* length) WARN_UNUSED_RESULT; |
50 | |
51 // A pointer to the data will be placed in |*data|. The caller specifies the | |
52 // number of bytes to read, and ReadBytes will validate this length. The | |
53 // pointer placed into |*data| points into the message's buffer so it will be | |
54 // scoped to the lifetime of the message (or until the message data is | |
55 // mutated). Do not keep the pointer around! | |
56 bool ReadBytes(const char** data, int length) WARN_UNUSED_RESULT; | 45 bool ReadBytes(const char** data, int length) WARN_UNUSED_RESULT; |
57 | 46 |
58 // A safer version of ReadInt() that checks for the result not being negative. | 47 // Safer version of ReadInt() checks for the result not being negative. |
59 // Use it for reading the object sizes. | 48 // Use it for reading the object sizes. |
60 bool ReadLength(int* result) WARN_UNUSED_RESULT { | 49 bool ReadLength(int* result) WARN_UNUSED_RESULT { |
61 return ReadInt(result) && *result >= 0; | 50 return ReadInt(result) && *result >= 0; |
62 } | 51 } |
63 | 52 |
64 // Skips bytes in the read buffer and returns true if there are at least | 53 // Skips bytes in the read buffer and returns true if there are at least |
65 // num_bytes available. Otherwise, does nothing and returns false. | 54 // num_bytes available. Otherwise, does nothing and returns false. |
66 bool SkipBytes(int num_bytes) WARN_UNUSED_RESULT { | 55 bool SkipBytes(int num_bytes) WARN_UNUSED_RESULT { |
67 return !!GetReadPointerAndAdvance(num_bytes); | 56 return !!GetReadPointerAndAdvance(num_bytes); |
68 } | 57 } |
69 | 58 |
70 private: | 59 private: |
71 // Aligns 'i' by rounding it up to the next multiple of 'alignment'. | 60 // Aligns 'i' by rounding it up to the next multiple of 'alignment' |
72 static size_t AlignInt(size_t i, int alignment) { | 61 static size_t AlignInt(size_t i, int alignment) { |
73 return i + (alignment - (i % alignment)) % alignment; | 62 return i + (alignment - (i % alignment)) % alignment; |
74 } | 63 } |
75 | 64 |
76 // Read Type from Pickle. | 65 // Read Type from Pickle. |
77 template <typename Type> | 66 template <typename Type> |
78 bool ReadBuiltinType(Type* result); | 67 bool ReadBuiltinType(Type* result); |
79 | 68 |
80 // Advance read_index_ but do not allow it to exceed end_index_. | 69 // Advance read_index_ but do not allow it to exceed end_index_. |
81 // Keeps read_index_ aligned. | 70 // Keeps read_index_ aligned. |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 | 135 |
147 // Performs a deep copy. | 136 // Performs a deep copy. |
148 Pickle& operator=(const Pickle& other); | 137 Pickle& operator=(const Pickle& other); |
149 | 138 |
150 // Returns the size of the Pickle's data. | 139 // Returns the size of the Pickle's data. |
151 size_t size() const { return header_size_ + header_->payload_size; } | 140 size_t size() const { return header_size_ + header_->payload_size; } |
152 | 141 |
153 // Returns the data for this Pickle. | 142 // Returns the data for this Pickle. |
154 const void* data() const { return header_; } | 143 const void* data() const { return header_; } |
155 | 144 |
| 145 // For compatibility, these older style read methods pass through to the |
| 146 // PickleIterator methods. |
| 147 // TODO(jbates) Remove these methods. |
| 148 bool ReadBool(PickleIterator* iter, |
| 149 bool* result) const WARN_UNUSED_RESULT { |
| 150 return iter->ReadBool(result); |
| 151 } |
| 152 bool ReadInt(PickleIterator* iter, |
| 153 int* result) const WARN_UNUSED_RESULT { |
| 154 return iter->ReadInt(result); |
| 155 } |
| 156 bool ReadLong(PickleIterator* iter, |
| 157 long* result) const WARN_UNUSED_RESULT { |
| 158 return iter->ReadLong(result); |
| 159 } |
| 160 bool ReadUInt16(PickleIterator* iter, |
| 161 uint16* result) const WARN_UNUSED_RESULT { |
| 162 return iter->ReadUInt16(result); |
| 163 } |
| 164 bool ReadUInt32(PickleIterator* iter, |
| 165 uint32* result) const WARN_UNUSED_RESULT { |
| 166 return iter->ReadUInt32(result); |
| 167 } |
| 168 bool ReadInt64(PickleIterator* iter, |
| 169 int64* result) const WARN_UNUSED_RESULT { |
| 170 return iter->ReadInt64(result); |
| 171 } |
| 172 bool ReadUInt64(PickleIterator* iter, |
| 173 uint64* result) const WARN_UNUSED_RESULT { |
| 174 return iter->ReadUInt64(result); |
| 175 } |
| 176 bool ReadSizeT(PickleIterator* iter, |
| 177 size_t* result) const WARN_UNUSED_RESULT { |
| 178 return iter->ReadSizeT(result); |
| 179 } |
| 180 bool ReadFloat(PickleIterator* iter, |
| 181 float* result) const WARN_UNUSED_RESULT { |
| 182 return iter->ReadFloat(result); |
| 183 } |
| 184 bool ReadDouble(PickleIterator* iter, |
| 185 double* result) const WARN_UNUSED_RESULT { |
| 186 return iter->ReadDouble(result); |
| 187 } |
| 188 bool ReadString(PickleIterator* iter, |
| 189 std::string* result) const WARN_UNUSED_RESULT { |
| 190 return iter->ReadString(result); |
| 191 } |
| 192 bool ReadWString(PickleIterator* iter, |
| 193 std::wstring* result) const WARN_UNUSED_RESULT { |
| 194 return iter->ReadWString(result); |
| 195 } |
| 196 bool ReadString16(PickleIterator* iter, |
| 197 base::string16* result) const WARN_UNUSED_RESULT { |
| 198 return iter->ReadString16(result); |
| 199 } |
| 200 // A pointer to the data will be placed in *data, and the length will be |
| 201 // placed in *length. This buffer will be into the message's buffer so will |
| 202 // be scoped to the lifetime of the message (or until the message data is |
| 203 // mutated). |
| 204 bool ReadData(PickleIterator* iter, |
| 205 const char** data, |
| 206 int* length) const WARN_UNUSED_RESULT { |
| 207 return iter->ReadData(data, length); |
| 208 } |
| 209 // A pointer to the data will be placed in *data. The caller specifies the |
| 210 // number of bytes to read, and ReadBytes will validate this length. The |
| 211 // returned buffer will be into the message's buffer so will be scoped to the |
| 212 // lifetime of the message (or until the message data is mutated). |
| 213 bool ReadBytes(PickleIterator* iter, |
| 214 const char** data, |
| 215 int length) const WARN_UNUSED_RESULT { |
| 216 return iter->ReadBytes(data, length); |
| 217 } |
| 218 |
| 219 // Safer version of ReadInt() checks for the result not being negative. |
| 220 // Use it for reading the object sizes. |
| 221 bool ReadLength(PickleIterator* iter, |
| 222 int* result) const WARN_UNUSED_RESULT { |
| 223 return iter->ReadLength(result); |
| 224 } |
| 225 |
156 // Methods for adding to the payload of the Pickle. These values are | 226 // Methods for adding to the payload of the Pickle. These values are |
157 // appended to the end of the Pickle's payload. When reading values from a | 227 // appended to the end of the Pickle's payload. When reading values from a |
158 // Pickle, it is important to read them in the order in which they were added | 228 // Pickle, it is important to read them in the order in which they were added |
159 // to the Pickle. | 229 // to the Pickle. |
160 | |
161 bool WriteBool(bool value) { | 230 bool WriteBool(bool value) { |
162 return WriteInt(value ? 1 : 0); | 231 return WriteInt(value ? 1 : 0); |
163 } | 232 } |
164 bool WriteInt(int value) { | 233 bool WriteInt(int value) { |
165 return WritePOD(value); | 234 return WritePOD(value); |
166 } | 235 } |
167 // WARNING: DO NOT USE THIS METHOD IF PICKLES ARE PERSISTED IN ANY WAY. | 236 // WARNING: DO NOT USE THIS METHOD IF PICKLES ARE PERSISTED IN ANY WAY. |
168 // It will write whatever a "long" is on this architecture. On 32-bit | 237 // It will write whatever a "long" is on this architecture. On 32-bit |
169 // platforms, it is 32 bits. On 64-bit platforms, it is 64 bits. If persisted | 238 // platforms, it is 32 bits. On 64-bit platforms, it is 64 bits. If persisted |
170 // pickles are still around after upgrading to 64-bit, or if they are copied | 239 // pickles are still around after upgrading to 64-bit, or if they are copied |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 } | 364 } |
296 inline void WriteBytesCommon(const void* data, size_t length); | 365 inline void WriteBytesCommon(const void* data, size_t length); |
297 | 366 |
298 FRIEND_TEST_ALL_PREFIXES(PickleTest, Resize); | 367 FRIEND_TEST_ALL_PREFIXES(PickleTest, Resize); |
299 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNext); | 368 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNext); |
300 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextWithIncompleteHeader); | 369 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextWithIncompleteHeader); |
301 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextOverflow); | 370 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextOverflow); |
302 }; | 371 }; |
303 | 372 |
304 #endif // BASE_PICKLE_H__ | 373 #endif // BASE_PICKLE_H__ |
OLD | NEW |