summaryrefslogtreecommitdiff
path: root/c2bin.c
blob: ac2a4caab89c044cfe9c6eb3e941eadd5e39cbe1 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void usage(){
	fprintf(stderr,
		"Usage: c2bin [inputfile] [outputfile]\n"
		"\n"
		"Converts a C string to its parsed contents. The inverse of bin2c.\n"
		"Default values for the arguments are stdin and stdout; also '-' indicates the\n"
		"standard stream.\n");
}

int unhex(int c){
	if(c>='0'&&c<='9'){
		return c-'0';
	} else if(c>='a'&&c<='f'){
		return c-'a'+10;
	} else if(c>='A'&&c<='F'){
		return c-'A'+10;
	} else {
		return -1;
	}
}

void c2bin(FILE *input,FILE *output){
	bool stringmode=false;
	int c;
	while((c=fgetc(input))!=EOF){
		if(c=='"'){
			stringmode=!stringmode;
		} else if(!stringmode){
			if(!isspace(c)){
				fprintf(stderr,"Characters outside string\n");
				exit(1);
			}
		} else if(c=='\n'){
			fprintf(stderr,"Invalid newline in string\n");
			exit(1);
		} else if(c!='\\'){
			fputc(c,output);
		} else {
			c=fgetc(input);
			if(c==EOF){
				fprintf(stderr,"Invalid backslash at end of input\n");
				exit(1);
			} else if(c=='\n'){
				continue;
			} else if(c!='x'&&!isdigit(c)){
				switch(c){
					case 'a': fputc('\a',output); break;
					case 'b': fputc('\b',output); break;
					case 'f': fputc('\f',output); break;
					case 'n': fputc('\n',output); break;
					case 'r': fputc('\r',output); break;
					case 't': fputc('\t',output); break;
					case 'v': fputc('\v',output); break;
					default: fputc(c,output); break;
				}
			} else if(c=='x'){
				int c1=fgetc(input);
				int c2=fgetc(input);
				if(c1==-1||c2==-1){
					fprintf(stderr,"Invalid end of input\n");
					exit(1);
				}
				c1=unhex(c1);
				c2=unhex(c2);
				if(c1==-1){
					fprintf(stderr,"Invalid hexadecimal escape\n");
					exit(1);
				}
				if(c2==-1){
					fputc(c1,output);
				} else {
					c=fgetc(input);
					if(c==-1){
						fprintf(stderr,"Invalid end of input\n");
						exit(1);
					}
					if(unhex(c)!=-1){
						fprintf(stderr,"Invalid wide hexadecimal escape\n");
						exit(1);
					}
					ungetc(c,input);

					fputc(16*c1+c2,output);
				}
			} else {
				ungetc(c,input);
				int res=0,i;
				for(i=0;i<3;i++){
					c=fgetc(input);
					if(c==EOF){
						fprintf(stderr,"Invalid end of input\n");
						exit(1);
					}
					if(c<'0'||c>='8'){
						ungetc(c,input);
						break;
					}
					res=8*res+c-'0';
				}
				if(i==0){
					fprintf(stderr,"Invalid octal escape\n");
					exit(1);
				}
				fputc(res,output);
			}
		}
		fflush(output);
	}
}

int main(int argc,char **argv){
	if(argc==1){
		c2bin(stdin,stdout);
	} else if(argc==2){
		if(strcmp(argv[1],"-h")==0||strcmp(argv[1],"--help")==0){
			usage();
		} else if(strcmp(argv[1],"-")==0){
			c2bin(stdin,stdout);
		} else {
			FILE *f=fopen(argv[1],"rb");
			if(!f){
				fprintf(stderr,"Could not open file '%s'\n",argv[1]);
				return 1;
			}
			c2bin(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;
			}
		}

		c2bin(fin,fout);

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