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"
39 #define TOKEN_STRING 2
40 #define TOKEN_INTEGER 3
42 #define TOKEN_INTEGER_LIST 6
43 #define TOKEN_FLOAT_LIST 7
44 #define TOKEN_OBRACE 10
45 #define TOKEN_CBRACE 11
46 #define TOKEN_OPAREN 12
47 #define TOKEN_CPAREN 13
48 #define TOKEN_OBRACKET 14
49 #define TOKEN_CBRACKET 15
50 #define TOKEN_OANGLE 16
51 #define TOKEN_CANGLE 17
53 #define TOKEN_COMMA 19
54 #define TOKEN_SEMICOLON 20
55 #define TOKEN_TEMPLATE 31
57 #define TOKEN_DWORD 41
58 #define TOKEN_FLOAT 42
59 #define TOKEN_DOUBLE 43
61 #define TOKEN_UCHAR 45
62 #define TOKEN_SWORD 46
63 #define TOKEN_SDWORD 47
65 #define TOKEN_LPSTR 49
66 #define TOKEN_UNICODE 50
67 #define TOKEN_CSTRING 51
68 #define TOKEN_ARRAY 52
76 static const struct keyword reserved_words
[] = {
77 {"ARRAY", TOKEN_ARRAY
},
79 {"CSTRING", TOKEN_CSTRING
},
80 {"DOUBLE", TOKEN_DOUBLE
},
81 {"DWORD", TOKEN_DWORD
},
82 {"FLOAT", TOKEN_FLOAT
},
83 {"SDWORD", TOKEN_SDWORD
},
84 {"STRING", TOKEN_LPSTR
},
85 {"SWORD", TOKEN_SWORD
},
86 {"TEMPLATE", TOKEN_TEMPLATE
},
87 {"UCHAR", TOKEN_UCHAR
},
88 {"UNICODE", TOKEN_UNICODE
},
93 extern int getopt(int argc
, char *const *argv
, const char *optstring
);
95 static BOOL option_header
;
96 static char *option_inc_var_name
= NULL
;
97 static char *option_inc_size_name
= NULL
;
98 static const char *option_outfile_name
= "-";
99 static char *program_name
;
102 static const char *infile_name
;
103 static FILE *outfile
;
104 static BYTE
*output_data
;
105 static UINT output_pos
, output_size
;
108 #define __attribute__(x)
111 static void fatal_error( const char *msg
, ... ) __attribute__ ((__format__ (__printf__
, 1, 2)));
113 static void fatal_error( const char *msg
, ... )
116 va_start( valist
, msg
);
119 fprintf( stderr
, "%s:%d:", infile_name
, line_no
);
120 fprintf( stderr
, " error: " );
122 else fprintf( stderr
, "%s: error: ", program_name
);
123 vfprintf( stderr
, msg
, valist
);
129 static inline BOOL
read_byte( char *byte
)
131 int c
= fgetc(infile
);
133 if (c
== '\n') line_no
++;
137 static inline BOOL
unread_byte( char last_byte
)
139 if (last_byte
== '\n') line_no
--;
140 return ungetc(last_byte
, infile
) != EOF
;
143 static inline BOOL
read_bytes( void *data
, DWORD size
)
145 return fread(data
, size
, 1, infile
) > 0;
148 static BOOL
write_c_hex_bytes(void)
151 for (i
= 0; i
< output_pos
; i
++)
154 fprintf(outfile
, "\n ");
155 fprintf(outfile
, " 0x%02x,", output_data
[i
]);
160 static BOOL
write_raw_bytes(void)
162 return fwrite(output_data
, output_pos
, 1, outfile
) > 0;
165 static inline BOOL
write_bytes(const void *data
, DWORD size
)
167 if (output_pos
+ size
> output_size
)
169 output_size
= max( output_size
* 2, size
);
170 output_data
= realloc( output_data
, output_size
);
171 if (!output_data
) return FALSE
;
173 memcpy( output_data
+ output_pos
, data
, size
);
178 static inline BOOL
write_byte(BYTE value
)
180 return write_bytes( &value
, sizeof(value
) );
183 static inline BOOL
write_word(WORD value
)
185 return write_byte( value
) &&
186 write_byte( value
>> 8 );
189 static inline BOOL
write_dword(DWORD value
)
191 return write_word( value
) &&
192 write_word( value
>> 16 );
195 static inline BOOL
write_float(float value
)
198 memcpy( &val
, &value
, sizeof(value
) );
199 return write_dword( val
);
202 static inline BOOL
write_guid(const GUID
*guid
)
204 return write_dword( guid
->Data1
) &&
205 write_word( guid
->Data2
) &&
206 write_word( guid
->Data3
) &&
207 write_bytes( guid
->Data4
, sizeof(guid
->Data4
) );
210 static int compare_names(const void *a
, const void *b
)
212 return strcasecmp(*(const char **)a
, *(const char **)b
);
215 static BOOL
parse_keyword( const char *name
)
217 const struct keyword
*keyword
;
219 keyword
= bsearch(&name
, reserved_words
, ARRAY_SIZE(reserved_words
),
220 sizeof(reserved_words
[0]), compare_names
);
224 return write_word(keyword
->token
);
227 static BOOL
parse_guid(void)
233 static const char *guidfmt
= "<%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X>";
236 if (!read_bytes(buf
+ 1, 37)) fatal_error( "truncated GUID\n" );
239 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);
240 if (ret
!= 11) fatal_error( "invalid GUID '%s'\n", buf
);
244 guid
.Data4
[0] = tab
[2];
245 guid
.Data4
[1] = tab
[3];
246 guid
.Data4
[2] = tab
[4];
247 guid
.Data4
[3] = tab
[5];
248 guid
.Data4
[4] = tab
[6];
249 guid
.Data4
[5] = tab
[7];
250 guid
.Data4
[6] = tab
[8];
251 guid
.Data4
[7] = tab
[9];
253 return write_word(TOKEN_GUID
) &&
257 static BOOL
parse_name(void)
263 while (read_byte(&c
) && len
< sizeof(name
) &&
264 (isalnum(c
) || c
== '_' || c
== '-'))
266 if (len
+ 1 < sizeof(name
))
272 if (parse_keyword(name
)) {
275 return write_word(TOKEN_NAME
) &&
277 write_bytes(name
, len
);
281 static BOOL
parse_number(void)
289 while (read_byte(&c
) &&
290 ((!len
&& c
== '-') || (!dot
&& c
== '.') || isdigit(c
)))
292 if (len
+ 1 < sizeof(buffer
))
302 ret
= sscanf(buffer
, "%f", &value
);
303 if (!ret
) fatal_error( "invalid float token\n" );
304 ret
= write_word(TOKEN_FLOAT
) &&
308 ret
= sscanf(buffer
, "%d", &value
);
309 if (!ret
) fatal_error( "invalid integer token\n" );
310 ret
= write_word(TOKEN_INTEGER
) &&
317 static BOOL
parse_token(void)
321 char *tok
, buffer
[512];
334 case '{': return write_word(TOKEN_OBRACE
);
335 case '}': return write_word(TOKEN_CBRACE
);
336 case '[': return write_word(TOKEN_OBRACKET
);
337 case ']': return write_word(TOKEN_CBRACKET
);
338 case '(': return write_word(TOKEN_OPAREN
);
339 case ')': return write_word(TOKEN_CPAREN
);
340 case ',': return write_word(TOKEN_COMMA
);
341 case ';': return write_word(TOKEN_SEMICOLON
);
342 case '.': return write_word(TOKEN_DOT
);
345 if (!read_byte(&c
) || c
!= '/')
346 fatal_error( "invalid single '/' comment token\n" );
347 while (read_byte(&c
) && c
!= '\n');
352 while (read_byte(&c
) && c
!= '\n')
353 if (len
+ 1 < sizeof(buffer
)) buffer
[len
++] = c
;
354 if (c
!= '\n') fatal_error( "line too long\n" );
356 tok
= strtok( buffer
, " \t" );
357 if (!tok
|| strcmp( tok
, "pragma" )) return TRUE
;
358 tok
= strtok( NULL
, " \t" );
359 if (!tok
|| strcmp( tok
, "xftmpl" )) return TRUE
;
360 tok
= strtok( NULL
, " \t" );
361 if (!tok
) return TRUE
;
362 if (!strcmp( tok
, "name" ))
364 tok
= strtok( NULL
, " \t" );
365 if (tok
&& !option_inc_var_name
) option_inc_var_name
= strdup( tok
);
367 else if (!strcmp( tok
, "size" ))
369 tok
= strtok( NULL
, " \t" );
370 if (tok
&& !option_inc_size_name
) option_inc_size_name
= strdup( tok
);
380 /* FIXME: Handle '\' (e.g. "valid\"string") */
381 while (read_byte(&c
) && c
!= '"') {
382 if (len
+ 1 < sizeof(buffer
))
385 if (c
!= '"') fatal_error( "unterminated string\n" );
386 return write_word(TOKEN_STRING
) &&
388 write_bytes(buffer
, len
);
392 if (isdigit(c
) || c
== '-')
393 return parse_number();
394 if (isalpha(c
) || c
== '_')
396 fatal_error( "invalid character '%c' to start token\n", c
);
402 static const char *output_file
;
404 static void cleanup_files(void)
406 if (output_file
) unlink(output_file
);
409 static void exit_on_signal( int sig
)
411 exit(1); /* this will call the atexit functions */
414 static void usage(void)
416 fprintf(stderr
, "Usage: %s [OPTIONS] INFILE\n"
418 " -H Output to a c header file instead of a binary file\n"
419 " -i NAME Output to a c header file, data in variable NAME\n"
420 " -s NAME In a c header file, define NAME to be the data size\n"
421 " -o FILE Write output to FILE\n",
425 static char **parse_options(int argc
, char **argv
)
429 while ((optc
= getopt(argc
, argv
, "hHi:o:s:")) != -1)
437 option_header
= TRUE
;
440 option_header
= TRUE
;
441 option_inc_var_name
= strdup(optarg
);
444 option_outfile_name
= strdup(optarg
);
447 option_inc_size_name
= strdup(optarg
);
451 return &argv
[optind
];
454 int main(int argc
, char **argv
)
458 char *header_name
= NULL
;
460 program_name
= argv
[0];
462 args
= parse_options(argc
, argv
);
463 infile_name
= *args
++;
464 if (!infile_name
|| *args
)
473 if (!strcmp(infile_name
, "-")) {
474 infile_name
= "stdin";
475 } else if (!(infile
= fopen(infile_name
, "rb"))) {
480 if (!read_bytes(header
, sizeof(header
))) {
481 fprintf(stderr
, "%s: Failed to read file header\n", program_name
);
484 if (strncmp(header
, "xof ", 4))
486 fprintf(stderr
, "%s: Invalid magic value '%.4s'\n", program_name
, header
);
489 if (strncmp(header
+ 4, "0302", 4) && strncmp(header
+ 4, "0303", 4))
491 fprintf(stderr
, "%s: Unsupported version '%.4s'\n", program_name
, header
+ 4);
494 if (strncmp(header
+ 8, "txt ", 4))
496 fprintf(stderr
, "%s: Only support conversion from text encoded X files.",
500 if (strncmp(header
+ 12, "0032", 4) && strncmp(header
+ 12, "0064", 4))
502 fprintf(stderr
, "%s: Only 32-bit or 64-bit float format supported, not '%.4s'.\n",
503 program_name
, header
+ 12);
507 if (!strcmp(option_outfile_name
, "-")) {
508 option_outfile_name
= "stdout";
511 output_file
= option_outfile_name
;
512 atexit(cleanup_files
);
513 signal(SIGTERM
, exit_on_signal
);
514 signal(SIGINT
, exit_on_signal
);
516 signal(SIGHUP
, exit_on_signal
);
518 if (!(outfile
= fopen(output_file
, "wb"))) {
519 perror(option_outfile_name
);
524 if (!write_bytes("xof 0302bin 0064", 16))
528 while (parse_token());
530 if (ferror(outfile
) || ferror(infile
))
537 if (!option_inc_var_name
)
538 fatal_error( "variable name must be specified with -i or #pragma name\n" );
540 header_name
= strrchr(option_outfile_name
, '/');
542 header_name
= strdup(header_name
+ 1);
544 header_name
= strdup(option_outfile_name
);
546 fprintf(stderr
, "Out of memory\n");
550 str_ptr
= header_name
;
555 *str_ptr
= toupper(*str_ptr
);
560 "/* File generated automatically from %s; do not edit */\n"
562 "#ifndef __WINE_%s\n"
563 "#define __WINE_%s\n"
565 "unsigned char %s[] = {",
566 infile_name
, header_name
, header_name
, option_inc_var_name
);
568 fprintf(outfile
, "\n};\n\n");
569 if (option_inc_size_name
)
570 fprintf(outfile
, "#define %s %u\n\n", option_inc_size_name
, output_pos
);
571 fprintf(outfile
, "#endif /* __WINE_%s */\n", header_name
);
575 else write_raw_bytes();
590 perror(option_outfile_name
);