2020-09-13 21:32:55 -04:00
|
|
|
#pragma once
|
|
|
|
|
2020-11-21 12:52:38 -05:00
|
|
|
/*!
|
|
|
|
* @file BinaryReader.h
|
|
|
|
* Read raw data like a stream.
|
|
|
|
*/
|
2020-08-22 23:30:17 -04:00
|
|
|
|
|
|
|
#include <cstdint>
|
2021-07-18 19:28:56 -04:00
|
|
|
#include <cstring>
|
2020-08-22 23:30:17 -04:00
|
|
|
#include <vector>
|
2022-06-22 23:37:46 -04:00
|
|
|
|
2022-01-02 18:02:10 -05:00
|
|
|
#include "common/common_types.h"
|
2022-02-08 19:02:47 -05:00
|
|
|
#include "common/util/Assert.h"
|
2020-08-22 23:30:17 -04:00
|
|
|
|
|
|
|
class BinaryReader {
|
2020-08-26 01:21:33 -04:00
|
|
|
public:
|
2021-07-18 19:28:56 -04:00
|
|
|
explicit BinaryReader(const std::vector<uint8_t>& _buffer) : m_buffer(_buffer) {}
|
2020-08-22 23:30:17 -04:00
|
|
|
|
2020-08-26 01:21:33 -04:00
|
|
|
template <typename T>
|
2020-08-22 23:30:17 -04:00
|
|
|
T read() {
|
2022-02-08 19:02:47 -05:00
|
|
|
ASSERT(m_seek + sizeof(T) <= m_buffer.size());
|
2021-07-18 19:28:56 -04:00
|
|
|
T obj;
|
|
|
|
memcpy(&obj, m_buffer.data() + m_seek, sizeof(T));
|
|
|
|
m_seek += sizeof(T);
|
2020-08-22 23:30:17 -04:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ffwd(int amount) {
|
2021-07-18 19:28:56 -04:00
|
|
|
m_seek += amount;
|
2022-02-08 19:02:47 -05:00
|
|
|
ASSERT(m_seek <= m_buffer.size());
|
2020-08-22 23:30:17 -04:00
|
|
|
}
|
|
|
|
|
2021-07-18 19:28:56 -04:00
|
|
|
uint32_t bytes_left() const { return m_buffer.size() - m_seek; }
|
|
|
|
uint8_t* here() { return m_buffer.data() + m_seek; }
|
|
|
|
uint32_t get_seek() const { return m_seek; }
|
|
|
|
void set_seek(u32 seek) { m_seek = seek; }
|
2020-08-22 23:30:17 -04:00
|
|
|
|
2020-08-26 01:21:33 -04:00
|
|
|
private:
|
2021-07-18 19:28:56 -04:00
|
|
|
std::vector<u8> m_buffer;
|
|
|
|
uint32_t m_seek = 0;
|
2020-08-22 23:30:17 -04:00
|
|
|
};
|