OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_PICKLE_H__ | 5 #ifndef BASE_PICKLE_H__ |
6 #define BASE_PICKLE_H__ | 6 #define BASE_PICKLE_H__ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/base_export.h" | 10 #include "base/base_export.h" |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 } | 209 } |
210 | 210 |
211 // Methods for adding to the payload of the Pickle. These values are | 211 // Methods for adding to the payload of the Pickle. These values are |
212 // appended to the end of the Pickle's payload. When reading values from a | 212 // appended to the end of the Pickle's payload. When reading values from a |
213 // Pickle, it is important to read them in the order in which they were added | 213 // Pickle, it is important to read them in the order in which they were added |
214 // to the Pickle. | 214 // to the Pickle. |
215 bool WriteBool(bool value) { | 215 bool WriteBool(bool value) { |
216 return WriteInt(value ? 1 : 0); | 216 return WriteInt(value ? 1 : 0); |
217 } | 217 } |
218 bool WriteInt(int value) { | 218 bool WriteInt(int value) { |
219 return WriteBytes(&value, sizeof(value)); | 219 return WritePOD(value); |
220 } | 220 } |
221 // WARNING: DO NOT USE THIS METHOD IF PICKLES ARE PERSISTED IN ANY WAY. | 221 // WARNING: DO NOT USE THIS METHOD IF PICKLES ARE PERSISTED IN ANY WAY. |
222 // It will write whatever a "long" is on this architecture. On 32-bit | 222 // It will write whatever a "long" is on this architecture. On 32-bit |
223 // platforms, it is 32 bits. On 64-bit platforms, it is 64 bits. If persisted | 223 // platforms, it is 32 bits. On 64-bit platforms, it is 64 bits. If persisted |
224 // pickles are still around after upgrading to 64-bit, or if they are copied | 224 // pickles are still around after upgrading to 64-bit, or if they are copied |
225 // between dissimilar systems, YOUR PICKLES WILL HAVE GONE BAD. | 225 // between dissimilar systems, YOUR PICKLES WILL HAVE GONE BAD. |
226 bool WriteLongUsingDangerousNonPortableLessPersistableForm(long value) { | 226 bool WriteLongUsingDangerousNonPortableLessPersistableForm(long value) { |
227 return WriteBytes(&value, sizeof(value)); | 227 return WritePOD(value); |
228 } | 228 } |
229 bool WriteUInt16(uint16 value) { | 229 bool WriteUInt16(uint16 value) { |
230 return WriteBytes(&value, sizeof(value)); | 230 return WritePOD(value); |
231 } | 231 } |
232 bool WriteUInt32(uint32 value) { | 232 bool WriteUInt32(uint32 value) { |
233 return WriteBytes(&value, sizeof(value)); | 233 return WritePOD(value); |
234 } | 234 } |
235 bool WriteInt64(int64 value) { | 235 bool WriteInt64(int64 value) { |
236 return WriteBytes(&value, sizeof(value)); | 236 return WritePOD(value); |
237 } | 237 } |
238 bool WriteUInt64(uint64 value) { | 238 bool WriteUInt64(uint64 value) { |
239 return WriteBytes(&value, sizeof(value)); | 239 return WritePOD(value); |
240 } | 240 } |
241 bool WriteFloat(float value) { | 241 bool WriteFloat(float value) { |
242 return WriteBytes(&value, sizeof(value)); | 242 return WritePOD(value); |
243 } | 243 } |
244 bool WriteString(const std::string& value); | 244 bool WriteString(const std::string& value); |
245 bool WriteWString(const std::wstring& value); | 245 bool WriteWString(const std::wstring& value); |
246 bool WriteString16(const string16& value); | 246 bool WriteString16(const string16& value); |
247 // "Data" is a blob with a length. When you read it out you will be given the | 247 // "Data" is a blob with a length. When you read it out you will be given the |
248 // length. See also WriteBytes. | 248 // length. See also WriteBytes. |
249 bool WriteData(const char* data, int length); | 249 bool WriteData(const char* data, int length); |
250 // "Bytes" is a blob with no length. The caller must specify the lenght both | 250 // "Bytes" is a blob with no length. The caller must specify the length both |
251 // when reading and writing. It is normally used to serialize PoD types of a | 251 // when reading and writing. It is normally used to serialize PoD types of a |
252 // known size. See also WriteData. | 252 // known size. See also WriteData. |
253 bool WriteBytes(const void* data, int data_len); | 253 bool WriteBytes(const void* data, int length); |
254 | 254 |
255 // Reserves space for upcoming writes when multiple writes will be made and | 255 // Reserves space for upcoming writes when multiple writes will be made and |
256 // their sizes are computed in advance. It can be significantly faster to call | 256 // their sizes are computed in advance. It can be significantly faster to call |
257 // Reserve() before calling WriteFoo() multiple times. | 257 // Reserve() before calling WriteFoo() multiple times. |
258 void Reserve(size_t additional_capacity); | 258 void Reserve(size_t additional_capacity); |
259 | 259 |
260 // Payload follows after allocation of Header (header size is customizable). | 260 // Payload follows after allocation of Header (header size is customizable). |
261 struct Header { | 261 struct Header { |
262 uint32 payload_size; // Specifies the size of the payload. | 262 uint32 payload_size; // Specifies the size of the payload. |
263 }; | 263 }; |
(...skipping 24 matching lines...) Expand all Loading... |
288 const char* end_of_payload() const { | 288 const char* end_of_payload() const { |
289 // This object may be invalid. | 289 // This object may be invalid. |
290 return header_ ? payload() + payload_size() : NULL; | 290 return header_ ? payload() + payload_size() : NULL; |
291 } | 291 } |
292 | 292 |
293 protected: | 293 protected: |
294 char* mutable_payload() { | 294 char* mutable_payload() { |
295 return reinterpret_cast<char*>(header_) + header_size_; | 295 return reinterpret_cast<char*>(header_) + header_size_; |
296 } | 296 } |
297 | 297 |
298 size_t capacity() const { | 298 size_t capacity_after_header() const { |
299 return capacity_; | 299 return capacity_after_header_; |
300 } | 300 } |
301 | 301 |
302 // Resizes the buffer for use when writing the specified amount of data. The | 302 // Resize the capacity, note that the input value should not include the size |
303 // location that the data should be written at is returned, or NULL if there | 303 // of the header. |
304 // was an error. Call EndWrite with the returned offset and the given length | 304 void Resize(size_t new_capacity); |
305 // to pad out for the next write. | |
306 char* BeginWrite(size_t length); | |
307 | |
308 // Completes the write operation by padding the data with NULL bytes until it | |
309 // is padded. Should be paired with BeginWrite, but it does not necessarily | |
310 // have to be called after the data is written. | |
311 void EndWrite(char* dest, int length); | |
312 | |
313 // Resize the capacity, note that the input value should include the size of | |
314 // the header: new_capacity = sizeof(Header) + desired_payload_capacity. | |
315 // A realloc() failure will cause a Resize failure... and caller should check | |
316 // the return result for true (i.e., successful resizing). | |
317 bool Resize(size_t new_capacity); | |
318 | 305 |
319 // Aligns 'i' by rounding it up to the next multiple of 'alignment' | 306 // Aligns 'i' by rounding it up to the next multiple of 'alignment' |
320 static size_t AlignInt(size_t i, int alignment) { | 307 static size_t AlignInt(size_t i, int alignment) { |
321 return i + (alignment - (i % alignment)) % alignment; | 308 return i + (alignment - (i % alignment)) % alignment; |
322 } | 309 } |
323 | 310 |
324 // Find the end of the pickled data that starts at range_start. Returns NULL | 311 // Find the end of the pickled data that starts at range_start. Returns NULL |
325 // if the entire Pickle is not found in the given data range. | 312 // if the entire Pickle is not found in the given data range. |
326 static const char* FindNext(size_t header_size, | 313 static const char* FindNext(size_t header_size, |
327 const char* range_start, | 314 const char* range_start, |
328 const char* range_end); | 315 const char* range_end); |
329 | 316 |
330 // The allocation granularity of the payload. | 317 // The allocation granularity of the payload. |
331 static const int kPayloadUnit; | 318 static const int kPayloadUnit; |
332 | 319 |
333 private: | 320 private: |
334 friend class PickleIterator; | 321 friend class PickleIterator; |
335 | 322 |
336 Header* header_; | 323 Header* header_; |
337 size_t header_size_; // Supports extra data between header and payload. | 324 size_t header_size_; // Supports extra data between header and payload. |
338 // Allocation size of payload (or -1 if allocation is const). | 325 // Allocation size of payload (or -1 if allocation is const). Note: this |
339 size_t capacity_; | 326 // doesn't count the header. |
| 327 size_t capacity_after_header_; |
| 328 // The offset at which we will write the next field. Note: this doesn't count |
| 329 // the header. |
| 330 size_t write_offset_; |
| 331 |
| 332 // Just like WriteBytes, but with a compile-time size, for performance. |
| 333 template<size_t length> void WriteBytesStatic(const void* data); |
| 334 |
| 335 // Writes a POD by copying its bytes. |
| 336 template <typename T> bool WritePOD(const T& data) { |
| 337 WriteBytesStatic<sizeof(data)>(&data); |
| 338 return true; |
| 339 } |
| 340 inline void WriteBytesCommon(const void* data, size_t length); |
340 | 341 |
341 FRIEND_TEST_ALL_PREFIXES(PickleTest, Resize); | 342 FRIEND_TEST_ALL_PREFIXES(PickleTest, Resize); |
342 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNext); | 343 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNext); |
343 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextWithIncompleteHeader); | 344 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextWithIncompleteHeader); |
344 }; | 345 }; |
345 | 346 |
346 #endif // BASE_PICKLE_H__ | 347 #endif // BASE_PICKLE_H__ |
OLD | NEW |