Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

executable file 3547 lines (3067 sloc) 135.535 kb
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 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547
/*
*
* The Sleuth Kit
*
* Contact: Brian Carrier [carrier <at> sleuthkit [dot] org]
* Copyright (c) 2010-2012 Basis Technology Corporation. All Rights
* reserved.
*
* This software is distributed under the Common Public License 1.0
*/

/**
* \file TskImgDBSqlite.cpp
* A SQLite based implementation of the framework data access layer.
*/

#include <stdio.h>
#include <cassert>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <map>

#include "TskImgDBSqlite.h"
#include "TskServices.h"
#include "Poco/UnicodeConverter.h"
#include "Utilities/TskException.h"
#include "TskDBBlackboard.h"

#define IMGDB_CHUNK_SIZE 1024*1024*1 // what size chunks should the database use when growing and shrinking
#define IMGDB_MAX_RETRY_COUNT 50 // how many times will we retry a SQL statement
#define IMGDB_RETRY_WAIT 100 // how long (in milliseconds) are we willing to wait between retries

/**
* Set the database location. Must call
* initialize() before the object can be used.
* @param a_outpath Directory to store the database in. This
* directory must already exist.
*/
TskImgDBSqlite::TskImgDBSqlite(const wchar_t * a_outpath)
{
    wcsncpy_s(m_outPath, a_outpath, 256);
    // ensure that the path ends with a '\'
    if (m_outPath[wcslen(m_outPath)-1] != '\\') {
        int len1 = wcslen(m_outPath);
        m_outPath[len1] = '\\';
        m_outPath[len1+1] = '\0';
    }
    wcsncpy_s(m_dbFilePath, m_outPath, 256);
    wcsncat_s(m_dbFilePath, L"image.db", 256);
    m_db = NULL;
}

TskImgDBSqlite::~TskImgDBSqlite()
{
    (void) close();
}

int TskImgDBSqlite::close()
{
    if (m_db) {
        if (sqlite3_close(m_db) == SQLITE_OK)
            m_db = NULL;
        else
            return 1;
    }
    return 0;
}

int TskImgDBSqlite::dropTables()
{
    if (!m_db)
        return 1;

    char * errmsg;
    // Drop all the tables. No error checking just Teutonic Destruction...
    sqlite3_exec(m_db, "DROP TABLE db_info",NULL, NULL, &errmsg);
    sqlite3_exec(m_db, "DROP TABLE image_info",NULL, NULL, &errmsg);
    sqlite3_exec(m_db, "DROP TABLE image_names",NULL, NULL, &errmsg);
    sqlite3_exec(m_db, "DROP TABLE vol_info",NULL, NULL, &errmsg);
    sqlite3_exec(m_db, "DROP TABLE fs_info",NULL, NULL, &errmsg);
    sqlite3_exec(m_db, "DROP TABLE fs_files",NULL, NULL, &errmsg);
    sqlite3_exec(m_db, "DROP TABLE fs_blocks",NULL, NULL, &errmsg);
    sqlite3_exec(m_db, "DROP TABLE files",NULL, NULL, &errmsg);
    sqlite3_exec(m_db, "DROP TABLE derived_files",NULL, NULL, &errmsg);
    sqlite3_exec(m_db, "DROP TABLE carved_files",NULL, NULL, &errmsg);
    sqlite3_exec(m_db, "DROP TABLE carved_sectors",NULL, NULL, &errmsg);
    sqlite3_exec(m_db, "DROP TABLE alloc_unalloc_map", NULL, NULL, &errmsg);
    sqlite3_exec(m_db, "DROP TABLE blackboard_artifacts", NULL, NULL, &errmsg);
    sqlite3_exec(m_db, "DROP TABLE blackboard_attributes", NULL, NULL, &errmsg);
    sqlite3_exec(m_db, "DROP TABLE blackboard_artifact_types", NULL, NULL, &errmsg);
    sqlite3_exec(m_db, "DROP TABLE blackboard_attribute_types", NULL, NULL, &errmsg);
    sqlite3_exec(m_db, "DROP TABLE file_hashes", NULL, NULL, &errmsg);
    sqlite3_exec(m_db, "DROP TABLE modules", NULL, NULL, &errmsg);
    sqlite3_exec(m_db, "DROP TABLE module_status", NULL, NULL, &errmsg);
    sqlite3_exec(m_db, "DROP TABLE unalloc_img_status", NULL, NULL, &errmsg);
    sqlite3_exec(m_db, "DROP TABLE unused_sectors", NULL, NULL, &errmsg);
    sqlite3_exec(m_db, "DROP INDEX attrs_artifact_id", NULL, NULL, &errmsg);
    sqlite3_exec(m_db, "DROP INDEX attrs_attribute_type", NULL, NULL, &errmsg);
    sqlite3_exec(m_db, "DROP INDEX attrs_obj_id", NULL, NULL, &errmsg);

    return 0;
}

int TskImgDBSqlite::initialize()
{
    wchar_t infoMessage[MAX_BUFF_LENGTH];
    char * errmsg;

    // Open the database.
    if (open() != 0)
    {
        // Error message will have been logged by open()
        return 1;
    }

    // Clean up the whole database.
    dropTables();

    char * stmt;

    // ----- DB_INFO
    stmt = "CREATE TABLE db_info (name TEXT PRIMARY KEY, version TEXT)";
    if (sqlite3_exec(m_db, stmt, NULL, NULL, &errmsg) != SQLITE_OK) {
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::initialize - Error creating db_info table: %S", errmsg);
        LOGERROR(infoMessage);

        sqlite3_free(errmsg);
        return 1;
    }

    // ----- IMAGE_INFO
    stmt = "CREATE TABLE image_info (type INTEGER, ssize INTEGER)";
    if (sqlite3_exec(m_db, stmt, NULL, NULL, &errmsg) != SQLITE_OK) {
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::initialize - Error creating image_info table: %S", errmsg);
        LOGERROR(infoMessage);

        sqlite3_free(errmsg);
        return 1;
    }

    // ----- IMAGE_NAMES
    stmt = "CREATE TABLE image_names (seq INTEGER PRIMARY KEY, name TEXT)";
    if (sqlite3_exec(m_db, stmt, NULL, NULL, &errmsg) != SQLITE_OK) {
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::initialize - Error creating image_names table: %S", errmsg);
        LOGERROR(infoMessage);

        sqlite3_free(errmsg);
        return 1;
    }

    // ----- VOL_INFO
    stmt = "CREATE TABLE vol_info (vol_id INTEGER PRIMARY KEY, sect_start INTEGER NOT NULL, "
        "sect_len INTEGER NOT NULL, description TEXT, flags INTEGER)";
    if (sqlite3_exec(m_db, stmt, NULL, NULL, &errmsg) != SQLITE_OK) {
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::initialize - Error creating vol_info table: %S", errmsg);
        LOGERROR(infoMessage);

        sqlite3_free(errmsg);
        return 1;
    }

    // ----- FS_INFO
    stmt = "CREATE TABLE fs_info (fs_id INTEGER PRIMARY KEY, img_byte_offset INTEGER, "
        "vol_id INTEGER NOT NULL, fs_type INTEGER, block_size INTEGER, "
        "block_count INTEGER, root_inum INTEGER, first_inum INTEGER, last_inum INTEGER)";
    if (sqlite3_exec(m_db, stmt , NULL, NULL, &errmsg) != SQLITE_OK) {
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::initialize - Error creating fs_info table: %S", errmsg);
        LOGERROR(infoMessage);

        sqlite3_free(errmsg);
        return 1;
    }

    // ----- FILES
    stmt = "CREATE TABLE files (file_id INTEGER PRIMARY KEY, type_id INTEGER, "
        "name TEXT, par_file_id INTEGER, dir_type INTEGER, meta_type INTEGER, "
        "dir_flags INTEGER, meta_flags INTEGER, size INTEGER, ctime INTEGER, "
        "crtime INTEGER, atime INTEGER, mtime INTEGER, mode INTEGER, uid INTEGER, "
        "gid INTEGER, status INTEGER, full_path TEXT)";
    if (sqlite3_exec(m_db, stmt, NULL, NULL, &errmsg) != SQLITE_OK) {
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::initialize - Error creating files table: %S", errmsg);
        LOGERROR(infoMessage);

        sqlite3_free(errmsg);
        return 1;
    }

    // ----- FS_FILES
    stmt = "CREATE TABLE fs_files (file_id INTEGER NOT NULL, fs_id INTEGER, "
        "fs_file_id INTEGER, attr_type INTEGER, attr_id INTEGER)";
    if (sqlite3_exec(m_db, stmt, NULL, NULL, &errmsg) != SQLITE_OK)
    {
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::initialize - Error creating fs_files table: %S", errmsg);
        LOGERROR(infoMessage);

        sqlite3_free(errmsg);
        return 1;
    }

    // ----- FS_BLOCKS
    stmt = "CREATE TABLE fs_blocks (fs_id INTEGER NOT NULL, file_id INTEGER NOT NULL, seq INTEGER, "
        "blk_start INTEGER NOT NULL, blk_len INTEGER NOT NULL)";
    if (sqlite3_exec(m_db, stmt, NULL, NULL, &errmsg) != SQLITE_OK)
    {
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::initialize - Error creating fs_blocks table: %S", errmsg);
        LOGERROR(infoMessage);

        sqlite3_free(errmsg);
        return 1;
    }

    // ----- CARVED_FILES
    stmt = "CREATE TABLE carved_files (file_id INTEGER, vol_id INTEGER)";
    if (sqlite3_exec(m_db, stmt, NULL, NULL, &errmsg) != SQLITE_OK)
    {
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::initialize - Error creating carved_files table: %S", errmsg);
        LOGERROR(infoMessage);

        sqlite3_free(errmsg);
        return 1;
    }

    // ----- SECTOR_LIST
    stmt = "CREATE TABLE carved_sectors ("
        "file_id INTEGER, seq INTEGER, sect_start INTEGER, sect_len INTEGER)";
    if (sqlite3_exec(m_db, stmt, NULL, NULL, &errmsg) != SQLITE_OK)
    {
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::initialize - Error creating carved_sectors table: %S", errmsg);
        LOGERROR(infoMessage);

        sqlite3_free(errmsg);
        return 1;
    }

    // ----- DERIVED_FILES
    stmt = "CREATE TABLE derived_files (file_id INTEGER PRIMARY KEY, derivation_details TEXT)";
    if (sqlite3_exec(m_db, stmt, NULL, NULL, &errmsg) != SQLITE_OK)
    {
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::initialize - Error creating derived_files table: %S", errmsg);
        LOGERROR(infoMessage);

        sqlite3_free(errmsg);
        return 1;
    }

    // ----- ALLOC_UNALLOC_MAP
    stmt = "CREATE TABLE alloc_unalloc_map (vol_id, unalloc_img_id INTEGER, "
        "unalloc_img_sect_start INTEGER, sect_len INTEGER, orig_img_sect_start INTEGER)";
    if (sqlite3_exec(m_db, stmt, NULL, NULL, &errmsg) != SQLITE_OK)
    {
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::initialize - Error creating alloc_unalloc_map table: %S", errmsg);
        LOGERROR(infoMessage);

        sqlite3_free(errmsg);
        return 1;
    }

    // ----- FILE_HASHES
    stmt = "CREATE TABLE file_hashes (file_id INTEGER PRIMARY KEY, md5 TEXT, sha1 TEXT, sha2_256 TEXT, sha2_512 TEXT, known INTEGER)";
    if (sqlite3_exec(m_db, stmt, NULL, NULL, &errmsg) != SQLITE_OK) {
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::initialize - Error creating file_hashes table: %S", errmsg);
        LOGERROR(infoMessage);

        sqlite3_free(errmsg);
        return 1;
    }

    // ----- MODULES
    stmt = "CREATE TABLE modules (module_id INTEGER PRIMARY KEY, name TEXT UNIQUE NOT NULL, description TEXT)";
    if (sqlite3_exec(m_db, stmt, NULL, NULL, &errmsg) != SQLITE_OK) {
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::initialize - Error creating module table: %S", errmsg);
        LOGERROR(infoMessage);

        sqlite3_free(errmsg);
        return 1;
    }

    // ----- MODULE_STATUS
    stmt = "CREATE TABLE module_status (file_id INTEGER, module_id INTEGER, status INTEGER, PRIMARY KEY (file_id, module_id))";
    if (sqlite3_exec(m_db, stmt, NULL, NULL, &errmsg) != SQLITE_OK) {
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::initialize - Error creating module_status table: %S", errmsg);
        LOGERROR(infoMessage);

        sqlite3_free(errmsg);
        return 1;
    }

    // ----- UNALLOC_IMG_STATUS
    stmt = "CREATE TABLE unalloc_img_status (unalloc_img_id INTEGER PRIMARY KEY, status INTEGER)";
    if (sqlite3_exec(m_db, stmt, NULL, NULL, &errmsg) != SQLITE_OK) {
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::initialize - Error creating unalloc_img_status table: %S", errmsg);
        LOGERROR(infoMessage);

        sqlite3_free(errmsg);
        return 1;
    }

    // ----- UNUSED_SECTORS
    stmt = "CREATE TABLE unused_sectors (file_id INTEGER PRIMARY KEY, sect_start INTEGER, sect_len INTEGER, vol_id INTEGER)";
    if (sqlite3_exec(m_db, stmt, NULL, NULL, &errmsg) != SQLITE_OK) {
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::initialize - Error creating unused_sectors table: %S", errmsg);
        LOGERROR(infoMessage);

        sqlite3_free(errmsg);
        return 1;
    }

    // ----- BLACKBOARD_ARTIFACTS
    stmt = "CREATE TABLE blackboard_artifacts (artifact_id INTEGER PRIMARY KEY, obj_id INTEGER NOT NULL, artifact_type_id INTEGER)";
    if (sqlite3_exec(m_db, stmt, NULL, NULL, &errmsg) != SQLITE_OK) {
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::initialize - Error creating blackboard_artifacts table: %S", errmsg);
        LOGERROR(infoMessage);

        sqlite3_free(errmsg);
        return 1;
    }

    // ----- BLACKBOARD_ATTRIBUTES
    stmt = "CREATE TABLE blackboard_attributes (artifact_id INTEGER NOT NULL, source TEXT, context TEXT, attribute_type_id INTEGER NOT NULL, value_type INTEGER NOT NULL, "
        "value_byte BLOB, value_text TEXT, value_int32 INTEGER, value_int64 INTEGER, value_double NUMERIC(20, 10), obj_id INTEGER NOT NULL)";
    if (sqlite3_exec(m_db, stmt, NULL, NULL, &errmsg) != SQLITE_OK) {
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::initialize - Error creating blackboard_attributes table: %S", errmsg);
        LOGERROR(infoMessage);

        sqlite3_free(errmsg);
        return 1;
    }

    // ----- BLACKBOARD_ARTIFACT_TYPES
    stmt = "CREATE TABLE blackboard_artifact_types (artifact_type_id INTEGER PRIMARY KEY, type_name TEXT, display_name TEXT)";
    if (sqlite3_exec(m_db, stmt, NULL, NULL, &errmsg) != SQLITE_OK) {
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::initialize - Error creating blackboard_artifact_types table: %S", errmsg);
        LOGERROR(infoMessage);

        sqlite3_free(errmsg);
        return 1;
    }

    // ----- BLACKBOARD_ATTRIBUTE_TYPES
    stmt = "CREATE TABLE blackboard_attribute_types (attribute_type_id INTEGER PRIMARY KEY, type_name TEXT, display_name TEXT)";
    if (sqlite3_exec(m_db, stmt, NULL, NULL, &errmsg) != SQLITE_OK) {
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::initialize - Error creating blackboard_attribute_types table: %S", errmsg);
        LOGERROR(infoMessage);

        sqlite3_free(errmsg);
        return 1;
    }

    // ----- CREATE INDEXES
    stmt = "CREATE INDEX attrs_artifact_id ON blackboard_attributes(artifact_id)";

    if (sqlite3_exec(m_db, stmt, NULL, NULL, &errmsg) != SQLITE_OK) {
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::initialize - Error creating attrs_artifact_id index: %S", errmsg);
        LOGERROR(infoMessage);

        sqlite3_free(errmsg);
        return 1;
    }

    stmt = "CREATE INDEX attrs_attribute_type ON blackboard_attributes(attribute_type_id)";

    if (sqlite3_exec(m_db, stmt, NULL, NULL, &errmsg) != SQLITE_OK) {
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::initialize - Error creating attrs_attribute_type index: %S", errmsg);
        LOGERROR(infoMessage);

        sqlite3_free(errmsg);
        return 1;
    }

    stmt = "CREATE INDEX attrs_obj_id ON blackboard_attributes(obj_id)";

    if (sqlite3_exec(m_db, stmt, NULL, NULL, &errmsg) != SQLITE_OK) {
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::initialize - Error creating attrs_obj_id index: %S", errmsg);
        LOGERROR(infoMessage);

        sqlite3_free(errmsg);
        return 1;
    }

    map<int, TskArtifactNames> artTypes = TskImgDB::getAllArtifactTypes();
    for(map<int, TskArtifactNames>::iterator it = artTypes.begin(); it != artTypes.end(); it++){
        addArtifactType(it->first, it->second.typeName, it->second.displayName);
    }
    map<int, TskAttributeNames> attrTypes = TskImgDB::getAllAttributeTypes();
    for(map<int, TskAttributeNames>::iterator it = attrTypes.begin(); it != attrTypes.end(); it++){
        addAttributeType(it->first, it->second.typeName, it->second.displayName);
    }

    addToolInfo("DBSchema", IMGDB_SCHEMA_VERSION);
    LOGINFO(L"ImgDB Created.");

    return 0;
}

