Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/tools/make_xftmpl.c
4386 views
1
/*
2
* Binary encode X templates from text format.
3
*
4
* Copyright 2011 Dylan Smith
5
*
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.
10
*
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.
15
*
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
19
*/
20
21
#include "config.h"
22
23
#include <stdarg.h>
24
#include <stdio.h>
25
#include <stdlib.h>
26
27
#include "windef.h"
28
#include "guiddef.h"
29
#include "tools.h"
30
31
#define TOKEN_NAME 1
32
#define TOKEN_STRING 2
33
#define TOKEN_INTEGER 3
34
#define TOKEN_GUID 5
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
45
#define TOKEN_DOT 18
46
#define TOKEN_COMMA 19
47
#define TOKEN_SEMICOLON 20
48
#define TOKEN_TEMPLATE 31
49
#define TOKEN_WORD 40
50
#define TOKEN_DWORD 41
51
#define TOKEN_FLOAT 42
52
#define TOKEN_DOUBLE 43
53
#define TOKEN_CHAR 44
54
#define TOKEN_UCHAR 45
55
#define TOKEN_SWORD 46
56
#define TOKEN_SDWORD 47
57
#define TOKEN_VOID 48
58
#define TOKEN_LPSTR 49
59
#define TOKEN_UNICODE 50
60
#define TOKEN_CSTRING 51
61
#define TOKEN_ARRAY 52
62
63
struct keyword
64
{
65
const char *word;
66
WORD token;
67
};
68
69
static const struct keyword reserved_words[] = {
70
{"ARRAY", TOKEN_ARRAY},
71
{"CHAR", TOKEN_CHAR},
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},
82
{"VOID", TOKEN_VOID},
83
{"WORD", TOKEN_WORD}
84
};
85
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;
91
static FILE *infile;
92
static int line_no;
93
static const char *infile_name;
94
static FILE *outfile;
95
96
unsigned char *output_buffer = NULL;
97
size_t output_buffer_pos = 0;
98
size_t output_buffer_size = 0;
99
100
static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
101
102
static void fatal_error( const char *msg, ... )
103
{
104
va_list valist;
105
va_start( valist, msg );
106
if (infile_name)
107
{
108
fprintf( stderr, "%s:%d:", infile_name, line_no );
109
fprintf( stderr, " error: " );
110
}
111
else fprintf( stderr, "%s: error: ", program_name );
112
vfprintf( stderr, msg, valist );
113
va_end( valist );
114
exit( 1 );
115
}
116
117
118
static inline BOOL read_byte( char *byte )
119
{
120
int c = fgetc(infile);
121
*byte = c;
122
if (c == '\n') line_no++;
123
return c != EOF;
124
}
125
126
static inline BOOL unread_byte( char last_byte )
127
{
128
if (last_byte == '\n') line_no--;
129
return ungetc(last_byte, infile) != EOF;
130
}
131
132
static inline BOOL read_bytes( void *data, DWORD size )
133
{
134
return fread(data, size, 1, infile) > 0;
135
}
136
137
static BOOL write_c_hex_bytes(void)
138
{
139
UINT i;
140
for (i = 0; i < output_buffer_pos; i++)
141
{
142
if (i % 12 == 0)
143
fprintf(outfile, "\n ");
144
fprintf(outfile, " 0x%02x,", output_buffer[i]);
145
}
146
return TRUE;
147
}
148
149
static BOOL write_raw_bytes(void)
150
{
151
return fwrite(output_buffer, output_buffer_pos, 1, outfile) > 0;
152
}
153
154
static inline void put_float(float value)
155
{
156
DWORD val;
157
memcpy( &val, &value, sizeof(value) );
158
return put_dword( val );
159
}
160
161
static inline void put_guid(const GUID *guid)
162
{
163
put_dword( guid->Data1 );
164
put_word( guid->Data2 );
165
put_word( guid->Data3 );
166
put_data( guid->Data4, sizeof(guid->Data4) );
167
}
168
169
static int compare_names(const void *a, const void *b)
170
{
171
return strcasecmp(*(const char **)a, *(const char **)b);
172
}
173
174
static BOOL parse_keyword( const char *name )
175
{
176
const struct keyword *keyword;
177
178
keyword = bsearch(&name, reserved_words, ARRAY_SIZE(reserved_words),
179
sizeof(reserved_words[0]), compare_names);
180
if (!keyword)
181
return FALSE;
182
183
put_word(keyword->token);
184
return TRUE;
185
}
186
187
static void parse_guid(void)
188
{
189
char buf[39];
190
GUID guid;
191
DWORD tab[10];
192
BOOL ret;
193
static const char *guidfmt = "<%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X>";
194
195
buf[0] = '<';
196
if (!read_bytes(buf + 1, 37)) fatal_error( "truncated GUID\n" );
197
buf[38] = 0;
198
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 );
201
202
guid.Data2 = tab[0];
203
guid.Data3 = tab[1];
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];
212
213
put_word(TOKEN_GUID);
214
put_guid(&guid);
215
}
216
217
static void parse_name(void)
218
{
219
char c;
220
int len = 0;
221
char name[512];
222
223
while (read_byte(&c) && len < sizeof(name) &&
224
(isalnum(c) || c == '_' || c == '-'))
225
{
226
if (len + 1 < sizeof(name))
227
name[len++] = c;
228
}
229
unread_byte(c);
230
name[len] = 0;
231
232
if (!parse_keyword(name))
233
{
234
put_word(TOKEN_NAME);
235
put_dword(len);
236
put_data(name, len);
237
}
238
}
239
240
static void parse_number(void)
241
{
242
int len = 0;
243
char c;
244
char buffer[512];
245
BOOL dot = FALSE;
246
BOOL ret;
247
248
while (read_byte(&c) &&
249
((!len && c == '-') || (!dot && c == '.') || isdigit(c)))
250
{
251
if (len + 1 < sizeof(buffer))
252
buffer[len++] = c;
253
if (c == '.')
254
dot = TRUE;
255
}
256
unread_byte(c);
257
buffer[len] = 0;
258
259
if (dot) {
260
float value;
261
ret = sscanf(buffer, "%f", &value);
262
if (!ret) fatal_error( "invalid float token\n" );
263
put_word(TOKEN_FLOAT);
264
put_float(value);
265
} else {
266
int value;
267
ret = sscanf(buffer, "%d", &value);
268
if (!ret) fatal_error( "invalid integer token\n" );
269
put_word(TOKEN_INTEGER);
270
put_dword(value);
271
}
272
}
273
274
static BOOL parse_token(void)
275
{
276
char c;
277
int len;
278
char *tok, buffer[512];
279
280
if (!read_byte(&c))
281
return FALSE;
282
283
switch (c)
284
{
285
case '\n':
286
case '\r':
287
case ' ':
288
case '\t':
289
break;
290
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;
300
301
case '/':
302
if (!read_byte(&c) || c != '/')
303
fatal_error( "invalid single '/' comment token\n" );
304
while (read_byte(&c) && c != '\n');
305
return c == '\n';
306
307
case '#':
308
len = 0;
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" );
312
buffer[len] = 0;
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" );
318
if (!tok) break;
319
if (!strcmp( tok, "name" ))
320
{
321
tok = strtok( NULL, " \t" );
322
if (tok && !option_inc_var_name) option_inc_var_name = xstrdup( tok );
323
}
324
else if (!strcmp( tok, "size" ))
325
{
326
tok = strtok( NULL, " \t" );
327
if (tok && !option_inc_size_name) option_inc_size_name = xstrdup( tok );
328
}
329
break;
330
331
case '<':
332
parse_guid();
333
break;
334
335
case '"':
336
len = 0;
337
338
/* FIXME: Handle '\' (e.g. "valid\"string") */
339
while (read_byte(&c) && c != '"') {
340
if (len + 1 < sizeof(buffer))
341
buffer[len++] = c;
342
}
343
if (c != '"') fatal_error( "unterminated string\n" );
344
put_word(TOKEN_STRING);
345
put_dword(len);
346
put_data(buffer, len);
347
break;
348
349
default:
350
unread_byte(c);
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 );
354
}
355
356
return TRUE;
357
}
358
359
static const char *output_file;
360
361
static void cleanup_files(void)
362
{
363
if (output_file) unlink(output_file);
364
}
365
366
static void exit_on_signal( int sig )
367
{
368
exit(1); /* this will call the atexit functions */
369
}
370
371
static void usage(void)
372
{
373
fprintf(stderr, "Usage: %s [OPTIONS] INFILE\n"
374
"Options:\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",
379
program_name);
380
}
381
382
static void option_callback( int optc, char *optarg )
383
{
384
switch (optc)
385
{
386
case 'h':
387
usage();
388
exit(0);
389
case 'H':
390
option_header = TRUE;
391
break;
392
case 'i':
393
option_header = TRUE;
394
option_inc_var_name = xstrdup(optarg);
395
break;
396
case 'o':
397
option_outfile_name = xstrdup(optarg);
398
break;
399
case 's':
400
option_inc_size_name = xstrdup(optarg);
401
break;
402
case '?':
403
fprintf( stderr, "%s: %s\n", program_name, optarg );
404
exit(1);
405
}
406
}
407
408
int main(int argc, char **argv)
409
{
410
char header[16];
411
struct strarray args;
412
char *header_name = NULL;
413
414
program_name = argv[0];
415
416
args = parse_options(argc, argv, "hHi:o:s:", NULL, 0, option_callback );
417
if (!args.count)
418
{
419
usage();
420
return 1;
421
}
422
infile_name = args.str[0];
423
424
infile = stdin;
425
outfile = NULL;
426
427
if (!strcmp(infile_name, "-")) {
428
infile_name = "stdin";
429
} else if (!(infile = fopen(infile_name, "rb"))) {
430
perror(infile_name);
431
goto error;
432
}
433
434
if (!read_bytes(header, sizeof(header))) {
435
fprintf(stderr, "%s: Failed to read file header\n", program_name);
436
goto error;
437
}
438
if (strncmp(header, "xof ", 4))
439
{
440
fprintf(stderr, "%s: Invalid magic value '%.4s'\n", program_name, header);
441
goto error;
442
}
443
if (strncmp(header + 4, "0302", 4) && strncmp(header + 4, "0303", 4))
444
{
445
fprintf(stderr, "%s: Unsupported version '%.4s'\n", program_name, header + 4);
446
goto error;
447
}
448
if (strncmp(header + 8, "txt ", 4))
449
{
450
fprintf(stderr, "%s: Only support conversion from text encoded X files.",
451
program_name);
452
goto error;
453
}
454
if (strncmp(header + 12, "0032", 4) && strncmp(header + 12, "0064", 4))
455
{
456
fprintf(stderr, "%s: Only 32-bit or 64-bit float format supported, not '%.4s'.\n",
457
program_name, header + 12);
458
goto error;
459
}
460
461
init_output_buffer();
462
put_data("xof 0302bin 0064", 16);
463
464
line_no = 1;
465
while (parse_token());
466
467
if (ferror(infile))
468
{
469
perror(infile_name);
470
return 1;
471
}
472
fclose(infile);
473
474
if (!strcmp(option_outfile_name, "-")) {
475
option_outfile_name = "stdout";
476
outfile = stdout;
477
} else {
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);
483
goto error;
484
}
485
}
486
487
if (ferror(outfile))
488
goto error;
489
490
if (option_header)
491
{
492
char *str_ptr;
493
494
if (!option_inc_var_name)
495
fatal_error( "variable name must be specified with -i or #pragma name\n" );
496
497
header_name = get_basename( option_outfile_name );
498
str_ptr = header_name;
499
while (*str_ptr) {
500
if (*str_ptr == '.')
501
*str_ptr = '_';
502
else
503
*str_ptr = toupper(*str_ptr);
504
str_ptr++;
505
}
506
507
fprintf(outfile,
508
"/* File generated automatically from %s; do not edit */\n"
509
"\n"
510
"#ifndef __WINE_%s\n"
511
"#define __WINE_%s\n"
512
"\n"
513
"unsigned char %s[] = {",
514
infile_name, header_name, header_name, option_inc_var_name);
515
write_c_hex_bytes();
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);
520
if (ferror(outfile))
521
goto error;
522
}
523
else write_raw_bytes();
524
525
fclose(outfile);
526
output_file = NULL;
527
528
return 0;
529
error:
530
if (outfile) {
531
if (ferror(outfile))
532
perror(option_outfile_name);
533
fclose(outfile);
534
}
535
return 1;
536
}
537
538