Skip to content

About

Memory-safe C parser for a binary asset container format, 39 libcheck tests and a sanitizer build

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

BUN Binary Asset Parser

A C11 command-line tool that parses and validates .bun binary asset bundle files, and its 39 libcheck tests all pass (make test, last run 23 Oct 2026: 100%: Checks: 39, Failures: 0, Errors: 0).

Every offset, size and count in a .bun file comes from the file itself, so the parser treats all of them as hostile, checks bounds before it seeks or allocates, and reports every violation it finds rather than stopping at the first one.

Built in a four-person university team in 2026. The specification and the provided sample files aren't included, so the test fixtures here are generated by my own bunfile_generator.py.

My part

I wrote the header and asset-table parser, which is the core of the tool (bun_parse.c, 464 of 487 lines), most of the CLI (main.c, 114 of 136), most of the shared header (bun.h, 92 of 143), the fixture generator (bunfile_generator.py, 718 of 931) and HACKING.md. My three teammates wrote the string-table module, the data and RLE module and most of the test suite (I wrote 193 of its 1,255 lines). By commits it's 20 of 63 on main.

The line counts come from git blame on this repo, e.g.

git blame --line-porcelain bun_parse.c | grep -c "^author-mail <dennisquek027@gmail.com>"
git blame --line-porcelain bun_parse.c | grep -c "^author-mail"

The history is the team's real history, imported with the coursework material filtered out. Two teammates show up as Teammate A and Teammate B because I didn't have their OK to publish their names and emails.

How to run

Needs gcc, GNU make, check, pkg-config and python3. On Ubuntu or WSL:

sudo apt install build-essential check pkg-config python3
make                                   # builds ./bun_parser
python3 bunfile_generator.py --all     # writes 29 sample files (6 valid, 23 invalid) to tests/fixtures/
./bun_parser tests/fixtures/valid/01-empty.bun

Exit codes:

Code Symbol Meaning
0 BUN_OK File is fully valid
1 BUN_MALFORMED File breaks the format rules
2 BUN_UNSUPPORTED File uses a feature the parser doesn't do
3 BUN_ERR_IO Generic I/O error
4 BUN_ERR_NOT_FOUND Path doesn't exist

Sanitizer build

Uncomment this line in the Makefile, then make clean && make test:

# CFLAGS += -fsanitize=address,undefined -fno-omit-frame-pointer -g

Tests

make test

Last result (23 Oct 2026, Ubuntu 24.04, gcc 13.3, check 0.15.2): 100%: Checks: 39, Failures: 0, Errors: 0. One of the 39 (the 650 MB large-file test) returns early when its file isn't there, and that file isn't in this repo, so 38 actually exercise the parser on a fresh clone.

With the sanitizer line switched on (ASan + UBSan) the same run gave the same 39 of 39, and running ./bun_parser over all 29 sample files the generator writes (6 valid, 23 invalid) gave the expected exit code every time, with no sanitizer reports.

TCase Tests What it covers
header-tests 12 Magic/version validation, file-too-short, misaligned offset/size, zero fields, reserved field, section-overlaps-header, combo bad magic+version
string-table-tests 9 Valid name, zero-length name, non-printable byte, name exceeds table, integer overflow, adjacent names, nonzero table offset, prefix read full, prefix read truncated
open-error-tests 1 BUN_ERR_NOT_FOUND on a missing path
header-bounds-tests 4 Sections past EOF, overlap, adjacency, overflow safety
dos-cap-tests 2 BUN_MAX_RECORDS rejects huge counts before calloc
data-preview-tests 2 End-to-end pipeline with printable + binary payloads
data-section-tests 9 RLE valid, zlib unsupported, nonzero checksum, unknown flags, RLE odd size, RLE zero count, RLE size mismatch, uncompressed bad size, large file (skipped if absent)
Total 39

Most tests build their input in memory with fmemopen(3). The three that read files use the ones make test generates into tests/fixtures/ (gitignored).

What broke

  • The overlap check could wrap. The first sections_overlap() computed offset + size, and because the past-EOF checks record a violation and keep going (so every problem gets reported), out-of-range 64-bit values from a hostile file could still reach it and wrap past zero, which reads as "no overlap". Code review caught it, and it now compares the gap between the two offsets against the size of the lower section, so there's no addition left to overflow.
  • A record count can ask for 206 GB. A file can declare asset_count = UINT32_MAX, about 4.3 billion 48-byte records, so any count over BUN_MAX_RECORDS (1,000,000, which is 48 MB) is refused with BUN_UNSUPPORTED before calloc runs, and the asset table is bounds-checked against the file size first. A merge had left a second, unreachable copy of that cap with a comment saying 56 MB (it's 48), and the same review cleaned both up.

Status and limits

  • Finished coursework, not maintained.
  • zlib-compressed assets return BUN_UNSUPPORTED, and non-zero checksums are reported, not verified.
  • Linux/WSL only (it uses fmemopen and POSIX fseeko).

Source layout

File Purpose
bun.h Shared types, structs, result codes, format constants
bun_parse.c Header parsing, asset table parsing, bounds and overlap checks
bun_string_table.c / .h String table validation, streaming name read
bun_data.c / .h Per-asset data read, RLE decompression, preview extraction
main.c CLI entry point, output formatting
tests/test_bun.c libcheck test suite
bunfile_generator.py Generates the .bun sample files the tests use
Makefile Build rules: all, test, fixtures, clean
HACKING.md Architecture and coding notes

AI use: this README was drafted with Claude from the repo history and checked against make test and git blame output.

No licence: all rights reserved, shared for viewing.

About

Memory-safe C parser for a binary asset container format, 39 libcheck tests and a sanitizer build

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages