1 /* resbin.c -- manipulate the Windows binary resource format.
2 Copyright 1997, 1998, 1999, 2002, 2003, 2005, 2006, 2007, 2009, 2010, 2011
3 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor, Cygnus Support.
5 Rewritten by Kai Tietz, Onevision.
7 This file is part of GNU Binutils.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
25 /* This file contains functions to convert between the binary resource
26 format and the internal structures that we want to use. The same
27 binary resource format is used in both res and COFF files. */
32 #include "libiberty.h"
35 /* Local functions. */
37 static void toosmall (const char *);
39 static unichar
*get_unicode (windres_bfd
*, const bfd_byte
*, rc_uint_type
, rc_uint_type
*);
40 static int get_resid (windres_bfd
*, rc_res_id
*, const bfd_byte
*, rc_uint_type
);
41 static rc_res_resource
*bin_to_res_generic (windres_bfd
*, enum rc_res_type
,
42 const bfd_byte
*, rc_uint_type
);
43 static rc_res_resource
*bin_to_res_cursor (windres_bfd
*, const bfd_byte
*, rc_uint_type
);
44 static rc_res_resource
*bin_to_res_menu (windres_bfd
*,const bfd_byte
*, rc_uint_type
);
45 static rc_menuitem
*bin_to_res_menuitems (windres_bfd
*, const bfd_byte
*, rc_uint_type
,
47 static rc_menuitem
*bin_to_res_menuexitems (windres_bfd
*, const bfd_byte
*, rc_uint_type
,
49 static rc_res_resource
*bin_to_res_dialog (windres_bfd
*, const bfd_byte
*, rc_uint_type
);
50 static rc_res_resource
*bin_to_res_string (windres_bfd
*,const bfd_byte
*, rc_uint_type
);
51 static rc_res_resource
*bin_to_res_fontdir (windres_bfd
*, const bfd_byte
*, rc_uint_type
);
52 static rc_res_resource
*bin_to_res_accelerators (windres_bfd
*, const bfd_byte
*, rc_uint_type
);
53 static rc_res_resource
*bin_to_res_rcdata (windres_bfd
*, const bfd_byte
*, rc_uint_type
, int);
54 static rc_res_resource
*bin_to_res_group_cursor (windres_bfd
*, const bfd_byte
*, rc_uint_type
);
55 static rc_res_resource
*bin_to_res_group_icon (windres_bfd
*, const bfd_byte
*, rc_uint_type
);
56 static rc_res_resource
*bin_to_res_version (windres_bfd
*, const bfd_byte
*, rc_uint_type
);
57 static rc_res_resource
*bin_to_res_userdata (windres_bfd
*, const bfd_byte
*, rc_uint_type
);
58 static rc_res_resource
*bin_to_res_toolbar (windres_bfd
*, const bfd_byte
*, rc_uint_type
);
59 static void get_version_header (windres_bfd
*, const bfd_byte
*, rc_uint_type
, const char *,
60 unichar
**, rc_uint_type
*, rc_uint_type
*, rc_uint_type
*,
63 /* Given a resource type ID, a pointer to data, a length, return a
64 rc_res_resource structure which represents that resource. The caller
65 is responsible for initializing the res_info and coff_info fields
66 of the returned structure. */
69 bin_to_res (windres_bfd
*wrbfd
, rc_res_id type
, const bfd_byte
*data
,
73 return bin_to_res_userdata (wrbfd
, data
, length
);
79 return bin_to_res_userdata (wrbfd
, data
, length
);
81 return bin_to_res_cursor (wrbfd
, data
, length
);
83 return bin_to_res_generic (wrbfd
, RES_TYPE_BITMAP
, data
, length
);
85 return bin_to_res_generic (wrbfd
, RES_TYPE_ICON
, data
, length
);
87 return bin_to_res_menu (wrbfd
, data
, length
);
89 return bin_to_res_dialog (wrbfd
, data
, length
);
91 return bin_to_res_string (wrbfd
, data
, length
);
93 return bin_to_res_fontdir (wrbfd
, data
, length
);
95 return bin_to_res_generic (wrbfd
, RES_TYPE_FONT
, data
, length
);
97 return bin_to_res_accelerators (wrbfd
, data
, length
);
99 return bin_to_res_rcdata (wrbfd
, data
, length
, RES_TYPE_RCDATA
);
100 case RT_MESSAGETABLE
:
101 return bin_to_res_generic (wrbfd
, RES_TYPE_MESSAGETABLE
, data
, length
);
102 case RT_GROUP_CURSOR
:
103 return bin_to_res_group_cursor (wrbfd
, data
, length
);
105 return bin_to_res_group_icon (wrbfd
, data
, length
);
107 return bin_to_res_version (wrbfd
, data
, length
);
109 return bin_to_res_toolbar (wrbfd
, data
, length
);
115 /* Give an error if the binary data is too small. */
118 toosmall (const char *msg
)
120 fatal (_("%s: not enough binary data"), msg
);
123 /* Swap in a NULL terminated unicode string. */
126 get_unicode (windres_bfd
*wrbfd
, const bfd_byte
*data
, rc_uint_type length
,
127 rc_uint_type
*retlen
)
135 if (length
< c
* 2 + 2)
136 toosmall (_("null terminated unicode string"));
137 if (windres_get_16 (wrbfd
, data
+ c
* 2, 2) == 0)
142 ret
= (unichar
*) res_alloc ((c
+ 1) * sizeof (unichar
));
144 for (i
= 0; i
< c
; i
++)
145 ret
[i
] = windres_get_16 (wrbfd
, data
+ i
* 2, 2);
154 /* Get a resource identifier. This returns the number of bytes used. */
157 get_resid (windres_bfd
*wrbfd
, rc_res_id
*id
, const bfd_byte
*data
,
163 toosmall (_("resource ID"));
165 first
= windres_get_16 (wrbfd
, data
, 2);
169 toosmall (_("resource ID"));
171 id
->u
.id
= windres_get_16 (wrbfd
, data
+ 2, 2);
177 id
->u
.n
.name
= get_unicode (wrbfd
, data
, length
, &id
->u
.n
.length
);
178 return id
->u
.n
.length
* 2 + 2;
182 /* Convert a resource which just stores uninterpreted data from
186 bin_to_res_generic (windres_bfd
*wrbfd ATTRIBUTE_UNUSED
, enum rc_res_type type
,
187 const bfd_byte
*data
, rc_uint_type length
)
191 r
= (rc_res_resource
*) res_alloc (sizeof (rc_res_resource
));
193 r
->u
.data
.data
= data
;
194 r
->u
.data
.length
= length
;
199 /* Convert a cursor resource from binary. */
202 bin_to_res_cursor (windres_bfd
*wrbfd
, const bfd_byte
*data
, rc_uint_type length
)
208 toosmall (_("cursor"));
210 c
= (rc_cursor
*) res_alloc (sizeof (rc_cursor
));
211 c
->xhotspot
= windres_get_16 (wrbfd
, data
, 2);
212 c
->yhotspot
= windres_get_16 (wrbfd
, data
+ 2, 2);
213 c
->length
= length
- 4;
216 r
= (rc_res_resource
*) res_alloc (sizeof *r
);
217 r
->type
= RES_TYPE_CURSOR
;
223 /* Convert a menu resource from binary. */
226 bin_to_res_menu (windres_bfd
*wrbfd
, const bfd_byte
*data
, rc_uint_type length
)
230 rc_uint_type version
, got
;
232 r
= (rc_res_resource
*) res_alloc (sizeof *r
);
233 r
->type
= RES_TYPE_MENU
;
235 m
= (rc_menu
*) res_alloc (sizeof (rc_menu
));
239 toosmall (_("menu header"));
241 version
= windres_get_16 (wrbfd
, data
, 2);
246 toosmall (_("menu header"));
248 m
->items
= bin_to_res_menuitems (wrbfd
, data
+ 4, length
- 4, &got
);
250 else if (version
== 1)
255 toosmall (_("menuex header"));
256 m
->help
= windres_get_32 (wrbfd
, data
+ 4, 4);
257 offset
= windres_get_16 (wrbfd
, data
+ 2, 2);
258 if (offset
+ 4 >= length
)
259 toosmall (_("menuex offset"));
260 m
->items
= bin_to_res_menuexitems (wrbfd
, data
+ 4 + offset
,
261 length
- (4 + offset
), &got
);
264 fatal (_("unsupported menu version %d"), (int) version
);
269 /* Convert menu items from binary. */
272 bin_to_res_menuitems (windres_bfd
*wrbfd
, const bfd_byte
*data
, rc_uint_type length
,
275 rc_menuitem
*first
, **pp
;
284 rc_uint_type flags
, slen
, itemlen
;
289 toosmall (_("menuitem header"));
291 mi
= (rc_menuitem
*) res_alloc (sizeof *mi
);
295 flags
= windres_get_16 (wrbfd
, data
, 2);
296 mi
->type
= flags
&~ (MENUITEM_POPUP
| MENUITEM_ENDMENU
);
298 if ((flags
& MENUITEM_POPUP
) == 0)
303 if (length
< stroff
+ 2)
304 toosmall (_("menuitem header"));
306 if (windres_get_16 (wrbfd
, data
+ stroff
, 2) == 0)
312 mi
->text
= get_unicode (wrbfd
, data
+ stroff
, length
- stroff
, &slen
);
314 itemlen
= stroff
+ slen
* 2 + 2;
316 if ((flags
& MENUITEM_POPUP
) == 0)
319 mi
->id
= windres_get_16 (wrbfd
, data
+ 2, 2);
323 rc_uint_type subread
;
326 mi
->popup
= bin_to_res_menuitems (wrbfd
, data
+ itemlen
, length
- itemlen
,
339 if ((flags
& MENUITEM_ENDMENU
) != 0)
346 /* Convert menuex items from binary. */
349 bin_to_res_menuexitems (windres_bfd
*wrbfd
, const bfd_byte
*data
, rc_uint_type length
,
352 rc_menuitem
*first
, **pp
;
361 rc_uint_type flags
, slen
;
362 rc_uint_type itemlen
;
366 toosmall (_("menuitem header"));
368 mi
= (rc_menuitem
*) res_alloc (sizeof (rc_menuitem
));
369 mi
->type
= windres_get_32 (wrbfd
, data
, 4);
370 mi
->state
= windres_get_32 (wrbfd
, data
+ 4, 4);
371 mi
->id
= windres_get_32 (wrbfd
, data
+ 8, 4);
373 flags
= windres_get_16 (wrbfd
, data
+ 12, 2);
375 if (windres_get_16 (wrbfd
, data
+ 14, 2) == 0)
381 mi
->text
= get_unicode (wrbfd
, data
+ 14, length
- 14, &slen
);
383 itemlen
= 14 + slen
* 2 + 2;
384 itemlen
= (itemlen
+ 3) &~ 3;
386 if ((flags
& 1) == 0)
393 rc_uint_type subread
;
395 if (length
< itemlen
+ 4)
396 toosmall (_("menuitem"));
397 mi
->help
= windres_get_32 (wrbfd
, data
+ itemlen
, 4);
400 mi
->popup
= bin_to_res_menuexitems (wrbfd
, data
+ itemlen
,
401 length
- itemlen
, &subread
);
413 if ((flags
& 0x80) != 0)
420 /* Convert a dialog resource from binary. */
422 static rc_res_resource
*
423 bin_to_res_dialog (windres_bfd
*wrbfd
, const bfd_byte
*data
, rc_uint_type length
)
425 rc_uint_type signature
;
427 rc_uint_type c
, sublen
, i
;
429 rc_dialog_control
**pp
;
433 toosmall (_("dialog header"));
435 d
= (rc_dialog
*) res_alloc (sizeof (rc_dialog
));
437 signature
= windres_get_16 (wrbfd
, data
+ 2, 2);
438 if (signature
!= 0xffff)
441 d
->style
= windres_get_32 (wrbfd
, data
, 4);
442 d
->exstyle
= windres_get_32 (wrbfd
, data
+ 4, 4);
449 version
= windres_get_16 (wrbfd
, data
, 2);
451 fatal (_("unexpected DIALOGEX version %d"), version
);
453 d
->ex
= (rc_dialog_ex
*) res_alloc (sizeof (rc_dialog_ex
));
454 d
->ex
->help
= windres_get_32 (wrbfd
, data
+ 4, 4);
455 d
->exstyle
= windres_get_32 (wrbfd
, data
+ 8, 4);
456 d
->style
= windres_get_32 (wrbfd
, data
+ 12, 4);
460 if (length
< off
+ 10)
461 toosmall (_("dialog header"));
463 c
= windres_get_16 (wrbfd
, data
+ off
, 2);
464 d
->x
= windres_get_16 (wrbfd
, data
+ off
+ 2, 2);
465 d
->y
= windres_get_16 (wrbfd
, data
+ off
+ 4, 2);
466 d
->width
= windres_get_16 (wrbfd
, data
+ off
+ 6, 2);
467 d
->height
= windres_get_16 (wrbfd
, data
+ off
+ 8, 2);
471 sublen
= get_resid (wrbfd
, &d
->menu
, data
+ off
, length
- off
);
474 sublen
= get_resid (wrbfd
, &d
->class, data
+ off
, length
- off
);
477 d
->caption
= get_unicode (wrbfd
, data
+ off
, length
- off
, &sublen
);
478 off
+= sublen
* 2 + 2;
482 if ((d
->style
& DS_SETFONT
) == 0)
490 d
->ex
->charset
= 1; /* Default charset. */
495 if (length
< off
+ 2)
496 toosmall (_("dialog font point size"));
498 d
->pointsize
= windres_get_16 (wrbfd
, data
+ off
, 2);
503 if (length
< off
+ 4)
504 toosmall (_("dialogex font information"));
505 d
->ex
->weight
= windres_get_16 (wrbfd
, data
+ off
, 2);
506 d
->ex
->italic
= windres_get_8 (wrbfd
, data
+ off
+ 2, 1);
507 d
->ex
->charset
= windres_get_8 (wrbfd
, data
+ off
+ 3, 1);
511 d
->font
= get_unicode (wrbfd
, data
+ off
, length
- off
, &sublen
);
512 off
+= sublen
* 2 + 2;
518 for (i
= 0; i
< c
; i
++)
520 rc_dialog_control
*dc
;
523 off
= (off
+ 3) &~ 3;
525 dc
= (rc_dialog_control
*) res_alloc (sizeof (rc_dialog_control
));
529 if (length
< off
+ 8)
530 toosmall (_("dialog control"));
532 dc
->style
= windres_get_32 (wrbfd
, data
+ off
, 4);
533 dc
->exstyle
= windres_get_32 (wrbfd
, data
+ off
+ 4, 4);
539 if (length
< off
+ 12)
540 toosmall (_("dialogex control"));
541 dc
->help
= windres_get_32 (wrbfd
, data
+ off
, 4);
542 dc
->exstyle
= windres_get_32 (wrbfd
, data
+ off
+ 4, 4);
543 dc
->style
= windres_get_32 (wrbfd
, data
+ off
+ 8, 4);
547 if (length
< off
+ (d
->ex
!= NULL
? 2 : 0) + 10)
548 toosmall (_("dialog control"));
550 dc
->x
= windres_get_16 (wrbfd
, data
+ off
, 2);
551 dc
->y
= windres_get_16 (wrbfd
, data
+ off
+ 2, 2);
552 dc
->width
= windres_get_16 (wrbfd
, data
+ off
+ 4, 2);
553 dc
->height
= windres_get_16 (wrbfd
, data
+ off
+ 6, 2);
556 dc
->id
= windres_get_32 (wrbfd
, data
+ off
+ 8, 4);
558 dc
->id
= windres_get_16 (wrbfd
, data
+ off
+ 8, 2);
560 off
+= 10 + (d
->ex
!= NULL
? 2 : 0);
562 sublen
= get_resid (wrbfd
, &dc
->class, data
+ off
, length
- off
);
565 sublen
= get_resid (wrbfd
, &dc
->text
, data
+ off
, length
- off
);
568 if (length
< off
+ 2)
569 toosmall (_("dialog control end"));
571 datalen
= windres_get_16 (wrbfd
, data
+ off
, 2);
578 off
= (off
+ 3) &~ 3;
580 if (length
< off
+ datalen
)
581 toosmall (_("dialog control data"));
583 dc
->data
= ((rc_rcdata_item
*)
584 res_alloc (sizeof (rc_rcdata_item
)));
585 dc
->data
->next
= NULL
;
586 dc
->data
->type
= RCDATA_BUFFER
;
587 dc
->data
->u
.buffer
.length
= datalen
;
588 dc
->data
->u
.buffer
.data
= data
+ off
;
598 r
= (rc_res_resource
*) res_alloc (sizeof *r
);
599 r
->type
= RES_TYPE_DIALOG
;
605 /* Convert a stringtable resource from binary. */
607 static rc_res_resource
*
608 bin_to_res_string (windres_bfd
*wrbfd
, const bfd_byte
*data
, rc_uint_type length
)
614 st
= (rc_stringtable
*) res_alloc (sizeof (rc_stringtable
));
616 for (i
= 0; i
< 16; i
++)
621 toosmall (_("stringtable string length"));
622 slen
= windres_get_16 (wrbfd
, data
, 2);
623 st
->strings
[i
].length
= slen
;
630 if (length
< 2 + 2 * slen
)
631 toosmall (_("stringtable string"));
633 s
= (unichar
*) res_alloc (slen
* sizeof (unichar
));
634 st
->strings
[i
].string
= s
;
636 for (j
= 0; j
< slen
; j
++)
637 s
[j
] = windres_get_16 (wrbfd
, data
+ 2 + j
* 2, 2);
640 data
+= 2 + 2 * slen
;
641 length
-= 2 + 2 * slen
;
644 r
= (rc_res_resource
*) res_alloc (sizeof *r
);
645 r
->type
= RES_TYPE_STRINGTABLE
;
646 r
->u
.stringtable
= st
;
651 /* Convert a fontdir resource from binary. */
653 static rc_res_resource
*
654 bin_to_res_fontdir (windres_bfd
*wrbfd
, const bfd_byte
*data
, rc_uint_type length
)
657 rc_fontdir
*first
, **pp
;
661 toosmall (_("fontdir header"));
663 c
= windres_get_16 (wrbfd
, data
, 2);
668 for (i
= 0; i
< c
; i
++)
670 const struct bin_fontdir_item
*bfi
;
675 toosmall (_("fontdir"));
677 bfi
= (const struct bin_fontdir_item
*) data
;
678 fd
= (rc_fontdir
*) res_alloc (sizeof *fd
);
679 fd
->index
= windres_get_16 (wrbfd
, bfi
->index
, 2);
681 /* To work out the length of the fontdir data, we must get the
682 length of the device name and face name strings, even though
683 we don't store them in the rc_fontdir. The
684 documentation says that these are NULL terminated char
685 strings, not Unicode strings. */
689 while (off
< length
&& data
[off
] != '\0')
692 toosmall (_("fontdir device name"));
695 while (off
< length
&& data
[off
] != '\0')
698 toosmall (_("fontdir face name"));
708 /* The documentation does not indicate that any rounding is
715 r
= (rc_res_resource
*) res_alloc (sizeof *r
);
716 r
->type
= RES_TYPE_FONTDIR
;
717 r
->u
.fontdir
= first
;
722 /* Convert an accelerators resource from binary. */
724 static rc_res_resource
*
725 bin_to_res_accelerators (windres_bfd
*wrbfd
, const bfd_byte
*data
, rc_uint_type length
)
727 rc_accelerator
*first
, **pp
;
738 toosmall (_("accelerator"));
740 a
= (rc_accelerator
*) res_alloc (sizeof (rc_accelerator
));
742 a
->flags
= windres_get_16 (wrbfd
, data
, 2);
743 a
->key
= windres_get_16 (wrbfd
, data
+ 2, 2);
744 a
->id
= windres_get_16 (wrbfd
, data
+ 4, 2);
750 if ((a
->flags
& ACC_LAST
) != 0)
757 r
= (rc_res_resource
*) res_alloc (sizeof (rc_res_resource
));
758 r
->type
= RES_TYPE_ACCELERATOR
;
764 /* Convert an rcdata resource from binary. */
766 static rc_res_resource
*
767 bin_to_res_rcdata (windres_bfd
*wrbfd ATTRIBUTE_UNUSED
, const bfd_byte
*data
,
768 rc_uint_type length
, int rctyp
)
773 ri
= (rc_rcdata_item
*) res_alloc (sizeof (rc_rcdata_item
));
776 ri
->type
= RCDATA_BUFFER
;
777 ri
->u
.buffer
.length
= length
;
778 ri
->u
.buffer
.data
= data
;
780 r
= (rc_res_resource
*) res_alloc (sizeof *r
);
787 /* Convert a group cursor resource from binary. */
789 static rc_res_resource
*
790 bin_to_res_group_cursor (windres_bfd
*wrbfd
, const bfd_byte
*data
, rc_uint_type length
)
793 rc_group_cursor
*first
, **pp
;
797 toosmall (_("group cursor header"));
799 type
= windres_get_16 (wrbfd
, data
+ 2, 2);
801 fatal (_("unexpected group cursor type %d"), type
);
803 c
= windres_get_16 (wrbfd
, data
+ 4, 2);
811 for (i
= 0; i
< c
; i
++)
816 toosmall (_("group cursor"));
818 gc
= (rc_group_cursor
*) res_alloc (sizeof *gc
);
820 gc
->width
= windres_get_16 (wrbfd
, data
, 2);
821 gc
->height
= windres_get_16 (wrbfd
, data
+ 2, 2);
822 gc
->planes
= windres_get_16 (wrbfd
, data
+ 4, 2);
823 gc
->bits
= windres_get_16 (wrbfd
, data
+ 6, 2);
824 gc
->bytes
= windres_get_32 (wrbfd
, data
+ 8, 4);
825 gc
->index
= windres_get_16 (wrbfd
, data
+ 12, 2);
835 r
= (rc_res_resource
*) res_alloc (sizeof (rc_res_resource
));
836 r
->type
= RES_TYPE_GROUP_CURSOR
;
837 r
->u
.group_cursor
= first
;
842 /* Convert a group icon resource from binary. */
844 static rc_res_resource
*
845 bin_to_res_group_icon (windres_bfd
*wrbfd
, const bfd_byte
*data
, rc_uint_type length
)
848 rc_group_icon
*first
, **pp
;
852 toosmall (_("group icon header"));
854 type
= windres_get_16 (wrbfd
, data
+ 2, 2);
856 fatal (_("unexpected group icon type %d"), type
);
858 c
= windres_get_16 (wrbfd
, data
+ 4, 2);
866 for (i
= 0; i
< c
; i
++)
871 toosmall (_("group icon"));
873 gi
= (rc_group_icon
*) res_alloc (sizeof (rc_group_icon
));
875 gi
->width
= windres_get_8 (wrbfd
, data
, 1);
876 gi
->height
= windres_get_8 (wrbfd
, data
+ 1, 1);
877 gi
->colors
= windres_get_8 (wrbfd
, data
+ 2, 1);
878 gi
->planes
= windres_get_16 (wrbfd
, data
+ 4, 2);
879 gi
->bits
= windres_get_16 (wrbfd
, data
+ 6, 2);
880 gi
->bytes
= windres_get_32 (wrbfd
, data
+ 8, 4);
881 gi
->index
= windres_get_16 (wrbfd
, data
+ 12, 2);
891 r
= (rc_res_resource
*) res_alloc (sizeof *r
);
892 r
->type
= RES_TYPE_GROUP_ICON
;
893 r
->u
.group_icon
= first
;
898 /* Extract data from a version header. If KEY is not NULL, then the
899 key must be KEY; otherwise, the key is returned in *PKEY. This
900 sets *LEN to the total length, *VALLEN to the value length, *TYPE
901 to the type, and *OFF to the offset to the children. */
904 get_version_header (windres_bfd
*wrbfd
, const bfd_byte
*data
, rc_uint_type length
,
905 const char *key
, unichar
**pkey
,
906 rc_uint_type
*len
, rc_uint_type
*vallen
, rc_uint_type
*type
,
912 *len
= windres_get_16 (wrbfd
, data
, 2);
913 *vallen
= windres_get_16 (wrbfd
, data
+ 2, 2);
914 *type
= windres_get_16 (wrbfd
, data
+ 4, 2);
925 *pkey
= get_unicode (wrbfd
, data
, length
, &sublen
);
926 *off
+= (sublen
+ 1) * sizeof (unichar
);
934 if (windres_get_16 (wrbfd
, data
, 2) != (bfd_byte
) *key
)
935 fatal (_("unexpected version string"));
948 *off
= (*off
+ 3) &~ 3;
951 /* Convert a version resource from binary. */
953 static rc_res_resource
*
954 bin_to_res_version (windres_bfd
*wrbfd
, const bfd_byte
*data
, rc_uint_type length
)
956 rc_uint_type verlen
, vallen
, type
, off
;
957 rc_fixed_versioninfo
*fi
;
958 rc_ver_info
*first
, **pp
;
962 get_version_header (wrbfd
, data
, length
, "VS_VERSION_INFO",
963 (unichar
**) NULL
, &verlen
, &vallen
, &type
, &off
);
965 if ((unsigned int) verlen
!= length
)
966 fatal (_("version length %d does not match resource length %lu"),
967 (int) verlen
, (unsigned long) length
);
970 fatal (_("unexpected version type %d"), (int) type
);
979 unsigned long signature
, fiv
;
982 fatal (_("unexpected fixed version information length %ld"), (long) vallen
);
985 toosmall (_("fixed version info"));
987 signature
= windres_get_32 (wrbfd
, data
, 4);
988 if (signature
!= 0xfeef04bd)
989 fatal (_("unexpected fixed version signature %lu"), signature
);
991 fiv
= windres_get_32 (wrbfd
, data
+ 4, 4);
992 if (fiv
!= 0 && fiv
!= 0x10000)
993 fatal (_("unexpected fixed version info version %lu"), fiv
);
995 fi
= (rc_fixed_versioninfo
*) res_alloc (sizeof (rc_fixed_versioninfo
));
997 fi
->file_version_ms
= windres_get_32 (wrbfd
, data
+ 8, 4);
998 fi
->file_version_ls
= windres_get_32 (wrbfd
, data
+ 12, 4);
999 fi
->product_version_ms
= windres_get_32 (wrbfd
, data
+ 16, 4);
1000 fi
->product_version_ls
= windres_get_32 (wrbfd
, data
+ 20, 4);
1001 fi
->file_flags_mask
= windres_get_32 (wrbfd
, data
+ 24, 4);
1002 fi
->file_flags
= windres_get_32 (wrbfd
, data
+ 28, 4);
1003 fi
->file_os
= windres_get_32 (wrbfd
, data
+ 32, 4);
1004 fi
->file_type
= windres_get_32 (wrbfd
, data
+ 36, 4);
1005 fi
->file_subtype
= windres_get_32 (wrbfd
, data
+ 40, 4);
1006 fi
->file_date_ms
= windres_get_32 (wrbfd
, data
+ 44, 4);
1007 fi
->file_date_ls
= windres_get_32 (wrbfd
, data
+ 48, 4);
1022 toosmall (_("version var info"));
1024 vi
= (rc_ver_info
*) res_alloc (sizeof (rc_ver_info
));
1026 ch
= windres_get_16 (wrbfd
, data
+ 6, 2);
1030 rc_ver_stringtable
**ppvst
;
1032 vi
->type
= VERINFO_STRING
;
1034 get_version_header (wrbfd
, data
, length
, "StringFileInfo",
1035 (unichar
**) NULL
, &verlen
, &vallen
, &type
,
1039 fatal (_("unexpected stringfileinfo value length %ld"), (long) vallen
);
1044 /* It's convenient to round verlen to a 4 byte alignment,
1045 since we round the subvariables in the loop. */
1047 verlen
= (verlen
+ 3) &~ 3;
1049 vi
->u
.string
.stringtables
= NULL
;
1050 ppvst
= &vi
->u
.string
.stringtables
;
1054 rc_ver_stringtable
*vst
;
1055 rc_uint_type stverlen
;
1056 rc_ver_stringinfo
**ppvs
;
1059 toosmall (_("version stringtable"));
1061 vst
= (rc_ver_stringtable
*) res_alloc (sizeof (rc_ver_stringtable
));
1063 get_version_header (wrbfd
, data
, length
, (const char *) NULL
,
1064 &vst
->language
, &stverlen
, &vallen
, &type
, &off
);
1067 fatal (_("unexpected version stringtable value length %ld"), (long) vallen
);
1073 stverlen
= (stverlen
+ 3) &~ 3;
1075 vst
->strings
= NULL
;
1076 ppvs
= &vst
->strings
;
1078 while (stverlen
> 0)
1080 rc_ver_stringinfo
*vs
;
1081 rc_uint_type sverlen
, vslen
, valoff
;
1084 toosmall (_("version string"));
1086 vs
= (rc_ver_stringinfo
*) res_alloc (sizeof (rc_ver_stringinfo
));
1088 get_version_header (wrbfd
, data
, length
, (const char *) NULL
,
1089 &vs
->key
, &sverlen
, &vallen
, &type
, &off
);
1091 sverlen
= (sverlen
+ 3) &~ 3;
1096 vs
->value
= get_unicode (wrbfd
, data
, length
, &vslen
);
1097 valoff
= vslen
* 2 + 2;
1098 valoff
= (valoff
+ 3) &~ 3;
1100 if (off
+ valoff
!= sverlen
)
1101 fatal (_("unexpected version string length %ld != %ld + %ld"),
1102 (long) sverlen
, (long) off
, (long) valoff
);
1107 if (stverlen
< sverlen
)
1108 fatal (_("unexpected version string length %ld < %ld"),
1109 (long) verlen
, (long) sverlen
);
1110 stverlen
-= sverlen
;
1124 rc_ver_varinfo
**ppvv
;
1126 vi
->type
= VERINFO_VAR
;
1128 get_version_header (wrbfd
, data
, length
, "VarFileInfo",
1129 (unichar
**) NULL
, &verlen
, &vallen
, &type
,
1133 fatal (_("unexpected varfileinfo value length %ld"), (long) vallen
);
1138 get_version_header (wrbfd
, data
, length
, (const char *) NULL
,
1139 &vi
->u
.var
.key
, &verlen
, &vallen
, &type
, &off
);
1144 vi
->u
.var
.var
= NULL
;
1145 ppvv
= &vi
->u
.var
.var
;
1152 toosmall (_("version varfileinfo"));
1154 vv
= (rc_ver_varinfo
*) res_alloc (sizeof (rc_ver_varinfo
));
1156 vv
->language
= windres_get_16 (wrbfd
, data
, 2);
1157 vv
->charset
= windres_get_16 (wrbfd
, data
+ 2, 2);
1167 fatal (_("unexpected version value length %ld"), (long) vallen
);
1173 fatal (_("unexpected version string"));
1180 v
= (rc_versioninfo
*) res_alloc (sizeof (rc_versioninfo
));
1184 r
= (rc_res_resource
*) res_alloc (sizeof *r
);
1185 r
->type
= RES_TYPE_VERSIONINFO
;
1186 r
->u
.versioninfo
= v
;
1191 /* Convert an arbitrary user defined resource from binary. */
1193 static rc_res_resource
*
1194 bin_to_res_userdata (windres_bfd
*wrbfd ATTRIBUTE_UNUSED
, const bfd_byte
*data
,
1195 rc_uint_type length
)
1200 ri
= (rc_rcdata_item
*) res_alloc (sizeof (rc_rcdata_item
));
1203 ri
->type
= RCDATA_BUFFER
;
1204 ri
->u
.buffer
.length
= length
;
1205 ri
->u
.buffer
.data
= data
;
1207 r
= (rc_res_resource
*) res_alloc (sizeof *r
);
1208 r
->type
= RES_TYPE_USERDATA
;
1214 static rc_res_resource
*
1215 bin_to_res_toolbar (windres_bfd
*wrbfd
, const bfd_byte
*data
, rc_uint_type length
)
1221 ri
= (rc_toolbar
*) res_alloc (sizeof (rc_toolbar
));
1222 ri
->button_width
= windres_get_32 (wrbfd
, data
, 4);
1223 ri
->button_height
= windres_get_32 (wrbfd
, data
+ 4, 4);
1224 ri
->nitems
= windres_get_32 (wrbfd
, data
+ 8, 4);
1229 for (i
=0 ; i
< ri
->nitems
; i
++)
1231 rc_toolbar_item
*it
;
1232 it
= (rc_toolbar_item
*) res_alloc (sizeof (rc_toolbar_item
));
1234 it
->id
.u
.id
= (int) windres_get_32 (wrbfd
, data
, 4);
1235 it
->prev
= it
->next
= NULL
;
1239 rc_toolbar_item
*ii
= ri
->items
;
1240 while (ii
->next
!= NULL
)
1248 r
= (rc_res_resource
*) res_alloc (sizeof *r
);
1249 r
->type
= RES_TYPE_TOOLBAR
;
1255 /* Local functions used to convert resources to binary format. */
1257 static rc_uint_type
resid_to_bin (windres_bfd
*, rc_uint_type
, rc_res_id
);
1258 static rc_uint_type
unicode_to_bin (windres_bfd
*, rc_uint_type
, const unichar
*);
1259 static rc_uint_type
res_to_bin_accelerator (windres_bfd
*, rc_uint_type
, const rc_accelerator
*);
1260 static rc_uint_type
res_to_bin_cursor (windres_bfd
*, rc_uint_type
, const rc_cursor
*);
1261 static rc_uint_type
res_to_bin_group_cursor (windres_bfd
*, rc_uint_type
, const rc_group_cursor
*);
1262 static rc_uint_type
res_to_bin_dialog (windres_bfd
*, rc_uint_type
, const rc_dialog
*);
1263 static rc_uint_type
res_to_bin_fontdir (windres_bfd
*, rc_uint_type
, const rc_fontdir
*);
1264 static rc_uint_type
res_to_bin_group_icon (windres_bfd
*, rc_uint_type
, const rc_group_icon
*);
1265 static rc_uint_type
res_to_bin_menu (windres_bfd
*, rc_uint_type
, const rc_menu
*);
1266 static rc_uint_type
res_to_bin_menuitems (windres_bfd
*, rc_uint_type
, const rc_menuitem
*);
1267 static rc_uint_type
res_to_bin_menuexitems (windres_bfd
*, rc_uint_type
, const rc_menuitem
*);
1268 static rc_uint_type
res_to_bin_rcdata (windres_bfd
*, rc_uint_type
, const rc_rcdata_item
*);
1269 static rc_uint_type
res_to_bin_stringtable (windres_bfd
*, rc_uint_type
, const rc_stringtable
*);
1270 static rc_uint_type
string_to_unicode_bin (windres_bfd
*, rc_uint_type
, const char *);
1271 static rc_uint_type
res_to_bin_toolbar (windres_bfd
*, rc_uint_type
, rc_toolbar
*tb
);
1272 static rc_uint_type
res_to_bin_versioninfo (windres_bfd
*, rc_uint_type
, const rc_versioninfo
*);
1273 static rc_uint_type
res_to_bin_generic (windres_bfd
*, rc_uint_type
, rc_uint_type
,
1276 /* Convert a resource to binary. */
1279 res_to_bin (windres_bfd
*wrbfd
, rc_uint_type off
, const rc_res_resource
*res
)
1283 case RES_TYPE_BITMAP
:
1286 case RES_TYPE_MESSAGETABLE
:
1287 return res_to_bin_generic (wrbfd
, off
, res
->u
.data
.length
, res
->u
.data
.data
);
1288 case RES_TYPE_ACCELERATOR
:
1289 return res_to_bin_accelerator (wrbfd
, off
, res
->u
.acc
);
1290 case RES_TYPE_CURSOR
:
1291 return res_to_bin_cursor (wrbfd
, off
, res
->u
.cursor
);
1292 case RES_TYPE_GROUP_CURSOR
:
1293 return res_to_bin_group_cursor (wrbfd
, off
, res
->u
.group_cursor
);
1294 case RES_TYPE_DIALOG
:
1295 return res_to_bin_dialog (wrbfd
, off
, res
->u
.dialog
);
1296 case RES_TYPE_FONTDIR
:
1297 return res_to_bin_fontdir (wrbfd
, off
, res
->u
.fontdir
);
1298 case RES_TYPE_GROUP_ICON
:
1299 return res_to_bin_group_icon (wrbfd
, off
, res
->u
.group_icon
);
1301 return res_to_bin_menu (wrbfd
, off
, res
->u
.menu
);
1302 case RES_TYPE_STRINGTABLE
:
1303 return res_to_bin_stringtable (wrbfd
, off
, res
->u
.stringtable
);
1304 case RES_TYPE_VERSIONINFO
:
1305 return res_to_bin_versioninfo (wrbfd
, off
, res
->u
.versioninfo
);
1306 case RES_TYPE_TOOLBAR
:
1307 return res_to_bin_toolbar (wrbfd
, off
, res
->u
.toolbar
);
1308 case RES_TYPE_USERDATA
:
1309 case RES_TYPE_RCDATA
:
1311 return res_to_bin_rcdata (wrbfd
, off
, res
->u
.rcdata
);
1315 /* Convert a resource ID to binary. This always returns exactly one
1316 bindata structure. */
1319 resid_to_bin (windres_bfd
*wrbfd
, rc_uint_type off
, rc_res_id id
)
1325 struct bin_res_id bri
;
1327 windres_put_16 (wrbfd
, bri
.sig
, 0xffff);
1328 windres_put_16 (wrbfd
, bri
.id
, id
.u
.id
);
1329 set_windres_bfd_content (wrbfd
, &bri
, off
, BIN_RES_ID
);
1335 rc_uint_type len
= (id
.u
.n
.name
? unichar_len (id
.u
.n
.name
) : 0);
1338 bfd_byte
*d
= (bfd_byte
*) reswr_alloc ((len
+ 1) * sizeof (unichar
));
1340 for (i
= 0; i
< len
; i
++)
1341 windres_put_16 (wrbfd
, d
+ (i
* sizeof (unichar
)), id
.u
.n
.name
[i
]);
1342 windres_put_16 (wrbfd
, d
+ (len
* sizeof (unichar
)), 0);
1343 set_windres_bfd_content (wrbfd
, d
, off
, (len
+ 1) * sizeof (unichar
));
1345 off
+= (rc_uint_type
) ((len
+ 1) * sizeof (unichar
));
1350 /* Convert a null terminated unicode string to binary. This always
1351 returns exactly one bindata structure. */
1354 unicode_to_bin (windres_bfd
*wrbfd
, rc_uint_type off
, const unichar
*str
)
1356 rc_uint_type len
= 0;
1359 len
= unichar_len (str
);
1365 d
= (bfd_byte
*) reswr_alloc ( (len
+ 1) * sizeof (unichar
));
1366 for (i
= 0; i
< len
; i
++)
1367 windres_put_16 (wrbfd
, d
+ (i
* sizeof (unichar
)), str
[i
]);
1368 windres_put_16 (wrbfd
, d
+ (len
* sizeof (unichar
)), 0);
1369 set_windres_bfd_content (wrbfd
, d
, off
, (len
+ 1) * sizeof (unichar
));
1371 off
+= (rc_uint_type
) ((len
+ 1) * sizeof (unichar
));
1376 /* Convert an accelerator resource to binary. */
1379 res_to_bin_accelerator (windres_bfd
*wrbfd
, rc_uint_type off
,
1380 const rc_accelerator
*accelerators
)
1382 const rc_accelerator
*a
;
1384 for (a
= accelerators
; a
!= NULL
; a
= a
->next
)
1388 struct bin_accelerator ba
;
1390 windres_put_16 (wrbfd
, ba
.flags
, a
->flags
| (a
->next
!= NULL
? 0 : ACC_LAST
));
1391 windres_put_16 (wrbfd
, ba
.key
, a
->key
);
1392 windres_put_16 (wrbfd
, ba
.id
, a
->id
);
1393 windres_put_16 (wrbfd
, ba
.pad
, 0);
1394 set_windres_bfd_content (wrbfd
, &ba
, off
, BIN_ACCELERATOR_SIZE
);
1396 off
+= BIN_ACCELERATOR_SIZE
;
1401 /* Convert a cursor resource to binary. */
1404 res_to_bin_cursor (windres_bfd
*wrbfd
, rc_uint_type off
, const rc_cursor
*c
)
1408 struct bin_cursor bc
;
1410 windres_put_16 (wrbfd
, bc
.xhotspot
, c
->xhotspot
);
1411 windres_put_16 (wrbfd
, bc
.yhotspot
, c
->yhotspot
);
1412 set_windres_bfd_content (wrbfd
, &bc
, off
, BIN_CURSOR_SIZE
);
1414 set_windres_bfd_content (wrbfd
, c
->data
, off
+ BIN_CURSOR_SIZE
, c
->length
);
1416 off
= (off
+ BIN_CURSOR_SIZE
+ (rc_uint_type
) c
->length
);
1420 /* Convert a group cursor resource to binary. */
1423 res_to_bin_group_cursor (windres_bfd
*wrbfd
, rc_uint_type off
,
1424 const rc_group_cursor
*group_cursors
)
1427 const rc_group_cursor
*gc
;
1428 struct bin_group_cursor bgc
;
1429 struct bin_group_cursor_item bgci
;
1430 rc_uint_type start
= off
;
1432 off
+= BIN_GROUP_CURSOR_SIZE
;
1434 for (c
= 0, gc
= group_cursors
; gc
!= NULL
; gc
= gc
->next
, c
++)
1438 windres_put_16 (wrbfd
, bgci
.width
, gc
->width
);
1439 windres_put_16 (wrbfd
, bgci
.height
, gc
->height
);
1440 windres_put_16 (wrbfd
, bgci
.planes
, gc
->planes
);
1441 windres_put_16 (wrbfd
, bgci
.bits
, gc
->bits
);
1442 windres_put_32 (wrbfd
, bgci
.bytes
, gc
->bytes
);
1443 windres_put_16 (wrbfd
, bgci
.index
, gc
->index
);
1444 set_windres_bfd_content (wrbfd
, &bgci
, off
, BIN_GROUP_CURSOR_ITEM_SIZE
);
1447 off
+= BIN_GROUP_CURSOR_ITEM_SIZE
;
1451 windres_put_16 (wrbfd
, bgc
.sig1
, 0);
1452 windres_put_16 (wrbfd
, bgc
.sig2
, 2);
1453 windres_put_16 (wrbfd
, bgc
.nitems
, c
);
1454 set_windres_bfd_content (wrbfd
, &bgc
, start
, BIN_GROUP_CURSOR_SIZE
);
1459 /* Convert a dialog resource to binary. */
1462 res_to_bin_dialog (windres_bfd
*wrbfd
, rc_uint_type off
, const rc_dialog
*dialog
)
1464 rc_uint_type off_delta
;
1465 rc_uint_type start
, marker
;
1468 rc_dialog_control
*dc
;
1469 struct bin_dialogex bdx
;
1470 struct bin_dialog bd
;
1474 dialogex
= extended_dialog (dialog
);
1480 windres_put_32 (wrbfd
, bd
.style
, dialog
->style
);
1481 windres_put_32 (wrbfd
, bd
.exstyle
, dialog
->exstyle
);
1482 windres_put_16 (wrbfd
, bd
.x
, dialog
->x
);
1483 windres_put_16 (wrbfd
, bd
.y
, dialog
->y
);
1484 windres_put_16 (wrbfd
, bd
.width
, dialog
->width
);
1485 windres_put_16 (wrbfd
, bd
.height
, dialog
->height
);
1489 windres_put_16 (wrbfd
, bdx
.sig1
, 1);
1490 windres_put_16 (wrbfd
, bdx
.sig2
, 0xffff);
1491 windres_put_32 (wrbfd
, bdx
.help
, (dialog
->ex
? dialog
->ex
->help
: 0));
1492 windres_put_32 (wrbfd
, bdx
.exstyle
, dialog
->exstyle
);
1493 windres_put_32 (wrbfd
, bdx
.style
, dialog
->style
);
1494 windres_put_16 (wrbfd
, bdx
.x
, dialog
->x
);
1495 windres_put_16 (wrbfd
, bdx
.y
, dialog
->y
);
1496 windres_put_16 (wrbfd
, bdx
.width
, dialog
->width
);
1497 windres_put_16 (wrbfd
, bdx
.height
, dialog
->height
);
1501 off
+= (dialogex
!= 0 ? BIN_DIALOGEX_SIZE
: BIN_DIALOG_SIZE
);
1503 off
= resid_to_bin (wrbfd
, off
, dialog
->menu
);
1504 off
= resid_to_bin (wrbfd
, off
, dialog
->class);
1505 off
= unicode_to_bin (wrbfd
, off
, dialog
->caption
);
1507 if ((dialog
->style
& DS_SETFONT
) != 0)
1513 struct bin_dialogfont bdf
;
1514 windres_put_16 (wrbfd
, bdf
.pointsize
, dialog
->pointsize
);
1515 set_windres_bfd_content (wrbfd
, &bdf
, off
, BIN_DIALOGFONT_SIZE
);
1519 struct bin_dialogexfont bdxf
;
1520 windres_put_16 (wrbfd
, bdxf
.pointsize
, dialog
->pointsize
);
1521 windres_put_16 (wrbfd
, bdxf
.weight
, (dialog
->ex
== NULL
? 0 : dialog
->ex
->weight
));
1522 windres_put_8 (wrbfd
, bdxf
.italic
, (dialog
->ex
== NULL
? 0 : dialog
->ex
->italic
));
1523 windres_put_8 (wrbfd
, bdxf
.charset
, (dialog
->ex
== NULL
? 1 : dialog
->ex
->charset
));
1524 set_windres_bfd_content (wrbfd
, &bdxf
, off
, BIN_DIALOGEXFONT_SIZE
);
1527 off
+= (dialogex
? BIN_DIALOGEXFONT_SIZE
: BIN_DIALOGFONT_SIZE
);
1528 off
= unicode_to_bin (wrbfd
, off
, dialog
->font
);
1530 for (c
= 0, dc
= dialog
->controls
; dc
!= NULL
; dc
= dc
->next
, c
++)
1532 bfd_byte dc_rclen
[2];
1534 off
+= (4 - ((off
- off_delta
) & 3)) & 3;
1539 struct bin_dialog_control bdc
;
1541 windres_put_32 (wrbfd
, bdc
.style
, dc
->style
);
1542 windres_put_32 (wrbfd
, bdc
.exstyle
, dc
->exstyle
);
1543 windres_put_16 (wrbfd
, bdc
.x
, dc
->x
);
1544 windres_put_16 (wrbfd
, bdc
.y
, dc
->y
);
1545 windres_put_16 (wrbfd
, bdc
.width
, dc
->width
);
1546 windres_put_16 (wrbfd
, bdc
.height
, dc
->height
);
1547 windres_put_16 (wrbfd
, bdc
.id
, dc
->id
);
1548 set_windres_bfd_content (wrbfd
, &bdc
, off
, BIN_DIALOG_CONTROL_SIZE
);
1552 struct bin_dialogex_control bdc
;
1554 windres_put_32 (wrbfd
, bdc
.help
, dc
->help
);
1555 windres_put_32 (wrbfd
, bdc
.exstyle
, dc
->exstyle
);
1556 windres_put_32 (wrbfd
, bdc
.style
, dc
->style
);
1557 windres_put_16 (wrbfd
, bdc
.x
, dc
->x
);
1558 windres_put_16 (wrbfd
, bdc
.y
, dc
->y
);
1559 windres_put_16 (wrbfd
, bdc
.width
, dc
->width
);
1560 windres_put_16 (wrbfd
, bdc
.height
, dc
->height
);
1561 windres_put_32 (wrbfd
, bdc
.id
, dc
->id
);
1562 set_windres_bfd_content (wrbfd
, &bdc
, off
, BIN_DIALOGEX_CONTROL_SIZE
);
1565 off
+= (dialogex
!= 0 ? BIN_DIALOGEX_CONTROL_SIZE
: BIN_DIALOG_CONTROL_SIZE
);
1567 off
= resid_to_bin (wrbfd
, off
, dc
->class);
1568 off
= resid_to_bin (wrbfd
, off
, dc
->text
);
1570 marker
= off
; /* Save two bytes for size of optional data. */
1573 if (dc
->data
== NULL
)
1576 windres_put_16 (wrbfd
, dc_rclen
, 0);
1580 rc_uint_type saved_off
= off
;
1581 rc_uint_type old_off
;
1582 off
+= (4 - ((off
- off_delta
) & 3)) & 3;
1585 off
= res_to_bin_rcdata (wrbfd
, off
, dc
->data
);
1586 if ((off
- old_off
) == 0)
1587 old_off
= off
= saved_off
;
1589 windres_put_16 (wrbfd
, dc_rclen
, off
- old_off
);
1592 set_windres_bfd_content (wrbfd
, dc_rclen
, marker
, 2);
1597 windres_put_16 (wrbfd
, (dialogex
!= 0 ? bdx
.off
: bd
.off
), c
);
1599 set_windres_bfd_content (wrbfd
, &bd
, start
, BIN_DIALOG_SIZE
);
1601 set_windres_bfd_content (wrbfd
, &bdx
, start
, BIN_DIALOGEX_SIZE
);
1607 /* Convert a fontdir resource to binary. */
1609 res_to_bin_fontdir (windres_bfd
*wrbfd
, rc_uint_type off
, const rc_fontdir
*fontdirs
)
1613 const rc_fontdir
*fd
;
1618 for (c
= 0, fd
= fontdirs
; fd
!= NULL
; fd
= fd
->next
, c
++)
1623 windres_put_16 (wrbfd
, d
, fd
->index
);
1624 set_windres_bfd_content (wrbfd
, d
, off
, 2);
1626 set_windres_bfd_content (wrbfd
, fd
->data
, off
+ 2, fd
->length
);
1628 off
+= (rc_uint_type
) fd
->length
+ 2;
1634 windres_put_16 (wrbfd
, d
, c
);
1635 set_windres_bfd_content (wrbfd
, d
, start
, 2);
1640 /* Convert a group icon resource to binary. */
1643 res_to_bin_group_icon (windres_bfd
*wrbfd
, rc_uint_type off
, const rc_group_icon
*group_icons
)
1646 struct bin_group_icon bgi
;
1648 const rc_group_icon
*gi
;
1651 off
+= BIN_GROUP_ICON_SIZE
;
1653 for (c
= 0, gi
= group_icons
; gi
!= NULL
; gi
= gi
->next
, c
++)
1655 struct bin_group_icon_item bgii
;
1659 windres_put_8 (wrbfd
, bgii
.width
, gi
->width
);
1660 windres_put_8 (wrbfd
, bgii
.height
, gi
->height
);
1661 windres_put_8 (wrbfd
, bgii
.colors
, gi
->colors
);
1662 windres_put_8 (wrbfd
, bgii
.pad
, 0);
1663 windres_put_16 (wrbfd
, bgii
.planes
, gi
->planes
);
1664 windres_put_16 (wrbfd
, bgii
.bits
, gi
->bits
);
1665 windres_put_32 (wrbfd
, bgii
.bytes
, gi
->bytes
);
1666 windres_put_16 (wrbfd
, bgii
.index
, gi
->index
);
1667 set_windres_bfd_content (wrbfd
, &bgii
, off
, BIN_GROUP_ICON_ITEM_SIZE
);
1669 off
+= BIN_GROUP_ICON_ITEM_SIZE
;
1674 windres_put_16 (wrbfd
, bgi
.sig1
, 0);
1675 windres_put_16 (wrbfd
, bgi
.sig2
, 1);
1676 windres_put_16 (wrbfd
, bgi
.count
, c
);
1677 set_windres_bfd_content (wrbfd
, &bgi
, start
, BIN_GROUP_ICON_SIZE
);
1682 /* Convert a menu resource to binary. */
1685 res_to_bin_menu (windres_bfd
*wrbfd
, rc_uint_type off
, const rc_menu
*menu
)
1689 menuex
= extended_menu (menu
);
1696 windres_put_16 (wrbfd
, bm
.sig1
, 0);
1697 windres_put_16 (wrbfd
, bm
.sig2
, 0);
1698 set_windres_bfd_content (wrbfd
, &bm
, off
, BIN_MENU_SIZE
);
1702 struct bin_menuex bm
;
1703 windres_put_16 (wrbfd
, bm
.sig1
, 1);
1704 windres_put_16 (wrbfd
, bm
.sig2
, 4);
1705 windres_put_32 (wrbfd
, bm
.help
, menu
->help
);
1706 set_windres_bfd_content (wrbfd
, &bm
, off
, BIN_MENUEX_SIZE
);
1709 off
+= (menuex
!= 0 ? BIN_MENUEX_SIZE
: BIN_MENU_SIZE
);
1712 off
= res_to_bin_menuitems (wrbfd
, off
, menu
->items
);
1716 off
= res_to_bin_menuexitems (wrbfd
, off
, menu
->items
);
1721 /* Convert menu items to binary. */
1724 res_to_bin_menuitems (windres_bfd
*wrbfd
, rc_uint_type off
, const rc_menuitem
*items
)
1726 const rc_menuitem
*mi
;
1728 for (mi
= items
; mi
!= NULL
; mi
= mi
->next
)
1730 struct bin_menuitem bmi
;
1734 if (mi
->next
== NULL
)
1735 flags
|= MENUITEM_ENDMENU
;
1736 if (mi
->popup
!= NULL
)
1737 flags
|= MENUITEM_POPUP
;
1741 windres_put_16 (wrbfd
, bmi
.flags
, flags
);
1742 if (mi
->popup
== NULL
)
1743 windres_put_16 (wrbfd
, bmi
.id
, mi
->id
);
1744 set_windres_bfd_content (wrbfd
, &bmi
, off
,
1745 mi
->popup
== NULL
? BIN_MENUITEM_SIZE
1746 : BIN_MENUITEM_POPUP_SIZE
);
1748 off
+= (mi
->popup
== NULL
? BIN_MENUITEM_SIZE
: BIN_MENUITEM_POPUP_SIZE
);
1750 off
= unicode_to_bin (wrbfd
, off
, mi
->text
);
1752 if (mi
->popup
!= NULL
)
1754 off
= res_to_bin_menuitems (wrbfd
, off
, mi
->popup
);
1760 /* Convert menuex items to binary. */
1763 res_to_bin_menuexitems (windres_bfd
*wrbfd
, rc_uint_type off
, const rc_menuitem
*items
)
1765 rc_uint_type off_delta
= off
;
1766 const rc_menuitem
*mi
;
1768 for (mi
= items
; mi
!= NULL
; mi
= mi
->next
)
1770 struct bin_menuitemex bmi
;
1773 off
+= (4 - ((off
- off_delta
) & 3)) & 3;
1776 if (mi
->next
== NULL
)
1778 if (mi
->popup
!= NULL
)
1783 windres_put_32 (wrbfd
, bmi
.type
, mi
->type
);
1784 windres_put_32 (wrbfd
, bmi
.state
, mi
->state
);
1785 windres_put_32 (wrbfd
, bmi
.id
, mi
->id
);
1786 windres_put_16 (wrbfd
, bmi
.flags
, flags
);
1787 set_windres_bfd_content (wrbfd
, &bmi
, off
, BIN_MENUITEMEX_SIZE
);
1789 off
+= BIN_MENUITEMEX_SIZE
;
1791 off
= unicode_to_bin (wrbfd
, off
, mi
->text
);
1793 if (mi
->popup
!= NULL
)
1797 off
+= (4 - ((off
- off_delta
) & 3)) & 3;
1801 windres_put_32 (wrbfd
, help
, mi
->help
);
1802 set_windres_bfd_content (wrbfd
, help
, off
, 4);
1805 off
= res_to_bin_menuexitems (wrbfd
, off
, mi
->popup
);
1811 /* Convert an rcdata resource to binary. This is also used to convert
1812 other information which happens to be stored in rc_rcdata_item lists
1816 res_to_bin_rcdata (windres_bfd
*wrbfd
, rc_uint_type off
, const rc_rcdata_item
*items
)
1818 const rc_rcdata_item
*ri
;
1820 for (ri
= items
; ri
!= NULL
; ri
= ri
->next
)
1834 len
= ri
->u
.string
.length
;
1836 case RCDATA_WSTRING
:
1837 len
= ri
->u
.wstring
.length
* sizeof (unichar
);
1840 len
= ri
->u
.buffer
.length
;
1846 bfd_byte
*hp
= &h
[0];
1850 windres_put_16 (wrbfd
, hp
, ri
->u
.word
);
1853 windres_put_32 (wrbfd
, hp
, ri
->u
.dword
);
1856 hp
= (bfd_byte
*) ri
->u
.string
.s
;
1858 case RCDATA_WSTRING
:
1862 hp
= (bfd_byte
*) reswr_alloc (len
);
1863 for (i
= 0; i
< ri
->u
.wstring
.length
; i
++)
1864 windres_put_16 (wrbfd
, hp
+ i
* sizeof (unichar
), ri
->u
.wstring
.w
[i
]);
1868 hp
= (bfd_byte
*) ri
->u
.buffer
.data
;
1871 set_windres_bfd_content (wrbfd
, hp
, off
, len
);
1878 /* Convert a stringtable resource to binary. */
1881 res_to_bin_stringtable (windres_bfd
*wrbfd
, rc_uint_type off
,
1882 const rc_stringtable
*st
)
1886 for (i
= 0; i
< 16; i
++)
1888 rc_uint_type slen
, length
;
1891 slen
= (rc_uint_type
) st
->strings
[i
].length
;
1892 if (slen
== 0xffffffff) slen
= 0;
1893 s
= st
->strings
[i
].string
;
1895 length
= 2 + slen
* 2;
1901 hp
= (bfd_byte
*) reswr_alloc (length
);
1902 windres_put_16 (wrbfd
, hp
, slen
);
1904 for (j
= 0; j
< slen
; j
++)
1905 windres_put_16 (wrbfd
, hp
+ 2 + j
* 2, s
[j
]);
1906 set_windres_bfd_content (wrbfd
, hp
, off
, length
);
1913 /* Convert an ASCII string to a unicode binary string. This always
1914 returns exactly one bindata structure. */
1917 string_to_unicode_bin (windres_bfd
*wrbfd
, rc_uint_type off
, const char *s
)
1921 len
= (rc_uint_type
) strlen (s
);
1928 hp
= (bfd_byte
*) reswr_alloc ((len
+ 1) * sizeof (unichar
));
1930 for (i
= 0; i
< len
; i
++)
1931 windres_put_16 (wrbfd
, hp
+ i
* 2, s
[i
]);
1932 windres_put_16 (wrbfd
, hp
+ i
* 2, 0);
1933 set_windres_bfd_content (wrbfd
, hp
, off
, (len
+ 1) * sizeof (unichar
));
1935 off
+= (rc_uint_type
) ((len
+ 1) * sizeof (unichar
));
1940 res_to_bin_toolbar (windres_bfd
*wrbfd
, rc_uint_type off
, rc_toolbar
*tb
)
1944 struct bin_toolbar bt
;
1945 windres_put_32 (wrbfd
, bt
.button_width
, tb
->button_width
);
1946 windres_put_32 (wrbfd
, bt
.button_height
, tb
->button_height
);
1947 windres_put_32 (wrbfd
, bt
.nitems
, tb
->nitems
);
1948 set_windres_bfd_content (wrbfd
, &bt
, off
, BIN_TOOLBAR_SIZE
);
1951 rc_toolbar_item
*it
;
1955 ids
= (bfd_byte
*) reswr_alloc (tb
->nitems
* 4);
1959 windres_put_32 (wrbfd
, ids
+ i
, it
->id
.u
.id
);
1963 set_windres_bfd_content (wrbfd
, ids
, off
+ BIN_TOOLBAR_SIZE
, i
);
1966 off
+= BIN_TOOLBAR_SIZE
+ tb
->nitems
* 4;
1971 /* Convert a versioninfo resource to binary. */
1974 res_to_bin_versioninfo (windres_bfd
*wrbfd
, rc_uint_type off
,
1975 const rc_versioninfo
*versioninfo
)
1977 rc_uint_type off_delta
= off
;
1979 struct bin_versioninfo bvi
;
1983 off
+= BIN_VERSIONINFO_SIZE
;
1984 off
= string_to_unicode_bin (wrbfd
, off
, "VS_VERSION_INFO");
1985 off
+= (4 - ((off
- off_delta
) & 3)) & 3;
1987 if (versioninfo
->fixed
!= NULL
)
1991 struct bin_fixed_versioninfo bfv
;
1992 const rc_fixed_versioninfo
*fi
;
1994 fi
= versioninfo
->fixed
;
1995 windres_put_32 (wrbfd
, bfv
.sig1
, 0xfeef04bd);
1996 windres_put_32 (wrbfd
, bfv
.sig2
, 0x10000);
1997 windres_put_32 (wrbfd
, bfv
.file_version
, fi
->file_version_ms
);
1998 windres_put_32 (wrbfd
, bfv
.file_version_ls
, fi
->file_version_ls
);
1999 windres_put_32 (wrbfd
, bfv
.product_version_ms
, fi
->product_version_ms
);
2000 windres_put_32 (wrbfd
, bfv
.product_version_ls
, fi
->product_version_ls
);
2001 windres_put_32 (wrbfd
, bfv
.file_flags_mask
, fi
->file_flags_mask
);
2002 windres_put_32 (wrbfd
, bfv
.file_flags
, fi
->file_flags
);
2003 windres_put_32 (wrbfd
, bfv
.file_os
, fi
->file_os
);
2004 windres_put_32 (wrbfd
, bfv
.file_type
, fi
->file_type
);
2005 windres_put_32 (wrbfd
, bfv
.file_subtype
, fi
->file_subtype
);
2006 windres_put_32 (wrbfd
, bfv
.file_date_ms
, fi
->file_date_ms
);
2007 windres_put_32 (wrbfd
, bfv
.file_date_ls
, fi
->file_date_ls
);
2008 set_windres_bfd_content (wrbfd
, &bfv
, off
, BIN_FIXED_VERSIONINFO_SIZE
);
2010 off
+= BIN_FIXED_VERSIONINFO_SIZE
;
2013 for (vi
= versioninfo
->var
; vi
!= NULL
; vi
= vi
->next
)
2015 struct bin_ver_info bv
;
2016 rc_uint_type bv_off
;
2018 off
+= (4 - ((off
- off_delta
) & 3)) & 3;
2022 off
+= BIN_VER_INFO_SIZE
;
2028 case VERINFO_STRING
:
2030 const rc_ver_stringtable
*vst
;
2032 off
= string_to_unicode_bin (wrbfd
, off
, "StringFileInfo");
2034 if (!vi
->u
.string
.stringtables
)
2035 off
+= (4 - ((off
- off_delta
) & 3)) & 3;
2037 for (vst
= vi
->u
.string
.stringtables
; vst
!= NULL
; vst
= vst
->next
)
2039 struct bin_ver_info bvst
;
2040 rc_uint_type vst_off
;
2041 const rc_ver_stringinfo
*vs
;
2043 off
+= (4 - ((off
- off_delta
) & 3)) & 3;
2046 off
+= BIN_VER_INFO_SIZE
;
2048 off
= unicode_to_bin (wrbfd
, off
, vst
->language
);
2050 for (vs
= vst
->strings
; vs
!= NULL
; vs
= vs
->next
)
2052 struct bin_ver_info bvs
;
2053 rc_uint_type vs_off
, str_off
;
2055 off
+= (4 - ((off
- off_delta
) & 3)) & 3;
2058 off
+= BIN_VER_INFO_SIZE
;
2060 off
= unicode_to_bin (wrbfd
, off
, vs
->key
);
2062 off
+= (4 - ((off
- off_delta
) & 3)) & 3;
2065 off
= unicode_to_bin (wrbfd
, off
, vs
->value
);
2069 windres_put_16 (wrbfd
, bvs
.size
, off
- vs_off
);
2070 windres_put_16 (wrbfd
, bvs
.sig1
, (off
- str_off
) / 2);
2071 windres_put_16 (wrbfd
, bvs
.sig2
, 1);
2072 set_windres_bfd_content (wrbfd
, &bvs
, vs_off
,
2079 windres_put_16 (wrbfd
, bvst
.size
, off
- vst_off
);
2080 windres_put_16 (wrbfd
, bvst
.sig1
, 0);
2081 windres_put_16 (wrbfd
, bvst
.sig2
, 1);
2082 set_windres_bfd_content (wrbfd
, &bvst
, vst_off
,
2091 rc_uint_type vvd_off
, vvvd_off
;
2092 struct bin_ver_info bvvd
;
2093 const rc_ver_varinfo
*vv
;
2095 off
= string_to_unicode_bin (wrbfd
, off
, "VarFileInfo");
2097 off
+= (4 - ((off
- off_delta
) & 3)) & 3;
2100 off
+= BIN_VER_INFO_SIZE
;
2102 off
= unicode_to_bin (wrbfd
, off
, vi
->u
.var
.key
);
2104 off
+= (4 - ((off
- off_delta
) & 3)) & 3;
2108 for (vv
= vi
->u
.var
.var
; vv
!= NULL
; vv
= vv
->next
)
2114 windres_put_16 (wrbfd
, &vvsd
[0], vv
->language
);
2115 windres_put_16 (wrbfd
, &vvsd
[2], vv
->charset
);
2116 set_windres_bfd_content (wrbfd
, vvsd
, off
, 4);
2122 windres_put_16 (wrbfd
, bvvd
.size
, off
- vvd_off
);
2123 windres_put_16 (wrbfd
, bvvd
.sig1
, off
- vvvd_off
);
2124 windres_put_16 (wrbfd
, bvvd
.sig2
, 0);
2125 set_windres_bfd_content (wrbfd
, &bvvd
, vvd_off
,
2135 windres_put_16 (wrbfd
, bv
.size
, off
- bv_off
);
2136 windres_put_16 (wrbfd
, bv
.sig1
, 0);
2137 windres_put_16 (wrbfd
, bv
.sig2
, 1);
2138 set_windres_bfd_content (wrbfd
, &bv
, bv_off
,
2145 windres_put_16 (wrbfd
, bvi
.size
, off
- start
);
2146 windres_put_16 (wrbfd
, bvi
.fixed_size
,
2147 versioninfo
->fixed
== NULL
? 0
2148 : BIN_FIXED_VERSIONINFO_SIZE
);
2149 windres_put_16 (wrbfd
, bvi
.sig2
, 0);
2150 set_windres_bfd_content (wrbfd
, &bvi
, start
, BIN_VER_INFO_SIZE
);
2155 /* Convert a generic resource to binary. */
2158 res_to_bin_generic (windres_bfd
*wrbfd
, rc_uint_type off
, rc_uint_type length
,
2159 const bfd_byte
*data
)
2161 if (wrbfd
&& length
!= 0)
2162 set_windres_bfd_content (wrbfd
, data
, off
, length
);
2163 return off
+ (rc_uint_type
) length
;