codecforge is an autonomous, round-trip-verified binary format reverse-engineering synthesizer, code generator, and visual byte-map inspector.
Given 2 or more binary sample files of an unknown format, codecforge aligns invariant vs. variable/shifted byte regions, detects magic headers, length prefixes, and CRC32 trailers, synthesizes a declarative bidirectional format specification (CodecSpec), verifies it against a 3-Gate Anti-Cheat Oracle, and compiles standalone Python modules, single-header #pragma once C++20 codecs, Kaitai Struct (.ksy) schemas, and standalone dark-mode HTML hex-map reports.
2+ Binary Samples
|
v
[ probe.py ] -----> Shannon entropy, monotonic SequenceMatcher alignment,
| length-prefix & trailer CRC32 detectors
v
[ synthesizer.py ] -> Offline Heuristic (--provider heuristic)
| OR BYOK LLM Self-Healing Loop (gemini | openai | ollama)
v
[ oracle.py ] ----> 3-Gate Anti-Cheat Verification
| 1. Bit-exact encode(decode(sample)) == sample
| 2. Anti-passthrough & header magic coverage guard
| 3. Semantic mutation fuzzing (length_of / count_of / crc32_of)
v
[ codegen_* / report_html ]
├── Python 3.10+ standalone module (zero external imports)
├── C++20 single-header #pragma once codec + inline ISO-3309 CRC32
├── Kaitai Struct (.ksy) YAML schema
└── Dark-mode HTML5 byte-map & entropy sparkline report
Synthesizing a schema that merely "parses" samples is easy to game (e.g., dumping the whole file into bytes_rest or hardcoding derived length/CRC fields). Every CodecSpec must pass all three gates:
- Gate 1 — Bit-Exact Round-Trip: Every sample must decode cleanly via the 3-pass runtime and re-encode byte-for-byte identically (
encode_obj(spec, decode_bytes(spec, sample)) == sample). - Gate 2 — Anti-Passthrough Guard: On non-high-entropy inputs (
entropy < 7.0), rejects single-fieldbytes_rest/fixed_bytesblobs, caps low-entropy opaque byte coverage (<= 0.50), and requires amagicfield when samples share a printable ASCII header prefix. - Gate 3 — Semantic Mutation Fuzzing: Mutates every non-derived field (growing
var_strby +3 bytes,cstrby +2 bytes,arrayby +2 items, incrementing scalars) without updating derivedlength_of,count_of, orcrc32_offields, then verifiesencode -> decode -> encoderemains bit-exact and all mutated values survive round-trip. Any omittedlength_of,count_of, orcrc32_ofrelation fails immediately.
Install from source:
pip install -e ".[dev]"Generate the 3 deterministic demo binary samples and run the full pipeline:
python examples/generate_demo_samples.py
# 1. Probe alignment, length prefixes, and CRC32 trailers
codecforge probe examples/samples/save_slot1.sav examples/samples/save_slot2.sav examples/samples/save_slot3.sav
# 2. Synthesize & 3-gate verify a CodecSpec offline
codecforge synthesize examples/samples/save_slot1.sav examples/samples/save_slot2.sav examples/samples/save_slot3.sav --provider heuristic --name simple_save -o simple_save.yaml
# 3. Verify an existing spec against samples
codecforge verify simple_save.yaml examples/samples/save_slot1.sav examples/samples/save_slot2.sav examples/samples/save_slot3.sav
# 4. Compile standalone Python, C++20, and Kaitai Struct codecs
codecforge compile simple_save.yaml --lang python -o simple_save.py
codecforge compile simple_save.yaml --lang cpp -o simple_save.hpp
codecforge compile simple_save.yaml --lang ksy -o simple_save.ksy
# 5. Generate a standalone dark-mode HTML hex-map inspection report
codecforge inspect simple_save.yaml examples/samples/save_slot1.sav examples/samples/save_slot2.sav -o report.htmlFor complex formats with nested arrays or multi-field records, use --provider:
- Gemini Interactions API (
--provider gemini, defaultgemini-4-hydrogen):export GEMINI_API_KEY="your-key" codecforge synthesize samples/*.bin --provider gemini -o spec.yaml
- OpenAI (
--provider openai, defaultgpt-4o-mini):export OPENAI_API_KEY="your-key" codecforge synthesize samples/*.bin --provider openai -o spec.yaml
- Local Ollama (
--provider ollama, defaultqwen2.5-coder:7b):codecforge synthesize samples/*.bin --provider ollama -o spec.yaml
import simple_save # zero external dependencies
with open("examples/samples/save_slot1.sav", "rb") as f:
obj = simple_save.decode(f.read())
obj["text_4"] = "UpdatedHeroName"
repacked = simple_save.encode(obj) # automatically recomputes length_of & crc32_of#include "simple_save.hpp"
#include <vector>
std::vector<std::uint8_t> raw = /* read file bytes */;
auto obj = codecforge_gen::SimpleSave::decode(raw);
obj.text_4 = "UpdatedHeroName";
auto repacked = codecforge_gen::SimpleSave::encode(obj); // UB-free endian & CRC32