make_xftmpl.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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. #include "config.h"
  21. #include "wine/port.h"
  22. #include <signal.h>
  23. #include <stdarg.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #ifdef HAVE_GETOPT_H
  27. # include <getopt.h>
  28. #endif
  29. #ifdef HAVE_UNISTD_H
  30. # include <unistd.h>
  31. #endif
  32. #include "windef.h"
  33. #include "guiddef.h"
  34. #define TOKEN_NAME 1
  35. #define TOKEN_STRING 2
  36. #define TOKEN_INTEGER 3
  37. #define TOKEN_GUID 5
  38. #define TOKEN_INTEGER_LIST 6
  39. #define TOKEN_FLOAT_LIST 7
  40. #define TOKEN_OBRACE 10
  41. #define TOKEN_CBRACE 11
  42. #define TOKEN_OPAREN 12
  43. #define TOKEN_CPAREN 13
  44. #define TOKEN_OBRACKET 14
  45. #define TOKEN_CBRACKET 15
  46. #define TOKEN_OANGLE 16
  47. #define TOKEN_CANGLE 17
  48. #define TOKEN_DOT 18
  49. #define TOKEN_COMMA 19
  50. #define TOKEN_SEMICOLON 20
  51. #define TOKEN_TEMPLATE 31
  52. #define TOKEN_WORD 40
  53. #define TOKEN_DWORD 41
  54. #define TOKEN_FLOAT 42
  55. #define TOKEN_DOUBLE 43
  56. #define TOKEN_CHAR 44
  57. #define TOKEN_UCHAR 45
  58. #define TOKEN_SWORD 46
  59. #define TOKEN_SDWORD 47
  60. #define TOKEN_VOID 48
  61. #define TOKEN_LPSTR 49
  62. #define TOKEN_UNICODE 50
  63. #define TOKEN_CSTRING 51
  64. #define TOKEN_ARRAY 52
  65. struct keyword
  66. {
  67. const char *word;
  68. WORD token;
  69. };
  70. static const struct keyword reserved_words[] = {
  71. {"ARRAY", TOKEN_ARRAY},
  72. {"CHAR", TOKEN_CHAR},
  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},
  83. {"VOID", TOKEN_VOID},
  84. {"WORD", TOKEN_WORD}
  85. };
  86. extern int getopt(int argc, char *const *argv, const char *optstring);
  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;
  92. static FILE *infile;
  93. static int line_no;
  94. static const char *infile_name;
  95. static FILE *outfile;
  96. static BYTE *output_data;
  97. static UINT output_pos, output_size;
  98. #ifndef __GNUC__
  99. #define __attribute__(x)
  100. #endif
  101. static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
  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. static inline BOOL read_byte( char *byte )
  117. {
  118. int c = fgetc(infile);
  119. *byte = c;
  120. if (c == '\n') line_no++;
  121. return c != EOF;
  122. }
  123. static inline BOOL unread_byte( char last_byte )
  124. {
  125. if (last_byte == '\n') line_no--;
  126. return ungetc(last_byte, infile) != EOF;
  127. }
  128. static inline BOOL read_bytes( void *data, DWORD size )
  129. {
  130. return fread(data, size, 1, infile) > 0;
  131. }
  132. static BOOL write_c_hex_bytes(void)
  133. {
  134. UINT i;
  135. for (i = 0; i < output_pos; i++)
  136. {
  137. if (i % 12 == 0)
  138. fprintf(outfile, "\n ");
  139. fprintf(outfile, " 0x%02x,", output_data[i]);
  140. }
  141. return TRUE;
  142. }
  143. static BOOL write_raw_bytes(void)
  144. {
  145. return fwrite(output_data, output_pos, 1, outfile) > 0;
  146. }
  147. static inline BOOL write_bytes(const void *data, DWORD size)
  148. {
  149. if (output_pos + size > output_size)
  150. {
  151. output_size = max( output_size * 2, size );
  152. output_data = realloc( output_data, output_size );
  153. if (!output_data) return FALSE;
  154. }
  155. memcpy( output_data + output_pos, data, size );
  156. output_pos += size;
  157. return TRUE;
  158. }
  159. static inline BOOL write_byte(BYTE value)
  160. {
  161. return write_bytes( &value, sizeof(value) );
  162. }
  163. static inline BOOL write_word(WORD value)
  164. {
  165. return write_byte( value ) &&
  166. write_byte( value >> 8 );
  167. }
  168. static inline BOOL write_dword(DWORD value)
  169. {
  170. return write_word( value ) &&
  171. write_word( value >> 16 );
  172. }
  173. static inline BOOL write_float(float value)
  174. {
  175. DWORD val;
  176. memcpy( &val, &value, sizeof(value) );
  177. return write_dword( val );
  178. }
  179. static inline BOOL write_guid(const GUID *guid)
  180. {
  181. return write_dword( guid->Data1 ) &&
  182. write_word( guid->Data2 ) &&
  183. write_word( guid->Data3 ) &&
  184. write_bytes( guid->Data4, sizeof(guid->Data4) );
  185. }
  186. static int compare_names(const void *a, const void *b)
  187. {
  188. return strcasecmp(*(const char **)a, *(const char **)b);
  189. }
  190. static BOOL parse_keyword( const char *name )
  191. {
  192. const struct keyword *keyword;
  193. keyword = bsearch(&name, reserved_words, ARRAY_SIZE(reserved_words),
  194. sizeof(reserved_words[0]), compare_names);
  195. if (!keyword)
  196. return FALSE;
  197. return write_word(keyword->token);
  198. }
  199. static BOOL parse_guid(void)
  200. {
  201. char buf[39];
  202. GUID guid;
  203. DWORD tab[10];
  204. BOOL ret;
  205. static const char *guidfmt = "<%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X>";
  206. buf[0] = '<';
  207. if (!read_bytes(buf + 1, 37)) fatal_error( "truncated GUID\n" );
  208. buf[38] = 0;
  209. 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);
  210. if (ret != 11) fatal_error( "invalid GUID '%s'\n", buf );
  211. guid.Data2 = tab[0];
  212. guid.Data3 = tab[1];
  213. guid.Data4[0] = tab[2];
  214. guid.Data4[1] = tab[3];
  215. guid.Data4[2] = tab[4];
  216. guid.Data4[3] = tab[5];
  217. guid.Data4[4] = tab[6];
  218. guid.Data4[5] = tab[7];
  219. guid.Data4[6] = tab[8];
  220. guid.Data4[7] = tab[9];
  221. return write_word(TOKEN_GUID) &&
  222. write_guid(&guid);
  223. }
  224. static BOOL parse_name(void)
  225. {
  226. char c;
  227. int len = 0;
  228. char name[512];
  229. while (read_byte(&c) && len < sizeof(name) &&
  230. (isalnum(c) || c == '_' || c == '-'))
  231. {
  232. if (len + 1 < sizeof(name))
  233. name[len++] = c;
  234. }
  235. unread_byte(c);
  236. name[len] = 0;
  237. if (parse_keyword(name)) {
  238. return TRUE;
  239. } else {
  240. return write_word(TOKEN_NAME) &&
  241. write_dword(len) &&
  242. write_bytes(name, len);
  243. }
  244. }
  245. static BOOL parse_number(void)
  246. {
  247. int len = 0;
  248. char c;
  249. char buffer[512];
  250. BOOL dot = FALSE;
  251. BOOL ret;
  252. while (read_byte(&c) &&
  253. ((!len && c == '-') || (!dot && c == '.') || isdigit(c)))
  254. {
  255. if (len + 1 < sizeof(buffer))
  256. buffer[len++] = c;
  257. if (c == '.')
  258. dot = TRUE;
  259. }
  260. unread_byte(c);
  261. buffer[len] = 0;
  262. if (dot) {
  263. float value;
  264. ret = sscanf(buffer, "%f", &value);
  265. if (!ret) fatal_error( "invalid float token\n" );
  266. ret = write_word(TOKEN_FLOAT) &&
  267. write_float(value);
  268. } else {
  269. int value;
  270. ret = sscanf(buffer, "%d", &value);
  271. if (!ret) fatal_error( "invalid integer token\n" );
  272. ret = write_word(TOKEN_INTEGER) &&
  273. write_dword(value);
  274. }
  275. return ret;
  276. }
  277. static BOOL parse_token(void)
  278. {
  279. char c;
  280. int len;
  281. char *tok, buffer[512];
  282. if (!read_byte(&c))
  283. return FALSE;
  284. switch (c)
  285. {
  286. case '\n':
  287. case '\r':
  288. case ' ':
  289. case '\t':
  290. return TRUE;
  291. case '{': return write_word(TOKEN_OBRACE);
  292. case '}': return write_word(TOKEN_CBRACE);
  293. case '[': return write_word(TOKEN_OBRACKET);
  294. case ']': return write_word(TOKEN_CBRACKET);
  295. case '(': return write_word(TOKEN_OPAREN);
  296. case ')': return write_word(TOKEN_CPAREN);
  297. case ',': return write_word(TOKEN_COMMA);
  298. case ';': return write_word(TOKEN_SEMICOLON);
  299. case '.': return write_word(TOKEN_DOT);
  300. case '/':
  301. if (!read_byte(&c) || c != '/')
  302. fatal_error( "invalid single '/' comment token\n" );
  303. while (read_byte(&c) && c != '\n');
  304. return c == '\n';
  305. case '#':
  306. len = 0;
  307. while (read_byte(&c) && c != '\n')
  308. if (len + 1 < sizeof(buffer)) buffer[len++] = c;
  309. if (c != '\n') fatal_error( "line too long\n" );
  310. buffer[len] = 0;
  311. tok = strtok( buffer, " \t" );
  312. if (!tok || strcmp( tok, "pragma" )) return TRUE;
  313. tok = strtok( NULL, " \t" );
  314. if (!tok || strcmp( tok, "xftmpl" )) return TRUE;
  315. tok = strtok( NULL, " \t" );
  316. if (!tok) return TRUE;
  317. if (!strcmp( tok, "name" ))
  318. {
  319. tok = strtok( NULL, " \t" );
  320. if (tok && !option_inc_var_name) option_inc_var_name = strdup( tok );
  321. }
  322. else if (!strcmp( tok, "size" ))
  323. {
  324. tok = strtok( NULL, " \t" );
  325. if (tok && !option_inc_size_name) option_inc_size_name = strdup( tok );
  326. }
  327. return TRUE;
  328. case '<':
  329. return parse_guid();
  330. case '"':
  331. len = 0;
  332. /* FIXME: Handle '\' (e.g. "valid\"string") */
  333. while (read_byte(&c) && c != '"') {
  334. if (len + 1 < sizeof(buffer))
  335. buffer[len++] = c;
  336. }
  337. if (c != '"') fatal_error( "unterminated string\n" );
  338. return write_word(TOKEN_STRING) &&
  339. write_dword(len) &&
  340. write_bytes(buffer, len);
  341. default:
  342. unread_byte(c);
  343. if (isdigit(c) || c == '-')
  344. return parse_number();
  345. if (isalpha(c) || c == '_')
  346. return parse_name();
  347. fatal_error( "invalid character '%c' to start token\n", c );
  348. }
  349. return TRUE;
  350. }
  351. static const char *output_file;
  352. static void cleanup_files(void)
  353. {
  354. if (output_file) unlink(output_file);
  355. }
  356. static void exit_on_signal( int sig )
  357. {
  358. exit(1); /* this will call the atexit functions */
  359. }
  360. static void usage(void)
  361. {
  362. fprintf(stderr, "Usage: %s [OPTIONS] INFILE\n"
  363. "Options:\n"
  364. " -H Output to a c header file instead of a binary file\n"
  365. " -i NAME Output to a c header file, data in variable NAME\n"
  366. " -s NAME In a c header file, define NAME to be the data size\n"
  367. " -o FILE Write output to FILE\n",
  368. program_name);
  369. }
  370. static char **parse_options(int argc, char **argv)
  371. {
  372. int optc;
  373. while ((optc = getopt(argc, argv, "hHi:o:s:")) != -1)
  374. {
  375. switch (optc)
  376. {
  377. case 'h':
  378. usage();
  379. exit(0);
  380. case 'H':
  381. option_header = TRUE;
  382. break;
  383. case 'i':
  384. option_header = TRUE;
  385. option_inc_var_name = strdup(optarg);
  386. break;
  387. case 'o':
  388. option_outfile_name = strdup(optarg);
  389. break;
  390. case 's':
  391. option_inc_size_name = strdup(optarg);
  392. break;
  393. }
  394. }
  395. return &argv[optind];
  396. }
  397. int main(int argc, char **argv)
  398. {
  399. char header[16];
  400. char **args;
  401. char *header_name = NULL;
  402. program_name = argv[0];
  403. args = parse_options(argc, argv);
  404. infile_name = *args++;
  405. if (!infile_name || *args)
  406. {
  407. usage();
  408. return 1;
  409. }
  410. infile = stdin;
  411. outfile = NULL;
  412. if (!strcmp(infile_name, "-")) {
  413. infile_name = "stdin";
  414. } else if (!(infile = fopen(infile_name, "rb"))) {
  415. perror(infile_name);
  416. goto error;
  417. }
  418. if (!read_bytes(header, sizeof(header))) {
  419. fprintf(stderr, "%s: Failed to read file header\n", program_name);
  420. goto error;
  421. }
  422. if (strncmp(header, "xof ", 4))
  423. {
  424. fprintf(stderr, "%s: Invalid magic value '%.4s'\n", program_name, header);
  425. goto error;
  426. }
  427. if (strncmp(header + 4, "0302", 4) && strncmp(header + 4, "0303", 4))
  428. {
  429. fprintf(stderr, "%s: Unsupported version '%.4s'\n", program_name, header + 4);
  430. goto error;
  431. }
  432. if (strncmp(header + 8, "txt ", 4))
  433. {
  434. fprintf(stderr, "%s: Only support conversion from text encoded X files.",
  435. program_name);
  436. goto error;
  437. }
  438. if (strncmp(header + 12, "0032", 4) && strncmp(header + 12, "0064", 4))
  439. {
  440. fprintf(stderr, "%s: Only 32-bit or 64-bit float format supported, not '%.4s'.\n",
  441. program_name, header + 12);
  442. goto error;
  443. }
  444. if (!strcmp(option_outfile_name, "-")) {
  445. option_outfile_name = "stdout";
  446. outfile = stdout;
  447. } else {
  448. output_file = option_outfile_name;
  449. atexit(cleanup_files);
  450. signal(SIGTERM, exit_on_signal);
  451. signal(SIGINT, exit_on_signal);
  452. #ifdef SIGHUP
  453. signal(SIGHUP, exit_on_signal);
  454. #endif
  455. if (!(outfile = fopen(output_file, "wb"))) {
  456. perror(option_outfile_name);
  457. goto error;
  458. }
  459. }
  460. if (!write_bytes("xof 0302bin 0064", 16))
  461. goto error;
  462. line_no = 1;
  463. while (parse_token());
  464. if (ferror(outfile) || ferror(infile))
  465. goto error;
  466. if (option_header)
  467. {
  468. char *str_ptr;
  469. if (!option_inc_var_name)
  470. fatal_error( "variable name must be specified with -i or #pragma name\n" );
  471. header_name = strrchr(option_outfile_name, '/');
  472. if (header_name)
  473. header_name = strdup(header_name + 1);
  474. else
  475. header_name = strdup(option_outfile_name);
  476. if (!header_name) {
  477. fprintf(stderr, "Out of memory\n");
  478. goto error;
  479. }
  480. str_ptr = header_name;
  481. while (*str_ptr) {
  482. if (*str_ptr == '.')
  483. *str_ptr = '_';
  484. else
  485. *str_ptr = toupper(*str_ptr);
  486. str_ptr++;
  487. }
  488. fprintf(outfile,
  489. "/* File generated automatically from %s; do not edit */\n"
  490. "\n"
  491. "#ifndef __WINE_%s\n"
  492. "#define __WINE_%s\n"
  493. "\n"
  494. "unsigned char %s[] = {",
  495. infile_name, header_name, header_name, option_inc_var_name);
  496. write_c_hex_bytes();
  497. fprintf(outfile, "\n};\n\n");
  498. if (option_inc_size_name)
  499. fprintf(outfile, "#define %s %u\n\n", option_inc_size_name, output_pos);
  500. fprintf(outfile, "#endif /* __WINE_%s */\n", header_name);
  501. if (ferror(outfile))
  502. goto error;
  503. }
  504. else write_raw_bytes();
  505. fclose(infile);
  506. fclose(outfile);
  507. output_file = NULL;
  508. return 0;
  509. error:
  510. if (infile) {
  511. if (ferror(infile))
  512. perror(infile_name);
  513. fclose(infile);
  514. }
  515. if (outfile) {
  516. if (ferror(outfile))
  517. perror(option_outfile_name);
  518. fclose(outfile);
  519. }
  520. return 1;
  521. }