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"
38 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(array[0]))
41 #define TOKEN_STRING 2
42 #define TOKEN_INTEGER 3
44 #define TOKEN_INTEGER_LIST 6
45 #define TOKEN_FLOAT_LIST 7
46 #define TOKEN_OBRACE 10
47 #define TOKEN_CBRACE 11
48 #define TOKEN_OPAREN 12
49 #define TOKEN_CPAREN 13
50 #define TOKEN_OBRACKET 14
51 #define TOKEN_CBRACKET 15
52 #define TOKEN_OANGLE 16
53 #define TOKEN_CANGLE 17
55 #define TOKEN_COMMA 19
56 #define TOKEN_SEMICOLON 20
57 #define TOKEN_TEMPLATE 31
59 #define TOKEN_DWORD 41
60 #define TOKEN_FLOAT 42
61 #define TOKEN_DOUBLE 43
63 #define TOKEN_UCHAR 45
64 #define TOKEN_SWORD 46
65 #define TOKEN_SDWORD 47
67 #define TOKEN_LPSTR 49
68 #define TOKEN_UNICODE 50
69 #define TOKEN_CSTRING 51
70 #define TOKEN_ARRAY 52
78 static const struct keyword reserved_words
[] = {
79 {"ARRAY", TOKEN_ARRAY
},
81 {"CSTRING", TOKEN_CSTRING
},
82 {"DOUBLE", TOKEN_DOUBLE
},
83 {"DWORD", TOKEN_DWORD
},
84 {"FLOAT", TOKEN_FLOAT
},
85 {"SDWORD", TOKEN_SDWORD
},
86 {"STRING", TOKEN_LPSTR
},
87 {"SWORD", TOKEN_SWORD
},
88 {"TEMPLATE", TOKEN_TEMPLATE
},
89 {"UCHAR", TOKEN_UCHAR
},
90 {"UNICODE", TOKEN_UNICODE
},
95 extern int getopt(int argc
, char *const *argv
, const char *optstring
);
97 static BOOL option_header
;
98 static char *option_inc_var_name
= NULL
;
99 static char *option_inc_size_name
= NULL
;
100 static const char *option_outfile_name
= "-";
101 static char *program_name
;
104 static const char *infile_name
;
105 static FILE *outfile
;
106 static BYTE
*output_data
;
107 static UINT output_pos
, output_size
;
110 #define __attribute__(x)
113 static void fatal_error( const char *msg
, ... ) __attribute__ ((__format__ (__printf__
, 1, 2)));
115 static void fatal_error( const char *msg
, ... )
118 va_start( valist
, msg
);
121 fprintf( stderr
, "%s:%d:", infile_name
, line_no
);
122 fprintf( stderr
, " error: " );
124 else fprintf( stderr
, "%s: error: ", program_name
);
125 vfprintf( stderr
, msg
, valist
);
131 static inline BOOL
read_byte( char *byte
)
133 int c
= fgetc(infile
);
135 if (c
== '\n') line_no
++;
139 static inline BOOL
unread_byte( char last_byte
)
141 if (last_byte
== '\n') line_no
--;
142 return ungetc(last_byte
, infile
) != EOF
;
145 static inline BOOL
read_bytes( void *data
, DWORD size
)
147 return fread(data
, size
, 1, infile
) > 0;
150 static BOOL
write_c_hex_bytes(void)
153 for (i
= 0; i
< output_pos
; i
++)
156 fprintf(outfile
, "\n ");
157 fprintf(outfile
, " 0x%02x,", output_data
[i
]);
162 static BOOL
write_raw_bytes(void)
164 return fwrite(output_data
, output_pos
, 1, outfile
) > 0;
167 static inline BOOL
write_bytes(const void *data
, DWORD size
)
169 if (output_pos
+ size
> output_size
)
171 output_size
= max( output_size
* 2, size
);
172 output_data
= realloc( output_data
, output_size
);
173 if (!output_data
) return FALSE
;
175 memcpy( output_data
+ output_pos
, data
, size
);
180 static inline BOOL
write_byte(BYTE value
)
182 return write_bytes( &value
, sizeof(value
) );
185 static inline BOOL
write_word(WORD value
)
187 return write_byte( value
) &&
188 write_byte( value
>> 8 );
191 static inline BOOL
write_dword(DWORD value
)
193 return write_word( value
) &&
194 write_word( value
>> 16 );
197 static inline BOOL
write_float(float value
)
200 memcpy( &val
, &value
, sizeof(value
) );
201 return write_dword( val
);
204 static inline BOOL
write_guid(const GUID
*guid
)
206 return write_dword( guid
->Data1
) &&
207 write_word( guid
->Data2
) &&
208 write_word( guid
->Data3
) &&
209 write_bytes( guid
->Data4
, sizeof(guid
->Data4
) );
212 static int compare_names(const void *a
, const void *b
)
214 return strcasecmp(*(const char **)a
, *(const char **)b
);
217 static BOOL
parse_keyword( const char *name
)
219 const struct keyword
*keyword
;
221 keyword
= bsearch(&name
, reserved_words
, ARRAY_SIZE(reserved_words
),
222 sizeof(reserved_words
[0]), compare_names
);
226 return write_word(keyword
->token
);
229 static BOOL
parse_guid(void)
235 static const char *guidfmt
= "<%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X>";
238 if (!read_bytes(buf
+ 1, 37)) fatal_error( "truncated GUID\n" );
241 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);
242 if (ret
!= 11) fatal_error( "invalid GUID '%s'\n", buf
);
246 guid
.Data4
[0] = tab
[2];
247 guid
.Data4
[1] = tab
[3];
248 guid
.Data4
[2] = tab
[4];
249 guid
.Data4
[3] = tab
[5];
250 guid
.Data4
[4] = tab
[6];
251 guid
.Data4
[5] = tab
[7];
252 guid
.Data4
[6] = tab
[8];
253 guid
.Data4
[7] = tab
[9];
255 return write_word(TOKEN_GUID
) &&
259 static BOOL
parse_name(void)
265 while (read_byte(&c
) && len
< sizeof(name
) &&
266 (isalnum(c
) || c
== '_' || c
== '-'))
268 if (len
+ 1 < sizeof(name
))
274 if (parse_keyword(name
)) {
277 return write_word(TOKEN_NAME
) &&
279 write_bytes(name
, len
);
283 static BOOL
parse_number(void)
291 while (read_byte(&c
) &&
292 ((!len
&& c
== '-') || (!dot
&& c
== '.') || isdigit(c
)))
294 if (len
+ 1 < sizeof(buffer
))
304 ret
= sscanf(buffer
, "%f", &value
);
305 if (!ret
) fatal_error( "invalid float token\n" );
306 ret
= write_word(TOKEN_FLOAT
) &&
310 ret
= sscanf(buffer
, "%d", &value
);
311 if (!ret
) fatal_error( "invalid integer token\n" );
312 ret
= write_word(TOKEN_INTEGER
) &&
319 static BOOL
parse_token(void)
323 char *tok
, buffer
[512];
336 case '{': return write_word(TOKEN_OBRACE
);
337 case '}': return write_word(TOKEN_CBRACE
);
338 case '[': return write_word(TOKEN_OBRACKET
);
339 case ']': return write_word(TOKEN_CBRACKET
);
340 case '(': return write_word(TOKEN_OPAREN
);
341 case ')': return write_word(TOKEN_CPAREN
);
342 case ',': return write_word(TOKEN_COMMA
);
343 case ';': return write_word(TOKEN_SEMICOLON
);
344 case '.': return write_word(TOKEN_DOT
);
347 if (!read_byte(&c
) || c
!= '/')
348 fatal_error( "invalid single '/' comment token\n" );
349 while (read_byte(&c
) && c
!= '\n');
354 while (read_byte(&c
) && c
!= '\n')
355 if (len
+ 1 < sizeof(buffer
)) buffer
[len
++] = c
;
356 if (c
!= '\n') fatal_error( "line too long\n" );
358 tok
= strtok( buffer
, " \t" );
359 if (!tok
|| strcmp( tok
, "pragma" )) return TRUE
;
360 tok
= strtok( NULL
, " \t" );
361 if (!tok
|| strcmp( tok
, "xftmpl" )) return TRUE
;
362 tok
= strtok( NULL
, " \t" );
363 if (!tok
) return TRUE
;
364 if (!strcmp( tok
, "name" ))
366 tok
= strtok( NULL
, " \t" );
367 if (tok
&& !option_inc_var_name
) option_inc_var_name
= strdup( tok
);
369 else if (!strcmp( tok
, "size" ))
371 tok
= strtok( NULL
, " \t" );
372 if (tok
&& !option_inc_size_name
) option_inc_size_name
= strdup( tok
);
382 /* FIXME: Handle '\' (e.g. "valid\"string") */
383 while (read_byte(&c
) && c
!= '"') {
384 if (len
+ 1 < sizeof(buffer
))
387 if (c
!= '"') fatal_error( "unterminated string\n" );
388 return write_word(TOKEN_STRING
) &&
390 write_bytes(buffer
, len
);
394 if (isdigit(c
) || c
== '-')
395 return parse_number();
396 if (isalpha(c
) || c
== '_')
398 fatal_error( "invalid character '%c' to start token\n", c
);
404 static const char *output_file
;
406 static void cleanup_files(void)
408 if (output_file
) unlink(output_file
);
411 static void exit_on_signal( int sig
)
413 exit(1); /* this will call the atexit functions */
416 static void usage(void)
418 fprintf(stderr
, "Usage: %s [OPTIONS] INFILE\n"
420 " -H Output to a c header file instead of a binary file\n"
421 " -i NAME Output to a c header file, data in variable NAME\n"
422 " -s NAME In a c header file, define NAME to be the data size\n"
423 " -o FILE Write output to FILE\n",
427 static char **parse_options(int argc
, char **argv
)
431 while ((optc
= getopt(argc
, argv
, "hHi:o:s:")) != -1)
439 option_header
= TRUE
;
442 option_header
= TRUE
;
443 option_inc_var_name
= strdup(optarg
);
446 option_outfile_name
= strdup(optarg
);
449 option_inc_size_name
= strdup(optarg
);
453 return &argv
[optind
];
456 int main(int argc
, char **argv
)
460 char *header_name
= NULL
;
462 program_name
= argv
[0];
464 args
= parse_options(argc
, argv
);
465 infile_name
= *args
++;
466 if (!infile_name
|| *args
)
475 if (!strcmp(infile_name
, "-")) {
476 infile_name
= "stdin";
477 } else if (!(infile
= fopen(infile_name
, "rb"))) {
482 if (!read_bytes(header
, sizeof(header
))) {
483 fprintf(stderr
, "%s: Failed to read file header\n", program_name
);
486 if (strncmp(header
, "xof ", 4))
488 fprintf(stderr
, "%s: Invalid magic value '%.4s'\n", program_name
, header
);
491 if (strncmp(header
+ 4, "0302", 4) && strncmp(header
+ 4, "0303", 4))
493 fprintf(stderr
, "%s: Unsupported version '%.4s'\n", program_name
, header
+ 4);
496 if (strncmp(header
+ 8, "txt ", 4))
498 fprintf(stderr
, "%s: Only support conversion from text encoded X files.",
502 if (strncmp(header
+ 12, "0032", 4) && strncmp(header
+ 12, "0064", 4))
504 fprintf(stderr
, "%s: Only 32-bit or 64-bit float format supported, not '%.4s'.\n",
505 program_name
, header
+ 12);
509 if (!strcmp(option_outfile_name
, "-")) {
510 option_outfile_name
= "stdout";
513 output_file
= option_outfile_name
;
514 atexit(cleanup_files
);
515 signal(SIGTERM
, exit_on_signal
);
516 signal(SIGINT
, exit_on_signal
);
518 signal(SIGHUP
, exit_on_signal
);
520 if (!(outfile
= fopen(output_file
, "wb"))) {
521 perror(option_outfile_name
);
526 if (!write_bytes("xof 0302bin 0064", 16))
530 while (parse_token());
532 if (ferror(outfile
) || ferror(infile
))
539 if (!option_inc_var_name
)
540 fatal_error( "variable name must be specified with -i or #pragma name\n" );
542 header_name
= strrchr(option_outfile_name
, '/');
544 header_name
= strdup(header_name
+ 1);
546 header_name
= strdup(option_outfile_name
);
548 fprintf(stderr
, "Out of memory\n");
552 str_ptr
= header_name
;
557 *str_ptr
= toupper(*str_ptr
);
562 "/* File generated automatically from %s; do not edit */\n"
564 "#ifndef __WINE_%s\n"
565 "#define __WINE_%s\n"
567 "unsigned char %s[] = {",
568 infile_name
, header_name
, header_name
, option_inc_var_name
);
570 fprintf(outfile
, "\n};\n\n");
571 if (option_inc_size_name
)
572 fprintf(outfile
, "#define %s %u\n\n", option_inc_size_name
, output_pos
);
573 fprintf(outfile
, "#endif /* __WINE_%s */\n", header_name
);
577 else write_raw_bytes();
592 perror(option_outfile_name
);