(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*) (** BSDIFF40 binary deltas. The patch format of Colin Percival's {{:https://www.daemonology.net/bsdiff/}bsdiff} and [bspatch] tools: a [BSDIFF40] header followed by three bzip2 streams: control triples, byte-wise difference data and extra data. Patches produced by {!diff} apply with the reference [bspatch], and {!apply} reads patches produced by the reference [bsdiff]: {[ let source = "the quick brown fox jumps over the lazy dog" let target = "the quick brown cat jumps over the lazy dog!" let patch = Bsdiff.diff ~source ~target let () = assert (Bsdiff.apply ~source patch = Ok target) ]} BSDIFF40 excels on executables and other data where most bytes change by small deltas at stable offsets: the matcher (a qsufsort suffix array over the source, see {!Suffix_array}) aligns regions even when every byte differs slightly, and the byte-wise difference section then compresses extremely well. For text-like data where regions are either identical or replaced, the VCDIFF format usually produces smaller patches. *) (** BSDIFF40 errors, as kinds of {!Loc.Error.kind}. *) module Error : sig type Loc.Error.kind += | Magic_mismatch of string (** The first bytes of the patch are not ["BSDIFF40"]. Carries the bytes found. *) | Truncated of string (** The patch ended while reading the named field or section. *) | Corrupt of string (** A section does not decode: a header length or control count with the sign bit set or past the native int range, a bzip2 stream error, or a control section that is not a whole number of triples. *) | Invalid of string (** The control program is inconsistent with the data: it overruns the declared size, reads outside the source, or leaves a section unconsumed. *) end module Suffix_array = Suffix_array (** The qsufsort suffix array the matcher uses. *) (** {1:wire Wire descriptions} *) type header (** The type for the binary description of the fixed 32-byte BSDIFF40 header. *) val header_codec : header Wire.Codec.t (** [header_codec] is the Wire codec for the magic, control-stream length, diff-stream length and target-size fields. It is exposed for EverParse C generation; ordinary OCaml callers should use {!diff} and {!apply}. *) val magic : (string, header) Wire.Codec.field (** [magic] is the field handle for the 8-byte ["BSDIFF40"] magic. *) val ctrl_len : (int, header) Wire.Codec.field (** [ctrl_len] is the field handle for the compressed control-stream length. *) val diff_len : (int, header) Wire.Codec.field (** [diff_len] is the field handle for the compressed diff-stream length. *) val new_size : (int, header) Wire.Codec.field (** [new_size] is the field handle for the reconstructed target size. *) type triple (** The type for the binary description of one 24-byte BSDIFF40 control triple. *) val triple_codec : triple Wire.Codec.t (** [triple_codec] is the Wire codec for one control triple: diff bytes to mix, extra bytes to copy, and signed source seek. It is exposed for EverParse C generation. *) val mix : (int, triple) Wire.Codec.field (** [mix] is the field handle for the diff-byte count. *) val copy : (int, triple) Wire.Codec.field (** [copy] is the field handle for the extra-byte count. *) val seek : (int64, triple) Wire.Codec.field (** [seek] is the raw sign-magnitude source seek: bit 63 is the sign flag and bits 0..62 are the magnitude. {!apply} validates that the magnitude fits the host's native integer range before executing the control program. *) (** {1:patching Patching} *) val diff : source:string -> target:string -> string (** [diff ~source ~target] is a BSDIFF40 patch turning [source] into [target]. [apply ~source (diff ~source ~target) = Ok target] holds for all inputs. *) val apply_writer : source:string -> string -> Bytesrw.Bytes.Writer.t -> (unit, Loc.Error.t) result (** [apply_writer ~source patch w] reconstructs the BSDIFF40 target of [patch] against [source] and streams it to the writer [w], leaving the final {!Bytesrw.Bytes.Slice.eod} to the caller. This is [Error e] if [patch] does not start with the ["BSDIFF40"] magic, is truncated, does not decode, or holds a control program inconsistent with [source]; see {!Error}. [w] may then have received part of the target. It decompresses the control, diff and extra sections incrementally and writes each target run as it is produced, so resident memory stays bounded by the stream slice length rather than the target size. [source] is held whole because the control program seeks freely within it. *) val apply : source:string -> string -> (string, Loc.Error.t) result (** [apply ~source patch] is the target {!apply_writer} streams, as a string. *)