118 lines
6.7 KiB
C++
118 lines
6.7 KiB
C++
#include "core/save_snapshot.h"
|
|
#include <cmath>
|
|
#include <cstring>
|
|
#include <fstream>
|
|
#include <limits>
|
|
#include <unordered_set>
|
|
#ifdef _WIN32
|
|
#include <windows.h>
|
|
#else
|
|
#include <unistd.h>
|
|
#endif
|
|
|
|
namespace playground {
|
|
namespace {
|
|
constexpr std::string_view magic("CHSV\x01\x00\x00\x00", 8);
|
|
bool fail(std::string* error, const char* message) { if (error) *error = message; return false; }
|
|
struct Writer {
|
|
std::string& out; const SaveLimits& limits; std::string* error;
|
|
size_t entries = 0; std::unordered_set<const SaveTable*> active;
|
|
bool append(const void* p, size_t n) {
|
|
if (n > limits.maxBytes || out.size() > limits.maxBytes - n) return fail(error, "save exceeds byte limit");
|
|
out.append(static_cast<const char*>(p), n); return true;
|
|
}
|
|
bool byte(uint8_t n) { return append(&n, 1); }
|
|
bool u32(uint32_t n) { uint8_t b[4]; for (int i=0;i<4;++i) b[i]=uint8_t(n >> (8*i)); return append(b,4); }
|
|
bool value(const SaveValue& v, size_t depth) {
|
|
if (depth > limits.maxDepth) return fail(error, "save exceeds depth limit");
|
|
if (auto p=std::get_if<bool>(&v.data)) return byte(*p ? 2 : 1);
|
|
if (auto p=std::get_if<double>(&v.data)) {
|
|
if (!std::isfinite(*p)) return fail(error, "nonfinite save number");
|
|
uint64_t bits; std::memcpy(&bits,p,8); uint8_t b[8];
|
|
for (int i=0;i<8;++i) b[i]=uint8_t(bits >> (8*i));
|
|
return byte(3) && append(b,8);
|
|
}
|
|
if (auto p=std::get_if<std::string>(&v.data)) {
|
|
if (p->size()>limits.maxStringBytes || p->size()>UINT32_MAX) return fail(error,"save string too long");
|
|
return byte(4) && u32(uint32_t(p->size())) && append(p->data(),p->size());
|
|
}
|
|
auto p=std::get<SaveValue::Table>(v.data);
|
|
if (!p) return fail(error,"null save table");
|
|
if (!active.insert(p.get()).second) return fail(error,"cyclic save table");
|
|
if (p->entries.size()>UINT32_MAX || p->entries.size()>limits.maxEntries-entries) return fail(error,"too many save entries");
|
|
entries += p->entries.size();
|
|
if (!byte(5) || !u32(uint32_t(p->entries.size()))) return false;
|
|
for (const auto& e:p->entries) {
|
|
if (auto k=std::get_if<std::string>(&e.key.data)) { if (k->empty()) return fail(error,"empty save key"); }
|
|
else if (!std::holds_alternative<double>(e.key.data)) return fail(error,"invalid save key type");
|
|
if (!value(e.key,depth+1) || !value(e.value,depth+1)) return false;
|
|
}
|
|
active.erase(p.get()); return true;
|
|
}
|
|
};
|
|
struct Reader {
|
|
std::string_view in; const SaveLimits& limits; std::string* error;
|
|
size_t at=0, entries=0;
|
|
bool take(void* p,size_t n) {
|
|
if (n>in.size()-at) return fail(error,"truncated save");
|
|
std::memcpy(p,in.data()+at,n); at+=n; return true;
|
|
}
|
|
bool byte(uint8_t& n) { return take(&n,1); }
|
|
bool u32(uint32_t& n) { uint8_t b[4]; if(!take(b,4))return false; n=0; for(int i=0;i<4;++i)n|=uint32_t(b[i])<<(8*i); return true; }
|
|
bool value(SaveValue& v,size_t depth) {
|
|
if(depth>limits.maxDepth)return fail(error,"save exceeds depth limit");
|
|
uint8_t tag; if(!byte(tag))return false;
|
|
if(tag==1 || tag==2){v=SaveValue(tag==2);return true;}
|
|
if(tag==3){uint8_t b[8];if(!take(b,8))return false;uint64_t bits=0;for(int i=0;i<8;++i)bits|=uint64_t(b[i])<<(8*i);double n;std::memcpy(&n,&bits,8);if(!std::isfinite(n))return fail(error,"nonfinite save number");v=SaveValue(n);return true;}
|
|
if(tag==4){uint32_t n;if(!u32(n))return false;if(n>limits.maxStringBytes || n>in.size()-at)return fail(error,"invalid save string length");v=SaveValue(std::string(in.substr(at,n)));at+=n;return true;}
|
|
if(tag!=5)return fail(error,"unknown save value type");
|
|
uint32_t count;if(!u32(count))return false;
|
|
if(count>limits.maxEntries-entries || count>(in.size()-at)/2)return fail(error,"invalid save entry count");
|
|
entries+=count;auto table=std::make_shared<SaveTable>();table->entries.reserve(count);
|
|
for(uint32_t i=0;i<count;++i){SaveEntry e;if(!value(e.key,depth+1))return false;
|
|
if(auto k=std::get_if<std::string>(&e.key.data)){if(k->empty())return fail(error,"empty save key");}
|
|
else if(!std::holds_alternative<double>(e.key.data))return fail(error,"invalid save key type");
|
|
if(!value(e.value,depth+1))return false;table->entries.push_back(std::move(e));}
|
|
v=SaveValue(table);return true;
|
|
}
|
|
};
|
|
} // namespace
|
|
|
|
bool encodeSave(const SaveValue& root,std::string& bytes,std::string* error,const SaveLimits& limits){
|
|
std::string result;Writer w{result,limits,error};
|
|
if(!w.append(magic.data(),magic.size()) || !w.value(root,0))return false;
|
|
bytes=std::move(result);return true;
|
|
}
|
|
bool decodeSave(std::string_view bytes,SaveValue& root,std::string* error,const SaveLimits& limits){
|
|
if(bytes.size()>limits.maxBytes)return fail(error,"save exceeds byte limit");
|
|
if(bytes.size()<magic.size() || bytes.substr(0,magic.size())!=magic)return fail(error,"unsupported save version");
|
|
Reader r{bytes,limits,error};r.at=magic.size();SaveValue result;
|
|
if(!r.value(result,0))return false;
|
|
if(r.at!=bytes.size())return fail(error,"trailing save data");
|
|
root=std::move(result);return true;
|
|
}
|
|
bool writeSaveAtomic(const std::filesystem::path& path,std::string_view bytes,std::string* error){
|
|
if(path.empty())return fail(error,"empty save path");
|
|
auto temporary=path;temporary += ".tmp";
|
|
{std::ofstream out(temporary,std::ios::binary|std::ios::trunc);
|
|
if(!out)return fail(error,"cannot open temporary save");
|
|
out.write(bytes.data(),std::streamsize(bytes.size()));out.flush();
|
|
if(!out){out.close();std::error_code ec;std::filesystem::remove(temporary,ec);return fail(error,"cannot write temporary save");}}
|
|
#ifdef _WIN32
|
|
if(!MoveFileExW(temporary.c_str(),path.c_str(),MOVEFILE_REPLACE_EXISTING|MOVEFILE_WRITE_THROUGH)){
|
|
#else
|
|
std::error_code renameError;std::filesystem::rename(temporary,path,renameError);if(renameError){
|
|
#endif
|
|
std::error_code ec;std::filesystem::remove(temporary,ec);return fail(error,"cannot replace save");
|
|
}
|
|
return true;
|
|
}
|
|
bool readSave(const std::filesystem::path& path,std::string& bytes,std::string* error,const SaveLimits& limits){
|
|
std::error_code ec;auto size=std::filesystem::file_size(path,ec);
|
|
if(ec)return fail(error,"cannot stat save");
|
|
if(size>limits.maxBytes)return fail(error,"save exceeds byte limit");
|
|
std::ifstream in(path,std::ios::binary);if(!in)return fail(error,"cannot open save");
|
|
std::string result(size,'\0');if(size && !in.read(result.data(),std::streamsize(size)))return fail(error,"cannot read save");
|
|
bytes=std::move(result);return true;
|
|
}
|
|
} // namespace playground
|