Code:
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h> // getcwd() ;
#include <sys/param.h> // maxplathlen
#define PLACEHOLDER '.'
#define FENCE "*"
#define OUTFILE "/Users/me/dumpdata.txt"
#define HEXCHARS "0123456789ABCDEF"
#define INFILE "/SomeFile"
void print_line(char *, char *, char *, FILE * ) ; // Function prototype
int main (int argc, const char * argv[]) {
unsigned int byte ;
int i, j ;
char infile[MAXPATHLEN+1] ; // Used for building the input file name
char outfile[MAXPATHLEN+1] ;
char outLine[6+2+(8*8)+8+1] ; // a 6-byte offset, 2 blanks, 8*8 data fields with 8 embeded blanks & 1 null term field.
char workBuff[8] ; // Temp work area
char eyeCatcher[70] ; // I need 35 bytes, but I might need escape sequences.
char *outPtr ; // Pointer into the output line
char *eyePtr ; // Pointer into eyecatcher
char *timePtr ;
time_t rightNow ;
struct tm *tm_time ;
FILE *inData, *outData ;
for (i=0 ; i < argc ; ++i) printf("argv[%d] = %s\n", i, argv[i]) ;
// Make sure the input file name buffer is plenty big.
/*
if (strlen(argv[0]) + strlen(argv[1]) > sizeof infile-1) {
printf("Work buffer too small. Exiting") ;
return 3 ;
}
*/
// argv[0] is the full path to the executing directory. Parse it to back up to the last forward slash, then
// append argv[1] to it to make the full path name of the input file.
//strcpy(infile, argv[0]) ;
//strcpy(strrchr(infile,'/')+1,argv[1]) ;
//if (argc < 2) {
// printf("\nPlease supply the input file name to be dumped as the first parm.\n") ;
// printf("Supply the output file name (the 'dump') as the second parm.\n\n") ;
// return 2 ;
//}
strcpy(infile,INFILE) ;
// Print the infile area with the current working directory path.
// getcwd(infile, sizeof infile) ;
// if (strrchr(infile,'/') != &infile[strlen(infile)-1]) strcat(infile,"/") ; // add an ending slash if not one
// strcat(infile, argv[1]) ; // append the passed file name
printf("Built input file name = %s\n", infile) ;
// inData = fopen(argv[1], "rb") ;
inData = fopen(infile, "rb") ;
if (!inData) {
printf("Cannot open input file.\n") ;
return 2 ;
}
// Allow user to set the output file name.
if (argc >= 3) strcpy(outfile,argv[2]) ; // user pass outfile name.
else strcpy(outfile,OUTFILE) ; // assume default outfile name.
printf("Built output file name = %s\n", outfile) ;
outData = fopen(outfile,"w") ;
if (!outData) {
printf("Cannot open output file.\n") ;
return 1 ;
}
// Print out a heading.
time(&rightNow) ;
tm_time = localtime(&rightNow) ;
timePtr = asctime(tm_time) ;
fprintf(outData, "(c) Copyright 2007 Todd Burch. All Rights Reserved.\n") ;
fprintf(outData, "\nDump of %s taken on %s\n" , infile, timePtr ) ;
fprintf(outData, "OFFSET:\n" ) ;
for ( i=0 ; ; ++i ) { // Infinite loop until END OF FILE
// Read a byte. Exit if we hit end of file
byte = fgetc(inData) ;
if (byte == EOF) break ;
if (i==100000) break ;
// print the dump offset if a new line.
if (i%32==0) {
for (j = 0, outPtr = outLine ; j < (sizeof outLine)-1 ; *outPtr++ = ' ', ++j ) ; // reset outLine
*outPtr = (char)NULL ; // null term it
for (j = 0, eyePtr = eyeCatcher ; j < sizeof eyeCatcher ; *eyePtr++ = (char)NULL , ++j ) ; // reset eyecather
eyePtr = eyeCatcher ; // reset pointer again
outPtr = outLine ; // set up the outline pointer again
sprintf(workBuff, "%06X ", i ) ; // Add an "000000" style offset to the front of the line.
memcpy(outPtr, workBuff, 8) ;
outPtr += 8 ; // bump pointer
}
// Convert byte to hex and add to output line.
*outPtr++ = HEXCHARS[ byte / 16 ] ;
*outPtr++ = HEXCHARS[ byte % 16 ] ;
// Set up the eyecatcher.
if (isprint(byte)) {
*eyePtr++ = byte ;
if (byte == '%') *eyePtr++ = byte ; // Double up if this is an escape character
}
else *eyePtr++ = PLACEHOLDER ; // print default non-printable character.
// Print and then reset the eyecatcher if at the end of the line.
if ( (i+1) % 32 == 0) print_line(eyePtr, outLine, eyeCatcher, outData ) ;
// If not at the end of the line, add some visual breaks in the hex data.
else if ( (i+1) % 16 == 0) { // 2 spaces in the middle
*outPtr++ = ' ' ;
*outPtr++ = ' ' ;
}
else if ( (i+1) % 4 == 0) *outPtr++ = ' ' ; // space every word
}
// We hit EOF. Process any remaining data.
if (!( i%32 ) == 0) print_line(eyePtr, outLine, eyeCatcher, outData ) ;
fclose(outData) ;
fclose(inData) ;
return 0 ;
}
void print_line(char *eyePtr, char *outLine, char *eyeCatcher, FILE *outData) {
// Build Eyecatcher
*eyePtr = (char)NULL ; // null term it early
strcat(outLine, " ") ;
strcat(outLine, FENCE ) ;
strcat(outLine, eyeCatcher) ; // append eyecatcher
strcat(outLine, FENCE ) ;
strcat(outLine, "\n"); // complete eyecatcher, line return
fprintf(outData,outLine) ; // write it out
return ;
}