aboutsummaryrefslogtreecommitdiff
path: root/graftest/blob2c.c
blob: 779b79f6953c1179fa24610c7988aa72df07ba0b (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>

/* since we're emitting C source, the comments containing the
	ASCII dump better not contain C comment markers! */
void fixasc(char *asc, int len) {
	int j;

	for(j=0; j<(len-1); j++) {
		/* slash-star or slash-slash become slash-dot */
		if(asc[j] == '/' && (asc[j+1] == '*' || asc[j+1] == '/'))
			asc[j+1] = '.';
		/* star-slash becomes dot-slash */
		else if(asc[j] == '*' && asc[j+1] == '/')
			asc[j] = '.';
	}
}

int main(int argc, char **argv) {
	int i, j, count = 0;
	char *p, asc[9];
	FILE *in;

	if(argc != 2) {
		fprintf(stderr,
				"#error usage: %s blobfile >blobfile.c 2>blobfile.h\n", argv[0]);
		exit(1);
	}

	if( !(in = fopen(argv[1], "rb")) ) {
		fprintf(stderr, "/* %s: %s */\n", argv[1], strerror(errno));
		exit(1);
	}

	printf("/* C source created by blob2c from input file %s */\n\n", argv[1]);
	fprintf(stderr,
			"/* C header created by blob2c from input file %s */\n\n", argv[1]);

	for(p = argv[1]; *p; p++)
		if(!isalnum(*p)) *p = '_';

	fprintf(stderr,
			"#ifndef %s_H\n"
			"#define %s_H\n\n",
			argv[1], argv[1]);

	/* start array definition */
	printf("unsigned char %s[] = {", argv[1]);

	asc[8] = '\0';

	/* read/process each input byte in loop, until EOF */
	while( (i = getc(in)) != EOF ) {
		if(count) putchar(',');

		if(count % 8 == 0) {
			if(count) {
				/* fix & print ASCII dump */
				fixasc(asc, 8);
				printf(" /* %s */", asc);
			}

			/* start next line */
			printf("\n\t/* %6d */  ", count);
		}

		/* store this byte of ASCII dump */
		asc[count % 8] = isprint(i) ? i : '.';

		/* print this byte of hex data */
		printf("0x%02x", i);

		count++;
	}

	/* fix ASCII dump for last line */
	i = count % 8;
	if(!i) i = 8;
	fixasc(asc, i);
	asc[i] = '\0';

	/* line it up with the previous lines */
	j = (8 - i) * 5;
	for(i=0; i<j; i++)
		putchar(' ');

	/* print it */
	printf("  /* %s */", asc);

	/* end of array definition */
	printf("\n}; /* %s */\n\n", argv[1]);

	/* array_len definition */
	printf("int %s_len = %d;\n", argv[1], count);

	/* check for read errors */
	i = 0;
	if(ferror(in)) {
		fprintf(stderr, "#error %s: %s\n", argv[1], strerror(errno));
		i = 1;
	}

	fclose(in);

	/* extern declarations to stderr */
	fprintf(stderr,
			"extern unsigned char %s[];\nextern int %s_len;\n",
			argv[1], argv[1]);

	fprintf(stderr, "\n#endif  /* %s_H */\n", argv[1]);

	return i;
}