Monday, November 19, 2007

bsdiff - binary diff

So I've tested the bsdiff. In conclusion, it does patch up the binary file as advertised. But, doesn't have any check for original file like GNU Patch has. So, here is how it works:

$ cat hello.c

int main()
printf("hello world!\n");
return 0;
$ cat hello-bsdiff.c

int main()
printf("hello bsfile!\n");
return 0;

Prepare two source code as above. The difference is bellow.

$ diff -u hello.c hello-bsdiff.c
--- hello.c 2007-11-20 13:44:39.000000000 +0900
+++ hello-bsdiff.c 2007-11-20 14:11:09.000000000 +0900
@@ -2,6 +2,6 @@

int main()
- printf("hello world!\n");
+ printf("hello bsdiff!\n");
return 0;

As you can see the code differ only in text section. Now compile both code to create binaries "hello.out" and "hello-bsdiff.out".

$ gcc -Wall hello.c -o hello
$ gcc -Wall hello-bsdiff.c -o hello-bsdiff

Generate binary diff with bsdiff.

The synopsis for bsdiff is:

bsdiff <oldfile> <newfile> <patchfile>

$ bsdiff hello hello-bsdiff hello-hello-bsdiff.bsdiff

Let's see how well bsdiff did, in size.

$ ls -l *.out *.bsdiff
-rwxr-xr-x 1 user user 8963 2007-11-20 14:12 hello-bsdiff.out
-rw-r--r-- 1 user user 199 2007-11-20 14:12 hello-hello-bsdiff.bsdiff
-rwxr-xr-x 1 user user 8956 2007-11-20 14:11 hello.out

Patch up the hello.out to create hello-new.out, which should be identical to hello-bsdiff.out.

The synopsis for bspatch is similer to bsdiff:
bspatch <oldfile> <newfile> <patchfile>

$ bspatch hello.out hello-new.out hello-hello-bsdiff.bsdiff
$ md5sum hello-bsdiff.out hello-new.out
3564457345c42a2e0cce1c0a2191bece hello-bsdiff.out
3564457345c42a2e0cce1c0a2191bece hello-new.out

See that hello-bsdiff.out and hello-new.out has same hash value, 356445....

Let's also apply the binary patch to the /dev/null to see how does the bspatch works.

$ bspatch /dev/null hello-new-bad.out hello-hello-bsdiff.bsdiff
$ md5sum hello-bsdiff.out hello-new-bad.out
3564457345c42a2e0cce1c0a2191bece hello-bsdiff.out
5f52d2ca0b2c01a1d05865ef2af102f4 hello-new-bad.out

bspatch doesn't even complain at all and, of cause, the generated file, hello-new-bad.out, is completely different from hello-bsdiff.out.

No comments :