Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* windres.c -- a program to manipulate Windows resources |
b3adc24a | 2 | Copyright (C) 1997-2020 Free Software Foundation, Inc. |
252b5132 | 3 | Written by Ian Lance Taylor, Cygnus Support. |
4a594fce | 4 | Rewritten by Kai Tietz, Onevision. |
252b5132 RH |
5 | |
6 | This file is part of GNU Binutils. | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
32866df7 | 10 | the Free Software Foundation; either version 3 of the License, or |
252b5132 RH |
11 | (at your option) any later version. |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program; if not, write to the Free Software | |
b43b5d5f NC |
20 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA |
21 | 02110-1301, USA. */ | |
252b5132 RH |
22 | |
23 | /* This program can read and write Windows resources in various | |
24 | formats. In particular, it can act like the rc resource compiler | |
25 | program, and it can act like the cvtres res to COFF conversion | |
26 | program. | |
27 | ||
28 | It is based on information taken from the following sources: | |
29 | ||
30 | * Microsoft documentation. | |
31 | ||
32 | * The rcl program, written by Gunther Ebert | |
33 | <gunther.ebert@ixos-leipzig.de>. | |
34 | ||
29b058f1 | 35 | * The res2coff program, written by Pedro A. Aranda <paag@tid.es>. */ |
252b5132 | 36 | |
3db64b00 | 37 | #include "sysdep.h" |
0af6db78 | 38 | #include <assert.h> |
252b5132 RH |
39 | #include "bfd.h" |
40 | #include "getopt.h" | |
4a594fce | 41 | #include "bucomm.h" |
252b5132 | 42 | #include "libiberty.h" |
3882b010 | 43 | #include "safe-ctype.h" |
252b5132 RH |
44 | #include "obstack.h" |
45 | #include "windres.h" | |
252b5132 | 46 | |
29b058f1 | 47 | /* Used by resrc.c at least. */ |
751d21b5 DD |
48 | |
49 | int verbose = 0; | |
50 | ||
4a594fce NC |
51 | int target_is_bigendian = 0; |
52 | const char *def_target_arch; | |
53 | ||
cc643b88 | 54 | static void set_endianness (bfd *, const char *); |
4a594fce | 55 | |
252b5132 RH |
56 | /* An enumeration of format types. */ |
57 | ||
58 | enum res_format | |
59 | { | |
60 | /* Unknown format. */ | |
61 | RES_FORMAT_UNKNOWN, | |
62 | /* Textual RC file. */ | |
63 | RES_FORMAT_RC, | |
64 | /* Binary RES file. */ | |
65 | RES_FORMAT_RES, | |
66 | /* COFF file. */ | |
67 | RES_FORMAT_COFF | |
68 | }; | |
69 | ||
70 | /* A structure used to map between format types and strings. */ | |
71 | ||
72 | struct format_map | |
73 | { | |
74 | const char *name; | |
75 | enum res_format format; | |
76 | }; | |
77 | ||
78 | /* A mapping between names and format types. */ | |
79 | ||
80 | static const struct format_map format_names[] = | |
81 | { | |
82 | { "rc", RES_FORMAT_RC }, | |
83 | { "res", RES_FORMAT_RES }, | |
84 | { "coff", RES_FORMAT_COFF }, | |
85 | { NULL, RES_FORMAT_UNKNOWN } | |
86 | }; | |
87 | ||
88 | /* A mapping from file extensions to format types. */ | |
89 | ||
90 | static const struct format_map format_fileexts[] = | |
91 | { | |
92 | { "rc", RES_FORMAT_RC }, | |
93 | { "res", RES_FORMAT_RES }, | |
94 | { "exe", RES_FORMAT_COFF }, | |
95 | { "obj", RES_FORMAT_COFF }, | |
96 | { "o", RES_FORMAT_COFF }, | |
97 | { NULL, RES_FORMAT_UNKNOWN } | |
98 | }; | |
99 | ||
100 | /* A list of include directories. */ | |
101 | ||
102 | struct include_dir | |
103 | { | |
104 | struct include_dir *next; | |
105 | char *dir; | |
106 | }; | |
107 | ||
108 | static struct include_dir *include_dirs; | |
109 | ||
252b5132 RH |
110 | /* Static functions. */ |
111 | ||
2da42df6 | 112 | static void res_init (void); |
4a594fce | 113 | static int extended_menuitems (const rc_menuitem *); |
2da42df6 AJ |
114 | static enum res_format format_from_name (const char *, int); |
115 | static enum res_format format_from_filename (const char *, int); | |
116 | static void usage (FILE *, int); | |
117 | static int cmp_res_entry (const void *, const void *); | |
4a594fce | 118 | static rc_res_directory *sort_resources (rc_res_directory *); |
2da42df6 AJ |
119 | static void reswr_init (void); |
120 | static const char * quot (const char *); | |
4a594fce NC |
121 | \f |
122 | static rc_uint_type target_get_8 (const void *, rc_uint_type); | |
123 | static void target_put_8 (void *, rc_uint_type); | |
124 | static rc_uint_type target_get_16 (const void *, rc_uint_type); | |
125 | static void target_put_16 (void *, rc_uint_type); | |
126 | static rc_uint_type target_get_32 (const void *, rc_uint_type); | |
127 | static void target_put_32 (void *, rc_uint_type); | |
128 | ||
252b5132 RH |
129 | \f |
130 | /* When we are building a resource tree, we allocate everything onto | |
131 | an obstack, so that we can free it all at once if we want. */ | |
132 | ||
133 | #define obstack_chunk_alloc xmalloc | |
134 | #define obstack_chunk_free free | |
135 | ||
136 | /* The resource building obstack. */ | |
137 | ||
138 | static struct obstack res_obstack; | |
139 | ||
140 | /* Initialize the resource building obstack. */ | |
141 | ||
142 | static void | |
2da42df6 | 143 | res_init (void) |
252b5132 RH |
144 | { |
145 | obstack_init (&res_obstack); | |
146 | } | |
147 | ||
148 | /* Allocate space on the resource building obstack. */ | |
149 | ||
2da42df6 | 150 | void * |
4a594fce | 151 | res_alloc (rc_uint_type bytes) |
252b5132 | 152 | { |
78aff5a5 | 153 | return obstack_alloc (&res_obstack, (size_t) bytes); |
252b5132 RH |
154 | } |
155 | ||
156 | /* We also use an obstack to save memory used while writing out a set | |
157 | of resources. */ | |
158 | ||
159 | static struct obstack reswr_obstack; | |
160 | ||
161 | /* Initialize the resource writing obstack. */ | |
162 | ||
163 | static void | |
2da42df6 | 164 | reswr_init (void) |
252b5132 RH |
165 | { |
166 | obstack_init (&reswr_obstack); | |
167 | } | |
168 | ||
169 | /* Allocate space on the resource writing obstack. */ | |
170 | ||
2da42df6 | 171 | void * |
4a594fce | 172 | reswr_alloc (rc_uint_type bytes) |
252b5132 | 173 | { |
78aff5a5 | 174 | return obstack_alloc (&reswr_obstack, (size_t) bytes); |
252b5132 RH |
175 | } |
176 | \f | |
177 | /* Open a file using the include directory search list. */ | |
178 | ||
179 | FILE * | |
2da42df6 AJ |
180 | open_file_search (const char *filename, const char *mode, const char *errmsg, |
181 | char **real_filename) | |
252b5132 RH |
182 | { |
183 | FILE *e; | |
184 | struct include_dir *d; | |
185 | ||
186 | e = fopen (filename, mode); | |
187 | if (e != NULL) | |
188 | { | |
189 | *real_filename = xstrdup (filename); | |
190 | return e; | |
191 | } | |
192 | ||
193 | if (errno == ENOENT) | |
194 | { | |
195 | for (d = include_dirs; d != NULL; d = d->next) | |
196 | { | |
197 | char *n; | |
198 | ||
199 | n = (char *) xmalloc (strlen (d->dir) + strlen (filename) + 2); | |
200 | sprintf (n, "%s/%s", d->dir, filename); | |
201 | e = fopen (n, mode); | |
202 | if (e != NULL) | |
203 | { | |
204 | *real_filename = n; | |
205 | return e; | |
206 | } | |
137d1369 | 207 | free (n); |
252b5132 RH |
208 | |
209 | if (errno != ENOENT) | |
210 | break; | |
211 | } | |
212 | } | |
213 | ||
214 | fatal (_("can't open %s `%s': %s"), errmsg, filename, strerror (errno)); | |
215 | ||
216 | /* Return a value to avoid a compiler warning. */ | |
217 | return NULL; | |
218 | } | |
219 | \f | |
220 | /* Compare two resource ID's. We consider name entries to come before | |
221 | numeric entries, because that is how they appear in the COFF .rsrc | |
222 | section. */ | |
223 | ||
224 | int | |
4a594fce | 225 | res_id_cmp (rc_res_id a, rc_res_id b) |
252b5132 RH |
226 | { |
227 | if (! a.named) | |
228 | { | |
229 | if (b.named) | |
230 | return 1; | |
231 | if (a.u.id > b.u.id) | |
232 | return 1; | |
233 | else if (a.u.id < b.u.id) | |
234 | return -1; | |
235 | else | |
236 | return 0; | |
237 | } | |
238 | else | |
239 | { | |
240 | unichar *as, *ase, *bs, *bse; | |
241 | ||
242 | if (! b.named) | |
243 | return -1; | |
244 | ||
245 | as = a.u.n.name; | |
246 | ase = as + a.u.n.length; | |
247 | bs = b.u.n.name; | |
248 | bse = bs + b.u.n.length; | |
249 | ||
250 | while (as < ase) | |
251 | { | |
252 | int i; | |
253 | ||
254 | if (bs >= bse) | |
255 | return 1; | |
256 | i = (int) *as - (int) *bs; | |
257 | if (i != 0) | |
258 | return i; | |
259 | ++as; | |
260 | ++bs; | |
261 | } | |
262 | ||
263 | if (bs < bse) | |
264 | return -1; | |
265 | ||
266 | return 0; | |
267 | } | |
268 | } | |
269 | ||
270 | /* Print a resource ID. */ | |
271 | ||
272 | void | |
4a594fce | 273 | res_id_print (FILE *stream, rc_res_id id, int quote) |
252b5132 RH |
274 | { |
275 | if (! id.named) | |
4a594fce | 276 | fprintf (stream, "%u", (int) id.u.id); |
252b5132 RH |
277 | else |
278 | { | |
279 | if (quote) | |
4a594fce NC |
280 | unicode_print_quoted (stream, id.u.n.name, id.u.n.length); |
281 | else | |
252b5132 | 282 | unicode_print (stream, id.u.n.name, id.u.n.length); |
252b5132 RH |
283 | } |
284 | } | |
285 | ||
286 | /* Print a list of resource ID's. */ | |
287 | ||
288 | void | |
4a594fce | 289 | res_ids_print (FILE *stream, int cids, const rc_res_id *ids) |
252b5132 RH |
290 | { |
291 | int i; | |
292 | ||
293 | for (i = 0; i < cids; i++) | |
294 | { | |
295 | res_id_print (stream, ids[i], 1); | |
296 | if (i + 1 < cids) | |
297 | fprintf (stream, ": "); | |
298 | } | |
299 | } | |
300 | ||
301 | /* Convert an ASCII string to a resource ID. */ | |
302 | ||
303 | void | |
4a594fce | 304 | res_string_to_id (rc_res_id *res_id, const char *string) |
252b5132 RH |
305 | { |
306 | res_id->named = 1; | |
307 | unicode_from_ascii (&res_id->u.n.length, &res_id->u.n.name, string); | |
308 | } | |
309 | ||
4a594fce NC |
310 | /* Convert an unicode string to a resource ID. */ |
311 | void | |
312 | res_unistring_to_id (rc_res_id *res_id, const unichar *u) | |
313 | { | |
314 | res_id->named = 1; | |
315 | res_id->u.n.length = unichar_len (u); | |
316 | res_id->u.n.name = unichar_dup_uppercase (u); | |
317 | } | |
318 | ||
252b5132 RH |
319 | /* Define a resource. The arguments are the resource tree, RESOURCES, |
320 | and the location at which to put it in the tree, CIDS and IDS. | |
4a594fce | 321 | This returns a newly allocated rc_res_resource structure, which the |
252b5132 RH |
322 | caller is expected to initialize. If DUPOK is non-zero, then if a |
323 | resource with this ID exists, it is returned. Otherwise, a warning | |
324 | is issued, and a new resource is created replacing the existing | |
325 | one. */ | |
326 | ||
4a594fce NC |
327 | rc_res_resource * |
328 | define_resource (rc_res_directory **resources, int cids, | |
329 | const rc_res_id *ids, int dupok) | |
252b5132 | 330 | { |
4a594fce | 331 | rc_res_entry *re = NULL; |
252b5132 RH |
332 | int i; |
333 | ||
334 | assert (cids > 0); | |
335 | for (i = 0; i < cids; i++) | |
336 | { | |
4a594fce | 337 | rc_res_entry **pp; |
252b5132 RH |
338 | |
339 | if (*resources == NULL) | |
340 | { | |
4a594fce NC |
341 | *resources = ((rc_res_directory *) |
342 | res_alloc (sizeof (rc_res_directory))); | |
252b5132 | 343 | (*resources)->characteristics = 0; |
0cb112f7 CF |
344 | /* Using a real timestamp only serves to create non-deterministic |
345 | results. Use zero instead. */ | |
346 | (*resources)->time = 0; | |
252b5132 RH |
347 | (*resources)->major = 0; |
348 | (*resources)->minor = 0; | |
349 | (*resources)->entries = NULL; | |
350 | } | |
351 | ||
352 | for (pp = &(*resources)->entries; *pp != NULL; pp = &(*pp)->next) | |
353 | if (res_id_cmp ((*pp)->id, ids[i]) == 0) | |
354 | break; | |
355 | ||
356 | if (*pp != NULL) | |
357 | re = *pp; | |
358 | else | |
359 | { | |
4a594fce | 360 | re = (rc_res_entry *) res_alloc (sizeof (rc_res_entry)); |
252b5132 RH |
361 | re->next = NULL; |
362 | re->id = ids[i]; | |
363 | if ((i + 1) < cids) | |
364 | { | |
365 | re->subdir = 1; | |
366 | re->u.dir = NULL; | |
367 | } | |
368 | else | |
369 | { | |
370 | re->subdir = 0; | |
371 | re->u.res = NULL; | |
372 | } | |
373 | ||
374 | *pp = re; | |
375 | } | |
376 | ||
377 | if ((i + 1) < cids) | |
378 | { | |
379 | if (! re->subdir) | |
380 | { | |
381 | fprintf (stderr, "%s: ", program_name); | |
382 | res_ids_print (stderr, i, ids); | |
383 | fprintf (stderr, _(": expected to be a directory\n")); | |
384 | xexit (1); | |
385 | } | |
386 | ||
387 | resources = &re->u.dir; | |
388 | } | |
389 | } | |
390 | ||
391 | if (re->subdir) | |
392 | { | |
393 | fprintf (stderr, "%s: ", program_name); | |
394 | res_ids_print (stderr, cids, ids); | |
395 | fprintf (stderr, _(": expected to be a leaf\n")); | |
396 | xexit (1); | |
397 | } | |
398 | ||
399 | if (re->u.res != NULL) | |
400 | { | |
401 | if (dupok) | |
402 | return re->u.res; | |
403 | ||
404 | fprintf (stderr, _("%s: warning: "), program_name); | |
405 | res_ids_print (stderr, cids, ids); | |
406 | fprintf (stderr, _(": duplicate value\n")); | |
407 | } | |
408 | ||
4a594fce NC |
409 | re->u.res = ((rc_res_resource *) |
410 | res_alloc (sizeof (rc_res_resource))); | |
411 | memset (re->u.res, 0, sizeof (rc_res_resource)); | |
252b5132 RH |
412 | |
413 | re->u.res->type = RES_TYPE_UNINITIALIZED; | |
252b5132 RH |
414 | return re->u.res; |
415 | } | |
416 | ||
417 | /* Define a standard resource. This is a version of define_resource | |
418 | that just takes type, name, and language arguments. */ | |
419 | ||
4a594fce NC |
420 | rc_res_resource * |
421 | define_standard_resource (rc_res_directory **resources, int type, | |
422 | rc_res_id name, rc_uint_type language, int dupok) | |
252b5132 | 423 | { |
4a594fce | 424 | rc_res_id a[3]; |
252b5132 RH |
425 | |
426 | a[0].named = 0; | |
427 | a[0].u.id = type; | |
428 | a[1] = name; | |
429 | a[2].named = 0; | |
430 | a[2].u.id = language; | |
431 | return define_resource (resources, 3, a, dupok); | |
432 | } | |
433 | ||
434 | /* Comparison routine for resource sorting. */ | |
435 | ||
436 | static int | |
2da42df6 | 437 | cmp_res_entry (const void *p1, const void *p2) |
252b5132 | 438 | { |
4a594fce | 439 | const rc_res_entry **re1, **re2; |
252b5132 | 440 | |
4a594fce NC |
441 | re1 = (const rc_res_entry **) p1; |
442 | re2 = (const rc_res_entry **) p2; | |
252b5132 RH |
443 | return res_id_cmp ((*re1)->id, (*re2)->id); |
444 | } | |
445 | ||
446 | /* Sort the resources. */ | |
447 | ||
4a594fce NC |
448 | static rc_res_directory * |
449 | sort_resources (rc_res_directory *resdir) | |
252b5132 RH |
450 | { |
451 | int c, i; | |
4a594fce NC |
452 | rc_res_entry *re; |
453 | rc_res_entry **a; | |
252b5132 RH |
454 | |
455 | if (resdir->entries == NULL) | |
456 | return resdir; | |
457 | ||
458 | c = 0; | |
459 | for (re = resdir->entries; re != NULL; re = re->next) | |
460 | ++c; | |
461 | ||
462 | /* This is a recursive routine, so using xmalloc is probably better | |
463 | than alloca. */ | |
4a594fce | 464 | a = (rc_res_entry **) xmalloc (c * sizeof (rc_res_entry *)); |
252b5132 RH |
465 | |
466 | for (i = 0, re = resdir->entries; re != NULL; re = re->next, i++) | |
467 | a[i] = re; | |
468 | ||
4a594fce | 469 | qsort (a, c, sizeof (rc_res_entry *), cmp_res_entry); |
252b5132 RH |
470 | |
471 | resdir->entries = a[0]; | |
472 | for (i = 0; i < c - 1; i++) | |
473 | a[i]->next = a[i + 1]; | |
474 | a[i]->next = NULL; | |
475 | ||
476 | free (a); | |
477 | ||
478 | /* Now sort the subdirectories. */ | |
479 | ||
480 | for (re = resdir->entries; re != NULL; re = re->next) | |
481 | if (re->subdir) | |
482 | re->u.dir = sort_resources (re->u.dir); | |
483 | ||
484 | return resdir; | |
485 | } | |
486 | \f | |
487 | /* Return whether the dialog resource DIALOG is a DIALOG or a | |
488 | DIALOGEX. */ | |
489 | ||
490 | int | |
4a594fce | 491 | extended_dialog (const rc_dialog *dialog) |
252b5132 | 492 | { |
4a594fce | 493 | const rc_dialog_control *c; |
252b5132 RH |
494 | |
495 | if (dialog->ex != NULL) | |
496 | return 1; | |
497 | ||
498 | for (c = dialog->controls; c != NULL; c = c->next) | |
499 | if (c->data != NULL || c->help != 0) | |
500 | return 1; | |
501 | ||
502 | return 0; | |
503 | } | |
504 | ||
505 | /* Return whether MENUITEMS are a MENU or a MENUEX. */ | |
506 | ||
507 | int | |
4a594fce | 508 | extended_menu (const rc_menu *menu) |
252b5132 RH |
509 | { |
510 | return extended_menuitems (menu->items); | |
511 | } | |
512 | ||
513 | static int | |
4a594fce | 514 | extended_menuitems (const rc_menuitem *menuitems) |
252b5132 | 515 | { |
4a594fce | 516 | const rc_menuitem *mi; |
252b5132 RH |
517 | |
518 | for (mi = menuitems; mi != NULL; mi = mi->next) | |
519 | { | |
520 | if (mi->help != 0 || mi->state != 0) | |
521 | return 1; | |
522 | if (mi->popup != NULL && mi->id != 0) | |
523 | return 1; | |
524 | if ((mi->type | |
525 | & ~ (MENUITEM_CHECKED | |
526 | | MENUITEM_GRAYED | |
527 | | MENUITEM_HELP | |
528 | | MENUITEM_INACTIVE | |
529 | | MENUITEM_MENUBARBREAK | |
d90171de NC |
530 | | MENUITEM_BITMAP |
531 | | MENUITEM_OWNERDRAW | |
252b5132 RH |
532 | | MENUITEM_MENUBREAK)) |
533 | != 0) | |
534 | return 1; | |
535 | if (mi->popup != NULL) | |
536 | { | |
537 | if (extended_menuitems (mi->popup)) | |
538 | return 1; | |
539 | } | |
540 | } | |
541 | ||
542 | return 0; | |
543 | } | |
544 | \f | |
545 | /* Convert a string to a format type, or exit if it can't be done. */ | |
546 | ||
547 | static enum res_format | |
2da42df6 | 548 | format_from_name (const char *name, int exit_on_error) |
252b5132 RH |
549 | { |
550 | const struct format_map *m; | |
551 | ||
552 | for (m = format_names; m->name != NULL; m++) | |
553 | if (strcasecmp (m->name, name) == 0) | |
554 | break; | |
555 | ||
85eb5110 | 556 | if (m->name == NULL && exit_on_error) |
252b5132 | 557 | { |
37cc8ec1 | 558 | non_fatal (_("unknown format type `%s'"), name); |
252b5132 RH |
559 | fprintf (stderr, _("%s: supported formats:"), program_name); |
560 | for (m = format_names; m->name != NULL; m++) | |
561 | fprintf (stderr, " %s", m->name); | |
562 | fprintf (stderr, "\n"); | |
563 | xexit (1); | |
564 | } | |
565 | ||
566 | return m->format; | |
567 | } | |
568 | ||
569 | /* Work out a format type given a file name. If INPUT is non-zero, | |
570 | it's OK to look at the file itself. */ | |
571 | ||
572 | static enum res_format | |
2da42df6 | 573 | format_from_filename (const char *filename, int input) |
252b5132 RH |
574 | { |
575 | const char *ext; | |
576 | FILE *e; | |
4a594fce | 577 | bfd_byte b1, b2, b3, b4, b5; |
252b5132 RH |
578 | int magic; |
579 | ||
580 | /* If we have an extension, see if we recognize it as implying a | |
581 | particular format. */ | |
582 | ext = strrchr (filename, '.'); | |
583 | if (ext != NULL) | |
584 | { | |
585 | const struct format_map *m; | |
586 | ||
587 | ++ext; | |
588 | for (m = format_fileexts; m->name != NULL; m++) | |
589 | if (strcasecmp (m->name, ext) == 0) | |
590 | return m->format; | |
591 | } | |
592 | ||
593 | /* If we don't recognize the name of an output file, assume it's a | |
594 | COFF file. */ | |
252b5132 RH |
595 | if (! input) |
596 | return RES_FORMAT_COFF; | |
597 | ||
598 | /* Read the first few bytes of the file to see if we can guess what | |
599 | it is. */ | |
252b5132 RH |
600 | e = fopen (filename, FOPEN_RB); |
601 | if (e == NULL) | |
602 | fatal ("%s: %s", filename, strerror (errno)); | |
603 | ||
604 | b1 = getc (e); | |
605 | b2 = getc (e); | |
606 | b3 = getc (e); | |
607 | b4 = getc (e); | |
608 | b5 = getc (e); | |
609 | ||
610 | fclose (e); | |
611 | ||
612 | /* A PE executable starts with 0x4d 0x5a. */ | |
613 | if (b1 == 0x4d && b2 == 0x5a) | |
614 | return RES_FORMAT_COFF; | |
615 | ||
616 | /* A COFF .o file starts with a COFF magic number. */ | |
617 | magic = (b2 << 8) | b1; | |
618 | switch (magic) | |
619 | { | |
620 | case 0x14c: /* i386 */ | |
621 | case 0x166: /* MIPS */ | |
622 | case 0x184: /* Alpha */ | |
623 | case 0x268: /* 68k */ | |
624 | case 0x1f0: /* PowerPC */ | |
625 | case 0x290: /* PA */ | |
626 | return RES_FORMAT_COFF; | |
627 | } | |
628 | ||
629 | /* A RES file starts with 0x0 0x0 0x0 0x0 0x20 0x0 0x0 0x0. */ | |
630 | if (b1 == 0 && b2 == 0 && b3 == 0 && b4 == 0 && b5 == 0x20) | |
631 | return RES_FORMAT_RES; | |
632 | ||
633 | /* If every character is printable or space, assume it's an RC file. */ | |
3882b010 L |
634 | if ((ISPRINT (b1) || ISSPACE (b1)) |
635 | && (ISPRINT (b2) || ISSPACE (b2)) | |
636 | && (ISPRINT (b3) || ISSPACE (b3)) | |
637 | && (ISPRINT (b4) || ISSPACE (b4)) | |
638 | && (ISPRINT (b5) || ISSPACE (b5))) | |
252b5132 RH |
639 | return RES_FORMAT_RC; |
640 | ||
641 | /* Otherwise, we give up. */ | |
d412a550 | 642 | fatal (_("can not determine type of file `%s'; use the -J option"), |
252b5132 RH |
643 | filename); |
644 | ||
645 | /* Return something to silence the compiler warning. */ | |
646 | return RES_FORMAT_UNKNOWN; | |
647 | } | |
648 | ||
649 | /* Print a usage message and exit. */ | |
650 | ||
651 | static void | |
2da42df6 | 652 | usage (FILE *stream, int status) |
252b5132 | 653 | { |
8b53311e | 654 | fprintf (stream, _("Usage: %s [option(s)] [input-file] [output-file]\n"), |
252b5132 | 655 | program_name); |
8b53311e NC |
656 | fprintf (stream, _(" The options are:\n\ |
657 | -i --input=<file> Name input file\n\ | |
658 | -o --output=<file> Name output file\n\ | |
85eb5110 | 659 | -J --input-format=<format> Specify input format\n\ |
8b53311e NC |
660 | -O --output-format=<format> Specify output format\n\ |
661 | -F --target=<target> Specify COFF target\n\ | |
662 | --preprocessor=<program> Program to use to preprocess rc file\n\ | |
ec25acb3 | 663 | --preprocessor-arg=<arg> Additional preprocessor argument\n\ |
85eb5110 | 664 | -I --include-dir=<dir> Include directory when preprocessing rc file\n\ |
8b53311e | 665 | -D --define <sym>[=<val>] Define SYM when preprocessing rc file\n\ |
29b058f1 | 666 | -U --undefine <sym> Undefine SYM when preprocessing rc file\n\ |
8b53311e | 667 | -v --verbose Verbose - tells you what it's doing\n\ |
d856f2dd | 668 | -c --codepage=<codepage> Specify default codepage\n\ |
85eb5110 | 669 | -l --language=<val> Set language when reading rc file\n\ |
8b53311e NC |
670 | --use-temp-file Use a temporary file instead of popen to read\n\ |
671 | the preprocessor output\n\ | |
672 | --no-use-temp-file Use popen (default)\n")); | |
252b5132 RH |
673 | #ifdef YYDEBUG |
674 | fprintf (stream, _("\ | |
8b53311e | 675 | --yydebug Turn on parser debugging\n")); |
252b5132 RH |
676 | #endif |
677 | fprintf (stream, _("\ | |
3126d709 | 678 | -r Ignored for compatibility with rc\n\ |
07012eee | 679 | @<file> Read options from <file>\n\ |
8b53311e NC |
680 | -h --help Print this help message\n\ |
681 | -V --version Print version information\n")); | |
252b5132 RH |
682 | fprintf (stream, _("\ |
683 | FORMAT is one of rc, res, or coff, and is deduced from the file name\n\ | |
684 | extension if not specified. A single file name is an input file.\n\ | |
685 | No input-file is stdin, default rc. No output-file is stdout, default rc.\n")); | |
8b53311e | 686 | |
252b5132 | 687 | list_supported_targets (program_name, stream); |
8b53311e | 688 | |
92f01d61 | 689 | if (REPORT_BUGS_TO[0] && status == 0) |
8ad3436c | 690 | fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO); |
8b53311e | 691 | |
252b5132 RH |
692 | exit (status); |
693 | } | |
694 | ||
8b53311e NC |
695 | /* Quote characters that will confuse the shell when we run the preprocessor. */ |
696 | ||
697 | static const char * | |
2da42df6 | 698 | quot (const char *string) |
09cda596 DD |
699 | { |
700 | static char *buf = 0; | |
701 | static int buflen = 0; | |
702 | int slen = strlen (string); | |
703 | const char *src; | |
704 | char *dest; | |
705 | ||
4a594fce | 706 | if ((buflen < slen * 2 + 2) || ! buf) |
09cda596 DD |
707 | { |
708 | buflen = slen * 2 + 2; | |
9db70fc3 | 709 | free (buf); |
09cda596 DD |
710 | buf = (char *) xmalloc (buflen); |
711 | } | |
712 | ||
713 | for (src=string, dest=buf; *src; src++, dest++) | |
714 | { | |
715 | if (*src == '(' || *src == ')' || *src == ' ') | |
716 | *dest++ = '\\'; | |
717 | *dest = *src; | |
718 | } | |
719 | *dest = 0; | |
720 | return buf; | |
721 | } | |
722 | ||
32df8966 NC |
723 | /* Long options. */ |
724 | ||
83bcb379 NC |
725 | enum option_values |
726 | { | |
727 | /* 150 isn't special; it's just an arbitrary non-ASCII char value. */ | |
728 | OPTION_PREPROCESSOR = 150, | |
729 | OPTION_USE_TEMP_FILE, | |
730 | OPTION_NO_USE_TEMP_FILE, | |
731 | OPTION_YYDEBUG, | |
ec25acb3 NC |
732 | OPTION_INCLUDE_DIR, |
733 | OPTION_PREPROCESSOR_ARG | |
83bcb379 | 734 | }; |
32df8966 NC |
735 | |
736 | static const struct option long_options[] = | |
737 | { | |
738 | {"input", required_argument, 0, 'i'}, | |
739 | {"output", required_argument, 0, 'o'}, | |
740 | {"input-format", required_argument, 0, 'J'}, | |
741 | {"output-format", required_argument, 0, 'O'}, | |
742 | {"target", required_argument, 0, 'F'}, | |
743 | {"preprocessor", required_argument, 0, OPTION_PREPROCESSOR}, | |
ec25acb3 | 744 | {"preprocessor-arg", required_argument, 0, OPTION_PREPROCESSOR_ARG}, |
83bcb379 | 745 | {"include-dir", required_argument, 0, OPTION_INCLUDE_DIR}, |
32df8966 NC |
746 | {"define", required_argument, 0, 'D'}, |
747 | {"undefine", required_argument, 0, 'U'}, | |
748 | {"verbose", no_argument, 0, 'v'}, | |
d856f2dd | 749 | {"codepage", required_argument, 0, 'c'}, |
32df8966 NC |
750 | {"language", required_argument, 0, 'l'}, |
751 | {"use-temp-file", no_argument, 0, OPTION_USE_TEMP_FILE}, | |
752 | {"no-use-temp-file", no_argument, 0, OPTION_NO_USE_TEMP_FILE}, | |
753 | {"yydebug", no_argument, 0, OPTION_YYDEBUG}, | |
754 | {"version", no_argument, 0, 'V'}, | |
755 | {"help", no_argument, 0, 'h'}, | |
756 | {0, no_argument, 0, 0} | |
757 | }; | |
758 | ||
c6998d15 NC |
759 | void |
760 | windres_add_include_dir (const char *p) | |
761 | { | |
762 | struct include_dir *n, **pp; | |
763 | ||
c7f0a8e0 CD |
764 | /* Computing paths is often complicated and error prone. |
765 | The easiest way to check for mistakes is at the time | |
766 | we add them to include_dirs. */ | |
767 | assert (p != NULL); | |
768 | assert (*p != '\0'); | |
769 | ||
c6998d15 NC |
770 | n = xmalloc (sizeof *n); |
771 | n->next = NULL; | |
772 | n->dir = (char * ) p; | |
773 | ||
774 | for (pp = &include_dirs; *pp != NULL; pp = &(*pp)->next) | |
775 | ; | |
776 | *pp = n; | |
777 | } | |
778 | ||
f7d63484 | 779 | /* This keeps gcc happy when using -Wmissing-prototypes -Wstrict-prototypes. */ |
2da42df6 | 780 | int main (int, char **); |
f7d63484 | 781 | |
252b5132 RH |
782 | /* The main function. */ |
783 | ||
784 | int | |
2da42df6 | 785 | main (int argc, char **argv) |
252b5132 RH |
786 | { |
787 | int c; | |
788 | char *input_filename; | |
789 | char *output_filename; | |
790 | enum res_format input_format; | |
85eb5110 | 791 | enum res_format input_format_tmp; |
252b5132 RH |
792 | enum res_format output_format; |
793 | char *target; | |
794 | char *preprocessor; | |
795 | char *preprocargs; | |
09cda596 | 796 | const char *quotedarg; |
252b5132 | 797 | int language; |
4a594fce | 798 | rc_res_directory *resources; |
5a298d2d | 799 | int use_temp_file; |
252b5132 RH |
800 | |
801 | #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES) | |
802 | setlocale (LC_MESSAGES, ""); | |
3882b010 L |
803 | #endif |
804 | #if defined (HAVE_SETLOCALE) | |
805 | setlocale (LC_CTYPE, ""); | |
252b5132 RH |
806 | #endif |
807 | bindtextdomain (PACKAGE, LOCALEDIR); | |
808 | textdomain (PACKAGE); | |
809 | ||
810 | program_name = argv[0]; | |
811 | xmalloc_set_program_name (program_name); | |
86eafac0 | 812 | bfd_set_error_program_name (program_name); |
252b5132 | 813 | |
c843b1bb | 814 | expandargv (&argc, &argv); |
869b9d07 | 815 | |
bf2dd8d7 AM |
816 | if (bfd_init () != BFD_INIT_MAGIC) |
817 | fatal (_("fatal error: libbfd ABI mismatch")); | |
252b5132 RH |
818 | set_default_bfd_target (); |
819 | ||
820 | res_init (); | |
821 | ||
822 | input_filename = NULL; | |
823 | output_filename = NULL; | |
824 | input_format = RES_FORMAT_UNKNOWN; | |
825 | output_format = RES_FORMAT_UNKNOWN; | |
826 | target = NULL; | |
827 | preprocessor = NULL; | |
828 | preprocargs = NULL; | |
f7d63484 | 829 | language = 0x409; /* LANG_ENGLISH, SUBLANG_ENGLISH_US. */ |
5a298d2d | 830 | use_temp_file = 0; |
252b5132 | 831 | |
d856f2dd | 832 | while ((c = getopt_long (argc, argv, "c:f:i:l:o:I:J:O:F:D:U:rhHvV", long_options, |
252b5132 RH |
833 | (int *) 0)) != EOF) |
834 | { | |
835 | switch (c) | |
836 | { | |
d856f2dd NC |
837 | case 'c': |
838 | { | |
839 | rc_uint_type ncp; | |
840 | ||
841 | if (optarg[0] == '0' && (optarg[1] == 'x' || optarg[1] == 'X')) | |
842 | ncp = (rc_uint_type) strtol (optarg + 2, NULL, 16); | |
843 | else | |
844 | ncp = (rc_uint_type) strtol (optarg, NULL, 10); | |
845 | if (ncp == CP_UTF16 || ! unicode_is_valid_codepage (ncp)) | |
846 | fatal (_("invalid codepage specified.\n")); | |
847 | wind_default_codepage = wind_current_codepage = ncp; | |
848 | } | |
849 | break; | |
850 | ||
252b5132 RH |
851 | case 'i': |
852 | input_filename = optarg; | |
853 | break; | |
854 | ||
32df8966 | 855 | case 'f': |
50c2245b | 856 | /* For compatibility with rc we accept "-fo <name>" as being the |
32df8966 NC |
857 | equivalent of "-o <name>". We do not advertise this fact |
858 | though, as we do not want users to use non-GNU like command | |
859 | line switches. */ | |
860 | if (*optarg != 'o') | |
861 | fatal (_("invalid option -f\n")); | |
862 | optarg++; | |
863 | if (* optarg == 0) | |
864 | { | |
865 | if (optind == argc) | |
2da42df6 | 866 | fatal (_("No filename following the -fo option.\n")); |
32df8966 NC |
867 | optarg = argv [optind++]; |
868 | } | |
869 | /* Fall through. */ | |
870 | ||
252b5132 RH |
871 | case 'o': |
872 | output_filename = optarg; | |
873 | break; | |
874 | ||
85eb5110 NC |
875 | case 'J': |
876 | input_format = format_from_name (optarg, 1); | |
252b5132 RH |
877 | break; |
878 | ||
879 | case 'O': | |
85eb5110 | 880 | output_format = format_from_name (optarg, 1); |
252b5132 RH |
881 | break; |
882 | ||
883 | case 'F': | |
884 | target = optarg; | |
885 | break; | |
886 | ||
887 | case OPTION_PREPROCESSOR: | |
888 | preprocessor = optarg; | |
889 | break; | |
890 | ||
ec25acb3 NC |
891 | case OPTION_PREPROCESSOR_ARG: |
892 | if (preprocargs == NULL) | |
893 | { | |
894 | quotedarg = quot (optarg); | |
895 | preprocargs = xstrdup (quotedarg); | |
896 | } | |
897 | else | |
898 | { | |
899 | char *n; | |
900 | ||
901 | quotedarg = quot (optarg); | |
902 | n = xmalloc (strlen (preprocargs) + strlen (quotedarg) + 2); | |
903 | sprintf (n, "%s %s", preprocargs, quotedarg); | |
904 | free (preprocargs); | |
905 | preprocargs = n; | |
906 | } | |
907 | break; | |
908 | ||
09cda596 | 909 | case 'D': |
29b058f1 | 910 | case 'U': |
252b5132 RH |
911 | if (preprocargs == NULL) |
912 | { | |
09cda596 DD |
913 | quotedarg = quot (optarg); |
914 | preprocargs = xmalloc (strlen (quotedarg) + 3); | |
29b058f1 | 915 | sprintf (preprocargs, "-%c%s", c, quotedarg); |
252b5132 RH |
916 | } |
917 | else | |
918 | { | |
919 | char *n; | |
920 | ||
09cda596 DD |
921 | quotedarg = quot (optarg); |
922 | n = xmalloc (strlen (preprocargs) + strlen (quotedarg) + 4); | |
29b058f1 | 923 | sprintf (n, "%s -%c%s", preprocargs, c, quotedarg); |
252b5132 RH |
924 | free (preprocargs); |
925 | preprocargs = n; | |
926 | } | |
927 | break; | |
928 | ||
3126d709 | 929 | case 'r': |
29b058f1 | 930 | /* Ignored for compatibility with rc. */ |
3126d709 CF |
931 | break; |
932 | ||
751d21b5 DD |
933 | case 'v': |
934 | verbose ++; | |
935 | break; | |
936 | ||
85eb5110 NC |
937 | case 'I': |
938 | /* For backward compatibility, should be removed in the future. */ | |
939 | input_format_tmp = format_from_name (optarg, 0); | |
940 | if (input_format_tmp != RES_FORMAT_UNKNOWN) | |
941 | { | |
83bcb379 NC |
942 | struct stat statbuf; |
943 | char modebuf[11]; | |
3aade688 | 944 | |
83bcb379 NC |
945 | if (stat (optarg, & statbuf) == 0 |
946 | /* Coded this way to avoid importing knowledge of S_ISDIR into this file. */ | |
947 | && (mode_string (statbuf.st_mode, modebuf), modebuf[0] == 'd')) | |
948 | /* We have a -I option with a directory name that just happens | |
949 | to match a format name as well. eg: -I res Assume that the | |
950 | user knows what they are doing and do not complain. */ | |
951 | ; | |
952 | else | |
953 | { | |
954 | fprintf (stderr, | |
955 | _("Option -I is deprecated for setting the input format, please use -J instead.\n")); | |
956 | input_format = input_format_tmp; | |
957 | break; | |
958 | } | |
85eb5110 | 959 | } |
83bcb379 | 960 | /* Fall through. */ |
2da42df6 | 961 | |
83bcb379 | 962 | case OPTION_INCLUDE_DIR: |
252b5132 RH |
963 | if (preprocargs == NULL) |
964 | { | |
09cda596 DD |
965 | quotedarg = quot (optarg); |
966 | preprocargs = xmalloc (strlen (quotedarg) + 3); | |
967 | sprintf (preprocargs, "-I%s", quotedarg); | |
252b5132 RH |
968 | } |
969 | else | |
970 | { | |
971 | char *n; | |
972 | ||
09cda596 DD |
973 | quotedarg = quot (optarg); |
974 | n = xmalloc (strlen (preprocargs) + strlen (quotedarg) + 4); | |
975 | sprintf (n, "%s -I%s", preprocargs, quotedarg); | |
252b5132 RH |
976 | free (preprocargs); |
977 | preprocargs = n; | |
978 | } | |
979 | ||
c6998d15 | 980 | windres_add_include_dir (optarg); |
252b5132 RH |
981 | |
982 | break; | |
983 | ||
3077f5d8 | 984 | case 'l': |
252b5132 RH |
985 | language = strtol (optarg, (char **) NULL, 16); |
986 | break; | |
987 | ||
5a298d2d NC |
988 | case OPTION_USE_TEMP_FILE: |
989 | use_temp_file = 1; | |
990 | break; | |
991 | ||
992 | case OPTION_NO_USE_TEMP_FILE: | |
993 | use_temp_file = 0; | |
994 | break; | |
995 | ||
252b5132 RH |
996 | #ifdef YYDEBUG |
997 | case OPTION_YYDEBUG: | |
998 | yydebug = 1; | |
999 | break; | |
1000 | #endif | |
1001 | ||
8b53311e NC |
1002 | case 'h': |
1003 | case 'H': | |
252b5132 RH |
1004 | usage (stdout, 0); |
1005 | break; | |
1006 | ||
8b53311e | 1007 | case 'V': |
252b5132 RH |
1008 | print_version ("windres"); |
1009 | break; | |
1010 | ||
1011 | default: | |
1012 | usage (stderr, 1); | |
1013 | break; | |
1014 | } | |
1015 | } | |
1016 | ||
1017 | if (input_filename == NULL && optind < argc) | |
1018 | { | |
1019 | input_filename = argv[optind]; | |
1020 | ++optind; | |
1021 | } | |
1022 | ||
1023 | if (output_filename == NULL && optind < argc) | |
1024 | { | |
1025 | output_filename = argv[optind]; | |
1026 | ++optind; | |
1027 | } | |
1028 | ||
1029 | if (argc != optind) | |
1030 | usage (stderr, 1); | |
1031 | ||
1032 | if (input_format == RES_FORMAT_UNKNOWN) | |
1033 | { | |
1034 | if (input_filename == NULL) | |
1035 | input_format = RES_FORMAT_RC; | |
1036 | else | |
1037 | input_format = format_from_filename (input_filename, 1); | |
1038 | } | |
1039 | ||
1040 | if (output_format == RES_FORMAT_UNKNOWN) | |
1041 | { | |
1042 | if (output_filename == NULL) | |
1043 | output_format = RES_FORMAT_RC; | |
1044 | else | |
1045 | output_format = format_from_filename (output_filename, 0); | |
1046 | } | |
1047 | ||
cc643b88 | 1048 | set_endianness (NULL, target); |
4a594fce | 1049 | |
252b5132 | 1050 | /* Read the input file. */ |
252b5132 RH |
1051 | switch (input_format) |
1052 | { | |
1053 | default: | |
1054 | abort (); | |
1055 | case RES_FORMAT_RC: | |
1056 | resources = read_rc_file (input_filename, preprocessor, preprocargs, | |
5a298d2d | 1057 | language, use_temp_file); |
252b5132 RH |
1058 | break; |
1059 | case RES_FORMAT_RES: | |
1060 | resources = read_res_file (input_filename); | |
1061 | break; | |
1062 | case RES_FORMAT_COFF: | |
1063 | resources = read_coff_rsrc (input_filename, target); | |
1064 | break; | |
1065 | } | |
1066 | ||
1067 | if (resources == NULL) | |
1068 | fatal (_("no resources")); | |
1069 | ||
1070 | /* Sort the resources. This is required for COFF, convenient for | |
1071 | rc, and unimportant for res. */ | |
252b5132 RH |
1072 | resources = sort_resources (resources); |
1073 | ||
1074 | /* Write the output file. */ | |
252b5132 RH |
1075 | reswr_init (); |
1076 | ||
1077 | switch (output_format) | |
1078 | { | |
1079 | default: | |
1080 | abort (); | |
1081 | case RES_FORMAT_RC: | |
1082 | write_rc_file (output_filename, resources); | |
1083 | break; | |
1084 | case RES_FORMAT_RES: | |
1085 | write_res_file (output_filename, resources); | |
1086 | break; | |
1087 | case RES_FORMAT_COFF: | |
1088 | write_coff_file (output_filename, target, resources); | |
1089 | break; | |
1090 | } | |
1091 | ||
1092 | xexit (0); | |
1093 | return 0; | |
1094 | } | |
4a594fce | 1095 | |
d25576aa | 1096 | static void |
cc643b88 | 1097 | set_endianness (bfd *abfd, const char *target) |
4a594fce NC |
1098 | { |
1099 | const bfd_target *target_vec; | |
1100 | ||
1101 | def_target_arch = NULL; | |
c4d1af07 KT |
1102 | target_vec = bfd_get_target_info (target, abfd, &target_is_bigendian, NULL, |
1103 | &def_target_arch); | |
4a594fce | 1104 | if (! target_vec) |
cc643b88 | 1105 | fatal ("Can't detect target endianness and architecture."); |
c4d1af07 KT |
1106 | if (! def_target_arch) |
1107 | fatal ("Can't detect architecture."); | |
4a594fce NC |
1108 | } |
1109 | ||
1110 | bfd * | |
1111 | windres_open_as_binary (const char *filename, int rdmode) | |
1112 | { | |
1113 | bfd *abfd; | |
1114 | ||
1115 | abfd = (rdmode ? bfd_openr (filename, "binary") : bfd_openw (filename, "binary")); | |
1116 | if (! abfd) | |
1117 | fatal ("can't open `%s' for %s", filename, (rdmode ? "input" : "output")); | |
1118 | ||
1119 | if (rdmode && ! bfd_check_format (abfd, bfd_object)) | |
1120 | fatal ("can't open `%s' for input.", filename); | |
3aade688 | 1121 | |
4a594fce NC |
1122 | return abfd; |
1123 | } | |
1124 | ||
1125 | void | |
cc643b88 | 1126 | set_windres_bfd_endianness (windres_bfd *wrbfd, int is_bigendian) |
4a594fce NC |
1127 | { |
1128 | assert (!! wrbfd); | |
1129 | switch (WR_KIND(wrbfd)) | |
1130 | { | |
1131 | case WR_KIND_BFD_BIN_L: | |
1132 | if (is_bigendian) | |
1133 | WR_KIND(wrbfd) = WR_KIND_BFD_BIN_B; | |
1134 | break; | |
1135 | case WR_KIND_BFD_BIN_B: | |
1136 | if (! is_bigendian) | |
1137 | WR_KIND(wrbfd) = WR_KIND_BFD_BIN_L; | |
1138 | break; | |
1139 | default: | |
1140 | /* only binary bfd can be overriden. */ | |
1141 | abort (); | |
1142 | } | |
1143 | } | |
1144 | ||
1145 | void | |
1146 | set_windres_bfd (windres_bfd *wrbfd, bfd *abfd, asection *sec, rc_uint_type kind) | |
1147 | { | |
1148 | assert (!! wrbfd); | |
1149 | switch (kind) | |
1150 | { | |
1151 | case WR_KIND_TARGET: | |
1152 | abfd = NULL; | |
1153 | sec = NULL; | |
1154 | break; | |
1155 | case WR_KIND_BFD: | |
1156 | case WR_KIND_BFD_BIN_L: | |
1157 | case WR_KIND_BFD_BIN_B: | |
1158 | assert (!! abfd); | |
1159 | assert (!!sec); | |
1160 | break; | |
1161 | default: | |
1162 | abort (); | |
1163 | } | |
1164 | WR_KIND(wrbfd) = kind; | |
1165 | WR_BFD(wrbfd) = abfd; | |
1166 | WR_SECTION(wrbfd) = sec; | |
1167 | } | |
1168 | ||
1169 | void | |
d25576aa NC |
1170 | set_windres_bfd_content (windres_bfd *wrbfd, const void *data, rc_uint_type off, |
1171 | rc_uint_type length) | |
4a594fce NC |
1172 | { |
1173 | if (WR_KIND(wrbfd) != WR_KIND_TARGET) | |
1174 | { | |
1175 | if (! bfd_set_section_contents (WR_BFD(wrbfd), WR_SECTION(wrbfd), data, off, length)) | |
1176 | bfd_fatal ("bfd_set_section_contents"); | |
1177 | } | |
1178 | else | |
1179 | abort (); | |
1180 | } | |
1181 | ||
1182 | void | |
d25576aa NC |
1183 | get_windres_bfd_content (windres_bfd *wrbfd, void *data, rc_uint_type off, |
1184 | rc_uint_type length) | |
4a594fce NC |
1185 | { |
1186 | if (WR_KIND(wrbfd) != WR_KIND_TARGET) | |
1187 | { | |
1188 | if (! bfd_get_section_contents (WR_BFD(wrbfd), WR_SECTION(wrbfd), data, off, length)) | |
1189 | bfd_fatal ("bfd_get_section_contents"); | |
1190 | } | |
1191 | else | |
1192 | abort (); | |
1193 | } | |
1194 | ||
1195 | void | |
1196 | windres_put_8 (windres_bfd *wrbfd, void *p, rc_uint_type value) | |
1197 | { | |
1198 | switch (WR_KIND(wrbfd)) | |
1199 | { | |
1200 | case WR_KIND_TARGET: | |
1201 | target_put_8 (p, value); | |
1202 | break; | |
1203 | case WR_KIND_BFD: | |
1204 | case WR_KIND_BFD_BIN_L: | |
1205 | case WR_KIND_BFD_BIN_B: | |
1206 | bfd_put_8 (WR_BFD(wrbfd), value, p); | |
1207 | break; | |
1208 | default: | |
1209 | abort (); | |
1210 | } | |
1211 | } | |
1212 | ||
1213 | void | |
1214 | windres_put_16 (windres_bfd *wrbfd, void *data, rc_uint_type value) | |
1215 | { | |
1216 | switch (WR_KIND(wrbfd)) | |
1217 | { | |
1218 | case WR_KIND_TARGET: | |
1219 | target_put_16 (data, value); | |
1220 | break; | |
1221 | case WR_KIND_BFD: | |
1222 | case WR_KIND_BFD_BIN_B: | |
1223 | bfd_put_16 (WR_BFD(wrbfd), value, data); | |
1224 | break; | |
1225 | case WR_KIND_BFD_BIN_L: | |
1226 | bfd_putl16 (value, data); | |
1227 | break; | |
1228 | default: | |
1229 | abort (); | |
1230 | } | |
1231 | } | |
1232 | ||
1233 | void | |
1234 | windres_put_32 (windres_bfd *wrbfd, void *data, rc_uint_type value) | |
1235 | { | |
1236 | switch (WR_KIND(wrbfd)) | |
1237 | { | |
1238 | case WR_KIND_TARGET: | |
1239 | target_put_32 (data, value); | |
1240 | break; | |
1241 | case WR_KIND_BFD: | |
1242 | case WR_KIND_BFD_BIN_B: | |
1243 | bfd_put_32 (WR_BFD(wrbfd), value, data); | |
1244 | break; | |
1245 | case WR_KIND_BFD_BIN_L: | |
1246 | bfd_putl32 (value, data); | |
1247 | break; | |
1248 | default: | |
1249 | abort (); | |
1250 | } | |
1251 | } | |
1252 | ||
1253 | rc_uint_type | |
1254 | windres_get_8 (windres_bfd *wrbfd, const void *data, rc_uint_type length) | |
1255 | { | |
1256 | if (length < 1) | |
1257 | fatal ("windres_get_8: unexpected eob."); | |
1258 | switch (WR_KIND(wrbfd)) | |
1259 | { | |
1260 | case WR_KIND_TARGET: | |
1261 | return target_get_8 (data, length); | |
1262 | case WR_KIND_BFD: | |
1263 | case WR_KIND_BFD_BIN_B: | |
1264 | case WR_KIND_BFD_BIN_L: | |
1265 | return bfd_get_8 (WR_BFD(wrbfd), data); | |
1266 | default: | |
1267 | abort (); | |
1268 | } | |
1269 | return 0; | |
1270 | } | |
1271 | ||
1272 | rc_uint_type | |
1273 | windres_get_16 (windres_bfd *wrbfd, const void *data, rc_uint_type length) | |
1274 | { | |
1275 | if (length < 2) | |
1276 | fatal ("windres_get_16: unexpected eob."); | |
1277 | switch (WR_KIND(wrbfd)) | |
1278 | { | |
1279 | case WR_KIND_TARGET: | |
1280 | return target_get_16 (data, length); | |
1281 | case WR_KIND_BFD: | |
1282 | case WR_KIND_BFD_BIN_B: | |
1283 | return bfd_get_16 (WR_BFD(wrbfd), data); | |
1284 | case WR_KIND_BFD_BIN_L: | |
1285 | return bfd_getl16 (data); | |
1286 | default: | |
1287 | abort (); | |
1288 | } | |
1289 | return 0; | |
1290 | } | |
1291 | ||
1292 | rc_uint_type | |
1293 | windres_get_32 (windres_bfd *wrbfd, const void *data, rc_uint_type length) | |
1294 | { | |
1295 | if (length < 4) | |
1296 | fatal ("windres_get_32: unexpected eob."); | |
1297 | switch (WR_KIND(wrbfd)) | |
1298 | { | |
1299 | case WR_KIND_TARGET: | |
1300 | return target_get_32 (data, length); | |
1301 | case WR_KIND_BFD: | |
1302 | case WR_KIND_BFD_BIN_B: | |
1303 | return bfd_get_32 (WR_BFD(wrbfd), data); | |
1304 | case WR_KIND_BFD_BIN_L: | |
1305 | return bfd_getl32 (data); | |
1306 | default: | |
1307 | abort (); | |
1308 | } | |
1309 | return 0; | |
1310 | } | |
1311 | ||
1312 | static rc_uint_type | |
1313 | target_get_8 (const void *p, rc_uint_type length) | |
1314 | { | |
1315 | rc_uint_type ret; | |
3aade688 | 1316 | |
4a594fce NC |
1317 | if (length < 1) |
1318 | fatal ("Resource too small for getting 8-bit value."); | |
1319 | ||
1320 | ret = (rc_uint_type) *((const bfd_byte *) p); | |
1321 | return ret & 0xff; | |
1322 | } | |
1323 | ||
1324 | static rc_uint_type | |
1325 | target_get_16 (const void *p, rc_uint_type length) | |
1326 | { | |
1327 | if (length < 2) | |
1328 | fatal ("Resource too small for getting 16-bit value."); | |
3aade688 | 1329 | |
4a594fce NC |
1330 | if (target_is_bigendian) |
1331 | return bfd_getb16 (p); | |
1332 | else | |
1333 | return bfd_getl16 (p); | |
1334 | } | |
1335 | ||
1336 | static rc_uint_type | |
1337 | target_get_32 (const void *p, rc_uint_type length) | |
1338 | { | |
1339 | if (length < 4) | |
1340 | fatal ("Resource too small for getting 32-bit value."); | |
3aade688 | 1341 | |
4a594fce NC |
1342 | if (target_is_bigendian) |
1343 | return bfd_getb32 (p); | |
1344 | else | |
1345 | return bfd_getl32 (p); | |
1346 | } | |
1347 | ||
1348 | static void | |
1349 | target_put_8 (void *p, rc_uint_type value) | |
1350 | { | |
1351 | assert (!! p); | |
1352 | *((bfd_byte *) p)=(bfd_byte) value; | |
1353 | } | |
1354 | ||
1355 | static void | |
1356 | target_put_16 (void *p, rc_uint_type value) | |
1357 | { | |
1358 | assert (!! p); | |
3aade688 | 1359 | |
4a594fce NC |
1360 | if (target_is_bigendian) |
1361 | bfd_putb16 (value, p); | |
1362 | else | |
1363 | bfd_putl16 (value, p); | |
1364 | } | |
1365 | ||
1366 | static void | |
1367 | target_put_32 (void *p, rc_uint_type value) | |
1368 | { | |
1369 | assert (!! p); | |
3aade688 | 1370 | |
4a594fce NC |
1371 | if (target_is_bigendian) |
1372 | bfd_putb32 (value, p); | |
1373 | else | |
1374 | bfd_putl32 (value, p); | |
1375 | } | |
1376 | ||
1377 | static int isInComment = 0; | |
1378 | ||
1379 | int wr_printcomment (FILE *e, const char *fmt, ...) | |
1380 | { | |
1381 | va_list arg; | |
1382 | int r = 0; | |
1383 | ||
1384 | if (isInComment) | |
1385 | r += fprintf (e, "\n "); | |
1386 | else | |
1387 | fprintf (e, "/* "); | |
1388 | isInComment = 1; | |
1389 | if (fmt == NULL) | |
1390 | return r; | |
1391 | va_start (arg, fmt); | |
1392 | r += vfprintf (e, fmt, arg); | |
1393 | va_end (arg); | |
1394 | return r; | |
1395 | } | |
1396 | ||
1397 | int wr_print (FILE *e, const char *fmt, ...) | |
1398 | { | |
1399 | va_list arg; | |
1400 | int r = 0; | |
1401 | if (isInComment) | |
1402 | r += fprintf (e, ". */\n"); | |
1403 | isInComment = 0; | |
1404 | if (! fmt) | |
1405 | return r; | |
1406 | va_start (arg, fmt); | |
1407 | r += vfprintf (e, fmt, arg); | |
1408 | va_end (arg); | |
3aade688 | 1409 | return r; |
4a594fce | 1410 | } |