diff - find specific change between two values in hex dump
Asked Answered
C

1

8

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!

Cosy answered 5/5, 2013 at 0:1 Comment(0)
M
19

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
Mantic answered 5/5, 2013 at 0:16 Comment(4)
That offers a text-mode comparison of the output of two binary-to-text files, which I'm already generating in vim. How does this address the problem of field-specific comparison given that I know that a single field will start at value x and in the second file will be value y?Cosy
I don't understand the problem. cmp -l will "Print the byte number (decimal) and the differing byte values (octal) for each difference." Just grep through that list for the known values, and it will give you the decimal offset.Mantic
Now I understand what you meant. Thanks for your patience!Cosy
a slight variation may be: vim -d <(xxd fileA) <(xxd fileB)Sinaloa

© 2022 - 2024 — McMap. All rights reserved.