summaryrefslogtreecommitdiff
path: root/bin2c.c
blob: 9cd9f0ef4d45785357087c778705ab41573d2275 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>

#define MAXLINEWIDTH (79)

void usage(){
	fprintf(stderr,
		"Usage: bin2c [inputfile] [outputfile]\n"
		"\n"
		"Converts a binary file to a C string that denotes the same binary contents, but\n"
		"is parseable by a C compiler.\n"
		"Default values for the arguments are stdin and stdout; also '-' indicates the\n"
		"standard stream.\n");
}

// Returns serialised string length
// Keeps state on the previous few characters to aid compression and prevent
// trigraphs
int serialisechar(char *dst,unsigned char c){
	static bool lastwasoctal=false;
	static unsigned char prevc;

	switch(c){
		case '\a': lastwasoctal=false; prevc=c; dst[0]='\\'; dst[1]='a'; return 2;
		case '\b': lastwasoctal=false; prevc=c; dst[0]='\\'; dst[1]='b'; return 2;
		case '\f': lastwasoctal=false; prevc=c; dst[0]='\\'; dst[1]='f'; return 2;
		case '\n': lastwasoctal=false; prevc=c; dst[0]='\\'; dst[1]='n'; return 2;
		case '\r': lastwasoctal=false; prevc=c; dst[0]='\\'; dst[1]='r'; return 2;
		case '\t': lastwasoctal=false; prevc=c; dst[0]='\\'; dst[1]='t'; return 2;
		case '\v': lastwasoctal=false; prevc=c; dst[0]='\\'; dst[1]='v'; return 2;
		case '"':  lastwasoctal=false; prevc=c; dst[0]='\\'; dst[1]='"'; return 2;
		case '\\': lastwasoctal=false; prevc=c; dst[0]='\\'; dst[1]='\\'; return 2;
		default:
			if(c<32||c>=127){
				lastwasoctal=true;
				prevc=c;
				return sprintf(dst,"\\%o",(int)c);
			} else if(isdigit(c)&&lastwasoctal&&prevc<'\100'){
				prevc=c;
				return sprintf(dst,"\\%o",(int)c);
			} else if(c=='?'&&prevc=='?'){
				lastwasoctal=false;
				dst[0]='\\'; dst[1]='?';
				return 2;
			} else {
				lastwasoctal=false;
				dst[0]=c;
				prevc=c;
				return 1;
			}
	}
}

// Returns new xposition
int convertbuffer(FILE *output,const char *buffer,int length,int xposition){
	char cbuf[8];
	for(int i=0;i<length;i++){
		int clen=serialisechar(cbuf,buffer[i]);
		if(xposition+clen>MAXLINEWIDTH){
			fputc('"',output);
			fputc('\n',output);
			fputc('"',output);
			xposition=1;
		}
		fwrite(cbuf,1,clen,output);
		xposition+=clen;
	}
	return xposition;
}

void bin2c(FILE *input,FILE *output){
	fputc('"',output);

	char buffer[1024];
	int xposition=1;

	while(true){
		size_t nread=fread(buffer,1,sizeof(buffer),input);
		if(nread==0){
			break;
		}
		xposition=convertbuffer(output,buffer,nread,xposition);
		if(feof(input)||ferror(input)){
			break;
		}
	}

	fputc('"',output);
	fputc('\n',output);
}

int main(int argc,char **argv){
	if(argc==1){
		bin2c(stdin,stdout);
	} else if(argc==2){
		if(strcmp(argv[1],"-h")==0||strcmp(argv[1],"--help")==0){
			usage();
		} else if(strcmp(argv[1],"-")==0){
			bin2c(stdin,stdout);
		} else {
			FILE *f=fopen(argv[1],"rb");
			if(!f){
				fprintf(stderr,"Could not open file '%s'\n",argv[1]);
				return 1;
			}
			bin2c(f,stdout);
			fclose(f);
		}
	} else if(argc==3){
		FILE *fin,*fout;
		if(strcmp(argv[1],"-")==0){
			fin=stdin;
		} else {
			fin=fopen(argv[1],"rb");
			if(!fin){
				fprintf(stderr,"Could not open file '%s'\n",argv[1]);
				return 1;
			}
		}

		if(strcmp(argv[2],"-")==0){
			fout=stdout;
		} else {
			fout=fopen(argv[2],"wb");
			if(!fout){
				fprintf(stderr,"Could not open file '%s'\n",argv[2]);
				return 1;
			}
		}

		bin2c(fin,fout);

		fclose(fin);
		fclose(fout);
	} else {
		usage();
		return 1;
	}
}