blob: f867de5de6f635d686b5f0691602a658837a2642 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2012 The Chromium Authors
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
tfarinaa31163512015-05-13 22:10:155#ifndef BASE_PICKLE_H_
6#define BASE_PICKLE_H_
initial.commitd7cae122008-07-26 21:49:387
avi9b6f42932015-12-26 22:15:148#include <stddef.h>
avic0279142015-12-04 22:38:529#include <stdint.h>
10
Arthur Sonzognie5fff99c2024-02-21 15:58:2411#include <optional>
initial.commitd7cae122008-07-26 21:49:3812#include <string>
Austin Sullivan31fcd06b2024-01-10 22:18:2913#include <string_view>
initial.commitd7cae122008-07-26 21:49:3814
[email protected]0bea7252011-08-05 15:34:0015#include "base/base_export.h"
Hans Wennborg7b533712020-06-22 20:52:2716#include "base/check_op.h"
Peter Kasting62b937d2024-10-09 20:47:4417#include "base/compiler_specific.h"
Peter Kasting4acf7062024-10-15 21:41:2818#include "base/containers/checked_iterators.h"
Jeremy Roman2d8d7802020-06-11 22:08:2019#include "base/containers/span.h"
[email protected]a918f872010-06-01 14:30:5120#include "base/gtest_prod_util.h"
Keishi Hattori488b7602022-05-02 13:09:3121#include "base/memory/raw_ptr_exclusion.h"
rockot502c94f2016-02-03 20:20:1622#include "base/memory/ref_counted.h"
initial.commitd7cae122008-07-26 21:49:3823
brettw05cfd8ddb2015-06-02 07:02:4724namespace base {
25
[email protected]ce208f872012-03-07 20:42:5626class Pickle;
27
28// PickleIterator reads data from a Pickle. The Pickle object must remain valid
29// while the PickleIterator object is in use.
30class BASE_EXPORT PickleIterator {
31 public:
Raul Tambref191b592019-01-21 23:56:2932 PickleIterator() : payload_(nullptr), read_index_(0), end_index_(0) {}
[email protected]ce208f872012-03-07 20:42:5633 explicit PickleIterator(const Pickle& pickle);
34
35 // Methods for reading the payload of the Pickle. To read from the start of
36 // the Pickle, create a PickleIterator from a Pickle. If successful, these
37 // methods return true. Otherwise, false is returned to indicate that the
avi48fc13b2014-12-28 23:31:4838 // result could not be extracted. It is not possible to read from the iterator
[email protected]a15016f2014-06-02 23:23:4939 // after that.
Daniel Cheng4455c9842022-01-13 23:26:3740 [[nodiscard]] bool ReadBool(bool* result);
41 [[nodiscard]] bool ReadInt(int* result);
42 [[nodiscard]] bool ReadLong(long* result);
43 [[nodiscard]] bool ReadUInt16(uint16_t* result);
44 [[nodiscard]] bool ReadUInt32(uint32_t* result);
45 [[nodiscard]] bool ReadInt64(int64_t* result);
46 [[nodiscard]] bool ReadUInt64(uint64_t* result);
47 [[nodiscard]] bool ReadFloat(float* result);
48 [[nodiscard]] bool ReadDouble(double* result);
49 [[nodiscard]] bool ReadString(std::string* result);
Helmut Januschka1dce9dc2024-06-11 13:05:3550 // The std::string_view data will only be valid for the lifetime of the
51 // message.
52 [[nodiscard]] bool ReadStringPiece(std::string_view* result);
Daniel Cheng4455c9842022-01-13 23:26:3753 [[nodiscard]] bool ReadString16(std::u16string* result);
Helmut Januschka1dce9dc2024-06-11 13:05:3554 // The std::u16string_view data will only be valid for the lifetime of the
55 // message.
56 [[nodiscard]] bool ReadStringPiece16(std::u16string_view* result);
avi48fc13b2014-12-28 23:31:4857
58 // A pointer to the data will be placed in |*data|, and the length will be
59 // placed in |*length|. The pointer placed into |*data| points into the
60 // message's buffer so it will be scoped to the lifetime of the message (or
61 // until the message data is mutated). Do not keep the pointer around!
Peter Kasting28b51cf2022-06-28 15:02:4362 [[nodiscard]] bool ReadData(const char** data, size_t* length);
avi48fc13b2014-12-28 23:31:4863
Peter Kasting4acf7062024-10-15 21:41:2864 // Similar, but using span for convenience.
65 [[nodiscard]] std::optional<span<const uint8_t>> ReadData();
Jeremy Roman2d8d7802020-06-11 22:08:2066
avi48fc13b2014-12-28 23:31:4867 // A pointer to the data will be placed in |*data|. The caller specifies the
68 // number of bytes to read, and ReadBytes will validate this length. The
69 // pointer placed into |*data| points into the message's buffer so it will be
70 // scoped to the lifetime of the message (or until the message data is
71 // mutated). Do not keep the pointer around!
Peter Kasting28b51cf2022-06-28 15:02:4372 [[nodiscard]] bool ReadBytes(const char** data, size_t length);
[email protected]ce208f872012-03-07 20:42:5673
Matt Menke4e486db2025-04-07 13:51:4374 // Similar, but using span for convenience.
75 [[nodiscard]] std::optional<span<const uint8_t>> ReadBytes(size_t length);
76
Peter Kasting28b51cf2022-06-28 15:02:4377 // A version of ReadInt() that checks for the result not being negative. Use
78 // it for reading the object sizes.
79 [[nodiscard]] bool ReadLength(size_t* result) {
80 int result_int;
Peter Kasting134ef9af2024-12-28 02:30:0981 if (!ReadInt(&result_int) || result_int < 0) {
Peter Kasting28b51cf2022-06-28 15:02:4382 return false;
Peter Kasting134ef9af2024-12-28 02:30:0983 }
Peter Kasting28b51cf2022-06-28 15:02:4384 *result = static_cast<size_t>(result_int);
85 return true;
[email protected]ce208f872012-03-07 20:42:5686 }
87
88 // Skips bytes in the read buffer and returns true if there are at least
89 // num_bytes available. Otherwise, does nothing and returns false.
Peter Kasting28b51cf2022-06-28 15:02:4390 [[nodiscard]] bool SkipBytes(size_t num_bytes) {
[email protected]ce208f872012-03-07 20:42:5691 return !!GetReadPointerAndAdvance(num_bytes);
92 }
93
Adam Rice2e8d92c2025-03-07 08:26:5594 // Returns true if all the data in the Pickle has been consumed.
Daniel Chengea877be2020-03-06 22:56:3195 bool ReachedEnd() const { return read_index_ == end_index_; }
96
Adam Rice2e8d92c2025-03-07 08:26:5597 // Returns the number of unused bytes remaining in the Pickle. Most code
98 // should not use this. Just call a Read* method and check the return value.
99 // Where this is useful is if you need to allocate space for a container
100 // before reading the data that will fill the container. In that case, this
101 // method can be used to check if the size is plausible before attempting the
102 // allocation.
103 size_t RemainingBytes() const { return end_index_ - read_index_; }
104
[email protected]ce208f872012-03-07 20:42:56105 private:
[email protected]ce208f872012-03-07 20:42:56106 // Read Type from Pickle.
107 template <typename Type>
[email protected]a15016f2014-06-02 23:23:49108 bool ReadBuiltinType(Type* result);
109
110 // Advance read_index_ but do not allow it to exceed end_index_.
111 // Keeps read_index_ aligned.
112 void Advance(size_t size);
[email protected]ce208f872012-03-07 20:42:56113
114 // Get read pointer for Type and advance read pointer.
Peter Kasting134ef9af2024-12-28 02:30:09115 template <typename Type>
[email protected]a15016f2014-06-02 23:23:49116 const char* GetReadPointerAndAdvance();
[email protected]ce208f872012-03-07 20:42:56117
118 // Get read pointer for |num_bytes| and advance read pointer. This method
Peter Kasting28b51cf2022-06-28 15:02:43119 // checks num_bytes for wrapping.
120 const char* GetReadPointerAndAdvance(size_t num_bytes);
[email protected]ce208f872012-03-07 20:42:56121
122 // Get read pointer for (num_elements * size_element) bytes and advance read
Peter Kasting28b51cf2022-06-28 15:02:43123 // pointer. This method checks for overflow and wrapping.
124 const char* GetReadPointerAndAdvance(size_t num_elements,
[email protected]a15016f2014-06-02 23:23:49125 size_t size_element);
[email protected]ce208f872012-03-07 20:42:56126
[email protected]a15016f2014-06-02 23:23:49127 const char* payload_; // Start of our pickle's payload.
Peter Kasting134ef9af2024-12-28 02:30:09128 size_t read_index_; // Offset of the next readable byte in payload.
129 size_t end_index_; // Payload size.
[email protected]ce208f872012-03-07 20:42:56130
131 FRIEND_TEST_ALL_PREFIXES(PickleTest, GetReadPointerAndAdvance);
132};
133
initial.commitd7cae122008-07-26 21:49:38134// This class provides facilities for basic binary value packing and unpacking.
135//
136// The Pickle class supports appending primitive values (ints, strings, etc.)
137// to a pickle instance. The Pickle instance grows its internal memory buffer
138// dynamically to hold the sequence of primitive values. The internal memory
139// buffer is exposed as the "data" of the Pickle. This "data" can be passed
140// to a Pickle object to initialize it for reading.
141//
142// When reading from a Pickle object, it is important for the consumer to know
143// what value types to read and in what order to read them as the Pickle does
144// not keep track of the type of data written to it.
145//
146// The Pickle's data has a header which contains the size of the Pickle's
147// payload. It can optionally support additional space in the header. That
148// space is controlled by the header_size parameter passed to the Pickle
149// constructor.
150//
[email protected]0bea7252011-08-05 15:34:00151class BASE_EXPORT Pickle {
initial.commitd7cae122008-07-26 21:49:38152 public:
rockot502c94f2016-02-03 20:20:16153 // Auxiliary data attached to a Pickle. Pickle must be subclassed along with
154 // this interface in order to provide a concrete implementation of support
155 // for attachments. The base Pickle implementation does not accept
156 // attachments.
157 class BASE_EXPORT Attachment : public RefCountedThreadSafe<Attachment> {
158 public:
159 Attachment();
David Bienvenu5f4d4f02020-09-27 16:55:03160 Attachment(const Attachment&) = delete;
161 Attachment& operator=(const Attachment&) = delete;
rockot502c94f2016-02-03 20:20:16162
163 protected:
164 friend class RefCountedThreadSafe<Attachment>;
165 virtual ~Attachment();
rockot502c94f2016-02-03 20:20:16166 };
167
Peter Kasting4acf7062024-10-15 21:41:28168 using iterator = CheckedContiguousIterator<const uint8_t>;
169
initial.commitd7cae122008-07-26 21:49:38170 // Initialize a Pickle object using the default header size.
171 Pickle();
172
173 // Initialize a Pickle object with the specified header size in bytes, which
Avi Drissman516e1922024-03-21 18:11:28174 // must be greater-than-or-equal-to `sizeof(Pickle::Header)`. The header size
175 // will be rounded up to ensure that the header size is 32bit-aligned. Note
176 // that the extra memory allocated due to the size difference between the
177 // requested header size and the size of a standard header is not initialized.
Peter Kasting28b51cf2022-06-28 15:02:43178 explicit Pickle(size_t header_size);
initial.commitd7cae122008-07-26 21:49:38179
Avi Drissman516e1922024-03-21 18:11:28180 // Returns a Pickle initialized from a block of data. The Pickle obtained by
181 // this call makes a copy of the data from which it is initialized, so it is
182 // safe to pass around without concern for the pointer to the original data
183 // dangling. The header padding size is deduced from the data length.
184 static Pickle WithData(span<const uint8_t> data);
initial.commitd7cae122008-07-26 21:49:38185
Avi Drissman516e1922024-03-21 18:11:28186 // Returns a Pickle initialized from a const block of data. The data is not
187 // copied, only referenced, which can be dangerous; please only use this
188 // initialization when the speed gain of not copying the data outweighs the
189 // danger of dangling pointers. If a Pickle is obtained from this call, it is
190 // a requirement that only const methods be called. The header padding size is
191 // deduced from the data length.
192 static Pickle WithUnownedBuffer(span<const uint8_t> data);
193
Avi Drissman516e1922024-03-21 18:11:28194 // Initializes a Pickle as a copy of another Pickle. If the original Pickle's
195 // data is unowned, the copy will have its own internalized copy of the data.
initial.commitd7cae122008-07-26 21:49:38196 Pickle(const Pickle& other);
197
Tom Sepez7baba4e2023-11-22 04:52:15198 // Note: Other classes are derived from this class, and they may well
Avi Drissman516e1922024-03-21 18:11:28199 // delete through this parent class, e.g. std::unique_ptr<Pickle> exists
Tom Sepez7baba4e2023-11-22 04:52:15200 // in several places the code.
[email protected]a502bbe72011-01-07 18:06:45201 virtual ~Pickle();
202
initial.commitd7cae122008-07-26 21:49:38203 // Performs a deep copy.
204 Pickle& operator=(const Pickle& other);
205
primiano9882cf342015-06-11 21:40:10206 // Returns the number of bytes written in the Pickle, including the header.
David Sanders42527592021-06-16 07:20:38207 size_t size() const {
208 return header_ ? header_size_ + header_->payload_size : 0;
209 }
initial.commitd7cae122008-07-26 21:49:38210
Peter Kasting62b937d2024-10-09 20:47:44211 bool empty() const { return !size(); }
212
initial.commitd7cae122008-07-26 21:49:38213 // Returns the data for this Pickle.
Claudio DeSouzac537f85d2023-03-02 18:42:52214 const uint8_t* data() const {
215 return reinterpret_cast<const uint8_t*>(header_);
216 }
217
218 // Handy method to simplify calling data() with a reinterpret_cast.
219 const char* data_as_char() const {
220 return reinterpret_cast<const char*>(data());
221 }
initial.commitd7cae122008-07-26 21:49:38222
Peter Kasting62b937d2024-10-09 20:47:44223 // Iteration. These allow `Pickle` to satisfy `std::ranges::contiguous_range`,
224 // which in turn allow it to be implicitly converted to a `span`.
Peter Kasting4acf7062024-10-15 21:41:28225 iterator begin() const {
Peter Kasting62b937d2024-10-09 20:47:44226 // SAFETY: `data()` always points to at least `size()` valid bytes, so this
227 // pointer is no further than just-past-the-end of the allocation.
Peter Kasting4acf7062024-10-15 21:41:28228 return UNSAFE_BUFFERS(iterator(data(), data() + size()));
229 }
230 iterator end() const {
231 // SAFETY: As in `begin()` above.
232 return UNSAFE_BUFFERS(iterator(data(), data() + size(), data() + size()));
Peter Kasting62b937d2024-10-09 20:47:44233 }
234
primiano9882cf342015-06-11 21:40:10235 // Returns the effective memory capacity of this Pickle, that is, the total
236 // number of bytes currently dynamically allocated or 0 in the case of a
237 // read-only Pickle. This should be used only for diagnostic / profiling
238 // purposes.
239 size_t GetTotalAllocatedSize() const;
240
initial.commitd7cae122008-07-26 21:49:38241 // Methods for adding to the payload of the Pickle. These values are
242 // appended to the end of the Pickle's payload. When reading values from a
243 // Pickle, it is important to read them in the order in which they were added
244 // to the Pickle.
avi48fc13b2014-12-28 23:31:48245
Daniel Cheng0d89f9222017-09-22 05:05:07246 void WriteBool(bool value) { WriteInt(value ? 1 : 0); }
247 void WriteInt(int value) { WritePOD(value); }
248 void WriteLong(long value) {
jam03d8a782016-02-10 20:13:39249 // Always write long as a 64-bit value to ensure compatibility between
250 // 32-bit and 64-bit processes.
Daniel Cheng0d89f9222017-09-22 05:05:07251 WritePOD(static_cast<int64_t>(value));
initial.commitd7cae122008-07-26 21:49:38252 }
Daniel Cheng0d89f9222017-09-22 05:05:07253 void WriteUInt16(uint16_t value) { WritePOD(value); }
254 void WriteUInt32(uint32_t value) { WritePOD(value); }
255 void WriteInt64(int64_t value) { WritePOD(value); }
256 void WriteUInt64(uint64_t value) { WritePOD(value); }
257 void WriteFloat(float value) { WritePOD(value); }
258 void WriteDouble(double value) { WritePOD(value); }
Helmut Januschka1dce9dc2024-06-11 13:05:35259 void WriteString(std::string_view value);
260 void WriteString16(std::u16string_view value);
[email protected]34d48612012-06-29 00:05:04261 // "Data" is a blob with a length. When you read it out you will be given the
262 // length. See also WriteBytes.
Avi Drissman516e1922024-03-21 18:11:28263 // TODO(https://crbug.com/40284755): Migrate callers to the span versions.
Peter Kasting28b51cf2022-06-28 15:02:43264 void WriteData(const char* data, size_t length);
Tom Sepez311d8782024-02-14 09:30:52265 void WriteData(span<const uint8_t> data);
Austin Sullivan31fcd06b2024-01-10 22:18:29266 void WriteData(std::string_view data);
[email protected]d1b319fc2013-10-31 04:03:02267 // "Bytes" is a blob with no length. The caller must specify the length both
[email protected]34d48612012-06-29 00:05:04268 // when reading and writing. It is normally used to serialize PoD types of a
269 // known size. See also WriteData.
Avi Drissman516e1922024-03-21 18:11:28270 // TODO(https://crbug.com/40284755): Migrate callers to the span version.
Peter Kasting28b51cf2022-06-28 15:02:43271 void WriteBytes(const void* data, size_t length);
Austin Sullivan31fcd06b2024-01-10 22:18:29272 void WriteBytes(span<const uint8_t> data);
initial.commitd7cae122008-07-26 21:49:38273
rockot502c94f2016-02-03 20:20:16274 // WriteAttachment appends |attachment| to the pickle. It returns
275 // false iff the set is full or if the Pickle implementation does not support
276 // attachments.
277 virtual bool WriteAttachment(scoped_refptr<Attachment> attachment);
278
279 // ReadAttachment parses an attachment given the parsing state |iter| and
280 // writes it to |*attachment|. It returns true on success.
Peter Kasting4acf7062024-10-15 21:41:28281 virtual bool ReadAttachment(PickleIterator* iter,
rockot502c94f2016-02-03 20:20:16282 scoped_refptr<Attachment>* attachment) const;
283
284 // Indicates whether the pickle has any attachments.
285 virtual bool HasAttachments() const;
286
[email protected]032bfc42013-10-29 22:23:52287 // Reserves space for upcoming writes when multiple writes will be made and
288 // their sizes are computed in advance. It can be significantly faster to call
289 // Reserve() before calling WriteFoo() multiple times.
290 void Reserve(size_t additional_capacity);
291
[email protected]c9046af2008-08-06 20:35:17292 // Payload follows after allocation of Header (header size is customizable).
initial.commitd7cae122008-07-26 21:49:38293 struct Header {
avic0279142015-12-04 22:38:52294 uint32_t payload_size; // Specifies the size of the payload.
initial.commitd7cae122008-07-26 21:49:38295 };
296
297 // Returns the header, cast to a user-specified type T. The type T must be a
298 // subclass of Header and its size must correspond to the header_size passed
299 // to the Pickle constructor.
300 template <class T>
301 T* headerT() {
[email protected]5d2b4492011-03-01 02:48:05302 DCHECK_EQ(header_size_, sizeof(T));
initial.commitd7cae122008-07-26 21:49:38303 return static_cast<T*>(header_);
304 }
305 template <class T>
306 const T* headerT() const {
[email protected]5d2b4492011-03-01 02:48:05307 DCHECK_EQ(header_size_, sizeof(T));
initial.commitd7cae122008-07-26 21:49:38308 return static_cast<const T*>(header_);
309 }
310
[email protected]73d96dc2012-03-30 22:35:27311 // The payload is the pickle data immediately following the header.
Peter Kasting134ef9af2024-12-28 02:30:09312 size_t payload_size() const { return header_ ? header_->payload_size : 0; }
[email protected]e00a6c0a2013-01-18 18:20:57313
Peter Kasting4acf7062024-10-15 21:41:28314 span<const uint8_t> payload_bytes() const {
Tom Sepezade72392025-01-07 00:47:11315 return as_bytes(UNSAFE_TODO(span(payload(), payload_size())));
Lukasz Anforowicz0d6a164c2024-02-05 21:58:08316 }
317
Tom Sepez311d8782024-02-14 09:30:52318 protected:
Avi Drissman516e1922024-03-21 18:11:28319 // The protected constructor. Note that this creates a Pickle that does not
320 // own its own data.
321 enum UnownedData { kUnownedData };
Lei Zhangb39481fd2025-06-26 01:03:40322 Pickle(UnownedData, span<const uint8_t> data);
Avi Drissman516e1922024-03-21 18:11:28323
Tom Sepez311d8782024-02-14 09:30:52324 // Returns size of the header, which can have default value, set by user or
325 // calculated by passed raw data.
326 size_t header_size() const { return header_size_; }
327
328 const char* payload() const {
Tom Sepezade72392025-01-07 00:47:11329 return UNSAFE_TODO(reinterpret_cast<const char*>(header_) + header_size_);
Tom Sepez311d8782024-02-14 09:30:52330 }
331
initial.commitd7cae122008-07-26 21:49:38332 // Returns the address of the byte immediately following the currently valid
333 // header + payload.
initial.commitd7cae122008-07-26 21:49:38334 const char* end_of_payload() const {
[email protected]d87f8e6f2010-11-15 19:31:23335 // This object may be invalid.
Tom Sepezade72392025-01-07 00:47:11336 return header_ ? UNSAFE_TODO(payload() + payload_size()) : NULL;
initial.commitd7cae122008-07-26 21:49:38337 }
338
[email protected]e00a6c0a2013-01-18 18:20:57339 char* mutable_payload() {
Tom Sepezade72392025-01-07 00:47:11340 return UNSAFE_TODO(reinterpret_cast<char*>(header_) + header_size_);
[email protected]e00a6c0a2013-01-18 18:20:57341 }
342
Peter Kasting134ef9af2024-12-28 02:30:09343 size_t capacity_after_header() const { return capacity_after_header_; }
initial.commitd7cae122008-07-26 21:49:38344
[email protected]d1b319fc2013-10-31 04:03:02345 // Resize the capacity, note that the input value should not include the size
346 // of the header.
347 void Resize(size_t new_capacity);
initial.commitd7cae122008-07-26 21:49:38348
rockot0776a502015-12-17 06:19:49349 // Claims |num_bytes| bytes of payload. This is similar to Reserve() in that
350 // it may grow the capacity, but it also advances the write offset of the
351 // pickle by |num_bytes|. Claimed memory, including padding, is zeroed.
352 //
353 // Returns the address of the first byte claimed.
354 void* ClaimBytes(size_t num_bytes);
355
initial.commitd7cae122008-07-26 21:49:38356 // Find the end of the pickled data that starts at range_start. Returns NULL
357 // if the entire Pickle is not found in the given data range.
358 static const char* FindNext(size_t header_size,
359 const char* range_start,
360 const char* range_end);
361
dskiba6f3790a2015-09-30 17:24:30362 // Parse pickle header and return total size of the pickle. Data range
363 // doesn't need to contain entire pickle.
364 // Returns true if pickle header was found and parsed. Callers must check
365 // returned |pickle_size| for sanity (against maximum message size, etc).
366 // NOTE: when function successfully parses a header, but encounters an
367 // overflow during pickle size calculation, it sets |pickle_size| to the
368 // maximum size_t value and returns true.
369 static bool PeekNext(size_t header_size,
370 const char* range_start,
371 const char* range_end,
372 size_t* pickle_size);
373
initial.commitd7cae122008-07-26 21:49:38374 // The allocation granularity of the payload.
Peter Kasting28b51cf2022-06-28 15:02:43375 static const size_t kPayloadUnit;
initial.commitd7cae122008-07-26 21:49:38376
377 private:
[email protected]ce208f872012-03-07 20:42:56378 friend class PickleIterator;
379
Keishi Hattori7ccb88c2022-01-18 16:48:45380 // `header_` is not a raw_ptr<...> for performance reasons (based on analysis
381 // of sampling profiler data).
Keishi Hattori488b7602022-05-02 13:09:31382 RAW_PTR_EXCLUSION Header* header_;
initial.commitd7cae122008-07-26 21:49:38383 size_t header_size_; // Supports extra data between header and payload.
[email protected]d1b319fc2013-10-31 04:03:02384 // Allocation size of payload (or -1 if allocation is const). Note: this
385 // doesn't count the header.
386 size_t capacity_after_header_;
387 // The offset at which we will write the next field. Note: this doesn't count
388 // the header.
389 size_t write_offset_;
390
391 // Just like WriteBytes, but with a compile-time size, for performance.
Peter Kasting134ef9af2024-12-28 02:30:09392 template <size_t length>
393 void BASE_EXPORT WriteBytesStatic(const void* data);
[email protected]d1b319fc2013-10-31 04:03:02394
395 // Writes a POD by copying its bytes.
Peter Kasting134ef9af2024-12-28 02:30:09396 template <typename T>
397 bool WritePOD(const T& data) {
[email protected]d1b319fc2013-10-31 04:03:02398 WriteBytesStatic<sizeof(data)>(&data);
399 return true;
400 }
rockot0776a502015-12-17 06:19:49401
402 inline void* ClaimUninitializedBytesInternal(size_t num_bytes);
Austin Sullivan31fcd06b2024-01-10 22:18:29403 inline void WriteBytesCommon(span<const uint8_t> data);
initial.commitd7cae122008-07-26 21:49:38404
erikchenf9ca8f5f02015-09-08 23:36:29405 FRIEND_TEST_ALL_PREFIXES(PickleTest, DeepCopyResize);
[email protected]a918f872010-06-01 14:30:51406 FRIEND_TEST_ALL_PREFIXES(PickleTest, Resize);
dskiba6f3790a2015-09-30 17:24:30407 FRIEND_TEST_ALL_PREFIXES(PickleTest, PeekNext);
408 FRIEND_TEST_ALL_PREFIXES(PickleTest, PeekNextOverflow);
[email protected]a918f872010-06-01 14:30:51409 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNext);
[email protected]137d2372011-01-26 13:02:27410 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextWithIncompleteHeader);
[email protected]33a38dd2013-11-01 09:06:26411 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextOverflow);
initial.commitd7cae122008-07-26 21:49:38412};
413
brettw05cfd8ddb2015-06-02 07:02:47414} // namespace base
415
tfarinaa31163512015-05-13 22:10:15416#endif // BASE_PICKLE_H_