aboutsummaryrefslogtreecommitdiff
path: root/ByteBuffer.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'ByteBuffer.cpp')
-rw-r--r--ByteBuffer.cpp23
1 files changed, 12 insertions, 11 deletions
diff --git a/ByteBuffer.cpp b/ByteBuffer.cpp
index f27deeb..a33589e 100644
--- a/ByteBuffer.cpp
+++ b/ByteBuffer.cpp
@@ -10,54 +10,55 @@ 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) {
assert(zero <= size);
+ assert(size <= capacity);
}
ByteBuffer &ByteBuffer::add8(uint8_t value) {
checkAndUpdateSize(1);
- bytes[zero + cursor++] = value;
+ bytes[cursor++] = value;
return *this;
}
ByteBuffer &ByteBuffer::add16le(uint16_t value) {
checkAndUpdateSize(2);
- bytes[zero + cursor++] = (uint8_t) (value & 0xff);
- bytes[zero + cursor++] = (uint8_t) ((value >> 8) & 0xff);
+ bytes[cursor++] = (uint8_t) (value & 0xff);
+ bytes[cursor++] = (uint8_t) ((value >> 8) & 0xff);
return *this;
}
uint8_t ByteBuffer::get8(size_t index) {
canAccessIndex(index);
- return bytes[zero + cursor];
+ return bytes[cursor];
}
uint8_t ByteBuffer::get8() {
canAccessIndex(cursor);
- return bytes[zero + cursor++];
+ return bytes[cursor++];
}
uint16_t ByteBuffer::get16le() {
canAccessIndex(cursor + 1);
uint16_t value;
- value = bytes[zero + cursor++];
- value |= ((uint16_t) bytes[zero + cursor++]) << 8;
+ value = bytes[cursor++];
+ value |= ((uint16_t) bytes[cursor++]) << 8;
return value;
}
void ByteBuffer::copy(uint8_t *bytes, size_t length) {
- canAccessIndex(cursor + length);
+ canAccessIndex(length);
- memcpy(bytes, &this->bytes[zero + cursor], length);
+ memcpy(bytes, &this->bytes[cursor], length);
cursor += length;
}
ByteBuffer ByteBuffer::view(size_t length) {
canAccessIndex(cursor + length);
- size_t s = zero + cursor + length;
+ size_t s = cursor + length;
return ByteBuffer(bytes, s, s, cursor);
}
void ByteBuffer::checkAndUpdateSize(size_t newBytes) {
- size_t newSize = zero + cursor + 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));
}