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