summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cfile.c48
-rw-r--r--cfile8.c46
2 files changed, 82 insertions, 12 deletions
diff --git a/cfile.c b/cfile.c
index 91b52e0..b06c1d8 100644
--- a/cfile.c
+++ b/cfile.c
@@ -1,55 +1,91 @@
/*
Convert a binary file to a C u_long array
*/
+#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
int main(int argc, char *argv[])
{
- char *name="bfile";
+ const char *name="bfile";
FILE *in,*out;
unsigned long num;
int col;
+ int first;
+ int len;
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");
+ }
fprintf(out,"unsigned long %s[]=\n\t\t\t{",name);
col=0;
num=0;
+ first=1;
+ len=0;
while(!feof(in))
- {
+ {
fread(&num,sizeof(unsigned long),1,in);
if (col==0)
- fprintf(out,"\n\t\t\t");
+ {
+ if (first)
+ {
+ fprintf(out,"\n\t\t\t");
+ first=0;
+ }
+ else
+ {
+ fprintf(out,",\n\t\t\t");
+ }
+ }
+ else
+ {
+ fprintf(out,",");
+ }
- fprintf(out,"0x%.8x,",num);
+ fprintf(out,"0x%.8x",num);
col=(col+1)%4;
num=0;
- }
+ len++;
+ }
- fprintf(out,"\n\t\t\t};\n");
+ fprintf(out,"\n\t\t\t};\n#define %s_LEN %d\n", name, len);
fclose(in);
fclose(out);
+
+ return 0;
}
diff --git a/cfile8.c b/cfile8.c
index 702da6f..647e1d5 100644
--- a/cfile8.c
+++ b/cfile8.c
@@ -1,54 +1,88 @@
/*
Convert a binary file to a C unsigned char array
*/
+#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
int main(int argc, char *argv[])
{
- char *name="bfile";
+ const char *name="bfile";
FILE *in,*out;
unsigned char num;
int col;
+ int first;
+ int len;
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");
+ }
- fprintf(out,"unsigned long %s[]=\n{",name);
+ fprintf(out,"unsigned char %s[]=\n{",name);
col=0;
num=0;
+ first=1;
+ len=0;
while(!feof(in))
{
fread(&num,sizeof num,1,in);
if (col==0)
- fprintf(out,"\n");
+ {
+ if (first)
+ {
+ fprintf(out,"\n\t");
+ first = 0;
+ }
+ else
+ {
+ fprintf(out,",\n\t");
+ }
+ }
+ else
+ {
+ fprintf(out,",");
+ }
- fprintf(out,"0x%.2x,",num);
+ fprintf(out,"0x%.2x",(unsigned)num);
- col=(col+1)%16;
+ col=(col+1)%8;
num=0;
+ len++;
}
- fprintf(out,"\n};\n");
+ fprintf(out,"\n\t};\n#define %s_LEN %d\n", name, len);
fclose(in);
fclose(out);