resrc.c 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390
  1. /* resrc.c -- read and write Windows rc files.
  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 that read and write Windows rc files.
  19. These are text files that represent resources. */
  20. #include "sysdep.h"
  21. #include "bfd.h"
  22. #include "bucomm.h"
  23. #include "libiberty.h"
  24. #include "safe-ctype.h"
  25. #include "windres.h"
  26. #include <assert.h>
  27. #ifdef HAVE_SYS_WAIT_H
  28. #include <sys/wait.h>
  29. #else /* ! HAVE_SYS_WAIT_H */
  30. #if ! defined (_WIN32) || defined (__CYGWIN__)
  31. #ifndef WIFEXITED
  32. #define WIFEXITED(w) (((w)&0377) == 0)
  33. #endif
  34. #ifndef WIFSIGNALED
  35. #define WIFSIGNALED(w) (((w)&0377) != 0177 && ((w)&~0377) == 0)
  36. #endif
  37. #ifndef WTERMSIG
  38. #define WTERMSIG(w) ((w) & 0177)
  39. #endif
  40. #ifndef WEXITSTATUS
  41. #define WEXITSTATUS(w) (((w) >> 8) & 0377)
  42. #endif
  43. #else /* defined (_WIN32) && ! defined (__CYGWIN__) */
  44. #ifndef WIFEXITED
  45. #define WIFEXITED(w) (((w) & 0xff) == 0)
  46. #endif
  47. #ifndef WIFSIGNALED
  48. #define WIFSIGNALED(w) (((w) & 0xff) != 0 && ((w) & 0xff) != 0x7f)
  49. #endif
  50. #ifndef WTERMSIG
  51. #define WTERMSIG(w) ((w) & 0x7f)
  52. #endif
  53. #ifndef WEXITSTATUS
  54. #define WEXITSTATUS(w) (((w) & 0xff00) >> 8)
  55. #endif
  56. #endif /* defined (_WIN32) && ! defined (__CYGWIN__) */
  57. #endif /* ! HAVE_SYS_WAIT_H */
  58. #ifndef STDOUT_FILENO
  59. #define STDOUT_FILENO 1
  60. #endif
  61. #if defined (_WIN32) && ! defined (__CYGWIN__)
  62. #define popen _popen
  63. #define pclose _pclose
  64. #endif
  65. /* The default preprocessor. */
  66. #define DEFAULT_PREPROCESSOR "gcc -E -xc -DRC_INVOKED"
  67. /* We read the directory entries in a cursor or icon file into
  68. instances of this structure. */
  69. struct icondir
  70. {
  71. /* Width of image. */
  72. bfd_byte width;
  73. /* Height of image. */
  74. bfd_byte height;
  75. /* Number of colors in image. */
  76. bfd_byte colorcount;
  77. union
  78. {
  79. struct
  80. {
  81. /* Color planes. */
  82. unsigned short planes;
  83. /* Bits per pixel. */
  84. unsigned short bits;
  85. } icon;
  86. struct
  87. {
  88. /* X coordinate of hotspot. */
  89. unsigned short xhotspot;
  90. /* Y coordinate of hotspot. */
  91. unsigned short yhotspot;
  92. } cursor;
  93. } u;
  94. /* Bytes in image. */
  95. unsigned long bytes;
  96. /* File offset of image. */
  97. unsigned long offset;
  98. };
  99. /* The name of the rc file we are reading. */
  100. char *rc_filename;
  101. /* The line number in the rc file. */
  102. int rc_lineno;
  103. /* The pipe we are reading from, so that we can close it if we exit. */
  104. FILE *cpp_pipe;
  105. /* The temporary file used if we're not using popen, so we can delete it
  106. if we exit. */
  107. static char *cpp_temp_file;
  108. /* Input stream is either a file or a pipe. */
  109. static enum {ISTREAM_PIPE, ISTREAM_FILE} istream_type;
  110. /* As we read the rc file, we attach information to this structure. */
  111. static rc_res_directory *resources;
  112. /* The number of cursor resources we have written out. */
  113. static int cursors;
  114. /* The number of font resources we have written out. */
  115. static int fonts;
  116. /* Font directory information. */
  117. rc_fontdir *fontdirs;
  118. /* Resource info to use for fontdirs. */
  119. rc_res_res_info fontdirs_resinfo;
  120. /* The number of icon resources we have written out. */
  121. static int icons;
  122. /* The windres target bfd . */
  123. static windres_bfd wrtarget =
  124. {
  125. (bfd *) NULL, (asection *) NULL, WR_KIND_TARGET
  126. };
  127. /* Local functions for rcdata based resource definitions. */
  128. static void define_font_rcdata (rc_res_id, const rc_res_res_info *,
  129. rc_rcdata_item *);
  130. static void define_icon_rcdata (rc_res_id, const rc_res_res_info *,
  131. rc_rcdata_item *);
  132. static void define_bitmap_rcdata (rc_res_id, const rc_res_res_info *,
  133. rc_rcdata_item *);
  134. static void define_cursor_rcdata (rc_res_id, const rc_res_res_info *,
  135. rc_rcdata_item *);
  136. static void define_fontdir_rcdata (rc_res_id, const rc_res_res_info *,
  137. rc_rcdata_item *);
  138. static void define_messagetable_rcdata (rc_res_id, const rc_res_res_info *,
  139. rc_rcdata_item *);
  140. static rc_uint_type rcdata_copy (const rc_rcdata_item *, bfd_byte *);
  141. static bfd_byte *rcdata_render_as_buffer (const rc_rcdata_item *, rc_uint_type *);
  142. static int run_cmd (char *, const char *);
  143. static FILE *open_input_stream (char *);
  144. static FILE *look_for_default
  145. (char *, const char *, int, const char *, const char *);
  146. static void close_input_stream (void);
  147. static void unexpected_eof (const char *);
  148. static int get_word (FILE *, const char *);
  149. static unsigned long get_long (FILE *, const char *);
  150. static void get_data (FILE *, bfd_byte *, rc_uint_type, const char *);
  151. static void define_fontdirs (void);
  152. /* Run `cmd' and redirect the output to `redir'. */
  153. static int
  154. run_cmd (char *cmd, const char *redir)
  155. {
  156. char *s;
  157. int pid, wait_status, retcode;
  158. int i;
  159. const char **argv;
  160. char *errmsg_fmt, *errmsg_arg;
  161. char *temp_base = choose_temp_base ();
  162. int in_quote;
  163. char sep;
  164. int redir_handle = -1;
  165. int stdout_save = -1;
  166. /* Count the args. */
  167. i = 0;
  168. for (s = cmd; *s; s++)
  169. if (*s == ' ')
  170. i++;
  171. i++;
  172. argv = alloca (sizeof (char *) * (i + 3));
  173. i = 0;
  174. s = cmd;
  175. while (1)
  176. {
  177. while (*s == ' ' && *s != 0)
  178. s++;
  179. if (*s == 0)
  180. break;
  181. in_quote = (*s == '\'' || *s == '"');
  182. sep = (in_quote) ? *s++ : ' ';
  183. argv[i++] = s;
  184. while (*s != sep && *s != 0)
  185. s++;
  186. if (*s == 0)
  187. break;
  188. *s++ = 0;
  189. if (in_quote)
  190. s++;
  191. }
  192. argv[i++] = NULL;
  193. /* Setup the redirection. We can't use the usual fork/exec and redirect
  194. since we may be running on non-POSIX Windows host. */
  195. fflush (stdout);
  196. fflush (stderr);
  197. /* Open temporary output file. */
  198. redir_handle = open (redir, O_WRONLY | O_TRUNC | O_CREAT, 0666);
  199. if (redir_handle == -1)
  200. fatal (_("can't open temporary file `%s': %s"), redir,
  201. strerror (errno));
  202. /* Duplicate the stdout file handle so it can be restored later. */
  203. stdout_save = dup (STDOUT_FILENO);
  204. if (stdout_save == -1)
  205. fatal (_("can't redirect stdout: `%s': %s"), redir, strerror (errno));
  206. /* Redirect stdout to our output file. */
  207. dup2 (redir_handle, STDOUT_FILENO);
  208. pid = pexecute (argv[0], (char * const *) argv, program_name, temp_base,
  209. &errmsg_fmt, &errmsg_arg, PEXECUTE_ONE | PEXECUTE_SEARCH);
  210. /* Restore stdout to its previous setting. */
  211. dup2 (stdout_save, STDOUT_FILENO);
  212. /* Close response file. */
  213. close (redir_handle);
  214. if (pid == -1)
  215. {
  216. fatal ("%s %s: %s", errmsg_fmt, errmsg_arg, strerror (errno));
  217. return 1;
  218. }
  219. retcode = 0;
  220. pid = pwait (pid, &wait_status, 0);
  221. if (pid == -1)
  222. {
  223. fatal (_("wait: %s"), strerror (errno));
  224. retcode = 1;
  225. }
  226. else if (WIFSIGNALED (wait_status))
  227. {
  228. fatal (_("subprocess got fatal signal %d"), WTERMSIG (wait_status));
  229. retcode = 1;
  230. }
  231. else if (WIFEXITED (wait_status))
  232. {
  233. if (WEXITSTATUS (wait_status) != 0)
  234. {
  235. fatal (_("%s exited with status %d"), cmd,
  236. WEXITSTATUS (wait_status));
  237. retcode = 1;
  238. }
  239. }
  240. else
  241. retcode = 1;
  242. return retcode;
  243. }
  244. static FILE *
  245. open_input_stream (char *cmd)
  246. {
  247. if (istream_type == ISTREAM_FILE)
  248. {
  249. char *fileprefix;
  250. fileprefix = choose_temp_base ();
  251. cpp_temp_file = (char *) xmalloc (strlen (fileprefix) + 5);
  252. sprintf (cpp_temp_file, "%s.irc", fileprefix);
  253. free (fileprefix);
  254. if (run_cmd (cmd, cpp_temp_file))
  255. fatal (_("can't execute `%s': %s"), cmd, strerror (errno));
  256. cpp_pipe = fopen (cpp_temp_file, FOPEN_RT);
  257. if (cpp_pipe == NULL)
  258. fatal (_("can't open temporary file `%s': %s"),
  259. cpp_temp_file, strerror (errno));
  260. if (verbose)
  261. fprintf (stderr,
  262. _("Using temporary file `%s' to read preprocessor output\n"),
  263. cpp_temp_file);
  264. }
  265. else
  266. {
  267. cpp_pipe = popen (cmd, FOPEN_RT);
  268. if (cpp_pipe == NULL)
  269. fatal (_("can't popen `%s': %s"), cmd, strerror (errno));
  270. if (verbose)
  271. fprintf (stderr, _("Using popen to read preprocessor output\n"));
  272. }
  273. xatexit (close_input_stream);
  274. return cpp_pipe;
  275. }
  276. /* Determine if FILENAME contains special characters that
  277. can cause problems unless the entire filename is quoted. */
  278. static int
  279. filename_need_quotes (const char *filename)
  280. {
  281. if (filename == NULL || (filename[0] == '-' && filename[1] == 0))
  282. return 0;
  283. while (*filename != 0)
  284. {
  285. switch (*filename)
  286. {
  287. case '&':
  288. case ' ':
  289. case '<':
  290. case '>':
  291. case '|':
  292. case '%':
  293. return 1;
  294. }
  295. ++filename;
  296. }
  297. return 0;
  298. }
  299. /* Look for the preprocessor program. */
  300. static FILE *
  301. look_for_default (char *cmd, const char *prefix, int end_prefix,
  302. const char *preprocargs, const char *filename)
  303. {
  304. char *space;
  305. int found;
  306. struct stat s;
  307. const char *fnquotes = (filename_need_quotes (filename) ? "\"" : "");
  308. strcpy (cmd, prefix);
  309. sprintf (cmd + end_prefix, "%s", DEFAULT_PREPROCESSOR);
  310. space = strchr (cmd + end_prefix, ' ');
  311. if (space)
  312. *space = 0;
  313. if (
  314. #if defined (__DJGPP__) || defined (__CYGWIN__) || defined (_WIN32)
  315. strchr (cmd, '\\') ||
  316. #endif
  317. strchr (cmd, '/'))
  318. {
  319. found = (stat (cmd, &s) == 0
  320. #ifdef HAVE_EXECUTABLE_SUFFIX
  321. || stat (strcat (cmd, EXECUTABLE_SUFFIX), &s) == 0
  322. #endif
  323. );
  324. if (! found)
  325. {
  326. if (verbose)
  327. fprintf (stderr, _("Tried `%s'\n"), cmd);
  328. return NULL;
  329. }
  330. }
  331. strcpy (cmd, prefix);
  332. sprintf (cmd + end_prefix, "%s %s %s%s%s",
  333. DEFAULT_PREPROCESSOR, preprocargs, fnquotes, filename, fnquotes);
  334. if (verbose)
  335. fprintf (stderr, _("Using `%s'\n"), cmd);
  336. cpp_pipe = open_input_stream (cmd);
  337. return cpp_pipe;
  338. }
  339. /* Read an rc file. */
  340. rc_res_directory *
  341. read_rc_file (const char *filename, const char *preprocessor,
  342. const char *preprocargs, int language, int use_temp_file)
  343. {
  344. char *cmd;
  345. const char *fnquotes = (filename_need_quotes (filename) ? "\"" : "");
  346. if (filename == NULL)
  347. filename = "-";
  348. /* Setup the default resource import path taken from input file. */
  349. else if (strchr (filename, '/') != NULL || strchr (filename, '\\') != NULL)
  350. {
  351. char *edit, *dir;
  352. if (filename[0] == '/'
  353. || filename[0] == '\\'
  354. || filename[1] == ':')
  355. /* Absolute path. */
  356. edit = dir = xstrdup (filename);
  357. else
  358. {
  359. /* Relative path. */
  360. edit = dir = xmalloc (strlen (filename) + 3);
  361. sprintf (dir, "./%s", filename);
  362. }
  363. /* Walk dir backwards stopping at the first directory separator. */
  364. edit += strlen (dir);
  365. while (edit > dir && (edit[-1] != '\\' && edit[-1] != '/'))
  366. {
  367. --edit;
  368. edit[0] = 0;
  369. }
  370. /* Cut off trailing slash. */
  371. --edit;
  372. edit[0] = 0;
  373. /* Convert all back slashes to forward slashes. */
  374. while ((edit = strchr (dir, '\\')) != NULL)
  375. *edit = '/';
  376. windres_add_include_dir (dir);
  377. }
  378. istream_type = (use_temp_file) ? ISTREAM_FILE : ISTREAM_PIPE;
  379. if (preprocargs == NULL)
  380. preprocargs = "";
  381. if (preprocessor)
  382. {
  383. cmd = xmalloc (strlen (preprocessor)
  384. + strlen (preprocargs)
  385. + strlen (filename)
  386. + strlen (fnquotes) * 2
  387. + 10);
  388. sprintf (cmd, "%s %s %s%s%s", preprocessor, preprocargs,
  389. fnquotes, filename, fnquotes);
  390. cpp_pipe = open_input_stream (cmd);
  391. }
  392. else
  393. {
  394. char *dash, *slash, *cp;
  395. preprocessor = DEFAULT_PREPROCESSOR;
  396. cmd = xmalloc (strlen (program_name)
  397. + strlen (preprocessor)
  398. + strlen (preprocargs)
  399. + strlen (filename)
  400. + strlen (fnquotes) * 2
  401. #ifdef HAVE_EXECUTABLE_SUFFIX
  402. + strlen (EXECUTABLE_SUFFIX)
  403. #endif
  404. + 10);
  405. dash = slash = 0;
  406. for (cp = program_name; *cp; cp++)
  407. {
  408. if (*cp == '-')
  409. dash = cp;
  410. if (
  411. #if defined (__DJGPP__) || defined (__CYGWIN__) || defined(_WIN32)
  412. *cp == ':' || *cp == '\\' ||
  413. #endif
  414. *cp == '/')
  415. {
  416. slash = cp;
  417. dash = 0;
  418. }
  419. }
  420. cpp_pipe = 0;
  421. if (dash)
  422. {
  423. /* First, try looking for a prefixed gcc in the windres
  424. directory, with the same prefix as windres */
  425. cpp_pipe = look_for_default (cmd, program_name, dash - program_name + 1,
  426. preprocargs, filename);
  427. }
  428. if (slash && ! cpp_pipe)
  429. {
  430. /* Next, try looking for a gcc in the same directory as
  431. that windres */
  432. cpp_pipe = look_for_default (cmd, program_name, slash - program_name + 1,
  433. preprocargs, filename);
  434. }
  435. if (! cpp_pipe)
  436. {
  437. /* Sigh, try the default */
  438. cpp_pipe = look_for_default (cmd, "", 0, preprocargs, filename);
  439. }
  440. }
  441. free (cmd);
  442. rc_filename = xstrdup (filename);
  443. rc_lineno = 1;
  444. if (language != -1)
  445. rcparse_set_language (language);
  446. yyparse ();
  447. rcparse_discard_strings ();
  448. close_input_stream ();
  449. if (fontdirs != NULL)
  450. define_fontdirs ();
  451. free (rc_filename);
  452. rc_filename = NULL;
  453. return resources;
  454. }
  455. /* Close the input stream if it is open. */
  456. static void
  457. close_input_stream (void)
  458. {
  459. if (istream_type == ISTREAM_FILE)
  460. {
  461. if (cpp_pipe != NULL)
  462. fclose (cpp_pipe);
  463. if (cpp_temp_file != NULL)
  464. {
  465. int errno_save = errno;
  466. unlink (cpp_temp_file);
  467. errno = errno_save;
  468. free (cpp_temp_file);
  469. }
  470. }
  471. else
  472. {
  473. if (cpp_pipe != NULL)
  474. {
  475. int err;
  476. err = pclose (cpp_pipe);
  477. /* We are reading from a pipe, therefore we don't
  478. know if cpp failed or succeeded until pclose. */
  479. if (err != 0 || errno == ECHILD)
  480. {
  481. /* Since this is also run via xatexit, safeguard. */
  482. cpp_pipe = NULL;
  483. cpp_temp_file = NULL;
  484. fatal (_("preprocessing failed."));
  485. }
  486. }
  487. }
  488. /* Since this is also run via xatexit, safeguard. */
  489. cpp_pipe = NULL;
  490. cpp_temp_file = NULL;
  491. }
  492. /* Report an error while reading an rc file. */
  493. void
  494. yyerror (const char *msg)
  495. {
  496. fatal ("%s:%d: %s", rc_filename, rc_lineno, msg);
  497. }
  498. /* Issue a warning while reading an rc file. */
  499. void
  500. rcparse_warning (const char *msg)
  501. {
  502. fprintf (stderr, "%s:%d: %s\n", rc_filename, rc_lineno, msg);
  503. }
  504. /* Die if we get an unexpected end of file. */
  505. static void
  506. unexpected_eof (const char *msg)
  507. {
  508. fatal (_("%s: unexpected EOF"), msg);
  509. }
  510. /* Read a 16 bit word from a file. The data is assumed to be little
  511. endian. */
  512. static int
  513. get_word (FILE *e, const char *msg)
  514. {
  515. int b1, b2;
  516. b1 = getc (e);
  517. b2 = getc (e);
  518. if (feof (e))
  519. unexpected_eof (msg);
  520. return ((b2 & 0xff) << 8) | (b1 & 0xff);
  521. }
  522. /* Read a 32 bit word from a file. The data is assumed to be little
  523. endian. */
  524. static unsigned long
  525. get_long (FILE *e, const char *msg)
  526. {
  527. int b1, b2, b3, b4;
  528. b1 = getc (e);
  529. b2 = getc (e);
  530. b3 = getc (e);
  531. b4 = getc (e);
  532. if (feof (e))
  533. unexpected_eof (msg);
  534. return (((((((b4 & 0xff) << 8)
  535. | (b3 & 0xff)) << 8)
  536. | (b2 & 0xff)) << 8)
  537. | (b1 & 0xff));
  538. }
  539. /* Read data from a file. This is a wrapper to do error checking. */
  540. static void
  541. get_data (FILE *e, bfd_byte *p, rc_uint_type c, const char *msg)
  542. {
  543. rc_uint_type got; // $$$d
  544. got = (rc_uint_type) fread (p, 1, c, e);
  545. if (got == c)
  546. return;
  547. fatal (_("%s: read of %lu returned %lu"),
  548. msg, (unsigned long) c, (unsigned long) got);
  549. }
  550. /* Define an accelerator resource. */
  551. void
  552. define_accelerator (rc_res_id id, const rc_res_res_info *resinfo,
  553. rc_accelerator *data)
  554. {
  555. rc_res_resource *r;
  556. r = define_standard_resource (&resources, RT_ACCELERATOR, id,
  557. resinfo->language, 0);
  558. r->type = RES_TYPE_ACCELERATOR;
  559. r->u.acc = data;
  560. r->res_info = *resinfo;
  561. }
  562. /* Define a bitmap resource. Bitmap data is stored in a file. The
  563. first 14 bytes of the file are a standard header, which is not
  564. included in the resource data. */
  565. #define BITMAP_SKIP (14)
  566. void
  567. define_bitmap (rc_res_id id, const rc_res_res_info *resinfo,
  568. const char *filename)
  569. {
  570. FILE *e;
  571. char *real_filename;
  572. struct stat s;
  573. bfd_byte *data;
  574. rc_uint_type i;
  575. rc_res_resource *r;
  576. e = open_file_search (filename, FOPEN_RB, "bitmap file", &real_filename);
  577. if (stat (real_filename, &s) < 0)
  578. fatal (_("stat failed on bitmap file `%s': %s"), real_filename,
  579. strerror (errno));
  580. data = (bfd_byte *) res_alloc (s.st_size - BITMAP_SKIP);
  581. for (i = 0; i < BITMAP_SKIP; i++)
  582. getc (e);
  583. get_data (e, data, s.st_size - BITMAP_SKIP, real_filename);
  584. fclose (e);
  585. free (real_filename);
  586. r = define_standard_resource (&resources, RT_BITMAP, id,
  587. resinfo->language, 0);
  588. r->type = RES_TYPE_BITMAP;
  589. r->u.data.length = s.st_size - BITMAP_SKIP;
  590. r->u.data.data = data;
  591. r->res_info = *resinfo;
  592. }
  593. /* Define a cursor resource. A cursor file may contain a set of
  594. bitmaps, each representing the same cursor at various different
  595. resolutions. They each get written out with a different ID. The
  596. real cursor resource is then a group resource which can be used to
  597. select one of the actual cursors. */
  598. void
  599. define_cursor (rc_res_id id, const rc_res_res_info *resinfo,
  600. const char *filename)
  601. {
  602. FILE *e;
  603. char *real_filename;
  604. int type, count, i;
  605. struct icondir *icondirs;
  606. int first_cursor;
  607. rc_res_resource *r;
  608. rc_group_cursor *first, **pp;
  609. e = open_file_search (filename, FOPEN_RB, "cursor file", &real_filename);
  610. /* A cursor file is basically an icon file. The start of the file
  611. is a three word structure. The first word is ignored. The
  612. second word is the type of data. The third word is the number of
  613. entries. */
  614. get_word (e, real_filename);
  615. type = get_word (e, real_filename);
  616. count = get_word (e, real_filename);
  617. if (type != 2)
  618. fatal (_("cursor file `%s' does not contain cursor data"), real_filename);
  619. /* Read in the icon directory entries. */
  620. icondirs = (struct icondir *) xmalloc (count * sizeof *icondirs);
  621. for (i = 0; i < count; i++)
  622. {
  623. icondirs[i].width = getc (e);
  624. icondirs[i].height = getc (e);
  625. icondirs[i].colorcount = getc (e);
  626. getc (e);
  627. icondirs[i].u.cursor.xhotspot = get_word (e, real_filename);
  628. icondirs[i].u.cursor.yhotspot = get_word (e, real_filename);
  629. icondirs[i].bytes = get_long (e, real_filename);
  630. icondirs[i].offset = get_long (e, real_filename);
  631. if (feof (e))
  632. unexpected_eof (real_filename);
  633. }
  634. /* Define each cursor as a unique resource. */
  635. first_cursor = cursors;
  636. for (i = 0; i < count; i++)
  637. {
  638. bfd_byte *data;
  639. rc_res_id name;
  640. rc_cursor *c;
  641. if (fseek (e, icondirs[i].offset, SEEK_SET) != 0)
  642. fatal (_("%s: fseek to %lu failed: %s"), real_filename,
  643. icondirs[i].offset, strerror (errno));
  644. data = (bfd_byte *) res_alloc (icondirs[i].bytes);
  645. get_data (e, data, icondirs[i].bytes, real_filename);
  646. c = (rc_cursor *) res_alloc (sizeof (rc_cursor));
  647. c->xhotspot = icondirs[i].u.cursor.xhotspot;
  648. c->yhotspot = icondirs[i].u.cursor.yhotspot;
  649. c->length = icondirs[i].bytes;
  650. c->data = data;
  651. ++cursors;
  652. name.named = 0;
  653. name.u.id = cursors;
  654. r = define_standard_resource (&resources, RT_CURSOR, name,
  655. resinfo->language, 0);
  656. r->type = RES_TYPE_CURSOR;
  657. r->u.cursor = c;
  658. r->res_info = *resinfo;
  659. }
  660. fclose (e);
  661. free (real_filename);
  662. /* Define a cursor group resource. */
  663. first = NULL;
  664. pp = &first;
  665. for (i = 0; i < count; i++)
  666. {
  667. rc_group_cursor *cg;
  668. cg = (rc_group_cursor *) res_alloc (sizeof (rc_group_cursor));
  669. cg->next = NULL;
  670. cg->width = icondirs[i].width;
  671. cg->height = 2 * icondirs[i].height;
  672. /* FIXME: What should these be set to? */
  673. cg->planes = 1;
  674. cg->bits = 1;
  675. cg->bytes = icondirs[i].bytes + 4;
  676. cg->index = first_cursor + i + 1;
  677. *pp = cg;
  678. pp = &(*pp)->next;
  679. }
  680. free (icondirs);
  681. r = define_standard_resource (&resources, RT_GROUP_CURSOR, id,
  682. resinfo->language, 0);
  683. r->type = RES_TYPE_GROUP_CURSOR;
  684. r->u.group_cursor = first;
  685. r->res_info = *resinfo;
  686. }
  687. /* Define a dialog resource. */
  688. void
  689. define_dialog (rc_res_id id, const rc_res_res_info *resinfo,
  690. const rc_dialog *dialog)
  691. {
  692. rc_dialog *copy;
  693. rc_res_resource *r;
  694. copy = (rc_dialog *) res_alloc (sizeof *copy);
  695. *copy = *dialog;
  696. r = define_standard_resource (&resources, RT_DIALOG, id,
  697. resinfo->language, 0);
  698. r->type = RES_TYPE_DIALOG;
  699. r->u.dialog = copy;
  700. r->res_info = *resinfo;
  701. }
  702. /* Define a dialog control. This does not define a resource, but
  703. merely allocates and fills in a structure. */
  704. rc_dialog_control *
  705. define_control (const rc_res_id iid, rc_uint_type id, rc_uint_type x,
  706. rc_uint_type y, rc_uint_type width, rc_uint_type height,
  707. const rc_res_id class, rc_uint_type style,
  708. rc_uint_type exstyle)
  709. {
  710. rc_dialog_control *n;
  711. n = (rc_dialog_control *) res_alloc (sizeof (rc_dialog_control));
  712. n->next = NULL;
  713. n->id = id;
  714. n->style = style;
  715. n->exstyle = exstyle;
  716. n->x = x;
  717. n->y = y;
  718. n->width = width;
  719. n->height = height;
  720. n->class = class;
  721. n->text = iid;
  722. n->data = NULL;
  723. n->help = 0;
  724. return n;
  725. }
  726. rc_dialog_control *
  727. define_icon_control (rc_res_id iid, rc_uint_type id, rc_uint_type x,
  728. rc_uint_type y, rc_uint_type style,
  729. rc_uint_type exstyle, rc_uint_type help,
  730. rc_rcdata_item *data, rc_dialog_ex *ex)
  731. {
  732. rc_dialog_control *n;
  733. rc_res_id tid;
  734. rc_res_id cid;
  735. if (style == 0)
  736. style = SS_ICON | WS_CHILD | WS_VISIBLE;
  737. res_string_to_id (&tid, "");
  738. cid.named = 0;
  739. cid.u.id = CTL_STATIC;
  740. n = define_control (tid, id, x, y, 0, 0, cid, style, exstyle);
  741. n->text = iid;
  742. if (help && ! ex)
  743. rcparse_warning (_("help ID requires DIALOGEX"));
  744. if (data && ! ex)
  745. rcparse_warning (_("control data requires DIALOGEX"));
  746. n->help = help;
  747. n->data = data;
  748. return n;
  749. }
  750. /* Define a font resource. */
  751. void
  752. define_font (rc_res_id id, const rc_res_res_info *resinfo,
  753. const char *filename)
  754. {
  755. FILE *e;
  756. char *real_filename;
  757. struct stat s;
  758. bfd_byte *data;
  759. rc_res_resource *r;
  760. long offset;
  761. long fontdatalength;
  762. bfd_byte *fontdata;
  763. rc_fontdir *fd;
  764. const char *device, *face;
  765. rc_fontdir **pp;
  766. e = open_file_search (filename, FOPEN_RB, "font file", &real_filename);
  767. if (stat (real_filename, &s) < 0)
  768. fatal (_("stat failed on font file `%s': %s"), real_filename,
  769. strerror (errno));
  770. data = (bfd_byte *) res_alloc (s.st_size);
  771. get_data (e, data, s.st_size, real_filename);
  772. fclose (e);
  773. free (real_filename);
  774. r = define_standard_resource (&resources, RT_FONT, id,
  775. resinfo->language, 0);
  776. r->type = RES_TYPE_FONT;
  777. r->u.data.length = s.st_size;
  778. r->u.data.data = data;
  779. r->res_info = *resinfo;
  780. /* For each font resource, we must add an entry in the FONTDIR
  781. resource. The FONTDIR resource includes some strings in the font
  782. file. To find them, we have to do some magic on the data we have
  783. read. */
  784. offset = ((((((data[47] << 8)
  785. | data[46]) << 8)
  786. | data[45]) << 8)
  787. | data[44]);
  788. if (offset > 0 && offset < s.st_size)
  789. device = (char *) data + offset;
  790. else
  791. device = "";
  792. offset = ((((((data[51] << 8)
  793. | data[50]) << 8)
  794. | data[49]) << 8)
  795. | data[48]);
  796. if (offset > 0 && offset < s.st_size)
  797. face = (char *) data + offset;
  798. else
  799. face = "";
  800. ++fonts;
  801. fontdatalength = 58 + strlen (device) + strlen (face);
  802. fontdata = (bfd_byte *) res_alloc (fontdatalength);
  803. memcpy (fontdata, data, 56);
  804. strcpy ((char *) fontdata + 56, device);
  805. strcpy ((char *) fontdata + 57 + strlen (device), face);
  806. fd = (rc_fontdir *) res_alloc (sizeof (rc_fontdir));
  807. fd->next = NULL;
  808. fd->index = fonts;
  809. fd->length = fontdatalength;
  810. fd->data = fontdata;
  811. for (pp = &fontdirs; *pp != NULL; pp = &(*pp)->next)
  812. ;
  813. *pp = fd;
  814. /* For the single fontdirs resource, we always use the resource
  815. information of the last font. I don't know what else to do. */
  816. fontdirs_resinfo = *resinfo;
  817. }
  818. static void
  819. define_font_rcdata (rc_res_id id,const rc_res_res_info *resinfo,
  820. rc_rcdata_item *data)
  821. {
  822. rc_res_resource *r;
  823. rc_uint_type len_data;
  824. bfd_byte *pb_data;
  825. r = define_standard_resource (&resources, RT_FONT, id,
  826. resinfo->language, 0);
  827. pb_data = rcdata_render_as_buffer (data, &len_data);
  828. r->type = RES_TYPE_FONT;
  829. r->u.data.length = len_data;
  830. r->u.data.data = pb_data;
  831. r->res_info = *resinfo;
  832. }
  833. /* Define the fontdirs resource. This is called after the entire rc
  834. file has been parsed, if any font resources were seen. */
  835. static void
  836. define_fontdirs (void)
  837. {
  838. rc_res_resource *r;
  839. rc_res_id id;
  840. id.named = 0;
  841. id.u.id = 1;
  842. r = define_standard_resource (&resources, RT_FONTDIR, id, 0x409, 0);
  843. r->type = RES_TYPE_FONTDIR;
  844. r->u.fontdir = fontdirs;
  845. r->res_info = fontdirs_resinfo;
  846. }
  847. static bfd_byte *
  848. rcdata_render_as_buffer (const rc_rcdata_item *data, rc_uint_type *plen)
  849. {
  850. const rc_rcdata_item *d;
  851. bfd_byte *ret = NULL, *pret;
  852. rc_uint_type len = 0;
  853. for (d = data; d != NULL; d = d->next)
  854. len += rcdata_copy (d, NULL);
  855. if (len != 0)
  856. {
  857. ret = pret = (bfd_byte *) res_alloc (len);
  858. for (d = data; d != NULL; d = d->next)
  859. pret += rcdata_copy (d, pret);
  860. }
  861. if (plen)
  862. *plen = len;
  863. return ret;
  864. }
  865. static void
  866. define_fontdir_rcdata (rc_res_id id,const rc_res_res_info *resinfo,
  867. rc_rcdata_item *data)
  868. {
  869. rc_res_resource *r;
  870. rc_fontdir *fd, *fd_first, *fd_cur;
  871. rc_uint_type len_data;
  872. bfd_byte *pb_data;
  873. rc_uint_type c;
  874. fd_cur = fd_first = NULL;
  875. r = define_standard_resource (&resources, RT_FONTDIR, id, 0x409, 0);
  876. pb_data = rcdata_render_as_buffer (data, &len_data);
  877. if (pb_data)
  878. {
  879. rc_uint_type off = 2;
  880. c = windres_get_16 (&wrtarget, pb_data, len_data);
  881. for (; c > 0; c--)
  882. {
  883. size_t len;
  884. rc_uint_type safe_pos = off;
  885. const struct bin_fontdir_item *bfi;
  886. bfi = (const struct bin_fontdir_item *) pb_data + off;
  887. fd = (rc_fontdir *) res_alloc (sizeof (rc_fontdir));
  888. fd->index = windres_get_16 (&wrtarget, bfi->index, len_data - off);
  889. fd->data = pb_data + off;
  890. off += 56;
  891. len = strlen ((char *) bfi->device_name) + 1;
  892. off += (rc_uint_type) len;
  893. off += (rc_uint_type) strlen ((char *) bfi->device_name + len) + 1;
  894. fd->length = (off - safe_pos);
  895. fd->next = NULL;
  896. if (fd_first == NULL)
  897. fd_first = fd;
  898. else
  899. fd_cur->next = fd;
  900. fd_cur = fd;
  901. }
  902. }
  903. r->type = RES_TYPE_FONTDIR;
  904. r->u.fontdir = fd_first;
  905. r->res_info = *resinfo;
  906. }
  907. static void define_messagetable_rcdata (rc_res_id id, const rc_res_res_info *resinfo,
  908. rc_rcdata_item *data)
  909. {
  910. rc_res_resource *r;
  911. rc_uint_type len_data;
  912. bfd_byte *pb_data;
  913. r = define_standard_resource (&resources, RT_MESSAGETABLE, id, resinfo->language, 0);
  914. pb_data = rcdata_render_as_buffer (data, &len_data);
  915. r->type = RES_TYPE_MESSAGETABLE;
  916. r->u.data.length = len_data;
  917. r->u.data.data = pb_data;
  918. r->res_info = *resinfo;
  919. }
  920. /* Define an icon resource. An icon file may contain a set of
  921. bitmaps, each representing the same icon at various different
  922. resolutions. They each get written out with a different ID. The
  923. real icon resource is then a group resource which can be used to
  924. select one of the actual icon bitmaps. */
  925. void
  926. define_icon (rc_res_id id, const rc_res_res_info *resinfo,
  927. const char *filename)
  928. {
  929. FILE *e;
  930. char *real_filename;
  931. int type, count, i;
  932. struct icondir *icondirs;
  933. int first_icon;
  934. rc_res_resource *r;
  935. rc_group_icon *first, **pp;
  936. e = open_file_search (filename, FOPEN_RB, "icon file", &real_filename);
  937. /* The start of an icon file is a three word structure. The first
  938. word is ignored. The second word is the type of data. The third
  939. word is the number of entries. */
  940. get_word (e, real_filename);
  941. type = get_word (e, real_filename);
  942. count = get_word (e, real_filename);
  943. if (type != 1)
  944. fatal (_("icon file `%s' does not contain icon data"), real_filename);
  945. /* Read in the icon directory entries. */
  946. icondirs = (struct icondir *) xmalloc (count * sizeof *icondirs);
  947. for (i = 0; i < count; i++)
  948. {
  949. icondirs[i].width = getc (e);
  950. icondirs[i].height = getc (e);
  951. icondirs[i].colorcount = getc (e);
  952. getc (e);
  953. icondirs[i].u.icon.planes = get_word (e, real_filename);
  954. icondirs[i].u.icon.bits = get_word (e, real_filename);
  955. icondirs[i].bytes = get_long (e, real_filename);
  956. icondirs[i].offset = get_long (e, real_filename);
  957. if (feof (e))
  958. unexpected_eof (real_filename);
  959. }
  960. /* Define each icon as a unique resource. */
  961. first_icon = icons;
  962. for (i = 0; i < count; i++)
  963. {
  964. bfd_byte *data;
  965. rc_res_id name;
  966. if (fseek (e, icondirs[i].offset, SEEK_SET) != 0)
  967. fatal (_("%s: fseek to %lu failed: %s"), real_filename,
  968. icondirs[i].offset, strerror (errno));
  969. data = (bfd_byte *) res_alloc (icondirs[i].bytes);
  970. get_data (e, data, icondirs[i].bytes, real_filename);
  971. ++icons;
  972. name.named = 0;
  973. name.u.id = icons;
  974. r = define_standard_resource (&resources, RT_ICON, name,
  975. resinfo->language, 0);
  976. r->type = RES_TYPE_ICON;
  977. r->u.data.length = icondirs[i].bytes;
  978. r->u.data.data = data;
  979. r->res_info = *resinfo;
  980. }
  981. fclose (e);
  982. free (real_filename);
  983. /* Define an icon group resource. */
  984. first = NULL;
  985. pp = &first;
  986. for (i = 0; i < count; i++)
  987. {
  988. rc_group_icon *cg;
  989. /* For some reason, at least in some files the planes and bits
  990. are zero. We instead set them from the color. This is
  991. copied from rcl. */
  992. cg = (rc_group_icon *) res_alloc (sizeof (rc_group_icon));
  993. cg->next = NULL;
  994. cg->width = icondirs[i].width;
  995. cg->height = icondirs[i].height;
  996. cg->colors = icondirs[i].colorcount;
  997. if (icondirs[i].u.icon.planes)
  998. cg->planes = icondirs[i].u.icon.planes;
  999. else
  1000. cg->planes = 1;
  1001. if (icondirs[i].u.icon.bits)
  1002. cg->bits = icondirs[i].u.icon.bits;
  1003. else
  1004. {
  1005. cg->bits = 0;
  1006. while ((1L << cg->bits) < cg->colors)
  1007. ++cg->bits;
  1008. }
  1009. cg->bytes = icondirs[i].bytes;
  1010. cg->index = first_icon + i + 1;
  1011. *pp = cg;
  1012. pp = &(*pp)->next;
  1013. }
  1014. free (icondirs);
  1015. r = define_standard_resource (&resources, RT_GROUP_ICON, id,
  1016. resinfo->language, 0);
  1017. r->type = RES_TYPE_GROUP_ICON;
  1018. r->u.group_icon = first;
  1019. r->res_info = *resinfo;
  1020. }
  1021. static void
  1022. define_group_icon_rcdata (rc_res_id id, const rc_res_res_info *resinfo,
  1023. rc_rcdata_item *data)
  1024. {
  1025. rc_res_resource *r;
  1026. rc_group_icon *cg, *first, *cur;
  1027. rc_uint_type len_data;
  1028. bfd_byte *pb_data;
  1029. pb_data = rcdata_render_as_buffer (data, &len_data);
  1030. cur = NULL;
  1031. first = NULL;
  1032. while (len_data >= 6)
  1033. {
  1034. int c, i;
  1035. unsigned short type;
  1036. type = windres_get_16 (&wrtarget, pb_data + 2, len_data - 2);
  1037. if (type != 1)
  1038. fatal (_("unexpected group icon type %d"), type);
  1039. c = windres_get_16 (&wrtarget, pb_data + 4, len_data - 4);
  1040. len_data -= 6;
  1041. pb_data += 6;
  1042. for (i = 0; i < c; i++)
  1043. {
  1044. if (len_data < 14)
  1045. fatal ("too small group icon rcdata");
  1046. cg = (rc_group_icon *) res_alloc (sizeof (rc_group_icon));
  1047. cg->next = NULL;
  1048. cg->width = pb_data[0];
  1049. cg->height = pb_data[1];
  1050. cg->colors = pb_data[2];
  1051. cg->planes = windres_get_16 (&wrtarget, pb_data + 4, len_data - 4);
  1052. cg->bits = windres_get_16 (&wrtarget, pb_data + 6, len_data - 6);
  1053. cg->bytes = windres_get_32 (&wrtarget, pb_data + 8, len_data - 8);
  1054. cg->index = windres_get_16 (&wrtarget, pb_data + 12, len_data - 12);
  1055. if (! first)
  1056. first = cg;
  1057. else
  1058. cur->next = cg;
  1059. cur = cg;
  1060. pb_data += 14;
  1061. len_data -= 14;
  1062. }
  1063. }
  1064. r = define_standard_resource (&resources, RT_GROUP_ICON, id,
  1065. resinfo->language, 0);
  1066. r->type = RES_TYPE_GROUP_ICON;
  1067. r->u.group_icon = first;
  1068. r->res_info = *resinfo;
  1069. }
  1070. static void
  1071. define_group_cursor_rcdata (rc_res_id id, const rc_res_res_info *resinfo,
  1072. rc_rcdata_item *data)
  1073. {
  1074. rc_res_resource *r;
  1075. rc_group_cursor *cg, *first, *cur;
  1076. rc_uint_type len_data;
  1077. bfd_byte *pb_data;
  1078. pb_data = rcdata_render_as_buffer (data, &len_data);
  1079. first = cur = NULL;
  1080. while (len_data >= 6)
  1081. {
  1082. int c, i;
  1083. unsigned short type;
  1084. type = windres_get_16 (&wrtarget, pb_data + 2, len_data - 2);
  1085. if (type != 2)
  1086. fatal (_("unexpected group cursor type %d"), type);
  1087. c = windres_get_16 (&wrtarget, pb_data + 4, len_data - 4);
  1088. len_data -= 6;
  1089. pb_data += 6;
  1090. for (i = 0; i < c; i++)
  1091. {
  1092. if (len_data < 14)
  1093. fatal ("too small group icon rcdata");
  1094. cg = (rc_group_cursor *) res_alloc (sizeof (rc_group_cursor));
  1095. cg->next = NULL;
  1096. cg->width = windres_get_16 (&wrtarget, pb_data, len_data);
  1097. cg->height = windres_get_16 (&wrtarget, pb_data + 2, len_data - 2);
  1098. cg->planes = windres_get_16 (&wrtarget, pb_data + 4, len_data - 4);
  1099. cg->bits = windres_get_16 (&wrtarget, pb_data + 6, len_data - 6);
  1100. cg->bytes = windres_get_32 (&wrtarget, pb_data + 8, len_data - 8);
  1101. cg->index = windres_get_16 (&wrtarget, pb_data + 12, len_data - 12);
  1102. if (! first)
  1103. first = cg;
  1104. else
  1105. cur->next = cg;
  1106. cur = cg;
  1107. pb_data += 14;
  1108. len_data -= 14;
  1109. }
  1110. }
  1111. r = define_standard_resource (&resources, RT_GROUP_ICON, id,
  1112. resinfo->language, 0);
  1113. r->type = RES_TYPE_GROUP_CURSOR;
  1114. r->u.group_cursor = first;
  1115. r->res_info = *resinfo;
  1116. }
  1117. static void
  1118. define_cursor_rcdata (rc_res_id id, const rc_res_res_info *resinfo,
  1119. rc_rcdata_item *data)
  1120. {
  1121. rc_cursor *c;
  1122. rc_res_resource *r;
  1123. rc_uint_type len_data;
  1124. bfd_byte *pb_data;
  1125. pb_data = rcdata_render_as_buffer (data, &len_data);
  1126. c = (rc_cursor *) res_alloc (sizeof (rc_cursor));
  1127. c->xhotspot = windres_get_16 (&wrtarget, pb_data, len_data);
  1128. c->yhotspot = windres_get_16 (&wrtarget, pb_data + 2, len_data - 2);
  1129. c->length = len_data - BIN_CURSOR_SIZE;
  1130. c->data = (const bfd_byte *) (data + BIN_CURSOR_SIZE);
  1131. r = define_standard_resource (&resources, RT_CURSOR, id, resinfo->language, 0);
  1132. r->type = RES_TYPE_CURSOR;
  1133. r->u.cursor = c;
  1134. r->res_info = *resinfo;
  1135. }
  1136. static void
  1137. define_bitmap_rcdata (rc_res_id id, const rc_res_res_info *resinfo,
  1138. rc_rcdata_item *data)
  1139. {
  1140. rc_res_resource *r;
  1141. rc_uint_type len_data;
  1142. bfd_byte *pb_data;
  1143. pb_data = rcdata_render_as_buffer (data, &len_data);
  1144. r = define_standard_resource (&resources, RT_BITMAP, id, resinfo->language, 0);
  1145. r->type = RES_TYPE_BITMAP;
  1146. r->u.data.length = len_data;
  1147. r->u.data.data = pb_data;
  1148. r->res_info = *resinfo;
  1149. }
  1150. static void
  1151. define_icon_rcdata (rc_res_id id, const rc_res_res_info *resinfo,
  1152. rc_rcdata_item *data)
  1153. {
  1154. rc_res_resource *r;
  1155. rc_uint_type len_data;
  1156. bfd_byte *pb_data;
  1157. pb_data = rcdata_render_as_buffer (data, &len_data);
  1158. r = define_standard_resource (&resources, RT_ICON, id, resinfo->language, 0);
  1159. r->type = RES_TYPE_ICON;
  1160. r->u.data.length = len_data;
  1161. r->u.data.data = pb_data;
  1162. r->res_info = *resinfo;
  1163. }
  1164. /* Define a menu resource. */
  1165. void
  1166. define_menu (rc_res_id id, const rc_res_res_info *resinfo,
  1167. rc_menuitem *menuitems)
  1168. {
  1169. rc_menu *m;
  1170. rc_res_resource *r;
  1171. m = (rc_menu *) res_alloc (sizeof (rc_menu));
  1172. m->items = menuitems;
  1173. m->help = 0;
  1174. r = define_standard_resource (&resources, RT_MENU, id, resinfo->language, 0);
  1175. r->type = RES_TYPE_MENU;
  1176. r->u.menu = m;
  1177. r->res_info = *resinfo;
  1178. }
  1179. /* Define a menu item. This does not define a resource, but merely
  1180. allocates and fills in a structure. */
  1181. rc_menuitem *
  1182. define_menuitem (const unichar *text, rc_uint_type menuid, rc_uint_type type,
  1183. rc_uint_type state, rc_uint_type help,
  1184. rc_menuitem *menuitems)
  1185. {
  1186. rc_menuitem *mi;
  1187. mi = (rc_menuitem *) res_alloc (sizeof (rc_menuitem));
  1188. mi->next = NULL;
  1189. mi->type = type;
  1190. mi->state = state;
  1191. mi->id = menuid;
  1192. mi->text = unichar_dup (text);
  1193. mi->help = help;
  1194. mi->popup = menuitems;
  1195. return mi;
  1196. }
  1197. /* Define a messagetable resource. */
  1198. void
  1199. define_messagetable (rc_res_id id, const rc_res_res_info *resinfo,
  1200. const char *filename)
  1201. {
  1202. FILE *e;
  1203. char *real_filename;
  1204. struct stat s;
  1205. bfd_byte *data;
  1206. rc_res_resource *r;
  1207. e = open_file_search (filename, FOPEN_RB, "messagetable file",
  1208. &real_filename);
  1209. if (stat (real_filename, &s) < 0)
  1210. fatal (_("stat failed on bitmap file `%s': %s"), real_filename,
  1211. strerror (errno));
  1212. data = (bfd_byte *) res_alloc (s.st_size);
  1213. get_data (e, data, s.st_size, real_filename);
  1214. fclose (e);
  1215. free (real_filename);
  1216. r = define_standard_resource (&resources, RT_MESSAGETABLE, id,
  1217. resinfo->language, 0);
  1218. r->type = RES_TYPE_MESSAGETABLE;
  1219. r->u.data.length = s.st_size;
  1220. r->u.data.data = data;
  1221. r->res_info = *resinfo;
  1222. }
  1223. /* Define an rcdata resource. */
  1224. void
  1225. define_rcdata (rc_res_id id, const rc_res_res_info *resinfo,
  1226. rc_rcdata_item *data)
  1227. {
  1228. rc_res_resource *r;
  1229. r = define_standard_resource (&resources, RT_RCDATA, id,
  1230. resinfo->language, 0);
  1231. r->type = RES_TYPE_RCDATA;
  1232. r->u.rcdata = data;
  1233. r->res_info = *resinfo;
  1234. }
  1235. /* Create an rcdata item holding a string. */
  1236. rc_rcdata_item *
  1237. define_rcdata_string (const char *string, rc_uint_type len)
  1238. {
  1239. rc_rcdata_item *ri;
  1240. char *s;
  1241. ri = (rc_rcdata_item *) res_alloc (sizeof (rc_rcdata_item));
  1242. ri->next = NULL;
  1243. ri->type = RCDATA_STRING;
  1244. ri->u.string.length = len;
  1245. s = (char *) res_alloc (len);
  1246. memcpy (s, string, len);
  1247. ri->u.string.s = s;
  1248. return ri;
  1249. }
  1250. /* Create an rcdata item holding a unicode string. */
  1251. rc_rcdata_item *
  1252. define_rcdata_unistring (const unichar *string, rc_uint_type len)
  1253. {
  1254. rc_rcdata_item *ri;
  1255. unichar *s;
  1256. ri = (rc_rcdata_item *) res_alloc (sizeof (rc_rcdata_item));
  1257. ri->next = NULL;
  1258. ri->type = RCDATA_WSTRING;
  1259. ri->u.wstring.length = len;
  1260. s = (unichar *) res_alloc (len * sizeof (unichar));
  1261. memcpy (s, string, len * sizeof (unichar));
  1262. ri->u.wstring.w = s;
  1263. return ri;
  1264. }
  1265. /* Create an rcdata item holding a number. */
  1266. rc_rcdata_item *
  1267. define_rcdata_number (rc_uint_type val, int dword)
  1268. {
  1269. rc_rcdata_item *ri;
  1270. ri = (rc_rcdata_item *) res_alloc (sizeof (rc_rcdata_item));
  1271. ri->next = NULL;
  1272. ri->type = dword ? RCDATA_DWORD : RCDATA_WORD;
  1273. ri->u.word = val;
  1274. return ri;
  1275. }
  1276. /* Define a stringtable resource. This is called for each string
  1277. which appears in a STRINGTABLE statement. */
  1278. void
  1279. define_stringtable (const rc_res_res_info *resinfo,
  1280. rc_uint_type stringid, const unichar *string, int len)
  1281. {
  1282. unichar *h;
  1283. rc_res_id id;
  1284. rc_res_resource *r;
  1285. id.named = 0;
  1286. id.u.id = (stringid >> 4) + 1;
  1287. r = define_standard_resource (&resources, RT_STRING, id,
  1288. resinfo->language, 1);
  1289. if (r->type == RES_TYPE_UNINITIALIZED)
  1290. {
  1291. int i;
  1292. r->type = RES_TYPE_STRINGTABLE;
  1293. r->u.stringtable = ((rc_stringtable *)
  1294. res_alloc (sizeof (rc_stringtable)));
  1295. for (i = 0; i < 16; i++)
  1296. {
  1297. r->u.stringtable->strings[i].length = 0;
  1298. r->u.stringtable->strings[i].string = NULL;
  1299. }
  1300. r->res_info = *resinfo;
  1301. }
  1302. h = (unichar *) res_alloc ((len + 1) * sizeof (unichar));
  1303. if (len)
  1304. memcpy (h, string, len * sizeof (unichar));
  1305. h[len] = 0;
  1306. r->u.stringtable->strings[stringid & 0xf].length = (rc_uint_type) len;
  1307. r->u.stringtable->strings[stringid & 0xf].string = h;
  1308. }
  1309. void
  1310. define_toolbar (rc_res_id id, rc_res_res_info *resinfo, rc_uint_type width, rc_uint_type height,
  1311. rc_toolbar_item *items)
  1312. {
  1313. rc_toolbar *t;
  1314. rc_res_resource *r;
  1315. t = (rc_toolbar *) res_alloc (sizeof (rc_toolbar));
  1316. t->button_width = width;
  1317. t->button_height = height;
  1318. t->nitems = 0;
  1319. t->items = items;
  1320. while (items != NULL)
  1321. {
  1322. t->nitems+=1;
  1323. items = items->next;
  1324. }
  1325. r = define_standard_resource (&resources, RT_TOOLBAR, id, resinfo->language, 0);
  1326. r->type = RES_TYPE_TOOLBAR;
  1327. r->u.toolbar = t;
  1328. r->res_info = *resinfo;
  1329. }
  1330. /* Define a user data resource where the data is in the rc file. */
  1331. void
  1332. define_user_data (rc_res_id id, rc_res_id type,
  1333. const rc_res_res_info *resinfo,
  1334. rc_rcdata_item *data)
  1335. {
  1336. rc_res_id ids[3];
  1337. rc_res_resource *r;
  1338. bfd_byte *pb_data;
  1339. rc_uint_type len_data;
  1340. /* We have to check if the binary data is parsed specially. */
  1341. if (type.named == 0)
  1342. {
  1343. switch (type.u.id)
  1344. {
  1345. case RT_FONTDIR:
  1346. define_fontdir_rcdata (id, resinfo, data);
  1347. return;
  1348. case RT_FONT:
  1349. define_font_rcdata (id, resinfo, data);
  1350. return;
  1351. case RT_ICON:
  1352. define_icon_rcdata (id, resinfo, data);
  1353. return;
  1354. case RT_BITMAP:
  1355. define_bitmap_rcdata (id, resinfo, data);
  1356. return;
  1357. case RT_CURSOR:
  1358. define_cursor_rcdata (id, resinfo, data);
  1359. return;
  1360. case RT_GROUP_ICON:
  1361. define_group_icon_rcdata (id, resinfo, data);
  1362. return;
  1363. case RT_GROUP_CURSOR:
  1364. define_group_cursor_rcdata (id, resinfo, data);
  1365. return;
  1366. case RT_MESSAGETABLE:
  1367. define_messagetable_rcdata (id, resinfo, data);
  1368. return;
  1369. default:
  1370. /* Treat as normal user-data. */
  1371. break;
  1372. }
  1373. }
  1374. ids[0] = type;
  1375. ids[1] = id;
  1376. ids[2].named = 0;
  1377. ids[2].u.id = resinfo->language;
  1378. r = define_resource (& resources, 3, ids, 0);
  1379. r->type = RES_TYPE_USERDATA;
  1380. r->u.userdata = ((rc_rcdata_item *)
  1381. res_alloc (sizeof (rc_rcdata_item)));
  1382. r->u.userdata->next = NULL;
  1383. r->u.userdata->type = RCDATA_BUFFER;
  1384. pb_data = rcdata_render_as_buffer (data, &len_data);
  1385. r->u.userdata->u.buffer.length = len_data;
  1386. r->u.userdata->u.buffer.data = pb_data;
  1387. r->res_info = *resinfo;
  1388. }
  1389. void
  1390. define_rcdata_file (rc_res_id id, const rc_res_res_info *resinfo,
  1391. const char *filename)
  1392. {
  1393. rc_rcdata_item *ri;
  1394. FILE *e;
  1395. char *real_filename;
  1396. struct stat s;
  1397. bfd_byte *data;
  1398. e = open_file_search (filename, FOPEN_RB, "file", &real_filename);
  1399. if (stat (real_filename, &s) < 0)
  1400. fatal (_("stat failed on file `%s': %s"), real_filename,
  1401. strerror (errno));
  1402. data = (bfd_byte *) res_alloc (s.st_size);
  1403. get_data (e, data, s.st_size, real_filename);
  1404. fclose (e);
  1405. free (real_filename);
  1406. ri = (rc_rcdata_item *) res_alloc (sizeof (rc_rcdata_item));
  1407. ri->next = NULL;
  1408. ri->type = RCDATA_BUFFER;
  1409. ri->u.buffer.length = s.st_size;
  1410. ri->u.buffer.data = data;
  1411. define_rcdata (id, resinfo, ri);
  1412. }
  1413. /* Define a user data resource where the data is in a file. */
  1414. void
  1415. define_user_file (rc_res_id id, rc_res_id type,
  1416. const rc_res_res_info *resinfo, const char *filename)
  1417. {
  1418. FILE *e;
  1419. char *real_filename;
  1420. struct stat s;
  1421. bfd_byte *data;
  1422. rc_res_id ids[3];
  1423. rc_res_resource *r;
  1424. e = open_file_search (filename, FOPEN_RB, "file", &real_filename);
  1425. if (stat (real_filename, &s) < 0)
  1426. fatal (_("stat failed on file `%s': %s"), real_filename,
  1427. strerror (errno));
  1428. data = (bfd_byte *) res_alloc (s.st_size);
  1429. get_data (e, data, s.st_size, real_filename);
  1430. fclose (e);
  1431. free (real_filename);
  1432. ids[0] = type;
  1433. ids[1] = id;
  1434. ids[2].named = 0;
  1435. ids[2].u.id = resinfo->language;
  1436. r = define_resource (&resources, 3, ids, 0);
  1437. r->type = RES_TYPE_USERDATA;
  1438. r->u.userdata = ((rc_rcdata_item *)
  1439. res_alloc (sizeof (rc_rcdata_item)));
  1440. r->u.userdata->next = NULL;
  1441. r->u.userdata->type = RCDATA_BUFFER;
  1442. r->u.userdata->u.buffer.length = s.st_size;
  1443. r->u.userdata->u.buffer.data = data;
  1444. r->res_info = *resinfo;
  1445. }
  1446. /* Define a versioninfo resource. */
  1447. void
  1448. define_versioninfo (rc_res_id id, rc_uint_type language,
  1449. rc_fixed_versioninfo *fixedverinfo,
  1450. rc_ver_info *verinfo)
  1451. {
  1452. rc_res_resource *r;
  1453. r = define_standard_resource (&resources, RT_VERSION, id, language, 0);
  1454. r->type = RES_TYPE_VERSIONINFO;
  1455. r->u.versioninfo = ((rc_versioninfo *)
  1456. res_alloc (sizeof (rc_versioninfo)));
  1457. r->u.versioninfo->fixed = fixedverinfo;
  1458. r->u.versioninfo->var = verinfo;
  1459. r->res_info.language = language;
  1460. }
  1461. /* Add string version info to a list of version information. */
  1462. rc_ver_info *
  1463. append_ver_stringfileinfo (rc_ver_info *verinfo,
  1464. rc_ver_stringtable *stringtables)
  1465. {
  1466. rc_ver_info *vi, **pp;
  1467. vi = (rc_ver_info *) res_alloc (sizeof (rc_ver_info));
  1468. vi->next = NULL;
  1469. vi->type = VERINFO_STRING;
  1470. vi->u.string.stringtables = stringtables;
  1471. for (pp = &verinfo; *pp != NULL; pp = &(*pp)->next)
  1472. ;
  1473. *pp = vi;
  1474. return verinfo;
  1475. }
  1476. rc_ver_stringtable *
  1477. append_ver_stringtable (rc_ver_stringtable *stringtable,
  1478. const char *language,
  1479. rc_ver_stringinfo *strings)
  1480. {
  1481. rc_ver_stringtable *vst, **pp;
  1482. vst = (rc_ver_stringtable *) res_alloc (sizeof (rc_ver_stringtable));
  1483. vst->next = NULL;
  1484. unicode_from_ascii ((rc_uint_type *) NULL, &vst->language, language);
  1485. vst->strings = strings;
  1486. for (pp = &stringtable; *pp != NULL; pp = &(*pp)->next)
  1487. ;
  1488. *pp = vst;
  1489. return stringtable;
  1490. }
  1491. /* Add variable version info to a list of version information. */
  1492. rc_ver_info *
  1493. append_ver_varfileinfo (rc_ver_info *verinfo, const unichar *key,
  1494. rc_ver_varinfo *var)
  1495. {
  1496. rc_ver_info *vi, **pp;
  1497. vi = (rc_ver_info *) res_alloc (sizeof *vi);
  1498. vi->next = NULL;
  1499. vi->type = VERINFO_VAR;
  1500. vi->u.var.key = unichar_dup (key);
  1501. vi->u.var.var = var;
  1502. for (pp = &verinfo; *pp != NULL; pp = &(*pp)->next)
  1503. ;
  1504. *pp = vi;
  1505. return verinfo;
  1506. }
  1507. /* Append version string information to a list. */
  1508. rc_ver_stringinfo *
  1509. append_verval (rc_ver_stringinfo *strings, const unichar *key,
  1510. const unichar *value)
  1511. {
  1512. rc_ver_stringinfo *vs, **pp;
  1513. vs = (rc_ver_stringinfo *) res_alloc (sizeof (rc_ver_stringinfo));
  1514. vs->next = NULL;
  1515. vs->key = unichar_dup (key);
  1516. vs->value = unichar_dup (value);
  1517. for (pp = &strings; *pp != NULL; pp = &(*pp)->next)
  1518. ;
  1519. *pp = vs;
  1520. return strings;
  1521. }
  1522. /* Append version variable information to a list. */
  1523. rc_ver_varinfo *
  1524. append_vertrans (rc_ver_varinfo *var, rc_uint_type language,
  1525. rc_uint_type charset)
  1526. {
  1527. rc_ver_varinfo *vv, **pp;
  1528. vv = (rc_ver_varinfo *) res_alloc (sizeof (rc_ver_varinfo));
  1529. vv->next = NULL;
  1530. vv->language = language;
  1531. vv->charset = charset;
  1532. for (pp = &var; *pp != NULL; pp = &(*pp)->next)
  1533. ;
  1534. *pp = vv;
  1535. return var;
  1536. }
  1537. /* Local functions used to write out an rc file. */
  1538. static void indent (FILE *, int);
  1539. static void write_rc_directory (FILE *, const rc_res_directory *, const rc_res_id *,
  1540. const rc_res_id *, rc_uint_type *, int);
  1541. static void write_rc_subdir (FILE *, const rc_res_entry *, const rc_res_id *,
  1542. const rc_res_id *, rc_uint_type *, int);
  1543. static void write_rc_resource (FILE *, const rc_res_id *, const rc_res_id *,
  1544. const rc_res_resource *, rc_uint_type *);
  1545. static void write_rc_accelerators (FILE *, const rc_accelerator *);
  1546. static void write_rc_cursor (FILE *, const rc_cursor *);
  1547. static void write_rc_group_cursor (FILE *, const rc_group_cursor *);
  1548. static void write_rc_dialog (FILE *, const rc_dialog *);
  1549. static void write_rc_dialog_control (FILE *, const rc_dialog_control *);
  1550. static void write_rc_fontdir (FILE *, const rc_fontdir *);
  1551. static void write_rc_group_icon (FILE *, const rc_group_icon *);
  1552. static void write_rc_menu (FILE *, const rc_menu *, int);
  1553. static void write_rc_toolbar (FILE *, const rc_toolbar *);
  1554. static void write_rc_menuitems (FILE *, const rc_menuitem *, int, int);
  1555. static void write_rc_messagetable (FILE *, rc_uint_type , const bfd_byte *);
  1556. static void write_rc_datablock (FILE *, rc_uint_type , const bfd_byte *, int, int, int);
  1557. static void write_rc_rcdata (FILE *, const rc_rcdata_item *, int);
  1558. static void write_rc_stringtable (FILE *, const rc_res_id *, const rc_stringtable *);
  1559. static void write_rc_versioninfo (FILE *, const rc_versioninfo *);
  1560. /* Indent a given number of spaces. */
  1561. static void
  1562. indent (FILE *e, int c)
  1563. {
  1564. int i;
  1565. for (i = 0; i < c; i++)
  1566. putc (' ', e);
  1567. }
  1568. /* Dump the resources we have read in the format of an rc file.
  1569. Reasoned by the fact, that some resources need to be stored into file and
  1570. refer to that file, we use the user-data model for that to express it binary
  1571. without the need to store it somewhere externally. */
  1572. void
  1573. write_rc_file (const char *filename, const rc_res_directory *res_dir)
  1574. {
  1575. FILE *e;
  1576. rc_uint_type language;
  1577. if (filename == NULL)
  1578. e = stdout;
  1579. else
  1580. {
  1581. e = fopen (filename, FOPEN_WT);
  1582. if (e == NULL)
  1583. fatal (_("can't open `%s' for output: %s"), filename, strerror (errno));
  1584. }
  1585. language = (rc_uint_type) ((bfd_signed_vma) -1);
  1586. write_rc_directory (e, res_dir, (const rc_res_id *) NULL,
  1587. (const rc_res_id *) NULL, &language, 1);
  1588. }
  1589. /* Write out a directory. E is the file to write to. RD is the
  1590. directory. TYPE is a pointer to the level 1 ID which serves as the
  1591. resource type. NAME is a pointer to the level 2 ID which serves as
  1592. an individual resource name. LANGUAGE is a pointer to the current
  1593. language. LEVEL is the level in the tree. */
  1594. static void
  1595. write_rc_directory (FILE *e, const rc_res_directory *rd,
  1596. const rc_res_id *type, const rc_res_id *name,
  1597. rc_uint_type *language, int level)
  1598. {
  1599. const rc_res_entry *re;
  1600. /* Print out some COFF information that rc files can't represent. */
  1601. if (rd->time != 0 || rd->characteristics != 0 || rd->major != 0 || rd->minor != 0)
  1602. {
  1603. wr_printcomment (e, "COFF information not part of RC");
  1604. if (rd->time != 0)
  1605. wr_printcomment (e, "Time stamp: %u", rd->time);
  1606. if (rd->characteristics != 0)
  1607. wr_printcomment (e, "Characteristics: %u", rd->characteristics);
  1608. if (rd->major != 0 || rd->minor != 0)
  1609. wr_printcomment (e, "Version major:%d minor:%d", rd->major, rd->minor);
  1610. }
  1611. for (re = rd->entries; re != NULL; re = re->next)
  1612. {
  1613. switch (level)
  1614. {
  1615. case 1:
  1616. /* If we're at level 1, the key of this resource is the
  1617. type. This normally duplicates the information we have
  1618. stored with the resource itself, but we need to remember
  1619. the type if this is a user define resource type. */
  1620. type = &re->id;
  1621. break;
  1622. case 2:
  1623. /* If we're at level 2, the key of this resource is the name
  1624. we are going to use in the rc printout. */
  1625. name = &re->id;
  1626. break;
  1627. case 3:
  1628. /* If we're at level 3, then this key represents a language.
  1629. Use it to update the current language. */
  1630. if (! re->id.named
  1631. && re->id.u.id != (unsigned long) (unsigned int) *language
  1632. && (re->id.u.id & 0xffff) == re->id.u.id)
  1633. {
  1634. wr_print (e, "LANGUAGE %u, %u\n",
  1635. re->id.u.id & ((1 << SUBLANG_SHIFT) - 1),
  1636. (re->id.u.id >> SUBLANG_SHIFT) & 0xff);
  1637. *language = re->id.u.id;
  1638. }
  1639. break;
  1640. default:
  1641. break;
  1642. }
  1643. if (re->subdir)
  1644. write_rc_subdir (e, re, type, name, language, level);
  1645. else
  1646. {
  1647. if (level == 3)
  1648. {
  1649. /* This is the normal case: the three levels are
  1650. TYPE/NAME/LANGUAGE. NAME will have been set at level
  1651. 2, and represents the name to use. We probably just
  1652. set LANGUAGE, and it will probably match what the
  1653. resource itself records if anything. */
  1654. write_rc_resource (e, type, name, re->u.res, language);
  1655. }
  1656. else
  1657. {
  1658. wr_printcomment (e, "Resource at unexpected level %d", level);
  1659. write_rc_resource (e, type, (rc_res_id *) NULL, re->u.res,
  1660. language);
  1661. }
  1662. }
  1663. }
  1664. if (rd->entries == NULL)
  1665. {
  1666. wr_print_flush (e);
  1667. }
  1668. }
  1669. /* Write out a subdirectory entry. E is the file to write to. RE is
  1670. the subdirectory entry. TYPE and NAME are pointers to higher level
  1671. IDs, or NULL. LANGUAGE is a pointer to the current language.
  1672. LEVEL is the level in the tree. */
  1673. static void
  1674. write_rc_subdir (FILE *e, const rc_res_entry *re,
  1675. const rc_res_id *type, const rc_res_id *name,
  1676. rc_uint_type *language, int level)
  1677. {
  1678. fprintf (e, "\n");
  1679. switch (level)
  1680. {
  1681. case 1:
  1682. wr_printcomment (e, "Type: ");
  1683. if (re->id.named)
  1684. res_id_print (e, re->id, 1);
  1685. else
  1686. {
  1687. const char *s;
  1688. switch (re->id.u.id)
  1689. {
  1690. case RT_CURSOR: s = "cursor"; break;
  1691. case RT_BITMAP: s = "bitmap"; break;
  1692. case RT_ICON: s = "icon"; break;
  1693. case RT_MENU: s = "menu"; break;
  1694. case RT_DIALOG: s = "dialog"; break;
  1695. case RT_STRING: s = "stringtable"; break;
  1696. case RT_FONTDIR: s = "fontdir"; break;
  1697. case RT_FONT: s = "font"; break;
  1698. case RT_ACCELERATOR: s = "accelerators"; break;
  1699. case RT_RCDATA: s = "rcdata"; break;
  1700. case RT_MESSAGETABLE: s = "messagetable"; break;
  1701. case RT_GROUP_CURSOR: s = "group cursor"; break;
  1702. case RT_GROUP_ICON: s = "group icon"; break;
  1703. case RT_VERSION: s = "version"; break;
  1704. case RT_DLGINCLUDE: s = "dlginclude"; break;
  1705. case RT_PLUGPLAY: s = "plugplay"; break;
  1706. case RT_VXD: s = "vxd"; break;
  1707. case RT_ANICURSOR: s = "anicursor"; break;
  1708. case RT_ANIICON: s = "aniicon"; break;
  1709. case RT_TOOLBAR: s = "toolbar"; break;
  1710. case RT_HTML: s = "html"; break;
  1711. default: s = NULL; break;
  1712. }
  1713. if (s != NULL)
  1714. fprintf (e, "%s", s);
  1715. else
  1716. res_id_print (e, re->id, 1);
  1717. }
  1718. break;
  1719. case 2:
  1720. wr_printcomment (e, "Name: ");
  1721. res_id_print (e, re->id, 1);
  1722. break;
  1723. case 3:
  1724. wr_printcomment (e, "Language: ");
  1725. res_id_print (e, re->id, 1);
  1726. break;
  1727. default:
  1728. wr_printcomment (e, "Level %d: ", level);
  1729. res_id_print (e, re->id, 1);
  1730. }
  1731. write_rc_directory (e, re->u.dir, type, name, language, level + 1);
  1732. }
  1733. /* Write out a single resource. E is the file to write to. TYPE is a
  1734. pointer to the type of the resource. NAME is a pointer to the name
  1735. of the resource; it will be NULL if there is a level mismatch. RES
  1736. is the resource data. LANGUAGE is a pointer to the current
  1737. language. */
  1738. static void
  1739. write_rc_resource (FILE *e, const rc_res_id *type,
  1740. const rc_res_id *name, const rc_res_resource *res,
  1741. rc_uint_type *language)
  1742. {
  1743. const char *s;
  1744. int rt;
  1745. int menuex = 0;
  1746. switch (res->type)
  1747. {
  1748. default:
  1749. abort ();
  1750. case RES_TYPE_ACCELERATOR:
  1751. s = "ACCELERATORS";
  1752. rt = RT_ACCELERATOR;
  1753. break;
  1754. case RES_TYPE_BITMAP:
  1755. s = "2 /* RT_BITMAP */";
  1756. rt = RT_BITMAP;
  1757. break;
  1758. case RES_TYPE_CURSOR:
  1759. s = "1 /* RT_CURSOR */";
  1760. rt = RT_CURSOR;
  1761. break;
  1762. case RES_TYPE_GROUP_CURSOR:
  1763. s = "12 /* RT_GROUP_CURSOR */";
  1764. rt = RT_GROUP_CURSOR;
  1765. break;
  1766. case RES_TYPE_DIALOG:
  1767. if (extended_dialog (res->u.dialog))
  1768. s = "DIALOGEX";
  1769. else
  1770. s = "DIALOG";
  1771. rt = RT_DIALOG;
  1772. break;
  1773. case RES_TYPE_FONT:
  1774. s = "8 /* RT_FONT */";
  1775. rt = RT_FONT;
  1776. break;
  1777. case RES_TYPE_FONTDIR:
  1778. s = "7 /* RT_FONTDIR */";
  1779. rt = RT_FONTDIR;
  1780. break;
  1781. case RES_TYPE_ICON:
  1782. s = "3 /* RT_ICON */";
  1783. rt = RT_ICON;
  1784. break;
  1785. case RES_TYPE_GROUP_ICON:
  1786. s = "14 /* RT_GROUP_ICON */";
  1787. rt = RT_GROUP_ICON;
  1788. break;
  1789. case RES_TYPE_MENU:
  1790. if (extended_menu (res->u.menu))
  1791. {
  1792. s = "MENUEX";
  1793. menuex = 1;
  1794. }
  1795. else
  1796. {
  1797. s = "MENU";
  1798. menuex = 0;
  1799. }
  1800. rt = RT_MENU;
  1801. break;
  1802. case RES_TYPE_MESSAGETABLE:
  1803. s = "11 /* RT_MESSAGETABLE */";
  1804. rt = RT_MESSAGETABLE;
  1805. break;
  1806. case RES_TYPE_RCDATA:
  1807. s = "RCDATA";
  1808. rt = RT_RCDATA;
  1809. break;
  1810. case RES_TYPE_STRINGTABLE:
  1811. s = "STRINGTABLE";
  1812. rt = RT_STRING;
  1813. break;
  1814. case RES_TYPE_USERDATA:
  1815. s = NULL;
  1816. rt = 0;
  1817. break;
  1818. case RES_TYPE_VERSIONINFO:
  1819. s = "VERSIONINFO";
  1820. rt = RT_VERSION;
  1821. break;
  1822. case RES_TYPE_TOOLBAR:
  1823. s = "TOOLBAR";
  1824. rt = RT_TOOLBAR;
  1825. break;
  1826. }
  1827. if (rt != 0
  1828. && type != NULL
  1829. && (type->named || type->u.id != (unsigned long) rt))
  1830. {
  1831. wr_printcomment (e, "Unexpected resource type mismatch: ");
  1832. res_id_print (e, *type, 1);
  1833. fprintf (e, " != %d", rt);
  1834. }
  1835. if (res->coff_info.codepage != 0)
  1836. wr_printcomment (e, "Code page: %u", res->coff_info.codepage);
  1837. if (res->coff_info.reserved != 0)
  1838. wr_printcomment (e, "COFF reserved value: %u", res->coff_info.reserved);
  1839. wr_print (e, "\n");
  1840. if (rt == RT_STRING)
  1841. ;
  1842. else
  1843. {
  1844. if (name != NULL)
  1845. res_id_print (e, *name, 1);
  1846. else
  1847. fprintf (e, "??Unknown-Name??");
  1848. fprintf (e, " ");
  1849. }
  1850. if (s != NULL)
  1851. fprintf (e, "%s", s);
  1852. else if (type != NULL)
  1853. {
  1854. if (type->named == 0)
  1855. {
  1856. #define PRINT_RT_NAME(NAME) case NAME: \
  1857. fprintf (e, "%u /* %s */", (unsigned int) NAME, #NAME); \
  1858. break
  1859. switch (type->u.id)
  1860. {
  1861. default:
  1862. res_id_print (e, *type, 0);
  1863. break;
  1864. PRINT_RT_NAME(RT_MANIFEST);
  1865. PRINT_RT_NAME(RT_ANICURSOR);
  1866. PRINT_RT_NAME(RT_ANIICON);
  1867. PRINT_RT_NAME(RT_RCDATA);
  1868. PRINT_RT_NAME(RT_ICON);
  1869. PRINT_RT_NAME(RT_CURSOR);
  1870. PRINT_RT_NAME(RT_BITMAP);
  1871. PRINT_RT_NAME(RT_PLUGPLAY);
  1872. PRINT_RT_NAME(RT_VXD);
  1873. PRINT_RT_NAME(RT_FONT);
  1874. PRINT_RT_NAME(RT_FONTDIR);
  1875. PRINT_RT_NAME(RT_HTML);
  1876. PRINT_RT_NAME(RT_MESSAGETABLE);
  1877. PRINT_RT_NAME(RT_DLGINCLUDE);
  1878. PRINT_RT_NAME(RT_DLGINIT);
  1879. }
  1880. #undef PRINT_RT_NAME
  1881. }
  1882. else
  1883. res_id_print (e, *type, 1);
  1884. }
  1885. else
  1886. fprintf (e, "??Unknown-Type??");
  1887. if (res->res_info.memflags != 0)
  1888. {
  1889. if ((res->res_info.memflags & MEMFLAG_MOVEABLE) != 0)
  1890. fprintf (e, " MOVEABLE");
  1891. if ((res->res_info.memflags & MEMFLAG_PURE) != 0)
  1892. fprintf (e, " PURE");
  1893. if ((res->res_info.memflags & MEMFLAG_PRELOAD) != 0)
  1894. fprintf (e, " PRELOAD");
  1895. if ((res->res_info.memflags & MEMFLAG_DISCARDABLE) != 0)
  1896. fprintf (e, " DISCARDABLE");
  1897. }
  1898. if (res->type == RES_TYPE_DIALOG)
  1899. {
  1900. fprintf (e, " %d, %d, %d, %d",
  1901. (int) res->u.dialog->x, (int) res->u.dialog->y,
  1902. (int) res->u.dialog->width, (int) res->u.dialog->height);
  1903. if (res->u.dialog->ex != NULL
  1904. && res->u.dialog->ex->help != 0)
  1905. fprintf (e, ", %u", (unsigned int) res->u.dialog->ex->help);
  1906. }
  1907. else if (res->type == RES_TYPE_TOOLBAR)
  1908. {
  1909. fprintf (e, " %d, %d", (int) res->u.toolbar->button_width,
  1910. (int) res->u.toolbar->button_height);
  1911. }
  1912. fprintf (e, "\n");
  1913. if ((res->res_info.language != 0 && res->res_info.language != *language)
  1914. || res->res_info.characteristics != 0
  1915. || res->res_info.version != 0)
  1916. {
  1917. int modifiers;
  1918. switch (res->type)
  1919. {
  1920. case RES_TYPE_ACCELERATOR:
  1921. case RES_TYPE_DIALOG:
  1922. case RES_TYPE_MENU:
  1923. case RES_TYPE_RCDATA:
  1924. case RES_TYPE_STRINGTABLE:
  1925. modifiers = 1;
  1926. break;
  1927. default:
  1928. modifiers = 0;
  1929. break;
  1930. }
  1931. if (res->res_info.language != 0 && res->res_info.language != *language)
  1932. fprintf (e, "%sLANGUAGE %d, %d\n",
  1933. modifiers ? "// " : "",
  1934. (int) res->res_info.language & ((1<<SUBLANG_SHIFT)-1),
  1935. (int) (res->res_info.language >> SUBLANG_SHIFT) & 0xff);
  1936. if (res->res_info.characteristics != 0)
  1937. fprintf (e, "%sCHARACTERISTICS %u\n",
  1938. modifiers ? "// " : "",
  1939. (unsigned int) res->res_info.characteristics);
  1940. if (res->res_info.version != 0)
  1941. fprintf (e, "%sVERSION %u\n",
  1942. modifiers ? "// " : "",
  1943. (unsigned int) res->res_info.version);
  1944. }
  1945. switch (res->type)
  1946. {
  1947. default:
  1948. abort ();
  1949. case RES_TYPE_ACCELERATOR:
  1950. write_rc_accelerators (e, res->u.acc);
  1951. break;
  1952. case RES_TYPE_CURSOR:
  1953. write_rc_cursor (e, res->u.cursor);
  1954. break;
  1955. case RES_TYPE_GROUP_CURSOR:
  1956. write_rc_group_cursor (e, res->u.group_cursor);
  1957. break;
  1958. case RES_TYPE_DIALOG:
  1959. write_rc_dialog (e, res->u.dialog);
  1960. break;
  1961. case RES_TYPE_FONTDIR:
  1962. write_rc_fontdir (e, res->u.fontdir);
  1963. break;
  1964. case RES_TYPE_GROUP_ICON:
  1965. write_rc_group_icon (e, res->u.group_icon);
  1966. break;
  1967. case RES_TYPE_MENU:
  1968. write_rc_menu (e, res->u.menu, menuex);
  1969. break;
  1970. case RES_TYPE_RCDATA:
  1971. write_rc_rcdata (e, res->u.rcdata, 0);
  1972. break;
  1973. case RES_TYPE_STRINGTABLE:
  1974. write_rc_stringtable (e, name, res->u.stringtable);
  1975. break;
  1976. case RES_TYPE_USERDATA:
  1977. write_rc_rcdata (e, res->u.userdata, 0);
  1978. break;
  1979. case RES_TYPE_TOOLBAR:
  1980. write_rc_toolbar (e, res->u.toolbar);
  1981. break;
  1982. case RES_TYPE_VERSIONINFO:
  1983. write_rc_versioninfo (e, res->u.versioninfo);
  1984. break;
  1985. case RES_TYPE_BITMAP:
  1986. case RES_TYPE_FONT:
  1987. case RES_TYPE_ICON:
  1988. write_rc_datablock (e, res->u.data.length, res->u.data.data, 0, 1, 0);
  1989. break;
  1990. case RES_TYPE_MESSAGETABLE:
  1991. write_rc_messagetable (e, res->u.data.length, res->u.data.data);
  1992. break;
  1993. }
  1994. }
  1995. /* Write out accelerator information. */
  1996. static void
  1997. write_rc_accelerators (FILE *e, const rc_accelerator *accelerators)
  1998. {
  1999. const rc_accelerator *acc;
  2000. fprintf (e, "BEGIN\n");
  2001. for (acc = accelerators; acc != NULL; acc = acc->next)
  2002. {
  2003. int printable;
  2004. fprintf (e, " ");
  2005. if ((acc->key & 0x7f) == acc->key
  2006. && ISPRINT (acc->key)
  2007. && (acc->flags & ACC_VIRTKEY) == 0)
  2008. {
  2009. fprintf (e, "\"%c\"", (char) acc->key);
  2010. printable = 1;
  2011. }
  2012. else
  2013. {
  2014. fprintf (e, "%d", (int) acc->key);
  2015. printable = 0;
  2016. }
  2017. fprintf (e, ", %d", (int) acc->id);
  2018. if (! printable)
  2019. {
  2020. if ((acc->flags & ACC_VIRTKEY) != 0)
  2021. fprintf (e, ", VIRTKEY");
  2022. else
  2023. fprintf (e, ", ASCII");
  2024. }
  2025. if ((acc->flags & ACC_SHIFT) != 0)
  2026. fprintf (e, ", SHIFT");
  2027. if ((acc->flags & ACC_CONTROL) != 0)
  2028. fprintf (e, ", CONTROL");
  2029. if ((acc->flags & ACC_ALT) != 0)
  2030. fprintf (e, ", ALT");
  2031. fprintf (e, "\n");
  2032. }
  2033. fprintf (e, "END\n");
  2034. }
  2035. /* Write out cursor information. This would normally be in a separate
  2036. file, which the rc file would include. */
  2037. static void
  2038. write_rc_cursor (FILE *e, const rc_cursor *cursor)
  2039. {
  2040. fprintf (e, "BEGIN\n");
  2041. indent (e, 2);
  2042. fprintf (e, " 0x%x, 0x%x,\t/* Hotspot x: %d, y: %d. */\n",
  2043. (unsigned int) cursor->xhotspot, (unsigned int) cursor->yhotspot,
  2044. (int) cursor->xhotspot, (int) cursor->yhotspot);
  2045. write_rc_datablock (e, (rc_uint_type) cursor->length, (const bfd_byte *) cursor->data,
  2046. 0, 0, 0);
  2047. fprintf (e, "END\n");
  2048. }
  2049. /* Write out group cursor data. This would normally be built from the
  2050. cursor data. */
  2051. static void
  2052. write_rc_group_cursor (FILE *e, const rc_group_cursor *group_cursor)
  2053. {
  2054. const rc_group_cursor *gc;
  2055. int c;
  2056. for (c = 0, gc = group_cursor; gc != NULL; gc = gc->next, c++)
  2057. ;
  2058. fprintf (e, "BEGIN\n");
  2059. indent (e, 2);
  2060. fprintf (e, "0, 2, %d%s\t /* Having %d items. */\n", c, (c != 0 ? "," : ""), c);
  2061. indent (e, 4);
  2062. fprintf (e, "/* width, height, planes, bits, bytes, index. */\n");
  2063. for (c = 1, gc = group_cursor; gc != NULL; gc = gc->next, c++)
  2064. {
  2065. indent (e, 4);
  2066. fprintf (e, "%d, %d, %d, %d, 0x%xL, %d%s /* Element %d. */\n",
  2067. (int) gc->width, (int) gc->height, (int) gc->planes, (int) gc->bits,
  2068. (unsigned int) gc->bytes, (int) gc->index, (gc->next != NULL ? "," : ""), c);
  2069. fprintf (e, "/* width: %d; height %d; planes %d; bits %d. */\n",
  2070. (int) gc->width, (int) gc->height, (int) gc->planes,
  2071. (int) gc->bits);
  2072. }
  2073. fprintf (e, "END\n");
  2074. }
  2075. /* Write dialog data. */
  2076. static void
  2077. write_rc_dialog (FILE *e, const rc_dialog *dialog)
  2078. {
  2079. const rc_dialog_control *control;
  2080. fprintf (e, "STYLE 0x%x\n", dialog->style);
  2081. if (dialog->exstyle != 0)
  2082. fprintf (e, "EXSTYLE 0x%x\n", (unsigned int) dialog->exstyle);
  2083. if ((dialog->class.named && dialog->class.u.n.length > 0)
  2084. || dialog->class.u.id != 0)
  2085. {
  2086. fprintf (e, "CLASS ");
  2087. res_id_print (e, dialog->class, 1);
  2088. fprintf (e, "\n");
  2089. }
  2090. if (dialog->caption != NULL)
  2091. {
  2092. fprintf (e, "CAPTION ");
  2093. unicode_print_quoted (e, dialog->caption, -1);
  2094. fprintf (e, "\n");
  2095. }
  2096. if ((dialog->menu.named && dialog->menu.u.n.length > 0)
  2097. || dialog->menu.u.id != 0)
  2098. {
  2099. fprintf (e, "MENU ");
  2100. res_id_print (e, dialog->menu, 0);
  2101. fprintf (e, "\n");
  2102. }
  2103. if (dialog->font != NULL)
  2104. {
  2105. fprintf (e, "FONT %d, ", (int) dialog->pointsize);
  2106. unicode_print_quoted (e, dialog->font, -1);
  2107. if (dialog->ex != NULL
  2108. && (dialog->ex->weight != 0
  2109. || dialog->ex->italic != 0
  2110. || dialog->ex->charset != 1))
  2111. fprintf (e, ", %d, %d, %d",
  2112. (int) dialog->ex->weight,
  2113. (int) dialog->ex->italic,
  2114. (int) dialog->ex->charset);
  2115. fprintf (e, "\n");
  2116. }
  2117. fprintf (e, "BEGIN\n");
  2118. for (control = dialog->controls; control != NULL; control = control->next)
  2119. write_rc_dialog_control (e, control);
  2120. fprintf (e, "END\n");
  2121. }
  2122. /* For each predefined control keyword, this table provides the class
  2123. and the style. */
  2124. struct control_info
  2125. {
  2126. const char *name;
  2127. unsigned short class;
  2128. unsigned long style;
  2129. };
  2130. static const struct control_info control_info[] =
  2131. {
  2132. { "AUTO3STATE", CTL_BUTTON, BS_AUTO3STATE },
  2133. { "AUTOCHECKBOX", CTL_BUTTON, BS_AUTOCHECKBOX },
  2134. { "AUTORADIOBUTTON", CTL_BUTTON, BS_AUTORADIOBUTTON },
  2135. { "CHECKBOX", CTL_BUTTON, BS_CHECKBOX },
  2136. { "COMBOBOX", CTL_COMBOBOX, (unsigned long) -1 },
  2137. { "CTEXT", CTL_STATIC, SS_CENTER },
  2138. { "DEFPUSHBUTTON", CTL_BUTTON, BS_DEFPUSHBUTTON },
  2139. { "EDITTEXT", CTL_EDIT, (unsigned long) -1 },
  2140. { "GROUPBOX", CTL_BUTTON, BS_GROUPBOX },
  2141. { "ICON", CTL_STATIC, SS_ICON },
  2142. { "LISTBOX", CTL_LISTBOX, (unsigned long) -1 },
  2143. { "LTEXT", CTL_STATIC, SS_LEFT },
  2144. { "PUSHBOX", CTL_BUTTON, BS_PUSHBOX },
  2145. { "PUSHBUTTON", CTL_BUTTON, BS_PUSHBUTTON },
  2146. { "RADIOBUTTON", CTL_BUTTON, BS_RADIOBUTTON },
  2147. { "RTEXT", CTL_STATIC, SS_RIGHT },
  2148. { "SCROLLBAR", CTL_SCROLLBAR, (unsigned long) -1 },
  2149. { "STATE3", CTL_BUTTON, BS_3STATE },
  2150. /* It's important that USERBUTTON come after all the other button
  2151. types, so that it won't be matched too early. */
  2152. { "USERBUTTON", CTL_BUTTON, (unsigned long) -1 },
  2153. { NULL, 0, 0 }
  2154. };
  2155. /* Write a dialog control. */
  2156. static void
  2157. write_rc_dialog_control (FILE *e, const rc_dialog_control *control)
  2158. {
  2159. const struct control_info *ci;
  2160. fprintf (e, " ");
  2161. if (control->class.named)
  2162. ci = NULL;
  2163. else
  2164. {
  2165. for (ci = control_info; ci->name != NULL; ++ci)
  2166. if (ci->class == control->class.u.id
  2167. && (ci->style == (unsigned long) -1
  2168. || ci->style == (control->style & 0xff)))
  2169. break;
  2170. }
  2171. if (ci == NULL)
  2172. fprintf (e, "CONTROL");
  2173. else if (ci->name != NULL)
  2174. fprintf (e, "%s", ci->name);
  2175. else
  2176. {
  2177. fprintf (e, "CONTROL");
  2178. ci = NULL;
  2179. }
  2180. /* For EDITTEXT, COMBOBOX, LISTBOX, and SCROLLBAR don't dump text. */
  2181. if ((control->text.named || control->text.u.id != 0)
  2182. && (!ci
  2183. || (ci->class != CTL_EDIT
  2184. && ci->class != CTL_COMBOBOX
  2185. && ci->class != CTL_LISTBOX
  2186. && ci->class != CTL_SCROLLBAR)))
  2187. {
  2188. fprintf (e, " ");
  2189. res_id_print (e, control->text, 1);
  2190. fprintf (e, ",");
  2191. }
  2192. fprintf (e, " %d, ", (int) control->id);
  2193. if (ci == NULL)
  2194. {
  2195. if (control->class.named)
  2196. fprintf (e, "\"");
  2197. res_id_print (e, control->class, 0);
  2198. if (control->class.named)
  2199. fprintf (e, "\"");
  2200. fprintf (e, ", 0x%x, ", (unsigned int) control->style);
  2201. }
  2202. fprintf (e, "%d, %d", (int) control->x, (int) control->y);
  2203. if (control->style != SS_ICON
  2204. || control->exstyle != 0
  2205. || control->width != 0
  2206. || control->height != 0
  2207. || control->help != 0)
  2208. {
  2209. fprintf (e, ", %d, %d", (int) control->width, (int) control->height);
  2210. /* FIXME: We don't need to print the style if it is the default.
  2211. More importantly, in certain cases we actually need to turn
  2212. off parts of the forced style, by using NOT. */
  2213. if (ci != NULL)
  2214. fprintf (e, ", 0x%x", (unsigned int) control->style);
  2215. if (control->exstyle != 0 || control->help != 0)
  2216. fprintf (e, ", 0x%x, %u", (unsigned int) control->exstyle,
  2217. (unsigned int) control->help);
  2218. }
  2219. fprintf (e, "\n");
  2220. if (control->data != NULL)
  2221. write_rc_rcdata (e, control->data, 2);
  2222. }
  2223. /* Write out font directory data. This would normally be built from
  2224. the font data. */
  2225. static void
  2226. write_rc_fontdir (FILE *e, const rc_fontdir *fontdir)
  2227. {
  2228. const rc_fontdir *fc;
  2229. int c;
  2230. for (c = 0, fc = fontdir; fc != NULL; fc = fc->next, c++)
  2231. ;
  2232. fprintf (e, "BEGIN\n");
  2233. indent (e, 2);
  2234. fprintf (e, "%d%s\t /* Has %d elements. */\n", c, (c != 0 ? "," : ""), c);
  2235. for (c = 1, fc = fontdir; fc != NULL; fc = fc->next, c++)
  2236. {
  2237. indent (e, 4);
  2238. fprintf (e, "%d,\t/* Font no %d with index %d. */\n",
  2239. (int) fc->index, c, (int) fc->index);
  2240. write_rc_datablock (e, (rc_uint_type) fc->length - 2,
  2241. (const bfd_byte *) fc->data + 4,fc->next != NULL,
  2242. 0, 0);
  2243. }
  2244. fprintf (e, "END\n");
  2245. }
  2246. /* Write out group icon data. This would normally be built from the
  2247. icon data. */
  2248. static void
  2249. write_rc_group_icon (FILE *e, const rc_group_icon *group_icon)
  2250. {
  2251. const rc_group_icon *gi;
  2252. int c;
  2253. for (c = 0, gi = group_icon; gi != NULL; gi = gi->next, c++)
  2254. ;
  2255. fprintf (e, "BEGIN\n");
  2256. indent (e, 2);
  2257. fprintf (e, " 0, 1, %d%s\t /* Has %d elements. */\n", c, (c != 0 ? "," : ""), c);
  2258. indent (e, 4);
  2259. fprintf (e, "/* \"width height colors pad\", planes, bits, bytes, index. */\n");
  2260. for (c = 1, gi = group_icon; gi != NULL; gi = gi->next, c++)
  2261. {
  2262. indent (e, 4);
  2263. fprintf (e, "\"\\%03o\\%03o\\%03o\\%03o\", %d, %d, 0x%xL, %d%s\t/* Element no %d. */\n",
  2264. gi->width, gi->height, gi->colors, 0, (int) gi->planes, (int) gi->bits,
  2265. (unsigned int) gi->bytes, (int) gi->index, (gi->next != NULL ? "," : ""), c);
  2266. }
  2267. fprintf (e, "END\n");
  2268. }
  2269. /* Write out a menu resource. */
  2270. static void
  2271. write_rc_menu (FILE *e, const rc_menu *menu, int menuex)
  2272. {
  2273. if (menu->help != 0)
  2274. fprintf (e, "// Help ID: %u\n", (unsigned int) menu->help);
  2275. write_rc_menuitems (e, menu->items, menuex, 0);
  2276. }
  2277. static void
  2278. write_rc_toolbar (FILE *e, const rc_toolbar *tb)
  2279. {
  2280. rc_toolbar_item *it;
  2281. indent (e, 0);
  2282. fprintf (e, "BEGIN\n");
  2283. it = tb->items;
  2284. while(it != NULL)
  2285. {
  2286. indent (e, 2);
  2287. if (it->id.u.id == 0)
  2288. fprintf (e, "SEPARATOR\n");
  2289. else
  2290. fprintf (e, "BUTTON %d\n", (int) it->id.u.id);
  2291. it = it->next;
  2292. }
  2293. indent (e, 0);
  2294. fprintf (e, "END\n");
  2295. }
  2296. /* Write out menuitems. */
  2297. static void
  2298. write_rc_menuitems (FILE *e, const rc_menuitem *menuitems, int menuex,
  2299. int ind)
  2300. {
  2301. const rc_menuitem *mi;
  2302. indent (e, ind);
  2303. fprintf (e, "BEGIN\n");
  2304. for (mi = menuitems; mi != NULL; mi = mi->next)
  2305. {
  2306. indent (e, ind + 2);
  2307. if (mi->popup == NULL)
  2308. fprintf (e, "MENUITEM");
  2309. else
  2310. fprintf (e, "POPUP");
  2311. if (! menuex
  2312. && mi->popup == NULL
  2313. && mi->text == NULL
  2314. && mi->type == 0
  2315. && mi->id == 0)
  2316. {
  2317. fprintf (e, " SEPARATOR\n");
  2318. continue;
  2319. }
  2320. if (mi->text == NULL)
  2321. fprintf (e, " \"\"");
  2322. else
  2323. {
  2324. fprintf (e, " ");
  2325. unicode_print_quoted (e, mi->text, -1);
  2326. }
  2327. if (! menuex)
  2328. {
  2329. if (mi->popup == NULL)
  2330. fprintf (e, ", %d", (int) mi->id);
  2331. if ((mi->type & MENUITEM_CHECKED) != 0)
  2332. fprintf (e, ", CHECKED");
  2333. if ((mi->type & MENUITEM_GRAYED) != 0)
  2334. fprintf (e, ", GRAYED");
  2335. if ((mi->type & MENUITEM_HELP) != 0)
  2336. fprintf (e, ", HELP");
  2337. if ((mi->type & MENUITEM_INACTIVE) != 0)
  2338. fprintf (e, ", INACTIVE");
  2339. if ((mi->type & MENUITEM_MENUBARBREAK) != 0)
  2340. fprintf (e, ", MENUBARBREAK");
  2341. if ((mi->type & MENUITEM_MENUBREAK) != 0)
  2342. fprintf (e, ", MENUBREAK");
  2343. }
  2344. else
  2345. {
  2346. if (mi->id != 0 || mi->type != 0 || mi->state != 0 || mi->help != 0)
  2347. {
  2348. fprintf (e, ", %d", (int) mi->id);
  2349. if (mi->type != 0 || mi->state != 0 || mi->help != 0)
  2350. {
  2351. fprintf (e, ", %u", (unsigned int) mi->type);
  2352. if (mi->state != 0 || mi->help != 0)
  2353. {
  2354. fprintf (e, ", %u", (unsigned int) mi->state);
  2355. if (mi->help != 0)
  2356. fprintf (e, ", %u", (unsigned int) mi->help);
  2357. }
  2358. }
  2359. }
  2360. }
  2361. fprintf (e, "\n");
  2362. if (mi->popup != NULL)
  2363. write_rc_menuitems (e, mi->popup, menuex, ind + 2);
  2364. }
  2365. indent (e, ind);
  2366. fprintf (e, "END\n");
  2367. }
  2368. static int
  2369. test_rc_datablock_unicode (rc_uint_type length, const bfd_byte *data)
  2370. {
  2371. rc_uint_type i;
  2372. if ((length & 1) != 0)
  2373. return 0;
  2374. for (i = 0; i < length; i += 2)
  2375. {
  2376. if (data[i] == 0 && data[i + 1] == 0 && (i + 2) < length)
  2377. return 0;
  2378. if (data[i] == 0xff && data[i + 1] == 0xff)
  2379. return 0;
  2380. }
  2381. return 1;
  2382. }
  2383. static int
  2384. test_rc_datablock_text (rc_uint_type length, const bfd_byte *data)
  2385. {
  2386. int has_nl;
  2387. rc_uint_type c;
  2388. rc_uint_type i;
  2389. if (length <= 1)
  2390. return 0;
  2391. has_nl = 0;
  2392. for (i = 0, c = 0; i < length; i++)
  2393. {
  2394. if (! ISPRINT (data[i]) && data[i] != '\n'
  2395. && ! (data[i] == '\r' && (i + 1) < length && data[i + 1] == '\n')
  2396. && data[i] != '\t'
  2397. && ! (data[i] == 0 && (i + 1) != length))
  2398. {
  2399. if (data[i] <= 7)
  2400. return 0;
  2401. c++;
  2402. }
  2403. else if (data[i] == '\n') has_nl++;
  2404. }
  2405. if (length > 80 && ! has_nl)
  2406. return 0;
  2407. c = (((c * 10000) + (i / 100) - 1)) / i;
  2408. if (c >= 150)
  2409. return 0;
  2410. return 1;
  2411. }
  2412. static void
  2413. write_rc_messagetable (FILE *e, rc_uint_type length, const bfd_byte *data)
  2414. {
  2415. int has_error = 0;
  2416. const struct bin_messagetable *mt;
  2417. fprintf (e, "BEGIN\n");
  2418. write_rc_datablock (e, length, data, 0, 0, 0);
  2419. fprintf (e, "\n");
  2420. wr_printcomment (e, "MC syntax dump");
  2421. if (length < BIN_MESSAGETABLE_SIZE)
  2422. has_error = 1;
  2423. else
  2424. do
  2425. {
  2426. rc_uint_type m, i;
  2427. mt = (const struct bin_messagetable *) data;
  2428. m = windres_get_32 (&wrtarget, mt->cblocks, length);
  2429. if (length < (BIN_MESSAGETABLE_SIZE + m * BIN_MESSAGETABLE_BLOCK_SIZE))
  2430. {
  2431. has_error = 1;
  2432. break;
  2433. }
  2434. for (i = 0; i < m; i++)
  2435. {
  2436. rc_uint_type low, high, offset;
  2437. const struct bin_messagetable_item *mti;
  2438. low = windres_get_32 (&wrtarget, mt->items[i].lowid, 4);
  2439. high = windres_get_32 (&wrtarget, mt->items[i].highid, 4);
  2440. offset = windres_get_32 (&wrtarget, mt->items[i].offset, 4);
  2441. while (low <= high)
  2442. {
  2443. rc_uint_type elen, flags;
  2444. if ((offset + BIN_MESSAGETABLE_ITEM_SIZE) > length)
  2445. {
  2446. has_error = 1;
  2447. break;
  2448. }
  2449. mti = (const struct bin_messagetable_item *) &data[offset];
  2450. elen = windres_get_16 (&wrtarget, mti->length, 2);
  2451. flags = windres_get_16 (&wrtarget, mti->flags, 2);
  2452. if ((offset + elen) > length)
  2453. {
  2454. has_error = 1;
  2455. break;
  2456. }
  2457. wr_printcomment (e, "MessageId = 0x%x", low);
  2458. wr_printcomment (e, "");
  2459. if ((flags & MESSAGE_RESOURCE_UNICODE) == MESSAGE_RESOURCE_UNICODE)
  2460. {
  2461. /* PR 17512: file: 5c3232dc. */
  2462. if (elen > BIN_MESSAGETABLE_ITEM_SIZE * 2)
  2463. unicode_print (e, (const unichar *) mti->data,
  2464. (elen - BIN_MESSAGETABLE_ITEM_SIZE) / 2);
  2465. }
  2466. else
  2467. {
  2468. if (elen > BIN_MESSAGETABLE_ITEM_SIZE)
  2469. ascii_print (e, (const char *) mti->data,
  2470. (elen - BIN_MESSAGETABLE_ITEM_SIZE));
  2471. }
  2472. wr_printcomment (e,"");
  2473. ++low;
  2474. offset += elen;
  2475. }
  2476. }
  2477. }
  2478. while (0);
  2479. if (has_error)
  2480. wr_printcomment (e, "Illegal data");
  2481. wr_print_flush (e);
  2482. fprintf (e, "END\n");
  2483. }
  2484. static void
  2485. write_rc_datablock (FILE *e, rc_uint_type length, const bfd_byte *data, int has_next,
  2486. int hasblock, int show_comment)
  2487. {
  2488. int plen;
  2489. if (hasblock)
  2490. fprintf (e, "BEGIN\n");
  2491. if (show_comment == -1)
  2492. {
  2493. if (test_rc_datablock_text(length, data))
  2494. {
  2495. rc_uint_type i, c;
  2496. for (i = 0; i < length;)
  2497. {
  2498. indent (e, 2);
  2499. fprintf (e, "\"");
  2500. for (c = 0; i < length && c < 160 && data[i] != '\n'; c++, i++)
  2501. ;
  2502. if (i < length && data[i] == '\n')
  2503. ++i, ++c;
  2504. ascii_print(e, (const char *) &data[i - c], c);
  2505. fprintf (e, "\"");
  2506. if (i < length)
  2507. fprintf (e, "\n");
  2508. }
  2509. if (i == 0)
  2510. {
  2511. indent (e, 2);
  2512. fprintf (e, "\"\"");
  2513. }
  2514. if (has_next)
  2515. fprintf (e, ",");
  2516. fprintf (e, "\n");
  2517. if (hasblock)
  2518. fprintf (e, "END\n");
  2519. return;
  2520. }
  2521. if (test_rc_datablock_unicode (length, data))
  2522. {
  2523. rc_uint_type i, c;
  2524. for (i = 0; i < length;)
  2525. {
  2526. const unichar *u;
  2527. u = (const unichar *) &data[i];
  2528. indent (e, 2);
  2529. fprintf (e, "L\"");
  2530. for (c = 0; i < length && c < 160 && u[c] != '\n'; c++, i += 2)
  2531. ;
  2532. if (i < length && u[c] == '\n')
  2533. i += 2, ++c;
  2534. unicode_print (e, u, c);
  2535. fprintf (e, "\"");
  2536. if (i < length)
  2537. fprintf (e, "\n");
  2538. }
  2539. if (i == 0)
  2540. {
  2541. indent (e, 2);
  2542. fprintf (e, "L\"\"");
  2543. }
  2544. if (has_next)
  2545. fprintf (e, ",");
  2546. fprintf (e, "\n");
  2547. if (hasblock)
  2548. fprintf (e, "END\n");
  2549. return;
  2550. }
  2551. show_comment = 0;
  2552. }
  2553. if (length != 0)
  2554. {
  2555. rc_uint_type i, max_row;
  2556. int first = 1;
  2557. max_row = (show_comment ? 4 : 8);
  2558. indent (e, 2);
  2559. for (i = 0; i + 3 < length;)
  2560. {
  2561. rc_uint_type k;
  2562. rc_uint_type comment_start;
  2563. comment_start = i;
  2564. if (! first)
  2565. indent (e, 2);
  2566. for (k = 0; k < max_row && i + 3 < length; k++, i += 4)
  2567. {
  2568. if (k == 0)
  2569. plen = fprintf (e, "0x%lxL",
  2570. (unsigned long) windres_get_32 (&wrtarget, data + i, length - i));
  2571. else
  2572. plen = fprintf (e, " 0x%lxL",
  2573. (unsigned long) windres_get_32 (&wrtarget, data + i, length - i)) - 1;
  2574. if (has_next || (i + 4) < length)
  2575. {
  2576. if (plen>0 && plen < 11)
  2577. indent (e, 11 - plen);
  2578. fprintf (e, ",");
  2579. }
  2580. }
  2581. if (show_comment)
  2582. {
  2583. fprintf (e, "\t/* ");
  2584. ascii_print (e, (const char *) &data[comment_start], i - comment_start);
  2585. fprintf (e, ". */");
  2586. }
  2587. fprintf (e, "\n");
  2588. first = 0;
  2589. }
  2590. if (i + 1 < length)
  2591. {
  2592. if (! first)
  2593. indent (e, 2);
  2594. plen = fprintf (e, "0x%x",
  2595. (int) windres_get_16 (&wrtarget, data + i, length - i));
  2596. if (has_next || i + 2 < length)
  2597. {
  2598. if (plen > 0 && plen < 11)
  2599. indent (e, 11 - plen);
  2600. fprintf (e, ",");
  2601. }
  2602. if (show_comment)
  2603. {
  2604. fprintf (e, "\t/* ");
  2605. ascii_print (e, (const char *) &data[i], 2);
  2606. fprintf (e, ". */");
  2607. }
  2608. fprintf (e, "\n");
  2609. i += 2;
  2610. first = 0;
  2611. }
  2612. if (i < length)
  2613. {
  2614. if (! first)
  2615. indent (e, 2);
  2616. fprintf (e, "\"");
  2617. ascii_print (e, (const char *) &data[i], 1);
  2618. fprintf (e, "\"");
  2619. if (has_next)
  2620. fprintf (e, ",");
  2621. fprintf (e, "\n");
  2622. first = 0;
  2623. }
  2624. }
  2625. if (hasblock)
  2626. fprintf (e, "END\n");
  2627. }
  2628. /* Write out an rcdata resource. This is also used for other types of
  2629. resources that need to print arbitrary data. */
  2630. static void
  2631. write_rc_rcdata (FILE *e, const rc_rcdata_item *rcdata, int ind)
  2632. {
  2633. const rc_rcdata_item *ri;
  2634. indent (e, ind);
  2635. fprintf (e, "BEGIN\n");
  2636. for (ri = rcdata; ri != NULL; ri = ri->next)
  2637. {
  2638. if (ri->type == RCDATA_BUFFER && ri->u.buffer.length == 0)
  2639. continue;
  2640. switch (ri->type)
  2641. {
  2642. default:
  2643. abort ();
  2644. case RCDATA_WORD:
  2645. indent (e, ind + 2);
  2646. fprintf (e, "%ld", (long) (ri->u.word & 0xffff));
  2647. break;
  2648. case RCDATA_DWORD:
  2649. indent (e, ind + 2);
  2650. fprintf (e, "%luL", (unsigned long) ri->u.dword);
  2651. break;
  2652. case RCDATA_STRING:
  2653. indent (e, ind + 2);
  2654. fprintf (e, "\"");
  2655. ascii_print (e, ri->u.string.s, ri->u.string.length);
  2656. fprintf (e, "\"");
  2657. break;
  2658. case RCDATA_WSTRING:
  2659. indent (e, ind + 2);
  2660. fprintf (e, "L\"");
  2661. unicode_print (e, ri->u.wstring.w, ri->u.wstring.length);
  2662. fprintf (e, "\"");
  2663. break;
  2664. case RCDATA_BUFFER:
  2665. write_rc_datablock (e, (rc_uint_type) ri->u.buffer.length,
  2666. (const bfd_byte *) ri->u.buffer.data,
  2667. ri->next != NULL, 0, -1);
  2668. break;
  2669. }
  2670. if (ri->type != RCDATA_BUFFER)
  2671. {
  2672. if (ri->next != NULL)
  2673. fprintf (e, ",");
  2674. fprintf (e, "\n");
  2675. }
  2676. }
  2677. indent (e, ind);
  2678. fprintf (e, "END\n");
  2679. }
  2680. /* Write out a stringtable resource. */
  2681. static void
  2682. write_rc_stringtable (FILE *e, const rc_res_id *name,
  2683. const rc_stringtable *stringtable)
  2684. {
  2685. rc_uint_type offset;
  2686. int i;
  2687. if (name != NULL && ! name->named)
  2688. offset = (name->u.id - 1) << 4;
  2689. else
  2690. {
  2691. fprintf (e, "/* %s string table name. */\n",
  2692. name == NULL ? "Missing" : "Invalid");
  2693. offset = 0;
  2694. }
  2695. fprintf (e, "BEGIN\n");
  2696. for (i = 0; i < 16; i++)
  2697. {
  2698. if (stringtable->strings[i].length != 0)
  2699. {
  2700. fprintf (e, " %lu, ", (unsigned long) offset + i);
  2701. unicode_print_quoted (e, stringtable->strings[i].string,
  2702. stringtable->strings[i].length);
  2703. fprintf (e, "\n");
  2704. }
  2705. }
  2706. fprintf (e, "END\n");
  2707. }
  2708. /* Write out a versioninfo resource. */
  2709. static void
  2710. write_rc_versioninfo (FILE *e, const rc_versioninfo *versioninfo)
  2711. {
  2712. const rc_fixed_versioninfo *f;
  2713. const rc_ver_info *vi;
  2714. f = versioninfo->fixed;
  2715. if (f->file_version_ms != 0 || f->file_version_ls != 0)
  2716. fprintf (e, " FILEVERSION %u, %u, %u, %u\n",
  2717. (unsigned int) ((f->file_version_ms >> 16) & 0xffff),
  2718. (unsigned int) (f->file_version_ms & 0xffff),
  2719. (unsigned int) ((f->file_version_ls >> 16) & 0xffff),
  2720. (unsigned int) (f->file_version_ls & 0xffff));
  2721. if (f->product_version_ms != 0 || f->product_version_ls != 0)
  2722. fprintf (e, " PRODUCTVERSION %u, %u, %u, %u\n",
  2723. (unsigned int) ((f->product_version_ms >> 16) & 0xffff),
  2724. (unsigned int) (f->product_version_ms & 0xffff),
  2725. (unsigned int) ((f->product_version_ls >> 16) & 0xffff),
  2726. (unsigned int) (f->product_version_ls & 0xffff));
  2727. if (f->file_flags_mask != 0)
  2728. fprintf (e, " FILEFLAGSMASK 0x%x\n", (unsigned int) f->file_flags_mask);
  2729. if (f->file_flags != 0)
  2730. fprintf (e, " FILEFLAGS 0x%x\n", (unsigned int) f->file_flags);
  2731. if (f->file_os != 0)
  2732. fprintf (e, " FILEOS 0x%x\n", (unsigned int) f->file_os);
  2733. if (f->file_type != 0)
  2734. fprintf (e, " FILETYPE 0x%x\n", (unsigned int) f->file_type);
  2735. if (f->file_subtype != 0)
  2736. fprintf (e, " FILESUBTYPE 0x%x\n", (unsigned int) f->file_subtype);
  2737. if (f->file_date_ms != 0 || f->file_date_ls != 0)
  2738. fprintf (e, "/* Date: %u, %u. */\n",
  2739. (unsigned int) f->file_date_ms, (unsigned int) f->file_date_ls);
  2740. fprintf (e, "BEGIN\n");
  2741. for (vi = versioninfo->var; vi != NULL; vi = vi->next)
  2742. {
  2743. switch (vi->type)
  2744. {
  2745. case VERINFO_STRING:
  2746. {
  2747. const rc_ver_stringtable *vst;
  2748. const rc_ver_stringinfo *vs;
  2749. fprintf (e, " BLOCK \"StringFileInfo\"\n");
  2750. fprintf (e, " BEGIN\n");
  2751. for (vst = vi->u.string.stringtables; vst != NULL; vst = vst->next)
  2752. {
  2753. fprintf (e, " BLOCK ");
  2754. unicode_print_quoted (e, vst->language, -1);
  2755. fprintf (e, "\n");
  2756. fprintf (e, " BEGIN\n");
  2757. for (vs = vst->strings; vs != NULL; vs = vs->next)
  2758. {
  2759. fprintf (e, " VALUE ");
  2760. unicode_print_quoted (e, vs->key, -1);
  2761. fprintf (e, ", ");
  2762. unicode_print_quoted (e, vs->value, -1);
  2763. fprintf (e, "\n");
  2764. }
  2765. fprintf (e, " END\n");
  2766. }
  2767. fprintf (e, " END\n");
  2768. break;
  2769. }
  2770. case VERINFO_VAR:
  2771. {
  2772. const rc_ver_varinfo *vv;
  2773. fprintf (e, " BLOCK \"VarFileInfo\"\n");
  2774. fprintf (e, " BEGIN\n");
  2775. fprintf (e, " VALUE ");
  2776. unicode_print_quoted (e, vi->u.var.key, -1);
  2777. for (vv = vi->u.var.var; vv != NULL; vv = vv->next)
  2778. fprintf (e, ", 0x%x, %d", (unsigned int) vv->language,
  2779. (int) vv->charset);
  2780. fprintf (e, "\n END\n");
  2781. break;
  2782. }
  2783. }
  2784. }
  2785. fprintf (e, "END\n");
  2786. }
  2787. static rc_uint_type
  2788. rcdata_copy (const rc_rcdata_item *src, bfd_byte *dst)
  2789. {
  2790. if (! src)
  2791. return 0;
  2792. switch (src->type)
  2793. {
  2794. case RCDATA_WORD:
  2795. if (dst)
  2796. windres_put_16 (&wrtarget, dst, (rc_uint_type) src->u.word);
  2797. return 2;
  2798. case RCDATA_DWORD:
  2799. if (dst)
  2800. windres_put_32 (&wrtarget, dst, (rc_uint_type) src->u.dword);
  2801. return 4;
  2802. case RCDATA_STRING:
  2803. if (dst && src->u.string.length)
  2804. memcpy (dst, src->u.string.s, src->u.string.length);
  2805. return (rc_uint_type) src->u.string.length;
  2806. case RCDATA_WSTRING:
  2807. if (dst && src->u.wstring.length)
  2808. memcpy (dst, src->u.wstring.w, src->u.wstring.length * sizeof (unichar));
  2809. return (rc_uint_type) (src->u.wstring.length * sizeof (unichar));
  2810. case RCDATA_BUFFER:
  2811. if (dst && src->u.buffer.length)
  2812. memcpy (dst, src->u.buffer.data, src->u.buffer.length);
  2813. return (rc_uint_type) src->u.buffer.length;
  2814. default:
  2815. abort ();
  2816. }
  2817. /* Never reached. */
  2818. return 0;
  2819. }