1. Overview
Tuntau Horizon writes two kinds of file:
.hz: a compressed file. One input in, one compressed output out, like.gzor.xz. Thehorizoncommand writes it (report.pdf→report.pdf.hz)..thzand.tar.hzare tar archives compressed this way..hzn: an archive. Many files and folders in one file, like.zipor.7z. The Horizon app and itshzntool write it. Its data blocks are.hzstreams (or stored bytes) and a small index lists the files.
So a reader for .hzn needs a reader for .hz. Sections 3 to 7 describe the .hz stream format, section 8 the .hzn container.
What is normative. This page specifies the container and stream framing: how a decoder finds out what a file holds, which stream types exist and how they nest. The entropy coders inside the leaf streams (context mixing, rANS, Huffman and LZ token coders) are defined by the reference implementation named next to each stream type (section 5.1), built with -ffp-contract=off. Where this page and the reference decoder disagree, the reference decoder's output is correct and this page is wrong: please tell us.
Stability. Format 1.0 is frozen. Every 1.x decoder decodes every file any 1.x encoder wrote, byte-exact. Newer 1.x minors may only add stream types or flag values (section 4.1). The product name may change; magics never do.
2. Recognising the files
Use the first bytes, not the extension. Every .hz stream starts with one of five first bytes (section 4); a .hzn archive starts with HZN\x1A.
| File | Extensions | First bytes | MIME type | macOS UTI |
|---|---|---|---|---|
| Archive | .hzn | 48 5A 4E 1A ("HZN\x1A") | application/x-horizon-archive | com.tuntau.horizon.hzn |
| Compressed file | .hz | 33 4B 57 42 10 (main frame), or 77, F0, F1, F3 | application/x-horizon | com.tuntau.horizon.hz-stream |
| Compressed tar | .thz, .tar.hz | as .hz | application/x-horizon-compressed-tar | (as .hz) |
The compact first bytes (77, F0, F1, F3) are too common to identify a file by themselves. Sniff on 3KWB\x10 and HZN\x1A, and trust the extension for the rest, as the Linux shared-mime-info file the app installs does.
3. Conventions
u8,u16,u32,u64: unsigned integers, big-endian in.hzstreams unless a stream says otherwise. (The.hzncontainer is little-endian: section 8.)varint: unsigned LEB128. 7 bits per byte, least significant group first, high bit set on every byte but the last, at most 10 bytes.- CRC-32: the IEEE / zlib polynomial (
0xEDB88320reflected), initial value0xFFFFFFFF, final XOR. It is the value zlib'scrc32()returns. - Magic names. Inner stream types are identified by a
u32magic, written big-endian. The names used here (BWK3,BWZ1, …) read the constant from its low byte up, so the bytes on disk are the name reversed: magicBWK3= constant0x334B5742= bytes33 4B 57 42= ASCII"3KWB". All magics share the low half0x5742, so bytes 3 and 4 on disk are always"WB".
4. Frames
A .hz file is exactly one frame. Its first byte selects the frame type:
| First byte | Frame | Written for |
|---|---|---|
0x77 ('w') | web / small-file frame | small inputs at every level; -web up to 64 MiB |
0xF0 | stored frame | incompressible input |
0xF1 | periodic frame | input that is one pattern of 1–255 bytes, repeated |
0x33 ("3KWB") | main frame BWK3 | everything else |
0xF3 | chunked stream | input larger than the encoder's memory budget, or of unknown length (a pipe) and larger than one chunk |
Frames do not nest, and no inner stream starts a file. Any other first byte means "not a Horizon file". Pre-release main frames (BWK1 / BWK2, first byte 0x31 / 0x32) must be refused with their own message ("made by a pre-release build").
4.1 Versioning
- The main frame and the chunked stream carry a format version byte,
(major << 4) | minor. 1.0 is0x10. Accept a file ifmajorequals yours andminoris not newer. - A newer minor may only add inner stream types (new magics) or new flag values. The layout of everything on this page never changes within major 1.
- The three compact frames (
0x77,0xF0,0xF1) have no version byte, because in them every byte counts (a 300-byte web response). Their first byte is the version: a changed layout gets a new first byte. Reserved for that:0x78–0x7F(web frame successors) and0xF2,0xF4–0xFF(compact frames). - The web frame names its dictionary by id (section 4.5), so a new built-in dictionary is a new id, not a new frame.
4.2 Main frame BWK3
u32 magic 0x334B5742 ("3KWB" on disk)
u8 format version (0x10)
varint n original size in bytes
u32 CRC-32 of the original n bytes
... one inner stream (section 5) that decodes to exactly n bytesCheck both the decoded size and the CRC-32; a mismatch is corruption.
4.3 Stored frame 0xF0
u8 0xF0
n bytes the original (n = file size - 5)
u32 CRC-32 of the original4.4 Periodic frame 0xF1
u8 0xF1
varint n original size
u8 p pattern length, 1..255 (0 is invalid)
p bytes pattern
u32 CRC-32 of the originalThe original is the pattern repeated to n bytes, the last copy cut short. Verify the CRC before writing (it can be computed in O(log n) by CRC combination), so a corrupt n cannot make you write a huge output.
4.5 Web / small-file frame 0x77
u8 0x77
u8 flags bit 7: stored (payload = the original bytes; all other bits 0)
bit 6: payload is not lzr; then bits 0-2 pick 0 = lzf tokens, 1 = hzh
bits 0-1: (lzr) literal-counter rate pair
bit 2: (lzr) mixed literals
bits 3-5: dictionary id (0 = none, 1 = webdict1, 2 = webdict)
varint n original size, at most 64 MiB
... payload
u32 CRC-32 of the originalPayload coders (reference sources in brackets):
- hzh (
hzh.h): Huffman-coded LZ with trained default tables. Matches may reach into the dictionary placed before the data. Used by-weband the small path of most levels. - lzr (
lzr.h): range-coded LZ tokens with a trained prior. The small path of the dense and ultra levels. - lzf (
lzf.h): copy-only LZ tokens.
Two built-in dictionaries are part of the format, each with its own lzr prior and hzh default tables. A decoder only has to unpack the dictionaries it meets. A stream naming an unknown id must be refused with "needs a newer version".
| Id | Source | Size | Dictionary CRC-32 | Prior CRC-32 |
|---|---|---|---|---|
| 1 | webdict1.h | 523,776 bytes | 0x8eaaf93f | 0x7e059920 |
| 2 | webdict.h | 7,915,008 bytes | 0x1b082f67 | 0x43e6e94a |
Both dictionaries are built only from license-cleared sources.
4.6 Chunked stream 0xF3
Written when the input does not fit the encoder's memory budget, or arrives through a pipe and is longer than one chunk. One pass, no seeking, bounded memory on both sides: the decoder holds one chunk and its output.
u8 0xF3
u8 format version (0x10)
repeat:
varint len > 0: length of the next piece
len bytes one complete frame (any frame of section 4 except a chunked stream)
holding the next piece of the original
varint 0 end of the pieces
varint n original size (sum of the pieces)
u32 CRC-32 of the whole original- Each piece checks its own size and CRC; the trailer checks the whole.
- A stream cut anywhere, including exactly between pieces, is corrupt (no trailer, or a wrong total). Bytes after the trailer are an error.
- Pieces are independent (no shared model or window).
- Refuse a piece larger than the memory you may use ("chunk larger than the memory budget").
5. Inner streams
An inner stream starts with its u32 magic. Transform streams (5.3) hold further inner streams. Nesting is at most 32 deep: refuse deeper nesting as corrupt.
5.1 Leaf streams (coders)
| Magic | On disk | Level | Layout / reference |
|---|---|---|---|
BWZ1 | 1ZWB | speed | varint n; per 4 MiB block a varint (len << 1) | stored; then the block data in order. Blocks are lzf.h v2 blocks (Huffman-coded literal, token and offset streams) or stored bytes. |
BWF8 | 8FWB | default | BWT stream, compact header (5.2); segments coded by fastrc.h frc2 (adaptive); bit-packed aux indexes. Inputs up to 128 MiB. |
BWF6 | 6FWB | default | BWT stream, fixed-width header with a u32 aux spacing; segments coded by frc2. Written for empty BWT input. |
BWF7 | 7FWB | default | As BWF6; segments coded by static rANS tables delta-coded against the previous block (fastrc.h). Inputs over 128 MiB. |
BWE1 | 1EWB | balanced | BWT stream, compact header; segments in interleaved pairs coded by bal.h. |
BWH1 | 1HWB | dense | BWT stream, compact header; segments coded by dense.h. |
BWCF | FCWB | ultra | BWT stream, fixed-width header; CM parameter block; segments coded by context-mixing model 4 (bwtcm.c, encode_segment_p with MV_CUR = 4). Floating point per section 7. |
BWCG | GCWB | ultra | As BWCF with the compact header. Blocks of 64 MiB and more compressed with 8 or more threads. |
BWCD | DCWB | (read only) | As BWCF with context-mixing model 3. Written by 1.0 release candidates. |
BWCE | ECWB | (read only) | As BWCD with the compact header. |
BWL3 | 3LWB | default–ultra | Independent LZ blocks (lzr.h), decodable in parallel: u64 n, u32 block size, u32 block count, u32 length per block (bit 31: stored), block data. Chosen when LZ beats BWT (binaries, already-compressed data). |
BWG1 | 1GWB | (HTTP modes) | BWL3 layout; blocks may match into all earlier blocks (global window). |
BWR0 | 0RWB | any | u64 n, then n stored bytes. |
5.2 BWT stream header
Fields in order. H is a varint in the compact header (BWF8, BWE1, BWH1, BWCE, BWCG) and a u32 (or u64 where marked wide) in the fixed-width header (BWF6, BWF7, BWCD, BWCF).
u32 magic
[BWCD/BWCE/BWCF/BWCG only] CM parameter block:
u8 count (19), u32 x 3 (float bits of the mixer learning rates),
then count - 3 values H (rates, shifts)
H wide original size (before the word transform)
word-transform header (wrt.h): H on; if on: H prefix count, H escape byte,
H per prefix, H word count, H dictionary length, H chunk count,
then per chunk H wide encoded length, H wide decoded length
H wide BWT input size n (fixed-width: u32 high, u32 low)
H block size (compact: 0 = one block of n); at most 1,000,000,000
H segment count
aux spacing: compact: u8 log2(spacing), 1..30
BWF6/BWF7: u32 spacing
BWCD/BWCF: fixed 2^23 (8 MiB), not stored
H per segment, its length (compact: all but the last, which is implied)
per block, the libsais aux indexes (one per spacing step):
compact: bit-packed, each in ceil(log2(block length + 1)) bits,
MSB first, byte-aligned per block
fixed-width: u32 each
H per segment, its coded length (bit 31 set: stored segment of raw BWT bytes)
the segment data, in orderTo decode: decode each segment with the level's coder into the BWT data; invert each block with its aux indexes (libsais unbwt_aux semantics); if the word transform was on, decode the result with wrt.h to the original size.
5.3 Transform streams
These wrap an inner stream with a reversible transform. Undo the inner stream first, then the transform.
| Magic | On disk | Written for | Layout |
|---|---|---|---|
BWX1 | 1XWB | executables | u32 filter flags (1 ARM64 BL, 2 x86 CALL/JMP, 4 ARM64 ADRP, 8 x86 Jcc, 16 applied per 1 MiB chunk; above 31 is invalid), then the inner stream of the filtered data (exef.h). |
BWQ1 | 1QWB | structured binary data (tables, samples) | varint n, varint header length, region list (rel.h), then the inner stream of the transformed data. |
BWP4 | 4PWB | embedded deflate (gzip, zlib, PNG, zip), Go flate and xz streams that can be rebuilt bit-exactly | u32 record count, records (type, positions, encoder settings, PNG pixel-model blobs: pz.h, pzgo.h, xz.h, pxm.h), then the inner stream of the data with those streams expanded. |
BWV1 | 1VWB | H.264 video | u64 blob size, blob (avc.h: CABAC/CAVLC slices re-coded), then the inner stream of the rest. |
BWC5 | 5CWB | long-range repeats | u64 n, u32 flags (0), u32 minimum copy length, then three streams (residual, diff, copy list), each a u64 length and a complete inner stream (lrm.h). |
BWS1 | 1SWB | dense / ultra inputs over 1.25 GiB | Per segment: u64 length and a complete inner stream; a u64 0 ends the list. |
BWD1 | 1DWB | disk images from 16 MiB (VHD, VHDX, qcow2, VMDK, VDI, raw images with many zero blocks) | u64 n, u32 block size, u32 flags (0), map stream (u64 length + stream), u32 chunk count, per chunk a u8 residual-stream id and a u64 length, then the chunk streams (dsk.h). Only directly inside BWK3; its n must equal the frame's. |
5.4 Retired magics
BWC3, BWC4, BWC6–BWCC, BWF4, BWF5, BWL1, BWL2, BWP1–BWP3, BWK1, BWK2 come from pre-release builds. A 1.x encoder never writes them and a 1.x decoder refuses them ("pre-release"). They will never be reused.
6. Levels and the streams they write
A decoder does not need to know the level: every stream describes itself. For reference, what the 1.0 encoder writes (outermost stream first, [ ] = only when it helps):
| Level | Small / text path | Main pipeline |
|---|---|---|
-speed | w (hzh, lazy parse) up to 256 KiB | BWK3 [BWD1] [BWX1] [BWQ1] BWZ1 |
| default | w (hzh) for text up to 256 KiB; both paths tried up to 16 MiB | BWK3 [BWD1] [BWV1] [BWP4] [BWX1] [BWC5] [BWQ1] BWF8 / BWF7 / BWL3 / BWR0 |
-balanced | as default | as default, BWT coder BWE1 |
-dense | w (lzr, mixed literals) up to 256 KiB; both tried up to 16 MiB | as default plus [BWS1] after BWD1, BWT coder BWH1 |
-ultra | as dense | as dense, BWT coder BWCF / BWCG |
-web | w (hzh, optimal parse) for text up to 64 MiB and binaries up to 256 KiB | larger inputs: the default pipeline |
-webfast, -webfastplus, -webturbo | w (hzh against dictionary id 1, quicker parses) | larger inputs: the default pipeline |
At every level, input that is one repeated pattern of up to 255 bytes becomes 0xF1, and input whose compressed form is not smaller becomes 0xF0. Output may depend on the thread count, never on the machine: the same command with the same thread count gives the same bytes on macOS, Linux and Windows, x86-64 and ARM64.
7. Floating point
Only the ultra level's context-mixing coder uses floating point: IEEE-754 single precision. Encoder and decoder must evaluate every expression exactly as written, in source order, without fused multiply-add contraction (-ffp-contract=off) and without fast-math. All other coders use integer arithmetic only.
8. The .hzn archive
A .hzn archive stores many files and folders. All its integers are little-endian, unlike the .hz streams inside it.
+--------------------+
| header (32 bytes) |
+--------------------+
| block 0 | each block: a complete .hz frame, or stored bytes
| block 1 ... |
+--------------------+
| index ("HIDX") |
+--------------------+
(after an append: ... [old index] [new blocks] [new index])8.1 Header
offset size
0 4 magic "HZN\x1A" (48 5A 4E 1A)
4 1 container version (1)
5 1 flags (0)
6 2 0
8 8 u64 index_offset
16 8 u64 index_length
24 4 u32 index_crc CRC-32 of the index bytes
28 4 u32 header_crc CRC-32 of header bytes 0..27Refuse any other magic ("not a Horizon archive") or version ("made by a newer version"). The header is the only part rewritten in place; it always points at the latest complete index.
8.2 Index
str = u32 byte length followed by that many UTF-8 bytes.
4 bytes "HIDX"
str creator e.g. "hzn 1.3.0; horizon 41f51c643265" (informational)
i64 created unix seconds
u32 n_blocks
per block:
u64 offset absolute file offset of the block's bytes
u64 csize bytes stored in the file
u64 usize bytes after decoding
u32 ccrc CRC-32 of the csize stored bytes
u32 ucrc CRC-32 of the usize decoded bytes
u8 codec 0 = stored, 1 = Horizon .hz frame
u8 level section 8.4
u32 n_entries
per entry:
u8 type 0 = file, 1 = directory, 2 = symlink
str path
u32 mode Unix mode bits
i64 mtime_sec
u32 mtime_nsec
u64 size
u64 stream_offset
u32 crc CRC-32 of the file's contents
str link_target empty unless type = 28.3 Data model
- The blocks' decoded contents, concatenated in index order, form one logical stream. A file is
sizebytes atstream_offsetin that stream, and may span blocks. - A block with codec 1 is a complete
.hzframe (section 4): decode it as a whole. The container never looks inside it. Blocks Horizon cannot shrink are stored (codec 0). - Small files are packed together into solid blocks of up to 64 MiB; files of 8 MiB or more start and end their own blocks and are cut into chunks of up to 256 MiB. Readers must not rely on these sizes: use the index.
- Directories are explicit entries, so empty folders and their modes and times survive. Symlinks are stored, not followed. Extended attributes, ACLs, resource forks and hard links are not stored.
- Appending writes new blocks and a complete new index after the old end of the file, then rewrites the header. Replaced entries and old indexes remain as unreferenced bytes. A reader only ever follows the header.
8.4 Level byte
Each block records the level it was compressed with. It is informational (the block decodes from its own frame header) but part of container version 1: refuse a block whose level byte is not in this table.
| Byte | Level | horizon option |
|---|---|---|
| 0 | store | (none) |
| 1 | speed | -speed |
| 2 | normal | default |
| 3 | ultra | -ultra |
| 4 | balanced | -balanced |
| 5 | dense | -dense |
| 6 | web | -web |
| 7 | web fast | -webfast or -webturbo (same stream format) |
Apps that show levels list them fastest first: store, speed, web fast, web, normal, balanced, dense, ultra.
8.5 Paths and safety
- Paths are relative,
/-separated UTF-8, exactly as the file system returned them (no Unicode normalisation, so NFC and NFD names round-trip byte for byte). - Reject the whole archive if any path is empty, absolute, longer than 4096 bytes, contains a NUL, or has an empty,
.or..component. \is an ordinary character in a name, never a separator. On Windows, map it (and<>:"|?*, control characters, trailing dots and spaces, and device names such asCONorCOM1) to_, so..\..\xcan never escape.- Do not create a symlink that points outside the destination. Drop setuid and setgid bits when restoring modes.
8.6 Reading an archive, step by step
read 32 bytes; check magic, version == 1, header_crc
seek index_offset; read index_length bytes; check index_crc; check "HIDX"
parse blocks; for each: offset + csize <= index_offset, level byte known
parse entries; validate every path (8.5) before extracting anything
stream_start[i] = sum of usize of blocks 0..i-1
to extract a file (stream_offset, size):
for each block overlapping [stream_offset, stream_offset + size):
read csize bytes at offset; check ccrc
codec 0: the bytes are the data; codec 1: decode the .hz frame
check usize and ucrc
copy the overlapping part
check the file's crcTo list an archive you only need the header and the index: no block has to be decoded.
9. Untrusted input
- Treat every size field as untrusted. A size the remaining input or the decoded result cannot satisfy is corruption.
- Refuse, up front, a single buffer larger than you can afford (the reference decoder's limit is twice the machine's physical memory) instead of letting the system kill the process later.
- Check every CRC the format gives you, and write nothing for a stream or file that fails.
- Ask before an archive expands to more than 100 times its size and more than 1 GB.
The reference command line reports these cases with exit status 2 ("corrupt, truncated, not a Horizon file, or from an incompatible version") and removes any partial output.
10. Conformance
The reference repository keeps a compatibility corpus: .hz files written by the 1.0 encoder at every level, covering every frame type and the transforms small inputs reach, with the expected size and SHA-256 of each decoded file. Every Horizon build must decode all of them byte-exact. Files in the corpus are never regenerated: a change that breaks one is a new major format, not a test update. A second list holds damaged files a decoder must refuse (chunked streams cut between pieces, without totals, with trailing bytes, with a wrong CRC).
If you are writing a decoder and want the corpus to test against, ask us (section 11).
11. Adding Horizon support to your app
Listing and extracting .hzn
Sections 8.1 to 8.3 are all you need to list an archive and to extract stored blocks. To extract compressed blocks you also need to decode .hz frames.
Decoding .hz with the horizon command
The free Tuntau Horizon download includes the horizon command line tool. The Horizon app itself uses it this way, as a black box:
horizon d file.hz out # decode one frame to a file
horizon -d -q - - < in.hz > out # stdin to stdout, any length, bounded memory
horizon t *.hz # test: decode and verify, write nothing
horizon l *.hz # list: sizes, ratio, CRC, stream type
horizon -V # program and format versionExit status 0 means the whole output was written; 1 is a usage or I/O error; 2 means the input is corrupt, truncated, not a Horizon file or from an incompatible version. On any status but 0, discard the output. Shipping horizon inside your own product is covered by the Horizon licence agreement; write to us if your case is not clearly allowed there.
Writing your own decoder
Start with the frames that need no coder: stored (0xF0), periodic (0xF1), chunked (0xF3) and BWR0. Then add the leaf coders your users meet most: the web frame with hzh (web content and small files), BWZ1 (speed) and BWF8 (default). The coded bits of each leaf stream are defined by the reference sources named in section 5.1.
Questions, the reference sources or the test corpus: write to hello@tuntau.com. I read every message myself, and I'd like to hear about any app that adds Horizon support.