1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
13 #include "base/base_export.h"
14 #include "base/check_op.h"
15 #include "base/containers/span.h"
16 #include "base/gtest_prod_util.h"
17 #include "base/memory/raw_ptr_exclusion.h"
18 #include "base/memory/ref_counted.h"
19 #include "base/strings/string_piece.h"
25 // PickleIterator reads data from a Pickle. The Pickle object must remain valid
26 // while the PickleIterator object is in use.
27 class BASE_EXPORT PickleIterator {
29 PickleIterator() : payload_(nullptr), read_index_(0), end_index_(0) {}
30 explicit PickleIterator(const Pickle& pickle);
32 // Methods for reading the payload of the Pickle. To read from the start of
33 // the Pickle, create a PickleIterator from a Pickle. If successful, these
34 // methods return true. Otherwise, false is returned to indicate that the
35 // result could not be extracted. It is not possible to read from the iterator
37 [[nodiscard]] bool ReadBool(bool* result);
38 [[nodiscard]] bool ReadInt(int* result);
39 [[nodiscard]] bool ReadLong(long* result);
40 [[nodiscard]] bool ReadUInt16(uint16_t* result);
41 [[nodiscard]] bool ReadUInt32(uint32_t* result);
42 [[nodiscard]] bool ReadInt64(int64_t* result);
43 [[nodiscard]] bool ReadUInt64(uint64_t* result);
44 [[nodiscard]] bool ReadFloat(float* result);
45 [[nodiscard]] bool ReadDouble(double* result);
46 [[nodiscard]] bool ReadString(std::string* result);
47 // The StringPiece data will only be valid for the lifetime of the message.
48 [[nodiscard]] bool ReadStringPiece(StringPiece* result);
49 [[nodiscard]] bool ReadString16(std::u16string* result);
50 // The StringPiece16 data will only be valid for the lifetime of the message.
51 [[nodiscard]] bool ReadStringPiece16(StringPiece16* result);
53 // A pointer to the data will be placed in |*data|, and the length will be
54 // placed in |*length|. The pointer placed into |*data| points into the
55 // message's buffer so it will be scoped to the lifetime of the message (or
56 // until the message data is mutated). Do not keep the pointer around!
57 [[nodiscard]] bool ReadData(const char** data, size_t* length);
59 // Similar, but using base::span for convenience.
60 [[nodiscard]] bool ReadData(base::span<const uint8_t>* data);
62 // A pointer to the data will be placed in |*data|. The caller specifies the
63 // number of bytes to read, and ReadBytes will validate this length. The
64 // pointer placed into |*data| points into the message's buffer so it will be
65 // scoped to the lifetime of the message (or until the message data is
66 // mutated). Do not keep the pointer around!
67 [[nodiscard]] bool ReadBytes(const char** data, size_t length);
69 // A version of ReadInt() that checks for the result not being negative. Use
70 // it for reading the object sizes.
71 [[nodiscard]] bool ReadLength(size_t* result) {
73 if (!ReadInt(&result_int) || result_int < 0)
75 *result = static_cast<size_t>(result_int);
79 // Skips bytes in the read buffer and returns true if there are at least
80 // num_bytes available. Otherwise, does nothing and returns false.
81 [[nodiscard]] bool SkipBytes(size_t num_bytes) {
82 return !!GetReadPointerAndAdvance(num_bytes);
85 bool ReachedEnd() const { return read_index_ == end_index_; }
88 // Read Type from Pickle.
89 template <typename Type>
90 bool ReadBuiltinType(Type* result);
92 // Advance read_index_ but do not allow it to exceed end_index_.
93 // Keeps read_index_ aligned.
94 void Advance(size_t size);
96 // Get read pointer for Type and advance read pointer.
97 template<typename Type>
98 const char* GetReadPointerAndAdvance();
100 // Get read pointer for |num_bytes| and advance read pointer. This method
101 // checks num_bytes for wrapping.
102 const char* GetReadPointerAndAdvance(size_t num_bytes);
104 // Get read pointer for (num_elements * size_element) bytes and advance read
105 // pointer. This method checks for overflow and wrapping.
106 const char* GetReadPointerAndAdvance(size_t num_elements,
107 size_t size_element);
109 const char* payload_; // Start of our pickle's payload.
110 size_t read_index_; // Offset of the next readable byte in payload.
111 size_t end_index_; // Payload size.
113 FRIEND_TEST_ALL_PREFIXES(PickleTest, GetReadPointerAndAdvance);
116 // This class provides facilities for basic binary value packing and unpacking.
118 // The Pickle class supports appending primitive values (ints, strings, etc.)
119 // to a pickle instance. The Pickle instance grows its internal memory buffer
120 // dynamically to hold the sequence of primitive values. The internal memory
121 // buffer is exposed as the "data" of the Pickle. This "data" can be passed
122 // to a Pickle object to initialize it for reading.
124 // When reading from a Pickle object, it is important for the consumer to know
125 // what value types to read and in what order to read them as the Pickle does
126 // not keep track of the type of data written to it.
128 // The Pickle's data has a header which contains the size of the Pickle's
129 // payload. It can optionally support additional space in the header. That
130 // space is controlled by the header_size parameter passed to the Pickle
133 class BASE_EXPORT Pickle {
135 // Auxiliary data attached to a Pickle. Pickle must be subclassed along with
136 // this interface in order to provide a concrete implementation of support
137 // for attachments. The base Pickle implementation does not accept
139 class BASE_EXPORT Attachment : public RefCountedThreadSafe<Attachment> {
142 Attachment(const Attachment&) = delete;
143 Attachment& operator=(const Attachment&) = delete;
146 friend class RefCountedThreadSafe<Attachment>;
147 virtual ~Attachment();
150 // Initialize a Pickle object using the default header size.
153 // Initialize a Pickle object with the specified header size in bytes, which
154 // must be greater-than-or-equal-to sizeof(Pickle::Header). The header size
155 // will be rounded up to ensure that the header size is 32bit-aligned.
156 explicit Pickle(size_t header_size);
158 // Initializes a Pickle from a const block of data. The data is not copied;
159 // instead the data is merely referenced by this Pickle. Only const methods
160 // should be used on the Pickle when initialized this way. The header
161 // padding size is deduced from the data length.
162 Pickle(const char* data, size_t data_len);
164 // Initializes a Pickle as a deep copy of another Pickle.
165 Pickle(const Pickle& other);
167 // Note: There are no virtual methods in this class. This destructor is
168 // virtual as an element of defensive coding. Other classes have derived from
169 // this class, and there is a *chance* that they will cast into this base
170 // class before destruction. At least one such class does have a virtual
171 // destructor, suggesting at least some need to call more derived destructors.
174 // Performs a deep copy.
175 Pickle& operator=(const Pickle& other);
177 // Returns the number of bytes written in the Pickle, including the header.
178 size_t size() const {
179 return header_ ? header_size_ + header_->payload_size : 0;
182 // Returns the data for this Pickle.
183 const void* data() const { return header_; }
185 // Returns the effective memory capacity of this Pickle, that is, the total
186 // number of bytes currently dynamically allocated or 0 in the case of a
187 // read-only Pickle. This should be used only for diagnostic / profiling
189 size_t GetTotalAllocatedSize() const;
191 // Methods for adding to the payload of the Pickle. These values are
192 // appended to the end of the Pickle's payload. When reading values from a
193 // Pickle, it is important to read them in the order in which they were added
196 void WriteBool(bool value) { WriteInt(value ? 1 : 0); }
197 void WriteInt(int value) { WritePOD(value); }
198 void WriteLong(long value) {
199 // Always write long as a 64-bit value to ensure compatibility between
200 // 32-bit and 64-bit processes.
201 WritePOD(static_cast<int64_t>(value));
203 void WriteUInt16(uint16_t value) { WritePOD(value); }
204 void WriteUInt32(uint32_t value) { WritePOD(value); }
205 void WriteInt64(int64_t value) { WritePOD(value); }
206 void WriteUInt64(uint64_t value) { WritePOD(value); }
207 void WriteFloat(float value) { WritePOD(value); }
208 void WriteDouble(double value) { WritePOD(value); }
209 void WriteString(const StringPiece& value);
210 void WriteString16(const StringPiece16& value);
211 // "Data" is a blob with a length. When you read it out you will be given the
212 // length. See also WriteBytes.
213 void WriteData(const char* data, size_t length);
214 // "Bytes" is a blob with no length. The caller must specify the length both
215 // when reading and writing. It is normally used to serialize PoD types of a
216 // known size. See also WriteData.
217 void WriteBytes(const void* data, size_t length);
219 // WriteAttachment appends |attachment| to the pickle. It returns
220 // false iff the set is full or if the Pickle implementation does not support
222 virtual bool WriteAttachment(scoped_refptr<Attachment> attachment);
224 // ReadAttachment parses an attachment given the parsing state |iter| and
225 // writes it to |*attachment|. It returns true on success.
226 virtual bool ReadAttachment(base::PickleIterator* iter,
227 scoped_refptr<Attachment>* attachment) const;
229 // Indicates whether the pickle has any attachments.
230 virtual bool HasAttachments() const;
232 // Reserves space for upcoming writes when multiple writes will be made and
233 // their sizes are computed in advance. It can be significantly faster to call
234 // Reserve() before calling WriteFoo() multiple times.
235 void Reserve(size_t additional_capacity);
237 // Payload follows after allocation of Header (header size is customizable).
239 uint32_t payload_size; // Specifies the size of the payload.
242 // Returns the header, cast to a user-specified type T. The type T must be a
243 // subclass of Header and its size must correspond to the header_size passed
244 // to the Pickle constructor.
247 DCHECK_EQ(header_size_, sizeof(T));
248 return static_cast<T*>(header_);
251 const T* headerT() const {
252 DCHECK_EQ(header_size_, sizeof(T));
253 return static_cast<const T*>(header_);
256 // The payload is the pickle data immediately following the header.
257 size_t payload_size() const {
258 return header_ ? header_->payload_size : 0;
261 const char* payload() const {
262 return reinterpret_cast<const char*>(header_) + header_size_;
265 // Returns the address of the byte immediately following the currently valid
267 const char* end_of_payload() const {
268 // This object may be invalid.
269 return header_ ? payload() + payload_size() : NULL;
273 // Returns size of the header, which can have default value, set by user or
274 // calculated by passed raw data.
275 size_t header_size() const { return header_size_; }
277 char* mutable_payload() {
278 return reinterpret_cast<char*>(header_) + header_size_;
281 size_t capacity_after_header() const {
282 return capacity_after_header_;
285 // Resize the capacity, note that the input value should not include the size
287 void Resize(size_t new_capacity);
289 // Claims |num_bytes| bytes of payload. This is similar to Reserve() in that
290 // it may grow the capacity, but it also advances the write offset of the
291 // pickle by |num_bytes|. Claimed memory, including padding, is zeroed.
293 // Returns the address of the first byte claimed.
294 void* ClaimBytes(size_t num_bytes);
296 // Find the end of the pickled data that starts at range_start. Returns NULL
297 // if the entire Pickle is not found in the given data range.
298 static const char* FindNext(size_t header_size,
299 const char* range_start,
300 const char* range_end);
302 // Parse pickle header and return total size of the pickle. Data range
303 // doesn't need to contain entire pickle.
304 // Returns true if pickle header was found and parsed. Callers must check
305 // returned |pickle_size| for sanity (against maximum message size, etc).
306 // NOTE: when function successfully parses a header, but encounters an
307 // overflow during pickle size calculation, it sets |pickle_size| to the
308 // maximum size_t value and returns true.
309 static bool PeekNext(size_t header_size,
310 const char* range_start,
311 const char* range_end,
312 size_t* pickle_size);
314 // The allocation granularity of the payload.
315 static const size_t kPayloadUnit;
318 friend class PickleIterator;
320 // `header_` is not a raw_ptr<...> for performance reasons (based on analysis
321 // of sampling profiler data).
322 RAW_PTR_EXCLUSION Header* header_;
323 size_t header_size_; // Supports extra data between header and payload.
324 // Allocation size of payload (or -1 if allocation is const). Note: this
325 // doesn't count the header.
326 size_t capacity_after_header_;
327 // The offset at which we will write the next field. Note: this doesn't count
329 size_t write_offset_;
331 // Just like WriteBytes, but with a compile-time size, for performance.
332 template<size_t length> void BASE_EXPORT WriteBytesStatic(const void* data);
334 // Writes a POD by copying its bytes.
335 template <typename T> bool WritePOD(const T& data) {
336 WriteBytesStatic<sizeof(data)>(&data);
340 inline void* ClaimUninitializedBytesInternal(size_t num_bytes);
341 inline void WriteBytesCommon(const void* data, size_t length);
343 FRIEND_TEST_ALL_PREFIXES(PickleTest, DeepCopyResize);
344 FRIEND_TEST_ALL_PREFIXES(PickleTest, Resize);
345 FRIEND_TEST_ALL_PREFIXES(PickleTest, PeekNext);
346 FRIEND_TEST_ALL_PREFIXES(PickleTest, PeekNextOverflow);
347 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNext);
348 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextWithIncompleteHeader);
349 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextOverflow);
354 #endif // BASE_PICKLE_H_