[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 | |
| 5 | #ifndef BASE_PICKLE_H__ |
| 6 | #define BASE_PICKLE_H__ |
| 7 | |
| 8 | #include <string> |
| 9 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 10 | #include "base/base_export.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 11 | #include "base/basictypes.h" |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 12 | #include "base/compiler_specific.h" |
[email protected] | a918f87 | 2010-06-01 14:30:51 | [diff] [blame] | 13 | #include "base/gtest_prod_util.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 14 | #include "base/logging.h" |
[email protected] | d529cb0 | 2013-06-10 19:06:57 | [diff] [blame] | 15 | #include "base/strings/string16.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 16 | |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 17 | class Pickle; |
| 18 | |
| 19 | // PickleIterator reads data from a Pickle. The Pickle object must remain valid |
| 20 | // while the PickleIterator object is in use. |
| 21 | class BASE_EXPORT PickleIterator { |
| 22 | public: |
[email protected] | a15016f | 2014-06-02 23:23:49 | [diff] [blame^] | 23 | PickleIterator() : payload_(NULL), read_index_(0), end_index_(0) {} |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 24 | 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] | a15016f | 2014-06-02 23:23:49 | [diff] [blame^] | 29 | // result could not be extracted. It is not possible to read from iterator |
| 30 | // after that. |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 31 | 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] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 34 | 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] | b1f61b03 | 2012-11-28 15:40:58 | [diff] [blame] | 38 | bool ReadFloat(float* result) WARN_UNUSED_RESULT; |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 39 | bool ReadString(std::string* result) WARN_UNUSED_RESULT; |
| 40 | bool ReadWString(std::wstring* result) WARN_UNUSED_RESULT; |
[email protected] | 476dafb | 2013-12-03 00:39:26 | [diff] [blame] | 41 | bool ReadString16(base::string16* result) WARN_UNUSED_RESULT; |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 42 | bool ReadData(const char** data, int* length) WARN_UNUSED_RESULT; |
| 43 | bool ReadBytes(const char** data, int length) WARN_UNUSED_RESULT; |
| 44 | |
| 45 | // Safer version of ReadInt() checks for the result not being negative. |
| 46 | // Use it for reading the object sizes. |
| 47 | bool ReadLength(int* result) WARN_UNUSED_RESULT { |
| 48 | return ReadInt(result) && *result >= 0; |
| 49 | } |
| 50 | |
| 51 | // Skips bytes in the read buffer and returns true if there are at least |
| 52 | // num_bytes available. Otherwise, does nothing and returns false. |
| 53 | bool SkipBytes(int num_bytes) WARN_UNUSED_RESULT { |
| 54 | return !!GetReadPointerAndAdvance(num_bytes); |
| 55 | } |
| 56 | |
| 57 | private: |
| 58 | // Aligns 'i' by rounding it up to the next multiple of 'alignment' |
| 59 | static size_t AlignInt(size_t i, int alignment) { |
| 60 | return i + (alignment - (i % alignment)) % alignment; |
| 61 | } |
| 62 | |
| 63 | // Read Type from Pickle. |
| 64 | template <typename Type> |
[email protected] | a15016f | 2014-06-02 23:23:49 | [diff] [blame^] | 65 | bool ReadBuiltinType(Type* result); |
| 66 | |
| 67 | // Advance read_index_ but do not allow it to exceed end_index_. |
| 68 | // Keeps read_index_ aligned. |
| 69 | void Advance(size_t size); |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 70 | |
| 71 | // Get read pointer for Type and advance read pointer. |
| 72 | template<typename Type> |
[email protected] | a15016f | 2014-06-02 23:23:49 | [diff] [blame^] | 73 | const char* GetReadPointerAndAdvance(); |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 74 | |
| 75 | // Get read pointer for |num_bytes| and advance read pointer. This method |
| 76 | // checks num_bytes for negativity and wrapping. |
| 77 | const char* GetReadPointerAndAdvance(int num_bytes); |
| 78 | |
| 79 | // Get read pointer for (num_elements * size_element) bytes and advance read |
| 80 | // pointer. This method checks for int overflow, negativity and wrapping. |
[email protected] | a15016f | 2014-06-02 23:23:49 | [diff] [blame^] | 81 | const char* GetReadPointerAndAdvance(int num_elements, |
| 82 | size_t size_element); |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 83 | |
[email protected] | a15016f | 2014-06-02 23:23:49 | [diff] [blame^] | 84 | const char* payload_; // Start of our pickle's payload. |
| 85 | size_t read_index_; // Offset of the next readable byte in payload. |
| 86 | size_t end_index_; // Payload size. |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 87 | |
| 88 | FRIEND_TEST_ALL_PREFIXES(PickleTest, GetReadPointerAndAdvance); |
| 89 | }; |
| 90 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 91 | // This class provides facilities for basic binary value packing and unpacking. |
| 92 | // |
| 93 | // The Pickle class supports appending primitive values (ints, strings, etc.) |
| 94 | // to a pickle instance. The Pickle instance grows its internal memory buffer |
| 95 | // dynamically to hold the sequence of primitive values. The internal memory |
| 96 | // buffer is exposed as the "data" of the Pickle. This "data" can be passed |
| 97 | // to a Pickle object to initialize it for reading. |
| 98 | // |
| 99 | // When reading from a Pickle object, it is important for the consumer to know |
| 100 | // what value types to read and in what order to read them as the Pickle does |
| 101 | // not keep track of the type of data written to it. |
| 102 | // |
| 103 | // The Pickle's data has a header which contains the size of the Pickle's |
| 104 | // payload. It can optionally support additional space in the header. That |
| 105 | // space is controlled by the header_size parameter passed to the Pickle |
| 106 | // constructor. |
| 107 | // |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 108 | class BASE_EXPORT Pickle { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 109 | public: |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 110 | // Initialize a Pickle object using the default header size. |
| 111 | Pickle(); |
| 112 | |
| 113 | // Initialize a Pickle object with the specified header size in bytes, which |
| 114 | // must be greater-than-or-equal-to sizeof(Pickle::Header). The header size |
| 115 | // will be rounded up to ensure that the header size is 32bit-aligned. |
| 116 | explicit Pickle(int header_size); |
| 117 | |
| 118 | // Initializes a Pickle from a const block of data. The data is not copied; |
| 119 | // instead the data is merely referenced by this Pickle. Only const methods |
| 120 | // should be used on the Pickle when initialized this way. The header |
| 121 | // padding size is deduced from the data length. |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 122 | Pickle(const char* data, int data_len); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 123 | |
| 124 | // Initializes a Pickle as a deep copy of another Pickle. |
| 125 | Pickle(const Pickle& other); |
| 126 | |
[email protected] | f60c32b | 2011-09-25 03:08:13 | [diff] [blame] | 127 | // Note: There are no virtual methods in this class. This destructor is |
| 128 | // virtual as an element of defensive coding. Other classes have derived from |
| 129 | // this class, and there is a *chance* that they will cast into this base |
| 130 | // class before destruction. At least one such class does have a virtual |
| 131 | // destructor, suggesting at least some need to call more derived destructors. |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 132 | virtual ~Pickle(); |
| 133 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 134 | // Performs a deep copy. |
| 135 | Pickle& operator=(const Pickle& other); |
| 136 | |
| 137 | // Returns the size of the Pickle's data. |
[email protected] | 8a86140 | 2011-01-28 19:59:11 | [diff] [blame] | 138 | size_t size() const { return header_size_ + header_->payload_size; } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 139 | |
| 140 | // Returns the data for this Pickle. |
| 141 | const void* data() const { return header_; } |
| 142 | |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 143 | // For compatibility, these older style read methods pass through to the |
| 144 | // PickleIterator methods. |
| 145 | // TODO(jbates) Remove these methods. |
[email protected] | aa63871 | 2013-10-16 17:54:49 | [diff] [blame] | 146 | bool ReadBool(PickleIterator* iter, |
| 147 | bool* result) const WARN_UNUSED_RESULT { |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 148 | return iter->ReadBool(result); |
| 149 | } |
[email protected] | aa63871 | 2013-10-16 17:54:49 | [diff] [blame] | 150 | bool ReadInt(PickleIterator* iter, |
| 151 | int* result) const WARN_UNUSED_RESULT { |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 152 | return iter->ReadInt(result); |
| 153 | } |
[email protected] | aa63871 | 2013-10-16 17:54:49 | [diff] [blame] | 154 | bool ReadLong(PickleIterator* iter, |
| 155 | long* result) const WARN_UNUSED_RESULT { |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 156 | return iter->ReadLong(result); |
| 157 | } |
[email protected] | aa63871 | 2013-10-16 17:54:49 | [diff] [blame] | 158 | bool ReadUInt16(PickleIterator* iter, |
| 159 | uint16* result) const WARN_UNUSED_RESULT { |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 160 | return iter->ReadUInt16(result); |
| 161 | } |
[email protected] | aa63871 | 2013-10-16 17:54:49 | [diff] [blame] | 162 | bool ReadUInt32(PickleIterator* iter, |
| 163 | uint32* result) const WARN_UNUSED_RESULT { |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 164 | return iter->ReadUInt32(result); |
| 165 | } |
[email protected] | aa63871 | 2013-10-16 17:54:49 | [diff] [blame] | 166 | bool ReadInt64(PickleIterator* iter, |
| 167 | int64* result) const WARN_UNUSED_RESULT { |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 168 | return iter->ReadInt64(result); |
| 169 | } |
[email protected] | aa63871 | 2013-10-16 17:54:49 | [diff] [blame] | 170 | bool ReadUInt64(PickleIterator* iter, |
| 171 | uint64* result) const WARN_UNUSED_RESULT { |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 172 | return iter->ReadUInt64(result); |
| 173 | } |
[email protected] | aa63871 | 2013-10-16 17:54:49 | [diff] [blame] | 174 | bool ReadFloat(PickleIterator* iter, |
| 175 | float* result) const WARN_UNUSED_RESULT { |
[email protected] | b1f61b03 | 2012-11-28 15:40:58 | [diff] [blame] | 176 | return iter->ReadFloat(result); |
| 177 | } |
[email protected] | aa63871 | 2013-10-16 17:54:49 | [diff] [blame] | 178 | bool ReadString(PickleIterator* iter, |
| 179 | std::string* result) const WARN_UNUSED_RESULT { |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 180 | return iter->ReadString(result); |
| 181 | } |
[email protected] | aa63871 | 2013-10-16 17:54:49 | [diff] [blame] | 182 | bool ReadWString(PickleIterator* iter, |
| 183 | std::wstring* result) const WARN_UNUSED_RESULT { |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 184 | return iter->ReadWString(result); |
| 185 | } |
[email protected] | aa63871 | 2013-10-16 17:54:49 | [diff] [blame] | 186 | bool ReadString16(PickleIterator* iter, |
[email protected] | 476dafb | 2013-12-03 00:39:26 | [diff] [blame] | 187 | base::string16* result) const WARN_UNUSED_RESULT { |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 188 | return iter->ReadString16(result); |
| 189 | } |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 190 | // A pointer to the data will be placed in *data, and the length will be |
| 191 | // placed in *length. This buffer will be into the message's buffer so will |
| 192 | // be scoped to the lifetime of the message (or until the message data is |
| 193 | // mutated). |
[email protected] | aa63871 | 2013-10-16 17:54:49 | [diff] [blame] | 194 | bool ReadData(PickleIterator* iter, |
| 195 | const char** data, |
| 196 | int* length) const WARN_UNUSED_RESULT { |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 197 | return iter->ReadData(data, length); |
| 198 | } |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 199 | // A pointer to the data will be placed in *data. The caller specifies the |
| 200 | // number of bytes to read, and ReadBytes will validate this length. The |
| 201 | // returned buffer will be into the message's buffer so will be scoped to the |
| 202 | // lifetime of the message (or until the message data is mutated). |
[email protected] | aa63871 | 2013-10-16 17:54:49 | [diff] [blame] | 203 | bool ReadBytes(PickleIterator* iter, |
| 204 | const char** data, |
| 205 | int length) const WARN_UNUSED_RESULT { |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 206 | return iter->ReadBytes(data, length); |
| 207 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 208 | |
| 209 | // Safer version of ReadInt() checks for the result not being negative. |
| 210 | // Use it for reading the object sizes. |
[email protected] | aa63871 | 2013-10-16 17:54:49 | [diff] [blame] | 211 | bool ReadLength(PickleIterator* iter, |
| 212 | int* result) const WARN_UNUSED_RESULT { |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 213 | return iter->ReadLength(result); |
| 214 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 215 | |
| 216 | // Methods for adding to the payload of the Pickle. These values are |
| 217 | // appended to the end of the Pickle's payload. When reading values from a |
| 218 | // Pickle, it is important to read them in the order in which they were added |
| 219 | // to the Pickle. |
| 220 | bool WriteBool(bool value) { |
| 221 | return WriteInt(value ? 1 : 0); |
| 222 | } |
| 223 | bool WriteInt(int value) { |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 224 | return WritePOD(value); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 225 | } |
[email protected] | c272d08 | 2012-03-23 00:03:10 | [diff] [blame] | 226 | // WARNING: DO NOT USE THIS METHOD IF PICKLES ARE PERSISTED IN ANY WAY. |
| 227 | // It will write whatever a "long" is on this architecture. On 32-bit |
| 228 | // platforms, it is 32 bits. On 64-bit platforms, it is 64 bits. If persisted |
| 229 | // pickles are still around after upgrading to 64-bit, or if they are copied |
| 230 | // between dissimilar systems, YOUR PICKLES WILL HAVE GONE BAD. |
| 231 | bool WriteLongUsingDangerousNonPortableLessPersistableForm(long value) { |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 232 | return WritePOD(value); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 233 | } |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 234 | bool WriteUInt16(uint16 value) { |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 235 | return WritePOD(value); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 236 | } |
[email protected] | 48ce616a | 2008-12-29 18:55:18 | [diff] [blame] | 237 | bool WriteUInt32(uint32 value) { |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 238 | return WritePOD(value); |
[email protected] | 48ce616a | 2008-12-29 18:55:18 | [diff] [blame] | 239 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 240 | bool WriteInt64(int64 value) { |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 241 | return WritePOD(value); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 242 | } |
[email protected] | b7a5d99 | 2009-10-28 04:21:01 | [diff] [blame] | 243 | bool WriteUInt64(uint64 value) { |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 244 | return WritePOD(value); |
[email protected] | b7a5d99 | 2009-10-28 04:21:01 | [diff] [blame] | 245 | } |
[email protected] | b1f61b03 | 2012-11-28 15:40:58 | [diff] [blame] | 246 | bool WriteFloat(float value) { |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 247 | return WritePOD(value); |
[email protected] | b1f61b03 | 2012-11-28 15:40:58 | [diff] [blame] | 248 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 249 | bool WriteString(const std::string& value); |
| 250 | bool WriteWString(const std::wstring& value); |
[email protected] | 476dafb | 2013-12-03 00:39:26 | [diff] [blame] | 251 | bool WriteString16(const base::string16& value); |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 252 | // "Data" is a blob with a length. When you read it out you will be given the |
| 253 | // length. See also WriteBytes. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 254 | bool WriteData(const char* data, int length); |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 255 | // "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] | 256 | // when reading and writing. It is normally used to serialize PoD types of a |
| 257 | // known size. See also WriteData. |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 258 | bool WriteBytes(const void* data, int length); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 259 | |
[email protected] | 032bfc4 | 2013-10-29 22:23:52 | [diff] [blame] | 260 | // Reserves space for upcoming writes when multiple writes will be made and |
| 261 | // their sizes are computed in advance. It can be significantly faster to call |
| 262 | // Reserve() before calling WriteFoo() multiple times. |
| 263 | void Reserve(size_t additional_capacity); |
| 264 | |
[email protected] | c9046af | 2008-08-06 20:35:17 | [diff] [blame] | 265 | // Payload follows after allocation of Header (header size is customizable). |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 266 | struct Header { |
[email protected] | c9046af | 2008-08-06 20:35:17 | [diff] [blame] | 267 | uint32 payload_size; // Specifies the size of the payload. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 268 | }; |
| 269 | |
| 270 | // Returns the header, cast to a user-specified type T. The type T must be a |
| 271 | // subclass of Header and its size must correspond to the header_size passed |
| 272 | // to the Pickle constructor. |
| 273 | template <class T> |
| 274 | T* headerT() { |
[email protected] | 5d2b449 | 2011-03-01 02:48:05 | [diff] [blame] | 275 | DCHECK_EQ(header_size_, sizeof(T)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 276 | return static_cast<T*>(header_); |
| 277 | } |
| 278 | template <class T> |
| 279 | const T* headerT() const { |
[email protected] | 5d2b449 | 2011-03-01 02:48:05 | [diff] [blame] | 280 | DCHECK_EQ(header_size_, sizeof(T)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 281 | return static_cast<const T*>(header_); |
| 282 | } |
| 283 | |
[email protected] | 73d96dc | 2012-03-30 22:35:27 | [diff] [blame] | 284 | // The payload is the pickle data immediately following the header. |
[email protected] | a15016f | 2014-06-02 23:23:49 | [diff] [blame^] | 285 | size_t payload_size() const { |
| 286 | return header_ ? header_->payload_size : 0; |
| 287 | } |
[email protected] | e00a6c0a | 2013-01-18 18:20:57 | [diff] [blame] | 288 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 289 | const char* payload() const { |
| 290 | return reinterpret_cast<const char*>(header_) + header_size_; |
| 291 | } |
| 292 | |
| 293 | // Returns the address of the byte immediately following the currently valid |
| 294 | // header + payload. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 295 | const char* end_of_payload() const { |
[email protected] | d87f8e6f | 2010-11-15 19:31:23 | [diff] [blame] | 296 | // This object may be invalid. |
| 297 | return header_ ? payload() + payload_size() : NULL; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 298 | } |
| 299 | |
[email protected] | e00a6c0a | 2013-01-18 18:20:57 | [diff] [blame] | 300 | protected: |
| 301 | char* mutable_payload() { |
| 302 | return reinterpret_cast<char*>(header_) + header_size_; |
| 303 | } |
| 304 | |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 305 | size_t capacity_after_header() const { |
| 306 | return capacity_after_header_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 307 | } |
| 308 | |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 309 | // Resize the capacity, note that the input value should not include the size |
| 310 | // of the header. |
| 311 | void Resize(size_t new_capacity); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 312 | |
| 313 | // Aligns 'i' by rounding it up to the next multiple of 'alignment' |
[email protected] | c9046af | 2008-08-06 20:35:17 | [diff] [blame] | 314 | static size_t AlignInt(size_t i, int alignment) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 315 | return i + (alignment - (i % alignment)) % alignment; |
| 316 | } |
| 317 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 318 | // Find the end of the pickled data that starts at range_start. Returns NULL |
| 319 | // if the entire Pickle is not found in the given data range. |
| 320 | static const char* FindNext(size_t header_size, |
| 321 | const char* range_start, |
| 322 | const char* range_end); |
| 323 | |
| 324 | // The allocation granularity of the payload. |
| 325 | static const int kPayloadUnit; |
| 326 | |
| 327 | private: |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 328 | friend class PickleIterator; |
| 329 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 330 | Header* header_; |
| 331 | size_t header_size_; // Supports extra data between header and payload. |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 332 | // Allocation size of payload (or -1 if allocation is const). Note: this |
| 333 | // doesn't count the header. |
| 334 | size_t capacity_after_header_; |
| 335 | // The offset at which we will write the next field. Note: this doesn't count |
| 336 | // the header. |
| 337 | size_t write_offset_; |
| 338 | |
| 339 | // Just like WriteBytes, but with a compile-time size, for performance. |
| 340 | template<size_t length> void WriteBytesStatic(const void* data); |
| 341 | |
| 342 | // Writes a POD by copying its bytes. |
| 343 | template <typename T> bool WritePOD(const T& data) { |
| 344 | WriteBytesStatic<sizeof(data)>(&data); |
| 345 | return true; |
| 346 | } |
| 347 | inline void WriteBytesCommon(const void* data, size_t length); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 348 | |
[email protected] | a918f87 | 2010-06-01 14:30:51 | [diff] [blame] | 349 | FRIEND_TEST_ALL_PREFIXES(PickleTest, Resize); |
| 350 | FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNext); |
[email protected] | 137d237 | 2011-01-26 13:02:27 | [diff] [blame] | 351 | FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextWithIncompleteHeader); |
[email protected] | 33a38dd | 2013-11-01 09:06:26 | [diff] [blame] | 352 | FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextOverflow); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 353 | }; |
| 354 | |
| 355 | #endif // BASE_PICKLE_H__ |