2 * Binary encode X templates from text format.
4 * Copyright 2011 Dylan Smith
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.
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.
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
22 #include "wine/port.h"
37 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(array[0]))
40 #define TOKEN_STRING 2
41 #define TOKEN_INTEGER 3
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
54 #define TOKEN_COMMA 19
55 #define TOKEN_SEMICOLON 20
56 #define TOKEN_TEMPLATE 31
58 #define TOKEN_DWORD 41
59 #define TOKEN_FLOAT 42
60 #define TOKEN_DOUBLE 43
62 #define TOKEN_UCHAR 45
63 #define TOKEN_SWORD 46
64 #define TOKEN_SDWORD 47
66 #define TOKEN_LPSTR 49
67 #define TOKEN_UNICODE 50
68 #define TOKEN_CSTRING 51
69 #define TOKEN_ARRAY 52
77 BOOL(*write_bytes)(struct parser *parser, const BYTE *data, DWORD size);
87 static const struct keyword reserved_words[] = {
88 {"ARRAY", TOKEN_ARRAY},
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},
104 static char *program_name;
106 static inline BOOL read_byte(struct parser *parser, char *byte)
108 int c = fgetc(parser->infile);
113 static inline BOOL unread_byte(struct parser *parser, char last_byte)
115 return ungetc(last_byte, parser->infile) != EOF;
118 static inline BOOL read_bytes(struct parser *parser, void *data, DWORD size)
120 return fread(data, size, 1, parser->infile) > 0;
123 static BOOL write_c_hex_bytes(struct parser *parser, const BYTE *data, DWORD size)
127 if (parser->bytes_output % 12 == 0)
128 fprintf(parser->outfile, "\n ");
129 fprintf(parser->outfile, " 0x%02x,", *data++);
130 parser->bytes_output++;
135 static BOOL write_raw_bytes(struct parser *parser, const BYTE *data, DWORD size)
137 return fwrite(data, size, 1, parser->outfile) > 0;
140 static inline BOOL write_bytes(struct parser *parser, const void *data, DWORD size)
142 return parser->write_bytes(parser, data, size);
145 static inline BOOL write_byte(struct parser *parser, BYTE value)
147 return write_bytes(parser, &value, sizeof(value));
150 static inline BOOL write_word(struct parser *parser, WORD value)
152 return write_bytes(parser, &value, sizeof(value));
155 static inline BOOL write_dword(struct parser *parser, DWORD value)
157 return write_bytes(parser, &value, sizeof(value));
160 static int compare_names(const void *a, const void *b)
162 return strcasecmp(*(const char **)a, *(const char **)b);
165 static BOOL parse_keyword(struct parser *parser, const char *name)
167 const struct keyword *keyword;
169 keyword = bsearch(&name, reserved_words, ARRAY_SIZE(reserved_words),
170 sizeof(reserved_words[0]), compare_names);
174 return write_word(parser, keyword->token);
177 static BOOL parse_guid(struct parser *parser)
183 static const char *guidfmt = "<%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X>";
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;
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);
196 fprintf(stderr, "%s: Invalid GUID '%s' (line %d)\n",
197 program_name, buf, parser->line_no);
198 parser->error = TRUE;
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];
213 return write_word(parser, TOKEN_GUID) &&
214 write_bytes(parser, &guid, sizeof(guid));
217 static BOOL parse_name(struct parser *parser)
223 while (read_byte(parser, &c) && len < sizeof(name) &&
224 (isalnum(c) || c == '_' || c == '-'))
226 if (len + 1 < sizeof(name))
229 unread_byte(parser, c);
232 if (parse_keyword(parser, name)) {
235 return write_word(parser, TOKEN_NAME) &&
236 write_dword(parser, len) &&
237 write_bytes(parser, name, len);
241 static BOOL parse_number(struct parser *parser)
249 while (read_byte(parser, &c) &&
250 ((!len && c == '-') || (!dot && c == '.') || isdigit(c)))
252 if (len + 1 < sizeof(buffer))
257 unread_byte(parser, c);
262 ret = sscanf(buffer, "%f", &value);
264 fprintf(stderr, "%s: Invalid float token (line %d).\n",
265 program_name, parser->line_no);
266 parser->error = TRUE;
268 ret = write_word(parser, TOKEN_FLOAT) &&
269 write_bytes(parser, &value, sizeof(value));
273 ret = sscanf(buffer, "%d", &value);
275 fprintf(stderr, "%s: Invalid integer token (line %d).\n",
276 program_name, parser->line_no);
277 parser->error = TRUE;
279 ret = write_word(parser, TOKEN_INTEGER) &&
280 write_dword(parser, value);
287 static BOOL parse_token(struct parser *parser)
291 if (!read_byte(parser, &c))
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);
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;
323 while (read_byte(parser, &c) && c != '\n');
327 return parse_guid(parser);
334 /* FIXME: Handle '\' (e.g. "valid\"string") */
335 while (read_byte(parser, &c) && c != '"') {
336 if (len + 1 < sizeof(buffer))
340 fprintf(stderr, "%s: Unterminated string (line %d).\n",
341 program_name, parser->line_no);
342 parser->error = TRUE;
345 return write_word(parser, TOKEN_STRING) &&
346 write_dword(parser, len) &&
347 write_bytes(parser, buffer, len);
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;
365 static const char *output_file;
367 static void cleanup_files(void)
369 if (output_file) unlink(output_file);
372 static void exit_on_signal( int sig )
374 exit(1); /* this will call the atexit functions */
377 static void usage(void)
379 fprintf(stderr, "Usage: %s [OPTIONS] INFILE\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",
387 static char *option_inc_var_name = NULL;
388 static char *option_inc_size_name = NULL;
389 static const char *option_outfile_name = "-";
391 static char **parse_options(int argc, char **argv)
395 while ((optc = getopt(argc, argv, "hi:o:s:")) != -1)
403 option_inc_var_name = strdup(optarg);
406 option_outfile_name = strdup(optarg);
409 option_inc_size_name = strdup(optarg);
413 return &argv[optind];
416 int main(int argc, char **argv)
418 const char *infile_name;
420 struct parser parser;
422 char *header_name = NULL;
424 program_name = argv[0];
426 args = parse_options(argc, argv);
427 infile_name = *args++;
428 if (!infile_name || *args)
434 parser.infile = stdin;
435 parser.outfile = NULL;
436 parser.error = FALSE;
438 if (!strcmp(infile_name, "-")) {
439 infile_name = "stdin";
440 } else if (!(parser.infile = fopen(infile_name, "rb"))) {
445 if (!read_bytes(&parser, header, sizeof(header))) {
446 fprintf(stderr, "%s: Failed to read file header\n", program_name);
449 if (strncmp(header, "xof ", 4))
451 fprintf(stderr, "%s: Invalid magic value '%.4s'\n", program_name, header);
454 if (strncmp(header + 4, "0302", 4) && strncmp(header + 4, "0303", 4))
456 fprintf(stderr, "%s: Unsupported version '%.4s'\n", program_name, header + 4);
459 if (strncmp(header + 8, "txt ", 4))
461 fprintf(stderr, "%s: Only support conversion from text encoded X files.",
465 if (strncmp(header + 12, "0032", 4) && strncmp(header + 12, "0064", 4))
467 fprintf(stderr, "%s: Only 32-bit or 64-bit float format supported, not '%.4s'.\n",
468 program_name, header + 12);
472 if (!strcmp(option_outfile_name, "-")) {
473 option_outfile_name = "stdout";
474 parser.outfile = stdout;
476 output_file = option_outfile_name;
477 atexit(cleanup_files);
478 signal(SIGTERM, exit_on_signal);
479 signal(SIGINT, exit_on_signal);
481 signal(SIGHUP, exit_on_signal);
483 if (!(parser.outfile = fopen(output_file, "wb"))) {
484 perror(option_outfile_name);
489 if (option_inc_var_name)
493 header_name = strrchr(option_outfile_name, '/');
495 header_name = strdup(header_name + 1);
497 header_name = strdup(option_outfile_name);
499 fprintf(stderr, "Out of memory\n");
503 str_ptr = header_name;
508 *str_ptr = toupper(*str_ptr);
512 fprintf(parser.outfile,
513 "/* File generated automatically from %s; do not edit */\n"
515 "#ifndef __WINE_%s\n"
516 "#define __WINE_%s\n"
518 "unsigned char %s[] = {",
519 infile_name, header_name, header_name, option_inc_var_name);
521 if (ferror(parser.outfile))
524 parser.write_bytes = &write_c_hex_bytes;
526 parser.write_bytes = &write_raw_bytes;
529 parser.bytes_output = 0;
530 if (!write_bytes(&parser, "xof 0302bin 0064", 16))
534 while (parse_token(&parser));
536 if (parser.error || ferror(parser.outfile) || ferror(parser.infile))
539 if (option_inc_var_name)
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))
549 fclose(parser.infile);
550 fclose(parser.outfile);
556 if (ferror(parser.infile))
558 fclose(parser.infile);
560 if (parser.outfile) {
561 if (ferror(parser.outfile))
562 perror(option_outfile_name);
563 fclose(parser.outfile);