Source

lp_solve / fortify.c

   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
/*
 * FILE:
 *   fortify.c
 *
 * DESCRIPTION:
 *     A fortified shell for malloc, realloc, calloc, strdup, getcwd, tempnam
 *     and free.
 *     To use Fortify, each source file will need to #include "fortify.h".  To
 * enable  Fortify,  define the symbol FORTIFY.  If FORTIFY is not defined, it
 * will compile away to nothing.  If you do not have stderr available, you may
 * wish  to  set  an  alternate output function.  See _Fortify_SetOutputFunc(),
 * below.
 *     You will also need to link in fortify.o
 *
 *     None of the functions in this file should really be called
 *   directly; they really should be called through the macros
 *   defined in fortify.h
 *
 */

#if defined FORTIFY

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <stdlib.h>

#if defined MSDOS || defined __BORLANDC__ || defined WIN32 || defined __HIGHC__
# include <direct.h>
#endif

extern int EndOfPgr(int);

#define __FORTIFY_C__ /* So fortify.h knows to not define the fortify macros */
#include "fortify.h"

#include "ufortify.h" /* the user's options */

char *_Fortify_file=NULL;
int _Fortify_line=0;

#ifndef FORTIFY_TRANSPARENT

#include <string.h>
#include <math.h>
#include <time.h>
#include <ctype.h>

#if defined MSDOS || defined __BORLANDC__ || defined WIN32 || defined __HIGHC__
# if !defined WIN32
#  undef MSDOS
#  define MSDOS
# endif
# include <conio.h>
#else
# include <unistd.h>
# include <termio.h>
# define getch() getchar()
#endif

#if defined _WINDOWS
# include "windows.h"
# if !defined WIN32
#  include "toolhelp.h"
# endif
#endif

#if defined LONGNAME
# include "longname.h"
#endif

#if !defined MIN
# define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif

struct Header
{
	char          *File;   /* The sourcefile of the caller   */
	unsigned short Line;   /* The sourceline of the caller   */
	size_t         Size;   /* The size of the malloc'd block */
	struct Header *Prev,   /* List pointers                  */
		      *Next;
	int            Checksum;  /* For validating the Header structure; see ChecksumHeader() */
	unsigned char  Scope;
};

#if defined AViiON || defined __GNUC__ || defined _MSC_VER
# define _static static
#else
# define _static
#endif

_static char *address __OF((void *addr));
_static int TimeToCheck __OF((void));
_static int CheckBlock __OF((struct Header *h, char *file, unsigned long line));
_static int CheckPointer __OF((unsigned char *ptr, unsigned long size, char *file, unsigned long line));
_static int CheckFortification __OF((unsigned char *ptr, unsigned char value, size_t size));
_static void SetFortification __OF((unsigned char *ptr, unsigned char value, size_t size));
_static void OutputFortification __OF((unsigned char *ptr, unsigned char value, size_t size));
_static int IsHeaderValid __OF((struct Header *h));
_static void MakeHeaderValid __OF((struct Header *h));
_static int ChecksumHeader __OF((struct Header *h));
_static int IsOnList __OF((struct Header *h));
_static void OutputHeader __OF((struct Header *h));
_static void OutputMemory __OF((struct Header *h));
_static void st_DefaultOutput __OF((char *String));
_static void WaitIfstdOutput __OF((void));

static char stdOutput = 0;        /* If true, did some stderr output */
static OutputFuncPtr  st_Output = st_DefaultOutput; /* Output function for errors */

#if !defined MSDOS && !defined WIN32
static int strnicmp(s1,s2,maxlen)
 char *s1,*s2;
 size_t maxlen;
 {
  while ((maxlen) && (*s1) && (*s2) && (toupper(*s1)==toupper(*s2))) {
   maxlen--;
   s1++;
   s2++;
  }
  return((maxlen) ? toupper(*s1)-toupper(*s2) : 0);
 }

static int stricmp(s1,s2)
 char *s1,*s2;
 {
  return(strnicmp(s1,s2,strlen(s1)+1));
 }
#endif

static char *address(void *addr)
{
        static char str[80];

#if defined KNOWS_POINTER_TYPE
        sprintf(str,"%p",addr);
#else
        sprintf(str,"%lx",(unsigned long) addr);
#endif
        return(str);
}

#ifdef FORTIFY_CheckInterval
int TimeToCheck()
{
        static time_t lastcheck=0L;
	time_t t;
        int ret = 0;

	time(&t);
	if ((lastcheck==0L) || (t-lastcheck>=FORTIFY_CheckInterval))
        {
                lastcheck = t;
                ret = 1;
        }
        return(ret);
}
#endif

static FILE *gfile=NULL;
static int Nchars=0,Nlines=0;
static char flag=0;

static void _Fortify_NoOutput()
 {
 }

static void st_DefaultOutput(char *String)
{
        static FILE *file;
        static char first=1;

        if (first) {
                file=stderr;
                first=0;
        }

        if (stdOutput==0) {
        	Nchars=Nlines=0;
                if (gfile!=NULL) rewind(gfile);
        }

        if (flag==0)
        {
                char *ptr;

        	file=stderr;
                flag = 1;
                if ((ptr=getenv("FORTIFY_OUTPUT"))!=NULL)
                {
                        if ((stricmp(ptr,"null")==0) || (stricmp(ptr,"nul")==0))
                                file=NULL;
                        else if (stricmp(ptr,"stderr")==0)
                                file=stderr;
                        else if (stricmp(ptr,"stdout")==0)
                                file=stdout;
#if defined MSDOS && !defined _WINDOWS && !defined WIN32
                        else if (stricmp(ptr,"stdprn")==0)
                                file=stdprn;
#endif
                        else if ((file=fopen(ptr,"w"))==NULL)
                        {
#if !defined _WINDOWS
                                fprintf(stderr,"\r\nFortify: Unable to create logfile %s\r\n",ptr);
				EndOfPgr(4);
#else
    				{
    					char str[255];

    					sprintf(str,"Unable to create logfile\n \"%s\"",ptr);
    					MessageBox((HWND) NULL,(LPCSTR) str,(LPCSTR) "Fortify",(UINT) MB_ICONSTOP);
#if 0
#if defined WIN32
				        /* TerminateProcess(GetCurrentProcess(),65535); */
                                        ExitProcess(65535);
#else
    					TerminateApp((HTASK) NULL,(WORD) NO_UAE_BOX);
#endif
#else
                                        EndOfPgr(1);
#endif
    				}
#endif
                        }
		}
		if ((file!=NULL) && (file!=stderr) && (file!=stdout))
		{
			time_t t;

			time(&t);
			fprintf(file,"Generated on: %s%s\n",
                                ctime(&t),
                                (file==stdout) || (file==stderr) ? "\r" : ""
                               );
		}
	}
	if (file!=NULL)
	{
#if defined _WINDOWS
                if ((file==stdout) || (file==stderr)) {
#if defined LINE_BY_LINE
                	if (MessageBox((HWND) NULL,(LPCSTR) String,(LPCSTR) "Fortify",(UINT) MB_OKCANCEL /* |MB_ICONINFORMATION */)==IDCANCEL)
#if 0
#if defined WIN32
                         /* TerminateProcess(GetCurrentProcess(),65535); */
                         ExitProcess(65535);
#else
    			 TerminateApp((HTASK) NULL,(WORD) NO_UAE_BOX);
#endif
#else
                         EndOfPgr(1);
#endif
#else
			{
                        	char *ptr;

                        	ptr="fortify.tmp";
				if ((ptr==NULL) || ((file=gfile=fopen(ptr,"w+"))==NULL))
                        	{
    					char str[255];

    					sprintf(str,"Unable to create temporary file\n \"%s\"",(ptr==NULL) ? "(NULL)" : ptr);
    					MessageBox((HWND) NULL,(LPCSTR) str,(LPCSTR) "Fortify",(UINT) MB_ICONSTOP);
#if 0
#if defined WIN32
                                        /* TerminateProcess(GetCurrentProcess(),65535); */
                                        ExitProcess(65535);
#else
    					TerminateApp((HTASK) NULL,(WORD) NO_UAE_BOX);
#endif
#else
                                        EndOfPgr(1);
#endif
                        	}
                    	}
#endif
                }
                if ((file!=stdout) && (file!=stderr))
#endif
                        {
                                int i,ch=-1;

                        	for (i=0;(String[i]) && (Nlines<30);i++)
                                 if (String[i]=='\n') Nlines++;
				if ((String[i]) && (String[i+1])) {
					ch=String[i+1];
					String[i+1]=0;
				}
                                if ((file==stdout) || (file==stderr)) {
                                 char *ptr=String;
                                 int i;

                                 do {
                                  for (i=0;(ptr[i]) && (ptr[i]!='\r') && (ptr[i]!='\n');i++);
                                  Nchars+=fprintf(file,"%-*.*s%s",
                                                  i,i,
                                                  ptr,
                                                  (ptr[i]) ? "\r\n" : ""
                                                 );
                                  ptr+=i;
                                  if (ptr[0]=='\r') ptr++;
                                  if (ptr[0]=='\n') ptr++;
                                 } while (*ptr);
                                }
                                else Nchars+=fprintf(file,String);
				if (ch>=0) String[i+1]=(char)ch;
				if (Nlines>=30) {
                                	WaitIfstdOutput();
                                    	Nchars=Nlines=0;
                                    	stdOutput = 0;
                                        if ((String[i]) && (String[i+1])) {
                                                if ((file==stderr) || (file==stdout) || ((gfile!=NULL) && (Nchars)))
                                                	stdOutput = 1;
                                         	st_DefaultOutput(String+i);
                                        }
                                }
                        }
                if ((file==stderr) || (file==stdout) || ((gfile!=NULL) && (Nchars)))
                	stdOutput = 1;
	}
}

