resbin.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160
  1. /* resbin.c -- manipulate the Windows binary resource format.
  2. Copyright (C) 1997-2015 Free Software Foundation, Inc.
  3. Written by Ian Lance Taylor, Cygnus Support.
  4. Rewritten by Kai Tietz, Onevision.
  5. This file is part of GNU Binutils.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
  17. 02110-1301, USA. */
  18. /* This file contains functions to convert between the binary resource
  19. format and the internal structures that we want to use. The same
  20. binary resource format is used in both res and COFF files. */
  21. #include "sysdep.h"
  22. #include "bfd.h"
  23. #include "bucomm.h"
  24. #include "libiberty.h"
  25. #include "windres.h"
  26. /* Local functions. */
  27. static void toosmall (const char *);
  28. static unichar *get_unicode (windres_bfd *, const bfd_byte *, rc_uint_type, rc_uint_type *);
  29. static int get_resid (windres_bfd *, rc_res_id *, const bfd_byte *, rc_uint_type);
  30. static rc_res_resource *bin_to_res_generic (windres_bfd *, enum rc_res_type,
  31. const bfd_byte *, rc_uint_type);
  32. static rc_res_resource *bin_to_res_cursor (windres_bfd *, const bfd_byte *, rc_uint_type);
  33. static rc_res_resource *bin_to_res_menu (windres_bfd *,const bfd_byte *, rc_uint_type);
  34. static rc_menuitem *bin_to_res_menuitems (windres_bfd *, const bfd_byte *, rc_uint_type,
  35. rc_uint_type *);
  36. static rc_menuitem *bin_to_res_menuexitems (windres_bfd *, const bfd_byte *, rc_uint_type,
  37. rc_uint_type *);
  38. static rc_res_resource *bin_to_res_dialog (windres_bfd *, const bfd_byte *, rc_uint_type);
  39. static rc_res_resource *bin_to_res_string (windres_bfd *,const bfd_byte *, rc_uint_type);
  40. static rc_res_resource *bin_to_res_fontdir (windres_bfd *, const bfd_byte *, rc_uint_type);
  41. static rc_res_resource *bin_to_res_accelerators (windres_bfd *, const bfd_byte *, rc_uint_type);
  42. static rc_res_resource *bin_to_res_rcdata (windres_bfd *, const bfd_byte *, rc_uint_type, int);
  43. static rc_res_resource *bin_to_res_group_cursor (windres_bfd *, const bfd_byte *, rc_uint_type);
  44. static rc_res_resource *bin_to_res_group_icon (windres_bfd *, const bfd_byte *, rc_uint_type);
  45. static rc_res_resource *bin_to_res_version (windres_bfd *, const bfd_byte *, rc_uint_type);
  46. static rc_res_resource *bin_to_res_userdata (windres_bfd *, const bfd_byte *, rc_uint_type);
  47. static rc_res_resource *bin_to_res_toolbar (windres_bfd *, const bfd_byte *, rc_uint_type);
  48. static void get_version_header (windres_bfd *, const bfd_byte *, rc_uint_type, const char *,
  49. unichar **, rc_uint_type *, rc_uint_type *, rc_uint_type *,
  50. rc_uint_type *);
  51. /* Given a resource type ID, a pointer to data, a length, return a
  52. rc_res_resource structure which represents that resource. The caller
  53. is responsible for initializing the res_info and coff_info fields
  54. of the returned structure. */
  55. rc_res_resource *
  56. bin_to_res (windres_bfd *wrbfd, rc_res_id type, const bfd_byte *data,
  57. rc_uint_type length)
  58. {
  59. if (type.named)
  60. return bin_to_res_userdata (wrbfd, data, length);
  61. else
  62. {
  63. switch (type.u.id)
  64. {
  65. default:
  66. return bin_to_res_userdata (wrbfd, data, length);
  67. case RT_CURSOR:
  68. return bin_to_res_cursor (wrbfd, data, length);
  69. case RT_BITMAP:
  70. return bin_to_res_generic (wrbfd, RES_TYPE_BITMAP, data, length);
  71. case RT_ICON:
  72. return bin_to_res_generic (wrbfd, RES_TYPE_ICON, data, length);
  73. case RT_MENU:
  74. return bin_to_res_menu (wrbfd, data, length);
  75. case RT_DIALOG:
  76. return bin_to_res_dialog (wrbfd, data, length);
  77. case RT_STRING:
  78. return bin_to_res_string (wrbfd, data, length);
  79. case RT_FONTDIR:
  80. return bin_to_res_fontdir (wrbfd, data, length);
  81. case RT_FONT:
  82. return bin_to_res_generic (wrbfd, RES_TYPE_FONT, data, length);
  83. case RT_ACCELERATOR:
  84. return bin_to_res_accelerators (wrbfd, data, length);
  85. case RT_RCDATA:
  86. return bin_to_res_rcdata (wrbfd, data, length, RES_TYPE_RCDATA);
  87. case RT_MESSAGETABLE:
  88. return bin_to_res_generic (wrbfd, RES_TYPE_MESSAGETABLE, data, length);
  89. case RT_GROUP_CURSOR:
  90. return bin_to_res_group_cursor (wrbfd, data, length);
  91. case RT_GROUP_ICON:
  92. return bin_to_res_group_icon (wrbfd, data, length);
  93. case RT_VERSION:
  94. return bin_to_res_version (wrbfd, data, length);
  95. case RT_TOOLBAR:
  96. return bin_to_res_toolbar (wrbfd, data, length);
  97. }
  98. }
  99. }
  100. /* Give an error if the binary data is too small. */
  101. static void
  102. toosmall (const char *msg)
  103. {
  104. fatal (_("%s: not enough binary data"), msg);
  105. }
  106. /* Swap in a NULL terminated unicode string. */
  107. static unichar *
  108. get_unicode (windres_bfd *wrbfd, const bfd_byte *data, rc_uint_type length,
  109. rc_uint_type *retlen)
  110. {
  111. rc_uint_type c, i;
  112. unichar *ret;
  113. c = 0;
  114. while (1)
  115. {
  116. if (length < c * 2 + 2)
  117. toosmall (_("null terminated unicode string"));
  118. if (windres_get_16 (wrbfd, data + c * 2, 2) == 0)
  119. break;
  120. ++c;
  121. }
  122. ret = (unichar *) res_alloc ((c + 1) * sizeof (unichar));
  123. for (i = 0; i < c; i++)
  124. ret[i] = windres_get_16 (wrbfd, data + i * 2, 2);
  125. ret[i] = 0;
  126. if (retlen != NULL)
  127. *retlen = c;
  128. return ret;
  129. }
  130. /* Get a resource identifier. This returns the number of bytes used. */
  131. static int
  132. get_resid (windres_bfd *wrbfd, rc_res_id *id, const bfd_byte *data,
  133. rc_uint_type length)
  134. {
  135. rc_uint_type first;
  136. if (length < 2)
  137. toosmall (_("resource ID"));
  138. first = windres_get_16 (wrbfd, data, 2);
  139. if (first == 0xffff)
  140. {
  141. if (length < 4)
  142. toosmall (_("resource ID"));
  143. id->named = 0;
  144. id->u.id = windres_get_16 (wrbfd, data + 2, 2);
  145. return 4;
  146. }
  147. else
  148. {
  149. id->named = 1;
  150. id->u.n.name = get_unicode (wrbfd, data, length, &id->u.n.length);
  151. return id->u.n.length * 2 + 2;
  152. }
  153. }
  154. /* Convert a resource which just stores uninterpreted data from
  155. binary. */
  156. rc_res_resource *
  157. bin_to_res_generic (windres_bfd *wrbfd ATTRIBUTE_UNUSED, enum rc_res_type type,
  158. const bfd_byte *data, rc_uint_type length)
  159. {
  160. rc_res_resource *r;
  161. r = (rc_res_resource *) res_alloc (sizeof (rc_res_resource));
  162. r->type = type;
  163. r->u.data.data = data;
  164. r->u.data.length = length;
  165. return r;
  166. }
  167. /* Convert a cursor resource from binary. */
  168. rc_res_resource *
  169. bin_to_res_cursor (windres_bfd *wrbfd, const bfd_byte *data, rc_uint_type length)
  170. {
  171. rc_cursor *c;
  172. rc_res_resource *r;
  173. if (length < 4)
  174. toosmall (_("cursor"));
  175. c = (rc_cursor *) res_alloc (sizeof (rc_cursor));
  176. c->xhotspot = windres_get_16 (wrbfd, data, 2);
  177. c->yhotspot = windres_get_16 (wrbfd, data + 2, 2);
  178. c->length = length - 4;
  179. c->data = data + 4;
  180. r = (rc_res_resource *) res_alloc (sizeof *r);
  181. r->type = RES_TYPE_CURSOR;
  182. r->u.cursor = c;
  183. return r;
  184. }
  185. /* Convert a menu resource from binary. */
  186. rc_res_resource *
  187. bin_to_res_menu (windres_bfd *wrbfd, const bfd_byte *data, rc_uint_type length)
  188. {
  189. rc_res_resource *r;
  190. rc_menu *m;
  191. rc_uint_type version, got;
  192. r = (rc_res_resource *) res_alloc (sizeof *r);
  193. r->type = RES_TYPE_MENU;
  194. m = (rc_menu *) res_alloc (sizeof (rc_menu));
  195. r->u.menu = m;
  196. if (length < 2)
  197. toosmall (_("menu header"));
  198. version = windres_get_16 (wrbfd, data, 2);
  199. if (version == 0)
  200. {
  201. if (length < 4)
  202. toosmall (_("menu header"));
  203. m->help = 0;
  204. m->items = bin_to_res_menuitems (wrbfd, data + 4, length - 4, &got);
  205. }
  206. else if (version == 1)
  207. {
  208. rc_uint_type offset;
  209. if (length < 8)
  210. toosmall (_("menuex header"));
  211. m->help = windres_get_32 (wrbfd, data + 4, 4);
  212. offset = windres_get_16 (wrbfd, data + 2, 2);
  213. if (offset + 4 >= length)
  214. toosmall (_("menuex offset"));
  215. m->items = bin_to_res_menuexitems (wrbfd, data + 4 + offset,
  216. length - (4 + offset), &got);
  217. }
  218. else
  219. fatal (_("unsupported menu version %d"), (int) version);
  220. return r;
  221. }
  222. /* Convert menu items from binary. */
  223. static rc_menuitem *
  224. bin_to_res_menuitems (windres_bfd *wrbfd, const bfd_byte *data, rc_uint_type length,
  225. rc_uint_type *got)
  226. {
  227. rc_menuitem *first, **pp;
  228. first = NULL;
  229. pp = &first;
  230. *got = 0;
  231. while (length > 0)
  232. {
  233. rc_uint_type flags, slen, itemlen;
  234. rc_uint_type stroff;
  235. rc_menuitem *mi;
  236. if (length < 4)
  237. toosmall (_("menuitem header"));
  238. mi = (rc_menuitem *) res_alloc (sizeof *mi);
  239. mi->state = 0;
  240. mi->help = 0;
  241. flags = windres_get_16 (wrbfd, data, 2);
  242. mi->type = flags &~ (MENUITEM_POPUP | MENUITEM_ENDMENU);
  243. if ((flags & MENUITEM_POPUP) == 0)
  244. stroff = 4;
  245. else
  246. stroff = 2;
  247. if (length < stroff + 2)
  248. toosmall (_("menuitem header"));
  249. if (windres_get_16 (wrbfd, data + stroff, 2) == 0)
  250. {
  251. slen = 0;
  252. mi->text = NULL;
  253. }
  254. else
  255. mi->text = get_unicode (wrbfd, data + stroff, length - stroff, &slen);
  256. itemlen = stroff + slen * 2 + 2;
  257. if ((flags & MENUITEM_POPUP) == 0)
  258. {
  259. mi->popup = NULL;
  260. mi->id = windres_get_16 (wrbfd, data + 2, 2);
  261. }
  262. else
  263. {
  264. rc_uint_type subread;
  265. mi->id = 0;
  266. mi->popup = bin_to_res_menuitems (wrbfd, data + itemlen, length - itemlen,
  267. &subread);
  268. itemlen += subread;
  269. }
  270. mi->next = NULL;
  271. *pp = mi;
  272. pp = &mi->next;
  273. data += itemlen;
  274. length -= itemlen;
  275. *got += itemlen;
  276. if ((flags & MENUITEM_ENDMENU) != 0)
  277. return first;
  278. }
  279. return first;
  280. }
  281. /* Convert menuex items from binary. */
  282. static rc_menuitem *
  283. bin_to_res_menuexitems (windres_bfd *wrbfd, const bfd_byte *data, rc_uint_type length,
  284. rc_uint_type *got)
  285. {
  286. rc_menuitem *first, **pp;
  287. first = NULL;
  288. pp = &first;
  289. *got = 0;
  290. while (length > 0)
  291. {
  292. rc_uint_type flags, slen;
  293. rc_uint_type itemlen;
  294. rc_menuitem *mi;
  295. if (length < 16)
  296. toosmall (_("menuitem header"));
  297. mi = (rc_menuitem *) res_alloc (sizeof (rc_menuitem));
  298. mi->type = windres_get_32 (wrbfd, data, 4);
  299. mi->state = windres_get_32 (wrbfd, data + 4, 4);
  300. mi->id = windres_get_32 (wrbfd, data + 8, 4);
  301. flags = windres_get_16 (wrbfd, data + 12, 2);
  302. if (windres_get_16 (wrbfd, data + 14, 2) == 0)
  303. {
  304. slen = 0;
  305. mi->text = NULL;
  306. }
  307. else
  308. mi->text = get_unicode (wrbfd, data + 14, length - 14, &slen);
  309. itemlen = 14 + slen * 2 + 2;
  310. itemlen = (itemlen + 3) &~ 3;
  311. if ((flags & 1) == 0)
  312. {
  313. mi->popup = NULL;
  314. mi->help = 0;
  315. }
  316. else
  317. {
  318. rc_uint_type subread;
  319. if (length < itemlen + 4)
  320. toosmall (_("menuitem"));
  321. mi->help = windres_get_32 (wrbfd, data + itemlen, 4);
  322. itemlen += 4;
  323. mi->popup = bin_to_res_menuexitems (wrbfd, data + itemlen,
  324. length - itemlen, &subread);
  325. itemlen += subread;
  326. }
  327. mi->next = NULL;
  328. *pp = mi;
  329. pp = &mi->next;
  330. data += itemlen;
  331. length -= itemlen;
  332. *got += itemlen;
  333. if ((flags & 0x80) != 0)
  334. return first;
  335. }
  336. return first;
  337. }
  338. /* Convert a dialog resource from binary. */
  339. static rc_res_resource *
  340. bin_to_res_dialog (windres_bfd *wrbfd, const bfd_byte *data, rc_uint_type length)
  341. {
  342. rc_uint_type signature;
  343. rc_dialog *d;
  344. rc_uint_type c, sublen, i;
  345. rc_uint_type off;
  346. rc_dialog_control **pp;
  347. rc_res_resource *r;
  348. if (length < 18)
  349. toosmall (_("dialog header"));
  350. d = (rc_dialog *) res_alloc (sizeof (rc_dialog));
  351. signature = windres_get_16 (wrbfd, data + 2, 2);
  352. if (signature != 0xffff)
  353. {
  354. d->ex = NULL;
  355. d->style = windres_get_32 (wrbfd, data, 4);
  356. d->exstyle = windres_get_32 (wrbfd, data + 4, 4);
  357. off = 8;
  358. }
  359. else
  360. {
  361. int version;
  362. version = windres_get_16 (wrbfd, data, 2);
  363. if (version != 1)
  364. fatal (_("unexpected DIALOGEX version %d"), version);
  365. d->ex = (rc_dialog_ex *) res_alloc (sizeof (rc_dialog_ex));
  366. d->ex->help = windres_get_32 (wrbfd, data + 4, 4);
  367. d->exstyle = windres_get_32 (wrbfd, data + 8, 4);
  368. d->style = windres_get_32 (wrbfd, data + 12, 4);
  369. off = 16;
  370. }
  371. if (length < off + 10)
  372. toosmall (_("dialog header"));
  373. c = windres_get_16 (wrbfd, data + off, 2);
  374. d->x = windres_get_16 (wrbfd, data + off + 2, 2);
  375. d->y = windres_get_16 (wrbfd, data + off + 4, 2);
  376. d->width = windres_get_16 (wrbfd, data + off + 6, 2);
  377. d->height = windres_get_16 (wrbfd, data + off + 8, 2);
  378. off += 10;
  379. sublen = get_resid (wrbfd, &d->menu, data + off, length - off);
  380. off += sublen;
  381. sublen = get_resid (wrbfd, &d->class, data + off, length - off);
  382. off += sublen;
  383. d->caption = get_unicode (wrbfd, data + off, length - off, &sublen);
  384. off += sublen * 2 + 2;
  385. if (sublen == 0)
  386. d->caption = NULL;
  387. if ((d->style & DS_SETFONT) == 0)
  388. {
  389. d->pointsize = 0;
  390. d->font = NULL;
  391. if (d->ex != NULL)
  392. {
  393. d->ex->weight = 0;
  394. d->ex->italic = 0;
  395. d->ex->charset = 1; /* Default charset. */
  396. }
  397. }
  398. else
  399. {
  400. if (length < off + 2)
  401. toosmall (_("dialog font point size"));
  402. d->pointsize = windres_get_16 (wrbfd, data + off, 2);
  403. off += 2;
  404. if (d->ex != NULL)
  405. {
  406. if (length < off + 4)
  407. toosmall (_("dialogex font information"));
  408. d->ex->weight = windres_get_16 (wrbfd, data + off, 2);
  409. d->ex->italic = windres_get_8 (wrbfd, data + off + 2, 1);
  410. d->ex->charset = windres_get_8 (wrbfd, data + off + 3, 1);
  411. off += 4;
  412. }
  413. d->font = get_unicode (wrbfd, data + off, length - off, &sublen);
  414. off += sublen * 2 + 2;
  415. }
  416. d->controls = NULL;
  417. pp = &d->controls;
  418. for (i = 0; i < c; i++)
  419. {
  420. rc_dialog_control *dc;
  421. int datalen;
  422. off = (off + 3) &~ 3;
  423. dc = (rc_dialog_control *) res_alloc (sizeof (rc_dialog_control));
  424. if (d->ex == NULL)
  425. {
  426. if (length < off + 8)
  427. toosmall (_("dialog control"));
  428. dc->style = windres_get_32 (wrbfd, data + off, 4);
  429. dc->exstyle = windres_get_32 (wrbfd, data + off + 4, 4);
  430. dc->help = 0;
  431. off += 8;
  432. }
  433. else
  434. {
  435. if (length < off + 12)
  436. toosmall (_("dialogex control"));
  437. dc->help = windres_get_32 (wrbfd, data + off, 4);
  438. dc->exstyle = windres_get_32 (wrbfd, data + off + 4, 4);
  439. dc->style = windres_get_32 (wrbfd, data + off + 8, 4);
  440. off += 12;
  441. }
  442. if (length < off + (d->ex != NULL ? 2 : 0) + 10)
  443. toosmall (_("dialog control"));
  444. dc->x = windres_get_16 (wrbfd, data + off, 2);
  445. dc->y = windres_get_16 (wrbfd, data + off + 2, 2);
  446. dc->width = windres_get_16 (wrbfd, data + off + 4, 2);
  447. dc->height = windres_get_16 (wrbfd, data + off + 6, 2);
  448. if (d->ex != NULL)
  449. dc->id = windres_get_32 (wrbfd, data + off + 8, 4);
  450. else
  451. dc->id = windres_get_16 (wrbfd, data + off + 8, 2);
  452. off += 10 + (d->ex != NULL ? 2 : 0);
  453. sublen = get_resid (wrbfd, &dc->class, data + off, length - off);
  454. off += sublen;
  455. sublen = get_resid (wrbfd, &dc->text, data + off, length - off);
  456. off += sublen;
  457. if (length < off + 2)
  458. toosmall (_("dialog control end"));
  459. datalen = windres_get_16 (wrbfd, data + off, 2);
  460. off += 2;
  461. if (datalen == 0)
  462. dc->data = NULL;
  463. else
  464. {
  465. off = (off + 3) &~ 3;
  466. if (length < off + datalen)
  467. toosmall (_("dialog control data"));
  468. dc->data = ((rc_rcdata_item *)
  469. res_alloc (sizeof (rc_rcdata_item)));
  470. dc->data->next = NULL;
  471. dc->data->type = RCDATA_BUFFER;
  472. dc->data->u.buffer.length = datalen;
  473. dc->data->u.buffer.data = data + off;
  474. off += datalen;
  475. }
  476. dc->next = NULL;
  477. *pp = dc;
  478. pp = &dc->next;
  479. }
  480. r = (rc_res_resource *) res_alloc (sizeof *r);
  481. r->type = RES_TYPE_DIALOG;
  482. r->u.dialog = d;
  483. return r;
  484. }
  485. /* Convert a stringtable resource from binary. */
  486. static rc_res_resource *
  487. bin_to_res_string (windres_bfd *wrbfd, const bfd_byte *data, rc_uint_type length)
  488. {
  489. rc_stringtable *st;
  490. int i;
  491. rc_res_resource *r;
  492. st = (rc_stringtable *) res_alloc (sizeof (rc_stringtable));
  493. for (i = 0; i < 16; i++)
  494. {
  495. unsigned int slen;
  496. if (length < 2)
  497. toosmall (_("stringtable string length"));
  498. slen = windres_get_16 (wrbfd, data, 2);
  499. st->strings[i].length = slen;
  500. if (slen > 0)
  501. {
  502. unichar *s;
  503. unsigned int j;
  504. if (length < 2 + 2 * slen)
  505. toosmall (_("stringtable string"));
  506. s = (unichar *) res_alloc (slen * sizeof (unichar));
  507. st->strings[i].string = s;
  508. for (j = 0; j < slen; j++)
  509. s[j] = windres_get_16 (wrbfd, data + 2 + j * 2, 2);
  510. }
  511. data += 2 + 2 * slen;
  512. length -= 2 + 2 * slen;
  513. }
  514. r = (rc_res_resource *) res_alloc (sizeof *r);
  515. r->type = RES_TYPE_STRINGTABLE;
  516. r->u.stringtable = st;
  517. return r;
  518. }
  519. /* Convert a fontdir resource from binary. */
  520. static rc_res_resource *
  521. bin_to_res_fontdir (windres_bfd *wrbfd, const bfd_byte *data, rc_uint_type length)
  522. {
  523. rc_uint_type c, i;
  524. rc_fontdir *first, **pp;
  525. rc_res_resource *r;
  526. if (length < 2)
  527. toosmall (_("fontdir header"));
  528. c = windres_get_16 (wrbfd, data, 2);
  529. first = NULL;
  530. pp = &first;
  531. for (i = 0; i < c; i++)
  532. {
  533. const struct bin_fontdir_item *bfi;
  534. rc_fontdir *fd;
  535. unsigned int off;
  536. if (length < 56)
  537. toosmall (_("fontdir"));
  538. bfi = (const struct bin_fontdir_item *) data;
  539. fd = (rc_fontdir *) res_alloc (sizeof *fd);
  540. fd->index = windres_get_16 (wrbfd, bfi->index, 2);
  541. /* To work out the length of the fontdir data, we must get the
  542. length of the device name and face name strings, even though
  543. we don't store them in the rc_fontdir. The
  544. documentation says that these are NULL terminated char
  545. strings, not Unicode strings. */
  546. off = 56;
  547. while (off < length && data[off] != '\0')
  548. ++off;
  549. if (off >= length)
  550. toosmall (_("fontdir device name"));
  551. ++off;
  552. while (off < length && data[off] != '\0')
  553. ++off;
  554. if (off >= length)
  555. toosmall (_("fontdir face name"));
  556. ++off;
  557. fd->length = off;
  558. fd->data = data;
  559. fd->next = NULL;
  560. *pp = fd;
  561. pp = &fd->next;
  562. /* The documentation does not indicate that any rounding is
  563. required. */
  564. data += off;
  565. length -= off;
  566. }
  567. r = (rc_res_resource *) res_alloc (sizeof *r);
  568. r->type = RES_TYPE_FONTDIR;
  569. r->u.fontdir = first;
  570. return r;
  571. }
  572. /* Convert an accelerators resource from binary. */
  573. static rc_res_resource *
  574. bin_to_res_accelerators (windres_bfd *wrbfd, const bfd_byte *data, rc_uint_type length)
  575. {
  576. rc_accelerator *first, **pp;
  577. rc_res_resource *r;
  578. first = NULL;
  579. pp = &first;
  580. while (1)
  581. {
  582. rc_accelerator *a;
  583. if (length < 8)
  584. toosmall (_("accelerator"));
  585. a = (rc_accelerator *) res_alloc (sizeof (rc_accelerator));
  586. a->flags = windres_get_16 (wrbfd, data, 2);
  587. a->key = windres_get_16 (wrbfd, data + 2, 2);
  588. a->id = windres_get_16 (wrbfd, data + 4, 2);
  589. a->next = NULL;
  590. *pp = a;
  591. pp = &a->next;
  592. if ((a->flags & ACC_LAST) != 0)
  593. break;
  594. data += 8;
  595. length -= 8;
  596. }
  597. r = (rc_res_resource *) res_alloc (sizeof (rc_res_resource));
  598. r->type = RES_TYPE_ACCELERATOR;
  599. r->u.acc = first;
  600. return r;
  601. }
  602. /* Convert an rcdata resource from binary. */
  603. static rc_res_resource *
  604. bin_to_res_rcdata (windres_bfd *wrbfd ATTRIBUTE_UNUSED, const bfd_byte *data,
  605. rc_uint_type length, int rctyp)
  606. {
  607. rc_rcdata_item *ri;
  608. rc_res_resource *r;
  609. ri = (rc_rcdata_item *) res_alloc (sizeof (rc_rcdata_item));
  610. ri->next = NULL;
  611. ri->type = RCDATA_BUFFER;
  612. ri->u.buffer.length = length;
  613. ri->u.buffer.data = data;
  614. r = (rc_res_resource *) res_alloc (sizeof *r);
  615. r->type = rctyp;
  616. r->u.rcdata = ri;
  617. return r;
  618. }
  619. /* Convert a group cursor resource from binary. */
  620. static rc_res_resource *
  621. bin_to_res_group_cursor (windres_bfd *wrbfd, const bfd_byte *data, rc_uint_type length)
  622. {
  623. int type, c, i;
  624. rc_group_cursor *first, **pp;
  625. rc_res_resource *r;
  626. if (length < 6)
  627. toosmall (_("group cursor header"));
  628. type = windres_get_16 (wrbfd, data + 2, 2);
  629. if (type != 2)
  630. fatal (_("unexpected group cursor type %d"), type);
  631. c = windres_get_16 (wrbfd, data + 4, 2);
  632. data += 6;
  633. length -= 6;
  634. first = NULL;
  635. pp = &first;
  636. for (i = 0; i < c; i++)
  637. {
  638. rc_group_cursor *gc;
  639. if (length < 14)
  640. toosmall (_("group cursor"));
  641. gc = (rc_group_cursor *) res_alloc (sizeof *gc);
  642. gc->width = windres_get_16 (wrbfd, data, 2);
  643. gc->height = windres_get_16 (wrbfd, data + 2, 2);
  644. gc->planes = windres_get_16 (wrbfd, data + 4, 2);
  645. gc->bits = windres_get_16 (wrbfd, data + 6, 2);
  646. gc->bytes = windres_get_32 (wrbfd, data + 8, 4);
  647. gc->index = windres_get_16 (wrbfd, data + 12, 2);
  648. gc->next = NULL;
  649. *pp = gc;
  650. pp = &gc->next;
  651. data += 14;
  652. length -= 14;
  653. }
  654. r = (rc_res_resource *) res_alloc (sizeof (rc_res_resource));
  655. r->type = RES_TYPE_GROUP_CURSOR;
  656. r->u.group_cursor = first;
  657. return r;
  658. }
  659. /* Convert a group icon resource from binary. */
  660. static rc_res_resource *
  661. bin_to_res_group_icon (windres_bfd *wrbfd, const bfd_byte *data, rc_uint_type length)
  662. {
  663. int type, c, i;
  664. rc_group_icon *first, **pp;
  665. rc_res_resource *r;
  666. if (length < 6)
  667. toosmall (_("group icon header"));
  668. type = windres_get_16 (wrbfd, data + 2, 2);
  669. if (type != 1)
  670. fatal (_("unexpected group icon type %d"), type);
  671. c = windres_get_16 (wrbfd, data + 4, 2);
  672. data += 6;
  673. length -= 6;
  674. first = NULL;
  675. pp = &first;
  676. for (i = 0; i < c; i++)
  677. {
  678. rc_group_icon *gi;
  679. if (length < 14)
  680. toosmall (_("group icon"));
  681. gi = (rc_group_icon *) res_alloc (sizeof (rc_group_icon));
  682. gi->width = windres_get_8 (wrbfd, data, 1);
  683. gi->height = windres_get_8 (wrbfd, data + 1, 1);
  684. gi->colors = windres_get_8 (wrbfd, data + 2, 1);
  685. gi->planes = windres_get_16 (wrbfd, data + 4, 2);
  686. gi->bits = windres_get_16 (wrbfd, data + 6, 2);
  687. gi->bytes = windres_get_32 (wrbfd, data + 8, 4);
  688. gi->index = windres_get_16 (wrbfd, data + 12, 2);
  689. gi->next = NULL;
  690. *pp = gi;
  691. pp = &gi->next;
  692. data += 14;
  693. length -= 14;
  694. }
  695. r = (rc_res_resource *) res_alloc (sizeof *r);
  696. r->type = RES_TYPE_GROUP_ICON;
  697. r->u.group_icon = first;
  698. return r;
  699. }
  700. /* Extract data from a version header. If KEY is not NULL, then the
  701. key must be KEY; otherwise, the key is returned in *PKEY. This
  702. sets *LEN to the total length, *VALLEN to the value length, *TYPE
  703. to the type, and *OFF to the offset to the children. */
  704. static void
  705. get_version_header (windres_bfd *wrbfd, const bfd_byte *data, rc_uint_type length,
  706. const char *key, unichar **pkey,
  707. rc_uint_type *len, rc_uint_type *vallen, rc_uint_type *type,
  708. rc_uint_type *off)
  709. {
  710. if (length < 8)
  711. toosmall (key);
  712. *len = (windres_get_16 (wrbfd, data, 2) + 3) & ~3;
  713. *vallen = windres_get_16 (wrbfd, data + 2, 2);
  714. *type = windres_get_16 (wrbfd, data + 4, 2);
  715. *off = 6;
  716. length -= 6;
  717. data += 6;
  718. if (key == NULL)
  719. {
  720. rc_uint_type sublen;
  721. *pkey = get_unicode (wrbfd, data, length, &sublen);
  722. *off += (sublen + 1) * sizeof (unichar);
  723. }
  724. else
  725. {
  726. while (1)
  727. {
  728. if (length < 2)
  729. toosmall (key);
  730. if (windres_get_16 (wrbfd, data, 2) != (bfd_byte) *key)
  731. fatal (_("unexpected version string"));
  732. *off += 2;
  733. length -= 2;
  734. data += 2;
  735. if (*key == '\0')
  736. break;
  737. ++key;
  738. }
  739. }
  740. *off = (*off + 3) &~ 3;
  741. }
  742. /* Convert a version resource from binary. */
  743. static rc_res_resource *
  744. bin_to_res_version (windres_bfd *wrbfd, const bfd_byte *data, rc_uint_type length)
  745. {
  746. rc_uint_type verlen, vallen, type, off;
  747. rc_fixed_versioninfo *fi;
  748. rc_ver_info *first, **pp;
  749. rc_versioninfo *v;
  750. rc_res_resource *r;
  751. get_version_header (wrbfd, data, length, "VS_VERSION_INFO",
  752. (unichar **) NULL, &verlen, &vallen, &type, &off);
  753. if ((unsigned int) verlen != length)
  754. fatal (_("version length %d does not match resource length %lu"),
  755. (int) verlen, (unsigned long) length);
  756. if (type != 0)
  757. fatal (_("unexpected version type %d"), (int) type);
  758. data += off;
  759. length -= off;
  760. if (vallen == 0)
  761. fi = NULL;
  762. else
  763. {
  764. unsigned long signature, fiv;
  765. if (vallen != 52)
  766. fatal (_("unexpected fixed version information length %ld"), (long) vallen);
  767. if (length < 52)
  768. toosmall (_("fixed version info"));
  769. signature = windres_get_32 (wrbfd, data, 4);
  770. if (signature != 0xfeef04bd)
  771. fatal (_("unexpected fixed version signature %lu"), signature);
  772. fiv = windres_get_32 (wrbfd, data + 4, 4);
  773. if (fiv != 0 && fiv != 0x10000)
  774. fatal (_("unexpected fixed version info version %lu"), fiv);
  775. fi = (rc_fixed_versioninfo *) res_alloc (sizeof (rc_fixed_versioninfo));
  776. fi->file_version_ms = windres_get_32 (wrbfd, data + 8, 4);
  777. fi->file_version_ls = windres_get_32 (wrbfd, data + 12, 4);
  778. fi->product_version_ms = windres_get_32 (wrbfd, data + 16, 4);
  779. fi->product_version_ls = windres_get_32 (wrbfd, data + 20, 4);
  780. fi->file_flags_mask = windres_get_32 (wrbfd, data + 24, 4);
  781. fi->file_flags = windres_get_32 (wrbfd, data + 28, 4);
  782. fi->file_os = windres_get_32 (wrbfd, data + 32, 4);
  783. fi->file_type = windres_get_32 (wrbfd, data + 36, 4);
  784. fi->file_subtype = windres_get_32 (wrbfd, data + 40, 4);
  785. fi->file_date_ms = windres_get_32 (wrbfd, data + 44, 4);
  786. fi->file_date_ls = windres_get_32 (wrbfd, data + 48, 4);
  787. data += 52;
  788. length -= 52;
  789. }
  790. first = NULL;
  791. pp = &first;
  792. while (length > 0)
  793. {
  794. rc_ver_info *vi;
  795. int ch;
  796. if (length < 8)
  797. toosmall (_("version var info"));
  798. vi = (rc_ver_info *) res_alloc (sizeof (rc_ver_info));
  799. ch = windres_get_16 (wrbfd, data + 6, 2);
  800. if (ch == 'S')
  801. {
  802. rc_ver_stringtable **ppvst;
  803. vi->type = VERINFO_STRING;
  804. get_version_header (wrbfd, data, length, "StringFileInfo",
  805. (unichar **) NULL, &verlen, &vallen, &type,
  806. &off);
  807. if (vallen != 0)
  808. fatal (_("unexpected stringfileinfo value length %ld"), (long) vallen);
  809. data += off;
  810. length -= off;
  811. verlen -= off;
  812. vi->u.string.stringtables = NULL;
  813. ppvst = &vi->u.string.stringtables;
  814. while (verlen > 0)
  815. {
  816. rc_ver_stringtable *vst;
  817. rc_uint_type stverlen;
  818. rc_ver_stringinfo **ppvs;
  819. if (length < 8)
  820. toosmall (_("version stringtable"));
  821. vst = (rc_ver_stringtable *) res_alloc (sizeof (rc_ver_stringtable));
  822. get_version_header (wrbfd, data, length, (const char *) NULL,
  823. &vst->language, &stverlen, &vallen, &type, &off);
  824. if (vallen != 0)
  825. fatal (_("unexpected version stringtable value length %ld"), (long) vallen);
  826. data += off;
  827. length -= off;
  828. verlen -= off;
  829. stverlen -= off;
  830. vst->strings = NULL;
  831. ppvs = &vst->strings;
  832. while (stverlen > 0)
  833. {
  834. rc_ver_stringinfo *vs;
  835. rc_uint_type sverlen, vslen, valoff;
  836. if (length < 8)
  837. toosmall (_("version string"));
  838. vs = (rc_ver_stringinfo *) res_alloc (sizeof (rc_ver_stringinfo));
  839. get_version_header (wrbfd, data, length, (const char *) NULL,
  840. &vs->key, &sverlen, &vallen, &type, &off);
  841. data += off;
  842. length -= off;
  843. vs->value = get_unicode (wrbfd, data, length, &vslen);
  844. valoff = vslen * 2 + 2;
  845. valoff = (valoff + 3) & ~3;
  846. if (off + valoff != sverlen)
  847. fatal (_("unexpected version string length %ld != %ld + %ld"),
  848. (long) sverlen, (long) off, (long) valoff);
  849. data += valoff;
  850. length -= valoff;
  851. if (stverlen < sverlen)
  852. fatal (_("unexpected version string length %ld < %ld"),
  853. (long) verlen, (long) sverlen);
  854. stverlen -= sverlen;
  855. verlen -= sverlen;
  856. vs->next = NULL;
  857. *ppvs = vs;
  858. ppvs = &vs->next;
  859. }
  860. vst->next = NULL;
  861. *ppvst = vst;
  862. ppvst = &vst->next;
  863. }
  864. }
  865. else if (ch == 'V')
  866. {
  867. rc_ver_varinfo **ppvv;
  868. vi->type = VERINFO_VAR;
  869. get_version_header (wrbfd, data, length, "VarFileInfo",
  870. (unichar **) NULL, &verlen, &vallen, &type,
  871. &off);
  872. if (vallen != 0)
  873. fatal (_("unexpected varfileinfo value length %ld"), (long) vallen);
  874. data += off;
  875. length -= off;
  876. get_version_header (wrbfd, data, length, (const char *) NULL,
  877. &vi->u.var.key, &verlen, &vallen, &type, &off);
  878. data += off;
  879. length -= off;
  880. vi->u.var.var = NULL;
  881. ppvv = &vi->u.var.var;
  882. while (vallen > 0)
  883. {
  884. rc_ver_varinfo *vv;
  885. if (length < 4)
  886. toosmall (_("version varfileinfo"));
  887. vv = (rc_ver_varinfo *) res_alloc (sizeof (rc_ver_varinfo));
  888. vv->language = windres_get_16 (wrbfd, data, 2);
  889. vv->charset = windres_get_16 (wrbfd, data + 2, 2);
  890. vv->next = NULL;
  891. *ppvv = vv;
  892. ppvv = &vv->next;
  893. data += 4;
  894. length -= 4;
  895. if (vallen < 4)
  896. fatal (_("unexpected version value length %ld"), (long) vallen);
  897. vallen -= 4;
  898. }
  899. }
  900. else
  901. fatal (_("unexpected version string"));
  902. vi->next = NULL;
  903. *pp = vi;
  904. pp = &vi->next;
  905. }
  906. v = (rc_versioninfo *) res_alloc (sizeof (rc_versioninfo));
  907. v->fixed = fi;
  908. v->var = first;
  909. r = (rc_res_resource *) res_alloc (sizeof *r);
  910. r->type = RES_TYPE_VERSIONINFO;
  911. r->u.versioninfo = v;
  912. return r;
  913. }
  914. /* Convert an arbitrary user defined resource from binary. */
  915. static rc_res_resource *
  916. bin_to_res_userdata (windres_bfd *wrbfd ATTRIBUTE_UNUSED, const bfd_byte *data,
  917. rc_uint_type length)
  918. {
  919. rc_rcdata_item *ri;
  920. rc_res_resource *r;
  921. ri = (rc_rcdata_item *) res_alloc (sizeof (rc_rcdata_item));
  922. ri->next = NULL;
  923. ri->type = RCDATA_BUFFER;
  924. ri->u.buffer.length = length;
  925. ri->u.buffer.data = data;
  926. r = (rc_res_resource *) res_alloc (sizeof *r);
  927. r->type = RES_TYPE_USERDATA;
  928. r->u.rcdata = ri;
  929. return r;
  930. }
  931. static rc_res_resource *
  932. bin_to_res_toolbar (windres_bfd *wrbfd, const bfd_byte *data, rc_uint_type length)
  933. {
  934. rc_toolbar *ri;
  935. rc_res_resource *r;
  936. rc_uint_type i;
  937. ri = (rc_toolbar *) res_alloc (sizeof (rc_toolbar));
  938. ri->button_width = windres_get_32 (wrbfd, data, 4);
  939. ri->button_height = windres_get_32 (wrbfd, data + 4, 4);
  940. ri->nitems = windres_get_32 (wrbfd, data + 8, 4);
  941. ri->items = NULL;
  942. data += 12;
  943. length -= 12;
  944. for (i=0 ; i < ri->nitems; i++)
  945. {
  946. rc_toolbar_item *it;
  947. it = (rc_toolbar_item *) res_alloc (sizeof (rc_toolbar_item));
  948. it->id.named = 0;
  949. it->id.u.id = (int) windres_get_32 (wrbfd, data, 4);
  950. it->prev = it->next = NULL;
  951. data += 4;
  952. length -= 4;
  953. if(ri->items) {
  954. rc_toolbar_item *ii = ri->items;
  955. while (ii->next != NULL)
  956. ii = ii->next;
  957. it->prev = ii;
  958. ii->next = it;
  959. }
  960. else
  961. ri->items = it;
  962. }
  963. r = (rc_res_resource *) res_alloc (sizeof *r);
  964. r->type = RES_TYPE_TOOLBAR;
  965. r->u.toolbar = ri;
  966. return r;
  967. }
  968. /* Local functions used to convert resources to binary format. */
  969. static rc_uint_type resid_to_bin (windres_bfd *, rc_uint_type, rc_res_id);
  970. static rc_uint_type unicode_to_bin (windres_bfd *, rc_uint_type, const unichar *);
  971. static rc_uint_type res_to_bin_accelerator (windres_bfd *, rc_uint_type, const rc_accelerator *);
  972. static rc_uint_type res_to_bin_cursor (windres_bfd *, rc_uint_type, const rc_cursor *);
  973. static rc_uint_type res_to_bin_group_cursor (windres_bfd *, rc_uint_type, const rc_group_cursor *);
  974. static rc_uint_type res_to_bin_dialog (windres_bfd *, rc_uint_type, const rc_dialog *);
  975. static rc_uint_type res_to_bin_fontdir (windres_bfd *, rc_uint_type, const rc_fontdir *);
  976. static rc_uint_type res_to_bin_group_icon (windres_bfd *, rc_uint_type, const rc_group_icon *);
  977. static rc_uint_type res_to_bin_menu (windres_bfd *, rc_uint_type, const rc_menu *);
  978. static rc_uint_type res_to_bin_menuitems (windres_bfd *, rc_uint_type, const rc_menuitem *);
  979. static rc_uint_type res_to_bin_menuexitems (windres_bfd *, rc_uint_type, const rc_menuitem *);
  980. static rc_uint_type res_to_bin_rcdata (windres_bfd *, rc_uint_type, const rc_rcdata_item *);
  981. static rc_uint_type res_to_bin_stringtable (windres_bfd *, rc_uint_type, const rc_stringtable *);
  982. static rc_uint_type string_to_unicode_bin (windres_bfd *, rc_uint_type, const char *);
  983. static rc_uint_type res_to_bin_toolbar (windres_bfd *, rc_uint_type, rc_toolbar *tb);
  984. static rc_uint_type res_to_bin_versioninfo (windres_bfd *, rc_uint_type, const rc_versioninfo *);
  985. static rc_uint_type res_to_bin_generic (windres_bfd *, rc_uint_type, rc_uint_type,
  986. const bfd_byte *);
  987. /* Convert a resource to binary. */
  988. rc_uint_type
  989. res_to_bin (windres_bfd *wrbfd, rc_uint_type off, const rc_res_resource *res)
  990. {
  991. switch (res->type)
  992. {
  993. case RES_TYPE_BITMAP:
  994. case RES_TYPE_FONT:
  995. case RES_TYPE_ICON:
  996. case RES_TYPE_MESSAGETABLE:
  997. return res_to_bin_generic (wrbfd, off, res->u.data.length, res->u.data.data);
  998. case RES_TYPE_ACCELERATOR:
  999. return res_to_bin_accelerator (wrbfd, off, res->u.acc);
  1000. case RES_TYPE_CURSOR:
  1001. return res_to_bin_cursor (wrbfd, off, res->u.cursor);
  1002. case RES_TYPE_GROUP_CURSOR:
  1003. return res_to_bin_group_cursor (wrbfd, off, res->u.group_cursor);
  1004. case RES_TYPE_DIALOG:
  1005. return res_to_bin_dialog (wrbfd, off, res->u.dialog);
  1006. case RES_TYPE_FONTDIR:
  1007. return res_to_bin_fontdir (wrbfd, off, res->u.fontdir);
  1008. case RES_TYPE_GROUP_ICON:
  1009. return res_to_bin_group_icon (wrbfd, off, res->u.group_icon);
  1010. case RES_TYPE_MENU:
  1011. return res_to_bin_menu (wrbfd, off, res->u.menu);
  1012. case RES_TYPE_STRINGTABLE:
  1013. return res_to_bin_stringtable (wrbfd, off, res->u.stringtable);
  1014. case RES_TYPE_VERSIONINFO:
  1015. return res_to_bin_versioninfo (wrbfd, off, res->u.versioninfo);
  1016. case RES_TYPE_TOOLBAR:
  1017. return res_to_bin_toolbar (wrbfd, off, res->u.toolbar);
  1018. case RES_TYPE_USERDATA:
  1019. case RES_TYPE_RCDATA:
  1020. default:
  1021. return res_to_bin_rcdata (wrbfd, off, res->u.rcdata);
  1022. }
  1023. }
  1024. /* Convert a resource ID to binary. This always returns exactly one
  1025. bindata structure. */
  1026. static rc_uint_type
  1027. resid_to_bin (windres_bfd *wrbfd, rc_uint_type off, rc_res_id id)
  1028. {
  1029. if (! id.named)
  1030. {
  1031. if (wrbfd)
  1032. {
  1033. struct bin_res_id bri;
  1034. windres_put_16 (wrbfd, bri.sig, 0xffff);
  1035. windres_put_16 (wrbfd, bri.id, id.u.id);
  1036. set_windres_bfd_content (wrbfd, &bri, off, BIN_RES_ID);
  1037. }
  1038. off += BIN_RES_ID;
  1039. }
  1040. else
  1041. {
  1042. rc_uint_type len = (id.u.n.name ? unichar_len (id.u.n.name) : 0);
  1043. if (wrbfd)
  1044. {
  1045. bfd_byte *d = (bfd_byte *) reswr_alloc ((len + 1) * sizeof (unichar));
  1046. rc_uint_type i;
  1047. for (i = 0; i < len; i++)
  1048. windres_put_16 (wrbfd, d + (i * sizeof (unichar)), id.u.n.name[i]);
  1049. windres_put_16 (wrbfd, d + (len * sizeof (unichar)), 0);
  1050. set_windres_bfd_content (wrbfd, d, off, (len + 1) * sizeof (unichar));
  1051. }
  1052. off += (rc_uint_type) ((len + 1) * sizeof (unichar));
  1053. }
  1054. return off;
  1055. }
  1056. /* Convert a null terminated unicode string to binary. This always
  1057. returns exactly one bindata structure. */
  1058. static rc_uint_type
  1059. unicode_to_bin (windres_bfd *wrbfd, rc_uint_type off, const unichar *str)
  1060. {
  1061. rc_uint_type len = 0;
  1062. if (str != NULL)
  1063. len = unichar_len (str);
  1064. if (wrbfd)
  1065. {
  1066. bfd_byte *d;
  1067. rc_uint_type i;
  1068. d = (bfd_byte *) reswr_alloc ( (len + 1) * sizeof (unichar));
  1069. for (i = 0; i < len; i++)
  1070. windres_put_16 (wrbfd, d + (i * sizeof (unichar)), str[i]);
  1071. windres_put_16 (wrbfd, d + (len * sizeof (unichar)), 0);
  1072. set_windres_bfd_content (wrbfd, d, off, (len + 1) * sizeof (unichar));
  1073. }
  1074. off += (rc_uint_type) ((len + 1) * sizeof (unichar));
  1075. return off;
  1076. }
  1077. /* Convert an accelerator resource to binary. */
  1078. static rc_uint_type
  1079. res_to_bin_accelerator (windres_bfd *wrbfd, rc_uint_type off,
  1080. const rc_accelerator *accelerators)
  1081. {
  1082. const rc_accelerator *a;
  1083. for (a = accelerators; a != NULL; a = a->next)
  1084. {
  1085. if (wrbfd)
  1086. {
  1087. struct bin_accelerator ba;
  1088. windres_put_16 (wrbfd, ba.flags, a->flags | (a->next != NULL ? 0 : ACC_LAST));
  1089. windres_put_16 (wrbfd, ba.key, a->key);
  1090. windres_put_16 (wrbfd, ba.id, a->id);
  1091. windres_put_16 (wrbfd, ba.pad, 0);
  1092. set_windres_bfd_content (wrbfd, &ba, off, BIN_ACCELERATOR_SIZE);
  1093. }
  1094. off += BIN_ACCELERATOR_SIZE;
  1095. }
  1096. return off;
  1097. }
  1098. /* Convert a cursor resource to binary. */
  1099. static rc_uint_type
  1100. res_to_bin_cursor (windres_bfd *wrbfd, rc_uint_type off, const rc_cursor *c)
  1101. {
  1102. if (wrbfd)
  1103. {
  1104. struct bin_cursor bc;
  1105. windres_put_16 (wrbfd, bc.xhotspot, c->xhotspot);
  1106. windres_put_16 (wrbfd, bc.yhotspot, c->yhotspot);
  1107. set_windres_bfd_content (wrbfd, &bc, off, BIN_CURSOR_SIZE);
  1108. if (c->length)
  1109. set_windres_bfd_content (wrbfd, c->data, off + BIN_CURSOR_SIZE, c->length);
  1110. }
  1111. off = (off + BIN_CURSOR_SIZE + (rc_uint_type) c->length);
  1112. return off;
  1113. }
  1114. /* Convert a group cursor resource to binary. */
  1115. static rc_uint_type
  1116. res_to_bin_group_cursor (windres_bfd *wrbfd, rc_uint_type off,
  1117. const rc_group_cursor *group_cursors)
  1118. {
  1119. int c = 0;
  1120. const rc_group_cursor *gc;
  1121. struct bin_group_cursor bgc;
  1122. struct bin_group_cursor_item bgci;
  1123. rc_uint_type start = off;
  1124. off += BIN_GROUP_CURSOR_SIZE;
  1125. for (c = 0, gc = group_cursors; gc != NULL; gc = gc->next, c++)
  1126. {
  1127. if (wrbfd)
  1128. {
  1129. windres_put_16 (wrbfd, bgci.width, gc->width);
  1130. windres_put_16 (wrbfd, bgci.height, gc->height);
  1131. windres_put_16 (wrbfd, bgci.planes, gc->planes);
  1132. windres_put_16 (wrbfd, bgci.bits, gc->bits);
  1133. windres_put_32 (wrbfd, bgci.bytes, gc->bytes);
  1134. windres_put_16 (wrbfd, bgci.index, gc->index);
  1135. set_windres_bfd_content (wrbfd, &bgci, off, BIN_GROUP_CURSOR_ITEM_SIZE);
  1136. }
  1137. off += BIN_GROUP_CURSOR_ITEM_SIZE;
  1138. }
  1139. if (wrbfd)
  1140. {
  1141. windres_put_16 (wrbfd, bgc.sig1, 0);
  1142. windres_put_16 (wrbfd, bgc.sig2, 2);
  1143. windres_put_16 (wrbfd, bgc.nitems, c);
  1144. set_windres_bfd_content (wrbfd, &bgc, start, BIN_GROUP_CURSOR_SIZE);
  1145. }
  1146. return off;
  1147. }
  1148. /* Convert a dialog resource to binary. */
  1149. static rc_uint_type
  1150. res_to_bin_dialog (windres_bfd *wrbfd, rc_uint_type off, const rc_dialog *dialog)
  1151. {
  1152. rc_uint_type off_delta;
  1153. rc_uint_type start, marker;
  1154. int dialogex;
  1155. int c;
  1156. rc_dialog_control *dc;
  1157. struct bin_dialogex bdx;
  1158. struct bin_dialog bd;
  1159. off_delta = off;
  1160. start = off;
  1161. dialogex = extended_dialog (dialog);
  1162. if (wrbfd)
  1163. {
  1164. if (! dialogex)
  1165. {
  1166. windres_put_32 (wrbfd, bd.style, dialog->style);
  1167. windres_put_32 (wrbfd, bd.exstyle, dialog->exstyle);
  1168. windres_put_16 (wrbfd, bd.x, dialog->x);
  1169. windres_put_16 (wrbfd, bd.y, dialog->y);
  1170. windres_put_16 (wrbfd, bd.width, dialog->width);
  1171. windres_put_16 (wrbfd, bd.height, dialog->height);
  1172. }
  1173. else
  1174. {
  1175. windres_put_16 (wrbfd, bdx.sig1, 1);
  1176. windres_put_16 (wrbfd, bdx.sig2, 0xffff);
  1177. windres_put_32 (wrbfd, bdx.help, (dialog->ex ? dialog->ex->help : 0));
  1178. windres_put_32 (wrbfd, bdx.exstyle, dialog->exstyle);
  1179. windres_put_32 (wrbfd, bdx.style, dialog->style);
  1180. windres_put_16 (wrbfd, bdx.x, dialog->x);
  1181. windres_put_16 (wrbfd, bdx.y, dialog->y);
  1182. windres_put_16 (wrbfd, bdx.width, dialog->width);
  1183. windres_put_16 (wrbfd, bdx.height, dialog->height);
  1184. }
  1185. }
  1186. off += (dialogex != 0 ? BIN_DIALOGEX_SIZE : BIN_DIALOG_SIZE);
  1187. off = resid_to_bin (wrbfd, off, dialog->menu);
  1188. off = resid_to_bin (wrbfd, off, dialog->class);
  1189. off = unicode_to_bin (wrbfd, off, dialog->caption);
  1190. if ((dialog->style & DS_SETFONT) != 0)
  1191. {
  1192. if (wrbfd)
  1193. {
  1194. if (! dialogex)
  1195. {
  1196. struct bin_dialogfont bdf;
  1197. windres_put_16 (wrbfd, bdf.pointsize, dialog->pointsize);
  1198. set_windres_bfd_content (wrbfd, &bdf, off, BIN_DIALOGFONT_SIZE);
  1199. }
  1200. else
  1201. {
  1202. struct bin_dialogexfont bdxf;
  1203. windres_put_16 (wrbfd, bdxf.pointsize, dialog->pointsize);
  1204. windres_put_16 (wrbfd, bdxf.weight, (dialog->ex == NULL ? 0 : dialog->ex->weight));
  1205. windres_put_8 (wrbfd, bdxf.italic, (dialog->ex == NULL ? 0 : dialog->ex->italic));
  1206. windres_put_8 (wrbfd, bdxf.charset, (dialog->ex == NULL ? 1 : dialog->ex->charset));
  1207. set_windres_bfd_content (wrbfd, &bdxf, off, BIN_DIALOGEXFONT_SIZE);
  1208. }
  1209. }
  1210. off += (dialogex ? BIN_DIALOGEXFONT_SIZE : BIN_DIALOGFONT_SIZE);
  1211. off = unicode_to_bin (wrbfd, off, dialog->font);
  1212. }
  1213. for (c = 0, dc = dialog->controls; dc != NULL; dc = dc->next, c++)
  1214. {
  1215. bfd_byte dc_rclen[2];
  1216. off += (4 - ((off - off_delta) & 3)) & 3;
  1217. if (wrbfd)
  1218. {
  1219. if (! dialogex)
  1220. {
  1221. struct bin_dialog_control bdc;
  1222. windres_put_32 (wrbfd, bdc.style, dc->style);
  1223. windres_put_32 (wrbfd, bdc.exstyle, dc->exstyle);
  1224. windres_put_16 (wrbfd, bdc.x, dc->x);
  1225. windres_put_16 (wrbfd, bdc.y, dc->y);
  1226. windres_put_16 (wrbfd, bdc.width, dc->width);
  1227. windres_put_16 (wrbfd, bdc.height, dc->height);
  1228. windres_put_16 (wrbfd, bdc.id, dc->id);
  1229. set_windres_bfd_content (wrbfd, &bdc, off, BIN_DIALOG_CONTROL_SIZE);
  1230. }
  1231. else
  1232. {
  1233. struct bin_dialogex_control bdc;
  1234. windres_put_32 (wrbfd, bdc.help, dc->help);
  1235. windres_put_32 (wrbfd, bdc.exstyle, dc->exstyle);
  1236. windres_put_32 (wrbfd, bdc.style, dc->style);
  1237. windres_put_16 (wrbfd, bdc.x, dc->x);
  1238. windres_put_16 (wrbfd, bdc.y, dc->y);
  1239. windres_put_16 (wrbfd, bdc.width, dc->width);
  1240. windres_put_16 (wrbfd, bdc.height, dc->height);
  1241. windres_put_32 (wrbfd, bdc.id, dc->id);
  1242. set_windres_bfd_content (wrbfd, &bdc, off, BIN_DIALOGEX_CONTROL_SIZE);
  1243. }
  1244. }
  1245. off += (dialogex != 0 ? BIN_DIALOGEX_CONTROL_SIZE : BIN_DIALOG_CONTROL_SIZE);
  1246. off = resid_to_bin (wrbfd, off, dc->class);
  1247. off = resid_to_bin (wrbfd, off, dc->text);
  1248. marker = off; /* Save two bytes for size of optional data. */
  1249. off += 2;
  1250. if (dc->data == NULL)
  1251. {
  1252. if (wrbfd)
  1253. windres_put_16 (wrbfd, dc_rclen, 0);
  1254. }
  1255. else
  1256. {
  1257. rc_uint_type saved_off = off;
  1258. rc_uint_type old_off;
  1259. off += (4 - ((off - off_delta) & 3)) & 3;
  1260. old_off = off;
  1261. off = res_to_bin_rcdata (wrbfd, off, dc->data);
  1262. if ((off - old_off) == 0)
  1263. old_off = off = saved_off;
  1264. if (wrbfd)
  1265. windres_put_16 (wrbfd, dc_rclen, off - old_off);
  1266. }
  1267. if (wrbfd)
  1268. set_windres_bfd_content (wrbfd, dc_rclen, marker, 2);
  1269. }
  1270. if (wrbfd)
  1271. {
  1272. windres_put_16 (wrbfd, (dialogex != 0 ? bdx.off : bd.off), c);
  1273. if (! dialogex)
  1274. set_windres_bfd_content (wrbfd, &bd, start, BIN_DIALOG_SIZE);
  1275. else
  1276. set_windres_bfd_content (wrbfd, &bdx, start, BIN_DIALOGEX_SIZE);
  1277. }
  1278. return off;
  1279. }
  1280. /* Convert a fontdir resource to binary. */
  1281. static rc_uint_type
  1282. res_to_bin_fontdir (windres_bfd *wrbfd, rc_uint_type off, const rc_fontdir *fontdirs)
  1283. {
  1284. rc_uint_type start;
  1285. int c;
  1286. const rc_fontdir *fd;
  1287. start = off;
  1288. off += 2;
  1289. for (c = 0, fd = fontdirs; fd != NULL; fd = fd->next, c++)
  1290. {
  1291. if (wrbfd)
  1292. {
  1293. bfd_byte d[2];
  1294. windres_put_16 (wrbfd, d, fd->index);
  1295. set_windres_bfd_content (wrbfd, d, off, 2);
  1296. if (fd->length)
  1297. set_windres_bfd_content (wrbfd, fd->data, off + 2, fd->length);
  1298. }
  1299. off += (rc_uint_type) fd->length + 2;
  1300. }
  1301. if (wrbfd)
  1302. {
  1303. bfd_byte d[2];
  1304. windres_put_16 (wrbfd, d, c);
  1305. set_windres_bfd_content (wrbfd, d, start, 2);
  1306. }
  1307. return off;
  1308. }
  1309. /* Convert a group icon resource to binary. */
  1310. static rc_uint_type
  1311. res_to_bin_group_icon (windres_bfd *wrbfd, rc_uint_type off, const rc_group_icon *group_icons)
  1312. {
  1313. rc_uint_type start;
  1314. struct bin_group_icon bgi;
  1315. int c;
  1316. const rc_group_icon *gi;
  1317. start = off;
  1318. off += BIN_GROUP_ICON_SIZE;
  1319. for (c = 0, gi = group_icons; gi != NULL; gi = gi->next, c++)
  1320. {
  1321. struct bin_group_icon_item bgii;
  1322. if (wrbfd)
  1323. {
  1324. windres_put_8 (wrbfd, bgii.width, gi->width);
  1325. windres_put_8 (wrbfd, bgii.height, gi->height);
  1326. windres_put_8 (wrbfd, bgii.colors, gi->colors);
  1327. windres_put_8 (wrbfd, bgii.pad, 0);
  1328. windres_put_16 (wrbfd, bgii.planes, gi->planes);
  1329. windres_put_16 (wrbfd, bgii.bits, gi->bits);
  1330. windres_put_32 (wrbfd, bgii.bytes, gi->bytes);
  1331. windres_put_16 (wrbfd, bgii.index, gi->index);
  1332. set_windres_bfd_content (wrbfd, &bgii, off, BIN_GROUP_ICON_ITEM_SIZE);
  1333. }
  1334. off += BIN_GROUP_ICON_ITEM_SIZE;
  1335. }
  1336. if (wrbfd)
  1337. {
  1338. windres_put_16 (wrbfd, bgi.sig1, 0);
  1339. windres_put_16 (wrbfd, bgi.sig2, 1);
  1340. windres_put_16 (wrbfd, bgi.count, c);
  1341. set_windres_bfd_content (wrbfd, &bgi, start, BIN_GROUP_ICON_SIZE);
  1342. }
  1343. return off;
  1344. }
  1345. /* Convert a menu resource to binary. */
  1346. static rc_uint_type
  1347. res_to_bin_menu (windres_bfd *wrbfd, rc_uint_type off, const rc_menu *menu)
  1348. {
  1349. int menuex;
  1350. menuex = extended_menu (menu);
  1351. if (wrbfd)
  1352. {
  1353. if (! menuex)
  1354. {
  1355. struct bin_menu bm;
  1356. windres_put_16 (wrbfd, bm.sig1, 0);
  1357. windres_put_16 (wrbfd, bm.sig2, 0);
  1358. set_windres_bfd_content (wrbfd, &bm, off, BIN_MENU_SIZE);
  1359. }
  1360. else
  1361. {
  1362. struct bin_menuex bm;
  1363. windres_put_16 (wrbfd, bm.sig1, 1);
  1364. windres_put_16 (wrbfd, bm.sig2, 4);
  1365. windres_put_32 (wrbfd, bm.help, menu->help);
  1366. set_windres_bfd_content (wrbfd, &bm, off, BIN_MENUEX_SIZE);
  1367. }
  1368. }
  1369. off += (menuex != 0 ? BIN_MENUEX_SIZE : BIN_MENU_SIZE);
  1370. if (! menuex)
  1371. {
  1372. off = res_to_bin_menuitems (wrbfd, off, menu->items);
  1373. }
  1374. else
  1375. {
  1376. off = res_to_bin_menuexitems (wrbfd, off, menu->items);
  1377. }
  1378. return off;
  1379. }
  1380. /* Convert menu items to binary. */
  1381. static rc_uint_type
  1382. res_to_bin_menuitems (windres_bfd *wrbfd, rc_uint_type off, const rc_menuitem *items)
  1383. {
  1384. const rc_menuitem *mi;
  1385. for (mi = items; mi != NULL; mi = mi->next)
  1386. {
  1387. struct bin_menuitem bmi;
  1388. int flags;
  1389. flags = mi->type;
  1390. if (mi->next == NULL)
  1391. flags |= MENUITEM_ENDMENU;
  1392. if (mi->popup != NULL)
  1393. flags |= MENUITEM_POPUP;
  1394. if (wrbfd)
  1395. {
  1396. windres_put_16 (wrbfd, bmi.flags, flags);
  1397. if (mi->popup == NULL)
  1398. windres_put_16 (wrbfd, bmi.id, mi->id);
  1399. set_windres_bfd_content (wrbfd, &bmi, off,
  1400. mi->popup == NULL ? BIN_MENUITEM_SIZE
  1401. : BIN_MENUITEM_POPUP_SIZE);
  1402. }
  1403. off += (mi->popup == NULL ? BIN_MENUITEM_SIZE : BIN_MENUITEM_POPUP_SIZE);
  1404. off = unicode_to_bin (wrbfd, off, mi->text);
  1405. if (mi->popup != NULL)
  1406. {
  1407. off = res_to_bin_menuitems (wrbfd, off, mi->popup);
  1408. }
  1409. }
  1410. return off;
  1411. }
  1412. /* Convert menuex items to binary. */
  1413. static rc_uint_type
  1414. res_to_bin_menuexitems (windres_bfd *wrbfd, rc_uint_type off, const rc_menuitem *items)
  1415. {
  1416. rc_uint_type off_delta = off;
  1417. const rc_menuitem *mi;
  1418. for (mi = items; mi != NULL; mi = mi->next)
  1419. {
  1420. struct bin_menuitemex bmi;
  1421. int flags;
  1422. off += (4 - ((off - off_delta) & 3)) & 3;
  1423. flags = 0;
  1424. if (mi->next == NULL)
  1425. flags |= 0x80;
  1426. if (mi->popup != NULL)
  1427. flags |= 1;
  1428. if (wrbfd)
  1429. {
  1430. windres_put_32 (wrbfd, bmi.type, mi->type);
  1431. windres_put_32 (wrbfd, bmi.state, mi->state);
  1432. windres_put_32 (wrbfd, bmi.id, mi->id);
  1433. windres_put_16 (wrbfd, bmi.flags, flags);
  1434. set_windres_bfd_content (wrbfd, &bmi, off, BIN_MENUITEMEX_SIZE);
  1435. }
  1436. off += BIN_MENUITEMEX_SIZE;
  1437. off = unicode_to_bin (wrbfd, off, mi->text);
  1438. if (mi->popup != NULL)
  1439. {
  1440. bfd_byte help[4];
  1441. off += (4 - ((off - off_delta) & 3)) & 3;
  1442. if (wrbfd)
  1443. {
  1444. windres_put_32 (wrbfd, help, mi->help);
  1445. set_windres_bfd_content (wrbfd, help, off, 4);
  1446. }
  1447. off += 4;
  1448. off = res_to_bin_menuexitems (wrbfd, off, mi->popup);
  1449. }
  1450. }
  1451. return off;
  1452. }
  1453. /* Convert an rcdata resource to binary. This is also used to convert
  1454. other information which happens to be stored in rc_rcdata_item lists
  1455. to binary. */
  1456. static rc_uint_type
  1457. res_to_bin_rcdata (windres_bfd *wrbfd, rc_uint_type off, const rc_rcdata_item *items)
  1458. {
  1459. const rc_rcdata_item *ri;
  1460. for (ri = items; ri != NULL; ri = ri->next)
  1461. {
  1462. rc_uint_type len;
  1463. switch (ri->type)
  1464. {
  1465. default:
  1466. abort ();
  1467. case RCDATA_WORD:
  1468. len = 2;
  1469. break;
  1470. case RCDATA_DWORD:
  1471. len = 4;
  1472. break;
  1473. case RCDATA_STRING:
  1474. len = ri->u.string.length;
  1475. break;
  1476. case RCDATA_WSTRING:
  1477. len = ri->u.wstring.length * sizeof (unichar);
  1478. break;
  1479. case RCDATA_BUFFER:
  1480. len = ri->u.buffer.length;
  1481. break;
  1482. }
  1483. if (wrbfd)
  1484. {
  1485. bfd_byte h[4];
  1486. bfd_byte *hp = &h[0];
  1487. switch (ri->type)
  1488. {
  1489. case RCDATA_WORD:
  1490. windres_put_16 (wrbfd, hp, ri->u.word);
  1491. break;
  1492. case RCDATA_DWORD:
  1493. windres_put_32 (wrbfd, hp, ri->u.dword);
  1494. break;
  1495. case RCDATA_STRING:
  1496. hp = (bfd_byte *) ri->u.string.s;
  1497. break;
  1498. case RCDATA_WSTRING:
  1499. {
  1500. rc_uint_type i;
  1501. hp = (bfd_byte *) reswr_alloc (len);
  1502. for (i = 0; i < ri->u.wstring.length; i++)
  1503. windres_put_16 (wrbfd, hp + i * sizeof (unichar), ri->u.wstring.w[i]);
  1504. }
  1505. break;
  1506. case RCDATA_BUFFER:
  1507. hp = (bfd_byte *) ri->u.buffer.data;
  1508. break;
  1509. }
  1510. set_windres_bfd_content (wrbfd, hp, off, len);
  1511. }
  1512. off += len;
  1513. }
  1514. return off;
  1515. }
  1516. /* Convert a stringtable resource to binary. */
  1517. static rc_uint_type
  1518. res_to_bin_stringtable (windres_bfd *wrbfd, rc_uint_type off,
  1519. const rc_stringtable *st)
  1520. {
  1521. int i;
  1522. for (i = 0; i < 16; i++)
  1523. {
  1524. rc_uint_type slen, length;
  1525. unichar *s;
  1526. slen = (rc_uint_type) st->strings[i].length;
  1527. if (slen == 0xffffffff) slen = 0;
  1528. s = st->strings[i].string;
  1529. length = 2 + slen * 2;
  1530. if (wrbfd)
  1531. {
  1532. bfd_byte *hp;
  1533. rc_uint_type j;
  1534. hp = (bfd_byte *) reswr_alloc (length);
  1535. windres_put_16 (wrbfd, hp, slen);
  1536. for (j = 0; j < slen; j++)
  1537. windres_put_16 (wrbfd, hp + 2 + j * 2, s[j]);
  1538. set_windres_bfd_content (wrbfd, hp, off, length);
  1539. }
  1540. off += length;
  1541. }
  1542. return off;
  1543. }
  1544. /* Convert an ASCII string to a unicode binary string. This always
  1545. returns exactly one bindata structure. */
  1546. static rc_uint_type
  1547. string_to_unicode_bin (windres_bfd *wrbfd, rc_uint_type off, const char *s)
  1548. {
  1549. rc_uint_type len;
  1550. len = (rc_uint_type) strlen (s);
  1551. if (wrbfd)
  1552. {
  1553. rc_uint_type i;
  1554. bfd_byte *hp;
  1555. hp = (bfd_byte *) reswr_alloc ((len + 1) * sizeof (unichar));
  1556. for (i = 0; i < len; i++)
  1557. windres_put_16 (wrbfd, hp + i * 2, s[i]);
  1558. windres_put_16 (wrbfd, hp + i * 2, 0);
  1559. set_windres_bfd_content (wrbfd, hp, off, (len + 1) * sizeof (unichar));
  1560. }
  1561. off += (rc_uint_type) ((len + 1) * sizeof (unichar));
  1562. return off;
  1563. }
  1564. static rc_uint_type
  1565. res_to_bin_toolbar (windres_bfd *wrbfd, rc_uint_type off, rc_toolbar *tb)
  1566. {
  1567. if (wrbfd)
  1568. {
  1569. struct bin_toolbar bt;
  1570. windres_put_32 (wrbfd, bt.button_width, tb->button_width);
  1571. windres_put_32 (wrbfd, bt.button_height, tb->button_height);
  1572. windres_put_32 (wrbfd, bt.nitems, tb->nitems);
  1573. set_windres_bfd_content (wrbfd, &bt, off, BIN_TOOLBAR_SIZE);
  1574. if (tb->nitems > 0)
  1575. {
  1576. rc_toolbar_item *it;
  1577. bfd_byte *ids;
  1578. rc_uint_type i = 0;
  1579. ids = (bfd_byte *) reswr_alloc (tb->nitems * 4);
  1580. it=tb->items;
  1581. while(it != NULL)
  1582. {
  1583. windres_put_32 (wrbfd, ids + i, it->id.u.id);
  1584. i += 4;
  1585. it = it->next;
  1586. }
  1587. set_windres_bfd_content (wrbfd, ids, off + BIN_TOOLBAR_SIZE, i);
  1588. }
  1589. }
  1590. off += BIN_TOOLBAR_SIZE + tb->nitems * 4;
  1591. return off;
  1592. }
  1593. /* Convert a versioninfo resource to binary. */
  1594. static rc_uint_type
  1595. res_to_bin_versioninfo (windres_bfd *wrbfd, rc_uint_type off,
  1596. const rc_versioninfo *versioninfo)
  1597. {
  1598. rc_uint_type off_delta = off;
  1599. rc_uint_type start;
  1600. struct bin_versioninfo bvi;
  1601. rc_ver_info *vi;
  1602. start = off;
  1603. off += BIN_VERSIONINFO_SIZE;
  1604. off = string_to_unicode_bin (wrbfd, off, "VS_VERSION_INFO");
  1605. off += (4 - ((off - off_delta) & 3)) & 3;
  1606. if (versioninfo->fixed != NULL)
  1607. {
  1608. if (wrbfd)
  1609. {
  1610. struct bin_fixed_versioninfo bfv;
  1611. const rc_fixed_versioninfo *fi;
  1612. fi = versioninfo->fixed;
  1613. windres_put_32 (wrbfd, bfv.sig1, 0xfeef04bd);
  1614. windres_put_32 (wrbfd, bfv.sig2, 0x10000);
  1615. windres_put_32 (wrbfd, bfv.file_version, fi->file_version_ms);
  1616. windres_put_32 (wrbfd, bfv.file_version_ls, fi->file_version_ls);
  1617. windres_put_32 (wrbfd, bfv.product_version_ms, fi->product_version_ms);
  1618. windres_put_32 (wrbfd, bfv.product_version_ls, fi->product_version_ls);
  1619. windres_put_32 (wrbfd, bfv.file_flags_mask, fi->file_flags_mask);
  1620. windres_put_32 (wrbfd, bfv.file_flags, fi->file_flags);
  1621. windres_put_32 (wrbfd, bfv.file_os, fi->file_os);
  1622. windres_put_32 (wrbfd, bfv.file_type, fi->file_type);
  1623. windres_put_32 (wrbfd, bfv.file_subtype, fi->file_subtype);
  1624. windres_put_32 (wrbfd, bfv.file_date_ms, fi->file_date_ms);
  1625. windres_put_32 (wrbfd, bfv.file_date_ls, fi->file_date_ls);
  1626. set_windres_bfd_content (wrbfd, &bfv, off, BIN_FIXED_VERSIONINFO_SIZE);
  1627. }
  1628. off += BIN_FIXED_VERSIONINFO_SIZE;
  1629. }
  1630. for (vi = versioninfo->var; vi != NULL; vi = vi->next)
  1631. {
  1632. struct bin_ver_info bv;
  1633. rc_uint_type bv_off;
  1634. off += (4 - ((off - off_delta) & 3)) & 3;
  1635. bv_off = off;
  1636. off += BIN_VER_INFO_SIZE;
  1637. switch (vi->type)
  1638. {
  1639. default:
  1640. abort ();
  1641. case VERINFO_STRING:
  1642. {
  1643. const rc_ver_stringtable *vst;
  1644. off = string_to_unicode_bin (wrbfd, off, "StringFileInfo");
  1645. if (!vi->u.string.stringtables)
  1646. off += (4 - ((off - off_delta) & 3)) & 3;
  1647. for (vst = vi->u.string.stringtables; vst != NULL; vst = vst->next)
  1648. {
  1649. struct bin_ver_info bvst;
  1650. rc_uint_type vst_off;
  1651. const rc_ver_stringinfo *vs;
  1652. off += (4 - ((off - off_delta) & 3)) & 3;
  1653. vst_off = off;
  1654. off += BIN_VER_INFO_SIZE;
  1655. off = unicode_to_bin (wrbfd, off, vst->language);
  1656. for (vs = vst->strings; vs != NULL; vs = vs->next)
  1657. {
  1658. struct bin_ver_info bvs;
  1659. rc_uint_type vs_off, str_off;
  1660. off += (4 - ((off - off_delta) & 3)) & 3;
  1661. vs_off = off;
  1662. off += BIN_VER_INFO_SIZE;
  1663. off = unicode_to_bin (wrbfd, off, vs->key);
  1664. off += (4 - ((off - off_delta) & 3)) & 3;
  1665. str_off = off;
  1666. off = unicode_to_bin (wrbfd, off, vs->value);
  1667. if (wrbfd)
  1668. {
  1669. windres_put_16 (wrbfd, bvs.size, off - vs_off);
  1670. windres_put_16 (wrbfd, bvs.sig1, (off - str_off) / 2);
  1671. windres_put_16 (wrbfd, bvs.sig2, 1);
  1672. set_windres_bfd_content (wrbfd, &bvs, vs_off,
  1673. BIN_VER_INFO_SIZE);
  1674. }
  1675. }
  1676. if (wrbfd)
  1677. {
  1678. windres_put_16 (wrbfd, bvst.size, off - vst_off);
  1679. windres_put_16 (wrbfd, bvst.sig1, 0);
  1680. windres_put_16 (wrbfd, bvst.sig2, 1);
  1681. set_windres_bfd_content (wrbfd, &bvst, vst_off,
  1682. BIN_VER_INFO_SIZE);
  1683. }
  1684. }
  1685. break;
  1686. }
  1687. case VERINFO_VAR:
  1688. {
  1689. rc_uint_type vvd_off, vvvd_off;
  1690. struct bin_ver_info bvvd;
  1691. const rc_ver_varinfo *vv;
  1692. off = string_to_unicode_bin (wrbfd, off, "VarFileInfo");
  1693. off += (4 - ((off - off_delta) & 3)) & 3;
  1694. vvd_off = off;
  1695. off += BIN_VER_INFO_SIZE;
  1696. off = unicode_to_bin (wrbfd, off, vi->u.var.key);
  1697. off += (4 - ((off - off_delta) & 3)) & 3;
  1698. vvvd_off = off;
  1699. for (vv = vi->u.var.var; vv != NULL; vv = vv->next)
  1700. {
  1701. if (wrbfd)
  1702. {
  1703. bfd_byte vvsd[4];
  1704. windres_put_16 (wrbfd, &vvsd[0], vv->language);
  1705. windres_put_16 (wrbfd, &vvsd[2], vv->charset);
  1706. set_windres_bfd_content (wrbfd, vvsd, off, 4);
  1707. }
  1708. off += 4;
  1709. }
  1710. if (wrbfd)
  1711. {
  1712. windres_put_16 (wrbfd, bvvd.size, off - vvd_off);
  1713. windres_put_16 (wrbfd, bvvd.sig1, off - vvvd_off);
  1714. windres_put_16 (wrbfd, bvvd.sig2, 0);
  1715. set_windres_bfd_content (wrbfd, &bvvd, vvd_off,
  1716. BIN_VER_INFO_SIZE);
  1717. }
  1718. break;
  1719. }
  1720. }
  1721. if (wrbfd)
  1722. {
  1723. windres_put_16 (wrbfd, bv.size, off - bv_off);
  1724. windres_put_16 (wrbfd, bv.sig1, 0);
  1725. windres_put_16 (wrbfd, bv.sig2, 1);
  1726. set_windres_bfd_content (wrbfd, &bv, bv_off,
  1727. BIN_VER_INFO_SIZE);
  1728. }
  1729. }
  1730. if (wrbfd)
  1731. {
  1732. windres_put_16 (wrbfd, bvi.size, off - start);
  1733. windres_put_16 (wrbfd, bvi.fixed_size,
  1734. versioninfo->fixed == NULL ? 0
  1735. : BIN_FIXED_VERSIONINFO_SIZE);
  1736. windres_put_16 (wrbfd, bvi.sig2, 0);
  1737. set_windres_bfd_content (wrbfd, &bvi, start, BIN_VER_INFO_SIZE);
  1738. }
  1739. return off;
  1740. }
  1741. /* Convert a generic resource to binary. */
  1742. static rc_uint_type
  1743. res_to_bin_generic (windres_bfd *wrbfd, rc_uint_type off, rc_uint_type length,
  1744. const bfd_byte *data)
  1745. {
  1746. if (wrbfd && length != 0)
  1747. set_windres_bfd_content (wrbfd, data, off, length);
  1748. return off + (rc_uint_type) length;
  1749. }