10#include <nlohmann/json.hpp>
16std::vector<uint8_t> gzip_decompress([[maybe_unused]]
const std::vector<uint8_t>& compressed)
21 std::vector<uint8_t> decompressed;
22 decompressed.resize(1024ULL * 128ULL);
25 auto decompressor = std::unique_ptr<libdeflate_decompressor, void (*)(libdeflate_decompressor*)>{
26 libdeflate_alloc_decompressor(), libdeflate_free_decompressor
28 size_t actual_size = 0;
29 libdeflate_result result = libdeflate_gzip_decompress(decompressor.get(),
36 if (result == LIBDEFLATE_INSUFFICIENT_SPACE) {
37 decompressed.resize(decompressed.size() * 2);
40 if (result == LIBDEFLATE_BAD_DATA) {
41 throw std::runtime_error(
"Invalid gzip data");
43 decompressed.resize(actual_size);
55 std::vector<uint8_t> gzipped(decoded.begin(), decoded.end());
56 return gzip_decompress(gzipped);
64 std::ifstream json_file(json_path);
65 if (!json_file.is_open()) {
66 throw std::runtime_error(
"Failed to open JSON file: " + json_path);
69 nlohmann::json json_data = nlohmann::json::parse(json_file);
70 std::string base64_bytecode = json_data[
"bytecode"];
76std::vector<uint8_t>
gunzip([[maybe_unused]]
const std::string& path)
81 std::ifstream file(path, std::ios::binary);
82 if (!file.is_open()) {
83 throw std::runtime_error(
"Failed to open file: " + path);
87 return gzip_decompress(compressed);
91std::vector<uint8_t>
get_bytecode([[maybe_unused]]
const std::string& bytecodePath)
96 if (bytecodePath ==
"-") {
99 std::filesystem::path filePath = bytecodePath;
100 if (filePath.extension() ==
".json") {
106 return gunzip(bytecodePath);
std::string base64_decode(std::string const &s, bool remove_linebreaks)
std::vector< uint8_t > get_bytecode_from_json(const std::string &json_path)
std::vector< uint8_t > decode_bytecode(const std::string &base64_bytecode)
std::vector< uint8_t > get_bytecode(const std::string &bytecodePath)
std::vector< uint8_t > gunzip(const std::string &path)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
void throw_or_abort(std::string const &err)