res32.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. /*
  2. * Builtin dlls resource support
  3. *
  4. * Copyright 2000 Alexandre Julliard
  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 <assert.h>
  23. #include <ctype.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <stdarg.h>
  27. #include <stdio.h>
  28. #ifdef HAVE_SYS_TYPES_H
  29. # include <sys/types.h>
  30. #endif
  31. #include <fcntl.h>
  32. #include "build.h"
  33. typedef unsigned short WCHAR;
  34. /* Unicode string or integer id */
  35. struct string_id
  36. {
  37. WCHAR *str; /* ptr to Unicode string */
  38. unsigned short id; /* integer id if str is NULL */
  39. };
  40. /* descriptor for a resource */
  41. struct resource
  42. {
  43. struct string_id type;
  44. struct string_id name;
  45. const void *data;
  46. unsigned int data_size;
  47. unsigned int data_offset;
  48. unsigned short mem_options;
  49. unsigned short lang;
  50. unsigned int version;
  51. };
  52. /* name level of the resource tree */
  53. struct res_name
  54. {
  55. const struct string_id *name; /* name */
  56. struct resource *res; /* resource */
  57. int nb_languages; /* number of languages */
  58. unsigned int dir_offset; /* offset of directory in resource dir */
  59. unsigned int name_offset; /* offset of name in resource dir */
  60. };
  61. /* type level of the resource tree */
  62. struct res_type
  63. {
  64. const struct string_id *type; /* type name */
  65. struct res_name *names; /* names array */
  66. unsigned int nb_names; /* total number of names */
  67. unsigned int nb_id_names; /* number of names that have a numeric id */
  68. unsigned int dir_offset; /* offset of directory in resource dir */
  69. unsigned int name_offset; /* offset of type name in resource dir */
  70. };
  71. /* top level of the resource tree */
  72. struct res_tree
  73. {
  74. struct res_type *types; /* types array */
  75. unsigned int nb_types; /* total number of types */
  76. };
  77. /* size of a resource directory with n entries */
  78. #define RESOURCE_DIR_SIZE (4 * sizeof(unsigned int))
  79. #define RESOURCE_DIR_ENTRY_SIZE (2 * sizeof(unsigned int))
  80. #define RESOURCE_DATA_ENTRY_SIZE (4 * sizeof(unsigned int))
  81. #define RESDIR_SIZE(n) (RESOURCE_DIR_SIZE + (n) * RESOURCE_DIR_ENTRY_SIZE)
  82. static inline struct resource *add_resource( DLLSPEC *spec )
  83. {
  84. spec->resources = xrealloc( spec->resources, (spec->nb_resources + 1) * sizeof(spec->resources[0]) );
  85. return &spec->resources[spec->nb_resources++];
  86. }
  87. static inline unsigned int strlenW( const WCHAR *str )
  88. {
  89. const WCHAR *s = str;
  90. while (*s) s++;
  91. return s - str;
  92. }
  93. static inline int strcmpW( const WCHAR *str1, const WCHAR *str2 )
  94. {
  95. while (*str1 && (*str1 == *str2)) { str1++; str2++; }
  96. return *str1 - *str2;
  97. }
  98. static struct res_name *add_name( struct res_type *type, struct resource *res )
  99. {
  100. struct res_name *name;
  101. type->names = xrealloc( type->names, (type->nb_names + 1) * sizeof(*type->names) );
  102. name = &type->names[type->nb_names++];
  103. name->name = &res->name;
  104. name->res = res;
  105. name->nb_languages = 1;
  106. if (!name->name->str) type->nb_id_names++;
  107. return name;
  108. }
  109. static struct res_type *add_type( struct res_tree *tree, struct resource *res )
  110. {
  111. struct res_type *type;
  112. tree->types = xrealloc( tree->types, (tree->nb_types + 1) * sizeof(*tree->types) );
  113. type = &tree->types[tree->nb_types++];
  114. type->type = &res->type;
  115. type->names = NULL;
  116. type->nb_names = 0;
  117. type->nb_id_names = 0;
  118. return type;
  119. }
  120. /* get a string from the current resource file */
  121. static void get_string( struct string_id *str )
  122. {
  123. WCHAR wc = get_word();
  124. if (wc == 0xffff)
  125. {
  126. str->str = NULL;
  127. str->id = get_word();
  128. }
  129. else
  130. {
  131. WCHAR *p = xmalloc( (strlenW( (const WCHAR *)(input_buffer + input_buffer_pos) - 1) + 1) * sizeof(WCHAR) );
  132. str->str = p;
  133. str->id = 0;
  134. if ((*p++ = wc)) while ((*p++ = get_word()));
  135. }
  136. }
  137. /* put a string into the resource file */
  138. static void put_string( const struct string_id *str )
  139. {
  140. if (str->str)
  141. {
  142. const WCHAR *p = str->str;
  143. while (*p) put_word( *p++ );
  144. put_word( 0 );
  145. }
  146. else
  147. {
  148. put_word( 0xffff );
  149. put_word( str->id );
  150. }
  151. }
  152. static void dump_res_data( const struct resource *res )
  153. {
  154. unsigned int i = 0;
  155. unsigned int size = (res->data_size + 3) & ~3;
  156. if (!size) return;
  157. input_buffer = res->data;
  158. input_buffer_pos = 0;
  159. input_buffer_size = size;
  160. output( "\t.long " );
  161. while (size > 4)
  162. {
  163. if ((i++ % 16) == 15) output( "0x%08x\n\t.long ", get_dword() );
  164. else output( "0x%08x,", get_dword() );
  165. size -= 4;
  166. }
  167. output( "0x%08x\n", get_dword() );
  168. assert( input_buffer_pos == input_buffer_size );
  169. }
  170. /* check the file header */
  171. /* all values must be zero except header size */
  172. static int check_header(void)
  173. {
  174. unsigned int size;
  175. if (get_dword()) return 0; /* data size */
  176. size = get_dword(); /* header size */
  177. if (size == 0x20000000) byte_swapped = 1;
  178. else if (size != 0x20) return 0;
  179. if (get_word() != 0xffff || get_word()) return 0; /* type, must be id 0 */
  180. if (get_word() != 0xffff || get_word()) return 0; /* name, must be id 0 */
  181. if (get_dword()) return 0; /* data version */
  182. if (get_word()) return 0; /* mem options */
  183. if (get_word()) return 0; /* language */
  184. if (get_dword()) return 0; /* version */
  185. if (get_dword()) return 0; /* characteristics */
  186. return 1;
  187. }
  188. /* load the next resource from the current file */
  189. static void load_next_resource( DLLSPEC *spec )
  190. {
  191. unsigned int hdr_size;
  192. struct resource *res = add_resource( spec );
  193. res->data_size = get_dword();
  194. hdr_size = get_dword();
  195. if (hdr_size & 3) fatal_error( "%s header size not aligned\n", input_buffer_filename );
  196. if (hdr_size < 32) fatal_error( "%s invalid header size %u\n", input_buffer_filename, hdr_size );
  197. res->data = input_buffer + input_buffer_pos - 2*sizeof(unsigned int) + hdr_size;
  198. if ((const unsigned char *)res->data < input_buffer ||
  199. (const unsigned char *)res->data >= input_buffer + input_buffer_size)
  200. fatal_error( "%s invalid header size %u\n", input_buffer_filename, hdr_size );
  201. get_string( &res->type );
  202. get_string( &res->name );
  203. if (input_buffer_pos & 2) get_word(); /* align to dword boundary */
  204. get_dword(); /* skip data version */
  205. res->mem_options = get_word();
  206. res->lang = get_word();
  207. res->version = get_dword();
  208. get_dword(); /* skip characteristics */
  209. input_buffer_pos = ((const unsigned char *)res->data - input_buffer) + ((res->data_size + 3) & ~3);
  210. input_buffer_pos = (input_buffer_pos + 3) & ~3;
  211. if (input_buffer_pos > input_buffer_size)
  212. fatal_error( "%s is a truncated file\n", input_buffer_filename );
  213. }
  214. /* load a Win32 .res file */
  215. int load_res32_file( const char *name, DLLSPEC *spec )
  216. {
  217. int ret;
  218. init_input_buffer( name );
  219. if ((ret = check_header()))
  220. {
  221. while (input_buffer_pos < input_buffer_size) load_next_resource( spec );
  222. }
  223. return ret;
  224. }
  225. /* compare two unicode strings/ids */
  226. static int cmp_string( const struct string_id *str1, const struct string_id *str2 )
  227. {
  228. if (!str1->str)
  229. {
  230. if (!str2->str) return str1->id - str2->id;
  231. return 1; /* an id compares larger than a string */
  232. }
  233. if (!str2->str) return -1;
  234. return strcmpW( str1->str, str2->str );
  235. }
  236. /* compare two resources for sorting the resource directory */
  237. /* resources are stored first by type, then by name, then by language */
  238. static int cmp_res( const void *ptr1, const void *ptr2 )
  239. {
  240. const struct resource *res1 = ptr1;
  241. const struct resource *res2 = ptr2;
  242. int ret;
  243. if ((ret = cmp_string( &res1->type, &res2->type ))) return ret;
  244. if ((ret = cmp_string( &res1->name, &res2->name ))) return ret;
  245. if ((ret = res1->lang - res2->lang)) return ret;
  246. return res1->version - res2->version;
  247. }
  248. static char *format_res_string( const struct string_id *str )
  249. {
  250. int i, len = str->str ? strlenW(str->str) + 1 : 5;
  251. char *ret = xmalloc( len );
  252. if (!str->str) sprintf( ret, "%04x", str->id );
  253. else for (i = 0; i < len; i++) ret[i] = str->str[i]; /* dumb W->A conversion */
  254. return ret;
  255. }
  256. /* get rid of duplicate resources with different versions */
  257. static void remove_duplicate_resources( DLLSPEC *spec )
  258. {
  259. unsigned int i, n;
  260. for (i = n = 0; i < spec->nb_resources; i++, n++)
  261. {
  262. if (i && !cmp_string( &spec->resources[i].type, &spec->resources[i-1].type ) &&
  263. !cmp_string( &spec->resources[i].name, &spec->resources[i-1].name ) &&
  264. spec->resources[i].lang == spec->resources[i-1].lang)
  265. {
  266. if (spec->resources[i].version == spec->resources[i-1].version)
  267. {
  268. char *type_str = format_res_string( &spec->resources[i].type );
  269. char *name_str = format_res_string( &spec->resources[i].name );
  270. error( "winebuild: duplicate resource type %s name %s language %04x version %08x\n",
  271. type_str, name_str, spec->resources[i].lang, spec->resources[i].version );
  272. }
  273. else n--; /* replace the previous one */
  274. }
  275. if (n < i) spec->resources[n] = spec->resources[i];
  276. }
  277. spec->nb_resources = n;
  278. }
  279. /* build the 3-level (type,name,language) resource tree */
  280. static struct res_tree *build_resource_tree( DLLSPEC *spec, unsigned int *dir_size )
  281. {
  282. unsigned int i, k, n, offset, data_offset;
  283. struct res_tree *tree;
  284. struct res_type *type = NULL;
  285. struct res_name *name = NULL;
  286. struct resource *res;
  287. qsort( spec->resources, spec->nb_resources, sizeof(*spec->resources), cmp_res );
  288. remove_duplicate_resources( spec );
  289. tree = xmalloc( sizeof(*tree) );
  290. tree->types = NULL;
  291. tree->nb_types = 0;
  292. for (i = 0; i < spec->nb_resources; i++)
  293. {
  294. if (!i || cmp_string( &spec->resources[i].type, &spec->resources[i-1].type )) /* new type */
  295. {
  296. type = add_type( tree, &spec->resources[i] );
  297. name = add_name( type, &spec->resources[i] );
  298. }
  299. else if (cmp_string( &spec->resources[i].name, &spec->resources[i-1].name )) /* new name */
  300. {
  301. name = add_name( type, &spec->resources[i] );
  302. }
  303. else
  304. {
  305. name->nb_languages++;
  306. }
  307. }
  308. /* compute the offsets */
  309. offset = RESDIR_SIZE( tree->nb_types );
  310. for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
  311. {
  312. type->dir_offset = offset;
  313. offset += RESDIR_SIZE( type->nb_names );
  314. for (n = 0, name = type->names; n < type->nb_names; n++, name++)
  315. {
  316. name->dir_offset = offset;
  317. offset += RESDIR_SIZE( name->nb_languages );
  318. }
  319. }
  320. data_offset = offset;
  321. offset += spec->nb_resources * RESOURCE_DATA_ENTRY_SIZE;
  322. for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
  323. {
  324. if (type->type->str)
  325. {
  326. type->name_offset = offset | 0x80000000;
  327. offset += (strlenW(type->type->str)+1) * sizeof(WCHAR);
  328. }
  329. else type->name_offset = type->type->id;
  330. for (n = 0, name = type->names; n < type->nb_names; n++, name++)
  331. {
  332. if (name->name->str)
  333. {
  334. name->name_offset = offset | 0x80000000;
  335. offset += (strlenW(name->name->str)+1) * sizeof(WCHAR);
  336. }
  337. else name->name_offset = name->name->id;
  338. for (k = 0, res = name->res; k < name->nb_languages; k++, res++)
  339. {
  340. unsigned int entry_offset = (res - spec->resources) * RESOURCE_DATA_ENTRY_SIZE;
  341. res->data_offset = data_offset + entry_offset;
  342. }
  343. }
  344. }
  345. if (dir_size) *dir_size = (offset + 3) & ~3;
  346. return tree;
  347. }
  348. /* free the resource tree */
  349. static void free_resource_tree( struct res_tree *tree )
  350. {
  351. unsigned int i;
  352. for (i = 0; i < tree->nb_types; i++) free( tree->types[i].names );
  353. free( tree->types );
  354. free( tree );
  355. }
  356. /* output a Unicode string */
  357. static void output_string( const WCHAR *name )
  358. {
  359. int i, len = strlenW(name);
  360. output( "\t.short 0x%04x", len );
  361. for (i = 0; i < len; i++) output( ",0x%04x", name[i] );
  362. output( " /* " );
  363. for (i = 0; i < len; i++) output( "%c", isprint((char)name[i]) ? (char)name[i] : '?' );
  364. output( " */\n" );
  365. }
  366. /* output a resource directory */
  367. static inline void output_res_dir( unsigned int nb_names, unsigned int nb_ids )
  368. {
  369. output( "\t.long 0\n" ); /* Characteristics */
  370. output( "\t.long 0\n" ); /* TimeDateStamp */
  371. output( "\t.short 0,0\n" ); /* Major/MinorVersion */
  372. output( "\t.short %u,%u\n", /* NumberOfNamed/IdEntries */
  373. nb_names, nb_ids );
  374. }
  375. /* output the resource definitions */
  376. void output_resources( DLLSPEC *spec )
  377. {
  378. int k, nb_id_types;
  379. unsigned int i, n;
  380. struct res_tree *tree;
  381. struct res_type *type;
  382. struct res_name *name;
  383. const struct resource *res;
  384. if (!spec->nb_resources) return;
  385. tree = build_resource_tree( spec, NULL );
  386. /* output the resource directories */
  387. output( "\n/* resources */\n\n" );
  388. output( "\t%s\n", get_asm_rsrc_section() );
  389. output( "\t.align %d\n", get_alignment(get_ptr_size()) );
  390. output( ".L__wine_spec_resources:\n" );
  391. for (i = nb_id_types = 0, type = tree->types; i < tree->nb_types; i++, type++)
  392. if (!type->type->str) nb_id_types++;
  393. output_res_dir( tree->nb_types - nb_id_types, nb_id_types );
  394. /* dump the type directory */
  395. for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
  396. output( "\t.long 0x%08x,0x%08x\n",
  397. type->name_offset, type->dir_offset | 0x80000000 );
  398. /* dump the names and languages directories */
  399. for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
  400. {
  401. output_res_dir( type->nb_names - type->nb_id_names, type->nb_id_names );
  402. for (n = 0, name = type->names; n < type->nb_names; n++, name++)
  403. output( "\t.long 0x%08x,0x%08x\n",
  404. name->name_offset, name->dir_offset | 0x80000000 );
  405. for (n = 0, name = type->names; n < type->nb_names; n++, name++)
  406. {
  407. output_res_dir( 0, name->nb_languages );
  408. for (k = 0, res = name->res; k < name->nb_languages; k++, res++)
  409. output( "\t.long 0x%08x,0x%08x\n", res->lang, res->data_offset );
  410. }
  411. }
  412. /* dump the resource data entries */
  413. for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
  414. {
  415. output_rva( ".L__wine_spec_res_%d", i );
  416. output( "\t.long %u,0,0\n", res->data_size );
  417. }
  418. /* dump the name strings */
  419. for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
  420. {
  421. if (type->type->str) output_string( type->type->str );
  422. for (n = 0, name = type->names; n < type->nb_names; n++, name++)
  423. if (name->name->str) output_string( name->name->str );
  424. }
  425. /* resource data */
  426. for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
  427. {
  428. output( "\n\t.align %d\n", get_alignment(4) );
  429. output( ".L__wine_spec_res_%d:\n", i );
  430. dump_res_data( res );
  431. }
  432. if (!is_pe())
  433. {
  434. output( ".L__wine_spec_resources_end:\n" );
  435. output( "\t.byte 0\n" );
  436. }
  437. free_resource_tree( tree );
  438. }
  439. /* output a Unicode string in binary format */
  440. static void output_bin_string( const WCHAR *name )
  441. {
  442. int i, len = strlenW(name);
  443. put_word( len );
  444. for (i = 0; i < len; i++) put_word( name[i] );
  445. }
  446. /* output a resource directory in binary format */
  447. static inline void output_bin_res_dir( unsigned int nb_names, unsigned int nb_ids )
  448. {
  449. put_dword( 0 ); /* Characteristics */
  450. put_dword( 0 ); /* TimeDateStamp */
  451. put_word( 0 ); /* MajorVersion */
  452. put_word( 0 ); /* MinorVersion */
  453. put_word( nb_names ); /* NumberOfNamedEntries */
  454. put_word( nb_ids ); /* NumberOfIdEntries */
  455. }
  456. /* output the resource definitions in binary format */
  457. void output_bin_resources( DLLSPEC *spec, unsigned int start_rva )
  458. {
  459. int k, nb_id_types;
  460. unsigned int i, n, data_offset;
  461. struct res_tree *tree;
  462. struct res_type *type;
  463. struct res_name *name;
  464. const struct resource *res;
  465. if (!spec->nb_resources) return;
  466. tree = build_resource_tree( spec, &data_offset );
  467. init_output_buffer();
  468. /* output the resource directories */
  469. for (i = nb_id_types = 0, type = tree->types; i < tree->nb_types; i++, type++)
  470. if (!type->type->str) nb_id_types++;
  471. output_bin_res_dir( tree->nb_types - nb_id_types, nb_id_types );
  472. /* dump the type directory */
  473. for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
  474. {
  475. put_dword( type->name_offset );
  476. put_dword( type->dir_offset | 0x80000000 );
  477. }
  478. /* dump the names and languages directories */
  479. for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
  480. {
  481. output_bin_res_dir( type->nb_names - type->nb_id_names, type->nb_id_names );
  482. for (n = 0, name = type->names; n < type->nb_names; n++, name++)
  483. {
  484. put_dword( name->name_offset );
  485. put_dword( name->dir_offset | 0x80000000 );
  486. }
  487. for (n = 0, name = type->names; n < type->nb_names; n++, name++)
  488. {
  489. output_bin_res_dir( 0, name->nb_languages );
  490. for (k = 0, res = name->res; k < name->nb_languages; k++, res++)
  491. {
  492. put_dword( res->lang );
  493. put_dword( res->data_offset );
  494. }
  495. }
  496. }
  497. /* dump the resource data entries */
  498. for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
  499. {
  500. put_dword( data_offset + start_rva );
  501. put_dword( res->data_size );
  502. put_dword( 0 );
  503. put_dword( 0 );
  504. data_offset += (res->data_size + 3) & ~3;
  505. }
  506. /* dump the name strings */
  507. for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
  508. {
  509. if (type->type->str) output_bin_string( type->type->str );
  510. for (n = 0, name = type->names; n < type->nb_names; n++, name++)
  511. if (name->name->str) output_bin_string( name->name->str );
  512. }
  513. /* resource data */
  514. align_output( 4 );
  515. for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
  516. {
  517. put_data( res->data, res->data_size );
  518. align_output( 4 );
  519. }
  520. free_resource_tree( tree );
  521. }
  522. static unsigned int get_resource_header_size( const struct resource *res )
  523. {
  524. unsigned int size = 5 * sizeof(unsigned int) + 2 * sizeof(unsigned short);
  525. if (!res->type.str) size += 2 * sizeof(unsigned short);
  526. else size += (strlenW(res->type.str) + 1) * sizeof(WCHAR);
  527. if (!res->name.str) size += 2 * sizeof(unsigned short);
  528. else size += (strlenW(res->name.str) + 1) * sizeof(WCHAR);
  529. return size;
  530. }
  531. /* output the resources into a .o file */
  532. void output_res_o_file( DLLSPEC *spec )
  533. {
  534. unsigned int i;
  535. char *res_file = NULL;
  536. const char *format;
  537. int fd;
  538. struct strarray args;
  539. if (!spec->nb_resources) fatal_error( "--resources mode needs at least one resource file as input\n" );
  540. if (!output_file_name) fatal_error( "No output file name specified\n" );
  541. qsort( spec->resources, spec->nb_resources, sizeof(*spec->resources), cmp_res );
  542. remove_duplicate_resources( spec );
  543. byte_swapped = 0;
  544. init_output_buffer();
  545. put_dword( 0 ); /* ResSize */
  546. put_dword( 32 ); /* HeaderSize */
  547. put_word( 0xffff ); /* ResType */
  548. put_word( 0x0000 );
  549. put_word( 0xffff ); /* ResName */
  550. put_word( 0x0000 );
  551. put_dword( 0 ); /* DataVersion */
  552. put_word( 0 ); /* Memory options */
  553. put_word( 0 ); /* Language */
  554. put_dword( 0 ); /* Version */
  555. put_dword( 0 ); /* Characteristics */
  556. for (i = 0; i < spec->nb_resources; i++)
  557. {
  558. unsigned int header_size = get_resource_header_size( &spec->resources[i] );
  559. put_dword( spec->resources[i].data_size );
  560. put_dword( (header_size + 3) & ~3 );
  561. put_string( &spec->resources[i].type );
  562. put_string( &spec->resources[i].name );
  563. align_output( 4 );
  564. put_dword( 0 );
  565. put_word( spec->resources[i].mem_options );
  566. put_word( spec->resources[i].lang );
  567. put_dword( spec->resources[i].version );
  568. put_dword( 0 );
  569. put_data( spec->resources[i].data, spec->resources[i].data_size );
  570. align_output( 4 );
  571. }
  572. /* if the output file name is a .res too, don't run the results through windres */
  573. if (strendswith( output_file_name, ".res"))
  574. {
  575. flush_output_buffer();
  576. return;
  577. }
  578. res_file = get_temp_file_name( output_file_name, ".res" );
  579. if ((fd = open( res_file, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0600 )) == -1)
  580. fatal_error( "Cannot create %s\n", res_file );
  581. if (write( fd, output_buffer, output_buffer_pos ) != output_buffer_pos)
  582. fatal_error( "Error writing to %s\n", res_file );
  583. close( fd );
  584. free( output_buffer );
  585. args = find_tool( "windres", NULL );
  586. switch (target_cpu)
  587. {
  588. case CPU_x86:
  589. format = "pe-i386";
  590. break;
  591. case CPU_x86_64:
  592. format = "pe-x86-64";
  593. break;
  594. default:
  595. format = NULL;
  596. break;
  597. }
  598. strarray_add( &args, "-i", res_file, "-o", output_file_name, NULL );
  599. if (format)
  600. strarray_add( &args, "-F", format, NULL );
  601. spawn( args );
  602. }