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
33 #define TOKEN_STRING 2
34 #define TOKEN_INTEGER 3
36 #define TOKEN_INTEGER_LIST 6
37 #define TOKEN_FLOAT_LIST 7
38 #define TOKEN_OBRACE 10
39 #define TOKEN_CBRACE 11
40 #define TOKEN_OPAREN 12
41 #define TOKEN_CPAREN 13
42 #define TOKEN_OBRACKET 14
43 #define TOKEN_CBRACKET 15
44 #define TOKEN_OANGLE 16
45 #define TOKEN_CANGLE 17
47 #define TOKEN_COMMA 19
48 #define TOKEN_SEMICOLON 20
49 #define TOKEN_TEMPLATE 31
51 #define TOKEN_DWORD 41
52 #define TOKEN_FLOAT 42
53 #define TOKEN_DOUBLE 43
55 #define TOKEN_UCHAR 45
56 #define TOKEN_SWORD 46
57 #define TOKEN_SDWORD 47
59 #define TOKEN_LPSTR 49
60 #define TOKEN_UNICODE 50
61 #define TOKEN_CSTRING 51
62 #define TOKEN_ARRAY 52
70 static const struct keyword reserved_words
[] = {
71 {"ARRAY", TOKEN_ARRAY
},
73 {"CSTRING", TOKEN_CSTRING
},
74 {"DOUBLE", TOKEN_DOUBLE
},
75 {"DWORD", TOKEN_DWORD
},
76 {"FLOAT", TOKEN_FLOAT
},
77 {"SDWORD", TOKEN_SDWORD
},
78 {"STRING", TOKEN_LPSTR
},
79 {"SWORD", TOKEN_SWORD
},
80 {"TEMPLATE", TOKEN_TEMPLATE
},
81 {"UCHAR", TOKEN_UCHAR
},
82 {"UNICODE", TOKEN_UNICODE
},
87 static BOOL option_header
;
88 static char *option_inc_var_name
= NULL
;
89 static char *option_inc_size_name
= NULL
;
90 static const char *option_outfile_name
= "-";
91 static char *program_name
;
94 static const char *infile_name
;
96 static BYTE
*output_data
;
97 static UINT output_pos
, output_size
;
99 static void fatal_error( const char *msg
, ... ) __attribute__ ((__format__ (__printf__
, 1, 2)));
101 static void fatal_error( const char *msg
, ... )
104 va_start( valist
, msg
);
107 fprintf( stderr
, "%s:%d:", infile_name
, line_no
);
108 fprintf( stderr
, " error: " );
110 else fprintf( stderr
, "%s: error: ", program_name
);
111 vfprintf( stderr
, msg
, valist
);
117 static inline BOOL
read_byte( char *byte
)
119 int c
= fgetc(infile
);
121 if (c
== '\n') line_no
++;
125 static inline BOOL
unread_byte( char last_byte
)
127 if (last_byte
== '\n') line_no
--;
128 return ungetc(last_byte
, infile
) != EOF
;
131 static inline BOOL
read_bytes( void *data
, DWORD size
)
133 return fread(data
, size
, 1, infile
) > 0;
136 static BOOL
write_c_hex_bytes(void)
139 for (i
= 0; i
< output_pos
; i
++)
142 fprintf(outfile
, "\n ");
143 fprintf(outfile
, " 0x%02x,", output_data
[i
]);
148 static BOOL
write_raw_bytes(void)
150 return fwrite(output_data
, output_pos
, 1, outfile
) > 0;
153 static inline BOOL
write_bytes(const void *data
, DWORD size
)
155 if (output_pos
+ size
> output_size
)
157 output_size
= max( output_size
* 2, size
);
158 output_data
= realloc( output_data
, output_size
);
159 if (!output_data
) return FALSE
;
161 memcpy( output_data
+ output_pos
, data
, size
);
166 static inline BOOL
write_byte(BYTE value
)
168 return write_bytes( &value
, sizeof(value
) );
171 static inline BOOL
write_word(WORD value
)
173 return write_byte( value
) &&
174 write_byte( value
>> 8 );
177 static inline BOOL
write_dword(DWORD value
)
179 return write_word( value
) &&
180 write_word( value
>> 16 );
183 static inline BOOL
write_float(float value
)
186 memcpy( &val
, &value
, sizeof(value
) );
187 return write_dword( val
);
190 static inline BOOL
write_guid(const GUID
*guid
)
192 return write_dword( guid
->Data1
) &&
193 write_word( guid
->Data2
) &&
194 write_word( guid
->Data3
) &&
195 write_bytes( guid
->Data4
, sizeof(guid
->Data4
) );
198 static int compare_names(const void *a
, const void *b
)
200 return strcasecmp(*(const char **)a
, *(const char **)b
);
203 static BOOL
parse_keyword( const char *name
)
205 const struct keyword
*keyword
;
207 keyword
= bsearch(&name
, reserved_words
, ARRAY_SIZE(reserved_words
),
208 sizeof(reserved_words
[0]), compare_names
);
212 return write_word(keyword
->token
);
215 static BOOL
parse_guid(void)
221 static const char *guidfmt
= "<%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X>";
224 if (!read_bytes(buf
+ 1, 37)) fatal_error( "truncated GUID\n" );
227 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);
228 if (ret
!= 11) fatal_error( "invalid GUID '%s'\n", buf
);
232 guid
.Data4
[0] = tab
[2];
233 guid
.Data4
[1] = tab
[3];
234 guid
.Data4
[2] = tab
[4];
235 guid
.Data4
[3] = tab
[5];
236 guid
.Data4
[4] = tab
[6];
237 guid
.Data4
[5] = tab
[7];
238 guid
.Data4
[6] = tab
[8];
239 guid
.Data4
[7] = tab
[9];
241 return write_word(TOKEN_GUID
) &&
245 static BOOL
parse_name(void)
251 while (read_byte(&c
) && len
< sizeof(name
) &&
252 (isalnum(c
) || c
== '_' || c
== '-'))
254 if (len
+ 1 < sizeof(name
))
260 if (parse_keyword(name
)) {
263 return write_word(TOKEN_NAME
) &&
265 write_bytes(name
, len
);
269 static BOOL
parse_number(void)
277 while (read_byte(&c
) &&
278 ((!len
&& c
== '-') || (!dot
&& c
== '.') || isdigit(c
)))
280 if (len
+ 1 < sizeof(buffer
))
290 ret
= sscanf(buffer
, "%f", &value
);
291 if (!ret
) fatal_error( "invalid float token\n" );
292 ret
= write_word(TOKEN_FLOAT
) &&
296 ret
= sscanf(buffer
, "%d", &value
);
297 if (!ret
) fatal_error( "invalid integer token\n" );
298 ret
= write_word(TOKEN_INTEGER
) &&
305 static BOOL
parse_token(void)
309 char *tok
, buffer
[512];
322 case '{': return write_word(TOKEN_OBRACE
);
323 case '}': return write_word(TOKEN_CBRACE
);
324 case '[': return write_word(TOKEN_OBRACKET
);
325 case ']': return write_word(TOKEN_CBRACKET
);
326 case '(': return write_word(TOKEN_OPAREN
);
327 case ')': return write_word(TOKEN_CPAREN
);
328 case ',': return write_word(TOKEN_COMMA
);
329 case ';': return write_word(TOKEN_SEMICOLON
);
330 case '.': return write_word(TOKEN_DOT
);
333 if (!read_byte(&c
) || c
!= '/')
334 fatal_error( "invalid single '/' comment token\n" );
335 while (read_byte(&c
) && c
!= '\n');
340 while (read_byte(&c
) && c
!= '\n')
341 if (len
+ 1 < sizeof(buffer
)) buffer
[len
++] = c
;
342 if (c
!= '\n') fatal_error( "line too long\n" );
344 tok
= strtok( buffer
, " \t" );
345 if (!tok
|| strcmp( tok
, "pragma" )) return TRUE
;
346 tok
= strtok( NULL
, " \t" );
347 if (!tok
|| strcmp( tok
, "xftmpl" )) return TRUE
;
348 tok
= strtok( NULL
, " \t" );
349 if (!tok
) return TRUE
;
350 if (!strcmp( tok
, "name" ))
352 tok
= strtok( NULL
, " \t" );
353 if (tok
&& !option_inc_var_name
) option_inc_var_name
= xstrdup( tok
);
355 else if (!strcmp( tok
, "size" ))
357 tok
= strtok( NULL
, " \t" );
358 if (tok
&& !option_inc_size_name
) option_inc_size_name
= xstrdup( tok
);
368 /* FIXME: Handle '\' (e.g. "valid\"string") */
369 while (read_byte(&c
) && c
!= '"') {
370 if (len
+ 1 < sizeof(buffer
))
373 if (c
!= '"') fatal_error( "unterminated string\n" );
374 return write_word(TOKEN_STRING
) &&
376 write_bytes(buffer
, len
);
380 if (isdigit(c
) || c
== '-')
381 return parse_number();
382 if (isalpha(c
) || c
== '_')
384 fatal_error( "invalid character '%c' to start token\n", c
);
390 static const char *output_file
;
392 static void cleanup_files(void)
394 if (output_file
) unlink(output_file
);
397 static void exit_on_signal( int sig
)
399 exit(1); /* this will call the atexit functions */
402 static void usage(void)
404 fprintf(stderr
, "Usage: %s [OPTIONS] INFILE\n"
406 " -H Output to a c header file instead of a binary file\n"
407 " -i NAME Output to a c header file, data in variable NAME\n"
408 " -s NAME In a c header file, define NAME to be the data size\n"
409 " -o FILE Write output to FILE\n",
413 static void option_callback( int optc
, char *optarg
)
421 option_header
= TRUE
;
424 option_header
= TRUE
;
425 option_inc_var_name
= xstrdup(optarg
);
428 option_outfile_name
= xstrdup(optarg
);
431 option_inc_size_name
= xstrdup(optarg
);
434 fprintf( stderr
, "%s: %s\n", program_name
, optarg
);
439 int main(int argc
, char **argv
)
442 struct strarray args
;
443 char *header_name
= NULL
;
445 program_name
= argv
[0];
447 args
= parse_options(argc
, argv
, "hHi:o:s:", NULL
, 0, option_callback
);
453 infile_name
= args
.str
[0];
458 if (!strcmp(infile_name
, "-")) {
459 infile_name
= "stdin";
460 } else if (!(infile
= fopen(infile_name
, "rb"))) {
465 if (!read_bytes(header
, sizeof(header
))) {
466 fprintf(stderr
, "%s: Failed to read file header\n", program_name
);
469 if (strncmp(header
, "xof ", 4))
471 fprintf(stderr
, "%s: Invalid magic value '%.4s'\n", program_name
, header
);
474 if (strncmp(header
+ 4, "0302", 4) && strncmp(header
+ 4, "0303", 4))
476 fprintf(stderr
, "%s: Unsupported version '%.4s'\n", program_name
, header
+ 4);
479 if (strncmp(header
+ 8, "txt ", 4))
481 fprintf(stderr
, "%s: Only support conversion from text encoded X files.",
485 if (strncmp(header
+ 12, "0032", 4) && strncmp(header
+ 12, "0064", 4))
487 fprintf(stderr
, "%s: Only 32-bit or 64-bit float format supported, not '%.4s'.\n",
488 program_name
, header
+ 12);
492 if (!strcmp(option_outfile_name
, "-")) {
493 option_outfile_name
= "stdout";
496 output_file
= option_outfile_name
;
497 atexit(cleanup_files
);
498 signal(SIGTERM
, exit_on_signal
);
499 signal(SIGINT
, exit_on_signal
);
501 signal(SIGHUP
, exit_on_signal
);
503 if (!(outfile
= fopen(output_file
, "wb"))) {
504 perror(option_outfile_name
);
509 if (!write_bytes("xof 0302bin 0064", 16))
513 while (parse_token());
515 if (ferror(outfile
) || ferror(infile
))
522 if (!option_inc_var_name
)
523 fatal_error( "variable name must be specified with -i or #pragma name\n" );
525 header_name
= get_basename( option_outfile_name
);
526 str_ptr
= header_name
;
531 *str_ptr
= toupper(*str_ptr
);
536 "/* File generated automatically from %s; do not edit */\n"
538 "#ifndef __WINE_%s\n"
539 "#define __WINE_%s\n"
541 "unsigned char %s[] = {",
542 infile_name
, header_name
, header_name
, option_inc_var_name
);
544 fprintf(outfile
, "\n};\n\n");
545 if (option_inc_size_name
)
546 fprintf(outfile
, "#define %s %u\n\n", option_inc_size_name
, output_pos
);
547 fprintf(outfile
, "#endif /* __WINE_%s */\n", header_name
);
551 else write_raw_bytes();
566 perror(option_outfile_name
);