bin2c.cpp
1 // BIN2C.CPP 2 // by Jonathon Fowler (jf@jonof.id.au) 3 4 // Converts a binary file to C source 5 // This is a DOS program originally written with Borland Turbo C++ for DOS 3.1 6 7 8 #include <string.h> 9 #include <ctype.h> 10 #include <stdio.h> 11 #include <dir.h> 12 #include <io.h> 13 #include <stdlib.h> 14 15 16 char defsrcext[] = ".DAT"; 17 char defoutext[] = ".C"; 18 19 char source[MAXPATH], output[MAXPATH], bytesize; 20 21 22 int PathAddExt(char *path, char *ext); 23 24 25 int main(int argc, char *argv[]) 26 { 27 printf("BIN2C - Binary to C data converter\n" 28 "Copyright (c) 1999 Jonathon Fowler\n\n"); 29 30 if (argc < 4) 31 { 32 printf("Usage:\n" 33 " BIN2C source<.DAT> output<.C> b|w\n\n" 34 " source<.DAT> Binary source file\n" 35 " output<.C> Output C code file\n" 36 " b|w Byte or word-sized data\n\n"); 37 exit(0); 38 } 39 40 int arg; 41 FILE *in, *out; 42 char datab1, datab2; 43 int across=0, maxacross; 44 int length, written=0; 45 46 47 // get the source file 48 strcpy(source, argv[1]); 49 strupr(source); 50 PathAddExt(source, defsrcext); 51 printf("� Source file: %s\n", source); 52 53 // get the output file 54 strcpy(output, argv[2]); 55 strupr(output); 56 PathAddExt(output, defoutext); 57 printf("� Output file: %s\n", output); 58 59 // get byte/word data 60 switch (tolower(argv[3][0])) 61 { 62 case 'b': 63 printf("� Byte data.\n"); 64 bytesize=1; 65 break; 66 67 case 'w': 68 printf("� Word data.\n"); 69 bytesize=0; 70 break; 71 72 default: 73 printf("� Unknown data size specified. Defaulting to byte.\n"); 74 bytesize=1; 75 break; 76 } 77 78 // open the input file 79 in = fopen(source, "rb"); 80 if (!in) 81 { 82 printf("Error opening %s\n", source); 83 exit(1); 84 } 85 86 // open the output file 87 out = fopen(output, "w+t"); 88 if (!out) 89 { 90 printf("Error creating %s\n", output); 91 exit(1); 92 } 93 94 length = filelength(fileno(in)); 95 96 // write a header out to the output file 97 fprintf(out, "// %s\n\n// Generated by BIN2C.EXE\n// By Jonathon Fowler\n\n", output); 98 99 // start a data block 100 fprintf(out, "%s datablock[] = {\n // %ld bytes", (bytesize) ? "char" : "unsigned", length); 101 102 if (bytesize) 103 maxacross = 12; 104 else 105 maxacross = 9; 106 across = maxacross; 107 108 // convert the data 109 for (written=0; written<length; written++) { 110 if (across == maxacross) 111 { 112 fprintf(out, "\n "); 113 across = 0; 114 } 115 116 if (bytesize) 117 { 118 datab1 = fgetc(in); 119 fprintf(out, " 0x%02X%c", datab1, ((length-written)>1) ? ',' : '\n'); 120 } else { 121 datab1 = fgetc(in); 122 datab2 = fgetc(in); 123 fprintf(out, " 0x%02X%02X%c", datab2, datab1, ((length-written)>2) ? ',' : '\n'); 124 } 125 126 across++; 127 128 if (!bytesize) written++; 129 } 130 131 132 fprintf(out, " };"); 133 134 fclose(out); 135 fclose(in); 136 137 return 0; 138 } 139 140 141 // Add an extention to a path if one doesn't exist 142 int PathAddExt(char *path, char *ext) 143 { 144 char drive[MAXDRIVE], dir[MAXDIR], name[MAXFILE], extn[MAXEXT]; 145 int flags; 146 147 flags = fnsplit(path, drive, dir, name, extn); 148 149 if (!(flags & EXTENSION)) // tack on an extension 150 strcat(path, ext); 151 152 return ((flags & EXTENSION) == 0); 153 }