static void WaitIfstdOutput()
{
#if !defined _WINDOWS
        if((stdOutput) && (st_Output != (OutputFuncPtr) _Fortify_NoOutput))
        {
#ifdef FORTIFY_WAIT_FOR_KEY
                static signed char wait_on_key=-1;

                if(wait_on_key<0)
		{
			char *ptr;

                        if (((ptr=getenv("FORTIFY_WAIT_FOR_KEY"))!=NULL) &&
			    (tolower(*ptr)=='n')) wait_on_key = 0;
                        else wait_on_key = 1;

                }
		if(wait_on_key)
		{
                        char c;

#if !defined MSDOS && !defined WIN32
                        struct termio tio,tiobak;
                        char flag;

                        if ((flag=ioctl(0,TCGETA,&tio))==0) /* handle 0 is stdin */
                        {
                                tiobak=tio;
                        	tio.c_lflag&=~ICANON;
                                tio.c_lflag&=~ECHO;
                        	tio.c_cc[VMIN]=1;
                        	ioctl(0,TCSETA,&tio);
	                }
#endif /* !MSDOS */
			c = (char)getch();

#if !defined MSDOS && !defined WIN32
                        if (flag==0)
                        	ioctl(0,TCSETA,&tiobak);
#endif /* !MSDOS */

			if ((c == 3) || (c == 0x1b)) EndOfPgr(3);
		}
#endif /* FORTIFY_WAIT_FOR_KEY */

        }
#else
# if !defined LINE_BY_LINE
        if ((stdOutput) && (gfile!=NULL) && (Nchars))
        {
                char *ptr;

                ptr=(char *) malloc(Nchars+1);
                if (ptr!=NULL)
                {
                        int n=0,l=0;

                        rewind(gfile);
                        while ((n<Nchars) && (l<Nlines))
                        {
                                fgets(ptr+n,Nchars-n+1,gfile);
                                n+=(int)strlen(ptr+n);
                                l++;
                        }
                	if (MessageBox((HWND) NULL,(LPCSTR) ptr,(LPCSTR) "Fortify",(UINT) MB_OKCANCEL /* |MB_ICONINFORMATION */)==IDCANCEL)
#if 0
#if defined WIN32
                         /* TerminateProcess(GetCurrentProcess(),65535); */
                         ExitProcess(65535);
#else
    			 TerminateApp((HTASK) NULL,(WORD) NO_UAE_BOX);
#endif
#else
                         EndOfPgr(1);
#endif
                }
                free(ptr);
        }
# endif
#endif
	stdOutput = 0;
}

static struct Header *st_Head = NULL; /* Head of alloc'd memory list */
static char st_Buffer[256];       /* Temporary buffer for sprintf's */
static int st_Disabled = 0;       /* If true, Fortify is inactive */
static int st_MallocFailRate = 0; /* % of the time to fail mallocs */

static char          *st_LastVerifiedFile = "unknown";
static unsigned short st_LastVerifiedLine = 0;
static unsigned char  st_Scope            = 0;
static void           OutputLastVerifiedPoint __OF((void));

void FORTIFY_STORAGE
_Fortify_Init(char *file,unsigned long line)
{
	if (gfile!=NULL) fclose(gfile);
	gfile=NULL;
	Nchars=Nlines=0;
        flag=0;
        st_Head=NULL;
        stdOutput=0;
        st_Output=st_DefaultOutput;
        st_Disabled=0;
        st_MallocFailRate=0;
        st_LastVerifiedFile="unknown";
        st_LastVerifiedLine=0;
        st_Scope=0;
}


/*
 * _Fortify_malloc() - Allocates a block of memory, with extra bits for
 *                    misuse protection/detection.
 *
 *    Features:
 *     +  Adds the malloc'd memory onto Fortify's own private list.
 *        (With a checksum'd header to detect corruption of the memory list)
 *     +  Places sentinals on either side of the user's memory with
 *        known data in them, to detect use outside of the bounds
 *        of the block
 *     +  Initializes the malloc'd memory to some "nasty" value, so code
 *        can't rely on it's contents.
 *     +  Can check all sentinals on every malloc.
 *     +  Can generate a warning message on a malloc fail.
 *     +  Can randomly "fail" at a set fail rate
 */
void *FORTIFY_STORAGE
_Fortify_malloc(size_t size,char *file,unsigned long line)
{
	unsigned char *ptr;
	struct Header *h;

        stdOutput = 0;

	FORTIFY_LOCK();

	if(st_Disabled)
	{
		ptr = (unsigned char *) malloc(size);
		FORTIFY_UNLOCK();
                WaitIfstdOutput();
		return((void *) ptr);
	}

#ifdef CHECK_ALL_MEMORY_ON_MALLOC
#ifdef FORTIFY_CheckInterval
        if (TimeToCheck())
#endif
		_Fortify_CheckAllMemory(file, line);
#endif

	if(size == 0)
	{
#ifdef WARN_ON_ZERO_MALLOC
    	sprintf(st_Buffer,
        	    "\nFortify: %s.%ld\n         malloc(0) attempted failed\n",
            	file, line);
		st_Output(st_Buffer);
#endif

		FORTIFY_UNLOCK();
		WaitIfstdOutput();
		return(0);
	}

	if(st_MallocFailRate > 0)
	{
		if(rand() % 100 < st_MallocFailRate)
		{
#ifdef WARN_ON_FALSE_FAIL
			sprintf(st_Buffer,
					"\nFortify: %s.%ld\n         malloc(%ld) \"false\" failed\n",
							file, line, (unsigned long)size);
			st_Output(st_Buffer);
#endif
			FORTIFY_UNLOCK();
                        WaitIfstdOutput();
                        return(0);
		}
	}

	/*
	 * malloc the memory, including the space for the header and fortification
	 * buffers
	 */
#ifdef WARN_ON_SIZE_T_OVERFLOW
	{
		size_t private_size = sizeof(struct Header)
							+ FORTIFY_BEFORE_SIZE + size + FORTIFY_AFTER_SIZE;

		if(private_size < size) /* Check to see if the added baggage is larger than size_t */
		{
			sprintf(st_Buffer,
					"\nFortify: %s.%ld\n         malloc(%ld) has overflowed size_t.\n",
					file, line, (unsigned long)size);
			st_Output(st_Buffer);
			FORTIFY_UNLOCK();
			WaitIfstdOutput();
			return(0);
		}
	}
#endif

	ptr = (unsigned char *) malloc(sizeof(struct Header) +
				 FORTIFY_BEFORE_SIZE + size + FORTIFY_AFTER_SIZE);
	if(!ptr)
	{
#ifdef WARN_ON_MALLOC_FAIL
		sprintf(st_Buffer, "\nFortify: %s.%ld\n         malloc(%ld) failed\n",
				file, line, (unsigned long)size);
		st_Output(st_Buffer);
#endif

		FORTIFY_UNLOCK();
                WaitIfstdOutput();
                return(0);
	}

	/*
	 * Initialize and validate the header
	 */
	h = (struct Header *)ptr;

	h->Size = size;

	h->File = file;
	h->Line = (unsigned short) line;

	h->Next = st_Head;
	h->Prev = 0;

	h->Scope = st_Scope;

	if(st_Head)
	{
		st_Head->Prev = h;
		MakeHeaderValid(st_Head);
	}

	st_Head = h;

	MakeHeaderValid(h);


	/*
	 * Initialize the fortifications
	 */
	SetFortification(ptr + sizeof(struct Header),
	                 FORTIFY_BEFORE_VALUE, FORTIFY_BEFORE_SIZE);
	SetFortification(ptr + sizeof(struct Header) + FORTIFY_BEFORE_SIZE + size,
	                 FORTIFY_AFTER_VALUE, FORTIFY_AFTER_SIZE);

#ifdef FILL_ON_MALLOC
	/*
	 * Fill the actual user memory
	 */
	SetFortification(ptr + sizeof(struct Header) + FORTIFY_BEFORE_SIZE,
	                 FILL_ON_MALLOC_VALUE, size);
#endif

	/*
	 * We return the address of the user's memory, not the start of the block,
	 * which points to our magic cookies
	 */

	FORTIFY_UNLOCK();
        WaitIfstdOutput();
	return((void *) (ptr + sizeof(struct Header) + FORTIFY_BEFORE_SIZE));
}

/*
 * _Fortify_free() - This free must be used for all memory allocated with
 *                  _Fortify_malloc().
 *
 *   Features:
 *     + Pointers are validated before attempting a free - the pointer
 *       must point to a valid malloc'd bit of memory.
 *     + Detects attempts at freeing the same block of memory twice
 *     + Can clear out memory as it is free'd, to prevent code from using
 *       the memory after it's been freed.
 *     + Checks the sentinals of the memory being freed.
 *     + Can check the sentinals of all memory.
 */