/*
* If the database file exists this method will open it otherwise
* it will create a new database.
* This method also configures the chunk size and the busy handler
* for the newly opened database.
*/
int TskImgDBSqlite::open()
{
    wchar_t infoMessage[MAX_BUFF_LENGTH];

    if (sqlite3_open16(m_dbFilePath, &m_db))
    {
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::open - Can't create new database: %S", sqlite3_errmsg(m_db));
        LOGERROR(infoMessage);

        sqlite3_close(m_db);
        return 1;
    }

    // The chunk size setting defines by how much the database will grow
    // or shrink. The primary motivation behind this setting is to reduce
    // database file fragmentation and potential performance improvements.
    // We, however, are using this setting as a workaround for database
    // corruption issues we have been experiencing when the database is
    // updated by multiple concurrent processes.
    // Database corruption was occuring when SQLite determined that the
    // number of database pages in the database was greater than a value
    // that it had previously cached.
    // This workaround is a crude mechanism to get around that situation.
    int chunkSize = IMGDB_CHUNK_SIZE;

    if (sqlite3_file_control(m_db, NULL, SQLITE_FCNTL_CHUNK_SIZE, &chunkSize) != SQLITE_OK)
    {
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::open - Failed to set chunk size: %S", sqlite3_errmsg(m_db));
        LOGERROR(infoMessage);

        sqlite3_close(m_db);
        return 1;
    }

    // Register a busy handler that will retry statements in situations
    // where the database is locked by another process.
    if (sqlite3_busy_handler(m_db, TskImgDBSqlite::busyHandler, m_db) != SQLITE_OK)
    {
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::open - Failed to set busy handler: %S", sqlite3_errmsg(m_db));
        LOGERROR(infoMessage);

        sqlite3_close(m_db);
        return 1;
    }

    LOGINFO(L"ImgDB Opened.");

    return 0;
}

int TskImgDBSqlite::addToolInfo(const char* name, const char* version)
{
    char *errmsg;
    char stmt[1024];

    if (!m_db)
        return 1;

    sqlite3_snprintf(1024, stmt,
        "INSERT INTO db_info (name, version) VALUES ('%q', '%q');",
        name, version);
    if (sqlite3_exec(m_db, stmt, NULL, NULL, &errmsg) != SQLITE_OK)
    {
        wchar_t infoMessage[MAX_BUFF_LENGTH];
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::addToolInfo - Error adding data to db_info table: %S", errmsg);
        LOGERROR(infoMessage);

        sqlite3_free(errmsg);
        return 1;
    }

    return 0;
}

int TskImgDBSqlite::addImageInfo(int type, int size)
{
    char *errmsg;
    char stmt[1024];

    if (!m_db)
        return 1;

    _snprintf_s(stmt, 1024, _TRUNCATE,
        "INSERT INTO image_info (type, ssize) VALUES (%d, %u);",
        type, size);
    if (sqlite3_exec(m_db, stmt, NULL, NULL, &errmsg) != SQLITE_OK)
    {
        wchar_t infoMessage[MAX_BUFF_LENGTH];
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::addImageInfo - Error adding data to image_info table: %S", errmsg);
        LOGERROR(infoMessage);

        sqlite3_free(errmsg);
        return 1;
    }

    return 0;
}

int TskImgDBSqlite::addImageName(char const * imgName)
{
    char *errmsg;
    char stmt[1024];

    if (!m_db)
        return 1;

    sqlite3_snprintf(1024, stmt,
        "INSERT INTO image_names (seq, name) VALUES (NULL, '%q')",
        imgName);
    if (sqlite3_exec(m_db, stmt, NULL, NULL, &errmsg) != SQLITE_OK)
    {
        wchar_t infoMessage[MAX_BUFF_LENGTH];
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::addImageName - "
            L"Error adding data to image_names table: %S", errmsg);
        LOGERROR(infoMessage);

        sqlite3_free(errmsg);
        return 1;
    }
    return 0;
}

/*
* Adds the sector addresses of the volumes into the db.
*/
int TskImgDBSqlite::addVolumeInfo(const TSK_VS_PART_INFO * vs_part)
{
    char stmt[1024];
    char * errmsg;

    if (!m_db)
        return 1;

    sqlite3_snprintf(1024, stmt,
        "INSERT INTO vol_info (vol_id, sect_start, sect_len, description, flags) VALUES (%d,%"
        PRIuOFF ",%" PRIuOFF ",'%q',%d)", (int)vs_part->addr,
        vs_part->start, vs_part->len, vs_part->desc, vs_part->flags);

    if (sqlite3_exec(m_db, stmt, NULL, NULL, &errmsg) != SQLITE_OK) {
        wchar_t infoMessage[MAX_BUFF_LENGTH];
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::addVolumeInfo - "
            L"Error adding data to vol_info table: %S", errmsg);
        LOGERROR(infoMessage);

        sqlite3_free(errmsg);
        return 1;
    }

    return 0;
}

int TskImgDBSqlite::addFsInfo(int volId, int fsId, const TSK_FS_INFO * fs_info)
{
    char stmt[1024];
    char * errmsg;

    if (!m_db)
        return 1;

    _snprintf_s(stmt, 1024, _TRUNCATE,
        "INSERT INTO fs_info (fs_id, img_byte_offset, vol_id, fs_type, block_size, "
        "block_count, root_inum, first_inum, last_inum) VALUES (%d,%"
        PRIuOFF ",%d,'%d',%d,%" PRIuDADDR ",%" PRIuINUM ",%" PRIuINUM ",%"
        PRIuINUM ")", fsId, fs_info->offset, volId,
        (int)fs_info->ftype, fs_info->block_size, fs_info->block_count,
        fs_info->root_inum, fs_info->first_inum, fs_info->last_inum);

    if (sqlite3_exec(m_db, stmt, NULL, NULL, &errmsg) != SQLITE_OK) {
        wchar_t infoMessage[MAX_BUFF_LENGTH];
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::addFsInfo - "
            L"Error adding data to fs_info table: %S", errmsg);
        LOGERROR(infoMessage);

        sqlite3_free(errmsg);
        return 1;
    }

    return 0;
}


/**
* Given a file system and fs_file_id, return the file_id.
*/
uint64_t TskImgDBSqlite::getFileId(int a_fsId, uint64_t a_fsFileId) const
{
    if (!m_db)
        return 0;

    sqlite3_stmt * statement;
    char stmt[1024];
    uint64_t fileId = 0;
    _snprintf_s(stmt, 1024, _TRUNCATE, "SELECT file_id FROM fs_files WHERE fs_id=%d and fs_file_id=%llu;", a_fsId, a_fsFileId);

    /********** FIND the unallocated volumes *************/
    if (sqlite3_prepare_v2(m_db, stmt, -1, &statement, 0) == SQLITE_OK) {
        int result = sqlite3_step(statement);
        if (result == SQLITE_ROW) {
            fileId = (uint64_t)sqlite3_column_int64(statement, 0);
        }
        sqlite3_finalize(statement);
    }
    else {
        wchar_t infoMessage[MAX_BUFF_LENGTH];
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::getFileId - Error querying fs_files table: %S", sqlite3_errmsg(m_db));
        LOGERROR(infoMessage);

        return 0;
    }
    return fileId;
}


int TskImgDBSqlite::getFileRecord(const uint64_t fileId, TskFileRecord& fileRecord) const
{
    if (!m_db)
        return -1;

    int ret = 0;

    sqlite3_stmt * statement;
    std::stringstream stmt;

    stmt << "SELECT f.file_id, f.type_id, f.name, f.par_file_id, f.dir_type, f.meta_type, f.dir_flags, "
        << "f.meta_flags, f.size, f.ctime, f.crtime, f.atime, f.mtime, f.mode, f.uid, f.gid, f.status, f.full_path, "
        << "fh.md5, fh.sha1, fh.sha2_256, fh.sha2_512 "
        << "FROM files f LEFT OUTER JOIN file_hashes fh ON f.file_id = fh.file_id WHERE f.file_id=" << fileId;

    if (sqlite3_prepare_v2(m_db, stmt.str().c_str(), -1, &statement, 0) == SQLITE_OK)
    {
        int result = sqlite3_step(statement);

        if (result == SQLITE_ROW)
        {
            fileRecord.fileId = sqlite3_column_int64(statement, 0);
            fileRecord.typeId = (TskImgDB::FILE_TYPES)sqlite3_column_int(statement, 1);
            fileRecord.name = (char *)sqlite3_column_text(statement, 2);
            fileRecord.parentFileId = sqlite3_column_int64(statement, 3);
            fileRecord.dirType = (TSK_FS_NAME_TYPE_ENUM) sqlite3_column_int(statement, 4);
            fileRecord.metaType = (TSK_FS_META_TYPE_ENUM) sqlite3_column_int(statement, 5);
            fileRecord.dirFlags = (TSK_FS_NAME_FLAG_ENUM) sqlite3_column_int(statement, 6);
            fileRecord.metaFlags = (TSK_FS_META_FLAG_ENUM) sqlite3_column_int(statement, 7);
            fileRecord.size = sqlite3_column_int64(statement, 8);
            fileRecord.ctime = sqlite3_column_int(statement, 9);
            fileRecord.crtime = sqlite3_column_int(statement, 10);
            fileRecord.atime = sqlite3_column_int(statement, 11);
            fileRecord.mtime = sqlite3_column_int(statement, 12);
            fileRecord.mode = (TSK_FS_META_MODE_ENUM)sqlite3_column_int(statement, 13);
            fileRecord.uid = sqlite3_column_int(statement, 14);
            fileRecord.gid = sqlite3_column_int(statement, 15);
            fileRecord.status = (TskImgDB::FILE_STATUS) sqlite3_column_int(statement, 16);
            fileRecord.fullPath = (char *)sqlite3_column_text(statement, 17);

            if (sqlite3_column_type(statement, 18) == SQLITE_TEXT)
                fileRecord.md5 = (char *)sqlite3_column_text(statement, 18);
            if (sqlite3_column_type(statement, 19) == SQLITE_TEXT)
                fileRecord.sha1 = (char *)sqlite3_column_text(statement, 19);
            if (sqlite3_column_type(statement, 20) == SQLITE_TEXT)
                fileRecord.sha2_256 = (char *)sqlite3_column_text(statement, 20);
            if (sqlite3_column_type(statement, 21) == SQLITE_TEXT)
                fileRecord.sha2_512 = (char *)sqlite3_column_text(statement, 21);
        }
        else
        {
            std::wstringstream msg;
            msg << L"TskImgDBSqlite::getFileRecord - Error querying files table for file id: " << fileId;
            LOGERROR(msg.str());

            ret = -1;
        }
        sqlite3_finalize(statement);
    }
    else
    {
        std::wstringstream msg;
        msg << L"TskImgDBSqlite::getFileRecord - Error querying files table for file id: " << fileId;
        LOGERROR(msg.str());

        ret = -1;
    }

    return ret;
}

/**
* Assign fileId on success.
* @returns 0 on success or -1 on error.
*/
int TskImgDBSqlite::addFsFileInfo(int fsId, TSK_FS_FILE const * fs_file, char const * name, int type, int idx, uint64_t & fileId, char const * path)
{
    char stmt[4096];
    char * errmsg;
    wchar_t infoMessage[MAX_BUFF_LENGTH];

    if (!m_db)
        return -1;

    fileId = 0;
    int mtime = 0;
    int crtime = 0;
    int ctime = 0;
    int atime = 0;
    TSK_OFF_T size = 0;
    int meta_type = 0;
    int meta_flags = 0;
    int meta_mode = 0;
    int gid = 0;
    int uid = 0;

    //char * fullpath = (char *) malloc(sizeof(char *));
    std::string fullpath;

    fullpath.append(path);
    fullpath.append(name);

    /*strncpy(fullpath, path, strlen(path));
strncat(fullpath, name, strlen(name));
fullpath[strlen(path)+strlen(name)] = '\0';*/

    if (fs_file->meta) {
        mtime = (int)fs_file->meta->mtime;
        atime = (int)fs_file->meta->atime;
        ctime = (int)fs_file->meta->ctime;
        crtime = (int)fs_file->meta->crtime;
        size = fs_file->meta->size;
        meta_type = fs_file->meta->type;
        meta_flags = fs_file->meta->flags;
        meta_mode = fs_file->meta->mode;
        gid = fs_file->meta->gid;
        uid = fs_file->meta->uid;
    }

    // Replace all single quotes by double single quotes for 'name' to comply with SQLLite syntax. Also, remove all the control characters that might be present in the file name.
    // Check whether the file name contains a single quote. If so replace it with a double single quote.
    std::string fileNameAsString(name);
    size_t found;

    found = fileNameAsString.find("'");
    if (found != std::string::npos) //Replace it and replace all its subsequent occurrences.
    {
        fileNameAsString.replace(found,1,"''");

        while ((found=fileNameAsString.find("'",found+2)) != std::string::npos)// found+2 because we want to move past the newly inserted single quote.
        {
            fileNameAsString.replace(found,1,"''");
        }
    }

    // Now remove all the control characters.
    for (int codePoint=1; codePoint < 32; codePoint++)
    {
        char codePointAsHex[10];
        codePointAsHex[0] = codePoint;
        codePointAsHex[1] = '\0';
        std::string stringToRemove(codePointAsHex);

        found = fileNameAsString.find(stringToRemove);
        if (found != std::string::npos) //Replace it and replace all its subsequent occurrences.
        {
            fileNameAsString.replace(found,1,"");

            while ((found=fileNameAsString.find(stringToRemove,found+1)) != std::string::npos)// found+1 because the control characters are just 1 character.
            {
                fileNameAsString.replace(found,1,"");
            }
        }
    }

    name = fileNameAsString.c_str();

    // insert into the files table
    // MAY-118 status=READY_FOR_ANALYSIS
    sqlite3_snprintf(4096, stmt,
        "INSERT INTO files (file_id, type_id, status, name, par_file_id, dir_type, meta_type, "
        "dir_flags, meta_flags, size, crtime, ctime, atime, mtime, mode, gid, uid, full_path) VALUES (NULL, %d, %d,"
        "'%q',%llu,%d,%d,%d,%d,%" PRIuOFF",%d,%d,%d,%d,%d,%d,%d,'%q')",
        IMGDB_FILES_TYPE_FS, IMGDB_FILES_STATUS_READY_FOR_ANALYSIS, name,
        getFileId(fsId, fs_file->name->par_addr),
        fs_file->name->type, meta_type,
        fs_file->name->flags, meta_flags, size, crtime, ctime, atime,
        mtime, meta_mode, gid, uid, fullpath.c_str());

    if (sqlite3_exec(m_db, stmt, NULL, NULL, &errmsg) != SQLITE_OK) {
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::addFsFileInfo - Error adding data to files table: %S", errmsg);
        LOGERROR(infoMessage);

        sqlite3_free(errmsg);
        return -1;
    }

    // get the file_id from the last insert
    fileId = sqlite3_last_insert_rowid(m_db);

    // insert into the fs_files table
    _snprintf_s(stmt, 4096, _TRUNCATE,
        "INSERT INTO fs_files (file_id, fs_id, fs_file_id, attr_type, attr_id) VALUES (%llu,%d,%"
        PRIuINUM ",%d,%d)", fileId, fsId, fs_file->name->meta_addr, type, idx);

    if (sqlite3_exec(m_db, stmt, NULL, NULL, &errmsg) != SQLITE_OK) {
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::addFsFileInfo - Error adding data to fs_files table: %S", errmsg);
        LOGERROR(infoMessage);

        sqlite3_free(errmsg);
        return -1;
    }

    return 0;
}

/**
* Add block info to the database. This table stores the run information for each file so that we
* can map which blocks are used by what files.
* @param a_fsId Id that the file is located in
* @param a_fileId ID of the file
* @param a_sequence The sequence number of this run in the file (0 for the first run, 1 for the second run, etc.)
* @param a_blk_addr Block address (the address that the file system uses -- NOT the physical sector addr)
* @param a_len The number of blocks in the run
* @returns 1 on error
*/
int TskImgDBSqlite::addFsBlockInfo(int a_fsId, uint64_t a_fileId, int a_sequence, uint64_t a_blk_addr, uint64_t a_len)
{
    char stmt[1024];
    char * errmsg;

    if (!m_db)
        return 1;

    _snprintf_s(stmt, 1024, _TRUNCATE,
        "INSERT INTO fs_blocks (fs_id, file_id, seq, blk_start, blk_len) VALUES (%d,%llu,%d,%"
        PRIuOFF ",%"PRIuOFF")", a_fsId, a_fileId, a_sequence, a_blk_addr, a_len);

    if (sqlite3_exec(m_db, stmt, NULL, NULL, &errmsg) != SQLITE_OK) {
        wchar_t infoMessage[MAX_BUFF_LENGTH];
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::addFsBlockInfo - Error adding data to fs_blocks table: %S", errmsg);
        LOGERROR(infoMessage);

        sqlite3_free(errmsg);
        return 1;
    }

    return 0;
}

