summaryrefslogtreecommitdiff
path: root/cfile8.c
blob: c41878a495a6cbe1ea6b4f8d2d8570368f6caf5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
	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[])
{
    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[1]);
	}
    }

    if (argc>2)
    {
    	if (!(out=fopen(argv[2],"w")))
	{
	    perror(argv[2]);
	}
    }

    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 char %s[]=\n{",name);

    col=0;
    num=0;
    first=1;
    len=0;

    fread(&num,sizeof num,1,in);

    while(!feof(in))
    {
	if (col==0)
	{
	    if (first)
	    {
		fprintf(out,"\n\t");
		first = 0;
	    }
	    else
	    {
		fprintf(out,",\n\t");
	    }
	}
	else
	{
	    fprintf(out,",");
	}

	fprintf(out,"0x%.2x",(unsigned)num);

	col=(col+1)%8;
	num=0;
	len++;

	fread(&num,sizeof num,1,in);
    }

    fprintf(out,"\n\t};\n#define %s_LEN %d\n", name, len);

    fclose(in);
    fclose(out);
}