comparison ffstools/newcomp/tiffs-mkfile.c @ 746:f19c347d0a80

tiffs-mkfile utility written
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 19 Oct 2020 17:39:19 +0000
parents
children
comparison
equal deleted inserted replaced
745:9e3b1ef1f440 746:f19c347d0a80
1 /*
2 * This program generates certain types of binary files that go into TIFFS.
3 * It is intended for use in shell scripts that prepare input trees for
4 * tiffs-mkfs.
5 */
6
7 #include <sys/types.h>
8 #include <sys/file.h>
9 #include <ctype.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <strings.h>
14 #include <unistd.h>
15
16 #define MAX_FILE_SIZE 256
17
18 u_char databuf[MAX_FILE_SIZE];
19 unsigned datalen;
20
21 void
22 write_binary_file(filename)
23 char *filename;
24 {
25 int fd, cc;
26
27 fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0666);
28 if (fd < 0) {
29 perror(filename);
30 exit(1);
31 }
32 cc = write(fd, databuf, datalen);
33 if (cc != datalen) {
34 perror("error writing to file");
35 exit(1);
36 }
37 close(fd);
38 }
39
40 void
41 do_filetype_ascii(str)
42 char *str;
43 {
44 unsigned len;
45
46 len = strlen(str);
47 if (len > MAX_FILE_SIZE) {
48 fprintf(stderr,
49 "error: ASCII string exceeds file size limit\n");
50 exit(1);
51 }
52 bcopy(str, databuf, len);
53 datalen = len;
54 }
55
56 hexdigit(c)
57 {
58 if (isdigit(c))
59 return(c - '0');
60 else if (isupper(c))
61 return(c - 'A' + 10);
62 else
63 return(c - 'a' + 10);
64 }
65
66 void
67 do_filetype_hex(hexstr)
68 char *hexstr;
69 {
70 char *cp;
71 unsigned len;
72
73 for (cp = hexstr, len = 0; ; cp += 2) {
74 while (isspace(*cp))
75 cp++;
76 if (!*cp)
77 break;
78 if (!isxdigit(cp[0]) || !isxdigit(cp[1])) {
79 fprintf(stderr, "error: invalid hex string argument\n");
80 exit(1);
81 }
82 if (len >= MAX_FILE_SIZE) {
83 fprintf(stderr,
84 "error: hex string exceeds file size limit\n");
85 exit(1);
86 }
87 databuf[len++] = hexdigit(cp[0]) << 4 | hexdigit(cp[1]);
88 }
89 datalen = len;
90 }
91
92 void
93 parse_imeisv_arg(input, buf)
94 char *input, *buf;
95 {
96 char *cp;
97 int i;
98
99 cp = input;
100 if (!isdigit(*cp)) {
101 inv: fprintf(stderr,
102 "error: IMEISV argument must have 16 decimal digits\n");
103 exit(1);
104 }
105 for (i = 0; i < 16; i++) {
106 if (ispunct(*cp))
107 cp++;
108 if (!isdigit(*cp))
109 goto inv;
110 buf[i] = *cp++ - '0';
111 }
112 if (*cp)
113 goto inv;
114 }
115
116 void
117 do_filetype_imeisv(strarg)
118 char *strarg;
119 {
120 char digits[16];
121 int i;
122
123 parse_imeisv_arg(strarg, digits);
124 for (i = 0; i < 8; i++)
125 databuf[i] = digits[i*2] << 4 | digits[i*2+1];
126 datalen = 8;
127 }
128
129 void
130 do_filetype_pcm_imei(strarg)
131 char *strarg;
132 {
133 char digits[16];
134 int i;
135
136 parse_imeisv_arg(strarg, digits);
137 for (i = 0; i < 8; i++)
138 databuf[i] = digits[i*2+1] << 4 | digits[i*2];
139 datalen = 8;
140 }
141
142 static struct band_table {
143 char *keyword;
144 u_char bytes[4];
145 } band_table[] = {
146 {"dual-eu", {0x00, 0x0B, 0x41, 0x00}},
147 {"dual-us", {0x00, 0x14, 0x00, 0x14}},
148 {"tri900", {0x00, 0x0F, 0x41, 0x10}},
149 {"tri850", {0x00, 0x16, 0x01, 0x14}},
150 {"quad", {0x00, 0x1F, 0x41, 0x14}},
151 {0, {0x00, 0x00, 0x00, 0x00}}
152 };
153
154 static u_char rfcap_tail[12] = {0x00, 0x00, 0x00, 0x00,
155 0x50, 0x00, 0x00, 0xA5,
156 0x05, 0x00, 0xC0, 0x00};
157
158 void
159 do_filetype_rfcap(band_config_kw)
160 char *band_config_kw;
161 {
162 struct band_table *tp;
163
164 for (tp = band_table; tp->keyword; tp++)
165 if (!strcmp(tp->keyword, band_config_kw))
166 break;
167 if (!tp->keyword) {
168 fprintf(stderr, "error: band configuration \"%s\" not known\n",
169 band_config_kw);
170 exit(1);
171 }
172 bcopy(tp->bytes, databuf, 4);
173 bcopy(rfcap_tail, databuf + 4, 12);
174 datalen = 16;
175 }
176
177 static struct file_type {
178 char *keyword;
179 void (*func)();
180 } file_type_table[] = {
181 {"ascii", do_filetype_ascii},
182 {"hex", do_filetype_hex},
183 {"imeisv", do_filetype_imeisv},
184 {"pcm-imei", do_filetype_pcm_imei},
185 {"rfcap", do_filetype_rfcap},
186 /* table search terminator */
187 {0, 0}
188 };
189
190 main(argc, argv)
191 char **argv;
192 {
193 struct file_type *tp;
194
195 if (argc != 4) {
196 fprintf(stderr, "usage: %s dest-file file-type content-str\n",
197 argv[0]);
198 exit(1);
199 }
200 for (tp = file_type_table; tp->keyword; tp++)
201 if (!strcmp(tp->keyword, argv[2]))
202 break;
203 if (!tp->func) {
204 fprintf(stderr, "error: file type \"%s\" not supported\n",
205 argv[2]);
206 exit(1);
207 }
208 tp->func(argv[3]);
209 write_binary_file(argv[1]);
210 exit(0);
211 }