void FORTIFY_STORAGE
_Fortify_free(void *uptr,char *file,unsigned long line)
{
	unsigned char *ptr;
	struct Header *h;

	if(uptr == NULL)
		return;

	ptr = (unsigned char *)uptr - sizeof(struct Header) - FORTIFY_BEFORE_SIZE;
	h = (struct Header *)ptr;

	stdOutput = 0;

	FORTIFY_LOCK();

	if(st_Disabled)
	{
		free(uptr);
		FORTIFY_UNLOCK();
                WaitIfstdOutput();
                return;
	}

#ifdef CHECK_ALL_MEMORY_ON_FREE
#ifdef FORTIFY_CheckInterval
        if (TimeToCheck())
#endif
		_Fortify_CheckAllMemory(file, line);
#endif

#ifdef PARANOID_FREE
	if(!IsOnList(h))
	{
		sprintf(st_Buffer,
		 "\nFortify: %s.%ld\n         Invalid pointer, corrupted header, or possible free twice\n",
				file, line);
		st_Output(st_Buffer);
		OutputLastVerifiedPoint();
		goto fail;
	}
#endif

	if(!CheckBlock(h, file, line))
		goto fail;

	/*
	 * Remove the block from the list
	 */
	if(h->Prev)
	{
		if(!CheckBlock(h->Prev, file, line))
			goto fail;

		h->Prev->Next = h->Next;
		MakeHeaderValid(h->Prev);
	}
	else
		st_Head = h->Next;

	if(h->Next)
	{
		if(!CheckBlock(h->Next, file, line))
			goto fail;

		h->Next->Prev = h->Prev;
		MakeHeaderValid(h->Next);
	}

#ifdef FILL_ON_FREE
	/*
	 * Nuke out all memory that is about to be freed
	 */
	SetFortification(ptr, FILL_ON_FREE_VALUE,
	                 sizeof(struct Header) + FORTIFY_BEFORE_SIZE + h->Size + FORTIFY_AFTER_SIZE);
#endif

	/*
	 * And do the actual free
	 */
	free(ptr);
	FORTIFY_UNLOCK();
        WaitIfstdOutput();
        return;

fail:
	sprintf(st_Buffer, "         free(%s) failed\n", address(uptr));
	st_Output(st_Buffer);
	FORTIFY_UNLOCK();
        WaitIfstdOutput();
}

/*
 * _Fortify_realloc() - Uses _Fortify_malloc() and _Fortify_free() to implement
 *                     realloc().
 *
 *   Features:
 *        + The realloc'd block is ALWAYS moved.
 *        + The pointer passed to realloc() is verified in the same way that
 *          _Fortify_free() verifies pointers before it frees them.
 *        + All the _Fortify_malloc() and _Fortify_free() protection
 */
void *FORTIFY_STORAGE
_Fortify_realloc(void *ptr,size_t new_size,char *file,unsigned long line)
{
	void *new_ptr;
	struct Header *h;

	if(new_size == 0)
	{
		_Fortify_free(ptr,file,line);
		return(NULL);
	}

	h = (struct Header *)((unsigned char *)ptr - sizeof(struct Header) - FORTIFY_BEFORE_SIZE);

        stdOutput = 0;

	if(st_Disabled)
	{
		FORTIFY_LOCK();
		new_ptr = (void *) realloc(ptr, new_size);
		FORTIFY_UNLOCK();
		WaitIfstdOutput();
                return(new_ptr);
	}

	if(!ptr)
        {
		void *FORTIFY_STORAGE ret = _Fortify_malloc(new_size, file, line);

                WaitIfstdOutput();
		return(ret);
        }

	FORTIFY_LOCK();

	if(!IsOnList(h))
	{
		sprintf(st_Buffer,
				"\nFortify: %s.%ld\n         Invalid pointer or corrupted header passed to realloc\n",
				file, line);
		st_Output(st_Buffer);
		goto fail;
	}

	if(!CheckBlock(h, file, line))
		goto fail;

	new_ptr = _Fortify_malloc(new_size, file, line);
	if(!new_ptr)
	{
		FORTIFY_UNLOCK();
                WaitIfstdOutput();
                return(0);
	}

	if(h->Size < new_size)
		memcpy(new_ptr, ptr, h->Size);
	else
		memcpy(new_ptr, ptr, new_size);

	_Fortify_free(ptr, file, line);
	FORTIFY_UNLOCK();
        WaitIfstdOutput();
        return(new_ptr);

fail:
	sprintf(st_Buffer, "         realloc(%s, %ld) failed\n", address(ptr), (unsigned long)new_size);
	st_Output(st_Buffer);
	FORTIFY_UNLOCK();
        WaitIfstdOutput();
        return(NULL);
}


/*
 * __Fortify_CheckPointer() - Returns true if the uptr points to a valid
 *   piece of _Fortify_malloc()'d memory. The memory must be on the malloc'd
 *   list, and it's sentinals must be in tact.
 *     If anything is wrong, an error message is issued.
 *
 *   (Note - if fortify is disabled, this function always returns true).
 */
static int FORTIFY_STORAGE
__Fortify_CheckPointer(void *uptr,char OnlyStart,unsigned long size,char *file,unsigned long line)
{
	unsigned char *ptr = (unsigned char *)uptr - sizeof(struct Header) - FORTIFY_BEFORE_SIZE;
	int r = 1, StartPointer;

        stdOutput = 0;

	if(st_Disabled)
        {
                WaitIfstdOutput();
		return(1);
        }

	FORTIFY_LOCK();

	StartPointer = IsOnList((struct Header *)ptr);
	if((OnlyStart) && (!StartPointer))
	{
		sprintf(st_Buffer,
		       "\nFortify: %s.%ld\n         Invalid pointer or corrupted header detected (%s)\n",
		       file, line, address(uptr));
		st_Output(st_Buffer);
		FORTIFY_UNLOCK();
                WaitIfstdOutput();
                return(0);
	}

	if((OnlyStart) || (StartPointer))
		r = CheckBlock((struct Header *)ptr, file, line);
	if(!OnlyStart)
		r = CheckPointer((unsigned char *)uptr, size, file, line);
	FORTIFY_UNLOCK();
        WaitIfstdOutput();
        return(r);
}


int FORTIFY_STORAGE
_Fortify_CheckPointer(void *uptr,char *file,unsigned long line)
{
	return(__Fortify_CheckPointer(uptr,1,0,file,line));
}

/*
 * Fortify_SetOutputFunc(OutputFuncPtr Output) - Sets the function used to
 *   output all error and diagnostic messages by fortify. The output function
 *   takes a single unsigned char * argument, and must be able to handle newlines.
 *     The function returns the old pointer.
 */
Fortify_OutputFuncPtr FORTIFY_STORAGE
_Fortify_SetOutputFunc(Fortify_OutputFuncPtr Output)
{
	OutputFuncPtr Old = st_Output;

	st_Output = (OutputFuncPtr) Output;

	return((Fortify_OutputFuncPtr FORTIFY_STORAGE) Old);
}

/*
 * _Fortify_SetMallocFailRate(int Percent) - _Fortify_malloc() will make the
 *   malloc attempt fail this Percent of the time, even if the memory is
 *   available. Useful to "stress-test" an application. Returns the old
 *   value. The fail rate defaults to 0.
 */
int FORTIFY_STORAGE
_Fortify_SetMallocFailRate(int Percent)
{
	int Old = st_MallocFailRate;

	st_MallocFailRate = Percent;

	return(Old);
}


/*
 * _Fortify_CheckAllMemory() - Checks the sentinals of all malloc'd memory.
 *   Returns the number of blocks that failed.
 *
 *  (If Fortify is disabled, this function always returns 0).
 */
int FORTIFY_STORAGE
_Fortify_CheckAllMemory(char *file,unsigned long line)
{
	struct Header *curr = st_Head;
	int count = 0;

	stdOutput = 0;

	if(st_Disabled)
	{
		WaitIfstdOutput();
		return(0);
	}

	FORTIFY_LOCK();

	while(curr)
	{
		if(!CheckBlock(curr, file, line))
			count++;

		curr = curr->Next;
	}

	if(file)
	{
		st_LastVerifiedFile = file;
		st_LastVerifiedLine = (short) line;
	}

	FORTIFY_UNLOCK();
        WaitIfstdOutput();
        return(count);
}

/* _Fortify_EnterScope - enters a new Fortify scope level.
 * returns the new scope level.
 */
int FORTIFY_STORAGE
_Fortify_EnterScope(char *file,unsigned long line)
{
	return((int) ++st_Scope);
}

/* _Fortify_LeaveScope - leaves a Fortify scope level,
 * also prints a memory dump of all non-freed memory that was allocated
 * during the scope being exited.
 */
int FORTIFY_STORAGE
_Fortify_LeaveScope(char *file,unsigned long line)
{
	struct Header *curr = st_Head;
	int count = 0;
	size_t size = 0;

        stdOutput = 0;

	if(st_Disabled)
        {
                WaitIfstdOutput();
                return(0);
        }

	FORTIFY_LOCK();

	st_Scope--;
	while(curr)
	{
		if(curr->Scope > st_Scope)
		{
			if(count == 0)
			{
				sprintf(st_Buffer, "\nFortify: Memory Dump at %s.%ld\n", file, line);
				st_Output(st_Buffer);
				OutputLastVerifiedPoint();
				sprintf(st_Buffer, "%11s %8s %s\n", "Address", "Size", "Allocator");
				st_Output(st_Buffer);
			}

			OutputHeader(curr);
			count++;
			size += curr->Size;
		}

		curr = curr->Next;
	}

	if(count)
	{
		sprintf(st_Buffer, "%11s %8ld bytes overhead\n", "and",
						(unsigned long)(count * (sizeof(struct Header) + FORTIFY_BEFORE_SIZE + FORTIFY_AFTER_SIZE)));
						st_Output(st_Buffer);

		sprintf(st_Buffer,"%11s %8ld bytes in %d blocks\n", "total", size, count);
		st_Output(st_Buffer);
	}

	FORTIFY_UNLOCK();
        WaitIfstdOutput();
        return(count);
}

