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
;
97 unsigned char *output_buffer
= NULL
;
98 size_t output_buffer_pos
= 0;
99 size_t output_buffer_size
= 0;
101 static void fatal_error( const char *msg
, ... ) __attribute__ ((__format__ (__printf__
, 1, 2)));
103 static void fatal_error( const char *msg
, ... )
106 va_start( valist
, msg
);
109 fprintf( stderr
, "%s:%d:", infile_name
, line_no
);
110 fprintf( stderr
, " error: " );
112 else fprintf( stderr
, "%s: error: ", program_name
);
113 vfprintf( stderr
, msg
, valist
);
119 static inline BOOL
read_byte( char *byte
)
121 int c
= fgetc(infile
);
123 if (c
== '\n') line_no
++;
127 static inline BOOL
unread_byte( char last_byte
)
129 if (last_byte
== '\n') line_no
--;
130 return ungetc(last_byte
, infile
) != EOF
;
133 static inline BOOL
read_bytes( void *data
, DWORD size
)
135 return fread(data
, size
, 1, infile
) > 0;
138 static BOOL
write_c_hex_bytes(void)
141 for (i
= 0; i
< output_buffer_pos
; i
++)
144 fprintf(outfile
, "\n ");
145 fprintf(outfile
, " 0x%02x,", output_buffer
[i
]);
150 static BOOL
write_raw_bytes(void)
152 return fwrite(output_buffer
, output_buffer_pos
, 1, outfile
) > 0;
155 static inline void put_float(float value
)
158 memcpy( &val
, &value
, sizeof(value
) );
159 return put_dword( val
);
162 static inline void put_guid(const GUID
*guid
)
164 put_dword( guid
->Data1
);
165 put_word( guid
->Data2
);
166 put_word( guid
->Data3
);
167 put_data( guid
->Data4
, sizeof(guid
->Data4
) );
170 static int compare_names(const void *a
, const void *b
)
172 return strcasecmp(*(const char **)a
, *(const char **)b
);
175 static BOOL
parse_keyword( const char *name
)
177 const struct keyword
*keyword
;
179 keyword
= bsearch(&name
, reserved_words
, ARRAY_SIZE(reserved_words
),
180 sizeof(reserved_words
[0]), compare_names
);
184 put_word(keyword
->token
);
188 static void parse_guid(void)
194 static const char *guidfmt
= "<%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X>";
197 if (!read_bytes(buf
+ 1, 37)) fatal_error( "truncated GUID\n" );
200 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);
201 if (ret
!= 11) fatal_error( "invalid GUID '%s'\n", buf
);
205 guid
.Data4
[0] = tab
[2];
206 guid
.Data4
[1] = tab
[3];
207 guid
.Data4
[2] = tab
[4];
208 guid
.Data4
[3] = tab
[5];
209 guid
.Data4
[4] = tab
[6];
210 guid
.Data4
[5] = tab
[7];
211 guid
.Data4
[6] = tab
[8];
212 guid
.Data4
[7] = tab
[9];
214 put_word(TOKEN_GUID
);
218 static void parse_name(void)
224 while (read_byte(&c
) && len
< sizeof(name
) &&
225 (isalnum(c
) || c
== '_' || c
== '-'))
227 if (len
+ 1 < sizeof(name
))
233 if (!parse_keyword(name
))
235 put_word(TOKEN_NAME
);
241 static void parse_number(void)
249 while (read_byte(&c
) &&
250 ((!len
&& c
== '-') || (!dot
&& c
== '.') || isdigit(c
)))
252 if (len
+ 1 < sizeof(buffer
))
262 ret
= sscanf(buffer
, "%f", &value
);
263 if (!ret
) fatal_error( "invalid float token\n" );
264 put_word(TOKEN_FLOAT
);
268 ret
= sscanf(buffer
, "%d", &value
);
269 if (!ret
) fatal_error( "invalid integer token\n" );
270 put_word(TOKEN_INTEGER
);
275 static BOOL
parse_token(void)
279 char *tok
, buffer
[512];
292 case '{': put_word(TOKEN_OBRACE
); break;
293 case '}': put_word(TOKEN_CBRACE
); break;
294 case '[': put_word(TOKEN_OBRACKET
); break;
295 case ']': put_word(TOKEN_CBRACKET
); break;
296 case '(': put_word(TOKEN_OPAREN
); break;
297 case ')': put_word(TOKEN_CPAREN
); break;
298 case ',': put_word(TOKEN_COMMA
); break;
299 case ';': put_word(TOKEN_SEMICOLON
); break;
300 case '.': put_word(TOKEN_DOT
); break;
303 if (!read_byte(&c
) || c
!= '/')
304 fatal_error( "invalid single '/' comment token\n" );
305 while (read_byte(&c
) && c
!= '\n');
310 while (read_byte(&c
) && c
!= '\n')
311 if (len
+ 1 < sizeof(buffer
)) buffer
[len
++] = c
;
312 if (c
!= '\n') fatal_error( "line too long\n" );
314 tok
= strtok( buffer
, " \t" );
315 if (!tok
|| strcmp( tok
, "pragma" )) break;
316 tok
= strtok( NULL
, " \t" );
317 if (!tok
|| strcmp( tok
, "xftmpl" )) break;
318 tok
= strtok( NULL
, " \t" );
320 if (!strcmp( tok
, "name" ))
322 tok
= strtok( NULL
, " \t" );
323 if (tok
&& !option_inc_var_name
) option_inc_var_name
= xstrdup( tok
);
325 else if (!strcmp( tok
, "size" ))
327 tok
= strtok( NULL
, " \t" );
328 if (tok
&& !option_inc_size_name
) option_inc_size_name
= xstrdup( tok
);
339 /* FIXME: Handle '\' (e.g. "valid\"string") */
340 while (read_byte(&c
) && c
!= '"') {
341 if (len
+ 1 < sizeof(buffer
))
344 if (c
!= '"') fatal_error( "unterminated string\n" );
345 put_word(TOKEN_STRING
);
347 put_data(buffer
, len
);
352 if (isdigit(c
) || c
== '-') parse_number();
353 else if (isalpha(c
) || c
== '_') parse_name();
354 else fatal_error( "invalid character '%c' to start token\n", c
);
360 static const char *output_file
;
362 static void cleanup_files(void)
364 if (output_file
) unlink(output_file
);
367 static void exit_on_signal( int sig
)
369 exit(1); /* this will call the atexit functions */
372 static void usage(void)
374 fprintf(stderr
, "Usage: %s [OPTIONS] INFILE\n"
376 " -H Output to a c header file instead of a binary file\n"
377 " -i NAME Output to a c header file, data in variable NAME\n"
378 " -s NAME In a c header file, define NAME to be the data size\n"
379 " -o FILE Write output to FILE\n",
383 static void option_callback( int optc
, char *optarg
)
391 option_header
= TRUE
;
394 option_header
= TRUE
;
395 option_inc_var_name
= xstrdup(optarg
);
398 option_outfile_name
= xstrdup(optarg
);
401 option_inc_size_name
= xstrdup(optarg
);
404 fprintf( stderr
, "%s: %s\n", program_name
, optarg
);
409 int main(int argc
, char **argv
)
412 struct strarray args
;
413 char *header_name
= NULL
;
415 program_name
= argv
[0];
417 args
= parse_options(argc
, argv
, "hHi:o:s:", NULL
, 0, option_callback
);
423 infile_name
= args
.str
[0];
428 if (!strcmp(infile_name
, "-")) {
429 infile_name
= "stdin";
430 } else if (!(infile
= fopen(infile_name
, "rb"))) {
435 if (!read_bytes(header
, sizeof(header
))) {
436 fprintf(stderr
, "%s: Failed to read file header\n", program_name
);
439 if (strncmp(header
, "xof ", 4))
441 fprintf(stderr
, "%s: Invalid magic value '%.4s'\n", program_name
, header
);
444 if (strncmp(header
+ 4, "0302", 4) && strncmp(header
+ 4, "0303", 4))
446 fprintf(stderr
, "%s: Unsupported version '%.4s'\n", program_name
, header
+ 4);
449 if (strncmp(header
+ 8, "txt ", 4))
451 fprintf(stderr
, "%s: Only support conversion from text encoded X files.",
455 if (strncmp(header
+ 12, "0032", 4) && strncmp(header
+ 12, "0064", 4))
457 fprintf(stderr
, "%s: Only 32-bit or 64-bit float format supported, not '%.4s'.\n",
458 program_name
, header
+ 12);
462 init_output_buffer();
463 put_data("xof 0302bin 0064", 16);
466 while (parse_token());
475 if (!strcmp(option_outfile_name
, "-")) {
476 option_outfile_name
= "stdout";
479 output_file
= option_outfile_name
;
480 atexit(cleanup_files
);
481 signal(SIGTERM
, exit_on_signal
);
482 signal(SIGINT
, exit_on_signal
);
484 signal(SIGHUP
, exit_on_signal
);
486 if (!(outfile
= fopen(output_file
, "wb"))) {
487 perror(option_outfile_name
);
499 if (!option_inc_var_name
)
500 fatal_error( "variable name must be specified with -i or #pragma name\n" );
502 header_name
= get_basename( option_outfile_name
);
503 str_ptr
= header_name
;
508 *str_ptr
= toupper(*str_ptr
);
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 fprintf(outfile
, "\n};\n\n");
522 if (option_inc_size_name
)
523 fprintf(outfile
, "#define %s %u\n\n", option_inc_size_name
, (unsigned int)output_buffer_pos
);
524 fprintf(outfile
, "#endif /* __WINE_%s */\n", header_name
);
528 else write_raw_bytes();
537 perror(option_outfile_name
);