From 83971ed7f55794a61e1d249c59867184a6eb97ba Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 2 Nov 2021 16:38:24 +0200 Subject: [PATCH] Recognize and handle raw data files in the data tool --- source/rawdata.cpp | 25 +++++++++++++++++++++++++ source/rawdata.h | 2 ++ tool/packer.cpp | 20 ++++++++++++++++++++ tool/packer.h | 2 ++ tool/tool.h | 1 + 5 files changed, 50 insertions(+) diff --git a/source/rawdata.cpp b/source/rawdata.cpp index b912a56..de4d7c2 100644 --- a/source/rawdata.cpp +++ b/source/rawdata.cpp @@ -96,5 +96,30 @@ void RawData::load_into(void *buffer) in = nullptr; } +void RawData::write_io(IO::Base &io, bool compress) +{ + if(!data) + throw logic_error("no data"); + + io.write(signature, sizeof(signature)); + + for(unsigned i=56; i<64; i-=8) + io.put((size>>i)&0xFF); + + uint16_t flags = 0; + if(compress) + flags |= COMPRESSED; + io.put((flags>>8)&0xFF); + io.put(flags&0xFF); + + if(compress) + { + IO::ZlibCompressed z(io, IO::M_WRITE); + z.write(data, size); + } + else + io.write(data, size); +} + } // namespace DataFile } // namespace Msp diff --git a/source/rawdata.h b/source/rawdata.h index 2744744..ad26557 100644 --- a/source/rawdata.h +++ b/source/rawdata.h @@ -38,6 +38,8 @@ public: void load(); void load_into(void *); + void write_io(IO::Base &, bool = false); + std::size_t get_size() const { return size; } const void *get_data() const { return data; } }; diff --git a/tool/packer.cpp b/tool/packer.cpp index fa4d7e2..2e773a2 100644 --- a/tool/packer.cpp +++ b/tool/packer.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -57,6 +58,8 @@ void Packer::pack_file(const string &fn) ObjectList objects; if(detect_data(in)) transfer_datafile(in, fn, *tmp_file, objects); + else if(detect_raw(in)) + transfer_raw_data(in, fn, *tmp_file); else transfer_unknown(in, *tmp_file); @@ -91,6 +94,15 @@ bool Packer::detect_data(IO::Seekable &in) return data; } +bool Packer::detect_raw(IO::Seekable &in) +{ + char header[4] = { }; + IO::SeekOffset offset = in.tell(); + in.read(header, sizeof(header)); + in.seek(offset, IO::S_BEG); + return DataFile::RawData::detect_signature(string(header, sizeof(header))); +} + void Packer::transfer_datafile(IO::Base &in, const string &fn, IO::Base &out, ObjectList &objects) { DataFile::Parser parser(in, fn); @@ -123,6 +135,14 @@ void Packer::transfer_datafile(IO::Base &in, const string &fn, IO::Base &out, Ob delete writer; } +void Packer::transfer_raw_data(IO::Base &in, const string &fn, IO::Base &out) +{ + DataFile::RawData raw; + raw.open_io(in, fn); + raw.load(); + raw.write_io(out, tool.is_compressed()); +} + void Packer::transfer_unknown(IO::Base &in, IO::Base &out) { while(!in.eof()) diff --git a/tool/packer.h b/tool/packer.h index 151947f..313ee03 100644 --- a/tool/packer.h +++ b/tool/packer.h @@ -34,7 +34,9 @@ public: void pack_file(const std::string &); private: bool detect_data(Msp::IO::Seekable &); + bool detect_raw(Msp::IO::Seekable &); void transfer_datafile(Msp::IO::Base &, const std::string &, Msp::IO::Base &, ObjectList &); + void transfer_raw_data(Msp::IO::Base &, const std::string &, Msp::IO::Base &); void transfer_unknown(Msp::IO::Base &, Msp::IO::Base &); public: void create_pack(const std::string &); diff --git a/tool/tool.h b/tool/tool.h index 0333c0d..354456c 100644 --- a/tool/tool.h +++ b/tool/tool.h @@ -34,6 +34,7 @@ private: Msp::IO::Base *open_output(const std::string &); Msp::IO::Base *open_input(const std::string &); public: + bool is_compressed() const { return compress; } Msp::DataFile::Writer *create_writer(Msp::IO::Base &); }; -- 2.45.2