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