/* The Nutcracker Program (with apologies to Clara) This program is designed to convert sizable files containing a mixture of sections of ASCII characters and sections of something else into readable form. It converts all nonprintable bytes into mnemonic or hexadecimal form. The user can then use the search/replace functions of a word processor to convert the files into the desired final form. Unlike the standard "strings.c" program, nutcracker is not primarily geared to finding text within machine-language programs, but to converting data files which are in proprietary formats. It incorporates non-printable bytes in expanded form in their proper place within the output, on the assumption that the user is going to want to convert these bytes into something else-- perhaps an aliased character, or perhaps a page markup tag. The assumption is that you will be using it on a file which is about ninety-five percent printable ASCII characters, but with a remaining five percent which causes conventional file conversion or file reader programs to crash. This program is a blunt instrument, and somewhat laborious to use, but its great virtue is that, provided the characters are in ASCII, and have not been compressed, the program WILL NEVER FAIL. I wrote it originally when I was trying to get an old-fashioned word processor, Wordstar 5, to work with a new printer, and needed to do a bit of reverse engineering. I have used Nutcracker to read Word and WordPerfect files, and practically, I should think the only significant limitation is that it won't handle files which have been compressed (eg. Acrobat). Andrew D. Todd 1249 Pineview Dr., Apt 4 Morgantown, WV 26505 U46A8@WVNVM.WVNET.EDU Aug 9, 1998 --------------------------------------------------------------- usage: nutcrack src dst where src, dst are filespecs ---------------------------------------------------------------- environment: MS-DOS, using MIX Power C compiler (superset of Microsoft C, Borland Turbo C), however, should be extremely portable. ----------------------------------------------------------------- This program is in the public domain, and may be used for any purpose whatsover, without permission or notification, but it is offered "as-is" without any warrentee, express or implied. ----------------------------------------------------------------- */ #include #include #include #include #include main(int argc, char *argv[]) { int c_row, status, f_in, f_out; char c, c1, c2, c1c, c2c; char *hex_rep = "[xx]"; char e_string[80]; if( argc < 3 ) { printf(" \nBoth Source and Destination Files Needed\n"); _exit(3); } f_in = open(argv[1], O_RDONLY|O_BINARY); if(f_in == -1) { printf(" \n%s Does Not Exist\n", argv[1]); _exit(4); } f_out = open(argv[2], O_WRONLY|O_CREAT|O_EXCL|O_BINARY, S_IREAD|S_IWRITE); if( f_out == -1) { strcpy(&e_string, " \nDestination File "); strcat(&e_string, argv[2]); strcat(&e_string, " Invalid\n"); perror(&e_string); _exit(5); } c_row=0; for(;;) /* Do for the whole file */ { status = read(f_in, &c, 1); if(status != 1) break; /*PRINTABLE*/ else if( isprint(c)) { write(f_out, &c, 1); c_row++; } /*CARRIAGE RETURN*/ else if( c == '\r') { write(f_out, "|\r\n[CR]|\r\n", 10); c_row=0; } /*LINE FEED*/ else if( c == '\n') { write(f_out, "[LF]|\r\n", 7); c_row=0; } /*BELL*/ else if( c == '\a') { write(f_out, "[BEL]", 5); c_row+=5; } /*BACKSPACE*/ else if( c == '\b') { write(f_out, "[BS]", 4); c_row+=4; } /*FORMFEED*/ else if( c == '\f') { write(f_out, "|\r\n[FF]|\r\n", 10); c_row=0; } /*TAB*/ else if( c == '\t') { write(f_out, "[TAB]", 5); c_row+=5; } /*ESCAPE*/ else if( c == '\x1b') { write(f_out, "|\r\n[ESC]", 8); c_row=0; } else { c2=c%16; c1=(c-c2)/16; if(c1 <= 9) c1c = c1 + '0'; else c1c = c1 + 'A'-10; if(c2 <= 9) c2c = c2 + '0'; else c2c = c2 + 'A'-10; hex_rep[1] = c1c; hex_rep[2] =c2c; write(f_out, hex_rep, 4); c_row+=4; } /*always*/ if(c_row > 65) { write(f_out, "|\r\n", 3); c_row=0; } } close(f_in); close(f_out); _exit(0); }