make_xftmpl.c 14 KB

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