int TskImgDBSqlite::addAllocUnallocMapInfo(int a_volID, int unallocImgID,
                                           uint64_t unallocImgStart, uint64_t length, uint64_t origImgStart)
{
    char stmt[1024];
    char * errmsg;

    if (!m_db)
        return 1;

    _snprintf_s(stmt, 1024, _TRUNCATE,
        "INSERT INTO alloc_unalloc_map (vol_id, unalloc_img_id, unalloc_img_sect_start, "
        "sect_len, orig_img_sect_start) VALUES (%d,%d,%"
        PRIuOFF ",%"PRIuOFF",%"PRIuOFF")", a_volID, unallocImgID,
        unallocImgStart, length, origImgStart);

    if (sqlite3_exec(m_db, stmt, NULL, NULL, &errmsg) != SQLITE_OK) {
        wchar_t infoMessage[MAX_BUFF_LENGTH];
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::addAllocUnallocMapInfo - Error adding data to alloc_unalloc_map table: %S", errmsg);
        LOGERROR(infoMessage);

        sqlite3_free(errmsg);
        return 1;
    }

    return 0;
}

/**
* Get information on all of the free sectors in an image.
*
* @return Info on unallocated runs (or NULL on error). Caller must free this when done.
*/
SectorRuns * TskImgDBSqlite::getFreeSectors() const
{
    wchar_t infoMessage[MAX_BUFF_LENGTH];
    std::wstringstream msg;

    if (!m_db)
        return NULL;

    SectorRuns * sr = new SectorRuns();

    LOGINFO(L"TskImgDBSqlite::getFreeSectors - Identifying Unallocated Sectors");

    sqlite3_stmt * statement;

    /********** FIND the unallocated volumes *************/
    if (sqlite3_prepare_v2(m_db,
        "SELECT vol_id, sect_start, sect_len, flags FROM vol_info;",
        -1, &statement, 0) == SQLITE_OK) {
            while(true) {
                int result = sqlite3_step(statement);
                if (result == SQLITE_ROW) {
                    int flags = sqlite3_column_int(statement, 3);

                    int vol_id = sqlite3_column_int(statement,0);
                    int64_t start = sqlite3_column_int64(statement,1);
                    int64_t len = sqlite3_column_int64(statement,2);

                    // add the unallocated volumes
                    if (flags & TSK_VS_PART_FLAG_UNALLOC) {
                        sr->addRun(start, len, vol_id);
                    }
                    // add the allocated volumes that don't have a known file system
                    else {
                        char stmt[512];
                        sqlite3_stmt *statement2;
                        _snprintf_s(stmt, 512, _TRUNCATE, "SELECT fs_id FROM fs_info WHERE vol_id = %d;", vol_id);
                        if (sqlite3_prepare_v2(m_db, stmt , -1, &statement2, 0) == SQLITE_OK) {
                            if (sqlite3_step(statement2) != SQLITE_ROW) {
                                sr->addRun(start, len, vol_id);
                            }
                            sqlite3_finalize(statement2);
                        }
                    }
                }
                else {
                    break;
                }
            }
            sqlite3_finalize(statement);
    }
    else {
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::getFreeSectors - Error querying vol_info table: %S", sqlite3_errmsg(m_db));
        LOGERROR(infoMessage);

        return NULL;
    }

    /*************** Find the unallocated blocks in each file system *************/
    // @@@ Need to make more dynamic
    int blk_size[32];
    memset(blk_size, 0, sizeof(blk_size));
    uint64_t blk_count[32];
    memset(blk_count, 0, sizeof(blk_count));
    int vol_id[32];
    uint64_t img_offset[32];

    // get basic info on each file system
    if (sqlite3_prepare_v2(m_db, "SELECT fs_id, vol_id, img_byte_offset, block_size, block_count FROM fs_info;", -1, &statement, 0) == SQLITE_OK) {
        LOGINFO(L"TskImgDBSqlite::getFreeSectors - START LOOP: Find the unallocated blocks in each file system.");
        while(true)
        {
            int result = sqlite3_step(statement);
            if (result == SQLITE_ROW)
            {
                int fs_id = sqlite3_column_int(statement, 0);
                if (fs_id > 32)
                {
                    _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::getFreeSectors - fs_id in fs_info is bigger than 32: %d", fs_id);
                    LOGERROR(infoMessage);
                    break;
                }
                vol_id[fs_id] = sqlite3_column_int(statement, 1);
                img_offset[fs_id] = sqlite3_column_int64(statement, 2) / 512;
                blk_size[fs_id] = sqlite3_column_int(statement, 3) / 512;
                blk_count[fs_id] = sqlite3_column_int64(statement, 4);
                // Debug Info
                msg.str(L"");
                msg << L"TskImgDBSqlite::getFreeSectors - fs_id=" << fs_id << " vol_id=" << vol_id[fs_id] << " img_offset=" << img_offset[fs_id] << " blk_size=" << blk_size[fs_id] <<
                    " blk_count=" << blk_count[fs_id];
                LOGINFO(msg.str().c_str());
            }
            else
            {
                break;
            }
        }
        sqlite3_finalize(statement);
        LOGINFO(L"TskImgDBSqlite::getFreeSectors - DONE: Find the unallocated blocks in each file system.");
    }
    else
    {
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::getFreeSectors - Error querying fs_info table: %S", sqlite3_errmsg(m_db));
        LOGERROR(infoMessage);

        return NULL;
    }

    // see what blocks have been used and add them to a list
    TSK_LIST *seen[32];
    memset(seen, 0, 32*sizeof(TSK_LIST *));

    if (sqlite3_prepare_v2(m_db, "SELECT fs_id, file_id, blk_start, blk_len FROM fs_blocks;", -1, &statement, 0) == SQLITE_OK) {
        LOGINFO(L"TskImgDBSqlite::getFreeSectors - START LOOP: see what blocks have been used and add them to a list.");
        while(true) {
            int result = sqlite3_step(statement);
            if (result == SQLITE_ROW) {
                int fs_id = sqlite3_column_int(statement, 0);
                if (fs_id > 32) {
                    _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::getFreeSectors - fs_id in fs_info is bigger than 32: %d", fs_id);
                    LOGERROR(infoMessage);
                    continue;
                }
                uint64_t file_id = (uint64_t)sqlite3_column_int64(statement, 1);
                int64_t addr = sqlite3_column_int64(statement, 2);
                int64_t len = sqlite3_column_int64(statement, 3);

                // We only want to consider the runs for files that we allocated.
                char stmt[1024];
                _snprintf_s(stmt, 1024, _TRUNCATE, "SELECT meta_flags from files WHERE file_id=%d;", file_id);

                sqlite3_stmt * statement2;
                if (sqlite3_prepare_v2(m_db, stmt, -1, &statement2, 0) != SQLITE_OK) {
                    _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::getFreeSectors - error finding flags for file %lld", file_id);
                    LOGERROR(infoMessage);
                    continue;
                }
                sqlite3_step(statement2);
                int flags = sqlite3_column_int(statement2, 0);
                sqlite3_finalize(statement2);

                if (flags & TSK_FS_META_FLAG_UNALLOC)
                    continue;

                // @@@ We can probably find a more effecient storage method than this...
                int error = 0;
                for (int64_t i = 0; i < len; i++) {
                    if (tsk_list_add(&seen[fs_id], addr+i)) {
                        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::getFreeSectors - Error adding seen block address to list");
                        LOGERROR(infoMessage);

                        error = 1;
                        break;
                    }
                }
                if (error)
                    break;
            }
            else {
                break;
            }
        }
        sqlite3_finalize(statement);
        LOGINFO(L"TskImgDBSqlite::getFreeSectors - DONE: see what blocks have been used and add them to a list.");
    }
    else {
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::getFreeSectors - Error querying fs_block table: %S", sqlite3_errmsg(m_db));
        LOGERROR(infoMessage);

        return NULL;
    }

    // cycle through each file system to find the unused blocks
    LOGINFO(L"TskImgDBSqlite::getFreeSectors - START LOOP: cycle through each file system to find the unused blocks.");
    for (int f = 0; f < 32; f++) {
        if (blk_count[f] == 0)
            continue;

        uint64_t st = 0;
        int len = 0;
        // we previously adjusted blk_size and img_offset to be in sectors

        msg.str(L"");
        msg << L"blk_count[" << f << "]=" << blk_count[f];
        LOGINFO(msg.str().c_str());

        for (uint64_t a = 0; a < blk_count[f]; a++) {
            // see if this addr was used in a file
            if (tsk_list_find(seen[f], a) == 0) {
                // we already have a run being defined
                if (len) {
                    // same run, so add on to it
                    if (st + len == a) {
                        len++;
                    }
                    // different run, make a new one
                    else {
                        sr->addRun(img_offset[f]+st*blk_size[f], len*blk_size[f], vol_id[f]);
                        st = a;
                        len = 1;
                    }
                }
                // start a new run
                else {
                    st = a;
                    len = 1;
                }
            }
        }
        // add the final run
        if (len) {
            sr->addRun(img_offset[f]+st*blk_size[f], len*blk_size[f], vol_id[f]);
        }
        tsk_list_free(seen[f]);
        seen[f] = NULL;
    }
    LOGINFO(L"TskImgDBSqlite::getFreeSectors - DONE: cycle through each file system to find the unused blocks.");

    return sr;
}

/**
* Returns the list of image names that were stored in the database.
* @returns empty list on error
*/
std::vector<std::wstring> TskImgDBSqlite::getImageNames() const
{
    std::vector<std::wstring> imgList;

    if (!m_db)
        return imgList;

    sqlite3_stmt *statement;

    if (sqlite3_prepare_v2(m_db, "SELECT name FROM image_names ORDER BY seq;",
        -1, &statement, 0) == SQLITE_OK)
    {
        while(true)
        {
            int result = sqlite3_step(statement);
            if (result == SQLITE_ROW) {
                imgList.push_back((wchar_t *)sqlite3_column_text16(statement, 0));
            }
            else {
                break;
            }
        }

        sqlite3_finalize(statement);
    }

    if (imgList.empty())
    {
        LOGERROR(L"No images found in TskImgDBSqlite");
    }

    return imgList;
}

/**
* @param a_fileId File id to get information about
* @param a_fsOffset Byte offset of start of file system that the file is located in
* @param a_fsFileId File system-specific id of the file
* @param a_attrType Type of attribute for this file
* @param a_attrId The ID of the attribute for this file
* @returns -1 on error
*/
int TskImgDBSqlite::getFileUniqueIdentifiers(uint64_t a_fileId, uint64_t &a_fsOffset, uint64_t &a_fsFileId, int &a_attrType, int &a_attrId) const
{
    if (!m_db)
        return -1;

    sqlite3_stmt * statement;
    char stmt[MAX_BUFF_LENGTH];

    _snprintf_s(stmt, MAX_BUFF_LENGTH, MAX_BUFF_LENGTH,
        "SELECT fs_file_id, attr_type, attr_id, fs_info.img_byte_offset "
        "FROM fs_files, fs_info WHERE file_id=%llu AND fs_info.fs_id = fs_files.fs_id;",
        a_fileId);
    if (sqlite3_prepare_v2(m_db, stmt, -1, &statement, 0) == SQLITE_OK) {
        int result = sqlite3_step(statement);
        if (result == SQLITE_ROW) {
            a_fsFileId = sqlite3_column_int64(statement, 0);
            a_attrType = sqlite3_column_int(statement, 1);
            a_attrId = sqlite3_column_int(statement, 2);
            a_fsOffset = sqlite3_column_int64(statement, 3);
        }
        else {
            return -1;
        }
        sqlite3_finalize(statement);
    }
    else {
        wchar_t infoMessage[MAX_BUFF_LENGTH];
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::getFileUniqueIdentifiers - "
            L"Error querying fs_files table : ", sqlite3_errmsg(m_db));
        LOGERROR(infoMessage);
        return -1;
    }

    return 0;
}

/**
* Get number of volumes in image.
* @return Number of volumes in image or -1 on error
*/
int TskImgDBSqlite::getNumVolumes() const
{
    if (!m_db)
        return 0;

    int count = 0;
    sqlite3_stmt * statement;

    /********** Get the number of volumes *************/
    if (sqlite3_prepare_v2(m_db, "SELECT count(*) from vol_info;", -1, &statement, 0) == SQLITE_OK)
    {
        int result = sqlite3_step(statement);
        if (result == SQLITE_ROW)
        {
            count = (int)sqlite3_column_int(statement, 0);
        }
        sqlite3_finalize(statement);
    }
    else
    {
        std::wstringstream msg;
        msg << L"TskImgDBSqlite::getNumVolumes - Error querying vol_info table: " << sqlite3_errmsg(m_db);
        LOGERROR(msg.str());

        return -1;
    }

    return count;
}
/**
* Get number of files in image.
* @return Number of files in image or -1 on error
*/
int TskImgDBSqlite::getNumFiles() const
{
    if (!m_db)
        return 0;

    std::string condition("");
    return getFileCount(condition);
}

/**
* @returns the session_id or -1 on error.
*/
int TskImgDBSqlite::getSessionID() const
{
    if (!m_db)
        return 0;

    sqlite3_stmt * statement;
    char stmt[1024];
    int sessionId;
    _snprintf_s(stmt, 1024, _TRUNCATE, "SELECT version from db_info WHERE name=%s;", "\"SID\"");

    /********** FIND the unallocated volumes *************/
    if (sqlite3_prepare_v2(m_db, stmt, -1, &statement, 0) == SQLITE_OK) {
        int result = sqlite3_step(statement);
        if (result == SQLITE_ROW) {
            sessionId = (int)sqlite3_column_int(statement, 0);
        }
        sqlite3_finalize(statement);
    }
    else {
        wchar_t infoMessage[MAX_BUFF_LENGTH];
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::getSessionID - Error querying db_info table for Session ID: %S", sqlite3_errmsg(m_db));
        LOGERROR(infoMessage);

        return -1;
    }
    return sessionId;
}

int TskImgDBSqlite::begin()
{
    char *errmsg;
    if (!m_db)
        return 1;

    if (sqlite3_exec(m_db, "BEGIN", NULL, NULL, &errmsg) != SQLITE_OK) {
        wchar_t infoMessage[MAX_BUFF_LENGTH];
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::begin - BEGIN Error: %S", errmsg);
        LOGERROR(infoMessage);

        sqlite3_free(errmsg);
        return 1;
    }
    return 0;
}

int TskImgDBSqlite::commit()
{
    char *errmsg;
    if (!m_db)
        return 1;

    if (sqlite3_exec(m_db, "COMMIT", NULL, NULL, &errmsg) != SQLITE_OK) {
        wchar_t infoMessage[MAX_BUFF_LENGTH];
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::commit - COMMIT Error: %S", errmsg);
        LOGERROR(infoMessage);

        sqlite3_free(errmsg);
        return 1;
    }
    return 0;
}

UnallocRun * TskImgDBSqlite::getUnallocRun(int a_unalloc_img_id, int a_file_offset) const
{
    char stmt[1024];
    char * errmsg;
    if (!m_db)
        return NULL;

    _snprintf_s(stmt, 1024, _TRUNCATE, "SELECT vol_id, unalloc_img_sect_start, sect_len, orig_img_sect_start FROM "
        "alloc_unalloc_map WHERE unalloc_img_id = %d AND unalloc_img_sect_start <= %d ORDER BY unalloc_img_sect_start DESC",
        a_unalloc_img_id, a_file_offset);
    char **result;
    int nrow, ncol;
    if (sqlite3_get_table(m_db, stmt, &result, &nrow, &ncol, &errmsg) != SQLITE_OK)
    {
        wchar_t infoMessage[MAX_BUFF_LENGTH];
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::getUnallocRun - Error fetching data from alloc_unalloc_map table: %S", errmsg);

        LOGERROR(infoMessage);

        sqlite3_free(errmsg);

        return new UnallocRun(-1, -1, -1, -1, -1);
    }
    else
    {
        int vol_id;
        int unalloc_img_sect_start;
        int sect_len;
        int orig_img_sect_start;
        // skip the headers
        // @@@ DO SOME ERROR CHECKING HERE to make sure that results has data...
        sscanf_s(result[4], "%d", &vol_id);
        sscanf_s(result[5], "%d", &unalloc_img_sect_start);
        sscanf_s(result[6], "%d", &sect_len);
        sscanf_s(result[7], "%d", &orig_img_sect_start);
        sqlite3_free_table(result);
        return new UnallocRun(vol_id, a_unalloc_img_id, unalloc_img_sect_start, sect_len, orig_img_sect_start);
    }
}

