1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695
|
/*
** bnfa_search.c
**
** Basic multi-pattern search engine using Aho-Corasick NFA construction.
**
** Version 3.0 (based on acsmx.c and acsmx2.c)
**
** author: marc norton
** date: started 12/21/05
**
** Copyright (C) 2005-2012 Sourcefire, Inc.
**
** General Design
** Aho-Corasick based NFA state machine.
** Compacted sparse storage mode for better performance.
** Up to 16 Million states + transitions (combined) in compacted sparse mode.
**
** ** Compacted sparse array storage **
**
** The primary data is held in one array.
** The patterns themselves are stored separately.
** The matching lists of patterns for each state are stored separately as well.
** The compacted sparse format improves caching/performance.
**
** word 1 : state ( only low 24 bits are used )
** word 2 : control word = cb << 24 | fs
** cb: control byte
** cb = mb | fb | nt
** mb : 8th bit - if set state has matching patterns bit
** fb : 7th bit - if set full storage array bit (256 entries used),
else sparse
** nt : 0-63= number of transitions (more than 63 requires full storage)
** fs: 24 bits for failure state transition index.
** word 3+ : transition word = input<<24 | next-state-index
** input : 8 bit character, input to state machine from search text
** next-state-index: 24 bits for index of next state
** (if we reallly need 16M states, we can add a state->index lookup array)
** ...repeat for each state ...
**
** * if a state is empty it has words 1 and 2, but no transition words.
**
** Construction:
**
** Patterns are added to a list based trie.
** The list based trie is compiled into a list based NFA with failure states.
** The list based NFA is converted to full or sparse format NFA.
** The Zero'th state sparse transitions may be stored in full format for
** performance.
** Sparse transition arrays are searched using linear and binary search
** strategies depending on the number of entries to search through in
** each state.
** The state machine in sparse mode is compacted into a single vector for
** better performance.
**
** Notes:
**
** The NFA can require twice the state transitions that a DFA uses. However,
** the construction of a DFA generates many additional transitions in each
** state which consumes significant additional memory. This particular
** implementation is best suited to environments where the very large memory
** requirements of a full state table implementation is not possible and/or
** the speed trade off is warranted to maintain a small memory footprint.
**
** Each state of an NFA usually has very few transitions but can have up to
** 256. It is important to not degenerate into a linear search so we utilize
** a binary search if there are more than 5 elements in the state to test for
** a match. This allows us to use a simple sparse memory design with an
** acceptable worst case search scenario. The binary search over 256 elements
** is limtied to a max of 8 tests. The zero'th state may use a full 256 state
** array, so a quick index lookup provides the next state transition. The
** zero'th state is generally visited much more than other states.
**
** Compiling : gcc, Intel C/C++, Microsoft C/C++, each optimize differently.
** My studies have shown Intel C/C++ 9,8,7 to be the fastest, Microsoft 8,7,6
** is next fastest, and gcc 4.x,3.x,2.x is the slowest of the three. My
** testing has been mainly on x86. In general gcc does a poor job with
** optimizing this state machine for performance, compared to other less cache
** and prefetch sensitive algorithms. I've documented this behavior in a
** paper 'Optimizing Pattern Matching for IDS' (www.sourcefire.com,
** www.idsresearch.org).
**
** The code is sensitive to cache optimization and prefetching, as well as
** instruction pipelining. Aren't we all. To this end, the number of
** patterns, length of search text, and cpu cache L1,L2,L3 all affect
** performance. The relative performance of the sparse and full format NFA and
** DFA varies as you vary the pattern charactersitics,and search text length,
** but strong performance trends are present and stable.
**
**
** BNFA API SUMMARY
**
** bnfa=bnfaNew(); create a state machine
** bnfaAddPattern(bnfa,..); add a pattern to the state machine
** bnfaCompile (bnfa,..) compile the state machine
** bnfaPrintInfo(bnfa); print memory usage and state info
** bnfaPrint(bnfa); print the state machine in total
** state=bnfaSearch(bnfa, ...,state); search a data buffer for a pattern match
** bnfaFree (bnfa); free the bnfa
**
**
** Reference - Efficient String matching: An Aid to Bibliographic Search
** Alfred V Aho and Margaret J Corasick
** Bell Labratories
** Copyright(C) 1975 Association for Computing Machinery,Inc
**
** 12/4/06 - man - modified summary
** 6/26/07 - man - Added last_match tracking, and accounted for nocase/case by
** preseting the last match state, and reverting if we fail the
** case memcmp test for any rule in the states matching rule
** list. The states in the defaul matcher represent either
** case or nocase states, so they are dual mode, that makes
** this a bit tricky. When we sue the pure exact match, or
** pure don't care matching routines, we just track the last
** state, and never need to revert. This only tracks the
** single repeated states and repeated data.
** 01/2008 - man - added 2 phase pattern matcher using a pattern match queue.
** Text is scanned and matching states are queued, duplicate
** matches are dropped, and after the complete buffer scan the
** queued matches are processed. This improves cacheing
** performance, and reduces duplicate rule processing. The
** queue is limited in size and is flushed if it becomes full
** during the scan. This allows simple insertions. Tracking
** queue ops is optional, as this can impose a modest
** performance hit of a few percent.
**
** LICENSE (GPL)
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License Version 2 as
** published by the Free Software Foundation. You may not use, modify or
** distribute this program under any other version of the GNU General
** Public License.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
**
*/
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "sf_types.h"
#define BNFA_TRACK_Q
#ifdef BNFA_TRACK_Q
# include "snort.h"
#endif
#include "bnfa_search.h"
#include "snort_debug.h"
#include "util.h"
/*
* Used to initialize last state, states are limited to 0-16M
* so this will not conflict.
*/
#define LAST_STATE_INIT 0xffffffff
#define printf LogMessage
/*
* Case Translation Table - his guarantees we use
* indexed lookups for case conversion
*/
static
unsigned char xlatcase[BNFA_MAX_ALPHABET_SIZE];
static
void init_xlatcase(void)
{
int i;
static int first=1;
if( !first )
return;
for(i=0; i<BNFA_MAX_ALPHABET_SIZE; i++)
{
xlatcase[i] = (unsigned char)toupper(i);
}
first=0;
}
/*
* Custom memory allocator
*/
static
void * bnfa_alloc( int n, int * m )
{
void * p = calloc(1,n);
if( p )
{
if(m)
{
m[0] += n;
}
}
return p;
}
static
void bnfa_free( void *p, int n, int * m )
{
if( p )
{
free(p);
if(m)
{
m[0] -= n;
}
}
}
#define BNFA_MALLOC(n,memory) bnfa_alloc(n,&(memory))
#define BNFA_FREE(p,n,memory) bnfa_free(p,n,&(memory))
/* queue memory traker */
static int queue_memory=0;
/*
* simple queue node
*/
typedef struct _qnode
{
unsigned state;
struct _qnode *next;
}
QNODE;
/*
* simple fifo queue structure
*/
typedef struct _queue
{
QNODE * head, *tail;
int count;
int maxcnt;
}
QUEUE;
/*
* Initialize the fifo queue
*/
static
void queue_init (QUEUE * s)
{
s->head = s->tail = 0;
s->count= 0;
s->maxcnt=0;
}
/*
* Add items to tail of queue (fifo)
*/
static
int queue_add (QUEUE * s, int state)
{
QNODE * q;
if (!s->head)
{
q = s->tail = s->head = (QNODE *) BNFA_MALLOC (sizeof(QNODE),queue_memory);
if(!q) return -1;
q->state = state;
q->next = 0;
}
else
{
q = (QNODE *) BNFA_MALLOC (sizeof(QNODE),queue_memory);
q->state = state;
q->next = 0;
s->tail->next = q;
s->tail = q;
}
s->count++;
if( s->count > s->maxcnt )
s->maxcnt = s->count;
return 0;
}
/*
* Remove items from head of queue (fifo)
*/
static
int queue_remove (QUEUE * s)
{
int state = 0;
QNODE * q;
if (s->head)
{
q = s->head;
state = q->state;
s->head = s->head->next;
s->count--;
if( !s->head )
{
s->tail = 0;
s->count = 0;
}
BNFA_FREE (q,sizeof(QNODE),queue_memory);
}
return state;
}
/*
* Return count of items in the queue
*/
static
int queue_count (QUEUE * s)
{
return s->count;
}
/*
* Free the queue
*/
static
void queue_free (QUEUE * s)
{
while (queue_count (s))
{
queue_remove (s);
}
}
/*
* Get next state from transition list
*/
static
int _bnfa_list_get_next_state( bnfa_struct_t * bnfa, int state, int input )
{
if ( state == 0 ) /* Full set of states always */
{
bnfa_state_t * p = (bnfa_state_t*)bnfa->bnfaTransTable[0];
if(!p)
{
return 0;
}
return p[input];
}
else
{
bnfa_trans_node_t * t = bnfa->bnfaTransTable[state];
while( t )
{
if( t->key == (unsigned)input )
{
return t->next_state;
}
t=t->next;
}
return BNFA_FAIL_STATE; /* Fail state */
}
}
/*
* Put next state - head insertion, and transition updates
*/
static
int _bnfa_list_put_next_state( bnfa_struct_t * bnfa, int state, int input, int next_state )
{
if( state >= bnfa->bnfaMaxStates )
{
return -1;
}
if( input >= bnfa->bnfaAlphabetSize )
{
return -1;
}
if( state == 0 )
{
bnfa_state_t * p;
p = (bnfa_state_t*)bnfa->bnfaTransTable[0];
if( !p )
{
p = (bnfa_state_t*)BNFA_MALLOC(sizeof(bnfa_state_t)*bnfa->bnfaAlphabetSize,bnfa->list_memory);
if( !p )
{
return -1;
}
bnfa->bnfaTransTable[0] = (bnfa_trans_node_t*)p;
}
if( p[input] )
{
p[input] = next_state;
return 0;
}
p[input] = next_state;
}
else
{
bnfa_trans_node_t * p;
bnfa_trans_node_t * tnew;
/* Check if the transition already exists, if so just update the next_state */
p = bnfa->bnfaTransTable[state];
while( p )
{
if( p->key == (unsigned)input ) /* transition already exists- reset the next state */
{
p->next_state = next_state;
return 0;
}
p=p->next;
}
/* Definitely not an existing transition - add it */
tnew = (bnfa_trans_node_t*)BNFA_MALLOC(sizeof(bnfa_trans_node_t),bnfa->list_memory);
if( !tnew )
{
return -1;
}
tnew->key = input;
tnew->next_state = next_state;
tnew->next = bnfa->bnfaTransTable[state];
bnfa->bnfaTransTable[state] = tnew;
}
bnfa->bnfaNumTrans++;
return 0;
}
/*
* Free the entire transition list table
*/
static
int _bnfa_list_free_table( bnfa_struct_t * bnfa )
{
int i;
bnfa_trans_node_t * t, *p;
if( !bnfa->bnfaTransTable ) return 0;
if( bnfa->bnfaTransTable[0] )
{
BNFA_FREE(bnfa->bnfaTransTable[0],sizeof(bnfa_state_t)*bnfa->bnfaAlphabetSize,bnfa->list_memory);
}
for(i=1; i<bnfa->bnfaMaxStates; i++)
{
t = bnfa->bnfaTransTable[i];
while( t )
{
p = t;
t = t->next;
BNFA_FREE(p,sizeof(bnfa_trans_node_t),bnfa->list_memory);
}
}
if( bnfa->bnfaTransTable )
{
BNFA_FREE(bnfa->bnfaTransTable,sizeof(bnfa_trans_node_t*)*bnfa->bnfaMaxStates,bnfa->list_memory);
bnfa->bnfaTransTable = 0;
}
return 0;
}
static
int bnfaBuildMatchStateTrees(bnfa_struct_t *bnfa,
int (*build_tree)(void *id, void **existing_tree),
int (*neg_list_func)(void *id, void **list))
{
int i,cnt = 0;
bnfa_match_node_t * mn;
bnfa_match_node_t ** MatchList = bnfa->bnfaMatchList;
bnfa_pattern_t * patrn;
for (i=0;i<bnfa->bnfaNumStates;i++)
{
for(mn = MatchList[i];
mn!= NULL;
mn = mn->next )
{
patrn = (bnfa_pattern_t *)mn->data;
if (patrn->userdata)
{
if (patrn->negative)
{
neg_list_func(patrn->userdata, &MatchList[i]->neg_list);
}
else
{
build_tree(patrn->userdata, &MatchList[i]->rule_option_tree);
}
}
cnt++;
}
/* Last call to finalize the tree */
if (MatchList[i])
{
build_tree(NULL, &MatchList[i]->rule_option_tree);
}
}
return cnt;
}
#ifdef ALLOW_LIST_PRINT
/*
* Print the transition list table to stdout
*/
static
int _bnfa_list_print_table( bnfa_struct_t * bnfa )
{
int i;
bnfa_trans_node_t * t;
bnfa_match_node_t * mn;
bnfa_pattern_t * patrn;
if( !bnfa->bnfaTransTable )
{
return 0;
}
printf("Print Transition Table- %d active states\n",bnfa->bnfaNumStates);
for(i=0;i< bnfa->bnfaNumStates;i++)
{
printf("state %3d: ",i);
if( i == 0 )
{
int k;
bnfa_state_t * p = (bnfa_state_t*)bnfa->bnfaTransTable[0];
if(!p) continue;
for(k=0;k<bnfa->bnfaAlphabetSize;k++)
{
if( p[k] == 0 ) continue;
if( isascii((int)p[k]) && isprint((int)p[k]) )
printf("%3c->%-5d\t",k,p[k]);
else
printf("%3d->%-5d\t",k,p[k]);
}
}
else
{
t = bnfa->bnfaTransTable[i];
while( t )
{
if( isascii((int)t->key) && isprint((int)t->key) )
printf("%3c->%-5d\t",t->key,t->next_state);
else
printf("%3d->%-5d\t",t->key,t->next_state);
t = t->next;
}
}
mn =bnfa->bnfaMatchList[i];
while( mn )
{
patrn =(bnfa_pattern_t *)mn->data;
printf("%.*s ",patrn->n,patrn->casepatrn);
mn = mn->next;
}
printf("\n");
}
return 0;
}
#endif
/*
* Converts a single row of states from list format to a full format
*/
static
int _bnfa_list_conv_row_to_full(bnfa_struct_t * bnfa, bnfa_state_t state, bnfa_state_t * full )
{
if( (int)state >= bnfa->bnfaMaxStates ) /* protects 'full' against overflow */
{
return -1;
}
if( state == 0 )
{
if( bnfa->bnfaTransTable[0] )
memcpy(full,bnfa->bnfaTransTable[0],sizeof(bnfa_state_t)*bnfa->bnfaAlphabetSize);
else
memset(full,0,sizeof(bnfa_state_t)*bnfa->bnfaAlphabetSize);
return bnfa->bnfaAlphabetSize;
}
else
{
int tcnt = 0;
bnfa_trans_node_t * t = bnfa->bnfaTransTable[ state ];
memset(full,0,sizeof(bnfa_state_t)*bnfa->bnfaAlphabetSize);
if( !t )
{
return 0;
}
while(t && (t->key < BNFA_MAX_ALPHABET_SIZE ) )
{
full[ t->key ] = t->next_state;
tcnt++;
t = t->next;
}
return tcnt;
}
}
/*
* Add pattern characters to the initial upper case trie
* unless Exact has been specified, in which case all patterns
* are assumed to be case specific.
*/
static
int _bnfa_add_pattern_states (bnfa_struct_t * bnfa, bnfa_pattern_t * p)
{
int state, next, n;
unsigned char * pattern;
bnfa_match_node_t * pmn;
n = p->n;
pattern = p->casepatrn;
state = 0;
/*
* Match up pattern with existing states
*/
for (; n > 0; pattern++, n--)
{
if( bnfa->bnfaCaseMode == BNFA_CASE )
next = _bnfa_list_get_next_state(bnfa,state,*pattern);
else
next = _bnfa_list_get_next_state(bnfa,state,xlatcase[*pattern]);
if( next == (int)BNFA_FAIL_STATE || next == 0 )
{
break;
}
state = next;
}
/*
* Add new states for the rest of the pattern bytes, 1 state per byte, uppercase
*/
for (; n > 0; pattern++, n--)
{
bnfa->bnfaNumStates++;
if( bnfa->bnfaCaseMode == BNFA_CASE )
{
if( _bnfa_list_put_next_state(bnfa,state,*pattern,bnfa->bnfaNumStates) < 0 )
return -1;
}
else
{
if( _bnfa_list_put_next_state(bnfa,state,xlatcase[*pattern],bnfa->bnfaNumStates) < 0 )
return -1;
}
state = bnfa->bnfaNumStates;
if ( bnfa->bnfaNumStates >= bnfa->bnfaMaxStates )
{
return -1;
}
}
/* Add a pattern to the list of patterns terminated at this state */
pmn = (bnfa_match_node_t*)BNFA_MALLOC(sizeof(bnfa_match_node_t),bnfa->matchlist_memory);
if( !pmn )
{
return -1;
}
pmn->data = p;
pmn->next = bnfa->bnfaMatchList[state];
bnfa->bnfaMatchList[state] = pmn;
return 0;
}
#ifdef XXXXX
int _bnfa_list_get_next_state( bnfa_struct_t * bnfa, int state, int input )
{
if ( state == 0 ) /* Full set of states always */
{
bnfa_state_t * p = (bnfa_state_t*)bnfa->bnfaTransTable[0];
if(!p)
{
return 0;
}
return p[input];
}
else
{
bnfa_trans_node_t * t = bnfa->bnfaTransTable[state];
while( t )
{
if( t->key == (unsigned)input )
{
return t->next_state;
}
t=t->next;
}
return BNFA_FAIL_STATE; /* Fail state */
}
}
#endif
static /* used only by KcontainsJ() */
int
_bnfa_conv_node_to_full(bnfa_trans_node_t *t, bnfa_state_t * full )
{
int tcnt = 0;
memset(full,0,sizeof(bnfa_state_t)*BNFA_MAX_ALPHABET_SIZE);
if( !t )
{
return 0;
}
while(t && (t->key < BNFA_MAX_ALPHABET_SIZE ) )
{
full[ t->key ] = t->next_state;
tcnt++;
t = t->next;
}
return tcnt;
}
/*
* containment test -
* test if all of tj transitions are in tk
*/
#ifdef XXXX
static
int KcontainsJx(bnfa_trans_node_t * tk, bnfa_trans_node_t *tj )
{
bnfa_trans_node_t *t;
int found;
while( tj )
{
found=0;
for( t=tk;t;t=t->next )
{
if( tj->key == t->key )
{
found=1;
break;
}
}
if( !found )
return 0;
tj=tj->next; /* get next tj key */
}
return 1;
}
#endif
static
int KcontainsJ(bnfa_trans_node_t * tk, bnfa_trans_node_t *tj )
{
bnfa_state_t full[BNFA_MAX_ALPHABET_SIZE];
if( !_bnfa_conv_node_to_full(tk,full) )
return 1; /* emtpy state */
while( tj )
{
if( !full[tj->key] )
return 0;
tj=tj->next; /* get next tj key */
}
return 1;
}
/*
* 1st optimization - eliminate duplicate fail states
*
* check if a fail state is a subset of the current state,
* if so recurse to the next fail state, and so on.
*/
static
int _bnfa_opt_nfa (bnfa_struct_t * bnfa)
{
int cnt=0;
int k, fs, fr;
bnfa_state_t * FailState = bnfa->bnfaFailState;
for(k=2;k<bnfa->bnfaNumStates;k++)
{
fr = fs = FailState[k];
while( fs && KcontainsJ(bnfa->bnfaTransTable[k],bnfa->bnfaTransTable[fs]) )
{
fs = FailState[fs];
}
if( fr != fs )
{
cnt++;
FailState[ k ] = fs;
}
}
#ifdef DEBUG
if( cnt)LogMessage("ac-bnfa: %d nfa optimizations found in %d states\n",cnt,bnfa->bnfaNumStates);
#endif
return 0;
}
/*
* Build a non-deterministic finite automata using Aho-Corasick construction
* The keyword trie must already be built via _bnfa_add_pattern_states()
*/
static
int _bnfa_build_nfa (bnfa_struct_t * bnfa)
{
int r, s, i;
QUEUE q, *queue = &q;
bnfa_state_t * FailState = bnfa->bnfaFailState;
bnfa_match_node_t ** MatchList = bnfa->bnfaMatchList;
bnfa_match_node_t * mlist;
bnfa_match_node_t * px;
/* Init a Queue */
queue_init (queue);
/* Add the state 0 transitions 1st,
* the states at depth 1, fail to state 0
*/
for (i = 0; i < bnfa->bnfaAlphabetSize; i++)
{
/* note that state zero deos not fail,
* it just returns 0..nstates-1
*/
s = _bnfa_list_get_next_state(bnfa,0,i);
if( s ) /* don't bother adding state zero */
{
if( queue_add (queue, s) )
{
return -1;
}
FailState[s] = 0;
}
}
/* Build the fail state successive layer of transitions */
while (queue_count (queue) > 0)
{
r = queue_remove (queue);
/* Find Final States for any Failure */
for(i = 0; i<bnfa->bnfaAlphabetSize; i++)
{
int fs, next;
s = _bnfa_list_get_next_state(bnfa,r,i);
if( s == (int)BNFA_FAIL_STATE )
continue;
if( queue_add (queue, s) )
{
return -1;
}
fs = FailState[r];
/*
* Locate the next valid state for 'i' starting at fs
*/
while( (next=_bnfa_list_get_next_state(bnfa,fs,i)) == (int)BNFA_FAIL_STATE )
{
fs = FailState[fs];
}
/*
* Update 's' state failure state to point to the next valid state
*/
FailState[s] = next;
/*
* Copy 'next'states MatchList into 's' states MatchList,
* we just create a new list nodes, the patterns are not copied.
*/
for( mlist = MatchList[next];mlist;mlist = mlist->next)
{
/* Dup the node, don't copy the data */
px = (bnfa_match_node_t*)BNFA_MALLOC(sizeof(bnfa_match_node_t),bnfa->matchlist_memory);
if( !px )
{
return 0;
}
px->data = mlist->data;
px->next = MatchList[s]; /* insert at head */
MatchList[s] = px;
}
}
}
/* Clean up the queue */
queue_free (queue);
/* optimize the failure states */
if( bnfa->bnfaOpt )
_bnfa_opt_nfa(bnfa);
return 0;
}
#ifdef ALLOW_NFA_FULL
/*
* Conver state machine to full format
*/
static
int _bnfa_conv_list_to_full(bnfa_struct_t * bnfa)
{
int k;
bnfa_state_t * p;
bnfa_state_t ** NextState = bnfa->bnfaNextState;
for(k=0;k<bnfa->bnfaNumStates;k++)
{
p = BNFA_MALLOC(sizeof(bnfa_state_t)*bnfa->bnfaAlphabetSize,bnfa->nextstate_memory);
if(!p)
{
return -1;
}
_bnfa_list_conv_row_to_full( bnfa, (bnfa_state_t)k, p );
NextState[k] = p; /* now we have a full format row vector */
}
return 0;
}
#endif
/*
* Convert state machine to csparse format
*
* Merges state/transition/failure arrays into one.
*
* For each state we use a state-word followed by the transition list for
* the state sw(state 0 )...tl(state 0) sw(state 1)...tl(state1) sw(state2)...
* tl(state2) ....
*
* The transition and failure states are replaced with the start index of
* transition state, this eliminates the NextState[] lookup....
*
* The compaction of multiple arays into a single array reduces the total
* number of states that can be handled since the max index is 2^24-1,
* whereas without compaction we had 2^24-1 states.
*/
static
int _bnfa_conv_list_to_csparse_array(bnfa_struct_t * bnfa)
{
int m, k, i, nc;
bnfa_state_t state;
bnfa_state_t * FailState = (bnfa_state_t *)bnfa->bnfaFailState;
bnfa_state_t * ps; /* transition list */
bnfa_state_t * pi; /* state indexes into ps */
bnfa_state_t ps_index=0;
unsigned nps;
bnfa_state_t full[BNFA_MAX_ALPHABET_SIZE];
/* count total state transitions, account for state and control words */
nps = 0;
for(k=0;k<bnfa->bnfaNumStates;k++)
{
nps++; /* state word */
nps++; /* control word */
/* count transitions */
nc = 0;
_bnfa_list_conv_row_to_full(bnfa, (bnfa_state_t)k, full );
for( i=0; i<bnfa->bnfaAlphabetSize; i++ )
{
state = full[i] & BNFA_SPARSE_MAX_STATE;
if( state != 0 )
{
nc++;
}
}
/* add in transition count */
if( (k == 0 && bnfa->bnfaForceFullZeroState) || nc > BNFA_SPARSE_MAX_ROW_TRANSITIONS )
{
nps += BNFA_MAX_ALPHABET_SIZE;
}
else
{
for( i=0; i<bnfa->bnfaAlphabetSize; i++ )
{
state = full[i] & BNFA_SPARSE_MAX_STATE;
if( state != 0 )
{
nps++;
}
}
}
}
/* check if we have too many states + transitions */
if( nps > BNFA_SPARSE_MAX_STATE )
{
/* Fatal */
return -1;
}
/*
Alloc The Transition List - we need an array of bnfa_state_t items of size 'nps'
*/
ps = BNFA_MALLOC( nps*sizeof(bnfa_state_t),bnfa->nextstate_memory);
if( !ps )
{
/* Fatal */
return -1;
}
bnfa->bnfaTransList = ps;
/*
State Index list for pi - we need an array of bnfa_state_t items of size 'NumStates'
*/
pi = BNFA_MALLOC( bnfa->bnfaNumStates*sizeof(bnfa_state_t),bnfa->nextstate_memory);
if( !pi )
{
/* Fatal */
return -1;
}
/*
Build the Transition List Array
*/
for(k=0;k<bnfa->bnfaNumStates;k++)
{
pi[k] = ps_index; /* save index of start of state 'k' */
ps[ ps_index ] = k; /* save the state were in as the 1st word */
ps_index++; /* skip past state word */
/* conver state 'k' to full format */
_bnfa_list_conv_row_to_full(bnfa, (bnfa_state_t)k, full );
/* count transitions */
nc = 0;
for( i=0; i<bnfa->bnfaAlphabetSize; i++ )
{
state = full[i] & BNFA_SPARSE_MAX_STATE;
if( state != 0 )
{
nc++;
}
}
/* add a full state or a sparse state */
if( (k == 0 && bnfa->bnfaForceFullZeroState) ||
nc > BNFA_SPARSE_MAX_ROW_TRANSITIONS )
{
/* set the control word */
ps[ps_index] = BNFA_SPARSE_FULL_BIT;
ps[ps_index] |= FailState[k] & BNFA_SPARSE_MAX_STATE;
if( bnfa->bnfaMatchList[k] )
{
ps[ps_index] |= BNFA_SPARSE_MATCH_BIT;
}
ps_index++;
/* copy the transitions */
_bnfa_list_conv_row_to_full(bnfa, (bnfa_state_t)k, &ps[ps_index] );
ps_index += BNFA_MAX_ALPHABET_SIZE; /* add in 256 transitions */
}
else
{
/* set the control word */
ps[ps_index] = nc<<BNFA_SPARSE_COUNT_SHIFT ;
ps[ps_index] |= FailState[k]&BNFA_SPARSE_MAX_STATE;
if( bnfa->bnfaMatchList[k] )
{
ps[ps_index] |= BNFA_SPARSE_MATCH_BIT;
}
ps_index++;
/* add in the transitions */
for( m=0, i=0; i<bnfa->bnfaAlphabetSize && m<nc; i++ )
{
state = full[i] & BNFA_SPARSE_MAX_STATE;
if( state != 0 )
{
ps[ps_index++] = (i<<BNFA_SPARSE_VALUE_SHIFT) | state;
m++;
}
}
}
}
/* sanity check we have not overflowed our buffer */
if( ps_index > nps )
{
/* Fatal */
return -1;
}
/*
Replace Transition states with Transition Indices.
This allows us to skip using NextState[] to locate the next state
This limits us to <16M transitions due to 24 bit state sizes, and the fact
we have now converted next-state fields to next-index fields in this array,
and we have merged the next-state and state arrays.
*/
ps_index=0;
for(k=0; k< bnfa->bnfaNumStates; k++ )
{
if( pi[k] >= nps )
{
/* Fatal */
return -1;
}
//ps_index = pi[k]; /* get index of next state */
ps_index++; /* skip state id */
/* Full Format */
if( ps[ps_index] & BNFA_SPARSE_FULL_BIT )
{
/* Do the fail-state */
ps[ps_index] = ( ps[ps_index] & 0xff000000 ) |
( pi[ ps[ps_index] & BNFA_SPARSE_MAX_STATE ] ) ;
ps_index++;
/* Do the transition-states */
for(i=0;i<BNFA_MAX_ALPHABET_SIZE;i++)
{
ps[ps_index] = ( ps[ps_index] & 0xff000000 ) |
( pi[ ps[ps_index] & BNFA_SPARSE_MAX_STATE ] ) ;
ps_index++;
}
}
/* Sparse Format */
else
{
nc = (ps[ps_index] & BNFA_SPARSE_COUNT_BITS)>>BNFA_SPARSE_COUNT_SHIFT;
/* Do the cw = [cb | fail-state] */
ps[ps_index] = ( ps[ps_index] & 0xff000000 ) |
( pi[ ps[ps_index] & BNFA_SPARSE_MAX_STATE ] );
ps_index++;
/* Do the transition-states */
for(i=0;i<nc;i++)
{
ps[ps_index] = ( ps[ps_index] & 0xff000000 ) |
( pi[ ps[ps_index] & BNFA_SPARSE_MAX_STATE ] );
ps_index++;
}
}
/* check for buffer overflow again */
if( ps_index > nps )
{
/* Fatal */
return -1;
}
}
BNFA_FREE(pi,bnfa->bnfaNumStates*sizeof(bnfa_state_t),bnfa->nextstate_memory);
return 0;
}
/*
* Print the state machine - rather verbose
*/
void bnfaPrint(bnfa_struct_t * bnfa)
{
int k;
bnfa_match_node_t ** MatchList = bnfa->bnfaMatchList;
bnfa_match_node_t * mlist;
int ps_index=0;
bnfa_state_t * ps=0;
if( !bnfa )
return;
if( !bnfa->bnfaNumStates )
return;
if( bnfa->bnfaFormat ==BNFA_SPARSE )
{
printf("Print NFA-SPARSE state machine : %d active states\n", bnfa->bnfaNumStates);
ps = bnfa->bnfaTransList;
if( !ps )
return;
}
#ifdef ALLOW_NFA_FULL
else if( bnfa->bnfaFormat ==BNFA_FULL )
{
printf("Print NFA-FULL state machine : %d active states\n", bnfa->bnfaNumStates);
}
#endif
for(k=0;k<bnfa->bnfaNumStates;k++)
{
printf(" state %-4d fmt=%d ",k,bnfa->bnfaFormat);
if( bnfa->bnfaFormat == BNFA_SPARSE )
{
unsigned i,cw,fs,nt,fb,mb;
ps_index++; /* skip state number */
cw = ps[ps_index]; /* control word */
fb = (cw & BNFA_SPARSE_FULL_BIT)>>BNFA_SPARSE_VALUE_SHIFT; /* full storage bit */
mb = (cw & BNFA_SPARSE_MATCH_BIT)>>BNFA_SPARSE_VALUE_SHIFT; /* matching state bit */
nt = (cw & BNFA_SPARSE_COUNT_BITS)>>BNFA_SPARSE_VALUE_SHIFT;/* number of transitions 0-63 */
fs = (cw & BNFA_SPARSE_MAX_STATE)>>BNFA_SPARSE_VALUE_SHIFT; /* fail state */
ps_index++; /* skip control word */
printf("mb=%3u fb=%3u fs=%-4u ",mb,fb,fs);
if( fb )
{
printf(" nt=%-3d : ",bnfa->bnfaAlphabetSize);
for( i=0; i<(unsigned)bnfa->bnfaAlphabetSize; i++, ps_index++ )
{
if( ps[ps_index] == 0 ) continue;
if( isascii((int)i) && isprint((int)i) )
printf("%3c->%-6d\t",i,ps[ps_index]);
else
printf("%3d->%-6d\t",i,ps[ps_index]);
}
}
else
{
printf(" nt=%-3d : ",nt);
for( i=0; i<nt; i++, ps_index++ )
{
if( isascii(ps[ps_index]>>BNFA_SPARSE_VALUE_SHIFT) &&
isprint(ps[ps_index]>>BNFA_SPARSE_VALUE_SHIFT) )
printf("%3c->%-6d\t",ps[ps_index]>>BNFA_SPARSE_VALUE_SHIFT,ps[ps_index] & BNFA_SPARSE_MAX_STATE);
else
printf("%3d->%-6d\t",ps[ps_index]>>BNFA_SPARSE_VALUE_SHIFT,ps[ps_index] & BNFA_SPARSE_MAX_STATE);
}
}
}
#ifdef ALLOW_NFA_FULL
else if( bnfa->bnfaFormat == BNFA_FULL )
{
int i;
bnfa_state_t state;
bnfa_state_t * p;
bnfa_state_t ** NextState;
NextState = (bnfa_state_t **)bnfa->bnfaNextState;
if( !NextState )
continue;
p = NextState[k];
printf("fs=%-4d nc=256 ",bnfa->bnfaFailState[k]);
for( i=0; i<bnfa->bnfaAlphabetSize; i++ )
{
state = p[i];
if( state != 0 && state != BNFA_FAIL_STATE )
{
if( isascii(i) && isprint(i) )
printf("%3c->%-5d\t",i,state);
else
printf("%3d->%-5d\t",i,state);
}
}
}
#endif
printf("\n");
if( MatchList[k] )
printf("---MatchList For State %d\n",k);
for( mlist = MatchList[k];
mlist!= NULL;
mlist = mlist->next )
{
bnfa_pattern_t * pat;
pat = (bnfa_pattern_t*)mlist->data;
printf("---pattern : %.*s\n",pat->n,pat->casepatrn);
}
}
}
/*
* Create a new AC state machine
*/
bnfa_struct_t * bnfaNew(void (*userfree)(void *p),
void (*optiontreefree)(void **p),
void (*neg_list_free)(void **p))
{
bnfa_struct_t * p;
int bnfa_memory=0;
init_xlatcase ();
p = (bnfa_struct_t *) BNFA_MALLOC(sizeof(bnfa_struct_t),bnfa_memory);
if(!p)
return 0;
if( p )
{
p->bnfaOpt = 0;
p->bnfaCaseMode = BNFA_PER_PAT_CASE;
p->bnfaFormat = BNFA_SPARSE;
p->bnfaAlphabetSize = BNFA_MAX_ALPHABET_SIZE;
p->bnfaForceFullZeroState = 1;
p->bnfa_memory = sizeof(bnfa_struct_t);
p->userfree = userfree;
p->optiontreefree = optiontreefree;
p->neg_list_free = neg_list_free;
}
queue_memory = 0;
return p;
}
void bnfaSetOpt(bnfa_struct_t * p, int flag)
{
p->bnfaOpt=flag;
}
void bnfaSetCase(bnfa_struct_t * p, int flag)
{
if( flag == BNFA_PER_PAT_CASE ) p->bnfaCaseMode = flag;
if( flag == BNFA_CASE ) p->bnfaCaseMode = flag;
if( flag == BNFA_NOCASE ) p->bnfaCaseMode = flag;
}
/*
* Fee all memory
*/
void bnfaFree (bnfa_struct_t * bnfa)
{
int i;
bnfa_pattern_t * patrn, *ipatrn;
bnfa_match_node_t * mlist, *ilist;
for(i = 0; i < bnfa->bnfaNumStates; i++)
{
/* free match list entries */
mlist = bnfa->bnfaMatchList[i];
while (mlist)
{
ilist = mlist;
mlist = mlist->next;
if (ilist->rule_option_tree && bnfa->optiontreefree)
{
bnfa->optiontreefree(&(ilist->rule_option_tree));
}
if (ilist->neg_list && bnfa->neg_list_free)
{
bnfa->neg_list_free(&(ilist->neg_list));
}
BNFA_FREE(ilist,sizeof(bnfa_match_node_t),bnfa->matchlist_memory);
}
bnfa->bnfaMatchList[i] = 0;
#ifdef ALLOW_NFA_FULL
/* free next state entries */
if( bnfa->bnfaFormat==BNFA_FULL )/* Full format */
{
if( bnfa->bnfaNextState[i] )
{
BNFA_FREE(bnfa->bnfaNextState[i],bnfa->bnfaAlphabetSize*sizeof(bnfa_state_t),bnfa->nextstate_memory);
}
}
#endif
}
/* Free patterns */
patrn = bnfa->bnfaPatterns;
while(patrn)
{
ipatrn=patrn;
patrn=patrn->next;
BNFA_FREE(ipatrn->casepatrn,ipatrn->n,bnfa->pat_memory);
if(bnfa->userfree && ipatrn->userdata)
bnfa->userfree(ipatrn->userdata);
BNFA_FREE(ipatrn,sizeof(bnfa_pattern_t),bnfa->pat_memory);
}
/* Free arrays */
BNFA_FREE(bnfa->bnfaFailState,bnfa->bnfaNumStates*sizeof(bnfa_state_t),bnfa->failstate_memory);
BNFA_FREE(bnfa->bnfaMatchList,bnfa->bnfaNumStates*sizeof(bnfa_pattern_t*),bnfa->matchlist_memory);
BNFA_FREE(bnfa->bnfaNextState,bnfa->bnfaNumStates*sizeof(bnfa_state_t*),bnfa->nextstate_memory);
BNFA_FREE(bnfa->bnfaTransList,(2*bnfa->bnfaNumStates+bnfa->bnfaNumTrans)*sizeof(bnfa_state_t*),bnfa->nextstate_memory);
free( bnfa ); /* cannot update memory tracker when deleting bnfa so just 'free' it !*/
}
/*
* Add a pattern to the pattern list
*/
int
bnfaAddPattern (bnfa_struct_t * p,
unsigned char *pat,
int n,
int nocase,
int negative,
void * userdata )
{
bnfa_pattern_t * plist;
plist = (bnfa_pattern_t *)BNFA_MALLOC(sizeof(bnfa_pattern_t),p->pat_memory);
if(!plist) return -1;
plist->casepatrn = (unsigned char *)BNFA_MALLOC(n,p->pat_memory );
if(!plist->casepatrn) return -1;
memcpy (plist->casepatrn, pat, n);
plist->n = n;
plist->nocase = nocase;
plist->negative = negative;
plist->userdata = userdata;
plist->next = p->bnfaPatterns; /* insert at front of list */
p->bnfaPatterns = plist;
p->bnfaPatternCnt++;
return 0;
}
/*
* Compile the patterns into an nfa state machine
*/
int
bnfaCompile (bnfa_struct_t * bnfa,
int (*build_tree)(void * id, void **existing_tree),
int (*neg_list_func )(void *id, void **list))
{
bnfa_pattern_t * plist;
bnfa_match_node_t ** tmpMatchList;
unsigned cntMatchStates;
int i;
queue_memory =0;
/* Count number of states */
for(plist = bnfa->bnfaPatterns; plist != NULL; plist = plist->next)
{
bnfa->bnfaMaxStates += plist->n;
}
bnfa->bnfaMaxStates++; /* one extra */
/* Alloc a List based State Transition table */
bnfa->bnfaTransTable =(bnfa_trans_node_t**) BNFA_MALLOC(sizeof(bnfa_trans_node_t*) * bnfa->bnfaMaxStates,bnfa->list_memory );
if(!bnfa->bnfaTransTable)
{
return -1;
}
/* Alloc a MatchList table - this has a list of pattern matches for each state */
bnfa->bnfaMatchList=(bnfa_match_node_t**) BNFA_MALLOC(sizeof(void*)*bnfa->bnfaMaxStates,bnfa->matchlist_memory );
if(!bnfa->bnfaMatchList)
{
return -1;
}
/* Add each Pattern to the State Table - This forms a keyword trie using lists */
bnfa->bnfaNumStates = 0;
for (plist = bnfa->bnfaPatterns; plist != NULL; plist = plist->next)
{
_bnfa_add_pattern_states (bnfa, plist);
}
bnfa->bnfaNumStates++;
if( bnfa->bnfaNumStates > BNFA_SPARSE_MAX_STATE )
{
return -1; /* Call bnfaFree to clean up */
}
/* ReAlloc a smaller MatchList table - only need NumStates */
tmpMatchList=bnfa->bnfaMatchList;
bnfa->bnfaMatchList=(bnfa_match_node_t**)BNFA_MALLOC(sizeof(void*) * bnfa->bnfaNumStates,bnfa->matchlist_memory);
if(!bnfa->bnfaMatchList)
{
return -1;
}
memcpy(bnfa->bnfaMatchList,tmpMatchList,sizeof(void*) * bnfa->bnfaNumStates);
BNFA_FREE(tmpMatchList,sizeof(void*) * bnfa->bnfaMaxStates,bnfa->matchlist_memory);
#ifdef MATCH_LIST_CNT
bnfa->bnfaMatchListCnt=(unsigned*)calloc(sizeof(unsigned) * bnfa->bnfaNumStates);
if(!bnfa->bnfaMatchListCnt)
{
return -1;
}
#endif
/* Alloc a failure state table - only need NumStates */
bnfa->bnfaFailState =(bnfa_state_t*)BNFA_MALLOC(sizeof(bnfa_state_t) * bnfa->bnfaNumStates,bnfa->failstate_memory);
if(!bnfa->bnfaFailState)
{
return -1;
}
#ifdef ALLOW_NFA_FULL
if( bnfa->bnfaFormat == BNFA_FULL )
{
/* Alloc a state transition table - only need NumStates */
bnfa->bnfaNextState=(bnfa_state_t**)BNFA_MALLOC(sizeof(bnfa_state_t*) * bnfa->bnfaNumStates,bnfa->nextstate_memory);
if(!bnfa->bnfaNextState)
{
return -1;
}
}
#endif
/* Build the nfa w/failure states - time the nfa construction */
if( _bnfa_build_nfa (bnfa) )
{
return -1;
}
/* Convert nfa storage format from list to full or sparse */
if( bnfa->bnfaFormat == BNFA_SPARSE )
{
if( _bnfa_conv_list_to_csparse_array(bnfa) )
{
return -1;
}
BNFA_FREE(bnfa->bnfaFailState,sizeof(bnfa_state_t)*bnfa->bnfaNumStates,bnfa->failstate_memory);
bnfa->bnfaFailState=0;
}
#ifdef ALLOW_NFA_FULL
else if( bnfa->bnfaFormat == BNFA_FULL )
{
if( _bnfa_conv_list_to_full( bnfa ) )
{
return -1;
}
}
#endif
else
{
return -1;
}
/* Free up the Table Of Transition Lists */
_bnfa_list_free_table( bnfa );
/* Count states with Pattern Matches */
cntMatchStates=0;
for(i=0;i<bnfa->bnfaNumStates;i++)
{
if( bnfa->bnfaMatchList[i] )
cntMatchStates++;
}
bnfa->bnfaMatchStates = cntMatchStates;
bnfa->queue_memory = queue_memory;
bnfaAccumInfo( bnfa );
if (build_tree && neg_list_func)
{
bnfaBuildMatchStateTrees( bnfa, build_tree, neg_list_func );
}
return 0;
}
#ifdef ALLOW_NFA_FULL
/*
* Full Matrix Format Search
*/
static
inline
unsigned
_bnfa_search_full_nfa( bnfa_struct_t * bnfa, unsigned char *Tx, int n,
int (*Match)(bnfa_pattern_t * id, void *tree, int index, void *data, void *neg_list),
void *data, bnfa_state_t state, int *current_state )
{
unsigned char * Tend;
unsigned char * T;
unsigned char Tchar;
unsigned index;
bnfa_state_t ** NextState= bnfa->bnfaNextState;
bnfa_state_t * FailState= bnfa->bnfaFailState;
bnfa_match_node_t ** MatchList= bnfa->bnfaMatchList;
bnfa_state_t * pcs;
bnfa_match_node_t * mlist;
bnfa_pattern_t * patrn;
unsigned nfound = 0;
int res;
unsigned last_match=LAST_STATE_INIT;
unsigned last_match_saved=LAST_STATE_INIT;
T = Tx;
Tend = T + n;
for( ; T < Tend; T++ )
{
Tchar = xlatcase[ *T ];
for(;;)
{
pcs = NextState[state];
if( pcs[Tchar] == 0 && state > 0 )
{
state = FailState[state];
}
else
{
state = pcs[Tchar];
break;
}
}
if( state )
{
if( state == last_match )
continue;
last_match_saved=last_match;
last_match = state;
{
mlist = MatchList[state];
if (!mlist)
{
continue;
}
patrn = (bnfa_pattern_t*)mlist->data;
index = T - Tx - patrn->n + 1;
nfound++;
/* Don't do anything specific for case sensitive patterns and not,
* since that will be covered by the rule tree itself. Each tree
* might have both case sensitive & case insensitive patterns.
*/
res = Match (patrn->userdata, mlist->rule_option_tree, index, data, mlist->neg_list);
if ( res > 0 )
{
*current_state = state;
return nfound;
}
else if( res < 0 )
{
last_match = last_match_saved;
}
}
}
}
return nfound;
}
/*
* Full Matrix Format Search - Exact matching patterns only
*/
static
inline
unsigned
_bnfa_search_full_nfa_case( bnfa_struct_t * bnfa, unsigned char *Tx, int n,
int (*Match)(bnfa_pattern_t * id, void *tree, int index, void *data, void *neg_list),
void *data, bnfa_state_t state, int *current_state )
{
unsigned char * Tend;
unsigned char * T;
unsigned char Tchar;
unsigned index;
bnfa_state_t ** NextState= bnfa->bnfaNextState;
bnfa_state_t * FailState= bnfa->bnfaFailState;
bnfa_match_node_t ** MatchList= bnfa->bnfaMatchList;
bnfa_state_t * pcs;
bnfa_match_node_t * mlist;
bnfa_pattern_t * patrn;
unsigned nfound = 0;
unsigned last_match=LAST_STATE_INIT;
unsigned last_match_saved=LAST_STATE_INIT;
int res;
T = Tx;
Tend = T + n;
for( ; T < Tend; T++ )
{
Tchar = *T ;
for(;;)
{
pcs = NextState[state];
if( pcs[Tchar] == 0 && state > 0 )
{
state = FailState[state];
}
else
{
state = pcs[Tchar];
break;
}
}
if( state )
{
if( state == last_match )
continue;
last_match_saved=last_match;
last_match = state;
{
mlist = MatchList[state];
if (!mlist)
{
continue;
}
patrn = (bnfa_pattern_t*)mlist->data;
index = T - Tx - patrn->n + 1;
nfound++;
/* Don't do anything specific for case (in)sensitive patterns
* since that will be covered by the rule tree itself. Each
* tree might have both case sensitive & case insensitive patterns.
*/
res = Match (patrn->userdata, mlist->rule_option_tree, index, data, mlist->neg_list);
if ( res > 0 )
{
*current_state = state;
return nfound;
}
else if( res < 0 )
{
last_match = last_match_saved;
}
}
}
}
return nfound;
}
/*
* Full Matrix Format Search - no case
*/
static
inline
unsigned
_bnfa_search_full_nfa_nocase( bnfa_struct_t * bnfa, unsigned char *Tx, int n,
int (*Match)(bnfa_pattern_t * id, void *tree, int index, void *data, void *neg_list),
void *data, bnfa_state_t state, int *current_state )
{
unsigned char * Tend;
unsigned char * T;
unsigned char Tchar;
unsigned index;
bnfa_state_t ** NextState= bnfa->bnfaNextState;
bnfa_state_t * FailState= bnfa->bnfaFailState;
bnfa_match_node_t ** MatchList= bnfa->bnfaMatchList;
bnfa_state_t * pcs;
bnfa_match_node_t * mlist;
bnfa_pattern_t * patrn;
unsigned nfound = 0;
unsigned last_match=LAST_STATE_INIT;
unsigned last_match_saved=LAST_STATE_INIT;
int res;
T = Tx;
Tend = T + n;
for( ; T < Tend; T++ )
{
Tchar = xlatcase[ *T ];
for(;;)
{
pcs = NextState[state];
if( pcs[Tchar] == 0 && state > 0 )
{
state = FailState[state];
}
else
{
state = pcs[Tchar];
break;
}
}
if( state )
{
if( state == last_match )
continue;
last_match_saved=last_match;
last_match = state;
{
mlist = MatchList[state];
if (!mlist)
{
continue;
}
patrn = (bnfa_pattern_t*)mlist->data;
index = T - Tx - patrn->n + 1;
/* Don't do anything specific for case sensitive patterns and not,
* since that will be covered by the rule tree itself. Each tree
* might have both case sensitive & case insensitive patterns.
*/
res = Match (patrn->userdata, mlist->rule_option_tree, index, data, mlist->neg_list);
if ( res > 0 )
{
*current_state = state;
return nfound;
}
else if( res < 0 )
{
last_match = last_match_saved;
}
}
}
}
return nfound;
}
#endif
/*
binary array search on sparse transition array
O(logN) search times..same as a binary tree.
data must be in sorted order in the array.
return: = -1 => not found
>= 0 => index of element 'val'
notes:
val is tested against the high 8 bits of the 'a' array entry,
this is particular to the storage format we are using.
*/
static
inline
int _bnfa_binearch( bnfa_state_t * a, int a_len, int val )
{
int m, l, r;
int c;
l = 0;
r = a_len - 1;
while( r >= l )
{
m = ( r + l ) >> 1;
c = a[m] >> BNFA_SPARSE_VALUE_SHIFT;
if( val == c )
{
return m;
}
else if( val < c )
{
r = m - 1;
}
else /* val > c */
{
l = m + 1;
}
}
return -1;
}
/*
* Sparse format for state table using single array storage
*
* word 1: state
* word 2: control-word = cb<<24| fs
* cb : control-byte
* : mb | fb | nt
* mb : bit 8 set if match state, zero otherwise
* fb : bit 7 set if using full format, zero otherwise
* nt : number of transitions 0..63 (more than 63 requires full format)
* fs: failure-transition-state
* word 3+: byte-value(0-255) << 24 | transition-state
*/
static
inline
unsigned
_bnfa_get_next_state_csparse_nfa_qx(bnfa_state_t * pcx, unsigned sindex, unsigned input)
{
int k;
int nc;
int index;
register bnfa_state_t * pcs;
for(;;)
{
pcs = pcx + sindex + 1; /* skip state-id == 1st word */
if( pcs[0] & BNFA_SPARSE_FULL_BIT )
{
if( sindex == 0 )
{
return pcs[1+input] & BNFA_SPARSE_MAX_STATE;
}
else
{
if( pcs[1+input] & BNFA_SPARSE_MAX_STATE )
return pcs[1+input] & BNFA_SPARSE_MAX_STATE;
}
}
else
{
nc = (pcs[0]>>BNFA_SPARSE_COUNT_SHIFT) & BNFA_SPARSE_MAX_ROW_TRANSITIONS;
if( nc > BNFA_SPARSE_LINEAR_SEARCH_LIMIT )
{
/* binary search... */
index = _bnfa_binearch( pcs+1, nc, input );
if( index >= 0 )
{
return pcs[index+1] & BNFA_SPARSE_MAX_STATE;
}
}
else
{
/* linear search... */
for( k=0; k<nc; k++ )
{
if( (pcs[k+1]>>BNFA_SPARSE_VALUE_SHIFT) == input )
{
return pcs[k+1] & BNFA_SPARSE_MAX_STATE;
}
}
}
}
return 0; /* no transition keyword match failed */
}
}
/*
* Sparse format for state table using single array storage
*
* word 1: state
* word 2: control-word = cb<<24| fs
* cb : control-byte
* : mb | fb | nt
* mb : bit 8 set if match state, zero otherwise
* fb : bit 7 set if using full format, zero otherwise
* nt : number of transitions 0..63 (more than 63 requires full format)
* fs: failure-transition-state
* word 3+: byte-value(0-255) << 24 | transition-state
*/
static
inline
unsigned
_bnfa_get_next_state_csparse_nfa(bnfa_state_t * pcx, unsigned sindex, unsigned input)
{
int k;
int nc;
int index;
register bnfa_state_t * pcs;
for(;;)
{
pcs = pcx + sindex + 1; /* skip state-id == 1st word */
if( pcs[0] & BNFA_SPARSE_FULL_BIT )
{
if( sindex == 0 )
{
return pcs[1+input] & BNFA_SPARSE_MAX_STATE;
}
else
{
if( pcs[1+input] & BNFA_SPARSE_MAX_STATE )
return pcs[1+input] & BNFA_SPARSE_MAX_STATE;
}
}
else
{
nc = (pcs[0]>>BNFA_SPARSE_COUNT_SHIFT) & BNFA_SPARSE_MAX_ROW_TRANSITIONS;
if( nc > BNFA_SPARSE_LINEAR_SEARCH_LIMIT )
{
/* binary search... */
index = _bnfa_binearch( pcs+1, nc, input );
if( index >= 0 )
{
return pcs[index+1] & BNFA_SPARSE_MAX_STATE;
}
}
else
{
/* linear search... */
for( k=0; k<nc; k++ )
{
if( (pcs[k+1]>>BNFA_SPARSE_VALUE_SHIFT) == input )
{
return pcs[k+1] & BNFA_SPARSE_MAX_STATE;
}
}
}
}
/* no transition found ... get the failure state and try again */
sindex = pcs[0] & BNFA_SPARSE_MAX_STATE;
}
}
/*
* Per Pattern case search, case is on per pattern basis
* standard snort search
* note: index is not used by snort, so it's commented
* TRACK_Q can impose a modest couple % performance difference in the
* pattern matching rate.
*/
/* Queue whole pattern groups at end states in AC */
void bnfa_print_qinfo(void)
{
#ifdef BNFA_TRACK_Q
if( snort_conf->max_inq )
{
LogMessage("ac-bnfa: queue size = %d, max = %d\n",snort_conf->max_inq, MAX_INQ );
LogMessage("ac-bnfa: queue flushes = "STDu64"\n", snort_conf->tot_inq_flush );
LogMessage("ac-bnfa: queue inserts = "STDu64"\n", snort_conf->tot_inq_inserts );
LogMessage("ac-bnfa: queue uinserts = "STDu64"\n", snort_conf->tot_inq_uinserts );
}
#endif
}
static
inline
void
_init_queue(bnfa_struct_t * b)
{
b->inq=0;
b->inq_flush=0;
}
/* uniquely insert into q, should splay elements for performance */
static
inline
int
_add_queue(bnfa_struct_t* b, bnfa_match_node_t * p )
{
int i;
#ifdef BNFA_TRACK_Q
snort_conf->tot_inq_inserts++;
#endif
for(i=(int)(b->inq)-1;i>=0;i--)
if( p == b->q[i] )
return 0;
#ifdef BNFA_TRACK_Q
snort_conf->tot_inq_uinserts++;
#endif
if( b->inq < MAX_INQ )
{
b->q[ b->inq++ ] = p;
}
if( b->inq == MAX_INQ )
{
#ifdef BNFA_TRACK_Q
b->inq_flush++;
#endif
return 1;
}
return 0;
}
static
inline
unsigned
_process_queue( bnfa_struct_t * bnfa,
int (*Match)(bnfa_pattern_t * id, void *tree, int index, void *data, void *neg_list),
void *data )
{
bnfa_match_node_t * mlist;
bnfa_pattern_t * patrn;
int res;
unsigned int i;
#ifdef BNFA_TRACK_Q
if( bnfa->inq > snort_conf->max_inq )
snort_conf->max_inq = bnfa->inq;
snort_conf->tot_inq_flush += bnfa->inq_flush;
#endif
for( i=0; i<bnfa->inq; i++ )
{
mlist = bnfa->q[i];
if (mlist)
{
patrn = (bnfa_pattern_t*)mlist->data;
/*process a pattern - case is handled by otn processing */
res = Match (patrn->userdata, mlist->rule_option_tree, 0, data, mlist->neg_list);
if ( res > 0 )
{ /* terminate matching */
bnfa->inq=0;/* clear the q */
return 1;
}
}
}
bnfa->inq=0;/* clear the q */
return 0;
}
static
inline
unsigned
_bnfa_search_csparse_nfa_qx(bnfa_struct_t * bnfa, unsigned char *T, int n,
int (*Match)(bnfa_pattern_t * id, void *tree, int index, void *data, void *neg_list),
void *data )
{
bnfa_match_node_t * mlist;
unsigned char * Tend;
bnfa_match_node_t ** MatchList = bnfa->bnfaMatchList;
bnfa_state_t * transList = bnfa->bnfaTransList;
unsigned sindex=0;
Tend = T + n;
for(; T<Tend; T++)
{
/* Transition to next state index */
sindex = _bnfa_get_next_state_csparse_nfa_qx(transList,sindex,xlatcase[*T]);
/* Log matches in this state - if any */
if( sindex )
{
if( transList[sindex+1] & BNFA_SPARSE_MATCH_BIT )
{
mlist = MatchList[ transList[sindex] ];
if( mlist )
{
if( _add_queue(bnfa,mlist) )
{
if( _process_queue( bnfa, Match, data ) )
{
return 1;
}
}
}
}
}
else
{
return 0;
}
}
return 0;
}
static
inline
unsigned
_bnfa_search_csparse_nfa_q( bnfa_struct_t * bnfa, unsigned char *T, int n,
int (*Match)(bnfa_pattern_t * id, void *tree, int index, void *data, void *neg_list),
void *data, unsigned sindex, int *current_state )
{
bnfa_match_node_t * mlist;
unsigned char * Tend;
bnfa_match_node_t ** MatchList = bnfa->bnfaMatchList;
bnfa_state_t * transList = bnfa->bnfaTransList;
unsigned last_sindex;
Tend = T + n;
_init_queue(bnfa);
for(; T<Tend; T++)
{
last_sindex = sindex;
/* Transition to next state index */
sindex = _bnfa_get_next_state_csparse_nfa(transList,sindex,xlatcase[*T]);
/* Log matches in this state - if any */
if(sindex && (transList[sindex+1] & BNFA_SPARSE_MATCH_BIT) )
{
/* Test for same as last state */
if( sindex == last_sindex )
continue;
mlist = MatchList[ transList[sindex] ];
if( mlist )
{
if( _add_queue(bnfa,mlist) )
{
if( _process_queue( bnfa, Match, data ) )
{
*current_state = sindex;
return 1;
}
}
}
}
}
*current_state = sindex;
return _process_queue( bnfa, Match, data );
}
/*
* Per Pattern case search, case is on per pattern basis
* standard snort search
*
* note: index is not used by snort, so it's commented
*/
static
inline
unsigned
_bnfa_search_csparse_nfa( bnfa_struct_t * bnfa, unsigned char *Tx, int n,
int (*Match)(bnfa_pattern_t * id, void *tree, int index, void *data, void *neg_list),
void *data, unsigned sindex, int *current_state )
{
bnfa_match_node_t * mlist;
unsigned char * Tend;
unsigned char * T;
unsigned char Tchar;
unsigned index;
bnfa_match_node_t ** MatchList = bnfa->bnfaMatchList;
bnfa_pattern_t * patrn;
bnfa_state_t * transList = bnfa->bnfaTransList;
unsigned nfound = 0;
unsigned last_match=LAST_STATE_INIT;
unsigned last_match_saved=LAST_STATE_INIT;
int res;
#ifdef MATCH_LIST_CNT
unsigned * MatchTestCnt = bnfa->bnfaMatchTestCnt;
#endif
T = Tx;
Tend = T + n;
for(; T<Tend; T++)
{
Tchar = xlatcase[ *T ];
/* Transition to next state index */
sindex = _bnfa_get_next_state_csparse_nfa(transList,sindex,Tchar);
/* Log matches in this state - if any */
if( sindex && (transList[sindex+1] & BNFA_SPARSE_MATCH_BIT) )
{
if( sindex == last_match )
continue;
last_match_saved = last_match;
last_match = sindex;
#ifdef MATCH_LIST_CNT
if( MatchList[ transList[sindex] ] )
MatchTestCnt[ transList[index] ]++;
#endif
{
mlist = MatchList[ transList[sindex] ];
patrn = (bnfa_pattern_t*)mlist->data;
index = T - Tx - patrn->n + 1;
nfound++;
/* Don't do anything specific for case sensitive patterns and not,
* since that will be covered by the rule tree itself. Each tree
* might have both case sensitive & case insensitive patterns.
*/
res = Match (patrn->userdata, mlist->rule_option_tree, index, data, mlist->neg_list);
if ( res > 0 )
{
*current_state = sindex;
return nfound;
}
else if( res < 0 )
{
last_match = last_match_saved;
}
}
}
}
return nfound;
}
/*
* Case specific search, global to all patterns
*
* note: index is not used by snort, so it's commented
*/
static
inline
unsigned
_bnfa_search_csparse_nfa_case( bnfa_struct_t * bnfa, unsigned char *Tx, int n,
int (*Match)(bnfa_pattern_t * id, void *tree, int index, void *data, void *neg_list),
void *data, unsigned sindex, int *current_state )
{
bnfa_match_node_t * mlist;
unsigned char * Tend;
unsigned char * T;
unsigned index;
bnfa_match_node_t ** MatchList = bnfa->bnfaMatchList;
bnfa_pattern_t * patrn;
bnfa_state_t * transList = bnfa->bnfaTransList;
unsigned nfound = 0;
unsigned last_match=LAST_STATE_INIT;
unsigned last_match_saved=LAST_STATE_INIT;
int res;
T = Tx;
Tend = T + n;
for(; T<Tend; T++)
{
/* Transition to next state index */
sindex = _bnfa_get_next_state_csparse_nfa(transList,sindex,*T);
/* Log matches in this state - if any */
if( sindex && (transList[sindex+1] & BNFA_SPARSE_MATCH_BIT) )
{
if( sindex == last_match )
continue;
last_match_saved = last_match;
last_match = sindex;
{
mlist = MatchList[ transList[sindex] ];
patrn = (bnfa_pattern_t*)mlist->data;
index = T - Tx - patrn->n + 1;
nfound++;
/* Don't do anything specific for case sensitive patterns and not,
* since that will be covered by the rule tree itself. Each tree
* might have both case sensitive & case insensitive patterns.
*/
res = Match (patrn->userdata, mlist->rule_option_tree, index, data, mlist->neg_list);
if ( res > 0 )
{
*current_state = sindex;
return nfound;
}
else if( res < 0 )
{
last_match = last_match_saved;
}
}
}
}
return nfound;
}
/*
* NoCase search - global to all patterns
*
* note: index is not used by snort, so it's commented
*/
static
inline
unsigned
_bnfa_search_csparse_nfa_nocase( bnfa_struct_t * bnfa, unsigned char *Tx, int n,
int (*Match)(bnfa_pattern_t * id, void *tree, int index, void *data, void *neg_list),
void *data, unsigned sindex, int *current_state )
{
bnfa_match_node_t * mlist;
unsigned char * Tend;
unsigned char * T;
unsigned char Tchar;
unsigned index;
bnfa_match_node_t ** MatchList = bnfa->bnfaMatchList;
bnfa_pattern_t * patrn;
bnfa_state_t * transList = bnfa->bnfaTransList;
unsigned nfound = 0;
unsigned last_match=LAST_STATE_INIT;
unsigned last_match_saved=LAST_STATE_INIT;
int res;
T = Tx;
Tend = T + n;
for(; T<Tend; T++)
{
Tchar = xlatcase[ *T ];
/* Transition to next state index */
sindex = _bnfa_get_next_state_csparse_nfa(transList,sindex,Tchar);
/* Log matches in this state - if any */
if( sindex && (transList[sindex+1] & BNFA_SPARSE_MATCH_BIT) )
{
if( sindex == last_match )
continue;
last_match_saved = last_match;
last_match = sindex;
{
mlist = MatchList[ transList[sindex] ];
patrn = (bnfa_pattern_t*)mlist->data;
index = T - Tx - patrn->n + 1;
nfound++;
/* Don't do anything specific for case sensitive patterns and not,
* since that will be covered by the rule tree itself. Each tree
* might have both case sensitive & case insensitive patterns.
*/
res = Match (patrn->userdata, mlist->rule_option_tree, index, data, mlist->neg_list);
if ( res > 0 )
{
*current_state = sindex;
return nfound;
}
else if( res < 0 )
{
last_match = last_match_saved;
}
}
}
}
return nfound;
}
/*
* BNFA Search Function
*
* bnfa - state machine
* Tx - text buffer to search
* n - number of bytes in Tx
* Match - function to call when a match is found
* data - user supplied data that is passed to the Match function
* sindex - state tracker, set value to zero to reset the state machine,
* zero should be the value passed in on the 1st buffer or each buffer
* that is to be analyzed on its own, the state machine updates this
* during searches. This allows for sequential buffer searchs without
* reseting the state machine. Save this value as returned from the
* previous search for the next search.
*
* returns
* The state or sindex of the state machine. This can than be passed back
* in on the next search, if desired.
*/
unsigned
bnfaSearchX( bnfa_struct_t * bnfa, unsigned char *T, int n,
int (*Match)(bnfa_pattern_t * id, void *tree, int index, void *data, void *neg_list),
void *data, unsigned sindex, int* current_state )
{
int ret;
_init_queue(bnfa);
while( n > 0)
{
ret = _bnfa_search_csparse_nfa_qx( bnfa, T++, n--, Match, data );
if( ret )
return 0;
}
return _process_queue( bnfa, Match, data );
}
unsigned
bnfaSearch( bnfa_struct_t * bnfa, unsigned char *Tx, int n,
int (*Match)(void * id, void *tree, int index, void *data, void *neg_list),
void *data, unsigned sindex, int* current_state )
{
int ret = 0;
/***** This should be tested before we use it *******/
/*
if (current_state)
{
sindex = (unsigned)*current_state;
}
*/
#ifdef ALLOW_NFA_FULL
if( bnfa->bnfaFormat == BNFA_SPARSE )
{
if( bnfa->bnfaCaseMode == BNFA_PER_PAT_CASE )
{
if (bnfa->bnfaMethod)
{
ret = _bnfa_search_csparse_nfa( bnfa, Tx, n,
(int (*)(bnfa_pattern_t * id, void *tree, int index, void *data, void *neg_list))
Match, data, sindex, current_state );
}
else
{
ret = _bnfa_search_csparse_nfa_q( bnfa, Tx, n,
(int (*)(bnfa_pattern_t * id, void *tree, int index, void *data, void *neg_list))
Match, data, sindex, current_state );
}
}
else if( bnfa->bnfaCaseMode == BNFA_CASE )
{
ret = _bnfa_search_csparse_nfa_case( bnfa, Tx, n,
(int (*)(bnfa_pattern_t * id, void *tree, int index, void *data, void *neg_list))
Match, data, sindex, current_state );
}
else /* NOCASE */
{
ret = _bnfa_search_csparse_nfa_nocase( bnfa, Tx, n,
(int (*)(bnfa_pattern_t * id, void *tree, int index, void *data, void *neg_list))
Match, data, sindex, current_state );
}
}
else if( bnfa->bnfaFormat == BNFA_FULL )
{
if( bnfa->bnfaCaseMode == BNFA_PER_PAT_CASE )
{
ret = _bnfa_search_full_nfa( bnfa, Tx, n,
(int (*)(bnfa_pattern_t * id, void *tree, int index, void *data, void *neg_list))
Match, data, (bnfa_state_t) sindex, current_state );
}
else if( bnfa->bnfaCaseMode == BNFA_CASE )
{
ret = _bnfa_search_full_nfa_case( bnfa, Tx, n,
(int (*)(bnfa_pattern_t * id, void *tree, int index, void *data, void *neg_list))
Match, data, (bnfa_state_t) sindex, current_state );
}
else
{
ret = _bnfa_search_full_nfa_nocase( bnfa, Tx, n,
(int (*)(bnfa_pattern_t * id, void *tree, int index, void *data, void *neg_list))
Match, data, (bnfa_state_t) sindex, current_state );
}
}
#else
if( bnfa->bnfaCaseMode == BNFA_PER_PAT_CASE )
{
if (bnfa->bnfaMethod)
{
ret = _bnfa_search_csparse_nfa( bnfa, Tx, n,
(int (*)(bnfa_pattern_t * id, void *tree, int index, void *data, void *neg_list))
Match, data, sindex, current_state );
}
else
{
ret = _bnfa_search_csparse_nfa_q( bnfa, Tx, n,
(int (*)(bnfa_pattern_t * id, void *tree, int index, void *data, void *neg_list))
Match, data, sindex, current_state );
}
}
else if( bnfa->bnfaCaseMode == BNFA_CASE )
{
ret = _bnfa_search_csparse_nfa_case( bnfa, Tx, n,
(int (*)(bnfa_pattern_t * id, void *tree, int index, void *data, void *neg_list))
Match, data, sindex, current_state );
}
else/* NOCASE */
{
ret = _bnfa_search_csparse_nfa_nocase( bnfa, Tx, n,
(int (*)(bnfa_pattern_t * id, void *tree, int index, void *data, void *neg_list))
Match, data, sindex, current_state );
}
#endif
return ret;
}
int bnfaPatternCount( bnfa_struct_t * p)
{
return p->bnfaPatternCnt;
}
/*
* Summary Info Data
*/
static bnfa_struct_t summary;
static int summary_cnt=0;
/*
* Info: Print info a particular state machine.
*/
void bnfaPrintInfoEx( bnfa_struct_t * p, char * text )
{
unsigned max_memory;
if( !p->bnfaNumStates )
{
return;
}
max_memory = p->bnfa_memory + p->pat_memory + p->list_memory +
p->matchlist_memory + p->failstate_memory + p->nextstate_memory;
if( text && summary_cnt )
{
LogMessage("+-[AC-BNFA Search Info%s]------------------------------\n",text);
LogMessage("| Instances : %d\n",summary_cnt);
}
else
{
LogMessage("+-[AC-BNFA Search Info]------------------------------\n");
}
LogMessage("| Patterns : %d\n",p->bnfaPatternCnt);
LogMessage("| Pattern Chars : %d\n",p->bnfaMaxStates);
LogMessage("| Num States : %d\n",p->bnfaNumStates);
LogMessage("| Num Match States : %d\n",p->bnfaMatchStates);
if( max_memory < 1024*1024 )
{
LogMessage("| Memory : %.2fKbytes\n", (double)max_memory/1024 );
LogMessage("| Patterns : %.2fK\n",(double)p->pat_memory/1024 );
LogMessage("| Match Lists : %.2fK\n",(double)p->matchlist_memory/1024 );
LogMessage("| Transitions : %.2fK\n",(double)p->nextstate_memory/1024 );
}
else
{
LogMessage("| Memory : %.2fMbytes\n", (double)max_memory/(1024*1024) );
LogMessage("| Patterns : %.2fM\n",(double)p->pat_memory/(1024*1024) );
LogMessage("| Match Lists : %.2fM\n",(double)p->matchlist_memory/(1024*1024) );
LogMessage("| Transitions : %.2fM\n",(double)p->nextstate_memory/(1024*1024) );
}
LogMessage("+-------------------------------------------------\n");
}
void bnfaPrintInfo( bnfa_struct_t * p )
{
bnfaPrintInfoEx( p, 0 );
}
void bnfaPrintSummary( void )
{
bnfaPrintInfoEx( &summary, " Summary" );
}
void bnfaInitSummary( void )
{
summary_cnt=0;
memset(&summary,0,sizeof(bnfa_struct_t));
}
void bnfaAccumInfo( bnfa_struct_t * p )
{
bnfa_struct_t * px = &summary;
summary_cnt++;
px->bnfaAlphabetSize = p->bnfaAlphabetSize;
px->bnfaPatternCnt += p->bnfaPatternCnt;
px->bnfaMaxStates += p->bnfaMaxStates;
px->bnfaNumStates += p->bnfaNumStates;
px->bnfaNumTrans += p->bnfaNumTrans;
px->bnfaMatchStates += p->bnfaMatchStates;
px->bnfa_memory += p->bnfa_memory;
px->pat_memory += p->pat_memory;
px->list_memory += p->list_memory;
px->matchlist_memory += p->matchlist_memory;
px->nextstate_memory += p->nextstate_memory;
px->failstate_memory += p->failstate_memory;
}
#ifdef MATCH_LIST_CNT
void bnfaPrintMatchListCnt( bnfa_struct_t * p )
{
unsigned * cnt = p->bnfaMatchListCnt;
int i;
bnfa_match_node_t * mn;
bnfa_pattern_t * patrn;
printf("[ MatchListCnt for ac-bnfa state machine\n ]");
for(i=0;i<bnfa->bnfaNumStates;i++)
{
if( cnt[i] )
{
printf("state[%d] cnt=%d",i,cnt[i]);
mn = bnfa->MatchList[i] ;
if( mn )
{
patrn =(bnfa_pattern_t *)mn->data;
//xprintOTNSidGid(cnt,patrn->userdata);
}
printf("\n");
fflush(stdout);
}
}
}
#endif
|