2012-11-01 15:19:01 +00:00
|
|
|
// 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
|
2012-11-04 22:01:49 +00:00
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
// 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/
|
|
|
|
|
2013-10-07 04:26:08 +00:00
|
|
|
#pragma once
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2017-03-07 19:10:18 +00:00
|
|
|
#include "ppsspp_config.h"
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
// Extremely simple serialization framework.
|
2014-06-22 15:02:04 +00:00
|
|
|
// Currently mis-named, a native ChunkFile is something different (a RIFF file)
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
// (mis)-features:
|
|
|
|
// + Super fast
|
|
|
|
// + Very simple
|
|
|
|
// + Same code is used for serialization and deserializaition (in most cases)
|
2013-10-07 04:26:08 +00:00
|
|
|
// + Sections can be versioned for backwards/forwards compatibility
|
2012-11-01 15:19:01 +00:00
|
|
|
// - Serialization code for anything complex has to be manually written.
|
|
|
|
|
2020-08-10 06:38:45 +00:00
|
|
|
#include <string>
|
2022-12-02 09:55:03 +00:00
|
|
|
#include <cstring>
|
2020-08-10 06:38:45 +00:00
|
|
|
#include <vector>
|
2020-10-14 22:03:47 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
|
2020-08-10 06:38:45 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
|
|
|
#include "Common/Log.h"
|
2021-05-05 23:31:38 +00:00
|
|
|
#include "Common/File/Path.h"
|
2020-08-10 06:38:45 +00:00
|
|
|
|
|
|
|
namespace File {
|
|
|
|
class IOFile;
|
|
|
|
};
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
template <class T>
|
|
|
|
struct LinkedListItem : public T
|
|
|
|
{
|
|
|
|
LinkedListItem<T> *next;
|
|
|
|
};
|
|
|
|
|
2013-09-15 03:19:58 +00:00
|
|
|
class PointerWrap;
|
|
|
|
|
|
|
|
class PointerWrapSection
|
|
|
|
{
|
|
|
|
public:
|
2022-12-02 09:55:03 +00:00
|
|
|
PointerWrapSection(PointerWrap &p, int ver, const char *title) : p_(p), ver_(ver), title_(title) {}
|
2013-09-15 03:19:58 +00:00
|
|
|
~PointerWrapSection();
|
|
|
|
|
|
|
|
bool operator == (const int &v) const { return ver_ == v; }
|
|
|
|
bool operator != (const int &v) const { return ver_ != v; }
|
|
|
|
bool operator <= (const int &v) const { return ver_ <= v; }
|
|
|
|
bool operator >= (const int &v) const { return ver_ >= v; }
|
|
|
|
bool operator < (const int &v) const { return ver_ < v; }
|
|
|
|
bool operator > (const int &v) const { return ver_ > v; }
|
|
|
|
|
2022-12-19 15:56:23 +00:00
|
|
|
operator bool() const {
|
2013-09-15 03:19:58 +00:00
|
|
|
return ver_ > 0;
|
|
|
|
}
|
|
|
|
|
2022-12-19 15:56:23 +00:00
|
|
|
int Version() const {
|
|
|
|
return ver_;
|
|
|
|
}
|
|
|
|
|
2013-09-15 03:19:58 +00:00
|
|
|
private:
|
|
|
|
PointerWrap &p_;
|
|
|
|
int ver_;
|
|
|
|
const char *title_;
|
|
|
|
};
|
|
|
|
|
2022-12-02 09:55:03 +00:00
|
|
|
// For measure vs write detailed verification
|
|
|
|
struct SerializeCheckpoint {
|
|
|
|
char title[17]; // 16-byte section header, plus a zero terminator for debug printing.
|
|
|
|
size_t offset;
|
|
|
|
|
|
|
|
SerializeCheckpoint(char _title[16], size_t off) {
|
|
|
|
memcpy(title, _title, 16);
|
|
|
|
title[16] = 0;
|
|
|
|
offset = off;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Matches(const char *_title, size_t off) const {
|
|
|
|
return memcmp(title, _title, 16) == 0 && off == offset;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
// Wrapper class
|
|
|
|
class PointerWrap
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum Mode {
|
|
|
|
MODE_READ = 1, // load
|
2022-12-02 12:28:06 +00:00
|
|
|
MODE_WRITE, // save
|
|
|
|
MODE_MEASURE, // calculate size
|
|
|
|
MODE_VERIFY, // compare
|
|
|
|
MODE_NOOP, // don't do anything. Useful to cleanly doing stuff once we've hit an error.
|
2012-11-01 15:19:01 +00:00
|
|
|
};
|
|
|
|
|
2013-04-13 08:39:17 +00:00
|
|
|
enum Error {
|
|
|
|
ERROR_NONE = 0,
|
|
|
|
ERROR_WARNING = 1,
|
|
|
|
ERROR_FAILURE = 2,
|
|
|
|
};
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
u8 **ptr;
|
|
|
|
Mode mode;
|
2020-08-02 15:11:09 +00:00
|
|
|
Error error = ERROR_NONE;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2022-12-02 21:45:28 +00:00
|
|
|
PointerWrap(u8 **ptr_, Mode mode_) : ptr(ptr_), ptrStart_(*ptr), mode(mode_) {
|
|
|
|
if (mode == MODE_MEASURE) {
|
|
|
|
checkpoints_.reserve(750);
|
|
|
|
}
|
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2022-12-02 09:55:03 +00:00
|
|
|
void RewindForWrite(u8 *writePtr);
|
|
|
|
bool CheckAfterWrite();
|
2013-09-15 03:19:58 +00:00
|
|
|
|
|
|
|
// The returned object can be compared against the version that was loaded.
|
|
|
|
// This can be used to support versions as old as minVer.
|
2013-09-15 14:58:52 +00:00
|
|
|
// Version = 0 means the section was not found.
|
2013-10-07 04:26:08 +00:00
|
|
|
PointerWrapSection Section(const char *title, int minVer, int ver);
|
2022-12-02 09:55:03 +00:00
|
|
|
PointerWrapSection Section(const char *title, int ver) {
|
|
|
|
return Section(title, ver, ver);
|
|
|
|
}
|
2013-09-15 03:19:58 +00:00
|
|
|
|
2022-12-02 09:55:03 +00:00
|
|
|
void SetMode(Mode mode_) { mode = mode_; }
|
|
|
|
Mode GetMode() const { return mode; }
|
|
|
|
u8 **GetPPtr() { return ptr; }
|
2013-10-07 04:26:08 +00:00
|
|
|
void SetError(Error error_);
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2020-08-02 15:11:09 +00:00
|
|
|
const char *GetBadSectionTitle() const {
|
|
|
|
return firstBadSectionTitle_;
|
|
|
|
}
|
|
|
|
|
2013-10-07 04:26:08 +00:00
|
|
|
// Same as DoVoid, except doesn't advance pointer if it doesn't match on read.
|
|
|
|
bool ExpectVoid(void *data, int size);
|
|
|
|
void DoVoid(void *data, int size);
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2013-10-07 04:26:08 +00:00
|
|
|
void DoMarker(const char *prevName, u32 arbitraryNumber = 0x42);
|
2020-08-10 04:20:42 +00:00
|
|
|
|
2023-07-01 10:31:46 +00:00
|
|
|
void SkipBytes(size_t bytes) {
|
|
|
|
// Should work in all modes.
|
|
|
|
*ptr += bytes;
|
|
|
|
}
|
|
|
|
|
2022-12-02 09:55:03 +00:00
|
|
|
size_t Offset() const { return *ptr - ptrStart_; }
|
|
|
|
|
2022-12-02 10:29:59 +00:00
|
|
|
private:
|
2020-08-10 04:20:42 +00:00
|
|
|
const char *firstBadSectionTitle_ = nullptr;
|
2022-12-02 09:55:03 +00:00
|
|
|
u8 *ptrStart_;
|
|
|
|
std::vector<SerializeCheckpoint> checkpoints_;
|
|
|
|
size_t curCheckpoint_ = 0;
|
|
|
|
size_t measuredSize_ = 0;
|
2012-11-01 15:19:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class CChunkFileReader
|
|
|
|
{
|
|
|
|
public:
|
2013-09-15 04:19:10 +00:00
|
|
|
enum Error {
|
|
|
|
ERROR_NONE,
|
|
|
|
ERROR_BAD_FILE,
|
|
|
|
ERROR_BROKEN_STATE,
|
2017-03-07 14:19:20 +00:00
|
|
|
ERROR_BAD_ALLOC,
|
2013-09-15 04:19:10 +00:00
|
|
|
};
|
|
|
|
|
2013-09-29 20:54:39 +00:00
|
|
|
// May fail badly if ptr doesn't point to valid data.
|
|
|
|
template<class T>
|
2020-08-02 15:11:09 +00:00
|
|
|
static Error LoadPtr(u8 *ptr, T &_class, std::string *errorString)
|
2013-09-29 20:54:39 +00:00
|
|
|
{
|
|
|
|
PointerWrap p(&ptr, PointerWrap::MODE_READ);
|
|
|
|
_class.DoState(p);
|
|
|
|
|
|
|
|
if (p.error != p.ERROR_FAILURE) {
|
|
|
|
return ERROR_NONE;
|
|
|
|
} else {
|
2021-07-24 16:10:38 +00:00
|
|
|
std::string badSectionTitle = p.GetBadSectionTitle() ? p.GetBadSectionTitle() : "(unknown bad section)";
|
|
|
|
*errorString = std::string("Failure at ") + badSectionTitle;
|
2013-09-29 20:54:39 +00:00
|
|
|
return ERROR_BROKEN_STATE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
static size_t MeasurePtr(T &_class)
|
|
|
|
{
|
2022-12-02 10:29:59 +00:00
|
|
|
u8 *ptr = nullptr;
|
2013-09-29 20:54:39 +00:00
|
|
|
PointerWrap p(&ptr, PointerWrap::MODE_MEASURE);
|
|
|
|
_class.DoState(p);
|
|
|
|
return (size_t)ptr;
|
|
|
|
}
|
|
|
|
|
2023-02-13 09:56:09 +00:00
|
|
|
// If *saved is null, will allocate storage using malloc.
|
|
|
|
// If it's not null, it will be used, but only hope can save you from overruns at the end. For libretro.
|
2013-09-29 20:54:39 +00:00
|
|
|
template<class T>
|
2023-02-13 09:56:09 +00:00
|
|
|
static Error MeasureAndSavePtr(T &_class, u8 **saved, size_t *savedSize)
|
2013-09-29 20:54:39 +00:00
|
|
|
{
|
2023-02-13 09:56:09 +00:00
|
|
|
u8 *ptr = nullptr;
|
|
|
|
PointerWrap p(&ptr, PointerWrap::MODE_MEASURE);
|
|
|
|
_class.DoState(p);
|
|
|
|
_assert_(p.error == PointerWrap::ERROR_NONE);
|
|
|
|
|
|
|
|
size_t measuredSize = p.Offset();
|
|
|
|
u8 *data;
|
|
|
|
if (*saved) {
|
|
|
|
data = *saved;
|
|
|
|
} else {
|
|
|
|
data = (u8 *)malloc(measuredSize);
|
|
|
|
if (!data)
|
|
|
|
return ERROR_BAD_ALLOC;
|
|
|
|
}
|
|
|
|
|
|
|
|
p.RewindForWrite(data);
|
2013-09-29 20:54:39 +00:00
|
|
|
_class.DoState(p);
|
|
|
|
|
2023-02-13 09:56:09 +00:00
|
|
|
if (p.CheckAfterWrite()) {
|
|
|
|
*saved = data;
|
|
|
|
*savedSize = measuredSize;
|
2013-09-29 20:54:39 +00:00
|
|
|
return ERROR_NONE;
|
|
|
|
} else {
|
2023-02-14 09:09:22 +00:00
|
|
|
if (!*saved) {
|
|
|
|
free(data);
|
|
|
|
}
|
2013-09-29 20:54:39 +00:00
|
|
|
return ERROR_BROKEN_STATE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-13 09:56:09 +00:00
|
|
|
// Duplicate of the above but takes and modifies a vector. Less invasive
|
|
|
|
// than modifying the rewind manager to keep things in something else than vectors.
|
2022-12-02 10:29:59 +00:00
|
|
|
template<class T>
|
2023-02-13 09:56:09 +00:00
|
|
|
static Error MeasureAndSavePtr(T &_class, std::vector<u8> *saved)
|
2022-12-02 10:29:59 +00:00
|
|
|
{
|
|
|
|
u8 *ptr = nullptr;
|
|
|
|
PointerWrap p(&ptr, PointerWrap::MODE_MEASURE);
|
|
|
|
_class.DoState(p);
|
|
|
|
_assert_(p.error == PointerWrap::ERROR_NONE);
|
|
|
|
|
|
|
|
size_t measuredSize = p.Offset();
|
2023-02-13 09:56:09 +00:00
|
|
|
saved->resize(measuredSize);
|
|
|
|
u8 *data = saved->data();
|
2022-12-02 10:29:59 +00:00
|
|
|
p.RewindForWrite(data);
|
|
|
|
_class.DoState(p);
|
|
|
|
if (p.CheckAfterWrite()) {
|
|
|
|
return ERROR_NONE;
|
|
|
|
} else {
|
2023-02-13 09:56:09 +00:00
|
|
|
saved->clear();
|
2022-12-02 10:29:59 +00:00
|
|
|
return ERROR_BROKEN_STATE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-13 09:56:09 +00:00
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
// Load file template
|
|
|
|
template<class T>
|
2021-05-05 23:31:38 +00:00
|
|
|
static Error Load(const Path &filename, std::string *gitVersion, T& _class, std::string *failureReason)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2016-01-23 20:53:03 +00:00
|
|
|
*failureReason = "LoadStateWrongVersion";
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2015-10-04 10:09:05 +00:00
|
|
|
u8 *ptr = nullptr;
|
2013-10-07 04:26:08 +00:00
|
|
|
size_t sz;
|
2016-01-23 20:53:03 +00:00
|
|
|
Error error = LoadFile(filename, gitVersion, ptr, sz, failureReason);
|
2013-10-07 04:26:08 +00:00
|
|
|
if (error == ERROR_NONE) {
|
2016-01-23 20:53:03 +00:00
|
|
|
failureReason->clear();
|
2020-08-02 15:11:09 +00:00
|
|
|
error = LoadPtr(ptr, _class, failureReason);
|
|
|
|
delete [] ptr;
|
|
|
|
INFO_LOG(SAVESTATE, "ChunkReader: Done loading '%s'", filename.c_str());
|
|
|
|
} else {
|
|
|
|
WARN_LOG(SAVESTATE, "ChunkReader: Error found during load of '%s'", filename.c_str());
|
2013-10-07 04:26:08 +00:00
|
|
|
}
|
2013-09-29 20:54:39 +00:00
|
|
|
return error;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
2013-09-29 20:54:39 +00:00
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
// Save file template
|
|
|
|
template<class T>
|
2021-05-05 23:31:38 +00:00
|
|
|
static Error Save(const Path &filename, const std::string &title, const char *gitVersion, T& _class)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2023-02-14 21:46:32 +00:00
|
|
|
u8 *buffer = nullptr;
|
|
|
|
size_t sz = 0;
|
2022-12-02 10:29:59 +00:00
|
|
|
Error error = MeasureAndSavePtr(_class, &buffer, &sz);
|
2013-01-02 21:10:41 +00:00
|
|
|
|
2022-12-02 09:55:03 +00:00
|
|
|
// SaveFile takes ownership of buffer (malloc/free)
|
2013-10-07 04:26:08 +00:00
|
|
|
if (error == ERROR_NONE)
|
2016-01-23 20:53:03 +00:00
|
|
|
error = SaveFile(filename, title, gitVersion, buffer, sz);
|
2013-09-29 20:54:39 +00:00
|
|
|
return error;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
2022-12-02 10:29:59 +00:00
|
|
|
|
2012-12-27 18:34:22 +00:00
|
|
|
template <class T>
|
2013-09-15 04:19:10 +00:00
|
|
|
static Error Verify(T& _class)
|
2012-12-27 18:34:22 +00:00
|
|
|
{
|
|
|
|
u8 *ptr = 0;
|
|
|
|
|
|
|
|
// Step 1: Measure the space required.
|
|
|
|
PointerWrap p(&ptr, PointerWrap::MODE_MEASURE);
|
|
|
|
_class.DoState(p);
|
|
|
|
size_t const sz = (size_t)ptr;
|
|
|
|
std::vector<u8> buffer(sz);
|
|
|
|
|
|
|
|
// Step 2: Dump the state.
|
|
|
|
ptr = &buffer[0];
|
|
|
|
p.SetMode(PointerWrap::MODE_WRITE);
|
|
|
|
_class.DoState(p);
|
|
|
|
|
|
|
|
// Step 3: Verify the state.
|
|
|
|
ptr = &buffer[0];
|
|
|
|
p.SetMode(PointerWrap::MODE_VERIFY);
|
|
|
|
_class.DoState(p);
|
|
|
|
|
2013-09-15 04:19:10 +00:00
|
|
|
return ERROR_NONE;
|
2012-12-27 18:34:22 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 23:31:38 +00:00
|
|
|
static Error GetFileTitle(const Path &filename, std::string *title);
|
2013-10-07 04:26:08 +00:00
|
|
|
|
2016-01-23 20:53:03 +00:00
|
|
|
private:
|
2012-11-01 15:19:01 +00:00
|
|
|
struct SChunkHeader
|
|
|
|
{
|
|
|
|
int Revision;
|
|
|
|
int Compress;
|
2013-10-19 21:16:07 +00:00
|
|
|
u32 ExpectedSize;
|
|
|
|
u32 UncompressedSize;
|
2013-07-17 05:33:26 +00:00
|
|
|
char GitVersion[32];
|
2012-11-01 15:19:01 +00:00
|
|
|
};
|
2016-01-23 20:53:03 +00:00
|
|
|
|
|
|
|
enum {
|
|
|
|
REVISION_MIN = 4,
|
|
|
|
REVISION_TITLE = 5,
|
|
|
|
REVISION_CURRENT = REVISION_TITLE,
|
|
|
|
};
|
|
|
|
|
2021-05-05 23:31:38 +00:00
|
|
|
static Error LoadFile(const Path &filename, std::string *gitVersion, u8 *&buffer, size_t &sz, std::string *failureReason);
|
|
|
|
static Error SaveFile(const Path &filename, const std::string &title, const char *gitVersion, u8 *buffer, size_t sz);
|
2016-01-23 20:53:03 +00:00
|
|
|
static Error LoadFileHeader(File::IOFile &pFile, SChunkHeader &header, std::string *title);
|
2012-11-01 15:19:01 +00:00
|
|
|
};
|