1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376
|
/* 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 &~ (MENUITEM_POPUP | MENUITEM_ENDMENU);
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 + 2 * slen;
length -= 2 + 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 = (struct accelerator *) res_alloc (sizeof *a);
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->exstyle, 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 ? 6 : 2;
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)
{
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
{
unsigned long sublen;
dword_align_bin (&pp, &length);
*pp = res_to_bin_rcdata (dc->data, big_endian);
sublen = 0;
while (*pp != NULL)
{
sublen += (*pp)->length;
pp = &(*pp)->next;
}
put_16 (big_endian, sublen, d->data);
length += sublen;
}
}
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 ? 8 : 4;
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;
}
|