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