// Copyright (c) 2026 Juhapekka Piiroinen // All Rights Reserved. // AmoosedWeb project: https://amoosed.com // Usage: native-memory ORIGIN VIRTUAL_RESOURCE_PATH #include #if defined(_WIN32) #include #include #include using Http = amoosed::WindowsHttpHost; using Crypto = amoosed::WindowsCryptoHost; using Media = amoosed::WindowsMediaDecoder; #else #include #include #include using Http = amoosed::CurlHttpHost; using Crypto = amoosed::OpenSslCryptoHost; using Media = amoosed::PosixMediaDecoder; #endif #include #include #include #include #include class MemoryOutput final : public amoosed::OutputHost { public: static constexpr std::size_t limit = 16u * 1024u * 1024u; ~MemoryOutput() override { abort(); } bool write(const std::uint8_t* data, std::size_t length) override { if (failed_ || committed_ || (length && !data) || length > limit - bytes_.size()) { failed_ = true; return false; } try { if (length) bytes_.insert(bytes_.end(), data, data + length); } catch (...) { failed_ = true; return false; } return true; } bool commit() override { if (failed_ || committed_) return false; committed_ = true; return true; } void abort() override { std::fill(bytes_.begin(), bytes_.end(), 0); bytes_.clear(); committed_ = false; failed_ = true; } const std::vector* verified_bytes() const { return committed_ ? &bytes_ : nullptr; } private: std::vector bytes_; bool failed_ = false, committed_ = false; }; int main(int argc, char** argv) { const bool expect_failure = argc == 4 && std::string(argv[3]) == "--expect-failure"; if (argc != 3 && !expect_failure) { std::cerr << "Usage: native-memory ORIGIN VIRTUAL_RESOURCE_PATH\n"; return 2; } #if !defined(_WIN32) if (!Http::initialize()) { std::cerr << "HTTP initialization failed\n"; return 3; } #endif std::string error; Http http(argv[1], error); if (!http.ready()) { std::cerr << error << '\n'; return 3; } Crypto crypto; Media media; amoosed::IndexResult index; if (!amoosed::open_hosted_index(http, crypto, index, error)) { std::cerr << error << '\n'; return 4; } MemoryOutput output; amoosed::DownloadResult result; if (!amoosed::download_resource(index.app, argv[2], nullptr, http, crypto, output, result, error, &media)) { if (expect_failure && !output.verified_bytes()) return 0; std::cerr << error << '\n'; return 5; } if (expect_failure) return 6; const auto* bytes = output.verified_bytes(); if (!bytes || bytes->size() != result.bytes) return 6; // Only verified_bytes() exposes content to application code. It remains // unavailable until the Reader verifies every part and the full digest. std::cout << "Verified " << result.bytes << " bytes in memory: " << result.sha256 << '\n'; }