Removing magic numbers

Kurt Wall kwall
Mon May 17 12:02:19 PDT 2004


In a 1.2K blaze of typing glory, Alan Jackson wrote:
> My son has a friend who ftp'd a bunch of perl scripts from Windoze
> to a Linux box for CGI stuff. Windows stupidly (or purposely?) screwed
> up the magic number in the files so that they are not recognized
> as perl scripts and won't run.
> 
> He figured out how to use hexedit to get rid of the magic number and fix
> the problem, but now we're wondering about a better solution - a way to
> script it and clean up the files automagically. We could write a little
> C program, but surely there is another way out there somewhere?
> 
> All we need is a way to delete the first 3 bytes from a binary file.

Joel's suggested sed script ought to work. I was thinking of using dd
to read and write:

$ dd if=incoming of=outoing bs=1 skip=3

This sets a block size of 1 byte and skips the first three blocks (which
is 3 bytes in this case) when reading the input file. Thus:

$ dd if=jul1706.jpg of=tmp.jpg bs=1 skip=3
40110+0 records in
40110+0 records out
$ ls -l jul1706.jpg tmp.jpg
-rw-------    1 kwall    users       40113 2004-05-07 12:43 jul1706.jpg
-rw-r--r--    1 kwall    users       40110 2004-05-14 04:24 tmp.jpg

So, you can see the output file is three bytes smaller. hexdump indicates
that the required bytes were removed:

$ hexdump -C -n 8 jul1706.jpg
00000000  ff d8 ff e0 00 10 4a 46                           |????..JF|
00000008
$ hexdump -C -n 8 tmp.jpg 
00000000  e0 00 10 4a 46 49 46 00                           |?..JFIF.|
00000008

The fourth byte in the input file is the first byte in the output file,
which indicates that it worked. As usual, YMMV and if it breaks, you get
to keep both pieces.

Kurt
-- 
Common sense and a sense of humor are the same thing, moving at
different speeds.  A sense of humor is just common sense, dancing.
	-- Clive James



More information about the Linux-users mailing list