summaryrefslogtreecommitdiff
path: root/src/common/chunk_file.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/chunk_file.h')
-rw-r--r--src/common/chunk_file.h874
1 files changed, 874 insertions, 0 deletions
diff --git a/src/common/chunk_file.h b/src/common/chunk_file.h
new file mode 100644
index 000000000..68c2943ab
--- /dev/null
+++ b/src/common/chunk_file.h
@@ -0,0 +1,874 @@
1// Copyright (C) 2003 Dolphin Project.
2
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, version 2.0 or later versions.
6
7// This program is distributed in the hope that it will be useful,
8// but WITHOUT ANY WARRANTY; without even the implied warranty of
9// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10// GNU General Public License 2.0 for more details.
11
12// A copy of the GPL 2.0 should have been included with the program.
13// If not, see http://www.gnu.org/licenses/
14
15// Official SVN repository and contact information can be found at
16// http://code.google.com/p/dolphin-emu/
17
18#ifndef _POINTERWRAP_H_
19#define _POINTERWRAP_H_
20
21// Extremely simple serialization framework.
22
23// (mis)-features:
24// + Super fast
25// + Very simple
26// + Same code is used for serialization and deserializaition (in most cases)
27// - Zero backwards/forwards compatibility
28// - Serialization code for anything complex has to be manually written.
29
30#include <map>
31#include <vector>
32#include <deque>
33#include <string>
34#include <list>
35#include <set>
36#ifndef __SYMBIAN32__
37#if defined(IOS) || defined(MACGNUSTD)
38#include <tr1/type_traits>
39#else
40#include <type_traits>
41#endif
42#endif
43
44#include "common.h"
45#include "file_util.h"
46//#include "../ext/snappy/snappy-c.h"
47
48#if defined(IOS) || defined(MACGNUSTD)
49namespace std {
50 using tr1::is_pointer;
51}
52#endif
53#ifdef __SYMBIAN32__
54namespace std {
55 template <bool bool_value>
56 struct bool_constant {
57 typedef bool_constant<bool_value> type;
58 static const bool value = bool_value;
59 };
60 template <bool bool_value> const bool bool_constant<bool_value>::value;
61 template <typename T> struct is_pointer : public bool_constant<false> {};
62 template <typename T> struct is_pointer<T*> : public bool_constant<true> {};
63}
64#endif
65
66template <class T>
67struct LinkedListItem : public T
68{
69 LinkedListItem<T> *next;
70};
71
72class PointerWrap;
73
74class PointerWrapSection
75{
76public:
77 PointerWrapSection(PointerWrap &p, int ver, const char *title) : p_(p), ver_(ver), title_(title) {
78 }
79 ~PointerWrapSection();
80
81 bool operator == (const int &v) const { return ver_ == v; }
82 bool operator != (const int &v) const { return ver_ != v; }
83 bool operator <= (const int &v) const { return ver_ <= v; }
84 bool operator >= (const int &v) const { return ver_ >= v; }
85 bool operator < (const int &v) const { return ver_ < v; }
86 bool operator > (const int &v) const { return ver_ > v; }
87
88 operator bool() const {
89 return ver_ > 0;
90 }
91
92private:
93 PointerWrap &p_;
94 int ver_;
95 const char *title_;
96};
97
98// Wrapper class
99class PointerWrap
100{
101 // This makes it a compile error if you forget to define DoState() on non-POD.
102 // Which also can be a problem, for example struct tm is non-POD on linux, for whatever reason...
103#ifdef _MSC_VER
104 template<typename T, bool isPOD = std::is_pod<T>::value, bool isPointer = std::is_pointer<T>::value>
105#else
106 template<typename T, bool isPOD = __is_pod(T), bool isPointer = std::is_pointer<T>::value>
107#endif
108 struct DoHelper
109 {
110 static void DoArray(PointerWrap *p, T *x, int count)
111 {
112 for (int i = 0; i < count; ++i)
113 p->Do(x[i]);
114 }
115
116 static void Do(PointerWrap *p, T &x)
117 {
118 p->DoClass(x);
119 }
120 };
121
122 template<typename T>
123 struct DoHelper<T, true, false>
124 {
125 static void DoArray(PointerWrap *p, T *x, int count)
126 {
127 p->DoVoid((void *)x, sizeof(T) * count);
128 }
129
130 static void Do(PointerWrap *p, T &x)
131 {
132 p->DoVoid((void *)&x, sizeof(x));
133 }
134 };
135
136public:
137 enum Mode {
138 MODE_READ = 1, // load
139 MODE_WRITE, // save
140 MODE_MEASURE, // calculate size
141 MODE_VERIFY, // compare
142 };
143
144 enum Error {
145 ERROR_NONE = 0,
146 ERROR_WARNING = 1,
147 ERROR_FAILURE = 2,
148 };
149
150 u8 **ptr;
151 Mode mode;
152 Error error;
153
154public:
155 PointerWrap(u8 **ptr_, Mode mode_) : ptr(ptr_), mode(mode_), error(ERROR_NONE) {}
156 PointerWrap(unsigned char **ptr_, int mode_) : ptr((u8**)ptr_), mode((Mode)mode_), error(ERROR_NONE) {}
157
158 PointerWrapSection Section(const char *title, int ver) {
159 return Section(title, ver, ver);
160 }
161
162 // The returned object can be compared against the version that was loaded.
163 // This can be used to support versions as old as minVer.
164 // Version = 0 means the section was not found.
165 PointerWrapSection Section(const char *title, int minVer, int ver) {
166 char marker[16] = {0};
167 int foundVersion = ver;
168
169 strncpy(marker, title, sizeof(marker));
170 if (!ExpectVoid(marker, sizeof(marker)))
171 {
172 // Might be before we added name markers for safety.
173 if (foundVersion == 1 && ExpectVoid(&foundVersion, sizeof(foundVersion)))
174 DoMarker(title);
175 // Wasn't found, but maybe we can still load the state.
176 else
177 foundVersion = 0;
178 }
179 else
180 Do(foundVersion);
181
182 if (error == ERROR_FAILURE || foundVersion < minVer || foundVersion > ver) {
183 WARN_LOG(COMMON, "Savestate failure: wrong version %d found for %s", foundVersion, title);
184 SetError(ERROR_FAILURE);
185 return PointerWrapSection(*this, -1, title);
186 }
187 return PointerWrapSection(*this, foundVersion, title);
188 }
189
190 void SetMode(Mode mode_) {mode = mode_;}
191 Mode GetMode() const {return mode;}
192 u8 **GetPPtr() {return ptr;}
193 void SetError(Error error_)
194 {
195 if (error < error_)
196 error = error_;
197 if (error > ERROR_WARNING)
198 mode = PointerWrap::MODE_MEASURE;
199 }
200
201 bool ExpectVoid(void *data, int size)
202 {
203 switch (mode) {
204 case MODE_READ: if (memcmp(data, *ptr, size) != 0) return false; break;
205 case MODE_WRITE: memcpy(*ptr, data, size); break;
206 case MODE_MEASURE: break; // MODE_MEASURE - don't need to do anything
207 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;
208 default: break; // throw an error?
209 }
210 (*ptr) += size;
211 return true;
212 }
213
214 void DoVoid(void *data, int size)
215 {
216 switch (mode) {
217 case MODE_READ: memcpy(data, *ptr, size); break;
218 case MODE_WRITE: memcpy(*ptr, data, size); break;
219 case MODE_MEASURE: break; // MODE_MEASURE - don't need to do anything
220 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;
221 default: break; // throw an error?
222 }
223 (*ptr) += size;
224 }
225
226 template<class K, class T>
227 void Do(std::map<K, T *> &x)
228 {
229 if (mode == MODE_READ)
230 {
231 for (auto it = x.begin(), end = x.end(); it != end; ++it)
232 {
233 if (it->second != NULL)
234 delete it->second;
235 }
236 }
237 T *dv = NULL;
238 DoMap(x, dv);
239 }
240
241 template<class K, class T>
242 void Do(std::map<K, T> &x)
243 {
244 T dv = T();
245 DoMap(x, dv);
246 }
247
248 template<class K, class T>
249 void DoMap(std::map<K, T> &x, T &default_val)
250 {
251 unsigned int number = (unsigned int)x.size();
252 Do(number);
253 switch (mode) {
254 case MODE_READ:
255 {
256 x.clear();
257 while (number > 0)
258 {
259 K first = K();
260 Do(first);
261 T second = default_val;
262 Do(second);
263 x[first] = second;
264 --number;
265 }
266 }
267 break;
268 case MODE_WRITE:
269 case MODE_MEASURE:
270 case MODE_VERIFY:
271 {
272 typename std::map<K, T>::iterator itr = x.begin();
273 while (number > 0)
274 {
275 K first = itr->first;
276 Do(first);
277 Do(itr->second);
278 --number;
279 ++itr;
280 }
281 }
282 break;
283 }
284 }
285
286 template<class K, class T>
287 void Do(std::multimap<K, T *> &x)
288 {
289 if (mode == MODE_READ)
290 {
291 for (auto it = x.begin(), end = x.end(); it != end; ++it)
292 {
293 if (it->second != NULL)
294 delete it->second;
295 }
296 }
297 T *dv = NULL;
298 DoMultimap(x, dv);
299 }
300
301 template<class K, class T>
302 void Do(std::multimap<K, T> &x)
303 {
304 T dv = T();
305 DoMultimap(x, dv);
306 }
307
308 template<class K, class T>
309 void DoMultimap(std::multimap<K, T> &x, T &default_val)
310 {
311 unsigned int number = (unsigned int)x.size();
312 Do(number);
313 switch (mode) {
314 case MODE_READ:
315 {
316 x.clear();
317 while (number > 0)
318 {
319 K first = K();
320 Do(first);
321 T second = default_val;
322 Do(second);
323 x.insert(std::make_pair(first, second));
324 --number;
325 }
326 }
327 break;
328 case MODE_WRITE:
329 case MODE_MEASURE:
330 case MODE_VERIFY:
331 {
332 typename std::multimap<K, T>::iterator itr = x.begin();
333 while (number > 0)
334 {
335 Do(itr->first);
336 Do(itr->second);
337 --number;
338 ++itr;
339 }
340 }
341 break;
342 }
343 }
344
345 // Store vectors.
346 template<class T>
347 void Do(std::vector<T *> &x)
348 {
349 T *dv = NULL;
350 DoVector(x, dv);
351 }
352
353 template<class T>
354 void Do(std::vector<T> &x)
355 {
356 T dv = T();
357 DoVector(x, dv);
358 }
359
360
361 template<class T>
362 void DoPOD(std::vector<T> &x)
363 {
364 T dv = T();
365 DoVectorPOD(x, dv);
366 }
367
368 template<class T>
369 void Do(std::vector<T> &x, T &default_val)
370 {
371 DoVector(x, default_val);
372 }
373
374 template<class T>
375 void DoVector(std::vector<T> &x, T &default_val)
376 {
377 u32 vec_size = (u32)x.size();
378 Do(vec_size);
379 x.resize(vec_size, default_val);
380 if (vec_size > 0)
381 DoArray(&x[0], vec_size);
382 }
383
384 template<class T>
385 void DoVectorPOD(std::vector<T> &x, T &default_val)
386 {
387 u32 vec_size = (u32)x.size();
388 Do(vec_size);
389 x.resize(vec_size, default_val);
390 if (vec_size > 0)
391 DoArray(&x[0], vec_size);
392 }
393
394 // Store deques.
395 template<class T>
396 void Do(std::deque<T *> &x)
397 {
398 T *dv = NULL;
399 DoDeque(x, dv);
400 }
401
402 template<class T>
403 void Do(std::deque<T> &x)
404 {
405 T dv = T();
406 DoDeque(x, dv);
407 }
408
409 template<class T>
410 void DoDeque(std::deque<T> &x, T &default_val)
411 {
412 u32 deq_size = (u32)x.size();
413 Do(deq_size);
414 x.resize(deq_size, default_val);
415 u32 i;
416 for(i = 0; i < deq_size; i++)
417 Do(x[i]);
418 }
419
420 // Store STL lists.
421 template<class T>
422 void Do(std::list<T *> &x)
423 {
424 T *dv = NULL;
425 Do(x, dv);
426 }
427
428 template<class T>
429 void Do(std::list<T> &x)
430 {
431 T dv = T();
432 DoList(x, dv);
433 }
434
435 template<class T>
436 void Do(std::list<T> &x, T &default_val)
437 {
438 DoList(x, default_val);
439 }
440
441 template<class T>
442 void DoList(std::list<T> &x, T &default_val)
443 {
444 u32 list_size = (u32)x.size();
445 Do(list_size);
446 x.resize(list_size, default_val);
447
448 typename std::list<T>::iterator itr, end;
449 for (itr = x.begin(), end = x.end(); itr != end; ++itr)
450 Do(*itr);
451 }
452
453
454 // Store STL sets.
455 template <class T>
456 void Do(std::set<T *> &x)
457 {
458 if (mode == MODE_READ)
459 {
460 for (auto it = x.begin(), end = x.end(); it != end; ++it)
461 {
462 if (*it != NULL)
463 delete *it;
464 }
465 }
466 DoSet(x);
467 }
468
469 template <class T>
470 void Do(std::set<T> &x)
471 {
472 DoSet(x);
473 }
474
475 template <class T>
476 void DoSet(std::set<T> &x)
477 {
478 unsigned int number = (unsigned int)x.size();
479 Do(number);
480
481 switch (mode)
482 {
483 case MODE_READ:
484 {
485 x.clear();
486 while (number-- > 0)
487 {
488 T it = T();
489 Do(it);
490 x.insert(it);
491 }
492 }
493 break;
494 case MODE_WRITE:
495 case MODE_MEASURE:
496 case MODE_VERIFY:
497 {
498 typename std::set<T>::iterator itr = x.begin();
499 while (number-- > 0)
500 Do(*itr++);
501 }
502 break;
503
504 default:
505 ERROR_LOG(COMMON, "Savestate error: invalid mode %d.", mode);
506 }
507 }
508
509 // Store strings.
510 void Do(std::string &x)
511 {
512 int stringLen = (int)x.length() + 1;
513 Do(stringLen);
514
515 switch (mode) {
516 case MODE_READ: x = (char*)*ptr; break;
517 case MODE_WRITE: memcpy(*ptr, x.c_str(), stringLen); break;
518 case MODE_MEASURE: break;
519 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;
520 }
521 (*ptr) += stringLen;
522 }
523
524 void Do(std::wstring &x)
525 {
526 int stringLen = sizeof(wchar_t)*((int)x.length() + 1);
527 Do(stringLen);
528
529 switch (mode) {
530 case MODE_READ: x = (wchar_t*)*ptr; break;
531 case MODE_WRITE: memcpy(*ptr, x.c_str(), stringLen); break;
532 case MODE_MEASURE: break;
533 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;
534 }
535 (*ptr) += stringLen;
536 }
537
538 template<class T>
539 void DoClass(T &x) {
540 x.DoState(*this);
541 }
542
543 template<class T>
544 void DoClass(T *&x) {
545 if (mode == MODE_READ)
546 {
547 if (x != NULL)
548 delete x;
549 x = new T();
550 }
551 x->DoState(*this);
552 }
553
554 template<class T>
555 void DoArray(T *x, int count) {
556 DoHelper<T>::DoArray(this, x, count);
557 }
558
559 template<class T>
560 void Do(T &x) {
561 DoHelper<T>::Do(this, x);
562 }
563
564 template<class T>
565 void DoPOD(T &x) {
566 DoHelper<T>::Do(this, x);
567 }
568
569 template<class T>
570 void DoPointer(T* &x, T*const base) {
571 // pointers can be more than 2^31 apart, but you're using this function wrong if you need that much range
572 s32 offset = x - base;
573 Do(offset);
574 if (mode == MODE_READ)
575 x = base + offset;
576 }
577
578 template<class T, LinkedListItem<T>* (*TNew)(), void (*TFree)(LinkedListItem<T>*), void (*TDo)(PointerWrap&, T*)>
579 void DoLinkedList(LinkedListItem<T>*& list_start, LinkedListItem<T>** list_end=0)
580 {
581 LinkedListItem<T>* list_cur = list_start;
582 LinkedListItem<T>* prev = 0;
583
584 while (true)
585 {
586 u8 shouldExist = (list_cur ? 1 : 0);
587 Do(shouldExist);
588 if (shouldExist == 1)
589 {
590 LinkedListItem<T>* cur = list_cur ? list_cur : TNew();
591 TDo(*this, (T*)cur);
592 if (!list_cur)
593 {
594 if (mode == MODE_READ)
595 {
596 cur->next = 0;
597 list_cur = cur;
598 if (prev)
599 prev->next = cur;
600 else
601 list_start = cur;
602 }
603 else
604 {
605 TFree(cur);
606 continue;
607 }
608 }
609 }
610 else
611 {
612 if (mode == MODE_READ)
613 {
614 if (prev)
615 prev->next = 0;
616 if (list_end)
617 *list_end = prev;
618 if (list_cur)
619 {
620 if (list_start == list_cur)
621 list_start = 0;
622 do
623 {
624 LinkedListItem<T>* next = list_cur->next;
625 TFree(list_cur);
626 list_cur = next;
627 }
628 while (list_cur);
629 }
630 }
631 break;
632 }
633 prev = list_cur;
634 list_cur = list_cur->next;
635 }
636 }
637
638 void DoMarker(const char* prevName, u32 arbitraryNumber=0x42)
639 {
640 u32 cookie = arbitraryNumber;
641 Do(cookie);
642 if(mode == PointerWrap::MODE_READ && cookie != arbitraryNumber)
643 {
644 PanicAlertT("Error: After \"%s\", found %d (0x%X) instead of save marker %d (0x%X). Aborting savestate load...", prevName, cookie, cookie, arbitraryNumber, arbitraryNumber);
645 SetError(ERROR_FAILURE);
646 }
647 }
648};
649
650inline PointerWrapSection::~PointerWrapSection() {
651 if (ver_ > 0) {
652 p_.DoMarker(title_);
653 }
654}
655
656
657class CChunkFileReader
658{
659public:
660 enum Error {
661 ERROR_NONE,
662 ERROR_BAD_FILE,
663 ERROR_BROKEN_STATE,
664 };
665
666 // Load file template
667 template<class T>
668 static Error Load(const std::string& _rFilename, int _Revision, const char *_VersionString, T& _class, std::string* _failureReason)
669 {
670 INFO_LOG(COMMON, "ChunkReader: Loading %s" , _rFilename.c_str());
671 _failureReason->clear();
672 _failureReason->append("LoadStateWrongVersion");
673
674 if (!File::Exists(_rFilename)) {
675 _failureReason->clear();
676 _failureReason->append("LoadStateDoesntExist");
677 ERROR_LOG(COMMON, "ChunkReader: File doesn't exist");
678 return ERROR_BAD_FILE;
679 }
680
681 // Check file size
682 const u64 fileSize = File::GetSize(_rFilename);
683 static const u64 headerSize = sizeof(SChunkHeader);
684 if (fileSize < headerSize)
685 {
686 ERROR_LOG(COMMON,"ChunkReader: File too small");
687 return ERROR_BAD_FILE;
688 }
689
690 File::IOFile pFile(_rFilename, "rb");
691 if (!pFile)
692 {
693 ERROR_LOG(COMMON,"ChunkReader: Can't open file for reading");
694 return ERROR_BAD_FILE;
695 }
696
697 // read the header
698 SChunkHeader header;
699 if (!pFile.ReadArray(&header, 1))
700 {
701 ERROR_LOG(COMMON,"ChunkReader: Bad header size");
702 return ERROR_BAD_FILE;
703 }
704
705 // Check revision
706 if (header.Revision != _Revision)
707 {
708 ERROR_LOG(COMMON,"ChunkReader: Wrong file revision, got %d expected %d",
709 header.Revision, _Revision);
710 return ERROR_BAD_FILE;
711 }
712
713 if (strcmp(header.GitVersion, _VersionString) != 0)
714 {
715 WARN_LOG(COMMON, "This savestate was generated by a different version of PPSSPP, %s. It may not load properly.",
716 header.GitVersion);
717 }
718
719 // get size
720 const int sz = (int)(fileSize - headerSize);
721 if (header.ExpectedSize != sz)
722 {
723 ERROR_LOG(COMMON,"ChunkReader: Bad file size, got %d expected %d",
724 sz, header.ExpectedSize);
725 return ERROR_BAD_FILE;
726 }
727
728 // read the state
729 u8* buffer = new u8[sz];
730 if (!pFile.ReadBytes(buffer, sz))
731 {
732 ERROR_LOG(COMMON,"ChunkReader: Error reading file");
733 return ERROR_BAD_FILE;
734 }
735
736 u8 *ptr = buffer;
737 u8 *buf = buffer;
738 if (header.Compress) {
739 u8 *uncomp_buffer = new u8[header.UncompressedSize];
740 size_t uncomp_size = header.UncompressedSize;
741 snappy_uncompress((const char *)buffer, sz, (char *)uncomp_buffer, &uncomp_size);
742 if ((int)uncomp_size != header.UncompressedSize) {
743 ERROR_LOG(COMMON,"Size mismatch: file: %i calc: %i", (int)header.UncompressedSize, (int)uncomp_size);
744 }
745 ptr = uncomp_buffer;
746 buf = uncomp_buffer;
747 delete [] buffer;
748 }
749
750 PointerWrap p(&ptr, PointerWrap::MODE_READ);
751 _class.DoState(p);
752 delete[] buf;
753
754 INFO_LOG(COMMON, "ChunkReader: Done loading %s" , _rFilename.c_str());
755 if (p.error != p.ERROR_FAILURE) {
756 return ERROR_NONE;
757 } else {
758 return ERROR_BROKEN_STATE;
759 }
760 }
761
762 // Save file template
763 template<class T>
764 static Error Save(const std::string& _rFilename, int _Revision, const char *_VersionString, T& _class)
765 {
766 INFO_LOG(COMMON, "ChunkReader: Writing %s" , _rFilename.c_str());
767
768 File::IOFile pFile(_rFilename, "wb");
769 if (!pFile)
770 {
771 ERROR_LOG(COMMON,"ChunkReader: Error opening file for write");
772 return ERROR_BAD_FILE;
773 }
774
775 bool compress = true;
776
777 // Get data
778 u8 *ptr = 0;
779 PointerWrap p(&ptr, PointerWrap::MODE_MEASURE);
780 _class.DoState(p);
781 size_t const sz = (size_t)ptr;
782
783 u8 * buffer = new u8[sz];
784 ptr = &buffer[0];
785 p.SetMode(PointerWrap::MODE_WRITE);
786 _class.DoState(p);
787
788 // Create header
789 SChunkHeader header;
790 header.Compress = compress ? 1 : 0;
791 header.Revision = _Revision;
792 header.ExpectedSize = (int)sz;
793 header.UncompressedSize = (int)sz;
794 strncpy(header.GitVersion, _VersionString, 32);
795 header.GitVersion[31] = '\0';
796
797 // Write to file
798 if (compress) {
799 size_t comp_len = snappy_max_compressed_length(sz);
800 u8 *compressed_buffer = new u8[comp_len];
801 snappy_compress((const char *)buffer, sz, (char *)compressed_buffer, &comp_len);
802 delete [] buffer;
803 header.ExpectedSize = (int)comp_len;
804 if (!pFile.WriteArray(&header, 1))
805 {
806 ERROR_LOG(COMMON,"ChunkReader: Failed writing header");
807 return ERROR_BAD_FILE;
808 }
809 if (!pFile.WriteBytes(&compressed_buffer[0], comp_len)) {
810 ERROR_LOG(COMMON,"ChunkReader: Failed writing compressed data");
811 return ERROR_BAD_FILE;
812 } else {
813 INFO_LOG(COMMON, "Savestate: Compressed %i bytes into %i", (int)sz, (int)comp_len);
814 }
815 delete [] compressed_buffer;
816 } else {
817 if (!pFile.WriteArray(&header, 1))
818 {
819 ERROR_LOG(COMMON,"ChunkReader: Failed writing header");
820 return ERROR_BAD_FILE;
821 }
822 if (!pFile.WriteBytes(&buffer[0], sz))
823 {
824 ERROR_LOG(COMMON,"ChunkReader: Failed writing data");
825 return ERROR_BAD_FILE;
826 }
827 delete [] buffer;
828 }
829
830 INFO_LOG(COMMON,"ChunkReader: Done writing %s",
831 _rFilename.c_str());
832 if (p.error != p.ERROR_FAILURE) {
833 return ERROR_NONE;
834 } else {
835 return ERROR_BROKEN_STATE;
836 }
837 }
838
839 template <class T>
840 static Error Verify(T& _class)
841 {
842 u8 *ptr = 0;
843
844 // Step 1: Measure the space required.
845 PointerWrap p(&ptr, PointerWrap::MODE_MEASURE);
846 _class.DoState(p);
847 size_t const sz = (size_t)ptr;
848 std::vector<u8> buffer(sz);
849
850 // Step 2: Dump the state.
851 ptr = &buffer[0];
852 p.SetMode(PointerWrap::MODE_WRITE);
853 _class.DoState(p);
854
855 // Step 3: Verify the state.
856 ptr = &buffer[0];
857 p.SetMode(PointerWrap::MODE_VERIFY);
858 _class.DoState(p);
859
860 return ERROR_NONE;
861 }
862
863private:
864 struct SChunkHeader
865 {
866 int Revision;
867 int Compress;
868 int ExpectedSize;
869 int UncompressedSize;
870 char GitVersion[32];
871 };
872};
873
874#endif // _POINTERWRAP_H_