1 /*
2 * Binary encode X templates from text format.
3 *
4 * Copyright 2011 Dylan Smith
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <signal.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #ifdef HAVE_GETOPT_H
28 # include <getopt.h>
29 #endif
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
33
34 #include "windef.h"
35 #include "guiddef.h"
36
37 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(array[0]))
38
39 #define TOKEN_NAME 1
40 #define TOKEN_STRING 2
41 #define TOKEN_INTEGER 3
42 #define TOKEN_GUID 5
43 #define TOKEN_INTEGER_LIST 6
44 #define TOKEN_FLOAT_LIST 7
45 #define TOKEN_OBRACE 10
46 #define TOKEN_CBRACE 11
47 #define TOKEN_OPAREN 12
48 #define TOKEN_CPAREN 13
49 #define TOKEN_OBRACKET 14
50 #define TOKEN_CBRACKET 15
51 #define TOKEN_OANGLE 16
52 #define TOKEN_CANGLE 17
53 #define TOKEN_DOT 18
54 #define TOKEN_COMMA 19
55 #define TOKEN_SEMICOLON 20
56 #define TOKEN_TEMPLATE 31
57 #define TOKEN_WORD 40
58 #define TOKEN_DWORD 41
59 #define TOKEN_FLOAT 42
60 #define TOKEN_DOUBLE 43
61 #define TOKEN_CHAR 44
62 #define TOKEN_UCHAR 45
63 #define TOKEN_SWORD 46
64 #define TOKEN_SDWORD 47
65 #define TOKEN_VOID 48
66 #define TOKEN_LPSTR 49
67 #define TOKEN_UNICODE 50
68 #define TOKEN_CSTRING 51
69 #define TOKEN_ARRAY 52
70
71 struct parser
72 {
73 FILE *infile;
74 FILE *outfile;
75 int line_no;
76 UINT bytes_output;
77 BOOL(*write_bytes)(struct parser *parser, const BYTE *data, DWORD size);
78 BOOL error;
79 };
80
81 struct keyword
82 {
83 const char *word;
84 WORD token;
85 };
86
87 static const struct keyword reserved_words[] = {
88 {"ARRAY", TOKEN_ARRAY},
89 {"CHAR", TOKEN_CHAR},
90 {"CSTRING", TOKEN_CSTRING},
91 {"DOUBLE", TOKEN_DOUBLE},
92 {"DWORD", TOKEN_DWORD},
93 {"FLOAT", TOKEN_FLOAT},
94 {"SDWORD", TOKEN_SDWORD},
95 {"STRING", TOKEN_LPSTR},
96 {"SWORD", TOKEN_SWORD},
97 {"TEMPLATE", TOKEN_TEMPLATE},
98 {"UCHAR", TOKEN_UCHAR},
99 {"UNICODE", TOKEN_UNICODE},
100 {"VOID", TOKEN_VOID},
101 {"WORD", TOKEN_WORD}
102 };
103
104 static char *program_name;
105
106 static inline BOOL read_byte(struct parser *parser, char *byte)
107 {
108 int c = fgetc(parser->infile);
109 *byte = c;
110 return c != EOF;
111 }
112
113 static inline BOOL unread_byte(struct parser *parser, char last_byte)
114 {
115 return ungetc(last_byte, parser->infile) != EOF;
116 }
117
118 static inline BOOL read_bytes(struct parser *parser, void *data, DWORD size)
119 {
120 return fread(data, size, 1, parser->infile) > 0;
121 }
122
123 static BOOL write_c_hex_bytes(struct parser *parser, const BYTE *data, DWORD size)
124 {
125 while (size--)
126 {
127 if (parser->bytes_output % 12 == 0)
128 fprintf(parser->outfile, "\n ");
129 fprintf(parser->outfile, " 0x%02x,", *data++);
130 parser->bytes_output++;
131 }
132 return TRUE;
133 }
134
135 static BOOL write_raw_bytes(struct parser *parser, const BYTE *data, DWORD size)
136 {
137 return fwrite(data, size, 1, parser->outfile) > 0;
138 }
139
140 static inline BOOL write_bytes(struct parser *parser, const void *data, DWORD size)
141 {
142 return parser->write_bytes(parser, data, size);
143 }
144
145 static inline BOOL write_byte(struct parser *parser, BYTE value)
146 {
147 return write_bytes(parser, &value, sizeof(value));
148 }
149
150 static inline BOOL write_word(struct parser *parser, WORD value)
151 {
152 return write_bytes(parser, &value, sizeof(value));
153 }
154
155 static inline BOOL write_dword(struct parser *parser, DWORD value)
156 {
157 return write_bytes(parser, &value, sizeof(value));
158 }
159
160 static int compare_names(const void *a, const void *b)
161 {
162 return strcasecmp(*(const char **)a, *(const char **)b);
163 }
164
165 static BOOL parse_keyword(struct parser *parser, const char *name)
166 {
167 const struct keyword *keyword;
168
169 keyword = bsearch(&name, reserved_words, ARRAY_SIZE(reserved_words),
170 sizeof(reserved_words[0]), compare_names);
171 if (!keyword)
172 return FALSE;
173
174 return write_word(parser, keyword->token);
175 }
176
177 static BOOL parse_guid(struct parser *parser)
178 {
179 char buf[39];
180 GUID guid;
181 DWORD tab[10];
182 BOOL ret;
183 static const char *guidfmt = "<%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X>";
184
185 buf[0] = '<';
186 if (!read_bytes(parser, buf + 1, 37)) {
187 fprintf(stderr, "%s: Truncated GUID (line %d)\n",
188 program_name, parser->line_no);
189 parser->error = TRUE;
190 return FALSE;
191 }
192 buf[38] = 0;
193
194 ret = sscanf(buf, guidfmt, &guid.Data1, tab, tab+1, tab+2, tab+3, tab+4, tab+5, tab+6, tab+7, tab+8, tab+9);
195 if (ret != 11) {
196 fprintf(stderr, "%s: Invalid GUID '%s' (line %d)\n",
197 program_name, buf, parser->line_no);
198 parser->error = TRUE;
199 return FALSE;
200 }
201
202 guid.Data2 = tab[0];
203 guid.Data3 = tab[1];
204 guid.Data4[0] = tab[2];
205 guid.Data4[1] = tab[3];
206 guid.Data4[2] = tab[4];
207 guid.Data4[3] = tab[5];
208 guid.Data4[4] = tab[6];
209 guid.Data4[5] = tab[7];
210 guid.Data4[6] = tab[8];
211 guid.Data4[7] = tab[9];
212
213 return write_word(parser, TOKEN_GUID) &&
214 write_bytes(parser, &guid, sizeof(guid));
215 }
216
217 static BOOL parse_name(struct parser *parser)
218 {
219 char c;
220 int len = 0;
221 char name[512];
222
223 while (read_byte(parser, &c) && len < sizeof(name) &&
224 (isalnum(c) || c == '_' || c == '-'))
225 {
226 if (len + 1 < sizeof(name))
227 name[len++] = c;
228 }
229 unread_byte(parser, c);
230 name[len] = 0;
231
232 if (parse_keyword(parser, name)) {
233 return TRUE;
234 } else {
235 return write_word(parser, TOKEN_NAME) &&
236 write_dword(parser, len) &&
237 write_bytes(parser, name, len);
238 }
239 }
240
241 static BOOL parse_number(struct parser *parser)
242 {
243 int len = 0;
244 char c;
245 char buffer[512];
246 BOOL dot = FALSE;
247 BOOL ret;
248
249 while (read_byte(parser, &c) &&
250 ((!len && c == '-') || (!dot && c == '.') || isdigit(c)))
251 {
252 if (len + 1 < sizeof(buffer))
253 buffer[len++] = c;
254 if (c == '.')
255 dot = TRUE;
256 }
257 unread_byte(parser, c);
258 buffer[len] = 0;
259
260 if (dot) {
261 float value;
262 ret = sscanf(buffer, "%f", &value);
263 if (!ret) {
264 fprintf(stderr, "%s: Invalid float token (line %d).\n",
265 program_name, parser->line_no);
266 parser->error = TRUE;
267 } else {
268 ret = write_word(parser, TOKEN_FLOAT) &&
269 write_bytes(parser, &value, sizeof(value));
270 }
271 } else {
272 int value;
273 ret = sscanf(buffer, "%d", &value);
274 if (!ret) {
275 fprintf(stderr, "%s: Invalid integer token (line %d).\n",
276 program_name, parser->line_no);
277 parser->error = TRUE;
278 } else {
279 ret = write_word(parser, TOKEN_INTEGER) &&
280 write_dword(parser, value);
281 }
282 }
283
284 return ret;
285 }
286
287 static BOOL parse_token(struct parser *parser)
288 {
289 char c;
290
291 if (!read_byte(parser, &c))
292 return FALSE;
293
294 switch (c)
295 {
296 case '\n':
297 parser->line_no++;
298 /* fall through */
299 case '\r':
300 case ' ':
301 case '\t':
302 return TRUE;
303
304 case '{': return write_word(parser, TOKEN_OBRACE);
305 case '}': return write_word(parser, TOKEN_CBRACE);
306 case '[': return write_word(parser, TOKEN_OBRACKET);
307 case ']': return write_word(parser, TOKEN_CBRACKET);
308 case '(': return write_word(parser, TOKEN_OPAREN);
309 case ')': return write_word(parser, TOKEN_CPAREN);
310 case ',': return write_word(parser, TOKEN_COMMA);
311 case ';': return write_word(parser, TOKEN_SEMICOLON);
312 case '.': return write_word(parser, TOKEN_DOT);
313
314 case '/':
315 if (!read_byte(parser, &c) || c != '/') {
316 fprintf(stderr, "%s: Invalid single '/' comment token (line %d).\n",
317 program_name, parser->line_no);
318 parser->error = TRUE;
319 return FALSE;
320 }
321 /* fall through */
322 case '#':
323 while (read_byte(parser, &c) && c != '\n');
324 return c == '\n';
325
326 case '<':
327 return parse_guid(parser);
328
329 case '"':
330 {
331 int len = 0;
332 char buffer[512];
333
334 /* FIXME: Handle '\' (e.g. "valid\"string") */
335 while (read_byte(parser, &c) && c != '"') {
336 if (len + 1 < sizeof(buffer))
337 buffer[len++] = c;
338 }
339 if (c == EOF) {
340 fprintf(stderr, "%s: Unterminated string (line %d).\n",
341 program_name, parser->line_no);
342 parser->error = TRUE;
343 return FALSE;
344 }
345 return write_word(parser, TOKEN_STRING) &&
346 write_dword(parser, len) &&
347 write_bytes(parser, buffer, len);
348 }
349
350 default:
351 unread_byte(parser, c);
352 if (isdigit(c) || c == '-')
353 return parse_number(parser);
354 if (isalpha(c) || c == '_')
355 return parse_name(parser);
356 fprintf(stderr, "%s: Invalid character (%d) to start token (line %d).\n",
357 program_name, c, parser->line_no);
358 parser->error = TRUE;
359 return FALSE;
360 }
361
362 return TRUE;
363 }
364
365 static const char *output_file;
366
367 static void cleanup_files(void)
368 {
369 if (output_file) unlink(output_file);
370 }
371
372 static void exit_on_signal( int sig )
373 {
374 exit(1); /* this will call the atexit functions */
375 }
376
377 static void usage(void)
378 {
379 fprintf(stderr, "Usage: %s [OPTIONS] INFILE\n"
380 "Options:\n"
381 " -i NAME Output to a c header file, data in variable NAME\n"
382 " -s NAME In a c header file, define NAME to be the data size\n"
383 " -o FILE Write output to FILE\n",
384 program_name);
385 }
386
387 static char *option_inc_var_name = NULL;
388 static char *option_inc_size_name = NULL;
389 static const char *option_outfile_name = "-";
390
391 static char **parse_options(int argc, char **argv)
392 {
393 int optc;
394
395 while ((optc = getopt(argc, argv, "hi:o:s:")) != -1)
396 {
397 switch (optc)
398 {
399 case 'h':
400 usage();
401 exit(0);
402 case 'i':
403 option_inc_var_name = strdup(optarg);
404 break;
405 case 'o':
406 option_outfile_name = strdup(optarg);
407 break;
408 case 's':
409 option_inc_size_name = strdup(optarg);
410 break;
411 }
412 }
413 return &argv[optind];
414 }
415
416 int main(int argc, char **argv)
417 {
418 const char *infile_name;
419 char header[16];
420 struct parser parser;
421 char **args;
422 char *header_name = NULL;
423
424 program_name = argv[0];
425
426 args = parse_options(argc, argv);
427 infile_name = *args++;
428 if (!infile_name || *args)
429 {
430 usage();
431 return 1;
432 }
433
434 parser.infile = stdin;
435 parser.outfile = NULL;
436 parser.error = FALSE;
437
438 if (!strcmp(infile_name, "-")) {
439 infile_name = "stdin";
440 } else if (!(parser.infile = fopen(infile_name, "rb"))) {
441 perror(infile_name);
442 goto error;
443 }
444
445 if (!read_bytes(&parser, header, sizeof(header))) {
446 fprintf(stderr, "%s: Failed to read file header\n", program_name);
447 goto error;
448 }
449 if (strncmp(header, "xof ", 4))
450 {
451 fprintf(stderr, "%s: Invalid magic value '%.4s'\n", program_name, header);
452 goto error;
453 }
454 if (strncmp(header + 4, "0302", 4) && strncmp(header + 4, "0303", 4))
455 {
456 fprintf(stderr, "%s: Unsupported version '%.4s'\n", program_name, header + 4);
457 goto error;
458 }
459 if (strncmp(header + 8, "txt ", 4))
460 {
461 fprintf(stderr, "%s: Only support conversion from text encoded X files.",
462 program_name);
463 goto error;
464 }
465 if (strncmp(header + 12, "0032", 4) && strncmp(header + 12, "0064", 4))
466 {
467 fprintf(stderr, "%s: Only 32-bit or 64-bit float format supported, not '%.4s'.\n",
468 program_name, header + 12);
469 goto error;
470 }
471
472 if (!strcmp(option_outfile_name, "-")) {
473 option_outfile_name = "stdout";
474 parser.outfile = stdout;
475 } else {
476 output_file = option_outfile_name;
477 atexit(cleanup_files);
478 signal(SIGTERM, exit_on_signal);
479 signal(SIGINT, exit_on_signal);
480 #ifdef SIGHUP
481 signal(SIGHUP, exit_on_signal);
482 #endif
483 if (!(parser.outfile = fopen(output_file, "wb"))) {
484 perror(option_outfile_name);
485 goto error;
486 }
487 }
488
489 if (option_inc_var_name)
490 {
491 char *str_ptr;
492
493 header_name = strrchr(option_outfile_name, '/');
494 if (header_name)
495 header_name = strdup(header_name + 1);
496 else
497 header_name = strdup(option_outfile_name);
498 if (!header_name) {
499 fprintf(stderr, "Out of memory\n");
500 goto error;
501 }
502
503 str_ptr = header_name;
504 while (*str_ptr) {
505 if (*str_ptr == '.')
506 *str_ptr = '_';
507 else
508 *str_ptr = toupper(*str_ptr);
509 str_ptr++;
510 }
511
512 fprintf(parser.outfile,
513 "/* File generated automatically from %s; do not edit */\n"
514 "\n"
515 "#ifndef __WINE_%s\n"
516 "#define __WINE_%s\n"
517 "\n"
518 "unsigned char %s[] = {",
519 infile_name, header_name, header_name, option_inc_var_name);
520
521 if (ferror(parser.outfile))
522 goto error;
523
524 parser.write_bytes = &write_c_hex_bytes;
525 } else {
526 parser.write_bytes = &write_raw_bytes;
527 }
528
529 parser.bytes_output = 0;
530 if (!write_bytes(&parser, "xof 0302bin 0064", 16))
531 goto error;
532
533 parser.line_no = 1;
534 while (parse_token(&parser));
535
536 if (parser.error || ferror(parser.outfile) || ferror(parser.infile))
537 goto error;
538
539 if (option_inc_var_name)
540 {
541 fprintf(parser.outfile, "\n};\n\n");
542 if (option_inc_size_name)
543 fprintf(parser.outfile, "#define %s %u\n\n", option_inc_size_name, parser.bytes_output);
544 fprintf(parser.outfile, "#endif /* __WINE_%s */\n", header_name);
545 if (ferror(parser.outfile))
546 goto error;
547 }
548
549 fclose(parser.infile);
550 fclose(parser.outfile);
551 output_file = NULL;
552
553 return 0;
554 error:
555 if (parser.infile) {
556 if (ferror(parser.infile))
557 perror(infile_name);
558 fclose(parser.infile);
559 }
560 if (parser.outfile) {
561 if (ferror(parser.outfile))
562 perror(option_outfile_name);
563 fclose(parser.outfile);
564 }
565 return 1;
566 }
567
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.