1*6777b538SAndroid Build Coastguard Worker // Copyright 2012 The Chromium Authors 2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file. 4*6777b538SAndroid Build Coastguard Worker 5*6777b538SAndroid Build Coastguard Worker #ifndef BASE_PICKLE_H_ 6*6777b538SAndroid Build Coastguard Worker #define BASE_PICKLE_H_ 7*6777b538SAndroid Build Coastguard Worker 8*6777b538SAndroid Build Coastguard Worker #include <stddef.h> 9*6777b538SAndroid Build Coastguard Worker #include <stdint.h> 10*6777b538SAndroid Build Coastguard Worker 11*6777b538SAndroid Build Coastguard Worker #include <optional> 12*6777b538SAndroid Build Coastguard Worker #include <string> 13*6777b538SAndroid Build Coastguard Worker #include <string_view> 14*6777b538SAndroid Build Coastguard Worker 15*6777b538SAndroid Build Coastguard Worker #include "base/base_export.h" 16*6777b538SAndroid Build Coastguard Worker #include "base/check_op.h" 17*6777b538SAndroid Build Coastguard Worker #include "base/containers/span.h" 18*6777b538SAndroid Build Coastguard Worker #include "base/gtest_prod_util.h" 19*6777b538SAndroid Build Coastguard Worker #include "base/memory/raw_ptr_exclusion.h" 20*6777b538SAndroid Build Coastguard Worker #include "base/memory/ref_counted.h" 21*6777b538SAndroid Build Coastguard Worker #include "base/strings/string_piece.h" 22*6777b538SAndroid Build Coastguard Worker 23*6777b538SAndroid Build Coastguard Worker namespace base { 24*6777b538SAndroid Build Coastguard Worker 25*6777b538SAndroid Build Coastguard Worker class Pickle; 26*6777b538SAndroid Build Coastguard Worker 27*6777b538SAndroid Build Coastguard Worker // PickleIterator reads data from a Pickle. The Pickle object must remain valid 28*6777b538SAndroid Build Coastguard Worker // while the PickleIterator object is in use. 29*6777b538SAndroid Build Coastguard Worker class BASE_EXPORT PickleIterator { 30*6777b538SAndroid Build Coastguard Worker public: PickleIterator()31*6777b538SAndroid Build Coastguard Worker PickleIterator() : payload_(nullptr), read_index_(0), end_index_(0) {} 32*6777b538SAndroid Build Coastguard Worker explicit PickleIterator(const Pickle& pickle); 33*6777b538SAndroid Build Coastguard Worker 34*6777b538SAndroid Build Coastguard Worker // Methods for reading the payload of the Pickle. To read from the start of 35*6777b538SAndroid Build Coastguard Worker // the Pickle, create a PickleIterator from a Pickle. If successful, these 36*6777b538SAndroid Build Coastguard Worker // methods return true. Otherwise, false is returned to indicate that the 37*6777b538SAndroid Build Coastguard Worker // result could not be extracted. It is not possible to read from the iterator 38*6777b538SAndroid Build Coastguard Worker // after that. 39*6777b538SAndroid Build Coastguard Worker [[nodiscard]] bool ReadBool(bool* result); 40*6777b538SAndroid Build Coastguard Worker [[nodiscard]] bool ReadInt(int* result); 41*6777b538SAndroid Build Coastguard Worker [[nodiscard]] bool ReadLong(long* result); 42*6777b538SAndroid Build Coastguard Worker [[nodiscard]] bool ReadUInt16(uint16_t* result); 43*6777b538SAndroid Build Coastguard Worker [[nodiscard]] bool ReadUInt32(uint32_t* result); 44*6777b538SAndroid Build Coastguard Worker [[nodiscard]] bool ReadInt64(int64_t* result); 45*6777b538SAndroid Build Coastguard Worker [[nodiscard]] bool ReadUInt64(uint64_t* result); 46*6777b538SAndroid Build Coastguard Worker [[nodiscard]] bool ReadFloat(float* result); 47*6777b538SAndroid Build Coastguard Worker [[nodiscard]] bool ReadDouble(double* result); 48*6777b538SAndroid Build Coastguard Worker [[nodiscard]] bool ReadString(std::string* result); 49*6777b538SAndroid Build Coastguard Worker // The StringPiece data will only be valid for the lifetime of the message. 50*6777b538SAndroid Build Coastguard Worker [[nodiscard]] bool ReadStringPiece(StringPiece* result); 51*6777b538SAndroid Build Coastguard Worker [[nodiscard]] bool ReadString16(std::u16string* result); 52*6777b538SAndroid Build Coastguard Worker // The StringPiece16 data will only be valid for the lifetime of the message. 53*6777b538SAndroid Build Coastguard Worker [[nodiscard]] bool ReadStringPiece16(StringPiece16* result); 54*6777b538SAndroid Build Coastguard Worker 55*6777b538SAndroid Build Coastguard Worker // A pointer to the data will be placed in |*data|, and the length will be 56*6777b538SAndroid Build Coastguard Worker // placed in |*length|. The pointer placed into |*data| points into the 57*6777b538SAndroid Build Coastguard Worker // message's buffer so it will be scoped to the lifetime of the message (or 58*6777b538SAndroid Build Coastguard Worker // until the message data is mutated). Do not keep the pointer around! 59*6777b538SAndroid Build Coastguard Worker [[nodiscard]] bool ReadData(const char** data, size_t* length); 60*6777b538SAndroid Build Coastguard Worker 61*6777b538SAndroid Build Coastguard Worker // Similar, but using base::span for convenience. 62*6777b538SAndroid Build Coastguard Worker [[nodiscard]] std::optional<base::span<const uint8_t>> ReadData(); 63*6777b538SAndroid Build Coastguard Worker 64*6777b538SAndroid Build Coastguard Worker // A pointer to the data will be placed in |*data|. The caller specifies the 65*6777b538SAndroid Build Coastguard Worker // number of bytes to read, and ReadBytes will validate this length. The 66*6777b538SAndroid Build Coastguard Worker // pointer placed into |*data| points into the message's buffer so it will be 67*6777b538SAndroid Build Coastguard Worker // scoped to the lifetime of the message (or until the message data is 68*6777b538SAndroid Build Coastguard Worker // mutated). Do not keep the pointer around! 69*6777b538SAndroid Build Coastguard Worker [[nodiscard]] bool ReadBytes(const char** data, size_t length); 70*6777b538SAndroid Build Coastguard Worker 71*6777b538SAndroid Build Coastguard Worker // A version of ReadInt() that checks for the result not being negative. Use 72*6777b538SAndroid Build Coastguard Worker // it for reading the object sizes. ReadLength(size_t * result)73*6777b538SAndroid Build Coastguard Worker [[nodiscard]] bool ReadLength(size_t* result) { 74*6777b538SAndroid Build Coastguard Worker int result_int; 75*6777b538SAndroid Build Coastguard Worker if (!ReadInt(&result_int) || result_int < 0) 76*6777b538SAndroid Build Coastguard Worker return false; 77*6777b538SAndroid Build Coastguard Worker *result = static_cast<size_t>(result_int); 78*6777b538SAndroid Build Coastguard Worker return true; 79*6777b538SAndroid Build Coastguard Worker } 80*6777b538SAndroid Build Coastguard Worker 81*6777b538SAndroid Build Coastguard Worker // Skips bytes in the read buffer and returns true if there are at least 82*6777b538SAndroid Build Coastguard Worker // num_bytes available. Otherwise, does nothing and returns false. SkipBytes(size_t num_bytes)83*6777b538SAndroid Build Coastguard Worker [[nodiscard]] bool SkipBytes(size_t num_bytes) { 84*6777b538SAndroid Build Coastguard Worker return !!GetReadPointerAndAdvance(num_bytes); 85*6777b538SAndroid Build Coastguard Worker } 86*6777b538SAndroid Build Coastguard Worker ReachedEnd()87*6777b538SAndroid Build Coastguard Worker bool ReachedEnd() const { return read_index_ == end_index_; } 88*6777b538SAndroid Build Coastguard Worker 89*6777b538SAndroid Build Coastguard Worker private: 90*6777b538SAndroid Build Coastguard Worker // Read Type from Pickle. 91*6777b538SAndroid Build Coastguard Worker template <typename Type> 92*6777b538SAndroid Build Coastguard Worker bool ReadBuiltinType(Type* result); 93*6777b538SAndroid Build Coastguard Worker 94*6777b538SAndroid Build Coastguard Worker // Advance read_index_ but do not allow it to exceed end_index_. 95*6777b538SAndroid Build Coastguard Worker // Keeps read_index_ aligned. 96*6777b538SAndroid Build Coastguard Worker void Advance(size_t size); 97*6777b538SAndroid Build Coastguard Worker 98*6777b538SAndroid Build Coastguard Worker // Get read pointer for Type and advance read pointer. 99*6777b538SAndroid Build Coastguard Worker template<typename Type> 100*6777b538SAndroid Build Coastguard Worker const char* GetReadPointerAndAdvance(); 101*6777b538SAndroid Build Coastguard Worker 102*6777b538SAndroid Build Coastguard Worker // Get read pointer for |num_bytes| and advance read pointer. This method 103*6777b538SAndroid Build Coastguard Worker // checks num_bytes for wrapping. 104*6777b538SAndroid Build Coastguard Worker const char* GetReadPointerAndAdvance(size_t num_bytes); 105*6777b538SAndroid Build Coastguard Worker 106*6777b538SAndroid Build Coastguard Worker // Get read pointer for (num_elements * size_element) bytes and advance read 107*6777b538SAndroid Build Coastguard Worker // pointer. This method checks for overflow and wrapping. 108*6777b538SAndroid Build Coastguard Worker const char* GetReadPointerAndAdvance(size_t num_elements, 109*6777b538SAndroid Build Coastguard Worker size_t size_element); 110*6777b538SAndroid Build Coastguard Worker 111*6777b538SAndroid Build Coastguard Worker const char* payload_; // Start of our pickle's payload. 112*6777b538SAndroid Build Coastguard Worker size_t read_index_; // Offset of the next readable byte in payload. 113*6777b538SAndroid Build Coastguard Worker size_t end_index_; // Payload size. 114*6777b538SAndroid Build Coastguard Worker 115*6777b538SAndroid Build Coastguard Worker FRIEND_TEST_ALL_PREFIXES(PickleTest, GetReadPointerAndAdvance); 116*6777b538SAndroid Build Coastguard Worker }; 117*6777b538SAndroid Build Coastguard Worker 118*6777b538SAndroid Build Coastguard Worker // This class provides facilities for basic binary value packing and unpacking. 119*6777b538SAndroid Build Coastguard Worker // 120*6777b538SAndroid Build Coastguard Worker // The Pickle class supports appending primitive values (ints, strings, etc.) 121*6777b538SAndroid Build Coastguard Worker // to a pickle instance. The Pickle instance grows its internal memory buffer 122*6777b538SAndroid Build Coastguard Worker // dynamically to hold the sequence of primitive values. The internal memory 123*6777b538SAndroid Build Coastguard Worker // buffer is exposed as the "data" of the Pickle. This "data" can be passed 124*6777b538SAndroid Build Coastguard Worker // to a Pickle object to initialize it for reading. 125*6777b538SAndroid Build Coastguard Worker // 126*6777b538SAndroid Build Coastguard Worker // When reading from a Pickle object, it is important for the consumer to know 127*6777b538SAndroid Build Coastguard Worker // what value types to read and in what order to read them as the Pickle does 128*6777b538SAndroid Build Coastguard Worker // not keep track of the type of data written to it. 129*6777b538SAndroid Build Coastguard Worker // 130*6777b538SAndroid Build Coastguard Worker // The Pickle's data has a header which contains the size of the Pickle's 131*6777b538SAndroid Build Coastguard Worker // payload. It can optionally support additional space in the header. That 132*6777b538SAndroid Build Coastguard Worker // space is controlled by the header_size parameter passed to the Pickle 133*6777b538SAndroid Build Coastguard Worker // constructor. 134*6777b538SAndroid Build Coastguard Worker // 135*6777b538SAndroid Build Coastguard Worker class BASE_EXPORT Pickle { 136*6777b538SAndroid Build Coastguard Worker public: 137*6777b538SAndroid Build Coastguard Worker // Auxiliary data attached to a Pickle. Pickle must be subclassed along with 138*6777b538SAndroid Build Coastguard Worker // this interface in order to provide a concrete implementation of support 139*6777b538SAndroid Build Coastguard Worker // for attachments. The base Pickle implementation does not accept 140*6777b538SAndroid Build Coastguard Worker // attachments. 141*6777b538SAndroid Build Coastguard Worker class BASE_EXPORT Attachment : public RefCountedThreadSafe<Attachment> { 142*6777b538SAndroid Build Coastguard Worker public: 143*6777b538SAndroid Build Coastguard Worker Attachment(); 144*6777b538SAndroid Build Coastguard Worker Attachment(const Attachment&) = delete; 145*6777b538SAndroid Build Coastguard Worker Attachment& operator=(const Attachment&) = delete; 146*6777b538SAndroid Build Coastguard Worker 147*6777b538SAndroid Build Coastguard Worker protected: 148*6777b538SAndroid Build Coastguard Worker friend class RefCountedThreadSafe<Attachment>; 149*6777b538SAndroid Build Coastguard Worker virtual ~Attachment(); 150*6777b538SAndroid Build Coastguard Worker }; 151*6777b538SAndroid Build Coastguard Worker 152*6777b538SAndroid Build Coastguard Worker // Initialize a Pickle object using the default header size. 153*6777b538SAndroid Build Coastguard Worker Pickle(); 154*6777b538SAndroid Build Coastguard Worker 155*6777b538SAndroid Build Coastguard Worker // Initialize a Pickle object with the specified header size in bytes, which 156*6777b538SAndroid Build Coastguard Worker // must be greater-than-or-equal-to `sizeof(Pickle::Header)`. The header size 157*6777b538SAndroid Build Coastguard Worker // will be rounded up to ensure that the header size is 32bit-aligned. Note 158*6777b538SAndroid Build Coastguard Worker // that the extra memory allocated due to the size difference between the 159*6777b538SAndroid Build Coastguard Worker // requested header size and the size of a standard header is not initialized. 160*6777b538SAndroid Build Coastguard Worker explicit Pickle(size_t header_size); 161*6777b538SAndroid Build Coastguard Worker 162*6777b538SAndroid Build Coastguard Worker // Returns a Pickle initialized from a block of data. The Pickle obtained by 163*6777b538SAndroid Build Coastguard Worker // this call makes a copy of the data from which it is initialized, so it is 164*6777b538SAndroid Build Coastguard Worker // safe to pass around without concern for the pointer to the original data 165*6777b538SAndroid Build Coastguard Worker // dangling. The header padding size is deduced from the data length. 166*6777b538SAndroid Build Coastguard Worker static Pickle WithData(span<const uint8_t> data); 167*6777b538SAndroid Build Coastguard Worker 168*6777b538SAndroid Build Coastguard Worker // Returns a Pickle initialized from a const block of data. The data is not 169*6777b538SAndroid Build Coastguard Worker // copied, only referenced, which can be dangerous; please only use this 170*6777b538SAndroid Build Coastguard Worker // initialization when the speed gain of not copying the data outweighs the 171*6777b538SAndroid Build Coastguard Worker // danger of dangling pointers. If a Pickle is obtained from this call, it is 172*6777b538SAndroid Build Coastguard Worker // a requirement that only const methods be called. The header padding size is 173*6777b538SAndroid Build Coastguard Worker // deduced from the data length. 174*6777b538SAndroid Build Coastguard Worker static Pickle WithUnownedBuffer(span<const uint8_t> data); 175*6777b538SAndroid Build Coastguard Worker 176*6777b538SAndroid Build Coastguard Worker // Initializes a Pickle as a copy of another Pickle. If the original Pickle's 177*6777b538SAndroid Build Coastguard Worker // data is unowned, the copy will have its own internalized copy of the data. 178*6777b538SAndroid Build Coastguard Worker Pickle(const Pickle& other); 179*6777b538SAndroid Build Coastguard Worker 180*6777b538SAndroid Build Coastguard Worker // Note: Other classes are derived from this class, and they may well 181*6777b538SAndroid Build Coastguard Worker // delete through this parent class, e.g. std::unique_ptr<Pickle> exists 182*6777b538SAndroid Build Coastguard Worker // in several places the code. 183*6777b538SAndroid Build Coastguard Worker virtual ~Pickle(); 184*6777b538SAndroid Build Coastguard Worker 185*6777b538SAndroid Build Coastguard Worker // Performs a deep copy. 186*6777b538SAndroid Build Coastguard Worker Pickle& operator=(const Pickle& other); 187*6777b538SAndroid Build Coastguard Worker 188*6777b538SAndroid Build Coastguard Worker // Returns the number of bytes written in the Pickle, including the header. size()189*6777b538SAndroid Build Coastguard Worker size_t size() const { 190*6777b538SAndroid Build Coastguard Worker return header_ ? header_size_ + header_->payload_size : 0; 191*6777b538SAndroid Build Coastguard Worker } 192*6777b538SAndroid Build Coastguard Worker 193*6777b538SAndroid Build Coastguard Worker // Returns the data for this Pickle. data()194*6777b538SAndroid Build Coastguard Worker const uint8_t* data() const { 195*6777b538SAndroid Build Coastguard Worker return reinterpret_cast<const uint8_t*>(header_); 196*6777b538SAndroid Build Coastguard Worker } 197*6777b538SAndroid Build Coastguard Worker 198*6777b538SAndroid Build Coastguard Worker // Handy method to simplify calling data() with a reinterpret_cast. data_as_char()199*6777b538SAndroid Build Coastguard Worker const char* data_as_char() const { 200*6777b538SAndroid Build Coastguard Worker return reinterpret_cast<const char*>(data()); 201*6777b538SAndroid Build Coastguard Worker } 202*6777b538SAndroid Build Coastguard Worker 203*6777b538SAndroid Build Coastguard Worker // Returns the effective memory capacity of this Pickle, that is, the total 204*6777b538SAndroid Build Coastguard Worker // number of bytes currently dynamically allocated or 0 in the case of a 205*6777b538SAndroid Build Coastguard Worker // read-only Pickle. This should be used only for diagnostic / profiling 206*6777b538SAndroid Build Coastguard Worker // purposes. 207*6777b538SAndroid Build Coastguard Worker size_t GetTotalAllocatedSize() const; 208*6777b538SAndroid Build Coastguard Worker 209*6777b538SAndroid Build Coastguard Worker // Methods for adding to the payload of the Pickle. These values are 210*6777b538SAndroid Build Coastguard Worker // appended to the end of the Pickle's payload. When reading values from a 211*6777b538SAndroid Build Coastguard Worker // Pickle, it is important to read them in the order in which they were added 212*6777b538SAndroid Build Coastguard Worker // to the Pickle. 213*6777b538SAndroid Build Coastguard Worker WriteBool(bool value)214*6777b538SAndroid Build Coastguard Worker void WriteBool(bool value) { WriteInt(value ? 1 : 0); } WriteInt(int value)215*6777b538SAndroid Build Coastguard Worker void WriteInt(int value) { WritePOD(value); } WriteLong(long value)216*6777b538SAndroid Build Coastguard Worker void WriteLong(long value) { 217*6777b538SAndroid Build Coastguard Worker // Always write long as a 64-bit value to ensure compatibility between 218*6777b538SAndroid Build Coastguard Worker // 32-bit and 64-bit processes. 219*6777b538SAndroid Build Coastguard Worker WritePOD(static_cast<int64_t>(value)); 220*6777b538SAndroid Build Coastguard Worker } WriteUInt16(uint16_t value)221*6777b538SAndroid Build Coastguard Worker void WriteUInt16(uint16_t value) { WritePOD(value); } WriteUInt32(uint32_t value)222*6777b538SAndroid Build Coastguard Worker void WriteUInt32(uint32_t value) { WritePOD(value); } WriteInt64(int64_t value)223*6777b538SAndroid Build Coastguard Worker void WriteInt64(int64_t value) { WritePOD(value); } WriteUInt64(uint64_t value)224*6777b538SAndroid Build Coastguard Worker void WriteUInt64(uint64_t value) { WritePOD(value); } WriteFloat(float value)225*6777b538SAndroid Build Coastguard Worker void WriteFloat(float value) { WritePOD(value); } WriteDouble(double value)226*6777b538SAndroid Build Coastguard Worker void WriteDouble(double value) { WritePOD(value); } 227*6777b538SAndroid Build Coastguard Worker void WriteString(const StringPiece& value); 228*6777b538SAndroid Build Coastguard Worker void WriteString16(const StringPiece16& value); 229*6777b538SAndroid Build Coastguard Worker // "Data" is a blob with a length. When you read it out you will be given the 230*6777b538SAndroid Build Coastguard Worker // length. See also WriteBytes. 231*6777b538SAndroid Build Coastguard Worker // TODO(https://crbug.com/40284755): Migrate callers to the span versions. 232*6777b538SAndroid Build Coastguard Worker void WriteData(const char* data, size_t length); 233*6777b538SAndroid Build Coastguard Worker void WriteData(span<const uint8_t> data); 234*6777b538SAndroid Build Coastguard Worker void WriteData(std::string_view data); 235*6777b538SAndroid Build Coastguard Worker // "Bytes" is a blob with no length. The caller must specify the length both 236*6777b538SAndroid Build Coastguard Worker // when reading and writing. It is normally used to serialize PoD types of a 237*6777b538SAndroid Build Coastguard Worker // known size. See also WriteData. 238*6777b538SAndroid Build Coastguard Worker // TODO(https://crbug.com/40284755): Migrate callers to the span version. 239*6777b538SAndroid Build Coastguard Worker void WriteBytes(const void* data, size_t length); 240*6777b538SAndroid Build Coastguard Worker void WriteBytes(span<const uint8_t> data); 241*6777b538SAndroid Build Coastguard Worker 242*6777b538SAndroid Build Coastguard Worker // WriteAttachment appends |attachment| to the pickle. It returns 243*6777b538SAndroid Build Coastguard Worker // false iff the set is full or if the Pickle implementation does not support 244*6777b538SAndroid Build Coastguard Worker // attachments. 245*6777b538SAndroid Build Coastguard Worker virtual bool WriteAttachment(scoped_refptr<Attachment> attachment); 246*6777b538SAndroid Build Coastguard Worker 247*6777b538SAndroid Build Coastguard Worker // ReadAttachment parses an attachment given the parsing state |iter| and 248*6777b538SAndroid Build Coastguard Worker // writes it to |*attachment|. It returns true on success. 249*6777b538SAndroid Build Coastguard Worker virtual bool ReadAttachment(base::PickleIterator* iter, 250*6777b538SAndroid Build Coastguard Worker scoped_refptr<Attachment>* attachment) const; 251*6777b538SAndroid Build Coastguard Worker 252*6777b538SAndroid Build Coastguard Worker // Indicates whether the pickle has any attachments. 253*6777b538SAndroid Build Coastguard Worker virtual bool HasAttachments() const; 254*6777b538SAndroid Build Coastguard Worker 255*6777b538SAndroid Build Coastguard Worker // Reserves space for upcoming writes when multiple writes will be made and 256*6777b538SAndroid Build Coastguard Worker // their sizes are computed in advance. It can be significantly faster to call 257*6777b538SAndroid Build Coastguard Worker // Reserve() before calling WriteFoo() multiple times. 258*6777b538SAndroid Build Coastguard Worker void Reserve(size_t additional_capacity); 259*6777b538SAndroid Build Coastguard Worker 260*6777b538SAndroid Build Coastguard Worker // Payload follows after allocation of Header (header size is customizable). 261*6777b538SAndroid Build Coastguard Worker struct Header { 262*6777b538SAndroid Build Coastguard Worker uint32_t payload_size; // Specifies the size of the payload. 263*6777b538SAndroid Build Coastguard Worker }; 264*6777b538SAndroid Build Coastguard Worker 265*6777b538SAndroid Build Coastguard Worker // Returns the header, cast to a user-specified type T. The type T must be a 266*6777b538SAndroid Build Coastguard Worker // subclass of Header and its size must correspond to the header_size passed 267*6777b538SAndroid Build Coastguard Worker // to the Pickle constructor. 268*6777b538SAndroid Build Coastguard Worker template <class T> headerT()269*6777b538SAndroid Build Coastguard Worker T* headerT() { 270*6777b538SAndroid Build Coastguard Worker DCHECK_EQ(header_size_, sizeof(T)); 271*6777b538SAndroid Build Coastguard Worker return static_cast<T*>(header_); 272*6777b538SAndroid Build Coastguard Worker } 273*6777b538SAndroid Build Coastguard Worker template <class T> headerT()274*6777b538SAndroid Build Coastguard Worker const T* headerT() const { 275*6777b538SAndroid Build Coastguard Worker DCHECK_EQ(header_size_, sizeof(T)); 276*6777b538SAndroid Build Coastguard Worker return static_cast<const T*>(header_); 277*6777b538SAndroid Build Coastguard Worker } 278*6777b538SAndroid Build Coastguard Worker 279*6777b538SAndroid Build Coastguard Worker // The payload is the pickle data immediately following the header. payload_size()280*6777b538SAndroid Build Coastguard Worker size_t payload_size() const { 281*6777b538SAndroid Build Coastguard Worker return header_ ? header_->payload_size : 0; 282*6777b538SAndroid Build Coastguard Worker } 283*6777b538SAndroid Build Coastguard Worker payload_bytes()284*6777b538SAndroid Build Coastguard Worker base::span<const uint8_t> payload_bytes() const { 285*6777b538SAndroid Build Coastguard Worker return base::as_bytes(base::make_span(payload(), payload_size())); 286*6777b538SAndroid Build Coastguard Worker } 287*6777b538SAndroid Build Coastguard Worker 288*6777b538SAndroid Build Coastguard Worker protected: 289*6777b538SAndroid Build Coastguard Worker // The protected constructor. Note that this creates a Pickle that does not 290*6777b538SAndroid Build Coastguard Worker // own its own data. 291*6777b538SAndroid Build Coastguard Worker enum UnownedData { kUnownedData }; 292*6777b538SAndroid Build Coastguard Worker explicit Pickle(UnownedData, span<const uint8_t> data); 293*6777b538SAndroid Build Coastguard Worker 294*6777b538SAndroid Build Coastguard Worker // Returns size of the header, which can have default value, set by user or 295*6777b538SAndroid Build Coastguard Worker // calculated by passed raw data. header_size()296*6777b538SAndroid Build Coastguard Worker size_t header_size() const { return header_size_; } 297*6777b538SAndroid Build Coastguard Worker payload()298*6777b538SAndroid Build Coastguard Worker const char* payload() const { 299*6777b538SAndroid Build Coastguard Worker return reinterpret_cast<const char*>(header_) + header_size_; 300*6777b538SAndroid Build Coastguard Worker } 301*6777b538SAndroid Build Coastguard Worker 302*6777b538SAndroid Build Coastguard Worker // Returns the address of the byte immediately following the currently valid 303*6777b538SAndroid Build Coastguard Worker // header + payload. end_of_payload()304*6777b538SAndroid Build Coastguard Worker const char* end_of_payload() const { 305*6777b538SAndroid Build Coastguard Worker // This object may be invalid. 306*6777b538SAndroid Build Coastguard Worker return header_ ? payload() + payload_size() : NULL; 307*6777b538SAndroid Build Coastguard Worker } 308*6777b538SAndroid Build Coastguard Worker mutable_payload()309*6777b538SAndroid Build Coastguard Worker char* mutable_payload() { 310*6777b538SAndroid Build Coastguard Worker return reinterpret_cast<char*>(header_) + header_size_; 311*6777b538SAndroid Build Coastguard Worker } 312*6777b538SAndroid Build Coastguard Worker capacity_after_header()313*6777b538SAndroid Build Coastguard Worker size_t capacity_after_header() const { 314*6777b538SAndroid Build Coastguard Worker return capacity_after_header_; 315*6777b538SAndroid Build Coastguard Worker } 316*6777b538SAndroid Build Coastguard Worker 317*6777b538SAndroid Build Coastguard Worker // Resize the capacity, note that the input value should not include the size 318*6777b538SAndroid Build Coastguard Worker // of the header. 319*6777b538SAndroid Build Coastguard Worker void Resize(size_t new_capacity); 320*6777b538SAndroid Build Coastguard Worker 321*6777b538SAndroid Build Coastguard Worker // Claims |num_bytes| bytes of payload. This is similar to Reserve() in that 322*6777b538SAndroid Build Coastguard Worker // it may grow the capacity, but it also advances the write offset of the 323*6777b538SAndroid Build Coastguard Worker // pickle by |num_bytes|. Claimed memory, including padding, is zeroed. 324*6777b538SAndroid Build Coastguard Worker // 325*6777b538SAndroid Build Coastguard Worker // Returns the address of the first byte claimed. 326*6777b538SAndroid Build Coastguard Worker void* ClaimBytes(size_t num_bytes); 327*6777b538SAndroid Build Coastguard Worker 328*6777b538SAndroid Build Coastguard Worker // Find the end of the pickled data that starts at range_start. Returns NULL 329*6777b538SAndroid Build Coastguard Worker // if the entire Pickle is not found in the given data range. 330*6777b538SAndroid Build Coastguard Worker static const char* FindNext(size_t header_size, 331*6777b538SAndroid Build Coastguard Worker const char* range_start, 332*6777b538SAndroid Build Coastguard Worker const char* range_end); 333*6777b538SAndroid Build Coastguard Worker 334*6777b538SAndroid Build Coastguard Worker // Parse pickle header and return total size of the pickle. Data range 335*6777b538SAndroid Build Coastguard Worker // doesn't need to contain entire pickle. 336*6777b538SAndroid Build Coastguard Worker // Returns true if pickle header was found and parsed. Callers must check 337*6777b538SAndroid Build Coastguard Worker // returned |pickle_size| for sanity (against maximum message size, etc). 338*6777b538SAndroid Build Coastguard Worker // NOTE: when function successfully parses a header, but encounters an 339*6777b538SAndroid Build Coastguard Worker // overflow during pickle size calculation, it sets |pickle_size| to the 340*6777b538SAndroid Build Coastguard Worker // maximum size_t value and returns true. 341*6777b538SAndroid Build Coastguard Worker static bool PeekNext(size_t header_size, 342*6777b538SAndroid Build Coastguard Worker const char* range_start, 343*6777b538SAndroid Build Coastguard Worker const char* range_end, 344*6777b538SAndroid Build Coastguard Worker size_t* pickle_size); 345*6777b538SAndroid Build Coastguard Worker 346*6777b538SAndroid Build Coastguard Worker // The allocation granularity of the payload. 347*6777b538SAndroid Build Coastguard Worker static const size_t kPayloadUnit; 348*6777b538SAndroid Build Coastguard Worker 349*6777b538SAndroid Build Coastguard Worker private: 350*6777b538SAndroid Build Coastguard Worker friend class PickleIterator; 351*6777b538SAndroid Build Coastguard Worker 352*6777b538SAndroid Build Coastguard Worker // `header_` is not a raw_ptr<...> for performance reasons (based on analysis 353*6777b538SAndroid Build Coastguard Worker // of sampling profiler data). 354*6777b538SAndroid Build Coastguard Worker RAW_PTR_EXCLUSION Header* header_; 355*6777b538SAndroid Build Coastguard Worker size_t header_size_; // Supports extra data between header and payload. 356*6777b538SAndroid Build Coastguard Worker // Allocation size of payload (or -1 if allocation is const). Note: this 357*6777b538SAndroid Build Coastguard Worker // doesn't count the header. 358*6777b538SAndroid Build Coastguard Worker size_t capacity_after_header_; 359*6777b538SAndroid Build Coastguard Worker // The offset at which we will write the next field. Note: this doesn't count 360*6777b538SAndroid Build Coastguard Worker // the header. 361*6777b538SAndroid Build Coastguard Worker size_t write_offset_; 362*6777b538SAndroid Build Coastguard Worker 363*6777b538SAndroid Build Coastguard Worker // Just like WriteBytes, but with a compile-time size, for performance. 364*6777b538SAndroid Build Coastguard Worker template<size_t length> void BASE_EXPORT WriteBytesStatic(const void* data); 365*6777b538SAndroid Build Coastguard Worker 366*6777b538SAndroid Build Coastguard Worker // Writes a POD by copying its bytes. WritePOD(const T & data)367*6777b538SAndroid Build Coastguard Worker template <typename T> bool WritePOD(const T& data) { 368*6777b538SAndroid Build Coastguard Worker WriteBytesStatic<sizeof(data)>(&data); 369*6777b538SAndroid Build Coastguard Worker return true; 370*6777b538SAndroid Build Coastguard Worker } 371*6777b538SAndroid Build Coastguard Worker 372*6777b538SAndroid Build Coastguard Worker inline void* ClaimUninitializedBytesInternal(size_t num_bytes); 373*6777b538SAndroid Build Coastguard Worker inline void WriteBytesCommon(span<const uint8_t> data); 374*6777b538SAndroid Build Coastguard Worker 375*6777b538SAndroid Build Coastguard Worker FRIEND_TEST_ALL_PREFIXES(PickleTest, DeepCopyResize); 376*6777b538SAndroid Build Coastguard Worker FRIEND_TEST_ALL_PREFIXES(PickleTest, Resize); 377*6777b538SAndroid Build Coastguard Worker FRIEND_TEST_ALL_PREFIXES(PickleTest, PeekNext); 378*6777b538SAndroid Build Coastguard Worker FRIEND_TEST_ALL_PREFIXES(PickleTest, PeekNextOverflow); 379*6777b538SAndroid Build Coastguard Worker FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNext); 380*6777b538SAndroid Build Coastguard Worker FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextWithIncompleteHeader); 381*6777b538SAndroid Build Coastguard Worker FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextOverflow); 382*6777b538SAndroid Build Coastguard Worker }; 383*6777b538SAndroid Build Coastguard Worker 384*6777b538SAndroid Build Coastguard Worker } // namespace base 385*6777b538SAndroid Build Coastguard Worker 386*6777b538SAndroid Build Coastguard Worker #endif // BASE_PICKLE_H_ 387