2014-05-13 18:00:26 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright 2004 The WebRTC Project Authors. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "webrtc/base/bytebuffer.h"
|
|
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
|
|
#include "webrtc/base/basictypes.h"
|
|
|
|
|
#include "webrtc/base/byteorder.h"
|
|
|
|
|
|
|
|
|
|
namespace rtc {
|
|
|
|
|
|
|
|
|
|
static const int DEFAULT_SIZE = 4096;
|
|
|
|
|
|
|
|
|
|
ByteBuffer::ByteBuffer() {
|
|
|
|
|
Construct(NULL, DEFAULT_SIZE, ORDER_NETWORK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ByteBuffer::ByteBuffer(ByteOrder byte_order) {
|
|
|
|
|
Construct(NULL, DEFAULT_SIZE, byte_order);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ByteBuffer::ByteBuffer(const char* bytes, size_t len) {
|
|
|
|
|
Construct(bytes, len, ORDER_NETWORK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ByteBuffer::ByteBuffer(const char* bytes, size_t len, ByteOrder byte_order) {
|
|
|
|
|
Construct(bytes, len, byte_order);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ByteBuffer::ByteBuffer(const char* bytes) {
|
|
|
|
|
Construct(bytes, strlen(bytes), ORDER_NETWORK);
|
|
|
|
|
}
|
|
|
|
|
|
rtc::Buffer improvements
1. Constructors, SetData(), and AppendData() now accept uint8_t*,
int8_t*, and char*. Previously, they accepted void*, meaning that
any kind of pointer was accepted. I think requiring an explicit
cast in cases where the input array isn't already of a byte-sized
type is a better compromise between convenience and safety.
2. data() can now return a uint8_t* instead of a char*, which seems
more appropriate for a byte array, and is harder to mix up with
zero-terminated C strings. data<int8_t>() is also available so
that callers that want that type instead won't have to cast, as
is data<char>() (which remains the default until all existing
callers have been fixed).
3. Constructors, SetData(), and AppendData() now accept arrays
natively, not just decayed to pointers. The advantage of this is
that callers don't have to pass the size separately.
4. There are new constructors that allow setting size and capacity
without initializing the array. Previously, this had to be done
separately after construction.
5. Instead of TransferTo(), Buffer now supports swap(), and move
construction and assignment, and has a Pass() method that works
just like std::move(). (The Pass method is modeled after
scoped_ptr::Pass().)
R=jmarusic@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/42989004
Cr-Commit-Position: refs/heads/master@{#9033}
2015-04-20 14:03:07 +02:00
|
|
|
ByteBuffer::ByteBuffer(const Buffer& buf) {
|
|
|
|
|
Construct(buf.data<char>(), buf.size(), ORDER_NETWORK);
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-13 18:00:26 +00:00
|
|
|
void ByteBuffer::Construct(const char* bytes, size_t len,
|
|
|
|
|
ByteOrder byte_order) {
|
|
|
|
|
version_ = 0;
|
|
|
|
|
start_ = 0;
|
|
|
|
|
size_ = len;
|
|
|
|
|
byte_order_ = byte_order;
|
|
|
|
|
bytes_ = new char[size_];
|
|
|
|
|
|
|
|
|
|
if (bytes) {
|
|
|
|
|
end_ = len;
|
|
|
|
|
memcpy(bytes_, bytes, end_);
|
|
|
|
|
} else {
|
|
|
|
|
end_ = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ByteBuffer::~ByteBuffer() {
|
|
|
|
|
delete[] bytes_;
|
|
|
|
|
}
|
|
|
|
|
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
bool ByteBuffer::ReadUInt8(uint8_t* val) {
|
2014-05-13 18:00:26 +00:00
|
|
|
if (!val) return false;
|
|
|
|
|
|
|
|
|
|
return ReadBytes(reinterpret_cast<char*>(val), 1);
|
|
|
|
|
}
|
|
|
|
|
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
bool ByteBuffer::ReadUInt16(uint16_t* val) {
|
2014-05-13 18:00:26 +00:00
|
|
|
if (!val) return false;
|
|
|
|
|
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
uint16_t v;
|
2014-05-13 18:00:26 +00:00
|
|
|
if (!ReadBytes(reinterpret_cast<char*>(&v), 2)) {
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
*val = (byte_order_ == ORDER_NETWORK) ? NetworkToHost16(v) : v;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
bool ByteBuffer::ReadUInt24(uint32_t* val) {
|
2014-05-13 18:00:26 +00:00
|
|
|
if (!val) return false;
|
|
|
|
|
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
uint32_t v = 0;
|
2014-05-13 18:00:26 +00:00
|
|
|
char* read_into = reinterpret_cast<char*>(&v);
|
|
|
|
|
if (byte_order_ == ORDER_NETWORK || IsHostBigEndian()) {
|
|
|
|
|
++read_into;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!ReadBytes(read_into, 3)) {
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
*val = (byte_order_ == ORDER_NETWORK) ? NetworkToHost32(v) : v;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
bool ByteBuffer::ReadUInt32(uint32_t* val) {
|
2014-05-13 18:00:26 +00:00
|
|
|
if (!val) return false;
|
|
|
|
|
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
uint32_t v;
|
2014-05-13 18:00:26 +00:00
|
|
|
if (!ReadBytes(reinterpret_cast<char*>(&v), 4)) {
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
*val = (byte_order_ == ORDER_NETWORK) ? NetworkToHost32(v) : v;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
bool ByteBuffer::ReadUInt64(uint64_t* val) {
|
2014-05-13 18:00:26 +00:00
|
|
|
if (!val) return false;
|
|
|
|
|
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
uint64_t v;
|
2014-05-13 18:00:26 +00:00
|
|
|
if (!ReadBytes(reinterpret_cast<char*>(&v), 8)) {
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
*val = (byte_order_ == ORDER_NETWORK) ? NetworkToHost64(v) : v;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ByteBuffer::ReadString(std::string* val, size_t len) {
|
|
|
|
|
if (!val) return false;
|
|
|
|
|
|
|
|
|
|
if (len > Length()) {
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
val->append(bytes_ + start_, len);
|
|
|
|
|
start_ += len;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ByteBuffer::ReadBytes(char* val, size_t len) {
|
|
|
|
|
if (len > Length()) {
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
memcpy(val, bytes_ + start_, len);
|
|
|
|
|
start_ += len;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
void ByteBuffer::WriteUInt8(uint8_t val) {
|
2014-05-13 18:00:26 +00:00
|
|
|
WriteBytes(reinterpret_cast<const char*>(&val), 1);
|
|
|
|
|
}
|
|
|
|
|
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
void ByteBuffer::WriteUInt16(uint16_t val) {
|
|
|
|
|
uint16_t v = (byte_order_ == ORDER_NETWORK) ? HostToNetwork16(val) : val;
|
2014-05-13 18:00:26 +00:00
|
|
|
WriteBytes(reinterpret_cast<const char*>(&v), 2);
|
|
|
|
|
}
|
|
|
|
|
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
void ByteBuffer::WriteUInt24(uint32_t val) {
|
|
|
|
|
uint32_t v = (byte_order_ == ORDER_NETWORK) ? HostToNetwork32(val) : val;
|
2014-05-13 18:00:26 +00:00
|
|
|
char* start = reinterpret_cast<char*>(&v);
|
|
|
|
|
if (byte_order_ == ORDER_NETWORK || IsHostBigEndian()) {
|
|
|
|
|
++start;
|
|
|
|
|
}
|
|
|
|
|
WriteBytes(start, 3);
|
|
|
|
|
}
|
|
|
|
|
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
void ByteBuffer::WriteUInt32(uint32_t val) {
|
|
|
|
|
uint32_t v = (byte_order_ == ORDER_NETWORK) ? HostToNetwork32(val) : val;
|
2014-05-13 18:00:26 +00:00
|
|
|
WriteBytes(reinterpret_cast<const char*>(&v), 4);
|
|
|
|
|
}
|
|
|
|
|
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
void ByteBuffer::WriteUInt64(uint64_t val) {
|
|
|
|
|
uint64_t v = (byte_order_ == ORDER_NETWORK) ? HostToNetwork64(val) : val;
|
2014-05-13 18:00:26 +00:00
|
|
|
WriteBytes(reinterpret_cast<const char*>(&v), 8);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ByteBuffer::WriteString(const std::string& val) {
|
|
|
|
|
WriteBytes(val.c_str(), val.size());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ByteBuffer::WriteBytes(const char* val, size_t len) {
|
|
|
|
|
memcpy(ReserveWriteBuffer(len), val, len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char* ByteBuffer::ReserveWriteBuffer(size_t len) {
|
|
|
|
|
if (Length() + len > Capacity())
|
|
|
|
|
Resize(Length() + len);
|
|
|
|
|
|
|
|
|
|
char* start = bytes_ + end_;
|
|
|
|
|
end_ += len;
|
|
|
|
|
return start;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ByteBuffer::Resize(size_t size) {
|
2015-02-12 11:54:26 +00:00
|
|
|
size_t len = std::min(end_ - start_, size);
|
2014-05-13 18:00:26 +00:00
|
|
|
if (size <= size_) {
|
|
|
|
|
// Don't reallocate, just move data backwards
|
|
|
|
|
memmove(bytes_, bytes_ + start_, len);
|
|
|
|
|
} else {
|
|
|
|
|
// Reallocate a larger buffer.
|
2015-02-12 11:54:26 +00:00
|
|
|
size_ = std::max(size, 3 * size_ / 2);
|
2014-05-13 18:00:26 +00:00
|
|
|
char* new_bytes = new char[size_];
|
|
|
|
|
memcpy(new_bytes, bytes_ + start_, len);
|
|
|
|
|
delete [] bytes_;
|
|
|
|
|
bytes_ = new_bytes;
|
|
|
|
|
}
|
|
|
|
|
start_ = 0;
|
|
|
|
|
end_ = len;
|
|
|
|
|
++version_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ByteBuffer::Consume(size_t size) {
|
|
|
|
|
if (size > Length())
|
|
|
|
|
return false;
|
|
|
|
|
start_ += size;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ByteBuffer::ReadPosition ByteBuffer::GetReadPosition() const {
|
|
|
|
|
return ReadPosition(start_, version_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ByteBuffer::SetReadPosition(const ReadPosition &position) {
|
|
|
|
|
if (position.version_ != version_) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
start_ = position.start_;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ByteBuffer::Clear() {
|
|
|
|
|
memset(bytes_, 0, size_);
|
|
|
|
|
start_ = end_ = 0;
|
|
|
|
|
++version_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace rtc
|