/*
 * _Fortify_OutputAllMemory() - Outputs the entire list of currently
 *     malloc'd memory. For each malloc'd block is output it's Address,
 *   Size, and the SourceFile and Line that allocated it.
 *
 *   If there is no memory on the list, this function outputs nothing.
 *
 *   It returns the number of blocks on the list, unless fortify has been
 *   disabled, in which case it always returns 0.
 */
int FORTIFY_STORAGE
_Fortify_OutputAllMemory(char *file,unsigned long line)
{
	struct Header *curr = st_Head;
	int count = 0;
	size_t size = 0;

        stdOutput = 0;

	if(st_Disabled)
        {
                WaitIfstdOutput();
                return(0);
        }

	FORTIFY_LOCK();

	if(curr)
	{
		sprintf(st_Buffer, "\nFortify: Memory Dump at %s.%ld\n", file, line);
		st_Output(st_Buffer);
		OutputLastVerifiedPoint();
		sprintf(st_Buffer, "%11s %8s %s\n", "Address", "Size", "Allocator");
		st_Output(st_Buffer);

		while(curr)
		{
			OutputHeader(curr);
			count++;
			size += curr->Size;
			curr = curr->Next;
		}

		sprintf(st_Buffer, "%11s %8ld bytes overhead\n", "and",
				(unsigned long)(count * (sizeof(struct Header) + FORTIFY_BEFORE_SIZE + FORTIFY_AFTER_SIZE)));
		st_Output(st_Buffer);

		sprintf(st_Buffer,"%11s %8ld bytes in %d blocks\n", "total", size, count);
		st_Output(st_Buffer);
	}

	FORTIFY_UNLOCK();
        WaitIfstdOutput();
	return(count);
}

/* _Fortify_DumpAllMemory(Scope) - Outputs the entire list of currently
 * new'd memory within the specified scope. For each new'd block is output
 * it's Address, Size, the SourceFile and Line that allocated it, a hex dump
 * of the contents of the memory and an ascii dump of printable characters.
 *
 * If there is no memory on the list, this function outputs nothing.
 *
 * It returns the number of blocks on the list, unless Fortify has been
 * disabled, in which case it always returns 0.
 */
int FORTIFY_STORAGE
_Fortify_DumpAllMemory(int scope,char *file,unsigned long line)
{
	struct Header *curr = st_Head;
	int count = 0;
	size_t size = 0;

        stdOutput = 0;

	if(st_Disabled)
        {
                WaitIfstdOutput();
		return(0);
        }

	FORTIFY_LOCK();

	while(curr)
	{
		if(curr->Scope >= scope)
		{
			if(count == 0)
			{
				sprintf(st_Buffer, "\nFortify: Memory Dump at %s.%ld\n", file, line);
				st_Output(st_Buffer);
				OutputLastVerifiedPoint();
				sprintf(st_Buffer, "%11s %8s %s\n", "Address", "Size", "Allocator");
				st_Output(st_Buffer);
			}

			OutputHeader(curr);
			OutputMemory(curr);
			st_Output("\n");
			count++;
			size += curr->Size;
		}

		curr = curr->Next;
	}

	if(count)
	{
		sprintf(st_Buffer, "%11s %8ld bytes overhead\n", "and",
						(unsigned long)(count * (sizeof(struct Header) + FORTIFY_BEFORE_SIZE + FORTIFY_AFTER_SIZE)));
						st_Output(st_Buffer);

		sprintf(st_Buffer,"%11s %8ld bytes in %d blocks\n", "total", size, count);
		st_Output(st_Buffer);
	}

	FORTIFY_UNLOCK();
        WaitIfstdOutput();
        return(count);
}

/*
 * _Fortify_Disable() - This function provides a mechanism to disable Fortify
 *   without recompiling all the sourcecode.
 *   If 'how' is zero then it can only be called when there is no memory on the
 *   Fortify malloc'd list. (Ideally, at the start of the program before any
 *   memory has been allocated). If you call this function when there IS
 *   memory on the Fortify malloc'd list, it will issue an error, and fortify
 *   will not be disabled.
 *   If 'how' is nonzero then output will only be disabled. This can always be
 *   done.
 */

int FORTIFY_STORAGE
_Fortify_Disable(char *file,unsigned long line,int how)
{
	int result;

        if (how <= 0)
        {
    	    stdOutput = 0;

    	    FORTIFY_LOCK();

    	    if((st_Head) && (how == 0))
    	    {
    		    sprintf(st_Buffer, "Fortify: %s.%d\n", file, line);
    		    st_Output(st_Buffer);
    		    st_Output("         Fortify_Disable failed\n");
    		    st_Output("         (because there is memory on the Fortify memory list)\n");

    		    _Fortify_OutputAllMemory(file, line);
    		    result = 0;
    	    }
    	    else
    	    {
    		    st_Disabled = (how >= -1 ? 1 : 0);
    		    result = 1;
    	    }

    	    FORTIFY_UNLOCK();
            WaitIfstdOutput();
        }
        else
        {
            _Fortify_SetOutputFunc((Fortify_OutputFuncPtr) _Fortify_NoOutput);
            result = 1;
        }
	return(result);
}

/*
 * Check a block's header and fortifications.
 */
static int CheckBlock(struct Header *h,char *file,unsigned long line)
{
	unsigned char *ptr = (unsigned char *)h;
	int result = 1;

        stdOutput = 0;

	if(!IsHeaderValid(h))
	{
		sprintf(st_Buffer,
				"\nFortify: %s.%ld\n         Invalid pointer or corrupted header detected (%s)\n",
				file, line, address(ptr + sizeof(struct Header) + FORTIFY_BEFORE_SIZE));
		st_Output(st_Buffer);
		OutputLastVerifiedPoint();
                WaitIfstdOutput();
                return(0);
	}

	if(!CheckFortification(ptr + sizeof(struct Header),
		                   FORTIFY_BEFORE_VALUE, FORTIFY_BEFORE_SIZE))
	{
		sprintf(st_Buffer,
		        "\nFortify: %s.%ld\n         Memory overrun detected before block\n",
		        file, line);
		st_Output(st_Buffer);

		sprintf(st_Buffer,"         (%s,%ld,%s.%u)\n",
			address(ptr + sizeof(struct Header) + FORTIFY_BEFORE_SIZE),
		       (unsigned long)h->Size, h->File, h->Line);
		st_Output(st_Buffer);

		OutputFortification(ptr + sizeof(struct Header),
		                    FORTIFY_BEFORE_VALUE, FORTIFY_BEFORE_SIZE);
		OutputLastVerifiedPoint();
		result = 0;
	}

	if(!CheckFortification(ptr + sizeof(struct Header) + FORTIFY_BEFORE_SIZE + h->Size,
	                       FORTIFY_AFTER_VALUE, FORTIFY_AFTER_SIZE))
	{
		sprintf(st_Buffer, "\nFortify: %s.%ld\n         Memory overrun detected after block\n",
		                   file, line);
		st_Output(st_Buffer);

		sprintf(st_Buffer,"         (%s,%ld,%s.%u)\n",
				  address(ptr + sizeof(struct Header) + FORTIFY_BEFORE_SIZE),
				 (unsigned long)h->Size, h->File, h->Line);
		st_Output(st_Buffer);

		OutputFortification(ptr + sizeof(struct Header) + FORTIFY_BEFORE_SIZE + h->Size,
		                    FORTIFY_AFTER_VALUE, FORTIFY_AFTER_SIZE);
		OutputLastVerifiedPoint();
		result = 0;
	}

        WaitIfstdOutput();
	return(result);
}

static int CheckPointer(unsigned char *ptr,unsigned long size,char *file,unsigned long line)
{
	struct Header *curr;
	unsigned char *ptr1;

	curr = st_Head;
	while(curr)
	{
		ptr1 = (unsigned char *)curr + sizeof(struct Header) + FORTIFY_BEFORE_SIZE;
		if(ptr + size <= (unsigned char *)curr)
			;
		else if(ptr >= ptr1)
			;
		else
		{
			sprintf(st_Buffer, "\nFortify: %s.%ld\n         Memory access detected before block\n",
		                file, line);
			st_Output(st_Buffer);

			sprintf(st_Buffer,"         (%s,%ld,%s.%u)\n",
					address(ptr1),
					(unsigned long)curr->Size, curr->File, curr->Line);
			st_Output(st_Buffer);

			WaitIfstdOutput();
			return(0);
		}

		if(ptr + size <= ptr1 + curr->Size)
			;
		else if(ptr >= ptr1 + curr->Size + FORTIFY_AFTER_SIZE)
			;
		else
		{
			sprintf(st_Buffer, "\nFortify: %s.%ld\n         Memory access detected after block\n",
		                file, line);
			st_Output(st_Buffer);

			sprintf(st_Buffer,"         (%s,%ld,%s.%u)\n",
					address(ptr1),
					(unsigned long)curr->Size, curr->File, curr->Line);
			st_Output(st_Buffer);

			WaitIfstdOutput();
			return(0);
		}

		if((ptr >= ptr1) && (ptr < ptr1 + curr->Size) && (ptr + size > ptr1 + curr->Size))
		{
			sprintf(st_Buffer, "\nFortify: %s.%ld\n         Memory access detected after block\n",
		                file, line);
			st_Output(st_Buffer);

			sprintf(st_Buffer,"         (%s,%ld,%s.%u)\n",
					address(ptr + sizeof(struct Header) + FORTIFY_BEFORE_SIZE),
					(unsigned long)curr->Size, curr->File, curr->Line);
			st_Output(st_Buffer);
			WaitIfstdOutput();
			return(0);
		}

		curr = curr->Next;
	}
	return(1);
}

