blob: 99add5eb42dac5b7a68954ca85eac418ce1922a6 [file] [log] [blame]
[email protected]ce208f82012-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]ce208f82012-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"
initial.commitd7cae122008-07-26 21:49:3816
[email protected]ce208f82012-03-07 20:42:5617class Pickle;
18
19// PickleIterator reads data from a Pickle. The Pickle object must remain valid
20// while the PickleIterator object is in use.
21class BASE_EXPORT PickleIterator {
22 public:
[email protected]a15016f2014-06-02 23:23:4923 PickleIterator() : payload_(NULL), read_index_(0), end_index_(0) {}
[email protected]ce208f82012-03-07 20:42:5624 explicit PickleIterator(const Pickle& pickle);
25
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
28 // methods return true. Otherwise, false is returned to indicate that the
[email protected]a15016f2014-06-02 23:23:4929 // result could not be extracted. It is not possible to read from iterator
30 // after that.
[email protected]ce208f82012-03-07 20:42:5631 bool ReadBool(bool* result) WARN_UNUSED_RESULT;
32 bool ReadInt(int* result) WARN_UNUSED_RESULT;
33 bool ReadLong(long* result) WARN_UNUSED_RESULT;
[email protected]ce208f82012-03-07 20:42:5634 bool ReadUInt16(uint16* result) WARN_UNUSED_RESULT;
35 bool ReadUInt32(uint32* result) WARN_UNUSED_RESULT;
36 bool ReadInt64(int64* result) WARN_UNUSED_RESULT;
37 bool ReadUInt64(uint64* result) WARN_UNUSED_RESULT;
[email protected]b1f61b02012-11-28 15:40:5838 bool ReadFloat(float* result) WARN_UNUSED_RESULT;
[email protected]915cc7d2014-07-14 22:50:3239 bool ReadDouble(double* result) WARN_UNUSED_RESULT;
[email protected]ce208f82012-03-07 20:42:5640 bool ReadString(std::string* result) WARN_UNUSED_RESULT;
41 bool ReadWString(std::wstring* result) WARN_UNUSED_RESULT;
[email protected]476dafb2013-12-03 00:39:2642 bool ReadString16(base::string16* result) WARN_UNUSED_RESULT;
[email protected]ce208f82012-03-07 20:42:5643 bool ReadData(const char** data, int* length) WARN_UNUSED_RESULT;
44 bool ReadBytes(const char** data, int length) WARN_UNUSED_RESULT;
45
46 // Safer version of ReadInt() checks for the result not being negative.
47 // Use it for reading the object sizes.
48 bool ReadLength(int* result) WARN_UNUSED_RESULT {
49 return ReadInt(result) && *result >= 0;
50 }
51
52 // Skips bytes in the read buffer and returns true if there are at least
53 // num_bytes available. Otherwise, does nothing and returns false.
54 bool SkipBytes(int num_bytes) WARN_UNUSED_RESULT {
55 return !!GetReadPointerAndAdvance(num_bytes);
56 }
57
58 private:
59 // Aligns 'i' by rounding it up to the next multiple of 'alignment'
60 static size_t AlignInt(size_t i, int alignment) {
61 return i + (alignment - (i % alignment)) % alignment;
62 }
63
64 // Read Type from Pickle.
65 template <typename Type>
[email protected]a15016f2014-06-02 23:23:4966 bool ReadBuiltinType(Type* result);
67
68 // Advance read_index_ but do not allow it to exceed end_index_.
69 // Keeps read_index_ aligned.
70 void Advance(size_t size);
[email protected]ce208f82012-03-07 20:42:5671
72 // Get read pointer for Type and advance read pointer.
73 template<typename Type>
[email protected]a15016f2014-06-02 23:23:4974 const char* GetReadPointerAndAdvance();
[email protected]ce208f82012-03-07 20:42:5675
76 // Get read pointer for |num_bytes| and advance read pointer. This method
77 // checks num_bytes for negativity and wrapping.
78 const char* GetReadPointerAndAdvance(int num_bytes);
79
80 // Get read pointer for (num_elements * size_element) bytes and advance read
81 // pointer. This method checks for int overflow, negativity and wrapping.
[email protected]a15016f2014-06-02 23:23:4982 const char* GetReadPointerAndAdvance(int num_elements,
83 size_t size_element);
[email protected]ce208f82012-03-07 20:42:5684
[email protected]a15016f2014-06-02 23:23:4985 const char* payload_; // Start of our pickle's payload.
86 size_t read_index_; // Offset of the next readable byte in payload.
87 size_t end_index_; // Payload size.
[email protected]ce208f82012-03-07 20:42:5688
89 FRIEND_TEST_ALL_PREFIXES(PickleTest, GetReadPointerAndAdvance);
90};
91
initial.commitd7cae122008-07-26 21:49:3892// This class provides facilities for basic binary value packing and unpacking.
93//
94// The Pickle class supports appending primitive values (ints, strings, etc.)
95// to a pickle instance. The Pickle instance grows its internal memory buffer
96// dynamically to hold the sequence of primitive values. The internal memory
97// buffer is exposed as the "data" of the Pickle. This "data" can be passed
98// to a Pickle object to initialize it for reading.
99//
100// When reading from a Pickle object, it is important for the consumer to know
101// what value types to read and in what order to read them as the Pickle does
102// not keep track of the type of data written to it.
103//
104// The Pickle's data has a header which contains the size of the Pickle's
105// payload. It can optionally support additional space in the header. That
106// space is controlled by the header_size parameter passed to the Pickle
107// constructor.
108//
[email protected]0bea7252011-08-05 15:34:00109class BASE_EXPORT Pickle {
initial.commitd7cae122008-07-26 21:49:38110 public:
initial.commitd7cae122008-07-26 21:49:38111 // Initialize a Pickle object using the default header size.
112 Pickle();
113
114 // Initialize a Pickle object with the specified header size in bytes, which
115 // must be greater-than-or-equal-to sizeof(Pickle::Header). The header size
116 // will be rounded up to ensure that the header size is 32bit-aligned.
117 explicit Pickle(int header_size);
118
119 // Initializes a Pickle from a const block of data. The data is not copied;
120 // instead the data is merely referenced by this Pickle. Only const methods
121 // should be used on the Pickle when initialized this way. The header
122 // padding size is deduced from the data length.
[email protected]753bb252013-11-04 22:28:12123 Pickle(const char* data, int data_len);
initial.commitd7cae122008-07-26 21:49:38124
125 // Initializes a Pickle as a deep copy of another Pickle.
126 Pickle(const Pickle& other);
127
[email protected]f60c32b2011-09-25 03:08:13128 // Note: There are no virtual methods in this class. This destructor is
129 // virtual as an element of defensive coding. Other classes have derived from
130 // this class, and there is a *chance* that they will cast into this base
131 // class before destruction. At least one such class does have a virtual
132 // destructor, suggesting at least some need to call more derived destructors.
[email protected]a502bbe2011-01-07 18:06:45133 virtual ~Pickle();
134
initial.commitd7cae122008-07-26 21:49:38135 // Performs a deep copy.
136 Pickle& operator=(const Pickle& other);
137
138 // Returns the size of the Pickle's data.
[email protected]8a861402011-01-28 19:59:11139 size_t size() const { return header_size_ + header_->payload_size; }
initial.commitd7cae122008-07-26 21:49:38140
141 // Returns the data for this Pickle.
142 const void* data() const { return header_; }
143
[email protected]ce208f82012-03-07 20:42:56144 // For compatibility, these older style read methods pass through to the
145 // PickleIterator methods.
146 // TODO(jbates) Remove these methods.
[email protected]aa638712013-10-16 17:54:49147 bool ReadBool(PickleIterator* iter,
148 bool* result) const WARN_UNUSED_RESULT {
[email protected]ce208f82012-03-07 20:42:56149 return iter->ReadBool(result);
150 }
[email protected]aa638712013-10-16 17:54:49151 bool ReadInt(PickleIterator* iter,
152 int* result) const WARN_UNUSED_RESULT {
[email protected]ce208f82012-03-07 20:42:56153 return iter->ReadInt(result);
154 }
[email protected]aa638712013-10-16 17:54:49155 bool ReadLong(PickleIterator* iter,
156 long* result) const WARN_UNUSED_RESULT {
[email protected]ce208f82012-03-07 20:42:56157 return iter->ReadLong(result);
158 }
[email protected]aa638712013-10-16 17:54:49159 bool ReadUInt16(PickleIterator* iter,
160 uint16* result) const WARN_UNUSED_RESULT {
[email protected]ce208f82012-03-07 20:42:56161 return iter->ReadUInt16(result);
162 }
[email protected]aa638712013-10-16 17:54:49163 bool ReadUInt32(PickleIterator* iter,
164 uint32* result) const WARN_UNUSED_RESULT {
[email protected]ce208f82012-03-07 20:42:56165 return iter->ReadUInt32(result);
166 }
[email protected]aa638712013-10-16 17:54:49167 bool ReadInt64(PickleIterator* iter,
168 int64* result) const WARN_UNUSED_RESULT {
[email protected]ce208f82012-03-07 20:42:56169 return iter->ReadInt64(result);
170 }
[email protected]aa638712013-10-16 17:54:49171 bool ReadUInt64(PickleIterator* iter,
172 uint64* result) const WARN_UNUSED_RESULT {
[email protected]ce208f82012-03-07 20:42:56173 return iter->ReadUInt64(result);
174 }
[email protected]aa638712013-10-16 17:54:49175 bool ReadFloat(PickleIterator* iter,
176 float* result) const WARN_UNUSED_RESULT {
[email protected]b1f61b02012-11-28 15:40:58177 return iter->ReadFloat(result);
178 }
[email protected]915cc7d2014-07-14 22:50:32179 bool ReadDouble(PickleIterator* iter,
180 double* result) const WARN_UNUSED_RESULT {
181 return iter->ReadDouble(result);
182 }
[email protected]aa638712013-10-16 17:54:49183 bool ReadString(PickleIterator* iter,
184 std::string* result) const WARN_UNUSED_RESULT {
[email protected]ce208f82012-03-07 20:42:56185 return iter->ReadString(result);
186 }
[email protected]aa638712013-10-16 17:54:49187 bool ReadWString(PickleIterator* iter,
188 std::wstring* result) const WARN_UNUSED_RESULT {
[email protected]ce208f82012-03-07 20:42:56189 return iter->ReadWString(result);
190 }
[email protected]aa638712013-10-16 17:54:49191 bool ReadString16(PickleIterator* iter,
[email protected]476dafb2013-12-03 00:39:26192 base::string16* result) const WARN_UNUSED_RESULT {
[email protected]ce208f82012-03-07 20:42:56193 return iter->ReadString16(result);
194 }
[email protected]34d48612012-06-29 00:05:04195 // A pointer to the data will be placed in *data, and the length will be
196 // placed in *length. This buffer will be into the message's buffer so will
197 // be scoped to the lifetime of the message (or until the message data is
198 // mutated).
[email protected]aa638712013-10-16 17:54:49199 bool ReadData(PickleIterator* iter,
200 const char** data,
201 int* length) const WARN_UNUSED_RESULT {
[email protected]ce208f82012-03-07 20:42:56202 return iter->ReadData(data, length);
203 }
[email protected]34d48612012-06-29 00:05:04204 // A pointer to the data will be placed in *data. The caller specifies the
205 // number of bytes to read, and ReadBytes will validate this length. The
206 // returned buffer will be into the message's buffer so will be scoped to the
207 // lifetime of the message (or until the message data is mutated).
[email protected]aa638712013-10-16 17:54:49208 bool ReadBytes(PickleIterator* iter,
209 const char** data,
210 int length) const WARN_UNUSED_RESULT {
[email protected]ce208f82012-03-07 20:42:56211 return iter->ReadBytes(data, length);
212 }
initial.commitd7cae122008-07-26 21:49:38213
214 // Safer version of ReadInt() checks for the result not being negative.
215 // Use it for reading the object sizes.
[email protected]aa638712013-10-16 17:54:49216 bool ReadLength(PickleIterator* iter,
217 int* result) const WARN_UNUSED_RESULT {
[email protected]ce208f82012-03-07 20:42:56218 return iter->ReadLength(result);
219 }
initial.commitd7cae122008-07-26 21:49:38220
221 // Methods for adding to the payload of the Pickle. These values are
222 // appended to the end of the Pickle's payload. When reading values from a
223 // Pickle, it is important to read them in the order in which they were added
224 // to the Pickle.
225 bool WriteBool(bool value) {
226 return WriteInt(value ? 1 : 0);
227 }
228 bool WriteInt(int value) {
[email protected]d1b319f2013-10-31 04:03:02229 return WritePOD(value);
initial.commitd7cae122008-07-26 21:49:38230 }
[email protected]c272d082012-03-23 00:03:10231 // WARNING: DO NOT USE THIS METHOD IF PICKLES ARE PERSISTED IN ANY WAY.
232 // It will write whatever a "long" is on this architecture. On 32-bit
233 // platforms, it is 32 bits. On 64-bit platforms, it is 64 bits. If persisted
234 // pickles are still around after upgrading to 64-bit, or if they are copied
235 // between dissimilar systems, YOUR PICKLES WILL HAVE GONE BAD.
236 bool WriteLongUsingDangerousNonPortableLessPersistableForm(long value) {
[email protected]d1b319f2013-10-31 04:03:02237 return WritePOD(value);
initial.commitd7cae122008-07-26 21:49:38238 }
[email protected]6d81b482011-02-22 19:47:19239 bool WriteUInt16(uint16 value) {
[email protected]d1b319f2013-10-31 04:03:02240 return WritePOD(value);
[email protected]6d81b482011-02-22 19:47:19241 }
[email protected]48ce6162008-12-29 18:55:18242 bool WriteUInt32(uint32 value) {
[email protected]d1b319f2013-10-31 04:03:02243 return WritePOD(value);
[email protected]48ce6162008-12-29 18:55:18244 }
initial.commitd7cae122008-07-26 21:49:38245 bool WriteInt64(int64 value) {
[email protected]d1b319f2013-10-31 04:03:02246 return WritePOD(value);
initial.commitd7cae122008-07-26 21:49:38247 }
[email protected]b7a5d992009-10-28 04:21:01248 bool WriteUInt64(uint64 value) {
[email protected]d1b319f2013-10-31 04:03:02249 return WritePOD(value);
[email protected]b7a5d992009-10-28 04:21:01250 }
[email protected]b1f61b02012-11-28 15:40:58251 bool WriteFloat(float value) {
[email protected]d1b319f2013-10-31 04:03:02252 return WritePOD(value);
[email protected]b1f61b02012-11-28 15:40:58253 }
[email protected]915cc7d2014-07-14 22:50:32254 bool WriteDouble(double value) {
255 return WritePOD(value);
256 }
initial.commitd7cae122008-07-26 21:49:38257 bool WriteString(const std::string& value);
258 bool WriteWString(const std::wstring& value);
[email protected]476dafb2013-12-03 00:39:26259 bool WriteString16(const base::string16& value);
[email protected]34d48612012-06-29 00:05:04260 // "Data" is a blob with a length. When you read it out you will be given the
261 // length. See also WriteBytes.
initial.commitd7cae122008-07-26 21:49:38262 bool WriteData(const char* data, int length);
[email protected]d1b319f2013-10-31 04:03:02263 // "Bytes" is a blob with no length. The caller must specify the length both
[email protected]34d48612012-06-29 00:05:04264 // when reading and writing. It is normally used to serialize PoD types of a
265 // known size. See also WriteData.
[email protected]d1b319f2013-10-31 04:03:02266 bool WriteBytes(const void* data, int length);
initial.commitd7cae122008-07-26 21:49:38267
[email protected]032bfc42013-10-29 22:23:52268 // Reserves space for upcoming writes when multiple writes will be made and
269 // their sizes are computed in advance. It can be significantly faster to call
270 // Reserve() before calling WriteFoo() multiple times.
271 void Reserve(size_t additional_capacity);
272
[email protected]c9046af2008-08-06 20:35:17273 // Payload follows after allocation of Header (header size is customizable).
initial.commitd7cae122008-07-26 21:49:38274 struct Header {
[email protected]c9046af2008-08-06 20:35:17275 uint32 payload_size; // Specifies the size of the payload.
initial.commitd7cae122008-07-26 21:49:38276 };
277
278 // Returns the header, cast to a user-specified type T. The type T must be a
279 // subclass of Header and its size must correspond to the header_size passed
280 // to the Pickle constructor.
281 template <class T>
282 T* headerT() {
[email protected]5d2b4492011-03-01 02:48:05283 DCHECK_EQ(header_size_, sizeof(T));
initial.commitd7cae122008-07-26 21:49:38284 return static_cast<T*>(header_);
285 }
286 template <class T>
287 const T* headerT() const {
[email protected]5d2b4492011-03-01 02:48:05288 DCHECK_EQ(header_size_, sizeof(T));
initial.commitd7cae122008-07-26 21:49:38289 return static_cast<const T*>(header_);
290 }
291
[email protected]73d96dc2012-03-30 22:35:27292 // The payload is the pickle data immediately following the header.
[email protected]a15016f2014-06-02 23:23:49293 size_t payload_size() const {
294 return header_ ? header_->payload_size : 0;
295 }
[email protected]e00a6c0a2013-01-18 18:20:57296
initial.commitd7cae122008-07-26 21:49:38297 const char* payload() const {
298 return reinterpret_cast<const char*>(header_) + header_size_;
299 }
300
301 // Returns the address of the byte immediately following the currently valid
302 // header + payload.
initial.commitd7cae122008-07-26 21:49:38303 const char* end_of_payload() const {
[email protected]d87f8e62010-11-15 19:31:23304 // This object may be invalid.
305 return header_ ? payload() + payload_size() : NULL;
initial.commitd7cae122008-07-26 21:49:38306 }
307
[email protected]e00a6c0a2013-01-18 18:20:57308 protected:
309 char* mutable_payload() {
310 return reinterpret_cast<char*>(header_) + header_size_;
311 }
312
[email protected]d1b319f2013-10-31 04:03:02313 size_t capacity_after_header() const {
314 return capacity_after_header_;
initial.commitd7cae122008-07-26 21:49:38315 }
316
[email protected]d1b319f2013-10-31 04:03:02317 // Resize the capacity, note that the input value should not include the size
318 // of the header.
319 void Resize(size_t new_capacity);
initial.commitd7cae122008-07-26 21:49:38320
321 // Aligns 'i' by rounding it up to the next multiple of 'alignment'
[email protected]c9046af2008-08-06 20:35:17322 static size_t AlignInt(size_t i, int alignment) {
initial.commitd7cae122008-07-26 21:49:38323 return i + (alignment - (i % alignment)) % alignment;
324 }
325
initial.commitd7cae122008-07-26 21:49:38326 // Find the end of the pickled data that starts at range_start. Returns NULL
327 // if the entire Pickle is not found in the given data range.
328 static const char* FindNext(size_t header_size,
329 const char* range_start,
330 const char* range_end);
331
332 // The allocation granularity of the payload.
333 static const int kPayloadUnit;
334
335 private:
[email protected]ce208f82012-03-07 20:42:56336 friend class PickleIterator;
337
initial.commitd7cae122008-07-26 21:49:38338 Header* header_;
339 size_t header_size_; // Supports extra data between header and payload.
[email protected]d1b319f2013-10-31 04:03:02340 // Allocation size of payload (or -1 if allocation is const). Note: this
341 // doesn't count the header.
342 size_t capacity_after_header_;
343 // The offset at which we will write the next field. Note: this doesn't count
344 // the header.
345 size_t write_offset_;
346
347 // Just like WriteBytes, but with a compile-time size, for performance.
[email protected]ba721602014-06-11 00:34:38348 template<size_t length> void BASE_EXPORT WriteBytesStatic(const void* data);
[email protected]d1b319f2013-10-31 04:03:02349
350 // Writes a POD by copying its bytes.
351 template <typename T> bool WritePOD(const T& data) {
352 WriteBytesStatic<sizeof(data)>(&data);
353 return true;
354 }
355 inline void WriteBytesCommon(const void* data, size_t length);
initial.commitd7cae122008-07-26 21:49:38356
[email protected]a918f872010-06-01 14:30:51357 FRIEND_TEST_ALL_PREFIXES(PickleTest, Resize);
358 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNext);
[email protected]137d2372011-01-26 13:02:27359 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextWithIncompleteHeader);
[email protected]33a38dd2013-11-01 09:06:26360 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextOverflow);
initial.commitd7cae122008-07-26 21:49:38361};
362
363#endif // BASE_PICKLE_H__