/* Convert a binary file to a C u_long array */ #include #include #include #define TYPE unsigned char #define STR(x) #x #define XSTR(x) STR(x) int main(int argc, char *argv[]) { const char *name="bfile"; FILE *in,*out; TYPE num; int col; int first; int len; int fldsz; in=stdin; out=stdout; if (argc>1) { if (!(in=fopen(argv[1],"rb"))) { perror(argv[0]); } } if (argc>2) { if (!(out=fopen(argv[2],"w"))) { perror(argv[0]); } } if (argc>3) { name=argv[3]; } if (argc>1) { fprintf(out,"/* Auto-generated binary of %s */\n\n",argv[1]); } else { fprintf(out,"/* Auto-generated binary */\n\n"); } fldsz = sizeof num * 2; fprintf(out,XSTR(TYPE) " %s[]=\n\t{",name); col=0; num=0; first=1; len=0; while(!feof(in)) { fread(&num,sizeof num,1,in); if (col==0) { if (first) { fprintf(out,"\n\t\t"); first=0; } else { fprintf(out,",\n\t\t"); } } else { fprintf(out,", "); } fprintf(out,"0x%.*x",fldsz, num); col=(col+1) % (8 / sizeof num); num=0; len++; } fprintf(out,"\n\t};\n#define %s_LEN %d\n", name, len); fclose(in); fclose(out); return 0; }