/* * binpatch 0.0.1, testing binary patching utility. * * Copyright (C) 2006 Jindrich Novy (jnovy@users.sourceforge.net) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include #include #include #include #include uint8_t *b1; off_t patchsize; void *check_malloc( size_t s ) { void *p = malloc(s); if ( !p ) { printf("Can't allocate %llu bytes, exiting...\n", s); exit(1); } return p; } uint32_t gc( FILE *f ) { uint32_t c; if ( !patchsize ) { fprintf(stderr, "Read request at EOF!"); exit(1); } if ( (c=fgetc(f)) == EOF ) { fprintf(stderr,"Unexpected EOF!\n"); exit(1); } patchsize--; return c; } uint32_t gn( FILE *f ) { return gc(f)|(gc(f)<<8)|(gc(f)<<16); } void pc( int c, FILE *f ) { if ( fputc(c,f) == EOF ) { fprintf(stderr,"Error in writing!\n"); exit(1); } } int main( int argc, char **argv ) { FILE *fi, *fp, *fo; struct stat s1, s2; if ( argc != 4 ) { fprintf(stderr,"%s \n", argv[0]); exit(1); } if ( stat(argv[1], &s1 ) ) { perror(""); exit(1); } if ( stat(argv[2], &s2 ) ) { perror(""); exit(1); } patchsize = s2.st_size; printf("%d\n", patchsize); fp = fopen(argv[2],"rb"); fo = fopen(argv[3],"wb"); fi = fopen(argv[1],"rb"); b1 = check_malloc(s1.st_size); fread(b1, 1, s1.st_size, fi); fclose(fi); { uint32_t unmatched, i; unmatched = gn(fp); for ( i=0; i