Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 16 Mar 2002 17:32:54 -0500
From:      "Clark C . Evans" <cce@clarkevans.com>
To:        hackers@freebsd.org
Subject:   simple binary patch utility (for iso cdrom images)
Message-ID:  <20020316173254.A15494@doublegemini.com>

next in thread | raw e-mail | index | archive | help
I looked around for quite a while for a simple program
to do a binary patch on an iso cdrom image.  I was hoping
that I could use "bvi" or similar binary editor, but it
wasn't clear how I could get them to do simple string
replacement.  So, I wrote one and am putting it in the
public domain, I hope someone finds it useful.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define TRUE	-1
#define FALSE	0

long findstr(FILE *file, const char *str,int len)  {
    long r = 0;
    int  c = 0;
    int  i = 0;
    while(-1 != (c=fgetc(file))) {
        if(str[i] == ((char)c)) {
            i++;
            if(i<len) continue;
            /* found it */
            return r;
        }
        if(i) {
            /* bad match */
            r += i;
            i = 0;
        }
        r += 1;
    }
    return 0;
}

void main(int argc, char **argv)
{
	FILE	*file;
        const char *find, *repl;
	long	pos, len;
	if ( argc < 4 )
	{
		printf("Usage: %s file find replace\n",argv[0]);
		return;
	}
        find = argv[2];
        repl = argv[3];
        len  = strlen(find);
        if ( len != strlen(repl) || len < 1)
        {
               printf("Find string is not same size as replace string.\n");
               return;
        }
	file = fopen( argv[1], "r+b");
	if ( file == NULL )
	{
		printf("Unable to open file <%s>\n", argv[1]);
		return;
	}
        pos = findstr(file,find,len);
        if (!pos) 
        {
                printf("Unable to find string '%s'\n",find);
                return;
        }
	fseek(file,pos,SEEK_SET);
	fwrite(repl,1,len,file);
	fclose( file );
}


-- 
Clark C. Evans                   Axista, Inc.
http://www.axista.com            800.926.5525
XCOLLA Collaborative Project Management Software

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20020316173254.A15494>