/*
 * Checks if the _size_ bytes from _ptr_ are all set to _value_
 */
static int CheckFortification(unsigned char *ptr,unsigned char value,size_t size)
{
	while(size--)
		if(*ptr++ != value)
			return(0);

	return(1);
}

/*
 * Set the _size_ bytes from _ptr_ to _value_.
 */
static void SetFortification(unsigned char *ptr,unsigned char value,size_t size)
{
	memset(ptr, value, size);
}

/*
 * Output the corrupted section of the fortification
 */
/* Output the corrupted section of the fortification */
static void
OutputFortification(unsigned char *ptr,unsigned char value,size_t size)
{
	unsigned long offset, column;
	char	ascii[17];

	st_Output("Address     Offset Data");

	offset = 0;
	column = 0;

	while(offset < size)
	{
		if(column == 0)
		{
			sprintf(st_Buffer, "\n%8s %8d ", address(ptr), offset);
			st_Output(st_Buffer);
		}

		sprintf(st_Buffer, "%02x ", *ptr);
		st_Output(st_Buffer);

		ascii[ (int) column ] = isprint( *ptr ) ? (char)(*ptr) : (char)(' ');
		ascii[ (int) (column + 1) ] = '\0';

		ptr++;
		offset++;
		column++;

		if(column == 16)
		{
			st_Output( "   \"" );
			st_Output( ascii );
			st_Output( "\"" );
			column = 0;
		}
	}

	if ( column != 0 )
	{
		while ( column ++ < 16 )
		{
			st_Output( "   " );
		}
		st_Output( "   \"" );
		st_Output( ascii );
		st_Output( "\"" );
	}

	st_Output("\n");
}

/*
 * Returns true if the supplied pointer does indeed point to a real Header
 */
static int IsHeaderValid(struct Header *h)
{
	return(!ChecksumHeader(h));
}

/*
 * Updates the checksum to make the header valid
 */
static void MakeHeaderValid(struct Header *h)
{
	h->Checksum = 0;
	h->Checksum = -ChecksumHeader(h);
}

/*
 * Calculate (and return) the checksum of the header. (Including the Checksum
 * variable itself. If all is well, the checksum returned by this function should
 * be 0.
 */
static int ChecksumHeader(struct Header *h)
{
	register int c, checksum, *p;

	for(c = 0, checksum = 0, p = (int *)h; c < sizeof(struct Header)/sizeof(int); c++)
		checksum += *p++;

	return(checksum);
}

/*
 * Examines the malloc'd list to see if the given header is on it.
 */
static int IsOnList(struct Header *h)
{
	struct Header *curr;

	curr = st_Head;
	while(curr)
	{
		if(curr == h)
			return(1);
		curr = curr->Next;
	}

	return(0);
}


/*
 * Hex and ascii dump the memory
 */
static void
OutputMemory(struct Header *h)
{
	OutputFortification((unsigned char*)h + sizeof(struct Header) + FORTIFY_BEFORE_SIZE,
						0, h->Size);
}


/*
 * Output the header...
 */
static void OutputHeader(struct Header *h)
{
	sprintf(st_Buffer, "%11s %8ld %s.%u (%d)\n",
			   address((unsigned char*)h + sizeof(struct Header) + FORTIFY_BEFORE_SIZE),
			   (unsigned long)h->Size,
			   h->File, h->Line, (int) h->Scope);
	st_Output(st_Buffer);
}

static void OutputLastVerifiedPoint()
{
	sprintf(st_Buffer, "\nLast Verified point: %s.%u\n",
			   st_LastVerifiedFile,
			   st_LastVerifiedLine);
	st_Output(st_Buffer);
}

#else  /* FORTIFY_TRANSPARENT */

void *FORTIFY_STORAGE
_Fortify_malloc(size,file,line)
 size_t size;
 char *file;
 unsigned long line;
{
	return(malloc(size));
}

void FORTIFY_STORAGE
_Fortify_free(uptr,file,line)
 void *uptr;
 char *file;
 unsigned long line;
{
	free(uptr);
}

void *FORTIFY_STORAGE
_Fortify_realloc(ptr,new_size,file,line)
 void *ptr;
 size_t new_size;
 char *file;
 unsigned long line;
{
	return(realloc(ptr, new_size));
}

int FORTIFY_STORAGE
_Fortify_CheckPointer(uptr,file,line)
 void *uptr;
 char *file;
 unsigned long line;
{
	return(1);
}

Fortify_OutputFuncPtr FORTIFY_STORAGE
_Fortify_SetOutputFunc(Output)
 Fortify_OutputFuncPtr Output;
{
	return(0);
}

int FORTIFY_STORAGE
_Fortify_SetMallocFailRate(Percent)
 int Percent;
{
	return(0);
}

int FORTIFY_STORAGE
_Fortify_CheckAllMemory(file,line)
 char *file;
 unsigned long line;
{
	return(0);
}

int FORTIFY_STORAGE
_Fortify_EnterScope(file,line)
 char *file;
 unsigned long line;
{
	return(0);
}

int FORTIFY_STORAGE
_Fortify_LeaveScope(file,line)
 char *file;
 unsigned long line;
{
	return(0);
}

int FORTIFY_STORAGE
_Fortify_OutputAllMemory(file,line)
 char *file;
 unsigned long line;
{
	return(0);
}

int FORTIFY_STORAGE
_Fortify_DumpAllMemory(scope,file,line)
 int scope;
 char *file;
 unsigned long line;
{
	return(0);
}

int FORTIFY_STORAGE
_Fortify_Disable(file,line)
 char *file;
 unsigned long line;
{
	return(1);
}

#endif /* !FORTIFY_TRANSPARENT */

/* function that use _Fortify_malloc(), _Fortify_realloc(), _Fortify_free() */

/*
 * Fortifty_calloc() - Uses _Fortify_malloc() to implement calloc(). Much
 *                     the same protection as _Fortify_malloc().
 */
void *FORTIFY_STORAGE
_Fortify_calloc(size_t nitems,size_t size,char *file,unsigned long line)
{
	void *ptr;

	ptr = _Fortify_malloc(nitems * size, file, line);

	if(ptr)
		memset(ptr, 0, nitems * size);

	return(ptr);
}

/*
 * Fortifty_strdup() - Uses _Fortify_malloc() to implement strdup(). Much
 *                     the same protection as _Fortify_malloc().
 * The library function is not used because it is not certain that getpwd
 * uses the library malloc function (if linked with an alternate library)
 * and if the memory is freed then strange things can happen
 */
char *FORTIFY_STORAGE
_Fortify_strdup(char *str,char *file,unsigned long line)
{
	char *ptr;
	unsigned long l;

	if(str == NULL) {
		sprintf(st_Buffer,
			"\nFortify: %s.%ld\n         ", file, line);
		sprintf(st_Buffer + strlen(st_Buffer), "%s", "strdup pointer is NULL", file, line);
		strcat(st_Buffer, "\n");
		st_Output(st_Buffer);
		FORTIFY_UNLOCK();
		WaitIfstdOutput();
		return(0);
	}

	l = (int) strlen(str) + 1;
	__Fortify_CheckPointer(str,0,l,file,line);

	ptr = (char *) _Fortify_malloc(l, file, line);

	if(ptr)
		strcpy(ptr, str);

	return(ptr);
}

/*
 * Fortifty_getpwd() - Uses _Fortify_malloc() to implement getpwd(). Much
 *                     the same protection as _Fortify_malloc().
 * Memory is not allocated bu getcwd but by our routine for the same reason
 * as for strdup
 */
char *FORTIFY_STORAGE
_Fortify_getcwd(char *buf,int size,char *file,unsigned long line)
{
	char *ptr;

        if(buf!=NULL)
                ptr = buf;
	else
        	ptr = (char *) _Fortify_malloc(size + 1, file, line);

	if(ptr)
		ptr = getcwd(ptr, size);

	return(ptr);
}

/*
 * Fortifty_tempnam() - Uses _Fortify_strdup() to implement tempnam(). Much
 *                     the same protection as _Fortify_malloc().
 */
char *FORTIFY_STORAGE
_Fortify_tempnam(char *dir,char *pfx,char *file,unsigned long line)
{
	char *ptr1, *ptr2;

        ptr1 = tempnam(dir,pfx);

	if(ptr1)
        {
                ptr2=_Fortify_strdup(ptr1,file,line);
                free(ptr1);
                ptr1=ptr2;
        }

	return(ptr1);
}

/*
 * Fortify_memcpy()  - check if from/to is in allocated space and if so, then check if start/end is not outside allocated space.
 */
