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
|
/* ncbimisc.c
* ===========================================================================
*
* PUBLIC DOMAIN NOTICE
* National Center for Biotechnology Information
*
* This software/database is a "United States Government Work" under the
* terms of the United States Copyright Act. It was written as part of
* the author's official duties as a United States Government employee and
* thus cannot be copyrighted. This software/database is freely available
* to the public for use. The National Library of Medicine and the U.S.
* Government have not placed any restriction on its use or reproduction.
*
* Although all reasonable efforts have been taken to ensure the accuracy
* and reliability of the software and data, the NLM and the U.S.
* Government do not and cannot warrant the performance or results that
* may be obtained by using this software or data. The NLM and the U.S.
* Government disclaim all warranties, express or implied, including
* warranties of performance, merchantability or fitness for any particular
* purpose.
*
* Please cite the author in any work or product based on this material.
*
* ===========================================================================
*
* File Name: ncbimisc.c
*
* Author: Gish, Kans, Ostell, Schuler
*
* Version Creation Date: 10/23/91
*
* $Revision: 6.37 $
*
* File Description:
* miscellaneous functions
*
* Modifications:
* --------------------------------------------------------------------------
* Date Name Description of modification
* ------- ---------- -----------------------------------------------------
* 04-15-93 Schuler Changed _cdecl to LIBCALL
* 06-15-93 Schuler SGML functions moved to ncbisgml.c
* 02-16-94 Epstein Retired Gestalt functions and definitions
*
* $Log: ncbimisc.c,v $
* Revision 6.37 2008/12/04 15:05:11 bollin
* Added ValNodeInsert, which inserts a new ValNode into a sorted list at the
* correct position.
*
* Revision 6.36 2008/09/11 20:30:37 bollin
* Added ValNodePurge function.
*
* Revision 6.35 2008/07/21 20:15:22 bollin
* Added ValNodeCompare function.
*
* Revision 6.34 2008/04/29 13:40:53 kans
* fixes for warnings caught by mingw cross-compiler
*
* Revision 6.33 2008/04/04 13:15:48 bollin
* Added ValNodeUnique function
*
* Revision 6.32 2006/11/09 17:47:16 kans
* added ValNodeMergeStrs
*
* Revision 6.31 2006/10/17 14:16:48 lavr
* ValNodeCopyStr() to take "const char*"
*
* Revision 6.30 2005/11/16 16:36:11 kans
* support for PowerPC and Intel chips for Macintosh
*
* Revision 6.29 2005/04/13 21:36:18 kans
* restored mac ifdefs around Nlm_P2Cstr and Nlm_C2Pstr functions
*
* Revision 6.28 2005/04/13 21:28:44 kans
* Nlm_CtoPstr and Nlm_PtoCstr always uses our own code so we are not dependent on a particular version of Carbon
*
* Revision 6.27 2005/04/13 18:03:21 rsmith
* On OSX don't define C2Pstr/P2Cstr, just implement Nlm_CtoPstr.
*
* Revision 6.26 2004/03/25 15:37:24 lavr
* Change UINT64_MAX into UINT8_MAX back for the toolkit to compile portably
*
* Revision 6.25 2004/03/24 18:48:49 lebedev
* Use UINT64_MAX in divray_init function
*
* Revision 6.24 2003/12/03 02:10:23 kans
* added defines missing from Mac OS 10.3 headers
*
* Revision 6.23 2002/11/06 21:25:10 ucko
* Don't assume MIPS is IRIX, or HPPA is HP/UX; allow Linux too, for both.
*
* Revision 6.22 2002/06/13 16:14:07 kans
* fix includes for OS_UNIX_DARWIN with WIN_MAC (EN)
*
* Revision 6.21 2001/04/05 03:03:28 juran
* Defined our own C2PStr() and P2Cstr() for use with Carbon (which omits them).
*
* Revision 6.20 2000/10/30 18:11:41 beloslyu
* FreeBSD was added
*
* Revision 6.19 2000/08/28 18:41:29 vakatov
* Added type cast in ValNodeSort() to pass C++ compilation
*
* Revision 6.18 2000/03/10 17:29:12 kans
* changed divray to Uint8 - fix supplied by DDBJ for Cray computer
*
* Revision 6.17 1999/12/21 17:52:40 kans
* removed MPW/THINKC conditional code, starting upgrade to Carbon compatibility - Churchill
*
* Revision 6.16 1999/07/29 15:58:48 kans
* added bigintvalue, ValNodeAddBigInt (PD)
*
* Revision 6.15 1999/06/07 18:22:20 beloslyu
* NetBSD port
*
* Revision 6.14 1999/04/23 18:00:15 beloslyu
* change Uint4 to Nlm_Uint4 and CharPtr to Nlm_CharPtr
*
* Revision 6.13 1999/04/23 17:28:50 shavirin
* Changed Uint4 to the Nlm_Uint4
*
* Revision 6.12 1999/04/23 16:10:26 shavirin
* Added new function Nlm_GetChecksum() which calculates
* checksum from given string
*
* Revision 6.11 1999/04/14 19:37:23 madden
* Add Nlm_ prefix to Int8
*
* Revision 6.10 1999/04/14 15:31:06 madden
* add Nlm_Int8tostr function
*
* Revision 6.9 1999/03/11 16:21:51 kans
* ValNodeSort is more visible copy of jzmisc SortValNode
*
* Revision 6.8 1999/01/29 19:23:05 kans
* use C2PStr and P2CStr from <TextUtils.h>
*
* Revision 6.7 1999/01/21 20:08:37 ostell
* added SwitchUint2 and 4, added integer bytestores
*
* Revision 6.6 1999/01/07 15:20:57 victorov
* added Nlm_ prefix to MD5 functions to avoid conflicts
*
* Revision 6.5 1999/01/06 22:49:26 victorov
* added MD5 hash calculator
*
* Revision 6.4 1998/10/13 20:49:38 vakatov
* + Nlm_PlatformName()
*
* Revision 6.3 1998/07/02 18:24:28 vakatov
* Cleaned the code & made it pass through the C++ compilation
*
* Revision 6.2 1998/04/08 16:49:56 kans
* ValNodeLen returns Int4
*
* Revision 6.1 1997/12/12 21:42:28 kans
* Strings.h dragged in QuickDraw.h, so it is now out of the header
*
* Revision 6.0 1997/08/25 18:16:45 madden
* Revision changed to 6.0
*
* Revision 5.4 1997/01/31 15:37:12 vakatov
* ListSort(): replaced Boolean --> Nlm_Boolean
*
* Revision 5.3 1997/01/28 21:26:56 epstein
* move doubly-linked list handling functions from ni_list.[ch]
*
* Revision 5.2 1996/12/10 16:09:16 kans
* added ValNodeLen function
*
* Revision 5.1 1996/12/03 21:48:33 vakatov
* Adopted for 32-bit MS-Windows DLLs
*
* Revision 5.0 1996/05/28 13:18:57 ostell
* Set to revision 5.0
*
* Revision 4.2 1996/03/26 16:31:06 epstein
* migrate byte-swapping functions to ncbimisc.[ch]
*
* Revision 4.1 1995/10/28 15:03:20 ostell
* added casts to quiet DOS compile warnings
*
* Revision 4.0 1995/07/26 13:46:50 ostell
* force revision to 4.0
*
* Revision 2.17 1995/05/15 18:45:58 ostell
* added Log line
*
*
*
* ==========================================================================
*/
#include <ncbi.h>
#include <ncbiwin.h>
#ifdef OS_MAC
#include <TextUtils.h>
#endif
/* Missing from /usr/include/gcc/darwin/3.3/machine/limits.h */
#ifdef __MWERKS__
#ifdef OS_UNIX_DARWIN
#ifndef __CHAR_BIT__
#define __CHAR_BIT__ 8
#endif
#endif
#endif
/* End missing from /usr/include/gcc/darwin/3.3/machine/limits.h */
/*
TRIPLE_MARK is the character inserted before the thousands, millions,
billions, etc. digit positions. Change TRIPLE_MARK to a period
for the International scene, or define it as '\0' if no magnitude
markers are desired.
*/
#define TRIPLE_MARK ','
#define MISC_COMMAS 1 /* insert commas only when |value| >= 10,000 */
#define MISC_ALLCOMMAS 2 /* insert commas for any |value| >= 1,000 */
#define MISC_ANYCOMMAS (MISC_COMMAS|MISC_ALLCOMMAS)
#define MISC_PLUSSIGNS 4 /* prepend a plus sign (+) to positive values */
/*
buf[NBUFS][] is a circularly-maintained list of buffers for storing
the results of calls to Nlm_Ltostr() and Nlm_Ultostr(). Up to NBUFS
usages of either function in a single printf() may be made.
NBUFS should be defined large enough to satisfy _all_ likely occurrences.
*/
#define NBUFS 10 /* No. of static buffers for the ASCII results */
static int bufno = 0; /* current buffer marker in the circular list */
static char buf[NBUFS][(CHAR_BIT)*sizeof(long)/2];
/* divray[] is a fixed array of power-of-10 divisors which must be initialized*/
static Uint8 divray[CHAR_BIT*sizeof(Uint8)/2];
/* divray_max is related to the maximum precision available in a long int */
static int divray_max;
/* commaray[] is a fixed array that identifies positions where commas belong */
static char commaray[DIM(divray)];
/* divray_init() initializes divray[] */
static void divray_init PROTO((void));
/* ncbi_ultostr() is the basic (unsigned) integer-to-ASCII conversion engine */
static void ncbi_ultostr PROTO((char *buff, Uint8 value, int commas));
/* ulwidth() is the basic length-determiner for integer-ASCII conversions */
static int ulwidth PROTO((unsigned long value, int commas));
/* heapify() is the basic heap-sort function used by Nlm_HeapSort() */
static void heapify PROTO((Nlm_CharPtr b0, Nlm_CharPtr b, Nlm_CharPtr lim, Nlm_CharPtr last, size_t w, int (LIBCALLBACK *compar) (Nlm_VoidPtr, Nlm_VoidPtr) ));
/* divray_init -- initialize array of divisors and array of marker locations */
static void
divray_init (void)
{
Uint8 j = UINT8_MAX, k = 1;
for (divray_max=0; divray_max < DIM(divray) && j != 0; ++divray_max) {
divray[divray_max] = k;
if ((divray_max+1)%3 == 0)
commaray[divray_max] = (TRIPLE_MARK != '\0');
j /= 10;
k *= 10;
}
--divray_max;
}
/* ncbi_ultostr is the basic (unsigned) integer->ASCII conversion engine */
static void
ncbi_ultostr (char *buff, Uint8 value, int commas)
{
Uint8 value_orig = value;
int i, quotient;
if (divray_max == 0)
divray_init();
/*
Insert commas when value_orig >= 10000 (the Macintosh Way),
unless MISC_ALLCOMMAS is set.
*/
commas = ((commas&MISC_ALLCOMMAS) && value >= 1000)
|| (commas && value > (10*1000));
for (i=divray_max; i > 0 && divray[i] > value; --i)
;
for (; i >= 0; --i) {
if (commas && commaray[i] != NULLB && value != value_orig)
*buff++ = TRIPLE_MARK;
quotient = (int)(value / divray[i]);
*buff++ = (char) '0' + (char) quotient;
switch (quotient) {
case 0: break;
case 1: value -= divray[i]; break;
case 2: value -= 2*divray[i]; break;
case 3: value -= 3*divray[i]; break;
case 4: value -= 4*divray[i]; break;
case 5: value -= 5*divray[i]; break;
case 6: value -= 6*divray[i]; break;
case 7: value -= 7*divray[i]; break;
case 8: value -= 8*divray[i]; break;
case 9: value -= 9*divray[i]; break;
default: value -= quotient*divray[i]; break; /* shouldn't be taken */
}
}
*buff = NULLB; /* tack on a NUL terminator */
return;
}
/* Nlm_Int8tostr -- convert a signed long integer to ASCII */
NLM_EXTERN char * LIBCALL Nlm_Int8tostr (Nlm_Int8 value, int opts)
{
char *bp0, *bp;
bp0 = bp = &buf[bufno][0];
if (++bufno >= NBUFS)
bufno = 0;
if (value < 0) {
*bp++ = '-';
value = -value;
}
else
if (opts&MISC_PLUSSIGNS && value > 0)
*bp++ = '+';
ncbi_ultostr(bp, (Uint8)value, opts&MISC_ANYCOMMAS);
return bp0;
}
/* Nlm_Ltostr -- convert a signed long integer to ASCII */
NLM_EXTERN char * LIBCALL Nlm_Ltostr (long value, int opts)
{
char *bp0, *bp;
bp0 = bp = &buf[bufno][0];
if (++bufno >= NBUFS)
bufno = 0;
if (value < 0) {
*bp++ = '-';
value = -value;
}
else
if (opts&MISC_PLUSSIGNS && value > 0)
*bp++ = '+';
ncbi_ultostr(bp, (Uint8)value, opts&MISC_ANYCOMMAS);
return bp0;
}
/* Nlm_Ultostr convert an unsigned long integer to ASCII */
NLM_EXTERN char * LIBCALL Nlm_Ultostr (unsigned long value, int opts)
{
char *bp0, *bp;
bp = bp0 = &buf[bufno][0];
if (++bufno >= NBUFS)
bufno = 0;
if (opts&MISC_PLUSSIGNS && value > 0)
*bp++ = '+';
ncbi_ultostr(bp, (Uint8) value, opts&MISC_ANYCOMMAS);
return bp0;
}
/*
Return the length (in characters) of the ASCII base 10 representation
of the specified integer.
If "opts&MISC_COMMAS" is non-zero, consider the additional length required
for commas before the thousands, millions, billions, etc. positions.
If "opts&MISC_ALLCOMMAS" is non-zero, insert commas even when the value
of the integer is less than 10,000.
If "opts&MISC_PLUSSIGNS" is non-zero, consider the length of a plus sign
in front of any positive value, as well as the standard minus sign in front
of negative values.
*/
NLM_EXTERN int LIBCALL Nlm_Lwidth (long value, int opts)
{
int len;
if (value < 0) {
len = 1; /* account for the minus sign */
value = -value;
}
else
/* account for a plus sign */
len = (opts&MISC_PLUSSIGNS) && (value > 0);
return len + ulwidth(value, opts&MISC_ANYCOMMAS);
}
/*
Return the length (in characters) of the ASCII base 10 representation
of the specified unsigned integer.
*/
NLM_EXTERN int LIBCALL Nlm_Ulwidth (unsigned long value, int opts)
{
int len;
len = ulwidth(value, opts&MISC_ANYCOMMAS);
return len + ((opts&MISC_PLUSSIGNS) && (value > 0));
}
static int
ulwidth (unsigned long value, int commas)
{
int j, len;
if (divray_max == 0)
divray_init();
for (len=divray_max; len > 0 && divray[len] > value; --len)
;
if ((commas&MISC_ALLCOMMAS) || (commas && value >= (10*1000)) ) {
for (j = len-1; j > 1; --j)
len += (commaray[j] != 0);
}
return len+1;
}
/*
Nlm_HeapSort -- sort a list using an heap sort algorithm
Performance is guaranteed O(NlogN). Compared to BSD UNIX(TM) qsort,
Nlm_HeapSort averages about half as fast--which may be acceptable
for a portable, public domain function which qsort is not.
This code was derived from original work by Professor Webb Miller
(Penn. State University), but don't blame him for this mess or any errors.
7/31/90 WRG
6/18/92 Modified for segmented memory safety. JMO
*/
NLM_EXTERN void LIBCALL Nlm_HeapSort (Nlm_VoidPtr b, size_t nel, size_t width, int (LIBCALLBACK *compar )PROTO ((Nlm_VoidPtr, Nlm_VoidPtr ))) /* Element comparison routine */
{
register Nlm_CharPtr base = (Nlm_CharPtr)b;
register size_t i;
register char ch;
register Nlm_CharPtr base0 = (Nlm_CharPtr)base, lim, basef;
if (nel < 2)
return;
lim = &base[((nel-2)/2)*width];
basef = &base[(nel-1)*width];
i = nel/2;
for (base = &base0[(i - 1)*width]; i > 0; base = base - width) {
heapify(base0, base, lim, basef, width, compar);
i--;
}
for (base = &base0[(nel-1)*width]; base > base0; base -= width) {
for (i=0; i<width; ++i) {
ch = base0[i];
base0[i] = base[i];
base[i] = ch;
}
lim = base0 + ((base-base0)/2 - width);
if (base > base0+width)
heapify(base0, base0, lim, base-width, width, compar);
}
}
static void
heapify (Nlm_CharPtr base0, Nlm_CharPtr base, Nlm_CharPtr lim, Nlm_CharPtr last, size_t width, int (LIBCALLBACK *compar )PROTO ((Nlm_VoidPtr, Nlm_VoidPtr )))
{
register size_t i;
register char ch;
register Nlm_CharPtr left_son, large_son;
left_son = base0 + 2*(base-base0) + width;
while (base <= lim) {
if (left_son == last)
large_son = left_son;
else
large_son = (*compar)(left_son, left_son+width) >= 0 ?
left_son : left_son+width;
if ((*compar)(base, large_son) < 0) {
for (i=0; i<width; ++i) {
ch = base[i];
base[i] = large_son[i];
large_son[i] = ch;
}
base = large_son;
left_son = base0 + 2*(base-base0) + width;
} else
break;
}
}
/*****************************************************************************
*
* ValNodeNew(vnp)
* adds after last node in list if vnp not NULL
*
*****************************************************************************/
NLM_EXTERN ValNodePtr LIBCALL ValNodeNew (ValNodePtr vnp)
{
ValNodePtr newnode;
newnode = (ValNodePtr) Nlm_MemNew(sizeof(ValNode));
if (vnp != NULL)
{
while (vnp->next != NULL)
vnp = vnp->next;
vnp->next = newnode;
}
return newnode;
}
/*****************************************************************************
*
* ValNodeLen(vnp)
* returns the number of nodes in the linked list
*
*****************************************************************************/
NLM_EXTERN Nlm_Int4 LIBCALL ValNodeLen (ValNodePtr vnp)
{
Nlm_Int4 len;
len = 0;
while (vnp != NULL) {
len++;
vnp = vnp->next;
}
return len;
}
/*****************************************************************************
*
* ValNodeAdd(head)
* adds after last node in list if *head not NULL
* If *head is NULL, sets it to the new ValNode
* returns pointer to the NEW node added
*
*****************************************************************************/
NLM_EXTERN ValNodePtr LIBCALL ValNodeAdd (ValNodePtr PNTR head)
{
ValNodePtr newnode;
if (head != NULL)
{
newnode = ValNodeNew(*head);
if (*head == NULL)
*head = newnode;
}
else
newnode = ValNodeNew(NULL);
return newnode;
}
/*****************************************************************************
*
* ValNodeLink(head, newnode)
* adds newnode at end of chain
* if (*head == NULL) *head = newnode
* ALWAYS returns pointer to START of chain
*
*****************************************************************************/
NLM_EXTERN ValNodePtr LIBCALL ValNodeLink (ValNodePtr PNTR head, ValNodePtr newnode)
{
ValNodePtr vnp;
if (head == NULL)
return newnode;
vnp = *head;
if (vnp != NULL )
{
while (vnp->next != NULL)
vnp = vnp->next;
vnp->next = newnode;
}
else
*head = newnode;
return *head;
}
/*****************************************************************************
*
* ValNodeAddStr (head, choice, str)
* adds like ValNodeAdd()
* sets newnode->choice = choice (if choice does not matter, use 0)
* sets newnode->data.ptrvalue = str
* does NOT copy str
* if str == NULL, does not add a ValNode
*
*****************************************************************************/
NLM_EXTERN ValNodePtr LIBCALL ValNodeAddStr (ValNodePtr PNTR head, Nlm_Int2 choice, Nlm_CharPtr str)
{
ValNodePtr newnode;
if (str == NULL) return NULL;
newnode = ValNodeAdd(head);
if (newnode != NULL)
{
newnode->choice = (Nlm_Uint1)choice;
newnode->data.ptrvalue = (Nlm_VoidPtr)str;
}
return newnode;
}
/*****************************************************************************
*
* ValNodeCopyStr (head, choice, str)
* adds like ValNodeAdd()
* sets newnode->choice = choice (if choice does not matter, use 0)
* sets newnode->data.ptrvalue = str
* makes a COPY of str
* if str == NULL, does not add a ValNode
*
*****************************************************************************/
NLM_EXTERN ValNodePtr LIBCALL ValNodeCopyStr (ValNodePtr PNTR head, Nlm_Int2 choice, const char* str)
{
ValNodePtr newnode;
if (str == NULL) return NULL;
newnode = ValNodeAdd(head);
if (newnode != NULL)
{
newnode->choice = (Nlm_Uint1)choice;
newnode->data.ptrvalue = StringSave(str);
}
return newnode;
}
/*****************************************************************************
*
* ValNodeAddInt (head, choice, value)
* adds like ValNodeAdd()
* sets newnode->choice = choice (if choice does not matter, use 0)
* sets newnode->data.intvalue = value
*
*****************************************************************************/
NLM_EXTERN ValNodePtr LIBCALL ValNodeAddInt (ValNodePtr PNTR head, Nlm_Int2 choice, Nlm_Int4 value)
{
ValNodePtr newnode;
newnode = ValNodeAdd(head);
if (newnode != NULL)
{
newnode->choice = (Nlm_Uint1)choice;
newnode->data.intvalue = value;
}
return newnode;
}
/*****************************************************************************
*
* ValNodeAddBigInt (head, choice, value)
* adds like ValNodeAdd()
* sets newnode->choice = choice (if choice does not matter, use 0)
* sets newnode->data.bigintvalue = value
*
*****************************************************************************/
NLM_EXTERN ValNodePtr LIBCALL ValNodeAddBigInt (ValNodePtr PNTR head, Nlm_Int2 choice, Nlm_Int8 value)
{
ValNodePtr newnode;
newnode = ValNodeAdd(head);
if (newnode != NULL)
{
newnode->choice = (Nlm_Uint1)choice;
newnode->data.bigintvalue = value;
}
return newnode;
}
/*****************************************************************************
*
* ValNodeAddBoolean (head, choice, value)
* adds like ValNodeAdd()
* sets newnode->choice = choice (if choice does not matter, use 0)
* sets newnode->data.boolvalue = value
*
*****************************************************************************/
NLM_EXTERN ValNodePtr LIBCALL ValNodeAddBoolean (ValNodePtr PNTR head, Nlm_Int2 choice, Nlm_Boolean value)
{
ValNodePtr newnode;
newnode = ValNodeAdd(head);
if (newnode != NULL)
{
newnode->choice = (Nlm_Uint1)choice;
newnode->data.boolvalue = value;
}
return newnode;
}
/*****************************************************************************
*
* ValNodeAddFloat (head, choice, value)
* adds like ValNodeAdd()
* sets newnode->choice = choice (if choice does not matter, use 0)
* sets newnode->data.realvalue = value
*
*****************************************************************************/
NLM_EXTERN ValNodePtr LIBCALL ValNodeAddFloat (ValNodePtr PNTR head, Nlm_Int2 choice, Nlm_FloatHi value)
{
ValNodePtr newnode;
newnode = ValNodeAdd(head);
if (newnode != NULL)
{
newnode->choice = (Nlm_Uint1)choice;
newnode->data.realvalue = value;
}
return newnode;
}
/*****************************************************************************
*
* ValNodeAddPointer (head, choice, value)
* adds like ValNodeAdd()
* sets newnode->choice = choice (if choice does not matter, use 0)
* sets newnode->data.ptrvalue = value
*
*****************************************************************************/
NLM_EXTERN ValNodePtr LIBCALL ValNodeAddPointer (ValNodePtr PNTR head, Nlm_Int2 choice, Nlm_VoidPtr value)
{
ValNodePtr newnode;
newnode = ValNodeAdd(head);
if (newnode != NULL)
{
newnode->choice = (Nlm_Uint1)choice;
newnode->data.ptrvalue = value;
}
return newnode;
}
/*****************************************************************************
*
* ValNodeAddFunction (head, choice, value)
* adds like ValNodeAdd()
* sets newnode->choice = choice (if choice does not matter, use 0)
* sets newnode->data.funcvalue = value
*
*****************************************************************************/
NLM_EXTERN ValNodePtr LIBCALL ValNodeAddFunction (ValNodePtr PNTR head, Nlm_Int2 choice, Nlm_FnPtr value)
{
ValNodePtr newnode;
newnode = ValNodeAdd(head);
if (newnode != NULL)
{
newnode->choice = (Nlm_Uint1)choice;
newnode->data.funcvalue = value;
}
return newnode;
}
/*****************************************************************************
*
* ValNodeFree(vnp)
* frees whole chain of ValNodes
* Does NOT free associated data pointers
* see ValNodeFreeData()
*
*****************************************************************************/
NLM_EXTERN ValNodePtr LIBCALL ValNodeFree (ValNodePtr vnp)
{
ValNodePtr next;
while (vnp != NULL)
{
next = vnp->next;
Nlm_MemFree(vnp);
vnp = next;
}
return NULL;
}
/*****************************************************************************
*
* ValNodeFreeData(vnp)
* frees whole chain of ValNodes
* frees associated data pointers - BEWARE of this if these are not
* allocated single memory block structures.
*
*****************************************************************************/
NLM_EXTERN ValNodePtr LIBCALL ValNodeFreeData (ValNodePtr vnp)
{
ValNodePtr next;
while (vnp != NULL)
{
Nlm_MemFree(vnp->data.ptrvalue);
next = vnp->next;
Nlm_MemFree(vnp);
vnp = next;
}
return NULL;
}
/*****************************************************************************
*
* ValNodePtr ValNodeExtract(headptr, choice)
* removes first node in chain where ->choice == choice
* rejoins chain after removing the node
* sets node->next to NULL
*
*****************************************************************************/
NLM_EXTERN ValNodePtr LIBCALL ValNodeExtract (ValNodePtr PNTR headptr, Nlm_Int2 choice)
{
ValNodePtr last = NULL,
vnp = * headptr;
while (vnp != NULL)
{
if (vnp->choice == (Nlm_Uint1)choice)
{
if (last == NULL) /* first one */
* headptr = vnp->next;
else
last->next = vnp->next;
vnp->next = NULL;
return vnp;
}
else
{
last = vnp;
vnp = vnp->next;
}
}
return NULL; /* not found */
}
/*****************************************************************************
*
* ValNodePtr ValNodeExtractList(headptr, choice)
* removes ALL nodes in chain where ->choice == choice
* rejoins chain after removing the nodes
* returns independent chain of extracted nodes
*
*****************************************************************************/
NLM_EXTERN ValNodePtr LIBCALL ValNodeExtractList (ValNodePtr PNTR headptr, Nlm_Int2 choice)
{
ValNodePtr last = NULL, first = NULL, vnp;
while ((vnp = ValNodeExtract(headptr, choice)) != NULL)
{
if (last == NULL)
{
last = vnp;
first = vnp;
}
else
last->next = vnp;
last = vnp;
}
return first;
}
/*****************************************************************************
*
* ValNodeFindNext (head, curr, choice)
* Finds next ValNode with vnp->choice == choice after curr
* If curr == NULL, starts at head of list
* If choice < 0 , returns all ValNodes
* Returns NULL, when no more found
*
*****************************************************************************/
NLM_EXTERN ValNodePtr LIBCALL ValNodeFindNext (ValNodePtr head, ValNodePtr curr, Nlm_Int2 choice)
{
if (head == NULL) return NULL;
if (curr == NULL)
curr = head;
else
curr = curr->next;
while (curr != NULL)
{
if ((choice < 0) || (curr->choice == (Nlm_Uint1)choice))
return curr;
curr = curr->next;
}
return curr;
}
/*****************************************************************************
*
* ValNodeSort(list, compar)
* Copied from SortValNode in jzcoll, renamed, for more general access
* Makes array from ValNode list, calls HeapSort, reconnects ValNode list
*
*****************************************************************************/
NLM_EXTERN ValNodePtr LIBCALL ValNodeSort (ValNodePtr list, int (LIBCALLBACK *compar )PROTO ((Nlm_VoidPtr, Nlm_VoidPtr )))
{
ValNodePtr tmp, PNTR head;
Nlm_Int4 count, i;
if (list == NULL) return NULL;
count = ValNodeLen (list);
head = (ValNodePtr *) MemNew (((size_t) count + 1) * sizeof (ValNodePtr));
for (tmp = list, i = 0; tmp != NULL && i < count; i++) {
head [i] = tmp;
tmp = tmp->next;
}
HeapSort (head, (size_t) count, sizeof (ValNodePtr), compar);
for (i = 0; i < count; i++) {
tmp = head [i];
tmp->next = head [i + 1];
}
list = head [0];
MemFree (head);
return list;
}
NLM_EXTERN void LIBCALL
ValNodeUnique
(ValNodePtr PNTR list,
int (LIBCALLBACK *compar )PROTO ((Nlm_VoidPtr, Nlm_VoidPtr )),
ValNodePtr (LIBCALLBACK *valnodefree ) PROTO ((ValNodePtr)))
{
ValNodePtr vnp, tmp;
if (list == NULL || *list == NULL || compar == NULL || valnodefree == NULL) return;
vnp = *list;
while (vnp->next != NULL) {
if (compar (&vnp, &(vnp->next)) == 0) {
tmp = vnp->next;
vnp->next = tmp->next;
tmp->next = NULL;
tmp = valnodefree (tmp);
} else {
vnp = vnp->next;
}
}
}
NLM_EXTERN void LIBCALL
ValNodeInsert
(ValNodePtr PNTR list,
ValNodePtr new_item,
int (LIBCALLBACK *compar )PROTO ((Nlm_VoidPtr, Nlm_VoidPtr )))
{
ValNodePtr vnp, vnp_prev = NULL;
if (list == NULL || new_item == NULL) {
return;
}
if (*list == NULL) {
*list = new_item;
} else if (compar == NULL) {
ValNodeLink (list, new_item);
} else {
vnp = *list;
while (vnp != NULL && compar (&vnp, &new_item) < 1) {
vnp_prev = vnp;
vnp = vnp->next;
}
if (vnp_prev == NULL) {
new_item->next= *list;
*list = new_item;
} else {
new_item->next= vnp_prev->next;
vnp_prev->next= new_item;
}
}
}
NLM_EXTERN void LIBCALL
ValNodePurge
(ValNodePtr PNTR list,
Nlm_Boolean (LIBCALLBACK *do_remove ) PROTO ((ValNodePtr)),
ValNodePtr (LIBCALLBACK *valnodefree ) PROTO ((ValNodePtr)))
{
ValNodePtr vnp_prev = NULL, vnp, vnp_next;
if (list == NULL || do_remove == NULL) {
return;
}
for (vnp = *list; vnp != NULL; vnp = vnp_next) {
vnp_next = vnp->next;
if (do_remove (vnp)) {
if (vnp_prev == NULL) {
*list = vnp_next;
} else {
vnp_prev->next = vnp_next;
}
vnp->next = NULL;
if (valnodefree == NULL) {
vnp = ValNodeFree (vnp);
} else {
vnp = valnodefree (vnp);
}
} else {
vnp_prev = vnp;
}
}
}
NLM_EXTERN int LIBCALL ValNodeCompare PROTO ((ValNodePtr vnp1, ValNodePtr vnp2, int (LIBCALLBACK *compar) (Nlm_VoidPtr, Nlm_VoidPtr)))
{
int rval = 0;
if (compar == NULL) {
return 0;
}
while (vnp1 != NULL && vnp2 != NULL && rval == 0)
{
rval = compar (&vnp1, &vnp2);
vnp1 = vnp1->next;
vnp2 = vnp2->next;
}
if (rval == 0)
{
if (vnp1 == NULL && vnp2 == NULL)
{
rval = 0;
}
else if (vnp1 == NULL)
{
rval = -1;
}
else
{
rval = 1;
}
}
return rval;
}
/*****************************************************************************
*
* ValNodeMergeStrs(list)
* Merges chain of val node strings into a single character array
*
*****************************************************************************/
NLM_EXTERN Nlm_CharPtr LIBCALL ValNodeMergeStrs (ValNodePtr list)
{
size_t len;
Nlm_CharPtr ptr;
Nlm_CharPtr str;
Nlm_CharPtr tmp;
ValNodePtr vnp;
if (list == NULL) return NULL;
for (vnp = list, len = 0; vnp != NULL; vnp = vnp->next) {
str = (Nlm_CharPtr) vnp->data.ptrvalue;
len += Nlm_StringLen (str);
}
if (len == 0) return NULL;
ptr = Nlm_MemNew (sizeof (Nlm_Char) * (len + 2));
if (ptr == NULL) return NULL;
for (vnp = list, tmp = ptr; vnp != NULL; vnp = vnp->next) {
str = (Nlm_CharPtr) vnp->data.ptrvalue;
tmp = Nlm_StringMove (tmp, str);
}
return ptr;
}
/*****************************************************************************
*
* Start Of Node List Functions
*
*****************************************************************************/
NLM_EXTERN ValNodePtr LIBCALL NodeListNew (void)
{
ValNodePtr vnp;
vnp = ValNodeNew (NULL);
return vnp;
}
NLM_EXTERN ValNodePtr LIBCALL NodeListFree (ValNodePtr head)
{
if (head != NULL) {
ValNodeFreeData (head);
}
return NULL;
}
NLM_EXTERN Nlm_Int2 LIBCALL NodeListLen (ValNodePtr head)
{
Nlm_Int2 item;
item = 0;
if (head != NULL) {
while (head->next != NULL) {
head = head->next;
item++;
}
}
return item;
}
NLM_EXTERN ValNodePtr LIBCALL NodeListFind (ValNodePtr head, Nlm_Int2 item, Nlm_Boolean extend)
{
ValNodePtr vnp;
vnp = NULL;
if (head != NULL && item > 0) {
vnp = head;
while (vnp->next != NULL && item > 0) {
vnp = vnp->next;
item--;
}
if (extend) {
while (item > 0) {
vnp = ValNodeNew (vnp);
item--;
}
} else if (item > 0) {
vnp = NULL;
}
}
return vnp;
}
NLM_EXTERN Nlm_Boolean LIBCALL NodeListRead (ValNodePtr head, Nlm_Int2 item, Nlm_VoidPtr ptr, size_t size)
{
Nlm_Boolean copied;
Nlm_BytePtr dst;
Nlm_BytePtr src;
ValNodePtr vnp;
copied = FALSE;
if (head != NULL && item > 0 && ptr != NULL) {
vnp = NodeListFind (head, item, FALSE);
if (vnp != NULL && vnp->data.ptrvalue != NULL) {
dst = (Nlm_BytePtr) ptr;
src = (Nlm_BytePtr) (vnp->data.ptrvalue);
while (size > 0) {
*dst = *src;
dst++;
src++;
size--;
}
copied = TRUE;
} else {
Nlm_MemFill (ptr, 0, size);
}
}
return copied;
}
static Nlm_Boolean LIBCALL Nlm_WriteToNode (ValNodePtr vnp, Nlm_VoidPtr ptr, size_t size)
{
Nlm_Boolean copied;
Nlm_BytePtr dst;
Nlm_BytePtr src;
copied = FALSE;
if (vnp != NULL) {
vnp->data.ptrvalue = MemFree (vnp->data.ptrvalue);
if (ptr != NULL) {
vnp->data.ptrvalue = MemNew (size);
if (vnp->data.ptrvalue != NULL) {
dst = (Nlm_BytePtr) (vnp->data.ptrvalue);
src = (Nlm_BytePtr) ptr;
while (size > 0) {
*dst = *src;
dst++;
src++;
size--;
}
copied = TRUE;
}
}
}
return copied;
}
NLM_EXTERN Nlm_Boolean LIBCALL NodeListWrite (ValNodePtr head, Nlm_Int2 item, Nlm_VoidPtr ptr, size_t size)
{
Nlm_Boolean copied;
ValNodePtr vnp;
copied = FALSE;
if (head != NULL && item > 0 && ptr != NULL) {
vnp = NodeListFind (head, item, TRUE);
copied = Nlm_WriteToNode (vnp, ptr, size);
}
return copied;
}
NLM_EXTERN Nlm_Boolean LIBCALL NodeListAppend (ValNodePtr head, Nlm_VoidPtr ptr, size_t size)
{
Nlm_Boolean copied;
ValNodePtr vnp;
copied = FALSE;
if (head != NULL && ptr != NULL) {
vnp = ValNodeNew (head);
copied = Nlm_WriteToNode (vnp, ptr, size);
}
return copied;
}
NLM_EXTERN Nlm_Boolean LIBCALL NodeListInsert (ValNodePtr head, Nlm_Int2 item, Nlm_VoidPtr ptr, size_t size)
{
Nlm_Boolean copied;
ValNodePtr prev;
ValNodePtr vnp;
copied = FALSE;
if (head != NULL && item > 0 && ptr != NULL) {
if (item > 1) {
prev = NodeListFind (head, (Nlm_Int2)(item - 1), FALSE);
} else {
prev = head;
}
if (prev != NULL) {
vnp = ValNodeNew (NULL);
if (vnp != NULL) {
vnp->next = prev->next;
prev->next = vnp;
copied = Nlm_WriteToNode (vnp, ptr, size);
}
}
}
return copied;
}
NLM_EXTERN Nlm_Boolean LIBCALL NodeListReplace (ValNodePtr head, Nlm_Int2 item, Nlm_VoidPtr ptr, size_t size)
{
Nlm_Boolean copied;
ValNodePtr vnp;
copied = FALSE;
if (head != NULL && item > 0 && ptr != NULL) {
vnp = NodeListFind (head, item, FALSE);
copied = Nlm_WriteToNode (vnp, ptr, size);
}
return copied;
}
NLM_EXTERN Nlm_Boolean LIBCALL NodeListDelete (ValNodePtr head, Nlm_Int2 item)
{
Nlm_Boolean deleted;
ValNodePtr prev;
ValNodePtr vnp;
deleted = FALSE;
if (head != NULL && item > 0) {
if (item > 1) {
prev = NodeListFind (head, (Nlm_Int2)(item - 1), FALSE);
} else {
prev = head;
}
if (prev != NULL) {
vnp = prev->next;
if (vnp != NULL) {
prev->next = vnp->next;
Nlm_MemFree (vnp->data.ptrvalue);
Nlm_MemFree (vnp);
deleted = TRUE;
}
}
}
return deleted;
}
/*****************************************************************************
*
* End Of Node List Functions
*
*****************************************************************************/
#if defined(OS_MAC) || defined(OS_UNIX_DARWIN)
/* C2PStr() and P2CStr() may or may not exist in Carbon, so we now always roll our own. */
void Nlm_CtoPstr (Nlm_CharPtr str)
{
char *ioStr = (char *) str;
size_t len = strlen(ioStr);
if (len > 255) {
len = 255;
}
memmove(ioStr + 1, ioStr, len);
ioStr[0] = len;
}
void Nlm_PtoCstr (Nlm_CharPtr str)
{
StringPtr ioStr = (StringPtr) str;
Byte len = ioStr[0];
memmove(ioStr, ioStr + 1, len);
ioStr[len] = '\0';
}
#endif
NLM_EXTERN Nlm_Uint2 Nlm_SwitchUint2 (Nlm_Uint2 value)
{
Nlm_Uint2 m;
m = ((value & (Nlm_Uint2)0xFF00) >> 8);
m |= ((value & (Nlm_Uint2)0x00FF) << 8);
return m;
}
NLM_EXTERN void Nlm_SwitchUint2Buff (Nlm_Uint2 *buff, int count)
{
Nlm_Uint2 *ptr, n, m;
for (ptr=buff; count >0; --count)
{
n = *ptr;
m = ((n & (Nlm_Uint2)0xFF00) >> 8);
m |= ((n & (Nlm_Uint2)0x00FF) << 8);
*ptr++ = m;
}
}
NLM_EXTERN unsigned long Nlm_SwitchLong (unsigned long value)
{
unsigned long m;
m = ((value & (unsigned long)0xFF000000) >> 24);
m |= ((value & (unsigned long)0x00FF0000) >> 8);
m |= ((value & (unsigned long)0x0000FF00) << 8);
m |= ((value & (unsigned long)0x000000FF) << 24);
return m;
}
NLM_EXTERN void Nlm_SwitchLongBuff (unsigned long *buff, int count)
{
unsigned long *ptr, n, m;
for (ptr=buff; count >0; --count)
{
n = *ptr;
m = ((n & (unsigned long)0xFF000000) >> 24);
m |= ((n & (unsigned long)0x00FF0000) >> 8);
m |= ((n & (unsigned long)0x0000FF00) << 8);
m |= ((n & (unsigned long)0x000000FF) << 24);
*ptr++ = m;
}
}
NLM_EXTERN Nlm_Uint4 Nlm_SwitchUint4 (Nlm_Uint4 value)
{
Nlm_Uint4 m;
m = ((value & (Nlm_Uint4)0xFF000000) >> 24);
m |= ((value & (Nlm_Uint4)0x00FF0000) >> 8);
m |= ((value & (Nlm_Uint4)0x0000FF00) << 8);
m |= ((value & (Nlm_Uint4)0x000000FF) << 24);
return m;
}
NLM_EXTERN void Nlm_SwitchUint4Buff (Nlm_Uint4 *buff, int count)
{
Nlm_Uint4 *ptr, n, m;
for (ptr=buff; count >0; --count)
{
n = *ptr;
m = ((n & (Nlm_Uint4)0xFF000000) >> 24);
m |= ((n & (Nlm_Uint4)0x00FF0000) >> 8);
m |= ((n & (Nlm_Uint4)0x0000FF00) << 8);
m |= ((n & (Nlm_Uint4)0x000000FF) << 24);
*ptr++ = m;
}
}
/* the following ListXXX() functions previously resided in ni_list.c */
/*
* Purpose: Insert an item as the next element in a doubly linked list(ring)
*
* Parameters:
* elem Next element to be inserted; this is data only,not a NodePtr
* ap Insertion point
*
* Returns:
* The newly allocated NodePtr, containing forward and backward
* pointers and a pointer to elem
*
*
* Description:
* Allocate the necessary memory for a "Node", attach the
* caller's data to that Node, and insert the Node after the
* specified node in the list, maintaining the integrity of
* a doubly-linked ring. If there are no other items in the
* ring, create a "minimal" ring which consists of the single
* Node pointing to itself in both directions.
*
* Note:
* Most "list" data is actually stored in a doubly-linked ring, as
* shown below. Furthermore, note that each node only contains a
* pointer to the actual data in the list, rather than the actual
* data itself.
*
* +------------------------------------------------------------------+
* ^ |
* | +-------------------------------------------------------+ |
* | | ^ |
* | V | |
* | +-------+ +-------+ +-------+ | |
* | | next |------>| next |------> ... ------->| next |-->+ |
* | +-------+ +-------+ +-------+ |
* +<--| last |<------| last |<------ ... <-------| last |<-----+
* +-------+ +-------+ +-------+
* | elem | | elem | | elem |
* +-------+ +-------+ +-------+
* | | |
* | | |
* V V V
* +-------+ +-------+ +-------+
* | actual| | actual| | actual|
* | data | | data | | data |
* +-------+ +-------+ +-------+
*/
NLM_EXTERN NodePtr LIBCALL
ListInsert(Nlm_VoidPtr elem, NodePtr ap) /* ptr to node to insert after */
{
NodePtr np;
if (elem == NULL)
return NULL;
np = (NodePtr) MemNew(sizeof(Node));
np->elem = elem;
if (ap == NULL) { /* no nodes in list */
np->last = np;
np->next = np;
return np;
}
else { /* 1 or more nodes in list */
np->next = ap->next;
ap->next = np;
np->next->last = np;
np->last = ap;
return np;
}
} /* ListInsert */
/*
* Purpose: Insert an item as the previous element in a doubly linked
* list(ring)
*
* Parameters:
* elem Next element to be inserted; this is data only,not a NodePtr
* ap Insertion point
*
* Returns:
* The newly allocated NodePtr, containing forward and backward
* pointers and a pointer to elem
*
*
* Description:
* Insert the specified item into the ring, before the specified
* insertion point. In the case where the specified insertion
* point was NULL, this is equivalent to ListInsert().
*/
NLM_EXTERN NodePtr LIBCALL
ListInsertPrev(Nlm_VoidPtr elem, NodePtr ap) /* ptr to node to insert before */
{
NodePtr np;
np = ap;
if (ap != NULL)
ap = ap->last; /* previous node */
ap = ListInsert(elem, ap);
return (np == NULL) ? ap : np;
} /* ListInsertPrev */
/*
* Purpose: Delete a single node from a list or ring
*
* Parameters:
* np Node to be deleted
*
* Returns:
* A pointer to the "next" node in the list/ring, after the
* deleted node.
*
*
* Description:
* Delete the specified node from a list or ring. It is the
* responsibility of the caller to free the memory associated
* with the "elem" (data), if appropriate.
*/
NLM_EXTERN NodePtr LIBCALL
ListDelete(NodePtr np)
{
NodePtr nextnode, lastnode;
if (np == NULL)
return NULL;
nextnode = np->next;
lastnode = np->last;
if (nextnode == NULL && lastnode == NULL) /* only node in a list */
;
else if (nextnode == NULL) { /* last in a list */
np->last->next = NULL;
nextnode = np->last;
}
else if (lastnode == NULL) { /* first in a list */
np->next->last = NULL;
nextnode = np->next;
}
else if (np == nextnode) /* last in a ring */
nextnode = NULL;
else { /* node with both neighbors */
np->last->next = nextnode;
np->next->last = np->last;
}
MemFree(np); /* assumes element memory has been freed */
return nextnode;
} /* ListDelete */
/*
* Purpose: Get the next element from a list or ring (non-destructively)
*
* Parameters:
* np Node before the node to be selected
*
* Returns:
* A pointer to the "next" node in the list/ring (or NULL
* if the list/ring was NULL). Note that for a list, the
* returned value can also be NULL.
*
*
* Description:
* Return the "next" node in the list or rin.g
*/
NLM_EXTERN NodePtr LIBCALL
ListGetNext(NodePtr np)
{
if (np == NULL)
return NULL;
return np->next;
} /* ListGetNext */
/*
* Purpose: Swap two adjacent nodes in a list or ring
*
* Parameters:
* np1 "Prior" node
* np2 "Next" node
*
*
* Description:
* Swap the two specified elements, provided that they are
* adjacent, and np1 precedes np2.
*/
NLM_EXTERN void LIBCALL
ListSwapAdj(NodePtr np1, NodePtr np2) /* priornode, nextnode */
{
if (np1 == NULL || np2 == NULL || np1->next->last != np1) /* must be sane */
return;
if (np1->next != np2 || np2->last != np1) /* must be in order */
return;
if (np1->last != NULL)
np1->last->next = np2;
if (np2->next != NULL)
np2->next->last = np1;
np1->next = np2->next;
np2->last = np1->last;
np1->last = np2;
np2->next = np1;
} /* ListSwapAdj */
/*
* Purpose: Sort the specified ring/list
*
* Parameters:
* head Head of the list to be sorted
* cmpfunc Comparison function (return values are like memcmp())
* order ASCEND or DESCEND
*
* Returns:
* A pointer to the first element of the sorted ring or list
*
*
* Description:
* Sort the specified list, in place, using bubble sort, and
* the specified comparison function. Determine prior to sorting
* whether this is a list or a ring. If it's a ring, break the
* ring prior to sorting, and restore it to a ring topology
* after sorting has been completed.
*/
NLM_EXTERN NodePtr LIBCALL
ListSort(NodePtr head, int (*cmpfunc )PROTO ((NodePtr, NodePtr )), int order)
/* 0 if equal, LT 0 if 1st element > 2nd element */
{
NodePtr np;
Nlm_Boolean sorted = FALSE, ring;
int result;
if (head == NULL)
return NULL;
if (head->last == NULL)
ring = FALSE;
else
ring = TRUE;
if (ring)
ListBreakRing(head);
/* just bubble sort for now */
while (! sorted) {
np = head;
sorted = TRUE;
while (np->next != NULL) {
result = (*cmpfunc)(np, np->next);
if ((result > 0 && order == ASCEND) || (result < 0 && order == DESCEND)) {
sorted = FALSE;
if (np == head)
head = np->next; /* keep head pointing at 1st element */
ListSwapAdj(np, np->next);
}
else
np = np->next;
}
}
if (ring)
ListConnectRing(head);
return head; /* ptr to first element */
} /* ListSort */
/*
* Purpose: Break the specified ring into a non-circular (linear) list
*
* Parameters:
* np Head of the ring to be broken
*
*
* Description:
* Break the specified ring between its head and tail.
*
* Note:
* This function may be called safely (without effect) if the
* passed parameter is already a list, rather than a ring.
*/
NLM_EXTERN void LIBCALL
ListBreakRing(NodePtr np)
{
if (np == NULL)
return;
if (np->last == NULL)
return;
np->last->next = NULL;
np->last = NULL;
} /* ListBreakRing */
/*
* Purpose: Convert a list into a ring.
*
* Parameters:
* head Head of the list to be connected
*
*
* Description:
* Connect the specified list between its head and tail, producing
* a ring.
*
* Note:
* This function may be called safely (without effect) if the
* passed parameter is already a ring, rather than a list.
*/
NLM_EXTERN void LIBCALL
ListConnectRing(NodePtr head)
{
NodePtr np;
if (head == NULL)
return;
np = head;
while (np->next != NULL) {
np = np->next;
if (np == head)
return;
}
np->next = head;
head->last = np;
} /* ListConnectRing */
/*
* Purpose: Copy a list where the list elements are character strings
*
* Parameters:
* strlist List to be copied
*
* Returns:
* A copy of the original list (which may be NULL)
*
*
* Description:
* Create a list which is a copy of the original list, and
* also make copies of the strings.
*
* Note:
* There is no obvious way to make a generic list copying
* routine, because, in general, the length of each list
* element is unknown. This is a simple case where it is
* easy to copy a list.
*/
NLM_EXTERN NodePtr LIBCALL
ListStrCopy (NodePtr strlist)
{
NodePtr newlist = NULL;
NodePtr np = strlist;
Nlm_CharPtr stringtext;
if (strlist == NULL)
return NULL;
do {
stringtext = StringSave((Nlm_CharPtr) np->elem);
newlist = ListInsert(stringtext, newlist);
np = ListGetNext(np);
} while (np != NULL && np != strlist);
return newlist->next; /* points to 1st element in new list */
}
/*
* Purpose: Delete a list where the list elements are character strings
*
* Parameters:
* np List to be deleted
*
*
* Description:
* Delete the list nodes and the character string data associated
* with each node.
*
* Note:
* This routine will work for any list element which is a single
* block of memory. However, it will not work in the more general
* case where a list element in turn references other memory
* which must also be freed.
*/
NLM_EXTERN void LIBCALL
ListStrDel (NodePtr np)
{
while (np != NULL)
{
MemFree (np->elem);
np = ListDelete(np);
}
}
NLM_EXTERN const Nlm_Char* Nlm_PlatformName(void)
{
/* Mac */
#if defined(OS_MAC)
# if defined(PROC_PPC)
return "MacOS_PPC";
# elif defined(PROC_I80X86)
return "MacOS_386";
# elif defined(PROC_MC680X0)
return "MacOS_68K";
# else
return "MacOS";
# endif
/* VMS */
#elif defined(OS_VMS)
# if defined(OS_AXP_VMS)
return "AXP/OpenVMS";
# else
return "VMS";
# endif
/* UNIX */
#elif defined(OS_UNIX)
# if defined(PROC_IBM370)
return "IBM370_AIX";
# elif defined(OS_UNIX_SUN)
return "SunOS";
# elif defined(OS_UNIX_SOL)
return "Solaris";
# elif defined(OS_UNIX_OSF1) && defined(PROC_ALPHA)
return "Alpha_OSF1";
# elif defined(COMP_AUX)
return "Mac_AUX";
# elif defined(COMP_CRAY) && defined(PROC_YMP)
return "Cray";
# elif defined(PROC_CONVEX)
return "Convex";
# elif defined(PROC_HPPA) && !defined(OS_UNIX_LINUX)
return "HPUX";
# elif defined(OS_UNIX_NEXT)
return "NEXT";
# elif defined(PROC_MIPS) && !defined(OS_UNIX_LINUX)
return "SGI_MIPS";
# elif defined(OS_UNIX_ULTRIX)
return "ULTRIX";
# elif defined(OS_UNIX_SYSV) && defined(PROC_SPARC)
return "SystemV_SPARC";
# elif defined(OS_UNIX_AIX)
return "AIX";
# elif defined(OS_UNIX_LINUX)
# if defined(PROC_ALPHA)
return "LINUX_ALPHA";
# else
return "LINUX";
# endif
# elif defined(OS_UNIX_NETBSD)
return "NetBSD";
# elif defined(OS_UNIX_FREEBSD)
return "FreeBSD";
# else
return "UNIX";
# endif
/* PC */
#elif defined(OS_MSWIN)
# if defined(WIN16)
return "WIN16";
# elif defined(WIN32)
return "WIN32";
# else
return "MSWIN";
# endif
/* NT */
#elif defined(OS_WINNT)
return "WIN_NT";
/* Unknown */
#else
return "Unknown";
#endif
}
/**
* MD5 stuff (used for security purposes)
*/
/*
* Note: this code is harmless on little-endian machines.
*/
#ifdef IS_BIG_ENDIAN
static
void
byteReverse(Nlm_UcharPtr buf, Nlm_Uint4 longs)
{
Nlm_Uint4 t;
do {
t = (Nlm_Uint4) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
((unsigned) buf[1] << 8 | buf[0]);
*(Nlm_Uint4Ptr) buf = t;
buf += 4;
} while (--longs);
}
#endif
/*
* Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
* initialization constants.
*/
NLM_EXTERN void LIBCALL
Nlm_MD5Init(Nlm_MD5ContextPtr ctx)
{
ctx->buf[0] = 0x67452301;
ctx->buf[1] = 0xefcdab89;
ctx->buf[2] = 0x98badcfe;
ctx->buf[3] = 0x10325476;
ctx->bits[0] = 0;
ctx->bits[1] = 0;
}
/*
* Update context to reflect the concatenation of another buffer full
* of bytes.
*/
NLM_EXTERN void LIBCALL
Nlm_MD5Update(Nlm_MD5ContextPtr ctx, Nlm_UcharPtr buf, Nlm_Uint4 len)
{
Nlm_Uint4 t;
/* Update bitcount */
t = ctx->bits[0];
if ((ctx->bits[0] = t + ((Nlm_Uint4) len << 3)) < t)
ctx->bits[1]++; /* Carry from low to high */
ctx->bits[1] += len >> 29;
t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
/* Handle any leading odd-sized chunks */
if (t) {
Nlm_UcharPtr p = (Nlm_UcharPtr) ctx->in + t;
t = 64 - t;
if (len < t) {
memcpy(p, buf, len);
return;
}
memcpy(p, buf, t);
#ifdef IS_BIG_ENDIAN
byteReverse(ctx->in, 16);
#endif
Nlm_MD5Transform(ctx->buf, (Nlm_Uint4Ptr) ctx->in);
buf += t;
len -= t;
}
/* Process data in 64-byte chunks */
while (len >= 64) {
memcpy(ctx->in, buf, 64);
#ifdef IS_BIG_ENDIAN
byteReverse(ctx->in, 16);
#endif
Nlm_MD5Transform(ctx->buf, (Nlm_Uint4Ptr) ctx->in);
buf += 64;
len -= 64;
}
/* Handle any remaining bytes of data. */
memcpy(ctx->in, buf, len);
}
/*
* Final wrapup - pad to 64-byte boundary with the bit pattern
* 1 0* (64-bit count of bits processed, MSB-first)
*/
NLM_EXTERN void LIBCALL
Nlm_MD5Final(Nlm_MD5ContextPtr ctx, Nlm_Uchar digest[16])
{
Nlm_Uint4 count;
Nlm_UcharPtr p;
/* Compute number of bytes mod 64 */
count = (ctx->bits[0] >> 3) & 0x3F;
/* Set the first char of padding to 0x80. This is safe since there is
always at least one byte free */
p = ctx->in + count;
*p++ = 0x80;
/* Bytes of padding needed to make 64 bytes */
count = 64 - 1 - count;
/* Pad out to 56 mod 64 */
if (count < 8) {
/* Two lots of padding: Pad the first block to 64 bytes */
memset(p, 0, count);
#ifdef IS_BIG_ENDIAN
byteReverse(ctx->in, 16);
#endif
Nlm_MD5Transform(ctx->buf, (Nlm_Uint4Ptr) ctx->in);
/* Now fill the next block with 56 bytes */
memset(ctx->in, 0, 56);
} else {
/* Pad block to 56 bytes */
memset(p, 0, count - 8);
}
#ifdef IS_BIG_ENDIAN
byteReverse(ctx->in, 14);
#endif
/* Append length in bits and transform */
((Nlm_Uint4Ptr) ctx->in)[14] = ctx->bits[0];
((Nlm_Uint4Ptr) ctx->in)[15] = ctx->bits[1];
Nlm_MD5Transform(ctx->buf, (Nlm_Uint4Ptr) ctx->in);
#ifdef IS_BIG_ENDIAN
byteReverse((Nlm_UcharPtr) ctx->buf, 4);
#endif
memcpy(digest, ctx->buf, 16);
memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */
}
/* The four core functions - F1 is optimized somewhat */
/* #define F1(x, y, z) (x & y | ~x & z) */
#define F1(x, y, z) (z ^ (x & (y ^ z)))
#define F2(x, y, z) F1(z, x, y)
#define F3(x, y, z) (x ^ y ^ z)
#define F4(x, y, z) (y ^ (x | ~z))
/* This is the central step in the MD5 algorithm. */
#define MD5STEP(f, w, x, y, z, data, s) \
( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
/*
* The core of the MD5 algorithm, this alters an existing MD5 hash to
* reflect the addition of 16 longwords of new data. MD5Update blocks
* the data and converts bytes into longwords for this routine.
*/
NLM_EXTERN void LIBCALL
Nlm_MD5Transform(Nlm_Uint4 buf[4], Nlm_Uint4 in[16])
{
register Nlm_Uint4 a, b, c, d;
a = buf[0];
b = buf[1];
c = buf[2];
d = buf[3];
MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
buf[0] += a;
buf[1] += b;
buf[2] += c;
buf[3] += d;
}
/*----- Here is simplified MD4 algorithm, that used in Blast E-mail server
and formatdb ------ */
#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
#define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
#define H(x, y, z) ((x) ^ (y) ^ (z))
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
#define FF(a, b, c, d, x, s) { \
(a) += F ((b), (c), (d)) + (x); \
(a) = ROTATE_LEFT ((a), (s)); \
}
#define GG(a, b, c, d, x, s) { \
(a) += G ((b), (c), (d)) + (x) + (Nlm_Uint4)0x5a827999; \
(a) = ROTATE_LEFT ((a), (s)); \
}
#define HH(a, b, c, d, x, s) { \
(a) += H ((b), (c), (d)) + (x) + (Nlm_Uint4)0x6ed9eba1; \
(a) = ROTATE_LEFT ((a), (s)); \
}
/* This function calculates checksum from a buffer */
Nlm_Uint4 Nlm_GetChecksum(Nlm_CharPtr p)
{
Nlm_Int4 len;
struct Hash {
Nlm_Uint4 x1;
Nlm_Uint4 x2;
Nlm_Uint4 x3;
Nlm_Uint4 x4;
Nlm_Uint4 x5;
Nlm_Uint4 x6;
Nlm_Uint4 x7;
Nlm_Uint4 x8;
Nlm_Uint4 x9;
Nlm_Uint4 x10;
Nlm_Uint4 x11;
Nlm_Uint4 x12;
} Hash;
Nlm_Uint4 a = 0x67452301;
Nlm_Uint4 b = 0xefcdab89;
Nlm_Uint4 c = 0x98badcfe;
Nlm_Uint4 d = 0x10325476;
if (p == NULL || p[0] == NULLB)
return 0;
MemSet(&Hash, 0, sizeof(Hash));
MemCopy(&Hash, p,
(len = StringLen(p)) > sizeof(Hash)? sizeof(Hash) : len);
FF (a, b, c, d, Hash.x1, 3); /* 1 */
FF (d, a, b, c, Hash.x2, 7); /* 2 */
FF (c, d, a, b, Hash.x3, 11); /* 3 */
FF (b, c, d, a, Hash.x4, 19); /* 4 */
GG (a, b, c, d, Hash.x5, 3); /* 17 */
GG (d, a, b, c, Hash.x6, 5); /* 18 */
GG (c, d, a, b, Hash.x7, 9); /* 19 */
GG (b, c, d, a, Hash.x8, 13); /* 20 */
HH (b, c, d, a, Hash.x9, 15); /* 40 */
HH (a, b, c, d, Hash.x10, 3); /* 41 */
HH (d, a, b, c, Hash.x11, 9); /* 42 */
HH (c, d, a, b, Hash.x12, 11); /* 43 */
return (a+b+c+d);
}
/* ----- End of simplified MD4 algorithm ------ */
|