diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2015-02-17 20:14:34 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2015-02-17 23:30:04 +0100 |
commit | 254be95ff2f37df8adec7ce068448ba8abc8d734 (patch) | |
tree | f1483c2ff4c2e194cf90c59ce741d96917f7aaf5 /ByteBuffer.cpp | |
parent | 0d0543e4daeb1c6b01d4799736026b6f3aef9779 (diff) | |
parent | 0d0749ff0f842f10fea6929dc466e4e1be458234 (diff) | |
download | ble-toys-254be95ff2f37df8adec7ce068448ba8abc8d734.tar.gz ble-toys-254be95ff2f37df8adec7ce068448ba8abc8d734.tar.bz2 ble-toys-254be95ff2f37df8adec7ce068448ba8abc8d734.tar.xz ble-toys-254be95ff2f37df8adec7ce068448ba8abc8d734.zip |
Merge remote-tracking branch 'origin/master'
Conflicts:
Bluetooth.cpp
Bluetooth.h
ByteBuffer.cpp
CMakeLists.txt
LinuxBluetooth.cpp
main.cpp
test/ByteBufferTest.cpp
Diffstat (limited to 'ByteBuffer.cpp')
-rw-r--r-- | ByteBuffer.cpp | 86 |
1 files changed, 53 insertions, 33 deletions
diff --git a/ByteBuffer.cpp b/ByteBuffer.cpp index a33589e..2d4a258 100644 --- a/ByteBuffer.cpp +++ b/ByteBuffer.cpp @@ -1,82 +1,102 @@ #include "ByteBuffer.h" -#include <stdint-gcc.h> #include <string.h> #include <sstream> #include <iomanip> -#include <cassert> using namespace std; -ByteBuffer::ByteBuffer(uint8_t *bytes, size_t capacity, size_t size, size_t zero) : - bytes(bytes), capacity(capacity), size(size), cursor(zero), zero(zero) { +ByteBuffer::ByteBuffer(const uint8_t *bytes, size_t capacity) : + bytes(bytes), capacity(capacity), zero(&bytes[0]), end(&bytes[capacity]) { + ptr = (uint8_t *) &bytes[0]; +} + +ByteBuffer::ByteBuffer(const uint8_t *bytes, size_t capacity, size_t zero, size_t size) : + bytes(bytes), capacity(capacity), zero(&bytes[0]), end(&bytes[size]) { assert(zero <= size); assert(size <= capacity); + ptr = (uint8_t *) &bytes[0]; +} + +ByteBuffer::ByteBuffer(const uint8_t *bytes, size_t capacity, const uint8_t *zero, const uint8_t *end) : + bytes(bytes), capacity(capacity), zero(zero), end(end), ptr((uint8_t *) zero) { } ByteBuffer &ByteBuffer::add8(uint8_t value) { - checkAndUpdateSize(1); - bytes[cursor++] = value; + checkAndUpdateEnd(1); + (*ptr++) = value; return *this; } ByteBuffer &ByteBuffer::add16le(uint16_t value) { - checkAndUpdateSize(2); - bytes[cursor++] = (uint8_t) (value & 0xff); - bytes[cursor++] = (uint8_t) ((value >> 8) & 0xff); + checkAndUpdateEnd(2); + (*ptr++) = (uint8_t) (value & 0xff); + (*ptr++) = (uint8_t) ((value >> 8) & 0xff); return *this; } uint8_t ByteBuffer::get8(size_t index) { - canAccessIndex(index); - return bytes[cursor]; + assertCanAccessRelative(index); + return *ptr++; } uint8_t ByteBuffer::get8() { - canAccessIndex(cursor); - return bytes[cursor++]; + assertCanAccessRelative(0); + return *ptr++; } uint16_t ByteBuffer::get16le() { - canAccessIndex(cursor + 1); + assertCanAccessRelative(0); uint16_t value; - value = bytes[cursor++]; - value |= ((uint16_t) bytes[cursor++]) << 8; + value = *ptr++; + value |= ((uint16_t) *ptr++) << 8; return value; } void ByteBuffer::copy(uint8_t *bytes, size_t length) { - canAccessIndex(length); + assertCanAccessRelative(length); - memcpy(bytes, &this->bytes[cursor], length); - cursor += length; + memcpy(bytes, ptr, length); + ptr += length; } -ByteBuffer ByteBuffer::view(size_t length) { - canAccessIndex(cursor + length); - size_t s = cursor + length; - return ByteBuffer(bytes, s, s, cursor); +ByteBuffer ByteBuffer::view() const { +// DF << "cursor=" << getCursor() << ", size=" << getSize() << ", new size=" << end - ptr << ", ptr=" << (uint64_t) ptr << ", zero=" << (uint64_t) zero; + return view(ptr, end); } -void ByteBuffer::checkAndUpdateSize(size_t newBytes) { - size_t newSize = cursor + newBytes; - if (newSize >= capacity) { - throw ByteBufferException(string("Out of bounds! zero=") + to_string(zero) + ", cursor=" + to_string(cursor) + ", size=" + to_string(size) + ", capacity=" + to_string(capacity) + ", newSize=" + to_string(newSize)); +ByteBuffer ByteBuffer::view(size_t length) const { + return ByteBuffer(bytes, length, ptr, ptr + length); +} + +ByteBuffer ByteBuffer::view(uint8_t *ptr, const uint8_t *end) const { + return ByteBuffer(bytes, end - ptr, ptr, end); +} + +void ByteBuffer::checkAndUpdateEnd(size_t newBytes) { + uint8_t *newPtr = ptr + newBytes; + if (newPtr >= end) { + if (newPtr >= &zero[capacity]) { + throw ByteBufferException(string("New size is too large! cursor=") + to_string(getCursor()) + ", size=" + to_string(getSize()) + ", capacity=" + to_string(capacity)); + } + end = newPtr; } +} - size = max(newSize, size); +void ByteBuffer::assertCanAccessRelative(size_t diff) const { + assertCanAccessIndex(ptr + diff); } -void ByteBuffer::canAccessIndex(size_t index) { - if (zero + index >= size) { - throw ByteBufferException(string("Out of bounds! zero=") + to_string(zero) + ", index=" + to_string(index) + ", size=" + to_string(size)); +void ByteBuffer::assertCanAccessIndex(uint8_t *p) const { + if (p >= end || p < zero) { + throw ByteBufferException(string("Out of bounds! size=") + to_string(getSize()) + ", index=" + to_string(p - zero)); } } std::string ByteBuffer::toString() const { stringstream s; - for(size_t i = zero; i < size; i++) { - s << hex << setfill('0') << setw(2) << (int) bytes[i] << " "; + for (uint8_t *i = (uint8_t *) zero; i < end; i++) { + s << hex << setfill('0') << setw(2) << (int) *i << " "; } return string(s.str()); |