/**
* Adds information about a carved file into the database. This includes the sector layout
* information.
*
* @param vol_id Volume in which the carved file was found in
* @param name Name of the file
* @param size Number of bytes in file
* @param runStarts Array with starting sector (relative to start of image) for each run in file.
* @param runLengths Array with number of sectors in each run
* @param numRuns Number of entries in previous arrays
* @param fileId Carved file Id (output)
* @returns 0 on success or -1 on error.
*/
int TskImgDBSqlite::addCarvedFileInfo(int vol_id, wchar_t * name, uint64_t size,
                                      uint64_t *runStarts, uint64_t *runLengths, int numRuns, uint64_t & fileId)
{
    char stmt[1024];
    std::string utf8Name;
    char * errmsg;
    wchar_t infoMessage[MAX_BUFF_LENGTH];
    if (!m_db)
        return -1;

    Poco::UnicodeConverter::toUTF8(name, utf8Name); // needed for sqlite3_sprintf
    // insert into files table
    sqlite3_snprintf(1024, stmt,
        "INSERT INTO files (file_id, type_id, name, par_file_id, dir_type, meta_type,"
        "dir_flags, meta_flags, size, ctime, crtime, atime, mtime, mode, uid, gid, status, full_path) "
        "VALUES (NULL, %d, '%q', NULL, %d, %d, %d, %d, %llu, 0, 0, 0, 0, NULL, NULL, NULL, %d, '%q')",
        IMGDB_FILES_TYPE_CARVED, utf8Name.c_str(), (int)TSK_FS_NAME_TYPE_REG, (int)TSK_FS_META_TYPE_REG,
        (int)TSK_FS_NAME_FLAG_UNALLOC, (int)TSK_FS_META_FLAG_UNALLOC, size, IMGDB_FILES_STATUS_CREATED, utf8Name.c_str());
    // MAY-118 NOTE: addCarvedFileInfo insert entry into files table, but actual file on disk has not been created yet.
    if (sqlite3_exec(m_db, stmt, NULL, NULL, &errmsg) != SQLITE_OK) {
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::addCarvedFileInfo - Error adding data to file table for carved file: %S %S", errmsg, stmt);

        LOGERROR(infoMessage);

        sqlite3_free(errmsg);
        return -1;
    }

    // get the assigned file_id
    fileId = (uint64_t)sqlite3_last_insert_rowid(m_db);

    // insert into the carved_files_table
    _snprintf_s(stmt, 1024, _TRUNCATE, "INSERT INTO carved_files (file_id, vol_id)"
        "VALUES (%llu, %d)", fileId, vol_id);
    if (sqlite3_exec(m_db, stmt, NULL, NULL, &errmsg) != SQLITE_OK) {
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::addCarvedFileInfo - Error adding data to carved_files table: %S", errmsg);

        LOGERROR(infoMessage);
        sqlite3_free(errmsg);
        return -1;
    }

    // insert into carved_sectors table
    for (int i = 0; i < numRuns; i++)
    {
        _snprintf_s(stmt, 1024, _TRUNCATE,
            "INSERT INTO carved_sectors (file_id, seq, sect_start, sect_len) "
            "VALUES (%llu, %d, %llu, %llu)",
            fileId, i, runStarts[i], runLengths[i]);
        if (sqlite3_exec(m_db, stmt, NULL, NULL, &errmsg) != SQLITE_OK) {
            _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::addCarvedFileInfo - Error adding data to carved_sectors table: %S", errmsg);

            LOGERROR(infoMessage);

            sqlite3_free(errmsg);
            return -1;
        }
    }

    return 0;
}

/**
* Adds information about derived files to the database. Derived files typically come
* from archives and may be compressed.
*
* @param name The name of the file.
* @param parentId The id of the file from which this file is derived.
* @param isDirectory True if entry is for a directory verus a file
* @param size The size of the file.
* @param details This is a string that may contain extra details related
* to the particular type of mechanism that was used to derive this file,
* e.g. files derived from zip archives may have extra information about the
* compressed size of the file.
* @param ctime Time file system file entry was changed.
* @param crtime Time the file was created.
* @param atime Last access time.
* @param mtime Last modified time.
* @param fileId Reference to location where file_id for file can be assigned
* @param path Path of file
*
* @returns 0 on success or -1 on error.
*/
int TskImgDBSqlite::addDerivedFileInfo(const std::string& name, const uint64_t parentId,
                                       const bool isDirectory, const uint64_t size,
                                       const std::string& details,
                                       const int ctime, const int crtime, const int atime, const int mtime,
                                       uint64_t &fileId, std::string path)
{
    if (!m_db)
        return -1;

    char stmt[1024];
    char * errmsg;

    TSK_FS_NAME_TYPE_ENUM dirType = isDirectory ? TSK_FS_NAME_TYPE_DIR : TSK_FS_NAME_TYPE_REG;

    // insert into files table
    sqlite3_snprintf(1024, stmt,
        "INSERT INTO files (file_id, type_id, name, par_file_id, dir_type, size, ctime, crtime, atime, mtime, status, full_path) "
        "VALUES (NULL, %d, '%q', %llu, %d, %llu, %d, %d, %d, %d, %d, '%q')",
        IMGDB_FILES_TYPE_DERIVED, name.c_str(), parentId, dirType, size, ctime, crtime, atime, mtime, IMGDB_FILES_STATUS_CREATED, path.c_str());

    if (sqlite3_exec(m_db, stmt, NULL, NULL, &errmsg) != SQLITE_OK)
    {
        std::wstringstream msg;
        msg << L"TskImgDBSqlite::addDerivedFileInfo - Error adding data to file table for derived file: "
            << errmsg << L" " << stmt;

        LOGERROR(msg.str());

        sqlite3_free(errmsg);
        return -1;
    }

    // get the assigned file_id
    fileId = sqlite3_last_insert_rowid(m_db);

    // insert into the derived_files table
    sqlite3_snprintf(1024, stmt, "INSERT INTO derived_files (file_id, derivation_details) "
        "VALUES (%llu, '%q')", fileId, details.c_str());
    if (sqlite3_exec(m_db, stmt, NULL, NULL, &errmsg) != SQLITE_OK)
    {
        std::wstringstream msg;
        msg << L"TskImgDBSqlite::addDerivedFileInfo - Error adding data to derived_files table : "
            << errmsg;

        LOGERROR(msg.str());
        sqlite3_free(errmsg);
        return -1;
    }

    return 0;
}

/**
* Fills outBuffer with file IDs that match the name fileName.
* Returns the number of file IDs written into outBuffer or -1 on error.
*/
int TskImgDBSqlite::getFileIds(char *a_fileName, uint64_t *a_outBuffer, int a_buffSize) const
{

    if (!m_db)
        return -1;

    int outIdx = 0;

    sqlite3_stmt * statement;
    char stmt[MAX_BUFF_LENGTH];
    _snprintf_s(stmt, MAX_BUFF_LENGTH, MAX_BUFF_LENGTH,
        "SELECT file_id FROM files WHERE name LIKE '%s';",
        a_fileName);
    if (sqlite3_prepare_v2(m_db, stmt, -1, &statement, 0) == SQLITE_OK) {
        while(sqlite3_step(statement) == SQLITE_ROW) {
            a_outBuffer[outIdx++] = (uint64_t)sqlite3_column_int64(statement, 0);
        }
        sqlite3_finalize(statement);
    }
    else {
        wchar_t infoMessage[MAX_BUFF_LENGTH];
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::getFileIds - "
            L"Error querying files table : %S", sqlite3_errmsg(m_db));
        LOGERROR(infoMessage);
        return -1;
    }

    return outIdx;
}

/*
* Return the minimum file id with status = READY_FOR_ANALYSIS in minFileId.
* Return 0 on success, -1 if failed.
*/
int TskImgDBSqlite::getMinFileIdReadyForAnalysis(uint64_t & minFileId) const
{
    if (!m_db)
        return -1;

    minFileId = 0;

    sqlite3_stmt * statement;
    char stmt[MAX_BUFF_LENGTH];
    _snprintf_s(stmt, MAX_BUFF_LENGTH, MAX_BUFF_LENGTH,
        "SELECT min(file_id) FROM files WHERE status = %d;", TskImgDB::IMGDB_FILES_STATUS_READY_FOR_ANALYSIS);
    if (sqlite3_prepare_v2(m_db, stmt, -1, &statement, 0) == SQLITE_OK) {
        int result = sqlite3_step(statement);
        if (result == SQLITE_ROW) {
            minFileId = (uint64_t)sqlite3_column_int64(statement, 0);
        }
        sqlite3_finalize(statement);
    }
    else {
        wchar_t infoMessage[MAX_BUFF_LENGTH];
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::getMinFileIdReadyForAnalysis - "
            L"Error querying files table : %S", sqlite3_errmsg(m_db));
        LOGERROR(infoMessage);
        return -1;
    }
    return 0;
}

/**
* Given the last file ID ready for analysis, find the largest file ID ready of analysis (in maxFileId)
* Returns 0 on success or -1 on error.
*/
int TskImgDBSqlite::getMaxFileIdReadyForAnalysis(uint64_t a_lastFileId, uint64_t & maxFileId) const
{
    if (!m_db)
        return -1;

    maxFileId = 0;

    sqlite3_stmt * statement;
    char stmt[MAX_BUFF_LENGTH];
    _snprintf_s(stmt, MAX_BUFF_LENGTH, MAX_BUFF_LENGTH,
        "SELECT max(file_id) FROM files WHERE status = %d AND file_id >= %d;", TskImgDB::IMGDB_FILES_STATUS_READY_FOR_ANALYSIS, a_lastFileId);
    if (sqlite3_prepare_v2(m_db, stmt, -1, &statement, 0) == SQLITE_OK) {
        int result = sqlite3_step(statement);
        if (result == SQLITE_ROW) {
            maxFileId = (uint64_t)sqlite3_column_int64(statement, 0);
        }
        sqlite3_finalize(statement);
    }
    else {
        wchar_t infoMessage[MAX_BUFF_LENGTH];
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::getMaxFileIdReadyForAnalysis - "
            L"Error querying files table : %S", sqlite3_errmsg(m_db));
        LOGERROR(infoMessage);
        return -1;
    }
    return 0;
}

SectorRuns * TskImgDBSqlite::getFileSectors(uint64_t a_fileId) const
{
    if (!m_db)
        return NULL;

    SectorRuns * sr = new SectorRuns();

    sqlite3_stmt * statement;
    char stmt[MAX_BUFF_LENGTH];
    int srCount = 0;
    _snprintf_s(stmt, MAX_BUFF_LENGTH, MAX_BUFF_LENGTH,
        "SELECT fs_blocks.blk_start, fs_blocks.blk_len, "
        "fs_info.block_size, fs_info.img_byte_offset, fs_info.vol_id "
        "FROM files "
        "JOIN fs_files ON files.file_id = fs_files.file_id "
        "JOIN fs_blocks ON files.file_id = fs_blocks.file_id "
        "JOIN fs_info ON fs_blocks.fs_id = fs_info.fs_id "
        "WHERE files.file_id = %u "
        "ORDER BY fs_blocks.seq;",
        a_fileId);
    if (sqlite3_prepare_v2(m_db, stmt, -1, &statement, 0) == SQLITE_OK) {
        while(sqlite3_step(statement) == SQLITE_ROW) {
            uint64_t blkStart = (uint64_t)sqlite3_column_int64(statement, 0);
            uint64_t blkLength = (uint64_t)sqlite3_column_int64(statement, 1);
            int blkSize = sqlite3_column_int(statement, 2);
            uint64_t imgByteOffset = (uint64_t)sqlite3_column_int64(statement, 3);
            int volId = sqlite3_column_int(statement, 4);

            uint64_t start = (imgByteOffset + blkStart * blkSize) / 512;
            uint64_t len = (blkLength * blkSize) / 512;

            sr->addRun(start, len, volId);
            srCount++;
        }

        sqlite3_finalize(statement);
    }
    else {
        wchar_t infoMessage[MAX_BUFF_LENGTH];
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH,
            L"TskImgDBSqlite::getFileSectors - "
            L"Error finding block data for file_id=%u: %S",
            a_fileId,
            sqlite3_errmsg(m_db));
        LOGERROR(infoMessage);
        return NULL;
    }

    if (srCount < 1) {
        delete sr;
        sr = NULL;
    }
    return sr;
}

/**
* This callback mechanism is registered with SQLite and is
* called whenever an operation would result in SQLITE_BUSY.
* Each time this method is called we will back off IMGDB_RETRY_WAIT
* x count milliseconds. A non zero return value tells SQLite to
* retry the statement and a zero return value tells SQLite to
* stop retrying, in which case it will return SQLITE_BUSY or
* SQLITE_IOERR_BLOCKED to the caller.
*
* @param pDB - a pointer to the sqlite3 structure
* @param count - the number of times this handler has been
* called for this blocking event.
*/
int TskImgDBSqlite::busyHandler(void * pDB, int count)
{
    if (count < IMGDB_MAX_RETRY_COUNT)
    {
        Sleep(IMGDB_RETRY_WAIT * count);
        return 1;
    }

    return 0;
}



int TskImgDBSqlite::updateFileStatus(uint64_t a_file_id, FILE_STATUS a_status)
{
    if (!m_db)
        return 1;

    char stmt[MAX_BUFF_LENGTH];
    char * errmsg;

    _snprintf_s(stmt, MAX_BUFF_LENGTH, MAX_BUFF_LENGTH,
        "UPDATE files SET status = %d WHERE file_id = %d;", a_status, a_file_id);
    if (sqlite3_exec(m_db, stmt, NULL, NULL, &errmsg) != SQLITE_OK) {
        wchar_t infoMessage[MAX_BUFF_LENGTH];
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::updateFileStatus - Error UPDATE file status: %S",
            sqlite3_errmsg(m_db));
        LOGERROR(infoMessage);
        return 1;
    }

    return 0;
}


int TskImgDBSqlite::updateKnownStatus(uint64_t a_file_id, KNOWN_STATUS a_status)
{
    if (!m_db)
        return 1;

    char stmt[MAX_BUFF_LENGTH];
    char * errmsg;

    _snprintf_s(stmt, MAX_BUFF_LENGTH, MAX_BUFF_LENGTH,
                "UPDATE file_hashes SET known = %d WHERE file_id = %d;", a_status, a_file_id);
    if (sqlite3_exec(m_db, stmt, NULL, NULL, &errmsg) != SQLITE_OK) {
        wchar_t infoMessage[MAX_BUFF_LENGTH];
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::updateFileStatus - Error UPDATE file status: %S",
            sqlite3_errmsg(m_db));
        LOGERROR(infoMessage);
        return 1;
    }

    return 0;
}

bool TskImgDBSqlite::dbExist() const
{
    if (m_db)
        return true;
    else
        return false;
}

void TskImgDBSqlite::getCarvedFileInfo(const std::string& stmt, std::map<uint64_t, std::string>& results) const
{
    sqlite3_stmt * statement;
    if (sqlite3_prepare_v2(m_db, stmt.c_str(), -1, &statement, 0) == SQLITE_OK)
    {
        while (sqlite3_step(statement) == SQLITE_ROW)
        {
            uint64_t fileId = (uint64_t)sqlite3_column_int64(statement, 0);
            std::string fileName = (char*)sqlite3_column_text(statement, 1);
            std::string cfileName = (char*)sqlite3_column_text(statement, 2);

            // Grab the extension and append it to the cfile name
            std::string::size_type pos = fileName.rfind('.');
            if (pos != std::string::npos)
                cfileName.append(fileName.substr(pos));

            results[fileId] = cfileName;
        }
        sqlite3_finalize(statement);
    } else
    {
        std::wstringstream msg;
        msg << L"TskImgDBSqlite::getCarvedFileInfo - Error retrieving carved file details: "
            << sqlite3_errmsg(m_db);
        LOGERROR(msg.str());
    }
}

/**
* Returns the file id and carved file name for a unique set of carved files.
* Uniqueness is based on the value of a particular hash type. Where duplicate
* hash values exist, the lowest file_id is chosen.
* @param hashType The type of hash value to use when determining uniqueness
* @return A map of file ids and the corresponding carved file name.
*/
std::map<uint64_t, std::string> TskImgDBSqlite::getUniqueCarvedFiles(HASH_TYPE hashType) const
{
    if (!m_db)
        throw TskException("No database.");

    std::map<uint64_t, std::string> results;

    string hash;
    switch (hashType) {
    case TskImgDB::MD5:
        hash = "md5";
        break;
    case TskImgDB::SHA1:
        hash = "sha1";
        break;
    case TskImgDB::SHA2_256:
        hash = "sha2_256";
        break;
    case TskImgDB::SHA2_512:
        hash = "sha2_512";
        break;
    default:
        std::wstringstream msg;
        msg << L"TskImgDBSqlite::getUniqueCarvedFiles - Unsupported hashType : " << hashType << std::endl;
        LOGERROR(msg.str());
        return results;
    }

    stringstream stmt;
    
    // If hashes have not been calculated return all carved files
    stmt << "SELECT count(*) FROM file_hashes;";

    sqlite3_stmt * statement;
    if (sqlite3_prepare_v2(m_db, stmt.str().c_str(), -1, &statement, 0) == SQLITE_OK)
    {
        if (sqlite3_step(statement) == SQLITE_ROW)
        {
            uint64_t counter = (uint64_t)sqlite3_column_int64(statement, 0);
            if (counter == 0)
            {
                sqlite3_finalize(statement);
                stmt.str("");
                stmt << "select c.file_id, f.name, 'cfile_' || c.vol_id || '_' || cs.sect_start || '_' "
                    << "|| c.file_id from files f, carved_files c, carved_sectors cs "
                    << "where c.file_id = cs.file_id and cs.seq = 0 and f.file_id = c.file_id order by c.file_id";
                getCarvedFileInfo(stmt.str(), results);
                return results;
            }
        }
        sqlite3_finalize(statement);
    } else
    {
        wchar_t infoMessage[MAX_BUFF_LENGTH];
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::getUniqueCarvedFiles - Error getting file_hashes count: %S", sqlite3_errmsg(m_db));
        LOGERROR(infoMessage);
    }

    stmt.str("");

    // Get the set of files for which the hash has been calculated.
    stmt << "select c.file_id, f.name, 'cfile_' || c.vol_id || '_' || cs.sect_start || '_' "
        << "|| c.file_id from files f, carved_files c, carved_sectors cs "
        << "where c.file_id = cs.file_id and cs.seq = 0 and f.file_id = c.file_id and c.file_id in "
        << "(select min(file_id) from file_hashes where " << hash << " != '' group by " << hash << ") order by c.file_id";

    getCarvedFileInfo(stmt.str(), results);

    // Next get the set of files for which the hash has *not* been calculated.
    stmt.str("");

    stmt << "select c.file_id, f.name, 'cfile_' || c.vol_id || '_' || cs.sect_start || '_' "
        << "|| c.file_id from files f, carved_files c, carved_sectors cs "
        << "where c.file_id = cs.file_id and cs.seq = 0 and f.file_id = c.file_id and c.file_id in "
        << "(select file_id from file_hashes where " << hash << " = '') order by c.file_id";

    getCarvedFileInfo(stmt.str(), results);

    return results;
}

