3 * Copyright 2004--2005, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "talk/base/bytebuffer.h"
34 #include "talk/base/basictypes.h"
35 #include "talk/base/byteorder.h"
39 static const int DEFAULT_SIZE = 4096;
41 ByteBuffer::ByteBuffer() {
42 Construct(NULL, DEFAULT_SIZE, ORDER_NETWORK);
45 ByteBuffer::ByteBuffer(ByteOrder byte_order) {
46 Construct(NULL, DEFAULT_SIZE, byte_order);
49 ByteBuffer::ByteBuffer(const char* bytes, size_t len) {
50 Construct(bytes, len, ORDER_NETWORK);
53 ByteBuffer::ByteBuffer(const char* bytes, size_t len, ByteOrder byte_order) {
54 Construct(bytes, len, byte_order);
57 ByteBuffer::ByteBuffer(const char* bytes) {
58 Construct(bytes, strlen(bytes), ORDER_NETWORK);
61 void ByteBuffer::Construct(const char* bytes, size_t len,
62 ByteOrder byte_order) {
66 byte_order_ = byte_order;
67 bytes_ = new char[size_];
71 memcpy(bytes_, bytes, end_);
77 ByteBuffer::~ByteBuffer() {
81 bool ByteBuffer::ReadUInt8(uint8* val) {
82 if (!val) return false;
84 return ReadBytes(reinterpret_cast<char*>(val), 1);
87 bool ByteBuffer::ReadUInt16(uint16* val) {
88 if (!val) return false;
91 if (!ReadBytes(reinterpret_cast<char*>(&v), 2)) {
94 *val = (byte_order_ == ORDER_NETWORK) ? NetworkToHost16(v) : v;
99 bool ByteBuffer::ReadUInt24(uint32* val) {
100 if (!val) return false;
103 char* read_into = reinterpret_cast<char*>(&v);
104 if (byte_order_ == ORDER_NETWORK || IsHostBigEndian()) {
108 if (!ReadBytes(read_into, 3)) {
111 *val = (byte_order_ == ORDER_NETWORK) ? NetworkToHost32(v) : v;
116 bool ByteBuffer::ReadUInt32(uint32* val) {
117 if (!val) return false;
120 if (!ReadBytes(reinterpret_cast<char*>(&v), 4)) {
123 *val = (byte_order_ == ORDER_NETWORK) ? NetworkToHost32(v) : v;
128 bool ByteBuffer::ReadUInt64(uint64* val) {
129 if (!val) return false;
132 if (!ReadBytes(reinterpret_cast<char*>(&v), 8)) {
135 *val = (byte_order_ == ORDER_NETWORK) ? NetworkToHost64(v) : v;
140 bool ByteBuffer::ReadString(std::string* val, size_t len) {
141 if (!val) return false;
143 if (len > Length()) {
146 val->append(bytes_ + start_, len);
152 bool ByteBuffer::ReadBytes(char* val, size_t len) {
153 if (len > Length()) {
156 memcpy(val, bytes_ + start_, len);
162 void ByteBuffer::WriteUInt8(uint8 val) {
163 WriteBytes(reinterpret_cast<const char*>(&val), 1);
166 void ByteBuffer::WriteUInt16(uint16 val) {
167 uint16 v = (byte_order_ == ORDER_NETWORK) ? HostToNetwork16(val) : val;
168 WriteBytes(reinterpret_cast<const char*>(&v), 2);
171 void ByteBuffer::WriteUInt24(uint32 val) {
172 uint32 v = (byte_order_ == ORDER_NETWORK) ? HostToNetwork32(val) : val;
173 char* start = reinterpret_cast<char*>(&v);
174 if (byte_order_ == ORDER_NETWORK || IsHostBigEndian()) {
177 WriteBytes(start, 3);
180 void ByteBuffer::WriteUInt32(uint32 val) {
181 uint32 v = (byte_order_ == ORDER_NETWORK) ? HostToNetwork32(val) : val;
182 WriteBytes(reinterpret_cast<const char*>(&v), 4);
185 void ByteBuffer::WriteUInt64(uint64 val) {
186 uint64 v = (byte_order_ == ORDER_NETWORK) ? HostToNetwork64(val) : val;
187 WriteBytes(reinterpret_cast<const char*>(&v), 8);
190 void ByteBuffer::WriteString(const std::string& val) {
191 WriteBytes(val.c_str(), val.size());
194 void ByteBuffer::WriteBytes(const char* val, size_t len) {
195 memcpy(ReserveWriteBuffer(len), val, len);
198 char* ByteBuffer::ReserveWriteBuffer(size_t len) {
199 if (Length() + len > Capacity())
200 Resize(Length() + len);
202 char* start = bytes_ + end_;
207 void ByteBuffer::Resize(size_t size) {
208 size_t len = _min(end_ - start_, size);
210 // Don't reallocate, just move data backwards
211 memmove(bytes_, bytes_ + start_, len);
213 // Reallocate a larger buffer.
214 size_ = _max(size, 3 * size_ / 2);
215 char* new_bytes = new char[size_];
216 memcpy(new_bytes, bytes_ + start_, len);
225 bool ByteBuffer::Consume(size_t size) {
232 ByteBuffer::ReadPosition ByteBuffer::GetReadPosition() const {
233 return ReadPosition(start_, version_);
236 bool ByteBuffer::SetReadPosition(const ReadPosition &position) {
237 if (position.version_ != version_) {
240 start_ = position.start_;
244 void ByteBuffer::Clear() {
245 memset(bytes_, 0, size_);
250 } // namespace talk_base