secur32/tests: Add some tests for "Negotiate" LSA package.
[wine.git] / tools / make_xftmpl.c
blob087020cdb99ba0f5bc9fa4e40229fc56cbbdc00b
1 /*
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
21 #include "config.h"
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
27 #include "windef.h"
28 #include "guiddef.h"
29 #include "tools.h"
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
63 struct keyword
65 const char *word;
66 WORD token;
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}
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;
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, ... )
104 va_list valist;
105 va_start( valist, msg );
106 if (infile_name)
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 );
113 va_end( valist );
114 exit( 1 );
118 static inline BOOL read_byte( char *byte )
120 int c = fgetc(infile);
121 *byte = c;
122 if (c == '\n') line_no++;
123 return c != EOF;
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)
139 UINT i;
140 for (i = 0; i < output_buffer_pos; i++)
142 if (i % 12 == 0)
143 fprintf(outfile, "\n ");
144 fprintf(outfile, " 0x%02x,", output_buffer[i]);
146 return TRUE;
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)
156 DWORD val;
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);
180 if (!keyword)
181 return FALSE;
183 put_word(keyword->token);
184 return TRUE;
187 static void parse_guid(void)
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>";
195 buf[0] = '<';
196 if (!read_bytes(buf + 1, 37)) fatal_error( "truncated GUID\n" );
197 buf[38] = 0;
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 );
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];
213 put_word(TOKEN_GUID);
214 put_guid(&guid);
217 static void parse_name(void)
219 char c;
220 int len = 0;
221 char name[512];
223 while (read_byte(&c) && len < sizeof(name) &&
224 (isalnum(c) || c == '_' || c == '-'))
226 if (len + 1 < sizeof(name))
227 name[len++] = c;
229 unread_byte(c);
230 name[len] = 0;
232 if (!parse_keyword(name))
234 put_word(TOKEN_NAME);
235 put_dword(len);
236 put_data(name, len);
240 static void parse_number(void)
242 int len = 0;
243 char c;
244 char buffer[512];
245 BOOL dot = FALSE;
246 BOOL ret;
248 while (read_byte(&c) &&
249 ((!len && c == '-') || (!dot && c == '.') || isdigit(c)))
251 if (len + 1 < sizeof(buffer))
252 buffer[len++] = c;
253 if (c == '.')
254 dot = TRUE;
256 unread_byte(c);
257 buffer[len] = 0;
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);
274 static BOOL parse_token(void)
276 char c;
277 int len;
278 char *tok, buffer[512];
280 if (!read_byte(&c))
281 return FALSE;
283 switch (c)
285 case '\n':
286 case '\r':
287 case ' ':
288 case '\t':
289 break;
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;
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';
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" ))
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 );
329 break;
331 case '<':
332 parse_guid();
333 break;
335 case '"':
336 len = 0;
338 /* FIXME: Handle '\' (e.g. "valid\"string") */
339 while (read_byte(&c) && c != '"') {
340 if (len + 1 < sizeof(buffer))
341 buffer[len++] = c;
343 if (c != '"') fatal_error( "unterminated string\n" );
344 put_word(TOKEN_STRING);
345 put_dword(len);
346 put_data(buffer, len);
347 break;
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 );
356 return TRUE;
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"
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);
382 static void option_callback( int optc, char *optarg )
384 switch (optc)
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);
408 int main(int argc, char **argv)
410 char header[16];
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 );
417 if (!args.count)
419 usage();
420 return 1;
422 infile_name = args.str[0];
424 infile = stdin;
425 outfile = NULL;
427 if (!strcmp(infile_name, "-")) {
428 infile_name = "stdin";
429 } else if (!(infile = fopen(infile_name, "rb"))) {
430 perror(infile_name);
431 goto error;
434 if (!read_bytes(header, sizeof(header))) {
435 fprintf(stderr, "%s: Failed to read file header\n", program_name);
436 goto error;
438 if (strncmp(header, "xof ", 4))
440 fprintf(stderr, "%s: Invalid magic value '%.4s'\n", program_name, header);
441 goto error;
443 if (strncmp(header + 4, "0302", 4) && strncmp(header + 4, "0303", 4))
445 fprintf(stderr, "%s: Unsupported version '%.4s'\n", program_name, header + 4);
446 goto error;
448 if (strncmp(header + 8, "txt ", 4))
450 fprintf(stderr, "%s: Only support conversion from text encoded X files.",
451 program_name);
452 goto error;
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);
458 goto error;
461 init_output_buffer();
462 put_data("xof 0302bin 0064", 16);
464 line_no = 1;
465 while (parse_token());
467 if (ferror(infile))
469 perror(infile_name);
470 return 1;
472 fclose(infile);
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;
487 if (ferror(outfile))
488 goto error;
490 if (option_header)
492 char *str_ptr;
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;
499 while (*str_ptr) {
500 if (*str_ptr == '.')
501 *str_ptr = '_';
502 else
503 *str_ptr = toupper(*str_ptr);
504 str_ptr++;
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;
523 else write_raw_bytes();
525 fclose(outfile);
526 output_file = NULL;
528 return 0;
529 error:
530 if (outfile) {
531 if (ferror(outfile))
532 perror(option_outfile_name);
533 fclose(outfile);
535 return 1;