/* binpatch.c Simple non-interactive binary patch utility DWF 10/10/94 Third version -- bug fixes only Changes from version 2 (7/23/94): Fixed logic error that caused some matching strings to be missed. Changes from version 1 (7/19/94): Fixed logic error in check for line length overflow. Temp1 and temp2 may now be used as names for the output file. Null and garbled patch files are now flagged. #included . Usage: binpatch infile outfile < patch Although gcc will compile it just fine under Unix, this program is written so that it should work under Messy-DOS too; i.e., it uses temporary files instead of reading the entire binary into memory. I have successfully compiled it with an old version of Turbo C, but I had to add an #ifdef to the includes. Here is an example patch file. Comments, with # in the first column, are ignored; the other lines alternate between search-string and replace-string in hexadecimal. # Patches for shareware doom.exe 1.5 beta posted to alt.games.doom by # ep104@cus.cam.ac.uk (Elias 'CaveMan' Papavassilopoulos) # # Fix crash & burn on idkfa + shotgun B9 09 00 00 00 C7 40 B0 02 00 00 00 B9 08 00 00 00 C7 40 B0 02 00 00 00 # # Enable cheat codes in Nightmare mode 0F 85 29 03 00 00 83 3D 70 82 02 00 04 0F 85 29 03 00 00 83 3D 70 82 02 00 45 */ #include #include #ifndef __TURBOC__ #include #endif #include #include /* Names for temporary files */ #define temp1 "temp1" #define temp2 "temp2" /* Maximum length of text lines on input */ #define maxlen 80 void usage () { puts ("Usage: binpatch infile outfile < patch"); exit (0); } void bail () { unlink (temp1); unlink (temp2); exit (-1); } void dont_clobber (char *fname) { FILE *inf; if ((inf = fopen (fname, "r"))) { printf ("Will not overwrite existing file %s -- rename or delete it\n", fname); exit (-1); } } int get_line (unsigned char line[maxlen], int *len) { char txtlin[maxlen]; int t, tt; *len = 0; do { if ((t = (int) fgets (txtlin, maxlen, stdin))) if (strlen (txtlin) >= maxlen-1) { printf ("Line in patch file >= %d characters: too long\n", maxlen-1); bail (); } } while ((txtlin[0] == '#') && t); if (!t) return 0; /* Check for garbled patch file */ for (t=0;t