jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [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 | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 4 | |
| 5 | #ifndef BASE_PICKLE_H__ |
| 6 | #define BASE_PICKLE_H__ |
| 7 | |
| 8 | #include <string> |
| 9 | |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 10 | #include "base/base_export.h" |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 11 | #include "base/basictypes.h" |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 12 | #include "base/compiler_specific.h" |
phajdan.jr@chromium.org | dd43b73 | 2010-06-01 23:30:51 +0900 | [diff] [blame] | 13 | #include "base/gtest_prod_util.h" |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 14 | #include "base/logging.h" |
avi@chromium.org | 67d593d | 2013-06-11 04:06:57 +0900 | [diff] [blame] | 15 | #include "base/strings/string16.h" |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 16 | |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [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: |
halyavin@google.com | 0398c72 | 2014-06-03 08:23:49 +0900 | [diff] [blame] | 23 | PickleIterator() : payload_(NULL), read_index_(0), end_index_(0) {} |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [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 |
halyavin@google.com | 0398c72 | 2014-06-03 08:23:49 +0900 | [diff] [blame] | 29 | // result could not be extracted. It is not possible to read from iterator |
| 30 | // after that. |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [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; |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [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; |
rbyers@chromium.org | a1f0b98 | 2012-11-29 00:40:58 +0900 | [diff] [blame] | 38 | bool ReadFloat(float* result) WARN_UNUSED_RESULT; |
mostynb@opera.com | f0b7853 | 2014-07-15 07:50:32 +0900 | [diff] [blame] | 39 | bool ReadDouble(double* result) WARN_UNUSED_RESULT; |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 40 | bool ReadString(std::string* result) WARN_UNUSED_RESULT; |
| 41 | bool ReadWString(std::wstring* result) WARN_UNUSED_RESULT; |
brettw@chromium.org | 5b04085 | 2013-12-03 09:39:26 +0900 | [diff] [blame] | 42 | bool ReadString16(base::string16* result) WARN_UNUSED_RESULT; |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 43 | 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> |
halyavin@google.com | 0398c72 | 2014-06-03 08:23:49 +0900 | [diff] [blame] | 66 | 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); |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 71 | |
| 72 | // Get read pointer for Type and advance read pointer. |
| 73 | template<typename Type> |
halyavin@google.com | 0398c72 | 2014-06-03 08:23:49 +0900 | [diff] [blame] | 74 | const char* GetReadPointerAndAdvance(); |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 75 | |
| 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. |
halyavin@google.com | 0398c72 | 2014-06-03 08:23:49 +0900 | [diff] [blame] | 82 | const char* GetReadPointerAndAdvance(int num_elements, |
| 83 | size_t size_element); |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 84 | |
halyavin@google.com | 0398c72 | 2014-06-03 08:23:49 +0900 | [diff] [blame] | 85 | 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. |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 88 | |
| 89 | FRIEND_TEST_ALL_PREFIXES(PickleTest, GetReadPointerAndAdvance); |
| 90 | }; |
| 91 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 92 | // 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 | // |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 109 | class BASE_EXPORT Pickle { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 110 | public: |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 111 | // 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. |
bbudge@chromium.org | ab4c6bc | 2013-11-05 07:28:12 +0900 | [diff] [blame] | 123 | Pickle(const char* data, int data_len); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 124 | |
| 125 | // Initializes a Pickle as a deep copy of another Pickle. |
| 126 | Pickle(const Pickle& other); |
| 127 | |
jar@chromium.org | 4bf312a | 2011-09-25 12:08:13 +0900 | [diff] [blame] | 128 | // 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. |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 133 | virtual ~Pickle(); |
| 134 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 135 | // Performs a deep copy. |
| 136 | Pickle& operator=(const Pickle& other); |
| 137 | |
| 138 | // Returns the size of the Pickle's data. |
mrossetti@chromium.org | 84db36d | 2011-01-29 04:59:11 +0900 | [diff] [blame] | 139 | size_t size() const { return header_size_ + header_->payload_size; } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 140 | |
| 141 | // Returns the data for this Pickle. |
| 142 | const void* data() const { return header_; } |
| 143 | |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 144 | // For compatibility, these older style read methods pass through to the |
| 145 | // PickleIterator methods. |
| 146 | // TODO(jbates) Remove these methods. |
tsepez@chromium.org | d74e7ad | 2013-10-17 02:54:49 +0900 | [diff] [blame] | 147 | bool ReadBool(PickleIterator* iter, |
| 148 | bool* result) const WARN_UNUSED_RESULT { |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 149 | return iter->ReadBool(result); |
| 150 | } |
tsepez@chromium.org | d74e7ad | 2013-10-17 02:54:49 +0900 | [diff] [blame] | 151 | bool ReadInt(PickleIterator* iter, |
| 152 | int* result) const WARN_UNUSED_RESULT { |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 153 | return iter->ReadInt(result); |
| 154 | } |
tsepez@chromium.org | d74e7ad | 2013-10-17 02:54:49 +0900 | [diff] [blame] | 155 | bool ReadLong(PickleIterator* iter, |
| 156 | long* result) const WARN_UNUSED_RESULT { |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 157 | return iter->ReadLong(result); |
| 158 | } |
tsepez@chromium.org | d74e7ad | 2013-10-17 02:54:49 +0900 | [diff] [blame] | 159 | bool ReadUInt16(PickleIterator* iter, |
| 160 | uint16* result) const WARN_UNUSED_RESULT { |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 161 | return iter->ReadUInt16(result); |
| 162 | } |
tsepez@chromium.org | d74e7ad | 2013-10-17 02:54:49 +0900 | [diff] [blame] | 163 | bool ReadUInt32(PickleIterator* iter, |
| 164 | uint32* result) const WARN_UNUSED_RESULT { |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 165 | return iter->ReadUInt32(result); |
| 166 | } |
tsepez@chromium.org | d74e7ad | 2013-10-17 02:54:49 +0900 | [diff] [blame] | 167 | bool ReadInt64(PickleIterator* iter, |
| 168 | int64* result) const WARN_UNUSED_RESULT { |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 169 | return iter->ReadInt64(result); |
| 170 | } |
tsepez@chromium.org | d74e7ad | 2013-10-17 02:54:49 +0900 | [diff] [blame] | 171 | bool ReadUInt64(PickleIterator* iter, |
| 172 | uint64* result) const WARN_UNUSED_RESULT { |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 173 | return iter->ReadUInt64(result); |
| 174 | } |
tsepez@chromium.org | d74e7ad | 2013-10-17 02:54:49 +0900 | [diff] [blame] | 175 | bool ReadFloat(PickleIterator* iter, |
| 176 | float* result) const WARN_UNUSED_RESULT { |
rbyers@chromium.org | a1f0b98 | 2012-11-29 00:40:58 +0900 | [diff] [blame] | 177 | return iter->ReadFloat(result); |
| 178 | } |
mostynb@opera.com | f0b7853 | 2014-07-15 07:50:32 +0900 | [diff] [blame] | 179 | bool ReadDouble(PickleIterator* iter, |
| 180 | double* result) const WARN_UNUSED_RESULT { |
| 181 | return iter->ReadDouble(result); |
| 182 | } |
tsepez@chromium.org | d74e7ad | 2013-10-17 02:54:49 +0900 | [diff] [blame] | 183 | bool ReadString(PickleIterator* iter, |
| 184 | std::string* result) const WARN_UNUSED_RESULT { |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 185 | return iter->ReadString(result); |
| 186 | } |
tsepez@chromium.org | d74e7ad | 2013-10-17 02:54:49 +0900 | [diff] [blame] | 187 | bool ReadWString(PickleIterator* iter, |
| 188 | std::wstring* result) const WARN_UNUSED_RESULT { |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 189 | return iter->ReadWString(result); |
| 190 | } |
tsepez@chromium.org | d74e7ad | 2013-10-17 02:54:49 +0900 | [diff] [blame] | 191 | bool ReadString16(PickleIterator* iter, |
brettw@chromium.org | 5b04085 | 2013-12-03 09:39:26 +0900 | [diff] [blame] | 192 | base::string16* result) const WARN_UNUSED_RESULT { |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 193 | return iter->ReadString16(result); |
| 194 | } |
brettw@chromium.org | f48910a | 2012-06-29 09:05:04 +0900 | [diff] [blame] | 195 | // 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). |
tsepez@chromium.org | d74e7ad | 2013-10-17 02:54:49 +0900 | [diff] [blame] | 199 | bool ReadData(PickleIterator* iter, |
| 200 | const char** data, |
| 201 | int* length) const WARN_UNUSED_RESULT { |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 202 | return iter->ReadData(data, length); |
| 203 | } |
brettw@chromium.org | f48910a | 2012-06-29 09:05:04 +0900 | [diff] [blame] | 204 | // 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). |
tsepez@chromium.org | d74e7ad | 2013-10-17 02:54:49 +0900 | [diff] [blame] | 208 | bool ReadBytes(PickleIterator* iter, |
| 209 | const char** data, |
| 210 | int length) const WARN_UNUSED_RESULT { |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 211 | return iter->ReadBytes(data, length); |
| 212 | } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 213 | |
| 214 | // Safer version of ReadInt() checks for the result not being negative. |
| 215 | // Use it for reading the object sizes. |
tsepez@chromium.org | d74e7ad | 2013-10-17 02:54:49 +0900 | [diff] [blame] | 216 | bool ReadLength(PickleIterator* iter, |
| 217 | int* result) const WARN_UNUSED_RESULT { |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 218 | return iter->ReadLength(result); |
| 219 | } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 220 | |
| 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) { |
piman@chromium.org | 5d3eee2 | 2013-10-31 13:03:02 +0900 | [diff] [blame] | 229 | return WritePOD(value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 230 | } |
mdm@chromium.org | 69bca9d | 2012-03-23 09:03:10 +0900 | [diff] [blame] | 231 | // 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) { |
piman@chromium.org | 5d3eee2 | 2013-10-31 13:03:02 +0900 | [diff] [blame] | 237 | return WritePOD(value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 238 | } |
bryner@chromium.org | c85d0fd | 2011-02-23 04:47:19 +0900 | [diff] [blame] | 239 | bool WriteUInt16(uint16 value) { |
piman@chromium.org | 5d3eee2 | 2013-10-31 13:03:02 +0900 | [diff] [blame] | 240 | return WritePOD(value); |
bryner@chromium.org | c85d0fd | 2011-02-23 04:47:19 +0900 | [diff] [blame] | 241 | } |
jeremy@chromium.org | 62ace90 | 2008-12-30 03:55:18 +0900 | [diff] [blame] | 242 | bool WriteUInt32(uint32 value) { |
piman@chromium.org | 5d3eee2 | 2013-10-31 13:03:02 +0900 | [diff] [blame] | 243 | return WritePOD(value); |
jeremy@chromium.org | 62ace90 | 2008-12-30 03:55:18 +0900 | [diff] [blame] | 244 | } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 245 | bool WriteInt64(int64 value) { |
piman@chromium.org | 5d3eee2 | 2013-10-31 13:03:02 +0900 | [diff] [blame] | 246 | return WritePOD(value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 247 | } |
thestig@chromium.org | eb9afb4 | 2009-10-28 13:21:01 +0900 | [diff] [blame] | 248 | bool WriteUInt64(uint64 value) { |
piman@chromium.org | 5d3eee2 | 2013-10-31 13:03:02 +0900 | [diff] [blame] | 249 | return WritePOD(value); |
thestig@chromium.org | eb9afb4 | 2009-10-28 13:21:01 +0900 | [diff] [blame] | 250 | } |
rbyers@chromium.org | a1f0b98 | 2012-11-29 00:40:58 +0900 | [diff] [blame] | 251 | bool WriteFloat(float value) { |
piman@chromium.org | 5d3eee2 | 2013-10-31 13:03:02 +0900 | [diff] [blame] | 252 | return WritePOD(value); |
rbyers@chromium.org | a1f0b98 | 2012-11-29 00:40:58 +0900 | [diff] [blame] | 253 | } |
mostynb@opera.com | f0b7853 | 2014-07-15 07:50:32 +0900 | [diff] [blame] | 254 | bool WriteDouble(double value) { |
| 255 | return WritePOD(value); |
| 256 | } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 257 | bool WriteString(const std::string& value); |
| 258 | bool WriteWString(const std::wstring& value); |
brettw@chromium.org | 5b04085 | 2013-12-03 09:39:26 +0900 | [diff] [blame] | 259 | bool WriteString16(const base::string16& value); |
brettw@chromium.org | f48910a | 2012-06-29 09:05:04 +0900 | [diff] [blame] | 260 | // "Data" is a blob with a length. When you read it out you will be given the |
| 261 | // length. See also WriteBytes. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 262 | bool WriteData(const char* data, int length); |
piman@chromium.org | 5d3eee2 | 2013-10-31 13:03:02 +0900 | [diff] [blame] | 263 | // "Bytes" is a blob with no length. The caller must specify the length both |
brettw@chromium.org | f48910a | 2012-06-29 09:05:04 +0900 | [diff] [blame] | 264 | // when reading and writing. It is normally used to serialize PoD types of a |
| 265 | // known size. See also WriteData. |
piman@chromium.org | 5d3eee2 | 2013-10-31 13:03:02 +0900 | [diff] [blame] | 266 | bool WriteBytes(const void* data, int length); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 267 | |
danakj@chromium.org | 888b12a | 2013-10-30 07:23:52 +0900 | [diff] [blame] | 268 | // 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 | |
maruel@google.com | 825f879 | 2008-08-07 05:35:17 +0900 | [diff] [blame] | 273 | // Payload follows after allocation of Header (header size is customizable). |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 274 | struct Header { |
maruel@google.com | 825f879 | 2008-08-07 05:35:17 +0900 | [diff] [blame] | 275 | uint32 payload_size; // Specifies the size of the payload. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 276 | }; |
| 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() { |
mhm@chromium.org | 73bfc43 | 2011-03-01 11:48:05 +0900 | [diff] [blame] | 283 | DCHECK_EQ(header_size_, sizeof(T)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 284 | return static_cast<T*>(header_); |
| 285 | } |
| 286 | template <class T> |
| 287 | const T* headerT() const { |
mhm@chromium.org | 73bfc43 | 2011-03-01 11:48:05 +0900 | [diff] [blame] | 288 | DCHECK_EQ(header_size_, sizeof(T)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 289 | return static_cast<const T*>(header_); |
| 290 | } |
| 291 | |
brettw@chromium.org | 6471c93 | 2012-03-31 07:35:27 +0900 | [diff] [blame] | 292 | // The payload is the pickle data immediately following the header. |
halyavin@google.com | 0398c72 | 2014-06-03 08:23:49 +0900 | [diff] [blame] | 293 | size_t payload_size() const { |
| 294 | return header_ ? header_->payload_size : 0; |
| 295 | } |
sergeyu@chromium.org | 2cbb1e2 | 2013-01-19 03:20:57 +0900 | [diff] [blame] | 296 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 297 | 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.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 303 | const char* end_of_payload() const { |
rvargas@google.com | 8d9b2b9 | 2010-11-16 04:31:23 +0900 | [diff] [blame] | 304 | // This object may be invalid. |
| 305 | return header_ ? payload() + payload_size() : NULL; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 306 | } |
| 307 | |
sergeyu@chromium.org | 2cbb1e2 | 2013-01-19 03:20:57 +0900 | [diff] [blame] | 308 | protected: |
| 309 | char* mutable_payload() { |
| 310 | return reinterpret_cast<char*>(header_) + header_size_; |
| 311 | } |
| 312 | |
piman@chromium.org | 5d3eee2 | 2013-10-31 13:03:02 +0900 | [diff] [blame] | 313 | size_t capacity_after_header() const { |
| 314 | return capacity_after_header_; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 315 | } |
| 316 | |
piman@chromium.org | 5d3eee2 | 2013-10-31 13:03:02 +0900 | [diff] [blame] | 317 | // 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.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 320 | |
| 321 | // Aligns 'i' by rounding it up to the next multiple of 'alignment' |
maruel@google.com | 825f879 | 2008-08-07 05:35:17 +0900 | [diff] [blame] | 322 | static size_t AlignInt(size_t i, int alignment) { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 323 | return i + (alignment - (i % alignment)) % alignment; |
| 324 | } |
| 325 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 326 | // 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: |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 336 | friend class PickleIterator; |
| 337 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 338 | Header* header_; |
| 339 | size_t header_size_; // Supports extra data between header and payload. |
piman@chromium.org | 5d3eee2 | 2013-10-31 13:03:02 +0900 | [diff] [blame] | 340 | // 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. |
hans@chromium.org | 048af68 | 2014-06-11 09:34:38 +0900 | [diff] [blame] | 348 | template<size_t length> void BASE_EXPORT WriteBytesStatic(const void* data); |
piman@chromium.org | 5d3eee2 | 2013-10-31 13:03:02 +0900 | [diff] [blame] | 349 | |
| 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.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 356 | |
phajdan.jr@chromium.org | dd43b73 | 2010-06-01 23:30:51 +0900 | [diff] [blame] | 357 | FRIEND_TEST_ALL_PREFIXES(PickleTest, Resize); |
| 358 | FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNext); |
glider@chromium.org | 8b725fa | 2011-01-26 22:02:27 +0900 | [diff] [blame] | 359 | FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextWithIncompleteHeader); |
halyavin@google.com | 893e5bb | 2013-11-01 18:06:26 +0900 | [diff] [blame] | 360 | FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextOverflow); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 361 | }; |
| 362 | |
| 363 | #endif // BASE_PICKLE_H__ |