2015-06-21 21:43:05 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
/* Copyright (c) FIRST 2015. All Rights Reserved. */
|
|
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#ifndef NT_WIREDECODER_H_
|
|
|
|
|
#define NT_WIREDECODER_H_
|
|
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
|
|
|
|
|
|
#include "ntcore.h"
|
2015-06-25 23:12:49 -07:00
|
|
|
#include "leb128.h"
|
|
|
|
|
#include "raw_istream.h"
|
2015-06-21 21:43:05 -07:00
|
|
|
|
2015-06-25 22:57:43 -07:00
|
|
|
namespace ntimpl {
|
|
|
|
|
|
|
|
|
|
class WireDecoder {
|
|
|
|
|
public:
|
|
|
|
|
explicit WireDecoder(raw_istream& is, unsigned int proto_rev);
|
|
|
|
|
~WireDecoder();
|
|
|
|
|
|
2015-06-25 23:25:29 -07:00
|
|
|
void set_proto_rev(unsigned int proto_rev) { m_proto_rev = proto_rev; }
|
2015-06-25 22:57:43 -07:00
|
|
|
|
|
|
|
|
void Reset() { m_error = nullptr; }
|
|
|
|
|
|
2015-06-25 23:25:29 -07:00
|
|
|
const char* error() const { return m_error; }
|
2015-06-25 22:57:43 -07:00
|
|
|
|
|
|
|
|
bool Read(char** buf, std::size_t len) {
|
|
|
|
|
if (len > m_allocated) Realloc(len);
|
|
|
|
|
*buf = m_buf;
|
|
|
|
|
return m_is.read(m_buf, len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Read8(unsigned int* val) {
|
|
|
|
|
char* buf;
|
|
|
|
|
if (!Read(&buf, 1)) return false;
|
|
|
|
|
*val = (*((unsigned char*)buf)) & 0xff;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Read16(unsigned int* val) {
|
|
|
|
|
char* buf;
|
|
|
|
|
if (!Read(&buf, 2)) return false;
|
|
|
|
|
unsigned int v = (*((unsigned char*)buf)) & 0xff;
|
|
|
|
|
++buf;
|
|
|
|
|
v <<= 8;
|
|
|
|
|
v |= (*((unsigned char*)buf)) & 0xff;
|
|
|
|
|
*val = v;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Read32(unsigned long* val) {
|
|
|
|
|
char* buf;
|
|
|
|
|
if (!Read(&buf, 4)) return false;
|
|
|
|
|
unsigned int v = (*((unsigned char*)buf)) & 0xff;
|
|
|
|
|
++buf;
|
|
|
|
|
v <<= 8;
|
|
|
|
|
v |= (*((unsigned char*)buf)) & 0xff;
|
|
|
|
|
++buf;
|
|
|
|
|
v <<= 8;
|
|
|
|
|
v |= (*((unsigned char*)buf)) & 0xff;
|
|
|
|
|
++buf;
|
|
|
|
|
v <<= 8;
|
|
|
|
|
v |= (*((unsigned char*)buf)) & 0xff;
|
|
|
|
|
*val = v;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ReadDouble(double* val);
|
|
|
|
|
|
2015-06-25 23:19:24 -07:00
|
|
|
bool ReadUleb128(unsigned long* val) {
|
|
|
|
|
return ntimpl::ReadUleb128(m_is, val);
|
|
|
|
|
}
|
2015-06-25 22:57:43 -07:00
|
|
|
|
|
|
|
|
bool ReadType(NT_Type* type);
|
|
|
|
|
bool ReadValue(NT_Type type, NT_Value* value);
|
|
|
|
|
bool ReadString(NT_String* str);
|
|
|
|
|
|
|
|
|
|
WireDecoder(const WireDecoder&) = delete;
|
|
|
|
|
WireDecoder& operator=(const WireDecoder&) = delete;
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
unsigned int m_proto_rev;
|
|
|
|
|
const char* m_error;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
void Realloc(std::size_t len);
|
|
|
|
|
|
|
|
|
|
raw_istream& m_is;
|
|
|
|
|
|
|
|
|
|
char* m_buf;
|
|
|
|
|
std::size_t m_allocated;
|
2015-06-21 21:43:05 -07:00
|
|
|
};
|
|
|
|
|
|
2015-06-25 22:57:43 -07:00
|
|
|
} // namespace ntimpl
|
2015-06-21 21:43:05 -07:00
|
|
|
|
2015-06-25 22:57:43 -07:00
|
|
|
#endif // NT_WIREDECODER_H_
|