void *FORTIFY_STORAGE
_Fortify_memcpy(void *to,void *from,size_t size,char *file,unsigned long line)
{
    if (size) {
	    if((from == NULL) || (to == NULL)) {
		    sprintf(st_Buffer,
			    "\nFortify: %s.%ld\n         ", file, line);
		    if(from == NULL)
			    sprintf(st_Buffer + strlen(st_Buffer), "%s", "memcpy from pointer is NULL", file, line);
		    if(to == NULL)
			    sprintf(st_Buffer + strlen(st_Buffer), "%s%s", (from == NULL) ? "" : " and ", "memcpy to pointer is NULL", file, line);
		    strcat(st_Buffer, "\n");
		    st_Output(st_Buffer);
		    FORTIFY_UNLOCK();
		    WaitIfstdOutput();
		    return(NULL);
	    }

	    __Fortify_CheckPointer(to,0,size,file,line);
	    __Fortify_CheckPointer(from,0,size,file,line);
    }
	return(memcpy(to,from,size));
}

/*
 * Fortify_memmove()  - check if from/to is in allocated space and if so, then check if start/end is not outside allocated space.
 */
void *FORTIFY_STORAGE
_Fortify_memmove(void *to,void *from,size_t size,char *file,unsigned long line)
{
	if((from == NULL) || (to == NULL)) {
		sprintf(st_Buffer,
			"\nFortify: %s.%ld\n         ", file, line);
		if(from == NULL)
			sprintf(st_Buffer + strlen(st_Buffer), "%s", "memmove from pointer is NULL", file, line);
		if(to == NULL)
			sprintf(st_Buffer + strlen(st_Buffer), "%s%s", (from == NULL) ? "" : " and ", "memmove to pointer is NULL", file, line);
		strcat(st_Buffer, "\n");
		st_Output(st_Buffer);
		FORTIFY_UNLOCK();
		WaitIfstdOutput();
		return(NULL);
	}

	__Fortify_CheckPointer(to,0,size,file,line);
	__Fortify_CheckPointer(from,0,size,file,line);
	return(memmove(to,from,size));
}

/*
 * Fortify_memccpy()  - check if from/to is in allocated space and if so, then check if start/end is not outside allocated space.
 */
void *FORTIFY_STORAGE
_Fortify_memccpy(void *to,void *from,int c,size_t size,char *file,unsigned long line)
{
	if((from == NULL) || (to == NULL)) {
		sprintf(st_Buffer,
			"\nFortify: %s.%ld\n         ", file, line);
		if(from == NULL)
			sprintf(st_Buffer + strlen(st_Buffer), "%s", "memccpy from pointer is NULL", file, line);
		if(to == NULL)
			sprintf(st_Buffer + strlen(st_Buffer), "%s%s", (from == NULL) ? "" : " and ", "memccpy to pointer is NULL", file, line);
		strcat(st_Buffer, "\n");
		st_Output(st_Buffer);
		FORTIFY_UNLOCK();
		WaitIfstdOutput();
		return(NULL);
	}

	__Fortify_CheckPointer(to,0,size,file,line);
	__Fortify_CheckPointer(from,0,size,file,line);
	return(memccpy(to,from,c,size));
}

/*
 * Fortify_memset()  - check if from/to is in allocated space and if so, then check if start/end is not outside allocated space.
 */
void *FORTIFY_STORAGE
_Fortify_memset(void *buffer,int c,size_t size,char *file,unsigned long line)
{
	if(buffer == NULL) {
		sprintf(st_Buffer,
			"\nFortify: %s.%ld\n         memset pointer is NULL\n", file, line);
		st_Output(st_Buffer);
		FORTIFY_UNLOCK();
		WaitIfstdOutput();
		return(NULL);
	}

	__Fortify_CheckPointer(buffer,0,size,file,line);
	return(memset(buffer,c,size));
}

/*
 * Fortify_memchr()  - check if from/to is in allocated space and if so, then check if start/end is not outside allocated space.
 */
void *FORTIFY_STORAGE
_Fortify_memchr(void *buffer,int c,size_t size,char *file,unsigned long line)
{
	if(buffer == NULL) {
		sprintf(st_Buffer,
			"\nFortify: %s.%ld\n         memchr pointer is NULL\n", file, line);
		st_Output(st_Buffer);
		FORTIFY_UNLOCK();
		WaitIfstdOutput();
		return(NULL);
	}

	__Fortify_CheckPointer(buffer,0,size,file,line);
	return(memchr(buffer,c,size));
}

/*
 * Fortify_memcmp()  - check if from/to is in allocated space and if so, then check if start/end is not outside allocated space.
 */
int FORTIFY_STORAGE
_Fortify_memcmp(void *buffer1,void *buffer2,size_t size,char *file,unsigned long line)
{
	if((buffer1 == NULL) || (buffer2 == NULL)) {
		sprintf(st_Buffer,
			"\nFortify: %s.%ld\n         ", file, line);
		if(buffer1 == NULL)
			sprintf(st_Buffer + strlen(st_Buffer), "%s", "memcmp first pointer is NULL", file, line);
		if(buffer2 == NULL)
			sprintf(st_Buffer + strlen(st_Buffer), "%s%s", (buffer2 == NULL) ? "" : " and ", "memcmp second pointer is NULL", file, line);
		strcat(st_Buffer, "\n");
		st_Output(st_Buffer);
		FORTIFY_UNLOCK();
		WaitIfstdOutput();
		return(0);
	}

	__Fortify_CheckPointer(buffer1,0,size,file,line);
	__Fortify_CheckPointer(buffer2,0,size,file,line);
	return(memcmp(buffer1,buffer2,size));
}

/*
 * Fortify_memicmp()  - check if from/to is in allocated space and if so, then check if start/end is not outside allocated space.
 */
int FORTIFY_STORAGE
_Fortify_memicmp(void *buffer1,void *buffer2,size_t size,char *file,unsigned long line)
{
	if((buffer1 == NULL) || (buffer2 == NULL)) {
		sprintf(st_Buffer,
			"\nFortify: %s.%ld\n         ", file, line);
		if(buffer1 == NULL)
			sprintf(st_Buffer + strlen(st_Buffer), "%s", "memicmp first pointer is NULL", file, line);
		if(buffer2 == NULL)
			sprintf(st_Buffer + strlen(st_Buffer), "%s%s", (buffer2 == NULL) ? "" : " and ", "memicmp second pointer is NULL", file, line);
		strcat(st_Buffer, "\n");
		st_Output(st_Buffer);
		FORTIFY_UNLOCK();
		WaitIfstdOutput();
		return(0);
	}

	__Fortify_CheckPointer(buffer1,0,size,file,line);
	__Fortify_CheckPointer(buffer2,0,size,file,line);
	return(memicmp(buffer1,buffer2,size));
}

/*
 * Fortify_strcoll()  - check if from/to is in allocated space and if so, then check if start/end is not outside allocated space.
 */
int FORTIFY_STORAGE
_Fortify_strcoll(char *buffer1,char *buffer2,char *file,unsigned long line)
{
	if((buffer1 == NULL) || (buffer2 == NULL)) {
		sprintf(st_Buffer,
			"\nFortify: %s.%ld\n         ", file, line);
		if(buffer1 == NULL)
			sprintf(st_Buffer + strlen(st_Buffer), "%s", "strcoll first pointer is NULL", file, line);
		if(buffer2 == NULL)
			sprintf(st_Buffer + strlen(st_Buffer), "%s%s", (buffer2 == NULL) ? "" : " and ", "strcoll second pointer is NULL", file, line);
		strcat(st_Buffer, "\n");
		st_Output(st_Buffer);
		FORTIFY_UNLOCK();
		WaitIfstdOutput();
		return(0);
	}

	__Fortify_CheckPointer(buffer1,0,strlen(buffer1)+1,file,line);
	__Fortify_CheckPointer(buffer2,0,strlen(buffer2)+1,file,line);
	return(strcoll(buffer1,buffer2));
}

/*
 * Fortify_strcspn()  - check if from/to is in allocated space and if so, then check if start/end is not outside allocated space.
 */
size_t FORTIFY_STORAGE
_Fortify_strcspn(char *buffer1,char *buffer2,char *file,unsigned long line)
{
	if((buffer1 == NULL) || (buffer2 == NULL)) {
		sprintf(st_Buffer,
			"\nFortify: %s.%ld\n         ", file, line);
		if(buffer1 == NULL)
			sprintf(st_Buffer + strlen(st_Buffer), "%s", "strcspn first pointer is NULL", file, line);
		if(buffer2 == NULL)
			sprintf(st_Buffer + strlen(st_Buffer), "%s%s", (buffer2 == NULL) ? "" : " and ", "strcspn second pointer is NULL", file, line);
		strcat(st_Buffer, "\n");
		st_Output(st_Buffer);
		FORTIFY_UNLOCK();
		WaitIfstdOutput();
		return(0);
	}

	__Fortify_CheckPointer(buffer1,0,strlen(buffer1)+1,file,line);
	__Fortify_CheckPointer(buffer2,0,strlen(buffer2)+1,file,line);
	return(strcspn(buffer1,buffer2));
}

/*
 * Fortify_strcmp()  - check if from/to is in allocated space and if so, then check if start/end is not outside allocated space.
 */
int FORTIFY_STORAGE
_Fortify_strcmp(char *buffer1,char *buffer2,char *file,unsigned long line)
{
	if((buffer1 == NULL) || (buffer2 == NULL)) {
		sprintf(st_Buffer,
			"\nFortify: %s.%ld\n         ", file, line);
		if(buffer1 == NULL)
			sprintf(st_Buffer + strlen(st_Buffer), "%s", "strcmp first pointer is NULL", file, line);
		if(buffer2 == NULL)
			sprintf(st_Buffer + strlen(st_Buffer), "%s%s", (buffer2 == NULL) ? "" : " and ", "strcmp second pointer is NULL", file, line);
		strcat(st_Buffer, "\n");
		st_Output(st_Buffer);
		FORTIFY_UNLOCK();
		WaitIfstdOutput();
		return(0);
	}

	__Fortify_CheckPointer(buffer1,0,strlen(buffer1)+1,file,line);
	__Fortify_CheckPointer(buffer2,0,strlen(buffer2)+1,file,line);
	return(strcmp(buffer1,buffer2));
}

