license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 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. |
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 | |
| 10 | #include "base/basictypes.h" |
| 11 | #include "base/logging.h" |
| 12 | #include "testing/gtest/include/gtest/gtest_prod.h" |
| 13 | |
| 14 | // This class provides facilities for basic binary value packing and unpacking. |
| 15 | // |
| 16 | // The Pickle class supports appending primitive values (ints, strings, etc.) |
| 17 | // to a pickle instance. The Pickle instance grows its internal memory buffer |
| 18 | // dynamically to hold the sequence of primitive values. The internal memory |
| 19 | // buffer is exposed as the "data" of the Pickle. This "data" can be passed |
| 20 | // to a Pickle object to initialize it for reading. |
| 21 | // |
| 22 | // When reading from a Pickle object, it is important for the consumer to know |
| 23 | // what value types to read and in what order to read them as the Pickle does |
| 24 | // not keep track of the type of data written to it. |
| 25 | // |
| 26 | // The Pickle's data has a header which contains the size of the Pickle's |
| 27 | // payload. It can optionally support additional space in the header. That |
| 28 | // space is controlled by the header_size parameter passed to the Pickle |
| 29 | // constructor. |
| 30 | // |
| 31 | class Pickle { |
| 32 | public: |
| 33 | ~Pickle(); |
| 34 | |
| 35 | // Initialize a Pickle object using the default header size. |
| 36 | Pickle(); |
| 37 | |
| 38 | // Initialize a Pickle object with the specified header size in bytes, which |
| 39 | // must be greater-than-or-equal-to sizeof(Pickle::Header). The header size |
| 40 | // will be rounded up to ensure that the header size is 32bit-aligned. |
| 41 | explicit Pickle(int header_size); |
| 42 | |
| 43 | // Initializes a Pickle from a const block of data. The data is not copied; |
| 44 | // instead the data is merely referenced by this Pickle. Only const methods |
| 45 | // should be used on the Pickle when initialized this way. The header |
| 46 | // padding size is deduced from the data length. |
| 47 | Pickle(const char* data, int data_len); |
| 48 | |
| 49 | // Initializes a Pickle as a deep copy of another Pickle. |
| 50 | Pickle(const Pickle& other); |
| 51 | |
| 52 | // Performs a deep copy. |
| 53 | Pickle& operator=(const Pickle& other); |
| 54 | |
| 55 | // Returns the size of the Pickle's data. |
| 56 | int size() const { return static_cast<int>(header_size_ + |
| 57 | header_->payload_size); } |
| 58 | |
| 59 | // Returns the data for this Pickle. |
| 60 | const void* data() const { return header_; } |
| 61 | |
| 62 | // Methods for reading the payload of the Pickle. To read from the start of |
| 63 | // the Pickle, initialize *iter to NULL. If successful, these methods return |
| 64 | // true. Otherwise, false is returned to indicate that the result could not |
| 65 | // be extracted. |
| 66 | bool ReadBool(void** iter, bool* result) const; |
| 67 | bool ReadInt(void** iter, int* result) const; |
klink@google.com | d7f6003 | 2008-08-23 08:24:54 +0900 | [diff] [blame] | 68 | bool ReadLong(void** iter, long* result) const; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 69 | bool ReadSize(void** iter, size_t* result) const; |
| 70 | bool ReadInt64(void** iter, int64* result) const; |
| 71 | bool ReadIntPtr(void** iter, intptr_t* result) const; |
| 72 | bool ReadString(void** iter, std::string* result) const; |
| 73 | bool ReadWString(void** iter, std::wstring* result) const; |
| 74 | bool ReadData(void** iter, const char** data, int* length) const; |
| 75 | bool ReadBytes(void** iter, const char** data, int length) const; |
| 76 | |
| 77 | // Safer version of ReadInt() checks for the result not being negative. |
| 78 | // Use it for reading the object sizes. |
deanm@google.com | 461aaed | 2008-08-11 20:00:13 +0900 | [diff] [blame] | 79 | bool ReadLength(void** iter, int* result) const; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 80 | |
| 81 | // Methods for adding to the payload of the Pickle. These values are |
| 82 | // appended to the end of the Pickle's payload. When reading values from a |
| 83 | // Pickle, it is important to read them in the order in which they were added |
| 84 | // to the Pickle. |
| 85 | bool WriteBool(bool value) { |
| 86 | return WriteInt(value ? 1 : 0); |
| 87 | } |
| 88 | bool WriteInt(int value) { |
| 89 | return WriteBytes(&value, sizeof(value)); |
| 90 | } |
klink@google.com | d7f6003 | 2008-08-23 08:24:54 +0900 | [diff] [blame] | 91 | bool WriteLong(long value) { |
| 92 | return WriteBytes(&value, sizeof(value)); |
| 93 | } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 94 | bool WriteSize(size_t value) { |
| 95 | return WriteBytes(&value, sizeof(value)); |
| 96 | } |
| 97 | bool WriteInt64(int64 value) { |
| 98 | return WriteBytes(&value, sizeof(value)); |
| 99 | } |
| 100 | bool WriteIntPtr(intptr_t value) { |
| 101 | return WriteBytes(&value, sizeof(value)); |
| 102 | } |
| 103 | bool WriteString(const std::string& value); |
| 104 | bool WriteWString(const std::wstring& value); |
| 105 | bool WriteData(const char* data, int length); |
| 106 | bool WriteBytes(const void* data, int data_len); |
| 107 | |
| 108 | // Same as WriteData, but allows the caller to write directly into the |
| 109 | // Pickle. This saves a copy in cases where the data is not already |
| 110 | // available in a buffer. The caller should take care to not write more |
| 111 | // than the length it declares it will. Use ReadData to get the data. |
| 112 | // Returns NULL on failure. |
| 113 | // |
| 114 | // The returned pointer will only be valid until the next write operation |
| 115 | // on this Pickle. |
| 116 | char* BeginWriteData(int length); |
| 117 | |
| 118 | // For Pickles which contain variable length buffers (e.g. those created |
| 119 | // with BeginWriteData), the Pickle can |
| 120 | // be 'trimmed' if the amount of data required is less than originally |
| 121 | // requested. For example, you may have created a buffer with 10K of data, |
| 122 | // but decided to only fill 10 bytes of that data. Use this function |
| 123 | // to trim the buffer so that we don't send 9990 bytes of unused data. |
| 124 | // You cannot increase the size of the variable buffer; only shrink it. |
| 125 | // This function assumes that the length of the variable buffer has |
| 126 | // not been changed. |
| 127 | void TrimWriteData(int length); |
| 128 | |
maruel@google.com | 825f879 | 2008-08-07 05:35:17 +0900 | [diff] [blame] | 129 | // Payload follows after allocation of Header (header size is customizable). |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 130 | struct Header { |
maruel@google.com | 825f879 | 2008-08-07 05:35:17 +0900 | [diff] [blame] | 131 | uint32 payload_size; // Specifies the size of the payload. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | // Returns the header, cast to a user-specified type T. The type T must be a |
| 135 | // subclass of Header and its size must correspond to the header_size passed |
| 136 | // to the Pickle constructor. |
| 137 | template <class T> |
| 138 | T* headerT() { |
| 139 | DCHECK(sizeof(T) == header_size_); |
| 140 | return static_cast<T*>(header_); |
| 141 | } |
| 142 | template <class T> |
| 143 | const T* headerT() const { |
| 144 | DCHECK(sizeof(T) == header_size_); |
| 145 | return static_cast<const T*>(header_); |
| 146 | } |
| 147 | |
| 148 | // Returns true if the given iterator could point to data with the given |
| 149 | // length. If there is no room for the given data before the end of the |
| 150 | // payload, returns false. |
| 151 | bool IteratorHasRoomFor(const void* iter, int len) const { |
| 152 | if ((len < 0) || (iter < header_) || iter > end_of_payload()) |
| 153 | return false; |
| 154 | const char* end_of_region = reinterpret_cast<const char*>(iter) + len; |
| 155 | // Watch out for overflow in pointer calculation, which wraps. |
| 156 | return (iter <= end_of_region) && (end_of_region <= end_of_payload()); |
| 157 | } |
| 158 | |
| 159 | protected: |
| 160 | size_t payload_size() const { return header_->payload_size; } |
| 161 | |
| 162 | char* payload() { |
| 163 | return reinterpret_cast<char*>(header_) + header_size_; |
| 164 | } |
| 165 | const char* payload() const { |
| 166 | return reinterpret_cast<const char*>(header_) + header_size_; |
| 167 | } |
| 168 | |
| 169 | // Returns the address of the byte immediately following the currently valid |
| 170 | // header + payload. |
| 171 | char* end_of_payload() { |
| 172 | return payload() + payload_size(); |
| 173 | } |
| 174 | const char* end_of_payload() const { |
| 175 | return payload() + payload_size(); |
| 176 | } |
| 177 | |
| 178 | size_t capacity() const { |
| 179 | return capacity_; |
| 180 | } |
| 181 | |
| 182 | // Resizes the buffer for use when writing the specified amount of data. The |
| 183 | // location that the data should be written at is returned, or NULL if there |
| 184 | // was an error. Call EndWrite with the returned offset and the given length |
| 185 | // to pad out for the next write. |
| 186 | char* BeginWrite(size_t length); |
| 187 | |
| 188 | // Completes the write operation by padding the data with NULL bytes until it |
| 189 | // is padded. Should be paired with BeginWrite, but it does not necessarily |
| 190 | // have to be called after the data is written. |
| 191 | void EndWrite(char* dest, int length); |
| 192 | |
| 193 | // Resize the capacity, note that the input value should include the size of |
| 194 | // the header: new_capacity = sizeof(Header) + desired_payload_capacity. |
| 195 | // A realloc() failure will cause a Resize failure... and caller should check |
| 196 | // the return result for true (i.e., successful resizing). |
| 197 | bool Resize(size_t new_capacity); |
| 198 | |
| 199 | // 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] | 200 | static size_t AlignInt(size_t i, int alignment) { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 201 | return i + (alignment - (i % alignment)) % alignment; |
| 202 | } |
| 203 | |
| 204 | // Moves the iterator by the given number of bytes, making sure it is aligned. |
| 205 | // Pointer (iterator) is NOT aligned, but the change in the pointer |
| 206 | // is guaranteed to be a multiple of sizeof(uint32). |
maruel@google.com | 825f879 | 2008-08-07 05:35:17 +0900 | [diff] [blame] | 207 | static void UpdateIter(void** iter, int bytes) { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 208 | *iter = static_cast<char*>(*iter) + AlignInt(bytes, sizeof(uint32)); |
| 209 | } |
| 210 | |
| 211 | // Find the end of the pickled data that starts at range_start. Returns NULL |
| 212 | // if the entire Pickle is not found in the given data range. |
| 213 | static const char* FindNext(size_t header_size, |
| 214 | const char* range_start, |
| 215 | const char* range_end); |
| 216 | |
| 217 | // The allocation granularity of the payload. |
| 218 | static const int kPayloadUnit; |
| 219 | |
| 220 | private: |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 221 | Header* header_; |
| 222 | size_t header_size_; // Supports extra data between header and payload. |
| 223 | // Allocation size of payload (or -1 if allocation is const). |
| 224 | size_t capacity_; |
| 225 | size_t variable_buffer_offset_; // IF non-zero, then offset to a buffer. |
| 226 | |
| 227 | FRIEND_TEST(PickleTest, Resize); |
| 228 | FRIEND_TEST(PickleTest, FindNext); |
| 229 | FRIEND_TEST(PickleTest, IteratorHasRoom); |
| 230 | }; |
| 231 | |
| 232 | #endif // BASE_PICKLE_H__ |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame] | 233 | |