a simple format and implementation for human-readable binary diffs.
the haxdiff/1.0 format looks pretty much like a standard unified text patch
(as produced with diff -u), and can therefore make use of existing
syntax highlighting facilities in an editor (e.g. nano
).
a patch consists of one or more hunks, with the following layout:
@@
, followed by the offset, ,-
,
number of bytes removed, ,+
number of bytes inserted.
all 3 values are in hexadecimal without leading 0x
.
example: @@ 17b0,-4,+4
-
and a hexadecimal byte sequence.
if provided, the total number of bytes in all -
sequences must be
identical to the number specified for -
in the hunk header.
the total length of each line shall not exceed 1000 bytes, but it is
advisable to keep it to 80 chars for readability.
the minus
byte sequence denotes the bytes that will be overwritten
by the patch, and its purpose is to make sure the input file is in
the expected format, plus it allows a reader to see the actual
difference and even to manually apply a patch using a hexeditor.
the -
lines are optional and may be omitted.+
and a hexadecimal byte sequence.
the same rules mentioned for the minus lines apply, except that these
are not optional.@@ offset,-0,+len
where offset is identical
to O's filesize, and len the difference between O and M (aka M size-O size).
it shall be followed by only +
lines describing all the additional
bytes of M.@@ offset,-len,+0
where offset is identical
to M's filesize, and len the difference between M and O (aka O size-M size).
the header may be followed by only -
lines describing all the removed
bytes.@
, -
and +
shall be ignored.\n
or \r\n
characters.a-f
.full example:
@@ 17b0,-4,+4
- 04020004
+ 00000000
@@ 3dc14,-4,+4
- 04020004
+ 00000000
@@ b666c,-8,+8
- 0e48396801600e48
+ 0048004701bb3e08
@@ 3ebcb0,-8,+0
- ffffffffffffffff
compared with other binary patch solutions, this format/impl has the following advantages:
vbindiff
.@-+,
and newline characters are
used, standard compression tools can very efficiently compress these
patches. for example a 172kb patch used for testing compresses to less than
1KB using gzip.disadvantages:
the haxdiff
reference implementation allows both to create and apply patches.
patches are always created including the optional -
lines, so a patch's
consistency can be controlled.
it currently does not allow for hunks that have differing +
and -
- except
for truncation and expansion cases.
the haxdiff reference implementation is (C) 2021 rofl0r and licensed as MIT. the format itself is licensed as public domain.
run make
.
if you want to use specific CFLAGS, you can use either of
make CFLAGS="-O2 -g2"
or echo "CFLAGS = -O0 -g3" > config.mak
.
haxdiff MODE FILE1 FILE2
creates or applies a binary diff.
MODE: d - diff, p - patch, P - force patch
in mode d, create a diff between files FILE1 and FILE2,
and write a patch to stdout.
the generated patch describes how to generate FILE2 from FILE1.
in patch mode, FILE1 denotes the file to be patched, and FILE2
the patched file that will be written (e.g. output file).
the patch itself is read from stdin.
if mode is p, the replaced bytes must match those described in
the patch. if mode is P, the replaced bytes will be ignored.
if mode is d, the patch will be written to stdout.