/*
 * Fortify_strcmpi()  - check if from/to is in allocated space and if so, then check if start/end is not outside allocated space.
 */
int FORTIFY_STORAGE
_Fortify_strcmpi(char *buffer1,char *buffer2,char *file,unsigned long line)
{
	if((buffer1 == NULL) || (buffer2 == NULL)) {
		sprintf(st_Buffer,
			"\nFortify: %s.%ld\n         ", file, line);
		if(buffer1 == NULL)
			sprintf(st_Buffer + strlen(st_Buffer), "%s", "strcmpi first pointer is NULL", file, line);
		if(buffer2 == NULL)
			sprintf(st_Buffer + strlen(st_Buffer), "%s%s", (buffer2 == NULL) ? "" : " and ", "strcmpi second pointer is NULL", file, line);
		strcat(st_Buffer, "\n");
		st_Output(st_Buffer);
		FORTIFY_UNLOCK();
		WaitIfstdOutput();
		return(0);
	}

	__Fortify_CheckPointer(buffer1,0,strlen(buffer1)+1,file,line);
	__Fortify_CheckPointer(buffer2,0,strlen(buffer2)+1,file,line);
	return(strcmpi(buffer1,buffer2));
}

/*
 * Fortify_strncmp()  - check if from/to is in allocated space and if so, then check if start/end is not outside allocated space.
 */
int FORTIFY_STORAGE
_Fortify_strncmp(char *buffer1,char *buffer2,size_t size,char *file,unsigned long line)
{
	if((buffer1 == NULL) || (buffer2 == NULL)) {
		sprintf(st_Buffer,
			"\nFortify: %s.%ld\n         ", file, line);
		if(buffer1 == NULL)
			sprintf(st_Buffer + strlen(st_Buffer), "%s", "strncmp first pointer is NULL", file, line);
		if(buffer2 == NULL)
			sprintf(st_Buffer + strlen(st_Buffer), "%s%s", (buffer2 == NULL) ? "" : " and ", "strncmp second pointer is NULL", file, line);
		strcat(st_Buffer, "\n");
		st_Output(st_Buffer);
		FORTIFY_UNLOCK();
		WaitIfstdOutput();
		return(0);
	}

	__Fortify_CheckPointer(buffer1,0,MIN(strlen(buffer1)+1,size),file,line);
	__Fortify_CheckPointer(buffer2,0,MIN(strlen(buffer2)+1,size),file,line);
	return(strncmp(buffer1,buffer2,size));
}

/*
 * Fortify_strnicmp()  - check if from/to is in allocated space and if so, then check if start/end is not outside allocated space.
 */
int FORTIFY_STORAGE
_Fortify_strnicmp(char *buffer1,char *buffer2,size_t size,char *file,unsigned long line)
{
	if((buffer1 == NULL) || (buffer2 == NULL)) {
		sprintf(st_Buffer,
			"\nFortify: %s.%ld\n         ", file, line);
		if(buffer1 == NULL)
			sprintf(st_Buffer + strlen(st_Buffer), "%s", "strnicmp first pointer is NULL", file, line);
		if(buffer2 == NULL)
			sprintf(st_Buffer + strlen(st_Buffer), "%s%s", (buffer2 == NULL) ? "" : " and ", "strnicmp second pointer is NULL", file, line);
		strcat(st_Buffer, "\n");
		st_Output(st_Buffer);
		FORTIFY_UNLOCK();
		WaitIfstdOutput();
		return(0);
	}

	__Fortify_CheckPointer(buffer1,0,MIN(strlen(buffer1)+1,size),file,line);
	__Fortify_CheckPointer(buffer2,0,MIN(strlen(buffer2)+1,size),file,line);
	return(strnicmp(buffer1,buffer2,size));
}

/*
 * Fortify_strchr()  - check if from/to is in allocated space and if so, then check if start/end is not outside allocated space.
 */
char *FORTIFY_STORAGE
_Fortify_strchr(char *buffer,int c,char *file,unsigned long line)
{
	if(buffer == NULL) {
		sprintf(st_Buffer,
			"\nFortify: %s.%ld\n         strchr pointer is NULL\n", file, line);
		st_Output(st_Buffer);
		FORTIFY_UNLOCK();
		WaitIfstdOutput();
		return(NULL);
	}

	__Fortify_CheckPointer(buffer,0,strlen(buffer)+1,file,line);
	return(strchr(buffer,c));
}

/*
 * Fortify_strrchr()  - check if from/to is in allocated space and if so, then check if start/end is not outside allocated space.
 */
char *FORTIFY_STORAGE
_Fortify_strrchr(char *buffer,int c,char *file,unsigned long line)
{
	if(buffer == NULL) {
		sprintf(st_Buffer,
			"\nFortify: %s.%ld\n         strchr pointer is NULL\n", file, line);
		st_Output(st_Buffer);
		FORTIFY_UNLOCK();
		WaitIfstdOutput();
		return(NULL);
	}

	__Fortify_CheckPointer(buffer,0,strlen(buffer)+1,file,line);
	return(strrchr(buffer,c));
}

/*
 * Fortify_strlwr()  - check if from/to is in allocated space and if so, then check if start/end is not outside allocated space.
 */
char *FORTIFY_STORAGE
_Fortify_strlwr(char *buffer,char *file,unsigned long line)
{
	if(buffer == NULL) {
		sprintf(st_Buffer,
			"\nFortify: %s.%ld\n         strlwr pointer is NULL\n", file, line);
		st_Output(st_Buffer);
		FORTIFY_UNLOCK();
		WaitIfstdOutput();
		return(NULL);
	}

	__Fortify_CheckPointer(buffer,0,strlen(buffer)+1,file,line);
	return(strlwr(buffer));
}

/*
 * Fortify_strlwr()  - check if from/to is in allocated space and if so, then check if start/end is not outside allocated space.
 */
char *FORTIFY_STORAGE
_Fortify_strupr(char *buffer,char *file,unsigned long line)
{
	if(buffer == NULL) {
		sprintf(st_Buffer,
			"\nFortify: %s.%ld\n         strupr pointer is NULL\n", file, line);
		st_Output(st_Buffer);
		FORTIFY_UNLOCK();
		WaitIfstdOutput();
		return(NULL);
	}

	__Fortify_CheckPointer(buffer,0,strlen(buffer)+1,file,line);
	return(strupr(buffer));
}

/*
 * Fortify_strrev()  - check if from/to is in allocated space and if so, then check if start/end is not outside allocated space.
 */
char *FORTIFY_STORAGE
_Fortify_strrev(char *buffer,char *file,unsigned long line)
{
	if(buffer == NULL) {
		sprintf(st_Buffer,
			"\nFortify: %s.%ld\n         strrev pointer is NULL\n", file, line);
		st_Output(st_Buffer);
		FORTIFY_UNLOCK();
		WaitIfstdOutput();
		return(NULL);
	}

	__Fortify_CheckPointer(buffer,0,strlen(buffer)+1,file,line);
	return(strrev(buffer));
}

/*
 * Fortify_strlen()  - check if from/to is in allocated space and if so, then check if start/end is not outside allocated space.
 */
size_t FORTIFY_STORAGE
_Fortify_strlen(char *buffer,char *file,unsigned long line)
{
	unsigned long l;

	if(buffer == NULL) {
		sprintf(st_Buffer,
			"\nFortify: %s.%ld\n         strlen pointer is NULL\n", file, line);
		st_Output(st_Buffer);
		FORTIFY_UNLOCK();
		WaitIfstdOutput();
		return(0);
	}

	l = strlen(buffer);
	__Fortify_CheckPointer(buffer,0,l+1,file,line);
	return(l);
}

/*
 * Fortify_strcat()  - check if from/to is in allocated space and if so, then check if start/end is not outside allocated space.
 */
char *FORTIFY_STORAGE
_Fortify_strcat(char *buffer1,char *buffer2,char *file,unsigned long line)
{
	unsigned long l;

	if((buffer1 == NULL) || (buffer2 == NULL)) {
		sprintf(st_Buffer,
			"\nFortify: %s.%ld\n         ", file, line);
		if(buffer1 == NULL)
			sprintf(st_Buffer + strlen(st_Buffer), "%s", "strcat first pointer is NULL", file, line);
		if(buffer2 == NULL)
			sprintf(st_Buffer + strlen(st_Buffer), "%s%s", (buffer2 == NULL) ? "" : " and ", "strcat second pointer is NULL", file, line);
		strcat(st_Buffer, "\n");
		st_Output(st_Buffer);
		FORTIFY_UNLOCK();
		WaitIfstdOutput();
		return(0);
	}

	l = strlen(buffer2)+1;
	__Fortify_CheckPointer(buffer1,0,l,file,line);
	__Fortify_CheckPointer(buffer2,0,l,file,line);
	return(strcat(buffer1,buffer2));
}

/*
 * Fortify_strpbrk()  - check if from/to is in allocated space and if so, then check if start/end is not outside allocated space.
 */
