[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
tfarina | a3116351 | 2015-05-13 22:10:15 | [diff] [blame] | 5 | #ifndef BASE_PICKLE_H_ |
| 6 | #define BASE_PICKLE_H_ |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 7 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 8 | #include <stddef.h> |
avi | c027914 | 2015-12-04 22:38:52 | [diff] [blame] | 9 | #include <stdint.h> |
| 10 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 11 | #include <string> |
| 12 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 13 | #include "base/base_export.h" |
Hans Wennborg | 7b53371 | 2020-06-22 20:52:27 | [diff] [blame] | 14 | #include "base/check_op.h" |
Jeremy Roman | 2d8d780 | 2020-06-11 22:08:20 | [diff] [blame] | 15 | #include "base/containers/span.h" |
[email protected] | a918f87 | 2010-06-01 14:30:51 | [diff] [blame] | 16 | #include "base/gtest_prod_util.h" |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 17 | #include "base/memory/ref_counted.h" |
brucedawson | eaa3896 | 2015-03-10 01:46:50 | [diff] [blame] | 18 | #include "base/strings/string_piece.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 19 | |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame] | 20 | namespace base { |
| 21 | |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 22 | class Pickle; |
| 23 | |
| 24 | // PickleIterator reads data from a Pickle. The Pickle object must remain valid |
| 25 | // while the PickleIterator object is in use. |
| 26 | class BASE_EXPORT PickleIterator { |
| 27 | public: |
Raul Tambre | f191b59 | 2019-01-21 23:56:29 | [diff] [blame] | 28 | PickleIterator() : payload_(nullptr), read_index_(0), end_index_(0) {} |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 29 | 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 |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 34 | // result could not be extracted. It is not possible to read from the iterator |
[email protected] | a15016f | 2014-06-02 23:23:49 | [diff] [blame] | 35 | // after that. |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 36 | bool ReadBool(bool* result) WARN_UNUSED_RESULT; |
| 37 | bool ReadInt(int* result) WARN_UNUSED_RESULT; |
| 38 | bool ReadLong(long* result) WARN_UNUSED_RESULT; |
avi | c027914 | 2015-12-04 22:38:52 | [diff] [blame] | 39 | 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; |
[email protected] | b1f61b03 | 2012-11-28 15:40:58 | [diff] [blame] | 43 | bool ReadFloat(float* result) WARN_UNUSED_RESULT; |
[email protected] | 915cc7d | 2014-07-14 22:50:32 | [diff] [blame] | 44 | bool ReadDouble(double* result) WARN_UNUSED_RESULT; |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 45 | bool ReadString(std::string* result) WARN_UNUSED_RESULT; |
brucedawson | eaa3896 | 2015-03-10 01:46:50 | [diff] [blame] | 46 | // The StringPiece data will only be valid for the lifetime of the message. |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame] | 47 | bool ReadStringPiece(StringPiece* result) WARN_UNUSED_RESULT; |
Jan Wilken Dörrie | 085b2aa | 2021-03-12 16:26:57 | [diff] [blame] | 48 | bool ReadString16(std::u16string* result) WARN_UNUSED_RESULT; |
brucedawson | eaa3896 | 2015-03-10 01:46:50 | [diff] [blame] | 49 | // The StringPiece16 data will only be valid for the lifetime of the message. |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame] | 50 | bool ReadStringPiece16(StringPiece16* result) WARN_UNUSED_RESULT; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 51 | |
| 52 | // A pointer to the data will be placed in |*data|, and the length will be |
| 53 | // placed in |*length|. The pointer placed into |*data| points into the |
| 54 | // message's buffer so it will be scoped to the lifetime of the message (or |
| 55 | // until the message data is mutated). Do not keep the pointer around! |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 56 | bool ReadData(const char** data, int* length) WARN_UNUSED_RESULT; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 57 | |
Jeremy Roman | 2d8d780 | 2020-06-11 22:08:20 | [diff] [blame] | 58 | // Similar, but using base::span for convenience. |
| 59 | bool ReadData(base::span<const uint8_t>* data) WARN_UNUSED_RESULT; |
| 60 | |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 61 | // A pointer to the data will be placed in |*data|. The caller specifies the |
| 62 | // number of bytes to read, and ReadBytes will validate this length. The |
| 63 | // pointer placed into |*data| points into the message's buffer so it will be |
| 64 | // scoped to the lifetime of the message (or until the message data is |
| 65 | // mutated). Do not keep the pointer around! |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 66 | bool ReadBytes(const char** data, int length) WARN_UNUSED_RESULT; |
| 67 | |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 68 | // A safer version of ReadInt() that checks for the result not being negative. |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 69 | // Use it for reading the object sizes. |
| 70 | bool ReadLength(int* result) WARN_UNUSED_RESULT { |
| 71 | return ReadInt(result) && *result >= 0; |
| 72 | } |
| 73 | |
| 74 | // Skips bytes in the read buffer and returns true if there are at least |
| 75 | // num_bytes available. Otherwise, does nothing and returns false. |
| 76 | bool SkipBytes(int num_bytes) WARN_UNUSED_RESULT { |
| 77 | return !!GetReadPointerAndAdvance(num_bytes); |
| 78 | } |
| 79 | |
Daniel Cheng | ea877be | 2020-03-06 22:56:31 | [diff] [blame] | 80 | bool ReachedEnd() const { return read_index_ == end_index_; } |
| 81 | |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 82 | private: |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 83 | // Read Type from Pickle. |
| 84 | template <typename Type> |
[email protected] | a15016f | 2014-06-02 23:23:49 | [diff] [blame] | 85 | bool ReadBuiltinType(Type* result); |
| 86 | |
| 87 | // Advance read_index_ but do not allow it to exceed end_index_. |
| 88 | // Keeps read_index_ aligned. |
| 89 | void Advance(size_t size); |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 90 | |
| 91 | // Get read pointer for Type and advance read pointer. |
| 92 | template<typename Type> |
[email protected] | a15016f | 2014-06-02 23:23:49 | [diff] [blame] | 93 | const char* GetReadPointerAndAdvance(); |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 94 | |
| 95 | // Get read pointer for |num_bytes| and advance read pointer. This method |
| 96 | // checks num_bytes for negativity and wrapping. |
| 97 | const char* GetReadPointerAndAdvance(int num_bytes); |
| 98 | |
| 99 | // Get read pointer for (num_elements * size_element) bytes and advance read |
| 100 | // pointer. This method checks for int overflow, negativity and wrapping. |
[email protected] | a15016f | 2014-06-02 23:23:49 | [diff] [blame] | 101 | const char* GetReadPointerAndAdvance(int num_elements, |
| 102 | size_t size_element); |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 103 | |
[email protected] | a15016f | 2014-06-02 23:23:49 | [diff] [blame] | 104 | const char* payload_; // Start of our pickle's payload. |
| 105 | size_t read_index_; // Offset of the next readable byte in payload. |
| 106 | size_t end_index_; // Payload size. |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 107 | |
| 108 | FRIEND_TEST_ALL_PREFIXES(PickleTest, GetReadPointerAndAdvance); |
| 109 | }; |
| 110 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 111 | // This class provides facilities for basic binary value packing and unpacking. |
| 112 | // |
| 113 | // The Pickle class supports appending primitive values (ints, strings, etc.) |
| 114 | // to a pickle instance. The Pickle instance grows its internal memory buffer |
| 115 | // dynamically to hold the sequence of primitive values. The internal memory |
| 116 | // buffer is exposed as the "data" of the Pickle. This "data" can be passed |
| 117 | // to a Pickle object to initialize it for reading. |
| 118 | // |
| 119 | // When reading from a Pickle object, it is important for the consumer to know |
| 120 | // what value types to read and in what order to read them as the Pickle does |
| 121 | // not keep track of the type of data written to it. |
| 122 | // |
| 123 | // The Pickle's data has a header which contains the size of the Pickle's |
| 124 | // payload. It can optionally support additional space in the header. That |
| 125 | // space is controlled by the header_size parameter passed to the Pickle |
| 126 | // constructor. |
| 127 | // |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 128 | class BASE_EXPORT Pickle { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 129 | public: |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 130 | // Auxiliary data attached to a Pickle. Pickle must be subclassed along with |
| 131 | // this interface in order to provide a concrete implementation of support |
| 132 | // for attachments. The base Pickle implementation does not accept |
| 133 | // attachments. |
| 134 | class BASE_EXPORT Attachment : public RefCountedThreadSafe<Attachment> { |
| 135 | public: |
| 136 | Attachment(); |
David Bienvenu | 5f4d4f0 | 2020-09-27 16:55:03 | [diff] [blame] | 137 | Attachment(const Attachment&) = delete; |
| 138 | Attachment& operator=(const Attachment&) = delete; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 139 | |
| 140 | protected: |
| 141 | friend class RefCountedThreadSafe<Attachment>; |
| 142 | virtual ~Attachment(); |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 143 | }; |
| 144 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 145 | // Initialize a Pickle object using the default header size. |
| 146 | Pickle(); |
| 147 | |
| 148 | // Initialize a Pickle object with the specified header size in bytes, which |
| 149 | // must be greater-than-or-equal-to sizeof(Pickle::Header). The header size |
| 150 | // will be rounded up to ensure that the header size is 32bit-aligned. |
| 151 | explicit Pickle(int header_size); |
| 152 | |
| 153 | // Initializes a Pickle from a const block of data. The data is not copied; |
| 154 | // instead the data is merely referenced by this Pickle. Only const methods |
| 155 | // should be used on the Pickle when initialized this way. The header |
| 156 | // padding size is deduced from the data length. |
Raul Tambre | f191b59 | 2019-01-21 23:56:29 | [diff] [blame] | 157 | Pickle(const char* data, size_t data_len); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 158 | |
| 159 | // Initializes a Pickle as a deep copy of another Pickle. |
| 160 | Pickle(const Pickle& other); |
| 161 | |
[email protected] | f60c32b | 2011-09-25 03:08:13 | [diff] [blame] | 162 | // Note: There are no virtual methods in this class. This destructor is |
| 163 | // virtual as an element of defensive coding. Other classes have derived from |
| 164 | // this class, and there is a *chance* that they will cast into this base |
| 165 | // class before destruction. At least one such class does have a virtual |
| 166 | // destructor, suggesting at least some need to call more derived destructors. |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 167 | virtual ~Pickle(); |
| 168 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 169 | // Performs a deep copy. |
| 170 | Pickle& operator=(const Pickle& other); |
| 171 | |
primiano | 9882cf34 | 2015-06-11 21:40:10 | [diff] [blame] | 172 | // Returns the number of bytes written in the Pickle, including the header. |
David Sanders | 4252759 | 2021-06-16 07:20:38 | [diff] [blame^] | 173 | size_t size() const { |
| 174 | return header_ ? header_size_ + header_->payload_size : 0; |
| 175 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 176 | |
| 177 | // Returns the data for this Pickle. |
| 178 | const void* data() const { return header_; } |
| 179 | |
primiano | 9882cf34 | 2015-06-11 21:40:10 | [diff] [blame] | 180 | // Returns the effective memory capacity of this Pickle, that is, the total |
| 181 | // number of bytes currently dynamically allocated or 0 in the case of a |
| 182 | // read-only Pickle. This should be used only for diagnostic / profiling |
| 183 | // purposes. |
| 184 | size_t GetTotalAllocatedSize() const; |
| 185 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 186 | // Methods for adding to the payload of the Pickle. These values are |
| 187 | // appended to the end of the Pickle's payload. When reading values from a |
| 188 | // Pickle, it is important to read them in the order in which they were added |
| 189 | // to the Pickle. |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 190 | |
Daniel Cheng | 0d89f922 | 2017-09-22 05:05:07 | [diff] [blame] | 191 | void WriteBool(bool value) { WriteInt(value ? 1 : 0); } |
| 192 | void WriteInt(int value) { WritePOD(value); } |
| 193 | void WriteLong(long value) { |
jam | 03d8a78 | 2016-02-10 20:13:39 | [diff] [blame] | 194 | // Always write long as a 64-bit value to ensure compatibility between |
| 195 | // 32-bit and 64-bit processes. |
Daniel Cheng | 0d89f922 | 2017-09-22 05:05:07 | [diff] [blame] | 196 | WritePOD(static_cast<int64_t>(value)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 197 | } |
Daniel Cheng | 0d89f922 | 2017-09-22 05:05:07 | [diff] [blame] | 198 | void WriteUInt16(uint16_t value) { WritePOD(value); } |
| 199 | void WriteUInt32(uint32_t value) { WritePOD(value); } |
| 200 | void WriteInt64(int64_t value) { WritePOD(value); } |
| 201 | void WriteUInt64(uint64_t value) { WritePOD(value); } |
| 202 | void WriteFloat(float value) { WritePOD(value); } |
| 203 | void WriteDouble(double value) { WritePOD(value); } |
| 204 | void WriteString(const StringPiece& value); |
| 205 | void WriteString16(const StringPiece16& value); |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 206 | // "Data" is a blob with a length. When you read it out you will be given the |
| 207 | // length. See also WriteBytes. |
Daniel Cheng | 0d89f922 | 2017-09-22 05:05:07 | [diff] [blame] | 208 | void WriteData(const char* data, int length); |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 209 | // "Bytes" is a blob with no length. The caller must specify the length both |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 210 | // when reading and writing. It is normally used to serialize PoD types of a |
| 211 | // known size. See also WriteData. |
Daniel Cheng | 0d89f922 | 2017-09-22 05:05:07 | [diff] [blame] | 212 | void WriteBytes(const void* data, int length); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 213 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 214 | // WriteAttachment appends |attachment| to the pickle. It returns |
| 215 | // false iff the set is full or if the Pickle implementation does not support |
| 216 | // attachments. |
| 217 | virtual bool WriteAttachment(scoped_refptr<Attachment> attachment); |
| 218 | |
| 219 | // ReadAttachment parses an attachment given the parsing state |iter| and |
| 220 | // writes it to |*attachment|. It returns true on success. |
| 221 | virtual bool ReadAttachment(base::PickleIterator* iter, |
| 222 | scoped_refptr<Attachment>* attachment) const; |
| 223 | |
| 224 | // Indicates whether the pickle has any attachments. |
| 225 | virtual bool HasAttachments() const; |
| 226 | |
[email protected] | 032bfc4 | 2013-10-29 22:23:52 | [diff] [blame] | 227 | // Reserves space for upcoming writes when multiple writes will be made and |
| 228 | // their sizes are computed in advance. It can be significantly faster to call |
| 229 | // Reserve() before calling WriteFoo() multiple times. |
| 230 | void Reserve(size_t additional_capacity); |
| 231 | |
[email protected] | c9046af | 2008-08-06 20:35:17 | [diff] [blame] | 232 | // Payload follows after allocation of Header (header size is customizable). |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 233 | struct Header { |
avi | c027914 | 2015-12-04 22:38:52 | [diff] [blame] | 234 | uint32_t payload_size; // Specifies the size of the payload. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 235 | }; |
| 236 | |
| 237 | // Returns the header, cast to a user-specified type T. The type T must be a |
| 238 | // subclass of Header and its size must correspond to the header_size passed |
| 239 | // to the Pickle constructor. |
| 240 | template <class T> |
| 241 | T* headerT() { |
[email protected] | 5d2b449 | 2011-03-01 02:48:05 | [diff] [blame] | 242 | DCHECK_EQ(header_size_, sizeof(T)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 243 | return static_cast<T*>(header_); |
| 244 | } |
| 245 | template <class T> |
| 246 | const T* headerT() const { |
[email protected] | 5d2b449 | 2011-03-01 02:48:05 | [diff] [blame] | 247 | DCHECK_EQ(header_size_, sizeof(T)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 248 | return static_cast<const T*>(header_); |
| 249 | } |
| 250 | |
[email protected] | 73d96dc | 2012-03-30 22:35:27 | [diff] [blame] | 251 | // The payload is the pickle data immediately following the header. |
[email protected] | a15016f | 2014-06-02 23:23:49 | [diff] [blame] | 252 | size_t payload_size() const { |
| 253 | return header_ ? header_->payload_size : 0; |
| 254 | } |
[email protected] | e00a6c0a | 2013-01-18 18:20:57 | [diff] [blame] | 255 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 256 | const char* payload() const { |
| 257 | return reinterpret_cast<const char*>(header_) + header_size_; |
| 258 | } |
| 259 | |
| 260 | // Returns the address of the byte immediately following the currently valid |
| 261 | // header + payload. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 262 | const char* end_of_payload() const { |
[email protected] | d87f8e6f | 2010-11-15 19:31:23 | [diff] [blame] | 263 | // This object may be invalid. |
| 264 | return header_ ? payload() + payload_size() : NULL; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 265 | } |
| 266 | |
[email protected] | e00a6c0a | 2013-01-18 18:20:57 | [diff] [blame] | 267 | protected: |
Roman Karasev | a43d5b4e | 2017-12-21 03:06:02 | [diff] [blame] | 268 | // Returns size of the header, which can have default value, set by user or |
| 269 | // calculated by passed raw data. |
| 270 | size_t header_size() const { return header_size_; } |
| 271 | |
[email protected] | e00a6c0a | 2013-01-18 18:20:57 | [diff] [blame] | 272 | char* mutable_payload() { |
| 273 | return reinterpret_cast<char*>(header_) + header_size_; |
| 274 | } |
| 275 | |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 276 | size_t capacity_after_header() const { |
| 277 | return capacity_after_header_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 278 | } |
| 279 | |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 280 | // Resize the capacity, note that the input value should not include the size |
| 281 | // of the header. |
| 282 | void Resize(size_t new_capacity); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 283 | |
rockot | 0776a50 | 2015-12-17 06:19:49 | [diff] [blame] | 284 | // Claims |num_bytes| bytes of payload. This is similar to Reserve() in that |
| 285 | // it may grow the capacity, but it also advances the write offset of the |
| 286 | // pickle by |num_bytes|. Claimed memory, including padding, is zeroed. |
| 287 | // |
| 288 | // Returns the address of the first byte claimed. |
| 289 | void* ClaimBytes(size_t num_bytes); |
| 290 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 291 | // Find the end of the pickled data that starts at range_start. Returns NULL |
| 292 | // if the entire Pickle is not found in the given data range. |
| 293 | static const char* FindNext(size_t header_size, |
| 294 | const char* range_start, |
| 295 | const char* range_end); |
| 296 | |
dskiba | 6f3790a | 2015-09-30 17:24:30 | [diff] [blame] | 297 | // Parse pickle header and return total size of the pickle. Data range |
| 298 | // doesn't need to contain entire pickle. |
| 299 | // Returns true if pickle header was found and parsed. Callers must check |
| 300 | // returned |pickle_size| for sanity (against maximum message size, etc). |
| 301 | // NOTE: when function successfully parses a header, but encounters an |
| 302 | // overflow during pickle size calculation, it sets |pickle_size| to the |
| 303 | // maximum size_t value and returns true. |
| 304 | static bool PeekNext(size_t header_size, |
| 305 | const char* range_start, |
| 306 | const char* range_end, |
| 307 | size_t* pickle_size); |
| 308 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 309 | // The allocation granularity of the payload. |
| 310 | static const int kPayloadUnit; |
| 311 | |
| 312 | private: |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 313 | friend class PickleIterator; |
| 314 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 315 | Header* header_; |
| 316 | size_t header_size_; // Supports extra data between header and payload. |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 317 | // Allocation size of payload (or -1 if allocation is const). Note: this |
| 318 | // doesn't count the header. |
| 319 | size_t capacity_after_header_; |
| 320 | // The offset at which we will write the next field. Note: this doesn't count |
| 321 | // the header. |
| 322 | size_t write_offset_; |
| 323 | |
| 324 | // Just like WriteBytes, but with a compile-time size, for performance. |
[email protected] | ba72160 | 2014-06-11 00:34:38 | [diff] [blame] | 325 | template<size_t length> void BASE_EXPORT WriteBytesStatic(const void* data); |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 326 | |
| 327 | // Writes a POD by copying its bytes. |
| 328 | template <typename T> bool WritePOD(const T& data) { |
| 329 | WriteBytesStatic<sizeof(data)>(&data); |
| 330 | return true; |
| 331 | } |
rockot | 0776a50 | 2015-12-17 06:19:49 | [diff] [blame] | 332 | |
| 333 | inline void* ClaimUninitializedBytesInternal(size_t num_bytes); |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 334 | inline void WriteBytesCommon(const void* data, size_t length); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 335 | |
erikchen | f9ca8f5f0 | 2015-09-08 23:36:29 | [diff] [blame] | 336 | FRIEND_TEST_ALL_PREFIXES(PickleTest, DeepCopyResize); |
[email protected] | a918f87 | 2010-06-01 14:30:51 | [diff] [blame] | 337 | FRIEND_TEST_ALL_PREFIXES(PickleTest, Resize); |
dskiba | 6f3790a | 2015-09-30 17:24:30 | [diff] [blame] | 338 | FRIEND_TEST_ALL_PREFIXES(PickleTest, PeekNext); |
| 339 | FRIEND_TEST_ALL_PREFIXES(PickleTest, PeekNextOverflow); |
[email protected] | a918f87 | 2010-06-01 14:30:51 | [diff] [blame] | 340 | FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNext); |
[email protected] | 137d237 | 2011-01-26 13:02:27 | [diff] [blame] | 341 | FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextWithIncompleteHeader); |
[email protected] | 33a38dd | 2013-11-01 09:06:26 | [diff] [blame] | 342 | FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextOverflow); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 343 | }; |
| 344 | |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame] | 345 | } // namespace base |
| 346 | |
tfarina | a3116351 | 2015-05-13 22:10:15 | [diff] [blame] | 347 | #endif // BASE_PICKLE_H_ |