-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate.cpp
297 lines (284 loc) · 9.5 KB
/
update.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include <cstdint>
#include <sstream>
#include <vector>
#include <array>
#include <span>
#include <string_view>
#include <iostream>
#include <fstream>
#if __has_include(<format>)
#include <format>
constexpr auto operator ""_format(const char *str, std::size_t len) {
return [=](auto ...args) {
return std::format({str, len}, std::forward<decltype(args)>(args)...);
};
}
#else
#include <fmt/format.h>
/* using namespace fmt; */
constexpr auto operator ""_format(const char *str, std::size_t len) {
return [=](auto ...args) {
return fmt::format(std::string_view{str, len}, std::forward<decltype(args)>(args)...);
};
}
#endif
#include "endian-helper.h"
using namespace std::literals::string_view_literals;
using namespace fmt;
struct Header {
enum class Type {
Controller = 0,
Module = 1,
Screen = 2
};
struct Entry {
Type type;
std::uint32_t offset;
std::uint32_t size;
};
std::string version;
std::uint32_t flags;
std::vector<Entry> entries;
};
struct Update {
std::string version;
std::uint32_t flags = 0;
std::optional<std::vector<char>> screen;
std::optional<std::vector<char>> controller;
std::vector<std::vector<char>> modules;
};
constexpr std::size_t headerSize(std::size_t entries) noexcept {
return 2 + 32 + 4 + 1 + 9 * entries;
}
Header parseHeader(std::span<const char> buffer) {
if(buffer.size() < headerSize(0))
throw "Buffer too small to contain header";
auto iter = buffer.begin();
std::uint16_t length = be16toh(*(std::uint16_t*)&*iter);
if(length > buffer.size())
throw "Header length exceeds buffer size";
iter += 2; length -= 2;
Header header;
header.version = [&] {
auto end_iter = iter + 32;
while(end_iter != iter && !*(end_iter - 1))
--end_iter;
return std::string(iter, end_iter);
}();
iter += 32; length -= 32;
header.flags = be32toh(*(std::uint32_t*)&*iter);
iter += 4; length -= 4;
header.entries.resize(*iter);
iter += 1; length -= 1;
if (length < header.entries.size() * 9)
throw "Too many entries for specified header size";
for (Header::Entry &entry : header.entries) {
entry.type = Header::Type(*iter);
iter += 1;
entry.offset = be32toh(*(std::uint32_t*)&*iter);
iter += 4; length -= 4;
entry.size = be32toh(*(std::uint32_t*)&*iter);
iter += 4; length -= 4;
}
return header;
}
Update parseUpdate(std::span<const char> buffer) {
Update update;
auto header = parseHeader(buffer);
update.version = std::move(header.version);
update.flags = header.flags;
for (auto &&entry : header.entries) {
if (entry.offset + entry.size > buffer.size())
throw "Length inconsistency detected";
auto &container = [&](Header::Type type) -> auto& {
switch(type) {
case Header::Type::Controller:
if (update.controller)
throw "Duplicate Controller packet";
return update.controller.emplace();
case Header::Type::Module:
update.modules.emplace_back();
return update.modules.back();
case Header::Type::Screen:
if (update.screen)
throw "Duplicate Screen packet";
return update.screen.emplace();
default: throw "Unknown entry type";
}
}(entry.type);
container.resize(entry.size);
auto begin_iter = buffer.begin() + entry.offset;
std::copy(begin_iter, begin_iter + entry.size, container.begin());
}
return update;
}
std::size_t getSize(const Update &update) {
std::size_t size = 0;
std::size_t packets = update.modules.size();
for (auto &&module : update.modules) {
size += module.size();
}
if (update.controller) {
size += update.controller->size(); ++packets;
}
if (update.screen) {
size += update.screen->size(); ++packets;
}
return size + headerSize(packets);
}
std::span<char> serialize(const Header &header, std::span<char> buffer) {
auto header_size = headerSize(header.entries.size());
if (buffer.size() < header_size)
throw "Buffer too small to hold header";
if (header.version.size() > 32)
throw "Version too long";
if (header.entries.size() >= 0x100)
throw "Too many entries in update header";
*reinterpret_cast<std::uint16_t*>(buffer.data()) = htobe16(header_size);
buffer = buffer.subspan<2>();
std::fill(
std::copy(header.version.begin(), header.version.end(), buffer.begin()),
buffer.begin() + 32,
0);
buffer = buffer.subspan<32>();
*reinterpret_cast<std::uint32_t*>(buffer.data()) = htobe32(header.flags);
buffer = buffer.subspan<4>();
buffer.front() = header.entries.size();
buffer = buffer.subspan<1>();
for (auto &&entry : header.entries) {
buffer.front() = (char)entry.type;
buffer = buffer.subspan<1>();
*reinterpret_cast<std::uint32_t*>(buffer.data()) = htobe32(entry.offset);
buffer = buffer.subspan<4>();
*reinterpret_cast<std::uint32_t*>(buffer.data()) = htobe32(entry.size);
buffer = buffer.subspan<4>();
}
return buffer;
}
std::span<char> serialize(const Update &update, std::span<char> buffer) {
Header header;
header.version = update.version;
header.flags = update.flags;
std::uint32_t offset = headerSize(update.modules.size() + (update.controller?1:0) + (update.screen?1:0));
for (auto &&module : update.modules) {
header.entries.push_back(Header::Entry(Header::Type::Module, offset, module.size()));
offset += module.size();
}
if (update.controller) {
header.entries.push_back(Header::Entry(Header::Type::Controller, offset, update.controller->size()));
offset += update.controller->size();
}
if (update.screen) {
header.entries.push_back(Header::Entry(Header::Type::Screen, offset, update.screen->size()));
offset += update.screen->size();
}
if (buffer.size() < offset)
throw "Buffer too small to hold update";
buffer = serialize(header, buffer);
for (auto &&module : update.modules) {
std::copy(module.begin(), module.end(), buffer.begin());
buffer = buffer.subspan(module.size());
}
if (update.controller) {
std::copy(update.controller->begin(), update.controller->end(), buffer.begin());
buffer = buffer.subspan(update.controller->size());
}
if (update.screen) {
std::copy(update.screen->begin(), update.screen->end(), buffer.begin());
buffer = buffer.subspan(update.screen->size());
}
return buffer;
}
std::vector<char> serialize(const Update &update) {
std::vector<char> buffer(getSize(update));
serialize(update, buffer);
return buffer;
}
auto read_file(const char *filename) {
std::vector<char> buffer(0x1000);
std::ifstream file(filename, std::ios_base::in | std::ios_base::binary);
while(file.read(buffer.data() + (buffer.size() - 0x1000), 0x1000))
buffer.resize(buffer.size() + 0x1000);
buffer.resize(buffer.size() - 0x1000 + file.gcount());
return buffer;
}
void write_file(const char *filename, const std::vector<char> &data) {
std::ofstream file(filename, std::ios_base::out | std::ios_base::binary);
if (!file)
throw "Unable to open output file\n";
file.write(data.data(), data.size());
}
int main(int argc, char const* argv[])
try {
switch(argc) {
case 0: case 1:
std::cerr << "Invalid usage. If you ever figure out the correct usage, feel free to contribute a nice help message.\n";
return 1;
case 2:
{
auto update = parseUpdate(read_file(argv[1]));
if (update.screen)
write_file("screen.apk", *update.screen);
if (update.controller)
write_file("controller.bin.packet", *update.controller);
auto mod_begin = update.modules.begin(), mod_end = update.modules.end();
for(auto iter = mod_begin; mod_end != iter; ++iter)
write_file("module{}.bin.packet"_format(iter - mod_begin).c_str(), *iter);
if (update.flags & 1)
std::cout << "--force ";
if (update.flags & ~1)
std::cerr << "WARNING: Unknown flags dropped\n";
std::cout << update.version << '\n';
}
break;
default:
{
Update update;
std::ofstream output;
while(argv[1]) {
std::string_view arg = argv[1];
if (arg == "--force"sv)
update.flags = 1;
else if (arg.starts_with("--output=")) {
arg.remove_prefix(sizeof("--output=")-1);
output.open(arg.data(), std::ios_base::out | std::ios_base::binary);
if (!output.is_open()) {
std::cerr << "Unable to open output file\n";
return 1;
}
} else break;
++argv; --argc;
}
if (argv[1] == "--force"sv) {
update.flags = 1; ++argv; --argc;
if (argc == 2) {
std::cerr << "Invalid usage\n";
return 1;
}
} else
update.flags = 0;
update.version = argv[1];
for (int i = 2; i != argc; ++i) {
auto content = read_file(argv[i]);
if (content.empty())
std::cerr << "Skipping empty input file\n";
else
switch (content[0]) {
case 0: update.controller = std::move(content); break;
case 'P': update.screen = std::move(content); break;
case 1: update.modules.push_back(std::move(content)); break;
default: throw "Invalid input file\n";
}
}
auto buffer = serialize(update);
(output.is_open() ? output : std::cout).write(buffer.data(), buffer.size());
}
}
return 0;
} catch(const char *str) {
std::cerr << "FATAL ERROR: " << str << '\n';
return 1;
} catch(std::exception &ex) {
std::cerr << "FATAL ERROR: " << ex.what() << '\n';
return 1;
}