Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

diff - find specific change between two values in hex dump

I am analyzing hex data from binary data dumps from a basic command-line program of mine. I'm basically dumping the exact contents of a struct (a large array of structs, actually) to a text file.

I then create a second binary dump, and compare the two files in vim using xxd to create binary-to-text representations of the original data.

Both files are the exact same size in bytes, and I'm trying to compare the two in a meaningful way. Even a small change in the data before I dump the file results in a large change in other parts of the file, due to other sections containing hashes, functions based on the value I changed, and so forth.

Is it possible to tell diff or vimdiff to say, compare two files, and show me only the parts of the file where in the original file (ie: file 1) a value was set to 1, and in the second file, the value was set to 32?

Thank you!

like image 285
Cloud Avatar asked Dec 09 '22 15:12

Cloud


1 Answers

I use:

diff <(xxd file1.bin) <(xxd file2.bin)

This uses process substitution to compare the output of two xxd processes. Note that this still shows line differences, so if any byte on a line is different it will be listed. This gives a nice hexdump-looking comparison.

The classical tool for this however, is cmp.

So, this could be handled like so:

cmp -l file1.raw file2.raw | grep -in "oldValue" | grep -in "newValue"

This will list exactly what you need, with the following fields printed out:

OFFSET  VALUE_IN_FILE_1 VALUE_IN_FILE_2
like image 72
Jonathon Reinhart Avatar answered Feb 03 '23 10:02

Jonathon Reinhart