std::vector<uint64_t> TskImgDBSqlite::getCarvedFileIds() const
{
    return getFileIdsWorker("carved_files");
}

std::vector<uint64_t> TskImgDBSqlite::getUniqueFileIds(HASH_TYPE hashType) const
{
    if (!m_db)
        throw TskException("No database.");

    std::vector<uint64_t> results;

    string hash;
    switch (hashType) {
    case TskImgDB::MD5:
        hash = "md5";
        break;
    case TskImgDB::SHA1:
        hash = "sha1";
        break;
    case TskImgDB::SHA2_256:
        hash = "sha2_256";
        break;
    case TskImgDB::SHA2_512:
        hash = "sha2_512";
        break;
    default:
        std::wstringstream errorMsg;
        errorMsg << L"TskImgDBSqlite::getUniqueFileIds - Unsupported hashType : " << hashType ;
        LOGERROR(errorMsg.str());
        return results;
    }

    stringstream stmt;

    stmt << "SELECT min(file_id) FROM file_hashes WHERE " << hash << " != '' group by " << hash ;

    sqlite3_stmt * statement;
    if (sqlite3_prepare_v2(m_db, stmt.str().c_str(), -1, &statement, 0) == SQLITE_OK) {
        while (sqlite3_step(statement) == SQLITE_ROW) {
            uint64_t fileId = (uint64_t)sqlite3_column_int64(statement, 0);
            results.push_back(fileId);
        }
        sqlite3_finalize(statement);
    } else {
        wchar_t infoMessage[MAX_BUFF_LENGTH];
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::getUniqueFileIds - Error querying file_hashes table: %S", sqlite3_errmsg(m_db));
        LOGERROR(infoMessage);
    }
    return results;
}

std::vector<uint64_t> TskImgDBSqlite::getFileIds() const
{
    return getFileIdsWorker("files");
}

std::vector<uint64_t> TskImgDBSqlite::getFileIdsWorker(std::string tableName, const std::string condition) const
{
    if (!m_db)
        throw TskException("No database.");

    std::vector<uint64_t> results;

    stringstream stmt;

    stmt << "SELECT file_id FROM " << tableName;
    if (condition.compare("") != 0)
        stmt << " WHERE " << condition;
    stmt << " ORDER BY file_id";

    sqlite3_stmt * statement;
    if (sqlite3_prepare_v2(m_db, stmt.str().c_str(), -1, &statement, 0) == SQLITE_OK) {
        while (sqlite3_step(statement) == SQLITE_ROW) {
            uint64_t fileId = (uint64_t)sqlite3_column_int64(statement, 0);
            results.push_back(fileId);
        }
        sqlite3_finalize(statement);
    } else {
        wchar_t infoMessage[MAX_BUFF_LENGTH];
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::getFileIdsWorker - Error getting file ids from table %s, %S", tableName.c_str(), sqlite3_errmsg(m_db));
        LOGERROR(infoMessage);
    }
    return results;
}

/**
* Get the list of file ids that match the given criteria.
* The given string will be appended to "select files.file_id from files".
*
* @param condition Must be a valid SQL string defining the selection criteria.
* @returns The collection of file ids matching the selection criteria. Throws
* TskException if database not initialized.
*/
std::vector<uint64_t> TskImgDBSqlite::getFileIds(std::string &condition) const
{
    if (!m_db)
        throw TskException("Database not initialized.");

    std::vector<uint64_t> results;

    std::string stmt("SELECT files.file_id FROM files");

    constructStmt(stmt, condition);

    sqlite3_stmt * statement;
    if (sqlite3_prepare_v2(m_db, stmt.c_str(), -1, &statement, 0) == SQLITE_OK)
    {
        while (sqlite3_step(statement) == SQLITE_ROW)
        {
            uint64_t fileId = (uint64_t)sqlite3_column_int64(statement, 0);
            results.push_back(fileId);
        }
        sqlite3_finalize(statement);
    } else
    {
        std::wstringstream msg;
        msg << L"TskImgDBSqlite::getFilesIds - Error getting file ids: " << sqlite3_errmsg(m_db);
        LOGERROR(msg.str());
    }
    return results;
}
/*
* Get the list of file records that match the given criteria.
* The given string will be appended to "select ... from files".
*
* @param condition Must be a valid SQL string defining the selection criteria.
* @returns The collection of file records matching the selection criteria. Throws
* TskException if database not initialized.
*/
std::vector<const TskFileRecord> TskImgDBSqlite::getFileRecords(std::string &condition) const
{
    if (!m_db)
        throw TskException("Database not initialized.");

    std::vector<const TskFileRecord> results;

    std::stringstream stmtstrm;

    stmtstrm << "SELECT f.file_id, f.type_id, f.name, f.par_file_id, f.dir_type, f.meta_type, f.dir_flags, "
        << "f.meta_flags, f.size, f.ctime, f.crtime, f.atime, f.mtime, f.mode, f.uid, f.gid, f.status, f.full_path, "
        << "fh.md5, fh.sha1, fh.sha2_256, fh.sha2_512 "
        << "FROM files f LEFT OUTER JOIN file_hashes fh ON f.file_id = fh.file_id ";

    std::string stmt = stmtstrm.str();
    constructStmt(stmt, condition);

    sqlite3_stmt * statement;
    if (sqlite3_prepare_v2(m_db, stmt.c_str(), -1, &statement, 0) == SQLITE_OK)
    {
        while(sqlite3_step(statement) == SQLITE_ROW) {
            TskFileRecord fileRecord;
            fileRecord.fileId = sqlite3_column_int64(statement, 0);
            fileRecord.typeId = (TskImgDB::FILE_TYPES) sqlite3_column_int(statement, 1);
            fileRecord.name = (char *)sqlite3_column_text(statement, 2);
            fileRecord.parentFileId = sqlite3_column_int64(statement, 3);
            fileRecord.dirType = (TSK_FS_NAME_TYPE_ENUM) sqlite3_column_int(statement, 4);
            fileRecord.metaType = (TSK_FS_META_TYPE_ENUM) sqlite3_column_int(statement, 5);
            fileRecord.dirFlags = (TSK_FS_NAME_FLAG_ENUM) sqlite3_column_int(statement, 6);
            fileRecord.metaFlags = (TSK_FS_META_FLAG_ENUM) sqlite3_column_int(statement, 7);
            fileRecord.size = sqlite3_column_int64(statement, 8);
            fileRecord.ctime = sqlite3_column_int(statement, 9);
            fileRecord.crtime = sqlite3_column_int(statement, 10);
            fileRecord.atime = sqlite3_column_int(statement, 11);
            fileRecord.mtime = sqlite3_column_int(statement, 12);
            fileRecord.mode = (TSK_FS_META_MODE_ENUM) sqlite3_column_int(statement, 13);
            fileRecord.uid = sqlite3_column_int(statement, 14);
            fileRecord.gid = sqlite3_column_int(statement, 15);
            fileRecord.status = (TskImgDB::FILE_STATUS) sqlite3_column_int(statement, 16);
            fileRecord.fullPath = (char *)sqlite3_column_text(statement, 17);

            if (sqlite3_column_type(statement, 18) == SQLITE_TEXT)
                fileRecord.md5 = (char *)sqlite3_column_text(statement, 18);
            if (sqlite3_column_type(statement, 19) == SQLITE_TEXT)
                fileRecord.sha1 = (char *)sqlite3_column_text(statement, 19);
            if (sqlite3_column_type(statement, 20) == SQLITE_TEXT)
                fileRecord.sha2_256 = (char *)sqlite3_column_text(statement, 20);
            if (sqlite3_column_type(statement, 21) == SQLITE_TEXT)
                fileRecord.sha2_512 = (char *)sqlite3_column_text(statement, 21);
            results.push_back(fileRecord);
        }
    }
    else
    {
        std::wstringstream msg;
        msg << L"TskImgDBSqlite::getFilesRecords - Error getting file reocrds: " << sqlite3_errmsg(m_db);
        LOGERROR(msg.str());
    }
    return results;
}


/**
* Get the number of files that match the given criteria.
* The given string will be appended to "select files.file_id from files".
*
* @param condition Must be a valid SQL string defining the selection criteria.
* @returns The number of files matching the selection criteria.
*/
int TskImgDBSqlite::getFileCount(std::string& condition) const
{
    if (!m_db)
        throw TskException("Database not initialized.");

    int result = 0;

    std::string stmt("SELECT COUNT(files.file_id) FROM files");

    constructStmt(stmt, condition);

    sqlite3_stmt * statement;
    if (sqlite3_prepare_v2(m_db, stmt.c_str(), -1, &statement, 0) == SQLITE_OK)
    {
        while (sqlite3_step(statement) == SQLITE_ROW)
        {
            result = (uint64_t)sqlite3_column_int(statement, 0);
        }
        sqlite3_finalize(statement);
    } else
    {
        std::wstringstream msg;
        msg << L"TskImgDBSqlite::getFileCount - Error getting file count: " << sqlite3_errmsg(m_db);
        LOGERROR(msg.str());
    }
    return result;
}

/* Append condition to stmt to make a single SQL query.
*/
void TskImgDBSqlite::constructStmt(std::string& stmt, std::string& condition) const
{
    if (!condition.empty())
    {
        // Remove leading whitespace from condition
        condition.erase(0, condition.find_first_not_of(' '));

        std::string whereClause("WHERE");
        std::string joinClause("JOIN");
        std::string leftClause("LEFT");
        std::string orderClause("ORDER");

        /* If the condition doesn't start with one of the below statements
* (WHERE, JOIN, etc.), then
* it is presumably extending the FROM clause with
* one or more table names. In this case we need to add the comma to
* the statement. */
        if (strnicmp(condition.c_str(), whereClause.c_str(), whereClause.length()) != 0 &&
            strnicmp(condition.c_str(), joinClause.c_str(), joinClause.length()) != 0 &&
            strnicmp(condition.c_str(), leftClause.c_str(), leftClause.length()) != 0 &&
            strnicmp(condition.c_str(), orderClause.c_str(), orderClause.length()) != 0 &&
            condition[0] != ',')
        {
            stmt.append(",");
        }
    }

    stmt.append(" ");
    stmt.append(condition);
}

// Set file hash for hashType for a_file_id
// Return 1 on failure, 0 on success.
int TskImgDBSqlite::setHash(const uint64_t a_file_id, const TskImgDB::HASH_TYPE hashType, const std::string& hash) const
{
    if (!m_db)
        throw TskException("No database.");

    string hashTypeStr;
    switch (hashType) {
    case TskImgDB::MD5:
        hashTypeStr = "md5";
        break;
    case TskImgDB::SHA1:
        hashTypeStr = "sha1";
        break;
    case TskImgDB::SHA2_256:
        hashTypeStr = "sha2_256";
        break;
    case TskImgDB::SHA2_512:
        hashTypeStr = "sha2_512";
        break;
    default:
        std::wstringstream errorMsg;
        errorMsg << L"TskImgDBSqlite::setHash - Unsupported hashType : " << hashType ;
        LOGERROR(errorMsg.str());
        return 1;
    }

    stringstream stmt;
    bool found = false;
    std::string md5, sha1, sha2_256, sha2_512;
    int known = IMGDB_FILES_UNKNOWN;
    std::stringstream stream;

    stmt << "SELECT md5, sha1, sha2_256, sha2_512, known from file_hashes WHERE file_id = " << a_file_id;

    sqlite3_stmt * statement;
    if (sqlite3_prepare_v2(m_db, stmt.str().c_str(), -1, &statement, 0) == SQLITE_OK) {
        int result = sqlite3_step(statement);
        if (result == SQLITE_ROW) {
            md5 = (char *)sqlite3_column_text(statement, 0);
            sha1 = (char *)sqlite3_column_text(statement, 1);
            sha2_256 = (char *)sqlite3_column_text(statement, 2);
            sha2_512 = (char *)sqlite3_column_text(statement, 3);
            known = (int)sqlite3_column_int(statement, 4);
        }
        sqlite3_finalize(statement);
    } else {
        ; // OK if not exists
    }

    // insert new record
    stmt.str("");
    stmt << "INSERT OR REPLACE INTO file_hashes (file_id, md5, sha1, sha2_256, sha2_512, known) VALUES (" << a_file_id;
    switch (hashType) {
    case TskImgDB::MD5:
        stmt << ", '" << hash << "'";
        stmt << ", '" << sha1 << "'";
        stmt << ", '" << sha2_256 << "'";
        stmt << ", '" << sha2_512 << "'";
        stream << known;
        stmt << ", " << stream.str();
        break;
    case TskImgDB::SHA1:
        stmt << ", '" << md5 << "'";
        stmt << ", '" << hash << "'";
        stmt << ", '" << sha2_256 << "'";
        stmt << ", '" << sha2_512 << "'";
        stream << known;
        stmt << ", " << stream.str();
        break;
    case TskImgDB::SHA2_256:
        stmt << ", '" << md5 << "'";
        stmt << ", '" << sha1 << "'";
        stmt << ", '" << hash << "'";
        stmt << ", '" << sha2_512 << "'";
        stream << known;
        stmt << ", " << stream.str();
        break;
    case TskImgDB::SHA2_512:
        stmt << ", '" << md5 << "'";
        stmt << ", '" << sha1 << "'";
        stmt << ", '" << sha2_256 << "'";
        stmt << ", '" << hash << "'";
        stream << known;
        stmt << ", " << stream.str();
        break;
    }
    stmt << ")";

    char *errmsg;
    if (sqlite3_exec(m_db, stmt.str().c_str(), NULL, NULL, &errmsg) != SQLITE_OK) {
        wchar_t infoMessage[MAX_BUFF_LENGTH];
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::setHash - Error adding hash to file_hashes table: %S", errmsg);
        LOGERROR(infoMessage);
        sqlite3_free(errmsg);
        return 1;
    }

    return 0;
}

std::string TskImgDBSqlite::getCfileName(const uint64_t a_file_id) const
{
    if (!m_db)
        throw TskException("No database.");

    std::string cfileName;
    stringstream stmt;

    stmt << "select 'cfile_' || c.vol_id || '_' || cs.sect_start || '_' || f.file_id"
        " from files f, carved_files c, carved_sectors cs where f.file_id = c.file_id and c.file_id = cs.file_id and cs.seq = 0"
        " and f.file_id = " << a_file_id;

    sqlite3_stmt * statement;
    if (sqlite3_prepare_v2(m_db, stmt.str().c_str(), -1, &statement, 0) == SQLITE_OK) {
        int result = sqlite3_step(statement);
        if (result == SQLITE_ROW) {
            cfileName = (char *)sqlite3_column_text(statement, 0);
        }
        sqlite3_finalize(statement);
    } else {
        wchar_t infoMessage[MAX_BUFF_LENGTH];
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::getCfileName - Error querying tables: %S", sqlite3_errmsg(m_db));
        LOGERROR(infoMessage);
    }

    stmt.str("");
    stmt << "select f.name "
        " from files f, carved_files c, carved_sectors cs where f.file_id = c.file_id and c.file_id = cs.file_id and cs.seq = 0"
        " and f.file_id = " << a_file_id;

    std::string name;
    if (sqlite3_prepare_v2(m_db, stmt.str().c_str(), -1, &statement, 0) == SQLITE_OK) {
        int result = sqlite3_step(statement);
        if (result == SQLITE_ROW) {
            name = (char *)sqlite3_column_text(statement, 0);
        }
        sqlite3_finalize(statement);
        int pos = name.rfind('.');
        if (pos != string::npos)
            cfileName += name.substr(pos);
    } else {
        wchar_t infoMessage[MAX_BUFF_LENGTH];
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::getCfileName - Error querying tables: %S", sqlite3_errmsg(m_db));
        LOGERROR(infoMessage);
    }

    return cfileName;
}

/**
* Return the ImageInfo
* @param type Image Type (output)
* @param sectorSize Image sector size (output)
* @returns 0 on success or -1 on error.
*/
int TskImgDBSqlite::getImageInfo(int & type, int & sectorSize) const
{
    int rc = -1;
    if (!m_db)
        return rc;

    stringstream stmt;

    stmt << "SELECT type, ssize FROM image_info";

    sqlite3_stmt * statement;
    if (sqlite3_prepare_v2(m_db, stmt.str().c_str(), -1, &statement, 0) == SQLITE_OK) {
        int result = sqlite3_step(statement);
        if (result == SQLITE_ROW) {
            type = (int)sqlite3_column_int64(statement, 0);
            sectorSize = (int)sqlite3_column_int64(statement, 1);
            rc = 0;
        }
        sqlite3_finalize(statement);
    } else {
        wchar_t infoMessage[MAX_BUFF_LENGTH];
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::getImageInfo - Error querying image_info table: %S", sqlite3_errmsg(m_db));
        LOGERROR(infoMessage);
        return -1;
    }
    return rc;
}

