res32.c 22 KB

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