- OCaml 99%
- Shell 1%
vcdiff #
Shipping a new version of a large file costs the whole file unless the receiver
already holds an old version and the sender ships only the difference. VCDIFF
(RFC 3284) is the IETF's generic format for that difference: a delta
encodes a target byte string as ADD, RUN and COPY instructions over a source
byte string. xdelta3 produces it, and HTTP delta encoding (RFC 3229) carries
it. vcdiff implements VCDIFF in OCaml:
Vcdiff.diffcomputes a delta with a greedy block-hash matcher;Vcdiff.applyreconstructs the target, including overlapping (self-extending) copies, VCD_TARGET windows and Adler-32 window checksums;Vcdiff.of_stringandVcdiff.to_stringdecode a delta to a value (windows of typed instructions, with the COPY address caches resolved) and encode it back.
A delta is a sequence of windows, each rebuilding one contiguous piece of the
target from a segment it names: VCD_SOURCE takes the segment from the source,
VCD_TARGET from the part of the target already rebuilt. A COPY may overlap
the bytes it is producing, which repeats a pattern (self-extending). Adler-32
is a 32-bit checksum made of two running sums modulo 65,521 (RFC 1950), which
xdelta3 stores per window so the receiver can detect a wrong
reconstruction. To keep COPY addresses short, the encoder writes each one
relative to a cache of recent addresses: a near cache of the last 4 and a
same cache of 768 entries (3 x 256) indexed by the address modulo 768,
naming the cache mode and a small offset. The code table maps each one-byte
opcode to up to two instructions with their sizes and modes, and RFC 3284
section 5.6 defines the default one of 256 entries.
The decoder accepts any delta that uses the default instruction code table,
including the xdelta3 application-header (VCD_APPHEADER) and window checksum
(VCD_ADLER32) extensions. Secondary compression and application-defined code
tables are refused with a typed Unsupported error.
Installation #
$ opam install vcdiff
If opam cannot find the package, it may not yet be released in the public
opam-repository. Add the overlay repository, then install it:
$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install vcdiff
Usage #
# let source = "the quick brown fox jumps over the lazy dog" in
let target = "the quick brown cat jumps over the lazy dog!" in
let delta = Vcdiff.diff ~source ~target in
Vcdiff.apply_exn ~source delta = target
- : bool = true
Vcdiff.diff indexes the source by the hash of every 16-byte block at an
offset that is a multiple of 16, keeping at most 4 source offsets per hash.
It then hashes the 16 bytes at each target offset, looks the hash up, extends
each candidate forward while the bytes agree and backward over the pending
literal bytes, and keeps the longest match of at least 16 bytes as a COPY;
the bytes between matches become one ADD. The index costs S bytes of hashing
for a source of S bytes and S/16 entries. The scan costs 16 byte reads per
target offset outside a match, since the hash is recomputed rather than
rolled, plus at most 4 extensions per lookup, so the whole diff is linear in
S + 16T for a target of T bytes. A match that starts inside a source block is
found from the next block boundary and extended backward.
In the second example below, the source is the bytes 0 to 255 repeated 16
times, and the target is the source from offset 7, 4,000 bytes long,
followed by tail. Every source block content occurs 16 times, so the index
keeps only its last 4 offsets. The scan misses at target offsets 0 to 8,
since no source block starts at offset 7. At target offset 9 the best
candidate is the source block at 3,088, which extends forward to the end of
the source and backward 9 bytes: COPY addr 3079, len 1017. The rest of the
target follows as COPY addr 3072 of 1,024, 1,024 and 935 bytes, then
ADD "tail". The encoded delta of five instructions is under 64 bytes
against a 4,004-byte target:
# let source = String.init 4096 (fun i -> Char.chr (i land 0xff)) in
let target = String.sub source 7 4000 ^ "tail" in
let delta = Vcdiff.to_string (Vcdiff.diff ~source ~target) in
String.length delta < 64
- : bool = true
Deltas are ordinary values, so they can be inspected and built directly:
# let w =
{
Vcdiff.Value.Window.source = None;
checksum = None;
insts =
[ Vcdiff.Value.Add "ab"; Vcdiff.Value.Copy { addr = 0; len = 6 } ];
}
in
Vcdiff.apply_exn ~source:"" { Vcdiff.Value.app_header = None; windows = [ w ] }
- : string = "abababab"
Licence #
ISC. See LICENSE.md.