00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <sys/types.h>
00021 #include <dirent.h>
00022 #include <stdio.h>
00023 #include <string.h>
00024 #include <stdlib.h>
00025
00026 #ifndef NAME_MAX
00027 #define NAME_MAX 255
00028 #endif
00029
00030 #define OPT_FORCE_HEX 1
00031
00032 int options = 0;
00033
00034 #define ERROR(s...) { printf("error: " s); puts("\n"); exit(1); }
00035
00036 void char2cchar(char *buf, char c)
00037 {
00038 if ((!(options & OPT_FORCE_HEX)) && ((c>=32) && (c<='~'))) {
00039 if (c=='\'') {
00040 strcpy(buf, "'\\''");
00041 } else if (c=='\\') {
00042 strcpy(buf, "'\\\\'");
00043 } else {
00044 sprintf(buf, "'%c'", (unsigned char)c);
00045 }
00046 } else {
00047 sprintf(buf, "0x%02x", (unsigned char)c);
00048 }
00049 }
00050
00051 #define BUFSIZE 1024
00052 #define CHARS_PER_LINE 8
00053
00054 void bin2c_table(int size, FILE *in, FILE *out)
00055 {
00056 char buf[BUFSIZE];
00057 size_t s = sizeof buf;
00058 int i, j=0;
00059 char elem[16];
00060
00061 while ((s = fread(buf, 1, s, in))) {
00062 for (i = 0; i < s; i++) {
00063 int elemlen;
00064 if (i % CHARS_PER_LINE == 0) {
00065 fputs("\t", out);
00066 }
00067 char2cchar(elem, buf[i]);
00068 elemlen = strlen(elem);
00069 fputs(elem, out);
00070 if (j+1 < size) {
00071 int k;
00072 fputs(",", out);
00073 for (k=0; k<4-elemlen; k++) fputs(" ", out);
00074 if ((j+1) % CHARS_PER_LINE == 0) {
00075 fputs("\n", out);
00076 }
00077 } else {
00078 fputs("\n", out);
00079 }
00080 j++;
00081 }
00082 }
00083 }
00084
00085 void bin2c(char *name, int size, FILE *in, FILE *out, char *inname)
00086 {
00087 fprintf(out, "/* generated by bin2c from %s */\n\n", inname);
00088 fprintf(out, "char %s[%d] = {\n", name, size);
00089 bin2c_table(size, in, out);
00090 fputs("};\n", out);
00091 }
00092
00093 void bin2h(char *name, int size, FILE *out, char *outname)
00094 {
00095 char q[NAME_MAX];
00096 int i, l;
00097 strcpy(q, outname);
00098 l=strlen(q);
00099 for (i=0; i<l; i++) {
00100 if ((q[i]>='a') && (q[i]<='z')) {
00101 q[i]+='A'-'a';
00102 }
00103 if (((q[i]<'A') || (q[i]>'Z')) &&
00104 ((q[i]<'0') || (q[i]>'9'))) {
00105 q[i]='_';
00106 }
00107 }
00108 fprintf(out, "#ifndef __%s\n", q);
00109 fprintf(out, "#define __%s\n\n", q);
00110 fputs("extern\n#ifdef __cplusplus\n\"C\"\n#endif\n", out);
00111 fprintf(out, "char %s[%d];\n", name, size);
00112 fprintf(out, "\n#endif /* __%s */\n", q);
00113 }
00114
00115 void syntax(char *name)
00116 {
00117 printf("syntax: %s [-Ndataname] infile c-outfile [h-outfile]\n", name);
00118 exit(1);
00119 }
00120
00121 int main(int argc, char *argv[])
00122 {
00123 int l;
00124 FILE *in=NULL, *out=NULL, *outh=NULL;
00125 char *inname, *outname, *outhname;
00126 char *name="bindata";
00127 int x=1;
00128 char n[NAME_MAX];
00129
00130 if ((argc>1) && (strncmp(argv[1], "-N", 2)==0)) {
00131 name=argv[1]+2;
00132 x++;
00133 }
00134
00135 if (argc>=x+1) {
00136 inname=argv[x];
00137 } else {
00138 syntax(argv[0]);
00139 }
00140 in=fopen(inname, "rb");
00141 if (!in) ERROR("can't open input file: %s", inname);
00142
00143 if (argc>=x+2) {
00144 outname=argv[x+1];
00145 if (argc>=x+3) {
00146 outhname=argv[x+2];
00147 } else {
00148 int k=strlen(outname);
00149 strcpy(n, outname);
00150 if ((k>2) && (strncmp(argv[x+1]+k-2, ".c", 2)==0)) {
00151 strcpy(n+k-2, ".h");
00152 } else {
00153 strcpy(n+k, ".h");
00154 }
00155 outhname=n;
00156 }
00157 outh=fopen(outhname, "wb");
00158 if (!outh) ERROR("can't open .h output file: %s", outhname);
00159 } else {
00160 ERROR("no output file");
00161 }
00162 out=fopen(outname, "wb");
00163 if (!out) ERROR("can't open .c output file: %s", outname);
00164
00165 fseek(in, 0, SEEK_END);
00166 l=ftell(in);
00167 fseek(in, 0, SEEK_SET);
00168
00169 bin2c(name, l, in, out, inname);
00170 bin2h(name, l, outh, outhname);
00171
00172 return 0;
00173 }