From 3dfd1fabdcf3d2c0df6e524368ef9d07b286357f Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 2 Jan 2022 12:14:47 +0200 Subject: [PATCH] Use explicit lengths in builtin data initialization This allows adding binary files which may contain embedded nul bytes. --- tool/builtingenerator.cpp | 13 ++++++++----- tool/builtingenerator.h | 8 +++++++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/tool/builtingenerator.cpp b/tool/builtingenerator.cpp index 45d5844..45465db 100644 --- a/tool/builtingenerator.cpp +++ b/tool/builtingenerator.cpp @@ -14,7 +14,7 @@ BuiltinGenerator::BuiltinGenerator(IO::Base &o): void BuiltinGenerator::begin(const std::string &ns) { - if(!namespc.empty() || !filenames.empty()) + if(!namespc.empty() || !files.empty()) throw logic_error("BuiltinGenerator::begin"); if(!ns.empty()) @@ -30,7 +30,9 @@ void BuiltinGenerator::add_file(const std::string &fn) IO::BufferedFile in(fn); string base_fn = FS::basename(fn); - filenames.push_back(base_fn); + files.emplace_back(); + File &file = files.back(); + file.filename = base_fn; out.write(format("\nconst char %s_data[] =\n", mangle_filename(base_fn))); string line; @@ -47,14 +49,15 @@ void BuiltinGenerator::add_file(const std::string &fn) out.put(';'); out.put('\n'); } + file.size += len; } } void BuiltinGenerator::end(const string &module_name) { out.write(format("\nvoid init_%s(DataFile::BuiltinSource &source)\n{\n", module_name)); - for(vector::const_iterator i=filenames.begin(); i!=filenames.end(); ++i) - out.write(format(" source.add_object(\"%s\", %s_data);\n", *i, mangle_filename(*i))); + for(vector::const_iterator i=files.begin(); i!=files.end(); ++i) + out.write(format(" source.add_object(\"%s\", %s_data, %d);\n", i->filename, mangle_filename(i->filename), i->size)); out.write("}\n"); if(!namespc.empty()) @@ -64,7 +67,7 @@ void BuiltinGenerator::end(const string &module_name) out.write(format("} // namespace %s\n", *i)); namespc.clear(); - filenames.clear(); + files.clear(); } } diff --git a/tool/builtingenerator.h b/tool/builtingenerator.h index d6a0886..ddae274 100644 --- a/tool/builtingenerator.h +++ b/tool/builtingenerator.h @@ -8,9 +8,15 @@ class BuiltinGenerator { private: + struct File + { + std::string filename; + std::size_t size = 0; + }; + Msp::IO::Base &out; std::vector namespc; - std::vector filenames; + std::vector files; public: BuiltinGenerator(Msp::IO::Base &); -- 2.45.2