/**
* Return a list of TskVolumeInfoRecord
* @param volumeInfoList A list of TskVolumeInfoRecord (output)
* @returns 0 on success or -1 on error.
*/
int TskImgDBSqlite::getVolumeInfo(std::list<TskVolumeInfoRecord> & volumeInfoList) const
{
    std::list<TskVolumeInfoRecord> list;

    if (!m_db)
        return -1;

    stringstream stmt;
    stmt << "SELECT vol_id, sect_start, sect_len, description, flags FROM vol_info";

    sqlite3_stmt * statement;
    if (sqlite3_prepare_v2(m_db, stmt.str().c_str(), -1, &statement, 0) == SQLITE_OK) {
        while (sqlite3_step(statement) == SQLITE_ROW) {
            TskVolumeInfoRecord vol_info;
            vol_info.vol_id = sqlite3_column_int(statement,0);
            vol_info.sect_start = sqlite3_column_int64(statement,1);
            vol_info.sect_len = sqlite3_column_int64(statement,2);
            vol_info.description.assign((char *)sqlite3_column_text(statement, 3));
            vol_info.flags = (TSK_VS_PART_FLAG_ENUM)sqlite3_column_int(statement, 4);
            volumeInfoList.push_back(vol_info);
        }
        sqlite3_finalize(statement);
    } else {
        wchar_t infoMessage[MAX_BUFF_LENGTH];
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::getVolumeInfo - Error getting from vol_info table: %S", sqlite3_errmsg(m_db));
        LOGERROR(infoMessage);
        return -1;
    }
    return 0;
}

/**
* Return a list of TskFsInfoRecord
* @param fsInfoList A list of TskFsInfoRecord (output)
* @returns 0 on success or -1 on error.
*/
int TskImgDBSqlite::getFsInfo(std::list<TskFsInfoRecord> & fsInfoList) const
{
    std::list<TskFsInfoRecord> list;

    if (!m_db)
        return -1;

    stringstream stmt;
    stmt << "SELECT fs_id, img_byte_offset, vol_id, fs_type, block_size, block_count, root_inum, first_inum, last_inum FROM fs_info";

    sqlite3_stmt * statement;
    if (sqlite3_prepare_v2(m_db, stmt.str().c_str(), -1, &statement, 0) == SQLITE_OK) {
        while (sqlite3_step(statement) == SQLITE_ROW) {
            TskFsInfoRecord fs_info;
            fs_info.fs_id = sqlite3_column_int(statement,0);
            fs_info.img_byte_offset = sqlite3_column_int64(statement,1);
            fs_info.vol_id = sqlite3_column_int(statement,2);
            fs_info.fs_type = (TSK_FS_TYPE_ENUM)sqlite3_column_int(statement,3);
            fs_info.block_size = sqlite3_column_int(statement,4);
            fs_info.block_count = sqlite3_column_int64(statement,5);
            fs_info.root_inum = sqlite3_column_int64(statement,6);
            fs_info.first_inum = sqlite3_column_int64(statement,7);
            fs_info.last_inum = sqlite3_column_int64(statement,8);
            fsInfoList.push_back(fs_info);
        }
        sqlite3_finalize(statement);
    } else {
        wchar_t infoMessage[MAX_BUFF_LENGTH];
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::getFsInfo - Error getting from fs_info table: %S", sqlite3_errmsg(m_db));
        LOGERROR(infoMessage);
        return -1;
    }
    return 0;
}

typedef std::map<std::string, int> FileTypeMap_t;

static std::string getFileType(const char *name)
{
    std::string filename = name;
    int pos = filename.rfind('.');
    if (pos != std::string::npos) {
        std::string suffix = filename.substr(pos);
        std::string result;
        for (size_t i=0; i < suffix.size(); i++) {
            result += (char)tolower(suffix[i]);
        }
        return result;
    }
    else
        return std::string("");
}

/**
* Return a list of TskFileTypeRecord for all files.
* @param fileTypeInfoList A list of TskFileTypeRecord (output)
* @returns 0 on success or -1 on error.
*/
int TskImgDBSqlite::getFileInfoSummary(std::list<TskFileTypeRecord> &fileTypeInfoList) const
{
    std::stringstream stmt;
    stmt << "SELECT name FROM files WHERE dir_type = " << TSK_FS_NAME_TYPE_REG;

    return getFileTypeRecords(stmt.str(), fileTypeInfoList);
}

/**
* Return a list of TskFileTypeRecord for fileType
* @param fileType FILE_TYPE to report
* @param fileTypeInfoList A list of TskFileTypeRecord (output)
* @returns 0 on success or -1 on error.
*/
int TskImgDBSqlite::getFileInfoSummary(FILE_TYPES fileType, std::list<TskFileTypeRecord> & fileTypeInfoList) const
{
    stringstream stmt;
    stmt << "SELECT name FROM files WHERE type_id = " << fileType << " AND dir_type = " << TSK_FS_NAME_TYPE_REG;

    return getFileTypeRecords(stmt.str(), fileTypeInfoList);
}

/**
* Return a list of TskFileTypeRecords matching the given SQL statement.
* @param stmt The SQL statement used to match file records.
* @param fileTypeInfoList A list of TskFileTypeRecord (output)
* @returns 0 on success of -1 on error.
*/
int TskImgDBSqlite::getFileTypeRecords(std::string& stmt, std::list<TskFileTypeRecord>& fileTypeInfoList) const
{
    if (!m_db)
        return -1;

    std::list<TskFileTypeRecord> list;

    sqlite3_stmt * statement;
    if (sqlite3_prepare_v2(m_db, stmt.c_str(), -1, &statement, 0) == SQLITE_OK) {
        FileTypeMap_t fileTypeMap;
        while (sqlite3_step(statement) == SQLITE_ROW) {
            char *name = (char *)sqlite3_column_text(statement, 0);
            std::string type = getFileType(name);
            FileTypeMap_t::iterator iter = fileTypeMap.find(type);
            if (iter != fileTypeMap.end()) {
                // increment file counter
                int count = iter->second;
                fileTypeMap[type] = ++count;
            } else {
                // add a new file type
                fileTypeMap.insert(pair<std::string, int>(type, 1));
            }
        }
        for (FileTypeMap_t::const_iterator iter=fileTypeMap.begin(); iter != fileTypeMap.end(); iter++) {
            TskFileTypeRecord info;
            info.suffix.assign((*iter).first.c_str());
            info.count = (*iter).second;
            info.description.assign("File Type Description");
            fileTypeInfoList.push_back(info);
        }
        sqlite3_finalize(statement);
    } else {
        wchar_t infoMessage[MAX_BUFF_LENGTH];
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::getFileTypeRecords - Error querying files table: %S", sqlite3_errmsg(m_db));
        LOGERROR(infoMessage);
        return -1;
    }
    return 0;
}

/**
* Insert the Module record, if module name does not already exist in modules table.
* Returns Module Id associated with the Module record.
* @param name Module name
* @param description Module description
* @param moduleId Module Id (output)
* @returns 0 on success, -1 on error.
*/
int TskImgDBSqlite::addModule(const std::string& name, const std::string& description, int & moduleId)
{
    if (!m_db)
        return -1;

    if (name.empty())
    {
        LOGWARN(L"TskImgDBSqlite::addModule - Given an empty module name.");
        return -1;
    }

    moduleId = 0;

    sqlite3_stmt * statement;
    char stmt[1024];
    sqlite3_snprintf(1024, stmt, "SELECT module_id FROM modules WHERE name = '%q';",
                     name.c_str());

    if (sqlite3_prepare_v2(m_db, stmt, -1, &statement, 0) == SQLITE_OK)
    {
        int result = sqlite3_step(statement);
        if (result == SQLITE_ROW)
        {
            // Already exists, return module_id
            moduleId = sqlite3_column_int(statement, 0);
        }
        else
        {
            // Create a new module record.
            char insertStmt[1024];
            char * errmsg;
            sqlite3_snprintf(1024, insertStmt,
                "INSERT INTO modules (module_id, name, description) VALUES (NULL, '%q', '%q');",
                name.c_str(), description.c_str());
            if (sqlite3_exec(m_db, insertStmt, NULL, NULL, &errmsg) == SQLITE_OK)
            {
                moduleId = sqlite3_last_insert_rowid(m_db);
            }
            else
            {
                std::wstringstream msg;
                msg << L"TskImgDBSqlite::addModule - Error adding record to modules table: " << errmsg;
                LOGERROR(msg.str());
                sqlite3_free(errmsg);
            }
        }
        sqlite3_finalize(statement);
    }
    else
    {
        std::wstringstream msg;
        msg << L"TskModule::addModule - Failed to prepare statement: " << stmt;
        LOGERROR(msg.str());
    }
    
    if (moduleId == 0)
        return -1;

    return 0;
}

/**
* Insert the module status record.
* @param file_id file_id
* @param module_id module_id
* @param status Status of module
* @returns 0 on success, -1 on error.
*/
int TskImgDBSqlite::setModuleStatus(uint64_t file_id, int module_id, int status)
{
    int rc = -1;

    if (!m_db)
        return rc;

    char * errmsg;
    std::stringstream stmt;
    stmt << "INSERT INTO module_status (file_id, module_id, status) VALUES (" <<
        file_id << ", " << module_id << ", " << status << ")";

    if (sqlite3_exec(m_db, stmt.str().c_str(), NULL, NULL, &errmsg) == SQLITE_OK) {
        rc = 0;
    } else {
        wchar_t infoMessage[MAX_BUFF_LENGTH];
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::setModuleStatus - Error adding data to module_status table: %S", errmsg);
        LOGERROR(infoMessage);
        sqlite3_free(errmsg);
    }
    return rc;
}

/**
* Get a list of TskModuleStatus.
* @param moduleStatusList A list of TskModuleStatus (output)
* @returns 0 on success, -1 on error.
*/
int TskImgDBSqlite::getModuleErrors(std::vector<TskModuleStatus> & moduleStatusList) const
{
    int rc = -1;

    if (!m_db)
        return rc;

    stringstream stmt;
    stmt << "SELECT f.file_id, m.name, ms.status FROM module_status ms, files f, modules m"
        << " WHERE ms.status != 0 AND ms.file_id = f.file_id AND m.module_id = ms.module_id"
        << " ORDER BY f.file_id";

    sqlite3_stmt * statement;
    if (sqlite3_prepare_v2(m_db, stmt.str().c_str(), -1, &statement, 0) == SQLITE_OK) {
        TskModuleStatus moduleStatus;
        while (sqlite3_step(statement) == SQLITE_ROW) {
            moduleStatus.file_id = (uint64_t)sqlite3_column_int64(statement, 0);
            moduleStatus.module_name = (char *)sqlite3_column_text(statement, 1);
            moduleStatus.status = (int)sqlite3_column_int(statement, 2);
            moduleStatusList.push_back(moduleStatus);
        }
        sqlite3_finalize(statement);
        rc = 0;
    } else {
        wchar_t infoMessage[MAX_BUFF_LENGTH];
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::getModuleErrors - Error querying module_status table: %S", sqlite3_errmsg(m_db));
        LOGERROR(infoMessage);
    }
    // Find report module errors. These have file_id = 0.
    stmt.str("");
    stmt << "SELECT 0, m.name, ms.status FROM module_status ms, modules m"
         << " WHERE ms.status != 0 AND ms.file_id = 0 AND m.module_id = ms.module_id";

    if (sqlite3_prepare_v2(m_db, stmt.str().c_str(), -1, &statement, 0) == SQLITE_OK) {
        TskModuleStatus moduleStatus;
        while (sqlite3_step(statement) == SQLITE_ROW) {
            moduleStatus.file_id = (uint64_t)sqlite3_column_int64(statement, 0);
            moduleStatus.module_name = (char *)sqlite3_column_text(statement, 1);
            moduleStatus.status = (int)sqlite3_column_int(statement, 2);
            moduleStatusList.push_back(moduleStatus);
        }
        sqlite3_finalize(statement);
        rc = 0;
    } else {
        wchar_t infoMessage[MAX_BUFF_LENGTH];
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::getModuleErrors - Error querying module_status table: %S", sqlite3_errmsg(m_db));
        LOGERROR(infoMessage);
    }
    return rc;
}

/*
* Return a file name associated with a file_id, prefer Cfilename, otherwise name in the files table.
* @param file_id file id
* @returns file name as std::string
*/
std::string TskImgDBSqlite::getFileName(uint64_t file_id) const
{
    std::string name;

    if (!m_db)
        return name;

    name = getCfileName(file_id);
    if (name == "") {
        TskFileRecord fileRecord;
        if (getFileRecord(file_id, fileRecord) == 0)
            name = fileRecord.name;
    }
    return name;
}


TskImgDB::KNOWN_STATUS TskImgDBSqlite::getKnownStatus(const uint64_t fileId) const
{
    int retval = -1;

    if (!m_db)
        return (KNOWN_STATUS)retval;
    
    stringstream stmt;
    stmt << "SELECT known FROM file_hashes WHERE file_id = " << fileId;

    sqlite3_stmt * statement;
    if (sqlite3_prepare_v2(m_db, stmt.str().c_str(), -1, &statement, 0) == SQLITE_OK) {
        if(sqlite3_step(statement) == SQLITE_ROW) {
            retval = (int)sqlite3_column_int(statement, 0);
        }
        sqlite3_finalize(statement);
    } else {
        wchar_t infoMessage[MAX_BUFF_LENGTH];
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::getKnownStatus - Error getting known status %S", sqlite3_errmsg(m_db));
        LOGERROR(infoMessage);
    }

    return (KNOWN_STATUS)retval;
}


/**
* Add a new row to the unalloc_img_status table, returning the unalloc_img_id.
* @param unallocImgId unalloc_img_id (output)
* @returns -1 on error, 0 on success.
*/
int TskImgDBSqlite::addUnallocImg(int & unallocImgId)
{
    int rc = -1;

    if (!m_db)
        return rc;

    std::stringstream stmt;
    stmt << "INSERT INTO unalloc_img_status (unalloc_img_id, status) VALUES (NULL, " << TskImgDB::IMGDB_UNALLOC_IMG_STATUS_CREATED << ")";
    char * errmsg;
    if (sqlite3_exec(m_db, stmt.str().c_str(), NULL, NULL, &errmsg) == SQLITE_OK) {
        unallocImgId = sqlite3_last_insert_rowid(m_db);
        rc = 0;
    } else {
        wchar_t infoMessage[MAX_BUFF_LENGTH];
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::addUnallocImg - Error adding unalloc_img_status table: %S", errmsg);
        LOGERROR(infoMessage);
        sqlite3_free(errmsg);
    }
    return rc;
}

/**
* Set the status in the unalloc_img_status table given the unalloc_img_id.
* @param unallocImgId unalloc_img_id
* @param status status of unalloc_img_id
* @returns -1 on error, 0 on success.
*/
int TskImgDBSqlite::setUnallocImgStatus(int unallocImgId, TskImgDB::UNALLOC_IMG_STATUS status)
{
    int rc = -1;

    if (!m_db)
        return rc;

    std::stringstream stmt;
    stmt << "UPDATE unalloc_img_status SET status = " << status << " WHERE unalloc_img_id = " << unallocImgId;
    char * errmsg;
    if (sqlite3_exec(m_db, stmt.str().c_str(), NULL, NULL, &errmsg) == SQLITE_OK) {
        rc = 0;
    } else {
        wchar_t infoMessage[MAX_BUFF_LENGTH];
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::addUnallocImg - Error adding unalloc_img_status table: %S", errmsg);
        LOGERROR(infoMessage);
        sqlite3_free(errmsg);
    }
    return rc;
}

/**
* Get the status of the unalloc_img_status table given the unalloc_img_id.
* Can throws TskException.
* @param unallocImgId unalloc_img_id
* @returns TskImgDB::UNALLOC_IMG_STATUS
*/
TskImgDB::UNALLOC_IMG_STATUS TskImgDBSqlite::getUnallocImgStatus(int unallocImgId) const
{
    if (!m_db)
        throw TskException("Database not initialized.");

    int status;
    stringstream stmt;
    stmt << "SELECT status FROM unalloc_img_status WHERE unalloc_img_id = " << unallocImgId;

    sqlite3_stmt * statement;
    if (sqlite3_prepare_v2(m_db, stmt.str().c_str(), -1, &statement, 0) == SQLITE_OK) {
        if (sqlite3_step(statement) == SQLITE_ROW) {
            status = (int)sqlite3_column_int(statement, 0);
        }
        sqlite3_finalize(statement);
    } else {
        wchar_t infoMessage[MAX_BUFF_LENGTH];
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::getUnallocImgStatus - Error getting unalloc_img_status: %S ", sqlite3_errmsg(m_db));
        LOGERROR(infoMessage);
    }
    return (TskImgDB::UNALLOC_IMG_STATUS)status;
}

/**
* Get all the unalloc_img_status table.
* @param unallocImgStatusList A vector of TskUnallocImgStatusRecord (output)
* @returns -1 on error, 0 on success.
*/
int TskImgDBSqlite::getAllUnallocImgStatus(std::vector<TskUnallocImgStatusRecord> & unallocImgStatusList) const
{
    int rc = -1;
    unallocImgStatusList.clear();

    if (!m_db)
        return rc;

    stringstream stmt;
    stmt << "SELECT unalloc_img_id, status FROM unalloc_img_status";

    sqlite3_stmt * statement;
    if (sqlite3_prepare_v2(m_db, stmt.str().c_str(), -1, &statement, 0) == SQLITE_OK) {
        while (sqlite3_step(statement) == SQLITE_ROW) {
            TskUnallocImgStatusRecord record;
            record.unallocImgId = (int)sqlite3_column_int(statement, 0);
            record.status = (TskImgDB::UNALLOC_IMG_STATUS)sqlite3_column_int(statement, 1);
            unallocImgStatusList.push_back(record);
        }
        rc = 0;
        sqlite3_finalize(statement);
    } else {
        wchar_t infoMessage[MAX_BUFF_LENGTH];
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::getAllUnallocImgStatus - Error getting unalloc_img_status: %S ", sqlite3_errmsg(m_db));
        LOGERROR(infoMessage);
    }
    return rc;
}