char *FORTIFY_STORAGE
_Fortify_strpbrk(char *buffer1,char *buffer2,char *file,unsigned long line)
{
	unsigned long l;

	if((buffer1 == NULL) || (buffer2 == NULL)) {
		sprintf(st_Buffer,
			"\nFortify: %s.%ld\n         ", file, line);
		if(buffer1 == NULL)
			sprintf(st_Buffer + strlen(st_Buffer), "%s", "strpbrk first pointer is NULL", file, line);
		if(buffer2 == NULL)
			sprintf(st_Buffer + strlen(st_Buffer), "%s%s", (buffer2 == NULL) ? "" : " and ", "strpbrk second pointer is NULL", file, line);
		strcat(st_Buffer, "\n");
		st_Output(st_Buffer);
		FORTIFY_UNLOCK();
		WaitIfstdOutput();
		return(0);
	}

	l = strlen(buffer2)+1;
	__Fortify_CheckPointer(buffer1,0,strlen(buffer1)+1,file,line);
	__Fortify_CheckPointer(buffer2,0,l,file,line);
	return(strpbrk(buffer1,buffer2));
}

/*
 * Fortify_strstr()  - check if from/to is in allocated space and if so, then check if start/end is not outside allocated space.
 */
char *FORTIFY_STORAGE
_Fortify_strstr(char *buffer1,char *buffer2,char *file,unsigned long line)
{
	unsigned long l;

	if((buffer1 == NULL) || (buffer2 == NULL)) {
		sprintf(st_Buffer,
			"\nFortify: %s.%ld\n         ", file, line);
		if(buffer1 == NULL)
			sprintf(st_Buffer + strlen(st_Buffer), "%s", "strstr first pointer is NULL", file, line);
		if(buffer2 == NULL)
			sprintf(st_Buffer + strlen(st_Buffer), "%s%s", (buffer2 == NULL) ? "" : " and ", "strstr second pointer is NULL", file, line);
		strcat(st_Buffer, "\n");
		st_Output(st_Buffer);
		FORTIFY_UNLOCK();
		WaitIfstdOutput();
		return(0);
	}

	l = strlen(buffer2)+1;
	__Fortify_CheckPointer(buffer1,0,strlen(buffer1)+1,file,line);
	__Fortify_CheckPointer(buffer2,0,l,file,line);
	return(strstr(buffer1,buffer2));
}

/*
 * Fortify_strtol()  - check if from/to is in allocated space and if so, then check if start/end is not outside allocated space.
 */
long FORTIFY_STORAGE
_Fortify_strtol(char *buffer1,char **buffer2,int n,char *file,unsigned long line)
{
	if(buffer1 == NULL) {
		sprintf(st_Buffer,
			"\nFortify: %s.%ld\n         ", file, line);
		sprintf(st_Buffer + strlen(st_Buffer), "%s", "strtol first pointer is NULL", file, line);
		strcat(st_Buffer, "\n");
		st_Output(st_Buffer);
		FORTIFY_UNLOCK();
		WaitIfstdOutput();
		return(0);
	}

	__Fortify_CheckPointer(buffer1,0,strlen(buffer1)+1,file,line);
	return(strtol(buffer1,buffer2,n));
}

/*
 * Fortify_atoi()  - check if from/to is in allocated space and if so, then check if start/end is not outside allocated space.
 */
int FORTIFY_STORAGE
_Fortify_atoi(char *buffer1,char *file,unsigned long line)
{
	if(buffer1 == NULL) {
		sprintf(st_Buffer,
			"\nFortify: %s.%ld\n         ", file, line);
		sprintf(st_Buffer + strlen(st_Buffer), "%s", "atoi first pointer is NULL", file, line);
		strcat(st_Buffer, "\n");
		st_Output(st_Buffer);
		FORTIFY_UNLOCK();
		WaitIfstdOutput();
		return(0);
	}

	__Fortify_CheckPointer(buffer1,0,strlen(buffer1)+1,file,line);
	return(atoi(buffer1));
}

/*
 * Fortify_atol()  - check if from/to is in allocated space and if so, then check if start/end is not outside allocated space.
 */
long FORTIFY_STORAGE
_Fortify_atol(char *buffer1,char *file,unsigned long line)
{
	if(buffer1 == NULL) {
		sprintf(st_Buffer,
			"\nFortify: %s.%ld\n         ", file, line);
		sprintf(st_Buffer + strlen(st_Buffer), "%s", "atol first pointer is NULL", file, line);
		strcat(st_Buffer, "\n");
		st_Output(st_Buffer);
		FORTIFY_UNLOCK();
		WaitIfstdOutput();
		return(0);
	}

	__Fortify_CheckPointer(buffer1,0,strlen(buffer1)+1,file,line);
	return(atol(buffer1));
}

/*
 * Fortify_atod()  - check if from/to is in allocated space and if so, then check if start/end is not outside allocated space.
 */
double FORTIFY_STORAGE
_Fortify_atof(char *buffer1,char *file,unsigned long line)
{
	if(buffer1 == NULL) {
		sprintf(st_Buffer,
			"\nFortify: %s.%ld\n         ", file, line);
		sprintf(st_Buffer + strlen(st_Buffer), "%s", "atod first pointer is NULL", file, line);
		strcat(st_Buffer, "\n");
		st_Output(st_Buffer);
		FORTIFY_UNLOCK();
		WaitIfstdOutput();
		return(0);
	}

	__Fortify_CheckPointer(buffer1,0,strlen(buffer1)+1,file,line);
	return(atof(buffer1));
}

/*
 * Fortify_strtoul()  - check if from/to is in allocated space and if so, then check if start/end is not outside allocated space.
 */
unsigned long FORTIFY_STORAGE
_Fortify_strtoul(char *buffer1,char **buffer2,int n,char *file,unsigned long line)
{
	if(buffer1 == NULL) {
		sprintf(st_Buffer,
			"\nFortify: %s.%ld\n         ", file, line);
		sprintf(st_Buffer + strlen(st_Buffer), "%s", "strtoul first pointer is NULL", file, line);
		strcat(st_Buffer, "\n");
		st_Output(st_Buffer);
		FORTIFY_UNLOCK();
		WaitIfstdOutput();
		return(0);
	}

	__Fortify_CheckPointer(buffer1,0,strlen(buffer1)+1,file,line);
	return(strtoul(buffer1,buffer2,n));
}

/*
 * Fortify_strtod()  - check if from/to is in allocated space and if so, then check if start/end is not outside allocated space.
 */
double FORTIFY_STORAGE
_Fortify_strtod(char *buffer1,char **buffer2,char *file,unsigned long line)
{
	if(buffer1 == NULL) {
		sprintf(st_Buffer,
			"\nFortify: %s.%ld\n         ", file, line);
		sprintf(st_Buffer + strlen(st_Buffer), "%s", "strtod first pointer is NULL", file, line);
		strcat(st_Buffer, "\n");
		st_Output(st_Buffer);
		FORTIFY_UNLOCK();
		WaitIfstdOutput();
		return(0);
	}

	__Fortify_CheckPointer(buffer1,0,strlen(buffer1)+1,file,line);
	return(strtod(buffer1,buffer2));
}

/*
 * Fortify_strset()  - check if from/to is in allocated space and if so, then check if start/end is not outside allocated space.
 */
char *FORTIFY_STORAGE
_Fortify_strset(char *buffer,int c,char *file,unsigned long line)
{
	if(buffer == NULL) {
		sprintf(st_Buffer,
			"\nFortify: %s.%ld\n         strset pointer is NULL\n", file, line);
		st_Output(st_Buffer);
		FORTIFY_UNLOCK();
		WaitIfstdOutput();
		return(NULL);
	}

	__Fortify_CheckPointer(buffer,0,strlen(buffer)+1,file,line);
	return(strset(buffer,c));
}

/*
 * Fortify_strnset()  - check if from/to is in allocated space and if so, then check if start/end is not outside allocated space.
 */
char *FORTIFY_STORAGE
_Fortify_strnset(char *buffer,int c,size_t size,char *file,unsigned long line)
{
	if(buffer == NULL) {
		sprintf(st_Buffer,
			"\nFortify: %s.%ld\n         strnset pointer is NULL\n", file, line);
		st_Output(st_Buffer);
		FORTIFY_UNLOCK();
		WaitIfstdOutput();
		return(NULL);
	}

	__Fortify_CheckPointer(buffer,0,strlen(buffer)+1,file,line);
	return(strnset(buffer,c,size));
}

/*
 * Fortify_strncpy()  - check if from/to is in allocated space and if so, then check if start/end is not outside allocated space.
 */
static char *FORTIFY_STORAGE
__Fortify_strncpy(char *to,char *from,size_t size,int usesize,char *file,unsigned long line)
{
	size_t size1;

	size1 = strlen(from) + 1;
	if(usesize)
	{
		if(size < size1)
			size1 = size;
	}

	return((char *) _Fortify_memcpy(to,from,size1,file,line));
}

/*
 * Fortify_strncpy()  - check if from/to is in allocated space and if so, then check if start/end is not outside allocated space.
 */
char *FORTIFY_STORAGE
_Fortify_strncpy(char *to,char *from,size_t size,char *file,unsigned long line)
{
	return(__Fortify_strncpy(to,from,size,1,file,line));
}

/*
 * Fortify_strncpy()  - check if from/to is in allocated space and if so, then check if start/end is not outside allocated space.
 */
char *FORTIFY_STORAGE
_Fortify_strcpy(char *to,char *from,char *file,unsigned long line)
{
	return(__Fortify_strncpy(to,from,0,0,file,line));
}

#endif /* FORTIFY */