8 #ifndef NT_WIREDECODER_H_
9 #define NT_WIREDECODER_H_
14 #include "support/leb128.h"
15 #include "support/raw_istream.h"
29 explicit WireDecoder(wpi::raw_istream& is,
unsigned int proto_rev);
32 void set_proto_rev(
unsigned int proto_rev) { m_proto_rev = proto_rev; }
35 unsigned int proto_rev()
const {
return m_proto_rev; }
38 void Reset() { m_error =
nullptr; }
43 const char* error()
const {
return m_error; }
45 void set_error(
const char* error) { m_error = error; }
52 bool Read(
const char** buf, std::size_t len) {
53 if (len > m_allocated) Realloc(len);
55 m_is.read(m_buf, len);
57 nt::Logger& logger = nt::Logger::GetInstance();
58 if (logger.min_level() <= NT_LOG_DEBUG4 && logger.HasLogger()) {
59 std::ostringstream oss;
60 oss <<
"read " << len <<
" bytes:" << std::hex;
64 for (std::size_t i=0; i < len; ++i)
65 oss <<
' ' << (
unsigned int)((*buf)[i]);
67 logger.Log(NT_LOG_DEBUG4, __FILE__, __LINE__, oss.str().c_str());
70 return !m_is.has_error();
74 bool Read8(
unsigned int* val) {
76 if (!Read(&buf, 1))
return false;
77 *val = (*
reinterpret_cast<const unsigned char*
>(buf)) & 0xff;
82 bool Read16(
unsigned int* val) {
84 if (!Read(&buf, 2))
return false;
85 unsigned int v = (*
reinterpret_cast<const unsigned char*
>(buf)) & 0xff;
88 v |= (*
reinterpret_cast<const unsigned char*
>(buf)) & 0xff;
94 bool Read32(
unsigned long* val) {
96 if (!Read(&buf, 4))
return false;
97 unsigned int v = (*
reinterpret_cast<const unsigned char*
>(buf)) & 0xff;
100 v |= (*
reinterpret_cast<const unsigned char*
>(buf)) & 0xff;
103 v |= (*
reinterpret_cast<const unsigned char*
>(buf)) & 0xff;
106 v |= (*
reinterpret_cast<const unsigned char*
>(buf)) & 0xff;
112 bool ReadDouble(
double* val);
115 bool ReadUleb128(
unsigned long* val) {
return wpi::ReadUleb128(m_is, val); }
117 bool ReadType(NT_Type* type);
118 bool ReadString(std::string* str);
119 std::shared_ptr<Value> ReadValue(NT_Type type);
126 unsigned int m_proto_rev;
133 void Realloc(std::size_t len);
136 wpi::raw_istream& m_is;
142 std::size_t m_allocated;
147 #endif // NT_WIREDECODER_H_
Definition: WireDecoder.h:27