blob: cd7b2c6cea8e73231ce8fd43fef75bcd0f0f393f [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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"
Karl Wiberg94784372015-04-20 14:03:07 +020017#include "webrtc/base/buffer.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000018#include "webrtc/base/constructormagic.h"
19
20namespace rtc {
21
22class ByteBuffer {
23 public:
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000024 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
jbauchf1f87202016-03-30 06:43:37 -070029 explicit ByteBuffer(ByteOrder byte_order) : byte_order_(byte_order) {}
30
31 ByteOrder Order() const { return byte_order_; }
32
33 private:
34 ByteOrder byte_order_;
35
36 RTC_DISALLOW_COPY_AND_ASSIGN(ByteBuffer);
37};
38
39class ByteBufferWriter : public ByteBuffer {
40 public:
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000041 // |byte_order| defines order of bytes in the buffer.
jbauchf1f87202016-03-30 06:43:37 -070042 ByteBufferWriter();
43 explicit ByteBufferWriter(ByteOrder byte_order);
44 ByteBufferWriter(const char* bytes, size_t len);
45 ByteBufferWriter(const char* bytes, size_t len, ByteOrder byte_order);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000046
jbauchf1f87202016-03-30 06:43:37 -070047 ~ByteBufferWriter();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000048
49 const char* Data() const { return bytes_ + start_; }
50 size_t Length() const { return end_ - start_; }
51 size_t Capacity() const { return size_ - start_; }
jbauchf1f87202016-03-30 06:43:37 -070052
53 // Write value to the buffer. Resizes the buffer when it is
54 // neccessary.
55 void WriteUInt8(uint8_t val);
56 void WriteUInt16(uint16_t val);
57 void WriteUInt24(uint32_t val);
58 void WriteUInt32(uint32_t val);
59 void WriteUInt64(uint64_t val);
mikescarlett9a20fa62016-04-11 16:11:38 -070060 void WriteUVarint(uint64_t val);
jbauchf1f87202016-03-30 06:43:37 -070061 void WriteString(const std::string& val);
62 void WriteBytes(const char* val, size_t len);
63
64 // Reserves the given number of bytes and returns a char* that can be written
65 // into. Useful for functions that require a char* buffer and not a
66 // ByteBufferWriter.
67 char* ReserveWriteBuffer(size_t len);
68
69 // Resize the buffer to the specified |size|.
70 void Resize(size_t size);
71
72 // Clears the contents of the buffer. After this, Length() will be 0.
73 void Clear();
74
75 private:
76 void Construct(const char* bytes, size_t size);
77
78 char* bytes_;
79 size_t size_;
80 size_t start_;
81 size_t end_;
82
83 // There are sensible ways to define these, but they aren't needed in our code
84 // base.
85 RTC_DISALLOW_COPY_AND_ASSIGN(ByteBufferWriter);
86};
87
88// The ByteBufferReader references the passed data, i.e. the pointer must be
89// valid during the lifetime of the reader.
90class ByteBufferReader : public ByteBuffer {
91 public:
92 ByteBufferReader(const char* bytes, size_t len);
93 ByteBufferReader(const char* bytes, size_t len, ByteOrder byte_order);
94
95 // Initializes buffer from a zero-terminated string.
96 explicit ByteBufferReader(const char* bytes);
97
98 explicit ByteBufferReader(const Buffer& buf);
99
100 explicit ByteBufferReader(const ByteBufferWriter& buf);
101
102 // Returns start of unprocessed data.
103 const char* Data() const { return bytes_ + start_; }
104 // Returns number of unprocessed bytes.
105 size_t Length() const { return end_ - start_; }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000106
107 // Read a next value from the buffer. Return false if there isn't
108 // enough data left for the specified type.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200109 bool ReadUInt8(uint8_t* val);
110 bool ReadUInt16(uint16_t* val);
111 bool ReadUInt24(uint32_t* val);
112 bool ReadUInt32(uint32_t* val);
113 bool ReadUInt64(uint64_t* val);
mikescarlett9a20fa62016-04-11 16:11:38 -0700114 bool ReadUVarint(uint64_t* val);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000115 bool ReadBytes(char* val, size_t len);
116
117 // Appends next |len| bytes from the buffer to |val|. Returns false
118 // if there is less than |len| bytes left.
119 bool ReadString(std::string* val, size_t len);
120
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000121 // Moves current position |size| bytes forward. Returns false if
122 // there is less than |size| bytes left in the buffer. Consume doesn't
123 // permanently remove data, so remembered read positions are still valid
124 // after this call.
125 bool Consume(size_t size);
126
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000127 private:
jbauchf1f87202016-03-30 06:43:37 -0700128 void Construct(const char* bytes, size_t size);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000129
jbauchf1f87202016-03-30 06:43:37 -0700130 const char* bytes_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000131 size_t size_;
132 size_t start_;
133 size_t end_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000134
jbauchf1f87202016-03-30 06:43:37 -0700135 RTC_DISALLOW_COPY_AND_ASSIGN(ByteBufferReader);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000136};
137
138} // namespace rtc
139
140#endif // WEBRTC_BASE_BYTEBUFFER_H_