ftp.nice.ch/pub/next/tools/printer/DeskJet.2.03.N.bs.tar.gz#/DeskJet_2.03/binfile.c

This is binfile.c in view mode; [Download] [Up]

#include <stdio.h>
#include <sys/file.h>
#include <sys/stat.h>

char   MeName[9] = "binfile";

/*
binfile
 
Function:    Utility program to allow one to search, verify and change simple
			strings in a binary file (i.e. a file which does not depend on
			null-terminated strings).

Syntax:
	binfile  [-options]  file
 
	file		is the file to be read. It must be specified.
	 
Options:   (controlled by [-options] in the command)  May be specified multiple times
	-f	String to find	(find string in file)
	-s	Offset Length	(to show data at place in file)
	-c	Offset String (to change data at a place in file)
	-v	Offset String (to verify data at a place in file)
	-z	(Enable some debugging)

Returns:	0

Note:	Not much effort has been made to make the searching clever.
		-c cause the input file to be rewritten. This could fail. Caveat emptor!
 
Copyright (c) Terence P. Gliedt, 1992.
This program may be freely redistributable in its original, unmodified form.
The author grants you a nonexclusive, royalty-free license to use this software,
and modify it as needed provided that this copyright notice remains, and any
changes are clearly indicated as such.  No part may be incorporated in products or
packages sold for profit without express permission of the copyright holder.
*/
int  main ( int argc, char *argv[] )
{
 
 	int	InFile;			/* File handle to read */
	int	I,K, L,M;			/* Bean counters */
	struct stat Stat;			/* Returned by stat call */
	char *P,*Q;			/* Bean pointers */
	char *Fileid;			/* Name of file we are reading */
	int	Debug = 0;		/* Debugging or not */
	int FindString();		/* Search for string with this */
	int	Writable = 0;		/* File must be writable */

	/*	Parse options */
	for(I=0; I<argc-1; I++) {
		if (argv[I][0] == '-') {
			switch (argv[I][1]) {
				case 'f' :		/* String to find */
					I++;
					if (argv[I][0] == '-') printf("%s: unexpected option '%s' encountered\n", MeName, argv[I]);
					break;
				case 's' :		/* Show data */
					for (K=0; K<2; K++) {
						I++;
						if (argv[I][0] == '-') printf("%s: unexpected option '%s' encountered\n", MeName, argv[I]);
					}
					break;
				case 'v' :		/* Verify data */
					for (K=0; K<2; K++) {
						I++;
						if (argv[I][0] == '-') printf("%s: unexpected option '%s' encountered\n", MeName, argv[I]);
					}
					break;
				case 'c' :		/* Change data */
					for (K=0; K<2; K++) {
						I++;
						if (argv[I][0] == '-') printf("%s: unexpected option '%s' encountered\n", MeName, argv[I]);
					}
					Writable++;		/* Open file writable please */
					break;
				case 'z' :
					Debug++;
					break;
				default :
					printf("%s: ignoring option '%s'\n", MeName, argv[I]);
			}
		}
 	}
	
	/*	Whats left is the name of the file to read. If nothing specified, use stdin. */
	if ((I==0) || (I==argc))  {
	fprintf(stderr,"usage: %s [-z] [-f string]  [-s offset length] [-c offset string] [-v offset string] input-file\n", MeName);
		return(2);
	}
    	else {
		Fileid = argv[I];
		if (Writable) InFile = open(Fileid, O_RDWR, 0666);
		else InFile = open(Fileid, O_RDONLY, 0444);
		if (InFile <= 0) {
			fprintf(stderr, "%s: Unable to open file '%s'\n", MeName, Fileid);
			return(2);
		}
	}
	argc--;				/* Number of args to search */
	
	if (Debug) {
		for(I = 0; I < argc; I++) {
			switch (argv[I][1]) {
				case 'f' :		/* String to find */
					I++;
					fprintf(stderr, "	searching for 'string %d bytes: '%s' \n", strlen(argv[I]), argv[I]);
					break;
				case 's' :		/* Show data */
					I++;
					L = atoi(argv[I+1]);
					K = atoi(argv[I]);
					fprintf(stderr, "	show %d bytes at offset %d\n", L, K);
					I++;
					break;
				case 'v' :		/*Verify data */
					I++;
					K = atoi(argv[I]);
					fprintf(stderr, "	verify%d bytes at offset %d='%s'\n", strlen(argv[I+1]), K, argv[I+1]);
					I++;
					break;
				case 'c' :		/* Change data */
					I++;
					K = atoi(argv[I]);
					fprintf(stderr, "	change %d bytes at offset %d to %s\n", strlen(argv[I+1]), K, argv[I+2]);
					I++;
					break;
			}
 		}
 	}

	/* Now read the file into a big buffer */
	fstat(InFile, &Stat);
	fprintf(stderr, "%s: Reading %d bytes...", MeName, Stat.st_size);
	if ((P = (char *) malloc(Stat.st_size)) == NULL)  {
		perror("malloc:");
		return(3);
	}
	if ((read(InFile, P, Stat.st_size)) <= 0) {
		perror("read:");
		return(4);
	}
	printf(" complete. \n");
	if (Debug > 2) for(Q=P, I=Stat.st_size; I>0; Q++, I--) fprintf(stderr, "%c", *Q);
	
	/* Now search the file. Everytime you get a match for a string, report it */
	for(I=0, L=-1; I < argc; I++) {
		if (argv[I][0]=='-') {
			switch (argv[I][1]) {
				case 'f' :		/* String to find */
					if (L == -1) L = 0;	/* Flag that we did a FindString */
					I++;
					K = FindString(argv[I], P, Stat.st_size);
					if (K<=0) fprintf(stderr, "%s: String '%s' not found\n", MeName, argv[I]);
					L = K + L;
					break;
			}
		}
 	}
	if (L==0) fprintf(stderr, "No strings found in '%s'\n", Fileid);

	/* Now show data that might be in the file */
	for(I=0; I<argc; I++) {
		if (argv[I][0]=='-') {
			switch (argv[I][1]) {
				case 's' :		/* Show data */
					I++;
					L = atoi(argv[I+1]);
					K = atoi(argv[I]);
					printf("  Data at offset %d: ", K);
					for (M=0; M<L; M++) printf("%2.2x", *(P+K+M));
					putchar('='); putchar('"');
					for (M=0; M<L; M++) printf("%c", *(P+K+M));
					putchar('"'); putchar('\n');
					I++;
					break;
			}
		}
	}

	/* Nowverify data that might be in the file */
	for(I=0; I<argc; I++) {
		if (argv[I][0]=='-') {
			switch (argv[I][1]) {
				case 'v' :		/* Verify data */
					I++;
					L = strlen(argv[I+1]);
					K = atoi(argv[I]);
					if (!strncmp(P+K, argv[I+1], L)) printf("  Verified %d bytes at offset %d='%s'\n", L, K, argv[I+1]);
					else printf("  Verified FAILED for %d bytes at offset %d<>'%s'\n", L, K, argv[I+1]);
					I++;
					break;
			}
		}
	}

	/* Now change some data in the file. Re-write the file. */
	for(I=0, L=-1; I<argc; I++) {
		if (argv[I][0]=='-') {
			switch (argv[I][1]) {
				case 'c' :		/* Change data */
					if (L==-1) printf("%s: Changing data at\n", MeName); 
					I++;
					L = strlen(argv[I+1]);
					K = atoi(argv[I]);
					strncpy(P+K, argv[I+1], L);
					printf("  at %d for %d bytes to '%s'\n", K, L, argv[I+1]);
					I++;
					break;
			}
		}
	}

	if (L!=-1) {		/* We changed some data, re-write the file */
		fprintf(stderr, "%s: Writing %d bytes to %s ", MeName, Stat.st_size, Fileid);
		lseek(InFile, 0, L_SET);		/* Re-write file */
		if ((write(InFile, P, Stat.st_size)) <= 0) {
			perror("write:");
			return(5);
		}
		printf("complete. \n");
	}
	return(0);
}

/*
FindString (String, Data, DataSize)
 
Function:		Search Data which is DataSize bytes for any occurance of 'String'

Returns:		Number of Strings found
*/
int FindString ( char *String, char *Data, int DataSize)
{
 
 	char	*FS, *EFS;		/* Bean pointers */
	int 	LString;			/* Length of string */
	int	Hit = 0;			/* Says we found something */
	
	EFS = Data + DataSize - 1;		/* End of data to search */
	if ((LString = strlen(String)) <= 0) return;		/* Nothing to look for */
	for (FS=Data; FS < EFS; FS++) {
		if (!strncmp(String, FS, LString)) {
			if (!Hit)  printf("  Found '%s' at offsets:", String);
			Hit ++;
			printf(" %d (X'%4.4x')", FS-Data, FS-Data);
		}
	}
	if (Hit) putchar('\n');		/* Add trailing LF */
	return(Hit);
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.