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
32 #define TOKEN_STRING 2
33 #define TOKEN_INTEGER 3
35 #define TOKEN_INTEGER_LIST 6
36 #define TOKEN_FLOAT_LIST 7
37 #define TOKEN_OBRACE 10
38 #define TOKEN_CBRACE 11
39 #define TOKEN_OPAREN 12
40 #define TOKEN_CPAREN 13
41 #define TOKEN_OBRACKET 14
42 #define TOKEN_CBRACKET 15
43 #define TOKEN_OANGLE 16
44 #define TOKEN_CANGLE 17
46 #define TOKEN_COMMA 19
47 #define TOKEN_SEMICOLON 20
48 #define TOKEN_TEMPLATE 31
50 #define TOKEN_DWORD 41
51 #define TOKEN_FLOAT 42
52 #define TOKEN_DOUBLE 43
54 #define TOKEN_UCHAR 45
55 #define TOKEN_SWORD 46
56 #define TOKEN_SDWORD 47
58 #define TOKEN_LPSTR 49
59 #define TOKEN_UNICODE 50
60 #define TOKEN_CSTRING 51
61 #define TOKEN_ARRAY 52
69 static const struct keyword reserved_words
[] = {
70 {"ARRAY", TOKEN_ARRAY
},
72 {"CSTRING", TOKEN_CSTRING
},
73 {"DOUBLE", TOKEN_DOUBLE
},
74 {"DWORD", TOKEN_DWORD
},
75 {"FLOAT", TOKEN_FLOAT
},
76 {"SDWORD", TOKEN_SDWORD
},
77 {"STRING", TOKEN_LPSTR
},
78 {"SWORD", TOKEN_SWORD
},
79 {"TEMPLATE", TOKEN_TEMPLATE
},
80 {"UCHAR", TOKEN_UCHAR
},
81 {"UNICODE", TOKEN_UNICODE
},
86 static BOOL option_header
;
87 static char *option_inc_var_name
= NULL
;
88 static char *option_inc_size_name
= NULL
;
89 static const char *option_outfile_name
= "-";
90 static char *program_name
;
93 static const char *infile_name
;
96 unsigned char *output_buffer
= NULL
;
97 size_t output_buffer_pos
= 0;
98 size_t output_buffer_size
= 0;
100 static void fatal_error( const char *msg
, ... ) __attribute__ ((__format__ (__printf__
, 1, 2)));
102 static void fatal_error( const char *msg
, ... )
105 va_start( valist
, msg
);
108 fprintf( stderr
, "%s:%d:", infile_name
, line_no
);
109 fprintf( stderr
, " error: " );
111 else fprintf( stderr
, "%s: error: ", program_name
);
112 vfprintf( stderr
, msg
, valist
);
118 static inline BOOL
read_byte( char *byte
)
120 int c
= fgetc(infile
);
122 if (c
== '\n') line_no
++;
126 static inline BOOL
unread_byte( char last_byte
)
128 if (last_byte
== '\n') line_no
--;
129 return ungetc(last_byte
, infile
) != EOF
;
132 static inline BOOL
read_bytes( void *data
, DWORD size
)
134 return fread(data
, size
, 1, infile
) > 0;
137 static BOOL
write_c_hex_bytes(void)
140 for (i
= 0; i
< output_buffer_pos
; i
++)
143 fprintf(outfile
, "\n ");
144 fprintf(outfile
, " 0x%02x,", output_buffer
[i
]);
149 static BOOL
write_raw_bytes(void)
151 return fwrite(output_buffer
, output_buffer_pos
, 1, outfile
) > 0;
154 static inline void put_float(float value
)
157 memcpy( &val
, &value
, sizeof(value
) );
158 return put_dword( val
);
161 static inline void put_guid(const GUID
*guid
)
163 put_dword( guid
->Data1
);
164 put_word( guid
->Data2
);
165 put_word( guid
->Data3
);
166 put_data( guid
->Data4
, sizeof(guid
->Data4
) );
169 static int compare_names(const void *a
, const void *b
)
171 return strcasecmp(*(const char **)a
, *(const char **)b
);
174 static BOOL
parse_keyword( const char *name
)
176 const struct keyword
*keyword
;
178 keyword
= bsearch(&name
, reserved_words
, ARRAY_SIZE(reserved_words
),
179 sizeof(reserved_words
[0]), compare_names
);
183 put_word(keyword
->token
);
187 static void parse_guid(void)
193 static const char *guidfmt
= "<%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X>";
196 if (!read_bytes(buf
+ 1, 37)) fatal_error( "truncated GUID\n" );
199 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);
200 if (ret
!= 11) fatal_error( "invalid GUID '%s'\n", buf
);
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 put_word(TOKEN_GUID
);
217 static void parse_name(void)
223 while (read_byte(&c
) && len
< sizeof(name
) &&
224 (isalnum(c
) || c
== '_' || c
== '-'))
226 if (len
+ 1 < sizeof(name
))
232 if (!parse_keyword(name
))
234 put_word(TOKEN_NAME
);
240 static void parse_number(void)
248 while (read_byte(&c
) &&
249 ((!len
&& c
== '-') || (!dot
&& c
== '.') || isdigit(c
)))
251 if (len
+ 1 < sizeof(buffer
))
261 ret
= sscanf(buffer
, "%f", &value
);
262 if (!ret
) fatal_error( "invalid float token\n" );
263 put_word(TOKEN_FLOAT
);
267 ret
= sscanf(buffer
, "%d", &value
);
268 if (!ret
) fatal_error( "invalid integer token\n" );
269 put_word(TOKEN_INTEGER
);
274 static BOOL
parse_token(void)
278 char *tok
, buffer
[512];
291 case '{': put_word(TOKEN_OBRACE
); break;
292 case '}': put_word(TOKEN_CBRACE
); break;
293 case '[': put_word(TOKEN_OBRACKET
); break;
294 case ']': put_word(TOKEN_CBRACKET
); break;
295 case '(': put_word(TOKEN_OPAREN
); break;
296 case ')': put_word(TOKEN_CPAREN
); break;
297 case ',': put_word(TOKEN_COMMA
); break;
298 case ';': put_word(TOKEN_SEMICOLON
); break;
299 case '.': put_word(TOKEN_DOT
); break;
302 if (!read_byte(&c
) || c
!= '/')
303 fatal_error( "invalid single '/' comment token\n" );
304 while (read_byte(&c
) && c
!= '\n');
309 while (read_byte(&c
) && c
!= '\n')
310 if (len
+ 1 < sizeof(buffer
)) buffer
[len
++] = c
;
311 if (c
!= '\n') fatal_error( "line too long\n" );
313 tok
= strtok( buffer
, " \t" );
314 if (!tok
|| strcmp( tok
, "pragma" )) break;
315 tok
= strtok( NULL
, " \t" );
316 if (!tok
|| strcmp( tok
, "xftmpl" )) break;
317 tok
= strtok( NULL
, " \t" );
319 if (!strcmp( tok
, "name" ))
321 tok
= strtok( NULL
, " \t" );
322 if (tok
&& !option_inc_var_name
) option_inc_var_name
= xstrdup( tok
);
324 else if (!strcmp( tok
, "size" ))
326 tok
= strtok( NULL
, " \t" );
327 if (tok
&& !option_inc_size_name
) option_inc_size_name
= xstrdup( tok
);
338 /* FIXME: Handle '\' (e.g. "valid\"string") */
339 while (read_byte(&c
) && c
!= '"') {
340 if (len
+ 1 < sizeof(buffer
))
343 if (c
!= '"') fatal_error( "unterminated string\n" );
344 put_word(TOKEN_STRING
);
346 put_data(buffer
, len
);
351 if (isdigit(c
) || c
== '-') parse_number();
352 else if (isalpha(c
) || c
== '_') parse_name();
353 else fatal_error( "invalid character '%c' to start token\n", c
);
359 static const char *output_file
;
361 static void cleanup_files(void)
363 if (output_file
) unlink(output_file
);
366 static void exit_on_signal( int sig
)
368 exit(1); /* this will call the atexit functions */
371 static void usage(void)
373 fprintf(stderr
, "Usage: %s [OPTIONS] INFILE\n"
375 " -H Output to a c header file instead of a binary file\n"
376 " -i NAME Output to a c header file, data in variable NAME\n"
377 " -s NAME In a c header file, define NAME to be the data size\n"
378 " -o FILE Write output to FILE\n",
382 static void option_callback( int optc
, char *optarg
)
390 option_header
= TRUE
;
393 option_header
= TRUE
;
394 option_inc_var_name
= xstrdup(optarg
);
397 option_outfile_name
= xstrdup(optarg
);
400 option_inc_size_name
= xstrdup(optarg
);
403 fprintf( stderr
, "%s: %s\n", program_name
, optarg
);
408 int main(int argc
, char **argv
)
411 struct strarray args
;
412 char *header_name
= NULL
;
414 program_name
= argv
[0];
416 args
= parse_options(argc
, argv
, "hHi:o:s:", NULL
, 0, option_callback
);
422 infile_name
= args
.str
[0];
427 if (!strcmp(infile_name
, "-")) {
428 infile_name
= "stdin";
429 } else if (!(infile
= fopen(infile_name
, "rb"))) {
434 if (!read_bytes(header
, sizeof(header
))) {
435 fprintf(stderr
, "%s: Failed to read file header\n", program_name
);
438 if (strncmp(header
, "xof ", 4))
440 fprintf(stderr
, "%s: Invalid magic value '%.4s'\n", program_name
, header
);
443 if (strncmp(header
+ 4, "0302", 4) && strncmp(header
+ 4, "0303", 4))
445 fprintf(stderr
, "%s: Unsupported version '%.4s'\n", program_name
, header
+ 4);
448 if (strncmp(header
+ 8, "txt ", 4))
450 fprintf(stderr
, "%s: Only support conversion from text encoded X files.",
454 if (strncmp(header
+ 12, "0032", 4) && strncmp(header
+ 12, "0064", 4))
456 fprintf(stderr
, "%s: Only 32-bit or 64-bit float format supported, not '%.4s'.\n",
457 program_name
, header
+ 12);
461 init_output_buffer();
462 put_data("xof 0302bin 0064", 16);
465 while (parse_token());
474 if (!strcmp(option_outfile_name
, "-")) {
475 option_outfile_name
= "stdout";
478 output_file
= option_outfile_name
;
479 atexit(cleanup_files
);
480 init_signals( exit_on_signal
);
481 if (!(outfile
= fopen(output_file
, "wb"))) {
482 perror(option_outfile_name
);
494 if (!option_inc_var_name
)
495 fatal_error( "variable name must be specified with -i or #pragma name\n" );
497 header_name
= get_basename( option_outfile_name
);
498 str_ptr
= header_name
;
503 *str_ptr
= toupper(*str_ptr
);
508 "/* File generated automatically from %s; do not edit */\n"
510 "#ifndef __WINE_%s\n"
511 "#define __WINE_%s\n"
513 "unsigned char %s[] = {",
514 infile_name
, header_name
, header_name
, option_inc_var_name
);
516 fprintf(outfile
, "\n};\n\n");
517 if (option_inc_size_name
)
518 fprintf(outfile
, "#define %s %u\n\n", option_inc_size_name
, (unsigned int)output_buffer_pos
);
519 fprintf(outfile
, "#endif /* __WINE_%s */\n", header_name
);
523 else write_raw_bytes();
532 perror(option_outfile_name
);