blob: 02bc432ad0bd27564e289f9d8fae0b9ca9408241 [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
tfarinaa31163512015-05-13 22:10:155#ifndef BASE_PICKLE_H_
6#define BASE_PICKLE_H_
initial.commitd7cae122008-07-26 21:49:387
avi9b6f42932015-12-26 22:15:148#include <stddef.h>
avic0279142015-12-04 22:38:529#include <stdint.h>
10
initial.commitd7cae122008-07-26 21:49:3811#include <string>
12
[email protected]0bea7252011-08-05 15:34:0013#include "base/base_export.h"
[email protected]ce208f872012-03-07 20:42:5614#include "base/compiler_specific.h"
[email protected]a918f872010-06-01 14:30:5115#include "base/gtest_prod_util.h"
initial.commitd7cae122008-07-26 21:49:3816#include "base/logging.h"
[email protected]d529cb02013-06-10 19:06:5717#include "base/strings/string16.h"
brucedawsoneaa38962015-03-10 01:46:5018#include "base/strings/string_piece.h"
initial.commitd7cae122008-07-26 21:49:3819
brettw05cfd8ddb2015-06-02 07:02:4720namespace base {
21
[email protected]ce208f872012-03-07 20:42:5622class Pickle;
23
24// PickleIterator reads data from a Pickle. The Pickle object must remain valid
25// while the PickleIterator object is in use.
26class BASE_EXPORT PickleIterator {
27 public:
[email protected]a15016f2014-06-02 23:23:4928 PickleIterator() : payload_(NULL), read_index_(0), end_index_(0) {}
[email protected]ce208f872012-03-07 20:42:5629 explicit PickleIterator(const Pickle& pickle);
30
31 // Methods for reading the payload of the Pickle. To read from the start of
32 // the Pickle, create a PickleIterator from a Pickle. If successful, these
33 // methods return true. Otherwise, false is returned to indicate that the
avi48fc13b2014-12-28 23:31:4834 // result could not be extracted. It is not possible to read from the iterator
[email protected]a15016f2014-06-02 23:23:4935 // after that.
[email protected]ce208f872012-03-07 20:42:5636 bool ReadBool(bool* result) WARN_UNUSED_RESULT;
37 bool ReadInt(int* result) WARN_UNUSED_RESULT;
38 bool ReadLong(long* result) WARN_UNUSED_RESULT;
avic0279142015-12-04 22:38:5239 bool ReadUInt16(uint16_t* result) WARN_UNUSED_RESULT;
40 bool ReadUInt32(uint32_t* result) WARN_UNUSED_RESULT;
41 bool ReadInt64(int64_t* result) WARN_UNUSED_RESULT;
42 bool ReadUInt64(uint64_t* result) WARN_UNUSED_RESULT;
pkasting89a19f142014-10-02 03:01:0443 bool ReadSizeT(size_t* result) WARN_UNUSED_RESULT;
[email protected]b1f61b032012-11-28 15:40:5844 bool ReadFloat(float* result) WARN_UNUSED_RESULT;
[email protected]915cc7d2014-07-14 22:50:3245 bool ReadDouble(double* result) WARN_UNUSED_RESULT;
[email protected]ce208f872012-03-07 20:42:5646 bool ReadString(std::string* result) WARN_UNUSED_RESULT;
brucedawsoneaa38962015-03-10 01:46:5047 // The StringPiece data will only be valid for the lifetime of the message.
brettw05cfd8ddb2015-06-02 07:02:4748 bool ReadStringPiece(StringPiece* result) WARN_UNUSED_RESULT;
49 bool ReadString16(string16* result) WARN_UNUSED_RESULT;
brucedawsoneaa38962015-03-10 01:46:5050 // The StringPiece16 data will only be valid for the lifetime of the message.
brettw05cfd8ddb2015-06-02 07:02:4751 bool ReadStringPiece16(StringPiece16* result) WARN_UNUSED_RESULT;
avi48fc13b2014-12-28 23:31:4852
53 // A pointer to the data will be placed in |*data|, and the length will be
54 // placed in |*length|. The pointer placed into |*data| points into the
55 // message's buffer so it will be scoped to the lifetime of the message (or
56 // until the message data is mutated). Do not keep the pointer around!
[email protected]ce208f872012-03-07 20:42:5657 bool ReadData(const char** data, int* length) WARN_UNUSED_RESULT;
avi48fc13b2014-12-28 23:31:4858
59 // A pointer to the data will be placed in |*data|. The caller specifies the
60 // number of bytes to read, and ReadBytes will validate this length. The
61 // pointer placed into |*data| points into the message's buffer so it will be
62 // scoped to the lifetime of the message (or until the message data is
63 // mutated). Do not keep the pointer around!
[email protected]ce208f872012-03-07 20:42:5664 bool ReadBytes(const char** data, int length) WARN_UNUSED_RESULT;
65
avi48fc13b2014-12-28 23:31:4866 // A safer version of ReadInt() that checks for the result not being negative.
[email protected]ce208f872012-03-07 20:42:5667 // Use it for reading the object sizes.
68 bool ReadLength(int* result) WARN_UNUSED_RESULT {
69 return ReadInt(result) && *result >= 0;
70 }
71
72 // Skips bytes in the read buffer and returns true if there are at least
73 // num_bytes available. Otherwise, does nothing and returns false.
74 bool SkipBytes(int num_bytes) WARN_UNUSED_RESULT {
75 return !!GetReadPointerAndAdvance(num_bytes);
76 }
77
78 private:
[email protected]ce208f872012-03-07 20:42:5679 // Read Type from Pickle.
80 template <typename Type>
[email protected]a15016f2014-06-02 23:23:4981 bool ReadBuiltinType(Type* result);
82
83 // Advance read_index_ but do not allow it to exceed end_index_.
84 // Keeps read_index_ aligned.
85 void Advance(size_t size);
[email protected]ce208f872012-03-07 20:42:5686
87 // Get read pointer for Type and advance read pointer.
88 template<typename Type>
[email protected]a15016f2014-06-02 23:23:4989 const char* GetReadPointerAndAdvance();
[email protected]ce208f872012-03-07 20:42:5690
91 // Get read pointer for |num_bytes| and advance read pointer. This method
92 // checks num_bytes for negativity and wrapping.
93 const char* GetReadPointerAndAdvance(int num_bytes);
94
95 // Get read pointer for (num_elements * size_element) bytes and advance read
96 // pointer. This method checks for int overflow, negativity and wrapping.
[email protected]a15016f2014-06-02 23:23:4997 const char* GetReadPointerAndAdvance(int num_elements,
98 size_t size_element);
[email protected]ce208f872012-03-07 20:42:5699
[email protected]a15016f2014-06-02 23:23:49100 const char* payload_; // Start of our pickle's payload.
101 size_t read_index_; // Offset of the next readable byte in payload.
102 size_t end_index_; // Payload size.
[email protected]ce208f872012-03-07 20:42:56103
104 FRIEND_TEST_ALL_PREFIXES(PickleTest, GetReadPointerAndAdvance);
105};
106
initial.commitd7cae122008-07-26 21:49:38107// This class provides facilities for basic binary value packing and unpacking.
108//
109// The Pickle class supports appending primitive values (ints, strings, etc.)
110// to a pickle instance. The Pickle instance grows its internal memory buffer
111// dynamically to hold the sequence of primitive values. The internal memory
112// buffer is exposed as the "data" of the Pickle. This "data" can be passed
113// to a Pickle object to initialize it for reading.
114//
115// When reading from a Pickle object, it is important for the consumer to know
116// what value types to read and in what order to read them as the Pickle does
117// not keep track of the type of data written to it.
118//
119// The Pickle's data has a header which contains the size of the Pickle's
120// payload. It can optionally support additional space in the header. That
121// space is controlled by the header_size parameter passed to the Pickle
122// constructor.
123//
[email protected]0bea7252011-08-05 15:34:00124class BASE_EXPORT Pickle {
initial.commitd7cae122008-07-26 21:49:38125 public:
initial.commitd7cae122008-07-26 21:49:38126 // Initialize a Pickle object using the default header size.
127 Pickle();
128
129 // Initialize a Pickle object with the specified header size in bytes, which
130 // must be greater-than-or-equal-to sizeof(Pickle::Header). The header size
131 // will be rounded up to ensure that the header size is 32bit-aligned.
132 explicit Pickle(int header_size);
133
134 // Initializes a Pickle from a const block of data. The data is not copied;
135 // instead the data is merely referenced by this Pickle. Only const methods
136 // should be used on the Pickle when initialized this way. The header
137 // padding size is deduced from the data length.
[email protected]753bb252013-11-04 22:28:12138 Pickle(const char* data, int data_len);
initial.commitd7cae122008-07-26 21:49:38139
140 // Initializes a Pickle as a deep copy of another Pickle.
141 Pickle(const Pickle& other);
142
[email protected]f60c32b2011-09-25 03:08:13143 // Note: There are no virtual methods in this class. This destructor is
144 // virtual as an element of defensive coding. Other classes have derived from
145 // this class, and there is a *chance* that they will cast into this base
146 // class before destruction. At least one such class does have a virtual
147 // destructor, suggesting at least some need to call more derived destructors.
[email protected]a502bbe72011-01-07 18:06:45148 virtual ~Pickle();
149
initial.commitd7cae122008-07-26 21:49:38150 // Performs a deep copy.
151 Pickle& operator=(const Pickle& other);
152
primiano9882cf342015-06-11 21:40:10153 // Returns the number of bytes written in the Pickle, including the header.
[email protected]8a861402011-01-28 19:59:11154 size_t size() const { return header_size_ + header_->payload_size; }
initial.commitd7cae122008-07-26 21:49:38155
156 // Returns the data for this Pickle.
157 const void* data() const { return header_; }
158
primiano9882cf342015-06-11 21:40:10159 // Returns the effective memory capacity of this Pickle, that is, the total
160 // number of bytes currently dynamically allocated or 0 in the case of a
161 // read-only Pickle. This should be used only for diagnostic / profiling
162 // purposes.
163 size_t GetTotalAllocatedSize() const;
164
initial.commitd7cae122008-07-26 21:49:38165 // Methods for adding to the payload of the Pickle. These values are
166 // appended to the end of the Pickle's payload. When reading values from a
167 // Pickle, it is important to read them in the order in which they were added
168 // to the Pickle.
avi48fc13b2014-12-28 23:31:48169
initial.commitd7cae122008-07-26 21:49:38170 bool WriteBool(bool value) {
171 return WriteInt(value ? 1 : 0);
172 }
173 bool WriteInt(int value) {
[email protected]d1b319fc2013-10-31 04:03:02174 return WritePOD(value);
initial.commitd7cae122008-07-26 21:49:38175 }
[email protected]c272d082012-03-23 00:03:10176 // WARNING: DO NOT USE THIS METHOD IF PICKLES ARE PERSISTED IN ANY WAY.
177 // It will write whatever a "long" is on this architecture. On 32-bit
178 // platforms, it is 32 bits. On 64-bit platforms, it is 64 bits. If persisted
179 // pickles are still around after upgrading to 64-bit, or if they are copied
180 // between dissimilar systems, YOUR PICKLES WILL HAVE GONE BAD.
181 bool WriteLongUsingDangerousNonPortableLessPersistableForm(long value) {
[email protected]d1b319fc2013-10-31 04:03:02182 return WritePOD(value);
initial.commitd7cae122008-07-26 21:49:38183 }
avic0279142015-12-04 22:38:52184 bool WriteUInt16(uint16_t value) { return WritePOD(value); }
185 bool WriteUInt32(uint32_t value) { return WritePOD(value); }
186 bool WriteInt64(int64_t value) { return WritePOD(value); }
187 bool WriteUInt64(uint64_t value) { return WritePOD(value); }
pkasting89a19f142014-10-02 03:01:04188 bool WriteSizeT(size_t value) {
189 // Always write size_t as a 64-bit value to ensure compatibility between
190 // 32-bit and 64-bit processes.
avic0279142015-12-04 22:38:52191 return WritePOD(static_cast<uint64_t>(value));
pkasting89a19f142014-10-02 03:01:04192 }
[email protected]b1f61b032012-11-28 15:40:58193 bool WriteFloat(float value) {
[email protected]d1b319fc2013-10-31 04:03:02194 return WritePOD(value);
[email protected]b1f61b032012-11-28 15:40:58195 }
[email protected]915cc7d2014-07-14 22:50:32196 bool WriteDouble(double value) {
197 return WritePOD(value);
198 }
brettw05cfd8ddb2015-06-02 07:02:47199 bool WriteString(const StringPiece& value);
200 bool WriteString16(const StringPiece16& value);
[email protected]34d48612012-06-29 00:05:04201 // "Data" is a blob with a length. When you read it out you will be given the
202 // length. See also WriteBytes.
initial.commitd7cae122008-07-26 21:49:38203 bool WriteData(const char* data, int length);
[email protected]d1b319fc2013-10-31 04:03:02204 // "Bytes" is a blob with no length. The caller must specify the length both
[email protected]34d48612012-06-29 00:05:04205 // when reading and writing. It is normally used to serialize PoD types of a
206 // known size. See also WriteData.
[email protected]d1b319fc2013-10-31 04:03:02207 bool WriteBytes(const void* data, int length);
initial.commitd7cae122008-07-26 21:49:38208
[email protected]032bfc42013-10-29 22:23:52209 // Reserves space for upcoming writes when multiple writes will be made and
210 // their sizes are computed in advance. It can be significantly faster to call
211 // Reserve() before calling WriteFoo() multiple times.
212 void Reserve(size_t additional_capacity);
213
[email protected]c9046af2008-08-06 20:35:17214 // Payload follows after allocation of Header (header size is customizable).
initial.commitd7cae122008-07-26 21:49:38215 struct Header {
avic0279142015-12-04 22:38:52216 uint32_t payload_size; // Specifies the size of the payload.
initial.commitd7cae122008-07-26 21:49:38217 };
218
219 // Returns the header, cast to a user-specified type T. The type T must be a
220 // subclass of Header and its size must correspond to the header_size passed
221 // to the Pickle constructor.
222 template <class T>
223 T* headerT() {
[email protected]5d2b4492011-03-01 02:48:05224 DCHECK_EQ(header_size_, sizeof(T));
initial.commitd7cae122008-07-26 21:49:38225 return static_cast<T*>(header_);
226 }
227 template <class T>
228 const T* headerT() const {
[email protected]5d2b4492011-03-01 02:48:05229 DCHECK_EQ(header_size_, sizeof(T));
initial.commitd7cae122008-07-26 21:49:38230 return static_cast<const T*>(header_);
231 }
232
[email protected]73d96dc2012-03-30 22:35:27233 // The payload is the pickle data immediately following the header.
[email protected]a15016f2014-06-02 23:23:49234 size_t payload_size() const {
235 return header_ ? header_->payload_size : 0;
236 }
[email protected]e00a6c0a2013-01-18 18:20:57237
initial.commitd7cae122008-07-26 21:49:38238 const char* payload() const {
239 return reinterpret_cast<const char*>(header_) + header_size_;
240 }
241
242 // Returns the address of the byte immediately following the currently valid
243 // header + payload.
initial.commitd7cae122008-07-26 21:49:38244 const char* end_of_payload() const {
[email protected]d87f8e6f2010-11-15 19:31:23245 // This object may be invalid.
246 return header_ ? payload() + payload_size() : NULL;
initial.commitd7cae122008-07-26 21:49:38247 }
248
[email protected]e00a6c0a2013-01-18 18:20:57249 protected:
250 char* mutable_payload() {
251 return reinterpret_cast<char*>(header_) + header_size_;
252 }
253
[email protected]d1b319fc2013-10-31 04:03:02254 size_t capacity_after_header() const {
255 return capacity_after_header_;
initial.commitd7cae122008-07-26 21:49:38256 }
257
[email protected]d1b319fc2013-10-31 04:03:02258 // Resize the capacity, note that the input value should not include the size
259 // of the header.
260 void Resize(size_t new_capacity);
initial.commitd7cae122008-07-26 21:49:38261
rockot0776a502015-12-17 06:19:49262 // Claims |num_bytes| bytes of payload. This is similar to Reserve() in that
263 // it may grow the capacity, but it also advances the write offset of the
264 // pickle by |num_bytes|. Claimed memory, including padding, is zeroed.
265 //
266 // Returns the address of the first byte claimed.
267 void* ClaimBytes(size_t num_bytes);
268
initial.commitd7cae122008-07-26 21:49:38269 // Find the end of the pickled data that starts at range_start. Returns NULL
270 // if the entire Pickle is not found in the given data range.
271 static const char* FindNext(size_t header_size,
272 const char* range_start,
273 const char* range_end);
274
dskiba6f3790a2015-09-30 17:24:30275 // Parse pickle header and return total size of the pickle. Data range
276 // doesn't need to contain entire pickle.
277 // Returns true if pickle header was found and parsed. Callers must check
278 // returned |pickle_size| for sanity (against maximum message size, etc).
279 // NOTE: when function successfully parses a header, but encounters an
280 // overflow during pickle size calculation, it sets |pickle_size| to the
281 // maximum size_t value and returns true.
282 static bool PeekNext(size_t header_size,
283 const char* range_start,
284 const char* range_end,
285 size_t* pickle_size);
286
initial.commitd7cae122008-07-26 21:49:38287 // The allocation granularity of the payload.
288 static const int kPayloadUnit;
289
290 private:
[email protected]ce208f872012-03-07 20:42:56291 friend class PickleIterator;
292
initial.commitd7cae122008-07-26 21:49:38293 Header* header_;
294 size_t header_size_; // Supports extra data between header and payload.
[email protected]d1b319fc2013-10-31 04:03:02295 // Allocation size of payload (or -1 if allocation is const). Note: this
296 // doesn't count the header.
297 size_t capacity_after_header_;
298 // The offset at which we will write the next field. Note: this doesn't count
299 // the header.
300 size_t write_offset_;
301
302 // Just like WriteBytes, but with a compile-time size, for performance.
[email protected]ba721602014-06-11 00:34:38303 template<size_t length> void BASE_EXPORT WriteBytesStatic(const void* data);
[email protected]d1b319fc2013-10-31 04:03:02304
305 // Writes a POD by copying its bytes.
306 template <typename T> bool WritePOD(const T& data) {
307 WriteBytesStatic<sizeof(data)>(&data);
308 return true;
309 }
rockot0776a502015-12-17 06:19:49310
311 inline void* ClaimUninitializedBytesInternal(size_t num_bytes);
[email protected]d1b319fc2013-10-31 04:03:02312 inline void WriteBytesCommon(const void* data, size_t length);
initial.commitd7cae122008-07-26 21:49:38313
erikchenf9ca8f5f02015-09-08 23:36:29314 FRIEND_TEST_ALL_PREFIXES(PickleTest, DeepCopyResize);
[email protected]a918f872010-06-01 14:30:51315 FRIEND_TEST_ALL_PREFIXES(PickleTest, Resize);
dskiba6f3790a2015-09-30 17:24:30316 FRIEND_TEST_ALL_PREFIXES(PickleTest, PeekNext);
317 FRIEND_TEST_ALL_PREFIXES(PickleTest, PeekNextOverflow);
[email protected]a918f872010-06-01 14:30:51318 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNext);
[email protected]137d2372011-01-26 13:02:27319 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextWithIncompleteHeader);
[email protected]33a38dd2013-11-01 09:06:26320 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextOverflow);
initial.commitd7cae122008-07-26 21:49:38321};
322
brettw05cfd8ddb2015-06-02 07:02:47323} // namespace base
324
tfarinaa31163512015-05-13 22:10:15325#endif // BASE_PICKLE_H_