/* * Dumper, by Dan Gookin * Displays the contents of a file in * hexadecimal and ASCII */ #include #include #include #include #include void main(int argc,char *argv[]) { FILE *dump_me; //File handle storage int i,t,x,b; char c; char buffer[16]; /* Check to see if a filename was typed */ if(argc<2) { puts("Format: DUMPER filename"); exit(0); } /* Open the file and make sure it's okay */ dump_me = fopen(argv[1],"rb"); if(dump_me==NULL) { printf("Error opening %s\n",argv[1]); exit(1); } /* * The file is open. It's displayed in upper case using * the STRUPR function. * Read each byte in the file and display it * using the format: * * OFFSET - 16 HEX bytes - 16 ASCII chars * * Each row contains 16 hex bytes and 16 ASCII * Variable X is the file length counter * Variable B counts to 16 for each row */ x = b = 0; printf("%s:\n",strupr(argv[1])); printf("%0.4X: ",x); //starting offset = 0 /* * The while loop continues until the EOF is encountered. * The character read is stored in integer variable I */ while((i=fgetc(dump_me))!= EOF) { printf("%0.2X ",i); //show char as 2-digit HEX x++; //increment file size count buffer[b] = i; //store char in buffer b++; //increment buffer count /* * X MOD 16 returns ZERO every time X is divisible by 16 * So you can use if(!(x%16)) to determine whenever 16 * characters have been read from the file */ if(!(x%16)) { printf(" "); //two spaces /* * The following displays the ASCII values of the characters * read and stored in the BUFFER. The ISCNTRL function * determines if the character is a control character. If so, * a dot is displayed. Otherwise, the character is displayed. */ for(t=0;t<16;t++) { if(iscntrl(buffer[t])) putchar('.'); else putchar(buffer[t]); } /* * After the ASCII display, a new line is shown, starting * with the offset. Then B is re-initialized back to zero */ printf("\n%0.4X: ",x); b = 0; } /* 384 is 24 rows of 16 items--a screen. So after each screen * A "Press Enter" message is dispalyed. The same MOD logic * is used in this IF comparision. * The \R in the PRINTF statement allows the "Press Enter" * message to overwrite the initial offset value that would * otherwise appear there. * The KBHIT function sits and waits for any character to be * pressed. However, if the ESC key (code '\x1b') is pressed * then the program quits. Otherwise, the offset value is * re-printed and the program continues with the next 16 * characters. */ if(!(x%384)) { printf("\rPress Enter:"); while(!kbhit()) ; c = getch(); if(c=='\x1b') exit(0); printf("\n%0.4X: ",x); } } fclose(dump_me); //ALWAYS CLOSE YOUR FILES! /* * Display summary information * STRUPR converts the filename to ALL CAPS */ printf("\n%s: size = %u bytes\n",strupr(argv[1]),x); }