/**
* Find and add all the unused sectors (unallocated and uncarved bytes) in the given unallocImgId
* @param unallocImgId The unalloc image id.
* @param unusedSectorsList A vector of TskUnusedSectorsRecord
* @returns -1 on error, 0 on success.
*/
int TskImgDBSqlite::addUnusedSectors(int unallocImgId, std::vector<TskUnusedSectorsRecord> & unusedSectorsList)
{
    assert(unallocImgId > 0);
    int rc = -1;
    if (!m_db)
        return rc;

    std::stringstream stmt;
    stmt << "SELECT vol_id, unalloc_img_sect_start, sect_len, orig_img_sect_start FROM alloc_unalloc_map "
        "WHERE unalloc_img_id = " << unallocImgId << " ORDER BY orig_img_sect_start ASC";

    sqlite3_stmt * statement;
    if (sqlite3_prepare_v2(m_db, stmt.str().c_str(), -1, &statement, 0) == SQLITE_OK) {
        vector<TskAllocUnallocMapRecord> allocUnallocMapList;

        while (sqlite3_step(statement) == SQLITE_ROW) {
            TskAllocUnallocMapRecord record;
            record.vol_id = (int)sqlite3_column_int(statement, 0);
            record.unalloc_img_id = unallocImgId;
            record.unalloc_img_sect_start = (uint64_t)sqlite3_column_int64(statement, 1);
            record.sect_len = (uint64_t)sqlite3_column_int64(statement, 2);
            record.orig_img_sect_start = (uint64_t)sqlite3_column_int64(statement, 3);
            allocUnallocMapList.push_back(record);
        }
        sqlite3_finalize(statement);

        for (std::vector<TskAllocUnallocMapRecord>::const_iterator it = allocUnallocMapList.begin();
             it != allocUnallocMapList.end(); it++)
        {
            // Sector position tracks our position through the unallocated map record.
            uint64_t sectPos = it->orig_img_sect_start;

            uint64_t endSect = it->orig_img_sect_start + it->sect_len;

            // Retrieve all carved_sector records in range for this section of unallocated space.
            stmt.str("");
            stmt << "SELECT cs.sect_start, cs.sect_len FROM carved_files cf, carved_sectors cs"
                << " WHERE cf.file_id = cs.file_id AND cs.sect_start >= " << it->orig_img_sect_start
                << " AND cs.sect_start < " << endSect << " ORDER BY cs.sect_start ASC";

            if (sqlite3_prepare_v2(m_db, stmt.str().c_str(), -1, &statement, 0) == SQLITE_OK) {
                while (sqlite3_step(statement) == SQLITE_ROW) {
                    uint64_t cfileSectStart = (uint64_t)sqlite3_column_int64(statement, 0);
                    uint64_t cfileSectLen = (uint64_t)sqlite3_column_int64(statement, 1);
                    if (cfileSectStart > sectPos)
                    {
                        // We have a block of unused sectors between this position in the unallocated map
                        // and the start of the carved file.
                        addUnusedSector(sectPos, cfileSectStart, it->vol_id, unusedSectorsList);
                    }

                    sectPos = cfileSectStart + cfileSectLen;
                }

                // Handle case where there is slack at the end of the unalloc range
                if (sectPos < endSect)
                    addUnusedSector(sectPos, endSect, it->vol_id, unusedSectorsList);

                sqlite3_finalize(statement);
            } else {
                wchar_t infoMessage[MAX_BUFF_LENGTH];
                _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::addUnusedSectors - Error querying carved_files, carved_sectors table: %S", sqlite3_errmsg(m_db));
                LOGERROR(infoMessage);
            }
        }
    } else {
        wchar_t infoMessage[MAX_BUFF_LENGTH];
        _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::addUnusedSectors - Error querying alloc_unalloc_map table: %S", sqlite3_errmsg(m_db));
        LOGERROR(infoMessage);
    }
    return 0;
}

/**
* Add one unused sector to the database, add it to the files and unused_sectors tables.
* @param sectStart Unused sector start.
* @param sectEnd Unused sector end.
* @param volId Volume Id of the unused sector.
* @param unusedSectorsList A vector of TskUnusedSectorsRecord (output)
* @returns -1 on error, 0 on success.
*/
int TskImgDBSqlite::addUnusedSector(uint64_t sectStart, uint64_t sectEnd, int volId, std::vector<TskUnusedSectorsRecord> & unusedSectorsList)
{
    assert(sectEnd > sectStart);
    int rc = -1;
    unusedSectorsList.clear();
    if (!m_db)
        return rc;

    char *ufilename = "ufile";
    std::stringstream stmt;

#define MIN(a,b) ((a) > (b) ? (b) : (a))
// 50 MB per unused sector
#define MAX_UNUSED_SECTOR_SIZE (50*1000000/512)

    uint64_t sectorIndex = 0;
    uint64_t sectorCount = (sectEnd - sectStart)/MAX_UNUSED_SECTOR_SIZE;

    while (sectorIndex <= sectorCount) {
        uint64_t thisSectStart = sectStart + (sectorIndex * MAX_UNUSED_SECTOR_SIZE);
        uint64_t thisSectEnd = thisSectStart + MIN(MAX_UNUSED_SECTOR_SIZE, sectEnd - thisSectStart);

        stmt.str("");
        stmt << "INSERT INTO files (file_id, type_id, name, par_file_id, dir_type, meta_type,"
            "dir_flags, meta_flags, size, ctime, crtime, atime, mtime, mode, uid, gid, status, full_path) "
            "VALUES (NULL, " << IMGDB_FILES_TYPE_UNUSED << ", " << "'ufile'"
            << ", NULL, " << TSK_FS_NAME_TYPE_REG << ", " << TSK_FS_META_TYPE_REG << ", "
            << TSK_FS_NAME_FLAG_UNALLOC << ", " << TSK_FS_META_FLAG_UNALLOC << ", "
            << (thisSectEnd - thisSectStart) * 512 << ", NULL, NULL, NULL, NULL, NULL, NULL, NULL, " << IMGDB_FILES_STATUS_READY_FOR_ANALYSIS << "," << "'ufile'" << ")";

        if (sqlite3_exec(m_db, stmt.str().c_str(), NULL, NULL, NULL) == SQLITE_OK) {

            TskUnusedSectorsRecord record;

            // get the file_id from the last insert
            record.fileId = sqlite3_last_insert_rowid(m_db);
            record.sectStart = thisSectStart;
            record.sectLen = thisSectEnd - thisSectStart;

            std::stringstream name;
            name << "ufile_" << thisSectStart << "_" << thisSectEnd << "_" << record.fileId;
            stmt.str("");
            char *item;
            item = sqlite3_mprintf("%Q", name.str().c_str());
            stmt << "UPDATE files SET name = " << item << ", full_path = "
                << item << " WHERE file_id = " << record.fileId;
            sqlite3_free(item);

            if (sqlite3_exec(m_db, stmt.str().c_str(), NULL, NULL, NULL) != SQLITE_OK) {
                wchar_t infoMessage[MAX_BUFF_LENGTH];
                _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::addUnusedSector - Error update into files table: %S", sqlite3_errmsg(m_db));
                LOGERROR(infoMessage);
                rc = -1;
                break;
            }

            stmt.str("");
            stmt << "INSERT INTO unused_sectors (file_id, sect_start, sect_len, vol_id) VALUES ("
                 << record.fileId << ", " << record.sectStart << ", " << record.sectLen << ", " << volId << ")";

            if (sqlite3_exec(m_db, stmt.str().c_str(), NULL, NULL, NULL) != SQLITE_OK) {
                wchar_t infoMessage[MAX_BUFF_LENGTH];
                _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::addUnusedSector - Error insert into unused_sectors table: %S", sqlite3_errmsg(m_db));
                LOGERROR(infoMessage);
                rc = -1;
                break;
            }

            unusedSectorsList.push_back(record);
            rc = 0;

        } else {

            wchar_t infoMessage[MAX_BUFF_LENGTH];
            _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::addUnusedSector - Error insert into files table: %S", sqlite3_errmsg(m_db));
            LOGERROR(infoMessage);
            rc = -1;
            break;
        }
        sectorIndex++;
    } // while
    return rc;
}

/**
* Get unused sector record given a file id.
* @param fileId File id of the unused sector.
* @param unusedSectorsRecord TskUnusedSectorsRecord (output)
* @returns -1 on error, 0 on success.
*/
int TskImgDBSqlite::getUnusedSector(uint64_t fileId, TskUnusedSectorsRecord & unusedSectorsRecord) const
{
    int rc = -1;
    if (!m_db)
        return rc;

    std::stringstream stmt;
    stmt << "SELECT sect_start, sect_len FROM unused_sectors WHERE file_id = " << fileId;

    sqlite3_stmt * statement;
    if (sqlite3_prepare_v2(m_db, stmt.str().c_str(), -1, &statement, 0) == SQLITE_OK) {
        if (sqlite3_step(statement) == SQLITE_ROW) {
            unusedSectorsRecord.fileId = fileId;
            unusedSectorsRecord.sectStart = (uint64_t)sqlite3_column_int64(statement, 0);
            unusedSectorsRecord.sectLen = (uint64_t)sqlite3_column_int64(statement, 1);
            rc = 0;
        } else {
            std::wstringstream msg;
            msg << L"TskDBSqlite::getUnusedSector - Error querying unused_sectors table for file_id "
                << fileId ;
            LOGERROR(msg.str());
        }
    } else {
        std::wstringstream msg;
        msg << L"TskDBSqlite::getUnusedSector - Error querying unused_sectors table: "
            << sqlite3_errmsg(m_db) ;
        LOGERROR(msg.str());
    }
    return rc;
}

///BLACKBOARD FUNCTIONS
/**
* Add the given blackboard attribute to the database
* @param attr input attribute. should be fully populated
*/
void TskImgDBSqlite::addBlackboardAttribute(TskBlackboardAttribute attr){
    if (!m_db)
        throw TskException("No database.");

    artifact_t artifactId = 0;
    std::stringstream str;
    char *item;
    sqlite3_stmt * statement;

    str << "INSERT INTO blackboard_attributes (artifact_id, source, context, attribute_type_id, value_type, "
        "value_byte, value_text, value_int32, value_int64, value_double, obj_id) VALUES (";
        str << attr.getArtifactID() << ", ";
    item = sqlite3_mprintf("%Q", attr.getModuleName().c_str()); str << item << ", ";
    sqlite3_free(item);
    item = sqlite3_mprintf("%Q", attr.getContext().c_str()); str << item << ", ";
    sqlite3_free(item);
    str << attr.getAttributeTypeID() << ", ";
    str << attr.getValueType() << ", ";
    switch (attr.getValueType()) {
        case TSK_BYTE:
            str << " ?, '', 0, 0, 0.0";
            break;
        case TSK_STRING:
            item = sqlite3_mprintf("%Q", attr.getValueString().c_str());
            str << " '', " << item << ", 0, 0, 0.0";
            sqlite3_free(item);
            break;
        case TSK_INTEGER:
            str << " '', '', " << attr.getValueInt() << ", 0, 0.0";
            break;
        case TSK_LONG:
            str << " '', '', 0, " << attr.getValueLong() << ", 0.0";
            break;
        case TSK_DOUBLE:
            str << " '', '', 0, 0, " << setprecision(20) << attr.getValueDouble();
            break;
    };
    str << ", " << attr.getObjectID();
    str << ")";

    if (sqlite3_prepare_v2(m_db, str.str().c_str(), -1, &statement, 0) == SQLITE_OK) {
        int result = SQLITE_OK;
        unsigned char *pBuf = 0;
        if (attr.getValueType() == TSK_BYTE) {
            // Bind the byte vector
            int a_size = attr.getValueBytes().size();
            pBuf = new unsigned char[a_size];
            for (int i = 0; i < a_size; i++) {
                pBuf[i] = attr.getValueBytes()[i];
            }
            result = sqlite3_bind_blob(statement, 1, pBuf, a_size, SQLITE_STATIC);
        }
        if (result == SQLITE_OK) {
            result = sqlite3_step(statement);
            if (!(result == SQLITE_ROW || result == SQLITE_DONE)) {
                sqlite3_finalize(statement);
                if (pBuf) delete [] pBuf;
                throw TskException("TskImgDBSqlite::addBlackboardAttribute - Insert failed");
            }
        } else {
            std::wstringstream infoMessage;
            infoMessage << L"TskImgDBSqlite::addBlackboardAttribute - Error in sqlite3_bind_blob: " << sqlite3_errmsg(m_db);
            LOGERROR(infoMessage.str());
            throw TskException("TskImgDBSqlite::addBlackboardAttribute - Insert failed");
        }
        sqlite3_finalize(statement);
        if (pBuf) delete [] pBuf;
    } else {
        std::wstringstream infoMessage;
        infoMessage << L"TskImgDBSqlite::addBlackboardAttribute - Error adding data to blackboard table: " << sqlite3_errmsg(m_db);
        LOGERROR(infoMessage.str());
        throw TskException("TskImgDBSqlite::addBlackboardAttribute - Insert failed");
    }
}

/**
* Get the display name for the given artifact type id
* @param artifactTypeID artifact type id
* @returns display name
*/
string TskImgDBSqlite::getArtifactTypeDisplayName(int artifactTypeID){
    if (!m_db)
        throw TskException("No database.");

    std::stringstream str;
    sqlite3_stmt * statement;
    std::string displayName = "";

    str << "SELECT display_name FROM blackboard_artifact_types WHERE artifact_type_id = " << artifactTypeID;

    if (sqlite3_prepare_v2(m_db, str.str().c_str(), -1, &statement, 0) == SQLITE_OK){
        int result = sqlite3_step(statement);
        if (result == SQLITE_ROW) {
            displayName = (char *)sqlite3_column_text(statement, 0);
        }
        else{
            std::wstringstream infoMessage;
            infoMessage << L"TskImgDBSqlite::getArtifactTypeDisplayName: " << sqlite3_errmsg(m_db);
            LOGERROR(infoMessage.str());
            throw TskException("TskImgDBSqlite::getArtifactTypeDisplayName - No artifact type with that ID");
        }
        sqlite3_finalize(statement);
        return displayName;
    }
    else{
        std::wstringstream infoMessage;
        infoMessage << L"TskImgDBSqlite::getArtifactTypeDisplayName: " << sqlite3_errmsg(m_db);
        LOGERROR(infoMessage.str());
        throw TskException("TskImgDBSqlite::getArtifactTypeDisplayName - Select failed");
    }
}

/**
* Get the artifact type id for the given artifact type string
* @param artifactTypeString display name
* @returns artifact type id
*/
int TskImgDBSqlite::getArtifactTypeID(string artifactTypeString){
    if (!m_db)
        throw TskException("No database.");

    std::stringstream str;
    sqlite3_stmt * statement;
    int typeID;

    str << "SELECT artifact_type_id FROM blackboard_artifact_types WHERE type_name = " << artifactTypeString;

    if (sqlite3_prepare_v2(m_db, str.str().c_str(), -1, &statement, 0) == SQLITE_OK){
        int result = sqlite3_step(statement);
        if (result == SQLITE_ROW) {
            typeID = (int) sqlite3_column_int(statement, 0);
        }
        else{
            std::wstringstream infoMessage;
            infoMessage << L"TskImgDBSqlite::getArtifactTypeID: " << sqlite3_errmsg(m_db);
            LOGERROR(infoMessage.str());
            throw TskException("TskImgDBSqlite::getArtifactTypeID - No artifact type with that name");
        }
        sqlite3_finalize(statement);
        return typeID;
    }
    else{
        std::wstringstream infoMessage;
        infoMessage << L"TskImgDBSqlite::getArtifactTypeID: " << sqlite3_errmsg(m_db);
        LOGERROR(infoMessage.str());
        throw TskException("TskImgDBSqlite::getArtifactTypeID - Select failed");
    }
}

/**
* Get the artifact type name for the given artifact type id
* @param artifactTypeID id
* @returns artifact type name
*/
string TskImgDBSqlite::getArtifactTypeName(int artifactTypeID){
    if (!m_db)
        throw TskException("No database.");

    std::stringstream str;
    sqlite3_stmt * statement;
    std::string typeName = "";

    str << "SELECT type_name FROM blackboard_artifact_types WHERE artifact_type_id = " << artifactTypeID;

    if (sqlite3_prepare_v2(m_db, str.str().c_str(), -1, &statement, 0) == SQLITE_OK){
        int result = sqlite3_step(statement);
        if (result == SQLITE_ROW) {
            typeName = (char *)sqlite3_column_text(statement, 0);
        }
        else{
            std::wstringstream infoMessage;
            infoMessage << L"TskImgDBSqlite::getArtifactTypeName: " << sqlite3_errmsg(m_db);
            LOGERROR(infoMessage.str());
            throw TskException("TskImgDBSqlite::getArtifactTypeName - No artifact type with that ID");
        }
        sqlite3_finalize(statement);
        return typeName;
    }
    else{
        std::wstringstream infoMessage;
        infoMessage << L"TskImgDBSqlite::getArtifactTypeName: " << sqlite3_errmsg(m_db);
        LOGERROR(infoMessage.str());
        throw TskException("TskImgDBSqlite::getArtifactTypeName - Select failed");
    }
}

