1/*
2 *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11#ifndef WEBRTC_BASE_BYTEBUFFER_H_
12#define WEBRTC_BASE_BYTEBUFFER_H_
13
14#include <string>
15
16#include "webrtc/base/basictypes.h"
17#include "webrtc/base/constructormagic.h"
18
19namespace rtc {
20
21class ByteBuffer {
22 public:
23
24  enum ByteOrder {
25    ORDER_NETWORK = 0,  // Default, use network byte order (big endian).
26    ORDER_HOST,         // Use the native order of the host.
27  };
28
29  // |byte_order| defines order of bytes in the buffer.
30  ByteBuffer();
31  explicit ByteBuffer(ByteOrder byte_order);
32  ByteBuffer(const char* bytes, size_t len);
33  ByteBuffer(const char* bytes, size_t len, ByteOrder byte_order);
34
35  // Initializes buffer from a zero-terminated string.
36  explicit ByteBuffer(const char* bytes);
37
38  ~ByteBuffer();
39
40  const char* Data() const { return bytes_ + start_; }
41  size_t Length() const { return end_ - start_; }
42  size_t Capacity() const { return size_ - start_; }
43  ByteOrder Order() const { return byte_order_; }
44
45  // Read a next value from the buffer. Return false if there isn't
46  // enough data left for the specified type.
47  bool ReadUInt8(uint8* val);
48  bool ReadUInt16(uint16* val);
49  bool ReadUInt24(uint32* val);
50  bool ReadUInt32(uint32* val);
51  bool ReadUInt64(uint64* val);
52  bool ReadBytes(char* val, size_t len);
53
54  // Appends next |len| bytes from the buffer to |val|. Returns false
55  // if there is less than |len| bytes left.
56  bool ReadString(std::string* val, size_t len);
57
58  // Write value to the buffer. Resizes the buffer when it is
59  // neccessary.
60  void WriteUInt8(uint8 val);
61  void WriteUInt16(uint16 val);
62  void WriteUInt24(uint32 val);
63  void WriteUInt32(uint32 val);
64  void WriteUInt64(uint64 val);
65  void WriteString(const std::string& val);
66  void WriteBytes(const char* val, size_t len);
67
68  // Reserves the given number of bytes and returns a char* that can be written
69  // into. Useful for functions that require a char* buffer and not a
70  // ByteBuffer.
71  char* ReserveWriteBuffer(size_t len);
72
73  // Resize the buffer to the specified |size|. This invalidates any remembered
74  // seek positions.
75  void Resize(size_t size);
76
77  // Moves current position |size| bytes forward. Returns false if
78  // there is less than |size| bytes left in the buffer. Consume doesn't
79  // permanently remove data, so remembered read positions are still valid
80  // after this call.
81  bool Consume(size_t size);
82
83  // Clears the contents of the buffer. After this, Length() will be 0.
84  void Clear();
85
86  // Used with GetReadPosition/SetReadPosition.
87  class ReadPosition {
88    friend class ByteBuffer;
89    ReadPosition(size_t start, int version)
90        : start_(start), version_(version) { }
91    size_t start_;
92    int version_;
93  };
94
95  // Remembers the current read position for a future SetReadPosition. Any
96  // calls to Shift or Resize in the interim will invalidate the position.
97  ReadPosition GetReadPosition() const;
98
99  // If the given position is still valid, restores that read position.
100  bool SetReadPosition(const ReadPosition &position);
101
102 private:
103  void Construct(const char* bytes, size_t size, ByteOrder byte_order);
104
105  char* bytes_;
106  size_t size_;
107  size_t start_;
108  size_t end_;
109  int version_;
110  ByteOrder byte_order_;
111
112  // There are sensible ways to define these, but they aren't needed in our code
113  // base.
114  DISALLOW_COPY_AND_ASSIGN(ByteBuffer);
115};
116
117}  // namespace rtc
118
119#endif  // WEBRTC_BASE_BYTEBUFFER_H_
120