#include "core/save_snapshot.h" #include #include #include #include #include #ifdef _WIN32 #include #else #include #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 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(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(&v.data)) return byte(*p ? 2 : 1); if (auto p=std::get_if(&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(&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(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(&e.key.data)) { if (k->empty()) return fail(error,"empty save key"); } else if (!std::holds_alternative(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();table->entries.reserve(count); for(uint32_t i=0;i(&e.key.data)){if(k->empty())return fail(error,"empty save key");} else if(!std::holds_alternative(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()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