-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsnss-parse-lib.hpp
471 lines (413 loc) · 15.3 KB
/
snss-parse-lib.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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <sstream>
#include <stdint.h>
#include <vector>
#include <memory>
// TODO Documentation
// ---------------- //
// Session commands //
// ---------------- //
struct Command {
uint16_t size;
uint32_t type;
virtual ~Command() {}
};
struct CommandSetWindowBounds3 : Command {
int32_t window_id;
int32_t x;
int32_t y;
int32_t width;
int32_t height;
int32_t show_state; // https://0x1.pl/s/36
virtual ~CommandSetWindowBounds3() {}
};
struct CommandLastActiveTime : Command {
int32_t id;
int32_t UNKNOWN_0; // TODO
int64_t last_active_time;
virtual ~CommandLastActiveTime() {}
};
struct CommandTabClosed : Command {
int32_t id;
int32_t UNKNOWN_0; // TODO
int64_t close_time;
virtual ~CommandTabClosed() {}
};
using CommandWindowClosed = CommandTabClosed;
struct CommandSetActiveWindow : Command {
uint32_t window_id;
virtual ~CommandSetActiveWindow() {}
};
struct IDAndIndex : Command {
uint32_t id;
int32_t index;
virtual ~IDAndIndex() {}
};
struct CommandUpdateTabNavigation : Command {
uint32_t pickle_size;
int32_t tabid;
int32_t index;
std::string url; // int + string
std::wstring title; // int + wstring
// TODO Add all the other fields that are missing
// See serialized_navigation_entry.cc => WriteToPickle()
virtual ~CommandUpdateTabNavigation() {}
};
struct CommandSessionStorageAssociated : Command {
uint32_t pickle_size;
int32_t tabid;
std::string session_storage_persistent_id; // int + string
virtual ~CommandSessionStorageAssociated() {}
};
struct CommandSetWindowWorkspace2 : Command {
uint32_t pickle_size;
int32_t window_id;
std::string workspace; // int + string
virtual ~CommandSetWindowWorkspace2() {}
};
// ---------------- //
// - Tab commands - //
// ---------------- //
// TODO Old struct didn't include timestamp https://0x1.pl/s/41
struct CommandSelectedNavigationInTab : Command {
int32_t tabid;
int32_t index;
int64_t timestamp;
virtual ~CommandSelectedNavigationInTab() {}
};
struct CommandRestoredEntry : Command {
int32_t entry_id;
virtual ~CommandRestoredEntry() {}
};
struct CommandPinnedState : Command {
bool pinned;
virtual ~CommandPinnedState() {}
};
// ---------------- //
// Read utilities //
// ---------------- //
template <typename T>
T read_arbitrary(std::ifstream& f, int size, int offset) {
T ret;
f.seekg(offset);
f.read(reinterpret_cast<char *>(&ret), size);
return ret;
}
template <typename T>
T read_arbitrary(std::ifstream& f, int size) {
T ret;
f.read(reinterpret_cast<char *>(&ret), size);
return ret;
}
void read_char(std::ifstream& f, char* out, int size, int offset) {
f.seekg(offset);
f.read(out, size);
}
void read_char(std::ifstream& f, char* out, int size) {
f.read(out, size);
}
bool read_string(std::ifstream& f, std::string& ret, bool terminate) {
uint32_t len = read_arbitrary<uint32_t>(f, 4);
int start = f.tellg();
int end = start + len;
ret.resize(end - start);
f.read(&ret[0], end - start);
if (terminate) {
uint16_t z = read_arbitrary<uint16_t>(f, 1);
// For whatever reason the string is padded with an arbitrary amount of zeroes
if (0 != z)
return false; // If there is no zero, a string16 won't follow
int count = 0;
while (0 == z) {
count += 1;
z = read_arbitrary<uint16_t>(f, 1);
}
if (count > 3)
return false;
f.seekg(f.tellg() - 1L);
}
return true;
}
std::wstring read_string16(std::ifstream& f, bool terminate) {
uint32_t len = read_arbitrary<uint32_t>(f, 4) * 2; // *2 because UTF-16
#ifdef DEBUG
std::cout << "Reading string16: " << std::endl;
std::cout << std::dec << len << std::endl;
std::cout << std::hex << f.tellg() << std::endl;
std::cout << std::dec;
#endif
char tmp[len];
read_char(f, tmp, len);
if (terminate) {
read_arbitrary<uint16_t>(f, 1); // String is null terminated with 0x00
}
if (len % 2) { len--; }
std::wstring ret;
for (size_t i = 0; i < len;) {
// little-endian
int lo = tmp[i++] & 0xFF;
int hi = tmp[i++] & 0xFF;
ret.push_back(hi << 8 | lo);
}
return ret;
}
void probe_next_command(std::ifstream& f) {
uint16_t size = read_arbitrary<uint16_t>(f, 2);
uint8_t type_id = read_arbitrary<uint8_t>(f, 1);
std::cout << "S: " << size << " T: " << static_cast<uint16_t>(type_id) << std::endl;
}
// ---------------- //
// Read functions //
// ---------------- //
// TODO Missing ids: 11,13,18
std::shared_ptr<Command> read_next_session_command(std::ifstream& f) {
std::shared_ptr<Command> ret;
int prev = f.tellg();
uint16_t size = read_arbitrary<uint16_t>(f, 2);
uint8_t type_id = read_arbitrary<uint8_t>(f, 1);
if (0 == size) {
std::cerr << "Error: Encountered command with size 0. This can indicate a corrupt file.";
}
// See https://0x1.pl/s/33 for all command ids
switch(type_id) {
case 6: // CommandUpdateTabNavigation
{
ret = std::shared_ptr<CommandUpdateTabNavigation>(new CommandUpdateTabNavigation());
std::shared_ptr<CommandUpdateTabNavigation> ret2 = std::static_pointer_cast<CommandUpdateTabNavigation>(ret);
ret2->pickle_size = read_arbitrary<uint32_t>(f, 4);
ret2->tabid = read_arbitrary<int32_t>(f, 4);
ret2->index = read_arbitrary<int32_t>(f, 4);
if (read_string(f, ret2->url, true))
ret2->title = read_string16(f, true);
// TODO Add missing stuff
break;
}
case 0: // CommandSetTabWindow
case 2: // CommandSetTabIndexInWindow
case 5: // CommandTabNavigationPathPrunedFromBack
case 7: // CommandSetSelectedNavigationIndex
case 8: // CommandSetSelectedTabInIndex
case 9: // CommandSetWindowType
case 12: // CommandSetPinnedState (TODO works, but we should read a bool here, not int!)
{
ret = std::shared_ptr<IDAndIndex>(new IDAndIndex());
std::shared_ptr<IDAndIndex> ret2 = std::static_pointer_cast<IDAndIndex>(ret);
//IDAndIndex & cmd = (IDAndIndex &) ret;
ret2->id = read_arbitrary<uint32_t>(f, 4);
ret2->index = read_arbitrary<int32_t>(f, 4);
break;
}
case 14: // CommandSetWindowBounds3
{
ret = std::shared_ptr<CommandSetWindowBounds3>(new CommandSetWindowBounds3());
std::shared_ptr<CommandSetWindowBounds3> ret2 = std::static_pointer_cast<CommandSetWindowBounds3>(ret);
ret2->window_id = read_arbitrary<int32_t>(f, 4);
ret2->x = read_arbitrary<int32_t>(f, 4);
ret2->y = read_arbitrary<int32_t>(f, 4);
ret2->width = read_arbitrary<int32_t>(f, 4);
ret2->height = read_arbitrary<int32_t>(f, 4);
ret2->show_state = read_arbitrary<int32_t>(f, 4);
break;
}
case 16: // CommandTabClosed
{
ret = std::shared_ptr<CommandTabClosed>(new CommandTabClosed());
std::shared_ptr<CommandTabClosed> ret2 = std::static_pointer_cast<CommandTabClosed>(ret);
ret2->id = read_arbitrary<int32_t>(f, 4);
ret2->UNKNOWN_0 = read_arbitrary<int32_t>(f, 4); // TODO
ret2->close_time = read_arbitrary<int64_t>(f, 8);
break;
}
case 17: // CommandWindowClosed
{
ret = std::shared_ptr<CommandWindowClosed>(new CommandWindowClosed());
std::shared_ptr<CommandWindowClosed> ret2 = std::static_pointer_cast<CommandWindowClosed>(ret);
ret2->id = read_arbitrary<int32_t>(f, 4);
ret2->UNKNOWN_0 = read_arbitrary<int32_t>(f, 4); // TODO
ret2->close_time = read_arbitrary<int64_t>(f, 8);
break;
}
case 19: // CommandSessionStorageAssociated
{
ret = std::shared_ptr<CommandSessionStorageAssociated>(new CommandSessionStorageAssociated());
std::shared_ptr<CommandSessionStorageAssociated> ret2 = std::static_pointer_cast<CommandSessionStorageAssociated>(ret);
ret2->pickle_size = read_arbitrary<uint32_t>(f, 4);
ret2->tabid = read_arbitrary<int32_t>(f, 4);
read_string(f, ret2->session_storage_persistent_id, false);
break;
}
case 20: // CommandSetActiveWindow
{
ret = std::shared_ptr<CommandSetActiveWindow>(new CommandSetActiveWindow());
std::shared_ptr<CommandSetActiveWindow> ret2 = std::static_pointer_cast<CommandSetActiveWindow>(ret);
ret2->window_id = read_arbitrary<uint32_t>(f, 4);
break;
}
case 21: // CommandLastActiveTime
{
ret = std::shared_ptr<CommandLastActiveTime>(new CommandLastActiveTime());
std::shared_ptr<CommandLastActiveTime> ret2 = std::static_pointer_cast<CommandLastActiveTime>(ret);
ret2->id = read_arbitrary<int32_t>(f, 4);
ret2->UNKNOWN_0 = read_arbitrary<int32_t>(f, 4); // TODO
ret2->last_active_time = read_arbitrary<int64_t>(f, 8);
break;
}
case 23: // CommandSetWindowWorkspace2
{
ret = std::shared_ptr<CommandSetWindowWorkspace2>(new CommandSetWindowWorkspace2());
std::shared_ptr<CommandSetWindowWorkspace2> ret2 = std::static_pointer_cast<CommandSetWindowWorkspace2>(ret);
ret2->pickle_size = read_arbitrary<uint32_t>(f, 4);
ret2->window_id = read_arbitrary<int32_t>(f, 4);
read_string(f, ret2->workspace, false);
break;
}
default:
{
std::cerr << "Warning: Encountered unsupported id " << static_cast<uint16_t>(type_id) << std::endl;
std::cerr << std::hex << "P: " << prev << std::dec << " S: " << size << " T: " << static_cast<uint16_t>(type_id) << std::endl;
ret = std::shared_ptr<Command>(new Command());
}
}
ret->size = size;
ret->type = type_id;
// Finish reading the command in case we skipped stuff
// Size already includes the 2 bytes from the uint16 size
prev += size + 2L;
if (f.tellg() < prev) {
f.seekg(prev);
}
return ret;
}
// TODO Missing ids: 3,6,7,8,9
std::shared_ptr<Command> read_next_tab_command(std::ifstream& f) {
std::shared_ptr<Command> ret;
int prev = f.tellg();
uint16_t size = read_arbitrary<uint16_t>(f, 2);
uint8_t type_id = read_arbitrary<uint8_t>(f, 1);
if (0 == size) {
std::cerr << "Error: Encountered command with size 0. This can indicate a corrupt file.";
}
switch(type_id) {
case 1: // CommandUpdateTabNavigation
{
// TODO Copied from read_next_session_command
ret = std::shared_ptr<CommandUpdateTabNavigation>(new CommandUpdateTabNavigation());
std::shared_ptr<CommandUpdateTabNavigation> ret2 = std::static_pointer_cast<CommandUpdateTabNavigation>(ret);
ret2->pickle_size = read_arbitrary<uint32_t>(f, 4);
ret2->tabid = read_arbitrary<int32_t>(f, 4);
ret2->index = read_arbitrary<int32_t>(f, 4);
if (read_string(f, ret2->url, true))
ret2->title = read_string16(f, true);
// TODO Add missing stuff
break;
}
case 2: // CommandRestoredEntry
{
ret = std::shared_ptr<CommandRestoredEntry>(new CommandRestoredEntry());
std::shared_ptr<CommandRestoredEntry> ret2 = std::static_pointer_cast<CommandRestoredEntry>(ret);
ret2->entry_id = read_arbitrary<int32_t>(f, 4);
break;
}
case 4: // CommandSelectedNavigationInTab
{
ret = std::shared_ptr<CommandSelectedNavigationInTab>(new CommandSelectedNavigationInTab());
std::shared_ptr<CommandSelectedNavigationInTab> ret2 = std::static_pointer_cast<CommandSelectedNavigationInTab>(ret);
ret2->tabid = read_arbitrary<int32_t>(f, 4);
ret2->index = read_arbitrary<int32_t>(f, 4);
ret2->timestamp = read_arbitrary<int64_t>(f, 8); // TODO First probe for next command
break;
}
case 5: // CommandPinnedState
{
std::cout << std::hex << prev << std::dec << size << " " << std::endl;
ret = std::shared_ptr<CommandPinnedState>(new CommandPinnedState());
std::shared_ptr<CommandPinnedState> ret2 = std::static_pointer_cast<CommandPinnedState>(ret);
ret2->pinned = read_arbitrary<bool>(f, 1);
break;
}
default:
{
std::cerr << "Warning: Encountered unsupported id " << static_cast<uint16_t>(type_id) << std::endl;
std::cerr << std::hex << "P: " << prev << std::dec << " S: " << size << " T: " << static_cast<uint16_t>(type_id) << std::endl;
ret = std::shared_ptr<Command>(new Command());
}
}
ret->size = size;
ret->type = type_id;
// Finish reading the command in case we skipped stuff
prev += size + 2L;
if (f.tellg() < prev) {
f.seekg(prev);
}
return ret;
}
int get_file_size(std::ifstream& f) {
f.seekg(0, std::ios::end);
int filesize = f.tellg();
f.seekg(0);
return filesize;
}
bool open_and_check_file(std::ifstream& f, int *filesize) {
*filesize = get_file_size(f);
char magic[4] = "";
read_char(f, magic, 4);
read_arbitrary<uint32_t>(f, 4); // version
if (0 != std::strncmp("SNSS", magic, 4)) {
std::cerr << "Error: Not a valid SNSS file." << std::endl;
return false;
}
return true;
}
std::vector<std::shared_ptr<Command>> read_session_file(std::ifstream& f) {
int filesize;
if (!open_and_check_file(f, &filesize))
return {};
std::vector<std::shared_ptr<Command>> ret;
while (f.tellg() < filesize) {
std::shared_ptr<Command> c = read_next_session_command(f);
ret.push_back(c);
if (c->size == 0) {
break;
}
}
return ret;
}
std::vector<std::shared_ptr<Command>> read_session_file(const std::string& filename) {
std::ifstream f(filename, std::ios::binary | std::ios::in);
if (!f.is_open() || f.fail()) {
std::cerr << "Error while trying to open file " << filename << "." << std::endl;
return {};
}
std::vector<std::shared_ptr<Command>> ret;
ret = read_session_file(f);
f.close();
return ret;
}
std::vector<std::shared_ptr<Command>> read_tabs_file(std::ifstream& f) {
int filesize;
if (!open_and_check_file(f, &filesize))
return {};
std::vector<std::shared_ptr<Command>> ret;
while (f.tellg() < filesize) {
std::shared_ptr<Command> c = read_next_tab_command(f);
ret.push_back(c);
if (c->size == 0) {
break;
}
}
return ret;
}
std::vector<std::shared_ptr<Command>> read_tabs_file(const std::string& filename) {
std::ifstream f(filename, std::ios::binary | std::ios::in);
if (!f.is_open() || f.fail()) {
std::cerr << "Error while trying to open file " << filename << "." << std::endl;
return {};
}
std::vector<std::shared_ptr<Command>> ret;
ret = read_tabs_file(f);
f.close();
return ret;
}