#include #include #include #include #include namespace { void wr32(std::vector &bytes, size_t off, uint32_t value) { bytes[off + 0] = uint8_t(value); bytes[off + 1] = uint8_t(value >> 8); bytes[off + 2] = uint8_t(value >> 16); bytes[off + 3] = uint8_t(value >> 24); } constexpr uint32_t kMagic32LE[] = { 0xCAB067B8, 0x0FB16DF8, 0x7E8C7284, 0x1E00195E }; constexpr uint32_t kMagic64LE[] = { 0x5E499BE5, 0x141F636F, 0xA9EB131E, 0xC4EDBE90 }; constexpr uint32_t kMagic32BE[] = { 0xB595110E, 0x4BB5A56A, 0x502828EB, 0x04B37825 }; std::vector granny_header(size_t size, uint32_t section_count, const uint32_t *magic = kMagic32LE) { std::vector bytes(size, 0); for (int i = 0; i < 4; ++i) { wr32(bytes, size_t(i) * 4, magic[i]); } constexpr size_t h = 32; wr32(bytes, h + 0, 6); // Version wr32(bytes, h + 4, uint32_t(size)); wr32(bytes, h + 12, 40); // section table starts at byte 72 wr32(bytes, h + 16, section_count); // Root type/object remain section 0, offset 0. With no section this must be // rejected by the FileInfo stage rather than returned as an empty File. return bytes; } bool expect_failure(const std::vector &bytes, const char *stage) { gr2::LoadError err; auto file = gr2::File::load(bytes.data(), bytes.size(), &err); if (file || err.stage != stage) { std::fprintf(stderr, "expected failure stage '%s', got file=%d stage='%s' message='%s'\n", stage, file.has_value(), err.stage.c_str(), err.message.c_str()); return false; } return true; } } // namespace int main() { bool ok = true; ok &= expect_failure(std::vector(80, 0), "header"); ok &= expect_failure(granny_header(80, 0), "fileinfo"); // 64-bit little-endian Granny (AVA2 plechi_* assets) passes the header stage; // big-endian is still rejected there. ok &= expect_failure(granny_header(80, 0, kMagic64LE), "fileinfo"); ok &= expect_failure(granny_header(80, 0, kMagic32BE), "header"); auto unsupported = granny_header(160, 1); constexpr size_t section = 72; wr32(unsupported, section + 0, 3); // BitKnit v1 is not implemented wr32(unsupported, section + 4, uint32_t(unsupported.size())); wr32(unsupported, section + 12, 1); // non-empty expanded section ok &= expect_failure(unsupported, "section"); // BitKnit2 (Format 4) with an empty stream: no 0x75B1 magic -> clean section failure. auto bitknit_empty = unsupported; wr32(bitknit_empty, section + 0, 4); ok &= expect_failure(bitknit_empty, "section"); // BitKnit2 stored quantum: magic 0x75B1, u16 0 marker, 8 raw bytes. The section // decodes, so loading proceeds to the (empty) type tree instead of failing in "section". auto bitknit_stored = granny_header(160, 1); const uint8_t stream[] = { 0xB1, 0x75, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; for (size_t i = 0; i < sizeof(stream); ++i) bitknit_stored[120 + i] = stream[i]; wr32(bitknit_stored, section + 0, 4); wr32(bitknit_stored, section + 4, 120); wr32(bitknit_stored, section + 8, uint32_t(sizeof(stream))); wr32(bitknit_stored, section + 12, 8); { gr2::LoadError err; auto file = gr2::File::load(bitknit_stored.data(), bitknit_stored.size(), &err); if (!file && err.stage == "section") { std::fprintf(stderr, "BitKnit2 stored quantum rejected: %s\n", err.message.c_str()); ok = false; } } // Truncated stored quantum (claims 8 bytes, carries 4) must fail cleanly. auto bitknit_short = bitknit_stored; wr32(bitknit_short, section + 8, 8); ok &= expect_failure(bitknit_short, "section"); // BitKnit2 fixup table: u32 compressed size pointing past EOF -> clean "fixup" failure. auto bitknit_fixup = bitknit_stored; wr32(bitknit_fixup, section + 28, 150); // PointerFixupArrayOffset wr32(bitknit_fixup, section + 32, 1); // PointerFixupArrayCount wr32(bitknit_fixup, 150, 1000); ok &= expect_failure(bitknit_fixup, "fixup"); auto bad_offset = granny_header(160, 1); wr32(bad_offset, section + 4, UINT32_MAX); ok &= expect_failure(bad_offset, "section"); return ok ? 0 : 1; }