#include "rustduck-sys/native/bridge/section_file.h"
#include "rustduck-sys/native/bridge/context.h"
#include "rustduck-sys/native/bridge/test_hooks.h"
#include "rustduck-sys/native/tspy.h"
#include <memory>
#include <stdexcept>
#include <utility>
#include <vector>
namespace {
rust::Vec<std::uint8_t> serialize(const RustDuckSectionFile §ion_file,
std::size_t (*call)(void *, std::uint8_t *,
std::size_t *)) {
rustduck_test_maybe_fail(RustDuckFailurePoint::SectionFileSerialize);
std::vector<std::uint8_t> buffer(2048);
for (;;) {
std::size_t size = buffer.size();
const std::size_t required = call(section_file.raw(), buffer.data(), &size);
if (required > buffer.size()) {
buffer.resize(required);
continue;
}
if (size > buffer.size()) {
throw std::runtime_error("native serialization returned an invalid size");
}
rust::Vec<std::uint8_t> output;
output.reserve(size);
for (std::size_t index = 0; index < size; ++index) {
output.push_back(buffer[index]);
}
return output;
}
}
}
RustDuckSectionFile::RustDuckSectionFile(
std::shared_ptr<void> section_file) noexcept
: section_file_(std::move(section_file)) {}
RustDuckSectionFile::~RustDuckSectionFile() noexcept = default;
void *RustDuckSectionFile::raw() const noexcept { return section_file_.get(); }
std::unique_ptr<RustDuckSectionFile>
rustduck_section_file_new(const RustDuckDuckContext &context) {
rustduck_test_maybe_fail(RustDuckFailurePoint::SectionFileConstruct);
auto section_file =
std::shared_ptr<void>(tspyNewSectionFile(context.raw()),
[context = context.retain()](void *pointer) {
if (pointer != nullptr) {
tspyDeleteSectionFile(pointer);
}
});
if (!section_file) {
throw std::runtime_error("native section file construction failed");
}
return std::make_unique<RustDuckSectionFile>(std::move(section_file));
}
void rustduck_section_file_clear(RustDuckSectionFile §ion_file) {
tspySectionFileClear(section_file.raw());
}
std::size_t
rustduck_section_file_binary_size(const RustDuckSectionFile §ion_file) {
return tspySectionFileBinarySize(section_file.raw());
}
std::size_t
rustduck_section_file_sections_count(const RustDuckSectionFile §ion_file) {
return tspySectionFileSectionsCount(section_file.raw());
}
std::size_t
rustduck_section_file_tables_count(const RustDuckSectionFile §ion_file) {
return tspySectionFileTablesCount(section_file.raw());
}
void rustduck_section_file_set_crc_validation(RustDuckSectionFile §ion_file,
std::int32_t mode) {
tspySectionFileSetCRCValidation(section_file.raw(), mode);
}
bool rustduck_section_file_load_binary_buffer(
RustDuckSectionFile §ion_file, rust::Slice<const std::uint8_t> data) {
return tspySectionLoadBuffer(
section_file.raw(), data.empty() ? nullptr : data.data(), data.size());
}
rust::Vec<std::uint8_t>
rustduck_section_file_to_binary(const RustDuckSectionFile §ion_file) {
rustduck_test_maybe_fail(RustDuckFailurePoint::SectionFileSerialize);
const std::size_t expected = tspySectionFileBinarySize(section_file.raw());
if (expected == 0) {
return {};
}
std::vector<std::uint8_t> buffer(expected);
std::size_t size = buffer.size();
tspySectionSaveBuffer(section_file.raw(), buffer.data(), &size);
if (size != expected) {
throw std::runtime_error("native binary serialization size changed");
}
rust::Vec<std::uint8_t> output;
output.reserve(size);
for (const std::uint8_t value : buffer) {
output.push_back(value);
}
return output;
}
bool rustduck_section_file_load_binary(RustDuckSectionFile §ion_file,
rust::Slice<const std::uint8_t> path) {
return tspySectionFileLoadBinary(section_file.raw(),
rustduck::utf16_data(path),
rustduck::utf16_size(path));
}
bool rustduck_section_file_save_binary(const RustDuckSectionFile §ion_file,
rust::Slice<const std::uint8_t> path) {
return tspySectionFileSaveBinary(section_file.raw(),
rustduck::utf16_data(path),
rustduck::utf16_size(path));
}
bool rustduck_section_file_load_xml(RustDuckSectionFile §ion_file,
rust::Slice<const std::uint8_t> source) {
return tspySectionFileLoadXML(section_file.raw(),
rustduck::utf16_data(source),
rustduck::utf16_size(source));
}
bool rustduck_section_file_save_xml(const RustDuckSectionFile §ion_file,
rust::Slice<const std::uint8_t> path) {
return tspySectionFileSaveXML(section_file.raw(), rustduck::utf16_data(path),
rustduck::utf16_size(path));
}
bool rustduck_section_file_save_json(const RustDuckSectionFile §ion_file,
rust::Slice<const std::uint8_t> path) {
return tspySectionFileSaveJSON(section_file.raw(), rustduck::utf16_data(path),
rustduck::utf16_size(path));
}
rust::Vec<std::uint8_t>
rustduck_section_file_to_xml_utf16(const RustDuckSectionFile §ion_file) {
return serialize(section_file, tspySectionFileToXML);
}
rust::Vec<std::uint8_t>
rustduck_section_file_to_json_utf16(const RustDuckSectionFile §ion_file) {
return serialize(section_file, tspySectionFileToJSON);
}