omega-zip
omega-zip: ZIP archive reader in seeded Omega (store and deflate, CRC-32, fail-closed refusals)
Brought by drakestapleton
omega-zip / README.md
| 1 | # omega-zip |
| 2 | |
| 3 | I wrote omega-zip so that a zip file, a zlib stream or a raw DEFLATE stream can be read by a tool |
| 4 | whose every line anyone can inspect. It is written in Omega (seeded Omegese) and built by the pinned |
| 5 | seed-born compiler; nothing else runs inside it. It is the first engine of the no-black-box tools |
| 6 | (slice S1): Office files (.docx, .xlsx, .pptx, .odt) are zip archives, and most PDF streams are zlib. |
| 7 | |
| 8 | One program, `ozip`: |
| 9 | |
| 10 | | command | what it does | |
| 11 | |---|---| |
| 12 | | `ozip crc32` | CRC-32 of stdin, 8 lowercase hex digits and a newline | |
| 13 | | `ozip inflate` | raw DEFLATE (RFC 1951) on stdin to stdout | |
| 14 | | `ozip zlib` | a zlib stream (RFC 1950) on stdin to stdout, Adler-32 checked | |
| 15 | | `ozip l ZIP` / `ozip v ZIP` | list the entries, byte for byte what Info-ZIP UnZip 6.0 prints for `unzip -l` / `unzip -v` (name, sizes, method, CRC, date) | |
| 16 | | `ozip t ZIP` | decompress every entry and check its CRC-32, no output | |
| 17 | | `ozip p ZIP [NAME]` | every entry (or the one named, an exact name) to stdout, like `unzip -p` | |
| 18 | | `ozip x ZIP DIR [NAME]` | extract into the existing directory DIR | |
| 19 | |
| 20 | Stored (0) and deflated (8) entries are read; the CRC-32 of every extracted entry is checked. |
| 21 | |
| 22 | ## What the proof proves, and what it does not |
| 23 | |
| 24 | The gate below proves that the `ozip` built from exactly these sources by the pinned compiler gives |
| 25 | the oracle's bytes on the corpus, refuses the malformed inputs with the stated codes, kills the stated |
| 26 | mutants, and never traps or hangs on 32,960 cut or changed inputs. It does not prove that `ozip` is |
| 27 | correct on every input, that it is fast, or that a file it reads is harmless: it reads what the archive |
| 28 | says, within the bounds below. |
| 29 | |
| 30 | ## Build |
| 31 | |
| 32 | The compiler is omega-mv's seed-born `seed/out/omc` at the commit in `PIN` (sha256 in `PIN`). With an |
| 33 | omega-mv checkout at that commit (`sh seed/omega-from-seed.sh` rebuilds omc from the seed and must give |
| 34 | the same sha256): |
| 35 | |
| 36 | ```sh |
| 37 | OMC=/path/to/omega-mv/seed/out/omc |
| 38 | cat src/head.om vendor/omega-mv/seed/text/text.om src/io.om src/crc32.om src/inflate.om \ |
| 39 | src/zlibd.om src/unzip.om src/main.om | $OMC > ozip && chmod +x ozip |
| 40 | ``` |
| 41 | |
| 42 | The units must be in that order (`src/head.om` carries the `om2` line). The output is a static Linux |
| 43 | AArch64 executable. `tests/gate.om` builds the same way (its `UNITS` text) and is the receipt. |
| 44 | |
| 45 | ## Gate |
| 46 | |
| 47 | ```sh |
| 48 | mkdir -p build && $OMC < tests/gate.om > build/gate-bin && chmod +x build/gate-bin && build/gate-bin $OMC |
| 49 | ``` |
| 50 | |
| 51 | Run from the repository root. The last line on success: |
| 52 | |
| 53 | ``` |
| 54 | OZIPGATE PASS cases=219 mutants-killed=19/19 robust-runs=32960 |
| 55 | ``` |
| 56 | |
| 57 | It exits 0 on PASS, 1 on any failure, 3 (NOT_RUN) when the compiler's SHA-256 is not the pin. It |
| 58 | computes that SHA-256 itself (FIPS 180-4). Then it: |
| 59 | |
| 60 | 1. builds `build/gate/ozip` from the units; |
| 61 | 2. runs the 219 cases of `tests/cases.txt`: exit status, stdout against the oracle's bytes, and |
| 62 | stderr (empty after success; exactly one line `ozip: ...` after a refusal), plus extraction checks |
| 63 | (`xcmp`, `xabsent`: the extracted file equals the original; nothing was written outside DIR); |
| 64 | 3. builds each of the 19 mutants of `tests/mutants.txt` (one source line replaced: a wrong distance |
| 65 | base, a wrong length base, a 16 KiB window, no distance-too-far check, no stored length check, a |
| 66 | wrong CRC polynomial, no CRC check, no path check, no local header check, no Adler-32 check, no |
| 67 | local name, method or flag comparison, no descriptor size comparison, no data-before-directory |
| 68 | check, no directory size or end check, no ZIP64 size sentinel, no overlap check) and requires a |
| 69 | failing case for each; |
| 70 | 4. robustness: every raw, zlib and zip corpus file (103 files) cut at 64 points and changed at 256 |
| 71 | single bytes; every run must end with 0 or a documented code, never a trap (exit 101..114), a |
| 72 | signal or the 10 s limit, and an exit 0 of `zlib` or `p` must give the oracle's bytes. Raw DEFLATE |
| 73 | has no checksum, so a changed byte may decode to other bytes; those runs are counted, not failed. |
| 74 | |
| 75 | The oracles are test tools only, never part of omega-zip: python3 `zlib` (Python 3.12.3, zlib 1.3) and |
| 76 | Info-ZIP UnZip 6.0 / Zip 3.0. Their output is committed under `corpus/` with the commands that made it |
| 77 | (`corpus/SOURCES.txt`), so the gate itself needs only the pinned omc. `corpus/refuse/ORACLE.txt` has |
| 78 | the oracle's verdict on each refusal fixture. `tests/mkcases.om` (Omega) regenerates `corpus/refuse`. |
| 79 | |
| 80 | ## Exit codes |
| 81 | |
| 82 | Every refusal writes one line `ozip: MESSAGE` to stderr. Output already written to stdout before a |
| 83 | refusal stays written (the stream is not buffered whole); with `x` the partly written file is removed. |
| 84 | |
| 85 | | code | meaning | zlib's message for the same input (python3) or UnZip | |
| 86 | |---|---|---| |
| 87 | | 0 | done | | |
| 88 | | 1 | usage, bad arguments | | |
| 89 | | 2 | cannot open the zip file | | |
| 90 | | 3 | read error | | |
| 91 | | 4 | write error | | |
| 92 | | 5 | `x`: cannot create an output file or directory (it may exist already: files are opened with O_EXCL and O_NOFOLLOW, never overwritten) | | |
| 93 | | 10 | input ends early (also: empty input) | incomplete or truncated stream | |
| 94 | | 11 | invalid block type (3) | invalid block type | |
| 95 | | 12 | stored block length does not match its complement | invalid stored block lengths | |
| 96 | | 13 | too many length or distance codes (HLIT > 286 or HDIST > 30) | too many length or distance symbols | |
| 97 | | 14 | code-length code over-subscribed or incomplete | invalid code lengths set | |
| 98 | | 15 | code-length repeat with no previous length, or past the end | invalid bit length repeat | |
| 99 | | 16 | literal/length code lengths over-subscribed or incomplete (one code of length 1 is admitted) | invalid literal/lengths set | |
| 100 | | 17 | distance code lengths over-subscribed or incomplete (likewise) | invalid distances set | |
| 101 | | 18 | no end-of-block code (symbol 256 has length 0) | invalid code -- missing end-of-block | |
| 102 | | 19 | invalid literal/length code (286, 287, or no code) | invalid literal/length code | |
| 103 | | 20 | invalid distance code (30, 31, or no code) | invalid distance code | |
| 104 | | 21 | distance too far back (before the start of the output) | invalid distance too far back | |
| 105 | | 22 | output above the bound (4294967295 bytes per stream; CRC-32 input above 2^40 bytes) | | |
| 106 | | 30 | bad zlib header (method not 8, window above 32 KiB, check bits) | incorrect header check / unknown compression method / invalid window size | |
| 107 | | 31 | zlib preset dictionary | Error 2 (Z_NEED_DICT) | |
| 108 | | 32 | Adler-32 mismatch | incorrect data check | |
| 109 | | 40 | no end of central directory record (not a zip file) | | |
| 110 | | 41 | ZIP64 archive: not yet supported | | |
| 111 | | 42 | split or spanned archive | UnZip warns and tries anyway | |
| 112 | | 43 | encrypted entry | UnZip asks for a password | |
| 113 | | 44 | compression method other than 0 and 8 | | |
| 114 | | 45 | `x`: unsafe name (absolute, `..`, `.`, empty part, backslash, NUL), or output path above 4095 bytes | UnZip strips `../` with a warning | |
| 115 | | 46 | local header or data descriptor disagrees with the central directory | stricter than `unzip -t` | |
| 116 | | 47 | CRC-32 mismatch | bad CRC | |
| 117 | | 48 | entry larger than the bound (1 GiB) | | |
| 118 | | 49 | no entry with that name | | |
| 119 | | 50 | central directory malformed (signature, count, size, offsets) | | |
| 120 | | 51 | entries overlap: their local headers and data need more bytes than lie before the central directory (an overlapping zip bomb) | UnZip: "invalid zip file with overlapped components (possible zip bomb)" | |
| 121 | | 52 | stored entry with different compressed and uncompressed sizes | | |
| 122 | | 53 | entry data longer or shorter than its stated size | stricter than `unzip -t` | |
| 123 | |
| 124 | A trap (exit 101..114) would be a defect: the gate fails on one. |
| 125 | |
| 126 | ## Bounds, and what happens at each |
| 127 | |
| 128 | Nothing holds a whole file. All storage is fixed: 57 globals, 64,124 bytes in all (a 32 KiB window |
| 129 | that is also the output buffer, a 16 KiB input buffer, Huffman tables, a 4 KiB name, a 4 KiB wanted |
| 130 | name, a 4 KiB output path), plus at most 12,376 bytes of local arrays. No array has more than 32,768 |
| 131 | elements, so the sizes of profile rule B3 (`seed/profile/README.md`: 65,536 elements per array, 65,536 |
| 132 | bytes of globals) hold. The B1 loop-bound rule (every bound at most 8192) is not met: streaming a file |
| 133 | of up to 4 GiB in one pass needs outer loops above 8192 passes, so the unit is not profiled. |
| 134 | |
| 135 | | bound | value | at the bound | |
| 136 | |---|---|---| |
| 137 | | window (distance) | 32768 bytes (RFC 1951) | a larger distance cannot be coded | |
| 138 | | output per raw or zlib stream | 4294967295 bytes | refused, 22 (bytes before it already written) | |
| 139 | | CRC-32 input | 2^40 bytes | refused, 22 | |
| 140 | | zip entry, uncompressed | 1073741824 bytes (1 GiB) | refused before any output, 48 | |
| 141 | | zip entry output vs its stated size | exactly the stated size | refused, 53 | |
| 142 | | entries' local headers, names and data, summed over the archive (`t`, `p`, `x`) | at most the bytes before the central directory, so the output is bounded by DEFLATE's ratio times the archive size | refused before any output, 51 | |
| 143 | | zip entries | 65535 (no ZIP64) | 0xFFFF means ZIP64: 41 | |
| 144 | | archive comment | 65535 bytes (the end record is searched in the last 65557 bytes) | not found: 40 | |
| 145 | | entry name | 4095 bytes | 45 | |
| 146 | | `x` path DIR/NAME | 4095 bytes (Linux PATH_MAX - 1) | 45 | |
| 147 | | ZIP path argument, DIR argument | 4095 bytes | 1 | |
| 148 | | directories | made with mode 0755; files 0644 | | |
| 149 | |
| 150 | ## Not supported (refused, never guessed) |
| 151 | |
| 152 | ZIP64 (not yet supported: 41), encryption (43), split or spanned archives (42), methods other than |
| 153 | stored and deflated (44: no BZIP2, LZMA, Deflate64, ...), archives with data before the first entry |
| 154 | (self-extractors: the offsets no longer match, 50), zip symbolic links and file modes (an entry is |
| 155 | written as a plain file with mode 0644), archive comments in `t`/`p`/`x` are ignored, gzip files (use |
| 156 | `ozip inflate` after the gzip header is removed). `ozip p NAME` takes the exact name, without |
| 157 | UnZip's wildcards. The `l`/`v` listings are equal to UnZip's for archives whose entries carry no |
| 158 | extended-timestamp extra field (UnZip then prints that time in the local time zone; omega-zip prints the |
| 159 | MS-DOS date and time fields) and whose names are printable ASCII (control bytes are shown as `^` and |
| 160 | the byte plus 64, as UnZip does). Output files are not given the archive's timestamps. |
| 161 | |
| 162 | ## Measured speed |
| 163 | |
| 164 | MEASURED 2026-11-10T04:53Z on the Spark (DGX Spark GB10, Linux AArch64), one core: `taskset -c 8` |
| 165 | inside the hive's pool-8-9 ticket, `nice -n 10`, load average about 20 on the machine. `ozip` built by |
| 166 | the gate (sha256 `3687bb9d...981d76`); wall time of `/usr/bin/time` (0.01 s resolution), the same in |
| 167 | each of 3 runs; MiB per second of uncompressed output (Wolfram: 64/0.47, 64/0.28, 128/0.99, 64/0.13). |
| 168 | |
| 169 | | command | input | time | throughput | |
| 170 | |---|---|---|---| |
| 171 | | `ozip inflate` | `text64M.raw6`: 64 MiB of OMEGESE.md repeated, python3 zlib level 6 raw, 25,504,782 bytes | 0.47 s | 136.2 MiB/s out (51.8 MiB/s in) | |
| 172 | | `ozip inflate` | `bin64M.raw6`: 64 MiB of omc repeated, level 6 raw, 14,554,550 bytes | 0.28 s | 228.6 MiB/s | |
| 173 | | `ozip t` | `tp.zip`: both files, `zip -6`, 40,051,393 bytes, CRC-32 checked | 0.99 s | 129.3 MiB/s | |
| 174 | | `ozip crc32` | `text64M` | 0.13 s | 492.3 MiB/s | |
| 175 | |
| 176 | The output bound was exercised once outside the gate (it needs 4 GiB of output): 4294967296 zero |
| 177 | bytes compressed at level 9 (4,174,507 bytes) gave `ozip: output above the bound`, exit 22, after |
| 178 | 5.99 s. The inputs are made by commands in the scratch directory and not committed; they are not part |
| 179 | of the corpus. Huffman codes are decoded one bit at a time (simple to read, not the fastest way). |
| 180 | |
| 181 | ## Standards followed |
| 182 | |
| 183 | - RFC 1951, DEFLATE Compressed Data Format Specification version 1.3 (sections 2, 3.2.1 to 3.2.7). |
| 184 | - RFC 1950, ZLIB Compressed Data Format Specification version 3.3 (sections 2.2 and 8.2, Adler-32). |
| 185 | - PKWARE APPNOTE.TXT, .ZIP File Format Specification, version 6.3.10 (sections 4.3.6 to 4.3.16, 4.4). |
| 186 | - CRC-32: the polynomial of ISO 3309 and ITU-T V.42 (0x04C11DB7, reflected 0xEDB88320), register |
| 187 | preset to all ones and inverted at the end, as APPNOTE 4.4.7 and RFC 1952 section 8 specify. |
| 188 | - SHA-256 in the gate: FIPS 180-4. |
| 189 | |
| 190 | ## Files |
| 191 | |
| 192 | - `src/`: the units (`head`, `io`, `crc32`, `inflate`, `zlibd`, `unzip`, `main`). |
| 193 | - Reuse: another program can take `io.om`, `crc32.om`, `inflate.om`, `zlibd.om` and `unzip.om` unchanged. |
| 194 | Their output goes through `w = wout n` (the window `win[0 .. n)`), which the program defines as |
| 195 | `fn wout n ulong to ulong`, returning n: `src/main.om` passes it to `woutfd` in `io.om` (the output |
| 196 | descriptor); omega-office's `src/sink.om` consumes it. Without a `wout` omc refuses the program (error 3). |
| 197 | - `vendor/omega-mv/seed/text/text.om`: omega-mv's text unit, unchanged (`NOTICE`, `PIN`). |
| 198 | - `tests/gate.om`, `tests/cases.txt`, `tests/mutants.txt`, `tests/mkcases.om`. |
| 199 | - `corpus/`: originals, oracle outputs, refusal fixtures; sources and licences in `corpus/SOURCES.txt`. |
| 200 | |
| 201 | ## Licence |
| 202 | |
| 203 | Apache-2.0 (`LICENSE`). The LibreOffice templates in `corpus/zip` are MPL-2.0 test data (`NOTICE`). |