2

I have seen and used the GUI hex-editor bless, to modify bitmaps to match a separate file type (form bmp to srf). My question is how to modify file headers programmatically with the command line. What programs would allow me to make changes to file headers from shell script?

I have looked in synaptic package manager, but I'm new to modifying files in hex. Do bitmaps count as binary files? Where this is a software recommendation request, an acceptable answer would allow me to write, overwrite, or change specific values to specific locations (such as the first 16 bytes of a file), in the command line.

dessert
  • 39,982
j0h
  • 14,825

1 Answers1

5

xxd is a very powerful command-line hex editor which allows you to change binary files with a single command line. You can use it this way:

xxd -r -p -o OFFSET <(echo NEW HEX SIGNATURE) FILE  # or
echo NEW HEX SIGNATURE | xxd -r -p -o OFFSET - FILE

Let's “convert” 01.png file to rar format (hex signature 5261 7221 1A07 0000, offset 01):

$ file 01.png 
01.png: PNG image data, 1280 x 1024, 8-bit/color RGB, non-interlaced
$ xxd 01.png | head -1
00000000: 8950 4e47 0d0a 1a0a 0000 000d 4948 4452  .PNG........IHDR
$ xxd -r -p -o 0 <(echo 5261 7221 1A07 0000) 01.png
$ file 01.png 
01.png: RAR archive data, vdb, os: MS-DOS
$ xxd 01.png | head -1
00000000: 5261 7221 1a07 0000 0000 000d 4948 4452  Rar!........IHDR

See man xxd for other useful options and lots of helpful examples.

1If the offset is 0 the option -o can be omitted, I include it here only to show its usage.

If you like it better you can do the truncating with dd as well, but that seems unnecessarily complicated:

dd conv=notrunc obs=1 if=<(xxd -r -p -o 0 <(echo 5261 7221 1A07 0000)) of=01.png

Further reading (besides usual command-line help):

dessert
  • 39,982