00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <ctype.h>
00027 #include <stdio.h>
00028 #include <string.h>
00029 #include <sys/stat.h>
00030
00031
00032
00033 int main(int argc, char *argv[])
00034 {
00035 if(argc < 2) return 1;
00036
00037 for(argc--; argc > 0; argc--)
00038 {
00039 FILE *in;
00040 FILE *out;
00041 char variable[1024], header_fn[1024], output_fn[1024], *suffix, *prefix;
00042 int i;
00043 int bytes_per_row = 16;
00044 char row[1024], byte[1024], character;
00045 struct stat st;
00046 long total_bytes;
00047
00048 in = fopen(argv[argc], "rb");
00049 if(!in) continue;
00050
00051 stat(argv[argc], &st);
00052 total_bytes = (long)st.st_size;
00053
00054
00055 strcpy(output_fn, argv[argc]);
00056 suffix = strrchr(output_fn, '.');
00057 if(suffix) *suffix = '_';
00058 strcat(output_fn, ".h");
00059
00060 out = fopen(output_fn, "w");
00061 if(!out)
00062 {
00063 fclose(in);
00064 continue;
00065 }
00066
00067
00068
00069 prefix = strrchr(output_fn, '/');
00070 if(!prefix)
00071 prefix = output_fn;
00072 else
00073 prefix++;
00074
00075 strcpy(header_fn, prefix);
00076 for(i = 0; i < strlen(header_fn); i++)
00077 {
00078
00079 if(i == 0 && isdigit(header_fn[i]))
00080 {
00081 int k;
00082 for(k = strlen(header_fn); k >= 0; k--)
00083 {
00084 header_fn[k + 1] = header_fn[k];
00085 }
00086 header_fn[0] = '_';
00087 }
00088
00089
00090 if(header_fn[i] == '.')
00091 header_fn[i] = '_';
00092 else
00093 header_fn[i] = toupper(header_fn[i]);
00094 }
00095
00096
00097 strcpy(variable, prefix);
00098 suffix = strrchr(variable, '.');
00099 if(suffix) *suffix = 0;
00100
00101
00102 if(isdigit(variable[0]))
00103 {
00104 int k;
00105 for(k = strlen(variable); k >= 0; k--)
00106 {
00107 variable[k + 1] = variable[k];
00108 }
00109 variable[0] = '_';
00110 }
00111
00112
00113 fprintf(out, "#ifndef %s\n"
00114 "#define %s\n"
00115 "\n"
00116 "static unsigned char %s[] = \n{\n",
00117 header_fn,
00118 header_fn,
00119 variable);
00120
00121
00122 fprintf(out, "\t0x%02x, 0x%02x, 0x%02x, 0x%02x, \n",
00123 (unsigned long)(total_bytes & 0xff000000) >> 24,
00124 (unsigned long)(total_bytes & 0xff0000) >> 16,
00125 (unsigned long)(total_bytes & 0xff00) >> 8,
00126 (unsigned long)(total_bytes & 0xff));
00127
00128 while(total_bytes > 0)
00129 {
00130 sprintf(row, "\t");
00131 for(i = 0; i < bytes_per_row && total_bytes > 0; i++)
00132 {
00133 if(i == 0)
00134 sprintf(byte, "0x%02x", fgetc(in));
00135 else
00136 sprintf(byte, ", 0x%02x", fgetc(in));
00137 strcat(row, byte);
00138 total_bytes--;
00139 }
00140 if(total_bytes > 0)
00141 sprintf(byte, ", \n");
00142 else
00143 sprintf(byte, "\n");
00144
00145 fprintf(out, "%s%s", row, byte);
00146 }
00147
00148 fprintf(out, "};\n\n#endif\n");
00149 fclose(out);
00150 }
00151 }