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