blob: b9b41267153e4f0f318440fef4304ce0462176b7 [file] [log] [blame]
[email protected]ce208f872012-03-07 20:42:561// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
5#ifndef BASE_PICKLE_H__
6#define BASE_PICKLE_H__
7
8#include <string>
9
[email protected]0bea7252011-08-05 15:34:0010#include "base/base_export.h"
initial.commitd7cae122008-07-26 21:49:3811#include "base/basictypes.h"
[email protected]ce208f872012-03-07 20:42:5612#include "base/compiler_specific.h"
[email protected]a918f872010-06-01 14:30:5113#include "base/gtest_prod_util.h"
initial.commitd7cae122008-07-26 21:49:3814#include "base/logging.h"
[email protected]d529cb02013-06-10 19:06:5715#include "base/strings/string16.h"
brucedawsoneaa38962015-03-10 01:46:5016#include "base/strings/string_piece.h"
initial.commitd7cae122008-07-26 21:49:3817
[email protected]ce208f872012-03-07 20:42:5618class Pickle;
19
20// PickleIterator reads data from a Pickle. The Pickle object must remain valid
21// while the PickleIterator object is in use.
22class BASE_EXPORT PickleIterator {
23 public:
[email protected]a15016f2014-06-02 23:23:4924 PickleIterator() : payload_(NULL), read_index_(0), end_index_(0) {}
[email protected]ce208f872012-03-07 20:42:5625 explicit PickleIterator(const Pickle& pickle);
26
27 // 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
29 // methods return true. Otherwise, false is returned to indicate that the
avi48fc13b2014-12-28 23:31:4830 // result could not be extracted. It is not possible to read from the iterator
[email protected]a15016f2014-06-02 23:23:4931 // after that.
[email protected]ce208f872012-03-07 20:42:5632 bool ReadBool(bool* result) WARN_UNUSED_RESULT;
33 bool ReadInt(int* result) WARN_UNUSED_RESULT;
34 bool ReadLong(long* result) WARN_UNUSED_RESULT;
[email protected]ce208f872012-03-07 20:42:5635 bool ReadUInt16(uint16* result) WARN_UNUSED_RESULT;
36 bool ReadUInt32(uint32* result) WARN_UNUSED_RESULT;
37 bool ReadInt64(int64* result) WARN_UNUSED_RESULT;
38 bool ReadUInt64(uint64* result) WARN_UNUSED_RESULT;
pkasting89a19f142014-10-02 03:01:0439 bool ReadSizeT(size_t* result) WARN_UNUSED_RESULT;
[email protected]b1f61b032012-11-28 15:40:5840 bool ReadFloat(float* result) WARN_UNUSED_RESULT;
[email protected]915cc7d2014-07-14 22:50:3241 bool ReadDouble(double* result) WARN_UNUSED_RESULT;
[email protected]ce208f872012-03-07 20:42:5642 bool ReadString(std::string* result) WARN_UNUSED_RESULT;
brucedawsoneaa38962015-03-10 01:46:5043 // The StringPiece data will only be valid for the lifetime of the message.
44 bool ReadStringPiece(base::StringPiece* result) WARN_UNUSED_RESULT;
[email protected]ce208f872012-03-07 20:42:5645 bool ReadWString(std::wstring* result) WARN_UNUSED_RESULT;
[email protected]476dafb2013-12-03 00:39:2646 bool ReadString16(base::string16* result) WARN_UNUSED_RESULT;
brucedawsoneaa38962015-03-10 01:46:5047 // The StringPiece16 data will only be valid for the lifetime of the message.
48 bool ReadStringPiece16(base::StringPiece16* result) WARN_UNUSED_RESULT;
avi48fc13b2014-12-28 23:31:4849
50 // 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
52 // 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!
[email protected]ce208f872012-03-07 20:42:5654 bool ReadData(const char** data, int* length) WARN_UNUSED_RESULT;
avi48fc13b2014-12-28 23:31:4855
56 // 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
58 // pointer placed into |*data| points into the message's buffer so it will be
59 // scoped to the lifetime of the message (or until the message data is
60 // mutated). Do not keep the pointer around!
[email protected]ce208f872012-03-07 20:42:5661 bool ReadBytes(const char** data, int length) WARN_UNUSED_RESULT;
62
avi48fc13b2014-12-28 23:31:4863 // A safer version of ReadInt() that checks for the result not being negative.
[email protected]ce208f872012-03-07 20:42:5664 // Use it for reading the object sizes.
65 bool ReadLength(int* result) WARN_UNUSED_RESULT {
66 return ReadInt(result) && *result >= 0;
67 }
68
69 // Skips bytes in the read buffer and returns true if there are at least
70 // num_bytes available. Otherwise, does nothing and returns false.
71 bool SkipBytes(int num_bytes) WARN_UNUSED_RESULT {
72 return !!GetReadPointerAndAdvance(num_bytes);
73 }
74
75 private:
avi48fc13b2014-12-28 23:31:4876 // Aligns 'i' by rounding it up to the next multiple of 'alignment'.
[email protected]ce208f872012-03-07 20:42:5677 static size_t AlignInt(size_t i, int alignment) {
78 return i + (alignment - (i % alignment)) % alignment;
79 }
80
81 // Read Type from Pickle.
82 template <typename Type>
[email protected]a15016f2014-06-02 23:23:4983 bool ReadBuiltinType(Type* result);
84
85 // Advance read_index_ but do not allow it to exceed end_index_.
86 // Keeps read_index_ aligned.
87 void Advance(size_t size);
[email protected]ce208f872012-03-07 20:42:5688
89 // Get read pointer for Type and advance read pointer.
90 template<typename Type>
[email protected]a15016f2014-06-02 23:23:4991 const char* GetReadPointerAndAdvance();
[email protected]ce208f872012-03-07 20:42:5692
93 // Get read pointer for |num_bytes| and advance read pointer. This method
94 // checks num_bytes for negativity and wrapping.
95 const char* GetReadPointerAndAdvance(int num_bytes);
96
97 // Get read pointer for (num_elements * size_element) bytes and advance read
98 // pointer. This method checks for int overflow, negativity and wrapping.
[email protected]a15016f2014-06-02 23:23:4999 const char* GetReadPointerAndAdvance(int num_elements,
100 size_t size_element);
[email protected]ce208f872012-03-07 20:42:56101
[email protected]a15016f2014-06-02 23:23:49102 const char* payload_; // Start of our pickle's payload.
103 size_t read_index_; // Offset of the next readable byte in payload.
104 size_t end_index_; // Payload size.
[email protected]ce208f872012-03-07 20:42:56105
106 FRIEND_TEST_ALL_PREFIXES(PickleTest, GetReadPointerAndAdvance);
107};
108
initial.commitd7cae122008-07-26 21:49:38109// This class provides facilities for basic binary value packing and unpacking.
110//
111// The Pickle class supports appending primitive values (ints, strings, etc.)
112// to a pickle instance. The Pickle instance grows its internal memory buffer
113// dynamically to hold the sequence of primitive values. The internal memory
114// buffer is exposed as the "data" of the Pickle. This "data" can be passed
115// to a Pickle object to initialize it for reading.
116//
117// When reading from a Pickle object, it is important for the consumer to know
118// what value types to read and in what order to read them as the Pickle does
119// not keep track of the type of data written to it.
120//
121// The Pickle's data has a header which contains the size of the Pickle's
122// payload. It can optionally support additional space in the header. That
123// space is controlled by the header_size parameter passed to the Pickle
124// constructor.
125//
[email protected]0bea7252011-08-05 15:34:00126class BASE_EXPORT Pickle {
initial.commitd7cae122008-07-26 21:49:38127 public:
initial.commitd7cae122008-07-26 21:49:38128 // Initialize a Pickle object using the default header size.
129 Pickle();
130
131 // Initialize a Pickle object with the specified header size in bytes, which
132 // must be greater-than-or-equal-to sizeof(Pickle::Header). The header size
133 // will be rounded up to ensure that the header size is 32bit-aligned.
134 explicit Pickle(int header_size);
135
136 // Initializes a Pickle from a const block of data. The data is not copied;
137 // instead the data is merely referenced by this Pickle. Only const methods
138 // should be used on the Pickle when initialized this way. The header
139 // padding size is deduced from the data length.
[email protected]753bb252013-11-04 22:28:12140 Pickle(const char* data, int data_len);
initial.commitd7cae122008-07-26 21:49:38141
142 // Initializes a Pickle as a deep copy of another Pickle.
143 Pickle(const Pickle& other);
144
[email protected]f60c32b2011-09-25 03:08:13145 // Note: There are no virtual methods in this class. This destructor is
146 // virtual as an element of defensive coding. Other classes have derived from
147 // this class, and there is a *chance* that they will cast into this base
148 // class before destruction. At least one such class does have a virtual
149 // destructor, suggesting at least some need to call more derived destructors.
[email protected]a502bbe72011-01-07 18:06:45150 virtual ~Pickle();
151
initial.commitd7cae122008-07-26 21:49:38152 // Performs a deep copy.
153 Pickle& operator=(const Pickle& other);
154
155 // Returns the size of the Pickle's data.
[email protected]8a861402011-01-28 19:59:11156 size_t size() const { return header_size_ + header_->payload_size; }
initial.commitd7cae122008-07-26 21:49:38157
158 // Returns the data for this Pickle.
159 const void* data() const { return header_; }
160
initial.commitd7cae122008-07-26 21:49:38161 // Methods for adding to the payload of the Pickle. These values are
162 // appended to the end of the Pickle's payload. When reading values from a
163 // Pickle, it is important to read them in the order in which they were added
164 // to the Pickle.
avi48fc13b2014-12-28 23:31:48165
initial.commitd7cae122008-07-26 21:49:38166 bool WriteBool(bool value) {
167 return WriteInt(value ? 1 : 0);
168 }
169 bool WriteInt(int value) {
[email protected]d1b319fc2013-10-31 04:03:02170 return WritePOD(value);
initial.commitd7cae122008-07-26 21:49:38171 }
[email protected]c272d082012-03-23 00:03:10172 // WARNING: DO NOT USE THIS METHOD IF PICKLES ARE PERSISTED IN ANY WAY.
173 // It will write whatever a "long" is on this architecture. On 32-bit
174 // platforms, it is 32 bits. On 64-bit platforms, it is 64 bits. If persisted
175 // pickles are still around after upgrading to 64-bit, or if they are copied
176 // between dissimilar systems, YOUR PICKLES WILL HAVE GONE BAD.
177 bool WriteLongUsingDangerousNonPortableLessPersistableForm(long value) {
[email protected]d1b319fc2013-10-31 04:03:02178 return WritePOD(value);
initial.commitd7cae122008-07-26 21:49:38179 }
[email protected]6d81b482011-02-22 19:47:19180 bool WriteUInt16(uint16 value) {
[email protected]d1b319fc2013-10-31 04:03:02181 return WritePOD(value);
[email protected]6d81b482011-02-22 19:47:19182 }
[email protected]48ce616a2008-12-29 18:55:18183 bool WriteUInt32(uint32 value) {
[email protected]d1b319fc2013-10-31 04:03:02184 return WritePOD(value);
[email protected]48ce616a2008-12-29 18:55:18185 }
initial.commitd7cae122008-07-26 21:49:38186 bool WriteInt64(int64 value) {
[email protected]d1b319fc2013-10-31 04:03:02187 return WritePOD(value);
initial.commitd7cae122008-07-26 21:49:38188 }
[email protected]b7a5d992009-10-28 04:21:01189 bool WriteUInt64(uint64 value) {
[email protected]d1b319fc2013-10-31 04:03:02190 return WritePOD(value);
[email protected]b7a5d992009-10-28 04:21:01191 }
pkasting89a19f142014-10-02 03:01:04192 bool WriteSizeT(size_t value) {
193 // Always write size_t as a 64-bit value to ensure compatibility between
194 // 32-bit and 64-bit processes.
195 return WritePOD(static_cast<uint64>(value));
196 }
[email protected]b1f61b032012-11-28 15:40:58197 bool WriteFloat(float value) {
[email protected]d1b319fc2013-10-31 04:03:02198 return WritePOD(value);
[email protected]b1f61b032012-11-28 15:40:58199 }
[email protected]915cc7d2014-07-14 22:50:32200 bool WriteDouble(double value) {
201 return WritePOD(value);
202 }
brucedawsoneaa38962015-03-10 01:46:50203 bool WriteString(const base::StringPiece& value);
initial.commitd7cae122008-07-26 21:49:38204 bool WriteWString(const std::wstring& value);
brucedawsoneaa38962015-03-10 01:46:50205 bool WriteString16(const base::StringPiece16& value);
[email protected]34d48612012-06-29 00:05:04206 // "Data" is a blob with a length. When you read it out you will be given the
207 // length. See also WriteBytes.
initial.commitd7cae122008-07-26 21:49:38208 bool WriteData(const char* data, int length);
[email protected]d1b319fc2013-10-31 04:03:02209 // "Bytes" is a blob with no length. The caller must specify the length both
[email protected]34d48612012-06-29 00:05:04210 // when reading and writing. It is normally used to serialize PoD types of a
211 // known size. See also WriteData.
[email protected]d1b319fc2013-10-31 04:03:02212 bool WriteBytes(const void* data, int length);
initial.commitd7cae122008-07-26 21:49:38213
[email protected]032bfc42013-10-29 22:23:52214 // 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
216 // Reserve() before calling WriteFoo() multiple times.
217 void Reserve(size_t additional_capacity);
218
[email protected]c9046af2008-08-06 20:35:17219 // Payload follows after allocation of Header (header size is customizable).
initial.commitd7cae122008-07-26 21:49:38220 struct Header {
[email protected]c9046af2008-08-06 20:35:17221 uint32 payload_size; // Specifies the size of the payload.
initial.commitd7cae122008-07-26 21:49:38222 };
223
224 // Returns the header, cast to a user-specified type T. The type T must be a
225 // subclass of Header and its size must correspond to the header_size passed
226 // to the Pickle constructor.
227 template <class T>
228 T* headerT() {
[email protected]5d2b4492011-03-01 02:48:05229 DCHECK_EQ(header_size_, sizeof(T));
initial.commitd7cae122008-07-26 21:49:38230 return static_cast<T*>(header_);
231 }
232 template <class T>
233 const T* headerT() const {
[email protected]5d2b4492011-03-01 02:48:05234 DCHECK_EQ(header_size_, sizeof(T));
initial.commitd7cae122008-07-26 21:49:38235 return static_cast<const T*>(header_);
236 }
237
[email protected]73d96dc2012-03-30 22:35:27238 // The payload is the pickle data immediately following the header.
[email protected]a15016f2014-06-02 23:23:49239 size_t payload_size() const {
240 return header_ ? header_->payload_size : 0;
241 }
[email protected]e00a6c0a2013-01-18 18:20:57242
initial.commitd7cae122008-07-26 21:49:38243 const char* payload() const {
244 return reinterpret_cast<const char*>(header_) + header_size_;
245 }
246
247 // Returns the address of the byte immediately following the currently valid
248 // header + payload.
initial.commitd7cae122008-07-26 21:49:38249 const char* end_of_payload() const {
[email protected]d87f8e6f2010-11-15 19:31:23250 // This object may be invalid.
251 return header_ ? payload() + payload_size() : NULL;
initial.commitd7cae122008-07-26 21:49:38252 }
253
[email protected]e00a6c0a2013-01-18 18:20:57254 protected:
255 char* mutable_payload() {
256 return reinterpret_cast<char*>(header_) + header_size_;
257 }
258
[email protected]d1b319fc2013-10-31 04:03:02259 size_t capacity_after_header() const {
260 return capacity_after_header_;
initial.commitd7cae122008-07-26 21:49:38261 }
262
[email protected]d1b319fc2013-10-31 04:03:02263 // Resize the capacity, note that the input value should not include the size
264 // of the header.
265 void Resize(size_t new_capacity);
initial.commitd7cae122008-07-26 21:49:38266
267 // Aligns 'i' by rounding it up to the next multiple of 'alignment'
[email protected]c9046af2008-08-06 20:35:17268 static size_t AlignInt(size_t i, int alignment) {
initial.commitd7cae122008-07-26 21:49:38269 return i + (alignment - (i % alignment)) % alignment;
270 }
271
initial.commitd7cae122008-07-26 21:49:38272 // Find the end of the pickled data that starts at range_start. Returns NULL
273 // if the entire Pickle is not found in the given data range.
274 static const char* FindNext(size_t header_size,
275 const char* range_start,
276 const char* range_end);
277
278 // The allocation granularity of the payload.
279 static const int kPayloadUnit;
280
281 private:
[email protected]ce208f872012-03-07 20:42:56282 friend class PickleIterator;
283
initial.commitd7cae122008-07-26 21:49:38284 Header* header_;
285 size_t header_size_; // Supports extra data between header and payload.
[email protected]d1b319fc2013-10-31 04:03:02286 // Allocation size of payload (or -1 if allocation is const). Note: this
287 // doesn't count the header.
288 size_t capacity_after_header_;
289 // The offset at which we will write the next field. Note: this doesn't count
290 // the header.
291 size_t write_offset_;
292
293 // Just like WriteBytes, but with a compile-time size, for performance.
[email protected]ba721602014-06-11 00:34:38294 template<size_t length> void BASE_EXPORT WriteBytesStatic(const void* data);
[email protected]d1b319fc2013-10-31 04:03:02295
296 // Writes a POD by copying its bytes.
297 template <typename T> bool WritePOD(const T& data) {
298 WriteBytesStatic<sizeof(data)>(&data);
299 return true;
300 }
301 inline void WriteBytesCommon(const void* data, size_t length);
initial.commitd7cae122008-07-26 21:49:38302
[email protected]a918f872010-06-01 14:30:51303 FRIEND_TEST_ALL_PREFIXES(PickleTest, Resize);
304 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNext);
[email protected]137d2372011-01-26 13:02:27305 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextWithIncompleteHeader);
[email protected]33a38dd2013-11-01 09:06:26306 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextOverflow);
initial.commitd7cae122008-07-26 21:49:38307};
308
309#endif // BASE_PICKLE_H__