1 /* File: "binpatch.c", Time-stamp: <2007-04-23 22:00:14 feeley> */
3 /* Copyright (c) 2007 by Marc Feeley, All Rights Reserved. */
5 /* This is a tool for patching binary files */
7 /*---------------------------------------------------------------------------*/
13 void binpatch (char *oldstr
, char *newstr
, char *filename
)
15 int oldlen
= strlen (oldstr
) + 1;
16 int newlen
= strlen (newstr
) + 1;
18 int buflen
= step
+ oldlen
- 1;
24 buf
= malloc (buflen
);
27 printf ("Could not allocate buffer\n");
31 f
= fopen (filename
, "r+b");
35 printf ("Could not open file \"%s\"\n", filename
);
39 fseek (f
, 0, SEEK_END
);
44 printf ("Could not seek\n");
56 int x
= fseek (f
, pos
, SEEK_SET
);
60 printf ("Could not seek\n");
63 if (fread (buf
, 1, n
, f
) != n
)
66 printf ("Could not read\n");
71 if (buf
[i
] == oldstr
[0])
74 for (j
=1; j
<oldlen
; j
++)
75 if (buf
[i
+j
] != oldstr
[j
])
78 x
= fseek (f
, pos
, SEEK_SET
);
82 printf ("Could not seek\n");
85 if (fwrite (newstr
, 1, newlen
, f
) != newlen
)
88 printf ("Could not write\n");
99 printf ("Did not find string in file \"%s\"\n", filename
);
103 int main (int argc
, char *argv
[])
109 printf ("Usage: binpatch <old-string> <new-string> <filename>...\n");
113 for (i
=3; i
<argc
; i
++)
114 binpatch (argv
[1], argv
[2], argv
[i
]);
119 /*---------------------------------------------------------------------------*/