/**
* Get the display name for the given attribute type id
* @param attributeTypeID attribute type id
* @returns display name
*/
string TskImgDBSqlite::getAttributeTypeDisplayName(int attributeTypeID){
    if (!m_db)
        throw TskException("No database.");

    std::stringstream str;
    sqlite3_stmt * statement;
    std::string displayName = "";

    str << "SELECT display_name FROM blackboard_attribute_types WHERE attribute_type_id = " << attributeTypeID;

    if (sqlite3_prepare_v2(m_db, str.str().c_str(), -1, &statement, 0) == SQLITE_OK){
        int result = sqlite3_step(statement);
        if (result == SQLITE_ROW) {
            displayName = (char *)sqlite3_column_text(statement, 0);
        }
        else{
            std::wstringstream infoMessage;
            infoMessage << L"TskImgDBSqlite::getAttributeTypeDisplayName: " << sqlite3_errmsg(m_db);
            LOGERROR(infoMessage.str());
            throw TskException("TskImgDBSqlite::getAttributeTypeDisplayName - No attribute type with that ID");
        }
        sqlite3_finalize(statement);
        return displayName;
    }
    else{
        std::wstringstream infoMessage;
        infoMessage << L"TskImgDBSqlite::getAttributeTypeDisplayName: " << sqlite3_errmsg(m_db);
        LOGERROR(infoMessage.str());
        throw TskException("TskImgDBSqlite::getAttributeTypeDisplayName - Select failed");
    }
}

/**
* Get the attribute type id for the given artifact type string
* @param attributeTypeString display name
* @returns attribute type id
*/
int TskImgDBSqlite::getAttributeTypeID(string attributeTypeString){
    if (!m_db)
        throw TskException("No database.");

    std::stringstream str;
    sqlite3_stmt * statement;
    int typeID;

    str << "SELECT attribute_type_id FROM blackboard_attribute_types WHERE type_name = " << attributeTypeString;

    if (sqlite3_prepare_v2(m_db, str.str().c_str(), -1, &statement, 0) == SQLITE_OK){
        int result = sqlite3_step(statement);
        if (result == SQLITE_ROW) {
            typeID = (int) sqlite3_column_int(statement, 0);
        }
        else{
            std::wstringstream infoMessage;
            infoMessage << L"TskImgDBSqlite::getAttributeTypeID: " << sqlite3_errmsg(m_db);
            LOGERROR(infoMessage.str());
            throw TskException("TskImgDBSqlite::getAttributeTypeID - No artifact type with that name");
        }
        sqlite3_finalize(statement);
        return typeID;
    }
    else{
        std::wstringstream infoMessage;
        infoMessage << L"TskImgDBSqlite::getAttributeTypeID: " << sqlite3_errmsg(m_db);
        LOGERROR(infoMessage.str());
        throw TskException("TskImgDBSqlite::getAttributeTypeID - Select failed");
    }
}

/**
* Get the attribute type name for the given artifact type id
* @param attributeTypeID id
* @returns attribute type name
*/
string TskImgDBSqlite::getAttributeTypeName(int attributeTypeID){
    if (!m_db)
        throw TskException("No database.");

    std::stringstream str;
    sqlite3_stmt * statement;
    std::string typeName = "";

    str << "SELECT type_name FROM blackboard_attribute_types WHERE attribute_type_id = " << attributeTypeID;

    if (sqlite3_prepare_v2(m_db, str.str().c_str(), -1, &statement, 0) == SQLITE_OK){
        int result = sqlite3_step(statement);
        if (result == SQLITE_ROW) {
            typeName = (char *)sqlite3_column_text(statement, 0);
        }
        else{
            std::wstringstream infoMessage;
            infoMessage << L"TskImgDBSqlite::getAttributeTypeName: " << sqlite3_errmsg(m_db);
            LOGERROR(infoMessage.str());
            throw TskException("TskImgDBSqlite::getAttributeTypeName - No attribute type with that ID");
        }
        sqlite3_finalize(statement);
        return typeName;
    }
    else{
        std::wstringstream infoMessage;
        infoMessage << L"TskImgDBSqlite::getAttributeTypeName: " << sqlite3_errmsg(m_db);
        LOGERROR(infoMessage.str());
        throw TskException("TskImgDBSqlite::getAttributeTypeName - Select failed");
    }
}

/**
* Get all artifacts by performing a SQL Select statement with the given where clause.
* @param condition The SQL select where clause that should be used in the query.
* @returns vector of matching artifacts
*/
vector<TskBlackboardArtifact> TskImgDBSqlite::getMatchingArtifacts(string condition){
    if (!m_db)
        throw TskException("No database.");
    
    int result = 0;
    vector<TskBlackboardArtifact> artifacts;
    std::string stmt("SELECT blackboard_artifacts.artifact_id, blackboard_artifacts.obj_id, blackboard_artifacts.artifact_type_id FROM blackboard_artifacts");

    constructStmt(stmt, condition);

    sqlite3_stmt * statement;
    if (sqlite3_prepare_v2(m_db, stmt.c_str(), -1, &statement, 0) == SQLITE_OK)
    {
        while (sqlite3_step(statement) == SQLITE_ROW)
        {
            int artifactTypeID = sqlite3_column_int(statement, 2);

            artifacts.push_back(TskImgDB::createArtifact(sqlite3_column_int64(statement, 0), sqlite3_column_int64(statement, 1), artifactTypeID));
        }
        sqlite3_finalize(statement);
    } else
    {
        std::wstringstream msg;
        msg << L"TskImgDBSqlite::getMatchingArtifacts - Error getting artifacts: " << sqlite3_errmsg(m_db);
        LOGERROR(msg.str());
        throw TskException("TskImgDBSqlite::getMatchingArtifacts - Select failed");
    }
    return artifacts;
}

/**
* Get all attributes with that match the given where clause
* @param condition where clause to use for matching
* @returns vector of matching attributes
*/
vector<TskBlackboardAttribute> TskImgDBSqlite::getMatchingAttributes(string condition){
    if (!m_db)
        throw TskException("No database.");
    
    int result = 0;
    vector<TskBlackboardAttribute> attributes;
    std::string stmt("SELECT blackboard_attributes.artifact_id, blackboard_attributes.source, blackboard_attributes.context, blackboard_attributes.attribute_type_id, blackboard_attributes.value_type, blackboard_attributes.value_byte, blackboard_attributes.value_text, blackboard_attributes.value_int32, blackboard_attributes.value_int64, blackboard_attributes.value_double, blackboard_attributes.obj_id FROM blackboard_attributes ");

    constructStmt(stmt, condition);

    sqlite3_stmt * statement;
    if (sqlite3_prepare_v2(m_db, stmt.c_str(), -1, &statement, 0) == SQLITE_OK)
    {
        while (sqlite3_step(statement) == SQLITE_ROW)
        {
            int blobSize = sqlite3_column_bytes(statement, 6);
            const unsigned char *pBlob = (const unsigned char *)sqlite3_column_blob(statement, 6);
            vector<unsigned char> bytes;
            bytes.reserve(blobSize);
            for (int i = 0; i < blobSize; i++) {
                bytes.push_back((unsigned char)pBlob[i]);
            }

            attributes.push_back(TskImgDB::createAttribute(sqlite3_column_int64(statement, 0),sqlite3_column_int(statement, 3), sqlite3_column_int64(statement, 10), std::string((char *)sqlite3_column_text(statement, 1)),
                std::string((char *)sqlite3_column_text(statement, 2)), (TSK_BLACKBOARD_ATTRIBUTE_VALUE_TYPE) sqlite3_column_int(statement, 4), sqlite3_column_int(statement, 7),
                sqlite3_column_int64(statement, 8), sqlite3_column_double(statement, 9), std::string((char *)sqlite3_column_text(statement, 6)), bytes));
        }
        sqlite3_finalize(statement);
    } else
    {
        std::wstringstream msg;
        msg << L"TskImgDBSqlite::getMatchingAttributes - Error getting attributes: " << sqlite3_errmsg(m_db);
        LOGERROR(msg.str());
        throw TskException("TskImgDBSqlite::getMatchingAttributes - Select failed");
    }
    return attributes;
}

/**
* Create a new blackboard artifact with the given type id and file id
* @param artifactTypeID artifact type id
* @param file_id associated file id
* @returns the new artifact
*/
TskBlackboardArtifact TskImgDBSqlite::createBlackboardArtifact(uint64_t file_id, int artifactTypeID){
    if (!m_db)
        throw TskException("No database.");

    uint64_t artifactId = 0;
    std::stringstream str;
    sqlite3_stmt * statement;

    str << "INSERT INTO blackboard_artifacts (artifact_id, obj_id, artifact_type_id) VALUES (NULL, " << file_id << ", " << artifactTypeID << ")";

    if (sqlite3_prepare_v2(m_db, str.str().c_str(), -1, &statement, 0) == SQLITE_OK) {
        if(!(sqlite3_step(statement) == SQLITE_DONE)){
            sqlite3_finalize(statement);
            throw TskException("TskImgDBSqlite::addBlackboardInfo - Insert failed");
        }
        // select max(artifact_id) from blackboard
        str.str("");
        str << "SELECT artifact_id from blackboard_artifacts WHERE obj_id = " << file_id << " AND artifact_type_id = " << artifactTypeID;
        sqlite3_finalize(statement);
        if (sqlite3_prepare_v2(m_db, str.str().c_str(), -1, &statement, 0) == SQLITE_OK) {
            while(sqlite3_step(statement) == SQLITE_ROW) {
                uint64_t newID = sqlite3_column_int64(statement, 0);
                if(newID > artifactId)
                    artifactId = newID;
            }
        } else {
            sqlite3_finalize(statement);
            throw TskException("TskImgDBSqlite::newBlackboardArtifact - Select artifact_id failed");
        }
        sqlite3_finalize(statement);
    } else {
        std::wstringstream infoMessage;
        infoMessage << L"TskImgDBSqlite::newBlackboardArtifact - Error adding new artifact: " << sqlite3_errmsg(m_db);
        LOGERROR(infoMessage.str());
        throw TskException("TskImgDBSqlite::newBlackboardArtifact - Insert failed");
    }

    return TskImgDB::createArtifact(artifactId, file_id, artifactTypeID);
}

/**
* Add a new artifact type with the given name, display name and id
* @param artifactTypeName type name
* @param displayName display name
* @param typeID type id
*/
void TskImgDBSqlite::addArtifactType(int typeID, string artifactTypeName, string displayName){
    if (!m_db)
        throw TskException("No database.");

    std::stringstream str;
    sqlite3_stmt * statement;

    str << "SELECT * FROM blackboard_artifact_types WHERE type_name = '" << artifactTypeName << "'";

    if (sqlite3_prepare_v2(m_db, str.str().c_str(), -1, &statement, 0) == SQLITE_OK) {
        if(!(sqlite3_step(statement) == SQLITE_ROW)){
            sqlite3_finalize(statement);
            str.str("");
            str << "INSERT INTO blackboard_artifact_types (artifact_type_id, type_name, display_name) VALUES (" << typeID << " , '" << artifactTypeName << "', '" << displayName << "')";
            if (sqlite3_prepare_v2(m_db, str.str().c_str(), -1, &statement, 0) == SQLITE_OK) {
                if(!(sqlite3_step(statement) == SQLITE_DONE)){
                    sqlite3_finalize(statement);
                    std::wstringstream infoMessage;
                    infoMessage << L"TskImgDBSqlite::addArtifactType - Error adding data to blackboard table: " << sqlite3_errmsg(m_db);
                    LOGERROR(infoMessage.str());
                    throw TskException("TskImgDBSqlite::addArtifactType - Artifact type insert failed");
                }
            }
        }
        else{
            sqlite3_finalize(statement);
            throw TskException("TskImgDBSqlite::addArtifactType - Artifact type with that name already exists");
        }
        sqlite3_finalize(statement);
    } else {
        std::wstringstream infoMessage;
        infoMessage << L"TskImgDBSqlite::addArtifactType - Error adding data to blackboard table: " << sqlite3_errmsg(m_db);
        LOGERROR(infoMessage.str());
        throw TskException("TskImgDBSqlite::addArtifactType - Insert failed");
    }
}

/**
* Add a new attribute type with the given name, display name and id
* @param attributeTypeName type name
* @param displayName display name
* @param typeID type id
*/
void TskImgDBSqlite::addAttributeType(int typeID, string attributeTypeName, string displayName){
    if (!m_db)
        throw TskException("No database.");

    std::stringstream str;
    sqlite3_stmt * statement;

    str << "SELECT * FROM blackboard_attribute_types WHERE type_name = '" << attributeTypeName << "'";

    if (sqlite3_prepare_v2(m_db, str.str().c_str(), -1, &statement, 0) == SQLITE_OK) {
        if(!(sqlite3_step(statement) == SQLITE_ROW)){
            sqlite3_finalize(statement);
            str.str("");
            str << "INSERT INTO blackboard_attribute_types (attribute_type_id, type_name, display_name) VALUES (" << typeID << " , '" << attributeTypeName << "', '" << displayName << "')";
            if (sqlite3_prepare_v2(m_db, str.str().c_str(), -1, &statement, 0) == SQLITE_OK) {
                if(!(sqlite3_step(statement) == SQLITE_DONE)){
                    sqlite3_finalize(statement);
                    wchar_t infoMessage[MAX_BUFF_LENGTH];
                    _snwprintf_s(infoMessage, MAX_BUFF_LENGTH, L"TskImgDBSqlite::addAttributeType - Error adding data to blackboard table: %S", sqlite3_errmsg(m_db));
                    LOGERROR(infoMessage);
                    throw TskException("TskImgDBSqlite::addAttributeType - Attribute type insert failed");
                }
            }

        }
        else{
            sqlite3_finalize(statement);
            throw TskException("TskImgDBSqlite::addAttributeType - Attribute type with that name already exists");
        }
        sqlite3_finalize(statement);
    } else {
        std::wstringstream infoMessage;
        infoMessage << L"TskImgDBSqlite::addAttributeType - Error adding data to blackboard table: " << sqlite3_errmsg(m_db);
        LOGERROR(infoMessage.str());
        throw TskException("TskImgDBSqlite::addAttributeType - Insert failed");
    }
}

/**
* Get all artifacts with the given type id, type name, and file id
* @param artifactTypeID type id
* @param artifactTypeName type name
* @param file_id file id
*/
vector<TskBlackboardArtifact> TskImgDBSqlite::getArtifactsHelper(uint64_t file_id, int artifactTypeID, string artifactTypeName){
    if (!m_db)
        throw TskException("No database.");
    
    int result = 0;
    vector<TskBlackboardArtifact> artifacts;
    std::stringstream stmt;
    stmt << "SELECT artifact_id, obj_id, artifact_type_id FROM blackboard_artifacts WHERE obj_id = " << file_id << " AND artifact_type_id = " << artifactTypeID;

    sqlite3_stmt * statement;
    if (sqlite3_prepare_v2(m_db, stmt.str().c_str(), -1, &statement, 0) == SQLITE_OK)
    {
        while (sqlite3_step(statement) == SQLITE_ROW)
        {
            int artifactTypeID = sqlite3_column_int(statement, 2);

            artifacts.push_back(TskImgDB::createArtifact(sqlite3_column_int64(statement, 0), file_id, artifactTypeID));
        }
        sqlite3_finalize(statement);
    } else
    {
        std::wstringstream msg;
        msg << L"TskImgDBSqlite::getArtifactsHelper - Error getting artifacts: " << sqlite3_errmsg(m_db);
        LOGERROR(msg.str());
        throw TskException("TskImgDBSqlite::getArtifactsHelper - Select failed");
    }
    return artifacts;
}

vector<int> TskImgDBSqlite::findAttributeTypes(int artifactTypeId){
    if(!m_db){
        throw TskException("No database.");
    }
    int result = 0;
    vector<int> attrTypes;
    std::stringstream stmt;
    stmt << "SELECT DISTINCT(attribute_type_id) FROM blackboard_attributes JOIN blackboard_artifacts ON blackboard_attributes.artifact_id = blackboard_artifacts.artifact_id WHERE artifact_type_id = " << artifactTypeId;

    sqlite3_stmt * statement;
    if (sqlite3_prepare_v2(m_db, stmt.str().c_str(), -1, &statement, 0) == SQLITE_OK)
    {
        while (sqlite3_step(statement) == SQLITE_ROW)
        {
            int artifactTypeID = sqlite3_column_int(statement, 0);

            attrTypes.push_back(artifactTypeID);
        }
        sqlite3_finalize(statement);
    } else
    {
        std::wstringstream msg;
        msg << L"TskImgDBSqlite::findAttributeTypes - Error finding attribute types: " << sqlite3_errmsg(m_db);
        LOGERROR(msg.str());
        throw TskException("TskImgDBSqlite::findAttributeTypes - Select failed");
    }
    return attrTypes;
}

std::string TskImgDBSqlite::quote(const std::string str) const
{
    char *item = sqlite3_mprintf("%Q", str.c_str());
std::string returnStr(item);
    sqlite3_free(item);
return returnStr;
}
Something went wrong with that request. Please try again.