Skip to content

Commit

Permalink
Add function to extract bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
milasudril committed Jun 18, 2023
1 parent 0d4fbda commit 70b8911
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
19 changes: 19 additions & 0 deletions lib/wad64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,25 @@ void Wad64::extract(ArchiveView const& archive,
}
}

void Wad64::extract(ArchiveView const& archive,
std::string_view src_name,
std::vector<std::byte>& dest)
{
InputFile file_in{archive, src_name};
auto buffer = std::make_unique<std::array<std::byte, 0x10000>>();

auto bytes_left = static_cast<size_t>(file_in.size());
dest.reserve(bytes_left);

while(bytes_left != 0)
{
auto const n =
read(file_in, std::span{buffer->data(), std::min(buffer->size(), bytes_left)});
dest.insert(std::end(dest), buffer->data(), buffer->data() + n);
bytes_left -= n;
}
}

void Wad64::extract(ArchiveView const& archive,
FileCreationMode mode,
std::span<std::pair<std::string, std::filesystem::path> const> names,
Expand Down
4 changes: 4 additions & 0 deletions lib/wad64.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ namespace Wad64
std::string_view src_name,
char const* dest_name);

void extract(ArchiveView const& archive,
std::string_view src_name,
std::vector<std::byte>& dest);

void extract(ArchiveView const& archive,
FileCreationMode mode,
std::span<std::pair<std::string, std::filesystem::path> const> names,
Expand Down
3 changes: 3 additions & 0 deletions lib/wad64.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ def ls(self):
def extract_file(self, file_creation_mode, src_name ,dest_name):
wad64py.extract_file(self.handle, file_creation_mode, src_name, dest_name)

def extract_data(self, src_file):
return wad64py.extract_data(self.handle, src_file)

def insert_file(self, file_creation_mode, src_name, dest_name):
wad64py.insert_file(self.handle, file_creation_mode, src_name, dest_name)

Expand Down
30 changes: 29 additions & 1 deletion lib/wad64py.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,33 @@ namespace
return Py_None;
}

PyObject* extract_data(PyObject*, PyObject* args)
{
PyObject* obj{};
char const* src_file{};

assert(PyArg_ParseTuple(args, "Os", &obj, &src_file));

auto const obj_ptr = PyLong_AsVoidPtr(obj);
assert(obj_ptr != nullptr);
auto& archive = *reinterpret_cast<Archive*>(obj_ptr);

try
{
std::vector<std::byte> buffer{};
extract(archive.archive, src_file, buffer);

return PyBytes_FromStringAndSize(reinterpret_cast<char const*>(std::data(buffer)), std::ssize(buffer));
}
catch(std::exception const& err)
{
PyErr_SetString(PyExc_RuntimeError, err.what());
return nullptr;
}

return Py_None;
}

PyObject* insert_file(PyObject*, PyObject* args)
{
PyObject* obj{};
Expand Down Expand Up @@ -226,12 +253,13 @@ namespace
return Py_None;
}

constinit std::array<PyMethodDef, 9> method_table
constinit std::array<PyMethodDef, 10> method_table
{
PyMethodDef{"open_archive_from_path", open_archive_from_path, METH_VARARGS, ""},
PyMethodDef{"close_archive", close_archive, METH_VARARGS, ""},
PyMethodDef{"list_archive", list_archive, METH_VARARGS, ""},
PyMethodDef{"extract_file", extract_file, METH_VARARGS, ""},
PyMethodDef{"extract_data", extract_data, METH_VARARGS, ""},
PyMethodDef{"insert_file", insert_file, METH_VARARGS, ""},
PyMethodDef{"insert_data", insert_data, METH_VARARGS, ""},
PyMethodDef{"remove_file", remove_file, METH_VARARGS, ""},
Expand Down

0 comments on commit 70b8911

Please sign in to comment.