Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

help diagnostics by reporting the position, step and end of the data being decoded #171

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions Runtime/C++/src/bebop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,19 @@ enum class GuidStyle {
};

struct MalformedPacketException : public std::exception {
MalformedPacketException(size_t start_pos, size_t step, size_t length) noexcept
: std::exception()
{
char buf[200];
sprintf(buf, "malformed Bebop packet: (0x%04x,+%u,0x%04x)", (unsigned)start_pos, (unsigned)step, (unsigned)length);
msg = buf;
}

const char* what () const throw () {
return "malformed Bebop packet";
return msg.c_str();
}

std::string msg;
};

#pragma pack(push, 1)
Expand Down Expand Up @@ -213,12 +223,12 @@ class Reader {
void skip(size_t amount) { m_pointer += amount; }

uint8_t readByte() {
if (m_pointer + sizeof(uint8_t) > m_end) throw MalformedPacketException();
if (m_pointer + sizeof(uint8_t) > m_end) throw MalformedPacketException(m_pointer - m_start, sizeof(uint8_t), m_end - m_start);
return *m_pointer++;
}

uint16_t readUint16() {
if (m_pointer + sizeof(uint16_t) > m_end) throw MalformedPacketException();
if (m_pointer + sizeof(uint16_t) > m_end) throw MalformedPacketException(m_pointer - m_start, sizeof(uint16_t), m_end - m_start);
#if BEBOP_ASSUME_LITTLE_ENDIAN
uint16_t v;
memcpy(&v, m_pointer, sizeof(uint16_t));
Expand All @@ -232,7 +242,7 @@ class Reader {
}

uint32_t readUint32() {
if (m_pointer + sizeof(uint32_t) > m_end) throw MalformedPacketException();
if (m_pointer + sizeof(uint32_t) > m_end) throw MalformedPacketException(m_pointer - m_start, sizeof(uint32_t), m_end - m_start);
#if BEBOP_ASSUME_LITTLE_ENDIAN
uint32_t v;
memcpy(&v, m_pointer, sizeof(uint32_t));
Expand All @@ -248,7 +258,7 @@ class Reader {
}

uint64_t readUint64() {
if (m_pointer + sizeof(uint64_t) > m_end) throw MalformedPacketException();
if (m_pointer + sizeof(uint64_t) > m_end) throw MalformedPacketException(m_pointer - m_start, sizeof(uint64_t), m_end - m_start);
#if BEBOP_ASSUME_LITTLE_ENDIAN
uint64_t v;
memcpy(&v, m_pointer, sizeof(uint64_t));
Expand All @@ -272,15 +282,15 @@ class Reader {
int64_t readInt64() { return static_cast<uint64_t>(readUint64()); }

float readFloat32() {
if (m_pointer + sizeof(float) > m_end) throw MalformedPacketException();
if (m_pointer + sizeof(float) > m_end) throw MalformedPacketException(m_pointer - m_start, sizeof(float), m_end - m_start);
float f;
const uint32_t v = readUint32();
memcpy(&f, &v, sizeof(float));
return f;
}

double readFloat64() {
if (m_pointer + sizeof(double) > m_end) throw MalformedPacketException();
if (m_pointer + sizeof(double) > m_end) throw MalformedPacketException(m_pointer - m_start, sizeof(double), m_end - m_start);
double f;
const uint64_t v = readUint64();
memcpy(&f, &v, sizeof(double));
Expand All @@ -294,7 +304,7 @@ class Reader {
uint32_t readLengthPrefix() {
const auto length = readUint32();
if (m_pointer + length > m_end) {
throw MalformedPacketException();
throw MalformedPacketException(m_pointer - m_start, length, m_end - m_start);
}
return length;
}
Expand All @@ -314,7 +324,7 @@ class Reader {
}

Guid readGuid() {
if (m_pointer + sizeof(Guid) > m_end) throw MalformedPacketException();
if (m_pointer + sizeof(Guid) > m_end) throw MalformedPacketException(m_pointer - m_start, sizeof(Guid), m_end - m_start);
Guid guid { m_pointer };
m_pointer += sizeof(Guid);
return guid;
Expand Down