-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.cpp
220 lines (185 loc) · 4.7 KB
/
Utils.cpp
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <stdexcept>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string.h>
#include "Utils.hpp"
uint32_t ActNut::ABuffer::readInt()
{
uint32_t n;
if (!this->readBytes((uint8_t*)&n, 4))
return 0;
return n;
}
uint8_t ActNut::ABuffer::readByte()
{
uint8_t n;
if (!this->readBytes(&n, 1))
return 0;
return n;
}
bool ActNut::ABuffer::checkTag(uint32_t iTag, const std::string& name)
{
uint32_t readTag = this->readInt();
if (readTag != iTag)
{
std::ostringstream ss;
ss << "Wrong " << name << " - should be "
<< "0x" << std::setfill('0') << std::setw(8) << std::hex << iTag
<< ", but is "
<< "0x" << std::setfill('0') << std::setw(8) << std::hex << readTag;
return Error::error(ss.str());
}
return true;
}
bool ActNut::ABuffer::writeInt(uint32_t n)
{
return this->writeBytes((uint8_t*)&n, 4);
}
bool ActNut::ABuffer::writeByte(uint8_t n)
{
return this->writeBytes(&n, 1);
}
ActNut::MemoryBuffer::MemoryBuffer(BufferOwnership ownership, uint8_t* buf, size_t buf_size, bool fixed_size)
: ownership(ownership), fixed_size(fixed_size)
{
switch (ownership)
{
case CREATE:
if (buf_size == 0)
buf_size = 1024;
this->buf = new uint8_t[buf_size];
memset(this->buf, 0, buf_size);
this->begin = this->buf;
this->end = this->buf;
this->end_alloc = this->end + buf_size;
return ;
case COPY:
this->buf = new uint8_t[buf_size];
memcpy(this->buf, buf, buf_size);
break ;
case STEAL:
case SHARE:
this->buf = buf;
break ;
}
this->begin = this->buf;
this->end = this->buf + buf_size;
this->end_alloc = this->end;
}
ActNut::MemoryBuffer::MemoryBuffer(const uint8_t* buf, size_t buf_size, bool fixed_size)
: ownership(COPY), fixed_size(fixed_size)
{
this->buf = new uint8_t[buf_size];
memcpy(this->buf, buf, buf_size);
this->begin = this->buf;
this->end = this->buf + buf_size;
this->end_alloc = this->end;
}
ActNut::MemoryBuffer::MemoryBuffer()
: MemoryBuffer(CREATE, nullptr, 0, false)
{}
ActNut::MemoryBuffer::~MemoryBuffer()
{
if (this->ownership != SHARE)
delete[] this->begin;
}
bool ActNut::MemoryBuffer::readBytes(uint8_t* out, size_t n)
{
if (this->buf + n > this->end)
{
Error::error("Not enough bytes.");
return false;
}
memcpy(out, this->buf, n);
this->buf += n;
return true;
}
bool ActNut::MemoryBuffer::writeBytes(const uint8_t* in, size_t n)
{
if (this->buf + n > this->end_alloc)
{
if (fixed_size == false)
{
uint8_t* new_buf = new uint8_t[this->end_alloc - this->begin + n + 1024];
memcpy(new_buf, this->begin, this->end - this->begin);
this->buf = this->buf - this->begin + new_buf;
this->end = this->end - this->begin + new_buf;
this->end_alloc = new_buf + (this->end_alloc - this->begin + n + 1024);
this->begin = new_buf;
}
else
{
Error::error("Not enough bytes.");
return false;
}
}
memcpy(this->buf, in, n);
this->buf += n;
if (this->buf > this->end)
this->end = this->buf;
return true;
}
const uint8_t* ActNut::MemoryBuffer::getBuffer() const
{
return this->begin;
}
size_t ActNut::MemoryBuffer::getSize() const
{
return this->end - this->begin;
}
size_t ActNut::MemoryBuffer::getPos()
{
return this->buf - this->begin;
}
ActNut::FileBuffer::FileBuffer(std::fstream&& fs)
: fs(std::move(fs))
{}
ActNut::FileBuffer::FileBuffer(const std::string& filename, std::ios_base::openmode mode)
: fs(filename.c_str(), mode | std::fstream::binary)
{}
ActNut::FileBuffer::~FileBuffer()
{}
bool ActNut::FileBuffer::readBytes(uint8_t* out, size_t n)
{
this->fs.read((char*)out, n);
return (bool)fs;
}
size_t ActNut::FileBuffer::getPos()
{
return static_cast<size_t>(this->fs.tellg());
}
bool ActNut::FileBuffer::writeBytes(const uint8_t* in, size_t n)
{
this->fs.write((const char*)in, n);
return (bool)fs;
}
ActNut::Error::ErrorMode ActNut::Error::errorMode = ActNut::Error::SILENT;
ActNut::Error::Callback ActNut::Error::callback = nullptr;
ActNut::IBuffer* ActNut::Error::buffer = nullptr;
bool ActNut::Error::error(std::string msg)
{
if (buffer)
msg += std::string(" (pos after parsing: ") + std::to_string(buffer->getPos()) + ")";
if (callback)
callback(msg.c_str());
if (errorMode == Error::EXCEPTION)
throw std::runtime_error(msg);
if (errorMode == Error::STDERR)
std::cerr << msg << std::endl;
return false;
}
void ActNut::Error::setErrorMode(ErrorMode newMode)
{
errorMode = newMode;
}
void ActNut::Error::setErrorCallback(Callback callback)
{
Error::callback = callback;
}
ActNut::IBuffer* ActNut::Error::setErrorBuffer(IBuffer* buffer)
{
ActNut::IBuffer* oldBuffer = Error::buffer;
Error::buffer = buffer;
return oldBuffer;
}