forked from rarten/ooz
-
Notifications
You must be signed in to change notification settings - Fork 8
/
util.h
57 lines (48 loc) · 1.17 KB
/
util.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#pragma once
#include <cstring>
#include <filesystem>
#include <string>
#include <vector>
std::string hex_dump(size_t width, void const* data, size_t size);
bool slurp_file(std::filesystem::path path, std::vector<uint8_t>& data);
bool dump_file(std::filesystem::path filename, void const* data, size_t size);
struct reader {
reader(void const* p, size_t n) : reader(reinterpret_cast<uint8_t const*>(p), n) {}
template <typename T>
reader(T const* p, size_t n) : p_(reinterpret_cast<uint8_t const*>(p)), n_(n * sizeof(T)) {}
template <typename T>
bool read(T& t) {
size_t const k = sizeof(T);
if (n_ < k) {
return false;
}
memcpy(&t, p_, k);
p_ += k;
n_ -= k;
return true;
}
template <typename T>
bool read(std::vector<T>& v) {
size_t const k = v.size() * sizeof(T);
if (n_ < k) {
return false;
}
memcpy(v.data(), p_, k);
p_ += k;
n_ -= k;
return true;
}
bool read(std::string& s) {
auto* beg = reinterpret_cast<char const*>(p_);
auto* end = reinterpret_cast<char const*>(memchr(p_, 0, n_));
if (!end) {
return false;
}
s.assign(beg, end);
p_ += s.size() + 1;
n_ -= s.size() + 1;
return true;
}
uint8_t const* p_;
size_t n_;
};