diff options
author | Ian Lance Taylor <ian@airs.com> | 1997-06-26 00:59:44 +0000 |
---|---|---|
committer | Ian Lance Taylor <ian@airs.com> | 1997-06-26 00:59:44 +0000 |
commit | 662cc41eafa15f94499988b0e1c9a9ffc0afc39e (patch) | |
tree | 3a343a28f84626f02a9c9e9d4db58a522a8194f6 /binutils | |
parent | 9fd0d551fc32d9ee5e96e1f5526fefaeb1ba9d3c (diff) | |
download | gdb-662cc41eafa15f94499988b0e1c9a9ffc0afc39e.zip gdb-662cc41eafa15f94499988b0e1c9a9ffc0afc39e.tar.gz gdb-662cc41eafa15f94499988b0e1c9a9ffc0afc39e.tar.bz2 |
* resbin.c: New file.
* rclex.l, rcparse.y, rescoff.c, resrc.c, windres.c, windres.h:
Numerous fixes and improvements.
* Makefile.in: Rebuild dependencies.
(CFILES): Add resbin.c.
(WINDRES_OBJS): Add resbin.o.
Diffstat (limited to 'binutils')
-rw-r--r-- | binutils/.Sanitize | 1 | ||||
-rw-r--r-- | binutils/ChangeLog | 9 | ||||
-rw-r--r-- | binutils/Makefile.in | 9 | ||||
-rw-r--r-- | binutils/rclex.l | 114 | ||||
-rw-r--r-- | binutils/rcparse.y | 148 | ||||
-rw-r--r-- | binutils/resbin.c | 2368 | ||||
-rw-r--r-- | binutils/rescoff.c | 523 | ||||
-rw-r--r-- | binutils/resrc.c | 261 | ||||
-rw-r--r-- | binutils/windres.c | 167 |
9 files changed, 3354 insertions, 246 deletions
diff --git a/binutils/.Sanitize b/binutils/.Sanitize index e66607a..c66a8ff 100644 --- a/binutils/.Sanitize +++ b/binutils/.Sanitize @@ -84,6 +84,7 @@ ranlib.1 ranlib.sh rclex.l rcparse.y +resbin.c rescoff.c resrc.c rdcoff.c diff --git a/binutils/ChangeLog b/binutils/ChangeLog index 3c8f28b..fab29fa 100644 --- a/binutils/ChangeLog +++ b/binutils/ChangeLog @@ -1,3 +1,12 @@ +Wed Jun 25 20:57:06 1997 Ian Lance Taylor <ian@cygnus.com> + + * resbin.c: New file. + * rclex.l, rcparse.y, rescoff.c, resrc.c, windres.c, windres.h: + Numerous fixes and improvements. + * Makefile.in: Rebuild dependencies. + (CFILES): Add resbin.c. + (WINDRES_OBJS): Add resbin.o. + Sun Jun 22 17:29:41 1997 Ian Lance Taylor <ian@cygnus.com> First stab at Windows resource compiler: diff --git a/binutils/Makefile.in b/binutils/Makefile.in index 3a0eb41..15e1c8d 100644 --- a/binutils/Makefile.in +++ b/binutils/Makefile.in @@ -138,7 +138,7 @@ CFILES = addr2line.c ar.c arsup.c bucomm.c coffdump.c coffgrok.c debug.c \ maybe-strip.c nlmconv.c nm.c not-ranlib.c not-strip.c \ objcopy.c objdump.c prdbg.c rdcoff.c rddbg.c size.c srconv.c \ stabs.c strings.c sysdump.c version.c wrstabs.c \ - windres.c resrc.c rescoff.c + windres.c resrc.c rescoff.c resbin.c GENERATED_CFILES = \ underscore.c arparse.c arlex.c sysroff.c sysinfo.c syslex.c \ @@ -409,7 +409,7 @@ nlmconv.o: nlmconv.c $(INCDIR)/coff/sym.h $(INCDIR)/coff/ecoff.h $(NLMCONV_PROG): nlmconv.o nlmheader.o $(ADDL_DEPS) $(HLDENV) $(CC) $(HLDFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ nlmconv.o nlmheader.o $(ADDL_LIBS) $(EXTRALIBS) -WINDRES_OBJS = windres.o resrc.o rescoff.o rcparse.o rclex.o +WINDRES_OBJS = windres.o resrc.o rescoff.o resbin.o rcparse.o rclex.o $(WINDRES_PROG): $(WINDRES_OBJS) $(ADDL_DEPS) $(HLDENV) $(CC) $(HLDFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $(WINDRES_OBJS) $(ADDL_LIBS) $(EXTRALIBS) @@ -762,7 +762,7 @@ wrstabs.o: wrstabs.c ../bfd/bfd.h $(INCDIR)/ansidecl.h \ $(INCDIR)/aout/stab.def windres.o: windres.c ../bfd/bfd.h $(INCDIR)/ansidecl.h \ $(INCDIR)/getopt.h bucomm.h config.h $(INCDIR)/fopen-same.h \ - $(INCDIR)/libiberty.h windres.h + $(INCDIR)/libiberty.h $(INCDIR)/obstack.h windres.h resrc.o: resrc.c ../bfd/bfd.h $(INCDIR)/ansidecl.h \ bucomm.h config.h $(INCDIR)/fopen-same.h $(INCDIR)/libiberty.h \ windres.h @@ -770,6 +770,9 @@ rescoff.o: rescoff.c ../bfd/bfd.h $(INCDIR)/ansidecl.h \ bucomm.h config.h $(INCDIR)/fopen-same.h $(INCDIR)/libiberty.h \ windres.h $(INCDIR)/coff/internal.h $(BFDDIR)/libcoff.h \ $(INCDIR)/bfdlink.h +resbin.o: resbin.c ../bfd/bfd.h $(INCDIR)/ansidecl.h \ + bucomm.h config.h $(INCDIR)/fopen-same.h $(INCDIR)/libiberty.h \ + windres.h underscore.o: underscore.c arparse.o: arparse.c ../bfd/bfd.h $(INCDIR)/ansidecl.h \ bucomm.h config.h $(INCDIR)/fopen-same.h arsup.h diff --git a/binutils/rclex.l b/binutils/rclex.l index eb36bf6..b92d9f3 100644 --- a/binutils/rclex.l +++ b/binutils/rclex.l @@ -32,8 +32,26 @@ #include <ctype.h> #include <assert.h> +/* Whether we are in rcdata mode, in which we returns the lengths of + strings. */ + +static int rcdata_mode; + +/* List of allocated strings. */ + +struct alloc_string +{ + struct alloc_string *next; + char *s; +}; + +static struct alloc_string *strings; + +/* Local functions. */ + static void cpp_line PARAMS ((const char *)); -static char *handle_quotes PARAMS ((const char *)); +static char *handle_quotes PARAMS ((const char *, unsigned long *)); +static char *get_string PARAMS ((int)); %} @@ -133,9 +151,12 @@ static char *handle_quotes PARAMS ((const char *)); return BLOCKVARFILEINFO; else { - yylval.s = (char *) xmalloc (send - s + 1); - strncpy (yylval.s, s, send - s); - yylval.s[send - s] = '\0'; + char *r; + + r = get_string (send - s + 1); + strncpy (r, s, send - s); + r[send - s] = '\0'; + yylval.s = r; return BLOCK; } } @@ -157,12 +178,29 @@ static char *handle_quotes PARAMS ((const char *)); } ("\""[^\"\n]*"\""[ \t]*)+ { - yylval.s = handle_quotes (yytext); - return QUOTEDSTRING; + char *s; + unsigned long length; + + s = handle_quotes (yytext, &length); + if (! rcdata_mode) + { + yylval.s = s; + return QUOTEDSTRING; + } + else + { + yylval.ss.length = length; + yylval.ss.s = s; + return SIZEDSTRING; + } } [A-Za-z][^ \t\r\n]* { - yylval.s = xstrdup (yytext); + char *s; + + s = get_string (strlen (yytext) + 1); + strcpy (s, yytext); + yylval.s = s; return STRING; } @@ -224,14 +262,15 @@ cpp_line (s) merged separated by whitespace are merged, as in C. */ static char * -handle_quotes (input) +handle_quotes (input, len) const char *input; + unsigned long *len; { char *ret, *s; const char *t; int ch; - ret = (char *) xmalloc (strlen (input) + 1); + ret = get_string (strlen (input) + 1); s = ret; t = input; @@ -316,5 +355,62 @@ handle_quotes (input) *s = '\0'; + *len = s - ret; + return ret; } + +/* Allocate a string of a given length. */ + +static char * +get_string (len) + int len; +{ + struct alloc_string *as; + + as = (struct alloc_string *) xmalloc (sizeof *as); + as->s = xmalloc (len); + + as->next = strings; + strings = as->next; + + return as->s; +} + +/* Discard all the strings we have allocated. The parser calls this + when it no longer needs them. */ + +void +rcparse_discard_strings () +{ + struct alloc_string *as; + + as = strings; + while (as != NULL) + { + struct alloc_string *n; + + free (as->s); + n = as->next; + free (as); + as = n; + } + + strings = NULL; +} + +/* Enter rcdata mode. */ + +void +rcparse_rcdata () +{ + rcdata_mode = 1; +} + +/* Go back to normal mode from rcdata mode. */ + +void +rcparse_normal () +{ + rcdata_mode = 0; +} diff --git a/binutils/rcparse.y b/binutils/rcparse.y index 56c6db8..9ea9cf5 100644 --- a/binutils/rcparse.y +++ b/binutils/rcparse.y @@ -27,9 +27,11 @@ #include "libiberty.h" #include "windres.h" -/* The current language. The default is U.S. English. */ +#include <ctype.h> -static unsigned short language = 0x409; +/* The current language. */ + +static unsigned short language; /* The resource information during a sub statement. */ @@ -60,7 +62,12 @@ static unsigned long class; struct accelerator *pacc; struct dialog_control *dialog_control; struct menuitem *menuitem; - struct rcdata_data *rcdata; + struct + { + struct rcdata_item *first; + struct rcdata_item *last; + } rcdata; + struct rcdata_item *rcdata_item; struct stringtable_data *stringtable; struct fixed_versioninfo *fixver; struct ver_info *verinfo; @@ -81,7 +88,12 @@ static unsigned long class; } i; unsigned long il; unsigned short is; - char *s; + const char *s; + struct + { + unsigned long length; + const char *s; + } ss; }; %token BEG END @@ -109,12 +121,14 @@ static unsigned long class; %token NOT %token <s> QUOTEDSTRING STRING %token <i> NUMBER +%token <ss> SIZEDSTRING %type <pacc> acc_entries %type <acc> acc_entry acc_event %type <dialog_control> control control_params %type <menuitem> menuitems menuitem menuexitems menuexitem -%type <rcdata> optrcdata_data rcdata_data opt_control_data +%type <rcdata> optrcdata_data optrcdata_data_int rcdata_data +%type <rcdata_item> opt_control_data %type <fixver> fixedverinfo %type <verinfo> verblocks %type <verstring> vervals @@ -139,20 +153,27 @@ static unsigned long class; input: /* empty */ - | input accelerator - | input bitmap - | input cursor - | input dialog - | input font - | input icon - | input language - | input menu - | input menuex - | input messagetable - | input rcdata - | input stringtable - | input user - | input versioninfo + | input newcmd accelerator + | input newcmd bitmap + | input newcmd cursor + | input newcmd dialog + | input newcmd font + | input newcmd icon + | input newcmd language + | input newcmd menu + | input newcmd menuex + | input newcmd messagetable + | input newcmd rcdata + | input newcmd stringtable + | input newcmd user + | input newcmd versioninfo + ; + +newcmd: + /* empty */ + { + rcparse_discard_strings (); + } ; /* Accelerator resources. */ @@ -173,7 +194,7 @@ acc_entries: { struct accelerator *a; - a = (struct accelerator *) xmalloc (sizeof *a); + a = (struct accelerator *) res_alloc (sizeof *a); *a = $2; if ($1 == NULL) $$ = a; @@ -206,7 +227,7 @@ acc_entry: acc_event: QUOTEDSTRING { - char *s = $1; + const char *s = $1; $$.id = 0; if (*s != '^') @@ -219,7 +240,6 @@ acc_event: $$.key = *s; if (s[1] != '\0') rcparse_warning ("accelerator should only be one character"); - free (s); } | posnumexpr { @@ -274,7 +294,6 @@ bitmap: id BITMAP memflags_move file_name { define_bitmap ($1, &$3, $4); - free ($4); } ; @@ -284,7 +303,6 @@ cursor: id CURSOR memflags_move_discard file_name { define_cursor ($1, &$3, $4); - free ($4); } ; @@ -322,7 +340,7 @@ dialog: dialog.exstyle = $4; dialog.font = NULL; dialog.ex = ((struct dialog_ex *) - xmalloc (sizeof (struct dialog_ex))); + res_alloc (sizeof (struct dialog_ex))); memset (dialog.ex, 0, sizeof (struct dialog_ex)); dialog.controls = NULL; sub_res_info = $3; @@ -343,7 +361,7 @@ dialog: dialog.exstyle = $4; dialog.font = NULL; dialog.ex = ((struct dialog_ex *) - xmalloc (sizeof (struct dialog_ex))); + res_alloc (sizeof (struct dialog_ex))); memset (dialog.ex, 0, sizeof (struct dialog_ex)); dialog.ex->help = $9; dialog.controls = NULL; @@ -370,7 +388,7 @@ styles: /* empty */ | styles CAPTION QUOTEDSTRING { - dialog.caption = $3; + unicode_from_ascii ((int *) NULL, &dialog.caption, $3); } | styles CLASS id { @@ -389,12 +407,12 @@ styles: | styles FONT numexpr ',' QUOTEDSTRING { dialog.pointsize = $3; - dialog.font = $5; + unicode_from_ascii ((int *) NULL, &dialog.font, $5); } | styles FONT numexpr ',' QUOTEDSTRING cnumexpr cnumexpr { dialog.pointsize = $3; - dialog.font = $5; + unicode_from_ascii ((int *) NULL, &dialog.font, $5); if (dialog.ex == NULL) rcparse_warning ("extended FONT requires DIALOGEX"); else @@ -766,7 +784,7 @@ opt_control_data: } | BEG optrcdata_data END { - $$ = $2; + $$ = $2.first; } ; @@ -796,7 +814,6 @@ font: id FONT memflags_move_discard file_name { define_font ($1, &$3, $4); - free ($4); } ; @@ -806,7 +823,6 @@ icon: id ICON memflags_move_discard file_name { define_icon ($1, &$3, $4); - free ($4); } ; @@ -975,7 +991,6 @@ messagetable: id MESSAGETABLE memflags_move file_name { define_messagetable ($1, &$3, $4); - free ($4); } ; @@ -984,14 +999,29 @@ messagetable: rcdata: id RCDATA suboptions BEG optrcdata_data END { - define_rcdata ($1, &$3, $5); + define_rcdata ($1, &$3, $5.first); } ; +/* We use a different lexing algorithm, because rcdata strings may + contain embedded null bytes, and we need to know the length to use. */ + optrcdata_data: + { + rcparse_rcdata (); + } + optrcdata_data_int + { + rcparse_normal (); + $$ = $2; + } + ; + +optrcdata_data_int: /* empty */ { - $$ = NULL; + $$.first = NULL; + $$.last = NULL; } | rcdata_data { @@ -1000,21 +1030,39 @@ optrcdata_data: ; rcdata_data: - QUOTEDSTRING + SIZEDSTRING { - $$ = append_rcdata_string (NULL, $1); + struct rcdata_item *ri; + + ri = define_rcdata_string ($1.s, $1.length); + $$.first = ri; + $$.last = ri; } | sizednumexpr { - $$ = append_rcdata_number (NULL, $1.val, $1.dword); + struct rcdata_item *ri; + + ri = define_rcdata_number ($1.val, $1.dword); + $$.first = ri; + $$.last = ri; } - | rcdata_data ',' QUOTEDSTRING + | rcdata_data ',' SIZEDSTRING { - $$ = append_rcdata_string ($1, $3); + struct rcdata_item *ri; + + ri = define_rcdata_string ($3.s, $3.length); + $$.first = $1.first; + $1.last->next = ri; + $$.last = ri; } | rcdata_data ',' sizednumexpr { - $$ = append_rcdata_number ($1, $3.val, $3.dword); + struct rcdata_item *ri; + + ri = define_rcdata_number ($3.val, $3.dword); + $$.first = $1.first; + $1.last->next = ri; + $$.last = ri; } ; @@ -1042,14 +1090,13 @@ string_data: file_name case to keep the parser happy. */ user: - id id suboptions BEG rcdata_data END + id id suboptions BEG optrcdata_data END { - define_user_data ($1, $2, &$3, $5); + define_user_data ($1, $2, &$3, $5.first); } | id id suboptions file_name { define_user_file ($1, $2, &$3, $4); - free ($4); } ; @@ -1066,7 +1113,7 @@ fixedverinfo: /* empty */ { $$ = ((struct fixed_versioninfo *) - xmalloc (sizeof (struct fixed_versioninfo))); + res_alloc (sizeof (struct fixed_versioninfo))); memset ($$, 0, sizeof (struct fixed_versioninfo)); } | fixedverinfo FILEVERSION numexpr cnumexpr cnumexpr cnumexpr @@ -1161,8 +1208,15 @@ id: } | STRING { - res_string_to_id (&$$, $1); - free ($1); + char *copy, *s; + + /* It seems that resource ID's are forced to upper case. */ + copy = xstrdup ($1); + for (s = copy; *s != '\0'; s++) + if (islower (*s)) + *s = toupper (*s); + res_string_to_id (&$$, copy); + free (copy); } ; diff --git a/binutils/resbin.c b/binutils/resbin.c new file mode 100644 index 0000000..47d102c --- /dev/null +++ b/binutils/resbin.c @@ -0,0 +1,2368 @@ +/* resbin.c -- manipulate the Windows binary resource format. + Copyright 1997 Free Software Foundation, Inc. + Written by Ian Lance Taylor, Cygnus Support. + + This file is part of GNU Binutils. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + 02111-1307, USA. */ + +/* This file contains functions to convert between the binary resource + format and the internal structures that we want to use. The same + binary resource format is used in both res and COFF files. */ + +#include "bfd.h" +#include "bucomm.h" +#include "libiberty.h" +#include "windres.h" + +/* Macros to swap in values. */ + +#define get_16(be, s) ((be) ? bfd_getb16 (s) : bfd_getl16 (s)) +#define get_32(be, s) ((be) ? bfd_getb32 (s) : bfd_getl32 (s)) + +/* Local functions. */ + +static void toosmall PARAMS ((const char *)); +static unichar *get_unicode + PARAMS ((const unsigned char *, unsigned long, int, int *)); +static int get_resid + PARAMS ((struct res_id *, const unsigned char *, unsigned long, int)); +static struct res_resource *bin_to_res_generic + PARAMS ((enum res_type, const unsigned char *, unsigned long)); +static struct res_resource *bin_to_res_cursor + PARAMS ((const unsigned char *, unsigned long, int)); +static struct res_resource *bin_to_res_menu + PARAMS ((const unsigned char *, unsigned long, int)); +static struct menuitem *bin_to_res_menuitems + PARAMS ((const unsigned char *, unsigned long, int, int *)); +static struct menuitem *bin_to_res_menuexitems + PARAMS ((const unsigned char *, unsigned long, int, int *)); +static struct res_resource *bin_to_res_dialog + PARAMS ((const unsigned char *, unsigned long, int)); +static struct res_resource *bin_to_res_string + PARAMS ((const unsigned char *, unsigned long, int)); +static struct res_resource *bin_to_res_fontdir + PARAMS ((const unsigned char *, unsigned long, int)); +static struct res_resource *bin_to_res_accelerators + PARAMS ((const unsigned char *, unsigned long, int)); +static struct res_resource *bin_to_res_rcdata + PARAMS ((const unsigned char *, unsigned long, int)); +static struct res_resource *bin_to_res_group_cursor + PARAMS ((const unsigned char *, unsigned long, int)); +static struct res_resource *bin_to_res_group_icon + PARAMS ((const unsigned char *, unsigned long, int)); +static struct res_resource *bin_to_res_version + PARAMS ((const unsigned char *, unsigned long, int)); +static struct res_resource *bin_to_res_userdata + PARAMS ((const unsigned char *, unsigned long, int)); + +/* Given a resource type ID, a pointer to data, a length, return a + res_resource structure which represents that resource. The caller + is responsible for initializing the res_info and coff_info fields + of the returned structure. */ + +struct res_resource * +bin_to_res (type, data, length, big_endian) + struct res_id type; + const unsigned char *data; + unsigned long length; + int big_endian; +{ + if (type.named) + return bin_to_res_userdata (data, length, big_endian); + else + { + switch (type.u.id) + { + default: + return bin_to_res_userdata (data, length, big_endian); + case RT_CURSOR: + return bin_to_res_cursor (data, length, big_endian); + case RT_BITMAP: + return bin_to_res_generic (RES_TYPE_BITMAP, data, length); + case RT_ICON: + return bin_to_res_generic (RES_TYPE_ICON, data, length); + case RT_MENU: + return bin_to_res_menu (data, length, big_endian); + case RT_DIALOG: + return bin_to_res_dialog (data, length, big_endian); + case RT_STRING: + return bin_to_res_string (data, length, big_endian); + case RT_FONTDIR: + return bin_to_res_fontdir (data, length, big_endian); + case RT_FONT: + return bin_to_res_generic (RES_TYPE_FONT, data, length); + case RT_ACCELERATORS: + return bin_to_res_accelerators (data, length, big_endian); + case RT_RCDATA: + return bin_to_res_rcdata (data, length, big_endian); + case RT_MESSAGETABLE: + return bin_to_res_generic (RES_TYPE_MESSAGETABLE, data, length); + case RT_GROUP_CURSOR: + return bin_to_res_group_cursor (data, length, big_endian); + case RT_GROUP_ICON: + return bin_to_res_group_icon (data, length, big_endian); + case RT_VERSION: + return bin_to_res_version (data, length, big_endian); + } + } +} + +/* Give an error if the binary data is too small. */ + +static void +toosmall (msg) + const char *msg; +{ + fatal ("%s: not enough binary data", msg); +} + +/* Swap in a NULL terminated unicode string. */ + +static unichar * +get_unicode (data, length, big_endian, retlen) + const unsigned char *data; + unsigned long length; + int big_endian; + int *retlen; +{ + int c, i; + unichar *ret; + + c = 0; + while (1) + { + if (length < c * 2 + 2) + toosmall ("null terminated unicode string"); + if (get_16 (big_endian, data + c * 2) == 0) + break; + ++c; + } + + ret = (unichar *) res_alloc ((c + 1) * sizeof (unichar)); + + for (i = 0; i < c; i++) + ret[i] = get_16 (big_endian, data + i * 2); + ret[i] = 0; + + if (retlen != NULL) + *retlen = c; + + return ret; +} + +/* Get a resource identifier. This returns the number of bytes used. */ + +static int +get_resid (id, data, length, big_endian) + struct res_id *id; + const unsigned char *data; + unsigned long length; + int big_endian; +{ + int first; + + if (length < 2) + toosmall ("resource ID"); + + first = get_16 (big_endian, data); + if (first == 0xffff) + { + if (length < 4) + toosmall ("resource ID"); + id->named = 0; + id->u.id = get_16 (big_endian, data + 2); + return 4; + } + else + { + id->named = 1; + id->u.n.name = get_unicode (data, length, big_endian, &id->u.n.length); + return id->u.n.length * 2 + 2; + } +} + +/* Convert a resource which just stores uninterpreted data from + binary. */ + +struct res_resource * +bin_to_res_generic (type, data, length) + enum res_type type; + const unsigned char *data; + unsigned long length; +{ + struct res_resource *r; + + r = (struct res_resource *) res_alloc (sizeof *r); + r->type = type; + r->u.data.data = data; + r->u.data.length = length; + + return r; +} + +/* Convert a cursor resource from binary. */ + +struct res_resource * +bin_to_res_cursor (data, length, big_endian) + const unsigned char *data; + unsigned long length; + int big_endian; +{ + struct cursor *c; + struct res_resource *r; + + if (length < 4) + toosmall ("cursor"); + + c = (struct cursor *) res_alloc (sizeof *c); + c->xhotspot = get_16 (big_endian, data); + c->yhotspot = get_16 (big_endian, data + 2); + c->length = length - 4; + c->data = data + 4; + + r = (struct res_resource *) res_alloc (sizeof *r); + r->type = RES_TYPE_CURSOR; + r->u.cursor = c; + + return r; +} + +/* Convert a menu resource from binary. */ + +struct res_resource * +bin_to_res_menu (data, length, big_endian) + const unsigned char *data; + unsigned long length; + int big_endian; +{ + struct res_resource *r; + struct menu *m; + int version, read; + + r = (struct res_resource *) res_alloc (sizeof *r); + r->type = RES_TYPE_MENU; + + m = (struct menu *) res_alloc (sizeof *m); + r->u.menu = m; + + if (length < 2) + toosmall ("menu header"); + + version = get_16 (big_endian, data); + + if (version == 0) + { + if (length < 4) + toosmall ("menu header"); + m->help = 0; + m->items = bin_to_res_menuitems (data + 4, length - 4, big_endian, + &read); + } + else if (version == 1) + { + int offset; + + if (length < 8) + toosmall ("menuex header"); + m->help = get_32 (big_endian, data + 4); + offset = get_16 (big_endian, data + 2); + if (offset + 4 >= length) + toosmall ("menuex offset"); + m->items = bin_to_res_menuexitems (data + 4 + offset, + length - (4 + offset), + big_endian, + &read); + } + else + fatal ("unsupported menu version %d", version); + + return r; +} + +/* Convert menu items from binary. */ + +static struct menuitem * +bin_to_res_menuitems (data, length, big_endian, read) + const unsigned char *data; + unsigned long length; + int big_endian; + int *read; +{ + struct menuitem *first, **pp; + + first = NULL; + pp = &first; + + *read = 0; + + while (length > 0) + { + int flags, stroff, slen, itemlen; + struct menuitem *mi; + + if (length < 4) + toosmall ("menuitem header"); + + mi = (struct menuitem *) res_alloc (sizeof *mi); + mi->state = 0; + mi->help = 0; + + flags = get_16 (big_endian, data); + mi->type = flags; + + if ((flags & MENUITEM_POPUP) == 0) + stroff = 4; + else + stroff = 2; + + if (length < stroff + 2) + toosmall ("menuitem header"); + + if (get_16 (big_endian, data + stroff) == 0) + { + slen = 0; + mi->text = NULL; + } + else + mi->text = get_unicode (data + stroff, length - stroff, big_endian, + &slen); + + itemlen = stroff + slen * 2 + 2; + + if ((flags & MENUITEM_POPUP) == 0) + { + mi->popup = NULL; + mi->id = get_16 (big_endian, data + 2); + } + else + { + int subread; + + mi->id = 0; + mi->popup = bin_to_res_menuitems (data + itemlen, length - itemlen, + big_endian, &subread); + itemlen += subread; + } + + mi->next = NULL; + *pp = mi; + pp = &mi->next; + + data += itemlen; + length -= itemlen; + *read += itemlen; + + if ((flags & MENUITEM_ENDMENU) != 0) + return first; + } + + return first; +} + +/* Convert menuex items from binary. */ + +static struct menuitem * +bin_to_res_menuexitems (data, length, big_endian, read) + const unsigned char *data; + unsigned long length; + int big_endian; + int *read; +{ + struct menuitem *first, **pp; + + first = NULL; + pp = &first; + + *read = 0; + + while (length > 0) + { + int flags, slen, itemlen; + struct menuitem *mi; + + if (length < 14) + toosmall ("menuitem header"); + + mi = (struct menuitem *) res_alloc (sizeof *mi); + mi->type = get_32 (big_endian, data); + mi->state = get_32 (big_endian, data + 4); + mi->id = get_16 (big_endian, data + 8); + + flags = get_16 (big_endian, data + 10); + + if (get_16 (big_endian, data + 12) == 0) + { + slen = 0; + mi->text = NULL; + } + else + mi->text = get_unicode (data + 12, length - 12, big_endian, &slen); + + itemlen = 12 + slen * 2 + 2; + itemlen = (itemlen + 3) &~ 3; + + if ((flags & 1) == 0) + { + mi->popup = NULL; + mi->help = 0; + } + else + { + int subread; + + if (length < itemlen + 4) + toosmall ("menuitem"); + mi->help = get_32 (big_endian, data + itemlen); + itemlen += 4; + + mi->popup = bin_to_res_menuexitems (data + itemlen, + length - itemlen, + big_endian, &subread); + itemlen += subread; + } + + mi->next = NULL; + *pp = mi; + pp = &mi->next; + + data += itemlen; + length -= itemlen; + *read += itemlen; + + if ((flags & 0x80) != 0) + return first; + } + + return first; +} + +/* Convert a dialog resource from binary. */ + +static struct res_resource * +bin_to_res_dialog (data, length, big_endian) + const unsigned char *data; + unsigned long length; + int big_endian; +{ + int version; + struct dialog *d; + int c, sublen, off, i; + struct dialog_control **pp; + struct res_resource *r; + + if (length < 18) + toosmall ("dialog header"); + + d = (struct dialog *) res_alloc (sizeof *d); + + version = get_16 (big_endian, data); + if (version != 0xffff) + { + d->ex = NULL; + d->style = get_32 (big_endian, data); + d->exstyle = get_32 (big_endian, data + 4); + off = 8; + } + else + { + int signature; + + signature = get_16 (big_endian, data + 2); + if (signature != 1) + fatal ("unexpected dialog signature %d", signature); + + d->ex = (struct dialog_ex *) res_alloc (sizeof (struct dialog_ex)); + d->ex->help = get_32 (big_endian, data + 4); + d->exstyle = get_32 (big_endian, data + 8); + d->style = get_32 (big_endian, data + 12); + off = 16; + } + + if (length < off + 10) + toosmall ("dialog header"); + + c = get_16 (big_endian, data + off); + d->x = get_16 (big_endian, data + off + 2); + d->y = get_16 (big_endian, data + off + 4); + d->width = get_16 (big_endian, data + off + 6); + d->height = get_16 (big_endian, data + off + 8); + + off += 10; + + sublen = get_resid (&d->menu, data + off, length - off, big_endian); + off += sublen; + + sublen = get_resid (&d->class, data + off, length - off, big_endian); + off += sublen; + + d->caption = get_unicode (data + off, length - off, big_endian, &sublen); + off += sublen * 2 + 2; + + if ((d->style & DS_SETFONT) == 0) + { + d->pointsize = 0; + d->font = NULL; + if (d->ex != NULL) + { + d->ex->weight = 0; + d->ex->italic = 0; + } + } + else + { + if (length < off + 2) + toosmall ("dialog font point size"); + + d->pointsize = get_16 (big_endian, data + off); + off += 2; + + if (d->ex != NULL) + { + if (length < off + 4) + toosmall ("dialogex font information"); + d->ex->weight = get_16 (big_endian, data + off); + d->ex->italic = get_16 (big_endian, data + off + 2); + off += 4; + } + + d->font = get_unicode (data + off, length - off, big_endian, &sublen); + off += sublen * 2 + 2; + } + + d->controls = NULL; + pp = &d->controls; + + for (i = 0; i < c; i++) + { + struct dialog_control *dc; + int datalen; + + off = (off + 3) &~ 3; + + dc = (struct dialog_control *) res_alloc (sizeof *dc); + + if (d->ex == NULL) + { + if (length < off + 8) + toosmall ("dialog control"); + + dc->style = get_32 (big_endian, data + off); + dc->exstyle = get_32 (big_endian, data + off + 4); + dc->help = 0; + off += 8; + } + else + { + if (length < off + 12) + toosmall ("dialogex control"); + dc->help = get_32 (big_endian, data + off); + dc->exstyle = get_32 (big_endian, data + off + 4); + dc->style = get_32 (big_endian, data + off + 18); + off += 12; + } + + if (length < off + 10) + toosmall ("dialog control"); + + dc->x = get_16 (big_endian, data + off); + dc->y = get_16 (big_endian, data + off + 2); + dc->width = get_16 (big_endian, data + off + 4); + dc->height = get_16 (big_endian, data + off + 6); + dc->id = get_16 (big_endian, data + off + 8); + + off += 10; + + sublen = get_resid (&dc->class, data + off, length - off, big_endian); + off += sublen; + + sublen = get_resid (&dc->text, data + off, length - off, big_endian); + off += sublen; + + if (length < off + 2) + toosmall ("dialog control end"); + + datalen = get_16 (big_endian, data + off); + off += 2; + + if (datalen == 0) + dc->data = NULL; + else + { + off = (off + 3) &~ 3; + + if (length < off + datalen) + toosmall ("dialog control data"); + + dc->data = ((struct rcdata_item *) + res_alloc (sizeof (struct rcdata_item))); + dc->data->next = NULL; + dc->data->type = RCDATA_BUFFER; + dc->data->u.buffer.length = datalen; + dc->data->u.buffer.data = data + off; + + off += datalen; + } + + dc->next = NULL; + *pp = dc; + pp = &dc->next; + } + + r = (struct res_resource *) res_alloc (sizeof *r); + r->type = RES_TYPE_DIALOG; + r->u.dialog = d; + + return r; +} + +/* Convert a stringtable resource from binary. */ + +static struct res_resource * +bin_to_res_string (data, length, big_endian) + const unsigned char *data; + unsigned long length; + int big_endian; +{ + struct stringtable *st; + int i; + struct res_resource *r; + + st = (struct stringtable *) res_alloc (sizeof *st); + + for (i = 0; i < 16; i++) + { + int slen; + + if (length < 2) + toosmall ("stringtable string length"); + slen = get_16 (big_endian, data); + st->strings[i].length = slen; + + if (slen > 0) + { + unichar *s; + int j; + + if (length < 2 + 2 * slen) + toosmall ("stringtable string"); + + s = (unichar *) res_alloc (slen * sizeof (unichar)); + st->strings[i].string = s; + + for (j = 0; j < slen; j++) + s[j] = get_16 (big_endian, data + 2 + j * 2); + } + + data += 2 + slen; + length -= 2 + slen; + } + + r = (struct res_resource *) res_alloc (sizeof *r); + r->type = RES_TYPE_STRINGTABLE; + r->u.stringtable = st; + + return r; +} + +/* Convert a fontdir resource from binary. */ + +static struct res_resource * +bin_to_res_fontdir (data, length, big_endian) + const unsigned char *data; + unsigned long length; + int big_endian; +{ + int c, i; + struct fontdir *first, **pp; + struct res_resource *r; + + if (length < 2) + toosmall ("fontdir header"); + + c = get_16 (big_endian, data); + + first = NULL; + pp = &first; + + for (i = 0; i < c; i++) + { + struct fontdir *fd; + int off; + + if (length < 56) + toosmall ("fontdir"); + + fd = (struct fontdir *) res_alloc (sizeof *fd); + fd->index = get_16 (big_endian, data); + + /* To work out the length of the fontdir data, we must get the + length of the device name and face name strings, even though + we don't store them in the fontdir structure. The + documentation says that these are NULL terminated char + strings, not Unicode strings. */ + + off = 56; + + while (off < length && data[off] != '\0') + ++off; + if (off >= length) + toosmall ("fontdir device name"); + ++off; + + while (off < length && data[off] != '\0') + ++off; + if (off >= length) + toosmall ("fontdir face name"); + ++off; + + fd->length = off; + fd->data = data; + + fd->next = NULL; + *pp = fd; + pp = &fd->next; + + /* The documentation does not indicate that any rounding is + required. */ + + data += off; + length -= off; + } + + r = (struct res_resource *) res_alloc (sizeof *r); + r->type = RES_TYPE_FONTDIR; + r->u.fontdir = first; + + return r; +} + +/* Convert an accelerators resource from binary. */ + +static struct res_resource * +bin_to_res_accelerators (data, length, big_endian) + const unsigned char *data; + unsigned long length; + int big_endian; +{ + struct accelerator *first, **pp; + struct res_resource *r; + + first = NULL; + pp = &first; + + while (1) + { + struct accelerator *a; + + if (length < 8) + toosmall ("accelerator"); + + a->flags = get_16 (big_endian, data); + a->key = get_16 (big_endian, data + 2); + a->id = get_16 (big_endian, data + 4); + + a->next = NULL; + *pp = a; + pp = &a->next; + + if ((a->flags & ACC_LAST) != 0) + break; + + data += 8; + length -= 8; + } + + r = (struct res_resource *) res_alloc (sizeof *r); + r->type = RES_TYPE_ACCELERATOR; + r->u.acc = first; + + return r; +} + +/* Convert an rcdata resource from binary. */ + +static struct res_resource * +bin_to_res_rcdata (data, length, big_endian) + const unsigned char *data; + unsigned long length; + int big_endian; +{ + struct rcdata_item *ri; + struct res_resource *r; + + ri = (struct rcdata_item *) res_alloc (sizeof *ri); + + ri->next = NULL; + ri->type = RCDATA_BUFFER; + ri->u.buffer.length = length; + ri->u.buffer.data = data; + + r = (struct res_resource *) res_alloc (sizeof *r); + r->type = RES_TYPE_RCDATA; + r->u.rcdata = ri; + + return r; +} + +/* Convert a group cursor resource from binary. */ + +static struct res_resource * +bin_to_res_group_cursor (data, length, big_endian) + const unsigned char *data; + unsigned long length; + int big_endian; +{ + int type, c, i; + struct group_cursor *first, **pp; + struct res_resource *r; + + if (length < 6) + toosmall ("group cursor header"); + + type = get_16 (big_endian, data + 2); + if (type != 2) + fatal ("unexpected group cursor type %d", type); + + c = get_16 (big_endian, data + 4); + + data += 6; + length -= 6; + + first = NULL; + pp = &first; + + for (i = 0; i < c; i++) + { + struct group_cursor *gc; + + if (length < 14) + toosmall ("group cursor"); + + gc = (struct group_cursor *) res_alloc (sizeof *gc); + + gc->width = get_16 (big_endian, data); + gc->height = get_16 (big_endian, data + 2); + gc->planes = get_16 (big_endian, data + 4); + gc->bits = get_16 (big_endian, data + 6); + gc->bytes = get_32 (big_endian, data + 8); + gc->index = get_16 (big_endian, data + 12); + + gc->next = NULL; + *pp = gc; + pp = &gc->next; + + data += 14; + length -= 14; + } + + r = (struct res_resource *) res_alloc (sizeof *r); + r->type = RES_TYPE_GROUP_CURSOR; + r->u.group_cursor = first; + + return r; +} + +/* Convert a group icon resource from binary. */ + +static struct res_resource * +bin_to_res_group_icon (data, length, big_endian) + const unsigned char *data; + unsigned long length; + int big_endian; +{ + int type, c, i; + struct group_icon *first, **pp; + struct res_resource *r; + + if (length < 6) + toosmall ("group icon header"); + + type = get_16 (big_endian, data + 2); + if (type != 1) + fatal ("unexpected group icon type %d", type); + + c = get_16 (big_endian, data + 4); + + data += 6; + length -= 6; + + first = NULL; + pp = &first; + + for (i = 0; i < c; i++) + { + struct group_icon *gi; + + if (length < 14) + toosmall ("group icon"); + + gi = (struct group_icon *) res_alloc (sizeof *gi); + + gi->width = data[0]; + gi->height = data[1]; + gi->colors = data[2]; + gi->planes = get_16 (big_endian, data + 4); + gi->bits = get_16 (big_endian, data + 6); + gi->bytes = get_32 (big_endian, data + 8); + gi->index = get_16 (big_endian, data + 12); + + gi->next = NULL; + *pp = gi; + pp = &gi->next; + + data += 14; + length -= 14; + } + + r = (struct res_resource *) res_alloc (sizeof *r); + r->type = RES_TYPE_GROUP_ICON; + r->u.group_icon = first; + + return r; +} + +/* Extract data from a version header. If KEY is not NULL, then the + key must be KEY; otherwise, the key is returned in *PKEY. This + sets *LEN to the total length, *VALLEN to the value length, *TYPE + to the type, and *OFF to the offset to the children. */ + +static void +get_version_header (data, length, big_endian, key, pkey, len, vallen, type, + off) + const unsigned char *data; + unsigned long length; + int big_endian; + const char *key; + unichar **pkey; + int *len; + int *vallen; + int *type; + int *off; +{ + if (length < 8) + toosmall (key); + + *len = get_16 (big_endian, data); + *vallen = get_16 (big_endian, data + 2); + *type = get_16 (big_endian, data + 4); + + *off = 6; + + length -= 6; + data += 6; + + if (key == NULL) + { + int sublen; + + *pkey = get_unicode (data, length, big_endian, &sublen); + *off += sublen * 2 + 2; + } + else + { + while (1) + { + if (length < 2) + toosmall (key); + if (get_16 (big_endian, data) != *key) + fatal ("unexpected version string"); + + *off += 2; + length -= 2; + data += 2; + + if (*key == '\0') + break; + + ++key; + } + } + + *off = (*off + 3) &~ 3; +} + +/* Convert a version resource from binary. */ + +static struct res_resource * +bin_to_res_version (data, length, big_endian) + const unsigned char *data; + unsigned long length; + int big_endian; +{ + int verlen, vallen, type, off; + struct fixed_versioninfo *fi; + struct ver_info *first, **pp; + struct versioninfo *v; + struct res_resource *r; + + get_version_header (data, length, big_endian, "VS_VERSION_INFO", + (unichar *) NULL, &verlen, &vallen, &type, &off); + + if (verlen != length) + fatal ("version length %d does not match resource length %lu", + verlen, length); + + if (type != 0) + fatal ("unexpected version type %d", type); + + data += off; + length -= off; + + if (vallen == 0) + fi = NULL; + else + { + unsigned long signature, fiv; + + if (vallen != 52) + fatal ("unexpected fixed version information length %d", vallen); + + if (length < 52) + toosmall ("fixed version info"); + + signature = get_32 (big_endian, data); + if (signature != 0xfeef04bd) + fatal ("unexpected fixed version signature %lu", signature); + + fiv = get_32 (big_endian, data + 4); + if (fiv != 0 && fiv != 0x10000) + fatal ("unexpected fixed version info version %lu", fiv); + + fi = (struct fixed_versioninfo *) res_alloc (sizeof *fi); + + fi->file_version_ms = get_32 (big_endian, data + 8); + fi->file_version_ls = get_32 (big_endian, data + 12); + fi->product_version_ms = get_32 (big_endian, data + 16); + fi->product_version_ls = get_32 (big_endian, data + 20); + fi->file_flags_mask = get_32 (big_endian, data + 24); + fi->file_flags = get_32 (big_endian, data + 28); + fi->file_os = get_32 (big_endian, data + 32); + fi->file_type = get_32 (big_endian, data + 36); + fi->file_subtype = get_32 (big_endian, data + 40); + fi->file_date_ms = get_32 (big_endian, data + 44); + fi->file_date_ls = get_32 (big_endian, data + 48); + + data += 52; + length -= 52; + } + + first = NULL; + pp = &first; + + while (length > 0) + { + struct ver_info *vi; + int ch; + + if (length < 8) + toosmall ("version var info"); + + vi = (struct ver_info *) res_alloc (sizeof *vi); + + ch = get_16 (big_endian, data + 6); + + if (ch == 'S') + { + struct ver_stringinfo **ppvs; + + vi->type = VERINFO_STRING; + + get_version_header (data, length, big_endian, "StringFileInfo", + (unichar *) NULL, &verlen, &vallen, &type, + &off); + + if (vallen != 0) + fatal ("unexpected stringfileinfo value length %d", vallen); + + data += off; + length -= off; + + get_version_header (data, length, big_endian, (const char *) NULL, + &vi->u.string.language, &verlen, &vallen, + &type, &off); + + if (vallen != 0) + fatal ("unexpected version stringtable value length %d", vallen); + + data += off; + length -= off; + verlen -= off; + + vi->u.string.strings = NULL; + ppvs = &vi->u.string.strings; + + /* It's convenient to round verlen to a 4 byte alignment, + since we round the subvariables in the loop. */ + verlen = (verlen + 3) &~ 3; + + while (verlen > 0) + { + struct ver_stringinfo *vs; + int subverlen, vslen, valoff; + + vs = (struct ver_stringinfo *) res_alloc (sizeof *vs); + + get_version_header (data, length, big_endian, + (const char *) NULL, &vs->key, &subverlen, + &vallen, &type, &off); + + subverlen = (subverlen + 3) &~ 3; + + data += off; + length -= off; + + vs->value = get_unicode (data, length, big_endian, &vslen); + valoff = vslen * 2 + 2; + valoff = (valoff + 3) &~ 3; + + if (off + valoff != subverlen) + fatal ("unexpected version string length %d != %d + %d", + subverlen, off, valoff); + + vs->next = NULL; + *ppvs = vs; + ppvs = &vs->next; + + data += valoff; + length -= valoff; + + if (verlen < subverlen) + fatal ("unexpected version string length %d < %d", + verlen, subverlen); + + verlen -= subverlen; + } + } + else if (ch == 'V') + { + struct ver_varinfo **ppvv; + + vi->type = VERINFO_VAR; + + get_version_header (data, length, big_endian, "VarFileInfo", + (unichar *) NULL, &verlen, &vallen, &type, + &off); + + if (vallen != 0) + fatal ("unexpected varfileinfo value length %d", vallen); + + data += off; + length -= off; + + get_version_header (data, length, big_endian, (const char *) NULL, + &vi->u.var.key, &verlen, &vallen, &type, &off); + + data += off; + length -= off; + + vi->u.var.var = NULL; + ppvv = &vi->u.var.var; + + while (vallen > 0) + { + struct ver_varinfo *vv; + + if (length < 4) + toosmall ("version varfileinfo"); + + vv = (struct ver_varinfo *) res_alloc (sizeof *vv); + + vv->language = get_16 (big_endian, data); + vv->charset = get_16 (big_endian, data + 2); + + vv->next = NULL; + *ppvv = vv; + ppvv = &vv->next; + + data += 4; + length -= 4; + + if (vallen < 4) + fatal ("unexpected version value length %d", vallen); + + vallen -= 4; + } + } + else + fatal ("unexpected version string"); + + vi->next = NULL; + *pp = vi; + pp = &vi->next; + } + + v = (struct versioninfo *) res_alloc (sizeof *v); + v->fixed = fi; + v->var = first; + + r = (struct res_resource *) res_alloc (sizeof *r); + r->type = RES_TYPE_VERSIONINFO; + r->u.versioninfo = v; + + return r; +} + +/* Convert an arbitrary user defined resource from binary. */ + +static struct res_resource * +bin_to_res_userdata (data, length, big_endian) + const unsigned char *data; + unsigned long length; + int big_endian; +{ + struct rcdata_item *ri; + struct res_resource *r; + + ri = (struct rcdata_item *) res_alloc (sizeof *ri); + + ri->next = NULL; + ri->type = RCDATA_BUFFER; + ri->u.buffer.length = length; + ri->u.buffer.data = data; + + r = (struct res_resource *) res_alloc (sizeof *r); + r->type = RES_TYPE_USERDATA; + r->u.rcdata = ri; + + return r; +} + +/* Macros to swap out values. */ + +#define put_16(be, v, s) ((be) ? bfd_putb16 ((v), (s)) : bfd_putl16 ((v), (s))) +#define put_32(be, v, s) ((be) ? bfd_putb32 ((v), (s)) : bfd_putl32 ((v), (s))) + +/* Local functions used to convert resources to binary format. */ + +static void dword_align_bin PARAMS ((struct bindata ***, unsigned long *)); +static struct bindata *resid_to_bin PARAMS ((struct res_id, int)); +static struct bindata *unicode_to_bin PARAMS ((const unichar *, int)); +static struct bindata *res_to_bin_accelerator + PARAMS ((const struct accelerator *, int)); +static struct bindata *res_to_bin_cursor + PARAMS ((const struct cursor *, int)); +static struct bindata *res_to_bin_group_cursor + PARAMS ((const struct group_cursor *, int)); +static struct bindata *res_to_bin_dialog + PARAMS ((const struct dialog *, int)); +static struct bindata *res_to_bin_fontdir + PARAMS ((const struct fontdir *, int)); +static struct bindata *res_to_bin_group_icon + PARAMS ((const struct group_icon *, int)); +static struct bindata *res_to_bin_menu + PARAMS ((const struct menu *, int)); +static struct bindata *res_to_bin_menuitems + PARAMS ((const struct menuitem *, int)); +static struct bindata *res_to_bin_menuexitems + PARAMS ((const struct menuitem *, int)); +static struct bindata *res_to_bin_rcdata + PARAMS ((const struct rcdata_item *, int)); +static struct bindata *res_to_bin_stringtable + PARAMS ((const struct stringtable *, int)); +static struct bindata *string_to_unicode_bin PARAMS ((const char *, int)); +static struct bindata *res_to_bin_versioninfo + PARAMS ((const struct versioninfo *, int)); +static struct bindata *res_to_bin_generic + PARAMS ((unsigned long, const unsigned char *)); + +/* Convert a resource to binary. */ + +struct bindata * +res_to_bin (res, big_endian) + const struct res_resource *res; + int big_endian; +{ + switch (res->type) + { + default: + abort (); + case RES_TYPE_BITMAP: + case RES_TYPE_FONT: + case RES_TYPE_ICON: + case RES_TYPE_MESSAGETABLE: + return res_to_bin_generic (res->u.data.length, res->u.data.data); + case RES_TYPE_ACCELERATOR: + return res_to_bin_accelerator (res->u.acc, big_endian); + case RES_TYPE_CURSOR: + return res_to_bin_cursor (res->u.cursor, big_endian); + case RES_TYPE_GROUP_CURSOR: + return res_to_bin_group_cursor (res->u.group_cursor, big_endian); + case RES_TYPE_DIALOG: + return res_to_bin_dialog (res->u.dialog, big_endian); + case RES_TYPE_FONTDIR: + return res_to_bin_fontdir (res->u.fontdir, big_endian); + case RES_TYPE_GROUP_ICON: + return res_to_bin_group_icon (res->u.group_icon, big_endian); + case RES_TYPE_MENU: + return res_to_bin_menu (res->u.menu, big_endian); + case RES_TYPE_RCDATA: + return res_to_bin_rcdata (res->u.rcdata, big_endian); + case RES_TYPE_STRINGTABLE: + return res_to_bin_stringtable (res->u.stringtable, big_endian); + case RES_TYPE_USERDATA: + return res_to_bin_rcdata (res->u.rcdata, big_endian); + case RES_TYPE_VERSIONINFO: + return res_to_bin_versioninfo (res->u.versioninfo, big_endian); + } +} + +/* Align to a 32 bit boundary. PPP points to the of a list of bindata + structures. LENGTH points to the length of the structures. If + necessary, this adds a new bindata to bring length up to a 32 bit + boundary. It updates *PPP and *LENGTH. */ + +static void +dword_align_bin (ppp, length) + struct bindata ***ppp; + unsigned long *length; +{ + int add; + struct bindata *d; + + if ((*length & 3) == 0) + return; + + add = 4 - (*length & 3); + + d = (struct bindata *) reswr_alloc (sizeof *d); + d->length = add; + d->data = (unsigned char *) reswr_alloc (add); + memset (d->data, 0, add); + + d->next = NULL; + **ppp = d; + *ppp = &(**ppp)->next; + + *length += add; +} + +/* Convert a resource ID to binary. This always returns exactly one + bindata structure. */ + +static struct bindata * +resid_to_bin (id, big_endian) + struct res_id id; + int big_endian; +{ + struct bindata *d; + + d = (struct bindata *) reswr_alloc (sizeof *d); + + if (! id.named) + { + d->length = 4; + d->data = (unsigned char *) reswr_alloc (4); + put_16 (big_endian, 0xffff, d->data); + put_16 (big_endian, id.u.id, d->data + 2); + } + else + { + int i; + + d->length = id.u.n.length * 2 + 2; + d->data = (unsigned char *) reswr_alloc (d->length); + for (i = 0; i < id.u.n.length; i++) + put_16 (big_endian, id.u.n.name[i], d->data + i * 2); + put_16 (big_endian, 0, d->data + i * 2); + } + + d->next = NULL; + + return d; +} + +/* Convert a null terminated unicode string to binary. This always + returns exactly one bindata structure. */ + +static struct bindata * +unicode_to_bin (str, big_endian) + const unichar *str; + int big_endian; +{ + int len; + struct bindata *d; + + len = 0; + if (str != NULL) + { + const unichar *s; + + for (s = str; *s != 0; s++) + ++len; + } + + d = (struct bindata *) reswr_alloc (sizeof *d); + d->length = len * 2 + 2; + d->data = (unsigned char *) reswr_alloc (d->length); + + if (str == NULL) + put_16 (big_endian, 0, d->data); + else + { + const unichar *s; + int i; + + for (s = str, i = 0; *s != 0; s++, i++) + put_16 (big_endian, *s, d->data + i * 2); + put_16 (big_endian, 0, d->data + i * 2); + } + + d->next = NULL; + + return d; +} + +/* Convert an accelerator resource to binary. */ + +static struct bindata * +res_to_bin_accelerator (accelerators, big_endian) + const struct accelerator *accelerators; + int big_endian; +{ + struct bindata *first, **pp; + const struct accelerator *a; + + first = NULL; + pp = &first; + + for (a = accelerators; a != NULL; a = a->next) + { + struct bindata *d; + + d = (struct bindata *) reswr_alloc (sizeof *d); + d->length = 8; + d->data = (unsigned char *) reswr_alloc (8); + + put_16 (big_endian, + a->flags | (a->next == NULL ? 0 : ACC_LAST), + d->data); + put_16 (big_endian, a->key, d->data + 2); + put_16 (big_endian, a->id, d->data + 4); + put_16 (big_endian, 0, d->data + 8); + + d->next = NULL; + *pp = d; + pp = &d->next; + } + + return first; +} + +/* Convert a cursor resource to binary. */ + +static struct bindata * +res_to_bin_cursor (c, big_endian) + const struct cursor *c; + int big_endian; +{ + struct bindata *d; + + d = (struct bindata *) reswr_alloc (sizeof *d); + d->length = 4; + d->data = (unsigned char *) reswr_alloc (4); + + put_16 (big_endian, c->xhotspot, d->data); + put_16 (big_endian, c->yhotspot, d->data + 2); + + d->next = (struct bindata *) reswr_alloc (sizeof *d); + d->next->length = c->length; + d->next->data = (unsigned char *) c->data; + d->next->next = NULL; + + return d; +} + +/* Convert a group cursor resource to binary. */ + +static struct bindata * +res_to_bin_group_cursor (group_cursors, big_endian) + const struct group_cursor *group_cursors; + int big_endian; +{ + struct bindata *first, **pp; + int c; + const struct group_cursor *gc; + + first = (struct bindata *) reswr_alloc (sizeof *first); + first->length = 6; + first->data = (unsigned char *) reswr_alloc (6); + + put_16 (big_endian, 0, first->data); + put_16 (big_endian, 2, first->data + 2); + + first->next = NULL; + pp = &first->next; + + c = 0; + for (gc = group_cursors; gc != NULL; gc = gc->next) + { + struct bindata *d; + + ++c; + + d = (struct bindata *) reswr_alloc (sizeof *d); + d->length = 14; + d->data = (unsigned char *) reswr_alloc (14); + + put_16 (big_endian, gc->width, d->data); + put_16 (big_endian, gc->height, d->data + 2); + put_16 (big_endian, gc->planes, d->data + 4); + put_16 (big_endian, gc->bits, d->data + 6); + put_32 (big_endian, gc->bytes, d->data + 8); + put_16 (big_endian, gc->index, d->data + 12); + + d->next = NULL; + *pp = d; + pp = &d->next; + } + + put_16 (big_endian, c, first->data + 4); + + return first; +} + +/* Convert a dialog resource to binary. */ + +static struct bindata * +res_to_bin_dialog (dialog, big_endian) + const struct dialog *dialog; + int big_endian; +{ + int dialogex; + struct bindata *first, **pp; + unsigned long length; + int off, c; + struct dialog_control *dc; + + dialogex = extended_dialog (dialog); + + first = (struct bindata *) reswr_alloc (sizeof *first); + first->length = dialogex ? 26 : 18; + first->data = (unsigned char *) reswr_alloc (first->length); + + length = first->length; + + if (! dialogex) + { + put_32 (big_endian, dialog->style, first->data); + put_32 (big_endian, dialog->style, first->data + 4); + off = 8; + } + else + { + put_16 (big_endian, 0xffff, first->data); + put_16 (big_endian, 1, first->data + 2); + if (dialog->ex == NULL) + put_32 (big_endian, 0, first->data + 4); + else + put_32 (big_endian, dialog->ex->help, first->data + 4); + put_32 (big_endian, dialog->exstyle, first->data + 8); + put_32 (big_endian, dialog->style, first->data + 12); + off = 16; + } + + put_16 (big_endian, dialog->x, first->data + off + 2); + put_16 (big_endian, dialog->y, first->data + off + 4); + put_16 (big_endian, dialog->width, first->data + off + 6); + put_16 (big_endian, dialog->height, first->data + off + 8); + + pp = &first->next; + + *pp = resid_to_bin (dialog->menu, big_endian); + length += (*pp)->length; + pp = &(*pp)->next; + + *pp = resid_to_bin (dialog->class, big_endian); + length += (*pp)->length; + pp = &(*pp)->next; + + *pp = unicode_to_bin (dialog->caption, big_endian); + length += (*pp)->length; + pp = &(*pp)->next; + + if ((dialog->style & DS_SETFONT) != 0) + { + struct bindata *d; + + d = (struct bindata *) reswr_alloc (sizeof *d); + d->length = dialogex ? 2 : 6; + d->data = (unsigned char *) reswr_alloc (d->length); + + length += d->length; + + put_16 (big_endian, dialog->pointsize, d->data); + + if (dialogex) + { + if (dialog->ex == NULL) + { + put_16 (big_endian, 0, d->data + 2); + put_16 (big_endian, 0, d->data + 4); + } + else + { + put_16 (big_endian, dialog->ex->weight, d->data + 2); + put_16 (big_endian, dialog->ex->italic, d->data + 4); + } + } + + *pp = d; + pp = &d->next; + + *pp = unicode_to_bin (dialog->font, big_endian); + length += (*pp)->length; + pp = &(*pp)->next; + } + + c = 0; + for (dc = dialog->controls; dc != NULL; dc = dc->next) + { + unsigned long length; + struct bindata *d; + int dcoff; + + ++c; + + dword_align_bin (&pp, &length); + + d = (struct bindata *) reswr_alloc (sizeof *d); + d->length = dialogex ? 22 : 18; + d->data = (unsigned char *) reswr_alloc (d->length); + + length += d->length; + + if (! dialogex) + { + put_32 (big_endian, dc->style, d->data); + put_32 (big_endian, dc->exstyle, d->data + 4); + dcoff = 8; + } + else + { + put_32 (big_endian, dc->help, d->data); + put_32 (big_endian, dc->exstyle, d->data + 4); + put_32 (big_endian, dc->style, d->data + 8); + dcoff = 12; + } + + put_16 (big_endian, dc->x, d->data + dcoff); + put_16 (big_endian, dc->y, d->data + dcoff + 2); + put_16 (big_endian, dc->width, d->data + dcoff + 4); + put_16 (big_endian, dc->height, d->data + dcoff + 6); + put_16 (big_endian, dc->id, d->data + dcoff + 8); + + *pp = d; + pp = &d->next; + + *pp = resid_to_bin (dc->class, big_endian); + length += (*pp)->length; + pp = &(*pp)->next; + + *pp = resid_to_bin (dc->text, big_endian); + length += (*pp)->length; + pp = &(*pp)->next; + + d = (struct bindata *) reswr_alloc (sizeof *d); + d->length = 2; + d->data = (unsigned char *) reswr_alloc (2); + + length += 2; + + d->next = NULL; + *pp = d; + pp = &d->next; + + if (dc->data == NULL) + put_16 (big_endian, 0, d->data); + else + { + dword_align_bin (&pp, &length); + + *pp = res_to_bin_rcdata (dc->data, big_endian); + while (*pp != NULL) + { + length += (*pp)->length; + pp = &(*pp)->next; + } + } + } + + put_16 (big_endian, c, first->data + off); + + return first; +} + +/* Convert a fontdir resource to binary. */ + +static struct bindata * +res_to_bin_fontdir (fontdirs, big_endian) + const struct fontdir *fontdirs; + int big_endian; +{ + struct bindata *first, **pp; + int c; + const struct fontdir *fd; + + first = (struct bindata *) reswr_alloc (sizeof *first); + first->length = 2; + first->data = (unsigned char *) reswr_alloc (2); + + first->next = NULL; + pp = &first->next; + + c = 0; + for (fd = fontdirs; fd != NULL; fd = fd->next) + { + struct bindata *d; + + ++c; + + d = (struct bindata *) reswr_alloc (sizeof *d); + d->length = 2; + d->data = (unsigned char *) reswr_alloc (2); + + put_16 (big_endian, fd->index, d->data); + + *pp = d; + pp = &d->next; + + d = (struct bindata *) reswr_alloc (sizeof *d); + d->length = fd->length; + d->data = (unsigned char *) fd->data; + + d->next = NULL; + *pp = d; + pp = &d->next; + } + + put_16 (big_endian, c, first->data); + + return first; +} + +/* Convert a group icon resource to binary. */ + +static struct bindata * +res_to_bin_group_icon (group_icons, big_endian) + const struct group_icon *group_icons; + int big_endian; +{ + struct bindata *first, **pp; + int c; + const struct group_icon *gi; + + first = (struct bindata *) reswr_alloc (sizeof *first); + first->length = 6; + first->data = (unsigned char *) reswr_alloc (6); + + put_16 (big_endian, 0, first->data); + put_16 (big_endian, 1, first->data + 2); + + first->next = NULL; + pp = &first->next; + + c = 0; + for (gi = group_icons; gi != NULL; gi = gi->next) + { + struct bindata *d; + + ++c; + + d = (struct bindata *) reswr_alloc (sizeof *d); + d->length = 14; + d->data = (unsigned char *) reswr_alloc (14); + + d->data[0] = gi->width; + d->data[1] = gi->height; + d->data[2] = gi->colors; + d->data[3] = 0; + put_16 (big_endian, gi->planes, d->data + 4); + put_16 (big_endian, gi->bits, d->data + 6); + put_32 (big_endian, gi->bytes, d->data + 8); + put_16 (big_endian, gi->index, d->data + 12); + + d->next = NULL; + *pp = d; + pp = &d->next; + } + + put_16 (big_endian, c, first->data + 4); + + return first; +} + +/* Convert a menu resource to binary. */ + +static struct bindata * +res_to_bin_menu (menu, big_endian) + const struct menu *menu; + int big_endian; +{ + int menuex; + struct bindata *d; + + menuex = extended_menu (menu); + + d = (struct bindata *) reswr_alloc (sizeof *d); + d->length = menuex ? 4 : 8; + d->data = (unsigned char *) reswr_alloc (d->length); + + if (! menuex) + { + put_16 (big_endian, 0, d->data); + put_16 (big_endian, 0, d->data + 2); + + d->next = res_to_bin_menuitems (menu->items, big_endian); + } + else + { + put_16 (big_endian, 1, d->data); + put_16 (big_endian, 4, d->data + 2); + put_32 (big_endian, menu->help, d->data + 4); + + d->next = res_to_bin_menuexitems (menu->items, big_endian); + } + + return d; +} + +/* Convert menu items to binary. */ + +static struct bindata * +res_to_bin_menuitems (items, big_endian) + const struct menuitem *items; + int big_endian; +{ + struct bindata *first, **pp; + const struct menuitem *mi; + + first = NULL; + pp = &first; + + for (mi = items; mi != NULL; mi = mi->next) + { + struct bindata *d; + int flags; + + d = (struct bindata *) reswr_alloc (sizeof *d); + d->length = mi->popup == NULL ? 4 : 2; + d->data = (unsigned char *) reswr_alloc (d->length); + + flags = mi->type; + if (mi->next == NULL) + flags |= MENUITEM_ENDMENU; + if (mi->popup != NULL) + flags |= MENUITEM_POPUP; + + put_16 (big_endian, flags, d->data); + + if (mi->popup == NULL) + put_16 (big_endian, mi->id, d->data + 2); + + *pp = d; + pp = &d->next; + + *pp = unicode_to_bin (mi->text, big_endian); + pp = &(*pp)->next; + + if (mi->popup != NULL) + { + *pp = res_to_bin_menuitems (mi->popup, big_endian); + while (*pp != NULL) + pp = &(*pp)->next; + } + } + + return first; +} + +/* Convert menuex items to binary. */ + +static struct bindata * +res_to_bin_menuexitems (items, big_endian) + const struct menuitem *items; + int big_endian; +{ + struct bindata *first, **pp; + unsigned long length; + const struct menuitem *mi; + + first = NULL; + pp = &first; + + length = 0; + + for (mi = items; mi != NULL; mi = mi->next) + { + struct bindata *d; + int flags; + + dword_align_bin (&pp, &length); + + d = (struct bindata *) reswr_alloc (sizeof *d); + d->length = 12; + d->data = (unsigned char *) reswr_alloc (12); + + length += 12; + + put_32 (big_endian, mi->type, d->data); + put_32 (big_endian, mi->state, d->data + 4); + put_16 (big_endian, mi->id, d->data + 8); + + flags = 0; + if (mi->next == NULL) + flags |= 0x80; + if (mi->popup != NULL) + flags |= 1; + put_16 (big_endian, flags, d->data + 10); + + *pp = d; + pp = &d->next; + + *pp = unicode_to_bin (mi->text, big_endian); + length += (*pp)->length; + pp = &(*pp)->next; + + if (mi->popup != NULL) + { + dword_align_bin (&pp, &length); + + d = (struct bindata *) reswr_alloc (sizeof *d); + d->length = 4; + d->data = (unsigned char *) reswr_alloc (4); + + put_32 (big_endian, mi->help, d->data); + + *pp = d; + pp = &d->next; + + *pp = res_to_bin_menuexitems (mi->popup, big_endian); + while (*pp != NULL) + { + length += (*pp)->length; + pp = &(*pp)->next; + } + } + } + + return first; +} + +/* Convert an rcdata resource to binary. This is also used to convert + other information which happens to be stored in rcdata_item lists + to binary. */ + +static struct bindata * +res_to_bin_rcdata (items, big_endian) + const struct rcdata_item *items; + int big_endian; +{ + struct bindata *first, **pp; + const struct rcdata_item *ri; + + first = NULL; + pp = &first; + + for (ri = items; ri != NULL; ri = ri->next) + { + struct bindata *d; + + d = (struct bindata *) reswr_alloc (sizeof *d); + + switch (ri->type) + { + default: + abort (); + + case RCDATA_WORD: + d->length = 2; + d->data = (unsigned char *) reswr_alloc (2); + put_16 (big_endian, ri->u.word, d->data); + break; + + case RCDATA_DWORD: + d->length = 4; + d->data = (unsigned char *) reswr_alloc (4); + put_32 (big_endian, ri->u.dword, d->data); + break; + + case RCDATA_STRING: + d->length = ri->u.string.length; + d->data = (unsigned char *) ri->u.string.s; + break; + + case RCDATA_WSTRING: + { + unsigned long i; + + d->length = ri->u.wstring.length * 2; + d->data = (unsigned char *) reswr_alloc (d->length); + for (i = 0; i < ri->u.wstring.length; i++) + put_16 (big_endian, ri->u.wstring.w[i], d->data + i * 2); + break; + } + + case RCDATA_BUFFER: + d->length = ri->u.buffer.length; + d->data = (unsigned char *) ri->u.buffer.data; + break; + } + + d->next = NULL; + *pp = d; + pp = &d->next; + } + + return first; +} + +/* Convert a stringtable resource to binary. */ + +static struct bindata * +res_to_bin_stringtable (st, big_endian) + const struct stringtable *st; + int big_endian; +{ + struct bindata *first, **pp; + int i; + + first = NULL; + pp = &first; + + for (i = 0; i < 16; i++) + { + int slen, j; + struct bindata *d; + unichar *s; + + slen = st->strings[i].length; + s = st->strings[i].string; + + d = (struct bindata *) reswr_alloc (sizeof *d); + d->length = 2 + slen * 2; + d->data = (unsigned char *) reswr_alloc (d->length); + + put_16 (big_endian, slen, d->data); + + for (j = 0; j < slen; j++) + put_16 (big_endian, s[j], d->data + 2 + j * 2); + + d->next = NULL; + *pp = d; + pp = &d->next; + } + + return first; +} + +/* Convert an ASCII string to a unicode binary string. This always + returns exactly one bindata structure. */ + +static struct bindata * +string_to_unicode_bin (s, big_endian) + const char *s; + int big_endian; +{ + size_t len, i; + struct bindata *d; + + len = strlen (s); + + d = (struct bindata *) reswr_alloc (sizeof *d); + d->length = len * 2 + 2; + d->data = (unsigned char *) reswr_alloc (d->length); + + for (i = 0; i < len; i++) + put_16 (big_endian, s[i], d->data + i * 2); + put_16 (big_endian, 0, d->data + i * 2); + + d->next = NULL; + + return d; +} + +/* Convert a versioninfo resource to binary. */ + +static struct bindata * +res_to_bin_versioninfo (versioninfo, big_endian) + const struct versioninfo *versioninfo; + int big_endian; +{ + struct bindata *first, **pp; + unsigned long length; + struct ver_info *vi; + + first = (struct bindata *) reswr_alloc (sizeof *first); + first->length = 6; + first->data = (unsigned char *) reswr_alloc (6); + + length = 6; + + if (versioninfo->fixed == NULL) + put_16 (big_endian, 0, first->data + 2); + else + put_16 (big_endian, 52, first->data + 2); + + put_16 (big_endian, 0, first->data + 4); + + pp = &first->next; + + *pp = string_to_unicode_bin ("VS_VERSION_INFO", big_endian); + length += (*pp)->length; + pp = &(*pp)->next; + + dword_align_bin (&pp, &length); + + if (versioninfo->fixed != NULL) + { + const struct fixed_versioninfo *fi; + struct bindata *d; + + d = (struct bindata *) reswr_alloc (sizeof *d); + d->length = 52; + d->data = (unsigned char *) reswr_alloc (52); + + length += 52; + + fi = versioninfo->fixed; + + put_32 (big_endian, 0xfeef04bd, d->data); + put_32 (big_endian, 0x10000, d->data + 4); + put_32 (big_endian, fi->file_version_ms, d->data + 8); + put_32 (big_endian, fi->file_version_ls, d->data + 12); + put_32 (big_endian, fi->product_version_ms, d->data + 16); + put_32 (big_endian, fi->product_version_ls, d->data + 20); + put_32 (big_endian, fi->file_flags_mask, d->data + 24); + put_32 (big_endian, fi->file_flags, d->data + 28); + put_32 (big_endian, fi->file_os, d->data + 32); + put_32 (big_endian, fi->file_type, d->data + 36); + put_32 (big_endian, fi->file_subtype, d->data + 40); + put_32 (big_endian, fi->file_date_ms, d->data + 44); + put_32 (big_endian, fi->file_date_ls, d->data + 48); + + d->next = NULL; + *pp = d; + pp = &d->next; + } + + for (vi = versioninfo->var; vi != NULL; vi = vi->next) + { + struct bindata *vid; + unsigned long vilen; + + dword_align_bin (&pp, &length); + + vid = (struct bindata *) reswr_alloc (sizeof *vid); + vid->length = 6; + vid->data = (unsigned char *) reswr_alloc (6); + + length += 6; + vilen = 6; + + put_16 (big_endian, 0, vid->data + 2); + put_16 (big_endian, 0, vid->data + 4); + + *pp = vid; + pp = &vid->next; + + switch (vi->type) + { + default: + abort (); + + case VERINFO_STRING: + { + unsigned long hold, vslen; + struct bindata *vsd; + const struct ver_stringinfo *vs; + + *pp = string_to_unicode_bin ("StringFileInfo", big_endian); + length += (*pp)->length; + vilen += (*pp)->length; + pp = &(*pp)->next; + + hold = length; + dword_align_bin (&pp, &length); + vilen += length - hold; + + vsd = (struct bindata *) reswr_alloc (sizeof *vsd); + vsd->length = 6; + vsd->data = (unsigned char *) reswr_alloc (6); + + length += 6; + vilen += 6; + vslen = 6; + + put_16 (big_endian, 0, vsd->data + 2); + put_16 (big_endian, 0, vsd->data + 4); + + *pp = vsd; + pp = &vsd->next; + + *pp = unicode_to_bin (vi->u.string.language, big_endian); + length += (*pp)->length; + vilen += (*pp)->length; + vslen += (*pp)->length; + pp = &(*pp)->next; + + for (vs = vi->u.string.strings; vs != NULL; vs = vs->next) + { + struct bindata *vssd; + unsigned long vsslen; + + hold = length; + dword_align_bin (&pp, &length); + vilen += length - hold; + vslen += length - hold; + + vssd = (struct bindata *) reswr_alloc (sizeof *vssd); + vssd->length = 6; + vssd->data = (unsigned char *) reswr_alloc (6); + + length += 6; + vilen += 6; + vslen += 6; + vsslen = 6; + + put_16 (big_endian, 0, vssd->data + 2); + put_16 (big_endian, 1, vssd->data + 4); + + *pp = vssd; + pp = &vssd->next; + + *pp = unicode_to_bin (vs->key, big_endian); + length += (*pp)->length; + vilen += (*pp)->length; + vslen += (*pp)->length; + vsslen += (*pp)->length; + pp = &(*pp)->next; + + hold = length; + dword_align_bin (&pp, &length); + vilen += length - hold; + vslen += length - hold; + vsslen += length - hold; + + *pp = unicode_to_bin (vs->value, big_endian); + length += (*pp)->length; + vilen += (*pp)->length; + vslen += (*pp)->length; + vsslen += (*pp)->length; + pp = &(*pp)->next; + + put_16 (big_endian, vsslen, vssd->data); + } + + put_16 (big_endian, vslen, vsd->data); + + break; + } + + case VERINFO_VAR: + { + unsigned long hold, vvlen, vvvlen; + struct bindata *vvd; + const struct ver_varinfo *vv; + + *pp = string_to_unicode_bin ("VarFileInfo", big_endian); + length += (*pp)->length; + vilen += (*pp)->length; + pp = &(*pp)->next; + + hold = length; + dword_align_bin (&pp, &length); + vilen += length - hold; + + vvd = (struct bindata *) reswr_alloc (sizeof *vvd); + vvd->length = 6; + vvd->data = (unsigned char *) reswr_alloc (6); + + length += 6; + vilen += 6; + vvlen = 6; + + put_16 (big_endian, 0, vvd->data + 4); + + *pp = vvd; + pp = &vvd->next; + + *pp = unicode_to_bin (vi->u.var.key, big_endian); + length += (*pp)->length; + vilen += (*pp)->length; + vvlen += (*pp)->length; + pp = &(*pp)->next; + + hold = length; + dword_align_bin (&pp, &length); + vilen += length - hold; + vvlen += length - hold; + + vvvlen = 0; + + for (vv = vi->u.var.var; vv != NULL; vv = vv->next) + { + struct bindata *vvsd; + + vvsd = (struct bindata *) reswr_alloc (sizeof *vvsd); + vvsd->length = 4; + vvsd->data = (unsigned char *) reswr_alloc (4); + + length += 4; + vilen += 4; + vvlen += 4; + vvvlen += 4; + + put_16 (big_endian, vv->language, vvsd->data); + put_16 (big_endian, vv->charset, vvsd->data + 2); + + vvsd->next = NULL; + *pp = vvsd; + pp = &vvsd->next; + } + + put_16 (big_endian, vvlen, vvd->data); + put_16 (big_endian, vvvlen, vvd->data + 2); + + break; + } + } + + put_16 (big_endian, vilen, vid->data); + } + + put_16 (big_endian, length, first->data); + + return first; +} + +/* Convert a generic resource to binary. */ + +static struct bindata * +res_to_bin_generic (length, data) + unsigned long length; + const unsigned char *data; +{ + struct bindata *d; + + d = (struct bindata *) reswr_alloc (sizeof *d); + d->length = length; + d->data = (unsigned char *) data; + + d->next = NULL; + + return d; +} diff --git a/binutils/rescoff.c b/binutils/rescoff.c index 65df9bf..0f83e54 100644 --- a/binutils/rescoff.c +++ b/binutils/rescoff.c @@ -1,4 +1,4 @@ -/* resrc.c -- read and write Windows rc files. +/* rescoff.c -- read and write resources in Windows COFF files. Copyright 1997 Free Software Foundation, Inc. Written by Ian Lance Taylor, Cygnus Support. @@ -27,6 +27,8 @@ #include "libiberty.h" #include "windres.h" +#include <assert.h> + /* In order to use the address of a resource data entry, we need to get the image base of the file. Right now we extract it from internal BFD information. FIXME. */ @@ -95,16 +97,18 @@ struct extern_res_data /* Macros to swap in values. */ -#define get_16(fi, s) ((fi)->big_endian ? bfd_getb16 (s) : bfd_getl16 (s)) -#define get_32(fi, s) ((fi)->big_endian ? bfd_getb32 (s) : bfd_getl32 (s)) +#define getfi_16(fi, s) ((fi)->big_endian ? bfd_getb16 (s) : bfd_getl16 (s)) +#define getfi_32(fi, s) ((fi)->big_endian ? bfd_getb32 (s) : bfd_getl32 (s)) /* Local functions. */ static void overrun PARAMS ((const struct coff_file_info *, const char *)); static struct res_directory *read_coff_res_dir - PARAMS ((const bfd_byte *, const struct coff_file_info *)); + PARAMS ((const bfd_byte *, const struct coff_file_info *, + const struct res_id *, int)); static struct res_resource *read_coff_data_entry - PARAMS ((const bfd_byte *, const struct coff_file_info *)); + PARAMS ((const bfd_byte *, const struct coff_file_info *, + const struct res_id *)); /* Read the resources in a COFF file. */ @@ -141,7 +145,7 @@ read_coff_rsrc (filename, target) } size = bfd_section_size (abfd, sec); - data = (bfd_byte *) xmalloc (size); + data = (bfd_byte *) res_alloc (size); if (! bfd_get_section_contents (abfd, sec, data, 0, size)) bfd_fatal ("can't read resource section"); @@ -160,7 +164,7 @@ read_coff_rsrc (filename, target) it. If we ever want to free up the resource information we read, this will have to be cleaned up. */ - return read_coff_res_dir (data, &finfo); + return read_coff_res_dir (data, &finfo, (const struct res_id *) NULL, 0); } /* Give an error if we are out of bounds. */ @@ -176,9 +180,11 @@ overrun (finfo, msg) /* Read a resource directory. */ static struct res_directory * -read_coff_res_dir (data, finfo) +read_coff_res_dir (data, finfo, type, level) const bfd_byte *data; const struct coff_file_info *finfo; + const struct res_id *type; + int level; { const struct extern_res_directory *erd; struct res_directory *rd; @@ -191,15 +197,15 @@ read_coff_res_dir (data, finfo) erd = (const struct extern_res_directory *) data; - rd = (struct res_directory *) xmalloc (sizeof *rd); - rd->characteristics = get_32 (finfo, erd->characteristics); - rd->time = get_32 (finfo, erd->time); - rd->major = get_16 (finfo, erd->major); - rd->minor = get_16 (finfo, erd->minor); + rd = (struct res_directory *) res_alloc (sizeof *rd); + rd->characteristics = getfi_32 (finfo, erd->characteristics); + rd->time = getfi_32 (finfo, erd->time); + rd->major = getfi_16 (finfo, erd->major); + rd->minor = getfi_16 (finfo, erd->minor); rd->entries = NULL; - name_count = get_16 (finfo, erd->name_count); - id_count = get_16 (finfo, erd->id_count); + name_count = getfi_16 (finfo, erd->name_count); + id_count = getfi_16 (finfo, erd->id_count); pp = &rd->entries; @@ -217,8 +223,8 @@ read_coff_res_dir (data, finfo) if ((const bfd_byte *) ere >= finfo->data_end) overrun (finfo, "named directory entry"); - name = get_32 (finfo, ere->name); - rva = get_32 (finfo, ere->rva); + name = getfi_32 (finfo, ere->name); + rva = getfi_32 (finfo, ere->rva); /* For some reason the high bit in NAME is set. */ name &=~ 0x80000000; @@ -228,15 +234,17 @@ read_coff_res_dir (data, finfo) ers = finfo->data + name; - re = (struct res_entry *) xmalloc (sizeof *re); + re = (struct res_entry *) res_alloc (sizeof *re); re->next = NULL; re->id.named = 1; - length = get_16 (finfo, ers); + length = getfi_16 (finfo, ers); re->id.u.n.length = length; - re->id.u.n.name = ((unsigned short *) - xmalloc (length * sizeof (unsigned short))); + re->id.u.n.name = (unichar *) res_alloc (length * sizeof (unichar)); for (j = 0; j < length; j++) - re->id.u.n.name[j] = get_16 (finfo, ers + j * 2 + 2); + re->id.u.n.name[j] = getfi_16 (finfo, ers + j * 2 + 2); + + if (level == 0) + type = &re->id; if ((rva & 0x80000000) != 0) { @@ -244,14 +252,15 @@ read_coff_res_dir (data, finfo) if (rva >= finfo->data_end - finfo->data) overrun (finfo, "named subdirectory"); re->subdir = 1; - re->u.dir = read_coff_res_dir (finfo->data + rva, finfo); + re->u.dir = read_coff_res_dir (finfo->data + rva, finfo, type, + level + 1); } else { if (rva >= finfo->data_end - finfo->data) overrun (finfo, "named resource"); re->subdir = 0; - re->u.res = read_coff_data_entry (finfo->data + rva, finfo); + re->u.res = read_coff_data_entry (finfo->data + rva, finfo, type); } *pp = re; @@ -266,28 +275,32 @@ read_coff_res_dir (data, finfo) if ((const bfd_byte *) ere >= finfo->data_end) overrun (finfo, "ID directory entry"); - name = get_32 (finfo, ere->name); - rva = get_32 (finfo, ere->rva); + name = getfi_32 (finfo, ere->name); + rva = getfi_32 (finfo, ere->rva); - re = (struct res_entry *) xmalloc (sizeof *re); + re = (struct res_entry *) res_alloc (sizeof *re); re->next = NULL; re->id.named = 0; re->id.u.id = name; + if (level == 0) + type = &re->id; + if ((rva & 0x80000000) != 0) { rva &=~ 0x80000000; if (rva >= finfo->data_end - finfo->data) overrun (finfo, "ID subdirectory"); re->subdir = 1; - re->u.dir = read_coff_res_dir (finfo->data + rva, finfo); + re->u.dir = read_coff_res_dir (finfo->data + rva, finfo, type, + level + 1); } else { if (rva >= finfo->data_end - finfo->data) overrun (finfo, "ID resource"); re->subdir = 0; - re->u.res = read_coff_data_entry (finfo->data + rva, finfo); + re->u.res = read_coff_data_entry (finfo->data + rva, finfo, type); } *pp = re; @@ -300,43 +313,459 @@ read_coff_res_dir (data, finfo) /* Read a resource data entry. */ static struct res_resource * -read_coff_data_entry (data, finfo) +read_coff_data_entry (data, finfo, type) const bfd_byte *data; const struct coff_file_info *finfo; + const struct res_id *type; { const struct extern_res_data *erd; struct res_resource *r; unsigned long size, rva; const bfd_byte *resdata; + if (type == NULL) + fatal ("resource type unknown"); + if (finfo->data_end - data < sizeof (struct extern_res_data)) overrun (finfo, "data entry"); erd = (const struct extern_res_data *) data; - r = (struct res_resource *) xmalloc (sizeof *r); - memset (&r->res_info, 0, sizeof (struct res_res_info)); - r->coff_info.codepage = get_32 (finfo, erd->codepage); - r->coff_info.reserved = get_32 (finfo, erd->reserved); - - size = get_32 (finfo, erd->size); - rva = get_32 (finfo, erd->rva); + size = getfi_32 (finfo, erd->size); + rva = getfi_32 (finfo, erd->rva); if (rva < finfo->secaddr || rva - finfo->secaddr >= finfo->data_end - finfo->data) overrun (finfo, "resource data"); resdata = finfo->data + (rva - finfo->secaddr); - r->type = RES_TYPE_USERDATA; - r->u.userdata = ((struct rcdata_data *) - xmalloc (sizeof (struct rcdata_data))); - r->u.userdata->first = ((struct rcdata_item *) - xmalloc (sizeof (struct rcdata_item))); - r->u.userdata->last = r->u.userdata->first; - r->u.userdata->first->next = NULL; - r->u.userdata->first->type = RCDATA_BUFFER; - r->u.userdata->first->u.buffer.length = size; - r->u.userdata->first->u.buffer.data = (unsigned char *) resdata; + if (size > finfo->data_end - resdata) + overrun (finfo, "resource data size"); + + r = bin_to_res (*type, resdata, size, finfo->big_endian); + + memset (&r->res_info, 0, sizeof (struct res_res_info)); + r->coff_info.codepage = getfi_32 (finfo, erd->codepage); + r->coff_info.reserved = getfi_32 (finfo, erd->reserved); return r; } + +/* This structure is used to build a list of bindata structures. */ + +struct bindata_build +{ + /* The data. */ + struct bindata *d; + /* The last structure we have added to the list. */ + struct bindata *last; + /* The size of the list as a whole. */ + unsigned long length; +}; + +/* This structure keeps track of information as we build the directory + tree. */ + +struct coff_write_info +{ + /* These fields are based on the BFD. */ + /* The BFD itself. */ + bfd *abfd; + /* Non-zero if the file is big endian. */ + int big_endian; + /* Pointer to section symbol used to build RVA relocs. */ + asymbol **sympp; + + /* These fields are computed initially, and then not changed. */ + /* Length of directory tables and entries. */ + unsigned long dirsize; + /* Length of directory entry strings. */ + unsigned long dirstrsize; + /* Length of resource data entries. */ + unsigned long dataentsize; + + /* These fields are updated as we add data. */ + /* Directory tables and entries. */ + struct bindata_build dirs; + /* Directory entry strings. */ + struct bindata_build dirstrs; + /* Resource data entries. */ + struct bindata_build dataents; + /* Actual resource data. */ + struct bindata_build resources; + /* Relocations. */ + arelent **relocs; + /* Number of relocations. */ + unsigned int reloc_count; +}; + +/* Macros to swap out values. */ + +#define putcwi_16(cwi, v, s) \ + ((cwi->big_endian) ? bfd_putb16 ((v), (s)) : bfd_putl16 ((v), (s))) +#define putcwi_32(cwi, v, s) \ + ((cwi->big_endian) ? bfd_putb32 ((v), (s)) : bfd_putl32 ((v), (s))) + +static void coff_bin_sizes + PARAMS ((const struct res_directory *, struct coff_write_info *)); +static unsigned char *coff_alloc PARAMS ((struct bindata_build *, size_t)); +static void coff_to_bin + PARAMS ((const struct res_directory *, struct coff_write_info *)); +static void coff_res_to_bin + PARAMS ((const struct res_resource *, struct coff_write_info *)); + +/* Write resources to a COFF file. RESOURCES should already be + sorted. + + Right now we always create a new file. Someday we should also + offer the ability to merge resources into an existing file. This + would require doing the basic work of objcopy, just modifying or + adding the .rsrc section. */ + + +void +write_coff_file (filename, target, resources) + const char *filename; + const char *target; + const struct res_directory *resources; +{ + bfd *abfd; + asection *sec; + struct coff_write_info cwi; + struct bindata *d; + unsigned long length, offset; + + abfd = bfd_openw (filename, target); + if (abfd == NULL) + bfd_fatal (filename); + + if (! bfd_set_format (abfd, bfd_object)) + bfd_fatal ("bfd_set_format"); + + /* FIXME: This is obviously i386 specific. */ + if (! bfd_set_arch_mach (abfd, bfd_arch_i386, 0)) + bfd_fatal ("bfd_set_arch_mach"); + + if (! bfd_set_file_flags (abfd, HAS_SYMS | HAS_RELOC)) + bfd_fatal ("bfd_set_file_flags"); + + sec = bfd_make_section (abfd, ".rsrc"); + if (sec == NULL) + bfd_fatal ("bfd_make_section"); + + if (! bfd_set_section_flags (abfd, sec, + (SEC_HAS_CONTENTS | SEC_ALLOC + | SEC_LOAD | SEC_DATA))) + bfd_fatal ("bfd_set_section_flags"); + + if (! bfd_set_symtab (abfd, sec->symbol_ptr_ptr, 1)) + bfd_fatal ("bfd_set_symtab"); + + /* Requiring this is probably a bug in BFD. */ + sec->output_section = sec; + + /* The order of data in the .rsrc section is + resource directory tables and entries + resource directory strings + resource data entries + actual resource data + + We build these different types of data in different lists. */ + + cwi.abfd = abfd; + cwi.big_endian = bfd_big_endian (abfd); + cwi.sympp = sec->symbol_ptr_ptr; + cwi.dirsize = 0; + cwi.dirstrsize = 0; + cwi.dataentsize = 0; + cwi.dirs.d = NULL; + cwi.dirs.last = NULL; + cwi.dirs.length = 0; + cwi.dirstrs.d = NULL; + cwi.dirstrs.last = NULL; + cwi.dirstrs.length = 0; + cwi.dataents.d = NULL; + cwi.dataents.last = NULL; + cwi.dataents.length = 0; + cwi.resources.d = NULL; + cwi.resources.last = NULL; + cwi.resources.length = 0; + cwi.relocs = NULL; + cwi.reloc_count = 0; + + /* Work out the sizes of the resource directory entries, so that we + know the various offsets we will need. */ + coff_bin_sizes (resources, &cwi); + + /* Force the directory strings to be 32 bit aligned. Every other + structure is 32 bit aligned anyhow. */ + cwi.dirstrsize = (cwi.dirstrsize + 3) &~ 3; + + /* Actually convert the resources to binary. */ + coff_to_bin (resources, &cwi); + + /* Add another 2 bytes to the directory strings if needed for + alignment. */ + if ((cwi.dirstrs.length & 3) != 0) + { + unsigned char *ex; + + ex = coff_alloc (&cwi.dirstrs, 2); + ex[0] = 0; + ex[1] = 0; + } + + /* Make sure that the data we built came out to the same size as we + calculated initially. */ + assert (cwi.dirs.length == cwi.dirsize); + assert (cwi.dirstrs.length == cwi.dirstrsize); + assert (cwi.dataents.length == cwi.dataentsize); + + length = (cwi.dirsize + + cwi.dirstrsize + + cwi.dataentsize + + cwi.resources.length); + + if (! bfd_set_section_size (abfd, sec, length)) + bfd_fatal ("bfd_set_section_size"); + + bfd_set_reloc (abfd, sec, cwi.relocs, cwi.reloc_count); + + /* We allocated the relocs array using malloc. */ + free (cwi.relocs); + + offset = 0; + for (d = cwi.dirs.d; d != NULL; d = d->next) + { + if (! bfd_set_section_contents (abfd, sec, d->data, offset, d->length)) + bfd_fatal ("bfd_set_section_contents"); + offset += d->length; + } + for (d = cwi.dirstrs.d; d != NULL; d = d->next) + { + if (! bfd_set_section_contents (abfd, sec, d->data, offset, d->length)) + bfd_fatal ("bfd_set_section_contents"); + offset += d->length; + } + for (d = cwi.dataents.d; d != NULL; d = d->next) + { + if (! bfd_set_section_contents (abfd, sec, d->data, offset, d->length)) + bfd_fatal ("bfd_set_section_contents"); + offset += d->length; + } + for (d = cwi.resources.d; d != NULL; d = d->next) + { + if (! bfd_set_section_contents (abfd, sec, d->data, offset, d->length)) + bfd_fatal ("bfd_set_section_contents"); + offset += d->length; + } + + assert (offset == length); + + if (! bfd_close (abfd)) + bfd_fatal ("bfd_close"); +} + +/* Work out the sizes of the various fixed size resource directory + entries. This updates fields in CWI. */ + +static void +coff_bin_sizes (resdir, cwi) + const struct res_directory *resdir; + struct coff_write_info *cwi; +{ + const struct res_entry *re; + + cwi->dirsize += sizeof (struct extern_res_directory); + + for (re = resdir->entries; re != NULL; re = re->next) + { + cwi->dirsize += sizeof (struct extern_res_entry); + + if (re->id.named) + cwi->dirstrsize += re->id.u.n.length * 2 + 2; + + if (re->subdir) + coff_bin_sizes (re->u.dir, cwi); + else + cwi->dataentsize += sizeof (struct extern_res_data); + } +} + +/* Allocate data for a particular list. */ + +static unsigned char * +coff_alloc (bb, size) + struct bindata_build *bb; + size_t size; +{ + struct bindata *d; + + d = (struct bindata *) reswr_alloc (sizeof *d); + + d->next = NULL; + d->data = (unsigned char *) reswr_alloc (size); + d->length = size; + + if (bb->d == NULL) + bb->d = d; + else + bb->last->next = d; + bb->last = d; + bb->length += size; + + return d->data; +} + +/* Convert the resource directory RESDIR to binary. */ + +static void +coff_to_bin (resdir, cwi) + const struct res_directory *resdir; + struct coff_write_info *cwi; +{ + struct extern_res_directory *erd; + int ci, cn; + const struct res_entry *e; + struct extern_res_entry *ere; + + /* Write out the directory table. */ + + erd = ((struct extern_res_directory *) + coff_alloc (&cwi->dirs, sizeof (*erd))); + + putcwi_32 (cwi, resdir->characteristics, erd->characteristics); + putcwi_32 (cwi, resdir->time, erd->time); + putcwi_16 (cwi, resdir->major, erd->major); + putcwi_16 (cwi, resdir->minor, erd->minor); + + ci = 0; + cn = 0; + for (e = resdir->entries; e != NULL; e = e->next) + { + if (e->id.named) + ++cn; + else + ++ci; + } + + putcwi_16 (cwi, cn, erd->name_count); + putcwi_16 (cwi, ci, erd->id_count); + + /* Write out the data entries. Note that we allocate space for all + the entries before writing them out. That permits a recursive + call to work correctly when writing out subdirectories. */ + + ere = ((struct extern_res_entry *) + coff_alloc (&cwi->dirs, (ci + cn) * sizeof (*ere))); + for (e = resdir->entries; e != NULL; e = e->next, ere++) + { + if (! e->id.named) + putcwi_32 (cwi, e->id.u.id, ere->name); + else + { + unsigned char *str; + int i; + + /* For some reason existing files seem to have the high bit + set on the address of the name, although that is not + documented. */ + putcwi_32 (cwi, + 0x80000000 | (cwi->dirsize + cwi->dirstrs.length), + ere->name); + + str = coff_alloc (&cwi->dirstrs, e->id.u.n.length * 2 + 2); + putcwi_16 (cwi, e->id.u.n.length, str); + for (i = 0; i < e->id.u.n.length; i++) + putcwi_16 (cwi, e->id.u.n.name[i], str + i * 2 + 2); + } + + if (e->subdir) + { + putcwi_32 (cwi, 0x80000000 | cwi->dirs.length, ere->rva); + coff_to_bin (e->u.dir, cwi); + } + else + { + putcwi_32 (cwi, + cwi->dirsize + cwi->dirstrsize + cwi->dataents.length, + ere->rva); + + coff_res_to_bin (e->u.res, cwi); + } + } +} + +/* Convert the resource RES to binary. */ + +static void +coff_res_to_bin (res, cwi) + const struct res_resource *res; + struct coff_write_info *cwi; +{ + arelent *r; + struct extern_res_data *erd; + struct bindata *d; + unsigned long length; + + /* For some reason, although every other address is a section + offset, the address of the resource data itself is an RVA. That + means that we need to generate a relocation for it. We allocate + the relocs array using malloc so that we can use realloc. FIXME: + This relocation handling is correct for the i386, but probably + not for any other target. */ + + r = (arelent *) reswr_alloc (sizeof (arelent)); + r->sym_ptr_ptr = cwi->sympp; + r->address = cwi->dirsize + cwi->dirstrsize + cwi->dataents.length; + r->addend = 0; + r->howto = bfd_reloc_type_lookup (cwi->abfd, BFD_RELOC_RVA); + if (r->howto == NULL) + bfd_fatal ("can't get BFD_RELOC_RVA relocation type"); + + cwi->relocs = xrealloc (cwi->relocs, + (cwi->reloc_count + 2) * sizeof (arelent *)); + cwi->relocs[cwi->reloc_count] = r; + cwi->relocs[cwi->reloc_count + 1] = NULL; + ++cwi->reloc_count; + + erd = (struct extern_res_data *) coff_alloc (&cwi->dataents, sizeof (*erd)); + + putcwi_32 (cwi, + (cwi->dirsize + + cwi->dirstrsize + + cwi->dataentsize + + cwi->resources.length), + erd->rva); + putcwi_32 (cwi, res->coff_info.codepage, erd->codepage); + putcwi_32 (cwi, res->coff_info.reserved, erd->reserved); + + d = res_to_bin (res, cwi->big_endian); + + if (cwi->resources.d == NULL) + cwi->resources.d = d; + else + cwi->resources.last->next = d; + + length = 0; + for (; d->next != NULL; d = d->next) + length += d->length; + length += d->length; + cwi->resources.last = d; + cwi->resources.length += length; + + putcwi_32 (cwi, length, erd->size); + + /* Force the next resource to have 32 bit alignment. */ + + if ((length & 3) != 0) + { + int add; + unsigned char *ex; + + add = 4 - (length & 3); + + ex = coff_alloc (&cwi->resources, add); + memset (ex, 0, add); + } +} diff --git a/binutils/resrc.c b/binutils/resrc.c index fb340fa..60cf4ed 100644 --- a/binutils/resrc.c +++ b/binutils/resrc.c @@ -143,6 +143,7 @@ read_rc_file (filename, preprocessor, preprocargs, language) cpp_pipe = popen (cmd, FOPEN_RT); if (cpp_pipe == NULL) fatal ("can't popen `%s': %s", cmd, strerror (errno)); + free (cmd); xatexit (close_pipe); @@ -301,7 +302,7 @@ define_bitmap (id, resinfo, filename) fatal ("stat failed on bitmap file `%s': %s", real_filename, strerror (errno)); - data = (unsigned char *) xmalloc (s.st_size - BITMAP_SKIP); + data = (unsigned char *) res_alloc (s.st_size - BITMAP_SKIP); for (i = 0; i < BITMAP_SKIP; i++) getc (e); @@ -386,11 +387,11 @@ define_cursor (id, resinfo, filename) fatal ("%s: fseek to %lu failed: %s", real_filename, icondirs[i].offset, strerror (errno)); - data = (unsigned char *) xmalloc (icondirs[i].bytes); + data = (unsigned char *) res_alloc (icondirs[i].bytes); get_data (e, data, icondirs[i].bytes, real_filename); - c = (struct cursor *) xmalloc (sizeof *c); + c = (struct cursor *) res_alloc (sizeof *c); c->xhotspot = icondirs[i].u.cursor.xhotspot; c->yhotspot = icondirs[i].u.cursor.yhotspot; c->length = icondirs[i].bytes; @@ -419,21 +420,24 @@ define_cursor (id, resinfo, filename) { struct group_cursor *cg; - /* These manipulations of icondirs into cg are copied from rcl. */ - - cg = (struct group_cursor *) xmalloc (sizeof *cg); + cg = (struct group_cursor *) res_alloc (sizeof *cg); cg->next = NULL; cg->width = icondirs[i].width; cg->height = 2 * icondirs[i].height; + + /* FIXME: What should these be set to? */ cg->planes = 1; - cg->bits = 4; - cg->bytes = icondirs[i].bytes + 8; + cg->bits = 1; + + cg->bytes = icondirs[i].bytes + 4; cg->index = first_cursor + i + 1; *pp = cg; pp = &(*pp)->next; } + free (icondirs); + r = define_standard_resource (&resources, RT_GROUP_CURSOR, id, resinfo->language, 0); r->type = RES_TYPE_GROUP_CURSOR; @@ -452,7 +456,7 @@ define_dialog (id, resinfo, dialog) struct dialog *copy; struct res_resource *r; - copy = (struct dialog *) xmalloc (sizeof *copy); + copy = (struct dialog *) res_alloc (sizeof *copy); *copy = *dialog; r = define_standard_resource (&resources, RT_DIALOG, id, @@ -467,7 +471,7 @@ define_dialog (id, resinfo, dialog) struct dialog_control * define_control (text, id, x, y, width, height, class, style, exstyle) - char *text; + const char *text; unsigned long id; unsigned long x; unsigned long y; @@ -479,7 +483,7 @@ define_control (text, id, x, y, width, height, class, style, exstyle) { struct dialog_control *n; - n = (struct dialog_control *) xmalloc (sizeof *n); + n = (struct dialog_control *) res_alloc (sizeof *n); n->next = NULL; n->id = id; n->style = style; @@ -497,7 +501,6 @@ define_control (text, id, x, y, width, height, class, style, exstyle) n->text.named = 0; n->text.u.id = 0; } - free (text); n->data = NULL; n->help = 0; @@ -517,8 +520,10 @@ define_font (id, resinfo, filename) struct stat s; unsigned char *data; struct res_resource *r; - struct fontdir *fd; long offset; + long fontdatalength; + unsigned char *fontdata; + struct fontdir *fd; const char *device, *face; struct fontdir **pp; @@ -528,7 +533,7 @@ define_font (id, resinfo, filename) fatal ("stat failed on bitmap file `%s': %s", real_filename, strerror (errno)); - data = (unsigned char *) xmalloc (s.st_size); + data = (unsigned char *) res_alloc (s.st_size); get_data (e, data, s.st_size, real_filename); @@ -568,15 +573,17 @@ define_font (id, resinfo, filename) ++fonts; - fd = (struct fontdir *) xmalloc (sizeof *fd); + fontdatalength = 58 + strlen (device) + strlen (face); + fontdata = (unsigned char *) res_alloc (fontdatalength); + memcpy (fontdata, data, 56); + strcpy ((char *) fontdata + 56, device); + strcpy ((char *) fontdata + 57 + strlen (device), face); + + fd = (struct fontdir *) res_alloc (sizeof *fd); fd->next = NULL; fd->index = fonts; - fd->length = 58 + strlen (device) + strlen (face); - fd->data = (unsigned char *) xmalloc (fd->length); - - memcpy (fd->data, data, 56); - strcpy ((char *) fd->data + 56, device); - strcpy ((char *) fd->data + 57 + strlen (device), face); + fd->length = fontdatalength; + fd->data = fontdata; for (pp = &fontdirs; *pp != NULL; pp = &(*pp)->next) ; @@ -670,7 +677,7 @@ define_icon (id, resinfo, filename) fatal ("%s: fseek to %lu failed: %s", real_filename, icondirs[i].offset, strerror (errno)); - data = (unsigned char *) xmalloc (icondirs[i].bytes); + data = (unsigned char *) res_alloc (icondirs[i].bytes); get_data (e, data, icondirs[i].bytes, real_filename); @@ -698,16 +705,21 @@ define_icon (id, resinfo, filename) { struct group_icon *cg; - /* FIXME: rcl sets planes and bits based on colors, rather than - just copying the values from the file. */ + /* For some reason, at least in some files the planes and bits + are zero. We instead set them from the color. This is + copied from rcl. */ - cg = (struct group_icon *) xmalloc (sizeof *cg); + cg = (struct group_icon *) res_alloc (sizeof *cg); cg->next = NULL; cg->width = icondirs[i].width; cg->height = icondirs[i].height; cg->colors = icondirs[i].colorcount; - cg->planes = icondirs[i].u.icon.planes; - cg->bits = icondirs[i].u.icon.bits; + + cg->planes = 1; + cg->bits = 0; + while ((1 << cg->bits) < cg->colors) + ++cg->bits; + cg->bytes = icondirs[i].bytes; cg->index = first_icon + i + 1; @@ -715,6 +727,8 @@ define_icon (id, resinfo, filename) pp = &(*pp)->next; } + free (icondirs); + r = define_standard_resource (&resources, RT_GROUP_ICON, id, resinfo->language, 0); r->type = RES_TYPE_GROUP_ICON; @@ -730,11 +744,16 @@ define_menu (id, resinfo, menuitems) const struct res_res_info *resinfo; struct menuitem *menuitems; { + struct menu *m; struct res_resource *r; + m = (struct menu *) res_alloc (sizeof *m); + m->items = menuitems; + m->help = 0; + r = define_standard_resource (&resources, RT_MENU, id, resinfo->language, 0); r->type = RES_TYPE_MENU; - r->u.menu = menuitems; + r->u.menu = m; r->res_info = *resinfo; } @@ -743,7 +762,7 @@ define_menu (id, resinfo, menuitems) struct menuitem * define_menuitem (text, menuid, type, state, help, menuitems) - char *text; + const char *text; int menuid; unsigned long type; unsigned long state; @@ -752,12 +771,15 @@ define_menuitem (text, menuid, type, state, help, menuitems) { struct menuitem *mi; - mi = (struct menuitem *) xmalloc (sizeof *mi); + mi = (struct menuitem *) res_alloc (sizeof *mi); mi->next = NULL; mi->type = type; mi->state = state; mi->id = menuid; - mi->text = text; + if (text == NULL) + mi->text = NULL; + else + unicode_from_ascii ((int *) NULL, &mi->text, text); mi->help = help; mi->popup = menuitems; return mi; @@ -784,7 +806,7 @@ define_messagetable (id, resinfo, filename) fatal ("stat failed on bitmap file `%s': %s", real_filename, strerror (errno)); - data = (unsigned char *) xmalloc (s.st_size); + data = (unsigned char *) res_alloc (s.st_size); get_data (e, data, s.st_size, real_filename); @@ -806,7 +828,7 @@ void define_rcdata (id, resinfo, data) struct res_id id; const struct res_res_info *resinfo; - struct rcdata_data *data; + struct rcdata_item *data; { struct res_resource *r; @@ -817,61 +839,42 @@ define_rcdata (id, resinfo, data) r->res_info = *resinfo; } -/* Add an rcdata_item to an rcdata resource. */ - -struct rcdata_data * -append_rcdata_item (data, item) - struct rcdata_data *data; - struct rcdata_item *item; -{ - if (data == NULL) - { - data = (struct rcdata_data *) xmalloc (sizeof *data); - data->first = item; - data->last = item; - } - else - { - data->last->next = item; - data->last = item; - } - - return data; -} - -/* Add a string to an rcdata resource. */ +/* Create an rcdata item holding a string. */ -struct rcdata_data * -append_rcdata_string (data, string) - struct rcdata_data *data; - char *string; +struct rcdata_item * +define_rcdata_string (string, len) + const char *string; + unsigned long len; { struct rcdata_item *ri; + char *s; - ri = (struct rcdata_item *) xmalloc (sizeof *ri); + ri = (struct rcdata_item *) res_alloc (sizeof *ri); ri->next = NULL; ri->type = RCDATA_STRING; - ri->u.string = string; + ri->u.string.length = len; + s = (char *) res_alloc (len); + memcpy (s, string, len); + ri->u.string.s = s; - return append_rcdata_item (data, ri); + return ri; } -/* Add a number to an rcdata resource. */ +/* Create an rcdata item holding a number. */ -struct rcdata_data * -append_rcdata_number (data, val, dword) - struct rcdata_data *data; +struct rcdata_item * +define_rcdata_number (val, dword) unsigned long val; int dword; { struct rcdata_item *ri; - ri = (struct rcdata_item *) xmalloc (sizeof *ri); + ri = (struct rcdata_item *) res_alloc (sizeof *ri); ri->next = NULL; ri->type = dword ? RCDATA_DWORD : RCDATA_WORD; ri->u.word = val; - return append_rcdata_item (data, ri); + return ri; } /* Define a stringtable resource. This is called for each string @@ -881,7 +884,7 @@ void define_stringtable (resinfo, stringid, string) const struct res_res_info *resinfo; unsigned long stringid; - char *string; + const char *string; { struct res_id id; struct res_resource *r; @@ -897,7 +900,7 @@ define_stringtable (resinfo, stringid, string) r->type = RES_TYPE_STRINGTABLE; r->u.stringtable = ((struct stringtable *) - xmalloc (sizeof (struct stringtable))); + res_alloc (sizeof (struct stringtable))); for (i = 0; i < 16; i++) { r->u.stringtable->strings[i].length = 0; @@ -910,7 +913,6 @@ define_stringtable (resinfo, stringid, string) unicode_from_ascii (&r->u.stringtable->strings[stringid & 0xf].length, &r->u.stringtable->strings[stringid & 0xf].string, string); - free (string); } /* Define a user data resource where the data is in the rc file. */ @@ -920,7 +922,7 @@ define_user_data (id, type, resinfo, data) struct res_id id; struct res_id type; const struct res_res_info *resinfo; - struct rcdata_data *data; + struct rcdata_item *data; { struct res_id ids[3]; struct res_resource *r; @@ -958,7 +960,7 @@ define_user_file (id, type, resinfo, filename) fatal ("stat failed on bitmap file `%s': %s", real_filename, strerror (errno)); - data = (unsigned char *) xmalloc (s.st_size); + data = (unsigned char *) res_alloc (s.st_size); get_data (e, data, s.st_size, real_filename); @@ -972,15 +974,12 @@ define_user_file (id, type, resinfo, filename) r = define_resource (&resources, 3, ids, 0); r->type = RES_TYPE_USERDATA; - r->u.userdata = ((struct rcdata_data *) - xmalloc (sizeof (struct rcdata_data))); - r->u.userdata->first = ((struct rcdata_item *) - xmalloc (sizeof (struct rcdata_item))); - r->u.userdata->last = r->u.userdata->first; - r->u.userdata->first->next = NULL; - r->u.userdata->first->type = RCDATA_BUFFER; - r->u.userdata->first->u.buffer.length = s.st_size; - r->u.userdata->first->u.buffer.data = data; + r->u.userdata = ((struct rcdata_item *) + res_alloc (sizeof (struct rcdata_item))); + r->u.userdata->next = NULL; + r->u.userdata->type = RCDATA_BUFFER; + r->u.userdata->u.buffer.length = s.st_size; + r->u.userdata->u.buffer.data = data; r->res_info = *resinfo; } @@ -998,7 +997,7 @@ define_versioninfo (id, language, fixedverinfo, verinfo) r = define_standard_resource (&resources, RT_VERSION, id, language, 0); r->type = RES_TYPE_VERSIONINFO; r->u.versioninfo = ((struct versioninfo *) - xmalloc (sizeof (struct versioninfo))); + res_alloc (sizeof (struct versioninfo))); r->u.versioninfo->fixed = fixedverinfo; r->u.versioninfo->var = verinfo; r->res_info.language = language; @@ -1009,17 +1008,15 @@ define_versioninfo (id, language, fixedverinfo, verinfo) struct ver_info * append_ver_stringfileinfo (verinfo, language, strings) struct ver_info *verinfo; - char *language; + const char *language; struct ver_stringinfo *strings; { struct ver_info *vi, **pp; - vi = (struct ver_info *) xmalloc (sizeof *vi); + vi = (struct ver_info *) res_alloc (sizeof *vi); vi->next = NULL; vi->type = VERINFO_STRING; - unicode_from_ascii ((unsigned short *) NULL, &vi->u.string.language, - language); - free (language); + unicode_from_ascii ((int *) NULL, &vi->u.string.language, language); vi->u.string.strings = strings; for (pp = &verinfo; *pp != NULL; pp = &(*pp)->next) @@ -1034,16 +1031,15 @@ append_ver_stringfileinfo (verinfo, language, strings) struct ver_info * append_ver_varfileinfo (verinfo, key, var) struct ver_info *verinfo; - char *key; + const char *key; struct ver_varinfo *var; { struct ver_info *vi, **pp; - vi = (struct ver_info *) xmalloc (sizeof *vi); + vi = (struct ver_info *) res_alloc (sizeof *vi); vi->next = NULL; vi->type = VERINFO_VAR; - unicode_from_ascii ((unsigned short *) NULL, &vi->u.var.key, key); - free (key); + unicode_from_ascii ((int *) NULL, &vi->u.var.key, key); vi->u.var.var = var; for (pp = &verinfo; *pp != NULL; pp = &(*pp)->next) @@ -1058,17 +1054,15 @@ append_ver_varfileinfo (verinfo, key, var) struct ver_stringinfo * append_verval (strings, key, value) struct ver_stringinfo *strings; - char *key; - char *value; + const char *key; + const char *value; { struct ver_stringinfo *vs, **pp; - vs = (struct ver_stringinfo *) xmalloc (sizeof *vs); + vs = (struct ver_stringinfo *) res_alloc (sizeof *vs); vs->next = NULL; - unicode_from_ascii ((unsigned short *) NULL, &vs->key, key); - free (key); - unicode_from_ascii ((unsigned short *) NULL, &vs->value, value); - free (value); + unicode_from_ascii ((int *) NULL, &vs->key, key); + unicode_from_ascii ((int *) NULL, &vs->value, value); for (pp = &strings; *pp != NULL; pp = &(*pp)->next) ; @@ -1087,7 +1081,7 @@ append_vertrans (var, language, charset) { struct ver_varinfo *vv, **pp; - vv = (struct ver_varinfo *) xmalloc (sizeof *vv); + vv = (struct ver_varinfo *) res_alloc (sizeof *vv); vv->next = NULL; vv->language = language; vv->charset = charset; @@ -1121,8 +1115,10 @@ static void write_rc_dialog_control PARAMS ((FILE *, const struct dialog_control *)); static void write_rc_fontdir PARAMS ((FILE *, const struct fontdir *)); static void write_rc_group_icon PARAMS ((FILE *, const struct group_icon *)); -static void write_rc_menu PARAMS ((FILE *, const struct menuitem *, int, int)); -static void write_rc_rcdata PARAMS ((FILE *, const struct rcdata_data *, int)); +static void write_rc_menu PARAMS ((FILE *, const struct menu *, int)); +static void write_rc_menuitems + PARAMS ((FILE *, const struct menuitem *, int, int)); +static void write_rc_rcdata PARAMS ((FILE *, const struct rcdata_item *, int)); static void write_rc_stringtable PARAMS ((FILE *, const struct res_id *, const struct stringtable *)); static void write_rc_versioninfo PARAMS ((FILE *, const struct versioninfo *)); @@ -1565,7 +1561,7 @@ write_rc_resource (e, type, name, res, language) break; case RES_TYPE_MENU: - write_rc_menu (e, res->u.menu, menuex, 0); + write_rc_menu (e, res->u.menu, menuex); break; case RES_TYPE_RCDATA: @@ -1697,7 +1693,11 @@ write_rc_dialog (e, dialog) fprintf (e, "\n"); } if (dialog->caption != NULL) - fprintf (e, "CAPTION \"%s\"\n", dialog->caption); + { + fprintf (e, "CAPTION \""); + unicode_print (e, dialog->caption, -1); + fprintf (e, "\"\n"); + } if (dialog->menu.named || dialog->menu.u.id != 0) { fprintf (e, "MENU "); @@ -1706,7 +1706,9 @@ write_rc_dialog (e, dialog) } if (dialog->font != NULL) { - fprintf (e, "FONT %d, \"%s\"", dialog->pointsize, dialog->font); + fprintf (e, "FONT %d, \"", dialog->pointsize); + unicode_print (e, dialog->font, -1); + fprintf (e, "\""); if (dialog->ex != NULL && (dialog->ex->weight != 0 || dialog->ex->italic != 0)) fprintf (e, ", %d, %d", dialog->ex->weight, dialog->ex->italic); @@ -1863,7 +1865,20 @@ write_rc_group_icon (e, group_icon) /* Write out a menu resource. */ static void -write_rc_menu (e, menuitems, menuex, ind) +write_rc_menu (e, menu, menuex) + FILE *e; + const struct menu *menu; + int menuex; +{ + if (menu->help != 0) + fprintf (e, "// Help ID: %lu\n", menu->help); + write_rc_menuitems (e, menu->items, menuex, 0); +} + +/* Write out menuitems. */ + +static void +write_rc_menuitems (e, menuitems, menuex, ind) FILE *e; const struct menuitem *menuitems; int menuex; @@ -1896,7 +1911,11 @@ write_rc_menu (e, menuitems, menuex, ind) if (mi->text == NULL) fprintf (e, " \"\""); else - fprintf (e, " \"%s\"", mi->text); + { + fprintf (e, " \""); + unicode_print (e, mi->text, -1); + fprintf (e, "\""); + } if (! menuex) { @@ -1937,7 +1956,7 @@ write_rc_menu (e, menuitems, menuex, ind) fprintf (e, "\n"); if (mi->popup != NULL) - write_rc_menu (e, mi->popup, menuex, ind + 2); + write_rc_menuitems (e, mi->popup, menuex, ind + 2); } indent (e, ind); @@ -1950,7 +1969,7 @@ write_rc_menu (e, menuitems, menuex, ind) static void write_rc_rcdata (e, rcdata, ind) FILE *e; - const struct rcdata_data *rcdata; + const struct rcdata_item *rcdata; int ind; { const struct rcdata_item *ri; @@ -1958,7 +1977,7 @@ write_rc_rcdata (e, rcdata, ind) indent (e, ind); fprintf (e, "BEGIN\n"); - for (ri = rcdata->first; ri != NULL; ri = ri->next) + for (ri = rcdata; ri != NULL; ri = ri->next) { if (ri->type == RCDATA_BUFFER && ri->u.buffer.length == 0) continue; @@ -1979,12 +1998,26 @@ write_rc_rcdata (e, rcdata, ind) break; case RCDATA_STRING: - fprintf (e, "\"%s\"", ri->u.string); - break; + { + const char *s; + unsigned long i; + + fprintf (e, "\""); + s = ri->u.string.s; + for (i = 0; i < ri->u.string.length; i++) + { + if (isprint (*s)) + putc (*s, e); + else + fprintf (e, "\\%03o", *s); + } + fprintf (e, "\""); + break; + } case RCDATA_WSTRING: fprintf (e, "L\""); - unicode_print (e, ri->u.wstring, -1); + unicode_print (e, ri->u.wstring.w, ri->u.wstring.length); fprintf (e, "\""); break; diff --git a/binutils/windres.c b/binutils/windres.c index bd17aaf..3134b3d 100644 --- a/binutils/windres.c +++ b/binutils/windres.c @@ -39,6 +39,7 @@ #include "getopt.h" #include "bucomm.h" #include "libiberty.h" +#include "obstack.h" #include "windres.h" #include <assert.h> @@ -127,9 +128,62 @@ static const struct option long_options[] = /* Static functions. */ +static void res_init PARAMS ((void)); +static int extended_menuitems PARAMS ((const struct menuitem *)); static enum res_format format_from_name PARAMS ((const char *)); static enum res_format format_from_filename PARAMS ((const char *, int)); static void usage PARAMS ((FILE *, int)); +static int cmp_res_entry PARAMS ((const PTR, const PTR)); +static struct res_directory *sort_resources PARAMS ((struct res_directory *)); + +/* When we are building a resource tree, we allocate everything onto + an obstack, so that we can free it all at once if we want. */ + +#define obstack_chunk_alloc xmalloc +#define obstack_chunk_free free + +/* The resource building obstack. */ + +static struct obstack res_obstack; + +/* Initialize the resource building obstack. */ + +static void +res_init () +{ + obstack_init (&res_obstack); +} + +/* Allocate space on the resource building obstack. */ + +PTR +res_alloc (bytes) + size_t bytes; +{ + return (PTR) obstack_alloc (&res_obstack, bytes); +} + +/* We also use an obstack to save memory used while writing out a set + of resources. */ + +static struct obstack reswr_obstack; + +/* Initialize the resource writing obstack. */ + +static void +reswr_init () +{ + obstack_init (&reswr_obstack); +} + +/* Allocate space on the resource writing obstack. */ + +PTR +reswr_alloc (bytes) + size_t bytes; +{ + return (PTR) obstack_alloc (&reswr_obstack, bytes); +} /* Open a file using the include directory search list. */ @@ -183,8 +237,8 @@ open_file_search (filename, mode, errmsg, real_filename) void unicode_from_ascii (length, unicode, ascii) - unsigned short *length; - unsigned short **unicode; + int *length; + unichar **unicode; const char *ascii; { int len; @@ -194,13 +248,9 @@ unicode_from_ascii (length, unicode, ascii) len = strlen (ascii); if (length != NULL) - { - if (len > 0xffff) - fatal ("string too long (%d chars > 0xffff)", len); - *length = len; - } + *length = len; - *unicode = (unsigned short *) xmalloc ((len + 1) * sizeof (unsigned short)); + *unicode = ((unichar *) res_alloc ((len + 1) * sizeof (unichar))); for (s = ascii, w = *unicode; *s != '\0'; s++, w++) *w = *s & 0xff; @@ -214,12 +264,12 @@ unicode_from_ascii (length, unicode, ascii) void unicode_print (e, unicode, length) FILE *e; - const unsigned short *unicode; + const unichar *unicode; int length; { while (1) { - unsigned short ch; + unichar ch; if (length == 0) return; @@ -228,7 +278,7 @@ unicode_print (e, unicode, length) ch = *unicode; - if (ch == 0) + if (ch == 0 && length < 0) return; ++unicode; @@ -264,7 +314,7 @@ res_id_cmp (a, b) } else { - unsigned short *as, *ase, *bs, *bse; + unichar *as, *ase, *bs, *bse; if (! b.named) return -1; @@ -368,7 +418,8 @@ define_resource (resources, cids, ids, dupok) if (*resources == NULL) { - *resources = (struct res_directory *) xmalloc (sizeof **resources); + *resources = ((struct res_directory *) + res_alloc (sizeof **resources)); (*resources)->characteristics = 0; (*resources)->time = 0; (*resources)->major = 0; @@ -384,7 +435,7 @@ define_resource (resources, cids, ids, dupok) re = *pp; else { - re = (struct res_entry *) xmalloc (sizeof *re); + re = (struct res_entry *) res_alloc (sizeof *re); re->next = NULL; re->id = ids[i]; if ((i + 1) < cids) @@ -433,7 +484,8 @@ define_resource (resources, cids, ids, dupok) fprintf (stderr, ": duplicate value\n"); } - re->u.res = (struct res_resource *) xmalloc (sizeof (struct res_resource)); + re->u.res = ((struct res_resource *) + res_alloc (sizeof (struct res_resource))); re->u.res->type = RES_TYPE_UNINITIALIZED; memset (&re->u.res->res_info, 0, sizeof (struct res_res_info)); @@ -462,6 +514,62 @@ define_standard_resource (resources, type, name, language, dupok) a[2].u.id = language; return define_resource (resources, 3, a, dupok); } + +/* Comparison routine for resource sorting. */ + +static int +cmp_res_entry (p1, p2) + const PTR p1; + const PTR p2; +{ + const struct res_entry **re1, **re2; + + re1 = (const struct res_entry **) p1; + re2 = (const struct res_entry **) p2; + return res_id_cmp ((*re1)->id, (*re2)->id); +} + +/* Sort the resources. */ + +static struct res_directory * +sort_resources (resdir) + struct res_directory *resdir; +{ + int c, i; + struct res_entry *re; + struct res_entry **a; + + if (resdir->entries == NULL) + return resdir; + + c = 0; + for (re = resdir->entries; re != NULL; re = re->next) + ++c; + + /* This is a recursive routine, so using xmalloc is probably better + than alloca. */ + a = (struct res_entry **) xmalloc (c * sizeof (struct res_entry *)); + + for (i = 0, re = resdir->entries; re != NULL; re = re->next, i++) + a[i] = re; + + qsort (a, c, sizeof (struct res_entry *), cmp_res_entry); + + resdir->entries = a[0]; + for (i = 0; i < c - 1; i++) + a[i]->next = a[i + 1]; + a[i]->next = NULL; + + free (a); + + /* Now sort the subdirectories. */ + + for (re = resdir->entries; re != NULL; re = re->next) + if (re->subdir) + re->u.dir = sort_resources (re->u.dir); + + return resdir; +} /* Return whether the dialog resource DIALOG is a DIALOG or a DIALOGEX. */ @@ -485,7 +593,14 @@ extended_dialog (dialog) /* Return whether MENUITEMS are a MENU or a MENUEX. */ int -extended_menu (menuitems) +extended_menu (menu) + const struct menu *menu; +{ + return extended_menuitems (menu->items); +} + +static int +extended_menuitems (menuitems) const struct menuitem *menuitems; { const struct menuitem *mi; @@ -507,7 +622,7 @@ extended_menu (menuitems) return 1; if (mi->popup != NULL) { - if (extended_menu (mi->popup)) + if (extended_menuitems (mi->popup)) return 1; } } @@ -685,6 +800,8 @@ main (argc, argv) bfd_init (); set_default_bfd_target (); + res_init (); + input_filename = NULL; output_filename = NULL; input_format = RES_FORMAT_UNKNOWN; @@ -843,8 +960,15 @@ main (argc, argv) break; } + /* Sort the resources. This is required for COFF, convenient for + rc, and unimportant for res. */ + + resources = sort_resources (resources); + /* Write the output file. */ + reswr_init (); + switch (output_format) { default: @@ -879,12 +1003,3 @@ write_res_file (filename, resources) { fatal ("write_res_file unimplemented"); } - -void -write_coff_file (filename, target, resources) - const char *filename; - const char *target; - const struct res_directory *resources; -{ - fatal ("write_coff_file unimplemented"); -} |