| 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" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 bool ReadUInt16(uint16* result) WARN_UNUSED_RESULT; | 33 bool ReadUInt16(uint16* result) WARN_UNUSED_RESULT; |
| 34 bool ReadUInt32(uint32* result) WARN_UNUSED_RESULT; | 34 bool ReadUInt32(uint32* result) WARN_UNUSED_RESULT; |
| 35 bool ReadInt64(int64* result) WARN_UNUSED_RESULT; | 35 bool ReadInt64(int64* result) WARN_UNUSED_RESULT; |
| 36 bool ReadUInt64(uint64* result) WARN_UNUSED_RESULT; | 36 bool ReadUInt64(uint64* result) WARN_UNUSED_RESULT; |
| 37 bool ReadFloat(float* result) WARN_UNUSED_RESULT; | 37 bool ReadFloat(float* result) WARN_UNUSED_RESULT; |
| 38 bool ReadString(std::string* result) WARN_UNUSED_RESULT; | 38 bool ReadString(std::string* result) WARN_UNUSED_RESULT; |
| 39 bool ReadWString(std::wstring* result) WARN_UNUSED_RESULT; | 39 bool ReadWString(std::wstring* result) WARN_UNUSED_RESULT; |
| 40 bool ReadString16(string16* result) WARN_UNUSED_RESULT; | 40 bool ReadString16(string16* result) WARN_UNUSED_RESULT; |
| 41 bool ReadData(const char** data, int* length) WARN_UNUSED_RESULT; | 41 bool ReadData(const char** data, int* length) WARN_UNUSED_RESULT; |
| 42 bool ReadBytes(const char** data, int length) WARN_UNUSED_RESULT; | 42 bool ReadBytes(const char** data, int length) WARN_UNUSED_RESULT; |
| 43 template<size_t n, typename T> |
| 44 bool ReadPODArray(const T** data) WARN_UNUSED_RESULT { |
| 45 const char* char_data; |
| 46 if (!ReadBytes(&char_data, sizeof(T[n]))) |
| 47 return false; |
| 48 *data = reinterpret_cast<const T*>(char_data); |
| 49 return true; |
| 50 } |
| 43 | 51 |
| 44 // Safer version of ReadInt() checks for the result not being negative. | 52 // Safer version of ReadInt() checks for the result not being negative. |
| 45 // Use it for reading the object sizes. | 53 // Use it for reading the object sizes. |
| 46 bool ReadLength(int* result) WARN_UNUSED_RESULT { | 54 bool ReadLength(int* result) WARN_UNUSED_RESULT { |
| 47 return ReadInt(result) && *result >= 0; | 55 return ReadInt(result) && *result >= 0; |
| 48 } | 56 } |
| 49 | 57 |
| 50 // Skips bytes in the read buffer and returns true if there are at least | 58 // Skips bytes in the read buffer and returns true if there are at least |
| 51 // num_bytes available. Otherwise, does nothing and returns false. | 59 // num_bytes available. Otherwise, does nothing and returns false. |
| 52 bool SkipBytes(int num_bytes) WARN_UNUSED_RESULT { | 60 bool SkipBytes(int num_bytes) WARN_UNUSED_RESULT { |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 } | 201 } |
| 194 // A pointer to the data will be placed in *data. The caller specifies the | 202 // A pointer to the data will be placed in *data. The caller specifies the |
| 195 // number of bytes to read, and ReadBytes will validate this length. The | 203 // number of bytes to read, and ReadBytes will validate this length. The |
| 196 // returned buffer will be into the message's buffer so will be scoped to the | 204 // returned buffer will be into the message's buffer so will be scoped to the |
| 197 // lifetime of the message (or until the message data is mutated). | 205 // lifetime of the message (or until the message data is mutated). |
| 198 bool ReadBytes(PickleIterator* iter, | 206 bool ReadBytes(PickleIterator* iter, |
| 199 const char** data, | 207 const char** data, |
| 200 int length) const WARN_UNUSED_RESULT { | 208 int length) const WARN_UNUSED_RESULT { |
| 201 return iter->ReadBytes(data, length); | 209 return iter->ReadBytes(data, length); |
| 202 } | 210 } |
| 211 // A pointer to the data will be placed in *data. The caller specifies the |
| 212 // number of objects to read as a template parameter. The returned buffer will |
| 213 // be into the message's buffer so will be scoped to the lifetime of the |
| 214 // message (or until the message data is mutated). |
| 215 // Example usage: |
| 216 // const Type* data; |
| 217 // if (pickle.ReadPODArray<4>(&data)) { |
| 218 // DoSomething(data[0], data[1], data[2], data[3]); |
| 219 // } |
| 220 template<size_t n, typename T> bool ReadPODArray( |
| 221 PickleIterator* iter, |
| 222 const T** data) const WARN_UNUSED_RESULT { |
| 223 return iter->ReadPODArray<n>(data); |
| 224 } |
| 203 | 225 |
| 204 // Safer version of ReadInt() checks for the result not being negative. | 226 // Safer version of ReadInt() checks for the result not being negative. |
| 205 // Use it for reading the object sizes. | 227 // Use it for reading the object sizes. |
| 206 bool ReadLength(PickleIterator* iter, | 228 bool ReadLength(PickleIterator* iter, |
| 207 int* result) const WARN_UNUSED_RESULT { | 229 int* result) const WARN_UNUSED_RESULT { |
| 208 return iter->ReadLength(result); | 230 return iter->ReadLength(result); |
| 209 } | 231 } |
| 210 | 232 |
| 211 // Methods for adding to the payload of the Pickle. These values are | 233 // Methods for adding to the payload of the Pickle. These values are |
| 212 // appended to the end of the Pickle's payload. When reading values from a | 234 // appended to the end of the Pickle's payload. When reading values from a |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 bool WriteWString(const std::wstring& value); | 267 bool WriteWString(const std::wstring& value); |
| 246 bool WriteString16(const string16& value); | 268 bool WriteString16(const string16& value); |
| 247 // "Data" is a blob with a length. When you read it out you will be given the | 269 // "Data" is a blob with a length. When you read it out you will be given the |
| 248 // length. See also WriteBytes. | 270 // length. See also WriteBytes. |
| 249 bool WriteData(const char* data, int length); | 271 bool WriteData(const char* data, int length); |
| 250 // "Bytes" is a blob with no length. The caller must specify the length both | 272 // "Bytes" is a blob with no length. The caller must specify the length both |
| 251 // when reading and writing. It is normally used to serialize PoD types of a | 273 // when reading and writing. It is normally used to serialize PoD types of a |
| 252 // known size. See also WriteData. | 274 // known size. See also WriteData. |
| 253 bool WriteBytes(const void* data, int length); | 275 bool WriteBytes(const void* data, int length); |
| 254 | 276 |
| 277 // Writes a fixed-size POD array by copying its bytes. Use as: |
| 278 // Type data[4] = {...} |
| 279 // pickle.WritePODArray<4>(data); |
| 280 template <size_t n, typename T> void WritePODArray(const T* data) { |
| 281 WriteBytesStatic<sizeof(T[n])>(data); |
| 282 } |
| 283 |
| 255 // Reserves space for upcoming writes when multiple writes will be made and | 284 // Reserves space for upcoming writes when multiple writes will be made and |
| 256 // their sizes are computed in advance. It can be significantly faster to call | 285 // their sizes are computed in advance. It can be significantly faster to call |
| 257 // Reserve() before calling WriteFoo() multiple times. | 286 // Reserve() before calling WriteFoo() multiple times. |
| 258 void Reserve(size_t additional_capacity); | 287 void Reserve(size_t additional_capacity); |
| 259 | 288 |
| 260 // Payload follows after allocation of Header (header size is customizable). | 289 // Payload follows after allocation of Header (header size is customizable). |
| 261 struct Header { | 290 struct Header { |
| 262 uint32 payload_size; // Specifies the size of the payload. | 291 uint32 payload_size; // Specifies the size of the payload. |
| 263 }; | 292 }; |
| 264 | 293 |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 338 return true; | 367 return true; |
| 339 } | 368 } |
| 340 inline void WriteBytesCommon(const void* data, size_t length); | 369 inline void WriteBytesCommon(const void* data, size_t length); |
| 341 | 370 |
| 342 FRIEND_TEST_ALL_PREFIXES(PickleTest, Resize); | 371 FRIEND_TEST_ALL_PREFIXES(PickleTest, Resize); |
| 343 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNext); | 372 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNext); |
| 344 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextWithIncompleteHeader); | 373 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextWithIncompleteHeader); |
| 345 }; | 374 }; |
| 346 | 375 |
| 347 #endif // BASE_PICKLE_H__ | 376 #endif // BASE_PICKLE_H__ |
| OLD | NEW |