/
ChunkFile.cpp
335 lines (290 loc) · 10 KB
/
ChunkFile.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
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
// Copyright (C) 2003 Dolphin Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0 or later versions.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/
#include <cstring>
#include <snappy-c.h>
#include "ChunkFile.h"
#include "StringUtils.h"
PointerWrapSection PointerWrap::Section(const char *title, int ver) {
return Section(title, ver, ver);
}
PointerWrapSection PointerWrap::Section(const char *title, int minVer, int ver) {
char marker[16] = {0};
int foundVersion = ver;
strncpy(marker, title, sizeof(marker));
if (!ExpectVoid(marker, sizeof(marker)))
{
// Might be before we added name markers for safety.
if (foundVersion == 1 && ExpectVoid(&foundVersion, sizeof(foundVersion)))
DoMarker(title);
// Wasn't found, but maybe we can still load the state.
else
foundVersion = 0;
}
else
Do(foundVersion);
if (error == ERROR_FAILURE || foundVersion < minVer || foundVersion > ver) {
WARN_LOG(COMMON, "Savestate failure: wrong version %d found for %s", foundVersion, title);
SetError(ERROR_FAILURE);
return PointerWrapSection(*this, -1, title);
}
return PointerWrapSection(*this, foundVersion, title);
}
void PointerWrap::SetError(Error error_) {
if (error < error_) {
error = error_;
}
if (error > ERROR_WARNING) {
mode = PointerWrap::MODE_MEASURE;
}
}
bool PointerWrap::ExpectVoid(void *data, int size) {
switch (mode) {
case MODE_READ: if (memcmp(data, *ptr, size) != 0) return false; break;
case MODE_WRITE: memcpy(*ptr, data, size); break;
case MODE_MEASURE: break; // MODE_MEASURE - don't need to do anything
case MODE_VERIFY:
for (int i = 0; i < size; i++)
_dbg_assert_msg_(COMMON, ((u8*)data)[i] == (*ptr)[i], "Savestate verification failure: %d (0x%X) (at %p) != %d (0x%X) (at %p).\n", ((u8*)data)[i], ((u8*)data)[i], &((u8*)data)[i], (*ptr)[i], (*ptr)[i], &(*ptr)[i]);
break;
default: break; // throw an error?
}
(*ptr) += size;
return true;
}
void PointerWrap::DoVoid(void *data, int size) {
switch (mode) {
case MODE_READ: memcpy(data, *ptr, size); break;
case MODE_WRITE: memcpy(*ptr, data, size); break;
case MODE_MEASURE: break; // MODE_MEASURE - don't need to do anything
case MODE_VERIFY:
for (int i = 0; i < size; i++)
_dbg_assert_msg_(COMMON, ((u8*)data)[i] == (*ptr)[i], "Savestate verification failure: %d (0x%X) (at %p) != %d (0x%X) (at %p).\n", ((u8*)data)[i], ((u8*)data)[i], &((u8*)data)[i], (*ptr)[i], (*ptr)[i], &(*ptr)[i]);
break;
default: break; // throw an error?
}
(*ptr) += size;
}
void PointerWrap::Do(std::string &x) {
int stringLen = (int)x.length() + 1;
Do(stringLen);
switch (mode) {
case MODE_READ: x = (char*)*ptr; break;
case MODE_WRITE: memcpy(*ptr, x.c_str(), stringLen); break;
case MODE_MEASURE: break;
case MODE_VERIFY: _dbg_assert_msg_(COMMON, !strcmp(x.c_str(), (char*)*ptr), "Savestate verification failure: \"%s\" != \"%s\" (at %p).\n", x.c_str(), (char*)*ptr, ptr); break;
}
(*ptr) += stringLen;
}
void PointerWrap::Do(std::wstring &x) {
int stringLen = sizeof(wchar_t)*((int)x.length() + 1);
Do(stringLen);
switch (mode) {
case MODE_READ: x = (wchar_t*)*ptr; break;
case MODE_WRITE: memcpy(*ptr, x.c_str(), stringLen); break;
case MODE_MEASURE: break;
case MODE_VERIFY: _dbg_assert_msg_(COMMON, x == (wchar_t*)*ptr, "Savestate verification failure: \"%ls\" != \"%ls\" (at %p).\n", x.c_str(), (wchar_t*)*ptr, ptr); break;
}
(*ptr) += stringLen;
}
struct standard_tm {
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};
void PointerWrap::Do(tm &t) {
// We savestate this separately because some platforms use extra data at the end.
// However, old files may have the native tm in them.
// Since the first value in the struct is 0-59, we save a funny value and check for it.
// If our funny value ('tm' 0x1337) exists, it's a new version savestate.
int funnyValue = 0x13376D74;
if (ExpectVoid(&funnyValue, sizeof(funnyValue))) {
standard_tm stm;
if (mode == MODE_READ) {
// Null out the extra members, e.g. tm_gmtoff or tm_zone.
memset(&t, 0, sizeof(t));
} else {
memcpy(&stm, &t, sizeof(stm));
}
DoVoid((void *)&stm, sizeof(stm));
memcpy(&t, &stm, sizeof(stm));
} else {
DoVoid((void *)&t, sizeof(t));
}
}
void PointerWrap::DoMarker(const char *prevName, u32 arbitraryNumber) {
u32 cookie = arbitraryNumber;
Do(cookie);
if (mode == PointerWrap::MODE_READ && cookie != arbitraryNumber) {
PanicAlert("Error: After \"%s\", found %d (0x%X) instead of save marker %d (0x%X). Aborting savestate load...", prevName, cookie, cookie, arbitraryNumber, arbitraryNumber);
SetError(ERROR_FAILURE);
}
}
PointerWrapSection::~PointerWrapSection() {
if (ver_ > 0) {
p_.DoMarker(title_);
}
}
CChunkFileReader::Error CChunkFileReader::LoadFileHeader(File::IOFile &pFile, SChunkHeader &header, std::string *title) {
if (!pFile) {
ERROR_LOG(COMMON, "ChunkReader: Can't open file for reading");
return ERROR_BAD_FILE;
}
const u64 fileSize = pFile.GetSize();
u64 headerSize = sizeof(SChunkHeader);
if (fileSize < headerSize) {
ERROR_LOG(COMMON, "ChunkReader: File too small");
return ERROR_BAD_FILE;
}
if (!pFile.ReadArray(&header, 1)) {
ERROR_LOG(COMMON, "ChunkReader: Bad header size");
return ERROR_BAD_FILE;
}
if (header.Revision < REVISION_MIN) {
ERROR_LOG(COMMON, "ChunkReader: Wrong file revision, got %d expected >= %d", header.Revision, REVISION_MIN);
return ERROR_BAD_FILE;
}
if (header.Revision >= REVISION_TITLE) {
char titleFixed[128];
if (!pFile.ReadArray(titleFixed, sizeof(titleFixed))) {
ERROR_LOG(COMMON, "ChunkReader: Unable to read title");
return ERROR_BAD_FILE;
}
if (title) {
*title = titleFixed;
}
headerSize += 128;
} else if (title) {
title->clear();
}
u32 sz = (u32)(fileSize - headerSize);
if (header.ExpectedSize != sz) {
ERROR_LOG(COMMON, "ChunkReader: Bad file size, got %u expected %u", sz, header.ExpectedSize);
return ERROR_BAD_FILE;
}
return ERROR_NONE;
}
CChunkFileReader::Error CChunkFileReader::GetFileTitle(const std::string &filename, std::string *title) {
if (!File::Exists(filename)) {
ERROR_LOG(COMMON, "ChunkReader: File doesn't exist");
return ERROR_BAD_FILE;
}
File::IOFile pFile(filename, "rb");
SChunkHeader header;
return LoadFileHeader(pFile, header, title);
}
CChunkFileReader::Error CChunkFileReader::LoadFile(const std::string &filename, const char *gitVersion, u8 *&_buffer, size_t &sz, std::string *failureReason) {
if (!File::Exists(filename)) {
*failureReason = "LoadStateDoesntExist";
ERROR_LOG(COMMON, "ChunkReader: File doesn't exist");
return ERROR_BAD_FILE;
}
File::IOFile pFile(filename, "rb");
SChunkHeader header;
Error err = LoadFileHeader(pFile, header, nullptr);
if (err != ERROR_NONE) {
return err;
}
// read the state
sz = header.ExpectedSize;
u8 *buffer = new u8[sz];
if (!pFile.ReadBytes(buffer, sz))
{
ERROR_LOG(COMMON, "ChunkReader: Error reading file");
delete [] buffer;
return ERROR_BAD_FILE;
}
_buffer = buffer;
if (header.Compress) {
u8 *uncomp_buffer = new u8[header.UncompressedSize];
size_t uncomp_size = header.UncompressedSize;
snappy_uncompress((const char *)buffer, sz, (char *)uncomp_buffer, &uncomp_size);
if ((u32)uncomp_size != header.UncompressedSize) {
ERROR_LOG(COMMON, "Size mismatch: file: %u calc: %u", header.UncompressedSize, (u32)uncomp_size);
delete [] uncomp_buffer;
return ERROR_BAD_FILE;
}
_buffer = uncomp_buffer;
sz = uncomp_size;
delete [] buffer;
}
return ERROR_NONE;
}
// Takes ownership of buffer.
CChunkFileReader::Error CChunkFileReader::SaveFile(const std::string &filename, const std::string &title, const char *gitVersion, u8 *buffer, size_t sz) {
INFO_LOG(COMMON, "ChunkReader: Writing %s", filename.c_str());
File::IOFile pFile(filename, "wb");
if (!pFile)
{
ERROR_LOG(COMMON, "ChunkReader: Error opening file for write");
delete[] buffer;
return ERROR_BAD_FILE;
}
bool compress = true;
// Create header
SChunkHeader header;
header.Compress = compress ? 1 : 0;
header.Revision = REVISION_CURRENT;
header.ExpectedSize = (u32)sz;
header.UncompressedSize = (u32)sz;
strncpy(header.GitVersion, gitVersion, 32);
header.GitVersion[31] = '\0';
// Setup the fixed-length title.
char titleFixed[128];
strncpy(titleFixed, title.c_str(), sizeof(titleFixed));
titleFixed[sizeof(titleFixed) - 1] = '\0';
// Write to file
if (compress) {
size_t comp_len = snappy_max_compressed_length(sz);
u8 *compressed_buffer = new u8[comp_len];
snappy_compress((const char *)buffer, sz, (char *)compressed_buffer, &comp_len);
delete [] buffer;
header.ExpectedSize = (u32)comp_len;
if (!pFile.WriteArray(&header, 1)) {
ERROR_LOG(COMMON, "ChunkReader: Failed writing header");
return ERROR_BAD_FILE;
}
if (!pFile.WriteArray(titleFixed, sizeof(titleFixed))) {
ERROR_LOG(COMMON, "ChunkReader: Failed writing title");
return ERROR_BAD_FILE;
}
if (!pFile.WriteBytes(&compressed_buffer[0], comp_len)) {
ERROR_LOG(COMMON, "ChunkReader: Failed writing compressed data");
return ERROR_BAD_FILE;
} else {
INFO_LOG(COMMON, "Savestate: Compressed %i bytes into %i", (int)sz, (int)comp_len);
}
delete [] compressed_buffer;
} else {
if (!pFile.WriteArray(&header, 1))
{
ERROR_LOG(COMMON, "ChunkReader: Failed writing header");
delete[] buffer;
return ERROR_BAD_FILE;
}
if (!pFile.WriteBytes(&buffer[0], sz))
{
ERROR_LOG(COMMON, "ChunkReader: Failed writing data");
delete[] buffer;
return ERROR_BAD_FILE;
}
delete [] buffer;
}
INFO_LOG(COMMON, "ChunkReader: Done writing %s", filename.c_str());
return ERROR_NONE;
}