-
Notifications
You must be signed in to change notification settings - Fork 11
/
storage.hpp
95 lines (79 loc) · 2.21 KB
/
storage.hpp
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#pragma once
#include <pro.h>
#include <nalt.hpp>
//-------------------------------------------------------------------------
class eanodes_t
{
easet_t eas;
qstring nodename;
bool use_relative;
public:
eanodes_t(const char *nodename, bool use_relative=true): nodename(nodename), use_relative(use_relative) { load(); }
easet_t& nodes() { return eas; }
void dump(const char* banner = nullptr)
{
if (banner)
msg("%s\n", banner);
for (auto ea : eas)
msg(" %a...\n", ea);
}
bool load()
{
eas.clear();
netnode node;
if (node.create(nodename.c_str())) // create failed -> exists already
return false;
size_t n;
ea_t image_base = 0;
if (use_relative)
{
image_base = get_imagebase();
if (image_base == BADADDR)
image_base = 0;
}
void* blob = node.getblob(NULL, &n, 0, 'I');
if (blob != nullptr)
{
auto pea = (ea_t*)blob;
for (size_t i = 0, count = n / sizeof(ea_t); i < count; ++i, ++pea)
eas.insert(*pea + image_base);
qfree(blob);
}
return true;
}
void save()
{
netnode node;
node.create(nodename.c_str());
ea_t image_base = 0;
if (use_relative)
{
image_base = get_imagebase();
if (image_base == BADADDR)
image_base = 0;
}
eavec_t copy;
copy.resize(eas.size());
size_t idx = 0;
for (auto ea : eas)
copy[idx++] = ea - image_base;
node.setblob(copy.begin(), copy.size() * sizeof(ea_t), 0, 'I');
}
void add(ea_t ea, bool flush = true)
{
eas.insert(ea);
if (flush)
save();
}
void reset()
{
netnode node;
node.create(nodename.c_str());
node.delblob(0, 'I');
node.kill();
eas.clear();
load();
}
bool contains(ea_t ea) const { return eas.find(ea) != eas.end(); }
bool empty() const { return eas.empty(); }
};